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/kernel.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
22 #include <linux/threads.h>
23 #include <linux/spinlock.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/stringify.h>
27 #include <linux/delay.h>
28 #include <linux/initrd.h>
29 #include <linux/bitops.h>
30 #include <linux/module.h>
31 #include <linux/kexec.h>
32 #include <linux/debugfs.h>
33 #include <linux/irq.h>
34 #include <linux/lmb.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/phyp_dump.h>
55 #include <asm/kexec.h>
56 #include <mm/mmu_decl.h>
59 #define DBG(fmt...) printk(KERN_ERR fmt)
65 static int __initdata dt_root_addr_cells
;
66 static int __initdata dt_root_size_cells
;
69 int __initdata iommu_is_off
;
70 int __initdata iommu_force_on
;
71 unsigned long tce_alloc_start
, tce_alloc_end
;
77 static struct boot_param_header
*initial_boot_params __initdata
;
79 struct boot_param_header
*initial_boot_params
;
82 extern struct device_node
*allnodes
; /* temporary while merging */
84 extern rwlock_t devtree_lock
; /* temporary while merging */
86 /* export that to outside world */
87 struct device_node
*of_chosen
;
89 static inline char *find_flat_dt_string(u32 offset
)
91 return ((char *)initial_boot_params
) +
92 initial_boot_params
->off_dt_strings
+ offset
;
96 * This function is used to scan the flattened device-tree, it is
97 * used to extract the memory informations at boot before we can
100 int __init
of_scan_flat_dt(int (*it
)(unsigned long node
,
101 const char *uname
, int depth
,
105 unsigned long p
= ((unsigned long)initial_boot_params
) +
106 initial_boot_params
->off_dt_struct
;
111 u32 tag
= *((u32
*)p
);
115 if (tag
== OF_DT_END_NODE
) {
119 if (tag
== OF_DT_NOP
)
121 if (tag
== OF_DT_END
)
123 if (tag
== OF_DT_PROP
) {
124 u32 sz
= *((u32
*)p
);
126 if (initial_boot_params
->version
< 0x10)
127 p
= _ALIGN(p
, sz
>= 8 ? 8 : 4);
132 if (tag
!= OF_DT_BEGIN_NODE
) {
133 printk(KERN_WARNING
"Invalid tag %x scanning flattened"
134 " device tree !\n", tag
);
139 p
= _ALIGN(p
+ strlen(pathp
) + 1, 4);
140 if ((*pathp
) == '/') {
142 for (lp
= NULL
, np
= pathp
; *np
; np
++)
148 rc
= it(p
, pathp
, depth
, data
);
156 unsigned long __init
of_get_flat_dt_root(void)
158 unsigned long p
= ((unsigned long)initial_boot_params
) +
159 initial_boot_params
->off_dt_struct
;
161 while(*((u32
*)p
) == OF_DT_NOP
)
163 BUG_ON (*((u32
*)p
) != OF_DT_BEGIN_NODE
);
165 return _ALIGN(p
+ strlen((char *)p
) + 1, 4);
169 * This function can be used within scan_flattened_dt callback to get
170 * access to properties
172 void* __init
of_get_flat_dt_prop(unsigned long node
, const char *name
,
175 unsigned long p
= node
;
178 u32 tag
= *((u32
*)p
);
183 if (tag
== OF_DT_NOP
)
185 if (tag
!= OF_DT_PROP
)
189 noff
= *((u32
*)(p
+ 4));
191 if (initial_boot_params
->version
< 0x10)
192 p
= _ALIGN(p
, sz
>= 8 ? 8 : 4);
194 nstr
= find_flat_dt_string(noff
);
196 printk(KERN_WARNING
"Can't find property index"
200 if (strcmp(name
, nstr
) == 0) {
210 int __init
of_flat_dt_is_compatible(unsigned long node
, const char *compat
)
213 unsigned long cplen
, l
;
215 cp
= of_get_flat_dt_prop(node
, "compatible", &cplen
);
219 if (strncasecmp(cp
, compat
, strlen(compat
)) == 0)
229 static void *__init
unflatten_dt_alloc(unsigned long *mem
, unsigned long size
,
234 *mem
= _ALIGN(*mem
, align
);
241 static unsigned long __init
unflatten_dt_node(unsigned long mem
,
243 struct device_node
*dad
,
244 struct device_node
***allnextpp
,
245 unsigned long fpsize
)
247 struct device_node
*np
;
248 struct property
*pp
, **prev_pp
= NULL
;
251 unsigned int l
, allocl
;
255 tag
= *((u32
*)(*p
));
256 if (tag
!= OF_DT_BEGIN_NODE
) {
257 printk("Weird tag at start of node: %x\n", tag
);
262 l
= allocl
= strlen(pathp
) + 1;
263 *p
= _ALIGN(*p
+ l
, 4);
265 /* version 0x10 has a more compact unit name here instead of the full
266 * path. we accumulate the full path size using "fpsize", we'll rebuild
267 * it later. We detect this because the first character of the name is
270 if ((*pathp
) != '/') {
273 /* root node: special case. fpsize accounts for path
274 * plus terminating zero. root node only has '/', so
275 * fpsize should be 2, but we want to avoid the first
276 * level nodes to have two '/' so we use fpsize 1 here
281 /* account for '/' and path size minus terminal 0
290 np
= unflatten_dt_alloc(&mem
, sizeof(struct device_node
) + allocl
,
291 __alignof__(struct device_node
));
293 memset(np
, 0, sizeof(*np
));
294 np
->full_name
= ((char*)np
) + sizeof(struct device_node
);
296 char *p
= np
->full_name
;
297 /* rebuild full path for new format */
298 if (dad
&& dad
->parent
) {
299 strcpy(p
, dad
->full_name
);
301 if ((strlen(p
) + l
+ 1) != allocl
) {
302 DBG("%s: p: %d, l: %d, a: %d\n",
303 pathp
, (int)strlen(p
), l
, allocl
);
311 memcpy(np
->full_name
, pathp
, l
);
312 prev_pp
= &np
->properties
;
314 *allnextpp
= &np
->allnext
;
317 /* we temporarily use the next field as `last_child'*/
321 dad
->next
->sibling
= np
;
324 kref_init(&np
->kref
);
330 tag
= *((u32
*)(*p
));
331 if (tag
== OF_DT_NOP
) {
335 if (tag
!= OF_DT_PROP
)
339 noff
= *((u32
*)((*p
) + 4));
341 if (initial_boot_params
->version
< 0x10)
342 *p
= _ALIGN(*p
, sz
>= 8 ? 8 : 4);
344 pname
= find_flat_dt_string(noff
);
346 printk("Can't find property name in list !\n");
349 if (strcmp(pname
, "name") == 0)
351 l
= strlen(pname
) + 1;
352 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
),
353 __alignof__(struct property
));
355 if (strcmp(pname
, "linux,phandle") == 0) {
356 np
->node
= *((u32
*)*p
);
357 if (np
->linux_phandle
== 0)
358 np
->linux_phandle
= np
->node
;
360 if (strcmp(pname
, "ibm,phandle") == 0)
361 np
->linux_phandle
= *((u32
*)*p
);
364 pp
->value
= (void *)*p
;
368 *p
= _ALIGN((*p
) + sz
, 4);
370 /* with version 0x10 we may not have the name property, recreate
371 * it here from the unit name if absent
374 char *p
= pathp
, *ps
= pathp
, *pa
= NULL
;
387 pp
= unflatten_dt_alloc(&mem
, sizeof(struct property
) + sz
,
388 __alignof__(struct property
));
395 memcpy(pp
->value
, ps
, sz
- 1);
396 ((char *)pp
->value
)[sz
- 1] = 0;
397 DBG("fixed up name for %s -> %s\n", pathp
,
403 np
->name
= of_get_property(np
, "name", NULL
);
404 np
->type
= of_get_property(np
, "device_type", NULL
);
411 while (tag
== OF_DT_BEGIN_NODE
) {
412 mem
= unflatten_dt_node(mem
, p
, np
, allnextpp
, fpsize
);
413 tag
= *((u32
*)(*p
));
415 if (tag
!= OF_DT_END_NODE
) {
416 printk("Weird tag at end of node: %x\n", tag
);
423 static int __init
early_parse_mem(char *p
)
428 memory_limit
= PAGE_ALIGN(memparse(p
, &p
));
429 DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit
);
433 early_param("mem", early_parse_mem
);
436 * move_device_tree - move tree to an unused area, if needed.
438 * The device tree may be allocated beyond our memory limit, or inside the
439 * crash kernel region for kdump. If so, move it out of the way.
441 static void __init
move_device_tree(void)
443 unsigned long start
, size
;
446 DBG("-> move_device_tree\n");
448 start
= __pa(initial_boot_params
);
449 size
= initial_boot_params
->totalsize
;
451 if ((memory_limit
&& (start
+ size
) > memory_limit
) ||
452 overlaps_crashkernel(start
, size
)) {
453 p
= __va(lmb_alloc_base(size
, PAGE_SIZE
, lmb
.rmo_size
));
454 memcpy(p
, initial_boot_params
, size
);
455 initial_boot_params
= (struct boot_param_header
*)p
;
456 DBG("Moved device tree to 0x%p\n", p
);
459 DBG("<- move_device_tree\n");
463 * unflattens the device-tree passed by the firmware, creating the
464 * tree of struct device_node. It also fills the "name" and "type"
465 * pointers of the nodes so the normal device-tree walking functions
466 * can be used (this used to be done by finish_device_tree)
468 void __init
unflatten_device_tree(void)
470 unsigned long start
, mem
, size
;
471 struct device_node
**allnextp
= &allnodes
;
473 DBG(" -> unflatten_device_tree()\n");
475 /* First pass, scan for size */
476 start
= ((unsigned long)initial_boot_params
) +
477 initial_boot_params
->off_dt_struct
;
478 size
= unflatten_dt_node(0, &start
, NULL
, NULL
, 0);
479 size
= (size
| 3) + 1;
481 DBG(" size is %lx, allocating...\n", size
);
483 /* Allocate memory for the expanded device tree */
484 mem
= lmb_alloc(size
+ 4, __alignof__(struct device_node
));
485 mem
= (unsigned long) __va(mem
);
487 ((u32
*)mem
)[size
/ 4] = 0xdeadbeef;
489 DBG(" unflattening %lx...\n", mem
);
491 /* Second pass, do actual unflattening */
492 start
= ((unsigned long)initial_boot_params
) +
493 initial_boot_params
->off_dt_struct
;
494 unflatten_dt_node(mem
, &start
, NULL
, &allnextp
, 0);
495 if (*((u32
*)start
) != OF_DT_END
)
496 printk(KERN_WARNING
"Weird tag at end of tree: %08x\n", *((u32
*)start
));
497 if (((u32
*)mem
)[size
/ 4] != 0xdeadbeef)
498 printk(KERN_WARNING
"End of tree marker overwritten: %08x\n",
499 ((u32
*)mem
)[size
/ 4] );
502 /* Get pointer to OF "/chosen" node for use everywhere */
503 of_chosen
= of_find_node_by_path("/chosen");
504 if (of_chosen
== NULL
)
505 of_chosen
= of_find_node_by_path("/chosen@0");
507 DBG(" <- unflatten_device_tree()\n");
511 * ibm,pa-features is a per-cpu property that contains a string of
512 * attribute descriptors, each of which has a 2 byte header plus up
513 * to 254 bytes worth of processor attribute bits. First header
514 * byte specifies the number of bytes following the header.
515 * Second header byte is an "attribute-specifier" type, of which
516 * zero is the only currently-defined value.
517 * Implementation: Pass in the byte and bit offset for the feature
518 * that we are interested in. The function will return -1 if the
519 * pa-features property is missing, or a 1/0 to indicate if the feature
520 * is supported/not supported. Note that the bit numbers are
521 * big-endian to match the definition in PAPR.
523 static struct ibm_pa_feature
{
524 unsigned long cpu_features
; /* CPU_FTR_xxx bit */
525 unsigned int cpu_user_ftrs
; /* PPC_FEATURE_xxx bit */
526 unsigned char pabyte
; /* byte number in ibm,pa-features */
527 unsigned char pabit
; /* bit number (big-endian) */
528 unsigned char invert
; /* if 1, pa bit set => clear feature */
529 } ibm_pa_features
[] __initdata
= {
530 {0, PPC_FEATURE_HAS_MMU
, 0, 0, 0},
531 {0, PPC_FEATURE_HAS_FPU
, 0, 1, 0},
532 {CPU_FTR_SLB
, 0, 0, 2, 0},
533 {CPU_FTR_CTRL
, 0, 0, 3, 0},
534 {CPU_FTR_NOEXECUTE
, 0, 0, 6, 0},
535 {CPU_FTR_NODSISRALIGN
, 0, 1, 1, 1},
536 {CPU_FTR_CI_LARGE_PAGE
, 0, 1, 2, 0},
537 {CPU_FTR_REAL_LE
, PPC_FEATURE_TRUE_LE
, 5, 0, 0},
540 static void __init
scan_features(unsigned long node
, unsigned char *ftrs
,
541 unsigned long tablelen
,
542 struct ibm_pa_feature
*fp
,
543 unsigned long ft_size
)
545 unsigned long i
, len
, bit
;
547 /* find descriptor with type == 0 */
553 return; /* descriptor 0 not found */
560 /* loop over bits we know about */
561 for (i
= 0; i
< ft_size
; ++i
, ++fp
) {
562 if (fp
->pabyte
>= ftrs
[0])
564 bit
= (ftrs
[2 + fp
->pabyte
] >> (7 - fp
->pabit
)) & 1;
565 if (bit
^ fp
->invert
) {
566 cur_cpu_spec
->cpu_features
|= fp
->cpu_features
;
567 cur_cpu_spec
->cpu_user_features
|= fp
->cpu_user_ftrs
;
569 cur_cpu_spec
->cpu_features
&= ~fp
->cpu_features
;
570 cur_cpu_spec
->cpu_user_features
&= ~fp
->cpu_user_ftrs
;
575 static void __init
check_cpu_pa_features(unsigned long node
)
577 unsigned char *pa_ftrs
;
578 unsigned long tablelen
;
580 pa_ftrs
= of_get_flat_dt_prop(node
, "ibm,pa-features", &tablelen
);
584 scan_features(node
, pa_ftrs
, tablelen
,
585 ibm_pa_features
, ARRAY_SIZE(ibm_pa_features
));
588 #ifdef CONFIG_PPC_STD_MMU_64
589 static void __init
check_cpu_slb_size(unsigned long node
)
593 slb_size_ptr
= of_get_flat_dt_prop(node
, "slb-size", NULL
);
594 if (slb_size_ptr
!= NULL
) {
595 mmu_slb_size
= *slb_size_ptr
;
598 slb_size_ptr
= of_get_flat_dt_prop(node
, "ibm,slb-size", NULL
);
599 if (slb_size_ptr
!= NULL
) {
600 mmu_slb_size
= *slb_size_ptr
;
604 #define check_cpu_slb_size(node) do { } while(0)
607 static struct feature_property
{
610 unsigned long cpu_feature
;
611 unsigned long cpu_user_ftr
;
612 } feature_properties
[] __initdata
= {
613 #ifdef CONFIG_ALTIVEC
614 {"altivec", 0, CPU_FTR_ALTIVEC
, PPC_FEATURE_HAS_ALTIVEC
},
615 {"ibm,vmx", 1, CPU_FTR_ALTIVEC
, PPC_FEATURE_HAS_ALTIVEC
},
616 #endif /* CONFIG_ALTIVEC */
618 /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
619 {"ibm,vmx", 2, CPU_FTR_VSX
, PPC_FEATURE_HAS_VSX
},
620 #endif /* CONFIG_VSX */
622 {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP
},
623 {"ibm,purr", 1, CPU_FTR_PURR
, 0},
624 {"ibm,spurr", 1, CPU_FTR_SPURR
, 0},
625 #endif /* CONFIG_PPC64 */
628 #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
629 static inline void identical_pvr_fixup(unsigned long node
)
632 char *model
= of_get_flat_dt_prop(node
, "model", NULL
);
635 * Since 440GR(x)/440EP(x) processors have the same pvr,
636 * we check the node path and set bit 28 in the cur_cpu_spec
637 * pvr for EP(x) processor version. This bit is always 0 in
638 * the "real" pvr. Then we call identify_cpu again with
639 * the new logical pvr to enable FPU support.
641 if (model
&& strstr(model
, "440EP")) {
642 pvr
= cur_cpu_spec
->pvr_value
| 0x8;
643 identify_cpu(0, pvr
);
644 DBG("Using logical pvr %x for %s\n", pvr
, model
);
648 #define identical_pvr_fixup(node) do { } while(0)
651 static void __init
check_cpu_feature_properties(unsigned long node
)
654 struct feature_property
*fp
= feature_properties
;
657 for (i
= 0; i
< ARRAY_SIZE(feature_properties
); ++i
, ++fp
) {
658 prop
= of_get_flat_dt_prop(node
, fp
->name
, NULL
);
659 if (prop
&& *prop
>= fp
->min_value
) {
660 cur_cpu_spec
->cpu_features
|= fp
->cpu_feature
;
661 cur_cpu_spec
->cpu_user_features
|= fp
->cpu_user_ftr
;
666 static int __init
early_init_dt_scan_cpus(unsigned long node
,
667 const char *uname
, int depth
,
670 static int logical_cpuid
= 0;
671 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
678 /* We are scanning "cpu" nodes only */
679 if (type
== NULL
|| strcmp(type
, "cpu") != 0)
682 /* Get physical cpuid */
683 intserv
= of_get_flat_dt_prop(node
, "ibm,ppc-interrupt-server#s", &len
);
685 nthreads
= len
/ sizeof(int);
687 intserv
= of_get_flat_dt_prop(node
, "reg", NULL
);
692 * Now see if any of these threads match our boot cpu.
693 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
695 for (i
= 0; i
< nthreads
; i
++) {
697 * version 2 of the kexec param format adds the phys cpuid of
700 if (initial_boot_params
&& initial_boot_params
->version
>= 2) {
702 initial_boot_params
->boot_cpuid_phys
) {
708 * Check if it's the boot-cpu, set it's hw index now,
709 * unfortunately this format did not support booting
710 * off secondary threads.
712 if (of_get_flat_dt_prop(node
,
713 "linux,boot-cpu", NULL
) != NULL
) {
720 /* logical cpu id is always 0 on UP kernels */
726 DBG("boot cpu: logical %d physical %d\n", logical_cpuid
,
728 boot_cpuid
= logical_cpuid
;
729 set_hard_smp_processor_id(boot_cpuid
, intserv
[i
]);
732 * PAPR defines "logical" PVR values for cpus that
733 * meet various levels of the architecture:
734 * 0x0f000001 Architecture version 2.04
735 * 0x0f000002 Architecture version 2.05
736 * If the cpu-version property in the cpu node contains
737 * such a value, we call identify_cpu again with the
738 * logical PVR value in order to use the cpu feature
739 * bits appropriate for the architecture level.
741 * A POWER6 partition in "POWER6 architected" mode
742 * uses the 0x0f000002 PVR value; in POWER5+ mode
743 * it uses 0x0f000001.
745 prop
= of_get_flat_dt_prop(node
, "cpu-version", NULL
);
746 if (prop
&& (*prop
& 0xff000000) == 0x0f000000)
747 identify_cpu(0, *prop
);
749 identical_pvr_fixup(node
);
752 check_cpu_feature_properties(node
);
753 check_cpu_pa_features(node
);
754 check_cpu_slb_size(node
);
756 #ifdef CONFIG_PPC_PSERIES
758 cur_cpu_spec
->cpu_features
|= CPU_FTR_SMT
;
760 cur_cpu_spec
->cpu_features
&= ~CPU_FTR_SMT
;
766 #ifdef CONFIG_BLK_DEV_INITRD
767 static void __init
early_init_dt_check_for_initrd(unsigned long node
)
772 DBG("Looking for initrd properties... ");
774 prop
= of_get_flat_dt_prop(node
, "linux,initrd-start", &l
);
776 initrd_start
= (unsigned long)__va(of_read_ulong(prop
, l
/4));
778 prop
= of_get_flat_dt_prop(node
, "linux,initrd-end", &l
);
780 initrd_end
= (unsigned long)
781 __va(of_read_ulong(prop
, l
/4));
782 initrd_below_start_ok
= 1;
788 DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start
, initrd_end
);
791 static inline void early_init_dt_check_for_initrd(unsigned long node
)
794 #endif /* CONFIG_BLK_DEV_INITRD */
796 static int __init
early_init_dt_scan_chosen(unsigned long node
,
797 const char *uname
, int depth
, void *data
)
799 unsigned long *lprop
;
803 DBG("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
806 (strcmp(uname
, "chosen") != 0 && strcmp(uname
, "chosen@0") != 0))
810 /* check if iommu is forced on or off */
811 if (of_get_flat_dt_prop(node
, "linux,iommu-off", NULL
) != NULL
)
813 if (of_get_flat_dt_prop(node
, "linux,iommu-force-on", NULL
) != NULL
)
817 /* mem=x on the command line is the preferred mechanism */
818 lprop
= of_get_flat_dt_prop(node
, "linux,memory-limit", NULL
);
820 memory_limit
= *lprop
;
823 lprop
= of_get_flat_dt_prop(node
, "linux,tce-alloc-start", NULL
);
825 tce_alloc_start
= *lprop
;
826 lprop
= of_get_flat_dt_prop(node
, "linux,tce-alloc-end", NULL
);
828 tce_alloc_end
= *lprop
;
832 lprop
= of_get_flat_dt_prop(node
, "linux,crashkernel-base", NULL
);
834 crashk_res
.start
= *lprop
;
836 lprop
= of_get_flat_dt_prop(node
, "linux,crashkernel-size", NULL
);
838 crashk_res
.end
= crashk_res
.start
+ *lprop
- 1;
841 early_init_dt_check_for_initrd(node
);
843 /* Retreive command line */
844 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
845 if (p
!= NULL
&& l
> 0)
846 strlcpy(cmd_line
, p
, min((int)l
, COMMAND_LINE_SIZE
));
848 #ifdef CONFIG_CMDLINE
849 if (p
== NULL
|| l
== 0 || (l
== 1 && (*p
) == 0))
850 strlcpy(cmd_line
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
851 #endif /* CONFIG_CMDLINE */
853 DBG("Command line is: %s\n", cmd_line
);
859 static int __init
early_init_dt_scan_root(unsigned long node
,
860 const char *uname
, int depth
, void *data
)
867 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
868 dt_root_size_cells
= (prop
== NULL
) ? 1 : *prop
;
869 DBG("dt_root_size_cells = %x\n", dt_root_size_cells
);
871 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
872 dt_root_addr_cells
= (prop
== NULL
) ? 2 : *prop
;
873 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells
);
879 static u64 __init
dt_mem_next_cell(int s
, cell_t
**cellp
)
884 return of_read_number(p
, s
);
887 #ifdef CONFIG_PPC_PSERIES
889 * Interpret the ibm,dynamic-memory property in the
890 * /ibm,dynamic-reconfiguration-memory node.
891 * This contains a list of memory blocks along with NUMA affinity
894 static int __init
early_init_dt_scan_drconf_memory(unsigned long node
)
896 cell_t
*dm
, *ls
, *usm
;
897 unsigned long l
, n
, flags
;
898 u64 base
, size
, lmb_size
;
899 unsigned int is_kexec_kdump
= 0, rngs
;
901 ls
= of_get_flat_dt_prop(node
, "ibm,lmb-size", &l
);
902 if (ls
== NULL
|| l
< dt_root_size_cells
* sizeof(cell_t
))
904 lmb_size
= dt_mem_next_cell(dt_root_size_cells
, &ls
);
906 dm
= of_get_flat_dt_prop(node
, "ibm,dynamic-memory", &l
);
907 if (dm
== NULL
|| l
< sizeof(cell_t
))
910 n
= *dm
++; /* number of entries */
911 if (l
< (n
* (dt_root_addr_cells
+ 4) + 1) * sizeof(cell_t
))
914 /* check if this is a kexec/kdump kernel. */
915 usm
= of_get_flat_dt_prop(node
, "linux,drconf-usable-memory",
920 for (; n
!= 0; --n
) {
921 base
= dt_mem_next_cell(dt_root_addr_cells
, &dm
);
923 /* skip DRC index, pad, assoc. list index, flags */
925 /* skip this block if the reserved bit is set in flags (0x80)
926 or if the block is not assigned to this partition (0x8) */
927 if ((flags
& 0x80) || !(flags
& 0x8))
931 if (is_kexec_kdump
) {
933 * For each lmb in ibm,dynamic-memory, a corresponding
934 * entry in linux,drconf-usable-memory property contains
935 * a counter 'p' followed by 'p' (base, size) duple.
936 * Now read the counter from
937 * linux,drconf-usable-memory property
939 rngs
= dt_mem_next_cell(dt_root_size_cells
, &usm
);
940 if (!rngs
) /* there are no (base, size) duple */
944 if (is_kexec_kdump
) {
945 base
= dt_mem_next_cell(dt_root_addr_cells
,
947 size
= dt_mem_next_cell(dt_root_size_cells
,
951 if (base
>= 0x80000000ul
)
953 if ((base
+ size
) > 0x80000000ul
)
954 size
= 0x80000000ul
- base
;
963 #define early_init_dt_scan_drconf_memory(node) 0
964 #endif /* CONFIG_PPC_PSERIES */
966 static int __init
early_init_dt_scan_memory(unsigned long node
,
967 const char *uname
, int depth
, void *data
)
969 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
973 /* Look for the ibm,dynamic-reconfiguration-memory node */
975 strcmp(uname
, "ibm,dynamic-reconfiguration-memory") == 0)
976 return early_init_dt_scan_drconf_memory(node
);
978 /* We are scanning "memory" nodes only */
981 * The longtrail doesn't have a device_type on the
982 * /memory node, so look for the node called /memory@0.
984 if (depth
!= 1 || strcmp(uname
, "memory@0") != 0)
986 } else if (strcmp(type
, "memory") != 0)
989 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
991 reg
= of_get_flat_dt_prop(node
, "reg", &l
);
995 endp
= reg
+ (l
/ sizeof(cell_t
));
997 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
998 uname
, l
, reg
[0], reg
[1], reg
[2], reg
[3]);
1000 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
1003 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
1004 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
1008 DBG(" - %llx , %llx\n", (unsigned long long)base
,
1009 (unsigned long long)size
);
1012 if (base
>= 0x80000000ul
)
1014 if ((base
+ size
) > 0x80000000ul
)
1015 size
= 0x80000000ul
- base
;
1018 lmb_add(base
, size
);
1020 memstart_addr
= min((u64
)memstart_addr
, base
);
1026 static void __init
early_reserve_mem(void)
1030 unsigned long self_base
;
1031 unsigned long self_size
;
1033 reserve_map
= (u64
*)(((unsigned long)initial_boot_params
) +
1034 initial_boot_params
->off_mem_rsvmap
);
1036 /* before we do anything, lets reserve the dt blob */
1037 self_base
= __pa((unsigned long)initial_boot_params
);
1038 self_size
= initial_boot_params
->totalsize
;
1039 lmb_reserve(self_base
, self_size
);
1041 #ifdef CONFIG_BLK_DEV_INITRD
1042 /* then reserve the initrd, if any */
1043 if (initrd_start
&& (initrd_end
> initrd_start
))
1044 lmb_reserve(__pa(initrd_start
), initrd_end
- initrd_start
);
1045 #endif /* CONFIG_BLK_DEV_INITRD */
1049 * Handle the case where we might be booting from an old kexec
1050 * image that setup the mem_rsvmap as pairs of 32-bit values
1052 if (*reserve_map
> 0xffffffffull
) {
1053 u32 base_32
, size_32
;
1054 u32
*reserve_map_32
= (u32
*)reserve_map
;
1057 base_32
= *(reserve_map_32
++);
1058 size_32
= *(reserve_map_32
++);
1061 /* skip if the reservation is for the blob */
1062 if (base_32
== self_base
&& size_32
== self_size
)
1064 DBG("reserving: %x -> %x\n", base_32
, size_32
);
1065 lmb_reserve(base_32
, size_32
);
1071 base
= *(reserve_map
++);
1072 size
= *(reserve_map
++);
1075 DBG("reserving: %llx -> %llx\n", base
, size
);
1076 lmb_reserve(base
, size
);
1080 #ifdef CONFIG_PHYP_DUMP
1082 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
1084 * Function to find the largest size we need to reserve
1085 * during early boot process.
1087 * It either looks for boot param and returns that OR
1088 * returns larger of 256 or 5% rounded down to multiples of 256MB.
1091 static inline unsigned long phyp_dump_calculate_reserve_size(void)
1095 if (phyp_dump_info
->reserve_bootvar
)
1096 return phyp_dump_info
->reserve_bootvar
;
1098 /* divide by 20 to get 5% of value */
1099 tmp
= lmb_end_of_DRAM();
1102 /* round it down in multiples of 256 */
1103 tmp
= tmp
& ~0x0FFFFFFFUL
;
1105 return (tmp
> PHYP_DUMP_RMR_END
? tmp
: PHYP_DUMP_RMR_END
);
1109 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
1111 * This routine may reserve memory regions in the kernel only
1112 * if the system is supported and a dump was taken in last
1113 * boot instance or if the hardware is supported and the
1114 * scratch area needs to be setup. In other instances it returns
1115 * without reserving anything. The memory in case of dump being
1116 * active is freed when the dump is collected (by userland tools).
1118 static void __init
phyp_dump_reserve_mem(void)
1120 unsigned long base
, size
;
1121 unsigned long variable_reserve_size
;
1123 if (!phyp_dump_info
->phyp_dump_configured
) {
1124 printk(KERN_ERR
"Phyp-dump not supported on this hardware\n");
1128 if (!phyp_dump_info
->phyp_dump_at_boot
) {
1129 printk(KERN_INFO
"Phyp-dump disabled at boot time\n");
1133 variable_reserve_size
= phyp_dump_calculate_reserve_size();
1135 if (phyp_dump_info
->phyp_dump_is_active
) {
1136 /* Reserve *everything* above RMR.Area freed by userland tools*/
1137 base
= variable_reserve_size
;
1138 size
= lmb_end_of_DRAM() - base
;
1140 /* XXX crashed_ram_end is wrong, since it may be beyond
1141 * the memory_limit, it will need to be adjusted. */
1142 lmb_reserve(base
, size
);
1144 phyp_dump_info
->init_reserve_start
= base
;
1145 phyp_dump_info
->init_reserve_size
= size
;
1147 size
= phyp_dump_info
->cpu_state_size
+
1148 phyp_dump_info
->hpte_region_size
+
1149 variable_reserve_size
;
1150 base
= lmb_end_of_DRAM() - size
;
1151 lmb_reserve(base
, size
);
1152 phyp_dump_info
->init_reserve_start
= base
;
1153 phyp_dump_info
->init_reserve_size
= size
;
1157 static inline void __init
phyp_dump_reserve_mem(void) {}
1158 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
1161 void __init
early_init_devtree(void *params
)
1165 DBG(" -> early_init_devtree(%p)\n", params
);
1167 /* Setup flat device-tree pointer */
1168 initial_boot_params
= params
;
1170 #ifdef CONFIG_PPC_RTAS
1171 /* Some machines might need RTAS info for debugging, grab it now. */
1172 of_scan_flat_dt(early_init_dt_scan_rtas
, NULL
);
1175 #ifdef CONFIG_PHYP_DUMP
1176 /* scan tree to see if dump occured during last boot */
1177 of_scan_flat_dt(early_init_dt_scan_phyp_dump
, NULL
);
1180 /* Retrieve various informations from the /chosen node of the
1181 * device-tree, including the platform type, initrd location and
1182 * size, TCE reserve, and more ...
1184 of_scan_flat_dt(early_init_dt_scan_chosen
, NULL
);
1186 /* Scan memory nodes and rebuild LMBs */
1188 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
1189 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
1191 /* Save command line for /proc/cmdline and then parse parameters */
1192 strlcpy(boot_command_line
, cmd_line
, COMMAND_LINE_SIZE
);
1193 parse_early_param();
1195 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1196 lmb_reserve(PHYSICAL_START
, __pa(klimit
) - PHYSICAL_START
);
1197 /* If relocatable, reserve first 32k for interrupt vectors etc. */
1198 if (PHYSICAL_START
> MEMORY_START
)
1199 lmb_reserve(MEMORY_START
, 0x8000);
1200 reserve_kdump_trampoline();
1201 reserve_crashkernel();
1202 early_reserve_mem();
1203 phyp_dump_reserve_mem();
1205 limit
= memory_limit
;
1207 phys_addr_t memsize
;
1209 /* Ensure that total memory size is page-aligned, because
1210 * otherwise mark_bootmem() gets upset. */
1212 memsize
= lmb_phys_mem_size();
1213 if ((memsize
& PAGE_MASK
) != memsize
)
1214 limit
= memsize
& PAGE_MASK
;
1216 lmb_enforce_memory_limit(limit
);
1221 DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
1223 /* We may need to relocate the flat tree, do it now.
1224 * FIXME .. and the initrd too? */
1227 DBG("Scanning CPUs ...\n");
1229 /* Retreive CPU related informations from the flat tree
1230 * (altivec support, boot CPU ID, ...)
1232 of_scan_flat_dt(early_init_dt_scan_cpus
, NULL
);
1234 DBG(" <- early_init_devtree()\n");
1239 * Indicates whether the root node has a given value in its
1240 * compatible property.
1242 int machine_is_compatible(const char *compat
)
1244 struct device_node
*root
;
1247 root
= of_find_node_by_path("/");
1249 rc
= of_device_is_compatible(root
, compat
);
1254 EXPORT_SYMBOL(machine_is_compatible
);
1258 * New implementation of the OF "find" APIs, return a refcounted
1259 * object, call of_node_put() when done. The device tree and list
1260 * are protected by a rw_lock.
1262 * Note that property management will need some locking as well,
1263 * this isn't dealt with yet.
1268 * of_find_node_by_phandle - Find a node given a phandle
1269 * @handle: phandle of the node to find
1271 * Returns a node pointer with refcount incremented, use
1272 * of_node_put() on it when done.
1274 struct device_node
*of_find_node_by_phandle(phandle handle
)
1276 struct device_node
*np
;
1278 read_lock(&devtree_lock
);
1279 for (np
= allnodes
; np
!= 0; np
= np
->allnext
)
1280 if (np
->linux_phandle
== handle
)
1283 read_unlock(&devtree_lock
);
1286 EXPORT_SYMBOL(of_find_node_by_phandle
);
1289 * of_find_next_cache_node - Find a node's subsidiary cache
1290 * @np: node of type "cpu" or "cache"
1292 * Returns a node pointer with refcount incremented, use
1293 * of_node_put() on it when done. Caller should hold a reference
1296 struct device_node
*of_find_next_cache_node(struct device_node
*np
)
1298 struct device_node
*child
;
1299 const phandle
*handle
;
1301 handle
= of_get_property(np
, "l2-cache", NULL
);
1303 handle
= of_get_property(np
, "next-level-cache", NULL
);
1306 return of_find_node_by_phandle(*handle
);
1308 /* OF on pmac has nodes instead of properties named "l2-cache"
1309 * beneath CPU nodes.
1311 if (!strcmp(np
->type
, "cpu"))
1312 for_each_child_of_node(np
, child
)
1313 if (!strcmp(child
->type
, "cache"))
1320 * of_node_get - Increment refcount of a node
1321 * @node: Node to inc refcount, NULL is supported to
1322 * simplify writing of callers
1326 struct device_node
*of_node_get(struct device_node
*node
)
1329 kref_get(&node
->kref
);
1332 EXPORT_SYMBOL(of_node_get
);
1334 static inline struct device_node
* kref_to_device_node(struct kref
*kref
)
1336 return container_of(kref
, struct device_node
, kref
);
1340 * of_node_release - release a dynamically allocated node
1341 * @kref: kref element of the node to be released
1343 * In of_node_put() this function is passed to kref_put()
1344 * as the destructor.
1346 static void of_node_release(struct kref
*kref
)
1348 struct device_node
*node
= kref_to_device_node(kref
);
1349 struct property
*prop
= node
->properties
;
1351 /* We should never be releasing nodes that haven't been detached. */
1352 if (!of_node_check_flag(node
, OF_DETACHED
)) {
1353 printk("WARNING: Bad of_node_put() on %s\n", node
->full_name
);
1355 kref_init(&node
->kref
);
1359 if (!of_node_check_flag(node
, OF_DYNAMIC
))
1363 struct property
*next
= prop
->next
;
1370 prop
= node
->deadprops
;
1371 node
->deadprops
= NULL
;
1374 kfree(node
->full_name
);
1380 * of_node_put - Decrement refcount of a node
1381 * @node: Node to dec refcount, NULL is supported to
1382 * simplify writing of callers
1385 void of_node_put(struct device_node
*node
)
1388 kref_put(&node
->kref
, of_node_release
);
1390 EXPORT_SYMBOL(of_node_put
);
1393 * Plug a device node into the tree and global list.
1395 void of_attach_node(struct device_node
*np
)
1397 unsigned long flags
;
1399 write_lock_irqsave(&devtree_lock
, flags
);
1400 np
->sibling
= np
->parent
->child
;
1401 np
->allnext
= allnodes
;
1402 np
->parent
->child
= np
;
1404 write_unlock_irqrestore(&devtree_lock
, flags
);
1408 * "Unplug" a node from the device tree. The caller must hold
1409 * a reference to the node. The memory associated with the node
1410 * is not freed until its refcount goes to zero.
1412 void of_detach_node(struct device_node
*np
)
1414 struct device_node
*parent
;
1415 unsigned long flags
;
1417 write_lock_irqsave(&devtree_lock
, flags
);
1419 parent
= np
->parent
;
1424 allnodes
= np
->allnext
;
1426 struct device_node
*prev
;
1427 for (prev
= allnodes
;
1428 prev
->allnext
!= np
;
1429 prev
= prev
->allnext
)
1431 prev
->allnext
= np
->allnext
;
1434 if (parent
->child
== np
)
1435 parent
->child
= np
->sibling
;
1437 struct device_node
*prevsib
;
1438 for (prevsib
= np
->parent
->child
;
1439 prevsib
->sibling
!= np
;
1440 prevsib
= prevsib
->sibling
)
1442 prevsib
->sibling
= np
->sibling
;
1445 of_node_set_flag(np
, OF_DETACHED
);
1448 write_unlock_irqrestore(&devtree_lock
, flags
);
1451 #ifdef CONFIG_PPC_PSERIES
1453 * Fix up the uninitialized fields in a new device node:
1454 * name, type and pci-specific fields
1457 static int of_finish_dynamic_node(struct device_node
*node
)
1459 struct device_node
*parent
= of_get_parent(node
);
1461 const phandle
*ibm_phandle
;
1463 node
->name
= of_get_property(node
, "name", NULL
);
1464 node
->type
= of_get_property(node
, "device_type", NULL
);
1467 node
->name
= "<NULL>";
1469 node
->type
= "<NULL>";
1476 /* We don't support that function on PowerMac, at least
1479 if (machine_is(powermac
))
1482 /* fix up new node's linux_phandle field */
1483 if ((ibm_phandle
= of_get_property(node
, "ibm,phandle", NULL
)))
1484 node
->linux_phandle
= *ibm_phandle
;
1487 of_node_put(parent
);
1491 static int prom_reconfig_notifier(struct notifier_block
*nb
,
1492 unsigned long action
, void *node
)
1497 case PSERIES_RECONFIG_ADD
:
1498 err
= of_finish_dynamic_node(node
);
1500 printk(KERN_ERR
"finish_node returned %d\n", err
);
1511 static struct notifier_block prom_reconfig_nb
= {
1512 .notifier_call
= prom_reconfig_notifier
,
1513 .priority
= 10, /* This one needs to run first */
1516 static int __init
prom_reconfig_setup(void)
1518 return pSeries_reconfig_notifier_register(&prom_reconfig_nb
);
1520 __initcall(prom_reconfig_setup
);
1524 * Add a property to a node
1526 int prom_add_property(struct device_node
* np
, struct property
* prop
)
1528 struct property
**next
;
1529 unsigned long flags
;
1532 write_lock_irqsave(&devtree_lock
, flags
);
1533 next
= &np
->properties
;
1535 if (strcmp(prop
->name
, (*next
)->name
) == 0) {
1536 /* duplicate ! don't insert it */
1537 write_unlock_irqrestore(&devtree_lock
, flags
);
1540 next
= &(*next
)->next
;
1543 write_unlock_irqrestore(&devtree_lock
, flags
);
1545 #ifdef CONFIG_PROC_DEVICETREE
1546 /* try to add to proc as well if it was initialized */
1548 proc_device_tree_add_prop(np
->pde
, prop
);
1549 #endif /* CONFIG_PROC_DEVICETREE */
1555 * Remove a property from a node. Note that we don't actually
1556 * remove it, since we have given out who-knows-how-many pointers
1557 * to the data using get-property. Instead we just move the property
1558 * to the "dead properties" list, so it won't be found any more.
1560 int prom_remove_property(struct device_node
*np
, struct property
*prop
)
1562 struct property
**next
;
1563 unsigned long flags
;
1566 write_lock_irqsave(&devtree_lock
, flags
);
1567 next
= &np
->properties
;
1569 if (*next
== prop
) {
1570 /* found the node */
1572 prop
->next
= np
->deadprops
;
1573 np
->deadprops
= prop
;
1577 next
= &(*next
)->next
;
1579 write_unlock_irqrestore(&devtree_lock
, flags
);
1584 #ifdef CONFIG_PROC_DEVICETREE
1585 /* try to remove the proc node as well */
1587 proc_device_tree_remove_prop(np
->pde
, prop
);
1588 #endif /* CONFIG_PROC_DEVICETREE */
1594 * Update a property in a node. Note that we don't actually
1595 * remove it, since we have given out who-knows-how-many pointers
1596 * to the data using get-property. Instead we just move the property
1597 * to the "dead properties" list, and add the new property to the
1600 int prom_update_property(struct device_node
*np
,
1601 struct property
*newprop
,
1602 struct property
*oldprop
)
1604 struct property
**next
;
1605 unsigned long flags
;
1608 write_lock_irqsave(&devtree_lock
, flags
);
1609 next
= &np
->properties
;
1611 if (*next
== oldprop
) {
1612 /* found the node */
1613 newprop
->next
= oldprop
->next
;
1615 oldprop
->next
= np
->deadprops
;
1616 np
->deadprops
= oldprop
;
1620 next
= &(*next
)->next
;
1622 write_unlock_irqrestore(&devtree_lock
, flags
);
1627 #ifdef CONFIG_PROC_DEVICETREE
1628 /* try to add to proc as well if it was initialized */
1630 proc_device_tree_update_prop(np
->pde
, newprop
, oldprop
);
1631 #endif /* CONFIG_PROC_DEVICETREE */
1637 /* Find the device node for a given logical cpu number, also returns the cpu
1638 * local thread number (index in ibm,interrupt-server#s) if relevant and
1639 * asked for (non NULL)
1641 struct device_node
*of_get_cpu_node(int cpu
, unsigned int *thread
)
1644 struct device_node
*np
;
1646 hardid
= get_hard_smp_processor_id(cpu
);
1648 for_each_node_by_type(np
, "cpu") {
1650 unsigned int plen
, t
;
1652 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1653 * fallback to "reg" property and assume no threads
1655 intserv
= of_get_property(np
, "ibm,ppc-interrupt-server#s",
1657 if (intserv
== NULL
) {
1658 const u32
*reg
= of_get_property(np
, "reg", NULL
);
1661 if (*reg
== hardid
) {
1667 plen
/= sizeof(u32
);
1668 for (t
= 0; t
< plen
; t
++) {
1669 if (hardid
== intserv
[t
]) {
1679 EXPORT_SYMBOL(of_get_cpu_node
);
1681 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1682 static struct debugfs_blob_wrapper flat_dt_blob
;
1684 static int __init
export_flat_device_tree(void)
1688 flat_dt_blob
.data
= initial_boot_params
;
1689 flat_dt_blob
.size
= initial_boot_params
->totalsize
;
1691 d
= debugfs_create_blob("flat-device-tree", S_IFREG
| S_IRUSR
,
1692 powerpc_debugfs_root
, &flat_dt_blob
);
1698 __initcall(export_flat_device_tree
);