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/kernel.h>
13 #include <linux/initrd.h>
14 #include <linux/module.h>
16 #include <linux/of_fdt.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
22 #include <asm/machdep.h>
23 #endif /* CONFIG_PPC */
27 char *of_fdt_get_string(struct boot_param_header *blob, u32 offset)
29 return ((char *)blob) +
30 be32_to_cpu(blob->off_dt_strings) + offset;
34 * of_fdt_get_property - Given a node in the given flat blob, return
37 void *of_fdt_get_property(struct boot_param_header *blob,
38 unsigned long node, const char *name,
41 unsigned long p = node;
44 u32 tag = be32_to_cpup((__be32 *)p);
51 if (tag != OF_DT_PROP)
54 sz = be32_to_cpup((__be32 *)p);
55 noff = be32_to_cpup((__be32 *)(p + 4));
57 if (be32_to_cpu(blob->version) < 0x10)
58 p = ALIGN(p, sz >= 8 ? 8 : 4);
60 nstr = of_fdt_get_string(blob, noff);
62 pr_warning("Can't find property index name !\n");
65 if (strcmp(name, nstr) == 0) {
76 * of_fdt_is_compatible - Return true if given node from the given blob has
77 * compat in its compatible list
78 * @blob: A device tree blob
80 * @compat: compatible string to compare with compatible list.
82 int of_fdt_is_compatible(struct boot_param_header *blob,
83 unsigned long node, const char *compat)
86 unsigned long cplen, l;
88 cp = of_fdt_get_property(blob, node, "compatible", &cplen);
92 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
102 static void *unflatten_dt_alloc(unsigned long *mem, unsigned long size,
107 *mem = ALIGN(*mem, align);
115 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
116 * @blob: The parent device tree blob
117 * @p: pointer to node in flat tree
118 * @dad: Parent struct device_node
119 * @allnextpp: pointer to ->allnext from last allocated device_node
120 * @fpsize: Size of the node path up at the current depth.
122 unsigned long unflatten_dt_node(struct boot_param_header *blob,
125 struct device_node *dad,
126 struct device_node ***allnextpp,
127 unsigned long fpsize)
129 struct device_node *np;
130 struct property *pp, **prev_pp = NULL;
133 unsigned int l, allocl;
137 tag = be32_to_cpup((__be32 *)(*p));
138 if (tag != OF_DT_BEGIN_NODE) {
139 pr_err("Weird tag at start of node: %x\n", tag);
144 l = allocl = strlen(pathp) + 1;
145 *p = ALIGN(*p + l, 4);
147 /* version 0x10 has a more compact unit name here instead of the full
148 * path. we accumulate the full path size using "fpsize", we'll rebuild
149 * it later. We detect this because the first character of the name is
152 if ((*pathp) != '/') {
155 /* root node: special case. fpsize accounts for path
156 * plus terminating zero. root node only has '/', so
157 * fpsize should be 2, but we want to avoid the first
158 * level nodes to have two '/' so we use fpsize 1 here
163 /* account for '/' and path size minus terminal 0
171 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
172 __alignof__(struct device_node));
174 memset(np, 0, sizeof(*np));
175 np->full_name = ((char *)np) + sizeof(struct device_node);
177 char *fn = np->full_name;
178 /* rebuild full path for new format */
179 if (dad && dad->parent) {
180 strcpy(fn, dad->full_name);
182 if ((strlen(fn) + l + 1) != allocl) {
183 pr_debug("%s: p: %d, l: %d, a: %d\n",
184 pathp, (int)strlen(fn),
191 memcpy(fn, pathp, l);
193 memcpy(np->full_name, pathp, l);
194 prev_pp = &np->properties;
196 *allnextpp = &np->allnext;
199 /* we temporarily use the next field as `last_child'*/
200 if (dad->next == NULL)
203 dad->next->sibling = np;
206 kref_init(&np->kref);
212 tag = be32_to_cpup((__be32 *)(*p));
213 if (tag == OF_DT_NOP) {
217 if (tag != OF_DT_PROP)
220 sz = be32_to_cpup((__be32 *)(*p));
221 noff = be32_to_cpup((__be32 *)((*p) + 4));
223 if (be32_to_cpu(blob->version) < 0x10)
224 *p = ALIGN(*p, sz >= 8 ? 8 : 4);
226 pname = of_fdt_get_string(blob, noff);
228 pr_info("Can't find property name in list !\n");
231 if (strcmp(pname, "name") == 0)
233 l = strlen(pname) + 1;
234 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
235 __alignof__(struct property));
237 /* We accept flattened tree phandles either in
238 * ePAPR-style "phandle" properties, or the
239 * legacy "linux,phandle" properties. If both
240 * appear and have different values, things
241 * will get weird. Don't do that. */
242 if ((strcmp(pname, "phandle") == 0) ||
243 (strcmp(pname, "linux,phandle") == 0)) {
244 if (np->phandle == 0)
245 np->phandle = be32_to_cpup((__be32*)*p);
247 /* And we process the "ibm,phandle" property
248 * used in pSeries dynamic device tree
250 if (strcmp(pname, "ibm,phandle") == 0)
251 np->phandle = be32_to_cpup((__be32 *)*p);
254 pp->value = (void *)*p;
258 *p = ALIGN((*p) + sz, 4);
260 /* with version 0x10 we may not have the name property, recreate
261 * it here from the unit name if absent
264 char *p1 = pathp, *ps = pathp, *pa = NULL;
277 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
278 __alignof__(struct property));
285 memcpy(pp->value, ps, sz - 1);
286 ((char *)pp->value)[sz - 1] = 0;
287 pr_debug("fixed up name for %s -> %s\n", pathp,
293 np->name = of_get_property(np, "name", NULL);
294 np->type = of_get_property(np, "device_type", NULL);
301 while (tag == OF_DT_BEGIN_NODE || tag == OF_DT_NOP) {
302 if (tag == OF_DT_NOP)
305 mem = unflatten_dt_node(blob, mem, p, np, allnextpp,
307 tag = be32_to_cpup((__be32 *)(*p));
309 if (tag != OF_DT_END_NODE) {
310 pr_err("Weird tag at end of node: %x\n", tag);
318 * __unflatten_device_tree - create tree of device_nodes from flat blob
320 * unflattens a device-tree, creating the
321 * tree of struct device_node. It also fills the "name" and "type"
322 * pointers of the nodes so the normal device-tree walking functions
324 * @blob: The blob to expand
325 * @mynodes: The device_node tree created by the call
326 * @dt_alloc: An allocator that provides a virtual address to memory
327 * for the resulting tree
329 void __unflatten_device_tree(struct boot_param_header *blob,
330 struct device_node **mynodes,
331 void * (*dt_alloc)(u64 size, u64 align))
333 unsigned long start, mem, size;
334 struct device_node **allnextp = mynodes;
336 pr_debug(" -> unflatten_device_tree()\n");
339 pr_debug("No device tree pointer\n");
343 pr_debug("Unflattening device tree:\n");
344 pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
345 pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
346 pr_debug("version: %08x\n", be32_to_cpu(blob->version));
348 if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
349 pr_err("Invalid device tree blob header\n");
353 /* First pass, scan for size */
354 start = ((unsigned long)blob) +
355 be32_to_cpu(blob->off_dt_struct);
356 size = unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
357 size = (size | 3) + 1;
359 pr_debug(" size is %lx, allocating...\n", size);
361 /* Allocate memory for the expanded device tree */
362 mem = (unsigned long)
363 dt_alloc(size + 4, __alignof__(struct device_node));
365 ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
367 pr_debug(" unflattening %lx...\n", mem);
369 /* Second pass, do actual unflattening */
370 start = ((unsigned long)blob) +
371 be32_to_cpu(blob->off_dt_struct);
372 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
373 if (be32_to_cpup((__be32 *)start) != OF_DT_END)
374 pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
375 if (be32_to_cpu(((__be32 *)mem)[size / 4]) != 0xdeadbeef)
376 pr_warning("End of tree marker overwritten: %08x\n",
377 be32_to_cpu(((__be32 *)mem)[size / 4]));
380 pr_debug(" <- unflatten_device_tree()\n");
383 static void *kernel_tree_alloc(u64 size, u64 align)
385 return kzalloc(size, GFP_KERNEL);
389 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
391 * unflattens the device-tree passed by the firmware, creating the
392 * tree of struct device_node. It also fills the "name" and "type"
393 * pointers of the nodes so the normal device-tree walking functions
396 void of_fdt_unflatten_tree(unsigned long *blob,
397 struct device_node **mynodes)
399 struct boot_param_header *device_tree =
400 (struct boot_param_header *)blob;
401 __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
403 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
405 /* Everything below here references initial_boot_params directly. */
406 int __initdata dt_root_addr_cells;
407 int __initdata dt_root_size_cells;
409 struct boot_param_header *initial_boot_params;
411 #ifdef CONFIG_OF_EARLY_FLATTREE
414 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
415 * @it: callback function
416 * @data: context data pointer
418 * This function is used to scan the flattened device-tree, it is
419 * used to extract the memory information at boot before we can
422 int __init of_scan_flat_dt(int (*it)(unsigned long node,
423 const char *uname, int depth,
427 unsigned long p = ((unsigned long)initial_boot_params) +
428 be32_to_cpu(initial_boot_params->off_dt_struct);
433 u32 tag = be32_to_cpup((__be32 *)p);
437 if (tag == OF_DT_END_NODE) {
441 if (tag == OF_DT_NOP)
443 if (tag == OF_DT_END)
445 if (tag == OF_DT_PROP) {
446 u32 sz = be32_to_cpup((__be32 *)p);
448 if (be32_to_cpu(initial_boot_params->version) < 0x10)
449 p = ALIGN(p, sz >= 8 ? 8 : 4);
454 if (tag != OF_DT_BEGIN_NODE) {
455 pr_err("Invalid tag %x in flat device tree!\n", tag);
460 p = ALIGN(p + strlen(pathp) + 1, 4);
461 if ((*pathp) == '/') {
463 for (lp = NULL, np = pathp; *np; np++)
469 rc = it(p, pathp, depth, data);
478 * of_get_flat_dt_root - find the root node in the flat blob
480 unsigned long __init of_get_flat_dt_root(void)
482 unsigned long p = ((unsigned long)initial_boot_params) +
483 be32_to_cpu(initial_boot_params->off_dt_struct);
485 while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
487 BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
489 return ALIGN(p + strlen((char *)p) + 1, 4);
493 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
495 * This function can be used within scan_flattened_dt callback to get
496 * access to properties
498 void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
501 return of_fdt_get_property(initial_boot_params, node, name, size);
505 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
506 * @node: node to test
507 * @compat: compatible string to compare with compatible list.
509 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
511 return of_fdt_is_compatible(initial_boot_params, node, compat);
514 #ifdef CONFIG_BLK_DEV_INITRD
516 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
517 * @node: reference to node containing initrd location ('chosen')
519 void __init early_init_dt_check_for_initrd(unsigned long node)
521 unsigned long start, end, len;
524 pr_debug("Looking for initrd properties... ");
526 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
529 start = of_read_ulong(prop, len/4);
531 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
534 end = of_read_ulong(prop, len/4);
536 early_init_dt_setup_initrd_arch(start, end);
537 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n", start, end);
540 inline void early_init_dt_check_for_initrd(unsigned long node)
543 #endif /* CONFIG_BLK_DEV_INITRD */
546 * early_init_dt_scan_root - fetch the top level address and size cells
548 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
549 int depth, void *data)
556 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
557 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
559 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
561 dt_root_size_cells = be32_to_cpup(prop);
562 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
564 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
566 dt_root_addr_cells = be32_to_cpup(prop);
567 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
573 u64 __init dt_mem_next_cell(int s, __be32 **cellp)
578 return of_read_number(p, s);
582 * early_init_dt_scan_memory - Look for an parse memory nodes
584 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
585 int depth, void *data)
587 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
591 /* We are scanning "memory" nodes only */
594 * The longtrail doesn't have a device_type on the
595 * /memory node, so look for the node called /memory@0.
597 if (depth != 1 || strcmp(uname, "memory@0") != 0)
599 } else if (strcmp(type, "memory") != 0)
602 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
604 reg = of_get_flat_dt_prop(node, "reg", &l);
608 endp = reg + (l / sizeof(__be32));
610 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
611 uname, l, reg[0], reg[1], reg[2], reg[3]);
613 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
616 base = dt_mem_next_cell(dt_root_addr_cells, ®);
617 size = dt_mem_next_cell(dt_root_size_cells, ®);
621 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
622 (unsigned long long)size);
624 early_init_dt_add_memory_arch(base, size);
630 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
631 int depth, void *data)
636 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
639 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
642 early_init_dt_check_for_initrd(node);
644 /* Retreive command line */
645 p = of_get_flat_dt_prop(node, "bootargs", &l);
646 if (p != NULL && l > 0)
647 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
649 #ifdef CONFIG_CMDLINE
650 #ifndef CONFIG_CMDLINE_FORCE
651 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
653 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
654 #endif /* CONFIG_CMDLINE */
656 pr_debug("Command line is: %s\n", cmd_line);
662 static void *__init early_device_tree_alloc(u64 size, u64 align)
664 unsigned long mem = early_init_dt_alloc_memory_arch(size, align);
669 * unflatten_device_tree - create tree of device_nodes from flat blob
671 * unflattens the device-tree passed by the firmware, creating the
672 * tree of struct device_node. It also fills the "name" and "type"
673 * pointers of the nodes so the normal device-tree walking functions
676 void __init unflatten_device_tree(void)
678 __unflatten_device_tree(initial_boot_params, &allnodes,
679 early_device_tree_alloc);
681 /* Get pointer to OF "/chosen" node for use everywhere */
682 of_chosen = of_find_node_by_path("/chosen");
683 if (of_chosen == NULL)
684 of_chosen = of_find_node_by_path("/chosen@0");
687 #endif /* CONFIG_OF_EARLY_FLATTREE */