]> git.ipfire.org Git - thirdparty/qemu.git/blame - device_tree.c
machine: query kvm-shadow-mem machine property
[thirdparty/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
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"
db013f81 23#include "qemu/error-report.h"
9c17d615 24#include "sysemu/device_tree.h"
2ff3de68 25#include "sysemu/sysemu.h"
39b7f20e 26#include "hw/loader.h"
1de7afc9
PB
27#include "qemu/option.h"
28#include "qemu/config-file.h"
f652e6af
AJ
29
30#include <libfdt.h>
31
ce36252c
AG
32#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 }
ef6de70e
PM
45 ret = fdt_finish_reservemap(fdt);
46 if (ret < 0) {
47 goto fail;
48 }
ce36252c
AG
49 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) {
508e221f 63 error_report("Unable to copy device tree in memory");
ce36252c
AG
64 exit(1);
65 }
66
67 return fdt;
68fail:
508e221f 69 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
ce36252c
AG
70 exit(1);
71}
72
7ec632b4 73void *load_device_tree(const char *filename_path, int *sizep)
f652e6af 74{
7ec632b4 75 int dt_size;
f652e6af 76 int dt_file_load_size;
f652e6af 77 int ret;
7ec632b4 78 void *fdt = NULL;
f652e6af 79
7ec632b4
PB
80 *sizep = 0;
81 dt_size = get_image_size(filename_path);
82 if (dt_size < 0) {
db013f81
LL
83 error_report("Unable to get size of device tree file '%s'",
84 filename_path);
f652e6af
AJ
85 goto fail;
86 }
87
7ec632b4 88 /* Expand to 2x size to give enough room for manipulation. */
ded57c5f 89 dt_size += 10000;
7ec632b4 90 dt_size *= 2;
f652e6af 91 /* First allocate space in qemu for device tree */
7267c094 92 fdt = g_malloc0(dt_size);
f652e6af 93
7ec632b4
PB
94 dt_file_load_size = load_image(filename_path, fdt);
95 if (dt_file_load_size < 0) {
db013f81
LL
96 error_report("Unable to open device tree file '%s'",
97 filename_path);
7ec632b4
PB
98 goto fail;
99 }
f652e6af 100
7ec632b4 101 ret = fdt_open_into(fdt, fdt, dt_size);
f652e6af 102 if (ret) {
db013f81 103 error_report("Unable to copy device tree in memory");
f652e6af
AJ
104 goto fail;
105 }
106
107 /* Check sanity of device tree */
108 if (fdt_check_header(fdt)) {
db013f81
LL
109 error_report("Device tree file loaded into memory is invalid: %s",
110 filename_path);
f652e6af
AJ
111 goto fail;
112 }
7ec632b4 113 *sizep = dt_size;
f652e6af
AJ
114 return fdt;
115
116fail:
7267c094 117 g_free(fdt);
f652e6af
AJ
118 return NULL;
119}
120
ccbcfedd 121static int findnode_nofail(void *fdt, const char *node_path)
f652e6af
AJ
122{
123 int offset;
124
125 offset = fdt_path_offset(fdt, node_path);
ccbcfedd 126 if (offset < 0) {
508e221f
LL
127 error_report("%s Couldn't find node %s: %s", __func__, node_path,
128 fdt_strerror(offset));
ccbcfedd
AG
129 exit(1);
130 }
131
132 return offset;
133}
134
5a4348d1 135int qemu_fdt_setprop(void *fdt, const char *node_path,
be5907f2 136 const char *property, const void *val, int size)
ccbcfedd
AG
137{
138 int r;
139
be5907f2 140 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
ccbcfedd 141 if (r < 0) {
508e221f
LL
142 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
143 property, fdt_strerror(r));
ccbcfedd
AG
144 exit(1);
145 }
f652e6af 146
ccbcfedd 147 return r;
f652e6af
AJ
148}
149
5a4348d1
PC
150int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
151 const char *property, uint32_t val)
f652e6af 152{
ccbcfedd 153 int r;
f652e6af 154
ccbcfedd
AG
155 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
156 if (r < 0) {
508e221f
LL
157 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
158 node_path, property, val, fdt_strerror(r));
ccbcfedd
AG
159 exit(1);
160 }
f652e6af 161
ccbcfedd 162 return r;
f652e6af
AJ
163}
164
5a4348d1
PC
165int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
166 const char *property, uint64_t val)
bb28eb37
AG
167{
168 val = cpu_to_be64(val);
5a4348d1 169 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
bb28eb37
AG
170}
171
5a4348d1
PC
172int qemu_fdt_setprop_string(void *fdt, const char *node_path,
173 const char *property, const char *string)
f652e6af 174{
ccbcfedd 175 int r;
f652e6af 176
ccbcfedd
AG
177 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
178 if (r < 0) {
508e221f
LL
179 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
180 node_path, property, string, fdt_strerror(r));
ccbcfedd
AG
181 exit(1);
182 }
f652e6af 183
ccbcfedd 184 return r;
f652e6af 185}
d69a8e63 186
5a4348d1
PC
187const void *qemu_fdt_getprop(void *fdt, const char *node_path,
188 const char *property, int *lenp)
f0aa713f
PM
189{
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) {
508e221f
LL
197 error_report("%s: Couldn't get %s/%s: %s", __func__,
198 node_path, property, fdt_strerror(*lenp));
f0aa713f
PM
199 exit(1);
200 }
201 return r;
202}
203
5a4348d1
PC
204uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
205 const char *property)
f0aa713f
PM
206{
207 int len;
5a4348d1 208 const uint32_t *p = qemu_fdt_getprop(fdt, node_path, property, &len);
f0aa713f 209 if (len != 4) {
508e221f
LL
210 error_report("%s: %s/%s not 4 bytes long (not a cell?)",
211 __func__, node_path, property);
f0aa713f
PM
212 exit(1);
213 }
214 return be32_to_cpu(*p);
215}
216
5a4348d1 217uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
7d5fd108
AG
218{
219 uint32_t r;
220
221 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
909a196d 222 if (r == 0) {
508e221f
LL
223 error_report("%s: Couldn't get phandle for %s: %s", __func__,
224 path, fdt_strerror(r));
7d5fd108
AG
225 exit(1);
226 }
227
228 return r;
229}
230
5a4348d1
PC
231int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
232 const char *property,
233 const char *target_node_path)
8535ab12 234{
5a4348d1
PC
235 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
236 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
8535ab12
AG
237}
238
5a4348d1 239uint32_t qemu_fdt_alloc_phandle(void *fdt)
3601b572 240{
4b1b1c89
AG
241 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) {
c1b71b0c
MA
248 phandle = qemu_opt_get_number(qemu_get_machine_opts(),
249 "phandle_start", 0);
4b1b1c89
AG
250 }
251
252 if (!phandle) {
253 /*
254 * None or invalid phandle given on the command line, so fall back to
255 * default starting point.
256 */
257 phandle = 0x8000;
258 }
3601b572
AG
259
260 return phandle++;
261}
262
5a4348d1 263int qemu_fdt_nop_node(void *fdt, const char *node_path)
d69a8e63 264{
ccbcfedd 265 int r;
d69a8e63 266
ccbcfedd
AG
267 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
268 if (r < 0) {
508e221f
LL
269 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
270 fdt_strerror(r));
ccbcfedd
AG
271 exit(1);
272 }
d69a8e63 273
ccbcfedd 274 return r;
d69a8e63 275}
80ad7816 276
5a4348d1 277int qemu_fdt_add_subnode(void *fdt, const char *name)
80ad7816 278{
80ad7816
AG
279 char *dupname = g_strdup(name);
280 char *basename = strrchr(dupname, '/');
281 int retval;
c640d088 282 int parent = 0;
80ad7816
AG
283
284 if (!basename) {
bff39b63 285 g_free(dupname);
80ad7816
AG
286 return -1;
287 }
288
289 basename[0] = '\0';
290 basename++;
291
c640d088
AG
292 if (dupname[0]) {
293 parent = findnode_nofail(fdt, dupname);
294 }
295
296 retval = fdt_add_subnode(fdt, parent, basename);
ccbcfedd 297 if (retval < 0) {
508e221f
LL
298 error_report("FDT: Failed to create subnode %s: %s", name,
299 fdt_strerror(retval));
ccbcfedd 300 exit(1);
80ad7816
AG
301 }
302
80ad7816
AG
303 g_free(dupname);
304 return retval;
305}
71193433 306
5a4348d1 307void qemu_fdt_dumpdtb(void *fdt, int size)
71193433 308{
2ff3de68 309 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
71193433 310
2ff3de68
MA
311 if (dumpdtb) {
312 /* Dump the dtb to a file and quit */
313 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
314 }
71193433 315}
97c38f8c 316
5a4348d1
PC
317int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
318 const char *node_path,
319 const char *property,
320 int numvalues,
321 uint64_t *values)
97c38f8c
PM
322{
323 uint32_t *propcells;
324 uint64_t value;
325 int cellnum, vnum, ncells;
326 uint32_t hival;
2bf9febc 327 int ret;
97c38f8c
PM
328
329 propcells = g_new0(uint32_t, numvalues * 2);
330
331 cellnum = 0;
332 for (vnum = 0; vnum < numvalues; vnum++) {
333 ncells = values[vnum * 2];
334 if (ncells != 1 && ncells != 2) {
2bf9febc
SF
335 ret = -1;
336 goto out;
97c38f8c
PM
337 }
338 value = values[vnum * 2 + 1];
339 hival = cpu_to_be32(value >> 32);
340 if (ncells > 1) {
341 propcells[cellnum++] = hival;
342 } else if (hival != 0) {
2bf9febc
SF
343 ret = -1;
344 goto out;
97c38f8c
PM
345 }
346 propcells[cellnum++] = cpu_to_be32(value);
347 }
348
2bf9febc
SF
349 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
350 cellnum * sizeof(uint32_t));
351out:
352 g_free(propcells);
353 return ret;
97c38f8c 354}