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>
15 #include <linux/of_fdt.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
20 #include <asm/machdep.h>
21 #endif /* CONFIG_PPC */
25 int __initdata dt_root_addr_cells
;
26 int __initdata dt_root_size_cells
;
28 struct boot_param_header
*initial_boot_params
;
30 char *find_flat_dt_string(u32 offset
)
32 return ((char *)initial_boot_params
) +
33 be32_to_cpu(initial_boot_params
->off_dt_strings
) + offset
;
37 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
38 * @it: callback function
39 * @data: context data pointer
41 * This function is used to scan the flattened device-tree, it is
42 * used to extract the memory information at boot before we can
45 int __init
of_scan_flat_dt(int (*it
)(unsigned long node
,
46 const char *uname
, int depth
,
50 unsigned long p
= ((unsigned long)initial_boot_params
) +
51 be32_to_cpu(initial_boot_params
->off_dt_struct
);
56 u32 tag
= be32_to_cpup((__be32
*)p
);
60 if (tag
== OF_DT_END_NODE
) {
68 if (tag
== OF_DT_PROP
) {
69 u32 sz
= be32_to_cpup((__be32
*)p
);
71 if (be32_to_cpu(initial_boot_params
->version
) < 0x10)
72 p
= ALIGN(p
, sz
>= 8 ? 8 : 4);
77 if (tag
!= OF_DT_BEGIN_NODE
) {
78 pr_err("Invalid tag %x in flat device tree!\n", tag
);
83 p
= ALIGN(p
+ strlen(pathp
) + 1, 4);
84 if ((*pathp
) == '/') {
86 for (lp
= NULL
, np
= pathp
; *np
; np
++)
92 rc
= it(p
, pathp
, depth
, data
);
101 * of_get_flat_dt_root - find the root node in the flat blob
103 unsigned long __init
of_get_flat_dt_root(void)
105 unsigned long p
= ((unsigned long)initial_boot_params
) +
106 be32_to_cpu(initial_boot_params
->off_dt_struct
);
108 while (be32_to_cpup((__be32
*)p
) == OF_DT_NOP
)
110 BUG_ON(be32_to_cpup((__be32
*)p
) != OF_DT_BEGIN_NODE
);
112 return ALIGN(p
+ strlen((char *)p
) + 1, 4);
116 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
118 * This function can be used within scan_flattened_dt callback to get
119 * access to properties
121 void *__init
of_get_flat_dt_prop(unsigned long node
, const char *name
,
124 unsigned long p
= node
;
127 u32 tag
= be32_to_cpup((__be32
*)p
);
132 if (tag
== OF_DT_NOP
)
134 if (tag
!= OF_DT_PROP
)
137 sz
= be32_to_cpup((__be32
*)p
);
138 noff
= be32_to_cpup((__be32
*)(p
+ 4));
140 if (be32_to_cpu(initial_boot_params
->version
) < 0x10)
141 p
= ALIGN(p
, sz
>= 8 ? 8 : 4);
143 nstr
= find_flat_dt_string(noff
);
145 pr_warning("Can't find property index name !\n");
148 if (strcmp(name
, nstr
) == 0) {
159 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
160 * @node: node to test
161 * @compat: compatible string to compare with compatible list.
163 int __init
of_flat_dt_is_compatible(unsigned long node
, const char *compat
)
166 unsigned long cplen
, l
;
168 cp
= of_get_flat_dt_prop(node
, "compatible", &cplen
);
172 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0)
182 static void *__init
unflatten_dt_alloc(unsigned long *mem
, unsigned long size
,
187 *mem
= ALIGN(*mem
, align
);
195 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
196 * @p: pointer to node in flat tree
197 * @dad: Parent struct device_node
198 * @allnextpp: pointer to ->allnext from last allocated device_node
199 * @fpsize: Size of the node path up at the current depth.
201 unsigned long __init
unflatten_dt_node(unsigned long mem
,
203 struct device_node
*dad
,
204 struct device_node
***allnextpp
,
205 unsigned long fpsize
)
207 struct device_node
*np
;
208 struct property
*pp
, **prev_pp
= NULL
;
211 unsigned int l
, allocl
;
215 tag
= be32_to_cpup((__be32
*)(*p
));
216 if (tag
!= OF_DT_BEGIN_NODE
) {
217 pr_err("Weird tag at start of node: %x\n", tag
);
222 l
= allocl
= strlen(pathp
) + 1;
223 *p
= ALIGN(*p
+ l
, 4);
225 /* version 0x10 has a more compact unit name here instead of the full
226 * path. we accumulate the full path size using "fpsize", we'll rebuild
227 * it later. We detect this because the first character of the name is
230 if ((*pathp
) != '/') {
233 /* root node: special case. fpsize accounts for path
234 * plus terminating zero. root node only has '/', so
235 * fpsize should be 2, but we want to avoid the first
236 * level nodes to have two '/' so we use fpsize 1 here
241 /* account for '/' and path size minus terminal 0
249 np
= unflatten_dt_alloc(&mem
, sizeof(struct device_node
) + allocl
,
250 __alignof__(struct device_node
));
252 memset(np
, 0, sizeof(*np
));
253 np
->full_name
= ((char *)np
) + sizeof(struct device_node
);
255 char *fn
= np
->full_name
;
256 /* rebuild full path for new format */
257 if (dad
&& dad
->parent
) {
258 strcpy(fn
, dad
->full_name
);
260 if ((strlen(fn
) + l
+ 1) != allocl
) {
261 pr_debug("%s: p: %d, l: %d, a: %d\n",
262 pathp
, (int)strlen(fn
),
269 memcpy(fn
, pathp
, l
);
271 memcpy(np
->full_name
, pathp
, l
);
272 prev_pp
= &np
->properties
;
274 *allnextpp
= &np
->allnext
;
277 /* we temporarily use the next field as `last_child'*/
278 if (dad
->next
== NULL
)
281 dad
->next
->sibling
= np
;
284 kref_init(&np
->kref
);
290 tag
= be32_to_cpup((__be32
*)(*p
));
291 if (tag
== OF_DT_NOP
) {
295 if (tag
!= OF_DT_PROP
)
298 sz
= be32_to_cpup((__be32
*)(*p
));
299 noff
= be32_to_cpup((__be32
*)((*p
) + 4));
301 if (be32_to_cpu(initial_boot_params
->version
) < 0x10)
302 *p
= ALIGN(*p
, sz
>= 8 ? 8 : 4);
304 pname
= find_flat_dt_string(noff
);
306 pr_info("Can't find property name in list !\n");
309 if (strcmp(pname
, "name") == 0)
311 l
= strlen(pname
) + 1;
312 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
313 __alignof__(struct property
));
315 /* We accept flattened tree phandles either in
316 * ePAPR-style "phandle" properties, or the
317 * legacy "linux,phandle" properties. If both
318 * appear and have different values, things
319 * will get weird. Don't do that. */
320 if ((strcmp(pname
, "phandle") == 0) ||
321 (strcmp(pname
, "linux,phandle") == 0)) {
322 if (np
->phandle
== 0)
323 np
->phandle
= be32_to_cpup((__be32
*)*p
);
325 /* And we process the "ibm,phandle" property
326 * used in pSeries dynamic device tree
328 if (strcmp(pname
, "ibm,phandle") == 0)
329 np
->phandle
= be32_to_cpup((__be32
*)*p
);
332 pp
->value
= (void *)*p
;
336 *p
= ALIGN((*p
) + sz
, 4);
338 /* with version 0x10 we may not have the name property, recreate
339 * it here from the unit name if absent
342 char *p1
= pathp
, *ps
= pathp
, *pa
= NULL
;
355 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
) + sz
,
356 __alignof__(struct property
));
363 memcpy(pp
->value
, ps
, sz
- 1);
364 ((char *)pp
->value
)[sz
- 1] = 0;
365 pr_debug("fixed up name for %s -> %s\n", pathp
,
371 np
->name
= of_get_property(np
, "name", NULL
);
372 np
->type
= of_get_property(np
, "device_type", NULL
);
379 while (tag
== OF_DT_BEGIN_NODE
|| tag
== OF_DT_NOP
) {
380 if (tag
== OF_DT_NOP
)
383 mem
= unflatten_dt_node(mem
, p
, np
, allnextpp
, fpsize
);
384 tag
= be32_to_cpup((__be32
*)(*p
));
386 if (tag
!= OF_DT_END_NODE
) {
387 pr_err("Weird tag at end of node: %x\n", tag
);
394 #ifdef CONFIG_BLK_DEV_INITRD
396 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
397 * @node: reference to node containing initrd location ('chosen')
399 void __init
early_init_dt_check_for_initrd(unsigned long node
)
401 unsigned long start
, end
, len
;
404 pr_debug("Looking for initrd properties... ");
406 prop
= of_get_flat_dt_prop(node
, "linux,initrd-start", &len
);
409 start
= of_read_ulong(prop
, len
/4);
411 prop
= of_get_flat_dt_prop(node
, "linux,initrd-end", &len
);
414 end
= of_read_ulong(prop
, len
/4);
416 early_init_dt_setup_initrd_arch(start
, end
);
417 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n", start
, end
);
420 inline void early_init_dt_check_for_initrd(unsigned long node
)
423 #endif /* CONFIG_BLK_DEV_INITRD */
426 * early_init_dt_scan_root - fetch the top level address and size cells
428 int __init
early_init_dt_scan_root(unsigned long node
, const char *uname
,
429 int depth
, void *data
)
436 dt_root_size_cells
= OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
437 dt_root_addr_cells
= OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
439 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
441 dt_root_size_cells
= be32_to_cpup(prop
);
442 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells
);
444 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
446 dt_root_addr_cells
= be32_to_cpup(prop
);
447 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells
);
453 u64 __init
dt_mem_next_cell(int s
, __be32
**cellp
)
458 return of_read_number(p
, s
);
462 * early_init_dt_scan_memory - Look for an parse memory nodes
464 int __init
early_init_dt_scan_memory(unsigned long node
, const char *uname
,
465 int depth
, void *data
)
467 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
471 /* We are scanning "memory" nodes only */
474 * The longtrail doesn't have a device_type on the
475 * /memory node, so look for the node called /memory@0.
477 if (depth
!= 1 || strcmp(uname
, "memory@0") != 0)
479 } else if (strcmp(type
, "memory") != 0)
482 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
484 reg
= of_get_flat_dt_prop(node
, "reg", &l
);
488 endp
= reg
+ (l
/ sizeof(__be32
));
490 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
491 uname
, l
, reg
[0], reg
[1], reg
[2], reg
[3]);
493 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
496 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
497 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
501 pr_debug(" - %llx , %llx\n", (unsigned long long)base
,
502 (unsigned long long)size
);
504 early_init_dt_add_memory_arch(base
, size
);
510 int __init
early_init_dt_scan_chosen(unsigned long node
, const char *uname
,
511 int depth
, void *data
)
516 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
519 (strcmp(uname
, "chosen") != 0 && strcmp(uname
, "chosen@0") != 0))
522 early_init_dt_check_for_initrd(node
);
524 /* Retreive command line */
525 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
526 if (p
!= NULL
&& l
> 0)
527 strlcpy(cmd_line
, p
, min((int)l
, COMMAND_LINE_SIZE
));
529 #ifdef CONFIG_CMDLINE
530 #ifndef CONFIG_CMDLINE_FORCE
531 if (p
== NULL
|| l
== 0 || (l
== 1 && (*p
) == 0))
533 strlcpy(cmd_line
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
534 #endif /* CONFIG_CMDLINE */
536 early_init_dt_scan_chosen_arch(node
);
538 pr_debug("Command line is: %s\n", cmd_line
);
545 * unflatten_device_tree - create tree of device_nodes from flat blob
547 * unflattens the device-tree passed by the firmware, creating the
548 * tree of struct device_node. It also fills the "name" and "type"
549 * pointers of the nodes so the normal device-tree walking functions
552 void __init
unflatten_device_tree(void)
554 unsigned long start
, mem
, size
;
555 struct device_node
**allnextp
= &allnodes
;
557 pr_debug(" -> unflatten_device_tree()\n");
559 if (!initial_boot_params
) {
560 pr_debug("No device tree pointer\n");
564 pr_debug("Unflattening device tree:\n");
565 pr_debug("magic: %08x\n", be32_to_cpu(initial_boot_params
->magic
));
566 pr_debug("size: %08x\n", be32_to_cpu(initial_boot_params
->totalsize
));
567 pr_debug("version: %08x\n", be32_to_cpu(initial_boot_params
->version
));
569 if (be32_to_cpu(initial_boot_params
->magic
) != OF_DT_HEADER
) {
570 pr_err("Invalid device tree blob header\n");
574 /* First pass, scan for size */
575 start
= ((unsigned long)initial_boot_params
) +
576 be32_to_cpu(initial_boot_params
->off_dt_struct
);
577 size
= unflatten_dt_node(0, &start
, NULL
, NULL
, 0);
578 size
= (size
| 3) + 1;
580 pr_debug(" size is %lx, allocating...\n", size
);
582 /* Allocate memory for the expanded device tree */
583 mem
= early_init_dt_alloc_memory_arch(size
+ 4,
584 __alignof__(struct device_node
));
585 mem
= (unsigned long) __va(mem
);
587 ((__be32
*)mem
)[size
/ 4] = cpu_to_be32(0xdeadbeef);
589 pr_debug(" unflattening %lx...\n", mem
);
591 /* Second pass, do actual unflattening */
592 start
= ((unsigned long)initial_boot_params
) +
593 be32_to_cpu(initial_boot_params
->off_dt_struct
);
594 unflatten_dt_node(mem
, &start
, NULL
, &allnextp
, 0);
595 if (be32_to_cpup((__be32
*)start
) != OF_DT_END
)
596 pr_warning("Weird tag at end of tree: %08x\n", *((u32
*)start
));
597 if (be32_to_cpu(((__be32
*)mem
)[size
/ 4]) != 0xdeadbeef)
598 pr_warning("End of tree marker overwritten: %08x\n",
599 be32_to_cpu(((__be32
*)mem
)[size
/ 4]));
602 /* Get pointer to OF "/chosen" node for use everywhere */
603 of_chosen
= of_find_node_by_path("/chosen");
604 if (of_chosen
== NULL
)
605 of_chosen
= of_find_node_by_path("/chosen@0");
607 pr_debug(" <- unflatten_device_tree()\n");