]> git.proxmox.com Git - mirror_qemu.git/blame - device_tree.c
hw/vfio/platform: amd-xgbe device
[mirror_qemu.git] / device_tree.c
CommitLineData
f652e6af
AJ
1/*
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
d38ea87a 14#include "qemu/osdep.h"
f652e6af 15
f652e6af 16#include "qemu-common.h"
db013f81 17#include "qemu/error-report.h"
9c17d615 18#include "sysemu/device_tree.h"
2ff3de68 19#include "sysemu/sysemu.h"
39b7f20e 20#include "hw/loader.h"
6cabe7fa 21#include "hw/boards.h"
1de7afc9 22#include "qemu/config-file.h"
f652e6af
AJ
23
24#include <libfdt.h>
25
ce36252c
AG
26#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 }
ef6de70e
PM
39 ret = fdt_finish_reservemap(fdt);
40 if (ret < 0) {
41 goto fail;
42 }
ce36252c
AG
43 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) {
508e221f 57 error_report("Unable to copy device tree in memory");
ce36252c
AG
58 exit(1);
59 }
60
61 return fdt;
62fail:
508e221f 63 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
ce36252c
AG
64 exit(1);
65}
66
7ec632b4 67void *load_device_tree(const char *filename_path, int *sizep)
f652e6af 68{
7ec632b4 69 int dt_size;
f652e6af 70 int dt_file_load_size;
f652e6af 71 int ret;
7ec632b4 72 void *fdt = NULL;
f652e6af 73
7ec632b4
PB
74 *sizep = 0;
75 dt_size = get_image_size(filename_path);
76 if (dt_size < 0) {
db013f81
LL
77 error_report("Unable to get size of device tree file '%s'",
78 filename_path);
f652e6af
AJ
79 goto fail;
80 }
81
7ec632b4 82 /* Expand to 2x size to give enough room for manipulation. */
ded57c5f 83 dt_size += 10000;
7ec632b4 84 dt_size *= 2;
f652e6af 85 /* First allocate space in qemu for device tree */
7267c094 86 fdt = g_malloc0(dt_size);
f652e6af 87
7ec632b4
PB
88 dt_file_load_size = load_image(filename_path, fdt);
89 if (dt_file_load_size < 0) {
db013f81
LL
90 error_report("Unable to open device tree file '%s'",
91 filename_path);
7ec632b4
PB
92 goto fail;
93 }
f652e6af 94
7ec632b4 95 ret = fdt_open_into(fdt, fdt, dt_size);
f652e6af 96 if (ret) {
db013f81 97 error_report("Unable to copy device tree in memory");
f652e6af
AJ
98 goto fail;
99 }
100
101 /* Check sanity of device tree */
102 if (fdt_check_header(fdt)) {
db013f81
LL
103 error_report("Device tree file loaded into memory is invalid: %s",
104 filename_path);
f652e6af
AJ
105 goto fail;
106 }
7ec632b4 107 *sizep = dt_size;
f652e6af
AJ
108 return fdt;
109
110fail:
7267c094 111 g_free(fdt);
f652e6af
AJ
112 return NULL;
113}
114
ccbcfedd 115static int findnode_nofail(void *fdt, const char *node_path)
f652e6af
AJ
116{
117 int offset;
118
119 offset = fdt_path_offset(fdt, node_path);
ccbcfedd 120 if (offset < 0) {
508e221f
LL
121 error_report("%s Couldn't find node %s: %s", __func__, node_path,
122 fdt_strerror(offset));
ccbcfedd
AG
123 exit(1);
124 }
125
126 return offset;
127}
128
5a4348d1 129int qemu_fdt_setprop(void *fdt, const char *node_path,
be5907f2 130 const char *property, const void *val, int size)
ccbcfedd
AG
131{
132 int r;
133
be5907f2 134 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
ccbcfedd 135 if (r < 0) {
508e221f
LL
136 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
137 property, fdt_strerror(r));
ccbcfedd
AG
138 exit(1);
139 }
f652e6af 140
ccbcfedd 141 return r;
f652e6af
AJ
142}
143
5a4348d1
PC
144int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
145 const char *property, uint32_t val)
f652e6af 146{
ccbcfedd 147 int r;
f652e6af 148
ccbcfedd
AG
149 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
150 if (r < 0) {
508e221f
LL
151 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
152 node_path, property, val, fdt_strerror(r));
ccbcfedd
AG
153 exit(1);
154 }
f652e6af 155
ccbcfedd 156 return r;
f652e6af
AJ
157}
158
5a4348d1
PC
159int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
160 const char *property, uint64_t val)
bb28eb37
AG
161{
162 val = cpu_to_be64(val);
5a4348d1 163 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
bb28eb37
AG
164}
165
5a4348d1
PC
166int qemu_fdt_setprop_string(void *fdt, const char *node_path,
167 const char *property, const char *string)
f652e6af 168{
ccbcfedd 169 int r;
f652e6af 170
ccbcfedd
AG
171 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
172 if (r < 0) {
508e221f
LL
173 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
174 node_path, property, string, fdt_strerror(r));
ccbcfedd
AG
175 exit(1);
176 }
f652e6af 177
ccbcfedd 178 return r;
f652e6af 179}
d69a8e63 180
5a4348d1
PC
181const void *qemu_fdt_getprop(void *fdt, const char *node_path,
182 const char *property, int *lenp)
f0aa713f
PM
183{
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) {
508e221f
LL
191 error_report("%s: Couldn't get %s/%s: %s", __func__,
192 node_path, property, fdt_strerror(*lenp));
f0aa713f
PM
193 exit(1);
194 }
195 return r;
196}
197
5a4348d1
PC
198uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
199 const char *property)
f0aa713f
PM
200{
201 int len;
5a4348d1 202 const uint32_t *p = qemu_fdt_getprop(fdt, node_path, property, &len);
f0aa713f 203 if (len != 4) {
508e221f
LL
204 error_report("%s: %s/%s not 4 bytes long (not a cell?)",
205 __func__, node_path, property);
f0aa713f
PM
206 exit(1);
207 }
208 return be32_to_cpu(*p);
209}
210
5a4348d1 211uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
7d5fd108
AG
212{
213 uint32_t r;
214
215 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
909a196d 216 if (r == 0) {
508e221f
LL
217 error_report("%s: Couldn't get phandle for %s: %s", __func__,
218 path, fdt_strerror(r));
7d5fd108
AG
219 exit(1);
220 }
221
222 return r;
223}
224
5a4348d1
PC
225int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
226 const char *property,
227 const char *target_node_path)
8535ab12 228{
5a4348d1
PC
229 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
230 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
8535ab12
AG
231}
232
5a4348d1 233uint32_t qemu_fdt_alloc_phandle(void *fdt)
3601b572 234{
4b1b1c89
AG
235 static int phandle = 0x0;
236
237 /*
238 * We need to find out if the user gave us special instruction at
cc47a16b 239 * which phandle id to start allocating phandles.
4b1b1c89
AG
240 */
241 if (!phandle) {
6cabe7fa 242 phandle = machine_phandle_start(current_machine);
4b1b1c89
AG
243 }
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 }
3601b572
AG
252
253 return phandle++;
254}
255
5a4348d1 256int qemu_fdt_nop_node(void *fdt, const char *node_path)
d69a8e63 257{
ccbcfedd 258 int r;
d69a8e63 259
ccbcfedd
AG
260 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
261 if (r < 0) {
508e221f
LL
262 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
263 fdt_strerror(r));
ccbcfedd
AG
264 exit(1);
265 }
d69a8e63 266
ccbcfedd 267 return r;
d69a8e63 268}
80ad7816 269
5a4348d1 270int qemu_fdt_add_subnode(void *fdt, const char *name)
80ad7816 271{
80ad7816
AG
272 char *dupname = g_strdup(name);
273 char *basename = strrchr(dupname, '/');
274 int retval;
c640d088 275 int parent = 0;
80ad7816
AG
276
277 if (!basename) {
bff39b63 278 g_free(dupname);
80ad7816
AG
279 return -1;
280 }
281
282 basename[0] = '\0';
283 basename++;
284
c640d088
AG
285 if (dupname[0]) {
286 parent = findnode_nofail(fdt, dupname);
287 }
288
289 retval = fdt_add_subnode(fdt, parent, basename);
ccbcfedd 290 if (retval < 0) {
508e221f
LL
291 error_report("FDT: Failed to create subnode %s: %s", name,
292 fdt_strerror(retval));
ccbcfedd 293 exit(1);
80ad7816
AG
294 }
295
80ad7816
AG
296 g_free(dupname);
297 return retval;
298}
71193433 299
5a4348d1 300void qemu_fdt_dumpdtb(void *fdt, int size)
71193433 301{
2ff3de68 302 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
71193433 303
2ff3de68
MA
304 if (dumpdtb) {
305 /* Dump the dtb to a file and quit */
306 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
307 }
71193433 308}
97c38f8c 309
5a4348d1
PC
310int 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)
97c38f8c
PM
315{
316 uint32_t *propcells;
317 uint64_t value;
318 int cellnum, vnum, ncells;
319 uint32_t hival;
2bf9febc 320 int ret;
97c38f8c
PM
321
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) {
2bf9febc
SF
328 ret = -1;
329 goto out;
97c38f8c
PM
330 }
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) {
2bf9febc
SF
336 ret = -1;
337 goto out;
97c38f8c
PM
338 }
339 propcells[cellnum++] = cpu_to_be32(value);
340 }
341
2bf9febc
SF
342 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
343 cellnum * sizeof(uint32_t));
344out:
345 g_free(propcells);
346 return ret;
97c38f8c 347}