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.
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/threads.h>
24 #include <linux/spinlock.h>
25 #include <linux/types.h>
26 #include <linux/pci.h>
27 #include <linux/stringify.h>
28 #include <linux/delay.h>
29 #include <linux/initrd.h>
30 #include <linux/bitops.h>
31 #include <linux/module.h>
32 #include <linux/kexec.h>
33 #include <linux/debugfs.h>
39 #include <asm/processor.h>
42 #include <asm/kdump.h>
44 #include <asm/system.h>
46 #include <asm/pgtable.h>
48 #include <asm/iommu.h>
49 #include <asm/btext.h>
50 #include <asm/sections.h>
51 #include <asm/machdep.h>
52 #include <asm/pSeries_reconfig.h>
53 #include <asm/pci-bridge.h>
54 #include <asm/kexec.h>
57 #define DBG(fmt...) printk(KERN_ERR fmt)
63 static int __initdata dt_root_addr_cells
;
64 static int __initdata dt_root_size_cells
;
67 int __initdata iommu_is_off
;
68 int __initdata iommu_force_on
;
69 unsigned long tce_alloc_start
, tce_alloc_end
;
75 static struct boot_param_header
*initial_boot_params __initdata
;
77 struct boot_param_header
*initial_boot_params
;
80 static struct device_node
*allnodes
= NULL
;
82 /* use when traversing tree through the allnext, child, sibling,
83 * or parent members of struct device_node.
85 static DEFINE_RWLOCK(devtree_lock
);
87 /* export that to outside world */
88 struct device_node
*of_chosen
;
90 struct device_node
*dflt_interrupt_controller
;
91 int num_interrupt_controllers
;
94 * Wrapper for allocating memory for various data that needs to be
95 * attached to device nodes as they are processed at boot or when
96 * added to the device tree later (e.g. DLPAR). At boot there is
97 * already a region reserved so we just increment *mem_start by size;
98 * otherwise we call kmalloc.
100 static void * prom_alloc(unsigned long size
, unsigned long *mem_start
)
105 return kmalloc(size
, GFP_KERNEL
);
113 * Find the device_node with a given phandle.
115 static struct device_node
* find_phandle(phandle ph
)
117 struct device_node
*np
;
119 for (np
= allnodes
; np
!= 0; np
= np
->allnext
)
120 if (np
->linux_phandle
== ph
)
126 * Find the interrupt parent of a node.
128 static struct device_node
* __devinit
intr_parent(struct device_node
*p
)
132 parp
= (phandle
*) get_property(p
, "interrupt-parent", NULL
);
135 p
= find_phandle(*parp
);
139 * On a powermac booted with BootX, we don't get to know the
140 * phandles for any nodes, so find_phandle will return NULL.
141 * Fortunately these machines only have one interrupt controller
142 * so there isn't in fact any ambiguity. -- paulus
144 if (num_interrupt_controllers
== 1)
145 p
= dflt_interrupt_controller
;
150 * Find out the size of each entry of the interrupts property
153 int __devinit
prom_n_intr_cells(struct device_node
*np
)
155 struct device_node
*p
;
158 for (p
= np
; (p
= intr_parent(p
)) != NULL
; ) {
159 icp
= (unsigned int *)
160 get_property(p
, "#interrupt-cells", NULL
);
163 if (get_property(p
, "interrupt-controller", NULL
) != NULL
164 || get_property(p
, "interrupt-map", NULL
) != NULL
) {
165 printk("oops, node %s doesn't have #interrupt-cells\n",
171 printk("prom_n_intr_cells failed for %s\n", np
->full_name
);
177 * Map an interrupt from a device up to the platform interrupt
180 static int __devinit
map_interrupt(unsigned int **irq
, struct device_node
**ictrler
,
181 struct device_node
*np
, unsigned int *ints
,
184 struct device_node
*p
, *ipar
;
185 unsigned int *imap
, *imask
, *ip
;
186 int i
, imaplen
, match
;
187 int newintrc
= 0, newaddrc
= 0;
191 reg
= (unsigned int *) get_property(np
, "reg", NULL
);
192 naddrc
= prom_n_addr_cells(np
);
195 if (get_property(p
, "interrupt-controller", NULL
) != NULL
)
196 /* this node is an interrupt controller, stop here */
198 imap
= (unsigned int *)
199 get_property(p
, "interrupt-map", &imaplen
);
204 imask
= (unsigned int *)
205 get_property(p
, "interrupt-map-mask", NULL
);
207 printk("oops, %s has interrupt-map but no mask\n",
211 imaplen
/= sizeof(unsigned int);
214 while (imaplen
> 0 && !match
) {
215 /* check the child-interrupt field */
217 for (i
= 0; i
< naddrc
&& match
; ++i
)
218 match
= ((reg
[i
] ^ imap
[i
]) & imask
[i
]) == 0;
219 for (; i
< naddrc
+ nintrc
&& match
; ++i
)
220 match
= ((ints
[i
-naddrc
] ^ imap
[i
]) & imask
[i
]) == 0;
221 imap
+= naddrc
+ nintrc
;
222 imaplen
-= naddrc
+ nintrc
;
223 /* grab the interrupt parent */
224 ipar
= find_phandle((phandle
) *imap
++);
226 if (ipar
== NULL
&& num_interrupt_controllers
== 1)
227 /* cope with BootX not giving us phandles */
228 ipar
= dflt_interrupt_controller
;
230 printk("oops, no int parent %x in map of %s\n",
231 imap
[-1], p
->full_name
);
234 /* find the parent's # addr and intr cells */
235 ip
= (unsigned int *)
236 get_property(ipar
, "#interrupt-cells", NULL
);
238 printk("oops, no #interrupt-cells on %s\n",
243 ip
= (unsigned int *)
244 get_property(ipar
, "#address-cells", NULL
);
245 newaddrc
= (ip
== NULL
)? 0: *ip
;
246 imap
+= newaddrc
+ newintrc
;
247 imaplen
-= newaddrc
+ newintrc
;
250 printk("oops, error decoding int-map on %s, len=%d\n",
251 p
->full_name
, imaplen
);
256 printk("oops, no match in %s int-map for %s\n",
257 p
->full_name
, np
->full_name
);
264 ints
= imap
- nintrc
;
269 printk("hmmm, int tree for %s doesn't have ctrler\n",
279 static unsigned char map_isa_senses
[4] = {
280 IRQ_SENSE_LEVEL
| IRQ_POLARITY_NEGATIVE
,
281 IRQ_SENSE_LEVEL
| IRQ_POLARITY_POSITIVE
,
282 IRQ_SENSE_EDGE
| IRQ_POLARITY_NEGATIVE
,
283 IRQ_SENSE_EDGE
| IRQ_POLARITY_POSITIVE
286 static unsigned char map_mpic_senses
[4] = {
287 IRQ_SENSE_EDGE
| IRQ_POLARITY_POSITIVE
,
288 IRQ_SENSE_LEVEL
| IRQ_POLARITY_NEGATIVE
,
289 /* 2 seems to be used for the 8259 cascade... */
290 IRQ_SENSE_LEVEL
| IRQ_POLARITY_POSITIVE
,
291 IRQ_SENSE_EDGE
| IRQ_POLARITY_NEGATIVE
,
294 static int __devinit
finish_node_interrupts(struct device_node
*np
,
295 unsigned long *mem_start
,
299 int intlen
, intrcells
, intrcount
;
301 unsigned int *irq
, virq
;
302 struct device_node
*ic
;
305 //#define TRACE(fmt...) do { if (trace) { printk(fmt); mdelay(1000); } } while(0)
306 #define TRACE(fmt...)
308 if (!strcmp(np
->name
, "smu-doorbell"))
311 TRACE("Finishing SMU doorbell ! num_interrupt_controllers = %d\n",
312 num_interrupt_controllers
);
314 if (num_interrupt_controllers
== 0) {
316 * Old machines just have a list of interrupt numbers
317 * and no interrupt-controller nodes.
319 ints
= (unsigned int *) get_property(np
, "AAPL,interrupts",
321 /* XXX old interpret_pci_props looked in parent too */
322 /* XXX old interpret_macio_props looked for interrupts
323 before AAPL,interrupts */
325 ints
= (unsigned int *) get_property(np
, "interrupts",
330 np
->n_intrs
= intlen
/ sizeof(unsigned int);
331 np
->intrs
= prom_alloc(np
->n_intrs
* sizeof(np
->intrs
[0]),
338 for (i
= 0; i
< np
->n_intrs
; ++i
) {
339 np
->intrs
[i
].line
= *ints
++;
340 np
->intrs
[i
].sense
= IRQ_SENSE_LEVEL
341 | IRQ_POLARITY_NEGATIVE
;
346 ints
= (unsigned int *) get_property(np
, "interrupts", &intlen
);
347 TRACE("ints=%p, intlen=%d\n", ints
, intlen
);
350 intrcells
= prom_n_intr_cells(np
);
351 intlen
/= intrcells
* sizeof(unsigned int);
352 TRACE("intrcells=%d, new intlen=%d\n", intrcells
, intlen
);
353 np
->intrs
= prom_alloc(intlen
* sizeof(*(np
->intrs
)), mem_start
);
361 for (i
= 0; i
< intlen
; ++i
, ints
+= intrcells
) {
362 n
= map_interrupt(&irq
, &ic
, np
, ints
, intrcells
);
363 TRACE("map, irq=%d, ic=%p, n=%d\n", irq
, ic
, n
);
367 /* don't map IRQ numbers under a cascaded 8259 controller */
368 if (ic
&& device_is_compatible(ic
, "chrp,iic")) {
369 np
->intrs
[intrcount
].line
= irq
[0];
370 sense
= (n
> 1)? (irq
[1] & 3): 3;
371 np
->intrs
[intrcount
].sense
= map_isa_senses
[sense
];
373 virq
= virt_irq_create_mapping(irq
[0]);
374 TRACE("virq=%d\n", virq
);
376 if (virq
== NO_IRQ
) {
377 printk(KERN_CRIT
"Could not allocate interrupt"
378 " number for %s\n", np
->full_name
);
382 np
->intrs
[intrcount
].line
= irq_offset_up(virq
);
383 sense
= (n
> 1)? (irq
[1] & 3): 1;
385 /* Apple uses bits in there in a different way, let's
386 * only keep the real sense bit on macs
388 if (machine_is(powermac
))
390 np
->intrs
[intrcount
].sense
= map_mpic_senses
[sense
];
394 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
395 if (machine_is(powermac
) && ic
&& ic
->parent
) {
396 char *name
= get_property(ic
->parent
, "name", NULL
);
397 if (name
&& !strcmp(name
, "u3"))
398 np
->intrs
[intrcount
].line
+= 128;
399 else if (!(name
&& (!strcmp(name
, "mac-io") ||
400 !strcmp(name
, "u4"))))
401 /* ignore other cascaded controllers, such as
405 #endif /* CONFIG_PPC64 */
407 printk("hmmm, got %d intr cells for %s:", n
,
409 for (j
= 0; j
< n
; ++j
)
410 printk(" %d", irq
[j
]);
415 np
->n_intrs
= intrcount
;
420 static int __devinit
finish_node(struct device_node
*np
,
421 unsigned long *mem_start
,
424 struct device_node
*child
;
427 rc
= finish_node_interrupts(np
, mem_start
, measure_only
);
431 for (child
= np
->child
; child
!= NULL
; child
= child
->sibling
) {
432 rc
= finish_node(child
, mem_start
, measure_only
);
440 static void __init
scan_interrupt_controllers(void)
442 struct device_node
*np
;
447 for (np
= allnodes
; np
!= NULL
; np
= np
->allnext
) {
448 ic
= get_property(np
, "interrupt-controller", &iclen
);
449 name
= get_property(np
, "name", NULL
);
450 /* checking iclen makes sure we don't get a false
451 match on /chosen.interrupt_controller */
453 && strcmp(name
, "interrupt-controller") == 0)
454 || (ic
!= NULL
&& iclen
== 0
455 && strcmp(name
, "AppleKiwi"))) {
457 dflt_interrupt_controller
= np
;
461 num_interrupt_controllers
= n
;
465 * finish_device_tree is called once things are running normally
466 * (i.e. with text and data mapped to the address they were linked at).
467 * It traverses the device tree and fills in some of the additional,
468 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
469 * mapping is also initialized at this point.
471 void __init
finish_device_tree(void)
473 unsigned long start
, end
, size
= 0;
475 DBG(" -> finish_device_tree\n");
478 /* Initialize virtual IRQ map */
481 scan_interrupt_controllers();
484 * Finish device-tree (pre-parsing some properties etc...)
485 * We do this in 2 passes. One with "measure_only" set, which
486 * will only measure the amount of memory needed, then we can
487 * allocate that memory, and call finish_node again. However,
488 * we must be careful as most routines will fail nowadays when
489 * prom_alloc() returns 0, so we must make sure our first pass
490 * doesn't start at 0. We pre-initialize size to 16 for that
491 * reason and then remove those additional 16 bytes
494 finish_node(allnodes
, &size
, 1);
500 end
= start
= (unsigned long)__va(lmb_alloc(size
, 128));
502 finish_node(allnodes
, &end
, 0);
503 BUG_ON(end
!= start
+ size
);
505 DBG(" <- finish_device_tree\n");
508 static inline char *find_flat_dt_string(u32 offset
)
510 return ((char *)initial_boot_params
) +
511 initial_boot_params
->off_dt_strings
+ offset
;
515 * This function is used to scan the flattened device-tree, it is
516 * used to extract the memory informations at boot before we can
519 int __init
of_scan_flat_dt(int (*it
)(unsigned long node
,
520 const char *uname
, int depth
,
524 unsigned long p
= ((unsigned long)initial_boot_params
) +
525 initial_boot_params
->off_dt_struct
;
530 u32 tag
= *((u32
*)p
);
534 if (tag
== OF_DT_END_NODE
) {
538 if (tag
== OF_DT_NOP
)
540 if (tag
== OF_DT_END
)
542 if (tag
== OF_DT_PROP
) {
543 u32 sz
= *((u32
*)p
);
545 if (initial_boot_params
->version
< 0x10)
546 p
= _ALIGN(p
, sz
>= 8 ? 8 : 4);
551 if (tag
!= OF_DT_BEGIN_NODE
) {
552 printk(KERN_WARNING
"Invalid tag %x scanning flattened"
553 " device tree !\n", tag
);
558 p
= _ALIGN(p
+ strlen(pathp
) + 1, 4);
559 if ((*pathp
) == '/') {
561 for (lp
= NULL
, np
= pathp
; *np
; np
++)
567 rc
= it(p
, pathp
, depth
, data
);
575 unsigned long __init
of_get_flat_dt_root(void)
577 unsigned long p
= ((unsigned long)initial_boot_params
) +
578 initial_boot_params
->off_dt_struct
;
580 while(*((u32
*)p
) == OF_DT_NOP
)
582 BUG_ON (*((u32
*)p
) != OF_DT_BEGIN_NODE
);
584 return _ALIGN(p
+ strlen((char *)p
) + 1, 4);
588 * This function can be used within scan_flattened_dt callback to get
589 * access to properties
591 void* __init
of_get_flat_dt_prop(unsigned long node
, const char *name
,
594 unsigned long p
= node
;
597 u32 tag
= *((u32
*)p
);
602 if (tag
== OF_DT_NOP
)
604 if (tag
!= OF_DT_PROP
)
608 noff
= *((u32
*)(p
+ 4));
610 if (initial_boot_params
->version
< 0x10)
611 p
= _ALIGN(p
, sz
>= 8 ? 8 : 4);
613 nstr
= find_flat_dt_string(noff
);
615 printk(KERN_WARNING
"Can't find property index"
619 if (strcmp(name
, nstr
) == 0) {
629 int __init
of_flat_dt_is_compatible(unsigned long node
, const char *compat
)
632 unsigned long cplen
, l
;
634 cp
= of_get_flat_dt_prop(node
, "compatible", &cplen
);
638 if (strncasecmp(cp
, compat
, strlen(compat
)) == 0)
648 static void *__init
unflatten_dt_alloc(unsigned long *mem
, unsigned long size
,
653 *mem
= _ALIGN(*mem
, align
);
660 static unsigned long __init
unflatten_dt_node(unsigned long mem
,
662 struct device_node
*dad
,
663 struct device_node
***allnextpp
,
664 unsigned long fpsize
)
666 struct device_node
*np
;
667 struct property
*pp
, **prev_pp
= NULL
;
670 unsigned int l
, allocl
;
674 tag
= *((u32
*)(*p
));
675 if (tag
!= OF_DT_BEGIN_NODE
) {
676 printk("Weird tag at start of node: %x\n", tag
);
681 l
= allocl
= strlen(pathp
) + 1;
682 *p
= _ALIGN(*p
+ l
, 4);
684 /* version 0x10 has a more compact unit name here instead of the full
685 * path. we accumulate the full path size using "fpsize", we'll rebuild
686 * it later. We detect this because the first character of the name is
689 if ((*pathp
) != '/') {
692 /* root node: special case. fpsize accounts for path
693 * plus terminating zero. root node only has '/', so
694 * fpsize should be 2, but we want to avoid the first
695 * level nodes to have two '/' so we use fpsize 1 here
700 /* account for '/' and path size minus terminal 0
709 np
= unflatten_dt_alloc(&mem
, sizeof(struct device_node
) + allocl
,
710 __alignof__(struct device_node
));
712 memset(np
, 0, sizeof(*np
));
713 np
->full_name
= ((char*)np
) + sizeof(struct device_node
);
715 char *p
= np
->full_name
;
716 /* rebuild full path for new format */
717 if (dad
&& dad
->parent
) {
718 strcpy(p
, dad
->full_name
);
720 if ((strlen(p
) + l
+ 1) != allocl
) {
721 DBG("%s: p: %d, l: %d, a: %d\n",
722 pathp
, (int)strlen(p
), l
, allocl
);
730 memcpy(np
->full_name
, pathp
, l
);
731 prev_pp
= &np
->properties
;
733 *allnextpp
= &np
->allnext
;
736 /* we temporarily use the next field as `last_child'*/
740 dad
->next
->sibling
= np
;
743 kref_init(&np
->kref
);
749 tag
= *((u32
*)(*p
));
750 if (tag
== OF_DT_NOP
) {
754 if (tag
!= OF_DT_PROP
)
758 noff
= *((u32
*)((*p
) + 4));
760 if (initial_boot_params
->version
< 0x10)
761 *p
= _ALIGN(*p
, sz
>= 8 ? 8 : 4);
763 pname
= find_flat_dt_string(noff
);
765 printk("Can't find property name in list !\n");
768 if (strcmp(pname
, "name") == 0)
770 l
= strlen(pname
) + 1;
771 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
772 __alignof__(struct property
));
774 if (strcmp(pname
, "linux,phandle") == 0) {
775 np
->node
= *((u32
*)*p
);
776 if (np
->linux_phandle
== 0)
777 np
->linux_phandle
= np
->node
;
779 if (strcmp(pname
, "ibm,phandle") == 0)
780 np
->linux_phandle
= *((u32
*)*p
);
783 pp
->value
= (void *)*p
;
787 *p
= _ALIGN((*p
) + sz
, 4);
789 /* with version 0x10 we may not have the name property, recreate
790 * it here from the unit name if absent
793 char *p
= pathp
, *ps
= pathp
, *pa
= NULL
;
806 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
) + sz
,
807 __alignof__(struct property
));
811 pp
->value
= (unsigned char *)(pp
+ 1);
814 memcpy(pp
->value
, ps
, sz
- 1);
815 ((char *)pp
->value
)[sz
- 1] = 0;
816 DBG("fixed up name for %s -> %s\n", pathp
, pp
->value
);
821 np
->name
= get_property(np
, "name", NULL
);
822 np
->type
= get_property(np
, "device_type", NULL
);
829 while (tag
== OF_DT_BEGIN_NODE
) {
830 mem
= unflatten_dt_node(mem
, p
, np
, allnextpp
, fpsize
);
831 tag
= *((u32
*)(*p
));
833 if (tag
!= OF_DT_END_NODE
) {
834 printk("Weird tag at end of node: %x\n", tag
);
841 static int __init
early_parse_mem(char *p
)
846 memory_limit
= PAGE_ALIGN(memparse(p
, &p
));
847 DBG("memory limit = 0x%lx\n", memory_limit
);
851 early_param("mem", early_parse_mem
);
854 * The device tree may be allocated below our memory limit, or inside the
855 * crash kernel region for kdump. If so, move it out now.
857 static void move_device_tree(void)
859 unsigned long start
, size
;
862 DBG("-> move_device_tree\n");
864 start
= __pa(initial_boot_params
);
865 size
= initial_boot_params
->totalsize
;
867 if ((memory_limit
&& (start
+ size
) > memory_limit
) ||
868 overlaps_crashkernel(start
, size
)) {
869 p
= __va(lmb_alloc_base(size
, PAGE_SIZE
, lmb
.rmo_size
));
870 memcpy(p
, initial_boot_params
, size
);
871 initial_boot_params
= (struct boot_param_header
*)p
;
872 DBG("Moved device tree to 0x%p\n", p
);
875 DBG("<- move_device_tree\n");
879 * unflattens the device-tree passed by the firmware, creating the
880 * tree of struct device_node. It also fills the "name" and "type"
881 * pointers of the nodes so the normal device-tree walking functions
882 * can be used (this used to be done by finish_device_tree)
884 void __init
unflatten_device_tree(void)
886 unsigned long start
, mem
, size
;
887 struct device_node
**allnextp
= &allnodes
;
889 DBG(" -> unflatten_device_tree()\n");
891 /* First pass, scan for size */
892 start
= ((unsigned long)initial_boot_params
) +
893 initial_boot_params
->off_dt_struct
;
894 size
= unflatten_dt_node(0, &start
, NULL
, NULL
, 0);
895 size
= (size
| 3) + 1;
897 DBG(" size is %lx, allocating...\n", size
);
899 /* Allocate memory for the expanded device tree */
900 mem
= lmb_alloc(size
+ 4, __alignof__(struct device_node
));
901 mem
= (unsigned long) __va(mem
);
903 ((u32
*)mem
)[size
/ 4] = 0xdeadbeef;
905 DBG(" unflattening %lx...\n", mem
);
907 /* Second pass, do actual unflattening */
908 start
= ((unsigned long)initial_boot_params
) +
909 initial_boot_params
->off_dt_struct
;
910 unflatten_dt_node(mem
, &start
, NULL
, &allnextp
, 0);
911 if (*((u32
*)start
) != OF_DT_END
)
912 printk(KERN_WARNING
"Weird tag at end of tree: %08x\n", *((u32
*)start
));
913 if (((u32
*)mem
)[size
/ 4] != 0xdeadbeef)
914 printk(KERN_WARNING
"End of tree marker overwritten: %08x\n",
915 ((u32
*)mem
)[size
/ 4] );
918 /* Get pointer to OF "/chosen" node for use everywhere */
919 of_chosen
= of_find_node_by_path("/chosen");
920 if (of_chosen
== NULL
)
921 of_chosen
= of_find_node_by_path("/chosen@0");
923 DBG(" <- unflatten_device_tree()\n");
927 * ibm,pa-features is a per-cpu property that contains a string of
928 * attribute descriptors, each of which has a 2 byte header plus up
929 * to 254 bytes worth of processor attribute bits. First header
930 * byte specifies the number of bytes following the header.
931 * Second header byte is an "attribute-specifier" type, of which
932 * zero is the only currently-defined value.
933 * Implementation: Pass in the byte and bit offset for the feature
934 * that we are interested in. The function will return -1 if the
935 * pa-features property is missing, or a 1/0 to indicate if the feature
936 * is supported/not supported. Note that the bit numbers are
937 * big-endian to match the definition in PAPR.
939 static struct ibm_pa_feature
{
940 unsigned long cpu_features
; /* CPU_FTR_xxx bit */
941 unsigned int cpu_user_ftrs
; /* PPC_FEATURE_xxx bit */
942 unsigned char pabyte
; /* byte number in ibm,pa-features */
943 unsigned char pabit
; /* bit number (big-endian) */
944 unsigned char invert
; /* if 1, pa bit set => clear feature */
945 } ibm_pa_features
[] __initdata
= {
946 {0, PPC_FEATURE_HAS_MMU
, 0, 0, 0},
947 {0, PPC_FEATURE_HAS_FPU
, 0, 1, 0},
948 {CPU_FTR_SLB
, 0, 0, 2, 0},
949 {CPU_FTR_CTRL
, 0, 0, 3, 0},
950 {CPU_FTR_NOEXECUTE
, 0, 0, 6, 0},
951 {CPU_FTR_NODSISRALIGN
, 0, 1, 1, 1},
953 /* put this back once we know how to test if firmware does 64k IO */
954 {CPU_FTR_CI_LARGE_PAGE
, 0, 1, 2, 0},
956 {CPU_FTR_REAL_LE
, PPC_FEATURE_TRUE_LE
, 5, 0, 0},
959 static void __init
check_cpu_pa_features(unsigned long node
)
961 unsigned char *pa_ftrs
;
962 unsigned long len
, tablelen
, i
, bit
;
964 pa_ftrs
= of_get_flat_dt_prop(node
, "ibm,pa-features", &tablelen
);
968 /* find descriptor with type == 0 */
972 len
= 2 + pa_ftrs
[0];
974 return; /* descriptor 0 not found */
981 /* loop over bits we know about */
982 for (i
= 0; i
< ARRAY_SIZE(ibm_pa_features
); ++i
) {
983 struct ibm_pa_feature
*fp
= &ibm_pa_features
[i
];
985 if (fp
->pabyte
>= pa_ftrs
[0])
987 bit
= (pa_ftrs
[2 + fp
->pabyte
] >> (7 - fp
->pabit
)) & 1;
988 if (bit
^ fp
->invert
) {
989 cur_cpu_spec
->cpu_features
|= fp
->cpu_features
;
990 cur_cpu_spec
->cpu_user_features
|= fp
->cpu_user_ftrs
;
992 cur_cpu_spec
->cpu_features
&= ~fp
->cpu_features
;
993 cur_cpu_spec
->cpu_user_features
&= ~fp
->cpu_user_ftrs
;
998 static int __init
early_init_dt_scan_cpus(unsigned long node
,
999 const char *uname
, int depth
,
1002 static int logical_cpuid
= 0;
1003 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
1004 #ifdef CONFIG_ALTIVEC
1012 /* We are scanning "cpu" nodes only */
1013 if (type
== NULL
|| strcmp(type
, "cpu") != 0)
1016 /* Get physical cpuid */
1017 intserv
= of_get_flat_dt_prop(node
, "ibm,ppc-interrupt-server#s", &len
);
1019 nthreads
= len
/ sizeof(int);
1021 intserv
= of_get_flat_dt_prop(node
, "reg", NULL
);
1026 * Now see if any of these threads match our boot cpu.
1027 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
1029 for (i
= 0; i
< nthreads
; i
++) {
1031 * version 2 of the kexec param format adds the phys cpuid of
1034 if (initial_boot_params
&& initial_boot_params
->version
>= 2) {
1036 initial_boot_params
->boot_cpuid_phys
) {
1042 * Check if it's the boot-cpu, set it's hw index now,
1043 * unfortunately this format did not support booting
1044 * off secondary threads.
1046 if (of_get_flat_dt_prop(node
,
1047 "linux,boot-cpu", NULL
) != NULL
) {
1054 /* logical cpu id is always 0 on UP kernels */
1060 DBG("boot cpu: logical %d physical %d\n", logical_cpuid
,
1062 boot_cpuid
= logical_cpuid
;
1063 set_hard_smp_processor_id(boot_cpuid
, intserv
[i
]);
1066 #ifdef CONFIG_ALTIVEC
1067 /* Check if we have a VMX and eventually update CPU features */
1068 prop
= (u32
*)of_get_flat_dt_prop(node
, "ibm,vmx", NULL
);
1069 if (prop
&& (*prop
) > 0) {
1070 cur_cpu_spec
->cpu_features
|= CPU_FTR_ALTIVEC
;
1071 cur_cpu_spec
->cpu_user_features
|= PPC_FEATURE_HAS_ALTIVEC
;
1074 /* Same goes for Apple's "altivec" property */
1075 prop
= (u32
*)of_get_flat_dt_prop(node
, "altivec", NULL
);
1077 cur_cpu_spec
->cpu_features
|= CPU_FTR_ALTIVEC
;
1078 cur_cpu_spec
->cpu_user_features
|= PPC_FEATURE_HAS_ALTIVEC
;
1080 #endif /* CONFIG_ALTIVEC */
1082 check_cpu_pa_features(node
);
1084 #ifdef CONFIG_PPC_PSERIES
1086 cur_cpu_spec
->cpu_features
|= CPU_FTR_SMT
;
1088 cur_cpu_spec
->cpu_features
&= ~CPU_FTR_SMT
;
1094 static int __init
early_init_dt_scan_chosen(unsigned long node
,
1095 const char *uname
, int depth
, void *data
)
1097 unsigned long *lprop
;
1101 DBG("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
1104 (strcmp(uname
, "chosen") != 0 && strcmp(uname
, "chosen@0") != 0))
1108 /* check if iommu is forced on or off */
1109 if (of_get_flat_dt_prop(node
, "linux,iommu-off", NULL
) != NULL
)
1111 if (of_get_flat_dt_prop(node
, "linux,iommu-force-on", NULL
) != NULL
)
1115 /* mem=x on the command line is the preferred mechanism */
1116 lprop
= of_get_flat_dt_prop(node
, "linux,memory-limit", NULL
);
1118 memory_limit
= *lprop
;
1121 lprop
= of_get_flat_dt_prop(node
, "linux,tce-alloc-start", NULL
);
1123 tce_alloc_start
= *lprop
;
1124 lprop
= of_get_flat_dt_prop(node
, "linux,tce-alloc-end", NULL
);
1126 tce_alloc_end
= *lprop
;
1130 lprop
= (u64
*)of_get_flat_dt_prop(node
, "linux,crashkernel-base", NULL
);
1132 crashk_res
.start
= *lprop
;
1134 lprop
= (u64
*)of_get_flat_dt_prop(node
, "linux,crashkernel-size", NULL
);
1136 crashk_res
.end
= crashk_res
.start
+ *lprop
- 1;
1139 /* Retreive command line */
1140 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
1141 if (p
!= NULL
&& l
> 0)
1142 strlcpy(cmd_line
, p
, min((int)l
, COMMAND_LINE_SIZE
));
1144 #ifdef CONFIG_CMDLINE
1145 if (l
== 0 || (l
== 1 && (*p
) == 0))
1146 strlcpy(cmd_line
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
1147 #endif /* CONFIG_CMDLINE */
1149 DBG("Command line is: %s\n", cmd_line
);
1155 static int __init
early_init_dt_scan_root(unsigned long node
,
1156 const char *uname
, int depth
, void *data
)
1163 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
1164 dt_root_size_cells
= (prop
== NULL
) ? 1 : *prop
;
1165 DBG("dt_root_size_cells = %x\n", dt_root_size_cells
);
1167 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
1168 dt_root_addr_cells
= (prop
== NULL
) ? 2 : *prop
;
1169 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells
);
1175 static unsigned long __init
dt_mem_next_cell(int s
, cell_t
**cellp
)
1180 /* Ignore more than 2 cells */
1181 while (s
> sizeof(unsigned long) / 4) {
1199 static int __init
early_init_dt_scan_memory(unsigned long node
,
1200 const char *uname
, int depth
, void *data
)
1202 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
1206 /* We are scanning "memory" nodes only */
1209 * The longtrail doesn't have a device_type on the
1210 * /memory node, so look for the node called /memory@0.
1212 if (depth
!= 1 || strcmp(uname
, "memory@0") != 0)
1214 } else if (strcmp(type
, "memory") != 0)
1217 reg
= (cell_t
*)of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
1219 reg
= (cell_t
*)of_get_flat_dt_prop(node
, "reg", &l
);
1223 endp
= reg
+ (l
/ sizeof(cell_t
));
1225 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
1226 uname
, l
, reg
[0], reg
[1], reg
[2], reg
[3]);
1228 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
1229 unsigned long base
, size
;
1231 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
1232 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
1236 DBG(" - %lx , %lx\n", base
, size
);
1239 if (base
>= 0x80000000ul
)
1241 if ((base
+ size
) > 0x80000000ul
)
1242 size
= 0x80000000ul
- base
;
1245 lmb_add(base
, size
);
1250 static void __init
early_reserve_mem(void)
1254 unsigned long self_base
;
1255 unsigned long self_size
;
1257 reserve_map
= (u64
*)(((unsigned long)initial_boot_params
) +
1258 initial_boot_params
->off_mem_rsvmap
);
1260 /* before we do anything, lets reserve the dt blob */
1261 self_base
= __pa((unsigned long)initial_boot_params
);
1262 self_size
= initial_boot_params
->totalsize
;
1263 lmb_reserve(self_base
, self_size
);
1267 * Handle the case where we might be booting from an old kexec
1268 * image that setup the mem_rsvmap as pairs of 32-bit values
1270 if (*reserve_map
> 0xffffffffull
) {
1271 u32 base_32
, size_32
;
1272 u32
*reserve_map_32
= (u32
*)reserve_map
;
1275 base_32
= *(reserve_map_32
++);
1276 size_32
= *(reserve_map_32
++);
1279 /* skip if the reservation is for the blob */
1280 if (base_32
== self_base
&& size_32
== self_size
)
1282 DBG("reserving: %x -> %x\n", base_32
, size_32
);
1283 lmb_reserve(base_32
, size_32
);
1289 base
= *(reserve_map
++);
1290 size
= *(reserve_map
++);
1293 /* skip if the reservation is for the blob */
1294 if (base
== self_base
&& size
== self_size
)
1296 DBG("reserving: %llx -> %llx\n", base
, size
);
1297 lmb_reserve(base
, size
);
1301 DBG("memory reserved, lmbs :\n");
1306 void __init
early_init_devtree(void *params
)
1308 DBG(" -> early_init_devtree()\n");
1310 /* Setup flat device-tree pointer */
1311 initial_boot_params
= params
;
1313 #ifdef CONFIG_PPC_RTAS
1314 /* Some machines might need RTAS info for debugging, grab it now. */
1315 of_scan_flat_dt(early_init_dt_scan_rtas
, NULL
);
1318 /* Retrieve various informations from the /chosen node of the
1319 * device-tree, including the platform type, initrd location and
1320 * size, TCE reserve, and more ...
1322 of_scan_flat_dt(early_init_dt_scan_chosen
, NULL
);
1324 /* Scan memory nodes and rebuild LMBs */
1326 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
1327 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
1329 /* Save command line for /proc/cmdline and then parse parameters */
1330 strlcpy(saved_command_line
, cmd_line
, COMMAND_LINE_SIZE
);
1331 parse_early_param();
1333 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1334 lmb_reserve(PHYSICAL_START
, __pa(klimit
) - PHYSICAL_START
);
1335 reserve_kdump_trampoline();
1336 reserve_crashkernel();
1337 early_reserve_mem();
1339 lmb_enforce_memory_limit(memory_limit
);
1342 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1344 /* We may need to relocate the flat tree, do it now.
1345 * FIXME .. and the initrd too? */
1348 DBG("Scanning CPUs ...\n");
1350 /* Retreive CPU related informations from the flat tree
1351 * (altivec support, boot CPU ID, ...)
1353 of_scan_flat_dt(early_init_dt_scan_cpus
, NULL
);
1355 DBG(" <- early_init_devtree()\n");
1361 prom_n_addr_cells(struct device_node
* np
)
1367 ip
= (int *) get_property(np
, "#address-cells", NULL
);
1370 } while (np
->parent
);
1371 /* No #address-cells property for the root node, default to 1 */
1374 EXPORT_SYMBOL(prom_n_addr_cells
);
1377 prom_n_size_cells(struct device_node
* np
)
1383 ip
= (int *) get_property(np
, "#size-cells", NULL
);
1386 } while (np
->parent
);
1387 /* No #size-cells property for the root node, default to 1 */
1390 EXPORT_SYMBOL(prom_n_size_cells
);
1393 * Work out the sense (active-low level / active-high edge)
1394 * of each interrupt from the device tree.
1396 void __init
prom_get_irq_senses(unsigned char *senses
, int off
, int max
)
1398 struct device_node
*np
;
1401 /* default to level-triggered */
1402 memset(senses
, IRQ_SENSE_LEVEL
| IRQ_POLARITY_NEGATIVE
, max
- off
);
1404 for (np
= allnodes
; np
!= 0; np
= np
->allnext
) {
1405 for (j
= 0; j
< np
->n_intrs
; j
++) {
1406 i
= np
->intrs
[j
].line
;
1407 if (i
>= off
&& i
< max
)
1408 senses
[i
-off
] = np
->intrs
[j
].sense
;
1414 * Construct and return a list of the device_nodes with a given name.
1416 struct device_node
*find_devices(const char *name
)
1418 struct device_node
*head
, **prevp
, *np
;
1421 for (np
= allnodes
; np
!= 0; np
= np
->allnext
) {
1422 if (np
->name
!= 0 && strcasecmp(np
->name
, name
) == 0) {
1430 EXPORT_SYMBOL(find_devices
);
1433 * Construct and return a list of the device_nodes with a given type.
1435 struct device_node
*find_type_devices(const char *type
)
1437 struct device_node
*head
, **prevp
, *np
;
1440 for (np
= allnodes
; np
!= 0; np
= np
->allnext
) {
1441 if (np
->type
!= 0 && strcasecmp(np
->type
, type
) == 0) {
1449 EXPORT_SYMBOL(find_type_devices
);
1452 * Returns all nodes linked together
1454 struct device_node
*find_all_nodes(void)
1456 struct device_node
*head
, **prevp
, *np
;
1459 for (np
= allnodes
; np
!= 0; np
= np
->allnext
) {
1466 EXPORT_SYMBOL(find_all_nodes
);
1468 /** Checks if the given "compat" string matches one of the strings in
1469 * the device's "compatible" property
1471 int device_is_compatible(struct device_node
*device
, const char *compat
)
1476 cp
= (char *) get_property(device
, "compatible", &cplen
);
1480 if (strncasecmp(cp
, compat
, strlen(compat
)) == 0)
1489 EXPORT_SYMBOL(device_is_compatible
);
1493 * Indicates whether the root node has a given value in its
1494 * compatible property.
1496 int machine_is_compatible(const char *compat
)
1498 struct device_node
*root
;
1501 root
= of_find_node_by_path("/");
1503 rc
= device_is_compatible(root
, compat
);
1508 EXPORT_SYMBOL(machine_is_compatible
);
1511 * Construct and return a list of the device_nodes with a given type
1512 * and compatible property.
1514 struct device_node
*find_compatible_devices(const char *type
,
1517 struct device_node
*head
, **prevp
, *np
;
1520 for (np
= allnodes
; np
!= 0; np
= np
->allnext
) {
1522 && !(np
->type
!= 0 && strcasecmp(np
->type
, type
) == 0))
1524 if (device_is_compatible(np
, compat
)) {
1532 EXPORT_SYMBOL(find_compatible_devices
);
1535 * Find the device_node with a given full_name.
1537 struct device_node
*find_path_device(const char *path
)
1539 struct device_node
*np
;
1541 for (np
= allnodes
; np
!= 0; np
= np
->allnext
)
1542 if (np
->full_name
!= 0 && strcasecmp(np
->full_name
, path
) == 0)
1546 EXPORT_SYMBOL(find_path_device
);
1550 * New implementation of the OF "find" APIs, return a refcounted
1551 * object, call of_node_put() when done. The device tree and list
1552 * are protected by a rw_lock.
1554 * Note that property management will need some locking as well,
1555 * this isn't dealt with yet.
1560 * of_find_node_by_name - Find a node by its "name" property
1561 * @from: The node to start searching from or NULL, the node
1562 * you pass will not be searched, only the next one
1563 * will; typically, you pass what the previous call
1564 * returned. of_node_put() will be called on it
1565 * @name: The name string to match against
1567 * Returns a node pointer with refcount incremented, use
1568 * of_node_put() on it when done.
1570 struct device_node
*of_find_node_by_name(struct device_node
*from
,
1573 struct device_node
*np
;
1575 read_lock(&devtree_lock
);
1576 np
= from
? from
->allnext
: allnodes
;
1577 for (; np
!= NULL
; np
= np
->allnext
)
1578 if (np
->name
!= NULL
&& strcasecmp(np
->name
, name
) == 0
1583 read_unlock(&devtree_lock
);
1586 EXPORT_SYMBOL(of_find_node_by_name
);
1589 * of_find_node_by_type - Find a node by its "device_type" property
1590 * @from: The node to start searching from or NULL, the node
1591 * you pass will not be searched, only the next one
1592 * will; typically, you pass what the previous call
1593 * returned. of_node_put() will be called on it
1594 * @name: The type string to match against
1596 * Returns a node pointer with refcount incremented, use
1597 * of_node_put() on it when done.
1599 struct device_node
*of_find_node_by_type(struct device_node
*from
,
1602 struct device_node
*np
;
1604 read_lock(&devtree_lock
);
1605 np
= from
? from
->allnext
: allnodes
;
1606 for (; np
!= 0; np
= np
->allnext
)
1607 if (np
->type
!= 0 && strcasecmp(np
->type
, type
) == 0
1612 read_unlock(&devtree_lock
);
1615 EXPORT_SYMBOL(of_find_node_by_type
);
1618 * of_find_compatible_node - Find a node based on type and one of the
1619 * tokens in its "compatible" property
1620 * @from: The node to start searching from or NULL, the node
1621 * you pass will not be searched, only the next one
1622 * will; typically, you pass what the previous call
1623 * returned. of_node_put() will be called on it
1624 * @type: The type string to match "device_type" or NULL to ignore
1625 * @compatible: The string to match to one of the tokens in the device
1626 * "compatible" list.
1628 * Returns a node pointer with refcount incremented, use
1629 * of_node_put() on it when done.
1631 struct device_node
*of_find_compatible_node(struct device_node
*from
,
1632 const char *type
, const char *compatible
)
1634 struct device_node
*np
;
1636 read_lock(&devtree_lock
);
1637 np
= from
? from
->allnext
: allnodes
;
1638 for (; np
!= 0; np
= np
->allnext
) {
1640 && !(np
->type
!= 0 && strcasecmp(np
->type
, type
) == 0))
1642 if (device_is_compatible(np
, compatible
) && of_node_get(np
))
1647 read_unlock(&devtree_lock
);
1650 EXPORT_SYMBOL(of_find_compatible_node
);
1653 * of_find_node_by_path - Find a node matching a full OF path
1654 * @path: The full path to match
1656 * Returns a node pointer with refcount incremented, use
1657 * of_node_put() on it when done.
1659 struct device_node
*of_find_node_by_path(const char *path
)
1661 struct device_node
*np
= allnodes
;
1663 read_lock(&devtree_lock
);
1664 for (; np
!= 0; np
= np
->allnext
) {
1665 if (np
->full_name
!= 0 && strcasecmp(np
->full_name
, path
) == 0
1669 read_unlock(&devtree_lock
);
1672 EXPORT_SYMBOL(of_find_node_by_path
);
1675 * of_find_node_by_phandle - Find a node given a phandle
1676 * @handle: phandle of the node to find
1678 * Returns a node pointer with refcount incremented, use
1679 * of_node_put() on it when done.
1681 struct device_node
*of_find_node_by_phandle(phandle handle
)
1683 struct device_node
*np
;
1685 read_lock(&devtree_lock
);
1686 for (np
= allnodes
; np
!= 0; np
= np
->allnext
)
1687 if (np
->linux_phandle
== handle
)
1691 read_unlock(&devtree_lock
);
1694 EXPORT_SYMBOL(of_find_node_by_phandle
);
1697 * of_find_all_nodes - Get next node in global list
1698 * @prev: Previous node or NULL to start iteration
1699 * of_node_put() will be called on it
1701 * Returns a node pointer with refcount incremented, use
1702 * of_node_put() on it when done.
1704 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
1706 struct device_node
*np
;
1708 read_lock(&devtree_lock
);
1709 np
= prev
? prev
->allnext
: allnodes
;
1710 for (; np
!= 0; np
= np
->allnext
)
1711 if (of_node_get(np
))
1715 read_unlock(&devtree_lock
);
1718 EXPORT_SYMBOL(of_find_all_nodes
);
1721 * of_get_parent - Get a node's parent if any
1722 * @node: Node to get parent
1724 * Returns a node pointer with refcount incremented, use
1725 * of_node_put() on it when done.
1727 struct device_node
*of_get_parent(const struct device_node
*node
)
1729 struct device_node
*np
;
1734 read_lock(&devtree_lock
);
1735 np
= of_node_get(node
->parent
);
1736 read_unlock(&devtree_lock
);
1739 EXPORT_SYMBOL(of_get_parent
);
1742 * of_get_next_child - Iterate a node childs
1743 * @node: parent node
1744 * @prev: previous child of the parent node, or NULL to get first
1746 * Returns a node pointer with refcount incremented, use
1747 * of_node_put() on it when done.
1749 struct device_node
*of_get_next_child(const struct device_node
*node
,
1750 struct device_node
*prev
)
1752 struct device_node
*next
;
1754 read_lock(&devtree_lock
);
1755 next
= prev
? prev
->sibling
: node
->child
;
1756 for (; next
!= 0; next
= next
->sibling
)
1757 if (of_node_get(next
))
1761 read_unlock(&devtree_lock
);
1764 EXPORT_SYMBOL(of_get_next_child
);
1767 * of_node_get - Increment refcount of a node
1768 * @node: Node to inc refcount, NULL is supported to
1769 * simplify writing of callers
1773 struct device_node
*of_node_get(struct device_node
*node
)
1776 kref_get(&node
->kref
);
1779 EXPORT_SYMBOL(of_node_get
);
1781 static inline struct device_node
* kref_to_device_node(struct kref
*kref
)
1783 return container_of(kref
, struct device_node
, kref
);
1787 * of_node_release - release a dynamically allocated node
1788 * @kref: kref element of the node to be released
1790 * In of_node_put() this function is passed to kref_put()
1791 * as the destructor.
1793 static void of_node_release(struct kref
*kref
)
1795 struct device_node
*node
= kref_to_device_node(kref
);
1796 struct property
*prop
= node
->properties
;
1798 if (!OF_IS_DYNAMIC(node
))
1801 struct property
*next
= prop
->next
;
1808 prop
= node
->deadprops
;
1809 node
->deadprops
= NULL
;
1813 kfree(node
->full_name
);
1819 * of_node_put - Decrement refcount of a node
1820 * @node: Node to dec refcount, NULL is supported to
1821 * simplify writing of callers
1824 void of_node_put(struct device_node
*node
)
1827 kref_put(&node
->kref
, of_node_release
);
1829 EXPORT_SYMBOL(of_node_put
);
1832 * Plug a device node into the tree and global list.
1834 void of_attach_node(struct device_node
*np
)
1836 write_lock(&devtree_lock
);
1837 np
->sibling
= np
->parent
->child
;
1838 np
->allnext
= allnodes
;
1839 np
->parent
->child
= np
;
1841 write_unlock(&devtree_lock
);
1845 * "Unplug" a node from the device tree. The caller must hold
1846 * a reference to the node. The memory associated with the node
1847 * is not freed until its refcount goes to zero.
1849 void of_detach_node(const struct device_node
*np
)
1851 struct device_node
*parent
;
1853 write_lock(&devtree_lock
);
1855 parent
= np
->parent
;
1858 allnodes
= np
->allnext
;
1860 struct device_node
*prev
;
1861 for (prev
= allnodes
;
1862 prev
->allnext
!= np
;
1863 prev
= prev
->allnext
)
1865 prev
->allnext
= np
->allnext
;
1868 if (parent
->child
== np
)
1869 parent
->child
= np
->sibling
;
1871 struct device_node
*prevsib
;
1872 for (prevsib
= np
->parent
->child
;
1873 prevsib
->sibling
!= np
;
1874 prevsib
= prevsib
->sibling
)
1876 prevsib
->sibling
= np
->sibling
;
1879 write_unlock(&devtree_lock
);
1882 #ifdef CONFIG_PPC_PSERIES
1884 * Fix up the uninitialized fields in a new device node:
1885 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1887 * A lot of boot-time code is duplicated here, because functions such
1888 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1891 * This should probably be split up into smaller chunks.
1894 static int of_finish_dynamic_node(struct device_node
*node
)
1896 struct device_node
*parent
= of_get_parent(node
);
1898 phandle
*ibm_phandle
;
1900 node
->name
= get_property(node
, "name", NULL
);
1901 node
->type
= get_property(node
, "device_type", NULL
);
1908 /* We don't support that function on PowerMac, at least
1911 if (machine_is(powermac
))
1914 /* fix up new node's linux_phandle field */
1915 if ((ibm_phandle
= (unsigned int *)get_property(node
,
1916 "ibm,phandle", NULL
)))
1917 node
->linux_phandle
= *ibm_phandle
;
1920 of_node_put(parent
);
1924 static int prom_reconfig_notifier(struct notifier_block
*nb
,
1925 unsigned long action
, void *node
)
1930 case PSERIES_RECONFIG_ADD
:
1931 err
= of_finish_dynamic_node(node
);
1933 finish_node(node
, NULL
, 0);
1935 printk(KERN_ERR
"finish_node returned %d\n", err
);
1946 static struct notifier_block prom_reconfig_nb
= {
1947 .notifier_call
= prom_reconfig_notifier
,
1948 .priority
= 10, /* This one needs to run first */
1951 static int __init
prom_reconfig_setup(void)
1953 return pSeries_reconfig_notifier_register(&prom_reconfig_nb
);
1955 __initcall(prom_reconfig_setup
);
1958 struct property
*of_find_property(struct device_node
*np
, const char *name
,
1961 struct property
*pp
;
1963 read_lock(&devtree_lock
);
1964 for (pp
= np
->properties
; pp
!= 0; pp
= pp
->next
)
1965 if (strcmp(pp
->name
, name
) == 0) {
1970 read_unlock(&devtree_lock
);
1976 * Find a property with a given name for a given node
1977 * and return the value.
1979 unsigned char *get_property(struct device_node
*np
, const char *name
,
1982 struct property
*pp
= of_find_property(np
,name
,lenp
);
1983 return pp
? pp
->value
: NULL
;
1985 EXPORT_SYMBOL(get_property
);
1988 * Add a property to a node
1990 int prom_add_property(struct device_node
* np
, struct property
* prop
)
1992 struct property
**next
;
1995 write_lock(&devtree_lock
);
1996 next
= &np
->properties
;
1998 if (strcmp(prop
->name
, (*next
)->name
) == 0) {
1999 /* duplicate ! don't insert it */
2000 write_unlock(&devtree_lock
);
2003 next
= &(*next
)->next
;
2006 write_unlock(&devtree_lock
);
2008 #ifdef CONFIG_PROC_DEVICETREE
2009 /* try to add to proc as well if it was initialized */
2011 proc_device_tree_add_prop(np
->pde
, prop
);
2012 #endif /* CONFIG_PROC_DEVICETREE */
2018 * Remove a property from a node. Note that we don't actually
2019 * remove it, since we have given out who-knows-how-many pointers
2020 * to the data using get-property. Instead we just move the property
2021 * to the "dead properties" list, so it won't be found any more.
2023 int prom_remove_property(struct device_node
*np
, struct property
*prop
)
2025 struct property
**next
;
2028 write_lock(&devtree_lock
);
2029 next
= &np
->properties
;
2031 if (*next
== prop
) {
2032 /* found the node */
2034 prop
->next
= np
->deadprops
;
2035 np
->deadprops
= prop
;
2039 next
= &(*next
)->next
;
2041 write_unlock(&devtree_lock
);
2046 #ifdef CONFIG_PROC_DEVICETREE
2047 /* try to remove the proc node as well */
2049 proc_device_tree_remove_prop(np
->pde
, prop
);
2050 #endif /* CONFIG_PROC_DEVICETREE */
2056 * Update a property in a node. Note that we don't actually
2057 * remove it, since we have given out who-knows-how-many pointers
2058 * to the data using get-property. Instead we just move the property
2059 * to the "dead properties" list, and add the new property to the
2062 int prom_update_property(struct device_node
*np
,
2063 struct property
*newprop
,
2064 struct property
*oldprop
)
2066 struct property
**next
;
2069 write_lock(&devtree_lock
);
2070 next
= &np
->properties
;
2072 if (*next
== oldprop
) {
2073 /* found the node */
2074 newprop
->next
= oldprop
->next
;
2076 oldprop
->next
= np
->deadprops
;
2077 np
->deadprops
= oldprop
;
2081 next
= &(*next
)->next
;
2083 write_unlock(&devtree_lock
);
2088 #ifdef CONFIG_PROC_DEVICETREE
2089 /* try to add to proc as well if it was initialized */
2091 proc_device_tree_update_prop(np
->pde
, newprop
, oldprop
);
2092 #endif /* CONFIG_PROC_DEVICETREE */
2098 /* Find the device node for a given logical cpu number, also returns the cpu
2099 * local thread number (index in ibm,interrupt-server#s) if relevant and
2100 * asked for (non NULL)
2102 struct device_node
*of_get_cpu_node(int cpu
, unsigned int *thread
)
2105 struct device_node
*np
;
2107 hardid
= get_hard_smp_processor_id(cpu
);
2109 for_each_node_by_type(np
, "cpu") {
2111 unsigned int plen
, t
;
2113 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
2114 * fallback to "reg" property and assume no threads
2116 intserv
= (u32
*)get_property(np
, "ibm,ppc-interrupt-server#s",
2118 if (intserv
== NULL
) {
2119 u32
*reg
= (u32
*)get_property(np
, "reg", NULL
);
2122 if (*reg
== hardid
) {
2128 plen
/= sizeof(u32
);
2129 for (t
= 0; t
< plen
; t
++) {
2130 if (hardid
== intserv
[t
]) {
2142 static struct debugfs_blob_wrapper flat_dt_blob
;
2144 static int __init
export_flat_device_tree(void)
2148 d
= debugfs_create_dir("powerpc", NULL
);
2152 flat_dt_blob
.data
= initial_boot_params
;
2153 flat_dt_blob
.size
= initial_boot_params
->totalsize
;
2155 d
= debugfs_create_blob("flat-device-tree", S_IFREG
| S_IRUSR
,
2162 __initcall(export_flat_device_tree
);