2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/threads.h>
21 #include <linux/spinlock.h>
22 #include <linux/types.h>
23 #include <linux/pci.h>
24 #include <linux/stringify.h>
25 #include <linux/delay.h>
26 #include <linux/initrd.h>
27 #include <linux/bitops.h>
28 #include <linux/module.h>
29 #include <linux/kexec.h>
30 #include <linux/debugfs.h>
31 #include <linux/irq.h>
32 #include <linux/lmb.h>
36 #include <asm/processor.h>
39 #include <asm/system.h>
41 #include <asm/pgtable.h>
42 #include <asm/sections.h>
43 #include <asm/pci-bridge.h>
45 static int __initdata dt_root_addr_cells;
46 static int __initdata dt_root_size_cells;
50 /* export that to outside world */
51 struct device_node *of_chosen;
53 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
58 *mem = _ALIGN(*mem, align);
65 static unsigned long __init unflatten_dt_node(unsigned long mem,
67 struct device_node *dad,
68 struct device_node ***allnextpp,
71 struct device_node *np;
72 struct property *pp, **prev_pp = NULL;
75 unsigned int l, allocl;
80 if (tag != OF_DT_BEGIN_NODE) {
81 printk("Weird tag at start of node: %x\n", tag);
86 l = allocl = strlen(pathp) + 1;
87 *p = _ALIGN(*p + l, 4);
89 /* version 0x10 has a more compact unit name here instead of the full
90 * path. we accumulate the full path size using "fpsize", we'll rebuild
91 * it later. We detect this because the first character of the name is
94 if ((*pathp) != '/') {
97 /* root node: special case. fpsize accounts for path
98 * plus terminating zero. root node only has '/', so
99 * fpsize should be 2, but we want to avoid the first
100 * level nodes to have two '/' so we use fpsize 1 here
105 /* account for '/' and path size minus terminal 0
113 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
114 __alignof__(struct device_node));
116 memset(np, 0, sizeof(*np));
117 np->full_name = ((char *)np) + sizeof(struct device_node);
119 char *p2 = np->full_name;
120 /* rebuild full path for new format */
121 if (dad && dad->parent) {
122 strcpy(p2, dad->full_name);
124 if ((strlen(p2) + l + 1) != allocl) {
125 pr_debug("%s: p: %d, l: %d, a: %d\n",
126 pathp, (int)strlen(p2),
133 memcpy(p2, pathp, l);
135 memcpy(np->full_name, pathp, l);
136 prev_pp = &np->properties;
138 *allnextpp = &np->allnext;
141 /* we temporarily use the next field as `last_child'*/
142 if (dad->next == NULL)
145 dad->next->sibling = np;
148 kref_init(&np->kref);
154 tag = *((u32 *)(*p));
155 if (tag == OF_DT_NOP) {
159 if (tag != OF_DT_PROP)
163 noff = *((u32 *)((*p) + 4));
165 if (initial_boot_params->version < 0x10)
166 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
168 pname = find_flat_dt_string(noff);
171 "Can't find property name in list !\n");
174 if (strcmp(pname, "name") == 0)
176 l = strlen(pname) + 1;
177 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
178 __alignof__(struct property));
180 if (strcmp(pname, "linux,phandle") == 0) {
181 np->node = *((u32 *)*p);
182 if (np->linux_phandle == 0)
183 np->linux_phandle = np->node;
185 if (strcmp(pname, "ibm,phandle") == 0)
186 np->linux_phandle = *((u32 *)*p);
189 pp->value = (void *)*p;
193 *p = _ALIGN((*p) + sz, 4);
195 /* with version 0x10 we may not have the name property, recreate
196 * it here from the unit name if absent
199 char *p1 = pathp, *ps = pathp, *pa = NULL;
212 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
213 __alignof__(struct property));
220 memcpy(pp->value, ps, sz - 1);
221 ((char *)pp->value)[sz - 1] = 0;
222 pr_debug("fixed up name for %s -> %s\n", pathp,
228 np->name = of_get_property(np, "name", NULL);
229 np->type = of_get_property(np, "device_type", NULL);
236 while (tag == OF_DT_BEGIN_NODE) {
237 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
238 tag = *((u32 *)(*p));
240 if (tag != OF_DT_END_NODE) {
241 printk(KERN_INFO "Weird tag at end of node: %x\n", tag);
249 * unflattens the device-tree passed by the firmware, creating the
250 * tree of struct device_node. It also fills the "name" and "type"
251 * pointers of the nodes so the normal device-tree walking functions
252 * can be used (this used to be done by finish_device_tree)
254 void __init unflatten_device_tree(void)
256 unsigned long start, mem, size;
257 struct device_node **allnextp = &allnodes;
259 pr_debug(" -> unflatten_device_tree()\n");
261 /* First pass, scan for size */
262 start = ((unsigned long)initial_boot_params) +
263 initial_boot_params->off_dt_struct;
264 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
265 size = (size | 3) + 1;
267 pr_debug(" size is %lx, allocating...\n", size);
269 /* Allocate memory for the expanded device tree */
270 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
271 mem = (unsigned long) __va(mem);
273 ((u32 *)mem)[size / 4] = 0xdeadbeef;
275 pr_debug(" unflattening %lx...\n", mem);
277 /* Second pass, do actual unflattening */
278 start = ((unsigned long)initial_boot_params) +
279 initial_boot_params->off_dt_struct;
280 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
281 if (*((u32 *)start) != OF_DT_END)
282 printk(KERN_WARNING "Weird tag at end of tree: %08x\n",
284 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
285 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
286 ((u32 *)mem)[size / 4]);
289 /* Get pointer to OF "/chosen" node for use everywhere */
290 of_chosen = of_find_node_by_path("/chosen");
291 if (of_chosen == NULL)
292 of_chosen = of_find_node_by_path("/chosen@0");
294 pr_debug(" <- unflatten_device_tree()\n");
297 #define early_init_dt_scan_drconf_memory(node) 0
299 static int __init early_init_dt_scan_cpus(unsigned long node,
300 const char *uname, int depth,
303 static int logical_cpuid;
304 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
309 /* We are scanning "cpu" nodes only */
310 if (type == NULL || strcmp(type, "cpu") != 0)
313 /* Get physical cpuid */
314 intserv = of_get_flat_dt_prop(node, "reg", NULL);
318 * Now see if any of these threads match our boot cpu.
319 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
321 for (i = 0; i < nthreads; i++) {
323 * version 2 of the kexec param format adds the phys cpuid of
326 if (initial_boot_params && initial_boot_params->version >= 2) {
328 initial_boot_params->boot_cpuid_phys) {
334 * Check if it's the boot-cpu, set it's hw index now,
335 * unfortunately this format did not support booting
336 * off secondary threads.
338 if (of_get_flat_dt_prop(node,
339 "linux,boot-cpu", NULL) != NULL) {
346 /* logical cpu id is always 0 on UP kernels */
352 pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
354 boot_cpuid = logical_cpuid;
360 #ifdef CONFIG_BLK_DEV_INITRD
361 static void __init early_init_dt_check_for_initrd(unsigned long node)
366 pr_debug("Looking for initrd properties... ");
368 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
370 initrd_start = (unsigned long)
371 __va((u32)of_read_ulong(prop, l/4));
373 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
375 initrd_end = (unsigned long)
376 __va((u32)of_read_ulong(prop, 1/4));
377 initrd_below_start_ok = 1;
383 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
384 initrd_start, initrd_end);
387 static inline void early_init_dt_check_for_initrd(unsigned long node)
390 #endif /* CONFIG_BLK_DEV_INITRD */
392 static int __init early_init_dt_scan_chosen(unsigned long node,
393 const char *uname, int depth, void *data)
398 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
401 (strcmp(uname, "chosen") != 0 &&
402 strcmp(uname, "chosen@0") != 0))
406 lprop = (u64 *)of_get_flat_dt_prop(node,
407 "linux,crashkernel-base", NULL);
409 crashk_res.start = *lprop;
411 lprop = (u64 *)of_get_flat_dt_prop(node,
412 "linux,crashkernel-size", NULL);
414 crashk_res.end = crashk_res.start + *lprop - 1;
417 early_init_dt_check_for_initrd(node);
419 /* Retreive command line */
420 p = of_get_flat_dt_prop(node, "bootargs", &l);
421 if (p != NULL && l > 0)
422 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
424 #ifdef CONFIG_CMDLINE
425 #ifndef CONFIG_CMDLINE_FORCE
426 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
428 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
429 #endif /* CONFIG_CMDLINE */
431 pr_debug("Command line is: %s\n", cmd_line);
437 static int __init early_init_dt_scan_root(unsigned long node,
438 const char *uname, int depth, void *data)
445 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
446 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
447 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
449 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
450 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
451 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
457 static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
462 return of_read_number(p, s);
465 static int __init early_init_dt_scan_memory(unsigned long node,
466 const char *uname, int depth, void *data)
468 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
472 /* Look for the ibm,dynamic-reconfiguration-memory node */
474 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
475 return early_init_dt_scan_drconf_memory(node);
477 /* We are scanning "memory" nodes only */
480 * The longtrail doesn't have a device_type on the
481 * /memory node, so look for the node called /memory@0.
483 if (depth != 1 || strcmp(uname, "memory@0") != 0)
485 } else if (strcmp(type, "memory") != 0)
488 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
490 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
494 endp = reg + (l / sizeof(cell_t));
496 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
497 uname, l, reg[0], reg[1], reg[2], reg[3]);
499 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
502 base = dt_mem_next_cell(dt_root_addr_cells, ®);
503 size = dt_mem_next_cell(dt_root_size_cells, ®);
507 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
508 (unsigned long long)size);
515 #ifdef CONFIG_PHYP_DUMP
517 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
519 * Function to find the largest size we need to reserve
520 * during early boot process.
522 * It either looks for boot param and returns that OR
523 * returns larger of 256 or 5% rounded down to multiples of 256MB.
526 static inline unsigned long phyp_dump_calculate_reserve_size(void)
530 if (phyp_dump_info->reserve_bootvar)
531 return phyp_dump_info->reserve_bootvar;
533 /* divide by 20 to get 5% of value */
534 tmp = lmb_end_of_DRAM();
537 /* round it down in multiples of 256 */
538 tmp = tmp & ~0x0FFFFFFFUL;
540 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
544 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
546 * This routine may reserve memory regions in the kernel only
547 * if the system is supported and a dump was taken in last
548 * boot instance or if the hardware is supported and the
549 * scratch area needs to be setup. In other instances it returns
550 * without reserving anything. The memory in case of dump being
551 * active is freed when the dump is collected (by userland tools).
553 static void __init phyp_dump_reserve_mem(void)
555 unsigned long base, size;
556 unsigned long variable_reserve_size;
558 if (!phyp_dump_info->phyp_dump_configured) {
559 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
563 if (!phyp_dump_info->phyp_dump_at_boot) {
564 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
568 variable_reserve_size = phyp_dump_calculate_reserve_size();
570 if (phyp_dump_info->phyp_dump_is_active) {
571 /* Reserve *everything* above RMR.Area freed by userland tools*/
572 base = variable_reserve_size;
573 size = lmb_end_of_DRAM() - base;
575 /* XXX crashed_ram_end is wrong, since it may be beyond
576 * the memory_limit, it will need to be adjusted. */
577 lmb_reserve(base, size);
579 phyp_dump_info->init_reserve_start = base;
580 phyp_dump_info->init_reserve_size = size;
582 size = phyp_dump_info->cpu_state_size +
583 phyp_dump_info->hpte_region_size +
584 variable_reserve_size;
585 base = lmb_end_of_DRAM() - size;
586 lmb_reserve(base, size);
587 phyp_dump_info->init_reserve_start = base;
588 phyp_dump_info->init_reserve_size = size;
592 static inline void __init phyp_dump_reserve_mem(void) {}
593 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
595 #ifdef CONFIG_EARLY_PRINTK
596 /* MS this is Microblaze specifig function */
597 static int __init early_init_dt_scan_serial(unsigned long node,
598 const char *uname, int depth, void *data)
604 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
606 /* find all serial nodes */
607 if (strncmp(uname, "serial", 6) != 0)
610 early_init_dt_check_for_initrd(node);
612 /* find compatible node with uartlite */
613 p = of_get_flat_dt_prop(node, "compatible", &l);
614 if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
615 (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
618 addr = of_get_flat_dt_prop(node, "reg", &l);
619 return *addr; /* return address */
622 /* this function is looking for early uartlite console - Microblaze specific */
623 int __init early_uartlite_console(void)
625 return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
629 void __init early_init_devtree(void *params)
631 pr_debug(" -> early_init_devtree(%p)\n", params);
633 /* Setup flat device-tree pointer */
634 initial_boot_params = params;
636 #ifdef CONFIG_PHYP_DUMP
637 /* scan tree to see if dump occured during last boot */
638 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
641 /* Retrieve various informations from the /chosen node of the
642 * device-tree, including the platform type, initrd location and
643 * size, TCE reserve, and more ...
645 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
647 /* Scan memory nodes and rebuild LMBs */
649 of_scan_flat_dt(early_init_dt_scan_root, NULL);
650 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
652 /* Save command line for /proc/cmdline and then parse parameters */
653 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
658 pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
660 pr_debug("Scanning CPUs ...\n");
662 /* Retreive CPU related informations from the flat tree
663 * (altivec support, boot CPU ID, ...)
665 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
667 pr_debug(" <- early_init_devtree()\n");
671 * Indicates whether the root node has a given value in its
672 * compatible property.
674 int machine_is_compatible(const char *compat)
676 struct device_node *root;
679 root = of_find_node_by_path("/");
681 rc = of_device_is_compatible(root, compat);
686 EXPORT_SYMBOL(machine_is_compatible);
690 * New implementation of the OF "find" APIs, return a refcounted
691 * object, call of_node_put() when done. The device tree and list
692 * are protected by a rw_lock.
694 * Note that property management will need some locking as well,
695 * this isn't dealt with yet.
700 * of_find_node_by_phandle - Find a node given a phandle
701 * @handle: phandle of the node to find
703 * Returns a node pointer with refcount incremented, use
704 * of_node_put() on it when done.
706 struct device_node *of_find_node_by_phandle(phandle handle)
708 struct device_node *np;
710 read_lock(&devtree_lock);
711 for (np = allnodes; np != NULL; np = np->allnext)
712 if (np->linux_phandle == handle)
715 read_unlock(&devtree_lock);
718 EXPORT_SYMBOL(of_find_node_by_phandle);
721 * of_node_get - Increment refcount of a node
722 * @node: Node to inc refcount, NULL is supported to
723 * simplify writing of callers
727 struct device_node *of_node_get(struct device_node *node)
730 kref_get(&node->kref);
733 EXPORT_SYMBOL(of_node_get);
735 static inline struct device_node *kref_to_device_node(struct kref *kref)
737 return container_of(kref, struct device_node, kref);
741 * of_node_release - release a dynamically allocated node
742 * @kref: kref element of the node to be released
744 * In of_node_put() this function is passed to kref_put()
747 static void of_node_release(struct kref *kref)
749 struct device_node *node = kref_to_device_node(kref);
750 struct property *prop = node->properties;
752 /* We should never be releasing nodes that haven't been detached. */
753 if (!of_node_check_flag(node, OF_DETACHED)) {
754 printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
757 kref_init(&node->kref);
761 if (!of_node_check_flag(node, OF_DYNAMIC))
765 struct property *next = prop->next;
772 prop = node->deadprops;
773 node->deadprops = NULL;
776 kfree(node->full_name);
782 * of_node_put - Decrement refcount of a node
783 * @node: Node to dec refcount, NULL is supported to
784 * simplify writing of callers
787 void of_node_put(struct device_node *node)
790 kref_put(&node->kref, of_node_release);
792 EXPORT_SYMBOL(of_node_put);
795 * Plug a device node into the tree and global list.
797 void of_attach_node(struct device_node *np)
801 write_lock_irqsave(&devtree_lock, flags);
802 np->sibling = np->parent->child;
803 np->allnext = allnodes;
804 np->parent->child = np;
806 write_unlock_irqrestore(&devtree_lock, flags);
810 * "Unplug" a node from the device tree. The caller must hold
811 * a reference to the node. The memory associated with the node
812 * is not freed until its refcount goes to zero.
814 void of_detach_node(struct device_node *np)
816 struct device_node *parent;
819 write_lock_irqsave(&devtree_lock, flags);
826 allnodes = np->allnext;
828 struct device_node *prev;
829 for (prev = allnodes;
831 prev = prev->allnext)
833 prev->allnext = np->allnext;
836 if (parent->child == np)
837 parent->child = np->sibling;
839 struct device_node *prevsib;
840 for (prevsib = np->parent->child;
841 prevsib->sibling != np;
842 prevsib = prevsib->sibling)
844 prevsib->sibling = np->sibling;
847 of_node_set_flag(np, OF_DETACHED);
850 write_unlock_irqrestore(&devtree_lock, flags);
854 * Add a property to a node
856 int prom_add_property(struct device_node *np, struct property *prop)
858 struct property **next;
862 write_lock_irqsave(&devtree_lock, flags);
863 next = &np->properties;
865 if (strcmp(prop->name, (*next)->name) == 0) {
866 /* duplicate ! don't insert it */
867 write_unlock_irqrestore(&devtree_lock, flags);
870 next = &(*next)->next;
873 write_unlock_irqrestore(&devtree_lock, flags);
875 #ifdef CONFIG_PROC_DEVICETREE
876 /* try to add to proc as well if it was initialized */
878 proc_device_tree_add_prop(np->pde, prop);
879 #endif /* CONFIG_PROC_DEVICETREE */
885 * Remove a property from a node. Note that we don't actually
886 * remove it, since we have given out who-knows-how-many pointers
887 * to the data using get-property. Instead we just move the property
888 * to the "dead properties" list, so it won't be found any more.
890 int prom_remove_property(struct device_node *np, struct property *prop)
892 struct property **next;
896 write_lock_irqsave(&devtree_lock, flags);
897 next = &np->properties;
902 prop->next = np->deadprops;
903 np->deadprops = prop;
907 next = &(*next)->next;
909 write_unlock_irqrestore(&devtree_lock, flags);
914 #ifdef CONFIG_PROC_DEVICETREE
915 /* try to remove the proc node as well */
917 proc_device_tree_remove_prop(np->pde, prop);
918 #endif /* CONFIG_PROC_DEVICETREE */
924 * Update a property in a node. Note that we don't actually
925 * remove it, since we have given out who-knows-how-many pointers
926 * to the data using get-property. Instead we just move the property
927 * to the "dead properties" list, and add the new property to the
930 int prom_update_property(struct device_node *np,
931 struct property *newprop,
932 struct property *oldprop)
934 struct property **next;
938 write_lock_irqsave(&devtree_lock, flags);
939 next = &np->properties;
941 if (*next == oldprop) {
943 newprop->next = oldprop->next;
945 oldprop->next = np->deadprops;
946 np->deadprops = oldprop;
950 next = &(*next)->next;
952 write_unlock_irqrestore(&devtree_lock, flags);
957 #ifdef CONFIG_PROC_DEVICETREE
958 /* try to add to proc as well if it was initialized */
960 proc_device_tree_update_prop(np->pde, newprop, oldprop);
961 #endif /* CONFIG_PROC_DEVICETREE */
966 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
967 static struct debugfs_blob_wrapper flat_dt_blob;
969 static int __init export_flat_device_tree(void)
973 flat_dt_blob.data = initial_boot_params;
974 flat_dt_blob.size = initial_boot_params->totalsize;
976 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
977 of_debugfs_root, &flat_dt_blob);
983 device_initcall(export_flat_device_tree);