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>
30 #include <linux/random.h>
32 #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
35 #include "of_private.h"
38 * of_fdt_limit_memory - limit the number of regions in the /memory node
39 * @limit: maximum entries
41 * Adjust the flattened device tree to have at most 'limit' number of
42 * memory entries in the /memory node. This function may be called
43 * any time after initial_boot_param is set.
45 void of_fdt_limit_memory(int limit)
50 int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
51 int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
52 const __be32 *addr_prop;
53 const __be32 *size_prop;
57 root_offset = fdt_path_offset(initial_boot_params, "/");
61 addr_prop = fdt_getprop(initial_boot_params, root_offset,
62 "#address-cells", NULL);
64 nr_address_cells = fdt32_to_cpu(*addr_prop);
66 size_prop = fdt_getprop(initial_boot_params, root_offset,
69 nr_size_cells = fdt32_to_cpu(*size_prop);
71 cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
73 memory = fdt_path_offset(initial_boot_params, "/memory");
75 val = fdt_getprop(initial_boot_params, memory, "reg", &len);
76 if (len > limit*cell_size) {
77 len = limit*cell_size;
78 pr_debug("Limiting number of entries to %d\n", limit);
79 fdt_setprop(initial_boot_params, memory, "reg", val,
86 * of_fdt_is_compatible - Return true if given node from the given blob has
87 * compat in its compatible list
88 * @blob: A device tree blob
90 * @compat: compatible string to compare with compatible list.
92 * On match, returns a non-zero value with smaller values returned for more
93 * specific compatible values.
95 static int of_fdt_is_compatible(const void *blob,
96 unsigned long node, const char *compat)
100 unsigned long l, score = 0;
102 cp = fdt_getprop(blob, node, "compatible", &cplen);
107 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
118 * of_fdt_is_big_endian - Return true if given node needs BE MMIO accesses
119 * @blob: A device tree blob
120 * @node: node to test
122 * Returns true if the node has a "big-endian" property, or if the kernel
123 * was compiled for BE *and* the node has a "native-endian" property.
124 * Returns false otherwise.
126 bool of_fdt_is_big_endian(const void *blob, unsigned long node)
128 if (fdt_getprop(blob, node, "big-endian", NULL))
130 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
131 fdt_getprop(blob, node, "native-endian", NULL))
137 * of_fdt_match - Return true if node matches a list of compatible values
139 int of_fdt_match(const void *blob, unsigned long node,
140 const char *const *compat)
142 unsigned int tmp, score = 0;
148 tmp = of_fdt_is_compatible(blob, node, *compat);
149 if (tmp && (score == 0 || (tmp < score)))
157 static void *unflatten_dt_alloc(void **mem, unsigned long size,
162 *mem = PTR_ALIGN(*mem, align);
169 static void populate_properties(const void *blob,
172 struct device_node *np,
173 const char *nodename,
176 struct property *pp, **pprev = NULL;
178 bool has_name = false;
180 pprev = &np->properties;
181 for (cur = fdt_first_property_offset(blob, offset);
183 cur = fdt_next_property_offset(blob, cur)) {
188 val = fdt_getprop_by_offset(blob, cur, &pname, &sz);
190 pr_warn("Cannot locate property at 0x%x\n", cur);
195 pr_warn("Cannot find property name at 0x%x\n", cur);
199 if (!strcmp(pname, "name"))
202 pp = unflatten_dt_alloc(mem, sizeof(struct property),
203 __alignof__(struct property));
207 /* We accept flattened tree phandles either in
208 * ePAPR-style "phandle" properties, or the
209 * legacy "linux,phandle" properties. If both
210 * appear and have different values, things
211 * will get weird. Don't do that.
213 if (!strcmp(pname, "phandle") ||
214 !strcmp(pname, "linux,phandle")) {
216 np->phandle = be32_to_cpup(val);
219 /* And we process the "ibm,phandle" property
220 * used in pSeries dynamic device tree
223 if (!strcmp(pname, "ibm,phandle"))
224 np->phandle = be32_to_cpup(val);
226 pp->name = (char *)pname;
228 pp->value = (__be32 *)val;
233 /* With version 0x10 we may not have the name property,
234 * recreate it here from the unit name if absent
237 const char *p = nodename, *ps = p, *pa = NULL;
243 else if ((*p) == '/')
251 pp = unflatten_dt_alloc(mem, sizeof(struct property) + len,
252 __alignof__(struct property));
259 memcpy(pp->value, ps, len - 1);
260 ((char *)pp->value)[len - 1] = 0;
261 pr_debug("fixed up name for %s -> %s\n",
262 nodename, (char *)pp->value);
270 static unsigned int populate_node(const void *blob,
273 struct device_node *dad,
275 struct device_node **pnp,
278 struct device_node *np;
280 unsigned int l, allocl;
283 pathp = fdt_get_name(blob, offset, &l);
291 /* version 0x10 has a more compact unit name here instead of the full
292 * path. we accumulate the full path size using "fpsize", we'll rebuild
293 * it later. We detect this because the first character of the name is
296 if ((*pathp) != '/') {
299 /* root node: special case. fpsize accounts for path
300 * plus terminating zero. root node only has '/', so
301 * fpsize should be 2, but we want to avoid the first
302 * level nodes to have two '/' so we use fpsize 1 here
309 /* account for '/' and path size minus terminal 0
317 np = unflatten_dt_alloc(mem, sizeof(struct device_node) + allocl,
318 __alignof__(struct device_node));
322 np->full_name = fn = ((char *)np) + sizeof(*np);
324 /* rebuild full path for new format */
325 if (dad && dad->parent) {
326 strcpy(fn, dad->full_name);
328 if ((strlen(fn) + l + 1) != allocl) {
329 pr_debug("%s: p: %d, l: %d, a: %d\n",
330 pathp, (int)strlen(fn),
338 memcpy(fn, pathp, l);
342 np->sibling = dad->child;
347 populate_properties(blob, offset, mem, np, pathp, dryrun);
349 np->name = of_get_property(np, "name", NULL);
350 np->type = of_get_property(np, "device_type", NULL);
362 static void reverse_nodes(struct device_node *parent)
364 struct device_node *child, *next;
367 child = parent->child;
369 reverse_nodes(child);
371 child = child->sibling;
374 /* Reverse the nodes in the child list */
375 child = parent->child;
376 parent->child = NULL;
378 next = child->sibling;
380 child->sibling = parent->child;
381 parent->child = child;
387 * unflatten_dt_nodes - Alloc and populate a device_node from the flat tree
388 * @blob: The parent device tree blob
389 * @mem: Memory chunk to use for allocating device nodes and properties
390 * @dad: Parent struct device_node
391 * @nodepp: The device_node tree created by the call
393 * It returns the size of unflattened device tree or error code
395 static int unflatten_dt_nodes(const void *blob,
397 struct device_node *dad,
398 struct device_node **nodepp)
400 struct device_node *root;
401 int offset = 0, depth = 0, initial_depth = 0;
402 #define FDT_MAX_DEPTH 64
403 unsigned int fpsizes[FDT_MAX_DEPTH];
404 struct device_node *nps[FDT_MAX_DEPTH];
412 * We're unflattening device sub-tree if @dad is valid. There are
413 * possibly multiple nodes in the first level of depth. We need
414 * set @depth to 1 to make fdt_next_node() happy as it bails
415 * immediately when negative @depth is found. Otherwise, the device
416 * nodes except the first one won't be unflattened successfully.
419 depth = initial_depth = 1;
422 fpsizes[depth] = dad ? strlen(of_node_full_name(dad)) : 0;
426 offset >= 0 && depth >= initial_depth;
427 offset = fdt_next_node(blob, offset, &depth)) {
428 if (WARN_ON_ONCE(depth >= FDT_MAX_DEPTH))
431 fpsizes[depth+1] = populate_node(blob, offset, &mem,
434 &nps[depth+1], dryrun);
435 if (!fpsizes[depth+1])
438 if (!dryrun && nodepp && !*nodepp)
439 *nodepp = nps[depth+1];
440 if (!dryrun && !root)
444 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
445 pr_err("Error %d processing FDT\n", offset);
450 * Reverse the child list. Some drivers assumes node order matches .dts
460 * __unflatten_device_tree - create tree of device_nodes from flat blob
462 * unflattens a device-tree, creating the
463 * tree of struct device_node. It also fills the "name" and "type"
464 * pointers of the nodes so the normal device-tree walking functions
466 * @blob: The blob to expand
467 * @dad: Parent device node
468 * @mynodes: The device_node tree created by the call
469 * @dt_alloc: An allocator that provides a virtual address to memory
470 * for the resulting tree
472 * Returns NULL on failure or the memory chunk containing the unflattened
473 * device tree on success.
475 void *__unflatten_device_tree(const void *blob,
476 struct device_node *dad,
477 struct device_node **mynodes,
478 void *(*dt_alloc)(u64 size, u64 align),
484 pr_debug(" -> unflatten_device_tree()\n");
487 pr_debug("No device tree pointer\n");
491 pr_debug("Unflattening device tree:\n");
492 pr_debug("magic: %08x\n", fdt_magic(blob));
493 pr_debug("size: %08x\n", fdt_totalsize(blob));
494 pr_debug("version: %08x\n", fdt_version(blob));
496 if (fdt_check_header(blob)) {
497 pr_err("Invalid device tree blob header\n");
501 /* First pass, scan for size */
502 size = unflatten_dt_nodes(blob, NULL, dad, NULL);
506 size = ALIGN(size, 4);
507 pr_debug(" size is %d, allocating...\n", size);
509 /* Allocate memory for the expanded device tree */
510 mem = dt_alloc(size + 4, __alignof__(struct device_node));
514 memset(mem, 0, size);
516 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
518 pr_debug(" unflattening %p...\n", mem);
520 /* Second pass, do actual unflattening */
521 unflatten_dt_nodes(blob, mem, dad, mynodes);
522 if (be32_to_cpup(mem + size) != 0xdeadbeef)
523 pr_warning("End of tree marker overwritten: %08x\n",
524 be32_to_cpup(mem + size));
526 if (detached && mynodes) {
527 of_node_set_flag(*mynodes, OF_DETACHED);
528 pr_debug("unflattened tree is detached\n");
531 pr_debug(" <- unflatten_device_tree()\n");
535 static void *kernel_tree_alloc(u64 size, u64 align)
537 return kzalloc(size, GFP_KERNEL);
540 static DEFINE_MUTEX(of_fdt_unflatten_mutex);
543 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
544 * @blob: Flat device tree blob
545 * @dad: Parent device node
546 * @mynodes: The device tree created by the call
548 * unflattens the device-tree passed by the firmware, creating the
549 * tree of struct device_node. It also fills the "name" and "type"
550 * pointers of the nodes so the normal device-tree walking functions
553 * Returns NULL on failure or the memory chunk containing the unflattened
554 * device tree on success.
556 void *of_fdt_unflatten_tree(const unsigned long *blob,
557 struct device_node *dad,
558 struct device_node **mynodes)
562 mutex_lock(&of_fdt_unflatten_mutex);
563 mem = __unflatten_device_tree(blob, dad, mynodes, &kernel_tree_alloc,
565 mutex_unlock(&of_fdt_unflatten_mutex);
569 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
571 /* Everything below here references initial_boot_params directly. */
572 int __initdata dt_root_addr_cells;
573 int __initdata dt_root_size_cells;
575 void *initial_boot_params;
577 #ifdef CONFIG_OF_EARLY_FLATTREE
579 static u32 of_fdt_crc32;
582 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
584 static int __init __reserved_mem_reserve_reg(unsigned long node,
587 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
588 phys_addr_t base, size;
591 int nomap, first = 1;
593 prop = of_get_flat_dt_prop(node, "reg", &len);
597 if (len && len % t_len != 0) {
598 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
603 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
605 while (len >= t_len) {
606 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
607 size = dt_mem_next_cell(dt_root_size_cells, &prop);
610 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
611 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
612 uname, &base, (unsigned long)(size / SZ_1M));
614 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
615 uname, &base, (unsigned long)(size / SZ_1M));
619 fdt_reserved_mem_save_node(node, uname, base, size);
627 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
628 * in /reserved-memory matches the values supported by the current implementation,
629 * also check if ranges property has been provided
631 static int __init __reserved_mem_check_root(unsigned long node)
635 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
636 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
639 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
640 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
643 prop = of_get_flat_dt_prop(node, "ranges", NULL);
650 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
652 static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
653 int depth, void *data)
659 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
660 if (__reserved_mem_check_root(node) != 0) {
661 pr_err("Reserved memory: unsupported node format, ignoring\n");
671 } else if (found && depth < 2) {
672 /* scanning of /reserved-memory has been finished */
676 status = of_get_flat_dt_prop(node, "status", NULL);
677 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
680 err = __reserved_mem_reserve_reg(node, uname);
681 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
682 fdt_reserved_mem_save_node(node, uname, 0, 0);
689 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
691 * This function grabs memory from early allocator for device exclusive use
692 * defined in device tree structures. It should be called by arch specific code
693 * once the early allocator (i.e. memblock) has been fully activated.
695 void __init early_init_fdt_scan_reserved_mem(void)
700 if (!initial_boot_params)
703 /* Process header /memreserve/ fields */
705 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
708 early_init_dt_reserve_memory_arch(base, size, 0);
711 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
712 fdt_init_reserved_mem();
716 * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
718 void __init early_init_fdt_reserve_self(void)
720 if (!initial_boot_params)
723 /* Reserve the dtb region */
724 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
725 fdt_totalsize(initial_boot_params),
730 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
731 * @it: callback function
732 * @data: context data pointer
734 * This function is used to scan the flattened device-tree, it is
735 * used to extract the memory information at boot before we can
738 int __init of_scan_flat_dt(int (*it)(unsigned long node,
739 const char *uname, int depth,
743 const void *blob = initial_boot_params;
745 int offset, rc = 0, depth = -1;
750 for (offset = fdt_next_node(blob, -1, &depth);
751 offset >= 0 && depth >= 0 && !rc;
752 offset = fdt_next_node(blob, offset, &depth)) {
754 pathp = fdt_get_name(blob, offset, NULL);
756 pathp = kbasename(pathp);
757 rc = it(offset, pathp, depth, data);
763 * of_scan_flat_dt_subnodes - scan sub-nodes of a node call callback on each.
764 * @it: callback function
765 * @data: context data pointer
767 * This function is used to scan sub-nodes of a node.
769 int __init of_scan_flat_dt_subnodes(unsigned long parent,
770 int (*it)(unsigned long node,
775 const void *blob = initial_boot_params;
778 fdt_for_each_subnode(node, blob, parent) {
782 pathp = fdt_get_name(blob, node, NULL);
784 pathp = kbasename(pathp);
785 rc = it(node, pathp, data);
793 * of_get_flat_dt_subnode_by_name - get the subnode by given name
795 * @node: the parent node
796 * @uname: the name of subnode
797 * @return offset of the subnode, or -FDT_ERR_NOTFOUND if there is none
800 int of_get_flat_dt_subnode_by_name(unsigned long node, const char *uname)
802 return fdt_subnode_offset(initial_boot_params, node, uname);
806 * of_get_flat_dt_root - find the root node in the flat blob
808 unsigned long __init of_get_flat_dt_root(void)
814 * of_get_flat_dt_size - Return the total size of the FDT
816 int __init of_get_flat_dt_size(void)
818 return fdt_totalsize(initial_boot_params);
822 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
824 * This function can be used within scan_flattened_dt callback to get
825 * access to properties
827 const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
830 return fdt_getprop(initial_boot_params, node, name, size);
834 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
835 * @node: node to test
836 * @compat: compatible string to compare with compatible list.
838 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
840 return of_fdt_is_compatible(initial_boot_params, node, compat);
844 * of_flat_dt_match - Return true if node matches a list of compatible values
846 int __init of_flat_dt_match(unsigned long node, const char *const *compat)
848 return of_fdt_match(initial_boot_params, node, compat);
852 * of_get_flat_dt_prop - Given a node in the flat blob, return the phandle
854 uint32_t __init of_get_flat_dt_phandle(unsigned long node)
856 return fdt_get_phandle(initial_boot_params, node);
859 struct fdt_scan_status {
864 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
868 const char * __init of_flat_dt_get_machine_name(void)
871 unsigned long dt_root = of_get_flat_dt_root();
873 name = of_get_flat_dt_prop(dt_root, "model", NULL);
875 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
880 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
882 * @default_match: A machine specific ptr to return in case of no match.
883 * @get_next_compat: callback function to return next compatible match table.
885 * Iterate through machine match tables to find the best match for the machine
886 * compatible string in the FDT.
888 const void * __init of_flat_dt_match_machine(const void *default_match,
889 const void * (*get_next_compat)(const char * const**))
891 const void *data = NULL;
892 const void *best_data = default_match;
893 const char *const *compat;
894 unsigned long dt_root;
895 unsigned int best_score = ~1, score = 0;
897 dt_root = of_get_flat_dt_root();
898 while ((data = get_next_compat(&compat))) {
899 score = of_flat_dt_match(dt_root, compat);
900 if (score > 0 && score < best_score) {
909 pr_err("\n unrecognized device tree list:\n[ ");
911 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
914 printk("'%s' ", prop);
915 size -= strlen(prop) + 1;
916 prop += strlen(prop) + 1;
923 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
928 #ifdef CONFIG_BLK_DEV_INITRD
929 #ifndef __early_init_dt_declare_initrd
930 static void __early_init_dt_declare_initrd(unsigned long start,
933 initrd_start = (unsigned long)__va(start);
934 initrd_end = (unsigned long)__va(end);
935 initrd_below_start_ok = 1;
940 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
941 * @node: reference to node containing initrd location ('chosen')
943 static void __init early_init_dt_check_for_initrd(unsigned long node)
949 pr_debug("Looking for initrd properties... ");
951 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
954 start = of_read_number(prop, len/4);
956 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
959 end = of_read_number(prop, len/4);
961 __early_init_dt_declare_initrd(start, end);
963 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
964 (unsigned long long)start, (unsigned long long)end);
967 static inline void early_init_dt_check_for_initrd(unsigned long node)
970 #endif /* CONFIG_BLK_DEV_INITRD */
972 #ifdef CONFIG_SERIAL_EARLYCON
974 int __init early_init_dt_scan_chosen_stdout(void)
977 const char *p, *q, *options = NULL;
979 const struct earlycon_id **p_match;
980 const void *fdt = initial_boot_params;
982 offset = fdt_path_offset(fdt, "/chosen");
984 offset = fdt_path_offset(fdt, "/chosen@0");
988 p = fdt_getprop(fdt, offset, "stdout-path", &l);
990 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
994 q = strchrnul(p, ':');
999 /* Get the node specified by stdout-path */
1000 offset = fdt_path_offset_namelen(fdt, p, l);
1002 pr_warn("earlycon: stdout-path %.*s not found\n", l, p);
1006 for (p_match = __earlycon_table; p_match < __earlycon_table_end;
1008 const struct earlycon_id *match = *p_match;
1010 if (!match->compatible[0])
1013 if (fdt_node_check_compatible(fdt, offset, match->compatible))
1016 of_setup_earlycon(match, offset, options);
1024 * early_init_dt_scan_root - fetch the top level address and size cells
1026 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
1027 int depth, void *data)
1034 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
1035 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
1037 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
1039 dt_root_size_cells = be32_to_cpup(prop);
1040 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
1042 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
1044 dt_root_addr_cells = be32_to_cpup(prop);
1045 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1051 u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
1053 const __be32 *p = *cellp;
1056 return of_read_number(p, s);
1060 * early_init_dt_scan_memory - Look for and parse memory nodes
1062 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
1063 int depth, void *data)
1065 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1066 const __be32 *reg, *endp;
1070 /* We are scanning "memory" nodes only */
1073 * The longtrail doesn't have a device_type on the
1074 * /memory node, so look for the node called /memory@0.
1076 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
1078 } else if (strcmp(type, "memory") != 0)
1081 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1083 reg = of_get_flat_dt_prop(node, "reg", &l);
1087 endp = reg + (l / sizeof(__be32));
1088 hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
1090 pr_debug("memory scan node %s, reg size %d,\n", uname, l);
1092 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1095 base = dt_mem_next_cell(dt_root_addr_cells, ®);
1096 size = dt_mem_next_cell(dt_root_size_cells, ®);
1100 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
1101 (unsigned long long)size);
1103 early_init_dt_add_memory_arch(base, size);
1108 if (early_init_dt_mark_hotplug_memory_arch(base, size))
1109 pr_warn("failed to mark hotplug range 0x%llx - 0x%llx\n",
1116 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
1117 int depth, void *data)
1121 const void *rng_seed;
1123 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1125 if (depth != 1 || !data ||
1126 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
1129 early_init_dt_check_for_initrd(node);
1131 /* Retrieve command line */
1132 p = of_get_flat_dt_prop(node, "bootargs", &l);
1133 if (p != NULL && l > 0)
1134 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
1137 * CONFIG_CMDLINE is meant to be a default in case nothing else
1138 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
1139 * is set in which case we override whatever was found earlier.
1141 #ifdef CONFIG_CMDLINE
1142 #if defined(CONFIG_CMDLINE_EXTEND)
1143 strlcat(data, " ", COMMAND_LINE_SIZE);
1144 strlcat(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1145 #elif defined(CONFIG_CMDLINE_FORCE)
1146 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1148 /* No arguments from boot loader, use kernel's cmdl*/
1149 if (!((char *)data)[0])
1150 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1152 #endif /* CONFIG_CMDLINE */
1154 pr_debug("Command line is: %s\n", (char*)data);
1156 rng_seed = of_get_flat_dt_prop(node, "rng-seed", &l);
1157 if (rng_seed && l > 0) {
1158 add_bootloader_randomness(rng_seed, l);
1160 /* try to clear seed so it won't be found. */
1161 fdt_nop_property(initial_boot_params, node, "rng-seed");
1163 /* update CRC check value */
1164 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1165 fdt_totalsize(initial_boot_params));
1172 #ifdef CONFIG_HAVE_MEMBLOCK
1173 #ifndef MIN_MEMBLOCK_ADDR
1174 #define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET)
1176 #ifndef MAX_MEMBLOCK_ADDR
1177 #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
1180 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
1182 const u64 phys_offset = MIN_MEMBLOCK_ADDR;
1184 if (!PAGE_ALIGNED(base)) {
1185 if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
1186 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
1190 size -= PAGE_SIZE - (base & ~PAGE_MASK);
1191 base = PAGE_ALIGN(base);
1195 if (base > MAX_MEMBLOCK_ADDR) {
1196 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1201 if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
1202 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1203 ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
1204 size = MAX_MEMBLOCK_ADDR - base + 1;
1207 if (base + size < phys_offset) {
1208 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1212 if (base < phys_offset) {
1213 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1215 size -= phys_offset - base;
1218 memblock_add(base, size);
1221 int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
1223 return memblock_mark_hotplug(base, size);
1226 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1227 phys_addr_t size, bool nomap)
1230 return memblock_remove(base, size);
1231 return memblock_reserve(base, size);
1235 * called from unflatten_device_tree() to bootstrap devicetree itself
1236 * Architectures can override this definition if memblock isn't used
1238 void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1240 return __va(memblock_alloc(size, align));
1243 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
1248 int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
1253 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1254 phys_addr_t size, bool nomap)
1256 pr_err("Reserved memory not supported, ignoring range %pa - %pa%s\n",
1257 &base, &size, nomap ? " (nomap)" : "");
1261 void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1268 bool __init early_init_dt_verify(void *params)
1273 /* check device tree validity */
1274 if (fdt_check_header(params))
1277 /* Setup flat device-tree pointer */
1278 initial_boot_params = params;
1279 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1280 fdt_totalsize(initial_boot_params));
1285 void __init early_init_dt_scan_nodes(void)
1287 /* Retrieve various information from the /chosen node */
1288 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1290 /* Initialize {size,address}-cells info */
1291 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1293 /* Setup memory, calling early_init_dt_add_memory_arch */
1294 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1297 bool __init early_init_dt_scan(void *params)
1301 status = early_init_dt_verify(params);
1305 early_init_dt_scan_nodes();
1310 * unflatten_device_tree - create tree of device_nodes from flat blob
1312 * unflattens the device-tree passed by the firmware, creating the
1313 * tree of struct device_node. It also fills the "name" and "type"
1314 * pointers of the nodes so the normal device-tree walking functions
1317 void __init unflatten_device_tree(void)
1319 __unflatten_device_tree(initial_boot_params, NULL, &of_root,
1320 early_init_dt_alloc_memory_arch, false);
1322 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
1323 of_alias_scan(early_init_dt_alloc_memory_arch);
1325 unittest_unflatten_overlay_base();
1329 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1331 * Copies and unflattens the device-tree passed by the firmware, creating the
1332 * tree of struct device_node. It also fills the "name" and "type"
1333 * pointers of the nodes so the normal device-tree walking functions
1334 * can be used. This should only be used when the FDT memory has not been
1335 * reserved such is the case when the FDT is built-in to the kernel init
1336 * section. If the FDT memory is reserved already then unflatten_device_tree
1337 * should be used instead.
1339 void __init unflatten_and_copy_device_tree(void)
1344 if (!initial_boot_params) {
1345 pr_warn("No valid device tree found, continuing without\n");
1349 size = fdt_totalsize(initial_boot_params);
1350 dt = early_init_dt_alloc_memory_arch(size,
1351 roundup_pow_of_two(FDT_V17_SIZE));
1354 memcpy(dt, initial_boot_params, size);
1355 initial_boot_params = dt;
1357 unflatten_device_tree();
1361 static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
1362 struct bin_attribute *bin_attr,
1363 char *buf, loff_t off, size_t count)
1365 memcpy(buf, initial_boot_params + off, count);
1369 static int __init of_fdt_raw_init(void)
1371 static struct bin_attribute of_fdt_raw_attr =
1372 __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
1374 if (!initial_boot_params)
1377 if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
1378 fdt_totalsize(initial_boot_params))) {
1379 pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n");
1382 of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
1383 return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
1385 late_initcall(of_fdt_raw_init);
1388 #endif /* CONFIG_OF_EARLY_FLATTREE */