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 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
56 unsigned long cplen, l;
58 cp = of_get_flat_dt_prop(node, "compatible", &cplen);
62 if (strncasecmp(cp, compat, strlen(compat)) == 0)
72 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
77 *mem = _ALIGN(*mem, align);
84 static unsigned long __init unflatten_dt_node(unsigned long mem,
86 struct device_node *dad,
87 struct device_node ***allnextpp,
90 struct device_node *np;
91 struct property *pp, **prev_pp = NULL;
94 unsigned int l, allocl;
99 if (tag != OF_DT_BEGIN_NODE) {
100 printk("Weird tag at start of node: %x\n", tag);
105 l = allocl = strlen(pathp) + 1;
106 *p = _ALIGN(*p + l, 4);
108 /* version 0x10 has a more compact unit name here instead of the full
109 * path. we accumulate the full path size using "fpsize", we'll rebuild
110 * it later. We detect this because the first character of the name is
113 if ((*pathp) != '/') {
116 /* root node: special case. fpsize accounts for path
117 * plus terminating zero. root node only has '/', so
118 * fpsize should be 2, but we want to avoid the first
119 * level nodes to have two '/' so we use fpsize 1 here
124 /* account for '/' and path size minus terminal 0
132 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
133 __alignof__(struct device_node));
135 memset(np, 0, sizeof(*np));
136 np->full_name = ((char *)np) + sizeof(struct device_node);
138 char *p2 = np->full_name;
139 /* rebuild full path for new format */
140 if (dad && dad->parent) {
141 strcpy(p2, dad->full_name);
143 if ((strlen(p2) + l + 1) != allocl) {
144 pr_debug("%s: p: %d, l: %d, a: %d\n",
145 pathp, (int)strlen(p2),
152 memcpy(p2, pathp, l);
154 memcpy(np->full_name, pathp, l);
155 prev_pp = &np->properties;
157 *allnextpp = &np->allnext;
160 /* we temporarily use the next field as `last_child'*/
161 if (dad->next == NULL)
164 dad->next->sibling = np;
167 kref_init(&np->kref);
173 tag = *((u32 *)(*p));
174 if (tag == OF_DT_NOP) {
178 if (tag != OF_DT_PROP)
182 noff = *((u32 *)((*p) + 4));
184 if (initial_boot_params->version < 0x10)
185 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
187 pname = find_flat_dt_string(noff);
190 "Can't find property name in list !\n");
193 if (strcmp(pname, "name") == 0)
195 l = strlen(pname) + 1;
196 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
197 __alignof__(struct property));
199 if (strcmp(pname, "linux,phandle") == 0) {
200 np->node = *((u32 *)*p);
201 if (np->linux_phandle == 0)
202 np->linux_phandle = np->node;
204 if (strcmp(pname, "ibm,phandle") == 0)
205 np->linux_phandle = *((u32 *)*p);
208 pp->value = (void *)*p;
212 *p = _ALIGN((*p) + sz, 4);
214 /* with version 0x10 we may not have the name property, recreate
215 * it here from the unit name if absent
218 char *p1 = pathp, *ps = pathp, *pa = NULL;
231 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
232 __alignof__(struct property));
239 memcpy(pp->value, ps, sz - 1);
240 ((char *)pp->value)[sz - 1] = 0;
241 pr_debug("fixed up name for %s -> %s\n", pathp,
247 np->name = of_get_property(np, "name", NULL);
248 np->type = of_get_property(np, "device_type", NULL);
255 while (tag == OF_DT_BEGIN_NODE) {
256 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
257 tag = *((u32 *)(*p));
259 if (tag != OF_DT_END_NODE) {
260 printk(KERN_INFO "Weird tag at end of node: %x\n", tag);
268 * unflattens the device-tree passed by the firmware, creating the
269 * tree of struct device_node. It also fills the "name" and "type"
270 * pointers of the nodes so the normal device-tree walking functions
271 * can be used (this used to be done by finish_device_tree)
273 void __init unflatten_device_tree(void)
275 unsigned long start, mem, size;
276 struct device_node **allnextp = &allnodes;
278 pr_debug(" -> unflatten_device_tree()\n");
280 /* First pass, scan for size */
281 start = ((unsigned long)initial_boot_params) +
282 initial_boot_params->off_dt_struct;
283 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
284 size = (size | 3) + 1;
286 pr_debug(" size is %lx, allocating...\n", size);
288 /* Allocate memory for the expanded device tree */
289 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
290 mem = (unsigned long) __va(mem);
292 ((u32 *)mem)[size / 4] = 0xdeadbeef;
294 pr_debug(" unflattening %lx...\n", mem);
296 /* Second pass, do actual unflattening */
297 start = ((unsigned long)initial_boot_params) +
298 initial_boot_params->off_dt_struct;
299 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
300 if (*((u32 *)start) != OF_DT_END)
301 printk(KERN_WARNING "Weird tag at end of tree: %08x\n",
303 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
304 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
305 ((u32 *)mem)[size / 4]);
308 /* Get pointer to OF "/chosen" node for use everywhere */
309 of_chosen = of_find_node_by_path("/chosen");
310 if (of_chosen == NULL)
311 of_chosen = of_find_node_by_path("/chosen@0");
313 pr_debug(" <- unflatten_device_tree()\n");
316 #define early_init_dt_scan_drconf_memory(node) 0
318 static int __init early_init_dt_scan_cpus(unsigned long node,
319 const char *uname, int depth,
322 static int logical_cpuid;
323 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
328 /* We are scanning "cpu" nodes only */
329 if (type == NULL || strcmp(type, "cpu") != 0)
332 /* Get physical cpuid */
333 intserv = of_get_flat_dt_prop(node, "reg", NULL);
337 * Now see if any of these threads match our boot cpu.
338 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
340 for (i = 0; i < nthreads; i++) {
342 * version 2 of the kexec param format adds the phys cpuid of
345 if (initial_boot_params && initial_boot_params->version >= 2) {
347 initial_boot_params->boot_cpuid_phys) {
353 * Check if it's the boot-cpu, set it's hw index now,
354 * unfortunately this format did not support booting
355 * off secondary threads.
357 if (of_get_flat_dt_prop(node,
358 "linux,boot-cpu", NULL) != NULL) {
365 /* logical cpu id is always 0 on UP kernels */
371 pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
373 boot_cpuid = logical_cpuid;
379 #ifdef CONFIG_BLK_DEV_INITRD
380 static void __init early_init_dt_check_for_initrd(unsigned long node)
385 pr_debug("Looking for initrd properties... ");
387 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
389 initrd_start = (unsigned long)
390 __va((u32)of_read_ulong(prop, l/4));
392 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
394 initrd_end = (unsigned long)
395 __va((u32)of_read_ulong(prop, 1/4));
396 initrd_below_start_ok = 1;
402 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
403 initrd_start, initrd_end);
406 static inline void early_init_dt_check_for_initrd(unsigned long node)
409 #endif /* CONFIG_BLK_DEV_INITRD */
411 static int __init early_init_dt_scan_chosen(unsigned long node,
412 const char *uname, int depth, void *data)
417 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
420 (strcmp(uname, "chosen") != 0 &&
421 strcmp(uname, "chosen@0") != 0))
425 lprop = (u64 *)of_get_flat_dt_prop(node,
426 "linux,crashkernel-base", NULL);
428 crashk_res.start = *lprop;
430 lprop = (u64 *)of_get_flat_dt_prop(node,
431 "linux,crashkernel-size", NULL);
433 crashk_res.end = crashk_res.start + *lprop - 1;
436 early_init_dt_check_for_initrd(node);
438 /* Retreive command line */
439 p = of_get_flat_dt_prop(node, "bootargs", &l);
440 if (p != NULL && l > 0)
441 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
443 #ifdef CONFIG_CMDLINE
444 #ifndef CONFIG_CMDLINE_FORCE
445 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
447 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
448 #endif /* CONFIG_CMDLINE */
450 pr_debug("Command line is: %s\n", cmd_line);
456 static int __init early_init_dt_scan_root(unsigned long node,
457 const char *uname, int depth, void *data)
464 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
465 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
466 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
468 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
469 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
470 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
476 static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
481 return of_read_number(p, s);
484 static int __init early_init_dt_scan_memory(unsigned long node,
485 const char *uname, int depth, void *data)
487 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
491 /* Look for the ibm,dynamic-reconfiguration-memory node */
493 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
494 return early_init_dt_scan_drconf_memory(node);
496 /* We are scanning "memory" nodes only */
499 * The longtrail doesn't have a device_type on the
500 * /memory node, so look for the node called /memory@0.
502 if (depth != 1 || strcmp(uname, "memory@0") != 0)
504 } else if (strcmp(type, "memory") != 0)
507 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
509 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
513 endp = reg + (l / sizeof(cell_t));
515 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
516 uname, l, reg[0], reg[1], reg[2], reg[3]);
518 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
521 base = dt_mem_next_cell(dt_root_addr_cells, ®);
522 size = dt_mem_next_cell(dt_root_size_cells, ®);
526 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
527 (unsigned long long)size);
534 #ifdef CONFIG_PHYP_DUMP
536 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
538 * Function to find the largest size we need to reserve
539 * during early boot process.
541 * It either looks for boot param and returns that OR
542 * returns larger of 256 or 5% rounded down to multiples of 256MB.
545 static inline unsigned long phyp_dump_calculate_reserve_size(void)
549 if (phyp_dump_info->reserve_bootvar)
550 return phyp_dump_info->reserve_bootvar;
552 /* divide by 20 to get 5% of value */
553 tmp = lmb_end_of_DRAM();
556 /* round it down in multiples of 256 */
557 tmp = tmp & ~0x0FFFFFFFUL;
559 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
563 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
565 * This routine may reserve memory regions in the kernel only
566 * if the system is supported and a dump was taken in last
567 * boot instance or if the hardware is supported and the
568 * scratch area needs to be setup. In other instances it returns
569 * without reserving anything. The memory in case of dump being
570 * active is freed when the dump is collected (by userland tools).
572 static void __init phyp_dump_reserve_mem(void)
574 unsigned long base, size;
575 unsigned long variable_reserve_size;
577 if (!phyp_dump_info->phyp_dump_configured) {
578 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
582 if (!phyp_dump_info->phyp_dump_at_boot) {
583 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
587 variable_reserve_size = phyp_dump_calculate_reserve_size();
589 if (phyp_dump_info->phyp_dump_is_active) {
590 /* Reserve *everything* above RMR.Area freed by userland tools*/
591 base = variable_reserve_size;
592 size = lmb_end_of_DRAM() - base;
594 /* XXX crashed_ram_end is wrong, since it may be beyond
595 * the memory_limit, it will need to be adjusted. */
596 lmb_reserve(base, size);
598 phyp_dump_info->init_reserve_start = base;
599 phyp_dump_info->init_reserve_size = size;
601 size = phyp_dump_info->cpu_state_size +
602 phyp_dump_info->hpte_region_size +
603 variable_reserve_size;
604 base = lmb_end_of_DRAM() - size;
605 lmb_reserve(base, size);
606 phyp_dump_info->init_reserve_start = base;
607 phyp_dump_info->init_reserve_size = size;
611 static inline void __init phyp_dump_reserve_mem(void) {}
612 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
614 #ifdef CONFIG_EARLY_PRINTK
615 /* MS this is Microblaze specifig function */
616 static int __init early_init_dt_scan_serial(unsigned long node,
617 const char *uname, int depth, void *data)
623 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
625 /* find all serial nodes */
626 if (strncmp(uname, "serial", 6) != 0)
629 early_init_dt_check_for_initrd(node);
631 /* find compatible node with uartlite */
632 p = of_get_flat_dt_prop(node, "compatible", &l);
633 if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
634 (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
637 addr = of_get_flat_dt_prop(node, "reg", &l);
638 return *addr; /* return address */
641 /* this function is looking for early uartlite console - Microblaze specific */
642 int __init early_uartlite_console(void)
644 return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
648 void __init early_init_devtree(void *params)
650 pr_debug(" -> early_init_devtree(%p)\n", params);
652 /* Setup flat device-tree pointer */
653 initial_boot_params = params;
655 #ifdef CONFIG_PHYP_DUMP
656 /* scan tree to see if dump occured during last boot */
657 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
660 /* Retrieve various informations from the /chosen node of the
661 * device-tree, including the platform type, initrd location and
662 * size, TCE reserve, and more ...
664 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
666 /* Scan memory nodes and rebuild LMBs */
668 of_scan_flat_dt(early_init_dt_scan_root, NULL);
669 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
671 /* Save command line for /proc/cmdline and then parse parameters */
672 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
677 pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
679 pr_debug("Scanning CPUs ...\n");
681 /* Retreive CPU related informations from the flat tree
682 * (altivec support, boot CPU ID, ...)
684 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
686 pr_debug(" <- early_init_devtree()\n");
690 * Indicates whether the root node has a given value in its
691 * compatible property.
693 int machine_is_compatible(const char *compat)
695 struct device_node *root;
698 root = of_find_node_by_path("/");
700 rc = of_device_is_compatible(root, compat);
705 EXPORT_SYMBOL(machine_is_compatible);
709 * New implementation of the OF "find" APIs, return a refcounted
710 * object, call of_node_put() when done. The device tree and list
711 * are protected by a rw_lock.
713 * Note that property management will need some locking as well,
714 * this isn't dealt with yet.
719 * of_find_node_by_phandle - Find a node given a phandle
720 * @handle: phandle of the node to find
722 * Returns a node pointer with refcount incremented, use
723 * of_node_put() on it when done.
725 struct device_node *of_find_node_by_phandle(phandle handle)
727 struct device_node *np;
729 read_lock(&devtree_lock);
730 for (np = allnodes; np != NULL; np = np->allnext)
731 if (np->linux_phandle == handle)
734 read_unlock(&devtree_lock);
737 EXPORT_SYMBOL(of_find_node_by_phandle);
740 * of_node_get - Increment refcount of a node
741 * @node: Node to inc refcount, NULL is supported to
742 * simplify writing of callers
746 struct device_node *of_node_get(struct device_node *node)
749 kref_get(&node->kref);
752 EXPORT_SYMBOL(of_node_get);
754 static inline struct device_node *kref_to_device_node(struct kref *kref)
756 return container_of(kref, struct device_node, kref);
760 * of_node_release - release a dynamically allocated node
761 * @kref: kref element of the node to be released
763 * In of_node_put() this function is passed to kref_put()
766 static void of_node_release(struct kref *kref)
768 struct device_node *node = kref_to_device_node(kref);
769 struct property *prop = node->properties;
771 /* We should never be releasing nodes that haven't been detached. */
772 if (!of_node_check_flag(node, OF_DETACHED)) {
773 printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
776 kref_init(&node->kref);
780 if (!of_node_check_flag(node, OF_DYNAMIC))
784 struct property *next = prop->next;
791 prop = node->deadprops;
792 node->deadprops = NULL;
795 kfree(node->full_name);
801 * of_node_put - Decrement refcount of a node
802 * @node: Node to dec refcount, NULL is supported to
803 * simplify writing of callers
806 void of_node_put(struct device_node *node)
809 kref_put(&node->kref, of_node_release);
811 EXPORT_SYMBOL(of_node_put);
814 * Plug a device node into the tree and global list.
816 void of_attach_node(struct device_node *np)
820 write_lock_irqsave(&devtree_lock, flags);
821 np->sibling = np->parent->child;
822 np->allnext = allnodes;
823 np->parent->child = np;
825 write_unlock_irqrestore(&devtree_lock, flags);
829 * "Unplug" a node from the device tree. The caller must hold
830 * a reference to the node. The memory associated with the node
831 * is not freed until its refcount goes to zero.
833 void of_detach_node(struct device_node *np)
835 struct device_node *parent;
838 write_lock_irqsave(&devtree_lock, flags);
845 allnodes = np->allnext;
847 struct device_node *prev;
848 for (prev = allnodes;
850 prev = prev->allnext)
852 prev->allnext = np->allnext;
855 if (parent->child == np)
856 parent->child = np->sibling;
858 struct device_node *prevsib;
859 for (prevsib = np->parent->child;
860 prevsib->sibling != np;
861 prevsib = prevsib->sibling)
863 prevsib->sibling = np->sibling;
866 of_node_set_flag(np, OF_DETACHED);
869 write_unlock_irqrestore(&devtree_lock, flags);
873 * Add a property to a node
875 int prom_add_property(struct device_node *np, struct property *prop)
877 struct property **next;
881 write_lock_irqsave(&devtree_lock, flags);
882 next = &np->properties;
884 if (strcmp(prop->name, (*next)->name) == 0) {
885 /* duplicate ! don't insert it */
886 write_unlock_irqrestore(&devtree_lock, flags);
889 next = &(*next)->next;
892 write_unlock_irqrestore(&devtree_lock, flags);
894 #ifdef CONFIG_PROC_DEVICETREE
895 /* try to add to proc as well if it was initialized */
897 proc_device_tree_add_prop(np->pde, prop);
898 #endif /* CONFIG_PROC_DEVICETREE */
904 * Remove a property from a node. Note that we don't actually
905 * remove it, since we have given out who-knows-how-many pointers
906 * to the data using get-property. Instead we just move the property
907 * to the "dead properties" list, so it won't be found any more.
909 int prom_remove_property(struct device_node *np, struct property *prop)
911 struct property **next;
915 write_lock_irqsave(&devtree_lock, flags);
916 next = &np->properties;
921 prop->next = np->deadprops;
922 np->deadprops = prop;
926 next = &(*next)->next;
928 write_unlock_irqrestore(&devtree_lock, flags);
933 #ifdef CONFIG_PROC_DEVICETREE
934 /* try to remove the proc node as well */
936 proc_device_tree_remove_prop(np->pde, prop);
937 #endif /* CONFIG_PROC_DEVICETREE */
943 * Update a property in a node. Note that we don't actually
944 * remove it, since we have given out who-knows-how-many pointers
945 * to the data using get-property. Instead we just move the property
946 * to the "dead properties" list, and add the new property to the
949 int prom_update_property(struct device_node *np,
950 struct property *newprop,
951 struct property *oldprop)
953 struct property **next;
957 write_lock_irqsave(&devtree_lock, flags);
958 next = &np->properties;
960 if (*next == oldprop) {
962 newprop->next = oldprop->next;
964 oldprop->next = np->deadprops;
965 np->deadprops = oldprop;
969 next = &(*next)->next;
971 write_unlock_irqrestore(&devtree_lock, flags);
976 #ifdef CONFIG_PROC_DEVICETREE
977 /* try to add to proc as well if it was initialized */
979 proc_device_tree_update_prop(np->pde, newprop, oldprop);
980 #endif /* CONFIG_PROC_DEVICETREE */
985 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
986 static struct debugfs_blob_wrapper flat_dt_blob;
988 static int __init export_flat_device_tree(void)
992 flat_dt_blob.data = initial_boot_params;
993 flat_dt_blob.size = initial_boot_params->totalsize;
995 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
996 of_debugfs_root, &flat_dt_blob);
1002 device_initcall(export_flat_device_tree);