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 static struct boot_param_header
*initial_boot_params
;
52 /* export that to outside world */
53 struct device_node
*of_chosen
;
55 static inline char *find_flat_dt_string(u32 offset
)
57 return ((char *)initial_boot_params
) +
58 initial_boot_params
->off_dt_strings
+ offset
;
62 * This function is used to scan the flattened device-tree, it is
63 * used to extract the memory informations at boot before we can
66 int __init
of_scan_flat_dt(int (*it
)(unsigned long node
,
67 const char *uname
, int depth
,
71 unsigned long p
= ((unsigned long)initial_boot_params
) +
72 initial_boot_params
->off_dt_struct
;
77 u32 tag
= *((u32
*)p
);
81 if (tag
== OF_DT_END_NODE
) {
89 if (tag
== OF_DT_PROP
) {
92 if (initial_boot_params
->version
< 0x10)
93 p
= _ALIGN(p
, sz
>= 8 ? 8 : 4);
98 if (tag
!= OF_DT_BEGIN_NODE
) {
99 printk(KERN_WARNING
"Invalid tag %x scanning flattened"
100 " device tree !\n", tag
);
105 p
= _ALIGN(p
+ strlen(pathp
) + 1, 4);
106 if ((*pathp
) == '/') {
108 for (lp
= NULL
, np
= pathp
; *np
; np
++)
114 rc
= it(p
, pathp
, depth
, data
);
122 unsigned long __init
of_get_flat_dt_root(void)
124 unsigned long p
= ((unsigned long)initial_boot_params
) +
125 initial_boot_params
->off_dt_struct
;
127 while (*((u32
*)p
) == OF_DT_NOP
)
129 BUG_ON(*((u32
*)p
) != OF_DT_BEGIN_NODE
);
131 return _ALIGN(p
+ strlen((char *)p
) + 1, 4);
135 * This function can be used within scan_flattened_dt callback to get
136 * access to properties
138 void *__init
of_get_flat_dt_prop(unsigned long node
, const char *name
,
141 unsigned long p
= node
;
144 u32 tag
= *((u32
*)p
);
149 if (tag
== OF_DT_NOP
)
151 if (tag
!= OF_DT_PROP
)
155 noff
= *((u32
*)(p
+ 4));
157 if (initial_boot_params
->version
< 0x10)
158 p
= _ALIGN(p
, sz
>= 8 ? 8 : 4);
160 nstr
= find_flat_dt_string(noff
);
162 printk(KERN_WARNING
"Can't find property index"
166 if (strcmp(name
, nstr
) == 0) {
176 int __init
of_flat_dt_is_compatible(unsigned long node
, const char *compat
)
179 unsigned long cplen
, l
;
181 cp
= of_get_flat_dt_prop(node
, "compatible", &cplen
);
185 if (strncasecmp(cp
, compat
, strlen(compat
)) == 0)
195 static void *__init
unflatten_dt_alloc(unsigned long *mem
, unsigned long size
,
200 *mem
= _ALIGN(*mem
, align
);
207 static unsigned long __init
unflatten_dt_node(unsigned long mem
,
209 struct device_node
*dad
,
210 struct device_node
***allnextpp
,
211 unsigned long fpsize
)
213 struct device_node
*np
;
214 struct property
*pp
, **prev_pp
= NULL
;
217 unsigned int l
, allocl
;
221 tag
= *((u32
*)(*p
));
222 if (tag
!= OF_DT_BEGIN_NODE
) {
223 printk("Weird tag at start of node: %x\n", tag
);
228 l
= allocl
= strlen(pathp
) + 1;
229 *p
= _ALIGN(*p
+ l
, 4);
231 /* version 0x10 has a more compact unit name here instead of the full
232 * path. we accumulate the full path size using "fpsize", we'll rebuild
233 * it later. We detect this because the first character of the name is
236 if ((*pathp
) != '/') {
239 /* root node: special case. fpsize accounts for path
240 * plus terminating zero. root node only has '/', so
241 * fpsize should be 2, but we want to avoid the first
242 * level nodes to have two '/' so we use fpsize 1 here
247 /* account for '/' and path size minus terminal 0
255 np
= unflatten_dt_alloc(&mem
, sizeof(struct device_node
) + allocl
,
256 __alignof__(struct device_node
));
258 memset(np
, 0, sizeof(*np
));
259 np
->full_name
= ((char *)np
) + sizeof(struct device_node
);
261 char *p2
= np
->full_name
;
262 /* rebuild full path for new format */
263 if (dad
&& dad
->parent
) {
264 strcpy(p2
, dad
->full_name
);
266 if ((strlen(p2
) + l
+ 1) != allocl
) {
267 pr_debug("%s: p: %d, l: %d, a: %d\n",
268 pathp
, (int)strlen(p2
),
275 memcpy(p2
, pathp
, l
);
277 memcpy(np
->full_name
, pathp
, l
);
278 prev_pp
= &np
->properties
;
280 *allnextpp
= &np
->allnext
;
283 /* we temporarily use the next field as `last_child'*/
284 if (dad
->next
== NULL
)
287 dad
->next
->sibling
= np
;
290 kref_init(&np
->kref
);
296 tag
= *((u32
*)(*p
));
297 if (tag
== OF_DT_NOP
) {
301 if (tag
!= OF_DT_PROP
)
305 noff
= *((u32
*)((*p
) + 4));
307 if (initial_boot_params
->version
< 0x10)
308 *p
= _ALIGN(*p
, sz
>= 8 ? 8 : 4);
310 pname
= find_flat_dt_string(noff
);
313 "Can't find property name in list !\n");
316 if (strcmp(pname
, "name") == 0)
318 l
= strlen(pname
) + 1;
319 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
320 __alignof__(struct property
));
322 if (strcmp(pname
, "linux,phandle") == 0) {
323 np
->node
= *((u32
*)*p
);
324 if (np
->linux_phandle
== 0)
325 np
->linux_phandle
= np
->node
;
327 if (strcmp(pname
, "ibm,phandle") == 0)
328 np
->linux_phandle
= *((u32
*)*p
);
331 pp
->value
= (void *)*p
;
335 *p
= _ALIGN((*p
) + sz
, 4);
337 /* with version 0x10 we may not have the name property, recreate
338 * it here from the unit name if absent
341 char *p1
= pathp
, *ps
= pathp
, *pa
= NULL
;
354 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
) + sz
,
355 __alignof__(struct property
));
362 memcpy(pp
->value
, ps
, sz
- 1);
363 ((char *)pp
->value
)[sz
- 1] = 0;
364 pr_debug("fixed up name for %s -> %s\n", pathp
,
370 np
->name
= of_get_property(np
, "name", NULL
);
371 np
->type
= of_get_property(np
, "device_type", NULL
);
378 while (tag
== OF_DT_BEGIN_NODE
) {
379 mem
= unflatten_dt_node(mem
, p
, np
, allnextpp
, fpsize
);
380 tag
= *((u32
*)(*p
));
382 if (tag
!= OF_DT_END_NODE
) {
383 printk(KERN_INFO
"Weird tag at end of node: %x\n", tag
);
391 * unflattens the device-tree passed by the firmware, creating the
392 * tree of struct device_node. It also fills the "name" and "type"
393 * pointers of the nodes so the normal device-tree walking functions
394 * can be used (this used to be done by finish_device_tree)
396 void __init
unflatten_device_tree(void)
398 unsigned long start
, mem
, size
;
399 struct device_node
**allnextp
= &allnodes
;
401 pr_debug(" -> unflatten_device_tree()\n");
403 /* First pass, scan for size */
404 start
= ((unsigned long)initial_boot_params
) +
405 initial_boot_params
->off_dt_struct
;
406 size
= unflatten_dt_node(0, &start
, NULL
, NULL
, 0);
407 size
= (size
| 3) + 1;
409 pr_debug(" size is %lx, allocating...\n", size
);
411 /* Allocate memory for the expanded device tree */
412 mem
= lmb_alloc(size
+ 4, __alignof__(struct device_node
));
413 mem
= (unsigned long) __va(mem
);
415 ((u32
*)mem
)[size
/ 4] = 0xdeadbeef;
417 pr_debug(" unflattening %lx...\n", mem
);
419 /* Second pass, do actual unflattening */
420 start
= ((unsigned long)initial_boot_params
) +
421 initial_boot_params
->off_dt_struct
;
422 unflatten_dt_node(mem
, &start
, NULL
, &allnextp
, 0);
423 if (*((u32
*)start
) != OF_DT_END
)
424 printk(KERN_WARNING
"Weird tag at end of tree: %08x\n",
426 if (((u32
*)mem
)[size
/ 4] != 0xdeadbeef)
427 printk(KERN_WARNING
"End of tree marker overwritten: %08x\n",
428 ((u32
*)mem
)[size
/ 4]);
431 /* Get pointer to OF "/chosen" node for use everywhere */
432 of_chosen
= of_find_node_by_path("/chosen");
433 if (of_chosen
== NULL
)
434 of_chosen
= of_find_node_by_path("/chosen@0");
436 pr_debug(" <- unflatten_device_tree()\n");
439 #define early_init_dt_scan_drconf_memory(node) 0
441 static int __init
early_init_dt_scan_cpus(unsigned long node
,
442 const char *uname
, int depth
,
445 static int logical_cpuid
;
446 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
451 /* We are scanning "cpu" nodes only */
452 if (type
== NULL
|| strcmp(type
, "cpu") != 0)
455 /* Get physical cpuid */
456 intserv
= of_get_flat_dt_prop(node
, "reg", NULL
);
460 * Now see if any of these threads match our boot cpu.
461 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
463 for (i
= 0; i
< nthreads
; i
++) {
465 * version 2 of the kexec param format adds the phys cpuid of
468 if (initial_boot_params
&& initial_boot_params
->version
>= 2) {
470 initial_boot_params
->boot_cpuid_phys
) {
476 * Check if it's the boot-cpu, set it's hw index now,
477 * unfortunately this format did not support booting
478 * off secondary threads.
480 if (of_get_flat_dt_prop(node
,
481 "linux,boot-cpu", NULL
) != NULL
) {
488 /* logical cpu id is always 0 on UP kernels */
494 pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid
,
496 boot_cpuid
= logical_cpuid
;
502 #ifdef CONFIG_BLK_DEV_INITRD
503 static void __init
early_init_dt_check_for_initrd(unsigned long node
)
508 pr_debug("Looking for initrd properties... ");
510 prop
= of_get_flat_dt_prop(node
, "linux,initrd-start", &l
);
512 initrd_start
= (unsigned long)
513 __va((u32
)of_read_ulong(prop
, l
/4));
515 prop
= of_get_flat_dt_prop(node
, "linux,initrd-end", &l
);
517 initrd_end
= (unsigned long)
518 __va((u32
)of_read_ulong(prop
, 1/4));
519 initrd_below_start_ok
= 1;
525 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
526 initrd_start
, initrd_end
);
529 static inline void early_init_dt_check_for_initrd(unsigned long node
)
532 #endif /* CONFIG_BLK_DEV_INITRD */
534 static int __init
early_init_dt_scan_chosen(unsigned long node
,
535 const char *uname
, int depth
, void *data
)
540 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
543 (strcmp(uname
, "chosen") != 0 &&
544 strcmp(uname
, "chosen@0") != 0))
548 lprop
= (u64
*)of_get_flat_dt_prop(node
,
549 "linux,crashkernel-base", NULL
);
551 crashk_res
.start
= *lprop
;
553 lprop
= (u64
*)of_get_flat_dt_prop(node
,
554 "linux,crashkernel-size", NULL
);
556 crashk_res
.end
= crashk_res
.start
+ *lprop
- 1;
559 early_init_dt_check_for_initrd(node
);
561 /* Retreive command line */
562 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
563 if (p
!= NULL
&& l
> 0)
564 strlcpy(cmd_line
, p
, min((int)l
, COMMAND_LINE_SIZE
));
566 #ifdef CONFIG_CMDLINE
567 #ifndef CONFIG_CMDLINE_FORCE
568 if (p
== NULL
|| l
== 0 || (l
== 1 && (*p
) == 0))
570 strlcpy(cmd_line
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
571 #endif /* CONFIG_CMDLINE */
573 pr_debug("Command line is: %s\n", cmd_line
);
579 static int __init
early_init_dt_scan_root(unsigned long node
,
580 const char *uname
, int depth
, void *data
)
587 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
588 dt_root_size_cells
= (prop
== NULL
) ? 1 : *prop
;
589 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells
);
591 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
592 dt_root_addr_cells
= (prop
== NULL
) ? 2 : *prop
;
593 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells
);
599 static u64 __init
dt_mem_next_cell(int s
, cell_t
**cellp
)
604 return of_read_number(p
, s
);
607 static int __init
early_init_dt_scan_memory(unsigned long node
,
608 const char *uname
, int depth
, void *data
)
610 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
614 /* Look for the ibm,dynamic-reconfiguration-memory node */
616 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
617 return early_init_dt_scan_drconf_memory(node);
619 /* We are scanning "memory" nodes only */
622 * The longtrail doesn't have a device_type on the
623 * /memory node, so look for the node called /memory@0.
625 if (depth
!= 1 || strcmp(uname
, "memory@0") != 0)
627 } else if (strcmp(type
, "memory") != 0)
630 reg
= (cell_t
*)of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
632 reg
= (cell_t
*)of_get_flat_dt_prop(node
, "reg", &l
);
636 endp
= reg
+ (l
/ sizeof(cell_t
));
638 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
639 uname
, l
, reg
[0], reg
[1], reg
[2], reg
[3]);
641 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
644 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
645 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
649 pr_debug(" - %llx , %llx\n", (unsigned long long)base
,
650 (unsigned long long)size
);
657 #ifdef CONFIG_PHYP_DUMP
659 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
661 * Function to find the largest size we need to reserve
662 * during early boot process.
664 * It either looks for boot param and returns that OR
665 * returns larger of 256 or 5% rounded down to multiples of 256MB.
668 static inline unsigned long phyp_dump_calculate_reserve_size(void)
672 if (phyp_dump_info
->reserve_bootvar
)
673 return phyp_dump_info
->reserve_bootvar
;
675 /* divide by 20 to get 5% of value */
676 tmp
= lmb_end_of_DRAM();
679 /* round it down in multiples of 256 */
680 tmp
= tmp
& ~0x0FFFFFFFUL
;
682 return (tmp
> PHYP_DUMP_RMR_END
? tmp
: PHYP_DUMP_RMR_END
);
686 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
688 * This routine may reserve memory regions in the kernel only
689 * if the system is supported and a dump was taken in last
690 * boot instance or if the hardware is supported and the
691 * scratch area needs to be setup. In other instances it returns
692 * without reserving anything. The memory in case of dump being
693 * active is freed when the dump is collected (by userland tools).
695 static void __init
phyp_dump_reserve_mem(void)
697 unsigned long base
, size
;
698 unsigned long variable_reserve_size
;
700 if (!phyp_dump_info
->phyp_dump_configured
) {
701 printk(KERN_ERR
"Phyp-dump not supported on this hardware\n");
705 if (!phyp_dump_info
->phyp_dump_at_boot
) {
706 printk(KERN_INFO
"Phyp-dump disabled at boot time\n");
710 variable_reserve_size
= phyp_dump_calculate_reserve_size();
712 if (phyp_dump_info
->phyp_dump_is_active
) {
713 /* Reserve *everything* above RMR.Area freed by userland tools*/
714 base
= variable_reserve_size
;
715 size
= lmb_end_of_DRAM() - base
;
717 /* XXX crashed_ram_end is wrong, since it may be beyond
718 * the memory_limit, it will need to be adjusted. */
719 lmb_reserve(base
, size
);
721 phyp_dump_info
->init_reserve_start
= base
;
722 phyp_dump_info
->init_reserve_size
= size
;
724 size
= phyp_dump_info
->cpu_state_size
+
725 phyp_dump_info
->hpte_region_size
+
726 variable_reserve_size
;
727 base
= lmb_end_of_DRAM() - size
;
728 lmb_reserve(base
, size
);
729 phyp_dump_info
->init_reserve_start
= base
;
730 phyp_dump_info
->init_reserve_size
= size
;
734 static inline void __init
phyp_dump_reserve_mem(void) {}
735 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
737 #ifdef CONFIG_EARLY_PRINTK
738 /* MS this is Microblaze specifig function */
739 static int __init
early_init_dt_scan_serial(unsigned long node
,
740 const char *uname
, int depth
, void *data
)
746 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
748 /* find all serial nodes */
749 if (strncmp(uname
, "serial", 6) != 0)
752 early_init_dt_check_for_initrd(node
);
754 /* find compatible node with uartlite */
755 p
= of_get_flat_dt_prop(node
, "compatible", &l
);
756 if ((strncmp(p
, "xlnx,xps-uartlite", 17) != 0) &&
757 (strncmp(p
, "xlnx,opb-uartlite", 17) != 0))
760 addr
= of_get_flat_dt_prop(node
, "reg", &l
);
761 return *addr
; /* return address */
764 /* this function is looking for early uartlite console - Microblaze specific */
765 int __init
early_uartlite_console(void)
767 return of_scan_flat_dt(early_init_dt_scan_serial
, NULL
);
771 void __init
early_init_devtree(void *params
)
773 pr_debug(" -> early_init_devtree(%p)\n", params
);
775 /* Setup flat device-tree pointer */
776 initial_boot_params
= params
;
778 #ifdef CONFIG_PHYP_DUMP
779 /* scan tree to see if dump occured during last boot */
780 of_scan_flat_dt(early_init_dt_scan_phyp_dump
, NULL
);
783 /* Retrieve various informations from the /chosen node of the
784 * device-tree, including the platform type, initrd location and
785 * size, TCE reserve, and more ...
787 of_scan_flat_dt(early_init_dt_scan_chosen
, NULL
);
789 /* Scan memory nodes and rebuild LMBs */
791 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
792 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
794 /* Save command line for /proc/cmdline and then parse parameters */
795 strlcpy(boot_command_line
, cmd_line
, COMMAND_LINE_SIZE
);
800 pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
802 pr_debug("Scanning CPUs ...\n");
804 /* Retreive CPU related informations from the flat tree
805 * (altivec support, boot CPU ID, ...)
807 of_scan_flat_dt(early_init_dt_scan_cpus
, NULL
);
809 pr_debug(" <- early_init_devtree()\n");
813 * Indicates whether the root node has a given value in its
814 * compatible property.
816 int machine_is_compatible(const char *compat
)
818 struct device_node
*root
;
821 root
= of_find_node_by_path("/");
823 rc
= of_device_is_compatible(root
, compat
);
828 EXPORT_SYMBOL(machine_is_compatible
);
832 * New implementation of the OF "find" APIs, return a refcounted
833 * object, call of_node_put() when done. The device tree and list
834 * are protected by a rw_lock.
836 * Note that property management will need some locking as well,
837 * this isn't dealt with yet.
842 * of_find_node_by_phandle - Find a node given a phandle
843 * @handle: phandle of the node to find
845 * Returns a node pointer with refcount incremented, use
846 * of_node_put() on it when done.
848 struct device_node
*of_find_node_by_phandle(phandle handle
)
850 struct device_node
*np
;
852 read_lock(&devtree_lock
);
853 for (np
= allnodes
; np
!= NULL
; np
= np
->allnext
)
854 if (np
->linux_phandle
== handle
)
857 read_unlock(&devtree_lock
);
860 EXPORT_SYMBOL(of_find_node_by_phandle
);
863 * of_find_all_nodes - Get next node in global list
864 * @prev: Previous node or NULL to start iteration
865 * of_node_put() will be called on it
867 * Returns a node pointer with refcount incremented, use
868 * of_node_put() on it when done.
870 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
872 struct device_node
*np
;
874 read_lock(&devtree_lock
);
875 np
= prev
? prev
->allnext
: allnodes
;
876 for (; np
!= NULL
; np
= np
->allnext
)
880 read_unlock(&devtree_lock
);
883 EXPORT_SYMBOL(of_find_all_nodes
);
886 * of_node_get - Increment refcount of a node
887 * @node: Node to inc refcount, NULL is supported to
888 * simplify writing of callers
892 struct device_node
*of_node_get(struct device_node
*node
)
895 kref_get(&node
->kref
);
898 EXPORT_SYMBOL(of_node_get
);
900 static inline struct device_node
*kref_to_device_node(struct kref
*kref
)
902 return container_of(kref
, struct device_node
, kref
);
906 * of_node_release - release a dynamically allocated node
907 * @kref: kref element of the node to be released
909 * In of_node_put() this function is passed to kref_put()
912 static void of_node_release(struct kref
*kref
)
914 struct device_node
*node
= kref_to_device_node(kref
);
915 struct property
*prop
= node
->properties
;
917 /* We should never be releasing nodes that haven't been detached. */
918 if (!of_node_check_flag(node
, OF_DETACHED
)) {
919 printk(KERN_INFO
"WARNING: Bad of_node_put() on %s\n",
922 kref_init(&node
->kref
);
926 if (!of_node_check_flag(node
, OF_DYNAMIC
))
930 struct property
*next
= prop
->next
;
937 prop
= node
->deadprops
;
938 node
->deadprops
= NULL
;
941 kfree(node
->full_name
);
947 * of_node_put - Decrement refcount of a node
948 * @node: Node to dec refcount, NULL is supported to
949 * simplify writing of callers
952 void of_node_put(struct device_node
*node
)
955 kref_put(&node
->kref
, of_node_release
);
957 EXPORT_SYMBOL(of_node_put
);
960 * Plug a device node into the tree and global list.
962 void of_attach_node(struct device_node
*np
)
966 write_lock_irqsave(&devtree_lock
, flags
);
967 np
->sibling
= np
->parent
->child
;
968 np
->allnext
= allnodes
;
969 np
->parent
->child
= np
;
971 write_unlock_irqrestore(&devtree_lock
, flags
);
975 * "Unplug" a node from the device tree. The caller must hold
976 * a reference to the node. The memory associated with the node
977 * is not freed until its refcount goes to zero.
979 void of_detach_node(struct device_node
*np
)
981 struct device_node
*parent
;
984 write_lock_irqsave(&devtree_lock
, flags
);
991 allnodes
= np
->allnext
;
993 struct device_node
*prev
;
994 for (prev
= allnodes
;
996 prev
= prev
->allnext
)
998 prev
->allnext
= np
->allnext
;
1001 if (parent
->child
== np
)
1002 parent
->child
= np
->sibling
;
1004 struct device_node
*prevsib
;
1005 for (prevsib
= np
->parent
->child
;
1006 prevsib
->sibling
!= np
;
1007 prevsib
= prevsib
->sibling
)
1009 prevsib
->sibling
= np
->sibling
;
1012 of_node_set_flag(np
, OF_DETACHED
);
1015 write_unlock_irqrestore(&devtree_lock
, flags
);
1019 * Add a property to a node
1021 int prom_add_property(struct device_node
*np
, struct property
*prop
)
1023 struct property
**next
;
1024 unsigned long flags
;
1027 write_lock_irqsave(&devtree_lock
, flags
);
1028 next
= &np
->properties
;
1030 if (strcmp(prop
->name
, (*next
)->name
) == 0) {
1031 /* duplicate ! don't insert it */
1032 write_unlock_irqrestore(&devtree_lock
, flags
);
1035 next
= &(*next
)->next
;
1038 write_unlock_irqrestore(&devtree_lock
, flags
);
1040 #ifdef CONFIG_PROC_DEVICETREE
1041 /* try to add to proc as well if it was initialized */
1043 proc_device_tree_add_prop(np
->pde
, prop
);
1044 #endif /* CONFIG_PROC_DEVICETREE */
1050 * Remove a property from a node. Note that we don't actually
1051 * remove it, since we have given out who-knows-how-many pointers
1052 * to the data using get-property. Instead we just move the property
1053 * to the "dead properties" list, so it won't be found any more.
1055 int prom_remove_property(struct device_node
*np
, struct property
*prop
)
1057 struct property
**next
;
1058 unsigned long flags
;
1061 write_lock_irqsave(&devtree_lock
, flags
);
1062 next
= &np
->properties
;
1064 if (*next
== prop
) {
1065 /* found the node */
1067 prop
->next
= np
->deadprops
;
1068 np
->deadprops
= prop
;
1072 next
= &(*next
)->next
;
1074 write_unlock_irqrestore(&devtree_lock
, flags
);
1079 #ifdef CONFIG_PROC_DEVICETREE
1080 /* try to remove the proc node as well */
1082 proc_device_tree_remove_prop(np
->pde
, prop
);
1083 #endif /* CONFIG_PROC_DEVICETREE */
1089 * Update a property in a node. Note that we don't actually
1090 * remove it, since we have given out who-knows-how-many pointers
1091 * to the data using get-property. Instead we just move the property
1092 * to the "dead properties" list, and add the new property to the
1095 int prom_update_property(struct device_node
*np
,
1096 struct property
*newprop
,
1097 struct property
*oldprop
)
1099 struct property
**next
;
1100 unsigned long flags
;
1103 write_lock_irqsave(&devtree_lock
, flags
);
1104 next
= &np
->properties
;
1106 if (*next
== oldprop
) {
1107 /* found the node */
1108 newprop
->next
= oldprop
->next
;
1110 oldprop
->next
= np
->deadprops
;
1111 np
->deadprops
= oldprop
;
1115 next
= &(*next
)->next
;
1117 write_unlock_irqrestore(&devtree_lock
, flags
);
1122 #ifdef CONFIG_PROC_DEVICETREE
1123 /* try to add to proc as well if it was initialized */
1125 proc_device_tree_update_prop(np
->pde
, newprop
, oldprop
);
1126 #endif /* CONFIG_PROC_DEVICETREE */
1131 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1132 static struct debugfs_blob_wrapper flat_dt_blob
;
1134 static int __init
export_flat_device_tree(void)
1138 flat_dt_blob
.data
= initial_boot_params
;
1139 flat_dt_blob
.size
= initial_boot_params
->totalsize
;
1141 d
= debugfs_create_blob("flat-device-tree", S_IFREG
| S_IRUSR
,
1142 of_debugfs_root
, &flat_dt_blob
);
1148 device_initcall(export_flat_device_tree
);