4 * Procedures for interfacing to Open Firmware.
6 * Paul Mackerras August 1996.
7 * Copyright (C) 1996 Paul Mackerras.
9 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
10 * {engebret|bergner}@us.ibm.com
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
21 #include <linux/config.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/init.h>
25 #include <linux/version.h>
26 #include <linux/threads.h>
27 #include <linux/spinlock.h>
28 #include <linux/types.h>
29 #include <linux/pci.h>
30 #include <linux/stringify.h>
31 #include <linux/delay.h>
32 #include <linux/initrd.h>
33 #include <linux/bitops.h>
34 #include <linux/module.h>
39 #include <asm/abs_addr.h>
41 #include <asm/processor.h>
45 #include <asm/system.h>
47 #include <asm/pgtable.h>
49 #include <asm/iommu.h>
50 #include <asm/bootinfo.h>
51 #include <asm/ppcdebug.h>
52 #include <asm/btext.h>
53 #include <asm/sections.h>
54 #include <asm/machdep.h>
55 #include <asm/pSeries_reconfig.h>
58 #define DBG(fmt...) udbg_printf(fmt)
63 struct pci_reg_property
{
64 struct pci_address addr
;
69 struct isa_reg_property
{
76 typedef int interpret_func(struct device_node
*, unsigned long *,
79 extern struct rtas_t rtas
;
80 extern struct lmb lmb
;
81 extern unsigned long klimit
;
83 static int __initdata dt_root_addr_cells
;
84 static int __initdata dt_root_size_cells
;
85 static int __initdata iommu_is_off
;
86 int __initdata iommu_force_on
;
90 static struct boot_param_header
*initial_boot_params __initdata
;
92 struct boot_param_header
*initial_boot_params
;
95 static struct device_node
*allnodes
= NULL
;
97 /* use when traversing tree through the allnext, child, sibling,
98 * or parent members of struct device_node.
100 static DEFINE_RWLOCK(devtree_lock
);
102 /* export that to outside world */
103 struct device_node
*of_chosen
;
106 * Wrapper for allocating memory for various data that needs to be
107 * attached to device nodes as they are processed at boot or when
108 * added to the device tree later (e.g. DLPAR). At boot there is
109 * already a region reserved so we just increment *mem_start by size;
110 * otherwise we call kmalloc.
112 static void * prom_alloc(unsigned long size
, unsigned long *mem_start
)
117 return kmalloc(size
, GFP_KERNEL
);
125 * Find the device_node with a given phandle.
127 static struct device_node
* find_phandle(phandle ph
)
129 struct device_node
*np
;
131 for (np
= allnodes
; np
!= 0; np
= np
->allnext
)
132 if (np
->linux_phandle
== ph
)
138 * Find the interrupt parent of a node.
140 static struct device_node
* __devinit
intr_parent(struct device_node
*p
)
144 parp
= (phandle
*) get_property(p
, "interrupt-parent", NULL
);
147 return find_phandle(*parp
);
151 * Find out the size of each entry of the interrupts property
154 int __devinit
prom_n_intr_cells(struct device_node
*np
)
156 struct device_node
*p
;
159 for (p
= np
; (p
= intr_parent(p
)) != NULL
; ) {
160 icp
= (unsigned int *)
161 get_property(p
, "#interrupt-cells", NULL
);
164 if (get_property(p
, "interrupt-controller", NULL
) != NULL
165 || get_property(p
, "interrupt-map", NULL
) != NULL
) {
166 printk("oops, node %s doesn't have #interrupt-cells\n",
172 printk("prom_n_intr_cells failed for %s\n", np
->full_name
);
178 * Map an interrupt from a device up to the platform interrupt
181 static int __devinit
map_interrupt(unsigned int **irq
, struct device_node
**ictrler
,
182 struct device_node
*np
, unsigned int *ints
,
185 struct device_node
*p
, *ipar
;
186 unsigned int *imap
, *imask
, *ip
;
187 int i
, imaplen
, match
;
188 int newintrc
= 0, newaddrc
= 0;
192 reg
= (unsigned int *) get_property(np
, "reg", NULL
);
193 naddrc
= prom_n_addr_cells(np
);
196 if (get_property(p
, "interrupt-controller", NULL
) != NULL
)
197 /* this node is an interrupt controller, stop here */
199 imap
= (unsigned int *)
200 get_property(p
, "interrupt-map", &imaplen
);
205 imask
= (unsigned int *)
206 get_property(p
, "interrupt-map-mask", NULL
);
208 printk("oops, %s has interrupt-map but no mask\n",
212 imaplen
/= sizeof(unsigned int);
215 while (imaplen
> 0 && !match
) {
216 /* check the child-interrupt field */
218 for (i
= 0; i
< naddrc
&& match
; ++i
)
219 match
= ((reg
[i
] ^ imap
[i
]) & imask
[i
]) == 0;
220 for (; i
< naddrc
+ nintrc
&& match
; ++i
)
221 match
= ((ints
[i
-naddrc
] ^ imap
[i
]) & imask
[i
]) == 0;
222 imap
+= naddrc
+ nintrc
;
223 imaplen
-= naddrc
+ nintrc
;
224 /* grab the interrupt parent */
225 ipar
= find_phandle((phandle
) *imap
++);
228 printk("oops, no int parent %x in map of %s\n",
229 imap
[-1], p
->full_name
);
232 /* find the parent's # addr and intr cells */
233 ip
= (unsigned int *)
234 get_property(ipar
, "#interrupt-cells", NULL
);
236 printk("oops, no #interrupt-cells on %s\n",
241 ip
= (unsigned int *)
242 get_property(ipar
, "#address-cells", NULL
);
243 newaddrc
= (ip
== NULL
)? 0: *ip
;
244 imap
+= newaddrc
+ newintrc
;
245 imaplen
-= newaddrc
+ newintrc
;
248 printk("oops, error decoding int-map on %s, len=%d\n",
249 p
->full_name
, imaplen
);
254 printk("oops, no match in %s int-map for %s\n",
255 p
->full_name
, np
->full_name
);
262 ints
= imap
- nintrc
;
267 printk("hmmm, int tree for %s doesn't have ctrler\n",
277 static int __devinit
finish_node_interrupts(struct device_node
*np
,
278 unsigned long *mem_start
,
282 int intlen
, intrcells
, intrcount
;
284 unsigned int *irq
, virq
;
285 struct device_node
*ic
;
287 ints
= (unsigned int *) get_property(np
, "interrupts", &intlen
);
290 intrcells
= prom_n_intr_cells(np
);
291 intlen
/= intrcells
* sizeof(unsigned int);
293 np
->intrs
= prom_alloc(intlen
* sizeof(*(np
->intrs
)), mem_start
);
301 for (i
= 0; i
< intlen
; ++i
, ints
+= intrcells
) {
302 n
= map_interrupt(&irq
, &ic
, np
, ints
, intrcells
);
306 /* don't map IRQ numbers under a cascaded 8259 controller */
307 if (ic
&& device_is_compatible(ic
, "chrp,iic")) {
308 np
->intrs
[intrcount
].line
= irq
[0];
310 virq
= virt_irq_create_mapping(irq
[0]);
311 if (virq
== NO_IRQ
) {
312 printk(KERN_CRIT
"Could not allocate interrupt"
313 " number for %s\n", np
->full_name
);
316 np
->intrs
[intrcount
].line
= irq_offset_up(virq
);
319 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
320 if (systemcfg
->platform
== PLATFORM_POWERMAC
&& ic
&& ic
->parent
) {
321 char *name
= get_property(ic
->parent
, "name", NULL
);
322 if (name
&& !strcmp(name
, "u3"))
323 np
->intrs
[intrcount
].line
+= 128;
325 np
->intrs
[intrcount
].sense
= 1;
327 np
->intrs
[intrcount
].sense
= irq
[1];
329 printk("hmmm, got %d intr cells for %s:", n
,
331 for (j
= 0; j
< n
; ++j
)
332 printk(" %d", irq
[j
]);
337 np
->n_intrs
= intrcount
;
342 static int __devinit
interpret_pci_props(struct device_node
*np
,
343 unsigned long *mem_start
,
344 int naddrc
, int nsizec
,
347 struct address_range
*adr
;
348 struct pci_reg_property
*pci_addrs
;
351 pci_addrs
= (struct pci_reg_property
*)
352 get_property(np
, "assigned-addresses", &l
);
356 n_addrs
= l
/ sizeof(*pci_addrs
);
358 adr
= prom_alloc(n_addrs
* sizeof(*adr
), mem_start
);
366 np
->n_addrs
= n_addrs
;
368 for (i
= 0; i
< n_addrs
; i
++) {
369 adr
[i
].space
= pci_addrs
[i
].addr
.a_hi
;
370 adr
[i
].address
= pci_addrs
[i
].addr
.a_lo
|
371 ((u64
)pci_addrs
[i
].addr
.a_mid
<< 32);
372 adr
[i
].size
= pci_addrs
[i
].size_lo
;
378 static int __init
interpret_dbdma_props(struct device_node
*np
,
379 unsigned long *mem_start
,
380 int naddrc
, int nsizec
,
383 struct reg_property32
*rp
;
384 struct address_range
*adr
;
385 unsigned long base_address
;
387 struct device_node
*db
;
391 for (db
= np
->parent
; db
!= NULL
; db
= db
->parent
) {
392 if (!strcmp(db
->type
, "dbdma") && db
->n_addrs
!= 0) {
393 base_address
= db
->addrs
[0].address
;
399 rp
= (struct reg_property32
*) get_property(np
, "reg", &l
);
400 if (rp
!= 0 && l
>= sizeof(struct reg_property32
)) {
402 adr
= (struct address_range
*) (*mem_start
);
403 while ((l
-= sizeof(struct reg_property32
)) >= 0) {
406 adr
[i
].address
= rp
[i
].address
+ base_address
;
407 adr
[i
].size
= rp
[i
].size
;
413 (*mem_start
) += i
* sizeof(struct address_range
);
419 static int __init
interpret_macio_props(struct device_node
*np
,
420 unsigned long *mem_start
,
421 int naddrc
, int nsizec
,
424 struct reg_property32
*rp
;
425 struct address_range
*adr
;
426 unsigned long base_address
;
428 struct device_node
*db
;
432 for (db
= np
->parent
; db
!= NULL
; db
= db
->parent
) {
433 if (!strcmp(db
->type
, "mac-io") && db
->n_addrs
!= 0) {
434 base_address
= db
->addrs
[0].address
;
440 rp
= (struct reg_property32
*) get_property(np
, "reg", &l
);
441 if (rp
!= 0 && l
>= sizeof(struct reg_property32
)) {
443 adr
= (struct address_range
*) (*mem_start
);
444 while ((l
-= sizeof(struct reg_property32
)) >= 0) {
447 adr
[i
].address
= rp
[i
].address
+ base_address
;
448 adr
[i
].size
= rp
[i
].size
;
454 (*mem_start
) += i
* sizeof(struct address_range
);
460 static int __init
interpret_isa_props(struct device_node
*np
,
461 unsigned long *mem_start
,
462 int naddrc
, int nsizec
,
465 struct isa_reg_property
*rp
;
466 struct address_range
*adr
;
469 rp
= (struct isa_reg_property
*) get_property(np
, "reg", &l
);
470 if (rp
!= 0 && l
>= sizeof(struct isa_reg_property
)) {
472 adr
= (struct address_range
*) (*mem_start
);
473 while ((l
-= sizeof(struct isa_reg_property
)) >= 0) {
475 adr
[i
].space
= rp
[i
].space
;
476 adr
[i
].address
= rp
[i
].address
;
477 adr
[i
].size
= rp
[i
].size
;
483 (*mem_start
) += i
* sizeof(struct address_range
);
489 static int __init
interpret_root_props(struct device_node
*np
,
490 unsigned long *mem_start
,
491 int naddrc
, int nsizec
,
494 struct address_range
*adr
;
497 int rpsize
= (naddrc
+ nsizec
) * sizeof(unsigned int);
499 rp
= (unsigned int *) get_property(np
, "reg", &l
);
500 if (rp
!= 0 && l
>= rpsize
) {
502 adr
= (struct address_range
*) (*mem_start
);
503 while ((l
-= rpsize
) >= 0) {
506 adr
[i
].address
= rp
[naddrc
- 1];
507 adr
[i
].size
= rp
[naddrc
+ nsizec
- 1];
510 rp
+= naddrc
+ nsizec
;
514 (*mem_start
) += i
* sizeof(struct address_range
);
520 static int __devinit
finish_node(struct device_node
*np
,
521 unsigned long *mem_start
,
522 interpret_func
*ifunc
,
523 int naddrc
, int nsizec
,
526 struct device_node
*child
;
529 /* get the device addresses and interrupts */
531 rc
= ifunc(np
, mem_start
, naddrc
, nsizec
, measure_only
);
535 rc
= finish_node_interrupts(np
, mem_start
, measure_only
);
539 /* Look for #address-cells and #size-cells properties. */
540 ip
= (int *) get_property(np
, "#address-cells", NULL
);
543 ip
= (int *) get_property(np
, "#size-cells", NULL
);
547 /* the f50 sets the name to 'display' and 'compatible' to what we
548 * expect for the name -- Cort
550 if (!strcmp(np
->name
, "display"))
551 np
->name
= get_property(np
, "compatible", NULL
);
553 if (!strcmp(np
->name
, "device-tree") || np
->parent
== NULL
)
554 ifunc
= interpret_root_props
;
555 else if (np
->type
== 0)
557 else if (!strcmp(np
->type
, "pci") || !strcmp(np
->type
, "vci"))
558 ifunc
= interpret_pci_props
;
559 else if (!strcmp(np
->type
, "dbdma"))
560 ifunc
= interpret_dbdma_props
;
561 else if (!strcmp(np
->type
, "mac-io") || ifunc
== interpret_macio_props
)
562 ifunc
= interpret_macio_props
;
563 else if (!strcmp(np
->type
, "isa"))
564 ifunc
= interpret_isa_props
;
565 else if (!strcmp(np
->name
, "uni-n") || !strcmp(np
->name
, "u3"))
566 ifunc
= interpret_root_props
;
567 else if (!((ifunc
== interpret_dbdma_props
568 || ifunc
== interpret_macio_props
)
569 && (!strcmp(np
->type
, "escc")
570 || !strcmp(np
->type
, "media-bay"))))
573 for (child
= np
->child
; child
!= NULL
; child
= child
->sibling
) {
574 rc
= finish_node(child
, mem_start
, ifunc
,
575 naddrc
, nsizec
, measure_only
);
584 * finish_device_tree is called once things are running normally
585 * (i.e. with text and data mapped to the address they were linked at).
586 * It traverses the device tree and fills in some of the additional,
587 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
588 * mapping is also initialized at this point.
590 void __init
finish_device_tree(void)
592 unsigned long start
, end
, size
= 0;
594 DBG(" -> finish_device_tree\n");
596 if (ppc64_interrupt_controller
== IC_INVALID
) {
597 DBG("failed to configure interrupt controller type\n");
598 panic("failed to configure interrupt controller type\n");
601 /* Initialize virtual IRQ map */
605 * Finish device-tree (pre-parsing some properties etc...)
606 * We do this in 2 passes. One with "measure_only" set, which
607 * will only measure the amount of memory needed, then we can
608 * allocate that memory, and call finish_node again. However,
609 * we must be careful as most routines will fail nowadays when
610 * prom_alloc() returns 0, so we must make sure our first pass
611 * doesn't start at 0. We pre-initialize size to 16 for that
612 * reason and then remove those additional 16 bytes
615 finish_node(allnodes
, &size
, NULL
, 0, 0, 1);
617 end
= start
= (unsigned long)abs_to_virt(lmb_alloc(size
, 128));
618 finish_node(allnodes
, &end
, NULL
, 0, 0, 0);
619 BUG_ON(end
!= start
+ size
);
621 DBG(" <- finish_device_tree\n");
625 #define printk udbg_printf
628 static inline char *find_flat_dt_string(u32 offset
)
630 return ((char *)initial_boot_params
) + initial_boot_params
->off_dt_strings
635 * This function is used to scan the flattened device-tree, it is
636 * used to extract the memory informations at boot before we can
639 static int __init
scan_flat_dt(int (*it
)(unsigned long node
,
640 const char *full_path
, void *data
),
643 unsigned long p
= ((unsigned long)initial_boot_params
) +
644 initial_boot_params
->off_dt_struct
;
648 u32 tag
= *((u32
*)p
);
652 if (tag
== OF_DT_END_NODE
)
654 if (tag
== OF_DT_END
)
656 if (tag
== OF_DT_PROP
) {
657 u32 sz
= *((u32
*)p
);
659 p
= _ALIGN(p
, sz
>= 8 ? 8 : 4);
664 if (tag
!= OF_DT_BEGIN_NODE
) {
665 printk(KERN_WARNING
"Invalid tag %x scanning flattened"
666 " device tree !\n", tag
);
670 p
= _ALIGN(p
+ strlen(pathp
) + 1, 4);
671 rc
= it(p
, pathp
, data
);
680 * This function can be used within scan_flattened_dt callback to get
681 * access to properties
683 static void* __init
get_flat_dt_prop(unsigned long node
, const char *name
,
686 unsigned long p
= node
;
689 u32 tag
= *((u32
*)p
);
694 if (tag
!= OF_DT_PROP
)
698 noff
= *((u32
*)(p
+ 4));
700 p
= _ALIGN(p
, sz
>= 8 ? 8 : 4);
702 nstr
= find_flat_dt_string(noff
);
704 printk(KERN_WARNING
"Can't find property index name !\n");
707 if (strcmp(name
, nstr
) == 0) {
717 static void *__init
unflatten_dt_alloc(unsigned long *mem
, unsigned long size
,
722 *mem
= _ALIGN(*mem
, align
);
729 static unsigned long __init
unflatten_dt_node(unsigned long mem
,
731 struct device_node
*dad
,
732 struct device_node
***allnextpp
)
734 struct device_node
*np
;
735 struct property
*pp
, **prev_pp
= NULL
;
740 tag
= *((u32
*)(*p
));
741 if (tag
!= OF_DT_BEGIN_NODE
) {
742 printk("Weird tag at start of node: %x\n", tag
);
747 l
= strlen(pathp
) + 1;
748 *p
= _ALIGN(*p
+ l
, 4);
750 np
= unflatten_dt_alloc(&mem
, sizeof(struct device_node
) + l
,
751 __alignof__(struct device_node
));
753 memset(np
, 0, sizeof(*np
));
754 np
->full_name
= ((char*)np
) + sizeof(struct device_node
);
755 memcpy(np
->full_name
, pathp
, l
);
756 prev_pp
= &np
->properties
;
758 *allnextpp
= &np
->allnext
;
761 /* we temporarily use the `next' field as `last_child'. */
765 dad
->next
->sibling
= np
;
768 kref_init(&np
->kref
);
774 tag
= *((u32
*)(*p
));
775 if (tag
!= OF_DT_PROP
)
779 noff
= *((u32
*)((*p
) + 4));
780 *p
= _ALIGN((*p
) + 8, sz
>= 8 ? 8 : 4);
782 pname
= find_flat_dt_string(noff
);
784 printk("Can't find property name in list !\n");
787 l
= strlen(pname
) + 1;
788 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
789 __alignof__(struct property
));
791 if (strcmp(pname
, "linux,phandle") == 0) {
792 np
->node
= *((u32
*)*p
);
793 if (np
->linux_phandle
== 0)
794 np
->linux_phandle
= np
->node
;
796 if (strcmp(pname
, "ibm,phandle") == 0)
797 np
->linux_phandle
= *((u32
*)*p
);
800 pp
->value
= (void *)*p
;
804 *p
= _ALIGN((*p
) + sz
, 4);
808 np
->name
= get_property(np
, "name", NULL
);
809 np
->type
= get_property(np
, "device_type", NULL
);
816 while (tag
== OF_DT_BEGIN_NODE
) {
817 mem
= unflatten_dt_node(mem
, p
, np
, allnextpp
);
818 tag
= *((u32
*)(*p
));
820 if (tag
!= OF_DT_END_NODE
) {
821 printk("Weird tag at start of node: %x\n", tag
);
830 * unflattens the device-tree passed by the firmware, creating the
831 * tree of struct device_node. It also fills the "name" and "type"
832 * pointers of the nodes so the normal device-tree walking functions
833 * can be used (this used to be done by finish_device_tree)
835 void __init
unflatten_device_tree(void)
837 unsigned long start
, mem
, size
;
838 struct device_node
**allnextp
= &allnodes
;
842 DBG(" -> unflatten_device_tree()\n");
844 /* First pass, scan for size */
845 start
= ((unsigned long)initial_boot_params
) +
846 initial_boot_params
->off_dt_struct
;
847 size
= unflatten_dt_node(0, &start
, NULL
, NULL
);
849 DBG(" size is %lx, allocating...\n", size
);
851 /* Allocate memory for the expanded device tree */
852 mem
= (unsigned long)abs_to_virt(lmb_alloc(size
,
853 __alignof__(struct device_node
)));
854 DBG(" unflattening...\n", mem
);
856 /* Second pass, do actual unflattening */
857 start
= ((unsigned long)initial_boot_params
) +
858 initial_boot_params
->off_dt_struct
;
859 unflatten_dt_node(mem
, &start
, NULL
, &allnextp
);
860 if (*((u32
*)start
) != OF_DT_END
)
861 printk(KERN_WARNING
"Weird tag at end of tree: %x\n", *((u32
*)start
));
864 /* Get pointer to OF "/chosen" node for use everywhere */
865 of_chosen
= of_find_node_by_path("/chosen");
867 /* Retreive command line */
868 if (of_chosen
!= NULL
) {
869 p
= (char *)get_property(of_chosen
, "bootargs", &l
);
870 if (p
!= NULL
&& l
> 0)
871 strlcpy(cmd_line
, p
, min(l
, COMMAND_LINE_SIZE
));
873 #ifdef CONFIG_CMDLINE
874 if (l
== 0 || (l
== 1 && (*p
) == 0))
875 strlcpy(cmd_line
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
876 #endif /* CONFIG_CMDLINE */
878 DBG("Command line is: %s\n", cmd_line
);
880 DBG(" <- unflatten_device_tree()\n");
884 static int __init
early_init_dt_scan_cpus(unsigned long node
,
885 const char *full_path
, void *data
)
887 char *type
= get_flat_dt_prop(node
, "device_type", NULL
);
889 /* We are scanning "cpu" nodes only */
890 if (type
== NULL
|| strcmp(type
, "cpu") != 0)
893 /* On LPAR, look for the first ibm,pft-size property for the hash table size
895 if (systemcfg
->platform
== PLATFORM_PSERIES_LPAR
&& ppc64_pft_size
== 0) {
897 pft_size
= (u32
*)get_flat_dt_prop(node
, "ibm,pft-size", NULL
);
898 if (pft_size
!= NULL
) {
899 /* pft_size[0] is the NUMA CEC cookie */
900 ppc64_pft_size
= pft_size
[1];
904 if (initial_boot_params
&& initial_boot_params
->version
>= 2) {
905 /* version 2 of the kexec param format adds the phys cpuid
908 boot_cpuid_phys
= initial_boot_params
->boot_cpuid_phys
;
911 /* Check if it's the boot-cpu, set it's hw index in paca now */
912 if (get_flat_dt_prop(node
, "linux,boot-cpu", NULL
) != NULL
) {
913 u32
*prop
= get_flat_dt_prop(node
, "reg", NULL
);
914 set_hard_smp_processor_id(0, prop
== NULL
? 0 : *prop
);
915 boot_cpuid_phys
= get_hard_smp_processor_id(0);
922 static int __init
early_init_dt_scan_chosen(unsigned long node
,
923 const char *full_path
, void *data
)
927 extern unsigned long memory_limit
, tce_alloc_start
, tce_alloc_end
;
929 if (strcmp(full_path
, "/chosen") != 0)
932 /* get platform type */
933 prop
= (u32
*)get_flat_dt_prop(node
, "linux,platform", NULL
);
936 systemcfg
->platform
= *prop
;
938 /* check if iommu is forced on or off */
939 if (get_flat_dt_prop(node
, "linux,iommu-off", NULL
) != NULL
)
941 if (get_flat_dt_prop(node
, "linux,iommu-force-on", NULL
) != NULL
)
944 prop64
= (u64
*)get_flat_dt_prop(node
, "linux,memory-limit", NULL
);
946 memory_limit
= *prop64
;
948 prop64
= (u64
*)get_flat_dt_prop(node
, "linux,tce-alloc-start", NULL
);
950 tce_alloc_start
= *prop64
;
952 prop64
= (u64
*)get_flat_dt_prop(node
, "linux,tce-alloc-end", NULL
);
954 tce_alloc_end
= *prop64
;
956 #ifdef CONFIG_PPC_RTAS
957 /* To help early debugging via the front panel, we retreive a minimal
958 * set of RTAS infos now if available
963 basep
= (u64
*)get_flat_dt_prop(node
, "linux,rtas-base", NULL
);
964 entryp
= (u64
*)get_flat_dt_prop(node
, "linux,rtas-entry", NULL
);
965 prop
= (u32
*)get_flat_dt_prop(node
, "linux,rtas-size", NULL
);
966 if (basep
&& entryp
&& prop
) {
968 rtas
.entry
= *entryp
;
972 #endif /* CONFIG_PPC_RTAS */
978 static int __init
early_init_dt_scan_root(unsigned long node
,
979 const char *full_path
, void *data
)
983 if (strcmp(full_path
, "/") != 0)
986 prop
= (u32
*)get_flat_dt_prop(node
, "#size-cells", NULL
);
987 dt_root_size_cells
= (prop
== NULL
) ? 1 : *prop
;
989 prop
= (u32
*)get_flat_dt_prop(node
, "#address-cells", NULL
);
990 dt_root_addr_cells
= (prop
== NULL
) ? 2 : *prop
;
996 static unsigned long __init
dt_mem_next_cell(int s
, cell_t
**cellp
)
1001 /* Ignore more than 2 cells */
1017 static int __init
early_init_dt_scan_memory(unsigned long node
,
1018 const char *full_path
, void *data
)
1020 char *type
= get_flat_dt_prop(node
, "device_type", NULL
);
1024 /* We are scanning "memory" nodes only */
1025 if (type
== NULL
|| strcmp(type
, "memory") != 0)
1028 reg
= (cell_t
*)get_flat_dt_prop(node
, "reg", &l
);
1032 endp
= reg
+ (l
/ sizeof(cell_t
));
1034 DBG("memory scan node %s ...\n", full_path
);
1035 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
1036 unsigned long base
, size
;
1038 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
1039 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
1043 DBG(" - %lx , %lx\n", base
, size
);
1045 if (base
>= 0x80000000ul
)
1047 if ((base
+ size
) > 0x80000000ul
)
1048 size
= 0x80000000ul
- base
;
1050 lmb_add(base
, size
);
1055 static void __init
early_reserve_mem(void)
1058 u64
*reserve_map
= (u64
*)(((unsigned long)initial_boot_params
) +
1059 initial_boot_params
->off_mem_rsvmap
);
1061 base
= *(reserve_map
++);
1062 size
= *(reserve_map
++);
1065 DBG("reserving: %lx -> %lx\n", base
, size
);
1066 lmb_reserve(base
, size
);
1070 DBG("memory reserved, lmbs :\n");
1075 void __init
early_init_devtree(void *params
)
1077 DBG(" -> early_init_devtree()\n");
1079 /* Setup flat device-tree pointer */
1080 initial_boot_params
= params
;
1082 /* By default, hash size is not set */
1085 /* Retreive various informations from the /chosen node of the
1086 * device-tree, including the platform type, initrd location and
1087 * size, TCE reserve, and more ...
1089 scan_flat_dt(early_init_dt_scan_chosen
, NULL
);
1091 /* Scan memory nodes and rebuild LMBs */
1093 scan_flat_dt(early_init_dt_scan_root
, NULL
);
1094 scan_flat_dt(early_init_dt_scan_memory
, NULL
);
1095 lmb_enforce_memory_limit();
1097 systemcfg
->physicalMemorySize
= lmb_phys_mem_size();
1098 lmb_reserve(0, __pa(klimit
));
1100 DBG("Phys. mem: %lx\n", systemcfg
->physicalMemorySize
);
1102 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1103 early_reserve_mem();
1105 DBG("Scanning CPUs ...\n");
1107 /* Retreive hash table size from flattened tree */
1108 scan_flat_dt(early_init_dt_scan_cpus
, NULL
);
1110 /* If hash size wasn't obtained above, we calculate it now based on
1111 * the total RAM size
1113 if (ppc64_pft_size
== 0) {
1114 unsigned long rnd_mem_size
, pteg_count
;
1116 /* round mem_size up to next power of 2 */
1117 rnd_mem_size
= 1UL << __ilog2(systemcfg
->physicalMemorySize
);
1118 if (rnd_mem_size
< systemcfg
->physicalMemorySize
)
1122 pteg_count
= max(rnd_mem_size
>> (12 + 1), 1UL << 11);
1124 ppc64_pft_size
= __ilog2(pteg_count
<< 7);
1127 DBG("Hash pftSize: %x\n", (int)ppc64_pft_size
);
1128 DBG(" <- early_init_devtree()\n");
1134 prom_n_addr_cells(struct device_node
* np
)
1140 ip
= (int *) get_property(np
, "#address-cells", NULL
);
1143 } while (np
->parent
);
1144 /* No #address-cells property for the root node, default to 1 */
1149 prom_n_size_cells(struct device_node
* np
)
1155 ip
= (int *) get_property(np
, "#size-cells", NULL
);
1158 } while (np
->parent
);
1159 /* No #size-cells property for the root node, default to 1 */
1164 * Work out the sense (active-low level / active-high edge)
1165 * of each interrupt from the device tree.
1167 void __init
prom_get_irq_senses(unsigned char *senses
, int off
, int max
)
1169 struct device_node
*np
;
1172 /* default to level-triggered */
1173 memset(senses
, 1, max
- off
);
1175 for (np
= allnodes
; np
!= 0; np
= np
->allnext
) {
1176 for (j
= 0; j
< np
->n_intrs
; j
++) {
1177 i
= np
->intrs
[j
].line
;
1178 if (i
>= off
&& i
< max
)
1179 senses
[i
-off
] = np
->intrs
[j
].sense
?
1180 IRQ_SENSE_LEVEL
| IRQ_POLARITY_NEGATIVE
:
1181 IRQ_SENSE_EDGE
| IRQ_POLARITY_POSITIVE
;
1187 * Construct and return a list of the device_nodes with a given name.
1189 struct device_node
*
1190 find_devices(const char *name
)
1192 struct device_node
*head
, **prevp
, *np
;
1195 for (np
= allnodes
; np
!= 0; np
= np
->allnext
) {
1196 if (np
->name
!= 0 && strcasecmp(np
->name
, name
) == 0) {
1204 EXPORT_SYMBOL(find_devices
);
1207 * Construct and return a list of the device_nodes with a given type.
1209 struct device_node
*
1210 find_type_devices(const char *type
)
1212 struct device_node
*head
, **prevp
, *np
;
1215 for (np
= allnodes
; np
!= 0; np
= np
->allnext
) {
1216 if (np
->type
!= 0 && strcasecmp(np
->type
, type
) == 0) {
1224 EXPORT_SYMBOL(find_type_devices
);
1227 * Returns all nodes linked together
1229 struct device_node
*
1230 find_all_nodes(void)
1232 struct device_node
*head
, **prevp
, *np
;
1235 for (np
= allnodes
; np
!= 0; np
= np
->allnext
) {
1242 EXPORT_SYMBOL(find_all_nodes
);
1244 /** Checks if the given "compat" string matches one of the strings in
1245 * the device's "compatible" property
1248 device_is_compatible(struct device_node
*device
, const char *compat
)
1253 cp
= (char *) get_property(device
, "compatible", &cplen
);
1257 if (strncasecmp(cp
, compat
, strlen(compat
)) == 0)
1266 EXPORT_SYMBOL(device_is_compatible
);
1270 * Indicates whether the root node has a given value in its
1271 * compatible property.
1274 machine_is_compatible(const char *compat
)
1276 struct device_node
*root
;
1279 root
= of_find_node_by_path("/");
1281 rc
= device_is_compatible(root
, compat
);
1286 EXPORT_SYMBOL(machine_is_compatible
);
1289 * Construct and return a list of the device_nodes with a given type
1290 * and compatible property.
1292 struct device_node
*
1293 find_compatible_devices(const char *type
, const char *compat
)
1295 struct device_node
*head
, **prevp
, *np
;
1298 for (np
= allnodes
; np
!= 0; np
= np
->allnext
) {
1300 && !(np
->type
!= 0 && strcasecmp(np
->type
, type
) == 0))
1302 if (device_is_compatible(np
, compat
)) {
1310 EXPORT_SYMBOL(find_compatible_devices
);
1313 * Find the device_node with a given full_name.
1315 struct device_node
*
1316 find_path_device(const char *path
)
1318 struct device_node
*np
;
1320 for (np
= allnodes
; np
!= 0; np
= np
->allnext
)
1321 if (np
->full_name
!= 0 && strcasecmp(np
->full_name
, path
) == 0)
1325 EXPORT_SYMBOL(find_path_device
);
1329 * New implementation of the OF "find" APIs, return a refcounted
1330 * object, call of_node_put() when done. The device tree and list
1331 * are protected by a rw_lock.
1333 * Note that property management will need some locking as well,
1334 * this isn't dealt with yet.
1339 * of_find_node_by_name - Find a node by its "name" property
1340 * @from: The node to start searching from or NULL, the node
1341 * you pass will not be searched, only the next one
1342 * will; typically, you pass what the previous call
1343 * returned. of_node_put() will be called on it
1344 * @name: The name string to match against
1346 * Returns a node pointer with refcount incremented, use
1347 * of_node_put() on it when done.
1349 struct device_node
*of_find_node_by_name(struct device_node
*from
,
1352 struct device_node
*np
;
1354 read_lock(&devtree_lock
);
1355 np
= from
? from
->allnext
: allnodes
;
1356 for (; np
!= 0; np
= np
->allnext
)
1357 if (np
->name
!= 0 && strcasecmp(np
->name
, name
) == 0
1362 read_unlock(&devtree_lock
);
1365 EXPORT_SYMBOL(of_find_node_by_name
);
1368 * of_find_node_by_type - Find a node by its "device_type" property
1369 * @from: The node to start searching from or NULL, the node
1370 * you pass will not be searched, only the next one
1371 * will; typically, you pass what the previous call
1372 * returned. of_node_put() will be called on it
1373 * @name: The type string to match against
1375 * Returns a node pointer with refcount incremented, use
1376 * of_node_put() on it when done.
1378 struct device_node
*of_find_node_by_type(struct device_node
*from
,
1381 struct device_node
*np
;
1383 read_lock(&devtree_lock
);
1384 np
= from
? from
->allnext
: allnodes
;
1385 for (; np
!= 0; np
= np
->allnext
)
1386 if (np
->type
!= 0 && strcasecmp(np
->type
, type
) == 0
1391 read_unlock(&devtree_lock
);
1394 EXPORT_SYMBOL(of_find_node_by_type
);
1397 * of_find_compatible_node - Find a node based on type and one of the
1398 * tokens in its "compatible" property
1399 * @from: The node to start searching from or NULL, the node
1400 * you pass will not be searched, only the next one
1401 * will; typically, you pass what the previous call
1402 * returned. of_node_put() will be called on it
1403 * @type: The type string to match "device_type" or NULL to ignore
1404 * @compatible: The string to match to one of the tokens in the device
1405 * "compatible" list.
1407 * Returns a node pointer with refcount incremented, use
1408 * of_node_put() on it when done.
1410 struct device_node
*of_find_compatible_node(struct device_node
*from
,
1411 const char *type
, const char *compatible
)
1413 struct device_node
*np
;
1415 read_lock(&devtree_lock
);
1416 np
= from
? from
->allnext
: allnodes
;
1417 for (; np
!= 0; np
= np
->allnext
) {
1419 && !(np
->type
!= 0 && strcasecmp(np
->type
, type
) == 0))
1421 if (device_is_compatible(np
, compatible
) && of_node_get(np
))
1426 read_unlock(&devtree_lock
);
1429 EXPORT_SYMBOL(of_find_compatible_node
);
1432 * of_find_node_by_path - Find a node matching a full OF path
1433 * @path: The full path to match
1435 * Returns a node pointer with refcount incremented, use
1436 * of_node_put() on it when done.
1438 struct device_node
*of_find_node_by_path(const char *path
)
1440 struct device_node
*np
= allnodes
;
1442 read_lock(&devtree_lock
);
1443 for (; np
!= 0; np
= np
->allnext
)
1444 if (np
->full_name
!= 0 && strcasecmp(np
->full_name
, path
) == 0
1447 read_unlock(&devtree_lock
);
1450 EXPORT_SYMBOL(of_find_node_by_path
);
1453 * of_find_node_by_phandle - Find a node given a phandle
1454 * @handle: phandle of the node to find
1456 * Returns a node pointer with refcount incremented, use
1457 * of_node_put() on it when done.
1459 struct device_node
*of_find_node_by_phandle(phandle handle
)
1461 struct device_node
*np
;
1463 read_lock(&devtree_lock
);
1464 for (np
= allnodes
; np
!= 0; np
= np
->allnext
)
1465 if (np
->linux_phandle
== handle
)
1469 read_unlock(&devtree_lock
);
1472 EXPORT_SYMBOL(of_find_node_by_phandle
);
1475 * of_find_all_nodes - Get next node in global list
1476 * @prev: Previous node or NULL to start iteration
1477 * of_node_put() will be called on it
1479 * Returns a node pointer with refcount incremented, use
1480 * of_node_put() on it when done.
1482 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
1484 struct device_node
*np
;
1486 read_lock(&devtree_lock
);
1487 np
= prev
? prev
->allnext
: allnodes
;
1488 for (; np
!= 0; np
= np
->allnext
)
1489 if (of_node_get(np
))
1493 read_unlock(&devtree_lock
);
1496 EXPORT_SYMBOL(of_find_all_nodes
);
1499 * of_get_parent - Get a node's parent if any
1500 * @node: Node to get parent
1502 * Returns a node pointer with refcount incremented, use
1503 * of_node_put() on it when done.
1505 struct device_node
*of_get_parent(const struct device_node
*node
)
1507 struct device_node
*np
;
1512 read_lock(&devtree_lock
);
1513 np
= of_node_get(node
->parent
);
1514 read_unlock(&devtree_lock
);
1517 EXPORT_SYMBOL(of_get_parent
);
1520 * of_get_next_child - Iterate a node childs
1521 * @node: parent node
1522 * @prev: previous child of the parent node, or NULL to get first
1524 * Returns a node pointer with refcount incremented, use
1525 * of_node_put() on it when done.
1527 struct device_node
*of_get_next_child(const struct device_node
*node
,
1528 struct device_node
*prev
)
1530 struct device_node
*next
;
1532 read_lock(&devtree_lock
);
1533 next
= prev
? prev
->sibling
: node
->child
;
1534 for (; next
!= 0; next
= next
->sibling
)
1535 if (of_node_get(next
))
1539 read_unlock(&devtree_lock
);
1542 EXPORT_SYMBOL(of_get_next_child
);
1545 * of_node_get - Increment refcount of a node
1546 * @node: Node to inc refcount, NULL is supported to
1547 * simplify writing of callers
1551 struct device_node
*of_node_get(struct device_node
*node
)
1554 kref_get(&node
->kref
);
1557 EXPORT_SYMBOL(of_node_get
);
1559 static inline struct device_node
* kref_to_device_node(struct kref
*kref
)
1561 return container_of(kref
, struct device_node
, kref
);
1565 * of_node_release - release a dynamically allocated node
1566 * @kref: kref element of the node to be released
1568 * In of_node_put() this function is passed to kref_put()
1569 * as the destructor.
1571 static void of_node_release(struct kref
*kref
)
1573 struct device_node
*node
= kref_to_device_node(kref
);
1574 struct property
*prop
= node
->properties
;
1576 if (!OF_IS_DYNAMIC(node
))
1579 struct property
*next
= prop
->next
;
1587 kfree(node
->full_name
);
1592 * of_node_put - Decrement refcount of a node
1593 * @node: Node to dec refcount, NULL is supported to
1594 * simplify writing of callers
1597 void of_node_put(struct device_node
*node
)
1600 kref_put(&node
->kref
, of_node_release
);
1602 EXPORT_SYMBOL(of_node_put
);
1605 * Fix up the uninitialized fields in a new device node:
1606 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1608 * A lot of boot-time code is duplicated here, because functions such
1609 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1612 * This should probably be split up into smaller chunks.
1615 static int of_finish_dynamic_node(struct device_node
*node
,
1616 unsigned long *unused1
, int unused2
,
1617 int unused3
, int unused4
)
1619 struct device_node
*parent
= of_get_parent(node
);
1621 phandle
*ibm_phandle
;
1623 node
->name
= get_property(node
, "name", NULL
);
1624 node
->type
= get_property(node
, "device_type", NULL
);
1631 /* We don't support that function on PowerMac, at least
1634 if (systemcfg
->platform
== PLATFORM_POWERMAC
)
1637 /* fix up new node's linux_phandle field */
1638 if ((ibm_phandle
= (unsigned int *)get_property(node
, "ibm,phandle", NULL
)))
1639 node
->linux_phandle
= *ibm_phandle
;
1642 of_node_put(parent
);
1647 * Plug a device node into the tree and global list.
1649 void of_attach_node(struct device_node
*np
)
1651 write_lock(&devtree_lock
);
1652 np
->sibling
= np
->parent
->child
;
1653 np
->allnext
= allnodes
;
1654 np
->parent
->child
= np
;
1656 write_unlock(&devtree_lock
);
1660 * "Unplug" a node from the device tree. The caller must hold
1661 * a reference to the node. The memory associated with the node
1662 * is not freed until its refcount goes to zero.
1664 void of_detach_node(const struct device_node
*np
)
1666 struct device_node
*parent
;
1668 write_lock(&devtree_lock
);
1670 parent
= np
->parent
;
1673 allnodes
= np
->allnext
;
1675 struct device_node
*prev
;
1676 for (prev
= allnodes
;
1677 prev
->allnext
!= np
;
1678 prev
= prev
->allnext
)
1680 prev
->allnext
= np
->allnext
;
1683 if (parent
->child
== np
)
1684 parent
->child
= np
->sibling
;
1686 struct device_node
*prevsib
;
1687 for (prevsib
= np
->parent
->child
;
1688 prevsib
->sibling
!= np
;
1689 prevsib
= prevsib
->sibling
)
1691 prevsib
->sibling
= np
->sibling
;
1694 write_unlock(&devtree_lock
);
1697 static int prom_reconfig_notifier(struct notifier_block
*nb
, unsigned long action
, void *node
)
1702 case PSERIES_RECONFIG_ADD
:
1703 err
= finish_node(node
, NULL
, of_finish_dynamic_node
, 0, 0, 0);
1705 printk(KERN_ERR
"finish_node returned %d\n", err
);
1716 static struct notifier_block prom_reconfig_nb
= {
1717 .notifier_call
= prom_reconfig_notifier
,
1718 .priority
= 10, /* This one needs to run first */
1721 static int __init
prom_reconfig_setup(void)
1723 return pSeries_reconfig_notifier_register(&prom_reconfig_nb
);
1725 __initcall(prom_reconfig_setup
);
1728 * Find a property with a given name for a given node
1729 * and return the value.
1732 get_property(struct device_node
*np
, const char *name
, int *lenp
)
1734 struct property
*pp
;
1736 for (pp
= np
->properties
; pp
!= 0; pp
= pp
->next
)
1737 if (strcmp(pp
->name
, name
) == 0) {
1744 EXPORT_SYMBOL(get_property
);
1747 * Add a property to a node
1750 prom_add_property(struct device_node
* np
, struct property
* prop
)
1752 struct property
**next
= &np
->properties
;
1756 next
= &(*next
)->next
;
1762 print_properties(struct device_node
*np
)
1764 struct property
*pp
;
1768 for (pp
= np
->properties
; pp
!= 0; pp
= pp
->next
) {
1769 printk(KERN_INFO
"%s", pp
->name
);
1770 for (i
= strlen(pp
->name
); i
< 16; ++i
)
1772 cp
= (char *) pp
->value
;
1773 for (i
= pp
->length
; i
> 0; --i
, ++cp
)
1774 if ((i
> 1 && (*cp
< 0x20 || *cp
> 0x7e))
1775 || (i
== 1 && *cp
!= 0))
1777 if (i
== 0 && pp
->length
> 1) {
1778 /* looks like a string */
1779 printk(" %s\n", (char *) pp
->value
);
1781 /* dump it in hex */
1785 if (pp
->length
% 4 == 0) {
1786 unsigned int *p
= (unsigned int *) pp
->value
;
1789 for (i
= 0; i
< n
; ++i
) {
1790 if (i
!= 0 && (i
% 4) == 0)
1792 printk(" %08x", *p
++);
1795 unsigned char *bp
= pp
->value
;
1797 for (i
= 0; i
< n
; ++i
) {
1798 if (i
!= 0 && (i
% 16) == 0)
1800 printk(" %02x", *bp
++);
1804 if (pp
->length
> 64)
1805 printk(" ... (length = %d)\n",