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 int __initdata iommu_is_off
;
66 int __initdata iommu_force_on
;
67 unsigned long tce_alloc_start
, tce_alloc_end
;
72 extern rwlock_t devtree_lock
; /* temporary while merging */
74 /* export that to outside world */
75 struct device_node
*of_chosen
;
77 static int __init
early_parse_mem(char *p
)
82 memory_limit
= PAGE_ALIGN(memparse(p
, &p
));
83 DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit
);
87 early_param("mem", early_parse_mem
);
90 * move_device_tree - move tree to an unused area, if needed.
92 * The device tree may be allocated beyond our memory limit, or inside the
93 * crash kernel region for kdump. If so, move it out of the way.
95 static void __init
move_device_tree(void)
97 unsigned long start
, size
;
100 DBG("-> move_device_tree\n");
102 start
= __pa(initial_boot_params
);
103 size
= initial_boot_params
->totalsize
;
105 if ((memory_limit
&& (start
+ size
) > memory_limit
) ||
106 overlaps_crashkernel(start
, size
)) {
107 p
= __va(lmb_alloc_base(size
, PAGE_SIZE
, lmb
.rmo_size
));
108 memcpy(p
, initial_boot_params
, size
);
109 initial_boot_params
= (struct boot_param_header
*)p
;
110 DBG("Moved device tree to 0x%p\n", p
);
113 DBG("<- move_device_tree\n");
117 * ibm,pa-features is a per-cpu property that contains a string of
118 * attribute descriptors, each of which has a 2 byte header plus up
119 * to 254 bytes worth of processor attribute bits. First header
120 * byte specifies the number of bytes following the header.
121 * Second header byte is an "attribute-specifier" type, of which
122 * zero is the only currently-defined value.
123 * Implementation: Pass in the byte and bit offset for the feature
124 * that we are interested in. The function will return -1 if the
125 * pa-features property is missing, or a 1/0 to indicate if the feature
126 * is supported/not supported. Note that the bit numbers are
127 * big-endian to match the definition in PAPR.
129 static struct ibm_pa_feature
{
130 unsigned long cpu_features
; /* CPU_FTR_xxx bit */
131 unsigned int cpu_user_ftrs
; /* PPC_FEATURE_xxx bit */
132 unsigned char pabyte
; /* byte number in ibm,pa-features */
133 unsigned char pabit
; /* bit number (big-endian) */
134 unsigned char invert
; /* if 1, pa bit set => clear feature */
135 } ibm_pa_features
[] __initdata
= {
136 {0, PPC_FEATURE_HAS_MMU
, 0, 0, 0},
137 {0, PPC_FEATURE_HAS_FPU
, 0, 1, 0},
138 {CPU_FTR_SLB
, 0, 0, 2, 0},
139 {CPU_FTR_CTRL
, 0, 0, 3, 0},
140 {CPU_FTR_NOEXECUTE
, 0, 0, 6, 0},
141 {CPU_FTR_NODSISRALIGN
, 0, 1, 1, 1},
142 {CPU_FTR_CI_LARGE_PAGE
, 0, 1, 2, 0},
143 {CPU_FTR_REAL_LE
, PPC_FEATURE_TRUE_LE
, 5, 0, 0},
146 static void __init
scan_features(unsigned long node
, unsigned char *ftrs
,
147 unsigned long tablelen
,
148 struct ibm_pa_feature
*fp
,
149 unsigned long ft_size
)
151 unsigned long i
, len
, bit
;
153 /* find descriptor with type == 0 */
159 return; /* descriptor 0 not found */
166 /* loop over bits we know about */
167 for (i
= 0; i
< ft_size
; ++i
, ++fp
) {
168 if (fp
->pabyte
>= ftrs
[0])
170 bit
= (ftrs
[2 + fp
->pabyte
] >> (7 - fp
->pabit
)) & 1;
171 if (bit
^ fp
->invert
) {
172 cur_cpu_spec
->cpu_features
|= fp
->cpu_features
;
173 cur_cpu_spec
->cpu_user_features
|= fp
->cpu_user_ftrs
;
175 cur_cpu_spec
->cpu_features
&= ~fp
->cpu_features
;
176 cur_cpu_spec
->cpu_user_features
&= ~fp
->cpu_user_ftrs
;
181 static void __init
check_cpu_pa_features(unsigned long node
)
183 unsigned char *pa_ftrs
;
184 unsigned long tablelen
;
186 pa_ftrs
= of_get_flat_dt_prop(node
, "ibm,pa-features", &tablelen
);
190 scan_features(node
, pa_ftrs
, tablelen
,
191 ibm_pa_features
, ARRAY_SIZE(ibm_pa_features
));
194 #ifdef CONFIG_PPC_STD_MMU_64
195 static void __init
check_cpu_slb_size(unsigned long node
)
199 slb_size_ptr
= of_get_flat_dt_prop(node
, "slb-size", NULL
);
200 if (slb_size_ptr
!= NULL
) {
201 mmu_slb_size
= *slb_size_ptr
;
204 slb_size_ptr
= of_get_flat_dt_prop(node
, "ibm,slb-size", NULL
);
205 if (slb_size_ptr
!= NULL
) {
206 mmu_slb_size
= *slb_size_ptr
;
210 #define check_cpu_slb_size(node) do { } while(0)
213 static struct feature_property
{
216 unsigned long cpu_feature
;
217 unsigned long cpu_user_ftr
;
218 } feature_properties
[] __initdata
= {
219 #ifdef CONFIG_ALTIVEC
220 {"altivec", 0, CPU_FTR_ALTIVEC
, PPC_FEATURE_HAS_ALTIVEC
},
221 {"ibm,vmx", 1, CPU_FTR_ALTIVEC
, PPC_FEATURE_HAS_ALTIVEC
},
222 #endif /* CONFIG_ALTIVEC */
224 /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
225 {"ibm,vmx", 2, CPU_FTR_VSX
, PPC_FEATURE_HAS_VSX
},
226 #endif /* CONFIG_VSX */
228 {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP
},
229 {"ibm,purr", 1, CPU_FTR_PURR
, 0},
230 {"ibm,spurr", 1, CPU_FTR_SPURR
, 0},
231 #endif /* CONFIG_PPC64 */
234 #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
235 static inline void identical_pvr_fixup(unsigned long node
)
238 char *model
= of_get_flat_dt_prop(node
, "model", NULL
);
241 * Since 440GR(x)/440EP(x) processors have the same pvr,
242 * we check the node path and set bit 28 in the cur_cpu_spec
243 * pvr for EP(x) processor version. This bit is always 0 in
244 * the "real" pvr. Then we call identify_cpu again with
245 * the new logical pvr to enable FPU support.
247 if (model
&& strstr(model
, "440EP")) {
248 pvr
= cur_cpu_spec
->pvr_value
| 0x8;
249 identify_cpu(0, pvr
);
250 DBG("Using logical pvr %x for %s\n", pvr
, model
);
254 #define identical_pvr_fixup(node) do { } while(0)
257 static void __init
check_cpu_feature_properties(unsigned long node
)
260 struct feature_property
*fp
= feature_properties
;
263 for (i
= 0; i
< ARRAY_SIZE(feature_properties
); ++i
, ++fp
) {
264 prop
= of_get_flat_dt_prop(node
, fp
->name
, NULL
);
265 if (prop
&& *prop
>= fp
->min_value
) {
266 cur_cpu_spec
->cpu_features
|= fp
->cpu_feature
;
267 cur_cpu_spec
->cpu_user_features
|= fp
->cpu_user_ftr
;
272 static int __init
early_init_dt_scan_cpus(unsigned long node
,
273 const char *uname
, int depth
,
276 static int logical_cpuid
= 0;
277 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
284 /* We are scanning "cpu" nodes only */
285 if (type
== NULL
|| strcmp(type
, "cpu") != 0)
288 /* Get physical cpuid */
289 intserv
= of_get_flat_dt_prop(node
, "ibm,ppc-interrupt-server#s", &len
);
291 nthreads
= len
/ sizeof(int);
293 intserv
= of_get_flat_dt_prop(node
, "reg", NULL
);
298 * Now see if any of these threads match our boot cpu.
299 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
301 for (i
= 0; i
< nthreads
; i
++) {
303 * version 2 of the kexec param format adds the phys cpuid of
306 if (initial_boot_params
&& initial_boot_params
->version
>= 2) {
308 initial_boot_params
->boot_cpuid_phys
) {
314 * Check if it's the boot-cpu, set it's hw index now,
315 * unfortunately this format did not support booting
316 * off secondary threads.
318 if (of_get_flat_dt_prop(node
,
319 "linux,boot-cpu", NULL
) != NULL
) {
326 /* logical cpu id is always 0 on UP kernels */
332 DBG("boot cpu: logical %d physical %d\n", logical_cpuid
,
334 boot_cpuid
= logical_cpuid
;
335 set_hard_smp_processor_id(boot_cpuid
, intserv
[i
]);
338 * PAPR defines "logical" PVR values for cpus that
339 * meet various levels of the architecture:
340 * 0x0f000001 Architecture version 2.04
341 * 0x0f000002 Architecture version 2.05
342 * If the cpu-version property in the cpu node contains
343 * such a value, we call identify_cpu again with the
344 * logical PVR value in order to use the cpu feature
345 * bits appropriate for the architecture level.
347 * A POWER6 partition in "POWER6 architected" mode
348 * uses the 0x0f000002 PVR value; in POWER5+ mode
349 * it uses 0x0f000001.
351 prop
= of_get_flat_dt_prop(node
, "cpu-version", NULL
);
352 if (prop
&& (*prop
& 0xff000000) == 0x0f000000)
353 identify_cpu(0, *prop
);
355 identical_pvr_fixup(node
);
358 check_cpu_feature_properties(node
);
359 check_cpu_pa_features(node
);
360 check_cpu_slb_size(node
);
362 #ifdef CONFIG_PPC_PSERIES
364 cur_cpu_spec
->cpu_features
|= CPU_FTR_SMT
;
366 cur_cpu_spec
->cpu_features
&= ~CPU_FTR_SMT
;
372 static int __init
early_init_dt_scan_chosen(unsigned long node
,
373 const char *uname
, int depth
, void *data
)
375 unsigned long *lprop
;
379 DBG("search \"chosen\", depth: %d, uname: %s\n", depth
, uname
);
382 (strcmp(uname
, "chosen") != 0 && strcmp(uname
, "chosen@0") != 0))
386 /* check if iommu is forced on or off */
387 if (of_get_flat_dt_prop(node
, "linux,iommu-off", NULL
) != NULL
)
389 if (of_get_flat_dt_prop(node
, "linux,iommu-force-on", NULL
) != NULL
)
393 /* mem=x on the command line is the preferred mechanism */
394 lprop
= of_get_flat_dt_prop(node
, "linux,memory-limit", NULL
);
396 memory_limit
= *lprop
;
399 lprop
= of_get_flat_dt_prop(node
, "linux,tce-alloc-start", NULL
);
401 tce_alloc_start
= *lprop
;
402 lprop
= of_get_flat_dt_prop(node
, "linux,tce-alloc-end", NULL
);
404 tce_alloc_end
= *lprop
;
408 lprop
= of_get_flat_dt_prop(node
, "linux,crashkernel-base", NULL
);
410 crashk_res
.start
= *lprop
;
412 lprop
= of_get_flat_dt_prop(node
, "linux,crashkernel-size", NULL
);
414 crashk_res
.end
= crashk_res
.start
+ *lprop
- 1;
417 early_init_dt_check_for_initrd(node
);
419 /* Retreive command line */
420 p
= of_get_flat_dt_prop(node
, "bootargs", &l
);
421 if (p
!= NULL
&& l
> 0)
422 strlcpy(cmd_line
, p
, min((int)l
, COMMAND_LINE_SIZE
));
424 #ifdef CONFIG_CMDLINE
425 if (p
== NULL
|| l
== 0 || (l
== 1 && (*p
) == 0))
426 strlcpy(cmd_line
, CONFIG_CMDLINE
, COMMAND_LINE_SIZE
);
427 #endif /* CONFIG_CMDLINE */
429 DBG("Command line is: %s\n", cmd_line
);
435 static u64 __init
dt_mem_next_cell(int s
, cell_t
**cellp
)
440 return of_read_number(p
, s
);
443 #ifdef CONFIG_PPC_PSERIES
445 * Interpret the ibm,dynamic-memory property in the
446 * /ibm,dynamic-reconfiguration-memory node.
447 * This contains a list of memory blocks along with NUMA affinity
450 static int __init
early_init_dt_scan_drconf_memory(unsigned long node
)
452 cell_t
*dm
, *ls
, *usm
;
453 unsigned long l
, n
, flags
;
454 u64 base
, size
, lmb_size
;
455 unsigned int is_kexec_kdump
= 0, rngs
;
457 ls
= of_get_flat_dt_prop(node
, "ibm,lmb-size", &l
);
458 if (ls
== NULL
|| l
< dt_root_size_cells
* sizeof(cell_t
))
460 lmb_size
= dt_mem_next_cell(dt_root_size_cells
, &ls
);
462 dm
= of_get_flat_dt_prop(node
, "ibm,dynamic-memory", &l
);
463 if (dm
== NULL
|| l
< sizeof(cell_t
))
466 n
= *dm
++; /* number of entries */
467 if (l
< (n
* (dt_root_addr_cells
+ 4) + 1) * sizeof(cell_t
))
470 /* check if this is a kexec/kdump kernel. */
471 usm
= of_get_flat_dt_prop(node
, "linux,drconf-usable-memory",
476 for (; n
!= 0; --n
) {
477 base
= dt_mem_next_cell(dt_root_addr_cells
, &dm
);
479 /* skip DRC index, pad, assoc. list index, flags */
481 /* skip this block if the reserved bit is set in flags (0x80)
482 or if the block is not assigned to this partition (0x8) */
483 if ((flags
& 0x80) || !(flags
& 0x8))
487 if (is_kexec_kdump
) {
489 * For each lmb in ibm,dynamic-memory, a corresponding
490 * entry in linux,drconf-usable-memory property contains
491 * a counter 'p' followed by 'p' (base, size) duple.
492 * Now read the counter from
493 * linux,drconf-usable-memory property
495 rngs
= dt_mem_next_cell(dt_root_size_cells
, &usm
);
496 if (!rngs
) /* there are no (base, size) duple */
500 if (is_kexec_kdump
) {
501 base
= dt_mem_next_cell(dt_root_addr_cells
,
503 size
= dt_mem_next_cell(dt_root_size_cells
,
507 if (base
>= 0x80000000ul
)
509 if ((base
+ size
) > 0x80000000ul
)
510 size
= 0x80000000ul
- base
;
519 #define early_init_dt_scan_drconf_memory(node) 0
520 #endif /* CONFIG_PPC_PSERIES */
522 static int __init
early_init_dt_scan_memory(unsigned long node
,
523 const char *uname
, int depth
, void *data
)
525 char *type
= of_get_flat_dt_prop(node
, "device_type", NULL
);
529 /* Look for the ibm,dynamic-reconfiguration-memory node */
531 strcmp(uname
, "ibm,dynamic-reconfiguration-memory") == 0)
532 return early_init_dt_scan_drconf_memory(node
);
534 /* We are scanning "memory" nodes only */
537 * The longtrail doesn't have a device_type on the
538 * /memory node, so look for the node called /memory@0.
540 if (depth
!= 1 || strcmp(uname
, "memory@0") != 0)
542 } else if (strcmp(type
, "memory") != 0)
545 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory", &l
);
547 reg
= of_get_flat_dt_prop(node
, "reg", &l
);
551 endp
= reg
+ (l
/ sizeof(cell_t
));
553 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
554 uname
, l
, reg
[0], reg
[1], reg
[2], reg
[3]);
556 while ((endp
- reg
) >= (dt_root_addr_cells
+ dt_root_size_cells
)) {
559 base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
560 size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
564 DBG(" - %llx , %llx\n", (unsigned long long)base
,
565 (unsigned long long)size
);
568 if (base
>= 0x80000000ul
)
570 if ((base
+ size
) > 0x80000000ul
)
571 size
= 0x80000000ul
- base
;
576 memstart_addr
= min((u64
)memstart_addr
, base
);
582 static void __init
early_reserve_mem(void)
586 unsigned long self_base
;
587 unsigned long self_size
;
589 reserve_map
= (u64
*)(((unsigned long)initial_boot_params
) +
590 initial_boot_params
->off_mem_rsvmap
);
592 /* before we do anything, lets reserve the dt blob */
593 self_base
= __pa((unsigned long)initial_boot_params
);
594 self_size
= initial_boot_params
->totalsize
;
595 lmb_reserve(self_base
, self_size
);
597 #ifdef CONFIG_BLK_DEV_INITRD
598 /* then reserve the initrd, if any */
599 if (initrd_start
&& (initrd_end
> initrd_start
))
600 lmb_reserve(__pa(initrd_start
), initrd_end
- initrd_start
);
601 #endif /* CONFIG_BLK_DEV_INITRD */
605 * Handle the case where we might be booting from an old kexec
606 * image that setup the mem_rsvmap as pairs of 32-bit values
608 if (*reserve_map
> 0xffffffffull
) {
609 u32 base_32
, size_32
;
610 u32
*reserve_map_32
= (u32
*)reserve_map
;
613 base_32
= *(reserve_map_32
++);
614 size_32
= *(reserve_map_32
++);
617 /* skip if the reservation is for the blob */
618 if (base_32
== self_base
&& size_32
== self_size
)
620 DBG("reserving: %x -> %x\n", base_32
, size_32
);
621 lmb_reserve(base_32
, size_32
);
627 base
= *(reserve_map
++);
628 size
= *(reserve_map
++);
631 DBG("reserving: %llx -> %llx\n", base
, size
);
632 lmb_reserve(base
, size
);
636 #ifdef CONFIG_PHYP_DUMP
638 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
640 * Function to find the largest size we need to reserve
641 * during early boot process.
643 * It either looks for boot param and returns that OR
644 * returns larger of 256 or 5% rounded down to multiples of 256MB.
647 static inline unsigned long phyp_dump_calculate_reserve_size(void)
651 if (phyp_dump_info
->reserve_bootvar
)
652 return phyp_dump_info
->reserve_bootvar
;
654 /* divide by 20 to get 5% of value */
655 tmp
= lmb_end_of_DRAM();
658 /* round it down in multiples of 256 */
659 tmp
= tmp
& ~0x0FFFFFFFUL
;
661 return (tmp
> PHYP_DUMP_RMR_END
? tmp
: PHYP_DUMP_RMR_END
);
665 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
667 * This routine may reserve memory regions in the kernel only
668 * if the system is supported and a dump was taken in last
669 * boot instance or if the hardware is supported and the
670 * scratch area needs to be setup. In other instances it returns
671 * without reserving anything. The memory in case of dump being
672 * active is freed when the dump is collected (by userland tools).
674 static void __init
phyp_dump_reserve_mem(void)
676 unsigned long base
, size
;
677 unsigned long variable_reserve_size
;
679 if (!phyp_dump_info
->phyp_dump_configured
) {
680 printk(KERN_ERR
"Phyp-dump not supported on this hardware\n");
684 if (!phyp_dump_info
->phyp_dump_at_boot
) {
685 printk(KERN_INFO
"Phyp-dump disabled at boot time\n");
689 variable_reserve_size
= phyp_dump_calculate_reserve_size();
691 if (phyp_dump_info
->phyp_dump_is_active
) {
692 /* Reserve *everything* above RMR.Area freed by userland tools*/
693 base
= variable_reserve_size
;
694 size
= lmb_end_of_DRAM() - base
;
696 /* XXX crashed_ram_end is wrong, since it may be beyond
697 * the memory_limit, it will need to be adjusted. */
698 lmb_reserve(base
, size
);
700 phyp_dump_info
->init_reserve_start
= base
;
701 phyp_dump_info
->init_reserve_size
= size
;
703 size
= phyp_dump_info
->cpu_state_size
+
704 phyp_dump_info
->hpte_region_size
+
705 variable_reserve_size
;
706 base
= lmb_end_of_DRAM() - size
;
707 lmb_reserve(base
, size
);
708 phyp_dump_info
->init_reserve_start
= base
;
709 phyp_dump_info
->init_reserve_size
= size
;
713 static inline void __init
phyp_dump_reserve_mem(void) {}
714 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
717 void __init
early_init_devtree(void *params
)
721 DBG(" -> early_init_devtree(%p)\n", params
);
723 /* Setup flat device-tree pointer */
724 initial_boot_params
= params
;
726 #ifdef CONFIG_PPC_RTAS
727 /* Some machines might need RTAS info for debugging, grab it now. */
728 of_scan_flat_dt(early_init_dt_scan_rtas
, NULL
);
731 #ifdef CONFIG_PHYP_DUMP
732 /* scan tree to see if dump occured during last boot */
733 of_scan_flat_dt(early_init_dt_scan_phyp_dump
, NULL
);
736 /* Retrieve various informations from the /chosen node of the
737 * device-tree, including the platform type, initrd location and
738 * size, TCE reserve, and more ...
740 of_scan_flat_dt(early_init_dt_scan_chosen
, NULL
);
742 /* Scan memory nodes and rebuild LMBs */
744 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
745 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
747 /* Save command line for /proc/cmdline and then parse parameters */
748 strlcpy(boot_command_line
, cmd_line
, COMMAND_LINE_SIZE
);
751 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
752 lmb_reserve(PHYSICAL_START
, __pa(klimit
) - PHYSICAL_START
);
753 /* If relocatable, reserve first 32k for interrupt vectors etc. */
754 if (PHYSICAL_START
> MEMORY_START
)
755 lmb_reserve(MEMORY_START
, 0x8000);
756 reserve_kdump_trampoline();
757 reserve_crashkernel();
759 phyp_dump_reserve_mem();
761 limit
= memory_limit
;
765 /* Ensure that total memory size is page-aligned, because
766 * otherwise mark_bootmem() gets upset. */
768 memsize
= lmb_phys_mem_size();
769 if ((memsize
& PAGE_MASK
) != memsize
)
770 limit
= memsize
& PAGE_MASK
;
772 lmb_enforce_memory_limit(limit
);
777 DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
779 /* We may need to relocate the flat tree, do it now.
780 * FIXME .. and the initrd too? */
783 DBG("Scanning CPUs ...\n");
785 /* Retreive CPU related informations from the flat tree
786 * (altivec support, boot CPU ID, ...)
788 of_scan_flat_dt(early_init_dt_scan_cpus
, NULL
);
790 DBG(" <- early_init_devtree()\n");
795 * Indicates whether the root node has a given value in its
796 * compatible property.
798 int machine_is_compatible(const char *compat
)
800 struct device_node
*root
;
803 root
= of_find_node_by_path("/");
805 rc
= of_device_is_compatible(root
, compat
);
810 EXPORT_SYMBOL(machine_is_compatible
);
814 * New implementation of the OF "find" APIs, return a refcounted
815 * object, call of_node_put() when done. The device tree and list
816 * are protected by a rw_lock.
818 * Note that property management will need some locking as well,
819 * this isn't dealt with yet.
824 * of_find_node_by_phandle - Find a node given a phandle
825 * @handle: phandle of the node to find
827 * Returns a node pointer with refcount incremented, use
828 * of_node_put() on it when done.
830 struct device_node
*of_find_node_by_phandle(phandle handle
)
832 struct device_node
*np
;
834 read_lock(&devtree_lock
);
835 for (np
= allnodes
; np
!= 0; np
= np
->allnext
)
836 if (np
->linux_phandle
== handle
)
839 read_unlock(&devtree_lock
);
842 EXPORT_SYMBOL(of_find_node_by_phandle
);
845 * of_find_next_cache_node - Find a node's subsidiary cache
846 * @np: node of type "cpu" or "cache"
848 * Returns a node pointer with refcount incremented, use
849 * of_node_put() on it when done. Caller should hold a reference
852 struct device_node
*of_find_next_cache_node(struct device_node
*np
)
854 struct device_node
*child
;
855 const phandle
*handle
;
857 handle
= of_get_property(np
, "l2-cache", NULL
);
859 handle
= of_get_property(np
, "next-level-cache", NULL
);
862 return of_find_node_by_phandle(*handle
);
864 /* OF on pmac has nodes instead of properties named "l2-cache"
867 if (!strcmp(np
->type
, "cpu"))
868 for_each_child_of_node(np
, child
)
869 if (!strcmp(child
->type
, "cache"))
876 * of_node_get - Increment refcount of a node
877 * @node: Node to inc refcount, NULL is supported to
878 * simplify writing of callers
882 struct device_node
*of_node_get(struct device_node
*node
)
885 kref_get(&node
->kref
);
888 EXPORT_SYMBOL(of_node_get
);
890 static inline struct device_node
* kref_to_device_node(struct kref
*kref
)
892 return container_of(kref
, struct device_node
, kref
);
896 * of_node_release - release a dynamically allocated node
897 * @kref: kref element of the node to be released
899 * In of_node_put() this function is passed to kref_put()
902 static void of_node_release(struct kref
*kref
)
904 struct device_node
*node
= kref_to_device_node(kref
);
905 struct property
*prop
= node
->properties
;
907 /* We should never be releasing nodes that haven't been detached. */
908 if (!of_node_check_flag(node
, OF_DETACHED
)) {
909 printk("WARNING: Bad of_node_put() on %s\n", node
->full_name
);
911 kref_init(&node
->kref
);
915 if (!of_node_check_flag(node
, OF_DYNAMIC
))
919 struct property
*next
= prop
->next
;
926 prop
= node
->deadprops
;
927 node
->deadprops
= NULL
;
930 kfree(node
->full_name
);
936 * of_node_put - Decrement refcount of a node
937 * @node: Node to dec refcount, NULL is supported to
938 * simplify writing of callers
941 void of_node_put(struct device_node
*node
)
944 kref_put(&node
->kref
, of_node_release
);
946 EXPORT_SYMBOL(of_node_put
);
949 * Plug a device node into the tree and global list.
951 void of_attach_node(struct device_node
*np
)
955 write_lock_irqsave(&devtree_lock
, flags
);
956 np
->sibling
= np
->parent
->child
;
957 np
->allnext
= allnodes
;
958 np
->parent
->child
= np
;
960 write_unlock_irqrestore(&devtree_lock
, flags
);
964 * "Unplug" a node from the device tree. The caller must hold
965 * a reference to the node. The memory associated with the node
966 * is not freed until its refcount goes to zero.
968 void of_detach_node(struct device_node
*np
)
970 struct device_node
*parent
;
973 write_lock_irqsave(&devtree_lock
, flags
);
980 allnodes
= np
->allnext
;
982 struct device_node
*prev
;
983 for (prev
= allnodes
;
985 prev
= prev
->allnext
)
987 prev
->allnext
= np
->allnext
;
990 if (parent
->child
== np
)
991 parent
->child
= np
->sibling
;
993 struct device_node
*prevsib
;
994 for (prevsib
= np
->parent
->child
;
995 prevsib
->sibling
!= np
;
996 prevsib
= prevsib
->sibling
)
998 prevsib
->sibling
= np
->sibling
;
1001 of_node_set_flag(np
, OF_DETACHED
);
1004 write_unlock_irqrestore(&devtree_lock
, flags
);
1007 #ifdef CONFIG_PPC_PSERIES
1009 * Fix up the uninitialized fields in a new device node:
1010 * name, type and pci-specific fields
1013 static int of_finish_dynamic_node(struct device_node
*node
)
1015 struct device_node
*parent
= of_get_parent(node
);
1017 const phandle
*ibm_phandle
;
1019 node
->name
= of_get_property(node
, "name", NULL
);
1020 node
->type
= of_get_property(node
, "device_type", NULL
);
1023 node
->name
= "<NULL>";
1025 node
->type
= "<NULL>";
1032 /* We don't support that function on PowerMac, at least
1035 if (machine_is(powermac
))
1038 /* fix up new node's linux_phandle field */
1039 if ((ibm_phandle
= of_get_property(node
, "ibm,phandle", NULL
)))
1040 node
->linux_phandle
= *ibm_phandle
;
1043 of_node_put(parent
);
1047 static int prom_reconfig_notifier(struct notifier_block
*nb
,
1048 unsigned long action
, void *node
)
1053 case PSERIES_RECONFIG_ADD
:
1054 err
= of_finish_dynamic_node(node
);
1056 printk(KERN_ERR
"finish_node returned %d\n", err
);
1067 static struct notifier_block prom_reconfig_nb
= {
1068 .notifier_call
= prom_reconfig_notifier
,
1069 .priority
= 10, /* This one needs to run first */
1072 static int __init
prom_reconfig_setup(void)
1074 return pSeries_reconfig_notifier_register(&prom_reconfig_nb
);
1076 __initcall(prom_reconfig_setup
);
1079 /* Find the device node for a given logical cpu number, also returns the cpu
1080 * local thread number (index in ibm,interrupt-server#s) if relevant and
1081 * asked for (non NULL)
1083 struct device_node
*of_get_cpu_node(int cpu
, unsigned int *thread
)
1086 struct device_node
*np
;
1088 hardid
= get_hard_smp_processor_id(cpu
);
1090 for_each_node_by_type(np
, "cpu") {
1092 unsigned int plen
, t
;
1094 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1095 * fallback to "reg" property and assume no threads
1097 intserv
= of_get_property(np
, "ibm,ppc-interrupt-server#s",
1099 if (intserv
== NULL
) {
1100 const u32
*reg
= of_get_property(np
, "reg", NULL
);
1103 if (*reg
== hardid
) {
1109 plen
/= sizeof(u32
);
1110 for (t
= 0; t
< plen
; t
++) {
1111 if (hardid
== intserv
[t
]) {
1121 EXPORT_SYMBOL(of_get_cpu_node
);
1123 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1124 static struct debugfs_blob_wrapper flat_dt_blob
;
1126 static int __init
export_flat_device_tree(void)
1130 flat_dt_blob
.data
= initial_boot_params
;
1131 flat_dt_blob
.size
= initial_boot_params
->totalsize
;
1133 d
= debugfs_create_blob("flat-device-tree", S_IFREG
| S_IRUSR
,
1134 powerpc_debugfs_root
, &flat_dt_blob
);
1140 __initcall(export_flat_device_tree
);