target/i386: introduce function to set rounding mode from FPCW or MXCSR bits
[qemu.git] / softmmu / device_tree.c
blobce74f3d48d7c7d6ef62cb8091c4fc6ffe0ece3b6
1 /*
2 * Functions to help device tree manipulation using libfdt.
3 * It also provides functions to read entries from device tree proc
4 * interface.
6 * Copyright 2008 IBM Corporation.
7 * Authors: Jerone Young <jyoung5@us.ibm.com>
8 * Hollis Blanchard <hollisb@us.ibm.com>
10 * This work is licensed under the GNU GPL license version 2 or later.
14 #include "qemu/osdep.h"
16 #ifdef CONFIG_LINUX
17 #include <dirent.h>
18 #endif
20 #include "qapi/error.h"
21 #include "qemu/error-report.h"
22 #include "qemu/option.h"
23 #include "qemu/bswap.h"
24 #include "qemu/cutils.h"
25 #include "sysemu/device_tree.h"
26 #include "hw/loader.h"
27 #include "hw/boards.h"
28 #include "qemu/config-file.h"
29 #include "qapi/qapi-commands-machine.h"
30 #include "qapi/qmp/qdict.h"
31 #include "monitor/hmp.h"
33 #include <libfdt.h>
35 #define FDT_MAX_SIZE 0x100000
37 void *create_device_tree(int *sizep)
39 void *fdt;
40 int ret;
42 *sizep = FDT_MAX_SIZE;
43 fdt = g_malloc0(FDT_MAX_SIZE);
44 ret = fdt_create(fdt, FDT_MAX_SIZE);
45 if (ret < 0) {
46 goto fail;
48 ret = fdt_finish_reservemap(fdt);
49 if (ret < 0) {
50 goto fail;
52 ret = fdt_begin_node(fdt, "");
53 if (ret < 0) {
54 goto fail;
56 ret = fdt_end_node(fdt);
57 if (ret < 0) {
58 goto fail;
60 ret = fdt_finish(fdt);
61 if (ret < 0) {
62 goto fail;
64 ret = fdt_open_into(fdt, fdt, *sizep);
65 if (ret) {
66 error_report("%s: Unable to copy device tree into memory: %s",
67 __func__, fdt_strerror(ret));
68 exit(1);
71 return fdt;
72 fail:
73 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
74 exit(1);
77 void *load_device_tree(const char *filename_path, int *sizep)
79 int dt_size;
80 int dt_file_load_size;
81 int ret;
82 void *fdt = NULL;
84 *sizep = 0;
85 dt_size = get_image_size(filename_path);
86 if (dt_size < 0) {
87 error_report("Unable to get size of device tree file '%s'",
88 filename_path);
89 goto fail;
91 if (dt_size > INT_MAX / 2 - 10000) {
92 error_report("Device tree file '%s' is too large", filename_path);
93 goto fail;
96 /* Expand to 2x size to give enough room for manipulation. */
97 dt_size += 10000;
98 dt_size *= 2;
99 /* First allocate space in qemu for device tree */
100 fdt = g_malloc0(dt_size);
102 dt_file_load_size = load_image_size(filename_path, fdt, dt_size);
103 if (dt_file_load_size < 0) {
104 error_report("Unable to open device tree file '%s'",
105 filename_path);
106 goto fail;
109 ret = fdt_open_into(fdt, fdt, dt_size);
110 if (ret) {
111 error_report("%s: Unable to copy device tree into memory: %s",
112 __func__, fdt_strerror(ret));
113 goto fail;
116 /* Check sanity of device tree */
117 if (fdt_check_header(fdt)) {
118 error_report("Device tree file loaded into memory is invalid: %s",
119 filename_path);
120 goto fail;
122 *sizep = dt_size;
123 return fdt;
125 fail:
126 g_free(fdt);
127 return NULL;
130 #ifdef CONFIG_LINUX
132 #define SYSFS_DT_BASEDIR "/proc/device-tree"
135 * read_fstree: this function is inspired from dtc read_fstree
136 * @fdt: preallocated fdt blob buffer, to be populated
137 * @dirname: directory to scan under SYSFS_DT_BASEDIR
138 * the search is recursive and the tree is searched down to the
139 * leaves (property files).
141 * the function asserts in case of error
143 static void read_fstree(void *fdt, const char *dirname)
145 DIR *d;
146 struct dirent *de;
147 struct stat st;
148 const char *root_dir = SYSFS_DT_BASEDIR;
149 const char *parent_node;
151 if (strstr(dirname, root_dir) != dirname) {
152 error_report("%s: %s must be searched within %s",
153 __func__, dirname, root_dir);
154 exit(1);
156 parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)];
158 d = opendir(dirname);
159 if (!d) {
160 error_report("%s cannot open %s", __func__, dirname);
161 exit(1);
164 while ((de = readdir(d)) != NULL) {
165 char *tmpnam;
167 if (!g_strcmp0(de->d_name, ".")
168 || !g_strcmp0(de->d_name, "..")) {
169 continue;
172 tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name);
174 if (lstat(tmpnam, &st) < 0) {
175 error_report("%s cannot lstat %s", __func__, tmpnam);
176 exit(1);
179 if (S_ISREG(st.st_mode)) {
180 gchar *val;
181 gsize len;
183 if (!g_file_get_contents(tmpnam, &val, &len, NULL)) {
184 error_report("%s not able to extract info from %s",
185 __func__, tmpnam);
186 exit(1);
189 if (strlen(parent_node) > 0) {
190 qemu_fdt_setprop(fdt, parent_node,
191 de->d_name, val, len);
192 } else {
193 qemu_fdt_setprop(fdt, "/", de->d_name, val, len);
195 g_free(val);
196 } else if (S_ISDIR(st.st_mode)) {
197 char *node_name;
199 node_name = g_strdup_printf("%s/%s",
200 parent_node, de->d_name);
201 qemu_fdt_add_subnode(fdt, node_name);
202 g_free(node_name);
203 read_fstree(fdt, tmpnam);
206 g_free(tmpnam);
209 closedir(d);
212 /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
213 void *load_device_tree_from_sysfs(void)
215 void *host_fdt;
216 int host_fdt_size;
218 host_fdt = create_device_tree(&host_fdt_size);
219 read_fstree(host_fdt, SYSFS_DT_BASEDIR);
220 if (fdt_check_header(host_fdt)) {
221 error_report("%s host device tree extracted into memory is invalid",
222 __func__);
223 exit(1);
225 return host_fdt;
228 #endif /* CONFIG_LINUX */
230 static int findnode_nofail(void *fdt, const char *node_path)
232 int offset;
234 offset = fdt_path_offset(fdt, node_path);
235 if (offset < 0) {
236 error_report("%s Couldn't find node %s: %s", __func__, node_path,
237 fdt_strerror(offset));
238 exit(1);
241 return offset;
244 char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp)
246 char *prefix = g_strdup_printf("%s@", name);
247 unsigned int path_len = 16, n = 0;
248 GSList *path_list = NULL, *iter;
249 const char *iter_name;
250 int offset, len, ret;
251 char **path_array;
253 offset = fdt_next_node(fdt, -1, NULL);
255 while (offset >= 0) {
256 iter_name = fdt_get_name(fdt, offset, &len);
257 if (!iter_name) {
258 offset = len;
259 break;
261 if (!strcmp(iter_name, name) || g_str_has_prefix(iter_name, prefix)) {
262 char *path;
264 path = g_malloc(path_len);
265 while ((ret = fdt_get_path(fdt, offset, path, path_len))
266 == -FDT_ERR_NOSPACE) {
267 path_len += 16;
268 path = g_realloc(path, path_len);
270 path_list = g_slist_prepend(path_list, path);
271 n++;
273 offset = fdt_next_node(fdt, offset, NULL);
275 g_free(prefix);
277 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
278 error_setg(errp, "%s: abort parsing dt for %s node units: %s",
279 __func__, name, fdt_strerror(offset));
280 for (iter = path_list; iter; iter = iter->next) {
281 g_free(iter->data);
283 g_slist_free(path_list);
284 return NULL;
287 path_array = g_new(char *, n + 1);
288 path_array[n--] = NULL;
290 for (iter = path_list; iter; iter = iter->next) {
291 path_array[n--] = iter->data;
294 g_slist_free(path_list);
296 return path_array;
299 char **qemu_fdt_node_path(void *fdt, const char *name, const char *compat,
300 Error **errp)
302 int offset, len, ret;
303 const char *iter_name;
304 unsigned int path_len = 16, n = 0;
305 GSList *path_list = NULL, *iter;
306 char **path_array;
308 offset = fdt_node_offset_by_compatible(fdt, -1, compat);
310 while (offset >= 0) {
311 iter_name = fdt_get_name(fdt, offset, &len);
312 if (!iter_name) {
313 offset = len;
314 break;
316 if (!name || !strcmp(iter_name, name)) {
317 char *path;
319 path = g_malloc(path_len);
320 while ((ret = fdt_get_path(fdt, offset, path, path_len))
321 == -FDT_ERR_NOSPACE) {
322 path_len += 16;
323 path = g_realloc(path, path_len);
325 path_list = g_slist_prepend(path_list, path);
326 n++;
328 offset = fdt_node_offset_by_compatible(fdt, offset, compat);
331 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
332 error_setg(errp, "%s: abort parsing dt for %s/%s: %s",
333 __func__, name, compat, fdt_strerror(offset));
334 for (iter = path_list; iter; iter = iter->next) {
335 g_free(iter->data);
337 g_slist_free(path_list);
338 return NULL;
341 path_array = g_new(char *, n + 1);
342 path_array[n--] = NULL;
344 for (iter = path_list; iter; iter = iter->next) {
345 path_array[n--] = iter->data;
348 g_slist_free(path_list);
350 return path_array;
353 int qemu_fdt_setprop(void *fdt, const char *node_path,
354 const char *property, const void *val, int size)
356 int r;
358 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
359 if (r < 0) {
360 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
361 property, fdt_strerror(r));
362 exit(1);
365 return r;
368 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
369 const char *property, uint32_t val)
371 int r;
373 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
374 if (r < 0) {
375 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
376 node_path, property, val, fdt_strerror(r));
377 exit(1);
380 return r;
383 int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
384 const char *property, uint64_t val)
386 val = cpu_to_be64(val);
387 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
390 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
391 const char *property, const char *string)
393 int r;
395 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
396 if (r < 0) {
397 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
398 node_path, property, string, fdt_strerror(r));
399 exit(1);
402 return r;
406 * libfdt doesn't allow us to add string arrays directly but they are
407 * test a series of null terminated strings with a length. We build
408 * the string up here so we can calculate the final length.
410 int qemu_fdt_setprop_string_array(void *fdt, const char *node_path,
411 const char *prop, char **array, int len)
413 int ret, i, total_len = 0;
414 char *str, *p;
415 for (i = 0; i < len; i++) {
416 total_len += strlen(array[i]) + 1;
418 p = str = g_malloc0(total_len);
419 for (i = 0; i < len; i++) {
420 int len = strlen(array[i]) + 1;
421 pstrcpy(p, len, array[i]);
422 p += len;
425 ret = qemu_fdt_setprop(fdt, node_path, prop, str, total_len);
426 g_free(str);
427 return ret;
430 const void *qemu_fdt_getprop(void *fdt, const char *node_path,
431 const char *property, int *lenp, Error **errp)
433 int len;
434 const void *r;
436 if (!lenp) {
437 lenp = &len;
439 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
440 if (!r) {
441 error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__,
442 node_path, property, fdt_strerror(*lenp));
444 return r;
447 uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
448 const char *property, int *lenp, Error **errp)
450 int len;
451 const uint32_t *p;
453 if (!lenp) {
454 lenp = &len;
456 p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp);
457 if (!p) {
458 return 0;
459 } else if (*lenp != 4) {
460 error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)",
461 __func__, node_path, property);
462 *lenp = -EINVAL;
463 return 0;
465 return be32_to_cpu(*p);
468 uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
470 uint32_t r;
472 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
473 if (r == 0) {
474 error_report("%s: Couldn't get phandle for %s: %s", __func__,
475 path, fdt_strerror(r));
476 exit(1);
479 return r;
482 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
483 const char *property,
484 const char *target_node_path)
486 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
487 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
490 uint32_t qemu_fdt_alloc_phandle(void *fdt)
492 static int phandle = 0x0;
495 * We need to find out if the user gave us special instruction at
496 * which phandle id to start allocating phandles.
498 if (!phandle) {
499 phandle = machine_phandle_start(current_machine);
502 if (!phandle) {
504 * None or invalid phandle given on the command line, so fall back to
505 * default starting point.
507 phandle = 0x8000;
510 return phandle++;
513 int qemu_fdt_nop_node(void *fdt, const char *node_path)
515 int r;
517 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
518 if (r < 0) {
519 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
520 fdt_strerror(r));
521 exit(1);
524 return r;
527 int qemu_fdt_add_subnode(void *fdt, const char *name)
529 char *dupname = g_strdup(name);
530 char *basename = strrchr(dupname, '/');
531 int retval;
532 int parent = 0;
534 if (!basename) {
535 g_free(dupname);
536 return -1;
539 basename[0] = '\0';
540 basename++;
542 if (dupname[0]) {
543 parent = findnode_nofail(fdt, dupname);
546 retval = fdt_add_subnode(fdt, parent, basename);
547 if (retval < 0) {
548 error_report("%s: Failed to create subnode %s: %s",
549 __func__, name, fdt_strerror(retval));
550 exit(1);
553 g_free(dupname);
554 return retval;
558 * qemu_fdt_add_path: Like qemu_fdt_add_subnode(), but will add
559 * all missing subnodes from the given path.
561 int qemu_fdt_add_path(void *fdt, const char *path)
563 const char *name;
564 int namelen, retval;
565 int parent = 0;
567 if (path[0] != '/') {
568 return -1;
571 do {
572 name = path + 1;
573 path = strchr(name, '/');
574 namelen = path != NULL ? path - name : strlen(name);
576 retval = fdt_subnode_offset_namelen(fdt, parent, name, namelen);
577 if (retval < 0 && retval != -FDT_ERR_NOTFOUND) {
578 error_report("%s: Unexpected error in finding subnode %.*s: %s",
579 __func__, namelen, name, fdt_strerror(retval));
580 exit(1);
581 } else if (retval == -FDT_ERR_NOTFOUND) {
582 retval = fdt_add_subnode_namelen(fdt, parent, name, namelen);
583 if (retval < 0) {
584 error_report("%s: Failed to create subnode %.*s: %s",
585 __func__, namelen, name, fdt_strerror(retval));
586 exit(1);
590 parent = retval;
591 } while (path);
593 return retval;
596 void qemu_fdt_dumpdtb(void *fdt, int size)
598 const char *dumpdtb = current_machine->dumpdtb;
600 if (dumpdtb) {
601 /* Dump the dtb to a file and quit */
602 if (g_file_set_contents(dumpdtb, fdt, size, NULL)) {
603 info_report("dtb dumped to %s. Exiting.", dumpdtb);
604 exit(0);
606 error_report("%s: Failed dumping dtb to %s", __func__, dumpdtb);
607 exit(1);
611 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
612 const char *node_path,
613 const char *property,
614 int numvalues,
615 uint64_t *values)
617 uint32_t *propcells;
618 uint64_t value;
619 int cellnum, vnum, ncells;
620 uint32_t hival;
621 int ret;
623 propcells = g_new0(uint32_t, numvalues * 2);
625 cellnum = 0;
626 for (vnum = 0; vnum < numvalues; vnum++) {
627 ncells = values[vnum * 2];
628 if (ncells != 1 && ncells != 2) {
629 ret = -1;
630 goto out;
632 value = values[vnum * 2 + 1];
633 hival = cpu_to_be32(value >> 32);
634 if (ncells > 1) {
635 propcells[cellnum++] = hival;
636 } else if (hival != 0) {
637 ret = -1;
638 goto out;
640 propcells[cellnum++] = cpu_to_be32(value);
643 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
644 cellnum * sizeof(uint32_t));
645 out:
646 g_free(propcells);
647 return ret;
650 void qmp_dumpdtb(const char *filename, Error **errp)
652 g_autoptr(GError) err = NULL;
653 uint32_t size;
655 if (!current_machine->fdt) {
656 error_setg(errp, "This machine doesn't have a FDT");
657 return;
660 size = fdt_totalsize(current_machine->fdt);
662 g_assert(size > 0);
664 if (!g_file_set_contents(filename, current_machine->fdt, size, &err)) {
665 error_setg(errp, "Error saving FDT to file %s: %s",
666 filename, err->message);
670 void hmp_dumpdtb(Monitor *mon, const QDict *qdict)
672 const char *filename = qdict_get_str(qdict, "filename");
673 Error *local_err = NULL;
675 qmp_dumpdtb(filename, &local_err);
677 if (hmp_handle_error(mon, local_err)) {
678 return;
681 info_report("dtb dumped to %s", filename);