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 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 initial_boot_params->off_dt_struct;
54 u32 tag = *((u32 *)p);
58 if (tag == OF_DT_END_NODE) {
66 if (tag == OF_DT_PROP) {
69 if (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 initial_boot_params->off_dt_struct;
106 while (*((u32 *)p) == OF_DT_NOP)
108 BUG_ON(*((u32 *)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 = *((u32 *)p);
130 if (tag == OF_DT_NOP)
132 if (tag != OF_DT_PROP)
136 noff = *((u32 *)(p + 4));
138 if (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 = *((u32 *)(*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 = *((u32 *)(*p));
289 if (tag == OF_DT_NOP) {
293 if (tag != OF_DT_PROP)
297 noff = *((u32 *)((*p) + 4));
299 if (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 = *((u32 *)(*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 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
423 dt_root_size_cells = prop ? *prop : OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
424 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
426 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
427 dt_root_addr_cells = prop ? *prop : OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
428 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
434 u64 __init dt_mem_next_cell(int s, u32 **cellp)
439 return of_read_number(p, s);
443 * early_init_dt_scan_memory - Look for an parse memory nodes
445 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
446 int depth, void *data)
448 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
452 /* We are scanning "memory" nodes only */
455 * The longtrail doesn't have a device_type on the
456 * /memory node, so look for the node called /memory@0.
458 if (depth != 1 || strcmp(uname, "memory@0") != 0)
460 } else if (strcmp(type, "memory") != 0)
463 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
465 reg = of_get_flat_dt_prop(node, "reg", &l);
469 endp = reg + (l / sizeof(__be32));
471 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
472 uname, l, reg[0], reg[1], reg[2], reg[3]);
474 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
477 base = dt_mem_next_cell(dt_root_addr_cells, ®);
478 size = dt_mem_next_cell(dt_root_size_cells, ®);
482 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
483 (unsigned long long)size);
485 early_init_dt_add_memory_arch(base, size);
491 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
492 int depth, void *data)
497 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
500 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
503 early_init_dt_check_for_initrd(node);
505 /* Retreive command line */
506 p = of_get_flat_dt_prop(node, "bootargs", &l);
507 if (p != NULL && l > 0)
508 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
510 #ifdef CONFIG_CMDLINE
511 #ifndef CONFIG_CMDLINE_FORCE
512 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
514 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
515 #endif /* CONFIG_CMDLINE */
517 early_init_dt_scan_chosen_arch(node);
519 pr_debug("Command line is: %s\n", cmd_line);
526 * unflatten_device_tree - create tree of device_nodes from flat blob
528 * unflattens the device-tree passed by the firmware, creating the
529 * tree of struct device_node. It also fills the "name" and "type"
530 * pointers of the nodes so the normal device-tree walking functions
533 void __init unflatten_device_tree(void)
535 unsigned long start, mem, size;
536 struct device_node **allnextp = &allnodes;
538 pr_debug(" -> unflatten_device_tree()\n");
540 /* First pass, scan for size */
541 start = ((unsigned long)initial_boot_params) +
542 initial_boot_params->off_dt_struct;
543 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
544 size = (size | 3) + 1;
546 pr_debug(" size is %lx, allocating...\n", size);
548 /* Allocate memory for the expanded device tree */
549 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
550 mem = (unsigned long) __va(mem);
552 ((u32 *)mem)[size / 4] = 0xdeadbeef;
554 pr_debug(" unflattening %lx...\n", mem);
556 /* Second pass, do actual unflattening */
557 start = ((unsigned long)initial_boot_params) +
558 initial_boot_params->off_dt_struct;
559 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
560 if (*((u32 *)start) != OF_DT_END)
561 pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
562 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
563 pr_warning("End of tree marker overwritten: %08x\n",
564 ((u32 *)mem)[size / 4]);
567 /* Get pointer to OF "/chosen" node for use everywhere */
568 of_chosen = of_find_node_by_path("/chosen");
569 if (of_chosen == NULL)
570 of_chosen = of_find_node_by_path("/chosen@0");
572 pr_debug(" <- unflatten_device_tree()\n");