nvme: use TYPE_NVME instead of constant string
[qemu/ar7.git] / device_tree.c
blob296278e12ae01020e7c0b8564587a7fa4d2dcf16
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 "sysemu/device_tree.h"
25 #include "sysemu/sysemu.h"
26 #include "hw/loader.h"
27 #include "hw/boards.h"
28 #include "qemu/config-file.h"
30 #include <libfdt.h>
32 #define FDT_MAX_SIZE 0x100000
34 void *create_device_tree(int *sizep)
36 void *fdt;
37 int ret;
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;
45 ret = fdt_finish_reservemap(fdt);
46 if (ret < 0) {
47 goto fail;
49 ret = fdt_begin_node(fdt, "");
50 if (ret < 0) {
51 goto fail;
53 ret = fdt_end_node(fdt);
54 if (ret < 0) {
55 goto fail;
57 ret = fdt_finish(fdt);
58 if (ret < 0) {
59 goto fail;
61 ret = fdt_open_into(fdt, fdt, *sizep);
62 if (ret) {
63 error_report("Unable to copy device tree in memory");
64 exit(1);
67 return fdt;
68 fail:
69 error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
70 exit(1);
73 void *load_device_tree(const char *filename_path, int *sizep)
75 int dt_size;
76 int dt_file_load_size;
77 int ret;
78 void *fdt = NULL;
80 *sizep = 0;
81 dt_size = get_image_size(filename_path);
82 if (dt_size < 0) {
83 error_report("Unable to get size of device tree file '%s'",
84 filename_path);
85 goto fail;
88 /* Expand to 2x size to give enough room for manipulation. */
89 dt_size += 10000;
90 dt_size *= 2;
91 /* First allocate space in qemu for device tree */
92 fdt = g_malloc0(dt_size);
94 dt_file_load_size = load_image_size(filename_path, fdt, dt_size);
95 if (dt_file_load_size < 0) {
96 error_report("Unable to open device tree file '%s'",
97 filename_path);
98 goto fail;
101 ret = fdt_open_into(fdt, fdt, dt_size);
102 if (ret) {
103 error_report("Unable to copy device tree in memory");
104 goto fail;
107 /* Check sanity of device tree */
108 if (fdt_check_header(fdt)) {
109 error_report("Device tree file loaded into memory is invalid: %s",
110 filename_path);
111 goto fail;
113 *sizep = dt_size;
114 return fdt;
116 fail:
117 g_free(fdt);
118 return NULL;
121 #ifdef CONFIG_LINUX
123 #define SYSFS_DT_BASEDIR "/proc/device-tree"
126 * read_fstree: this function is inspired from dtc read_fstree
127 * @fdt: preallocated fdt blob buffer, to be populated
128 * @dirname: directory to scan under SYSFS_DT_BASEDIR
129 * the search is recursive and the tree is searched down to the
130 * leaves (property files).
132 * the function asserts in case of error
134 static void read_fstree(void *fdt, const char *dirname)
136 DIR *d;
137 struct dirent *de;
138 struct stat st;
139 const char *root_dir = SYSFS_DT_BASEDIR;
140 const char *parent_node;
142 if (strstr(dirname, root_dir) != dirname) {
143 error_report("%s: %s must be searched within %s",
144 __func__, dirname, root_dir);
145 exit(1);
147 parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)];
149 d = opendir(dirname);
150 if (!d) {
151 error_report("%s cannot open %s", __func__, dirname);
152 exit(1);
155 while ((de = readdir(d)) != NULL) {
156 char *tmpnam;
158 if (!g_strcmp0(de->d_name, ".")
159 || !g_strcmp0(de->d_name, "..")) {
160 continue;
163 tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name);
165 if (lstat(tmpnam, &st) < 0) {
166 error_report("%s cannot lstat %s", __func__, tmpnam);
167 exit(1);
170 if (S_ISREG(st.st_mode)) {
171 gchar *val;
172 gsize len;
174 if (!g_file_get_contents(tmpnam, &val, &len, NULL)) {
175 error_report("%s not able to extract info from %s",
176 __func__, tmpnam);
177 exit(1);
180 if (strlen(parent_node) > 0) {
181 qemu_fdt_setprop(fdt, parent_node,
182 de->d_name, val, len);
183 } else {
184 qemu_fdt_setprop(fdt, "/", de->d_name, val, len);
186 g_free(val);
187 } else if (S_ISDIR(st.st_mode)) {
188 char *node_name;
190 node_name = g_strdup_printf("%s/%s",
191 parent_node, de->d_name);
192 qemu_fdt_add_subnode(fdt, node_name);
193 g_free(node_name);
194 read_fstree(fdt, tmpnam);
197 g_free(tmpnam);
200 closedir(d);
203 /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
204 void *load_device_tree_from_sysfs(void)
206 void *host_fdt;
207 int host_fdt_size;
209 host_fdt = create_device_tree(&host_fdt_size);
210 read_fstree(host_fdt, SYSFS_DT_BASEDIR);
211 if (fdt_check_header(host_fdt)) {
212 error_report("%s host device tree extracted into memory is invalid",
213 __func__);
214 exit(1);
216 return host_fdt;
219 #endif /* CONFIG_LINUX */
221 static int findnode_nofail(void *fdt, const char *node_path)
223 int offset;
225 offset = fdt_path_offset(fdt, node_path);
226 if (offset < 0) {
227 error_report("%s Couldn't find node %s: %s", __func__, node_path,
228 fdt_strerror(offset));
229 exit(1);
232 return offset;
235 char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp)
237 char *prefix = g_strdup_printf("%s@", name);
238 unsigned int path_len = 16, n = 0;
239 GSList *path_list = NULL, *iter;
240 const char *iter_name;
241 int offset, len, ret;
242 char **path_array;
244 offset = fdt_next_node(fdt, -1, NULL);
246 while (offset >= 0) {
247 iter_name = fdt_get_name(fdt, offset, &len);
248 if (!iter_name) {
249 offset = len;
250 break;
252 if (!strcmp(iter_name, name) || g_str_has_prefix(iter_name, prefix)) {
253 char *path;
255 path = g_malloc(path_len);
256 while ((ret = fdt_get_path(fdt, offset, path, path_len))
257 == -FDT_ERR_NOSPACE) {
258 path_len += 16;
259 path = g_realloc(path, path_len);
261 path_list = g_slist_prepend(path_list, path);
262 n++;
264 offset = fdt_next_node(fdt, offset, NULL);
266 g_free(prefix);
268 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
269 error_setg(errp, "%s: abort parsing dt for %s node units: %s",
270 __func__, name, fdt_strerror(offset));
271 for (iter = path_list; iter; iter = iter->next) {
272 g_free(iter->data);
274 g_slist_free(path_list);
275 return NULL;
278 path_array = g_new(char *, n + 1);
279 path_array[n--] = NULL;
281 for (iter = path_list; iter; iter = iter->next) {
282 path_array[n--] = iter->data;
285 g_slist_free(path_list);
287 return path_array;
290 char **qemu_fdt_node_path(void *fdt, const char *name, char *compat,
291 Error **errp)
293 int offset, len, ret;
294 const char *iter_name;
295 unsigned int path_len = 16, n = 0;
296 GSList *path_list = NULL, *iter;
297 char **path_array;
299 offset = fdt_node_offset_by_compatible(fdt, -1, compat);
301 while (offset >= 0) {
302 iter_name = fdt_get_name(fdt, offset, &len);
303 if (!iter_name) {
304 offset = len;
305 break;
307 if (!strcmp(iter_name, name)) {
308 char *path;
310 path = g_malloc(path_len);
311 while ((ret = fdt_get_path(fdt, offset, path, path_len))
312 == -FDT_ERR_NOSPACE) {
313 path_len += 16;
314 path = g_realloc(path, path_len);
316 path_list = g_slist_prepend(path_list, path);
317 n++;
319 offset = fdt_node_offset_by_compatible(fdt, offset, compat);
322 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
323 error_setg(errp, "%s: abort parsing dt for %s/%s: %s",
324 __func__, name, compat, fdt_strerror(offset));
325 for (iter = path_list; iter; iter = iter->next) {
326 g_free(iter->data);
328 g_slist_free(path_list);
329 return NULL;
332 path_array = g_new(char *, n + 1);
333 path_array[n--] = NULL;
335 for (iter = path_list; iter; iter = iter->next) {
336 path_array[n--] = iter->data;
339 g_slist_free(path_list);
341 return path_array;
344 int qemu_fdt_setprop(void *fdt, const char *node_path,
345 const char *property, const void *val, int size)
347 int r;
349 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
350 if (r < 0) {
351 error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
352 property, fdt_strerror(r));
353 exit(1);
356 return r;
359 int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
360 const char *property, uint32_t val)
362 int r;
364 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
365 if (r < 0) {
366 error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
367 node_path, property, val, fdt_strerror(r));
368 exit(1);
371 return r;
374 int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
375 const char *property, uint64_t val)
377 val = cpu_to_be64(val);
378 return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
381 int qemu_fdt_setprop_string(void *fdt, const char *node_path,
382 const char *property, const char *string)
384 int r;
386 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
387 if (r < 0) {
388 error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
389 node_path, property, string, fdt_strerror(r));
390 exit(1);
393 return r;
396 const void *qemu_fdt_getprop(void *fdt, const char *node_path,
397 const char *property, int *lenp, Error **errp)
399 int len;
400 const void *r;
402 if (!lenp) {
403 lenp = &len;
405 r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
406 if (!r) {
407 error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__,
408 node_path, property, fdt_strerror(*lenp));
410 return r;
413 uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
414 const char *property, int *lenp, Error **errp)
416 int len;
417 const uint32_t *p;
419 if (!lenp) {
420 lenp = &len;
422 p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp);
423 if (!p) {
424 return 0;
425 } else if (*lenp != 4) {
426 error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)",
427 __func__, node_path, property);
428 *lenp = -EINVAL;
429 return 0;
431 return be32_to_cpu(*p);
434 uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
436 uint32_t r;
438 r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
439 if (r == 0) {
440 error_report("%s: Couldn't get phandle for %s: %s", __func__,
441 path, fdt_strerror(r));
442 exit(1);
445 return r;
448 int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
449 const char *property,
450 const char *target_node_path)
452 uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
453 return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
456 uint32_t qemu_fdt_alloc_phandle(void *fdt)
458 static int phandle = 0x0;
461 * We need to find out if the user gave us special instruction at
462 * which phandle id to start allocating phandles.
464 if (!phandle) {
465 phandle = machine_phandle_start(current_machine);
468 if (!phandle) {
470 * None or invalid phandle given on the command line, so fall back to
471 * default starting point.
473 phandle = 0x8000;
476 return phandle++;
479 int qemu_fdt_nop_node(void *fdt, const char *node_path)
481 int r;
483 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
484 if (r < 0) {
485 error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
486 fdt_strerror(r));
487 exit(1);
490 return r;
493 int qemu_fdt_add_subnode(void *fdt, const char *name)
495 char *dupname = g_strdup(name);
496 char *basename = strrchr(dupname, '/');
497 int retval;
498 int parent = 0;
500 if (!basename) {
501 g_free(dupname);
502 return -1;
505 basename[0] = '\0';
506 basename++;
508 if (dupname[0]) {
509 parent = findnode_nofail(fdt, dupname);
512 retval = fdt_add_subnode(fdt, parent, basename);
513 if (retval < 0) {
514 error_report("FDT: Failed to create subnode %s: %s", name,
515 fdt_strerror(retval));
516 exit(1);
519 g_free(dupname);
520 return retval;
523 void qemu_fdt_dumpdtb(void *fdt, int size)
525 const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
527 if (dumpdtb) {
528 /* Dump the dtb to a file and quit */
529 exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
533 int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
534 const char *node_path,
535 const char *property,
536 int numvalues,
537 uint64_t *values)
539 uint32_t *propcells;
540 uint64_t value;
541 int cellnum, vnum, ncells;
542 uint32_t hival;
543 int ret;
545 propcells = g_new0(uint32_t, numvalues * 2);
547 cellnum = 0;
548 for (vnum = 0; vnum < numvalues; vnum++) {
549 ncells = values[vnum * 2];
550 if (ncells != 1 && ncells != 2) {
551 ret = -1;
552 goto out;
554 value = values[vnum * 2 + 1];
555 hival = cpu_to_be32(value >> 32);
556 if (ncells > 1) {
557 propcells[cellnum++] = hival;
558 } else if (hival != 0) {
559 ret = -1;
560 goto out;
562 propcells[cellnum++] = cpu_to_be32(value);
565 ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
566 cellnum * sizeof(uint32_t));
567 out:
568 g_free(propcells);
569 return ret;