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;
76 extern rwlock_t devtree_lock; /* temporary while merging */
78 /* export that to outside world */
79 struct device_node *of_chosen;
81 static int __init early_parse_mem(char *p)
86 memory_limit = PAGE_ALIGN(memparse(p, &p));
87 DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit);
91 early_param("mem", early_parse_mem);
94 * move_device_tree - move tree to an unused area, if needed.
96 * The device tree may be allocated beyond our memory limit, or inside the
97 * crash kernel region for kdump. If so, move it out of the way.
99 static void __init move_device_tree(void)
101 unsigned long start, size;
104 DBG("-> move_device_tree\n");
106 start = __pa(initial_boot_params);
107 size = initial_boot_params->totalsize;
109 if ((memory_limit && (start + size) > memory_limit) ||
110 overlaps_crashkernel(start, size)) {
111 p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
112 memcpy(p, initial_boot_params, size);
113 initial_boot_params = (struct boot_param_header *)p;
114 DBG("Moved device tree to 0x%p\n", p);
117 DBG("<- move_device_tree\n");
121 * ibm,pa-features is a per-cpu property that contains a string of
122 * attribute descriptors, each of which has a 2 byte header plus up
123 * to 254 bytes worth of processor attribute bits. First header
124 * byte specifies the number of bytes following the header.
125 * Second header byte is an "attribute-specifier" type, of which
126 * zero is the only currently-defined value.
127 * Implementation: Pass in the byte and bit offset for the feature
128 * that we are interested in. The function will return -1 if the
129 * pa-features property is missing, or a 1/0 to indicate if the feature
130 * is supported/not supported. Note that the bit numbers are
131 * big-endian to match the definition in PAPR.
133 static struct ibm_pa_feature {
134 unsigned long cpu_features; /* CPU_FTR_xxx bit */
135 unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
136 unsigned char pabyte; /* byte number in ibm,pa-features */
137 unsigned char pabit; /* bit number (big-endian) */
138 unsigned char invert; /* if 1, pa bit set => clear feature */
139 } ibm_pa_features[] __initdata = {
140 {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
141 {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
142 {CPU_FTR_SLB, 0, 0, 2, 0},
143 {CPU_FTR_CTRL, 0, 0, 3, 0},
144 {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
145 {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
146 {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
147 {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
150 static void __init scan_features(unsigned long node, unsigned char *ftrs,
151 unsigned long tablelen,
152 struct ibm_pa_feature *fp,
153 unsigned long ft_size)
155 unsigned long i, len, bit;
157 /* find descriptor with type == 0 */
163 return; /* descriptor 0 not found */
170 /* loop over bits we know about */
171 for (i = 0; i < ft_size; ++i, ++fp) {
172 if (fp->pabyte >= ftrs[0])
174 bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
175 if (bit ^ fp->invert) {
176 cur_cpu_spec->cpu_features |= fp->cpu_features;
177 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
179 cur_cpu_spec->cpu_features &= ~fp->cpu_features;
180 cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
185 static void __init check_cpu_pa_features(unsigned long node)
187 unsigned char *pa_ftrs;
188 unsigned long tablelen;
190 pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
194 scan_features(node, pa_ftrs, tablelen,
195 ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
198 #ifdef CONFIG_PPC_STD_MMU_64
199 static void __init check_cpu_slb_size(unsigned long node)
203 slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
204 if (slb_size_ptr != NULL) {
205 mmu_slb_size = *slb_size_ptr;
208 slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
209 if (slb_size_ptr != NULL) {
210 mmu_slb_size = *slb_size_ptr;
214 #define check_cpu_slb_size(node) do { } while(0)
217 static struct feature_property {
220 unsigned long cpu_feature;
221 unsigned long cpu_user_ftr;
222 } feature_properties[] __initdata = {
223 #ifdef CONFIG_ALTIVEC
224 {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
225 {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
226 #endif /* CONFIG_ALTIVEC */
228 /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
229 {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
230 #endif /* CONFIG_VSX */
232 {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
233 {"ibm,purr", 1, CPU_FTR_PURR, 0},
234 {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
235 #endif /* CONFIG_PPC64 */
238 #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
239 static inline void identical_pvr_fixup(unsigned long node)
242 char *model = of_get_flat_dt_prop(node, "model", NULL);
245 * Since 440GR(x)/440EP(x) processors have the same pvr,
246 * we check the node path and set bit 28 in the cur_cpu_spec
247 * pvr for EP(x) processor version. This bit is always 0 in
248 * the "real" pvr. Then we call identify_cpu again with
249 * the new logical pvr to enable FPU support.
251 if (model && strstr(model, "440EP")) {
252 pvr = cur_cpu_spec->pvr_value | 0x8;
253 identify_cpu(0, pvr);
254 DBG("Using logical pvr %x for %s\n", pvr, model);
258 #define identical_pvr_fixup(node) do { } while(0)
261 static void __init check_cpu_feature_properties(unsigned long node)
264 struct feature_property *fp = feature_properties;
267 for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
268 prop = of_get_flat_dt_prop(node, fp->name, NULL);
269 if (prop && *prop >= fp->min_value) {
270 cur_cpu_spec->cpu_features |= fp->cpu_feature;
271 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
276 static int __init early_init_dt_scan_cpus(unsigned long node,
277 const char *uname, int depth,
280 static int logical_cpuid = 0;
281 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
288 /* We are scanning "cpu" nodes only */
289 if (type == NULL || strcmp(type, "cpu") != 0)
292 /* Get physical cpuid */
293 intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
295 nthreads = len / sizeof(int);
297 intserv = of_get_flat_dt_prop(node, "reg", NULL);
302 * Now see if any of these threads match our boot cpu.
303 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
305 for (i = 0; i < nthreads; i++) {
307 * version 2 of the kexec param format adds the phys cpuid of
310 if (initial_boot_params && initial_boot_params->version >= 2) {
312 initial_boot_params->boot_cpuid_phys) {
318 * Check if it's the boot-cpu, set it's hw index now,
319 * unfortunately this format did not support booting
320 * off secondary threads.
322 if (of_get_flat_dt_prop(node,
323 "linux,boot-cpu", NULL) != NULL) {
330 /* logical cpu id is always 0 on UP kernels */
336 DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
338 boot_cpuid = logical_cpuid;
339 set_hard_smp_processor_id(boot_cpuid, intserv[i]);
342 * PAPR defines "logical" PVR values for cpus that
343 * meet various levels of the architecture:
344 * 0x0f000001 Architecture version 2.04
345 * 0x0f000002 Architecture version 2.05
346 * If the cpu-version property in the cpu node contains
347 * such a value, we call identify_cpu again with the
348 * logical PVR value in order to use the cpu feature
349 * bits appropriate for the architecture level.
351 * A POWER6 partition in "POWER6 architected" mode
352 * uses the 0x0f000002 PVR value; in POWER5+ mode
353 * it uses 0x0f000001.
355 prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
356 if (prop && (*prop & 0xff000000) == 0x0f000000)
357 identify_cpu(0, *prop);
359 identical_pvr_fixup(node);
362 check_cpu_feature_properties(node);
363 check_cpu_pa_features(node);
364 check_cpu_slb_size(node);
366 #ifdef CONFIG_PPC_PSERIES
368 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
370 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
376 #ifdef CONFIG_BLK_DEV_INITRD
377 static void __init early_init_dt_check_for_initrd(unsigned long node)
382 DBG("Looking for initrd properties... ");
384 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
386 initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
388 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
390 initrd_end = (unsigned long)
391 __va(of_read_ulong(prop, l/4));
392 initrd_below_start_ok = 1;
398 DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end);
401 static inline void early_init_dt_check_for_initrd(unsigned long node)
404 #endif /* CONFIG_BLK_DEV_INITRD */
406 static int __init early_init_dt_scan_chosen(unsigned long node,
407 const char *uname, int depth, void *data)
409 unsigned long *lprop;
413 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
416 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
420 /* check if iommu is forced on or off */
421 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
423 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
427 /* mem=x on the command line is the preferred mechanism */
428 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
430 memory_limit = *lprop;
433 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
435 tce_alloc_start = *lprop;
436 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
438 tce_alloc_end = *lprop;
442 lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
444 crashk_res.start = *lprop;
446 lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
448 crashk_res.end = crashk_res.start + *lprop - 1;
451 early_init_dt_check_for_initrd(node);
453 /* Retreive command line */
454 p = of_get_flat_dt_prop(node, "bootargs", &l);
455 if (p != NULL && l > 0)
456 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
458 #ifdef CONFIG_CMDLINE
459 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
460 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
461 #endif /* CONFIG_CMDLINE */
463 DBG("Command line is: %s\n", cmd_line);
469 static int __init early_init_dt_scan_root(unsigned long node,
470 const char *uname, int depth, void *data)
477 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
478 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
479 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
481 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
482 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
483 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
489 static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
494 return of_read_number(p, s);
497 #ifdef CONFIG_PPC_PSERIES
499 * Interpret the ibm,dynamic-memory property in the
500 * /ibm,dynamic-reconfiguration-memory node.
501 * This contains a list of memory blocks along with NUMA affinity
504 static int __init early_init_dt_scan_drconf_memory(unsigned long node)
506 cell_t *dm, *ls, *usm;
507 unsigned long l, n, flags;
508 u64 base, size, lmb_size;
509 unsigned int is_kexec_kdump = 0, rngs;
511 ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
512 if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
514 lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
516 dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
517 if (dm == NULL || l < sizeof(cell_t))
520 n = *dm++; /* number of entries */
521 if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
524 /* check if this is a kexec/kdump kernel. */
525 usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
530 for (; n != 0; --n) {
531 base = dt_mem_next_cell(dt_root_addr_cells, &dm);
533 /* skip DRC index, pad, assoc. list index, flags */
535 /* skip this block if the reserved bit is set in flags (0x80)
536 or if the block is not assigned to this partition (0x8) */
537 if ((flags & 0x80) || !(flags & 0x8))
541 if (is_kexec_kdump) {
543 * For each lmb in ibm,dynamic-memory, a corresponding
544 * entry in linux,drconf-usable-memory property contains
545 * a counter 'p' followed by 'p' (base, size) duple.
546 * Now read the counter from
547 * linux,drconf-usable-memory property
549 rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
550 if (!rngs) /* there are no (base, size) duple */
554 if (is_kexec_kdump) {
555 base = dt_mem_next_cell(dt_root_addr_cells,
557 size = dt_mem_next_cell(dt_root_size_cells,
561 if (base >= 0x80000000ul)
563 if ((base + size) > 0x80000000ul)
564 size = 0x80000000ul - base;
573 #define early_init_dt_scan_drconf_memory(node) 0
574 #endif /* CONFIG_PPC_PSERIES */
576 static int __init early_init_dt_scan_memory(unsigned long node,
577 const char *uname, int depth, void *data)
579 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
583 /* Look for the ibm,dynamic-reconfiguration-memory node */
585 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
586 return early_init_dt_scan_drconf_memory(node);
588 /* We are scanning "memory" nodes only */
591 * The longtrail doesn't have a device_type on the
592 * /memory node, so look for the node called /memory@0.
594 if (depth != 1 || strcmp(uname, "memory@0") != 0)
596 } else if (strcmp(type, "memory") != 0)
599 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
601 reg = of_get_flat_dt_prop(node, "reg", &l);
605 endp = reg + (l / sizeof(cell_t));
607 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
608 uname, l, reg[0], reg[1], reg[2], reg[3]);
610 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
613 base = dt_mem_next_cell(dt_root_addr_cells, ®);
614 size = dt_mem_next_cell(dt_root_size_cells, ®);
618 DBG(" - %llx , %llx\n", (unsigned long long)base,
619 (unsigned long long)size);
622 if (base >= 0x80000000ul)
624 if ((base + size) > 0x80000000ul)
625 size = 0x80000000ul - base;
630 memstart_addr = min((u64)memstart_addr, base);
636 static void __init early_reserve_mem(void)
640 unsigned long self_base;
641 unsigned long self_size;
643 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
644 initial_boot_params->off_mem_rsvmap);
646 /* before we do anything, lets reserve the dt blob */
647 self_base = __pa((unsigned long)initial_boot_params);
648 self_size = initial_boot_params->totalsize;
649 lmb_reserve(self_base, self_size);
651 #ifdef CONFIG_BLK_DEV_INITRD
652 /* then reserve the initrd, if any */
653 if (initrd_start && (initrd_end > initrd_start))
654 lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
655 #endif /* CONFIG_BLK_DEV_INITRD */
659 * Handle the case where we might be booting from an old kexec
660 * image that setup the mem_rsvmap as pairs of 32-bit values
662 if (*reserve_map > 0xffffffffull) {
663 u32 base_32, size_32;
664 u32 *reserve_map_32 = (u32 *)reserve_map;
667 base_32 = *(reserve_map_32++);
668 size_32 = *(reserve_map_32++);
671 /* skip if the reservation is for the blob */
672 if (base_32 == self_base && size_32 == self_size)
674 DBG("reserving: %x -> %x\n", base_32, size_32);
675 lmb_reserve(base_32, size_32);
681 base = *(reserve_map++);
682 size = *(reserve_map++);
685 DBG("reserving: %llx -> %llx\n", base, size);
686 lmb_reserve(base, size);
690 #ifdef CONFIG_PHYP_DUMP
692 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
694 * Function to find the largest size we need to reserve
695 * during early boot process.
697 * It either looks for boot param and returns that OR
698 * returns larger of 256 or 5% rounded down to multiples of 256MB.
701 static inline unsigned long phyp_dump_calculate_reserve_size(void)
705 if (phyp_dump_info->reserve_bootvar)
706 return phyp_dump_info->reserve_bootvar;
708 /* divide by 20 to get 5% of value */
709 tmp = lmb_end_of_DRAM();
712 /* round it down in multiples of 256 */
713 tmp = tmp & ~0x0FFFFFFFUL;
715 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
719 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
721 * This routine may reserve memory regions in the kernel only
722 * if the system is supported and a dump was taken in last
723 * boot instance or if the hardware is supported and the
724 * scratch area needs to be setup. In other instances it returns
725 * without reserving anything. The memory in case of dump being
726 * active is freed when the dump is collected (by userland tools).
728 static void __init phyp_dump_reserve_mem(void)
730 unsigned long base, size;
731 unsigned long variable_reserve_size;
733 if (!phyp_dump_info->phyp_dump_configured) {
734 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
738 if (!phyp_dump_info->phyp_dump_at_boot) {
739 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
743 variable_reserve_size = phyp_dump_calculate_reserve_size();
745 if (phyp_dump_info->phyp_dump_is_active) {
746 /* Reserve *everything* above RMR.Area freed by userland tools*/
747 base = variable_reserve_size;
748 size = lmb_end_of_DRAM() - base;
750 /* XXX crashed_ram_end is wrong, since it may be beyond
751 * the memory_limit, it will need to be adjusted. */
752 lmb_reserve(base, size);
754 phyp_dump_info->init_reserve_start = base;
755 phyp_dump_info->init_reserve_size = size;
757 size = phyp_dump_info->cpu_state_size +
758 phyp_dump_info->hpte_region_size +
759 variable_reserve_size;
760 base = lmb_end_of_DRAM() - size;
761 lmb_reserve(base, size);
762 phyp_dump_info->init_reserve_start = base;
763 phyp_dump_info->init_reserve_size = size;
767 static inline void __init phyp_dump_reserve_mem(void) {}
768 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
771 void __init early_init_devtree(void *params)
775 DBG(" -> early_init_devtree(%p)\n", params);
777 /* Setup flat device-tree pointer */
778 initial_boot_params = params;
780 #ifdef CONFIG_PPC_RTAS
781 /* Some machines might need RTAS info for debugging, grab it now. */
782 of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
785 #ifdef CONFIG_PHYP_DUMP
786 /* scan tree to see if dump occured during last boot */
787 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
790 /* Retrieve various informations from the /chosen node of the
791 * device-tree, including the platform type, initrd location and
792 * size, TCE reserve, and more ...
794 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
796 /* Scan memory nodes and rebuild LMBs */
798 of_scan_flat_dt(early_init_dt_scan_root, NULL);
799 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
801 /* Save command line for /proc/cmdline and then parse parameters */
802 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
805 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
806 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
807 /* If relocatable, reserve first 32k for interrupt vectors etc. */
808 if (PHYSICAL_START > MEMORY_START)
809 lmb_reserve(MEMORY_START, 0x8000);
810 reserve_kdump_trampoline();
811 reserve_crashkernel();
813 phyp_dump_reserve_mem();
815 limit = memory_limit;
819 /* Ensure that total memory size is page-aligned, because
820 * otherwise mark_bootmem() gets upset. */
822 memsize = lmb_phys_mem_size();
823 if ((memsize & PAGE_MASK) != memsize)
824 limit = memsize & PAGE_MASK;
826 lmb_enforce_memory_limit(limit);
831 DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
833 /* We may need to relocate the flat tree, do it now.
834 * FIXME .. and the initrd too? */
837 DBG("Scanning CPUs ...\n");
839 /* Retreive CPU related informations from the flat tree
840 * (altivec support, boot CPU ID, ...)
842 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
844 DBG(" <- early_init_devtree()\n");
849 * Indicates whether the root node has a given value in its
850 * compatible property.
852 int machine_is_compatible(const char *compat)
854 struct device_node *root;
857 root = of_find_node_by_path("/");
859 rc = of_device_is_compatible(root, compat);
864 EXPORT_SYMBOL(machine_is_compatible);
868 * New implementation of the OF "find" APIs, return a refcounted
869 * object, call of_node_put() when done. The device tree and list
870 * are protected by a rw_lock.
872 * Note that property management will need some locking as well,
873 * this isn't dealt with yet.
878 * of_find_node_by_phandle - Find a node given a phandle
879 * @handle: phandle of the node to find
881 * Returns a node pointer with refcount incremented, use
882 * of_node_put() on it when done.
884 struct device_node *of_find_node_by_phandle(phandle handle)
886 struct device_node *np;
888 read_lock(&devtree_lock);
889 for (np = allnodes; np != 0; np = np->allnext)
890 if (np->linux_phandle == handle)
893 read_unlock(&devtree_lock);
896 EXPORT_SYMBOL(of_find_node_by_phandle);
899 * of_find_next_cache_node - Find a node's subsidiary cache
900 * @np: node of type "cpu" or "cache"
902 * Returns a node pointer with refcount incremented, use
903 * of_node_put() on it when done. Caller should hold a reference
906 struct device_node *of_find_next_cache_node(struct device_node *np)
908 struct device_node *child;
909 const phandle *handle;
911 handle = of_get_property(np, "l2-cache", NULL);
913 handle = of_get_property(np, "next-level-cache", NULL);
916 return of_find_node_by_phandle(*handle);
918 /* OF on pmac has nodes instead of properties named "l2-cache"
921 if (!strcmp(np->type, "cpu"))
922 for_each_child_of_node(np, child)
923 if (!strcmp(child->type, "cache"))
930 * of_node_get - Increment refcount of a node
931 * @node: Node to inc refcount, NULL is supported to
932 * simplify writing of callers
936 struct device_node *of_node_get(struct device_node *node)
939 kref_get(&node->kref);
942 EXPORT_SYMBOL(of_node_get);
944 static inline struct device_node * kref_to_device_node(struct kref *kref)
946 return container_of(kref, struct device_node, kref);
950 * of_node_release - release a dynamically allocated node
951 * @kref: kref element of the node to be released
953 * In of_node_put() this function is passed to kref_put()
956 static void of_node_release(struct kref *kref)
958 struct device_node *node = kref_to_device_node(kref);
959 struct property *prop = node->properties;
961 /* We should never be releasing nodes that haven't been detached. */
962 if (!of_node_check_flag(node, OF_DETACHED)) {
963 printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
965 kref_init(&node->kref);
969 if (!of_node_check_flag(node, OF_DYNAMIC))
973 struct property *next = prop->next;
980 prop = node->deadprops;
981 node->deadprops = NULL;
984 kfree(node->full_name);
990 * of_node_put - Decrement refcount of a node
991 * @node: Node to dec refcount, NULL is supported to
992 * simplify writing of callers
995 void of_node_put(struct device_node *node)
998 kref_put(&node->kref, of_node_release);
1000 EXPORT_SYMBOL(of_node_put);
1003 * Plug a device node into the tree and global list.
1005 void of_attach_node(struct device_node *np)
1007 unsigned long flags;
1009 write_lock_irqsave(&devtree_lock, flags);
1010 np->sibling = np->parent->child;
1011 np->allnext = allnodes;
1012 np->parent->child = np;
1014 write_unlock_irqrestore(&devtree_lock, flags);
1018 * "Unplug" a node from the device tree. The caller must hold
1019 * a reference to the node. The memory associated with the node
1020 * is not freed until its refcount goes to zero.
1022 void of_detach_node(struct device_node *np)
1024 struct device_node *parent;
1025 unsigned long flags;
1027 write_lock_irqsave(&devtree_lock, flags);
1029 parent = np->parent;
1034 allnodes = np->allnext;
1036 struct device_node *prev;
1037 for (prev = allnodes;
1038 prev->allnext != np;
1039 prev = prev->allnext)
1041 prev->allnext = np->allnext;
1044 if (parent->child == np)
1045 parent->child = np->sibling;
1047 struct device_node *prevsib;
1048 for (prevsib = np->parent->child;
1049 prevsib->sibling != np;
1050 prevsib = prevsib->sibling)
1052 prevsib->sibling = np->sibling;
1055 of_node_set_flag(np, OF_DETACHED);
1058 write_unlock_irqrestore(&devtree_lock, flags);
1061 #ifdef CONFIG_PPC_PSERIES
1063 * Fix up the uninitialized fields in a new device node:
1064 * name, type and pci-specific fields
1067 static int of_finish_dynamic_node(struct device_node *node)
1069 struct device_node *parent = of_get_parent(node);
1071 const phandle *ibm_phandle;
1073 node->name = of_get_property(node, "name", NULL);
1074 node->type = of_get_property(node, "device_type", NULL);
1077 node->name = "<NULL>";
1079 node->type = "<NULL>";
1086 /* We don't support that function on PowerMac, at least
1089 if (machine_is(powermac))
1092 /* fix up new node's linux_phandle field */
1093 if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
1094 node->linux_phandle = *ibm_phandle;
1097 of_node_put(parent);
1101 static int prom_reconfig_notifier(struct notifier_block *nb,
1102 unsigned long action, void *node)
1107 case PSERIES_RECONFIG_ADD:
1108 err = of_finish_dynamic_node(node);
1110 printk(KERN_ERR "finish_node returned %d\n", err);
1121 static struct notifier_block prom_reconfig_nb = {
1122 .notifier_call = prom_reconfig_notifier,
1123 .priority = 10, /* This one needs to run first */
1126 static int __init prom_reconfig_setup(void)
1128 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1130 __initcall(prom_reconfig_setup);
1134 * Add a property to a node
1136 int prom_add_property(struct device_node* np, struct property* prop)
1138 struct property **next;
1139 unsigned long flags;
1142 write_lock_irqsave(&devtree_lock, flags);
1143 next = &np->properties;
1145 if (strcmp(prop->name, (*next)->name) == 0) {
1146 /* duplicate ! don't insert it */
1147 write_unlock_irqrestore(&devtree_lock, flags);
1150 next = &(*next)->next;
1153 write_unlock_irqrestore(&devtree_lock, flags);
1155 #ifdef CONFIG_PROC_DEVICETREE
1156 /* try to add to proc as well if it was initialized */
1158 proc_device_tree_add_prop(np->pde, prop);
1159 #endif /* CONFIG_PROC_DEVICETREE */
1165 * Remove a property from a node. Note that we don't actually
1166 * remove it, since we have given out who-knows-how-many pointers
1167 * to the data using get-property. Instead we just move the property
1168 * to the "dead properties" list, so it won't be found any more.
1170 int prom_remove_property(struct device_node *np, struct property *prop)
1172 struct property **next;
1173 unsigned long flags;
1176 write_lock_irqsave(&devtree_lock, flags);
1177 next = &np->properties;
1179 if (*next == prop) {
1180 /* found the node */
1182 prop->next = np->deadprops;
1183 np->deadprops = prop;
1187 next = &(*next)->next;
1189 write_unlock_irqrestore(&devtree_lock, flags);
1194 #ifdef CONFIG_PROC_DEVICETREE
1195 /* try to remove the proc node as well */
1197 proc_device_tree_remove_prop(np->pde, prop);
1198 #endif /* CONFIG_PROC_DEVICETREE */
1204 * Update a property in a node. Note that we don't actually
1205 * remove it, since we have given out who-knows-how-many pointers
1206 * to the data using get-property. Instead we just move the property
1207 * to the "dead properties" list, and add the new property to the
1210 int prom_update_property(struct device_node *np,
1211 struct property *newprop,
1212 struct property *oldprop)
1214 struct property **next;
1215 unsigned long flags;
1218 write_lock_irqsave(&devtree_lock, flags);
1219 next = &np->properties;
1221 if (*next == oldprop) {
1222 /* found the node */
1223 newprop->next = oldprop->next;
1225 oldprop->next = np->deadprops;
1226 np->deadprops = oldprop;
1230 next = &(*next)->next;
1232 write_unlock_irqrestore(&devtree_lock, flags);
1237 #ifdef CONFIG_PROC_DEVICETREE
1238 /* try to add to proc as well if it was initialized */
1240 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1241 #endif /* CONFIG_PROC_DEVICETREE */
1247 /* Find the device node for a given logical cpu number, also returns the cpu
1248 * local thread number (index in ibm,interrupt-server#s) if relevant and
1249 * asked for (non NULL)
1251 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
1254 struct device_node *np;
1256 hardid = get_hard_smp_processor_id(cpu);
1258 for_each_node_by_type(np, "cpu") {
1260 unsigned int plen, t;
1262 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1263 * fallback to "reg" property and assume no threads
1265 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
1267 if (intserv == NULL) {
1268 const u32 *reg = of_get_property(np, "reg", NULL);
1271 if (*reg == hardid) {
1277 plen /= sizeof(u32);
1278 for (t = 0; t < plen; t++) {
1279 if (hardid == intserv[t]) {
1289 EXPORT_SYMBOL(of_get_cpu_node);
1291 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1292 static struct debugfs_blob_wrapper flat_dt_blob;
1294 static int __init export_flat_device_tree(void)
1298 flat_dt_blob.data = initial_boot_params;
1299 flat_dt_blob.size = initial_boot_params->totalsize;
1301 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1302 powerpc_debugfs_root, &flat_dt_blob);
1308 __initcall(export_flat_device_tree);