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 #define early_init_dt_scan_drconf_memory(node) 0
55 static int __init early_init_dt_scan_cpus(unsigned long node,
56 const char *uname, int depth,
59 static int logical_cpuid;
60 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
65 /* We are scanning "cpu" nodes only */
66 if (type == NULL || strcmp(type, "cpu") != 0)
69 /* Get physical cpuid */
70 intserv = of_get_flat_dt_prop(node, "reg", NULL);
74 * Now see if any of these threads match our boot cpu.
75 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
77 for (i = 0; i < nthreads; i++) {
79 * version 2 of the kexec param format adds the phys cpuid of
82 if (initial_boot_params && initial_boot_params->version >= 2) {
84 initial_boot_params->boot_cpuid_phys) {
90 * Check if it's the boot-cpu, set it's hw index now,
91 * unfortunately this format did not support booting
92 * off secondary threads.
94 if (of_get_flat_dt_prop(node,
95 "linux,boot-cpu", NULL) != NULL) {
102 /* logical cpu id is always 0 on UP kernels */
108 pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
110 boot_cpuid = logical_cpuid;
116 #ifdef CONFIG_BLK_DEV_INITRD
117 static void __init early_init_dt_check_for_initrd(unsigned long node)
122 pr_debug("Looking for initrd properties... ");
124 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
126 initrd_start = (unsigned long)
127 __va((u32)of_read_ulong(prop, l/4));
129 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
131 initrd_end = (unsigned long)
132 __va((u32)of_read_ulong(prop, 1/4));
133 initrd_below_start_ok = 1;
139 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
140 initrd_start, initrd_end);
143 static inline void early_init_dt_check_for_initrd(unsigned long node)
146 #endif /* CONFIG_BLK_DEV_INITRD */
148 static int __init early_init_dt_scan_chosen(unsigned long node,
149 const char *uname, int depth, void *data)
154 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
157 (strcmp(uname, "chosen") != 0 &&
158 strcmp(uname, "chosen@0") != 0))
162 lprop = (u64 *)of_get_flat_dt_prop(node,
163 "linux,crashkernel-base", NULL);
165 crashk_res.start = *lprop;
167 lprop = (u64 *)of_get_flat_dt_prop(node,
168 "linux,crashkernel-size", NULL);
170 crashk_res.end = crashk_res.start + *lprop - 1;
173 early_init_dt_check_for_initrd(node);
175 /* Retreive command line */
176 p = of_get_flat_dt_prop(node, "bootargs", &l);
177 if (p != NULL && l > 0)
178 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
180 #ifdef CONFIG_CMDLINE
181 #ifndef CONFIG_CMDLINE_FORCE
182 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
184 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
185 #endif /* CONFIG_CMDLINE */
187 pr_debug("Command line is: %s\n", cmd_line);
193 static int __init early_init_dt_scan_root(unsigned long node,
194 const char *uname, int depth, void *data)
201 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
202 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
203 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
205 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
206 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
207 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
213 static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
218 return of_read_number(p, s);
221 static int __init early_init_dt_scan_memory(unsigned long node,
222 const char *uname, int depth, void *data)
224 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
228 /* Look for the ibm,dynamic-reconfiguration-memory node */
230 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
231 return early_init_dt_scan_drconf_memory(node);
233 /* We are scanning "memory" nodes only */
236 * The longtrail doesn't have a device_type on the
237 * /memory node, so look for the node called /memory@0.
239 if (depth != 1 || strcmp(uname, "memory@0") != 0)
241 } else if (strcmp(type, "memory") != 0)
244 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
246 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
250 endp = reg + (l / sizeof(cell_t));
252 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
253 uname, l, reg[0], reg[1], reg[2], reg[3]);
255 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
258 base = dt_mem_next_cell(dt_root_addr_cells, ®);
259 size = dt_mem_next_cell(dt_root_size_cells, ®);
263 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
264 (unsigned long long)size);
271 #ifdef CONFIG_PHYP_DUMP
273 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
275 * Function to find the largest size we need to reserve
276 * during early boot process.
278 * It either looks for boot param and returns that OR
279 * returns larger of 256 or 5% rounded down to multiples of 256MB.
282 static inline unsigned long phyp_dump_calculate_reserve_size(void)
286 if (phyp_dump_info->reserve_bootvar)
287 return phyp_dump_info->reserve_bootvar;
289 /* divide by 20 to get 5% of value */
290 tmp = lmb_end_of_DRAM();
293 /* round it down in multiples of 256 */
294 tmp = tmp & ~0x0FFFFFFFUL;
296 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
300 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
302 * This routine may reserve memory regions in the kernel only
303 * if the system is supported and a dump was taken in last
304 * boot instance or if the hardware is supported and the
305 * scratch area needs to be setup. In other instances it returns
306 * without reserving anything. The memory in case of dump being
307 * active is freed when the dump is collected (by userland tools).
309 static void __init phyp_dump_reserve_mem(void)
311 unsigned long base, size;
312 unsigned long variable_reserve_size;
314 if (!phyp_dump_info->phyp_dump_configured) {
315 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
319 if (!phyp_dump_info->phyp_dump_at_boot) {
320 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
324 variable_reserve_size = phyp_dump_calculate_reserve_size();
326 if (phyp_dump_info->phyp_dump_is_active) {
327 /* Reserve *everything* above RMR.Area freed by userland tools*/
328 base = variable_reserve_size;
329 size = lmb_end_of_DRAM() - base;
331 /* XXX crashed_ram_end is wrong, since it may be beyond
332 * the memory_limit, it will need to be adjusted. */
333 lmb_reserve(base, size);
335 phyp_dump_info->init_reserve_start = base;
336 phyp_dump_info->init_reserve_size = size;
338 size = phyp_dump_info->cpu_state_size +
339 phyp_dump_info->hpte_region_size +
340 variable_reserve_size;
341 base = lmb_end_of_DRAM() - size;
342 lmb_reserve(base, size);
343 phyp_dump_info->init_reserve_start = base;
344 phyp_dump_info->init_reserve_size = size;
348 static inline void __init phyp_dump_reserve_mem(void) {}
349 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
351 #ifdef CONFIG_EARLY_PRINTK
352 /* MS this is Microblaze specifig function */
353 static int __init early_init_dt_scan_serial(unsigned long node,
354 const char *uname, int depth, void *data)
360 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
362 /* find all serial nodes */
363 if (strncmp(uname, "serial", 6) != 0)
366 early_init_dt_check_for_initrd(node);
368 /* find compatible node with uartlite */
369 p = of_get_flat_dt_prop(node, "compatible", &l);
370 if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
371 (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
374 addr = of_get_flat_dt_prop(node, "reg", &l);
375 return *addr; /* return address */
378 /* this function is looking for early uartlite console - Microblaze specific */
379 int __init early_uartlite_console(void)
381 return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
385 void __init early_init_devtree(void *params)
387 pr_debug(" -> early_init_devtree(%p)\n", params);
389 /* Setup flat device-tree pointer */
390 initial_boot_params = params;
392 #ifdef CONFIG_PHYP_DUMP
393 /* scan tree to see if dump occured during last boot */
394 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
397 /* Retrieve various informations from the /chosen node of the
398 * device-tree, including the platform type, initrd location and
399 * size, TCE reserve, and more ...
401 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
403 /* Scan memory nodes and rebuild LMBs */
405 of_scan_flat_dt(early_init_dt_scan_root, NULL);
406 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
408 /* Save command line for /proc/cmdline and then parse parameters */
409 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
414 pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
416 pr_debug("Scanning CPUs ...\n");
418 /* Retreive CPU related informations from the flat tree
419 * (altivec support, boot CPU ID, ...)
421 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
423 pr_debug(" <- early_init_devtree()\n");
427 * Indicates whether the root node has a given value in its
428 * compatible property.
430 int machine_is_compatible(const char *compat)
432 struct device_node *root;
435 root = of_find_node_by_path("/");
437 rc = of_device_is_compatible(root, compat);
442 EXPORT_SYMBOL(machine_is_compatible);
446 * New implementation of the OF "find" APIs, return a refcounted
447 * object, call of_node_put() when done. The device tree and list
448 * are protected by a rw_lock.
450 * Note that property management will need some locking as well,
451 * this isn't dealt with yet.
456 * of_find_node_by_phandle - Find a node given a phandle
457 * @handle: phandle of the node to find
459 * Returns a node pointer with refcount incremented, use
460 * of_node_put() on it when done.
462 struct device_node *of_find_node_by_phandle(phandle handle)
464 struct device_node *np;
466 read_lock(&devtree_lock);
467 for (np = allnodes; np != NULL; np = np->allnext)
468 if (np->linux_phandle == handle)
471 read_unlock(&devtree_lock);
474 EXPORT_SYMBOL(of_find_node_by_phandle);
477 * of_node_get - Increment refcount of a node
478 * @node: Node to inc refcount, NULL is supported to
479 * simplify writing of callers
483 struct device_node *of_node_get(struct device_node *node)
486 kref_get(&node->kref);
489 EXPORT_SYMBOL(of_node_get);
491 static inline struct device_node *kref_to_device_node(struct kref *kref)
493 return container_of(kref, struct device_node, kref);
497 * of_node_release - release a dynamically allocated node
498 * @kref: kref element of the node to be released
500 * In of_node_put() this function is passed to kref_put()
503 static void of_node_release(struct kref *kref)
505 struct device_node *node = kref_to_device_node(kref);
506 struct property *prop = node->properties;
508 /* We should never be releasing nodes that haven't been detached. */
509 if (!of_node_check_flag(node, OF_DETACHED)) {
510 printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
513 kref_init(&node->kref);
517 if (!of_node_check_flag(node, OF_DYNAMIC))
521 struct property *next = prop->next;
528 prop = node->deadprops;
529 node->deadprops = NULL;
532 kfree(node->full_name);
538 * of_node_put - Decrement refcount of a node
539 * @node: Node to dec refcount, NULL is supported to
540 * simplify writing of callers
543 void of_node_put(struct device_node *node)
546 kref_put(&node->kref, of_node_release);
548 EXPORT_SYMBOL(of_node_put);
551 * Plug a device node into the tree and global list.
553 void of_attach_node(struct device_node *np)
557 write_lock_irqsave(&devtree_lock, flags);
558 np->sibling = np->parent->child;
559 np->allnext = allnodes;
560 np->parent->child = np;
562 write_unlock_irqrestore(&devtree_lock, flags);
566 * "Unplug" a node from the device tree. The caller must hold
567 * a reference to the node. The memory associated with the node
568 * is not freed until its refcount goes to zero.
570 void of_detach_node(struct device_node *np)
572 struct device_node *parent;
575 write_lock_irqsave(&devtree_lock, flags);
582 allnodes = np->allnext;
584 struct device_node *prev;
585 for (prev = allnodes;
587 prev = prev->allnext)
589 prev->allnext = np->allnext;
592 if (parent->child == np)
593 parent->child = np->sibling;
595 struct device_node *prevsib;
596 for (prevsib = np->parent->child;
597 prevsib->sibling != np;
598 prevsib = prevsib->sibling)
600 prevsib->sibling = np->sibling;
603 of_node_set_flag(np, OF_DETACHED);
606 write_unlock_irqrestore(&devtree_lock, flags);
610 * Add a property to a node
612 int prom_add_property(struct device_node *np, struct property *prop)
614 struct property **next;
618 write_lock_irqsave(&devtree_lock, flags);
619 next = &np->properties;
621 if (strcmp(prop->name, (*next)->name) == 0) {
622 /* duplicate ! don't insert it */
623 write_unlock_irqrestore(&devtree_lock, flags);
626 next = &(*next)->next;
629 write_unlock_irqrestore(&devtree_lock, flags);
631 #ifdef CONFIG_PROC_DEVICETREE
632 /* try to add to proc as well if it was initialized */
634 proc_device_tree_add_prop(np->pde, prop);
635 #endif /* CONFIG_PROC_DEVICETREE */
641 * Remove a property from a node. Note that we don't actually
642 * remove it, since we have given out who-knows-how-many pointers
643 * to the data using get-property. Instead we just move the property
644 * to the "dead properties" list, so it won't be found any more.
646 int prom_remove_property(struct device_node *np, struct property *prop)
648 struct property **next;
652 write_lock_irqsave(&devtree_lock, flags);
653 next = &np->properties;
658 prop->next = np->deadprops;
659 np->deadprops = prop;
663 next = &(*next)->next;
665 write_unlock_irqrestore(&devtree_lock, flags);
670 #ifdef CONFIG_PROC_DEVICETREE
671 /* try to remove the proc node as well */
673 proc_device_tree_remove_prop(np->pde, prop);
674 #endif /* CONFIG_PROC_DEVICETREE */
680 * Update a property in a node. Note that we don't actually
681 * remove it, since we have given out who-knows-how-many pointers
682 * to the data using get-property. Instead we just move the property
683 * to the "dead properties" list, and add the new property to the
686 int prom_update_property(struct device_node *np,
687 struct property *newprop,
688 struct property *oldprop)
690 struct property **next;
694 write_lock_irqsave(&devtree_lock, flags);
695 next = &np->properties;
697 if (*next == oldprop) {
699 newprop->next = oldprop->next;
701 oldprop->next = np->deadprops;
702 np->deadprops = oldprop;
706 next = &(*next)->next;
708 write_unlock_irqrestore(&devtree_lock, flags);
713 #ifdef CONFIG_PROC_DEVICETREE
714 /* try to add to proc as well if it was initialized */
716 proc_device_tree_update_prop(np->pde, newprop, oldprop);
717 #endif /* CONFIG_PROC_DEVICETREE */
722 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
723 static struct debugfs_blob_wrapper flat_dt_blob;
725 static int __init export_flat_device_tree(void)
729 flat_dt_blob.data = initial_boot_params;
730 flat_dt_blob.size = initial_boot_params->totalsize;
732 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
733 of_debugfs_root, &flat_dt_blob);
739 device_initcall(export_flat_device_tree);