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/module.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_reserved_mem.h>
18 #include <linux/sizes.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/memblock.h>
23 #include <linux/libfdt.h>
25 #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
27 #include <asm/machdep.h>
28 #endif /* CONFIG_PPC */
33 * of_fdt_is_compatible - Return true if given node from the given blob has
34 * compat in its compatible list
35 * @blob: A device tree blob
37 * @compat: compatible string to compare with compatible list.
39 * On match, returns a non-zero value with smaller values returned for more
40 * specific compatible values.
42 int of_fdt_is_compatible(struct boot_param_header *blob,
43 unsigned long node, const char *compat)
47 unsigned long l, score = 0;
49 cp = fdt_getprop(blob, node, "compatible", &cplen);
54 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
65 * of_fdt_match - Return true if node matches a list of compatible values
67 int of_fdt_match(struct boot_param_header *blob, unsigned long node,
68 const char *const *compat)
70 unsigned int tmp, score = 0;
76 tmp = of_fdt_is_compatible(blob, node, *compat);
77 if (tmp && (score == 0 || (tmp < score)))
85 static void *unflatten_dt_alloc(void **mem, unsigned long size,
90 *mem = PTR_ALIGN(*mem, align);
98 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
99 * @blob: The parent device tree blob
100 * @mem: Memory chunk to use for allocating device nodes and properties
101 * @p: pointer to node in flat tree
102 * @dad: Parent struct device_node
103 * @allnextpp: pointer to ->allnext from last allocated device_node
104 * @fpsize: Size of the node path up at the current depth.
106 static void * unflatten_dt_node(struct boot_param_header *blob,
109 struct device_node *dad,
110 struct device_node ***allnextpp,
111 unsigned long fpsize)
114 struct device_node *np;
115 struct property *pp, **prev_pp = NULL;
117 unsigned int l, allocl;
118 static int depth = 0;
124 pathp = fdt_get_name(blob, *poffset, &l);
130 /* version 0x10 has a more compact unit name here instead of the full
131 * path. we accumulate the full path size using "fpsize", we'll rebuild
132 * it later. We detect this because the first character of the name is
135 if ((*pathp) != '/') {
138 /* root node: special case. fpsize accounts for path
139 * plus terminating zero. root node only has '/', so
140 * fpsize should be 2, but we want to avoid the first
141 * level nodes to have two '/' so we use fpsize 1 here
148 /* account for '/' and path size minus terminal 0
156 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
157 __alignof__(struct device_node));
160 memset(np, 0, sizeof(*np));
161 np->full_name = fn = ((char *)np) + sizeof(*np);
163 /* rebuild full path for new format */
164 if (dad && dad->parent) {
165 strcpy(fn, dad->full_name);
167 if ((strlen(fn) + l + 1) != allocl) {
168 pr_debug("%s: p: %d, l: %d, a: %d\n",
169 pathp, (int)strlen(fn),
177 memcpy(fn, pathp, l);
179 prev_pp = &np->properties;
181 *allnextpp = &np->allnext;
184 /* we temporarily use the next field as `last_child'*/
185 if (dad->next == NULL)
188 dad->next->sibling = np;
191 kref_init(&np->kref);
193 /* process properties */
194 for (offset = fdt_first_property_offset(blob, *poffset);
196 (offset = fdt_next_property_offset(blob, offset))) {
200 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
201 offset = -FDT_ERR_INTERNAL;
206 pr_info("Can't find property name in list !\n");
209 if (strcmp(pname, "name") == 0)
211 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
212 __alignof__(struct property));
214 /* We accept flattened tree phandles either in
215 * ePAPR-style "phandle" properties, or the
216 * legacy "linux,phandle" properties. If both
217 * appear and have different values, things
218 * will get weird. Don't do that. */
219 if ((strcmp(pname, "phandle") == 0) ||
220 (strcmp(pname, "linux,phandle") == 0)) {
221 if (np->phandle == 0)
222 np->phandle = be32_to_cpup(p);
224 /* And we process the "ibm,phandle" property
225 * used in pSeries dynamic device tree
227 if (strcmp(pname, "ibm,phandle") == 0)
228 np->phandle = be32_to_cpup(p);
229 pp->name = (char *)pname;
231 pp->value = (__be32 *)p;
236 /* with version 0x10 we may not have the name property, recreate
237 * it here from the unit name if absent
240 const char *p1 = pathp, *ps = pathp, *pa = NULL;
253 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
254 __alignof__(struct property));
261 memcpy(pp->value, ps, sz - 1);
262 ((char *)pp->value)[sz - 1] = 0;
263 pr_debug("fixed up name for %s -> %s\n", pathp,
269 np->name = of_get_property(np, "name", NULL);
270 np->type = of_get_property(np, "device_type", NULL);
279 *poffset = fdt_next_node(blob, *poffset, &depth);
282 while (*poffset > 0 && depth > old_depth)
283 mem = unflatten_dt_node(blob, mem, poffset, np, allnextpp,
286 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
287 pr_err("unflatten: error %d processing FDT\n", *poffset);
293 * __unflatten_device_tree - create tree of device_nodes from flat blob
295 * unflattens a device-tree, creating the
296 * tree of struct device_node. It also fills the "name" and "type"
297 * pointers of the nodes so the normal device-tree walking functions
299 * @blob: The blob to expand
300 * @mynodes: The device_node tree created by the call
301 * @dt_alloc: An allocator that provides a virtual address to memory
302 * for the resulting tree
304 static void __unflatten_device_tree(struct boot_param_header *blob,
305 struct device_node **mynodes,
306 void * (*dt_alloc)(u64 size, u64 align))
311 struct device_node **allnextp = mynodes;
313 pr_debug(" -> unflatten_device_tree()\n");
316 pr_debug("No device tree pointer\n");
320 pr_debug("Unflattening device tree:\n");
321 pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
322 pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
323 pr_debug("version: %08x\n", be32_to_cpu(blob->version));
325 if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
326 pr_err("Invalid device tree blob header\n");
330 /* First pass, scan for size */
332 size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
333 size = ALIGN(size, 4);
335 pr_debug(" size is %lx, allocating...\n", size);
337 /* Allocate memory for the expanded device tree */
338 mem = dt_alloc(size + 4, __alignof__(struct device_node));
340 memset((void *)mem, 0, size);
342 ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
344 pr_debug(" unflattening %p...\n", mem);
346 /* Second pass, do actual unflattening */
348 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
349 if (be32_to_cpup(mem + size) != 0xdeadbeef)
350 pr_warning("End of tree marker overwritten: %08x\n",
351 be32_to_cpu(((__be32 *)mem)[size / 4]));
354 pr_debug(" <- unflatten_device_tree()\n");
357 static void *kernel_tree_alloc(u64 size, u64 align)
359 return kzalloc(size, GFP_KERNEL);
363 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
365 * unflattens the device-tree passed by the firmware, creating the
366 * tree of struct device_node. It also fills the "name" and "type"
367 * pointers of the nodes so the normal device-tree walking functions
370 void of_fdt_unflatten_tree(unsigned long *blob,
371 struct device_node **mynodes)
373 struct boot_param_header *device_tree =
374 (struct boot_param_header *)blob;
375 __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
377 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
379 /* Everything below here references initial_boot_params directly. */
380 int __initdata dt_root_addr_cells;
381 int __initdata dt_root_size_cells;
383 struct boot_param_header *initial_boot_params;
385 #ifdef CONFIG_OF_EARLY_FLATTREE
388 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
390 static int __init __reserved_mem_reserve_reg(unsigned long node,
393 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
394 phys_addr_t base, size;
397 int nomap, first = 1;
399 prop = of_get_flat_dt_prop(node, "reg", &len);
403 if (len && len % t_len != 0) {
404 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
409 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
411 while (len >= t_len) {
412 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
413 size = dt_mem_next_cell(dt_root_size_cells, &prop);
416 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
417 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
418 uname, &base, (unsigned long)size / SZ_1M);
420 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
421 uname, &base, (unsigned long)size / SZ_1M);
425 fdt_reserved_mem_save_node(node, uname, base, size);
433 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
434 * in /reserved-memory matches the values supported by the current implementation,
435 * also check if ranges property has been provided
437 static int __init __reserved_mem_check_root(unsigned long node)
441 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
442 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
445 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
446 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
449 prop = of_get_flat_dt_prop(node, "ranges", NULL);
456 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
458 static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
459 int depth, void *data)
465 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
466 if (__reserved_mem_check_root(node) != 0) {
467 pr_err("Reserved memory: unsupported node format, ignoring\n");
477 } else if (found && depth < 2) {
478 /* scanning of /reserved-memory has been finished */
482 status = of_get_flat_dt_prop(node, "status", NULL);
483 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
486 err = __reserved_mem_reserve_reg(node, uname);
487 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
488 fdt_reserved_mem_save_node(node, uname, 0, 0);
495 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
497 * This function grabs memory from early allocator for device exclusive use
498 * defined in device tree structures. It should be called by arch specific code
499 * once the early allocator (i.e. memblock) has been fully activated.
501 void __init early_init_fdt_scan_reserved_mem(void)
503 if (!initial_boot_params)
506 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
507 fdt_init_reserved_mem();
511 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
512 * @it: callback function
513 * @data: context data pointer
515 * This function is used to scan the flattened device-tree, it is
516 * used to extract the memory information at boot before we can
519 int __init of_scan_flat_dt(int (*it)(unsigned long node,
520 const char *uname, int depth,
524 const void *blob = initial_boot_params;
526 int offset, rc = 0, depth = -1;
528 for (offset = fdt_next_node(blob, -1, &depth);
529 offset >= 0 && depth >= 0 && !rc;
530 offset = fdt_next_node(blob, offset, &depth)) {
532 pathp = fdt_get_name(blob, offset, NULL);
534 pathp = kbasename(pathp);
535 rc = it(offset, pathp, depth, data);
541 * of_get_flat_dt_root - find the root node in the flat blob
543 unsigned long __init of_get_flat_dt_root(void)
549 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
551 * This function can be used within scan_flattened_dt callback to get
552 * access to properties
554 const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
557 return fdt_getprop(initial_boot_params, node, name, size);
561 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
562 * @node: node to test
563 * @compat: compatible string to compare with compatible list.
565 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
567 return of_fdt_is_compatible(initial_boot_params, node, compat);
571 * of_flat_dt_match - Return true if node matches a list of compatible values
573 int __init of_flat_dt_match(unsigned long node, const char *const *compat)
575 return of_fdt_match(initial_boot_params, node, compat);
578 struct fdt_scan_status {
583 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
587 #ifdef CONFIG_BLK_DEV_INITRD
589 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
590 * @node: reference to node containing initrd location ('chosen')
592 void __init early_init_dt_check_for_initrd(unsigned long node)
598 pr_debug("Looking for initrd properties... ");
600 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
603 start = of_read_number(prop, len/4);
605 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
608 end = of_read_number(prop, len/4);
610 early_init_dt_setup_initrd_arch(start, end);
611 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
612 (unsigned long long)start, (unsigned long long)end);
615 inline void early_init_dt_check_for_initrd(unsigned long node)
618 #endif /* CONFIG_BLK_DEV_INITRD */
621 * early_init_dt_scan_root - fetch the top level address and size cells
623 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
624 int depth, void *data)
631 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
632 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
634 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
636 dt_root_size_cells = be32_to_cpup(prop);
637 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
639 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
641 dt_root_addr_cells = be32_to_cpup(prop);
642 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
648 u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
650 const __be32 *p = *cellp;
653 return of_read_number(p, s);
657 * early_init_dt_scan_memory - Look for an parse memory nodes
659 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
660 int depth, void *data)
662 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
663 const __be32 *reg, *endp;
666 /* We are scanning "memory" nodes only */
669 * The longtrail doesn't have a device_type on the
670 * /memory node, so look for the node called /memory@0.
672 if (depth != 1 || strcmp(uname, "memory@0") != 0)
674 } else if (strcmp(type, "memory") != 0)
677 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
679 reg = of_get_flat_dt_prop(node, "reg", &l);
683 endp = reg + (l / sizeof(__be32));
685 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
686 uname, l, reg[0], reg[1], reg[2], reg[3]);
688 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
691 base = dt_mem_next_cell(dt_root_addr_cells, ®);
692 size = dt_mem_next_cell(dt_root_size_cells, ®);
696 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
697 (unsigned long long)size);
699 early_init_dt_add_memory_arch(base, size);
705 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
706 int depth, void *data)
711 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
713 if (depth != 1 || !data ||
714 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
717 early_init_dt_check_for_initrd(node);
719 /* Retrieve command line */
720 p = of_get_flat_dt_prop(node, "bootargs", &l);
721 if (p != NULL && l > 0)
722 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
725 * CONFIG_CMDLINE is meant to be a default in case nothing else
726 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
727 * is set in which case we override whatever was found earlier.
729 #ifdef CONFIG_CMDLINE
730 #ifndef CONFIG_CMDLINE_FORCE
731 if (!((char *)data)[0])
733 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
734 #endif /* CONFIG_CMDLINE */
736 pr_debug("Command line is: %s\n", (char*)data);
742 #ifdef CONFIG_HAVE_MEMBLOCK
743 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
745 const u64 phys_offset = __pa(PAGE_OFFSET);
748 if (base + size < phys_offset) {
749 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
753 if (base < phys_offset) {
754 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
756 size -= phys_offset - base;
759 memblock_add(base, size);
762 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
763 phys_addr_t size, bool nomap)
765 if (memblock_is_region_reserved(base, size))
768 return memblock_remove(base, size);
769 return memblock_reserve(base, size);
773 * called from unflatten_device_tree() to bootstrap devicetree itself
774 * Architectures can override this definition if memblock isn't used
776 void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
778 return __va(memblock_alloc(size, align));
781 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
782 phys_addr_t size, bool nomap)
784 pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
785 base, size, nomap ? " (nomap)" : "");
790 bool __init early_init_dt_scan(void *params)
795 /* Setup flat device-tree pointer */
796 initial_boot_params = params;
798 /* check device tree validity */
799 if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
800 initial_boot_params = NULL;
804 /* Retrieve various information from the /chosen node */
805 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
807 /* Initialize {size,address}-cells info */
808 of_scan_flat_dt(early_init_dt_scan_root, NULL);
810 /* Setup memory, calling early_init_dt_add_memory_arch */
811 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
817 * unflatten_device_tree - create tree of device_nodes from flat blob
819 * unflattens the device-tree passed by the firmware, creating the
820 * tree of struct device_node. It also fills the "name" and "type"
821 * pointers of the nodes so the normal device-tree walking functions
824 void __init unflatten_device_tree(void)
826 __unflatten_device_tree(initial_boot_params, &of_allnodes,
827 early_init_dt_alloc_memory_arch);
829 /* Get pointer to "/chosen" and "/aliasas" nodes for use everywhere */
830 of_alias_scan(early_init_dt_alloc_memory_arch);
833 #endif /* CONFIG_OF_EARLY_FLATTREE */