blob: b1ad836073786c903dae631c6bccee685cf2ce08 [file] [log] [blame]
aurel32f652e6a2008-12-16 10:43:48 +00001/*
2 * Functions to help device tree manipulation using libfdt.
3 * It also provides functions to read entries from device tree proc
4 * interface.
5 *
6 * Copyright 2008 IBM Corporation.
7 * Authors: Jerone Young <jyoung5@us.ibm.com>
8 * Hollis Blanchard <hollisb@us.ibm.com>
9 *
10 * This work is licensed under the GNU GPL license version 2 or later.
11 *
12 */
13
Peter Maydelld38ea872016-01-29 17:50:05 +000014#include "qemu/osdep.h"
aurel32f652e6a2008-12-16 10:43:48 +000015
aurel32f652e6a2008-12-16 10:43:48 +000016#include "qemu-common.h"
Li Liudb013f82014-08-26 14:38:02 +080017#include "qemu/error-report.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010018#include "sysemu/device_tree.h"
Markus Armbruster2ff3de62013-07-04 15:09:22 +020019#include "sysemu/sysemu.h"
Blue Swirl39b7f202009-09-22 17:51:36 +000020#include "hw/loader.h"
Marcel Apfelbaum6cabe7f2015-02-04 17:43:53 +020021#include "hw/boards.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010022#include "qemu/config-file.h"
aurel32f652e6a2008-12-16 10:43:48 +000023
24#include <libfdt.h>
25
Alexander Grafce362522012-05-17 15:33:54 +020026#define FDT_MAX_SIZE 0x10000
27
28void *create_device_tree(int *sizep)
29{
30 void *fdt;
31 int ret;
32
33 *sizep = FDT_MAX_SIZE;
34 fdt = g_malloc0(FDT_MAX_SIZE);
35 ret = fdt_create(fdt, FDT_MAX_SIZE);
36 if (ret < 0) {
37 goto fail;
38 }
Peter Maydellef6de702013-11-22 17:17:09 +000039 ret = fdt_finish_reservemap(fdt);
40 if (ret < 0) {
41 goto fail;
42 }
Alexander Grafce362522012-05-17 15:33:54 +020043 ret = fdt_begin_node(fdt, "");
44 if (ret < 0) {
45 goto fail;
46 }
47 ret = fdt_end_node(fdt);
48 if (ret < 0) {
49 goto fail;
50 }
51 ret = fdt_finish(fdt);
52 if (ret < 0) {
53 goto fail;
54 }
55 ret = fdt_open_into(fdt, fdt, *sizep);
56 if (ret) {
Li Liu508e2212014-08-26 14:38:03 +080057 error_report("Unable to copy device tree in memory");
Alexander Grafce362522012-05-17 15:33:54 +020058 exit(1);
59 }
60
61 return fdt;
62fail:
Li Liu508e2212014-08-26 14:38:03 +080063 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
Alexander Grafce362522012-05-17 15:33:54 +020064 exit(1);
65}
66
pbrook7ec632b2009-04-10 16:23:59 +000067void *load_device_tree(const char *filename_path, int *sizep)
aurel32f652e6a2008-12-16 10:43:48 +000068{
pbrook7ec632b2009-04-10 16:23:59 +000069 int dt_size;
aurel32f652e6a2008-12-16 10:43:48 +000070 int dt_file_load_size;
aurel32f652e6a2008-12-16 10:43:48 +000071 int ret;
pbrook7ec632b2009-04-10 16:23:59 +000072 void *fdt = NULL;
aurel32f652e6a2008-12-16 10:43:48 +000073
pbrook7ec632b2009-04-10 16:23:59 +000074 *sizep = 0;
75 dt_size = get_image_size(filename_path);
76 if (dt_size < 0) {
Li Liudb013f82014-08-26 14:38:02 +080077 error_report("Unable to get size of device tree file '%s'",
78 filename_path);
aurel32f652e6a2008-12-16 10:43:48 +000079 goto fail;
80 }
81
pbrook7ec632b2009-04-10 16:23:59 +000082 /* Expand to 2x size to give enough room for manipulation. */
Alexander Grafded57c52011-07-23 10:54:11 +020083 dt_size += 10000;
pbrook7ec632b2009-04-10 16:23:59 +000084 dt_size *= 2;
aurel32f652e6a2008-12-16 10:43:48 +000085 /* First allocate space in qemu for device tree */
Anthony Liguori7267c092011-08-20 22:09:37 -050086 fdt = g_malloc0(dt_size);
aurel32f652e6a2008-12-16 10:43:48 +000087
pbrook7ec632b2009-04-10 16:23:59 +000088 dt_file_load_size = load_image(filename_path, fdt);
89 if (dt_file_load_size < 0) {
Li Liudb013f82014-08-26 14:38:02 +080090 error_report("Unable to open device tree file '%s'",
91 filename_path);
pbrook7ec632b2009-04-10 16:23:59 +000092 goto fail;
93 }
aurel32f652e6a2008-12-16 10:43:48 +000094
pbrook7ec632b2009-04-10 16:23:59 +000095 ret = fdt_open_into(fdt, fdt, dt_size);
aurel32f652e6a2008-12-16 10:43:48 +000096 if (ret) {
Li Liudb013f82014-08-26 14:38:02 +080097 error_report("Unable to copy device tree in memory");
aurel32f652e6a2008-12-16 10:43:48 +000098 goto fail;
99 }
100
101 /* Check sanity of device tree */
102 if (fdt_check_header(fdt)) {
Li Liudb013f82014-08-26 14:38:02 +0800103 error_report("Device tree file loaded into memory is invalid: %s",
104 filename_path);
aurel32f652e6a2008-12-16 10:43:48 +0000105 goto fail;
106 }
pbrook7ec632b2009-04-10 16:23:59 +0000107 *sizep = dt_size;
aurel32f652e6a2008-12-16 10:43:48 +0000108 return fdt;
109
110fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500111 g_free(fdt);
aurel32f652e6a2008-12-16 10:43:48 +0000112 return NULL;
113}
114
Alexander Grafccbcfed2011-07-23 10:52:00 +0200115static int findnode_nofail(void *fdt, const char *node_path)
aurel32f652e6a2008-12-16 10:43:48 +0000116{
117 int offset;
118
119 offset = fdt_path_offset(fdt, node_path);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200120 if (offset < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800121 error_report("%s Couldn't find node %s: %s", __func__, node_path,
122 fdt_strerror(offset));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200123 exit(1);
124 }
aurel32f652e6a2008-12-16 10:43:48 +0000125
Alexander Grafccbcfed2011-07-23 10:52:00 +0200126 return offset;
127}
128
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000129int qemu_fdt_setprop(void *fdt, const char *node_path,
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000130 const char *property, const void *val, int size)
Alexander Grafccbcfed2011-07-23 10:52:00 +0200131{
132 int r;
133
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000134 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200135 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800136 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
137 property, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200138 exit(1);
139 }
140
141 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000142}
143
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000144int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
145 const char *property, uint32_t val)
aurel32f652e6a2008-12-16 10:43:48 +0000146{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200147 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000148
Alexander Grafccbcfed2011-07-23 10:52:00 +0200149 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
150 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800151 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
152 node_path, property, val, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200153 exit(1);
154 }
aurel32f652e6a2008-12-16 10:43:48 +0000155
Alexander Grafccbcfed2011-07-23 10:52:00 +0200156 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000157}
158
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000159int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
160 const char *property, uint64_t val)
Alexander Grafbb28eb32012-05-18 01:53:01 +0200161{
162 val = cpu_to_be64(val);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000163 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
Alexander Grafbb28eb32012-05-18 01:53:01 +0200164}
165
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000166int qemu_fdt_setprop_string(void *fdt, const char *node_path,
167 const char *property, const char *string)
aurel32f652e6a2008-12-16 10:43:48 +0000168{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200169 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000170
Alexander Grafccbcfed2011-07-23 10:52:00 +0200171 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
172 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800173 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
174 node_path, property, string, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200175 exit(1);
176 }
aurel32f652e6a2008-12-16 10:43:48 +0000177
Alexander Grafccbcfed2011-07-23 10:52:00 +0200178 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000179}
Alexander Grafd69a8e62011-07-21 01:52:57 +0200180
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000181const void *qemu_fdt_getprop(void *fdt, const char *node_path,
182 const char *property, int *lenp)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100183{
184 int len;
185 const void *r;
186 if (!lenp) {
187 lenp = &len;
188 }
189 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
190 if (!r) {
Li Liu508e2212014-08-26 14:38:03 +0800191 error_report("%s: Couldn't get %s/%s: %s", __func__,
192 node_path, property, fdt_strerror(*lenp));
Peter Maydellf0aa7132012-07-20 13:34:50 +0100193 exit(1);
194 }
195 return r;
196}
197
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000198uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
199 const char *property)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100200{
201 int len;
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000202 const uint32_t *p = qemu_fdt_getprop(fdt, node_path, property, &len);
Peter Maydellf0aa7132012-07-20 13:34:50 +0100203 if (len != 4) {
Li Liu508e2212014-08-26 14:38:03 +0800204 error_report("%s: %s/%s not 4 bytes long (not a cell?)",
205 __func__, node_path, property);
Peter Maydellf0aa7132012-07-20 13:34:50 +0100206 exit(1);
207 }
208 return be32_to_cpu(*p);
209}
210
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000211uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
Alexander Graf7d5fd102012-05-17 15:23:39 +0200212{
213 uint32_t r;
214
215 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
Stefan Weil909a1962013-06-10 22:12:25 +0200216 if (r == 0) {
Li Liu508e2212014-08-26 14:38:03 +0800217 error_report("%s: Couldn't get phandle for %s: %s", __func__,
218 path, fdt_strerror(r));
Alexander Graf7d5fd102012-05-17 15:23:39 +0200219 exit(1);
220 }
221
222 return r;
223}
224
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000225int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
226 const char *property,
227 const char *target_node_path)
Alexander Graf8535ab12012-05-17 14:11:52 +0200228{
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000229 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
230 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
Alexander Graf8535ab12012-05-17 14:11:52 +0200231}
232
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000233uint32_t qemu_fdt_alloc_phandle(void *fdt)
Alexander Graf3601b572012-05-17 16:58:55 +0200234{
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200235 static int phandle = 0x0;
236
237 /*
238 * We need to find out if the user gave us special instruction at
Kamalesh Babulalcc47a162015-07-24 13:48:13 +0530239 * which phandle id to start allocating phandles.
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200240 */
241 if (!phandle) {
Marcel Apfelbaum6cabe7f2015-02-04 17:43:53 +0200242 phandle = machine_phandle_start(current_machine);
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200243 }
244
245 if (!phandle) {
246 /*
247 * None or invalid phandle given on the command line, so fall back to
248 * default starting point.
249 */
250 phandle = 0x8000;
251 }
Alexander Graf3601b572012-05-17 16:58:55 +0200252
253 return phandle++;
254}
255
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000256int qemu_fdt_nop_node(void *fdt, const char *node_path)
Alexander Grafd69a8e62011-07-21 01:52:57 +0200257{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200258 int r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200259
Alexander Grafccbcfed2011-07-23 10:52:00 +0200260 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
261 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800262 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
263 fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200264 exit(1);
265 }
Alexander Grafd69a8e62011-07-21 01:52:57 +0200266
Alexander Grafccbcfed2011-07-23 10:52:00 +0200267 return r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200268}
Alexander Graf80ad7812011-07-22 13:55:37 +0200269
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000270int qemu_fdt_add_subnode(void *fdt, const char *name)
Alexander Graf80ad7812011-07-22 13:55:37 +0200271{
Alexander Graf80ad7812011-07-22 13:55:37 +0200272 char *dupname = g_strdup(name);
273 char *basename = strrchr(dupname, '/');
274 int retval;
Alexander Grafc640d082012-05-17 11:40:42 +0200275 int parent = 0;
Alexander Graf80ad7812011-07-22 13:55:37 +0200276
277 if (!basename) {
Stefan Weilbff39b62011-10-17 22:12:09 +0200278 g_free(dupname);
Alexander Graf80ad7812011-07-22 13:55:37 +0200279 return -1;
280 }
281
282 basename[0] = '\0';
283 basename++;
284
Alexander Grafc640d082012-05-17 11:40:42 +0200285 if (dupname[0]) {
286 parent = findnode_nofail(fdt, dupname);
287 }
288
289 retval = fdt_add_subnode(fdt, parent, basename);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200290 if (retval < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800291 error_report("FDT: Failed to create subnode %s: %s", name,
292 fdt_strerror(retval));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200293 exit(1);
Alexander Graf80ad7812011-07-22 13:55:37 +0200294 }
295
Alexander Graf80ad7812011-07-22 13:55:37 +0200296 g_free(dupname);
297 return retval;
298}
Alexander Graf71193432012-09-23 08:37:59 +0200299
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000300void qemu_fdt_dumpdtb(void *fdt, int size)
Alexander Graf71193432012-09-23 08:37:59 +0200301{
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200302 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
Alexander Graf71193432012-09-23 08:37:59 +0200303
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200304 if (dumpdtb) {
305 /* Dump the dtb to a file and quit */
306 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
Alexander Graf71193432012-09-23 08:37:59 +0200307 }
Alexander Graf71193432012-09-23 08:37:59 +0200308}
Peter Maydell97c38f82013-07-16 13:25:05 +0100309
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000310int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
311 const char *node_path,
312 const char *property,
313 int numvalues,
314 uint64_t *values)
Peter Maydell97c38f82013-07-16 13:25:05 +0100315{
316 uint32_t *propcells;
317 uint64_t value;
318 int cellnum, vnum, ncells;
319 uint32_t hival;
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300320 int ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100321
322 propcells = g_new0(uint32_t, numvalues * 2);
323
324 cellnum = 0;
325 for (vnum = 0; vnum < numvalues; vnum++) {
326 ncells = values[vnum * 2];
327 if (ncells != 1 && ncells != 2) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300328 ret = -1;
329 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100330 }
331 value = values[vnum * 2 + 1];
332 hival = cpu_to_be32(value >> 32);
333 if (ncells > 1) {
334 propcells[cellnum++] = hival;
335 } else if (hival != 0) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300336 ret = -1;
337 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100338 }
339 propcells[cellnum++] = cpu_to_be32(value);
340 }
341
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300342 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
343 cellnum * sizeof(uint32_t));
344out:
345 g_free(propcells);
346 return ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100347}