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 u64 __init
dt_mem_next_cell(int s
, cell_t
**cellp
)
163 return of_read_number(p
, s
);
166 static int __init
early_init_dt_scan_memory(unsigned long node
,
167 const char *uname
, int depth
, void *data
)
169 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
173 /* Look for the ibm,dynamic-reconfiguration-memory node */
175 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
176 return early_init_dt_scan_drconf_memory(node);
178 /* We are scanning "memory" nodes only */
181 * The longtrail doesn't have a device_type on the
182 * /memory node, so look for the node called /memory@0.
184 if (depth
!= 1 || strcmp(uname
, "memory@0") != 0)
186 } else if (strcmp(type
, "memory") != 0)
189 reg
= (cell_t
*)of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
191 reg
= (cell_t
*)of_get_flat_dt_prop(node
, "reg", &l
);
195 endp
= reg
+ (l
/ sizeof(cell_t
));
197 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
198 uname
, l
, reg
[0], reg
[1], reg
[2], reg
[3]);
200 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
203 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
204 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
208 pr_debug(" - %llx , %llx\n", (unsigned long long)base
,
209 (unsigned long long)size
);
216 #ifdef CONFIG_PHYP_DUMP
218 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
220 * Function to find the largest size we need to reserve
221 * during early boot process.
223 * It either looks for boot param and returns that OR
224 * returns larger of 256 or 5% rounded down to multiples of 256MB.
227 static inline unsigned long phyp_dump_calculate_reserve_size(void)
231 if (phyp_dump_info
->reserve_bootvar
)
232 return phyp_dump_info
->reserve_bootvar
;
234 /* divide by 20 to get 5% of value */
235 tmp
= lmb_end_of_DRAM();
238 /* round it down in multiples of 256 */
239 tmp
= tmp
& ~0x0FFFFFFFUL
;
241 return (tmp
> PHYP_DUMP_RMR_END
? tmp
: PHYP_DUMP_RMR_END
);
245 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
247 * This routine may reserve memory regions in the kernel only
248 * if the system is supported and a dump was taken in last
249 * boot instance or if the hardware is supported and the
250 * scratch area needs to be setup. In other instances it returns
251 * without reserving anything. The memory in case of dump being
252 * active is freed when the dump is collected (by userland tools).
254 static void __init
phyp_dump_reserve_mem(void)
256 unsigned long base
, size
;
257 unsigned long variable_reserve_size
;
259 if (!phyp_dump_info
->phyp_dump_configured
) {
260 printk(KERN_ERR
"Phyp-dump not supported on this hardware\n");
264 if (!phyp_dump_info
->phyp_dump_at_boot
) {
265 printk(KERN_INFO
"Phyp-dump disabled at boot time\n");
269 variable_reserve_size
= phyp_dump_calculate_reserve_size();
271 if (phyp_dump_info
->phyp_dump_is_active
) {
272 /* Reserve *everything* above RMR.Area freed by userland tools*/
273 base
= variable_reserve_size
;
274 size
= lmb_end_of_DRAM() - base
;
276 /* XXX crashed_ram_end is wrong, since it may be beyond
277 * the memory_limit, it will need to be adjusted. */
278 lmb_reserve(base
, size
);
280 phyp_dump_info
->init_reserve_start
= base
;
281 phyp_dump_info
->init_reserve_size
= size
;
283 size
= phyp_dump_info
->cpu_state_size
+
284 phyp_dump_info
->hpte_region_size
+
285 variable_reserve_size
;
286 base
= lmb_end_of_DRAM() - size
;
287 lmb_reserve(base
, size
);
288 phyp_dump_info
->init_reserve_start
= base
;
289 phyp_dump_info
->init_reserve_size
= size
;
293 static inline void __init
phyp_dump_reserve_mem(void) {}
294 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
296 #ifdef CONFIG_EARLY_PRINTK
297 /* MS this is Microblaze specifig function */
298 static int __init
early_init_dt_scan_serial(unsigned long node
,
299 const char *uname
, int depth
, void *data
)
305 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
307 /* find all serial nodes */
308 if (strncmp(uname
, "serial", 6) != 0)
311 early_init_dt_check_for_initrd(node
);
313 /* find compatible node with uartlite */
314 p
= of_get_flat_dt_prop(node
, "compatible", &l
);
315 if ((strncmp(p
, "xlnx,xps-uartlite", 17) != 0) &&
316 (strncmp(p
, "xlnx,opb-uartlite", 17) != 0))
319 addr
= of_get_flat_dt_prop(node
, "reg", &l
);
320 return *addr
; /* return address */
323 /* this function is looking for early uartlite console - Microblaze specific */
324 int __init
early_uartlite_console(void)
326 return of_scan_flat_dt(early_init_dt_scan_serial
, NULL
);
330 void __init
early_init_devtree(void *params
)
332 pr_debug(" -> early_init_devtree(%p)\n", params
);
334 /* Setup flat device-tree pointer */
335 initial_boot_params
= params
;
337 #ifdef CONFIG_PHYP_DUMP
338 /* scan tree to see if dump occured during last boot */
339 of_scan_flat_dt(early_init_dt_scan_phyp_dump
, NULL
);
342 /* Retrieve various informations from the /chosen node of the
343 * device-tree, including the platform type, initrd location and
344 * size, TCE reserve, and more ...
346 of_scan_flat_dt(early_init_dt_scan_chosen
, NULL
);
348 /* Scan memory nodes and rebuild LMBs */
350 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
351 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
353 /* Save command line for /proc/cmdline and then parse parameters */
354 strlcpy(boot_command_line
, cmd_line
, COMMAND_LINE_SIZE
);
359 pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
361 pr_debug("Scanning CPUs ...\n");
363 /* Retreive CPU related informations from the flat tree
364 * (altivec support, boot CPU ID, ...)
366 of_scan_flat_dt(early_init_dt_scan_cpus
, NULL
);
368 pr_debug(" <- early_init_devtree()\n");
372 * Indicates whether the root node has a given value in its
373 * compatible property.
375 int machine_is_compatible(const char *compat
)
377 struct device_node
*root
;
380 root
= of_find_node_by_path("/");
382 rc
= of_device_is_compatible(root
, compat
);
387 EXPORT_SYMBOL(machine_is_compatible
);
391 * New implementation of the OF "find" APIs, return a refcounted
392 * object, call of_node_put() when done. The device tree and list
393 * are protected by a rw_lock.
395 * Note that property management will need some locking as well,
396 * this isn't dealt with yet.
401 * of_find_node_by_phandle - Find a node given a phandle
402 * @handle: phandle of the node to find
404 * Returns a node pointer with refcount incremented, use
405 * of_node_put() on it when done.
407 struct device_node
*of_find_node_by_phandle(phandle handle
)
409 struct device_node
*np
;
411 read_lock(&devtree_lock
);
412 for (np
= allnodes
; np
!= NULL
; np
= np
->allnext
)
413 if (np
->linux_phandle
== handle
)
416 read_unlock(&devtree_lock
);
419 EXPORT_SYMBOL(of_find_node_by_phandle
);
422 * of_node_get - Increment refcount of a node
423 * @node: Node to inc refcount, NULL is supported to
424 * simplify writing of callers
428 struct device_node
*of_node_get(struct device_node
*node
)
431 kref_get(&node
->kref
);
434 EXPORT_SYMBOL(of_node_get
);
436 static inline struct device_node
*kref_to_device_node(struct kref
*kref
)
438 return container_of(kref
, struct device_node
, kref
);
442 * of_node_release - release a dynamically allocated node
443 * @kref: kref element of the node to be released
445 * In of_node_put() this function is passed to kref_put()
448 static void of_node_release(struct kref
*kref
)
450 struct device_node
*node
= kref_to_device_node(kref
);
451 struct property
*prop
= node
->properties
;
453 /* We should never be releasing nodes that haven't been detached. */
454 if (!of_node_check_flag(node
, OF_DETACHED
)) {
455 printk(KERN_INFO
"WARNING: Bad of_node_put() on %s\n",
458 kref_init(&node
->kref
);
462 if (!of_node_check_flag(node
, OF_DYNAMIC
))
466 struct property
*next
= prop
->next
;
473 prop
= node
->deadprops
;
474 node
->deadprops
= NULL
;
477 kfree(node
->full_name
);
483 * of_node_put - Decrement refcount of a node
484 * @node: Node to dec refcount, NULL is supported to
485 * simplify writing of callers
488 void of_node_put(struct device_node
*node
)
491 kref_put(&node
->kref
, of_node_release
);
493 EXPORT_SYMBOL(of_node_put
);
496 * Plug a device node into the tree and global list.
498 void of_attach_node(struct device_node
*np
)
502 write_lock_irqsave(&devtree_lock
, flags
);
503 np
->sibling
= np
->parent
->child
;
504 np
->allnext
= allnodes
;
505 np
->parent
->child
= np
;
507 write_unlock_irqrestore(&devtree_lock
, flags
);
511 * "Unplug" a node from the device tree. The caller must hold
512 * a reference to the node. The memory associated with the node
513 * is not freed until its refcount goes to zero.
515 void of_detach_node(struct device_node
*np
)
517 struct device_node
*parent
;
520 write_lock_irqsave(&devtree_lock
, flags
);
527 allnodes
= np
->allnext
;
529 struct device_node
*prev
;
530 for (prev
= allnodes
;
532 prev
= prev
->allnext
)
534 prev
->allnext
= np
->allnext
;
537 if (parent
->child
== np
)
538 parent
->child
= np
->sibling
;
540 struct device_node
*prevsib
;
541 for (prevsib
= np
->parent
->child
;
542 prevsib
->sibling
!= np
;
543 prevsib
= prevsib
->sibling
)
545 prevsib
->sibling
= np
->sibling
;
548 of_node_set_flag(np
, OF_DETACHED
);
551 write_unlock_irqrestore(&devtree_lock
, flags
);
554 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
555 static struct debugfs_blob_wrapper flat_dt_blob
;
557 static int __init
export_flat_device_tree(void)
561 flat_dt_blob
.data
= initial_boot_params
;
562 flat_dt_blob
.size
= initial_boot_params
->totalsize
;
564 d
= debugfs_create_blob("flat-device-tree", S_IFREG
| S_IRUSR
,
565 of_debugfs_root
, &flat_dt_blob
);
571 device_initcall(export_flat_device_tree
);