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 * @poffset: pointer to node in flat tree
168 * @dad: Parent struct device_node
169 * @nodepp: The device_node tree created by the call
170 * @fpsize: Size of the node path up at the current depth.
171 * @dryrun: If true, do not allocate device nodes but still calculate needed
174 static void * unflatten_dt_node(const void *blob
,
177 struct device_node
*dad
,
178 struct device_node
**nodepp
,
179 unsigned long fpsize
,
183 struct device_node
*np
;
184 struct property
*pp
, **prev_pp
= NULL
;
186 unsigned int l
, allocl
;
193 pathp
= fdt_get_name(blob
, *poffset
, &l
);
199 /* version 0x10 has a more compact unit name here instead of the full
200 * path. we accumulate the full path size using "fpsize", we'll rebuild
201 * it later. We detect this because the first character of the name is
204 if ((*pathp
) != '/') {
207 /* root node: special case. fpsize accounts for path
208 * plus terminating zero. root node only has '/', so
209 * fpsize should be 2, but we want to avoid the first
210 * level nodes to have two '/' so we use fpsize 1 here
217 /* account for '/' and path size minus terminal 0
225 np
= unflatten_dt_alloc(&mem
, sizeof(struct device_node
) + allocl
,
226 __alignof__(struct device_node
));
230 np
->full_name
= fn
= ((char *)np
) + sizeof(*np
);
232 /* rebuild full path for new format */
233 if (dad
&& dad
->parent
) {
234 strcpy(fn
, dad
->full_name
);
236 if ((strlen(fn
) + l
+ 1) != allocl
) {
237 pr_debug("%s: p: %d, l: %d, a: %d\n",
238 pathp
, (int)strlen(fn
),
246 memcpy(fn
, pathp
, l
);
248 prev_pp
= &np
->properties
;
251 np
->sibling
= dad
->child
;
255 /* process properties */
256 for (offset
= fdt_first_property_offset(blob
, *poffset
);
258 (offset
= fdt_next_property_offset(blob
, offset
))) {
262 if (!(p
= fdt_getprop_by_offset(blob
, offset
, &pname
, &sz
))) {
263 offset
= -FDT_ERR_INTERNAL
;
268 pr_info("Can't find property name in list !\n");
271 if (strcmp(pname
, "name") == 0)
273 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
274 __alignof__(struct property
));
276 /* We accept flattened tree phandles either in
277 * ePAPR-style "phandle" properties, or the
278 * legacy "linux,phandle" properties. If both
279 * appear and have different values, things
280 * will get weird. Don't do that. */
281 if ((strcmp(pname
, "phandle") == 0) ||
282 (strcmp(pname
, "linux,phandle") == 0)) {
283 if (np
->phandle
== 0)
284 np
->phandle
= be32_to_cpup(p
);
286 /* And we process the "ibm,phandle" property
287 * used in pSeries dynamic device tree
289 if (strcmp(pname
, "ibm,phandle") == 0)
290 np
->phandle
= be32_to_cpup(p
);
291 pp
->name
= (char *)pname
;
293 pp
->value
= (__be32
*)p
;
298 /* with version 0x10 we may not have the name property, recreate
299 * it here from the unit name if absent
302 const char *p1
= pathp
, *ps
= pathp
, *pa
= NULL
;
315 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
) + sz
,
316 __alignof__(struct property
));
323 memcpy(pp
->value
, ps
, sz
- 1);
324 ((char *)pp
->value
)[sz
- 1] = 0;
325 pr_debug("fixed up name for %s -> %s\n", pathp
,
331 np
->name
= of_get_property(np
, "name", NULL
);
332 np
->type
= of_get_property(np
, "device_type", NULL
);
341 *poffset
= fdt_next_node(blob
, *poffset
, &depth
);
344 while (*poffset
> 0 && depth
> old_depth
)
345 mem
= unflatten_dt_node(blob
, mem
, poffset
, np
, NULL
,
348 if (*poffset
< 0 && *poffset
!= -FDT_ERR_NOTFOUND
)
349 pr_err("unflatten: error %d processing FDT\n", *poffset
);
352 * Reverse the child list. Some drivers assumes node order matches .dts
355 if (!dryrun
&& np
->child
) {
356 struct device_node
*child
= np
->child
;
359 struct device_node
*next
= child
->sibling
;
360 child
->sibling
= np
->child
;
373 * __unflatten_device_tree - create tree of device_nodes from flat blob
375 * unflattens a device-tree, creating the
376 * tree of struct device_node. It also fills the "name" and "type"
377 * pointers of the nodes so the normal device-tree walking functions
379 * @blob: The blob to expand
380 * @mynodes: The device_node tree created by the call
381 * @dt_alloc: An allocator that provides a virtual address to memory
382 * for the resulting tree
384 static void __unflatten_device_tree(const void *blob
,
385 struct device_node
**mynodes
,
386 void * (*dt_alloc
)(u64 size
, u64 align
))
392 pr_debug(" -> unflatten_device_tree()\n");
395 pr_debug("No device tree pointer\n");
399 pr_debug("Unflattening device tree:\n");
400 pr_debug("magic: %08x\n", fdt_magic(blob
));
401 pr_debug("size: %08x\n", fdt_totalsize(blob
));
402 pr_debug("version: %08x\n", fdt_version(blob
));
404 if (fdt_check_header(blob
)) {
405 pr_err("Invalid device tree blob header\n");
409 /* First pass, scan for size */
411 size
= (unsigned long)unflatten_dt_node(blob
, NULL
, &start
, NULL
, NULL
, 0, true);
412 size
= ALIGN(size
, 4);
414 pr_debug(" size is %lx, allocating...\n", size
);
416 /* Allocate memory for the expanded device tree */
417 mem
= dt_alloc(size
+ 4, __alignof__(struct device_node
));
418 memset(mem
, 0, size
);
420 *(__be32
*)(mem
+ size
) = cpu_to_be32(0xdeadbeef);
422 pr_debug(" unflattening %p...\n", mem
);
424 /* Second pass, do actual unflattening */
426 unflatten_dt_node(blob
, mem
, &start
, NULL
, mynodes
, 0, false);
427 if (be32_to_cpup(mem
+ size
) != 0xdeadbeef)
428 pr_warning("End of tree marker overwritten: %08x\n",
429 be32_to_cpup(mem
+ size
));
431 pr_debug(" <- unflatten_device_tree()\n");
434 static void *kernel_tree_alloc(u64 size
, u64 align
)
436 return kzalloc(size
, GFP_KERNEL
);
440 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
442 * unflattens the device-tree passed by the firmware, creating the
443 * tree of struct device_node. It also fills the "name" and "type"
444 * pointers of the nodes so the normal device-tree walking functions
447 void of_fdt_unflatten_tree(const unsigned long *blob
,
448 struct device_node
**mynodes
)
450 __unflatten_device_tree(blob
, mynodes
, &kernel_tree_alloc
);
452 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree
);
454 /* Everything below here references initial_boot_params directly. */
455 int __initdata dt_root_addr_cells
;
456 int __initdata dt_root_size_cells
;
458 void *initial_boot_params
;
460 #ifdef CONFIG_OF_EARLY_FLATTREE
462 static u32 of_fdt_crc32
;
465 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
467 static int __init
__reserved_mem_reserve_reg(unsigned long node
,
470 int t_len
= (dt_root_addr_cells
+ dt_root_size_cells
) * sizeof(__be32
);
471 phys_addr_t base
, size
;
474 int nomap
, first
= 1;
476 prop
= of_get_flat_dt_prop(node
, "reg", &len
);
480 if (len
&& len
% t_len
!= 0) {
481 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
486 nomap
= of_get_flat_dt_prop(node
, "no-map", NULL
) != NULL
;
488 while (len
>= t_len
) {
489 base
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
490 size
= dt_mem_next_cell(dt_root_size_cells
, &prop
);
493 early_init_dt_reserve_memory_arch(base
, size
, nomap
) == 0)
494 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
495 uname
, &base
, (unsigned long)size
/ SZ_1M
);
497 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
498 uname
, &base
, (unsigned long)size
/ SZ_1M
);
502 fdt_reserved_mem_save_node(node
, uname
, base
, size
);
510 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
511 * in /reserved-memory matches the values supported by the current implementation,
512 * also check if ranges property has been provided
514 static int __init
__reserved_mem_check_root(unsigned long node
)
518 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
519 if (!prop
|| be32_to_cpup(prop
) != dt_root_size_cells
)
522 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
523 if (!prop
|| be32_to_cpup(prop
) != dt_root_addr_cells
)
526 prop
= of_get_flat_dt_prop(node
, "ranges", NULL
);
533 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
535 static int __init
__fdt_scan_reserved_mem(unsigned long node
, const char *uname
,
536 int depth
, void *data
)
542 if (!found
&& depth
== 1 && strcmp(uname
, "reserved-memory") == 0) {
543 if (__reserved_mem_check_root(node
) != 0) {
544 pr_err("Reserved memory: unsupported node format, ignoring\n");
554 } else if (found
&& depth
< 2) {
555 /* scanning of /reserved-memory has been finished */
559 status
= of_get_flat_dt_prop(node
, "status", NULL
);
560 if (status
&& strcmp(status
, "okay") != 0 && strcmp(status
, "ok") != 0)
563 err
= __reserved_mem_reserve_reg(node
, uname
);
564 if (err
== -ENOENT
&& of_get_flat_dt_prop(node
, "size", NULL
))
565 fdt_reserved_mem_save_node(node
, uname
, 0, 0);
572 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
574 * This function grabs memory from early allocator for device exclusive use
575 * defined in device tree structures. It should be called by arch specific code
576 * once the early allocator (i.e. memblock) has been fully activated.
578 void __init
early_init_fdt_scan_reserved_mem(void)
583 if (!initial_boot_params
)
586 /* Process header /memreserve/ fields */
588 fdt_get_mem_rsv(initial_boot_params
, n
, &base
, &size
);
591 early_init_dt_reserve_memory_arch(base
, size
, 0);
594 of_scan_flat_dt(__fdt_scan_reserved_mem
, NULL
);
595 fdt_init_reserved_mem();
599 * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
601 void __init
early_init_fdt_reserve_self(void)
603 if (!initial_boot_params
)
606 /* Reserve the dtb region */
607 early_init_dt_reserve_memory_arch(__pa(initial_boot_params
),
608 fdt_totalsize(initial_boot_params
),
613 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
614 * @it: callback function
615 * @data: context data pointer
617 * This function is used to scan the flattened device-tree, it is
618 * used to extract the memory information at boot before we can
621 int __init
of_scan_flat_dt(int (*it
)(unsigned long node
,
622 const char *uname
, int depth
,
626 const void *blob
= initial_boot_params
;
628 int offset
, rc
= 0, depth
= -1;
630 for (offset
= fdt_next_node(blob
, -1, &depth
);
631 offset
>= 0 && depth
>= 0 && !rc
;
632 offset
= fdt_next_node(blob
, offset
, &depth
)) {
634 pathp
= fdt_get_name(blob
, offset
, NULL
);
636 pathp
= kbasename(pathp
);
637 rc
= it(offset
, pathp
, depth
, data
);
643 * of_get_flat_dt_root - find the root node in the flat blob
645 unsigned long __init
of_get_flat_dt_root(void)
651 * of_get_flat_dt_size - Return the total size of the FDT
653 int __init
of_get_flat_dt_size(void)
655 return fdt_totalsize(initial_boot_params
);
659 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
661 * This function can be used within scan_flattened_dt callback to get
662 * access to properties
664 const void *__init
of_get_flat_dt_prop(unsigned long node
, const char *name
,
667 return fdt_getprop(initial_boot_params
, node
, name
, size
);
671 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
672 * @node: node to test
673 * @compat: compatible string to compare with compatible list.
675 int __init
of_flat_dt_is_compatible(unsigned long node
, const char *compat
)
677 return of_fdt_is_compatible(initial_boot_params
, node
, compat
);
681 * of_flat_dt_match - Return true if node matches a list of compatible values
683 int __init
of_flat_dt_match(unsigned long node
, const char *const *compat
)
685 return of_fdt_match(initial_boot_params
, node
, compat
);
688 struct fdt_scan_status
{
693 int (*iterator
)(unsigned long node
, const char *uname
, int depth
, void *data
);
697 const char * __init
of_flat_dt_get_machine_name(void)
700 unsigned long dt_root
= of_get_flat_dt_root();
702 name
= of_get_flat_dt_prop(dt_root
, "model", NULL
);
704 name
= of_get_flat_dt_prop(dt_root
, "compatible", NULL
);
709 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
711 * @default_match: A machine specific ptr to return in case of no match.
712 * @get_next_compat: callback function to return next compatible match table.
714 * Iterate through machine match tables to find the best match for the machine
715 * compatible string in the FDT.
717 const void * __init
of_flat_dt_match_machine(const void *default_match
,
718 const void * (*get_next_compat
)(const char * const**))
720 const void *data
= NULL
;
721 const void *best_data
= default_match
;
722 const char *const *compat
;
723 unsigned long dt_root
;
724 unsigned int best_score
= ~1, score
= 0;
726 dt_root
= of_get_flat_dt_root();
727 while ((data
= get_next_compat(&compat
))) {
728 score
= of_flat_dt_match(dt_root
, compat
);
729 if (score
> 0 && score
< best_score
) {
738 pr_err("\n unrecognized device tree list:\n[ ");
740 prop
= of_get_flat_dt_prop(dt_root
, "compatible", &size
);
743 printk("'%s' ", prop
);
744 size
-= strlen(prop
) + 1;
745 prop
+= strlen(prop
) + 1;
752 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
757 #ifdef CONFIG_BLK_DEV_INITRD
759 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
760 * @node: reference to node containing initrd location ('chosen')
762 static void __init
early_init_dt_check_for_initrd(unsigned long node
)
768 pr_debug("Looking for initrd properties... ");
770 prop
= of_get_flat_dt_prop(node
, "linux,initrd-start", &len
);
773 start
= of_read_number(prop
, len
/4);
775 prop
= of_get_flat_dt_prop(node
, "linux,initrd-end", &len
);
778 end
= of_read_number(prop
, len
/4);
780 initrd_start
= (unsigned long)__va(start
);
781 initrd_end
= (unsigned long)__va(end
);
782 initrd_below_start_ok
= 1;
784 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
785 (unsigned long long)start
, (unsigned long long)end
);
788 static inline void early_init_dt_check_for_initrd(unsigned long node
)
791 #endif /* CONFIG_BLK_DEV_INITRD */
793 #ifdef CONFIG_SERIAL_EARLYCON
794 extern struct of_device_id __earlycon_of_table
[];
796 static int __init
early_init_dt_scan_chosen_serial(void)
801 const struct of_device_id
*match
= __earlycon_of_table
;
802 const void *fdt
= initial_boot_params
;
804 offset
= fdt_path_offset(fdt
, "/chosen");
806 offset
= fdt_path_offset(fdt
, "/chosen@0");
810 p
= fdt_getprop(fdt
, offset
, "stdout-path", &l
);
812 p
= fdt_getprop(fdt
, offset
, "linux,stdout-path", &l
);
816 /* Remove console options if present */
817 l
= strchrnul(p
, ':') - p
;
819 /* Get the node specified by stdout-path */
820 offset
= fdt_path_offset_namelen(fdt
, p
, l
);
824 while (match
->compatible
[0]) {
827 if (fdt_node_check_compatible(fdt
, offset
, match
->compatible
)) {
832 addr
= fdt_translate_address(fdt
, offset
);
833 if (addr
== OF_BAD_ADDR
)
836 of_setup_earlycon(addr
, match
->data
);
842 static int __init
setup_of_earlycon(char *buf
)
847 return early_init_dt_scan_chosen_serial();
849 early_param("earlycon", setup_of_earlycon
);
853 * early_init_dt_scan_root - fetch the top level address and size cells
855 int __init
early_init_dt_scan_root(unsigned long node
, const char *uname
,
856 int depth
, void *data
)
863 dt_root_size_cells
= OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
864 dt_root_addr_cells
= OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
866 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
868 dt_root_size_cells
= be32_to_cpup(prop
);
869 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells
);
871 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
873 dt_root_addr_cells
= be32_to_cpup(prop
);
874 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells
);
880 u64 __init
dt_mem_next_cell(int s
, const __be32
**cellp
)
882 const __be32
*p
= *cellp
;
885 return of_read_number(p
, s
);
889 * early_init_dt_scan_memory - Look for an parse memory nodes
891 int __init
early_init_dt_scan_memory(unsigned long node
, const char *uname
,
892 int depth
, void *data
)
894 const char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
895 const __be32
*reg
, *endp
;
898 /* We are scanning "memory" nodes only */
901 * The longtrail doesn't have a device_type on the
902 * /memory node, so look for the node called /memory@0.
904 if (!IS_ENABLED(CONFIG_PPC32
) || depth
!= 1 || strcmp(uname
, "memory@0") != 0)
906 } else if (strcmp(type
, "memory") != 0)
909 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
911 reg
= of_get_flat_dt_prop(node
, "reg", &l
);
915 endp
= reg
+ (l
/ sizeof(__be32
));
917 pr_debug("memory scan node %s, reg size %d,\n", uname
, l
);
919 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
922 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
923 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
927 pr_debug(" - %llx , %llx\n", (unsigned long long)base
,
928 (unsigned long long)size
);
930 early_init_dt_add_memory_arch(base
, size
);
936 int __init
early_init_dt_scan_chosen(unsigned long node
, const char *uname
,
937 int depth
, void *data
)
942 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
944 if (depth
!= 1 || !data
||
945 (strcmp(uname
, "chosen") != 0 && strcmp(uname
, "chosen@0") != 0))
948 early_init_dt_check_for_initrd(node
);
950 /* Retrieve command line */
951 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
952 if (p
!= NULL
&& l
> 0)
953 strlcpy(data
, p
, min((int)l
, COMMAND_LINE_SIZE
));
956 * CONFIG_CMDLINE is meant to be a default in case nothing else
957 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
958 * is set in which case we override whatever was found earlier.
960 #ifdef CONFIG_CMDLINE
961 #ifndef CONFIG_CMDLINE_FORCE
962 if (!((char *)data
)[0])
964 strlcpy(data
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
965 #endif /* CONFIG_CMDLINE */
967 pr_debug("Command line is: %s\n", (char*)data
);
973 #ifdef CONFIG_HAVE_MEMBLOCK
974 #ifndef MAX_MEMBLOCK_ADDR
975 #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
978 void __init __weak
early_init_dt_add_memory_arch(u64 base
, u64 size
)
980 const u64 phys_offset
= __pa(PAGE_OFFSET
);
982 if (!PAGE_ALIGNED(base
)) {
983 if (size
< PAGE_SIZE
- (base
& ~PAGE_MASK
)) {
984 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
988 size
-= PAGE_SIZE
- (base
& ~PAGE_MASK
);
989 base
= PAGE_ALIGN(base
);
993 if (base
> MAX_MEMBLOCK_ADDR
) {
994 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
999 if (base
+ size
- 1 > MAX_MEMBLOCK_ADDR
) {
1000 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1001 ((u64
)MAX_MEMBLOCK_ADDR
) + 1, base
+ size
);
1002 size
= MAX_MEMBLOCK_ADDR
- base
+ 1;
1005 if (base
+ size
< phys_offset
) {
1006 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1010 if (base
< phys_offset
) {
1011 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1013 size
-= phys_offset
- base
;
1016 memblock_add(base
, size
);
1019 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
1020 phys_addr_t size
, bool nomap
)
1023 return memblock_remove(base
, size
);
1024 return memblock_reserve(base
, size
);
1028 * called from unflatten_device_tree() to bootstrap devicetree itself
1029 * Architectures can override this definition if memblock isn't used
1031 void * __init __weak
early_init_dt_alloc_memory_arch(u64 size
, u64 align
)
1033 return __va(memblock_alloc(size
, align
));
1036 void __init __weak
early_init_dt_add_memory_arch(u64 base
, u64 size
)
1041 int __init __weak
early_init_dt_reserve_memory_arch(phys_addr_t base
,
1042 phys_addr_t size
, bool nomap
)
1044 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
1045 &base
, &size
, nomap
? " (nomap)" : "");
1049 void * __init __weak
early_init_dt_alloc_memory_arch(u64 size
, u64 align
)
1056 bool __init
early_init_dt_verify(void *params
)
1061 /* check device tree validity */
1062 if (fdt_check_header(params
))
1065 /* Setup flat device-tree pointer */
1066 initial_boot_params
= params
;
1067 of_fdt_crc32
= crc32_be(~0, initial_boot_params
,
1068 fdt_totalsize(initial_boot_params
));
1073 void __init
early_init_dt_scan_nodes(void)
1075 /* Retrieve various information from the /chosen node */
1076 of_scan_flat_dt(early_init_dt_scan_chosen
, boot_command_line
);
1078 /* Initialize {size,address}-cells info */
1079 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
1081 /* Setup memory, calling early_init_dt_add_memory_arch */
1082 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
1085 bool __init
early_init_dt_scan(void *params
)
1089 status
= early_init_dt_verify(params
);
1093 early_init_dt_scan_nodes();
1098 * unflatten_device_tree - create tree of device_nodes from flat blob
1100 * unflattens the device-tree passed by the firmware, creating the
1101 * tree of struct device_node. It also fills the "name" and "type"
1102 * pointers of the nodes so the normal device-tree walking functions
1105 void __init
unflatten_device_tree(void)
1107 __unflatten_device_tree(initial_boot_params
, &of_root
,
1108 early_init_dt_alloc_memory_arch
);
1110 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
1111 of_alias_scan(early_init_dt_alloc_memory_arch
);
1115 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1117 * Copies and unflattens the device-tree passed by the firmware, creating the
1118 * tree of struct device_node. It also fills the "name" and "type"
1119 * pointers of the nodes so the normal device-tree walking functions
1120 * can be used. This should only be used when the FDT memory has not been
1121 * reserved such is the case when the FDT is built-in to the kernel init
1122 * section. If the FDT memory is reserved already then unflatten_device_tree
1123 * should be used instead.
1125 void __init
unflatten_and_copy_device_tree(void)
1130 if (!initial_boot_params
) {
1131 pr_warn("No valid device tree found, continuing without\n");
1135 size
= fdt_totalsize(initial_boot_params
);
1136 dt
= early_init_dt_alloc_memory_arch(size
,
1137 roundup_pow_of_two(FDT_V17_SIZE
));
1140 memcpy(dt
, initial_boot_params
, size
);
1141 initial_boot_params
= dt
;
1143 unflatten_device_tree();
1147 static ssize_t
of_fdt_raw_read(struct file
*filp
, struct kobject
*kobj
,
1148 struct bin_attribute
*bin_attr
,
1149 char *buf
, loff_t off
, size_t count
)
1151 memcpy(buf
, initial_boot_params
+ off
, count
);
1155 static int __init
of_fdt_raw_init(void)
1157 static struct bin_attribute of_fdt_raw_attr
=
1158 __BIN_ATTR(fdt
, S_IRUSR
, of_fdt_raw_read
, NULL
, 0);
1160 if (!initial_boot_params
)
1163 if (of_fdt_crc32
!= crc32_be(~0, initial_boot_params
,
1164 fdt_totalsize(initial_boot_params
))) {
1165 pr_warn("fdt: not creating '/sys/firmware/fdt': CRC check failed\n");
1168 of_fdt_raw_attr
.size
= fdt_totalsize(initial_boot_params
);
1169 return sysfs_create_bin_file(firmware_kobj
, &of_fdt_raw_attr
);
1171 late_initcall(of_fdt_raw_init
);
1174 #endif /* CONFIG_OF_EARLY_FLATTREE */