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/crc32.h>
13 #include <linux/kernel.h>
14 #include <linux/initrd.h>
15 #include <linux/memblock.h>
17 #include <linux/of_fdt.h>
18 #include <linux/of_reserved_mem.h>
19 #include <linux/sizes.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/libfdt.h>
24 #include <linux/debugfs.h>
25 #include <linux/serial_core.h>
26 #include <linux/sysfs.h>
28 #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
32 * of_fdt_limit_memory - limit the number of regions in the /memory node
33 * @limit: maximum entries
35 * Adjust the flattened device tree to have at most 'limit' number of
36 * memory entries in the /memory node. This function may be called
37 * any time after initial_boot_param is set.
39 void of_fdt_limit_memory(int limit
)
44 int nr_address_cells
= OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
45 int nr_size_cells
= OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
46 const uint32_t *addr_prop
;
47 const uint32_t *size_prop
;
51 root_offset
= fdt_path_offset(initial_boot_params
, "/");
55 addr_prop
= fdt_getprop(initial_boot_params
, root_offset
,
56 "#address-cells", NULL
);
58 nr_address_cells
= fdt32_to_cpu(*addr_prop
);
60 size_prop
= fdt_getprop(initial_boot_params
, root_offset
,
63 nr_size_cells
= fdt32_to_cpu(*size_prop
);
65 cell_size
= sizeof(uint32_t)*(nr_address_cells
+ nr_size_cells
);
67 memory
= fdt_path_offset(initial_boot_params
, "/memory");
69 val
= fdt_getprop(initial_boot_params
, memory
, "reg", &len
);
70 if (len
> limit
*cell_size
) {
71 len
= limit
*cell_size
;
72 pr_debug("Limiting number of entries to %d\n", limit
);
73 fdt_setprop(initial_boot_params
, memory
, "reg", val
,
80 * of_fdt_is_compatible - Return true if given node from the given blob has
81 * compat in its compatible list
82 * @blob: A device tree blob
84 * @compat: compatible string to compare with compatible list.
86 * On match, returns a non-zero value with smaller values returned for more
87 * specific compatible values.
89 int of_fdt_is_compatible(const void *blob
,
90 unsigned long node
, const char *compat
)
94 unsigned long l
, score
= 0;
96 cp
= fdt_getprop(blob
, node
, "compatible", &cplen
);
101 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
112 * of_fdt_is_big_endian - Return true if given node needs BE MMIO accesses
113 * @blob: A device tree blob
114 * @node: node to test
116 * Returns true if the node has a "big-endian" property, or if the kernel
117 * was compiled for BE *and* the node has a "native-endian" property.
118 * Returns false otherwise.
120 bool of_fdt_is_big_endian(const void *blob
, unsigned long node
)
122 if (fdt_getprop(blob
, node
, "big-endian", NULL
))
124 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
) &&
125 fdt_getprop(blob
, node
, "native-endian", NULL
))
131 * of_fdt_match - Return true if node matches a list of compatible values
133 int of_fdt_match(const void *blob
, unsigned long node
,
134 const char *const *compat
)
136 unsigned int tmp
, score
= 0;
142 tmp
= of_fdt_is_compatible(blob
, node
, *compat
);
143 if (tmp
&& (score
== 0 || (tmp
< score
)))
151 static void *unflatten_dt_alloc(void **mem
, unsigned long size
,
156 *mem
= PTR_ALIGN(*mem
, align
);
164 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
165 * @blob: The parent device tree blob
166 * @mem: Memory chunk to use for allocating device nodes and properties
167 * @p: pointer to node in flat tree
168 * @dad: Parent struct device_node
169 * @fpsize: Size of the node path up at the current depth.
171 static void * unflatten_dt_node(void *blob
,
174 struct device_node
*dad
,
175 struct device_node
**nodepp
,
176 unsigned long fpsize
,
180 struct device_node
*np
;
181 struct property
*pp
, **prev_pp
= NULL
;
183 unsigned int l
, allocl
;
184 static int depth
= 0;
190 pathp
= fdt_get_name(blob
, *poffset
, &l
);
196 /* version 0x10 has a more compact unit name here instead of the full
197 * path. we accumulate the full path size using "fpsize", we'll rebuild
198 * it later. We detect this because the first character of the name is
201 if ((*pathp
) != '/') {
204 /* root node: special case. fpsize accounts for path
205 * plus terminating zero. root node only has '/', so
206 * fpsize should be 2, but we want to avoid the first
207 * level nodes to have two '/' so we use fpsize 1 here
214 /* account for '/' and path size minus terminal 0
222 np
= unflatten_dt_alloc(&mem
, sizeof(struct device_node
) + allocl
,
223 __alignof__(struct device_node
));
227 np
->full_name
= fn
= ((char *)np
) + sizeof(*np
);
229 /* rebuild full path for new format */
230 if (dad
&& dad
->parent
) {
231 strcpy(fn
, dad
->full_name
);
233 if ((strlen(fn
) + l
+ 1) != allocl
) {
234 pr_debug("%s: p: %d, l: %d, a: %d\n",
235 pathp
, (int)strlen(fn
),
243 memcpy(fn
, pathp
, l
);
245 prev_pp
= &np
->properties
;
248 np
->sibling
= dad
->child
;
252 /* process properties */
253 for (offset
= fdt_first_property_offset(blob
, *poffset
);
255 (offset
= fdt_next_property_offset(blob
, offset
))) {
259 if (!(p
= fdt_getprop_by_offset(blob
, offset
, &pname
, &sz
))) {
260 offset
= -FDT_ERR_INTERNAL
;
265 pr_info("Can't find property name in list !\n");
268 if (strcmp(pname
, "name") == 0)
270 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
271 __alignof__(struct property
));
273 /* We accept flattened tree phandles either in
274 * ePAPR-style "phandle" properties, or the
275 * legacy "linux,phandle" properties. If both
276 * appear and have different values, things
277 * will get weird. Don't do that. */
278 if ((strcmp(pname
, "phandle") == 0) ||
279 (strcmp(pname
, "linux,phandle") == 0)) {
280 if (np
->phandle
== 0)
281 np
->phandle
= be32_to_cpup(p
);
283 /* And we process the "ibm,phandle" property
284 * used in pSeries dynamic device tree
286 if (strcmp(pname
, "ibm,phandle") == 0)
287 np
->phandle
= be32_to_cpup(p
);
288 pp
->name
= (char *)pname
;
290 pp
->value
= (__be32
*)p
;
295 /* with version 0x10 we may not have the name property, recreate
296 * it here from the unit name if absent
299 const char *p1
= pathp
, *ps
= pathp
, *pa
= NULL
;
312 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
) + sz
,
313 __alignof__(struct property
));
320 memcpy(pp
->value
, ps
, sz
- 1);
321 ((char *)pp
->value
)[sz
- 1] = 0;
322 pr_debug("fixed up name for %s -> %s\n", pathp
,
328 np
->name
= of_get_property(np
, "name", NULL
);
329 np
->type
= of_get_property(np
, "device_type", NULL
);
338 *poffset
= fdt_next_node(blob
, *poffset
, &depth
);
341 while (*poffset
> 0 && depth
> old_depth
)
342 mem
= unflatten_dt_node(blob
, mem
, poffset
, np
, NULL
,
345 if (*poffset
< 0 && *poffset
!= -FDT_ERR_NOTFOUND
)
346 pr_err("unflatten: error %d processing FDT\n", *poffset
);
349 * Reverse the child list. Some drivers assumes node order matches .dts
352 if (!dryrun
&& np
->child
) {
353 struct device_node
*child
= np
->child
;
356 struct device_node
*next
= child
->sibling
;
357 child
->sibling
= np
->child
;
370 * __unflatten_device_tree - create tree of device_nodes from flat blob
372 * unflattens a device-tree, creating the
373 * tree of struct device_node. It also fills the "name" and "type"
374 * pointers of the nodes so the normal device-tree walking functions
376 * @blob: The blob to expand
377 * @mynodes: The device_node tree created by the call
378 * @dt_alloc: An allocator that provides a virtual address to memory
379 * for the resulting tree
381 static void __unflatten_device_tree(void *blob
,
382 struct device_node
**mynodes
,
383 void * (*dt_alloc
)(u64 size
, u64 align
))
389 pr_debug(" -> unflatten_device_tree()\n");
392 pr_debug("No device tree pointer\n");
396 pr_debug("Unflattening device tree:\n");
397 pr_debug("magic: %08x\n", fdt_magic(blob
));
398 pr_debug("size: %08x\n", fdt_totalsize(blob
));
399 pr_debug("version: %08x\n", fdt_version(blob
));
401 if (fdt_check_header(blob
)) {
402 pr_err("Invalid device tree blob header\n");
406 /* First pass, scan for size */
408 size
= (unsigned long)unflatten_dt_node(blob
, NULL
, &start
, NULL
, NULL
, 0, true);
409 size
= ALIGN(size
, 4);
411 pr_debug(" size is %lx, allocating...\n", size
);
413 /* Allocate memory for the expanded device tree */
414 mem
= dt_alloc(size
+ 4, __alignof__(struct device_node
));
415 memset(mem
, 0, size
);
417 *(__be32
*)(mem
+ size
) = cpu_to_be32(0xdeadbeef);
419 pr_debug(" unflattening %p...\n", mem
);
421 /* Second pass, do actual unflattening */
423 unflatten_dt_node(blob
, mem
, &start
, NULL
, mynodes
, 0, false);
424 if (be32_to_cpup(mem
+ size
) != 0xdeadbeef)
425 pr_warning("End of tree marker overwritten: %08x\n",
426 be32_to_cpup(mem
+ size
));
428 pr_debug(" <- unflatten_device_tree()\n");
431 static void *kernel_tree_alloc(u64 size
, u64 align
)
433 return kzalloc(size
, GFP_KERNEL
);
437 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
439 * unflattens the device-tree passed by the firmware, creating the
440 * tree of struct device_node. It also fills the "name" and "type"
441 * pointers of the nodes so the normal device-tree walking functions
444 void of_fdt_unflatten_tree(unsigned long *blob
,
445 struct device_node
**mynodes
)
447 __unflatten_device_tree(blob
, mynodes
, &kernel_tree_alloc
);
449 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree
);
451 /* Everything below here references initial_boot_params directly. */
452 int __initdata dt_root_addr_cells
;
453 int __initdata dt_root_size_cells
;
455 void *initial_boot_params
;
457 #ifdef CONFIG_OF_EARLY_FLATTREE
459 static u32 of_fdt_crc32
;
462 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
464 static int __init
__reserved_mem_reserve_reg(unsigned long node
,
467 int t_len
= (dt_root_addr_cells
+ dt_root_size_cells
) * sizeof(__be32
);
468 phys_addr_t base
, size
;
471 int nomap
, first
= 1;
473 prop
= of_get_flat_dt_prop(node
, "reg", &len
);
477 if (len
&& len
% t_len
!= 0) {
478 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
483 nomap
= of_get_flat_dt_prop(node
, "no-map", NULL
) != NULL
;
485 while (len
>= t_len
) {
486 base
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
487 size
= dt_mem_next_cell(dt_root_size_cells
, &prop
);
490 early_init_dt_reserve_memory_arch(base
, size
, nomap
) == 0)
491 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
492 uname
, &base
, (unsigned long)size
/ SZ_1M
);
494 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
495 uname
, &base
, (unsigned long)size
/ SZ_1M
);
499 fdt_reserved_mem_save_node(node
, uname
, base
, size
);
507 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
508 * in /reserved-memory matches the values supported by the current implementation,
509 * also check if ranges property has been provided
511 static int __init
__reserved_mem_check_root(unsigned long node
)
515 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
516 if (!prop
|| be32_to_cpup(prop
) != dt_root_size_cells
)
519 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
520 if (!prop
|| be32_to_cpup(prop
) != dt_root_addr_cells
)
523 prop
= of_get_flat_dt_prop(node
, "ranges", NULL
);
530 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
532 static int __init
__fdt_scan_reserved_mem(unsigned long node
, const char *uname
,
533 int depth
, void *data
)
539 if (!found
&& depth
== 1 && strcmp(uname
, "reserved-memory") == 0) {
540 if (__reserved_mem_check_root(node
) != 0) {
541 pr_err("Reserved memory: unsupported node format, ignoring\n");
551 } else if (found
&& depth
< 2) {
552 /* scanning of /reserved-memory has been finished */
556 status
= of_get_flat_dt_prop(node
, "status", NULL
);
557 if (status
&& strcmp(status
, "okay") != 0 && strcmp(status
, "ok") != 0)
560 err
= __reserved_mem_reserve_reg(node
, uname
);
561 if (err
== -ENOENT
&& of_get_flat_dt_prop(node
, "size", NULL
))
562 fdt_reserved_mem_save_node(node
, uname
, 0, 0);
569 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
571 * This function grabs memory from early allocator for device exclusive use
572 * defined in device tree structures. It should be called by arch specific code
573 * once the early allocator (i.e. memblock) has been fully activated.
575 void __init
early_init_fdt_scan_reserved_mem(void)
580 if (!initial_boot_params
)
583 /* Reserve the dtb region */
584 early_init_dt_reserve_memory_arch(__pa(initial_boot_params
),
585 fdt_totalsize(initial_boot_params
),
588 /* Process header /memreserve/ fields */
590 fdt_get_mem_rsv(initial_boot_params
, n
, &base
, &size
);
593 early_init_dt_reserve_memory_arch(base
, size
, 0);
596 of_scan_flat_dt(__fdt_scan_reserved_mem
, NULL
);
597 fdt_init_reserved_mem();
601 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
602 * @it: callback function
603 * @data: context data pointer
605 * This function is used to scan the flattened device-tree, it is
606 * used to extract the memory information at boot before we can
609 int __init
of_scan_flat_dt(int (*it
)(unsigned long node
,
610 const char *uname
, int depth
,
614 const void *blob
= initial_boot_params
;
616 int offset
, rc
= 0, depth
= -1;
618 for (offset
= fdt_next_node(blob
, -1, &depth
);
619 offset
>= 0 && depth
>= 0 && !rc
;
620 offset
= fdt_next_node(blob
, offset
, &depth
)) {
622 pathp
= fdt_get_name(blob
, offset
, NULL
);
624 pathp
= kbasename(pathp
);
625 rc
= it(offset
, pathp
, depth
, data
);
631 * of_get_flat_dt_root - find the root node in the flat blob
633 unsigned long __init
of_get_flat_dt_root(void)
639 * of_get_flat_dt_size - Return the total size of the FDT
641 int __init
of_get_flat_dt_size(void)
643 return fdt_totalsize(initial_boot_params
);
647 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
649 * This function can be used within scan_flattened_dt callback to get
650 * access to properties
652 const void *__init
of_get_flat_dt_prop(unsigned long node
, const char *name
,
655 return fdt_getprop(initial_boot_params
, node
, name
, size
);
659 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
660 * @node: node to test
661 * @compat: compatible string to compare with compatible list.
663 int __init
of_flat_dt_is_compatible(unsigned long node
, const char *compat
)
665 return of_fdt_is_compatible(initial_boot_params
, node
, compat
);
669 * of_flat_dt_match - Return true if node matches a list of compatible values
671 int __init
of_flat_dt_match(unsigned long node
, const char *const *compat
)
673 return of_fdt_match(initial_boot_params
, node
, compat
);
676 struct fdt_scan_status
{
681 int (*iterator
)(unsigned long node
, const char *uname
, int depth
, void *data
);
685 const char * __init
of_flat_dt_get_machine_name(void)
688 unsigned long dt_root
= of_get_flat_dt_root();
690 name
= of_get_flat_dt_prop(dt_root
, "model", NULL
);
692 name
= of_get_flat_dt_prop(dt_root
, "compatible", NULL
);
697 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
699 * @default_match: A machine specific ptr to return in case of no match.
700 * @get_next_compat: callback function to return next compatible match table.
702 * Iterate through machine match tables to find the best match for the machine
703 * compatible string in the FDT.
705 const void * __init
of_flat_dt_match_machine(const void *default_match
,
706 const void * (*get_next_compat
)(const char * const**))
708 const void *data
= NULL
;
709 const void *best_data
= default_match
;
710 const char *const *compat
;
711 unsigned long dt_root
;
712 unsigned int best_score
= ~1, score
= 0;
714 dt_root
= of_get_flat_dt_root();
715 while ((data
= get_next_compat(&compat
))) {
716 score
= of_flat_dt_match(dt_root
, compat
);
717 if (score
> 0 && score
< best_score
) {
726 pr_err("\n unrecognized device tree list:\n[ ");
728 prop
= of_get_flat_dt_prop(dt_root
, "compatible", &size
);
731 printk("'%s' ", prop
);
732 size
-= strlen(prop
) + 1;
733 prop
+= strlen(prop
) + 1;
740 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
745 #ifdef CONFIG_BLK_DEV_INITRD
747 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
748 * @node: reference to node containing initrd location ('chosen')
750 static void __init
early_init_dt_check_for_initrd(unsigned long node
)
756 pr_debug("Looking for initrd properties... ");
758 prop
= of_get_flat_dt_prop(node
, "linux,initrd-start", &len
);
761 start
= of_read_number(prop
, len
/4);
763 prop
= of_get_flat_dt_prop(node
, "linux,initrd-end", &len
);
766 end
= of_read_number(prop
, len
/4);
768 initrd_start
= (unsigned long)__va(start
);
769 initrd_end
= (unsigned long)__va(end
);
770 initrd_below_start_ok
= 1;
772 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
773 (unsigned long long)start
, (unsigned long long)end
);
776 static inline void early_init_dt_check_for_initrd(unsigned long node
)
779 #endif /* CONFIG_BLK_DEV_INITRD */
781 #ifdef CONFIG_SERIAL_EARLYCON
782 extern struct of_device_id __earlycon_of_table
[];
784 static int __init
early_init_dt_scan_chosen_serial(void)
789 const struct of_device_id
*match
= __earlycon_of_table
;
790 const void *fdt
= initial_boot_params
;
792 offset
= fdt_path_offset(fdt
, "/chosen");
794 offset
= fdt_path_offset(fdt
, "/chosen@0");
798 p
= fdt_getprop(fdt
, offset
, "stdout-path", &l
);
800 p
= fdt_getprop(fdt
, offset
, "linux,stdout-path", &l
);
804 /* Get the node specified by stdout-path */
805 offset
= fdt_path_offset(fdt
, p
);
809 while (match
->compatible
[0]) {
811 if (fdt_node_check_compatible(fdt
, offset
, match
->compatible
)) {
816 addr
= fdt_translate_address(fdt
, offset
);
820 of_setup_earlycon(addr
, match
->data
);
826 static int __init
setup_of_earlycon(char *buf
)
831 return early_init_dt_scan_chosen_serial();
833 early_param("earlycon", setup_of_earlycon
);
837 * early_init_dt_scan_root - fetch the top level address and size cells
839 int __init
early_init_dt_scan_root(unsigned long node
, const char *uname
,
840 int depth
, void *data
)
847 dt_root_size_cells
= OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
848 dt_root_addr_cells
= OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
850 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
852 dt_root_size_cells
= be32_to_cpup(prop
);
853 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells
);
855 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
857 dt_root_addr_cells
= be32_to_cpup(prop
);
858 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells
);
864 u64 __init
dt_mem_next_cell(int s
, const __be32
**cellp
)
866 const __be32
*p
= *cellp
;
869 return of_read_number(p
, s
);
873 * early_init_dt_scan_memory - Look for an parse memory nodes
875 int __init
early_init_dt_scan_memory(unsigned long node
, const char *uname
,
876 int depth
, void *data
)
878 const char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
879 const __be32
*reg
, *endp
;
882 /* We are scanning "memory" nodes only */
885 * The longtrail doesn't have a device_type on the
886 * /memory node, so look for the node called /memory@0.
888 if (!IS_ENABLED(CONFIG_PPC32
) || depth
!= 1 || strcmp(uname
, "memory@0") != 0)
890 } else if (strcmp(type
, "memory") != 0)
893 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
895 reg
= of_get_flat_dt_prop(node
, "reg", &l
);
899 endp
= reg
+ (l
/ sizeof(__be32
));
901 pr_debug("memory scan node %s, reg size %d,\n", uname
, l
);
903 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
906 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
907 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
911 pr_debug(" - %llx , %llx\n", (unsigned long long)base
,
912 (unsigned long long)size
);
914 early_init_dt_add_memory_arch(base
, size
);
920 int __init
early_init_dt_scan_chosen(unsigned long node
, const char *uname
,
921 int depth
, void *data
)
926 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
928 if (depth
!= 1 || !data
||
929 (strcmp(uname
, "chosen") != 0 && strcmp(uname
, "chosen@0") != 0))
932 early_init_dt_check_for_initrd(node
);
934 /* Retrieve command line */
935 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
936 if (p
!= NULL
&& l
> 0)
937 strlcpy(data
, p
, min((int)l
, COMMAND_LINE_SIZE
));
940 * CONFIG_CMDLINE is meant to be a default in case nothing else
941 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
942 * is set in which case we override whatever was found earlier.
944 #ifdef CONFIG_CMDLINE
945 #ifndef CONFIG_CMDLINE_FORCE
946 if (!((char *)data
)[0])
948 strlcpy(data
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
949 #endif /* CONFIG_CMDLINE */
951 pr_debug("Command line is: %s\n", (char*)data
);
957 #ifdef CONFIG_HAVE_MEMBLOCK
958 #ifndef MAX_MEMBLOCK_ADDR
959 #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
962 void __init __weak
early_init_dt_add_memory_arch(u64 base
, u64 size
)
964 const u64 phys_offset
= __pa(PAGE_OFFSET
);
966 if (!PAGE_ALIGNED(base
)) {
967 if (size
< PAGE_SIZE
- (base
& ~PAGE_MASK
)) {
968 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
972 size
-= PAGE_SIZE
- (base
& ~PAGE_MASK
);
973 base
= PAGE_ALIGN(base
);
977 if (base
> MAX_MEMBLOCK_ADDR
) {
978 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
983 if (base
+ size
- 1 > MAX_MEMBLOCK_ADDR
) {
984 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
985 ((u64
)MAX_MEMBLOCK_ADDR
) + 1, base
+ size
);
986 size
= MAX_MEMBLOCK_ADDR
- base
+ 1;
989 if (base
+ size
< phys_offset
) {
990 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
994 if (base
< phys_offset
) {
995 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
997 size
-= phys_offset
- base
;
1000 memblock_add(base
, size
);
1003 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
1004 phys_addr_t size
, bool nomap
)
1007 return memblock_remove(base
, size
);
1008 return memblock_reserve(base
, size
);
1012 * called from unflatten_device_tree() to bootstrap devicetree itself
1013 * Architectures can override this definition if memblock isn't used
1015 void * __init __weak
early_init_dt_alloc_memory_arch(u64 size
, u64 align
)
1017 return __va(memblock_alloc(size
, align
));
1020 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
1021 phys_addr_t size
, bool nomap
)
1023 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
1024 &base
, &size
, nomap
? " (nomap)" : "");
1029 bool __init
early_init_dt_verify(void *params
)
1034 /* check device tree validity */
1035 if (fdt_check_header(params
))
1038 /* Setup flat device-tree pointer */
1039 initial_boot_params
= params
;
1040 of_fdt_crc32
= crc32_be(~0, initial_boot_params
,
1041 fdt_totalsize(initial_boot_params
));
1046 void __init
early_init_dt_scan_nodes(void)
1048 /* Retrieve various information from the /chosen node */
1049 of_scan_flat_dt(early_init_dt_scan_chosen
, boot_command_line
);
1051 /* Initialize {size,address}-cells info */
1052 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
1054 /* Setup memory, calling early_init_dt_add_memory_arch */
1055 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
1058 bool __init
early_init_dt_scan(void *params
)
1062 status
= early_init_dt_verify(params
);
1066 early_init_dt_scan_nodes();
1071 * unflatten_device_tree - create tree of device_nodes from flat blob
1073 * unflattens the device-tree passed by the firmware, creating the
1074 * tree of struct device_node. It also fills the "name" and "type"
1075 * pointers of the nodes so the normal device-tree walking functions
1078 void __init
unflatten_device_tree(void)
1080 __unflatten_device_tree(initial_boot_params
, &of_root
,
1081 early_init_dt_alloc_memory_arch
);
1083 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
1084 of_alias_scan(early_init_dt_alloc_memory_arch
);
1088 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1090 * Copies and unflattens the device-tree passed by the firmware, creating the
1091 * tree of struct device_node. It also fills the "name" and "type"
1092 * pointers of the nodes so the normal device-tree walking functions
1093 * can be used. This should only be used when the FDT memory has not been
1094 * reserved such is the case when the FDT is built-in to the kernel init
1095 * section. If the FDT memory is reserved already then unflatten_device_tree
1096 * should be used instead.
1098 void __init
unflatten_and_copy_device_tree(void)
1103 if (!initial_boot_params
) {
1104 pr_warn("No valid device tree found, continuing without\n");
1108 size
= fdt_totalsize(initial_boot_params
);
1109 dt
= early_init_dt_alloc_memory_arch(size
,
1110 roundup_pow_of_two(FDT_V17_SIZE
));
1113 memcpy(dt
, initial_boot_params
, size
);
1114 initial_boot_params
= dt
;
1116 unflatten_device_tree();
1120 static ssize_t
of_fdt_raw_read(struct file
*filp
, struct kobject
*kobj
,
1121 struct bin_attribute
*bin_attr
,
1122 char *buf
, loff_t off
, size_t count
)
1124 memcpy(buf
, initial_boot_params
+ off
, count
);
1128 static int __init
of_fdt_raw_init(void)
1130 static struct bin_attribute of_fdt_raw_attr
=
1131 __BIN_ATTR(fdt
, S_IRUSR
, of_fdt_raw_read
, NULL
, 0);
1133 if (!initial_boot_params
)
1136 if (of_fdt_crc32
!= crc32_be(~0, initial_boot_params
,
1137 fdt_totalsize(initial_boot_params
))) {
1138 pr_warn("fdt: not creating '/sys/firmware/fdt': CRC check failed\n");
1141 of_fdt_raw_attr
.size
= fdt_totalsize(initial_boot_params
);
1142 return sysfs_create_bin_file(firmware_kobj
, &of_fdt_raw_attr
);
1144 late_initcall(of_fdt_raw_init
);
1147 #endif /* CONFIG_OF_EARLY_FLATTREE */