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;
54 * unflattens the device-tree passed by the firmware, creating the
55 * tree of struct device_node. It also fills the "name" and "type"
56 * pointers of the nodes so the normal device-tree walking functions
57 * can be used (this used to be done by finish_device_tree)
59 void __init unflatten_device_tree(void)
61 unsigned long start, mem, size;
62 struct device_node **allnextp = &allnodes;
64 pr_debug(" -> unflatten_device_tree()\n");
66 /* First pass, scan for size */
67 start = ((unsigned long)initial_boot_params) +
68 initial_boot_params->off_dt_struct;
69 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
70 size = (size | 3) + 1;
72 pr_debug(" size is %lx, allocating...\n", size);
74 /* Allocate memory for the expanded device tree */
75 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
76 mem = (unsigned long) __va(mem);
78 ((u32 *)mem)[size / 4] = 0xdeadbeef;
80 pr_debug(" unflattening %lx...\n", mem);
82 /* Second pass, do actual unflattening */
83 start = ((unsigned long)initial_boot_params) +
84 initial_boot_params->off_dt_struct;
85 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
86 if (*((u32 *)start) != OF_DT_END)
87 printk(KERN_WARNING "Weird tag at end of tree: %08x\n",
89 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
90 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
91 ((u32 *)mem)[size / 4]);
94 /* Get pointer to OF "/chosen" node for use everywhere */
95 of_chosen = of_find_node_by_path("/chosen");
96 if (of_chosen == NULL)
97 of_chosen = of_find_node_by_path("/chosen@0");
99 pr_debug(" <- unflatten_device_tree()\n");
102 #define early_init_dt_scan_drconf_memory(node) 0
104 static int __init early_init_dt_scan_cpus(unsigned long node,
105 const char *uname, int depth,
108 static int logical_cpuid;
109 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
114 /* We are scanning "cpu" nodes only */
115 if (type == NULL || strcmp(type, "cpu") != 0)
118 /* Get physical cpuid */
119 intserv = of_get_flat_dt_prop(node, "reg", NULL);
123 * Now see if any of these threads match our boot cpu.
124 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
126 for (i = 0; i < nthreads; i++) {
128 * version 2 of the kexec param format adds the phys cpuid of
131 if (initial_boot_params && initial_boot_params->version >= 2) {
133 initial_boot_params->boot_cpuid_phys) {
139 * Check if it's the boot-cpu, set it's hw index now,
140 * unfortunately this format did not support booting
141 * off secondary threads.
143 if (of_get_flat_dt_prop(node,
144 "linux,boot-cpu", NULL) != NULL) {
151 /* logical cpu id is always 0 on UP kernels */
157 pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
159 boot_cpuid = logical_cpuid;
165 #ifdef CONFIG_BLK_DEV_INITRD
166 static void __init early_init_dt_check_for_initrd(unsigned long node)
171 pr_debug("Looking for initrd properties... ");
173 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
175 initrd_start = (unsigned long)
176 __va((u32)of_read_ulong(prop, l/4));
178 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
180 initrd_end = (unsigned long)
181 __va((u32)of_read_ulong(prop, 1/4));
182 initrd_below_start_ok = 1;
188 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
189 initrd_start, initrd_end);
192 static inline void early_init_dt_check_for_initrd(unsigned long node)
195 #endif /* CONFIG_BLK_DEV_INITRD */
197 static int __init early_init_dt_scan_chosen(unsigned long node,
198 const char *uname, int depth, void *data)
203 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
206 (strcmp(uname, "chosen") != 0 &&
207 strcmp(uname, "chosen@0") != 0))
211 lprop = (u64 *)of_get_flat_dt_prop(node,
212 "linux,crashkernel-base", NULL);
214 crashk_res.start = *lprop;
216 lprop = (u64 *)of_get_flat_dt_prop(node,
217 "linux,crashkernel-size", NULL);
219 crashk_res.end = crashk_res.start + *lprop - 1;
222 early_init_dt_check_for_initrd(node);
224 /* Retreive command line */
225 p = of_get_flat_dt_prop(node, "bootargs", &l);
226 if (p != NULL && l > 0)
227 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
229 #ifdef CONFIG_CMDLINE
230 #ifndef CONFIG_CMDLINE_FORCE
231 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
233 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
234 #endif /* CONFIG_CMDLINE */
236 pr_debug("Command line is: %s\n", cmd_line);
242 static int __init early_init_dt_scan_root(unsigned long node,
243 const char *uname, int depth, void *data)
250 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
251 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
252 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
254 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
255 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
256 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
262 static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
267 return of_read_number(p, s);
270 static int __init early_init_dt_scan_memory(unsigned long node,
271 const char *uname, int depth, void *data)
273 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
277 /* Look for the ibm,dynamic-reconfiguration-memory node */
279 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
280 return early_init_dt_scan_drconf_memory(node);
282 /* We are scanning "memory" nodes only */
285 * The longtrail doesn't have a device_type on the
286 * /memory node, so look for the node called /memory@0.
288 if (depth != 1 || strcmp(uname, "memory@0") != 0)
290 } else if (strcmp(type, "memory") != 0)
293 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
295 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
299 endp = reg + (l / sizeof(cell_t));
301 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
302 uname, l, reg[0], reg[1], reg[2], reg[3]);
304 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
307 base = dt_mem_next_cell(dt_root_addr_cells, ®);
308 size = dt_mem_next_cell(dt_root_size_cells, ®);
312 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
313 (unsigned long long)size);
320 #ifdef CONFIG_PHYP_DUMP
322 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
324 * Function to find the largest size we need to reserve
325 * during early boot process.
327 * It either looks for boot param and returns that OR
328 * returns larger of 256 or 5% rounded down to multiples of 256MB.
331 static inline unsigned long phyp_dump_calculate_reserve_size(void)
335 if (phyp_dump_info->reserve_bootvar)
336 return phyp_dump_info->reserve_bootvar;
338 /* divide by 20 to get 5% of value */
339 tmp = lmb_end_of_DRAM();
342 /* round it down in multiples of 256 */
343 tmp = tmp & ~0x0FFFFFFFUL;
345 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
349 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
351 * This routine may reserve memory regions in the kernel only
352 * if the system is supported and a dump was taken in last
353 * boot instance or if the hardware is supported and the
354 * scratch area needs to be setup. In other instances it returns
355 * without reserving anything. The memory in case of dump being
356 * active is freed when the dump is collected (by userland tools).
358 static void __init phyp_dump_reserve_mem(void)
360 unsigned long base, size;
361 unsigned long variable_reserve_size;
363 if (!phyp_dump_info->phyp_dump_configured) {
364 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
368 if (!phyp_dump_info->phyp_dump_at_boot) {
369 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
373 variable_reserve_size = phyp_dump_calculate_reserve_size();
375 if (phyp_dump_info->phyp_dump_is_active) {
376 /* Reserve *everything* above RMR.Area freed by userland tools*/
377 base = variable_reserve_size;
378 size = lmb_end_of_DRAM() - base;
380 /* XXX crashed_ram_end is wrong, since it may be beyond
381 * the memory_limit, it will need to be adjusted. */
382 lmb_reserve(base, size);
384 phyp_dump_info->init_reserve_start = base;
385 phyp_dump_info->init_reserve_size = size;
387 size = phyp_dump_info->cpu_state_size +
388 phyp_dump_info->hpte_region_size +
389 variable_reserve_size;
390 base = lmb_end_of_DRAM() - size;
391 lmb_reserve(base, size);
392 phyp_dump_info->init_reserve_start = base;
393 phyp_dump_info->init_reserve_size = size;
397 static inline void __init phyp_dump_reserve_mem(void) {}
398 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
400 #ifdef CONFIG_EARLY_PRINTK
401 /* MS this is Microblaze specifig function */
402 static int __init early_init_dt_scan_serial(unsigned long node,
403 const char *uname, int depth, void *data)
409 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
411 /* find all serial nodes */
412 if (strncmp(uname, "serial", 6) != 0)
415 early_init_dt_check_for_initrd(node);
417 /* find compatible node with uartlite */
418 p = of_get_flat_dt_prop(node, "compatible", &l);
419 if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
420 (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
423 addr = of_get_flat_dt_prop(node, "reg", &l);
424 return *addr; /* return address */
427 /* this function is looking for early uartlite console - Microblaze specific */
428 int __init early_uartlite_console(void)
430 return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
434 void __init early_init_devtree(void *params)
436 pr_debug(" -> early_init_devtree(%p)\n", params);
438 /* Setup flat device-tree pointer */
439 initial_boot_params = params;
441 #ifdef CONFIG_PHYP_DUMP
442 /* scan tree to see if dump occured during last boot */
443 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
446 /* Retrieve various informations from the /chosen node of the
447 * device-tree, including the platform type, initrd location and
448 * size, TCE reserve, and more ...
450 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
452 /* Scan memory nodes and rebuild LMBs */
454 of_scan_flat_dt(early_init_dt_scan_root, NULL);
455 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
457 /* Save command line for /proc/cmdline and then parse parameters */
458 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
463 pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
465 pr_debug("Scanning CPUs ...\n");
467 /* Retreive CPU related informations from the flat tree
468 * (altivec support, boot CPU ID, ...)
470 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
472 pr_debug(" <- early_init_devtree()\n");
476 * Indicates whether the root node has a given value in its
477 * compatible property.
479 int machine_is_compatible(const char *compat)
481 struct device_node *root;
484 root = of_find_node_by_path("/");
486 rc = of_device_is_compatible(root, compat);
491 EXPORT_SYMBOL(machine_is_compatible);
495 * New implementation of the OF "find" APIs, return a refcounted
496 * object, call of_node_put() when done. The device tree and list
497 * are protected by a rw_lock.
499 * Note that property management will need some locking as well,
500 * this isn't dealt with yet.
505 * of_find_node_by_phandle - Find a node given a phandle
506 * @handle: phandle of the node to find
508 * Returns a node pointer with refcount incremented, use
509 * of_node_put() on it when done.
511 struct device_node *of_find_node_by_phandle(phandle handle)
513 struct device_node *np;
515 read_lock(&devtree_lock);
516 for (np = allnodes; np != NULL; np = np->allnext)
517 if (np->linux_phandle == handle)
520 read_unlock(&devtree_lock);
523 EXPORT_SYMBOL(of_find_node_by_phandle);
526 * of_node_get - Increment refcount of a node
527 * @node: Node to inc refcount, NULL is supported to
528 * simplify writing of callers
532 struct device_node *of_node_get(struct device_node *node)
535 kref_get(&node->kref);
538 EXPORT_SYMBOL(of_node_get);
540 static inline struct device_node *kref_to_device_node(struct kref *kref)
542 return container_of(kref, struct device_node, kref);
546 * of_node_release - release a dynamically allocated node
547 * @kref: kref element of the node to be released
549 * In of_node_put() this function is passed to kref_put()
552 static void of_node_release(struct kref *kref)
554 struct device_node *node = kref_to_device_node(kref);
555 struct property *prop = node->properties;
557 /* We should never be releasing nodes that haven't been detached. */
558 if (!of_node_check_flag(node, OF_DETACHED)) {
559 printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
562 kref_init(&node->kref);
566 if (!of_node_check_flag(node, OF_DYNAMIC))
570 struct property *next = prop->next;
577 prop = node->deadprops;
578 node->deadprops = NULL;
581 kfree(node->full_name);
587 * of_node_put - Decrement refcount of a node
588 * @node: Node to dec refcount, NULL is supported to
589 * simplify writing of callers
592 void of_node_put(struct device_node *node)
595 kref_put(&node->kref, of_node_release);
597 EXPORT_SYMBOL(of_node_put);
600 * Plug a device node into the tree and global list.
602 void of_attach_node(struct device_node *np)
606 write_lock_irqsave(&devtree_lock, flags);
607 np->sibling = np->parent->child;
608 np->allnext = allnodes;
609 np->parent->child = np;
611 write_unlock_irqrestore(&devtree_lock, flags);
615 * "Unplug" a node from the device tree. The caller must hold
616 * a reference to the node. The memory associated with the node
617 * is not freed until its refcount goes to zero.
619 void of_detach_node(struct device_node *np)
621 struct device_node *parent;
624 write_lock_irqsave(&devtree_lock, flags);
631 allnodes = np->allnext;
633 struct device_node *prev;
634 for (prev = allnodes;
636 prev = prev->allnext)
638 prev->allnext = np->allnext;
641 if (parent->child == np)
642 parent->child = np->sibling;
644 struct device_node *prevsib;
645 for (prevsib = np->parent->child;
646 prevsib->sibling != np;
647 prevsib = prevsib->sibling)
649 prevsib->sibling = np->sibling;
652 of_node_set_flag(np, OF_DETACHED);
655 write_unlock_irqrestore(&devtree_lock, flags);
659 * Add a property to a node
661 int prom_add_property(struct device_node *np, struct property *prop)
663 struct property **next;
667 write_lock_irqsave(&devtree_lock, flags);
668 next = &np->properties;
670 if (strcmp(prop->name, (*next)->name) == 0) {
671 /* duplicate ! don't insert it */
672 write_unlock_irqrestore(&devtree_lock, flags);
675 next = &(*next)->next;
678 write_unlock_irqrestore(&devtree_lock, flags);
680 #ifdef CONFIG_PROC_DEVICETREE
681 /* try to add to proc as well if it was initialized */
683 proc_device_tree_add_prop(np->pde, prop);
684 #endif /* CONFIG_PROC_DEVICETREE */
690 * Remove a property from a node. Note that we don't actually
691 * remove it, since we have given out who-knows-how-many pointers
692 * to the data using get-property. Instead we just move the property
693 * to the "dead properties" list, so it won't be found any more.
695 int prom_remove_property(struct device_node *np, struct property *prop)
697 struct property **next;
701 write_lock_irqsave(&devtree_lock, flags);
702 next = &np->properties;
707 prop->next = np->deadprops;
708 np->deadprops = prop;
712 next = &(*next)->next;
714 write_unlock_irqrestore(&devtree_lock, flags);
719 #ifdef CONFIG_PROC_DEVICETREE
720 /* try to remove the proc node as well */
722 proc_device_tree_remove_prop(np->pde, prop);
723 #endif /* CONFIG_PROC_DEVICETREE */
729 * Update a property in a node. Note that we don't actually
730 * remove it, since we have given out who-knows-how-many pointers
731 * to the data using get-property. Instead we just move the property
732 * to the "dead properties" list, and add the new property to the
735 int prom_update_property(struct device_node *np,
736 struct property *newprop,
737 struct property *oldprop)
739 struct property **next;
743 write_lock_irqsave(&devtree_lock, flags);
744 next = &np->properties;
746 if (*next == oldprop) {
748 newprop->next = oldprop->next;
750 oldprop->next = np->deadprops;
751 np->deadprops = oldprop;
755 next = &(*next)->next;
757 write_unlock_irqrestore(&devtree_lock, flags);
762 #ifdef CONFIG_PROC_DEVICETREE
763 /* try to add to proc as well if it was initialized */
765 proc_device_tree_update_prop(np->pde, newprop, oldprop);
766 #endif /* CONFIG_PROC_DEVICETREE */
771 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
772 static struct debugfs_blob_wrapper flat_dt_blob;
774 static int __init export_flat_device_tree(void)
778 flat_dt_blob.data = initial_boot_params;
779 flat_dt_blob.size = initial_boot_params->totalsize;
781 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
782 of_debugfs_root, &flat_dt_blob);
788 device_initcall(export_flat_device_tree);