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/lmb.h>
14 #include <linux/initrd.h>
16 #include <linux/of_fdt.h>
20 #include <asm/machdep.h>
21 #endif /* CONFIG_PPC */
23 int __initdata dt_root_addr_cells
;
24 int __initdata dt_root_size_cells
;
26 struct boot_param_header
*initial_boot_params
;
28 char *find_flat_dt_string(u32 offset
)
30 return ((char *)initial_boot_params
) +
31 be32_to_cpu(initial_boot_params
->off_dt_strings
) + offset
;
35 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
36 * @it: callback function
37 * @data: context data pointer
39 * This function is used to scan the flattened device-tree, it is
40 * used to extract the memory information at boot before we can
43 int __init
of_scan_flat_dt(int (*it
)(unsigned long node
,
44 const char *uname
, int depth
,
48 unsigned long p
= ((unsigned long)initial_boot_params
) +
49 be32_to_cpu(initial_boot_params
->off_dt_struct
);
54 u32 tag
= be32_to_cpup((__be32
*)p
);
58 if (tag
== OF_DT_END_NODE
) {
66 if (tag
== OF_DT_PROP
) {
67 u32 sz
= be32_to_cpup((__be32
*)p
);
69 if (be32_to_cpu(initial_boot_params
->version
) < 0x10)
70 p
= _ALIGN(p
, sz
>= 8 ? 8 : 4);
75 if (tag
!= OF_DT_BEGIN_NODE
) {
76 pr_err("Invalid tag %x in flat device tree!\n", tag
);
81 p
= _ALIGN(p
+ strlen(pathp
) + 1, 4);
82 if ((*pathp
) == '/') {
84 for (lp
= NULL
, np
= pathp
; *np
; np
++)
90 rc
= it(p
, pathp
, depth
, data
);
99 * of_get_flat_dt_root - find the root node in the flat blob
101 unsigned long __init
of_get_flat_dt_root(void)
103 unsigned long p
= ((unsigned long)initial_boot_params
) +
104 be32_to_cpu(initial_boot_params
->off_dt_struct
);
106 while (be32_to_cpup((__be32
*)p
) == OF_DT_NOP
)
108 BUG_ON(be32_to_cpup((__be32
*)p
) != OF_DT_BEGIN_NODE
);
110 return _ALIGN(p
+ strlen((char *)p
) + 1, 4);
114 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
116 * This function can be used within scan_flattened_dt callback to get
117 * access to properties
119 void *__init
of_get_flat_dt_prop(unsigned long node
, const char *name
,
122 unsigned long p
= node
;
125 u32 tag
= be32_to_cpup((__be32
*)p
);
130 if (tag
== OF_DT_NOP
)
132 if (tag
!= OF_DT_PROP
)
135 sz
= be32_to_cpup((__be32
*)p
);
136 noff
= be32_to_cpup((__be32
*)(p
+ 4));
138 if (be32_to_cpu(initial_boot_params
->version
) < 0x10)
139 p
= _ALIGN(p
, sz
>= 8 ? 8 : 4);
141 nstr
= find_flat_dt_string(noff
);
143 pr_warning("Can't find property index name !\n");
146 if (strcmp(name
, nstr
) == 0) {
157 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
158 * @node: node to test
159 * @compat: compatible string to compare with compatible list.
161 int __init
of_flat_dt_is_compatible(unsigned long node
, const char *compat
)
164 unsigned long cplen
, l
;
166 cp
= of_get_flat_dt_prop(node
, "compatible", &cplen
);
170 if (strncasecmp(cp
, compat
, strlen(compat
)) == 0)
180 static void *__init
unflatten_dt_alloc(unsigned long *mem
, unsigned long size
,
185 *mem
= _ALIGN(*mem
, align
);
193 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
194 * @p: pointer to node in flat tree
195 * @dad: Parent struct device_node
196 * @allnextpp: pointer to ->allnext from last allocated device_node
197 * @fpsize: Size of the node path up at the current depth.
199 unsigned long __init
unflatten_dt_node(unsigned long mem
,
201 struct device_node
*dad
,
202 struct device_node
***allnextpp
,
203 unsigned long fpsize
)
205 struct device_node
*np
;
206 struct property
*pp
, **prev_pp
= NULL
;
209 unsigned int l
, allocl
;
213 tag
= be32_to_cpup((__be32
*)(*p
));
214 if (tag
!= OF_DT_BEGIN_NODE
) {
215 pr_err("Weird tag at start of node: %x\n", tag
);
220 l
= allocl
= strlen(pathp
) + 1;
221 *p
= _ALIGN(*p
+ l
, 4);
223 /* version 0x10 has a more compact unit name here instead of the full
224 * path. we accumulate the full path size using "fpsize", we'll rebuild
225 * it later. We detect this because the first character of the name is
228 if ((*pathp
) != '/') {
231 /* root node: special case. fpsize accounts for path
232 * plus terminating zero. root node only has '/', so
233 * fpsize should be 2, but we want to avoid the first
234 * level nodes to have two '/' so we use fpsize 1 here
239 /* account for '/' and path size minus terminal 0
247 np
= unflatten_dt_alloc(&mem
, sizeof(struct device_node
) + allocl
,
248 __alignof__(struct device_node
));
250 memset(np
, 0, sizeof(*np
));
251 np
->full_name
= ((char *)np
) + sizeof(struct device_node
);
253 char *fn
= np
->full_name
;
254 /* rebuild full path for new format */
255 if (dad
&& dad
->parent
) {
256 strcpy(fn
, dad
->full_name
);
258 if ((strlen(fn
) + l
+ 1) != allocl
) {
259 pr_debug("%s: p: %d, l: %d, a: %d\n",
260 pathp
, (int)strlen(fn
),
267 memcpy(fn
, pathp
, l
);
269 memcpy(np
->full_name
, pathp
, l
);
270 prev_pp
= &np
->properties
;
272 *allnextpp
= &np
->allnext
;
275 /* we temporarily use the next field as `last_child'*/
276 if (dad
->next
== NULL
)
279 dad
->next
->sibling
= np
;
282 kref_init(&np
->kref
);
288 tag
= be32_to_cpup((__be32
*)(*p
));
289 if (tag
== OF_DT_NOP
) {
293 if (tag
!= OF_DT_PROP
)
296 sz
= be32_to_cpup((__be32
*)(*p
));
297 noff
= be32_to_cpup((__be32
*)((*p
) + 4));
299 if (be32_to_cpu(initial_boot_params
->version
) < 0x10)
300 *p
= _ALIGN(*p
, sz
>= 8 ? 8 : 4);
302 pname
= find_flat_dt_string(noff
);
304 pr_info("Can't find property name in list !\n");
307 if (strcmp(pname
, "name") == 0)
309 l
= strlen(pname
) + 1;
310 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
311 __alignof__(struct property
));
313 if (strcmp(pname
, "linux,phandle") == 0) {
314 if (np
->phandle
== 0)
315 np
->phandle
= *((u32
*)*p
);
317 if (strcmp(pname
, "ibm,phandle") == 0)
318 np
->phandle
= *((u32
*)*p
);
321 pp
->value
= (void *)*p
;
325 *p
= _ALIGN((*p
) + sz
, 4);
327 /* with version 0x10 we may not have the name property, recreate
328 * it here from the unit name if absent
331 char *p1
= pathp
, *ps
= pathp
, *pa
= NULL
;
344 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
) + sz
,
345 __alignof__(struct property
));
352 memcpy(pp
->value
, ps
, sz
- 1);
353 ((char *)pp
->value
)[sz
- 1] = 0;
354 pr_debug("fixed up name for %s -> %s\n", pathp
,
360 np
->name
= of_get_property(np
, "name", NULL
);
361 np
->type
= of_get_property(np
, "device_type", NULL
);
368 while (tag
== OF_DT_BEGIN_NODE
) {
369 mem
= unflatten_dt_node(mem
, p
, np
, allnextpp
, fpsize
);
370 tag
= be32_to_cpup((__be32
*)(*p
));
372 if (tag
!= OF_DT_END_NODE
) {
373 pr_err("Weird tag at end of node: %x\n", tag
);
380 #ifdef CONFIG_BLK_DEV_INITRD
382 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
383 * @node: reference to node containing initrd location ('chosen')
385 void __init
early_init_dt_check_for_initrd(unsigned long node
)
387 unsigned long start
, end
, len
;
390 pr_debug("Looking for initrd properties... ");
392 prop
= of_get_flat_dt_prop(node
, "linux,initrd-start", &len
);
395 start
= of_read_ulong(prop
, len
/4);
397 prop
= of_get_flat_dt_prop(node
, "linux,initrd-end", &len
);
400 end
= of_read_ulong(prop
, len
/4);
402 early_init_dt_setup_initrd_arch(start
, end
);
403 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n", start
, end
);
406 inline void early_init_dt_check_for_initrd(unsigned long node
)
409 #endif /* CONFIG_BLK_DEV_INITRD */
412 * early_init_dt_scan_root - fetch the top level address and size cells
414 int __init
early_init_dt_scan_root(unsigned long node
, const char *uname
,
415 int depth
, void *data
)
422 dt_root_size_cells
= OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
423 dt_root_addr_cells
= OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
425 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
427 dt_root_size_cells
= be32_to_cpup(prop
);
428 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells
);
430 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
432 dt_root_addr_cells
= be32_to_cpup(prop
);
433 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells
);
439 u64 __init
dt_mem_next_cell(int s
, __be32
**cellp
)
444 return of_read_number(p
, s
);
448 * early_init_dt_scan_memory - Look for an parse memory nodes
450 int __init
early_init_dt_scan_memory(unsigned long node
, const char *uname
,
451 int depth
, void *data
)
453 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
457 /* We are scanning "memory" nodes only */
460 * The longtrail doesn't have a device_type on the
461 * /memory node, so look for the node called /memory@0.
463 if (depth
!= 1 || strcmp(uname
, "memory@0") != 0)
465 } else if (strcmp(type
, "memory") != 0)
468 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
470 reg
= of_get_flat_dt_prop(node
, "reg", &l
);
474 endp
= reg
+ (l
/ sizeof(__be32
));
476 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
477 uname
, l
, reg
[0], reg
[1], reg
[2], reg
[3]);
479 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
482 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
483 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
487 pr_debug(" - %llx , %llx\n", (unsigned long long)base
,
488 (unsigned long long)size
);
490 early_init_dt_add_memory_arch(base
, size
);
496 int __init
early_init_dt_scan_chosen(unsigned long node
, const char *uname
,
497 int depth
, void *data
)
502 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
505 (strcmp(uname
, "chosen") != 0 && strcmp(uname
, "chosen@0") != 0))
508 early_init_dt_check_for_initrd(node
);
510 /* Retreive command line */
511 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
512 if (p
!= NULL
&& l
> 0)
513 strlcpy(cmd_line
, p
, min((int)l
, COMMAND_LINE_SIZE
));
515 #ifdef CONFIG_CMDLINE
516 #ifndef CONFIG_CMDLINE_FORCE
517 if (p
== NULL
|| l
== 0 || (l
== 1 && (*p
) == 0))
519 strlcpy(cmd_line
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
520 #endif /* CONFIG_CMDLINE */
522 early_init_dt_scan_chosen_arch(node
);
524 pr_debug("Command line is: %s\n", cmd_line
);
531 * unflatten_device_tree - create tree of device_nodes from flat blob
533 * unflattens the device-tree passed by the firmware, creating the
534 * tree of struct device_node. It also fills the "name" and "type"
535 * pointers of the nodes so the normal device-tree walking functions
538 void __init
unflatten_device_tree(void)
540 unsigned long start
, mem
, size
;
541 struct device_node
**allnextp
= &allnodes
;
543 pr_debug(" -> unflatten_device_tree()\n");
545 /* First pass, scan for size */
546 start
= ((unsigned long)initial_boot_params
) +
547 be32_to_cpu(initial_boot_params
->off_dt_struct
);
548 size
= unflatten_dt_node(0, &start
, NULL
, NULL
, 0);
549 size
= (size
| 3) + 1;
551 pr_debug(" size is %lx, allocating...\n", size
);
553 /* Allocate memory for the expanded device tree */
554 mem
= lmb_alloc(size
+ 4, __alignof__(struct device_node
));
555 mem
= (unsigned long) __va(mem
);
557 ((__be32
*)mem
)[size
/ 4] = cpu_to_be32(0xdeadbeef);
559 pr_debug(" unflattening %lx...\n", mem
);
561 /* Second pass, do actual unflattening */
562 start
= ((unsigned long)initial_boot_params
) +
563 be32_to_cpu(initial_boot_params
->off_dt_struct
);
564 unflatten_dt_node(mem
, &start
, NULL
, &allnextp
, 0);
565 if (be32_to_cpup((__be32
*)start
) != OF_DT_END
)
566 pr_warning("Weird tag at end of tree: %08x\n", *((u32
*)start
));
567 if (be32_to_cpu(((__be32
*)mem
)[size
/ 4]) != 0xdeadbeef)
568 pr_warning("End of tree marker overwritten: %08x\n",
569 be32_to_cpu(((__be32
*)mem
)[size
/ 4]));
572 /* Get pointer to OF "/chosen" node for use everywhere */
573 of_chosen
= of_find_node_by_path("/chosen");
574 if (of_chosen
== NULL
)
575 of_chosen
= of_find_node_by_path("/chosen@0");
577 pr_debug(" <- unflatten_device_tree()\n");