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 /* export that to outside world */
46 struct device_node *of_chosen;
48 #define early_init_dt_scan_drconf_memory(node) 0
50 static int __init early_init_dt_scan_cpus(unsigned long node,
51 const char *uname, int depth,
54 static int logical_cpuid;
55 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
60 /* We are scanning "cpu" nodes only */
61 if (type == NULL || strcmp(type, "cpu") != 0)
64 /* Get physical cpuid */
65 intserv = of_get_flat_dt_prop(node, "reg", NULL);
69 * Now see if any of these threads match our boot cpu.
70 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
72 for (i = 0; i < nthreads; i++) {
74 * version 2 of the kexec param format adds the phys cpuid of
77 if (initial_boot_params && initial_boot_params->version >= 2) {
79 initial_boot_params->boot_cpuid_phys) {
85 * Check if it's the boot-cpu, set it's hw index now,
86 * unfortunately this format did not support booting
87 * off secondary threads.
89 if (of_get_flat_dt_prop(node,
90 "linux,boot-cpu", NULL) != NULL) {
97 /* logical cpu id is always 0 on UP kernels */
103 pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
105 boot_cpuid = logical_cpuid;
111 void __init early_init_dt_scan_chosen_arch(unsigned long node)
113 /* No Microblaze specific code here */
116 static int __init early_init_dt_scan_memory(unsigned long node,
117 const char *uname, int depth, void *data)
119 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
123 /* Look for the ibm,dynamic-reconfiguration-memory node */
125 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
126 return early_init_dt_scan_drconf_memory(node);
128 /* We are scanning "memory" nodes only */
131 * The longtrail doesn't have a device_type on the
132 * /memory node, so look for the node called /memory@0.
134 if (depth != 1 || strcmp(uname, "memory@0") != 0)
136 } else if (strcmp(type, "memory") != 0)
139 reg = (__be32 *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
141 reg = (__be32 *)of_get_flat_dt_prop(node, "reg", &l);
145 endp = reg + (l / sizeof(__be32));
147 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
148 uname, l, reg[0], reg[1], reg[2], reg[3]);
150 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
153 base = dt_mem_next_cell(dt_root_addr_cells, ®);
154 size = dt_mem_next_cell(dt_root_size_cells, ®);
158 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
159 (unsigned long long)size);
166 #ifdef CONFIG_PHYP_DUMP
168 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
170 * Function to find the largest size we need to reserve
171 * during early boot process.
173 * It either looks for boot param and returns that OR
174 * returns larger of 256 or 5% rounded down to multiples of 256MB.
177 static inline unsigned long phyp_dump_calculate_reserve_size(void)
181 if (phyp_dump_info->reserve_bootvar)
182 return phyp_dump_info->reserve_bootvar;
184 /* divide by 20 to get 5% of value */
185 tmp = lmb_end_of_DRAM();
188 /* round it down in multiples of 256 */
189 tmp = tmp & ~0x0FFFFFFFUL;
191 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
195 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
197 * This routine may reserve memory regions in the kernel only
198 * if the system is supported and a dump was taken in last
199 * boot instance or if the hardware is supported and the
200 * scratch area needs to be setup. In other instances it returns
201 * without reserving anything. The memory in case of dump being
202 * active is freed when the dump is collected (by userland tools).
204 static void __init phyp_dump_reserve_mem(void)
206 unsigned long base, size;
207 unsigned long variable_reserve_size;
209 if (!phyp_dump_info->phyp_dump_configured) {
210 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
214 if (!phyp_dump_info->phyp_dump_at_boot) {
215 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
219 variable_reserve_size = phyp_dump_calculate_reserve_size();
221 if (phyp_dump_info->phyp_dump_is_active) {
222 /* Reserve *everything* above RMR.Area freed by userland tools*/
223 base = variable_reserve_size;
224 size = lmb_end_of_DRAM() - base;
226 /* XXX crashed_ram_end is wrong, since it may be beyond
227 * the memory_limit, it will need to be adjusted. */
228 lmb_reserve(base, size);
230 phyp_dump_info->init_reserve_start = base;
231 phyp_dump_info->init_reserve_size = size;
233 size = phyp_dump_info->cpu_state_size +
234 phyp_dump_info->hpte_region_size +
235 variable_reserve_size;
236 base = lmb_end_of_DRAM() - size;
237 lmb_reserve(base, size);
238 phyp_dump_info->init_reserve_start = base;
239 phyp_dump_info->init_reserve_size = size;
243 static inline void __init phyp_dump_reserve_mem(void) {}
244 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
246 #ifdef CONFIG_EARLY_PRINTK
247 /* MS this is Microblaze specifig function */
248 static int __init early_init_dt_scan_serial(unsigned long node,
249 const char *uname, int depth, void *data)
255 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
257 /* find all serial nodes */
258 if (strncmp(uname, "serial", 6) != 0)
261 early_init_dt_check_for_initrd(node);
263 /* find compatible node with uartlite */
264 p = of_get_flat_dt_prop(node, "compatible", &l);
265 if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
266 (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
269 addr = of_get_flat_dt_prop(node, "reg", &l);
270 return *addr; /* return address */
273 /* this function is looking for early uartlite console - Microblaze specific */
274 int __init early_uartlite_console(void)
276 return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
280 void __init early_init_devtree(void *params)
282 pr_debug(" -> early_init_devtree(%p)\n", params);
284 /* Setup flat device-tree pointer */
285 initial_boot_params = params;
287 #ifdef CONFIG_PHYP_DUMP
288 /* scan tree to see if dump occured during last boot */
289 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
292 /* Retrieve various informations from the /chosen node of the
293 * device-tree, including the platform type, initrd location and
294 * size, TCE reserve, and more ...
296 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
298 /* Scan memory nodes and rebuild LMBs */
300 of_scan_flat_dt(early_init_dt_scan_root, NULL);
301 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
303 /* Save command line for /proc/cmdline and then parse parameters */
304 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
309 pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
311 pr_debug("Scanning CPUs ...\n");
313 /* Retreive CPU related informations from the flat tree
314 * (altivec support, boot CPU ID, ...)
316 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
318 pr_debug(" <- early_init_devtree()\n");
323 * New implementation of the OF "find" APIs, return a refcounted
324 * object, call of_node_put() when done. The device tree and list
325 * are protected by a rw_lock.
327 * Note that property management will need some locking as well,
328 * this isn't dealt with yet.
333 * of_find_node_by_phandle - Find a node given a phandle
334 * @handle: phandle of the node to find
336 * Returns a node pointer with refcount incremented, use
337 * of_node_put() on it when done.
339 struct device_node *of_find_node_by_phandle(phandle handle)
341 struct device_node *np;
343 read_lock(&devtree_lock);
344 for (np = allnodes; np != NULL; np = np->allnext)
345 if (np->linux_phandle == handle)
348 read_unlock(&devtree_lock);
351 EXPORT_SYMBOL(of_find_node_by_phandle);
354 * Plug a device node into the tree and global list.
356 void of_attach_node(struct device_node *np)
360 write_lock_irqsave(&devtree_lock, flags);
361 np->sibling = np->parent->child;
362 np->allnext = allnodes;
363 np->parent->child = np;
365 write_unlock_irqrestore(&devtree_lock, flags);
369 * "Unplug" a node from the device tree. The caller must hold
370 * a reference to the node. The memory associated with the node
371 * is not freed until its refcount goes to zero.
373 void of_detach_node(struct device_node *np)
375 struct device_node *parent;
378 write_lock_irqsave(&devtree_lock, flags);
385 allnodes = np->allnext;
387 struct device_node *prev;
388 for (prev = allnodes;
390 prev = prev->allnext)
392 prev->allnext = np->allnext;
395 if (parent->child == np)
396 parent->child = np->sibling;
398 struct device_node *prevsib;
399 for (prevsib = np->parent->child;
400 prevsib->sibling != np;
401 prevsib = prevsib->sibling)
403 prevsib->sibling = np->sibling;
406 of_node_set_flag(np, OF_DETACHED);
409 write_unlock_irqrestore(&devtree_lock, flags);
412 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
413 static struct debugfs_blob_wrapper flat_dt_blob;
415 static int __init export_flat_device_tree(void)
419 flat_dt_blob.data = initial_boot_params;
420 flat_dt_blob.size = initial_boot_params->totalsize;
422 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
423 of_debugfs_root, &flat_dt_blob);
429 device_initcall(export_flat_device_tree);