2 * Functions for working with the Flattened Device Tree data format
4 * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
5 * benh@kernel.crashing.org
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/initrd.h>
14 #include <linux/memblock.h>
15 #include <linux/module.h>
17 #include <linux/of_fdt.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/random.h>
23 #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
25 #include <asm/machdep.h>
26 #endif /* CONFIG_PPC */
30 char *of_fdt_get_string(struct boot_param_header *blob, u32 offset)
32 return ((char *)blob) +
33 be32_to_cpu(blob->off_dt_strings) + offset;
37 * of_fdt_get_property - Given a node in the given flat blob, return
40 void *of_fdt_get_property(struct boot_param_header *blob,
41 unsigned long node, const char *name,
44 unsigned long p = node;
47 u32 tag = be32_to_cpup((__be32 *)p);
54 if (tag != OF_DT_PROP)
57 sz = be32_to_cpup((__be32 *)p);
58 noff = be32_to_cpup((__be32 *)(p + 4));
60 if (be32_to_cpu(blob->version) < 0x10)
61 p = ALIGN(p, sz >= 8 ? 8 : 4);
63 nstr = of_fdt_get_string(blob, noff);
65 pr_warning("Can't find property index name !\n");
68 if (strcmp(name, nstr) == 0) {
79 * of_fdt_is_compatible - Return true if given node from the given blob has
80 * compat in its compatible list
81 * @blob: A device tree blob
83 * @compat: compatible string to compare with compatible list.
85 * On match, returns a non-zero value with smaller values returned for more
86 * specific compatible values.
88 int of_fdt_is_compatible(struct boot_param_header *blob,
89 unsigned long node, const char *compat)
92 unsigned long cplen, l, score = 0;
94 cp = of_fdt_get_property(blob, node, "compatible", &cplen);
99 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
110 * of_fdt_match - Return true if node matches a list of compatible values
112 int of_fdt_match(struct boot_param_header *blob, unsigned long node,
113 const char *const *compat)
115 unsigned int tmp, score = 0;
121 tmp = of_fdt_is_compatible(blob, node, *compat);
122 if (tmp && (score == 0 || (tmp < score)))
130 static void *unflatten_dt_alloc(void **mem, unsigned long size,
135 *mem = PTR_ALIGN(*mem, align);
143 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
144 * @blob: The parent device tree blob
145 * @mem: Memory chunk to use for allocating device nodes and properties
146 * @p: pointer to node in flat tree
147 * @dad: Parent struct device_node
148 * @allnextpp: pointer to ->allnext from last allocated device_node
149 * @fpsize: Size of the node path up at the current depth.
151 static void * unflatten_dt_node(struct boot_param_header *blob,
154 struct device_node *dad,
155 struct device_node ***allnextpp,
156 unsigned long fpsize)
158 struct device_node *np;
159 struct property *pp, **prev_pp = NULL;
162 unsigned int l, allocl;
166 tag = be32_to_cpup(*p);
167 if (tag != OF_DT_BEGIN_NODE) {
168 pr_err("Weird tag at start of node: %x\n", tag);
173 l = allocl = strlen(pathp) + 1;
174 *p = PTR_ALIGN(*p + l, 4);
176 /* version 0x10 has a more compact unit name here instead of the full
177 * path. we accumulate the full path size using "fpsize", we'll rebuild
178 * it later. We detect this because the first character of the name is
181 if ((*pathp) != '/') {
184 /* root node: special case. fpsize accounts for path
185 * plus terminating zero. root node only has '/', so
186 * fpsize should be 2, but we want to avoid the first
187 * level nodes to have two '/' so we use fpsize 1 here
194 /* account for '/' and path size minus terminal 0
202 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
203 __alignof__(struct device_node));
206 np->full_name = fn = ((char *)np) + sizeof(*np);
208 /* rebuild full path for new format */
209 if (dad && dad->parent) {
210 strcpy(fn, dad->full_name);
212 if ((strlen(fn) + l + 1) != allocl) {
213 pr_debug("%s: p: %d, l: %d, a: %d\n",
214 pathp, (int)strlen(fn),
222 memcpy(fn, pathp, l);
224 prev_pp = &np->properties;
226 *allnextpp = &np->allnext;
229 /* we temporarily use the next field as `last_child'*/
230 if (dad->next == NULL)
233 dad->next->sibling = np;
236 kref_init(&np->kref);
238 /* process properties */
243 tag = be32_to_cpup(*p);
244 if (tag == OF_DT_NOP) {
248 if (tag != OF_DT_PROP)
251 sz = be32_to_cpup(*p);
252 noff = be32_to_cpup(*p + 4);
254 if (be32_to_cpu(blob->version) < 0x10)
255 *p = PTR_ALIGN(*p, sz >= 8 ? 8 : 4);
257 pname = of_fdt_get_string(blob, noff);
259 pr_info("Can't find property name in list !\n");
262 if (strcmp(pname, "name") == 0)
264 l = strlen(pname) + 1;
265 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
266 __alignof__(struct property));
268 /* We accept flattened tree phandles either in
269 * ePAPR-style "phandle" properties, or the
270 * legacy "linux,phandle" properties. If both
271 * appear and have different values, things
272 * will get weird. Don't do that. */
273 if ((strcmp(pname, "phandle") == 0) ||
274 (strcmp(pname, "linux,phandle") == 0)) {
275 if (np->phandle == 0)
276 np->phandle = be32_to_cpup((__be32*)*p);
278 /* And we process the "ibm,phandle" property
279 * used in pSeries dynamic device tree
281 if (strcmp(pname, "ibm,phandle") == 0)
282 np->phandle = be32_to_cpup((__be32 *)*p);
289 *p = PTR_ALIGN((*p) + sz, 4);
291 /* with version 0x10 we may not have the name property, recreate
292 * it here from the unit name if absent
295 char *p1 = pathp, *ps = pathp, *pa = NULL;
308 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
309 __alignof__(struct property));
316 memcpy(pp->value, ps, sz - 1);
317 ((char *)pp->value)[sz - 1] = 0;
318 pr_debug("fixed up name for %s -> %s\n", pathp,
324 np->name = of_get_property(np, "name", NULL);
325 np->type = of_get_property(np, "device_type", NULL);
332 while (tag == OF_DT_BEGIN_NODE || tag == OF_DT_NOP) {
333 if (tag == OF_DT_NOP)
336 mem = unflatten_dt_node(blob, mem, p, np, allnextpp,
338 tag = be32_to_cpup(*p);
340 if (tag != OF_DT_END_NODE) {
341 pr_err("Weird tag at end of node: %x\n", tag);
349 * __unflatten_device_tree - create tree of device_nodes from flat blob
351 * unflattens a device-tree, creating the
352 * tree of struct device_node. It also fills the "name" and "type"
353 * pointers of the nodes so the normal device-tree walking functions
355 * @blob: The blob to expand
356 * @mynodes: The device_node tree created by the call
357 * @dt_alloc: An allocator that provides a virtual address to memory
358 * for the resulting tree
360 static void __unflatten_device_tree(struct boot_param_header *blob,
361 struct device_node **mynodes,
362 void * (*dt_alloc)(u64 size, u64 align))
366 struct device_node **allnextp = mynodes;
368 pr_debug(" -> unflatten_device_tree()\n");
371 pr_debug("No device tree pointer\n");
375 pr_debug("Unflattening device tree:\n");
376 pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
377 pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
378 pr_debug("version: %08x\n", be32_to_cpu(blob->version));
380 if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
381 pr_err("Invalid device tree blob header\n");
385 /* First pass, scan for size */
386 start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct);
387 size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
388 size = ALIGN(size, 4);
390 pr_debug(" size is %lx, allocating...\n", size);
392 /* Allocate memory for the expanded device tree */
393 mem = dt_alloc(size + 4, __alignof__(struct device_node));
394 memset(mem, 0, size);
396 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
398 pr_debug(" unflattening %p...\n", mem);
400 /* Second pass, do actual unflattening */
401 start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct);
402 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
403 if (be32_to_cpup(start) != OF_DT_END)
404 pr_warning("Weird tag at end of tree: %08x\n", be32_to_cpup(start));
405 if (be32_to_cpup(mem + size) != 0xdeadbeef)
406 pr_warning("End of tree marker overwritten: %08x\n",
407 be32_to_cpup(mem + size));
410 pr_debug(" <- unflatten_device_tree()\n");
413 static void *kernel_tree_alloc(u64 size, u64 align)
415 return kzalloc(size, GFP_KERNEL);
419 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
421 * unflattens the device-tree passed by the firmware, creating the
422 * tree of struct device_node. It also fills the "name" and "type"
423 * pointers of the nodes so the normal device-tree walking functions
426 void of_fdt_unflatten_tree(unsigned long *blob,
427 struct device_node **mynodes)
429 struct boot_param_header *device_tree =
430 (struct boot_param_header *)blob;
431 __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
433 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
435 /* Everything below here references initial_boot_params directly. */
436 int __initdata dt_root_addr_cells;
437 int __initdata dt_root_size_cells;
439 struct boot_param_header *initial_boot_params;
441 #ifdef CONFIG_OF_EARLY_FLATTREE
444 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
445 * @it: callback function
446 * @data: context data pointer
448 * This function is used to scan the flattened device-tree, it is
449 * used to extract the memory information at boot before we can
452 int __init of_scan_flat_dt(int (*it)(unsigned long node,
453 const char *uname, int depth,
457 unsigned long p = ((unsigned long)initial_boot_params) +
458 be32_to_cpu(initial_boot_params->off_dt_struct);
463 u32 tag = be32_to_cpup((__be32 *)p);
467 if (tag == OF_DT_END_NODE) {
471 if (tag == OF_DT_NOP)
473 if (tag == OF_DT_END)
475 if (tag == OF_DT_PROP) {
476 u32 sz = be32_to_cpup((__be32 *)p);
478 if (be32_to_cpu(initial_boot_params->version) < 0x10)
479 p = ALIGN(p, sz >= 8 ? 8 : 4);
484 if (tag != OF_DT_BEGIN_NODE) {
485 pr_err("Invalid tag %x in flat device tree!\n", tag);
490 p = ALIGN(p + strlen(pathp) + 1, 4);
492 pathp = kbasename(pathp);
493 rc = it(p, pathp, depth, data);
502 * of_get_flat_dt_root - find the root node in the flat blob
504 unsigned long __init of_get_flat_dt_root(void)
506 unsigned long p = ((unsigned long)initial_boot_params) +
507 be32_to_cpu(initial_boot_params->off_dt_struct);
509 while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
511 BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
513 return ALIGN(p + strlen((char *)p) + 1, 4);
517 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
519 * This function can be used within scan_flattened_dt callback to get
520 * access to properties
522 void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
525 return of_fdt_get_property(initial_boot_params, node, name, size);
529 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
530 * @node: node to test
531 * @compat: compatible string to compare with compatible list.
533 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
535 return of_fdt_is_compatible(initial_boot_params, node, compat);
539 * of_flat_dt_match - Return true if node matches a list of compatible values
541 int __init of_flat_dt_match(unsigned long node, const char *const *compat)
543 return of_fdt_match(initial_boot_params, node, compat);
546 struct fdt_scan_status {
551 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
556 * fdt_scan_node_by_path - iterator for of_scan_flat_dt_by_path function
558 static int __init fdt_scan_node_by_path(unsigned long node, const char *uname,
559 int depth, void *data)
561 struct fdt_scan_status *st = data;
564 * if scan at the requested fdt node has been completed,
565 * return -ENXIO to abort further scanning
567 if (depth <= st->depth)
570 /* requested fdt node has been found, so call iterator function */
572 return st->iterator(node, uname, depth, st->data);
574 /* check if scanning automata is entering next level of fdt nodes */
575 if (depth == st->depth + 1 &&
576 strncmp(st->name, uname, st->namelen) == 0 &&
577 uname[st->namelen] == 0) {
579 if (st->name[st->namelen] == 0) {
582 const char *next = st->name + st->namelen + 1;
584 st->namelen = strcspn(next, "/");
589 /* scan next fdt node */
594 * of_scan_flat_dt_by_path - scan flattened tree blob and call callback on each
595 * child of the given path.
596 * @path: path to start searching for children
597 * @it: callback function
598 * @data: context data pointer
600 * This function is used to scan the flattened device-tree starting from the
601 * node given by path. It is used to extract information (like reserved
602 * memory), which is required on ealy boot before we can unflatten the tree.
604 int __init of_scan_flat_dt_by_path(const char *path,
605 int (*it)(unsigned long node, const char *name, int depth, void *data),
608 struct fdt_scan_status st = {path, 0, -1, 0, it, data};
611 if (initial_boot_params)
612 ret = of_scan_flat_dt(fdt_scan_node_by_path, &st);
616 else if (ret == -ENXIO) /* scan has been completed */
622 #ifdef CONFIG_BLK_DEV_INITRD
624 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
625 * @node: reference to node containing initrd location ('chosen')
627 void __init early_init_dt_check_for_initrd(unsigned long node)
633 pr_debug("Looking for initrd properties... ");
635 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
638 start = of_read_number(prop, len/4);
640 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
643 end = of_read_number(prop, len/4);
645 early_init_dt_setup_initrd_arch(start, end);
646 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
647 (unsigned long long)start, (unsigned long long)end);
650 inline void early_init_dt_check_for_initrd(unsigned long node)
653 #endif /* CONFIG_BLK_DEV_INITRD */
656 * early_init_dt_scan_root - fetch the top level address and size cells
658 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
659 int depth, void *data)
666 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
667 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
669 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
671 dt_root_size_cells = be32_to_cpup(prop);
672 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
674 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
676 dt_root_addr_cells = be32_to_cpup(prop);
677 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
683 u64 __init dt_mem_next_cell(int s, __be32 **cellp)
688 return of_read_number(p, s);
692 * early_init_dt_scan_memory - Look for an parse memory nodes
694 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
695 int depth, void *data)
697 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
701 /* We are scanning "memory" nodes only */
704 * The longtrail doesn't have a device_type on the
705 * /memory node, so look for the node called /memory@0.
707 if (depth != 1 || strcmp(uname, "memory@0") != 0)
709 } else if (strcmp(type, "memory") != 0)
712 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
714 reg = of_get_flat_dt_prop(node, "reg", &l);
718 endp = reg + (l / sizeof(__be32));
720 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
721 uname, l, reg[0], reg[1], reg[2], reg[3]);
723 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
726 base = dt_mem_next_cell(dt_root_addr_cells, ®);
727 size = dt_mem_next_cell(dt_root_size_cells, ®);
731 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
732 (unsigned long long)size);
734 early_init_dt_add_memory_arch(base, size);
740 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
741 int depth, void *data)
746 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
748 if (depth != 1 || !data ||
749 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
752 early_init_dt_check_for_initrd(node);
754 /* Retrieve command line */
755 p = of_get_flat_dt_prop(node, "bootargs", &l);
756 if (p != NULL && l > 0)
757 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
760 * CONFIG_CMDLINE is meant to be a default in case nothing else
761 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
762 * is set in which case we override whatever was found earlier.
764 #ifdef CONFIG_CMDLINE
765 #ifndef CONFIG_CMDLINE_FORCE
766 if (!((char *)data)[0])
768 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
769 #endif /* CONFIG_CMDLINE */
771 pr_debug("Command line is: %s\n", (char*)data);
777 #ifdef CONFIG_HAVE_MEMBLOCK
779 * called from unflatten_device_tree() to bootstrap devicetree itself
780 * Architectures can override this definition if memblock isn't used
782 void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
784 return __va(memblock_alloc(size, align));
789 * unflatten_device_tree - create tree of device_nodes from flat blob
791 * unflattens the device-tree passed by the firmware, creating the
792 * tree of struct device_node. It also fills the "name" and "type"
793 * pointers of the nodes so the normal device-tree walking functions
796 void __init unflatten_device_tree(void)
798 __unflatten_device_tree(initial_boot_params, &of_allnodes,
799 early_init_dt_alloc_memory_arch);
801 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
802 of_alias_scan(early_init_dt_alloc_memory_arch);
805 #endif /* CONFIG_OF_EARLY_FLATTREE */
807 /* Feed entire flattened device tree into the random pool */
808 static int __init add_fdt_randomness(void)
810 if (initial_boot_params)
811 add_device_randomness(initial_boot_params,
812 be32_to_cpu(initial_boot_params->totalsize));
816 core_initcall(add_fdt_randomness);