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));
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;
192 /* process properties */
193 for (offset = fdt_first_property_offset(blob, *poffset);
195 (offset = fdt_next_property_offset(blob, offset))) {
199 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
200 offset = -FDT_ERR_INTERNAL;
205 pr_info("Can't find property name in list !\n");
208 if (strcmp(pname, "name") == 0)
210 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
211 __alignof__(struct property));
213 /* We accept flattened tree phandles either in
214 * ePAPR-style "phandle" properties, or the
215 * legacy "linux,phandle" properties. If both
216 * appear and have different values, things
217 * will get weird. Don't do that. */
218 if ((strcmp(pname, "phandle") == 0) ||
219 (strcmp(pname, "linux,phandle") == 0)) {
220 if (np->phandle == 0)
221 np->phandle = be32_to_cpup(p);
223 /* And we process the "ibm,phandle" property
224 * used in pSeries dynamic device tree
226 if (strcmp(pname, "ibm,phandle") == 0)
227 np->phandle = be32_to_cpup(p);
228 pp->name = (char *)pname;
230 pp->value = (__be32 *)p;
235 /* with version 0x10 we may not have the name property, recreate
236 * it here from the unit name if absent
239 const char *p1 = pathp, *ps = pathp, *pa = NULL;
252 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
253 __alignof__(struct property));
260 memcpy(pp->value, ps, sz - 1);
261 ((char *)pp->value)[sz - 1] = 0;
262 pr_debug("fixed up name for %s -> %s\n", pathp,
268 np->name = of_get_property(np, "name", NULL);
269 np->type = of_get_property(np, "device_type", NULL);
278 *poffset = fdt_next_node(blob, *poffset, &depth);
281 while (*poffset > 0 && depth > old_depth)
282 mem = unflatten_dt_node(blob, mem, poffset, np, allnextpp,
285 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
286 pr_err("unflatten: error %d processing FDT\n", *poffset);
292 * __unflatten_device_tree - create tree of device_nodes from flat blob
294 * unflattens a device-tree, creating the
295 * tree of struct device_node. It also fills the "name" and "type"
296 * pointers of the nodes so the normal device-tree walking functions
298 * @blob: The blob to expand
299 * @mynodes: The device_node tree created by the call
300 * @dt_alloc: An allocator that provides a virtual address to memory
301 * for the resulting tree
303 static void __unflatten_device_tree(struct boot_param_header *blob,
304 struct device_node **mynodes,
305 void * (*dt_alloc)(u64 size, u64 align))
310 struct device_node **allnextp = mynodes;
312 pr_debug(" -> unflatten_device_tree()\n");
315 pr_debug("No device tree pointer\n");
319 pr_debug("Unflattening device tree:\n");
320 pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
321 pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
322 pr_debug("version: %08x\n", be32_to_cpu(blob->version));
324 if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
325 pr_err("Invalid device tree blob header\n");
329 /* First pass, scan for size */
331 size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
332 size = ALIGN(size, 4);
334 pr_debug(" size is %lx, allocating...\n", size);
336 /* Allocate memory for the expanded device tree */
337 mem = dt_alloc(size + 4, __alignof__(struct device_node));
339 memset((void *)mem, 0, size);
341 memset((void *)mem, 0, size);
343 ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
345 pr_debug(" unflattening %p...\n", mem);
347 /* Second pass, do actual unflattening */
349 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
350 if (be32_to_cpup(mem + size) != 0xdeadbeef)
351 pr_warning("End of tree marker overwritten: %08x\n",
352 be32_to_cpu(((__be32 *)mem)[size / 4]));
355 pr_debug(" <- unflatten_device_tree()\n");
358 static void *kernel_tree_alloc(u64 size, u64 align)
360 return kzalloc(size, GFP_KERNEL);
364 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
366 * unflattens the device-tree passed by the firmware, creating the
367 * tree of struct device_node. It also fills the "name" and "type"
368 * pointers of the nodes so the normal device-tree walking functions
371 void of_fdt_unflatten_tree(unsigned long *blob,
372 struct device_node **mynodes)
374 struct boot_param_header *device_tree =
375 (struct boot_param_header *)blob;
376 __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
378 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
380 /* Everything below here references initial_boot_params directly. */
381 int __initdata dt_root_addr_cells;
382 int __initdata dt_root_size_cells;
384 struct boot_param_header *initial_boot_params;
386 #ifdef CONFIG_OF_EARLY_FLATTREE
389 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
391 static int __init __reserved_mem_reserve_reg(unsigned long node,
394 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
395 phys_addr_t base, size;
398 int nomap, first = 1;
400 prop = of_get_flat_dt_prop(node, "reg", &len);
404 if (len && len % t_len != 0) {
405 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
410 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
412 while (len >= t_len) {
413 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
414 size = dt_mem_next_cell(dt_root_size_cells, &prop);
417 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
418 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
419 uname, &base, (unsigned long)size / SZ_1M);
421 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
422 uname, &base, (unsigned long)size / SZ_1M);
426 fdt_reserved_mem_save_node(node, uname, base, size);
434 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
435 * in /reserved-memory matches the values supported by the current implementation,
436 * also check if ranges property has been provided
438 static int __init __reserved_mem_check_root(unsigned long node)
442 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
443 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
446 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
447 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
450 prop = of_get_flat_dt_prop(node, "ranges", NULL);
457 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
459 static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
460 int depth, void *data)
466 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
467 if (__reserved_mem_check_root(node) != 0) {
468 pr_err("Reserved memory: unsupported node format, ignoring\n");
478 } else if (found && depth < 2) {
479 /* scanning of /reserved-memory has been finished */
483 status = of_get_flat_dt_prop(node, "status", NULL);
484 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
487 err = __reserved_mem_reserve_reg(node, uname);
488 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
489 fdt_reserved_mem_save_node(node, uname, 0, 0);
496 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
498 * This function grabs memory from early allocator for device exclusive use
499 * defined in device tree structures. It should be called by arch specific code
500 * once the early allocator (i.e. memblock) has been fully activated.
502 void __init early_init_fdt_scan_reserved_mem(void)
504 if (!initial_boot_params)
507 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
508 fdt_init_reserved_mem();
512 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
513 * @it: callback function
514 * @data: context data pointer
516 * This function is used to scan the flattened device-tree, it is
517 * used to extract the memory information at boot before we can
520 int __init of_scan_flat_dt(int (*it)(unsigned long node,
521 const char *uname, int depth,
525 const void *blob = initial_boot_params;
527 int offset, rc = 0, depth = -1;
529 for (offset = fdt_next_node(blob, -1, &depth);
530 offset >= 0 && depth >= 0 && !rc;
531 offset = fdt_next_node(blob, offset, &depth)) {
533 pathp = fdt_get_name(blob, offset, NULL);
535 pathp = kbasename(pathp);
536 rc = it(offset, pathp, depth, data);
542 * of_get_flat_dt_root - find the root node in the flat blob
544 unsigned long __init of_get_flat_dt_root(void)
550 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
552 * This function can be used within scan_flattened_dt callback to get
553 * access to properties
555 const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
558 return fdt_getprop(initial_boot_params, node, name, size);
562 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
563 * @node: node to test
564 * @compat: compatible string to compare with compatible list.
566 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
568 return of_fdt_is_compatible(initial_boot_params, node, compat);
572 * of_flat_dt_match - Return true if node matches a list of compatible values
574 int __init of_flat_dt_match(unsigned long node, const char *const *compat)
576 return of_fdt_match(initial_boot_params, node, compat);
579 struct fdt_scan_status {
584 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
588 #ifdef CONFIG_BLK_DEV_INITRD
590 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
591 * @node: reference to node containing initrd location ('chosen')
593 void __init early_init_dt_check_for_initrd(unsigned long node)
599 pr_debug("Looking for initrd properties... ");
601 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
604 start = of_read_number(prop, len/4);
606 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
609 end = of_read_number(prop, len/4);
611 early_init_dt_setup_initrd_arch(start, end);
612 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
613 (unsigned long long)start, (unsigned long long)end);
616 inline void early_init_dt_check_for_initrd(unsigned long node)
619 #endif /* CONFIG_BLK_DEV_INITRD */
622 * early_init_dt_scan_root - fetch the top level address and size cells
624 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
625 int depth, void *data)
632 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
633 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
635 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
637 dt_root_size_cells = be32_to_cpup(prop);
638 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
640 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
642 dt_root_addr_cells = be32_to_cpup(prop);
643 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
649 u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
651 const __be32 *p = *cellp;
654 return of_read_number(p, s);
658 * early_init_dt_scan_memory - Look for an parse memory nodes
660 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
661 int depth, void *data)
663 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
664 const __be32 *reg, *endp;
667 /* We are scanning "memory" nodes only */
670 * The longtrail doesn't have a device_type on the
671 * /memory node, so look for the node called /memory@0.
673 if (depth != 1 || strcmp(uname, "memory@0") != 0)
675 } else if (strcmp(type, "memory") != 0)
678 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
680 reg = of_get_flat_dt_prop(node, "reg", &l);
684 endp = reg + (l / sizeof(__be32));
686 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
687 uname, l, reg[0], reg[1], reg[2], reg[3]);
689 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
692 base = dt_mem_next_cell(dt_root_addr_cells, ®);
693 size = dt_mem_next_cell(dt_root_size_cells, ®);
697 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
698 (unsigned long long)size);
700 early_init_dt_add_memory_arch(base, size);
707 * Convert configs to something easy to use in C code
709 #if defined(CONFIG_CMDLINE_FORCE)
710 static const int overwrite_incoming_cmdline = 1;
711 static const int read_dt_cmdline;
712 static const int concat_cmdline;
713 #elif defined(CONFIG_CMDLINE_EXTEND)
714 static const int overwrite_incoming_cmdline;
715 static const int read_dt_cmdline = 1;
716 static const int concat_cmdline = 1;
717 #else /* CMDLINE_FROM_BOOTLOADER */
718 static const int overwrite_incoming_cmdline;
719 static const int read_dt_cmdline = 1;
720 static const int concat_cmdline;
723 #ifdef CONFIG_CMDLINE
724 static const char *config_cmdline = CONFIG_CMDLINE;
726 static const char *config_cmdline = "";
729 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
730 int depth, void *data)
734 char *cmdline = data;
736 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
738 if (depth != 1 || !cmdline ||
739 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
742 early_init_dt_check_for_initrd(node);
744 /* Put CONFIG_CMDLINE in if forced or if data had nothing in it to start */
745 if (overwrite_incoming_cmdline || !cmdline[0])
746 strlcpy(cmdline, config_cmdline, COMMAND_LINE_SIZE);
748 /* Retrieve command line unless forcing */
750 p = of_get_flat_dt_prop(node, "bootargs", &l);
752 if (p != NULL && l > 0) {
753 if (concat_cmdline) {
756 strlcat(cmdline, " ", COMMAND_LINE_SIZE);
757 cmdline_len = strlen(cmdline);
758 copy_len = COMMAND_LINE_SIZE - cmdline_len - 1;
759 copy_len = min((int)l, copy_len);
760 strncpy(cmdline + cmdline_len, p, copy_len);
761 cmdline[cmdline_len + copy_len] = '\0';
763 strlcpy(cmdline, p, min((int)l, COMMAND_LINE_SIZE));
767 pr_debug("Command line is: %s\n", (char*)data);
773 #ifdef CONFIG_HAVE_MEMBLOCK
774 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
776 const u64 phys_offset = __pa(PAGE_OFFSET);
779 if (base + size < phys_offset) {
780 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
784 if (base < phys_offset) {
785 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
787 size -= phys_offset - base;
790 memblock_add(base, size);
793 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
794 phys_addr_t size, bool nomap)
796 if (memblock_is_region_reserved(base, size))
799 return memblock_remove(base, size);
800 return memblock_reserve(base, size);
804 * called from unflatten_device_tree() to bootstrap devicetree itself
805 * Architectures can override this definition if memblock isn't used
807 void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
809 return __va(memblock_alloc(size, align));
812 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
813 phys_addr_t size, bool nomap)
815 pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
816 base, size, nomap ? " (nomap)" : "");
821 bool __init early_init_dt_scan(void *params)
826 /* Setup flat device-tree pointer */
827 initial_boot_params = params;
829 /* check device tree validity */
830 if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
831 initial_boot_params = NULL;
835 /* Retrieve various information from the /chosen node */
836 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
838 /* Initialize {size,address}-cells info */
839 of_scan_flat_dt(early_init_dt_scan_root, NULL);
841 /* Setup memory, calling early_init_dt_add_memory_arch */
842 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
848 * unflatten_device_tree - create tree of device_nodes from flat blob
850 * unflattens the device-tree passed by the firmware, creating the
851 * tree of struct device_node. It also fills the "name" and "type"
852 * pointers of the nodes so the normal device-tree walking functions
855 void __init unflatten_device_tree(void)
857 __unflatten_device_tree(initial_boot_params, &of_allnodes,
858 early_init_dt_alloc_memory_arch);
860 /* Get pointer to "/chosen" and "/aliasas" nodes for use everywhere */
861 of_alias_scan(early_init_dt_alloc_memory_arch);
864 #endif /* CONFIG_OF_EARLY_FLATTREE */