blob: 3d119ef0bdb15db633a97bf737a706782cbb3361 [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
21#include "config.h"
22#include "qemu-common.h"
Li Liudb013f82014-08-26 14:38:02 +080023#include "qemu/error-report.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010024#include "sysemu/device_tree.h"
Markus Armbruster2ff3de62013-07-04 15:09:22 +020025#include "sysemu/sysemu.h"
Blue Swirl39b7f202009-09-22 17:51:36 +000026#include "hw/loader.h"
Marcel Apfelbaum6cabe7f2015-02-04 17:43:53 +020027#include "hw/boards.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/config-file.h"
aurel32f652e6a2008-12-16 10:43:48 +000029
30#include <libfdt.h>
31
Alexander Grafce362522012-05-17 15:33:54 +020032#define FDT_MAX_SIZE 0x10000
33
34void *create_device_tree(int *sizep)
35{
36 void *fdt;
37 int ret;
38
39 *sizep = FDT_MAX_SIZE;
40 fdt = g_malloc0(FDT_MAX_SIZE);
41 ret = fdt_create(fdt, FDT_MAX_SIZE);
42 if (ret < 0) {
43 goto fail;
44 }
Peter Maydellef6de702013-11-22 17:17:09 +000045 ret = fdt_finish_reservemap(fdt);
46 if (ret < 0) {
47 goto fail;
48 }
Alexander Grafce362522012-05-17 15:33:54 +020049 ret = fdt_begin_node(fdt, "");
50 if (ret < 0) {
51 goto fail;
52 }
53 ret = fdt_end_node(fdt);
54 if (ret < 0) {
55 goto fail;
56 }
57 ret = fdt_finish(fdt);
58 if (ret < 0) {
59 goto fail;
60 }
61 ret = fdt_open_into(fdt, fdt, *sizep);
62 if (ret) {
Li Liu508e2212014-08-26 14:38:03 +080063 error_report("Unable to copy device tree in memory");
Alexander Grafce362522012-05-17 15:33:54 +020064 exit(1);
65 }
66
67 return fdt;
68fail:
Li Liu508e2212014-08-26 14:38:03 +080069 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
Alexander Grafce362522012-05-17 15:33:54 +020070 exit(1);
71}
72
pbrook7ec632b2009-04-10 16:23:59 +000073void *load_device_tree(const char *filename_path, int *sizep)
aurel32f652e6a2008-12-16 10:43:48 +000074{
pbrook7ec632b2009-04-10 16:23:59 +000075 int dt_size;
aurel32f652e6a2008-12-16 10:43:48 +000076 int dt_file_load_size;
aurel32f652e6a2008-12-16 10:43:48 +000077 int ret;
pbrook7ec632b2009-04-10 16:23:59 +000078 void *fdt = NULL;
aurel32f652e6a2008-12-16 10:43:48 +000079
pbrook7ec632b2009-04-10 16:23:59 +000080 *sizep = 0;
81 dt_size = get_image_size(filename_path);
82 if (dt_size < 0) {
Li Liudb013f82014-08-26 14:38:02 +080083 error_report("Unable to get size of device tree file '%s'",
84 filename_path);
aurel32f652e6a2008-12-16 10:43:48 +000085 goto fail;
86 }
87
pbrook7ec632b2009-04-10 16:23:59 +000088 /* Expand to 2x size to give enough room for manipulation. */
Alexander Grafded57c52011-07-23 10:54:11 +020089 dt_size += 10000;
pbrook7ec632b2009-04-10 16:23:59 +000090 dt_size *= 2;
aurel32f652e6a2008-12-16 10:43:48 +000091 /* First allocate space in qemu for device tree */
Anthony Liguori7267c092011-08-20 22:09:37 -050092 fdt = g_malloc0(dt_size);
aurel32f652e6a2008-12-16 10:43:48 +000093
pbrook7ec632b2009-04-10 16:23:59 +000094 dt_file_load_size = load_image(filename_path, fdt);
95 if (dt_file_load_size < 0) {
Li Liudb013f82014-08-26 14:38:02 +080096 error_report("Unable to open device tree file '%s'",
97 filename_path);
pbrook7ec632b2009-04-10 16:23:59 +000098 goto fail;
99 }
aurel32f652e6a2008-12-16 10:43:48 +0000100
pbrook7ec632b2009-04-10 16:23:59 +0000101 ret = fdt_open_into(fdt, fdt, dt_size);
aurel32f652e6a2008-12-16 10:43:48 +0000102 if (ret) {
Li Liudb013f82014-08-26 14:38:02 +0800103 error_report("Unable to copy device tree in memory");
aurel32f652e6a2008-12-16 10:43:48 +0000104 goto fail;
105 }
106
107 /* Check sanity of device tree */
108 if (fdt_check_header(fdt)) {
Li Liudb013f82014-08-26 14:38:02 +0800109 error_report("Device tree file loaded into memory is invalid: %s",
110 filename_path);
aurel32f652e6a2008-12-16 10:43:48 +0000111 goto fail;
112 }
pbrook7ec632b2009-04-10 16:23:59 +0000113 *sizep = dt_size;
aurel32f652e6a2008-12-16 10:43:48 +0000114 return fdt;
115
116fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500117 g_free(fdt);
aurel32f652e6a2008-12-16 10:43:48 +0000118 return NULL;
119}
120
Alexander Grafccbcfed2011-07-23 10:52:00 +0200121static int findnode_nofail(void *fdt, const char *node_path)
aurel32f652e6a2008-12-16 10:43:48 +0000122{
123 int offset;
124
125 offset = fdt_path_offset(fdt, node_path);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200126 if (offset < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800127 error_report("%s Couldn't find node %s: %s", __func__, node_path,
128 fdt_strerror(offset));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200129 exit(1);
130 }
aurel32f652e6a2008-12-16 10:43:48 +0000131
Alexander Grafccbcfed2011-07-23 10:52:00 +0200132 return offset;
133}
134
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000135int qemu_fdt_setprop(void *fdt, const char *node_path,
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000136 const char *property, const void *val, int size)
Alexander Grafccbcfed2011-07-23 10:52:00 +0200137{
138 int r;
139
Peter Crosthwaitebe5907f2013-11-11 18:15:21 +1000140 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200141 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800142 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
143 property, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200144 exit(1);
145 }
146
147 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000148}
149
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000150int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
151 const char *property, uint32_t val)
aurel32f652e6a2008-12-16 10:43:48 +0000152{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200153 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000154
Alexander Grafccbcfed2011-07-23 10:52:00 +0200155 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
156 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800157 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
158 node_path, property, val, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200159 exit(1);
160 }
aurel32f652e6a2008-12-16 10:43:48 +0000161
Alexander Grafccbcfed2011-07-23 10:52:00 +0200162 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000163}
164
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000165int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
166 const char *property, uint64_t val)
Alexander Grafbb28eb32012-05-18 01:53:01 +0200167{
168 val = cpu_to_be64(val);
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000169 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
Alexander Grafbb28eb32012-05-18 01:53:01 +0200170}
171
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000172int qemu_fdt_setprop_string(void *fdt, const char *node_path,
173 const char *property, const char *string)
aurel32f652e6a2008-12-16 10:43:48 +0000174{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200175 int r;
aurel32f652e6a2008-12-16 10:43:48 +0000176
Alexander Grafccbcfed2011-07-23 10:52:00 +0200177 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
178 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800179 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
180 node_path, property, string, fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200181 exit(1);
182 }
aurel32f652e6a2008-12-16 10:43:48 +0000183
Alexander Grafccbcfed2011-07-23 10:52:00 +0200184 return r;
aurel32f652e6a2008-12-16 10:43:48 +0000185}
Alexander Grafd69a8e62011-07-21 01:52:57 +0200186
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000187const void *qemu_fdt_getprop(void *fdt, const char *node_path,
188 const char *property, int *lenp)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100189{
190 int len;
191 const void *r;
192 if (!lenp) {
193 lenp = &len;
194 }
195 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
196 if (!r) {
Li Liu508e2212014-08-26 14:38:03 +0800197 error_report("%s: Couldn't get %s/%s: %s", __func__,
198 node_path, property, fdt_strerror(*lenp));
Peter Maydellf0aa7132012-07-20 13:34:50 +0100199 exit(1);
200 }
201 return r;
202}
203
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000204uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
205 const char *property)
Peter Maydellf0aa7132012-07-20 13:34:50 +0100206{
207 int len;
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000208 const uint32_t *p = qemu_fdt_getprop(fdt, node_path, property, &len);
Peter Maydellf0aa7132012-07-20 13:34:50 +0100209 if (len != 4) {
Li Liu508e2212014-08-26 14:38:03 +0800210 error_report("%s: %s/%s not 4 bytes long (not a cell?)",
211 __func__, node_path, property);
Peter Maydellf0aa7132012-07-20 13:34:50 +0100212 exit(1);
213 }
214 return be32_to_cpu(*p);
215}
216
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000217uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
Alexander Graf7d5fd102012-05-17 15:23:39 +0200218{
219 uint32_t r;
220
221 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
Stefan Weil909a1962013-06-10 22:12:25 +0200222 if (r == 0) {
Li Liu508e2212014-08-26 14:38:03 +0800223 error_report("%s: Couldn't get phandle for %s: %s", __func__,
224 path, fdt_strerror(r));
Alexander Graf7d5fd102012-05-17 15:23:39 +0200225 exit(1);
226 }
227
228 return r;
229}
230
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000231int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
232 const char *property,
233 const char *target_node_path)
Alexander Graf8535ab12012-05-17 14:11:52 +0200234{
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000235 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
236 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
Alexander Graf8535ab12012-05-17 14:11:52 +0200237}
238
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000239uint32_t qemu_fdt_alloc_phandle(void *fdt)
Alexander Graf3601b572012-05-17 16:58:55 +0200240{
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200241 static int phandle = 0x0;
242
243 /*
244 * We need to find out if the user gave us special instruction at
245 * which phandle id to start allocting phandles.
246 */
247 if (!phandle) {
Marcel Apfelbaum6cabe7f2015-02-04 17:43:53 +0200248 phandle = machine_phandle_start(current_machine);
Alexander Graf4b1b1c82012-06-06 01:01:23 +0200249 }
250
251 if (!phandle) {
252 /*
253 * None or invalid phandle given on the command line, so fall back to
254 * default starting point.
255 */
256 phandle = 0x8000;
257 }
Alexander Graf3601b572012-05-17 16:58:55 +0200258
259 return phandle++;
260}
261
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000262int qemu_fdt_nop_node(void *fdt, const char *node_path)
Alexander Grafd69a8e62011-07-21 01:52:57 +0200263{
Alexander Grafccbcfed2011-07-23 10:52:00 +0200264 int r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200265
Alexander Grafccbcfed2011-07-23 10:52:00 +0200266 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
267 if (r < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800268 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
269 fdt_strerror(r));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200270 exit(1);
271 }
Alexander Grafd69a8e62011-07-21 01:52:57 +0200272
Alexander Grafccbcfed2011-07-23 10:52:00 +0200273 return r;
Alexander Grafd69a8e62011-07-21 01:52:57 +0200274}
Alexander Graf80ad7812011-07-22 13:55:37 +0200275
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000276int qemu_fdt_add_subnode(void *fdt, const char *name)
Alexander Graf80ad7812011-07-22 13:55:37 +0200277{
Alexander Graf80ad7812011-07-22 13:55:37 +0200278 char *dupname = g_strdup(name);
279 char *basename = strrchr(dupname, '/');
280 int retval;
Alexander Grafc640d082012-05-17 11:40:42 +0200281 int parent = 0;
Alexander Graf80ad7812011-07-22 13:55:37 +0200282
283 if (!basename) {
Stefan Weilbff39b62011-10-17 22:12:09 +0200284 g_free(dupname);
Alexander Graf80ad7812011-07-22 13:55:37 +0200285 return -1;
286 }
287
288 basename[0] = '\0';
289 basename++;
290
Alexander Grafc640d082012-05-17 11:40:42 +0200291 if (dupname[0]) {
292 parent = findnode_nofail(fdt, dupname);
293 }
294
295 retval = fdt_add_subnode(fdt, parent, basename);
Alexander Grafccbcfed2011-07-23 10:52:00 +0200296 if (retval < 0) {
Li Liu508e2212014-08-26 14:38:03 +0800297 error_report("FDT: Failed to create subnode %s: %s", name,
298 fdt_strerror(retval));
Alexander Grafccbcfed2011-07-23 10:52:00 +0200299 exit(1);
Alexander Graf80ad7812011-07-22 13:55:37 +0200300 }
301
Alexander Graf80ad7812011-07-22 13:55:37 +0200302 g_free(dupname);
303 return retval;
304}
Alexander Graf71193432012-09-23 08:37:59 +0200305
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000306void qemu_fdt_dumpdtb(void *fdt, int size)
Alexander Graf71193432012-09-23 08:37:59 +0200307{
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200308 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
Alexander Graf71193432012-09-23 08:37:59 +0200309
Markus Armbruster2ff3de62013-07-04 15:09:22 +0200310 if (dumpdtb) {
311 /* Dump the dtb to a file and quit */
312 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
Alexander Graf71193432012-09-23 08:37:59 +0200313 }
Alexander Graf71193432012-09-23 08:37:59 +0200314}
Peter Maydell97c38f82013-07-16 13:25:05 +0100315
Peter Crosthwaite5a4348d2013-11-11 18:14:41 +1000316int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
317 const char *node_path,
318 const char *property,
319 int numvalues,
320 uint64_t *values)
Peter Maydell97c38f82013-07-16 13:25:05 +0100321{
322 uint32_t *propcells;
323 uint64_t value;
324 int cellnum, vnum, ncells;
325 uint32_t hival;
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300326 int ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100327
328 propcells = g_new0(uint32_t, numvalues * 2);
329
330 cellnum = 0;
331 for (vnum = 0; vnum < numvalues; vnum++) {
332 ncells = values[vnum * 2];
333 if (ncells != 1 && ncells != 2) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300334 ret = -1;
335 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100336 }
337 value = values[vnum * 2 + 1];
338 hival = cpu_to_be32(value >> 32);
339 if (ncells > 1) {
340 propcells[cellnum++] = hival;
341 } else if (hival != 0) {
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300342 ret = -1;
343 goto out;
Peter Maydell97c38f82013-07-16 13:25:05 +0100344 }
345 propcells[cellnum++] = cpu_to_be32(value);
346 }
347
Sergey Fedorov2bf9feb2014-12-11 18:45:05 +0300348 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
349 cellnum * sizeof(uint32_t));
350out:
351 g_free(propcells);
352 return ret;
Peter Maydell97c38f82013-07-16 13:25:05 +0100353}