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>
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/libfdt.h>
23 #include <linux/debugfs.h>
24 #include <linux/serial_core.h>
26 #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
30 * of_fdt_is_compatible - Return true if given node from the given blob has
31 * compat in its compatible list
32 * @blob: A device tree blob
34 * @compat: compatible string to compare with compatible list.
36 * On match, returns a non-zero value with smaller values returned for more
37 * specific compatible values.
39 int of_fdt_is_compatible(const void *blob
,
40 unsigned long node
, const char *compat
)
44 unsigned long l
, score
= 0;
46 cp
= fdt_getprop(blob
, node
, "compatible", &cplen
);
51 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
62 * of_fdt_match - Return true if node matches a list of compatible values
64 int of_fdt_match(const void *blob
, unsigned long node
,
65 const char *const *compat
)
67 unsigned int tmp
, score
= 0;
73 tmp
= of_fdt_is_compatible(blob
, node
, *compat
);
74 if (tmp
&& (score
== 0 || (tmp
< score
)))
82 static void *unflatten_dt_alloc(void **mem
, unsigned long size
,
87 *mem
= PTR_ALIGN(*mem
, align
);
95 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
96 * @blob: The parent device tree blob
97 * @mem: Memory chunk to use for allocating device nodes and properties
98 * @p: pointer to node in flat tree
99 * @dad: Parent struct device_node
100 * @allnextpp: pointer to ->allnext from last allocated device_node
101 * @fpsize: Size of the node path up at the current depth.
103 static void * unflatten_dt_node(void *blob
,
106 struct device_node
*dad
,
107 struct device_node
***allnextpp
,
108 unsigned long fpsize
)
111 struct device_node
*np
;
112 struct property
*pp
, **prev_pp
= NULL
;
114 unsigned int l
, allocl
;
115 static int depth
= 0;
121 pathp
= fdt_get_name(blob
, *poffset
, &l
);
127 /* version 0x10 has a more compact unit name here instead of the full
128 * path. we accumulate the full path size using "fpsize", we'll rebuild
129 * it later. We detect this because the first character of the name is
132 if ((*pathp
) != '/') {
135 /* root node: special case. fpsize accounts for path
136 * plus terminating zero. root node only has '/', so
137 * fpsize should be 2, but we want to avoid the first
138 * level nodes to have two '/' so we use fpsize 1 here
145 /* account for '/' and path size minus terminal 0
153 np
= unflatten_dt_alloc(&mem
, sizeof(struct device_node
) + allocl
,
154 __alignof__(struct device_node
));
158 np
->full_name
= fn
= ((char *)np
) + sizeof(*np
);
160 /* rebuild full path for new format */
161 if (dad
&& dad
->parent
) {
162 strcpy(fn
, dad
->full_name
);
164 if ((strlen(fn
) + l
+ 1) != allocl
) {
165 pr_debug("%s: p: %d, l: %d, a: %d\n",
166 pathp
, (int)strlen(fn
),
174 memcpy(fn
, pathp
, l
);
176 prev_pp
= &np
->properties
;
178 *allnextpp
= &np
->allnext
;
181 /* we temporarily use the next field as `last_child'*/
182 if (dad
->next
== NULL
)
185 dad
->next
->sibling
= np
;
189 /* process properties */
190 for (offset
= fdt_first_property_offset(blob
, *poffset
);
192 (offset
= fdt_next_property_offset(blob
, offset
))) {
196 if (!(p
= fdt_getprop_by_offset(blob
, offset
, &pname
, &sz
))) {
197 offset
= -FDT_ERR_INTERNAL
;
202 pr_info("Can't find property name in list !\n");
205 if (strcmp(pname
, "name") == 0)
207 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
208 __alignof__(struct property
));
210 /* We accept flattened tree phandles either in
211 * ePAPR-style "phandle" properties, or the
212 * legacy "linux,phandle" properties. If both
213 * appear and have different values, things
214 * will get weird. Don't do that. */
215 if ((strcmp(pname
, "phandle") == 0) ||
216 (strcmp(pname
, "linux,phandle") == 0)) {
217 if (np
->phandle
== 0)
218 np
->phandle
= be32_to_cpup(p
);
220 /* And we process the "ibm,phandle" property
221 * used in pSeries dynamic device tree
223 if (strcmp(pname
, "ibm,phandle") == 0)
224 np
->phandle
= be32_to_cpup(p
);
225 pp
->name
= (char *)pname
;
227 pp
->value
= (__be32
*)p
;
232 /* with version 0x10 we may not have the name property, recreate
233 * it here from the unit name if absent
236 const char *p1
= pathp
, *ps
= pathp
, *pa
= NULL
;
249 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
) + sz
,
250 __alignof__(struct property
));
257 memcpy(pp
->value
, ps
, sz
- 1);
258 ((char *)pp
->value
)[sz
- 1] = 0;
259 pr_debug("fixed up name for %s -> %s\n", pathp
,
265 np
->name
= of_get_property(np
, "name", NULL
);
266 np
->type
= of_get_property(np
, "device_type", NULL
);
275 *poffset
= fdt_next_node(blob
, *poffset
, &depth
);
278 while (*poffset
> 0 && depth
> old_depth
)
279 mem
= unflatten_dt_node(blob
, mem
, poffset
, np
, allnextpp
,
282 if (*poffset
< 0 && *poffset
!= -FDT_ERR_NOTFOUND
)
283 pr_err("unflatten: error %d processing FDT\n", *poffset
);
289 * __unflatten_device_tree - create tree of device_nodes from flat blob
291 * unflattens a device-tree, creating the
292 * tree of struct device_node. It also fills the "name" and "type"
293 * pointers of the nodes so the normal device-tree walking functions
295 * @blob: The blob to expand
296 * @mynodes: The device_node tree created by the call
297 * @dt_alloc: An allocator that provides a virtual address to memory
298 * for the resulting tree
300 static void __unflatten_device_tree(void *blob
,
301 struct device_node
**mynodes
,
302 void * (*dt_alloc
)(u64 size
, u64 align
))
307 struct device_node
**allnextp
= mynodes
;
309 pr_debug(" -> unflatten_device_tree()\n");
312 pr_debug("No device tree pointer\n");
316 pr_debug("Unflattening device tree:\n");
317 pr_debug("magic: %08x\n", fdt_magic(blob
));
318 pr_debug("size: %08x\n", fdt_totalsize(blob
));
319 pr_debug("version: %08x\n", fdt_version(blob
));
321 if (fdt_check_header(blob
)) {
322 pr_err("Invalid device tree blob header\n");
326 /* First pass, scan for size */
328 size
= (unsigned long)unflatten_dt_node(blob
, NULL
, &start
, NULL
, NULL
, 0);
329 size
= ALIGN(size
, 4);
331 pr_debug(" size is %lx, allocating...\n", size
);
333 /* Allocate memory for the expanded device tree */
334 mem
= dt_alloc(size
+ 4, __alignof__(struct device_node
));
335 memset(mem
, 0, size
);
337 *(__be32
*)(mem
+ size
) = cpu_to_be32(0xdeadbeef);
339 pr_debug(" unflattening %p...\n", mem
);
341 /* Second pass, do actual unflattening */
343 unflatten_dt_node(blob
, mem
, &start
, NULL
, &allnextp
, 0);
344 if (be32_to_cpup(mem
+ size
) != 0xdeadbeef)
345 pr_warning("End of tree marker overwritten: %08x\n",
346 be32_to_cpup(mem
+ size
));
349 pr_debug(" <- unflatten_device_tree()\n");
352 static void *kernel_tree_alloc(u64 size
, u64 align
)
354 return kzalloc(size
, GFP_KERNEL
);
358 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
360 * unflattens the device-tree passed by the firmware, creating the
361 * tree of struct device_node. It also fills the "name" and "type"
362 * pointers of the nodes so the normal device-tree walking functions
365 void of_fdt_unflatten_tree(unsigned long *blob
,
366 struct device_node
**mynodes
)
368 __unflatten_device_tree(blob
, mynodes
, &kernel_tree_alloc
);
370 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree
);
372 /* Everything below here references initial_boot_params directly. */
373 int __initdata dt_root_addr_cells
;
374 int __initdata dt_root_size_cells
;
376 void *initial_boot_params
;
378 #ifdef CONFIG_OF_EARLY_FLATTREE
381 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
383 static int __init
__reserved_mem_reserve_reg(unsigned long node
,
386 int t_len
= (dt_root_addr_cells
+ dt_root_size_cells
) * sizeof(__be32
);
387 phys_addr_t base
, size
;
390 int nomap
, first
= 1;
392 prop
= of_get_flat_dt_prop(node
, "reg", &len
);
396 if (len
&& len
% t_len
!= 0) {
397 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
402 nomap
= of_get_flat_dt_prop(node
, "no-map", NULL
) != NULL
;
404 while (len
>= t_len
) {
405 base
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
406 size
= dt_mem_next_cell(dt_root_size_cells
, &prop
);
409 early_init_dt_reserve_memory_arch(base
, size
, nomap
) == 0)
410 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
411 uname
, &base
, (unsigned long)size
/ SZ_1M
);
413 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
414 uname
, &base
, (unsigned long)size
/ SZ_1M
);
418 fdt_reserved_mem_save_node(node
, uname
, base
, size
);
426 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
427 * in /reserved-memory matches the values supported by the current implementation,
428 * also check if ranges property has been provided
430 static int __init
__reserved_mem_check_root(unsigned long node
)
434 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
435 if (!prop
|| be32_to_cpup(prop
) != dt_root_size_cells
)
438 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
439 if (!prop
|| be32_to_cpup(prop
) != dt_root_addr_cells
)
442 prop
= of_get_flat_dt_prop(node
, "ranges", NULL
);
449 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
451 static int __init
__fdt_scan_reserved_mem(unsigned long node
, const char *uname
,
452 int depth
, void *data
)
458 if (!found
&& depth
== 1 && strcmp(uname
, "reserved-memory") == 0) {
459 if (__reserved_mem_check_root(node
) != 0) {
460 pr_err("Reserved memory: unsupported node format, ignoring\n");
470 } else if (found
&& depth
< 2) {
471 /* scanning of /reserved-memory has been finished */
475 status
= of_get_flat_dt_prop(node
, "status", NULL
);
476 if (status
&& strcmp(status
, "okay") != 0 && strcmp(status
, "ok") != 0)
479 err
= __reserved_mem_reserve_reg(node
, uname
);
480 if (err
== -ENOENT
&& of_get_flat_dt_prop(node
, "size", NULL
))
481 fdt_reserved_mem_save_node(node
, uname
, 0, 0);
488 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
490 * This function grabs memory from early allocator for device exclusive use
491 * defined in device tree structures. It should be called by arch specific code
492 * once the early allocator (i.e. memblock) has been fully activated.
494 void __init
early_init_fdt_scan_reserved_mem(void)
499 if (!initial_boot_params
)
502 /* Reserve the dtb region */
503 early_init_dt_reserve_memory_arch(__pa(initial_boot_params
),
504 fdt_totalsize(initial_boot_params
),
507 /* Process header /memreserve/ fields */
509 fdt_get_mem_rsv(initial_boot_params
, n
, &base
, &size
);
512 early_init_dt_reserve_memory_arch(base
, size
, 0);
515 of_scan_flat_dt(__fdt_scan_reserved_mem
, NULL
);
516 fdt_init_reserved_mem();
520 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
521 * @it: callback function
522 * @data: context data pointer
524 * This function is used to scan the flattened device-tree, it is
525 * used to extract the memory information at boot before we can
528 int __init
of_scan_flat_dt(int (*it
)(unsigned long node
,
529 const char *uname
, int depth
,
533 const void *blob
= initial_boot_params
;
535 int offset
, rc
= 0, depth
= -1;
537 for (offset
= fdt_next_node(blob
, -1, &depth
);
538 offset
>= 0 && depth
>= 0 && !rc
;
539 offset
= fdt_next_node(blob
, offset
, &depth
)) {
541 pathp
= fdt_get_name(blob
, offset
, NULL
);
543 pathp
= kbasename(pathp
);
544 rc
= it(offset
, pathp
, depth
, data
);
550 * of_get_flat_dt_root - find the root node in the flat blob
552 unsigned long __init
of_get_flat_dt_root(void)
558 * of_get_flat_dt_size - Return the total size of the FDT
560 int __init
of_get_flat_dt_size(void)
562 return fdt_totalsize(initial_boot_params
);
566 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
568 * This function can be used within scan_flattened_dt callback to get
569 * access to properties
571 const void *__init
of_get_flat_dt_prop(unsigned long node
, const char *name
,
574 return fdt_getprop(initial_boot_params
, node
, name
, size
);
578 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
579 * @node: node to test
580 * @compat: compatible string to compare with compatible list.
582 int __init
of_flat_dt_is_compatible(unsigned long node
, const char *compat
)
584 return of_fdt_is_compatible(initial_boot_params
, node
, compat
);
588 * of_flat_dt_match - Return true if node matches a list of compatible values
590 int __init
of_flat_dt_match(unsigned long node
, const char *const *compat
)
592 return of_fdt_match(initial_boot_params
, node
, compat
);
595 struct fdt_scan_status
{
600 int (*iterator
)(unsigned long node
, const char *uname
, int depth
, void *data
);
604 const char * __init
of_flat_dt_get_machine_name(void)
607 unsigned long dt_root
= of_get_flat_dt_root();
609 name
= of_get_flat_dt_prop(dt_root
, "model", NULL
);
611 name
= of_get_flat_dt_prop(dt_root
, "compatible", NULL
);
616 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
618 * @default_match: A machine specific ptr to return in case of no match.
619 * @get_next_compat: callback function to return next compatible match table.
621 * Iterate through machine match tables to find the best match for the machine
622 * compatible string in the FDT.
624 const void * __init
of_flat_dt_match_machine(const void *default_match
,
625 const void * (*get_next_compat
)(const char * const**))
627 const void *data
= NULL
;
628 const void *best_data
= default_match
;
629 const char *const *compat
;
630 unsigned long dt_root
;
631 unsigned int best_score
= ~1, score
= 0;
633 dt_root
= of_get_flat_dt_root();
634 while ((data
= get_next_compat(&compat
))) {
635 score
= of_flat_dt_match(dt_root
, compat
);
636 if (score
> 0 && score
< best_score
) {
645 pr_err("\n unrecognized device tree list:\n[ ");
647 prop
= of_get_flat_dt_prop(dt_root
, "compatible", &size
);
650 printk("'%s' ", prop
);
651 size
-= strlen(prop
) + 1;
652 prop
+= strlen(prop
) + 1;
659 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
664 #ifdef CONFIG_BLK_DEV_INITRD
666 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
667 * @node: reference to node containing initrd location ('chosen')
669 static void __init
early_init_dt_check_for_initrd(unsigned long node
)
675 pr_debug("Looking for initrd properties... ");
677 prop
= of_get_flat_dt_prop(node
, "linux,initrd-start", &len
);
680 start
= of_read_number(prop
, len
/4);
682 prop
= of_get_flat_dt_prop(node
, "linux,initrd-end", &len
);
685 end
= of_read_number(prop
, len
/4);
687 initrd_start
= (unsigned long)__va(start
);
688 initrd_end
= (unsigned long)__va(end
);
689 initrd_below_start_ok
= 1;
691 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
692 (unsigned long long)start
, (unsigned long long)end
);
695 static inline void early_init_dt_check_for_initrd(unsigned long node
)
698 #endif /* CONFIG_BLK_DEV_INITRD */
700 #ifdef CONFIG_SERIAL_EARLYCON
701 extern struct of_device_id __earlycon_of_table
[];
703 int __init
early_init_dt_scan_chosen_serial(void)
708 const struct of_device_id
*match
= __earlycon_of_table
;
709 const void *fdt
= initial_boot_params
;
711 offset
= fdt_path_offset(fdt
, "/chosen");
713 offset
= fdt_path_offset(fdt
, "/chosen@0");
717 p
= fdt_getprop(fdt
, offset
, "stdout-path", &l
);
719 p
= fdt_getprop(fdt
, offset
, "linux,stdout-path", &l
);
723 /* Get the node specified by stdout-path */
724 offset
= fdt_path_offset(fdt
, p
);
728 while (match
->compatible
) {
730 if (fdt_node_check_compatible(fdt
, offset
, match
->compatible
)) {
735 addr
= fdt_translate_address(fdt
, offset
);
739 of_setup_earlycon(addr
, match
->data
);
745 static int __init
setup_of_earlycon(char *buf
)
750 return early_init_dt_scan_chosen_serial();
752 early_param("earlycon", setup_of_earlycon
);
756 * early_init_dt_scan_root - fetch the top level address and size cells
758 int __init
early_init_dt_scan_root(unsigned long node
, const char *uname
,
759 int depth
, void *data
)
766 dt_root_size_cells
= OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
767 dt_root_addr_cells
= OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
769 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
771 dt_root_size_cells
= be32_to_cpup(prop
);
772 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells
);
774 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
776 dt_root_addr_cells
= be32_to_cpup(prop
);
777 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells
);
783 u64 __init
dt_mem_next_cell(int s
, const __be32
**cellp
)
785 const __be32
*p
= *cellp
;
788 return of_read_number(p
, s
);
792 * early_init_dt_scan_memory - Look for an parse memory nodes
794 int __init
early_init_dt_scan_memory(unsigned long node
, const char *uname
,
795 int depth
, void *data
)
797 const char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
798 const __be32
*reg
, *endp
;
801 /* We are scanning "memory" nodes only */
804 * The longtrail doesn't have a device_type on the
805 * /memory node, so look for the node called /memory@0.
807 if (!IS_ENABLED(CONFIG_PPC32
) || depth
!= 1 || strcmp(uname
, "memory@0") != 0)
809 } else if (strcmp(type
, "memory") != 0)
812 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
814 reg
= of_get_flat_dt_prop(node
, "reg", &l
);
818 endp
= reg
+ (l
/ sizeof(__be32
));
820 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
821 uname
, l
, reg
[0], reg
[1], reg
[2], reg
[3]);
823 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
826 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
827 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
831 pr_debug(" - %llx , %llx\n", (unsigned long long)base
,
832 (unsigned long long)size
);
834 early_init_dt_add_memory_arch(base
, size
);
840 int __init
early_init_dt_scan_chosen(unsigned long node
, const char *uname
,
841 int depth
, void *data
)
846 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
848 if (depth
!= 1 || !data
||
849 (strcmp(uname
, "chosen") != 0 && strcmp(uname
, "chosen@0") != 0))
852 early_init_dt_check_for_initrd(node
);
854 /* Retrieve command line */
855 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
856 if (p
!= NULL
&& l
> 0)
857 strlcpy(data
, p
, min((int)l
, COMMAND_LINE_SIZE
));
860 * CONFIG_CMDLINE is meant to be a default in case nothing else
861 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
862 * is set in which case we override whatever was found earlier.
864 #ifdef CONFIG_CMDLINE
865 #ifndef CONFIG_CMDLINE_FORCE
866 if (!((char *)data
)[0])
868 strlcpy(data
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
869 #endif /* CONFIG_CMDLINE */
871 pr_debug("Command line is: %s\n", (char*)data
);
877 #ifdef CONFIG_HAVE_MEMBLOCK
878 void __init __weak
early_init_dt_add_memory_arch(u64 base
, u64 size
)
880 const u64 phys_offset
= __pa(PAGE_OFFSET
);
884 if (sizeof(phys_addr_t
) < sizeof(u64
)) {
885 if (base
> ULONG_MAX
) {
886 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
891 if (base
+ size
> ULONG_MAX
) {
892 pr_warning("Ignoring memory range 0x%lx - 0x%llx\n",
893 ULONG_MAX
, base
+ size
);
894 size
= ULONG_MAX
- base
;
898 if (base
+ size
< phys_offset
) {
899 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
903 if (base
< phys_offset
) {
904 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
906 size
-= phys_offset
- base
;
909 memblock_add(base
, size
);
912 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
913 phys_addr_t size
, bool nomap
)
915 if (memblock_is_region_reserved(base
, size
))
918 return memblock_remove(base
, size
);
919 return memblock_reserve(base
, size
);
923 * called from unflatten_device_tree() to bootstrap devicetree itself
924 * Architectures can override this definition if memblock isn't used
926 void * __init __weak
early_init_dt_alloc_memory_arch(u64 size
, u64 align
)
928 return __va(memblock_alloc(size
, align
));
931 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
932 phys_addr_t size
, bool nomap
)
934 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
935 &base
, &size
, nomap
? " (nomap)" : "");
940 bool __init
early_init_dt_scan(void *params
)
945 /* Setup flat device-tree pointer */
946 initial_boot_params
= params
;
948 /* check device tree validity */
949 if (fdt_check_header(params
)) {
950 initial_boot_params
= NULL
;
954 /* Retrieve various information from the /chosen node */
955 of_scan_flat_dt(early_init_dt_scan_chosen
, boot_command_line
);
957 /* Initialize {size,address}-cells info */
958 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
960 /* Setup memory, calling early_init_dt_add_memory_arch */
961 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
967 * unflatten_device_tree - create tree of device_nodes from flat blob
969 * unflattens the device-tree passed by the firmware, creating the
970 * tree of struct device_node. It also fills the "name" and "type"
971 * pointers of the nodes so the normal device-tree walking functions
974 void __init
unflatten_device_tree(void)
976 __unflatten_device_tree(initial_boot_params
, &of_allnodes
,
977 early_init_dt_alloc_memory_arch
);
979 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
980 of_alias_scan(early_init_dt_alloc_memory_arch
);
984 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
986 * Copies and unflattens the device-tree passed by the firmware, creating the
987 * tree of struct device_node. It also fills the "name" and "type"
988 * pointers of the nodes so the normal device-tree walking functions
989 * can be used. This should only be used when the FDT memory has not been
990 * reserved such is the case when the FDT is built-in to the kernel init
991 * section. If the FDT memory is reserved already then unflatten_device_tree
992 * should be used instead.
994 void __init
unflatten_and_copy_device_tree(void)
999 if (!initial_boot_params
) {
1000 pr_warn("No valid device tree found, continuing without\n");
1004 size
= fdt_totalsize(initial_boot_params
);
1005 dt
= early_init_dt_alloc_memory_arch(size
,
1006 roundup_pow_of_two(FDT_V17_SIZE
));
1009 memcpy(dt
, initial_boot_params
, size
);
1010 initial_boot_params
= dt
;
1012 unflatten_device_tree();
1015 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1016 static struct debugfs_blob_wrapper flat_dt_blob
;
1018 static int __init
of_flat_dt_debugfs_export_fdt(void)
1020 struct dentry
*d
= debugfs_create_dir("device-tree", NULL
);
1025 flat_dt_blob
.data
= initial_boot_params
;
1026 flat_dt_blob
.size
= fdt_totalsize(initial_boot_params
);
1028 d
= debugfs_create_blob("flat-device-tree", S_IFREG
| S_IRUSR
,
1035 module_init(of_flat_dt_debugfs_export_fdt
);
1038 #endif /* CONFIG_OF_EARLY_FLATTREE */