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 #ifdef CONFIG_PPC_PSERIES
437 * Interpret the ibm,dynamic-memory property in the
438 * /ibm,dynamic-reconfiguration-memory node.
439 * This contains a list of memory blocks along with NUMA affinity
442 static int __init early_init_dt_scan_drconf_memory(unsigned long node)
444 cell_t *dm, *ls, *usm;
445 unsigned long l, n, flags;
446 u64 base, size, lmb_size;
447 unsigned int is_kexec_kdump = 0, rngs;
449 ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
450 if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
452 lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
454 dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
455 if (dm == NULL || l < sizeof(cell_t))
458 n = *dm++; /* number of entries */
459 if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
462 /* check if this is a kexec/kdump kernel. */
463 usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
468 for (; n != 0; --n) {
469 base = dt_mem_next_cell(dt_root_addr_cells, &dm);
471 /* skip DRC index, pad, assoc. list index, flags */
473 /* skip this block if the reserved bit is set in flags (0x80)
474 or if the block is not assigned to this partition (0x8) */
475 if ((flags & 0x80) || !(flags & 0x8))
479 if (is_kexec_kdump) {
481 * For each lmb in ibm,dynamic-memory, a corresponding
482 * entry in linux,drconf-usable-memory property contains
483 * a counter 'p' followed by 'p' (base, size) duple.
484 * Now read the counter from
485 * linux,drconf-usable-memory property
487 rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
488 if (!rngs) /* there are no (base, size) duple */
492 if (is_kexec_kdump) {
493 base = dt_mem_next_cell(dt_root_addr_cells,
495 size = dt_mem_next_cell(dt_root_size_cells,
499 if (base >= 0x80000000ul)
501 if ((base + size) > 0x80000000ul)
502 size = 0x80000000ul - base;
511 #define early_init_dt_scan_drconf_memory(node) 0
512 #endif /* CONFIG_PPC_PSERIES */
514 static int __init early_init_dt_scan_memory(unsigned long node,
515 const char *uname, int depth, void *data)
517 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
521 /* Look for the ibm,dynamic-reconfiguration-memory node */
523 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
524 return early_init_dt_scan_drconf_memory(node);
526 /* We are scanning "memory" nodes only */
529 * The longtrail doesn't have a device_type on the
530 * /memory node, so look for the node called /memory@0.
532 if (depth != 1 || strcmp(uname, "memory@0") != 0)
534 } else if (strcmp(type, "memory") != 0)
537 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
539 reg = of_get_flat_dt_prop(node, "reg", &l);
543 endp = reg + (l / sizeof(cell_t));
545 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
546 uname, l, reg[0], reg[1], reg[2], reg[3]);
548 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
551 base = dt_mem_next_cell(dt_root_addr_cells, ®);
552 size = dt_mem_next_cell(dt_root_size_cells, ®);
556 DBG(" - %llx , %llx\n", (unsigned long long)base,
557 (unsigned long long)size);
560 if (base >= 0x80000000ul)
562 if ((base + size) > 0x80000000ul)
563 size = 0x80000000ul - base;
568 memstart_addr = min((u64)memstart_addr, base);
574 static void __init early_reserve_mem(void)
578 unsigned long self_base;
579 unsigned long self_size;
581 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
582 initial_boot_params->off_mem_rsvmap);
584 /* before we do anything, lets reserve the dt blob */
585 self_base = __pa((unsigned long)initial_boot_params);
586 self_size = initial_boot_params->totalsize;
587 lmb_reserve(self_base, self_size);
589 #ifdef CONFIG_BLK_DEV_INITRD
590 /* then reserve the initrd, if any */
591 if (initrd_start && (initrd_end > initrd_start))
592 lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
593 #endif /* CONFIG_BLK_DEV_INITRD */
597 * Handle the case where we might be booting from an old kexec
598 * image that setup the mem_rsvmap as pairs of 32-bit values
600 if (*reserve_map > 0xffffffffull) {
601 u32 base_32, size_32;
602 u32 *reserve_map_32 = (u32 *)reserve_map;
605 base_32 = *(reserve_map_32++);
606 size_32 = *(reserve_map_32++);
609 /* skip if the reservation is for the blob */
610 if (base_32 == self_base && size_32 == self_size)
612 DBG("reserving: %x -> %x\n", base_32, size_32);
613 lmb_reserve(base_32, size_32);
619 base = *(reserve_map++);
620 size = *(reserve_map++);
623 DBG("reserving: %llx -> %llx\n", base, size);
624 lmb_reserve(base, size);
628 #ifdef CONFIG_PHYP_DUMP
630 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
632 * Function to find the largest size we need to reserve
633 * during early boot process.
635 * It either looks for boot param and returns that OR
636 * returns larger of 256 or 5% rounded down to multiples of 256MB.
639 static inline unsigned long phyp_dump_calculate_reserve_size(void)
643 if (phyp_dump_info->reserve_bootvar)
644 return phyp_dump_info->reserve_bootvar;
646 /* divide by 20 to get 5% of value */
647 tmp = lmb_end_of_DRAM();
650 /* round it down in multiples of 256 */
651 tmp = tmp & ~0x0FFFFFFFUL;
653 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
657 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
659 * This routine may reserve memory regions in the kernel only
660 * if the system is supported and a dump was taken in last
661 * boot instance or if the hardware is supported and the
662 * scratch area needs to be setup. In other instances it returns
663 * without reserving anything. The memory in case of dump being
664 * active is freed when the dump is collected (by userland tools).
666 static void __init phyp_dump_reserve_mem(void)
668 unsigned long base, size;
669 unsigned long variable_reserve_size;
671 if (!phyp_dump_info->phyp_dump_configured) {
672 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
676 if (!phyp_dump_info->phyp_dump_at_boot) {
677 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
681 variable_reserve_size = phyp_dump_calculate_reserve_size();
683 if (phyp_dump_info->phyp_dump_is_active) {
684 /* Reserve *everything* above RMR.Area freed by userland tools*/
685 base = variable_reserve_size;
686 size = lmb_end_of_DRAM() - base;
688 /* XXX crashed_ram_end is wrong, since it may be beyond
689 * the memory_limit, it will need to be adjusted. */
690 lmb_reserve(base, size);
692 phyp_dump_info->init_reserve_start = base;
693 phyp_dump_info->init_reserve_size = size;
695 size = phyp_dump_info->cpu_state_size +
696 phyp_dump_info->hpte_region_size +
697 variable_reserve_size;
698 base = lmb_end_of_DRAM() - size;
699 lmb_reserve(base, size);
700 phyp_dump_info->init_reserve_start = base;
701 phyp_dump_info->init_reserve_size = size;
705 static inline void __init phyp_dump_reserve_mem(void) {}
706 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
709 void __init early_init_devtree(void *params)
713 DBG(" -> early_init_devtree(%p)\n", params);
715 /* Setup flat device-tree pointer */
716 initial_boot_params = params;
718 #ifdef CONFIG_PPC_RTAS
719 /* Some machines might need RTAS info for debugging, grab it now. */
720 of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
723 #ifdef CONFIG_PHYP_DUMP
724 /* scan tree to see if dump occured during last boot */
725 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
728 /* Retrieve various informations from the /chosen node of the
729 * device-tree, including the platform type, initrd location and
730 * size, TCE reserve, and more ...
732 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
734 /* Scan memory nodes and rebuild LMBs */
736 of_scan_flat_dt(early_init_dt_scan_root, NULL);
737 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
739 /* Save command line for /proc/cmdline and then parse parameters */
740 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
743 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
744 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
745 /* If relocatable, reserve first 32k for interrupt vectors etc. */
746 if (PHYSICAL_START > MEMORY_START)
747 lmb_reserve(MEMORY_START, 0x8000);
748 reserve_kdump_trampoline();
749 reserve_crashkernel();
751 phyp_dump_reserve_mem();
753 limit = memory_limit;
757 /* Ensure that total memory size is page-aligned, because
758 * otherwise mark_bootmem() gets upset. */
760 memsize = lmb_phys_mem_size();
761 if ((memsize & PAGE_MASK) != memsize)
762 limit = memsize & PAGE_MASK;
764 lmb_enforce_memory_limit(limit);
769 DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
771 /* We may need to relocate the flat tree, do it now.
772 * FIXME .. and the initrd too? */
775 DBG("Scanning CPUs ...\n");
777 /* Retreive CPU related informations from the flat tree
778 * (altivec support, boot CPU ID, ...)
780 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
782 DBG(" <- early_init_devtree()\n");
787 * Indicates whether the root node has a given value in its
788 * compatible property.
790 int machine_is_compatible(const char *compat)
792 struct device_node *root;
795 root = of_find_node_by_path("/");
797 rc = of_device_is_compatible(root, compat);
802 EXPORT_SYMBOL(machine_is_compatible);
806 * New implementation of the OF "find" APIs, return a refcounted
807 * object, call of_node_put() when done. The device tree and list
808 * are protected by a rw_lock.
810 * Note that property management will need some locking as well,
811 * this isn't dealt with yet.
816 * of_find_node_by_phandle - Find a node given a phandle
817 * @handle: phandle of the node to find
819 * Returns a node pointer with refcount incremented, use
820 * of_node_put() on it when done.
822 struct device_node *of_find_node_by_phandle(phandle handle)
824 struct device_node *np;
826 read_lock(&devtree_lock);
827 for (np = allnodes; np != 0; np = np->allnext)
828 if (np->linux_phandle == handle)
831 read_unlock(&devtree_lock);
834 EXPORT_SYMBOL(of_find_node_by_phandle);
837 * of_find_next_cache_node - Find a node's subsidiary cache
838 * @np: node of type "cpu" or "cache"
840 * Returns a node pointer with refcount incremented, use
841 * of_node_put() on it when done. Caller should hold a reference
844 struct device_node *of_find_next_cache_node(struct device_node *np)
846 struct device_node *child;
847 const phandle *handle;
849 handle = of_get_property(np, "l2-cache", NULL);
851 handle = of_get_property(np, "next-level-cache", NULL);
854 return of_find_node_by_phandle(*handle);
856 /* OF on pmac has nodes instead of properties named "l2-cache"
859 if (!strcmp(np->type, "cpu"))
860 for_each_child_of_node(np, child)
861 if (!strcmp(child->type, "cache"))
868 * of_node_get - Increment refcount of a node
869 * @node: Node to inc refcount, NULL is supported to
870 * simplify writing of callers
874 struct device_node *of_node_get(struct device_node *node)
877 kref_get(&node->kref);
880 EXPORT_SYMBOL(of_node_get);
882 static inline struct device_node * kref_to_device_node(struct kref *kref)
884 return container_of(kref, struct device_node, kref);
888 * of_node_release - release a dynamically allocated node
889 * @kref: kref element of the node to be released
891 * In of_node_put() this function is passed to kref_put()
894 static void of_node_release(struct kref *kref)
896 struct device_node *node = kref_to_device_node(kref);
897 struct property *prop = node->properties;
899 /* We should never be releasing nodes that haven't been detached. */
900 if (!of_node_check_flag(node, OF_DETACHED)) {
901 printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
903 kref_init(&node->kref);
907 if (!of_node_check_flag(node, OF_DYNAMIC))
911 struct property *next = prop->next;
918 prop = node->deadprops;
919 node->deadprops = NULL;
922 kfree(node->full_name);
928 * of_node_put - Decrement refcount of a node
929 * @node: Node to dec refcount, NULL is supported to
930 * simplify writing of callers
933 void of_node_put(struct device_node *node)
936 kref_put(&node->kref, of_node_release);
938 EXPORT_SYMBOL(of_node_put);
941 * Plug a device node into the tree and global list.
943 void of_attach_node(struct device_node *np)
947 write_lock_irqsave(&devtree_lock, flags);
948 np->sibling = np->parent->child;
949 np->allnext = allnodes;
950 np->parent->child = np;
952 write_unlock_irqrestore(&devtree_lock, flags);
956 * "Unplug" a node from the device tree. The caller must hold
957 * a reference to the node. The memory associated with the node
958 * is not freed until its refcount goes to zero.
960 void of_detach_node(struct device_node *np)
962 struct device_node *parent;
965 write_lock_irqsave(&devtree_lock, flags);
972 allnodes = np->allnext;
974 struct device_node *prev;
975 for (prev = allnodes;
977 prev = prev->allnext)
979 prev->allnext = np->allnext;
982 if (parent->child == np)
983 parent->child = np->sibling;
985 struct device_node *prevsib;
986 for (prevsib = np->parent->child;
987 prevsib->sibling != np;
988 prevsib = prevsib->sibling)
990 prevsib->sibling = np->sibling;
993 of_node_set_flag(np, OF_DETACHED);
996 write_unlock_irqrestore(&devtree_lock, flags);
999 #ifdef CONFIG_PPC_PSERIES
1001 * Fix up the uninitialized fields in a new device node:
1002 * name, type and pci-specific fields
1005 static int of_finish_dynamic_node(struct device_node *node)
1007 struct device_node *parent = of_get_parent(node);
1009 const phandle *ibm_phandle;
1011 node->name = of_get_property(node, "name", NULL);
1012 node->type = of_get_property(node, "device_type", NULL);
1015 node->name = "<NULL>";
1017 node->type = "<NULL>";
1024 /* We don't support that function on PowerMac, at least
1027 if (machine_is(powermac))
1030 /* fix up new node's linux_phandle field */
1031 if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
1032 node->linux_phandle = *ibm_phandle;
1035 of_node_put(parent);
1039 static int prom_reconfig_notifier(struct notifier_block *nb,
1040 unsigned long action, void *node)
1045 case PSERIES_RECONFIG_ADD:
1046 err = of_finish_dynamic_node(node);
1048 printk(KERN_ERR "finish_node returned %d\n", err);
1059 static struct notifier_block prom_reconfig_nb = {
1060 .notifier_call = prom_reconfig_notifier,
1061 .priority = 10, /* This one needs to run first */
1064 static int __init prom_reconfig_setup(void)
1066 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1068 __initcall(prom_reconfig_setup);
1071 /* Find the device node for a given logical cpu number, also returns the cpu
1072 * local thread number (index in ibm,interrupt-server#s) if relevant and
1073 * asked for (non NULL)
1075 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
1078 struct device_node *np;
1080 hardid = get_hard_smp_processor_id(cpu);
1082 for_each_node_by_type(np, "cpu") {
1084 unsigned int plen, t;
1086 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1087 * fallback to "reg" property and assume no threads
1089 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
1091 if (intserv == NULL) {
1092 const u32 *reg = of_get_property(np, "reg", NULL);
1095 if (*reg == hardid) {
1101 plen /= sizeof(u32);
1102 for (t = 0; t < plen; t++) {
1103 if (hardid == intserv[t]) {
1113 EXPORT_SYMBOL(of_get_cpu_node);
1115 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1116 static struct debugfs_blob_wrapper flat_dt_blob;
1118 static int __init export_flat_device_tree(void)
1122 flat_dt_blob.data = initial_boot_params;
1123 flat_dt_blob.size = initial_boot_params->totalsize;
1125 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1126 powerpc_debugfs_root, &flat_dt_blob);
1132 __initcall(export_flat_device_tree);