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_limit_memory - limit the number of regions in the /memory node
31 * @limit: maximum entries
33 * Adjust the flattened device tree to have at most 'limit' number of
34 * memory entries in the /memory node. This function may be called
35 * any time after initial_boot_param is set.
37 void of_fdt_limit_memory(int limit
)
42 int nr_address_cells
= OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
43 int nr_size_cells
= OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
44 const uint32_t *addr_prop
;
45 const uint32_t *size_prop
;
49 root_offset
= fdt_path_offset(initial_boot_params
, "/");
53 addr_prop
= fdt_getprop(initial_boot_params
, root_offset
,
54 "#address-cells", NULL
);
56 nr_address_cells
= fdt32_to_cpu(*addr_prop
);
58 size_prop
= fdt_getprop(initial_boot_params
, root_offset
,
61 nr_size_cells
= fdt32_to_cpu(*size_prop
);
63 cell_size
= sizeof(uint32_t)*(nr_address_cells
+ nr_size_cells
);
65 memory
= fdt_path_offset(initial_boot_params
, "/memory");
67 val
= fdt_getprop(initial_boot_params
, memory
, "reg", &len
);
68 if (len
> limit
*cell_size
) {
69 len
= limit
*cell_size
;
70 pr_debug("Limiting number of entries to %d\n", limit
);
71 fdt_setprop(initial_boot_params
, memory
, "reg", val
,
78 * of_fdt_is_compatible - Return true if given node from the given blob has
79 * compat in its compatible list
80 * @blob: A device tree blob
82 * @compat: compatible string to compare with compatible list.
84 * On match, returns a non-zero value with smaller values returned for more
85 * specific compatible values.
87 int of_fdt_is_compatible(const void *blob
,
88 unsigned long node
, const char *compat
)
92 unsigned long l
, score
= 0;
94 cp
= fdt_getprop(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(const void *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(void *blob
,
154 struct device_node
*dad
,
155 struct device_node
***allnextpp
,
156 unsigned long fpsize
)
159 struct device_node
*np
;
160 struct property
*pp
, **prev_pp
= NULL
;
162 unsigned int l
, allocl
;
163 static int depth
= 0;
169 pathp
= fdt_get_name(blob
, *poffset
, &l
);
175 /* version 0x10 has a more compact unit name here instead of the full
176 * path. we accumulate the full path size using "fpsize", we'll rebuild
177 * it later. We detect this because the first character of the name is
180 if ((*pathp
) != '/') {
183 /* root node: special case. fpsize accounts for path
184 * plus terminating zero. root node only has '/', so
185 * fpsize should be 2, but we want to avoid the first
186 * level nodes to have two '/' so we use fpsize 1 here
193 /* account for '/' and path size minus terminal 0
201 np
= unflatten_dt_alloc(&mem
, sizeof(struct device_node
) + allocl
,
202 __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
;
237 /* process properties */
238 for (offset
= fdt_first_property_offset(blob
, *poffset
);
240 (offset
= fdt_next_property_offset(blob
, offset
))) {
244 if (!(p
= fdt_getprop_by_offset(blob
, offset
, &pname
, &sz
))) {
245 offset
= -FDT_ERR_INTERNAL
;
250 pr_info("Can't find property name in list !\n");
253 if (strcmp(pname
, "name") == 0)
255 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
256 __alignof__(struct property
));
258 /* We accept flattened tree phandles either in
259 * ePAPR-style "phandle" properties, or the
260 * legacy "linux,phandle" properties. If both
261 * appear and have different values, things
262 * will get weird. Don't do that. */
263 if ((strcmp(pname
, "phandle") == 0) ||
264 (strcmp(pname
, "linux,phandle") == 0)) {
265 if (np
->phandle
== 0)
266 np
->phandle
= be32_to_cpup(p
);
268 /* And we process the "ibm,phandle" property
269 * used in pSeries dynamic device tree
271 if (strcmp(pname
, "ibm,phandle") == 0)
272 np
->phandle
= be32_to_cpup(p
);
273 pp
->name
= (char *)pname
;
275 pp
->value
= (__be32
*)p
;
280 /* with version 0x10 we may not have the name property, recreate
281 * it here from the unit name if absent
284 const char *p1
= pathp
, *ps
= pathp
, *pa
= NULL
;
297 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
) + sz
,
298 __alignof__(struct property
));
305 memcpy(pp
->value
, ps
, sz
- 1);
306 ((char *)pp
->value
)[sz
- 1] = 0;
307 pr_debug("fixed up name for %s -> %s\n", pathp
,
313 np
->name
= of_get_property(np
, "name", NULL
);
314 np
->type
= of_get_property(np
, "device_type", NULL
);
323 *poffset
= fdt_next_node(blob
, *poffset
, &depth
);
326 while (*poffset
> 0 && depth
> old_depth
)
327 mem
= unflatten_dt_node(blob
, mem
, poffset
, np
, allnextpp
,
330 if (*poffset
< 0 && *poffset
!= -FDT_ERR_NOTFOUND
)
331 pr_err("unflatten: error %d processing FDT\n", *poffset
);
337 * __unflatten_device_tree - create tree of device_nodes from flat blob
339 * unflattens a device-tree, creating the
340 * tree of struct device_node. It also fills the "name" and "type"
341 * pointers of the nodes so the normal device-tree walking functions
343 * @blob: The blob to expand
344 * @mynodes: The device_node tree created by the call
345 * @dt_alloc: An allocator that provides a virtual address to memory
346 * for the resulting tree
348 static void __unflatten_device_tree(void *blob
,
349 struct device_node
**mynodes
,
350 void * (*dt_alloc
)(u64 size
, u64 align
))
355 struct device_node
**allnextp
= mynodes
;
357 pr_debug(" -> unflatten_device_tree()\n");
360 pr_debug("No device tree pointer\n");
364 pr_debug("Unflattening device tree:\n");
365 pr_debug("magic: %08x\n", fdt_magic(blob
));
366 pr_debug("size: %08x\n", fdt_totalsize(blob
));
367 pr_debug("version: %08x\n", fdt_version(blob
));
369 if (fdt_check_header(blob
)) {
370 pr_err("Invalid device tree blob header\n");
374 /* First pass, scan for size */
376 size
= (unsigned long)unflatten_dt_node(blob
, NULL
, &start
, NULL
, NULL
, 0);
377 size
= ALIGN(size
, 4);
379 pr_debug(" size is %lx, allocating...\n", size
);
381 /* Allocate memory for the expanded device tree */
382 mem
= dt_alloc(size
+ 4, __alignof__(struct device_node
));
383 memset(mem
, 0, size
);
385 *(__be32
*)(mem
+ size
) = cpu_to_be32(0xdeadbeef);
387 pr_debug(" unflattening %p...\n", mem
);
389 /* Second pass, do actual unflattening */
391 unflatten_dt_node(blob
, mem
, &start
, NULL
, &allnextp
, 0);
392 if (be32_to_cpup(mem
+ size
) != 0xdeadbeef)
393 pr_warning("End of tree marker overwritten: %08x\n",
394 be32_to_cpup(mem
+ size
));
397 pr_debug(" <- unflatten_device_tree()\n");
400 static void *kernel_tree_alloc(u64 size
, u64 align
)
402 return kzalloc(size
, GFP_KERNEL
);
406 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
408 * unflattens the device-tree passed by the firmware, creating the
409 * tree of struct device_node. It also fills the "name" and "type"
410 * pointers of the nodes so the normal device-tree walking functions
413 void of_fdt_unflatten_tree(unsigned long *blob
,
414 struct device_node
**mynodes
)
416 __unflatten_device_tree(blob
, mynodes
, &kernel_tree_alloc
);
418 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree
);
420 /* Everything below here references initial_boot_params directly. */
421 int __initdata dt_root_addr_cells
;
422 int __initdata dt_root_size_cells
;
424 void *initial_boot_params
;
426 #ifdef CONFIG_OF_EARLY_FLATTREE
429 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
431 static int __init
__reserved_mem_reserve_reg(unsigned long node
,
434 int t_len
= (dt_root_addr_cells
+ dt_root_size_cells
) * sizeof(__be32
);
435 phys_addr_t base
, size
;
438 int nomap
, first
= 1;
440 prop
= of_get_flat_dt_prop(node
, "reg", &len
);
444 if (len
&& len
% t_len
!= 0) {
445 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
450 nomap
= of_get_flat_dt_prop(node
, "no-map", NULL
) != NULL
;
452 while (len
>= t_len
) {
453 base
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
454 size
= dt_mem_next_cell(dt_root_size_cells
, &prop
);
457 early_init_dt_reserve_memory_arch(base
, size
, nomap
) == 0)
458 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
459 uname
, &base
, (unsigned long)size
/ SZ_1M
);
461 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
462 uname
, &base
, (unsigned long)size
/ SZ_1M
);
466 fdt_reserved_mem_save_node(node
, uname
, base
, size
);
474 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
475 * in /reserved-memory matches the values supported by the current implementation,
476 * also check if ranges property has been provided
478 static int __init
__reserved_mem_check_root(unsigned long node
)
482 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
483 if (!prop
|| be32_to_cpup(prop
) != dt_root_size_cells
)
486 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
487 if (!prop
|| be32_to_cpup(prop
) != dt_root_addr_cells
)
490 prop
= of_get_flat_dt_prop(node
, "ranges", NULL
);
497 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
499 static int __init
__fdt_scan_reserved_mem(unsigned long node
, const char *uname
,
500 int depth
, void *data
)
506 if (!found
&& depth
== 1 && strcmp(uname
, "reserved-memory") == 0) {
507 if (__reserved_mem_check_root(node
) != 0) {
508 pr_err("Reserved memory: unsupported node format, ignoring\n");
518 } else if (found
&& depth
< 2) {
519 /* scanning of /reserved-memory has been finished */
523 status
= of_get_flat_dt_prop(node
, "status", NULL
);
524 if (status
&& strcmp(status
, "okay") != 0 && strcmp(status
, "ok") != 0)
527 err
= __reserved_mem_reserve_reg(node
, uname
);
528 if (err
== -ENOENT
&& of_get_flat_dt_prop(node
, "size", NULL
))
529 fdt_reserved_mem_save_node(node
, uname
, 0, 0);
536 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
538 * This function grabs memory from early allocator for device exclusive use
539 * defined in device tree structures. It should be called by arch specific code
540 * once the early allocator (i.e. memblock) has been fully activated.
542 void __init
early_init_fdt_scan_reserved_mem(void)
547 if (!initial_boot_params
)
550 /* Reserve the dtb region */
551 early_init_dt_reserve_memory_arch(__pa(initial_boot_params
),
552 fdt_totalsize(initial_boot_params
),
555 /* Process header /memreserve/ fields */
557 fdt_get_mem_rsv(initial_boot_params
, n
, &base
, &size
);
560 early_init_dt_reserve_memory_arch(base
, size
, 0);
563 of_scan_flat_dt(__fdt_scan_reserved_mem
, NULL
);
564 fdt_init_reserved_mem();
568 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
569 * @it: callback function
570 * @data: context data pointer
572 * This function is used to scan the flattened device-tree, it is
573 * used to extract the memory information at boot before we can
576 int __init
of_scan_flat_dt(int (*it
)(unsigned long node
,
577 const char *uname
, int depth
,
581 const void *blob
= initial_boot_params
;
583 int offset
, rc
= 0, depth
= -1;
585 for (offset
= fdt_next_node(blob
, -1, &depth
);
586 offset
>= 0 && depth
>= 0 && !rc
;
587 offset
= fdt_next_node(blob
, offset
, &depth
)) {
589 pathp
= fdt_get_name(blob
, offset
, NULL
);
591 pathp
= kbasename(pathp
);
592 rc
= it(offset
, pathp
, depth
, data
);
598 * of_get_flat_dt_root - find the root node in the flat blob
600 unsigned long __init
of_get_flat_dt_root(void)
606 * of_get_flat_dt_size - Return the total size of the FDT
608 int __init
of_get_flat_dt_size(void)
610 return fdt_totalsize(initial_boot_params
);
614 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
616 * This function can be used within scan_flattened_dt callback to get
617 * access to properties
619 const void *__init
of_get_flat_dt_prop(unsigned long node
, const char *name
,
622 return fdt_getprop(initial_boot_params
, node
, name
, size
);
626 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
627 * @node: node to test
628 * @compat: compatible string to compare with compatible list.
630 int __init
of_flat_dt_is_compatible(unsigned long node
, const char *compat
)
632 return of_fdt_is_compatible(initial_boot_params
, node
, compat
);
636 * of_flat_dt_match - Return true if node matches a list of compatible values
638 int __init
of_flat_dt_match(unsigned long node
, const char *const *compat
)
640 return of_fdt_match(initial_boot_params
, node
, compat
);
643 struct fdt_scan_status
{
648 int (*iterator
)(unsigned long node
, const char *uname
, int depth
, void *data
);
652 const char * __init
of_flat_dt_get_machine_name(void)
655 unsigned long dt_root
= of_get_flat_dt_root();
657 name
= of_get_flat_dt_prop(dt_root
, "model", NULL
);
659 name
= of_get_flat_dt_prop(dt_root
, "compatible", NULL
);
664 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
666 * @default_match: A machine specific ptr to return in case of no match.
667 * @get_next_compat: callback function to return next compatible match table.
669 * Iterate through machine match tables to find the best match for the machine
670 * compatible string in the FDT.
672 const void * __init
of_flat_dt_match_machine(const void *default_match
,
673 const void * (*get_next_compat
)(const char * const**))
675 const void *data
= NULL
;
676 const void *best_data
= default_match
;
677 const char *const *compat
;
678 unsigned long dt_root
;
679 unsigned int best_score
= ~1, score
= 0;
681 dt_root
= of_get_flat_dt_root();
682 while ((data
= get_next_compat(&compat
))) {
683 score
= of_flat_dt_match(dt_root
, compat
);
684 if (score
> 0 && score
< best_score
) {
693 pr_err("\n unrecognized device tree list:\n[ ");
695 prop
= of_get_flat_dt_prop(dt_root
, "compatible", &size
);
698 printk("'%s' ", prop
);
699 size
-= strlen(prop
) + 1;
700 prop
+= strlen(prop
) + 1;
707 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
712 #ifdef CONFIG_BLK_DEV_INITRD
714 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
715 * @node: reference to node containing initrd location ('chosen')
717 static void __init
early_init_dt_check_for_initrd(unsigned long node
)
723 pr_debug("Looking for initrd properties... ");
725 prop
= of_get_flat_dt_prop(node
, "linux,initrd-start", &len
);
728 start
= of_read_number(prop
, len
/4);
730 prop
= of_get_flat_dt_prop(node
, "linux,initrd-end", &len
);
733 end
= of_read_number(prop
, len
/4);
735 initrd_start
= (unsigned long)__va(start
);
736 initrd_end
= (unsigned long)__va(end
);
737 initrd_below_start_ok
= 1;
739 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
740 (unsigned long long)start
, (unsigned long long)end
);
743 static inline void early_init_dt_check_for_initrd(unsigned long node
)
746 #endif /* CONFIG_BLK_DEV_INITRD */
748 #ifdef CONFIG_SERIAL_EARLYCON
749 extern struct of_device_id __earlycon_of_table
[];
751 int __init
early_init_dt_scan_chosen_serial(void)
756 const struct of_device_id
*match
= __earlycon_of_table
;
757 const void *fdt
= initial_boot_params
;
759 offset
= fdt_path_offset(fdt
, "/chosen");
761 offset
= fdt_path_offset(fdt
, "/chosen@0");
765 p
= fdt_getprop(fdt
, offset
, "stdout-path", &l
);
767 p
= fdt_getprop(fdt
, offset
, "linux,stdout-path", &l
);
771 /* Get the node specified by stdout-path */
772 offset
= fdt_path_offset(fdt
, p
);
776 while (match
->compatible
) {
778 if (fdt_node_check_compatible(fdt
, offset
, match
->compatible
)) {
783 addr
= fdt_translate_address(fdt
, offset
);
787 of_setup_earlycon(addr
, match
->data
);
793 static int __init
setup_of_earlycon(char *buf
)
798 return early_init_dt_scan_chosen_serial();
800 early_param("earlycon", setup_of_earlycon
);
804 * early_init_dt_scan_root - fetch the top level address and size cells
806 int __init
early_init_dt_scan_root(unsigned long node
, const char *uname
,
807 int depth
, void *data
)
814 dt_root_size_cells
= OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
815 dt_root_addr_cells
= OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
817 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
819 dt_root_size_cells
= be32_to_cpup(prop
);
820 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells
);
822 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
824 dt_root_addr_cells
= be32_to_cpup(prop
);
825 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells
);
831 u64 __init
dt_mem_next_cell(int s
, const __be32
**cellp
)
833 const __be32
*p
= *cellp
;
836 return of_read_number(p
, s
);
840 * early_init_dt_scan_memory - Look for an parse memory nodes
842 int __init
early_init_dt_scan_memory(unsigned long node
, const char *uname
,
843 int depth
, void *data
)
845 const char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
846 const __be32
*reg
, *endp
;
849 /* We are scanning "memory" nodes only */
852 * The longtrail doesn't have a device_type on the
853 * /memory node, so look for the node called /memory@0.
855 if (!IS_ENABLED(CONFIG_PPC32
) || depth
!= 1 || strcmp(uname
, "memory@0") != 0)
857 } else if (strcmp(type
, "memory") != 0)
860 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
862 reg
= of_get_flat_dt_prop(node
, "reg", &l
);
866 endp
= reg
+ (l
/ sizeof(__be32
));
868 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
869 uname
, l
, reg
[0], reg
[1], reg
[2], reg
[3]);
871 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
874 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
875 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
879 pr_debug(" - %llx , %llx\n", (unsigned long long)base
,
880 (unsigned long long)size
);
882 early_init_dt_add_memory_arch(base
, size
);
888 int __init
early_init_dt_scan_chosen(unsigned long node
, const char *uname
,
889 int depth
, void *data
)
894 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
896 if (depth
!= 1 || !data
||
897 (strcmp(uname
, "chosen") != 0 && strcmp(uname
, "chosen@0") != 0))
900 early_init_dt_check_for_initrd(node
);
902 /* Retrieve command line */
903 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
904 if (p
!= NULL
&& l
> 0)
905 strlcpy(data
, p
, min((int)l
, COMMAND_LINE_SIZE
));
908 * CONFIG_CMDLINE is meant to be a default in case nothing else
909 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
910 * is set in which case we override whatever was found earlier.
912 #ifdef CONFIG_CMDLINE
913 #ifndef CONFIG_CMDLINE_FORCE
914 if (!((char *)data
)[0])
916 strlcpy(data
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
917 #endif /* CONFIG_CMDLINE */
919 pr_debug("Command line is: %s\n", (char*)data
);
925 #ifdef CONFIG_HAVE_MEMBLOCK
926 #define MAX_PHYS_ADDR ((phys_addr_t)~0)
928 void __init __weak
early_init_dt_add_memory_arch(u64 base
, u64 size
)
930 const u64 phys_offset
= __pa(PAGE_OFFSET
);
934 if (base
> MAX_PHYS_ADDR
) {
935 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
940 if (base
+ size
> MAX_PHYS_ADDR
) {
941 pr_warning("Ignoring memory range 0x%lx - 0x%llx\n",
942 ULONG_MAX
, base
+ size
);
943 size
= MAX_PHYS_ADDR
- base
;
946 if (base
+ size
< phys_offset
) {
947 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
951 if (base
< phys_offset
) {
952 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
954 size
-= phys_offset
- base
;
957 memblock_add(base
, size
);
960 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
961 phys_addr_t size
, bool nomap
)
963 if (memblock_is_region_reserved(base
, size
))
966 return memblock_remove(base
, size
);
967 return memblock_reserve(base
, size
);
971 * called from unflatten_device_tree() to bootstrap devicetree itself
972 * Architectures can override this definition if memblock isn't used
974 void * __init __weak
early_init_dt_alloc_memory_arch(u64 size
, u64 align
)
976 return __va(memblock_alloc(size
, align
));
979 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
980 phys_addr_t size
, bool nomap
)
982 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
983 &base
, &size
, nomap
? " (nomap)" : "");
988 bool __init
early_init_dt_verify(void *params
)
993 /* Setup flat device-tree pointer */
994 initial_boot_params
= params
;
996 /* check device tree validity */
997 if (fdt_check_header(params
)) {
998 initial_boot_params
= NULL
;
1006 void __init
early_init_dt_scan_nodes(void)
1008 /* Retrieve various information from the /chosen node */
1009 of_scan_flat_dt(early_init_dt_scan_chosen
, boot_command_line
);
1011 /* Initialize {size,address}-cells info */
1012 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
1014 /* Setup memory, calling early_init_dt_add_memory_arch */
1015 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
1018 bool __init
early_init_dt_scan(void *params
)
1022 status
= early_init_dt_verify(params
);
1026 early_init_dt_scan_nodes();
1031 * unflatten_device_tree - create tree of device_nodes from flat blob
1033 * unflattens the device-tree passed by the firmware, creating the
1034 * tree of struct device_node. It also fills the "name" and "type"
1035 * pointers of the nodes so the normal device-tree walking functions
1038 void __init
unflatten_device_tree(void)
1040 __unflatten_device_tree(initial_boot_params
, &of_allnodes
,
1041 early_init_dt_alloc_memory_arch
);
1043 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
1044 of_alias_scan(early_init_dt_alloc_memory_arch
);
1048 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1050 * Copies and unflattens the device-tree passed by the firmware, creating the
1051 * tree of struct device_node. It also fills the "name" and "type"
1052 * pointers of the nodes so the normal device-tree walking functions
1053 * can be used. This should only be used when the FDT memory has not been
1054 * reserved such is the case when the FDT is built-in to the kernel init
1055 * section. If the FDT memory is reserved already then unflatten_device_tree
1056 * should be used instead.
1058 void __init
unflatten_and_copy_device_tree(void)
1063 if (!initial_boot_params
) {
1064 pr_warn("No valid device tree found, continuing without\n");
1068 size
= fdt_totalsize(initial_boot_params
);
1069 dt
= early_init_dt_alloc_memory_arch(size
,
1070 roundup_pow_of_two(FDT_V17_SIZE
));
1073 memcpy(dt
, initial_boot_params
, size
);
1074 initial_boot_params
= dt
;
1076 unflatten_device_tree();
1079 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1080 static struct debugfs_blob_wrapper flat_dt_blob
;
1082 static int __init
of_flat_dt_debugfs_export_fdt(void)
1084 struct dentry
*d
= debugfs_create_dir("device-tree", NULL
);
1089 flat_dt_blob
.data
= initial_boot_params
;
1090 flat_dt_blob
.size
= fdt_totalsize(initial_boot_params
);
1092 d
= debugfs_create_blob("flat-device-tree", S_IFREG
| S_IRUSR
,
1099 module_init(of_flat_dt_debugfs_export_fdt
);
1102 #endif /* CONFIG_OF_EARLY_FLATTREE */