blob: a9f5f8e598d334f796fe250742e967b6f9b34424 [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
14#include <stdio.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <unistd.h>
19#include <stdlib.h>
20
aurel32f652e6a2008-12-16 10:43:48 +000021#include "qemu-common.h"
Li Liudb013f82014-08-26 14:38:02 +080022#include "qemu/error-report.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010023#include "sysemu/device_tree.h"
Markus Armbruster2ff3de62013-07-04 15:09:22 +020024#include "sysemu/sysemu.h"
Blue Swirl39b7f202009-09-22 17:51:36 +000025#include "hw/loader.h"
Marcel Apfelbaum6cabe7f2015-02-04 17:43:53 +020026#include "hw/boards.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/config-file.h"
aurel32f652e6a2008-12-16 10:43:48 +000028
29#include <libfdt.h>
30
Alexander Grafce362522012-05-17 15:33:54 +020031#define FDT_MAX_SIZE 0x10000
32
33void *create_device_tree(int *sizep)
34{
35 void *fdt;
36 int ret;
37
38 *sizep = FDT_MAX_SIZE;
39 fdt = g_malloc0(FDT_MAX_SIZE);
40 ret = fdt_create(fdt, FDT_MAX_SIZE);
41 if (ret < 0) {
42 goto fail;
43 }
Peter Maydellef6de702013-11-22 17:17:09 +000044 ret = fdt_finish_reservemap(fdt);
45 if (ret < 0) {
46 goto fail;
47 }
Alexander Grafce362522012-05-17 15:33:54 +020048 ret = fdt_begin_node(fdt, "");
49 if (ret < 0) {
50 goto fail;
51 }
52 ret = fdt_end_node(fdt);
53 if (ret < 0) {
54 goto fail;
55 }
56 ret = fdt_finish(fdt);
57 if (ret < 0) {
58 goto fail;
59 }
60 ret = fdt_open_into(fdt, fdt, *sizep);
61 if (ret) {
Li Liu508e2212014-08-26 14:38:03 +080062 error_report("Unable to copy device tree in memory");
Alexander Grafce362522012-05-17 15:33:54 +020063 exit(1);
64 }
65
66 return fdt;
67fail:
Li Liu508e2212014-08-26 14:38:03 +080068 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
Alexander Grafce362522012-05-17 15:33:54 +020069 exit(1);
70}
71
pbrook7ec632b2009-04-10 16:23:59 +000072void *load_device_tree(const char *filename_path, int *sizep)
aurel32f652e6a2008-12-16 10:43:48 +000073{
pbrook7ec632b2009-04-10 16:23:59 +000074 int dt_size;
aurel32f652e6a2008-12-16 10:43:48 +000075 int dt_file_load_size;
aurel32f652e6a2008-12-16 10:43:48 +000076 int ret;
pbrook7ec632b2009-04-10 16:23:59 +000077 void *fdt = NULL;
aurel32f652e6a2008-12-16 10:43:48 +000078
pbrook7ec632b2009-04-10 16:23:59 +000079 *sizep = 0;
80 dt_size = get_image_size(filename_path);
81 if (dt_size < 0) {
Li Liudb013f82014-08-26 14:38:02 +080082 error_report("Unable to get size of device tree file '%s'",
83 filename_path);
aurel32f652e6a2008-12-16 10:43:48 +000084 goto fail;
85 }
86
pbrook7ec632b2009-04-10 16:23:59 +000087 /* Expand to 2x size to give enough room for manipulation. */
Alexander Grafded57c52011-07-23 10:54:11 +020088 dt_size += 10000;
pbrook7ec632b2009-04-10 16:23:59 +000089 dt_size *= 2;
aurel32f652e6a2008-12-16 10:43:48 +000090 /* First allocate space in qemu for device tree */
Anthony Liguori7267c092011-08-20 22:09:37 -050091 fdt = g_malloc0(dt_size);
aurel32f652e6a2008-12-16 10:43:48 +000092
pbrook7ec632b2009-04-10 16:23:59 +000093 dt_file_load_size = load_image(filename_path, fdt);
94 if (dt_file_load_size < 0) {
Li Liudb013f82014-08-26 14:38:02 +080095 error_report("Unable to open device tree file '%s'",
96 filename_path);
pbrook7ec632b2009-04-10 16:23:59 +000097 goto fail;
98 }
aurel32f652e6a2008-12-16 10:43:48 +000099
pbrook7ec632b2009-04-10 16:23:59 +0000100 ret = fdt_open_into(fdt, fdt, dt_size);
aurel32f652e6a2008-12-16 10:43:48 +0000101 if (ret) {
Li Liudb013f82014-08-26 14:38:02 +0800102 error_report("Unable to copy device tree in memory");
aurel32f652e6a2008-12-16 10:43:48 +0000103 goto fail;
104 }
105
106 /* Check sanity of device tree */
107 if (fdt_check_header(fdt)) {
Li Liudb013f82014-08-26 14:38:02 +0800108 error_report("Device tree file loaded into memory is invalid: %s",
109 filename_path);
aurel32f652e6a2008-12-16 10:43:48 +0000110 goto fail;
111 }
pbrook7ec632b2009-04-10 16:23:59 +0000112 *sizep = dt_size;
aurel32f652e6a2008-12-16 10:43:48 +0000113 return fdt;
114
115fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500116 g_free(fdt);
aurel32f652e6a2008-12-16 10:43:48 +0000117 return NULL;
118}
119
Alexander Grafccbcfed2011-07-23 10:52:00 +0200120static int findnode_nofail(void *fdt, const char *node_path)
aurel32f652e6a2008-12-16 10:43:48 +0000121{
122 int offset;
123
124 offset = fdt_path_offset(fdt, node_path);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200125 if (offset < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800126 error_report("%s Couldn't find node %s: %s", __func__, node_path,
127 fdt_strerror(offset));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200128 exit(1);
129 }
aurel32f652e6a2008-12-16 10:43:48 +0000130
Alexander Grafccbcfed2011-07-23 10:52:00 +0200131 return offset;
132}
133
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000134int qemu_fdt_setprop(void *fdt, const char *node_path,
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000135 const char *property, const void *val, int size)
Alexander Grafccbcfed2011-07-23 10:52:00 +0200136{
137 int r;
138
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000139 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200140 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800141 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
142 property, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200143 exit(1);
144 }
145
146 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000147}
148
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000149int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
150 const char *property, uint32_t val)
aurel32f652e6a2008-12-16 10:43:48 +0000151{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200152 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000153
Alexander Grafccbcfed2011-07-23 10:52:00 +0200154 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
155 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800156 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
157 node_path, property, val, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200158 exit(1);
159 }
aurel32f652e6a2008-12-16 10:43:48 +0000160
Alexander Grafccbcfed2011-07-23 10:52:00 +0200161 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000162}
163
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000164int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
165 const char *property, uint64_t val)
Alexander Grafbb28eb32012-05-18 01:53:01 +0200166{
167 val = cpu_to_be64(val);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000168 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
Alexander Grafbb28eb32012-05-18 01:53:01 +0200169}
170
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000171int qemu_fdt_setprop_string(void *fdt, const char *node_path,
172 const char *property, const char *string)
aurel32f652e6a2008-12-16 10:43:48 +0000173{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200174 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000175
Alexander Grafccbcfed2011-07-23 10:52:00 +0200176 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
177 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800178 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
179 node_path, property, string, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200180 exit(1);
181 }
aurel32f652e6a2008-12-16 10:43:48 +0000182
Alexander Grafccbcfed2011-07-23 10:52:00 +0200183 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000184}
Alexander Grafd69a8e62011-07-21 01:52:57 +0200185
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000186const void *qemu_fdt_getprop(void *fdt, const char *node_path,
187 const char *property, int *lenp)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100188{
189 int len;
190 const void *r;
191 if (!lenp) {
192 lenp = &len;
193 }
194 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
195 if (!r) {
Li Liu508e2212014-08-26 14:38:03 +0800196 error_report("%s: Couldn't get %s/%s: %s", __func__,
197 node_path, property, fdt_strerror(*lenp));
Peter Maydellf0aa7132012-07-20 13:34:50 +0100198 exit(1);
199 }
200 return r;
201}
202
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000203uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
204 const char *property)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100205{
206 int len;
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000207 const uint32_t *p = qemu_fdt_getprop(fdt, node_path, property, &len);
Peter Maydellf0aa7132012-07-20 13:34:50 +0100208 if (len != 4) {
Li Liu508e2212014-08-26 14:38:03 +0800209 error_report("%s: %s/%s not 4 bytes long (not a cell?)",
210 __func__, node_path, property);
Peter Maydellf0aa7132012-07-20 13:34:50 +0100211 exit(1);
212 }
213 return be32_to_cpu(*p);
214}
215
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000216uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
Alexander Graf7d5fd102012-05-17 15:23:39 +0200217{
218 uint32_t r;
219
220 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
Stefan Weil909a1962013-06-10 22:12:25 +0200221 if (r == 0) {
Li Liu508e2212014-08-26 14:38:03 +0800222 error_report("%s: Couldn't get phandle for %s: %s", __func__,
223 path, fdt_strerror(r));
Alexander Graf7d5fd102012-05-17 15:23:39 +0200224 exit(1);
225 }
226
227 return r;
228}
229
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000230int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
231 const char *property,
232 const char *target_node_path)
Alexander Graf8535ab12012-05-17 14:11:52 +0200233{
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000234 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
235 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
Alexander Graf8535ab12012-05-17 14:11:52 +0200236}
237
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000238uint32_t qemu_fdt_alloc_phandle(void *fdt)
Alexander Graf3601b572012-05-17 16:58:55 +0200239{
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200240 static int phandle = 0x0;
241
242 /*
243 * We need to find out if the user gave us special instruction at
Kamalesh Babulalcc47a162015-07-24 13:48:13 +0530244 * which phandle id to start allocating phandles.
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200245 */
246 if (!phandle) {
Marcel Apfelbaum6cabe7f2015-02-04 17:43:53 +0200247 phandle = machine_phandle_start(current_machine);
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200248 }
249
250 if (!phandle) {
251 /*
252 * None or invalid phandle given on the command line, so fall back to
253 * default starting point.
254 */
255 phandle = 0x8000;
256 }
Alexander Graf3601b572012-05-17 16:58:55 +0200257
258 return phandle++;
259}
260
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000261int qemu_fdt_nop_node(void *fdt, const char *node_path)
Alexander Grafd69a8e62011-07-21 01:52:57 +0200262{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200263 int r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200264
Alexander Grafccbcfed2011-07-23 10:52:00 +0200265 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
266 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800267 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
268 fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200269 exit(1);
270 }
Alexander Grafd69a8e62011-07-21 01:52:57 +0200271
Alexander Grafccbcfed2011-07-23 10:52:00 +0200272 return r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200273}
Alexander Graf80ad7812011-07-22 13:55:37 +0200274
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000275int qemu_fdt_add_subnode(void *fdt, const char *name)
Alexander Graf80ad7812011-07-22 13:55:37 +0200276{
Alexander Graf80ad7812011-07-22 13:55:37 +0200277 char *dupname = g_strdup(name);
278 char *basename = strrchr(dupname, '/');
279 int retval;
Alexander Grafc640d082012-05-17 11:40:42 +0200280 int parent = 0;
Alexander Graf80ad7812011-07-22 13:55:37 +0200281
282 if (!basename) {
Stefan Weilbff39b62011-10-17 22:12:09 +0200283 g_free(dupname);
Alexander Graf80ad7812011-07-22 13:55:37 +0200284 return -1;
285 }
286
287 basename[0] = '\0';
288 basename++;
289
Alexander Grafc640d082012-05-17 11:40:42 +0200290 if (dupname[0]) {
291 parent = findnode_nofail(fdt, dupname);
292 }
293
294 retval = fdt_add_subnode(fdt, parent, basename);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200295 if (retval < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800296 error_report("FDT: Failed to create subnode %s: %s", name,
297 fdt_strerror(retval));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200298 exit(1);
Alexander Graf80ad7812011-07-22 13:55:37 +0200299 }
300
Alexander Graf80ad7812011-07-22 13:55:37 +0200301 g_free(dupname);
302 return retval;
303}
Alexander Graf71193432012-09-23 08:37:59 +0200304
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000305void qemu_fdt_dumpdtb(void *fdt, int size)
Alexander Graf71193432012-09-23 08:37:59 +0200306{
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200307 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
Alexander Graf71193432012-09-23 08:37:59 +0200308
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200309 if (dumpdtb) {
310 /* Dump the dtb to a file and quit */
311 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
Alexander Graf71193432012-09-23 08:37:59 +0200312 }
Alexander Graf71193432012-09-23 08:37:59 +0200313}
Peter Maydell97c38f82013-07-16 13:25:05 +0100314
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000315int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
316 const char *node_path,
317 const char *property,
318 int numvalues,
319 uint64_t *values)
Peter Maydell97c38f82013-07-16 13:25:05 +0100320{
321 uint32_t *propcells;
322 uint64_t value;
323 int cellnum, vnum, ncells;
324 uint32_t hival;
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300325 int ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100326
327 propcells = g_new0(uint32_t, numvalues * 2);
328
329 cellnum = 0;
330 for (vnum = 0; vnum < numvalues; vnum++) {
331 ncells = values[vnum * 2];
332 if (ncells != 1 && ncells != 2) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300333 ret = -1;
334 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100335 }
336 value = values[vnum * 2 + 1];
337 hival = cpu_to_be32(value >> 32);
338 if (ncells > 1) {
339 propcells[cellnum++] = hival;
340 } else if (hival != 0) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300341 ret = -1;
342 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100343 }
344 propcells[cellnum++] = cpu_to_be32(value);
345 }
346
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300347 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
348 cellnum * sizeof(uint32_t));
349out:
350 g_free(propcells);
351 return ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100352}