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>
47 /* export that to outside world */
48 struct device_node *of_chosen;
50 #define early_init_dt_scan_drconf_memory(node) 0
52 static int __init early_init_dt_scan_cpus(unsigned long node,
53 const char *uname, int depth,
56 static int logical_cpuid;
57 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
62 /* We are scanning "cpu" nodes only */
63 if (type == NULL || strcmp(type, "cpu") != 0)
66 /* Get physical cpuid */
67 intserv = of_get_flat_dt_prop(node, "reg", NULL);
71 * Now see if any of these threads match our boot cpu.
72 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
74 for (i = 0; i < nthreads; i++) {
76 * version 2 of the kexec param format adds the phys cpuid of
79 if (initial_boot_params && initial_boot_params->version >= 2) {
81 initial_boot_params->boot_cpuid_phys) {
87 * Check if it's the boot-cpu, set it's hw index now,
88 * unfortunately this format did not support booting
89 * off secondary threads.
91 if (of_get_flat_dt_prop(node,
92 "linux,boot-cpu", NULL) != NULL) {
99 /* logical cpu id is always 0 on UP kernels */
105 pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
107 boot_cpuid = logical_cpuid;
113 static int __init early_init_dt_scan_chosen(unsigned long node,
114 const char *uname, int depth, void *data)
119 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
122 (strcmp(uname, "chosen") != 0 &&
123 strcmp(uname, "chosen@0") != 0))
127 lprop = (u64 *)of_get_flat_dt_prop(node,
128 "linux,crashkernel-base", NULL);
130 crashk_res.start = *lprop;
132 lprop = (u64 *)of_get_flat_dt_prop(node,
133 "linux,crashkernel-size", NULL);
135 crashk_res.end = crashk_res.start + *lprop - 1;
138 early_init_dt_check_for_initrd(node);
140 /* Retreive command line */
141 p = of_get_flat_dt_prop(node, "bootargs", &l);
142 if (p != NULL && l > 0)
143 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
145 #ifdef CONFIG_CMDLINE
146 #ifndef CONFIG_CMDLINE_FORCE
147 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
149 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
150 #endif /* CONFIG_CMDLINE */
152 pr_debug("Command line is: %s\n", cmd_line);
158 static int __init early_init_dt_scan_memory(unsigned long node,
159 const char *uname, int depth, void *data)
161 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
165 /* Look for the ibm,dynamic-reconfiguration-memory node */
167 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
168 return early_init_dt_scan_drconf_memory(node);
170 /* We are scanning "memory" nodes only */
173 * The longtrail doesn't have a device_type on the
174 * /memory node, so look for the node called /memory@0.
176 if (depth != 1 || strcmp(uname, "memory@0") != 0)
178 } else if (strcmp(type, "memory") != 0)
181 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
183 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
187 endp = reg + (l / sizeof(cell_t));
189 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
190 uname, l, reg[0], reg[1], reg[2], reg[3]);
192 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
195 base = dt_mem_next_cell(dt_root_addr_cells, ®);
196 size = dt_mem_next_cell(dt_root_size_cells, ®);
200 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
201 (unsigned long long)size);
208 #ifdef CONFIG_PHYP_DUMP
210 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
212 * Function to find the largest size we need to reserve
213 * during early boot process.
215 * It either looks for boot param and returns that OR
216 * returns larger of 256 or 5% rounded down to multiples of 256MB.
219 static inline unsigned long phyp_dump_calculate_reserve_size(void)
223 if (phyp_dump_info->reserve_bootvar)
224 return phyp_dump_info->reserve_bootvar;
226 /* divide by 20 to get 5% of value */
227 tmp = lmb_end_of_DRAM();
230 /* round it down in multiples of 256 */
231 tmp = tmp & ~0x0FFFFFFFUL;
233 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
237 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
239 * This routine may reserve memory regions in the kernel only
240 * if the system is supported and a dump was taken in last
241 * boot instance or if the hardware is supported and the
242 * scratch area needs to be setup. In other instances it returns
243 * without reserving anything. The memory in case of dump being
244 * active is freed when the dump is collected (by userland tools).
246 static void __init phyp_dump_reserve_mem(void)
248 unsigned long base, size;
249 unsigned long variable_reserve_size;
251 if (!phyp_dump_info->phyp_dump_configured) {
252 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
256 if (!phyp_dump_info->phyp_dump_at_boot) {
257 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
261 variable_reserve_size = phyp_dump_calculate_reserve_size();
263 if (phyp_dump_info->phyp_dump_is_active) {
264 /* Reserve *everything* above RMR.Area freed by userland tools*/
265 base = variable_reserve_size;
266 size = lmb_end_of_DRAM() - base;
268 /* XXX crashed_ram_end is wrong, since it may be beyond
269 * the memory_limit, it will need to be adjusted. */
270 lmb_reserve(base, size);
272 phyp_dump_info->init_reserve_start = base;
273 phyp_dump_info->init_reserve_size = size;
275 size = phyp_dump_info->cpu_state_size +
276 phyp_dump_info->hpte_region_size +
277 variable_reserve_size;
278 base = lmb_end_of_DRAM() - size;
279 lmb_reserve(base, size);
280 phyp_dump_info->init_reserve_start = base;
281 phyp_dump_info->init_reserve_size = size;
285 static inline void __init phyp_dump_reserve_mem(void) {}
286 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
288 #ifdef CONFIG_EARLY_PRINTK
289 /* MS this is Microblaze specifig function */
290 static int __init early_init_dt_scan_serial(unsigned long node,
291 const char *uname, int depth, void *data)
297 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
299 /* find all serial nodes */
300 if (strncmp(uname, "serial", 6) != 0)
303 early_init_dt_check_for_initrd(node);
305 /* find compatible node with uartlite */
306 p = of_get_flat_dt_prop(node, "compatible", &l);
307 if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
308 (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
311 addr = of_get_flat_dt_prop(node, "reg", &l);
312 return *addr; /* return address */
315 /* this function is looking for early uartlite console - Microblaze specific */
316 int __init early_uartlite_console(void)
318 return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
322 void __init early_init_devtree(void *params)
324 pr_debug(" -> early_init_devtree(%p)\n", params);
326 /* Setup flat device-tree pointer */
327 initial_boot_params = params;
329 #ifdef CONFIG_PHYP_DUMP
330 /* scan tree to see if dump occured during last boot */
331 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
334 /* Retrieve various informations from the /chosen node of the
335 * device-tree, including the platform type, initrd location and
336 * size, TCE reserve, and more ...
338 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
340 /* Scan memory nodes and rebuild LMBs */
342 of_scan_flat_dt(early_init_dt_scan_root, NULL);
343 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
345 /* Save command line for /proc/cmdline and then parse parameters */
346 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
351 pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
353 pr_debug("Scanning CPUs ...\n");
355 /* Retreive CPU related informations from the flat tree
356 * (altivec support, boot CPU ID, ...)
358 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
360 pr_debug(" <- early_init_devtree()\n");
364 * Indicates whether the root node has a given value in its
365 * compatible property.
367 int machine_is_compatible(const char *compat)
369 struct device_node *root;
372 root = of_find_node_by_path("/");
374 rc = of_device_is_compatible(root, compat);
379 EXPORT_SYMBOL(machine_is_compatible);
383 * New implementation of the OF "find" APIs, return a refcounted
384 * object, call of_node_put() when done. The device tree and list
385 * are protected by a rw_lock.
387 * Note that property management will need some locking as well,
388 * this isn't dealt with yet.
393 * of_find_node_by_phandle - Find a node given a phandle
394 * @handle: phandle of the node to find
396 * Returns a node pointer with refcount incremented, use
397 * of_node_put() on it when done.
399 struct device_node *of_find_node_by_phandle(phandle handle)
401 struct device_node *np;
403 read_lock(&devtree_lock);
404 for (np = allnodes; np != NULL; np = np->allnext)
405 if (np->linux_phandle == handle)
408 read_unlock(&devtree_lock);
411 EXPORT_SYMBOL(of_find_node_by_phandle);
414 * of_node_get - Increment refcount of a node
415 * @node: Node to inc refcount, NULL is supported to
416 * simplify writing of callers
420 struct device_node *of_node_get(struct device_node *node)
423 kref_get(&node->kref);
426 EXPORT_SYMBOL(of_node_get);
428 static inline struct device_node *kref_to_device_node(struct kref *kref)
430 return container_of(kref, struct device_node, kref);
434 * of_node_release - release a dynamically allocated node
435 * @kref: kref element of the node to be released
437 * In of_node_put() this function is passed to kref_put()
440 static void of_node_release(struct kref *kref)
442 struct device_node *node = kref_to_device_node(kref);
443 struct property *prop = node->properties;
445 /* We should never be releasing nodes that haven't been detached. */
446 if (!of_node_check_flag(node, OF_DETACHED)) {
447 printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
450 kref_init(&node->kref);
454 if (!of_node_check_flag(node, OF_DYNAMIC))
458 struct property *next = prop->next;
465 prop = node->deadprops;
466 node->deadprops = NULL;
469 kfree(node->full_name);
475 * of_node_put - Decrement refcount of a node
476 * @node: Node to dec refcount, NULL is supported to
477 * simplify writing of callers
480 void of_node_put(struct device_node *node)
483 kref_put(&node->kref, of_node_release);
485 EXPORT_SYMBOL(of_node_put);
488 * Plug a device node into the tree and global list.
490 void of_attach_node(struct device_node *np)
494 write_lock_irqsave(&devtree_lock, flags);
495 np->sibling = np->parent->child;
496 np->allnext = allnodes;
497 np->parent->child = np;
499 write_unlock_irqrestore(&devtree_lock, flags);
503 * "Unplug" a node from the device tree. The caller must hold
504 * a reference to the node. The memory associated with the node
505 * is not freed until its refcount goes to zero.
507 void of_detach_node(struct device_node *np)
509 struct device_node *parent;
512 write_lock_irqsave(&devtree_lock, flags);
519 allnodes = np->allnext;
521 struct device_node *prev;
522 for (prev = allnodes;
524 prev = prev->allnext)
526 prev->allnext = np->allnext;
529 if (parent->child == np)
530 parent->child = np->sibling;
532 struct device_node *prevsib;
533 for (prevsib = np->parent->child;
534 prevsib->sibling != np;
535 prevsib = prevsib->sibling)
537 prevsib->sibling = np->sibling;
540 of_node_set_flag(np, OF_DETACHED);
543 write_unlock_irqrestore(&devtree_lock, flags);
546 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
547 static struct debugfs_blob_wrapper flat_dt_blob;
549 static int __init export_flat_device_tree(void)
553 flat_dt_blob.data = initial_boot_params;
554 flat_dt_blob.size = initial_boot_params->totalsize;
556 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
557 of_debugfs_root, &flat_dt_blob);
563 device_initcall(export_flat_device_tree);