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 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
))
136 * of_fdt_match - Return true if node matches a list of compatible values
138 int of_fdt_match(const void *blob
, unsigned long node
,
139 const char *const *compat
)
141 unsigned int tmp
, score
= 0;
147 tmp
= of_fdt_is_compatible(blob
, node
, *compat
);
148 if (tmp
&& (score
== 0 || (tmp
< score
)))
156 static void *unflatten_dt_alloc(void **mem
, unsigned long size
,
161 *mem
= PTR_ALIGN(*mem
, align
);
168 static void populate_properties(const void *blob
,
171 struct device_node
*np
,
172 const char *nodename
,
175 struct property
*pp
, **pprev
= NULL
;
177 bool has_name
= false;
179 pprev
= &np
->properties
;
180 for (cur
= fdt_first_property_offset(blob
, offset
);
182 cur
= fdt_next_property_offset(blob
, cur
)) {
187 val
= fdt_getprop_by_offset(blob
, cur
, &pname
, &sz
);
189 pr_warn("Cannot locate property at 0x%x\n", cur
);
194 pr_warn("Cannot find property name at 0x%x\n", cur
);
198 if (!strcmp(pname
, "name"))
201 pp
= unflatten_dt_alloc(mem
, sizeof(struct property
),
202 __alignof__(struct property
));
206 /* We accept flattened tree phandles either in
207 * ePAPR-style "phandle" properties, or the
208 * legacy "linux,phandle" properties. If both
209 * appear and have different values, things
210 * will get weird. Don't do that.
212 if (!strcmp(pname
, "phandle") ||
213 !strcmp(pname
, "linux,phandle")) {
215 np
->phandle
= be32_to_cpup(val
);
218 /* And we process the "ibm,phandle" property
219 * used in pSeries dynamic device tree
222 if (!strcmp(pname
, "ibm,phandle"))
223 np
->phandle
= be32_to_cpup(val
);
225 pp
->name
= (char *)pname
;
227 pp
->value
= (__be32
*)val
;
232 /* With version 0x10 we may not have the name property,
233 * recreate it here from the unit name if absent
236 const char *p
= nodename
, *ps
= p
, *pa
= NULL
;
242 else if ((*p
) == '/')
250 pp
= unflatten_dt_alloc(mem
, sizeof(struct property
) + len
,
251 __alignof__(struct property
));
258 memcpy(pp
->value
, ps
, len
- 1);
259 ((char *)pp
->value
)[len
- 1] = 0;
260 pr_debug("fixed up name for %s -> %s\n",
261 nodename
, (char *)pp
->value
);
269 static unsigned int populate_node(const void *blob
,
272 struct device_node
*dad
,
274 struct device_node
**pnp
,
277 struct device_node
*np
;
279 unsigned int l
, allocl
;
282 pathp
= fdt_get_name(blob
, offset
, &l
);
290 /* version 0x10 has a more compact unit name here instead of the full
291 * path. we accumulate the full path size using "fpsize", we'll rebuild
292 * it later. We detect this because the first character of the name is
295 if ((*pathp
) != '/') {
298 /* root node: special case. fpsize accounts for path
299 * plus terminating zero. root node only has '/', so
300 * fpsize should be 2, but we want to avoid the first
301 * level nodes to have two '/' so we use fpsize 1 here
308 /* account for '/' and path size minus terminal 0
316 np
= unflatten_dt_alloc(mem
, sizeof(struct device_node
) + allocl
,
317 __alignof__(struct device_node
));
321 np
->full_name
= fn
= ((char *)np
) + sizeof(*np
);
323 /* rebuild full path for new format */
324 if (dad
&& dad
->parent
) {
325 strcpy(fn
, dad
->full_name
);
327 if ((strlen(fn
) + l
+ 1) != allocl
) {
328 pr_debug("%s: p: %d, l: %d, a: %d\n",
329 pathp
, (int)strlen(fn
),
337 memcpy(fn
, pathp
, l
);
341 np
->sibling
= dad
->child
;
346 populate_properties(blob
, offset
, mem
, np
, pathp
, dryrun
);
348 np
->name
= of_get_property(np
, "name", NULL
);
349 np
->type
= of_get_property(np
, "device_type", NULL
);
361 static void reverse_nodes(struct device_node
*parent
)
363 struct device_node
*child
, *next
;
366 child
= parent
->child
;
368 reverse_nodes(child
);
370 child
= child
->sibling
;
373 /* Reverse the nodes in the child list */
374 child
= parent
->child
;
375 parent
->child
= NULL
;
377 next
= child
->sibling
;
379 child
->sibling
= parent
->child
;
380 parent
->child
= child
;
386 * unflatten_dt_nodes - Alloc and populate a device_node from the flat tree
387 * @blob: The parent device tree blob
388 * @mem: Memory chunk to use for allocating device nodes and properties
389 * @dad: Parent struct device_node
390 * @nodepp: The device_node tree created by the call
392 * It returns the size of unflattened device tree or error code
394 static int unflatten_dt_nodes(const void *blob
,
396 struct device_node
*dad
,
397 struct device_node
**nodepp
)
399 struct device_node
*root
;
400 int offset
= 0, depth
= 0, initial_depth
= 0;
401 #define FDT_MAX_DEPTH 64
402 unsigned int fpsizes
[FDT_MAX_DEPTH
];
403 struct device_node
*nps
[FDT_MAX_DEPTH
];
411 * We're unflattening device sub-tree if @dad is valid. There are
412 * possibly multiple nodes in the first level of depth. We need
413 * set @depth to 1 to make fdt_next_node() happy as it bails
414 * immediately when negative @depth is found. Otherwise, the device
415 * nodes except the first one won't be unflattened successfully.
418 depth
= initial_depth
= 1;
421 fpsizes
[depth
] = dad
? strlen(of_node_full_name(dad
)) : 0;
425 offset
>= 0 && depth
>= initial_depth
;
426 offset
= fdt_next_node(blob
, offset
, &depth
)) {
427 if (WARN_ON_ONCE(depth
>= FDT_MAX_DEPTH
))
430 fpsizes
[depth
+1] = populate_node(blob
, offset
, &mem
,
433 &nps
[depth
+1], dryrun
);
434 if (!fpsizes
[depth
+1])
437 if (!dryrun
&& nodepp
&& !*nodepp
)
438 *nodepp
= nps
[depth
+1];
439 if (!dryrun
&& !root
)
443 if (offset
< 0 && offset
!= -FDT_ERR_NOTFOUND
) {
444 pr_err("Error %d processing FDT\n", offset
);
449 * Reverse the child list. Some drivers assumes node order matches .dts
459 * __unflatten_device_tree - create tree of device_nodes from flat blob
461 * unflattens a device-tree, creating the
462 * tree of struct device_node. It also fills the "name" and "type"
463 * pointers of the nodes so the normal device-tree walking functions
465 * @blob: The blob to expand
466 * @dad: Parent device node
467 * @mynodes: The device_node tree created by the call
468 * @dt_alloc: An allocator that provides a virtual address to memory
469 * for the resulting tree
471 * Returns NULL on failure or the memory chunk containing the unflattened
472 * device tree on success.
474 void *__unflatten_device_tree(const void *blob
,
475 struct device_node
*dad
,
476 struct device_node
**mynodes
,
477 void *(*dt_alloc
)(u64 size
, u64 align
),
483 pr_debug(" -> unflatten_device_tree()\n");
486 pr_debug("No device tree pointer\n");
490 pr_debug("Unflattening device tree:\n");
491 pr_debug("magic: %08x\n", fdt_magic(blob
));
492 pr_debug("size: %08x\n", fdt_totalsize(blob
));
493 pr_debug("version: %08x\n", fdt_version(blob
));
495 if (fdt_check_header(blob
)) {
496 pr_err("Invalid device tree blob header\n");
500 /* First pass, scan for size */
501 size
= unflatten_dt_nodes(blob
, NULL
, dad
, NULL
);
505 size
= ALIGN(size
, 4);
506 pr_debug(" size is %d, allocating...\n", size
);
508 /* Allocate memory for the expanded device tree */
509 mem
= dt_alloc(size
+ 4, __alignof__(struct device_node
));
510 memset(mem
, 0, size
);
512 *(__be32
*)(mem
+ size
) = cpu_to_be32(0xdeadbeef);
514 pr_debug(" unflattening %p...\n", mem
);
516 /* Second pass, do actual unflattening */
517 unflatten_dt_nodes(blob
, mem
, dad
, mynodes
);
518 if (be32_to_cpup(mem
+ size
) != 0xdeadbeef)
519 pr_warning("End of tree marker overwritten: %08x\n",
520 be32_to_cpup(mem
+ size
));
522 if (detached
&& mynodes
) {
523 of_node_set_flag(*mynodes
, OF_DETACHED
);
524 pr_debug("unflattened tree is detached\n");
527 pr_debug(" <- unflatten_device_tree()\n");
531 static void *kernel_tree_alloc(u64 size
, u64 align
)
533 return kzalloc(size
, GFP_KERNEL
);
536 static DEFINE_MUTEX(of_fdt_unflatten_mutex
);
539 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
540 * @blob: Flat device tree blob
541 * @dad: Parent device node
542 * @mynodes: The device tree created by the call
544 * unflattens the device-tree passed by the firmware, creating the
545 * tree of struct device_node. It also fills the "name" and "type"
546 * pointers of the nodes so the normal device-tree walking functions
549 * Returns NULL on failure or the memory chunk containing the unflattened
550 * device tree on success.
552 void *of_fdt_unflatten_tree(const unsigned long *blob
,
553 struct device_node
*dad
,
554 struct device_node
**mynodes
)
558 mutex_lock(&of_fdt_unflatten_mutex
);
559 mem
= __unflatten_device_tree(blob
, dad
, mynodes
, &kernel_tree_alloc
,
561 mutex_unlock(&of_fdt_unflatten_mutex
);
565 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree
);
567 /* Everything below here references initial_boot_params directly. */
568 int __initdata dt_root_addr_cells
;
569 int __initdata dt_root_size_cells
;
571 void *initial_boot_params
;
573 #ifdef CONFIG_OF_EARLY_FLATTREE
575 static u32 of_fdt_crc32
;
578 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
580 static int __init
__reserved_mem_reserve_reg(unsigned long node
,
583 int t_len
= (dt_root_addr_cells
+ dt_root_size_cells
) * sizeof(__be32
);
584 phys_addr_t base
, size
;
587 int nomap
, first
= 1;
589 prop
= of_get_flat_dt_prop(node
, "reg", &len
);
593 if (len
&& len
% t_len
!= 0) {
594 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
599 nomap
= of_get_flat_dt_prop(node
, "no-map", NULL
) != NULL
;
601 while (len
>= t_len
) {
602 base
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
603 size
= dt_mem_next_cell(dt_root_size_cells
, &prop
);
606 early_init_dt_reserve_memory_arch(base
, size
, nomap
) == 0)
607 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
608 uname
, &base
, (unsigned long)size
/ SZ_1M
);
610 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
611 uname
, &base
, (unsigned long)size
/ SZ_1M
);
615 fdt_reserved_mem_save_node(node
, uname
, base
, size
);
623 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
624 * in /reserved-memory matches the values supported by the current implementation,
625 * also check if ranges property has been provided
627 static int __init
__reserved_mem_check_root(unsigned long node
)
631 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
632 if (!prop
|| be32_to_cpup(prop
) != dt_root_size_cells
)
635 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
636 if (!prop
|| be32_to_cpup(prop
) != dt_root_addr_cells
)
639 prop
= of_get_flat_dt_prop(node
, "ranges", NULL
);
646 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
648 static int __init
__fdt_scan_reserved_mem(unsigned long node
, const char *uname
,
649 int depth
, void *data
)
655 if (!found
&& depth
== 1 && strcmp(uname
, "reserved-memory") == 0) {
656 if (__reserved_mem_check_root(node
) != 0) {
657 pr_err("Reserved memory: unsupported node format, ignoring\n");
667 } else if (found
&& depth
< 2) {
668 /* scanning of /reserved-memory has been finished */
672 status
= of_get_flat_dt_prop(node
, "status", NULL
);
673 if (status
&& strcmp(status
, "okay") != 0 && strcmp(status
, "ok") != 0)
676 err
= __reserved_mem_reserve_reg(node
, uname
);
677 if (err
== -ENOENT
&& of_get_flat_dt_prop(node
, "size", NULL
))
678 fdt_reserved_mem_save_node(node
, uname
, 0, 0);
685 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
687 * This function grabs memory from early allocator for device exclusive use
688 * defined in device tree structures. It should be called by arch specific code
689 * once the early allocator (i.e. memblock) has been fully activated.
691 void __init
early_init_fdt_scan_reserved_mem(void)
696 if (!initial_boot_params
)
699 /* Process header /memreserve/ fields */
701 fdt_get_mem_rsv(initial_boot_params
, n
, &base
, &size
);
704 early_init_dt_reserve_memory_arch(base
, size
, 0);
707 of_scan_flat_dt(__fdt_scan_reserved_mem
, NULL
);
708 fdt_init_reserved_mem();
712 * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
714 void __init
early_init_fdt_reserve_self(void)
716 if (!initial_boot_params
)
719 /* Reserve the dtb region */
720 early_init_dt_reserve_memory_arch(__pa(initial_boot_params
),
721 fdt_totalsize(initial_boot_params
),
726 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
727 * @it: callback function
728 * @data: context data pointer
730 * This function is used to scan the flattened device-tree, it is
731 * used to extract the memory information at boot before we can
734 int __init
of_scan_flat_dt(int (*it
)(unsigned long node
,
735 const char *uname
, int depth
,
739 const void *blob
= initial_boot_params
;
741 int offset
, rc
= 0, depth
= -1;
746 for (offset
= fdt_next_node(blob
, -1, &depth
);
747 offset
>= 0 && depth
>= 0 && !rc
;
748 offset
= fdt_next_node(blob
, offset
, &depth
)) {
750 pathp
= fdt_get_name(blob
, offset
, NULL
);
752 pathp
= kbasename(pathp
);
753 rc
= it(offset
, pathp
, depth
, data
);
759 * of_scan_flat_dt_subnodes - scan sub-nodes of a node call callback on each.
760 * @it: callback function
761 * @data: context data pointer
763 * This function is used to scan sub-nodes of a node.
765 int __init
of_scan_flat_dt_subnodes(unsigned long parent
,
766 int (*it
)(unsigned long node
,
771 const void *blob
= initial_boot_params
;
774 fdt_for_each_subnode(node
, blob
, parent
) {
778 pathp
= fdt_get_name(blob
, node
, NULL
);
780 pathp
= kbasename(pathp
);
781 rc
= it(node
, pathp
, data
);
789 * of_get_flat_dt_subnode_by_name - get the subnode by given name
791 * @node: the parent node
792 * @uname: the name of subnode
793 * @return offset of the subnode, or -FDT_ERR_NOTFOUND if there is none
796 int of_get_flat_dt_subnode_by_name(unsigned long node
, const char *uname
)
798 return fdt_subnode_offset(initial_boot_params
, node
, uname
);
802 * of_get_flat_dt_root - find the root node in the flat blob
804 unsigned long __init
of_get_flat_dt_root(void)
810 * of_get_flat_dt_size - Return the total size of the FDT
812 int __init
of_get_flat_dt_size(void)
814 return fdt_totalsize(initial_boot_params
);
818 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
820 * This function can be used within scan_flattened_dt callback to get
821 * access to properties
823 const void *__init
of_get_flat_dt_prop(unsigned long node
, const char *name
,
826 return fdt_getprop(initial_boot_params
, node
, name
, size
);
830 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
831 * @node: node to test
832 * @compat: compatible string to compare with compatible list.
834 int __init
of_flat_dt_is_compatible(unsigned long node
, const char *compat
)
836 return of_fdt_is_compatible(initial_boot_params
, node
, compat
);
840 * of_flat_dt_match - Return true if node matches a list of compatible values
842 int __init
of_flat_dt_match(unsigned long node
, const char *const *compat
)
844 return of_fdt_match(initial_boot_params
, node
, compat
);
848 * of_get_flat_dt_prop - Given a node in the flat blob, return the phandle
850 uint32_t __init
of_get_flat_dt_phandle(unsigned long node
)
852 return fdt_get_phandle(initial_boot_params
, node
);
855 struct fdt_scan_status
{
860 int (*iterator
)(unsigned long node
, const char *uname
, int depth
, void *data
);
864 const char * __init
of_flat_dt_get_machine_name(void)
867 unsigned long dt_root
= of_get_flat_dt_root();
869 name
= of_get_flat_dt_prop(dt_root
, "model", NULL
);
871 name
= of_get_flat_dt_prop(dt_root
, "compatible", NULL
);
876 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
878 * @default_match: A machine specific ptr to return in case of no match.
879 * @get_next_compat: callback function to return next compatible match table.
881 * Iterate through machine match tables to find the best match for the machine
882 * compatible string in the FDT.
884 const void * __init
of_flat_dt_match_machine(const void *default_match
,
885 const void * (*get_next_compat
)(const char * const**))
887 const void *data
= NULL
;
888 const void *best_data
= default_match
;
889 const char *const *compat
;
890 unsigned long dt_root
;
891 unsigned int best_score
= ~1, score
= 0;
893 dt_root
= of_get_flat_dt_root();
894 while ((data
= get_next_compat(&compat
))) {
895 score
= of_flat_dt_match(dt_root
, compat
);
896 if (score
> 0 && score
< best_score
) {
905 pr_err("\n unrecognized device tree list:\n[ ");
907 prop
= of_get_flat_dt_prop(dt_root
, "compatible", &size
);
910 printk("'%s' ", prop
);
911 size
-= strlen(prop
) + 1;
912 prop
+= strlen(prop
) + 1;
919 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
924 #ifdef CONFIG_BLK_DEV_INITRD
925 #ifndef __early_init_dt_declare_initrd
926 static void __early_init_dt_declare_initrd(unsigned long start
,
929 initrd_start
= (unsigned long)__va(start
);
930 initrd_end
= (unsigned long)__va(end
);
931 initrd_below_start_ok
= 1;
936 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
937 * @node: reference to node containing initrd location ('chosen')
939 static void __init
early_init_dt_check_for_initrd(unsigned long node
)
945 pr_debug("Looking for initrd properties... ");
947 prop
= of_get_flat_dt_prop(node
, "linux,initrd-start", &len
);
950 start
= of_read_number(prop
, len
/4);
952 prop
= of_get_flat_dt_prop(node
, "linux,initrd-end", &len
);
955 end
= of_read_number(prop
, len
/4);
957 __early_init_dt_declare_initrd(start
, end
);
959 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
960 (unsigned long long)start
, (unsigned long long)end
);
963 static inline void early_init_dt_check_for_initrd(unsigned long node
)
966 #endif /* CONFIG_BLK_DEV_INITRD */
968 #ifdef CONFIG_SERIAL_EARLYCON
970 int __init
early_init_dt_scan_chosen_stdout(void)
973 const char *p
, *q
, *options
= NULL
;
975 const struct earlycon_id
*match
;
976 const void *fdt
= initial_boot_params
;
978 offset
= fdt_path_offset(fdt
, "/chosen");
980 offset
= fdt_path_offset(fdt
, "/chosen@0");
984 p
= fdt_getprop(fdt
, offset
, "stdout-path", &l
);
986 p
= fdt_getprop(fdt
, offset
, "linux,stdout-path", &l
);
990 q
= strchrnul(p
, ':');
995 /* Get the node specified by stdout-path */
996 offset
= fdt_path_offset_namelen(fdt
, p
, l
);
998 pr_warn("earlycon: stdout-path %.*s not found\n", l
, p
);
1002 for (match
= __earlycon_table
; match
< __earlycon_table_end
; match
++) {
1003 if (!match
->compatible
[0])
1006 if (fdt_node_check_compatible(fdt
, offset
, match
->compatible
))
1009 of_setup_earlycon(match
, offset
, options
);
1017 * early_init_dt_scan_root - fetch the top level address and size cells
1019 int __init
early_init_dt_scan_root(unsigned long node
, const char *uname
,
1020 int depth
, void *data
)
1027 dt_root_size_cells
= OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
1028 dt_root_addr_cells
= OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
1030 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
1032 dt_root_size_cells
= be32_to_cpup(prop
);
1033 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells
);
1035 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
1037 dt_root_addr_cells
= be32_to_cpup(prop
);
1038 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells
);
1044 u64 __init
dt_mem_next_cell(int s
, const __be32
**cellp
)
1046 const __be32
*p
= *cellp
;
1049 return of_read_number(p
, s
);
1053 * early_init_dt_scan_memory - Look for an parse memory nodes
1055 int __init
early_init_dt_scan_memory(unsigned long node
, const char *uname
,
1056 int depth
, void *data
)
1058 const char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
1059 const __be32
*reg
, *endp
;
1063 /* We are scanning "memory" nodes only */
1066 * The longtrail doesn't have a device_type on the
1067 * /memory node, so look for the node called /memory@0.
1069 if (!IS_ENABLED(CONFIG_PPC32
) || depth
!= 1 || strcmp(uname
, "memory@0") != 0)
1071 } else if (strcmp(type
, "memory") != 0)
1074 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
1076 reg
= of_get_flat_dt_prop(node
, "reg", &l
);
1080 endp
= reg
+ (l
/ sizeof(__be32
));
1081 hotpluggable
= of_get_flat_dt_prop(node
, "hotpluggable", NULL
);
1083 pr_debug("memory scan node %s, reg size %d,\n", uname
, l
);
1085 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
1088 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
1089 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
1093 pr_debug(" - %llx , %llx\n", (unsigned long long)base
,
1094 (unsigned long long)size
);
1096 early_init_dt_add_memory_arch(base
, size
);
1101 if (early_init_dt_mark_hotplug_memory_arch(base
, size
))
1102 pr_warn("failed to mark hotplug range 0x%llx - 0x%llx\n",
1109 int __init
early_init_dt_scan_chosen(unsigned long node
, const char *uname
,
1110 int depth
, void *data
)
1115 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
1117 if (depth
!= 1 || !data
||
1118 (strcmp(uname
, "chosen") != 0 && strcmp(uname
, "chosen@0") != 0))
1121 early_init_dt_check_for_initrd(node
);
1123 /* Retrieve command line */
1124 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
1125 if (p
!= NULL
&& l
> 0)
1126 strlcpy(data
, p
, min((int)l
, COMMAND_LINE_SIZE
));
1129 * CONFIG_CMDLINE is meant to be a default in case nothing else
1130 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
1131 * is set in which case we override whatever was found earlier.
1133 #ifdef CONFIG_CMDLINE
1134 #if defined(CONFIG_CMDLINE_EXTEND)
1135 strlcat(data
, " ", COMMAND_LINE_SIZE
);
1136 strlcat(data
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
1137 #elif defined(CONFIG_CMDLINE_FORCE)
1138 strlcpy(data
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
1140 /* No arguments from boot loader, use kernel's cmdl*/
1141 if (!((char *)data
)[0])
1142 strlcpy(data
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
1144 #endif /* CONFIG_CMDLINE */
1146 pr_debug("Command line is: %s\n", (char*)data
);
1152 #ifdef CONFIG_HAVE_MEMBLOCK
1153 #ifndef MIN_MEMBLOCK_ADDR
1154 #define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET)
1156 #ifndef MAX_MEMBLOCK_ADDR
1157 #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
1160 void __init __weak
early_init_dt_add_memory_arch(u64 base
, u64 size
)
1162 const u64 phys_offset
= MIN_MEMBLOCK_ADDR
;
1164 if (!PAGE_ALIGNED(base
)) {
1165 if (size
< PAGE_SIZE
- (base
& ~PAGE_MASK
)) {
1166 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
1170 size
-= PAGE_SIZE
- (base
& ~PAGE_MASK
);
1171 base
= PAGE_ALIGN(base
);
1175 if (base
> MAX_MEMBLOCK_ADDR
) {
1176 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1181 if (base
+ size
- 1 > MAX_MEMBLOCK_ADDR
) {
1182 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1183 ((u64
)MAX_MEMBLOCK_ADDR
) + 1, base
+ size
);
1184 size
= MAX_MEMBLOCK_ADDR
- base
+ 1;
1187 if (base
+ size
< phys_offset
) {
1188 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1192 if (base
< phys_offset
) {
1193 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1195 size
-= phys_offset
- base
;
1198 memblock_add(base
, size
);
1201 int __init __weak
early_init_dt_mark_hotplug_memory_arch(u64 base
, u64 size
)
1203 return memblock_mark_hotplug(base
, size
);
1206 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
1207 phys_addr_t size
, bool nomap
)
1210 return memblock_remove(base
, size
);
1211 return memblock_reserve(base
, size
);
1215 * called from unflatten_device_tree() to bootstrap devicetree itself
1216 * Architectures can override this definition if memblock isn't used
1218 void * __init __weak
early_init_dt_alloc_memory_arch(u64 size
, u64 align
)
1220 return __va(memblock_alloc(size
, align
));
1223 void __init __weak
early_init_dt_add_memory_arch(u64 base
, u64 size
)
1228 int __init __weak
early_init_dt_mark_hotplug_memory_arch(u64 base
, u64 size
)
1233 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
1234 phys_addr_t size
, bool nomap
)
1236 pr_err("Reserved memory not supported, ignoring range %pa - %pa%s\n",
1237 &base
, &size
, nomap
? " (nomap)" : "");
1241 void * __init __weak
early_init_dt_alloc_memory_arch(u64 size
, u64 align
)
1248 bool __init
early_init_dt_verify(void *params
)
1253 /* check device tree validity */
1254 if (fdt_check_header(params
))
1257 /* Setup flat device-tree pointer */
1258 initial_boot_params
= params
;
1259 of_fdt_crc32
= crc32_be(~0, initial_boot_params
,
1260 fdt_totalsize(initial_boot_params
));
1265 void __init
early_init_dt_scan_nodes(void)
1267 /* Retrieve various information from the /chosen node */
1268 of_scan_flat_dt(early_init_dt_scan_chosen
, boot_command_line
);
1270 /* Initialize {size,address}-cells info */
1271 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
1273 /* Setup memory, calling early_init_dt_add_memory_arch */
1274 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
1277 bool __init
early_init_dt_scan(void *params
)
1281 status
= early_init_dt_verify(params
);
1285 early_init_dt_scan_nodes();
1290 * unflatten_device_tree - create tree of device_nodes from flat blob
1292 * unflattens the device-tree passed by the firmware, creating the
1293 * tree of struct device_node. It also fills the "name" and "type"
1294 * pointers of the nodes so the normal device-tree walking functions
1297 void __init
unflatten_device_tree(void)
1299 __unflatten_device_tree(initial_boot_params
, NULL
, &of_root
,
1300 early_init_dt_alloc_memory_arch
, false);
1302 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
1303 of_alias_scan(early_init_dt_alloc_memory_arch
);
1305 unittest_unflatten_overlay_base();
1309 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1311 * Copies and unflattens the device-tree passed by the firmware, creating the
1312 * tree of struct device_node. It also fills the "name" and "type"
1313 * pointers of the nodes so the normal device-tree walking functions
1314 * can be used. This should only be used when the FDT memory has not been
1315 * reserved such is the case when the FDT is built-in to the kernel init
1316 * section. If the FDT memory is reserved already then unflatten_device_tree
1317 * should be used instead.
1319 void __init
unflatten_and_copy_device_tree(void)
1324 if (!initial_boot_params
) {
1325 pr_warn("No valid device tree found, continuing without\n");
1329 size
= fdt_totalsize(initial_boot_params
);
1330 dt
= early_init_dt_alloc_memory_arch(size
,
1331 roundup_pow_of_two(FDT_V17_SIZE
));
1334 memcpy(dt
, initial_boot_params
, size
);
1335 initial_boot_params
= dt
;
1337 unflatten_device_tree();
1341 static ssize_t
of_fdt_raw_read(struct file
*filp
, struct kobject
*kobj
,
1342 struct bin_attribute
*bin_attr
,
1343 char *buf
, loff_t off
, size_t count
)
1345 memcpy(buf
, initial_boot_params
+ off
, count
);
1349 static int __init
of_fdt_raw_init(void)
1351 static struct bin_attribute of_fdt_raw_attr
=
1352 __BIN_ATTR(fdt
, S_IRUSR
, of_fdt_raw_read
, NULL
, 0);
1354 if (!initial_boot_params
)
1357 if (of_fdt_crc32
!= crc32_be(~0, initial_boot_params
,
1358 fdt_totalsize(initial_boot_params
))) {
1359 pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n");
1362 of_fdt_raw_attr
.size
= fdt_totalsize(initial_boot_params
);
1363 return sysfs_create_bin_file(firmware_kobj
, &of_fdt_raw_attr
);
1365 late_initcall(of_fdt_raw_init
);
1368 #endif /* CONFIG_OF_EARLY_FLATTREE */