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 #define pr_fmt(fmt) "OF: fdt: " fmt
14 #include <linux/crc32.h>
15 #include <linux/kernel.h>
16 #include <linux/initrd.h>
17 #include <linux/memblock.h>
18 #include <linux/mutex.h>
20 #include <linux/of_fdt.h>
21 #include <linux/of_reserved_mem.h>
22 #include <linux/sizes.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <linux/slab.h>
26 #include <linux/libfdt.h>
27 #include <linux/debugfs.h>
28 #include <linux/serial_core.h>
29 #include <linux/sysfs.h>
31 #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
34 #include "of_private.h"
37 * of_fdt_limit_memory - limit the number of regions in the /memory node
38 * @limit: maximum entries
40 * Adjust the flattened device tree to have at most 'limit' number of
41 * memory entries in the /memory node. This function may be called
42 * any time after initial_boot_param is set.
44 void of_fdt_limit_memory(int limit
)
49 int nr_address_cells
= OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
50 int nr_size_cells
= OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
51 const __be32
*addr_prop
;
52 const __be32
*size_prop
;
56 root_offset
= fdt_path_offset(initial_boot_params
, "/");
60 addr_prop
= fdt_getprop(initial_boot_params
, root_offset
,
61 "#address-cells", NULL
);
63 nr_address_cells
= fdt32_to_cpu(*addr_prop
);
65 size_prop
= fdt_getprop(initial_boot_params
, root_offset
,
68 nr_size_cells
= fdt32_to_cpu(*size_prop
);
70 cell_size
= sizeof(uint32_t)*(nr_address_cells
+ nr_size_cells
);
72 memory
= fdt_path_offset(initial_boot_params
, "/memory");
74 val
= fdt_getprop(initial_boot_params
, memory
, "reg", &len
);
75 if (len
> limit
*cell_size
) {
76 len
= limit
*cell_size
;
77 pr_debug("Limiting number of entries to %d\n", limit
);
78 fdt_setprop(initial_boot_params
, memory
, "reg", val
,
85 * of_fdt_is_compatible - Return true if given node from the given blob has
86 * compat in its compatible list
87 * @blob: A device tree blob
89 * @compat: compatible string to compare with compatible list.
91 * On match, returns a non-zero value with smaller values returned for more
92 * specific compatible values.
94 static int of_fdt_is_compatible(const void *blob
,
95 unsigned long node
, const char *compat
)
99 unsigned long l
, score
= 0;
101 cp
= fdt_getprop(blob
, node
, "compatible", &cplen
);
106 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
117 * of_fdt_is_big_endian - Return true if given node needs BE MMIO accesses
118 * @blob: A device tree blob
119 * @node: node to test
121 * Returns true if the node has a "big-endian" property, or if the kernel
122 * was compiled for BE *and* the node has a "native-endian" property.
123 * Returns false otherwise.
125 bool of_fdt_is_big_endian(const void *blob
, unsigned long node
)
127 if (fdt_getprop(blob
, node
, "big-endian", NULL
))
129 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
) &&
130 fdt_getprop(blob
, node
, "native-endian", NULL
))
135 static bool of_fdt_device_is_available(const void *blob
, unsigned long node
)
137 const char *status
= fdt_getprop(blob
, node
, "status", NULL
);
142 if (!strcmp(status
, "ok") || !strcmp(status
, "okay"))
149 * of_fdt_match - Return true if node matches a list of compatible values
151 int of_fdt_match(const void *blob
, unsigned long node
,
152 const char *const *compat
)
154 unsigned int tmp
, score
= 0;
160 tmp
= of_fdt_is_compatible(blob
, node
, *compat
);
161 if (tmp
&& (score
== 0 || (tmp
< score
)))
169 static void *unflatten_dt_alloc(void **mem
, unsigned long size
,
174 *mem
= PTR_ALIGN(*mem
, align
);
181 static void populate_properties(const void *blob
,
184 struct device_node
*np
,
185 const char *nodename
,
188 struct property
*pp
, **pprev
= NULL
;
190 bool has_name
= false;
192 pprev
= &np
->properties
;
193 for (cur
= fdt_first_property_offset(blob
, offset
);
195 cur
= fdt_next_property_offset(blob
, cur
)) {
200 val
= fdt_getprop_by_offset(blob
, cur
, &pname
, &sz
);
202 pr_warn("Cannot locate property at 0x%x\n", cur
);
207 pr_warn("Cannot find property name at 0x%x\n", cur
);
211 if (!strcmp(pname
, "name"))
214 pp
= unflatten_dt_alloc(mem
, sizeof(struct property
),
215 __alignof__(struct property
));
219 /* We accept flattened tree phandles either in
220 * ePAPR-style "phandle" properties, or the
221 * legacy "linux,phandle" properties. If both
222 * appear and have different values, things
223 * will get weird. Don't do that.
225 if (!strcmp(pname
, "phandle") ||
226 !strcmp(pname
, "linux,phandle")) {
228 np
->phandle
= be32_to_cpup(val
);
231 /* And we process the "ibm,phandle" property
232 * used in pSeries dynamic device tree
235 if (!strcmp(pname
, "ibm,phandle"))
236 np
->phandle
= be32_to_cpup(val
);
238 pp
->name
= (char *)pname
;
240 pp
->value
= (__be32
*)val
;
245 /* With version 0x10 we may not have the name property,
246 * recreate it here from the unit name if absent
249 const char *p
= nodename
, *ps
= p
, *pa
= NULL
;
255 else if ((*p
) == '/')
263 pp
= unflatten_dt_alloc(mem
, sizeof(struct property
) + len
,
264 __alignof__(struct property
));
271 memcpy(pp
->value
, ps
, len
- 1);
272 ((char *)pp
->value
)[len
- 1] = 0;
273 pr_debug("fixed up name for %s -> %s\n",
274 nodename
, (char *)pp
->value
);
282 static bool populate_node(const void *blob
,
285 struct device_node
*dad
,
286 struct device_node
**pnp
,
289 struct device_node
*np
;
291 unsigned int l
, allocl
;
293 pathp
= fdt_get_name(blob
, offset
, &l
);
301 np
= unflatten_dt_alloc(mem
, sizeof(struct device_node
) + allocl
,
302 __alignof__(struct device_node
));
306 np
->full_name
= fn
= ((char *)np
) + sizeof(*np
);
308 memcpy(fn
, pathp
, l
);
312 np
->sibling
= dad
->child
;
317 populate_properties(blob
, offset
, mem
, np
, pathp
, dryrun
);
319 np
->name
= of_get_property(np
, "name", NULL
);
320 np
->type
= of_get_property(np
, "device_type", NULL
);
332 static void reverse_nodes(struct device_node
*parent
)
334 struct device_node
*child
, *next
;
337 child
= parent
->child
;
339 reverse_nodes(child
);
341 child
= child
->sibling
;
344 /* Reverse the nodes in the child list */
345 child
= parent
->child
;
346 parent
->child
= NULL
;
348 next
= child
->sibling
;
350 child
->sibling
= parent
->child
;
351 parent
->child
= child
;
357 * unflatten_dt_nodes - Alloc and populate a device_node from the flat tree
358 * @blob: The parent device tree blob
359 * @mem: Memory chunk to use for allocating device nodes and properties
360 * @dad: Parent struct device_node
361 * @nodepp: The device_node tree created by the call
363 * It returns the size of unflattened device tree or error code
365 static int unflatten_dt_nodes(const void *blob
,
367 struct device_node
*dad
,
368 struct device_node
**nodepp
)
370 struct device_node
*root
;
371 int offset
= 0, depth
= 0, initial_depth
= 0;
372 #define FDT_MAX_DEPTH 64
373 struct device_node
*nps
[FDT_MAX_DEPTH
];
381 * We're unflattening device sub-tree if @dad is valid. There are
382 * possibly multiple nodes in the first level of depth. We need
383 * set @depth to 1 to make fdt_next_node() happy as it bails
384 * immediately when negative @depth is found. Otherwise, the device
385 * nodes except the first one won't be unflattened successfully.
388 depth
= initial_depth
= 1;
394 offset
>= 0 && depth
>= initial_depth
;
395 offset
= fdt_next_node(blob
, offset
, &depth
)) {
396 if (WARN_ON_ONCE(depth
>= FDT_MAX_DEPTH
))
399 if (!IS_ENABLED(CONFIG_OF_KOBJ
) &&
400 !of_fdt_device_is_available(blob
, offset
))
403 if (!populate_node(blob
, offset
, &mem
, nps
[depth
],
404 &nps
[depth
+1], dryrun
))
407 if (!dryrun
&& nodepp
&& !*nodepp
)
408 *nodepp
= nps
[depth
+1];
409 if (!dryrun
&& !root
)
413 if (offset
< 0 && offset
!= -FDT_ERR_NOTFOUND
) {
414 pr_err("Error %d processing FDT\n", offset
);
419 * Reverse the child list. Some drivers assumes node order matches .dts
429 * __unflatten_device_tree - create tree of device_nodes from flat blob
431 * unflattens a device-tree, creating the
432 * tree of struct device_node. It also fills the "name" and "type"
433 * pointers of the nodes so the normal device-tree walking functions
435 * @blob: The blob to expand
436 * @dad: Parent device node
437 * @mynodes: The device_node tree created by the call
438 * @dt_alloc: An allocator that provides a virtual address to memory
439 * for the resulting tree
440 * @detached: if true set OF_DETACHED on @mynodes
442 * Returns NULL on failure or the memory chunk containing the unflattened
443 * device tree on success.
445 void *__unflatten_device_tree(const void *blob
,
446 struct device_node
*dad
,
447 struct device_node
**mynodes
,
448 void *(*dt_alloc
)(u64 size
, u64 align
),
454 pr_debug(" -> unflatten_device_tree()\n");
457 pr_debug("No device tree pointer\n");
461 pr_debug("Unflattening device tree:\n");
462 pr_debug("magic: %08x\n", fdt_magic(blob
));
463 pr_debug("size: %08x\n", fdt_totalsize(blob
));
464 pr_debug("version: %08x\n", fdt_version(blob
));
466 if (fdt_check_header(blob
)) {
467 pr_err("Invalid device tree blob header\n");
471 /* First pass, scan for size */
472 size
= unflatten_dt_nodes(blob
, NULL
, dad
, NULL
);
476 size
= ALIGN(size
, 4);
477 pr_debug(" size is %d, allocating...\n", size
);
479 /* Allocate memory for the expanded device tree */
480 mem
= dt_alloc(size
+ 4, __alignof__(struct device_node
));
484 memset(mem
, 0, size
);
486 *(__be32
*)(mem
+ size
) = cpu_to_be32(0xdeadbeef);
488 pr_debug(" unflattening %p...\n", mem
);
490 /* Second pass, do actual unflattening */
491 unflatten_dt_nodes(blob
, mem
, dad
, mynodes
);
492 if (be32_to_cpup(mem
+ size
) != 0xdeadbeef)
493 pr_warning("End of tree marker overwritten: %08x\n",
494 be32_to_cpup(mem
+ size
));
496 if (detached
&& mynodes
) {
497 of_node_set_flag(*mynodes
, OF_DETACHED
);
498 pr_debug("unflattened tree is detached\n");
501 pr_debug(" <- unflatten_device_tree()\n");
505 static void *kernel_tree_alloc(u64 size
, u64 align
)
507 return kzalloc(size
, GFP_KERNEL
);
510 static DEFINE_MUTEX(of_fdt_unflatten_mutex
);
513 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
514 * @blob: Flat device tree blob
515 * @dad: Parent device node
516 * @mynodes: The device tree created by the call
518 * unflattens the device-tree passed by the firmware, creating the
519 * tree of struct device_node. It also fills the "name" and "type"
520 * pointers of the nodes so the normal device-tree walking functions
523 * Returns NULL on failure or the memory chunk containing the unflattened
524 * device tree on success.
526 void *of_fdt_unflatten_tree(const unsigned long *blob
,
527 struct device_node
*dad
,
528 struct device_node
**mynodes
)
532 mutex_lock(&of_fdt_unflatten_mutex
);
533 mem
= __unflatten_device_tree(blob
, dad
, mynodes
, &kernel_tree_alloc
,
535 mutex_unlock(&of_fdt_unflatten_mutex
);
539 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree
);
541 /* Everything below here references initial_boot_params directly. */
542 int __initdata dt_root_addr_cells
;
543 int __initdata dt_root_size_cells
;
545 void *initial_boot_params
;
547 #ifdef CONFIG_OF_EARLY_FLATTREE
549 static u32 of_fdt_crc32
;
552 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
554 static int __init
__reserved_mem_reserve_reg(unsigned long node
,
557 int t_len
= (dt_root_addr_cells
+ dt_root_size_cells
) * sizeof(__be32
);
558 phys_addr_t base
, size
;
561 int nomap
, first
= 1;
563 prop
= of_get_flat_dt_prop(node
, "reg", &len
);
567 if (len
&& len
% t_len
!= 0) {
568 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
573 nomap
= of_get_flat_dt_prop(node
, "no-map", NULL
) != NULL
;
575 while (len
>= t_len
) {
576 base
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
577 size
= dt_mem_next_cell(dt_root_size_cells
, &prop
);
580 early_init_dt_reserve_memory_arch(base
, size
, nomap
) == 0)
581 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
582 uname
, &base
, (unsigned long)size
/ SZ_1M
);
584 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
585 uname
, &base
, (unsigned long)size
/ SZ_1M
);
589 fdt_reserved_mem_save_node(node
, uname
, base
, size
);
597 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
598 * in /reserved-memory matches the values supported by the current implementation,
599 * also check if ranges property has been provided
601 static int __init
__reserved_mem_check_root(unsigned long node
)
605 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
606 if (!prop
|| be32_to_cpup(prop
) != dt_root_size_cells
)
609 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
610 if (!prop
|| be32_to_cpup(prop
) != dt_root_addr_cells
)
613 prop
= of_get_flat_dt_prop(node
, "ranges", NULL
);
620 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
622 static int __init
__fdt_scan_reserved_mem(unsigned long node
, const char *uname
,
623 int depth
, void *data
)
628 if (!found
&& depth
== 1 && strcmp(uname
, "reserved-memory") == 0) {
629 if (__reserved_mem_check_root(node
) != 0) {
630 pr_err("Reserved memory: unsupported node format, ignoring\n");
640 } else if (found
&& depth
< 2) {
641 /* scanning of /reserved-memory has been finished */
645 if (!of_fdt_device_is_available(initial_boot_params
, node
))
648 err
= __reserved_mem_reserve_reg(node
, uname
);
649 if (err
== -ENOENT
&& of_get_flat_dt_prop(node
, "size", NULL
))
650 fdt_reserved_mem_save_node(node
, uname
, 0, 0);
657 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
659 * This function grabs memory from early allocator for device exclusive use
660 * defined in device tree structures. It should be called by arch specific code
661 * once the early allocator (i.e. memblock) has been fully activated.
663 void __init
early_init_fdt_scan_reserved_mem(void)
668 if (!initial_boot_params
)
671 /* Process header /memreserve/ fields */
673 fdt_get_mem_rsv(initial_boot_params
, n
, &base
, &size
);
676 early_init_dt_reserve_memory_arch(base
, size
, 0);
679 of_scan_flat_dt(__fdt_scan_reserved_mem
, NULL
);
680 fdt_init_reserved_mem();
684 * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
686 void __init
early_init_fdt_reserve_self(void)
688 if (!initial_boot_params
)
691 /* Reserve the dtb region */
692 early_init_dt_reserve_memory_arch(__pa(initial_boot_params
),
693 fdt_totalsize(initial_boot_params
),
698 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
699 * @it: callback function
700 * @data: context data pointer
702 * This function is used to scan the flattened device-tree, it is
703 * used to extract the memory information at boot before we can
706 int __init
of_scan_flat_dt(int (*it
)(unsigned long node
,
707 const char *uname
, int depth
,
711 const void *blob
= initial_boot_params
;
713 int offset
, rc
= 0, depth
= -1;
718 for (offset
= fdt_next_node(blob
, -1, &depth
);
719 offset
>= 0 && depth
>= 0 && !rc
;
720 offset
= fdt_next_node(blob
, offset
, &depth
)) {
722 pathp
= fdt_get_name(blob
, offset
, NULL
);
724 pathp
= kbasename(pathp
);
725 rc
= it(offset
, pathp
, depth
, data
);
731 * of_scan_flat_dt_subnodes - scan sub-nodes of a node call callback on each.
732 * @it: callback function
733 * @data: context data pointer
735 * This function is used to scan sub-nodes of a node.
737 int __init
of_scan_flat_dt_subnodes(unsigned long parent
,
738 int (*it
)(unsigned long node
,
743 const void *blob
= initial_boot_params
;
746 fdt_for_each_subnode(node
, blob
, parent
) {
750 pathp
= fdt_get_name(blob
, node
, NULL
);
752 pathp
= kbasename(pathp
);
753 rc
= it(node
, pathp
, data
);
761 * of_get_flat_dt_subnode_by_name - get the subnode by given name
763 * @node: the parent node
764 * @uname: the name of subnode
765 * @return offset of the subnode, or -FDT_ERR_NOTFOUND if there is none
768 int of_get_flat_dt_subnode_by_name(unsigned long node
, const char *uname
)
770 return fdt_subnode_offset(initial_boot_params
, node
, uname
);
774 * of_get_flat_dt_root - find the root node in the flat blob
776 unsigned long __init
of_get_flat_dt_root(void)
782 * of_get_flat_dt_size - Return the total size of the FDT
784 int __init
of_get_flat_dt_size(void)
786 return fdt_totalsize(initial_boot_params
);
790 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
792 * This function can be used within scan_flattened_dt callback to get
793 * access to properties
795 const void *__init
of_get_flat_dt_prop(unsigned long node
, const char *name
,
798 return fdt_getprop(initial_boot_params
, node
, name
, size
);
802 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
803 * @node: node to test
804 * @compat: compatible string to compare with compatible list.
806 int __init
of_flat_dt_is_compatible(unsigned long node
, const char *compat
)
808 return of_fdt_is_compatible(initial_boot_params
, node
, compat
);
812 * of_flat_dt_match - Return true if node matches a list of compatible values
814 int __init
of_flat_dt_match(unsigned long node
, const char *const *compat
)
816 return of_fdt_match(initial_boot_params
, node
, compat
);
820 * of_get_flat_dt_prop - Given a node in the flat blob, return the phandle
822 uint32_t __init
of_get_flat_dt_phandle(unsigned long node
)
824 return fdt_get_phandle(initial_boot_params
, node
);
827 struct fdt_scan_status
{
832 int (*iterator
)(unsigned long node
, const char *uname
, int depth
, void *data
);
836 const char * __init
of_flat_dt_get_machine_name(void)
839 unsigned long dt_root
= of_get_flat_dt_root();
841 name
= of_get_flat_dt_prop(dt_root
, "model", NULL
);
843 name
= of_get_flat_dt_prop(dt_root
, "compatible", NULL
);
848 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
850 * @default_match: A machine specific ptr to return in case of no match.
851 * @get_next_compat: callback function to return next compatible match table.
853 * Iterate through machine match tables to find the best match for the machine
854 * compatible string in the FDT.
856 const void * __init
of_flat_dt_match_machine(const void *default_match
,
857 const void * (*get_next_compat
)(const char * const**))
859 const void *data
= NULL
;
860 const void *best_data
= default_match
;
861 const char *const *compat
;
862 unsigned long dt_root
;
863 unsigned int best_score
= ~1, score
= 0;
865 dt_root
= of_get_flat_dt_root();
866 while ((data
= get_next_compat(&compat
))) {
867 score
= of_flat_dt_match(dt_root
, compat
);
868 if (score
> 0 && score
< best_score
) {
877 pr_err("\n unrecognized device tree list:\n[ ");
879 prop
= of_get_flat_dt_prop(dt_root
, "compatible", &size
);
882 printk("'%s' ", prop
);
883 size
-= strlen(prop
) + 1;
884 prop
+= strlen(prop
) + 1;
891 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
896 #ifdef CONFIG_BLK_DEV_INITRD
897 #ifndef __early_init_dt_declare_initrd
898 static void __early_init_dt_declare_initrd(unsigned long start
,
901 initrd_start
= (unsigned long)__va(start
);
902 initrd_end
= (unsigned long)__va(end
);
903 initrd_below_start_ok
= 1;
908 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
909 * @node: reference to node containing initrd location ('chosen')
911 static void __init
early_init_dt_check_for_initrd(unsigned long node
)
917 pr_debug("Looking for initrd properties... ");
919 prop
= of_get_flat_dt_prop(node
, "linux,initrd-start", &len
);
922 start
= of_read_number(prop
, len
/4);
924 prop
= of_get_flat_dt_prop(node
, "linux,initrd-end", &len
);
927 end
= of_read_number(prop
, len
/4);
929 __early_init_dt_declare_initrd(start
, end
);
931 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
932 (unsigned long long)start
, (unsigned long long)end
);
935 static inline void early_init_dt_check_for_initrd(unsigned long node
)
938 #endif /* CONFIG_BLK_DEV_INITRD */
940 #ifdef CONFIG_SERIAL_EARLYCON
942 int __init
early_init_dt_scan_chosen_stdout(void)
945 const char *p
, *q
, *options
= NULL
;
947 const struct earlycon_id
*match
;
948 const void *fdt
= initial_boot_params
;
950 offset
= fdt_path_offset(fdt
, "/chosen");
952 offset
= fdt_path_offset(fdt
, "/chosen@0");
956 p
= fdt_getprop(fdt
, offset
, "stdout-path", &l
);
958 p
= fdt_getprop(fdt
, offset
, "linux,stdout-path", &l
);
962 q
= strchrnul(p
, ':');
967 /* Get the node specified by stdout-path */
968 offset
= fdt_path_offset_namelen(fdt
, p
, l
);
970 pr_warn("earlycon: stdout-path %.*s not found\n", l
, p
);
974 for (match
= __earlycon_table
; match
< __earlycon_table_end
; match
++) {
975 if (!match
->compatible
[0])
978 if (fdt_node_check_compatible(fdt
, offset
, match
->compatible
))
981 of_setup_earlycon(match
, offset
, options
);
989 * early_init_dt_scan_root - fetch the top level address and size cells
991 int __init
early_init_dt_scan_root(unsigned long node
, const char *uname
,
992 int depth
, void *data
)
999 dt_root_size_cells
= OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
1000 dt_root_addr_cells
= OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
1002 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
1004 dt_root_size_cells
= be32_to_cpup(prop
);
1005 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells
);
1007 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
1009 dt_root_addr_cells
= be32_to_cpup(prop
);
1010 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells
);
1016 u64 __init
dt_mem_next_cell(int s
, const __be32
**cellp
)
1018 const __be32
*p
= *cellp
;
1021 return of_read_number(p
, s
);
1025 * early_init_dt_scan_memory - Look for and parse memory nodes
1027 int __init
early_init_dt_scan_memory(unsigned long node
, const char *uname
,
1028 int depth
, void *data
)
1030 const char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
1031 const __be32
*reg
, *endp
;
1035 /* We are scanning "memory" nodes only */
1038 * The longtrail doesn't have a device_type on the
1039 * /memory node, so look for the node called /memory@0.
1041 if (!IS_ENABLED(CONFIG_PPC32
) || depth
!= 1 || strcmp(uname
, "memory@0") != 0)
1043 } else if (strcmp(type
, "memory") != 0)
1046 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
1048 reg
= of_get_flat_dt_prop(node
, "reg", &l
);
1052 endp
= reg
+ (l
/ sizeof(__be32
));
1053 hotpluggable
= of_get_flat_dt_prop(node
, "hotpluggable", NULL
);
1055 pr_debug("memory scan node %s, reg size %d,\n", uname
, l
);
1057 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
1060 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
1061 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
1065 pr_debug(" - %llx , %llx\n", (unsigned long long)base
,
1066 (unsigned long long)size
);
1068 early_init_dt_add_memory_arch(base
, size
);
1073 if (early_init_dt_mark_hotplug_memory_arch(base
, size
))
1074 pr_warn("failed to mark hotplug range 0x%llx - 0x%llx\n",
1081 int __init
early_init_dt_scan_chosen(unsigned long node
, const char *uname
,
1082 int depth
, void *data
)
1087 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
1089 if (depth
!= 1 || !data
||
1090 (strcmp(uname
, "chosen") != 0 && strcmp(uname
, "chosen@0") != 0))
1093 early_init_dt_check_for_initrd(node
);
1095 /* Retrieve command line */
1096 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
1097 if (p
!= NULL
&& l
> 0)
1098 strlcpy(data
, p
, min((int)l
, COMMAND_LINE_SIZE
));
1101 * CONFIG_CMDLINE is meant to be a default in case nothing else
1102 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
1103 * is set in which case we override whatever was found earlier.
1105 #ifdef CONFIG_CMDLINE
1106 #if defined(CONFIG_CMDLINE_EXTEND)
1107 strlcat(data
, " ", COMMAND_LINE_SIZE
);
1108 strlcat(data
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
1109 #elif defined(CONFIG_CMDLINE_FORCE)
1110 strlcpy(data
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
1112 /* No arguments from boot loader, use kernel's cmdl*/
1113 if (!((char *)data
)[0])
1114 strlcpy(data
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
1116 #endif /* CONFIG_CMDLINE */
1118 pr_debug("Command line is: %s\n", (char*)data
);
1124 #ifdef CONFIG_HAVE_MEMBLOCK
1125 #ifndef MIN_MEMBLOCK_ADDR
1126 #define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET)
1128 #ifndef MAX_MEMBLOCK_ADDR
1129 #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
1132 void __init __weak
early_init_dt_add_memory_arch(u64 base
, u64 size
)
1134 const u64 phys_offset
= MIN_MEMBLOCK_ADDR
;
1136 if (!PAGE_ALIGNED(base
)) {
1137 if (size
< PAGE_SIZE
- (base
& ~PAGE_MASK
)) {
1138 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
1142 size
-= PAGE_SIZE
- (base
& ~PAGE_MASK
);
1143 base
= PAGE_ALIGN(base
);
1147 if (base
> MAX_MEMBLOCK_ADDR
) {
1148 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1153 if (base
+ size
- 1 > MAX_MEMBLOCK_ADDR
) {
1154 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1155 ((u64
)MAX_MEMBLOCK_ADDR
) + 1, base
+ size
);
1156 size
= MAX_MEMBLOCK_ADDR
- base
+ 1;
1159 if (base
+ size
< phys_offset
) {
1160 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1164 if (base
< phys_offset
) {
1165 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1167 size
-= phys_offset
- base
;
1170 memblock_add(base
, size
);
1173 int __init __weak
early_init_dt_mark_hotplug_memory_arch(u64 base
, u64 size
)
1175 return memblock_mark_hotplug(base
, size
);
1178 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
1179 phys_addr_t size
, bool nomap
)
1182 return memblock_remove(base
, size
);
1183 return memblock_reserve(base
, size
);
1187 * called from unflatten_device_tree() to bootstrap devicetree itself
1188 * Architectures can override this definition if memblock isn't used
1190 void * __init __weak
early_init_dt_alloc_memory_arch(u64 size
, u64 align
)
1192 return __va(memblock_alloc(size
, align
));
1195 void __init __weak
early_init_dt_add_memory_arch(u64 base
, u64 size
)
1200 int __init __weak
early_init_dt_mark_hotplug_memory_arch(u64 base
, u64 size
)
1205 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
1206 phys_addr_t size
, bool nomap
)
1208 pr_err("Reserved memory not supported, ignoring range %pa - %pa%s\n",
1209 &base
, &size
, nomap
? " (nomap)" : "");
1213 void * __init __weak
early_init_dt_alloc_memory_arch(u64 size
, u64 align
)
1220 bool __init
early_init_dt_verify(void *params
)
1225 /* check device tree validity */
1226 if (fdt_check_header(params
))
1229 /* Setup flat device-tree pointer */
1230 initial_boot_params
= params
;
1231 of_fdt_crc32
= crc32_be(~0, initial_boot_params
,
1232 fdt_totalsize(initial_boot_params
));
1237 void __init
early_init_dt_scan_nodes(void)
1239 /* Retrieve various information from the /chosen node */
1240 of_scan_flat_dt(early_init_dt_scan_chosen
, boot_command_line
);
1242 /* Initialize {size,address}-cells info */
1243 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
1245 /* Setup memory, calling early_init_dt_add_memory_arch */
1246 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
1249 bool __init
early_init_dt_scan(void *params
)
1253 status
= early_init_dt_verify(params
);
1257 early_init_dt_scan_nodes();
1262 * unflatten_device_tree - create tree of device_nodes from flat blob
1264 * unflattens the device-tree passed by the firmware, creating the
1265 * tree of struct device_node. It also fills the "name" and "type"
1266 * pointers of the nodes so the normal device-tree walking functions
1269 void __init
unflatten_device_tree(void)
1271 __unflatten_device_tree(initial_boot_params
, NULL
, &of_root
,
1272 early_init_dt_alloc_memory_arch
, false);
1274 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
1275 of_alias_scan(early_init_dt_alloc_memory_arch
);
1277 unittest_unflatten_overlay_base();
1281 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1283 * Copies and unflattens the device-tree passed by the firmware, creating the
1284 * tree of struct device_node. It also fills the "name" and "type"
1285 * pointers of the nodes so the normal device-tree walking functions
1286 * can be used. This should only be used when the FDT memory has not been
1287 * reserved such is the case when the FDT is built-in to the kernel init
1288 * section. If the FDT memory is reserved already then unflatten_device_tree
1289 * should be used instead.
1291 void __init
unflatten_and_copy_device_tree(void)
1296 if (!initial_boot_params
) {
1297 pr_warn("No valid device tree found, continuing without\n");
1301 size
= fdt_totalsize(initial_boot_params
);
1302 dt
= early_init_dt_alloc_memory_arch(size
,
1303 roundup_pow_of_two(FDT_V17_SIZE
));
1306 memcpy(dt
, initial_boot_params
, size
);
1307 initial_boot_params
= dt
;
1309 unflatten_device_tree();
1313 static ssize_t
of_fdt_raw_read(struct file
*filp
, struct kobject
*kobj
,
1314 struct bin_attribute
*bin_attr
,
1315 char *buf
, loff_t off
, size_t count
)
1317 memcpy(buf
, initial_boot_params
+ off
, count
);
1321 static int __init
of_fdt_raw_init(void)
1323 static struct bin_attribute of_fdt_raw_attr
=
1324 __BIN_ATTR(fdt
, S_IRUSR
, of_fdt_raw_read
, NULL
, 0);
1326 if (!initial_boot_params
)
1329 if (of_fdt_crc32
!= crc32_be(~0, initial_boot_params
,
1330 fdt_totalsize(initial_boot_params
))) {
1331 pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n");
1334 of_fdt_raw_attr
.size
= fdt_totalsize(initial_boot_params
);
1335 return sysfs_create_bin_file(firmware_kobj
, &of_fdt_raw_attr
);
1337 late_initcall(of_fdt_raw_init
);
1340 #endif /* CONFIG_OF_EARLY_FLATTREE */