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>
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/kexec.h>
55 #include <asm/system.h>
58 #define DBG(fmt...) printk(KERN_ERR fmt)
64 static int __initdata dt_root_addr_cells;
65 static int __initdata dt_root_size_cells;
68 int __initdata iommu_is_off;
69 int __initdata iommu_force_on;
70 unsigned long tce_alloc_start, tce_alloc_end;
76 static struct boot_param_header *initial_boot_params __initdata;
78 struct boot_param_header *initial_boot_params;
81 static struct device_node *allnodes = NULL;
83 /* use when traversing tree through the allnext, child, sibling,
84 * or parent members of struct device_node.
86 static DEFINE_RWLOCK(devtree_lock);
88 /* export that to outside world */
89 struct device_node *of_chosen;
91 static inline char *find_flat_dt_string(u32 offset)
93 return ((char *)initial_boot_params) +
94 initial_boot_params->off_dt_strings + offset;
98 * This function is used to scan the flattened device-tree, it is
99 * used to extract the memory informations at boot before we can
102 int __init of_scan_flat_dt(int (*it)(unsigned long node,
103 const char *uname, int depth,
107 unsigned long p = ((unsigned long)initial_boot_params) +
108 initial_boot_params->off_dt_struct;
113 u32 tag = *((u32 *)p);
117 if (tag == OF_DT_END_NODE) {
121 if (tag == OF_DT_NOP)
123 if (tag == OF_DT_END)
125 if (tag == OF_DT_PROP) {
126 u32 sz = *((u32 *)p);
128 if (initial_boot_params->version < 0x10)
129 p = _ALIGN(p, sz >= 8 ? 8 : 4);
134 if (tag != OF_DT_BEGIN_NODE) {
135 printk(KERN_WARNING "Invalid tag %x scanning flattened"
136 " device tree !\n", tag);
141 p = _ALIGN(p + strlen(pathp) + 1, 4);
142 if ((*pathp) == '/') {
144 for (lp = NULL, np = pathp; *np; np++)
150 rc = it(p, pathp, depth, data);
158 unsigned long __init of_get_flat_dt_root(void)
160 unsigned long p = ((unsigned long)initial_boot_params) +
161 initial_boot_params->off_dt_struct;
163 while(*((u32 *)p) == OF_DT_NOP)
165 BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
167 return _ALIGN(p + strlen((char *)p) + 1, 4);
171 * This function can be used within scan_flattened_dt callback to get
172 * access to properties
174 void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
177 unsigned long p = node;
180 u32 tag = *((u32 *)p);
185 if (tag == OF_DT_NOP)
187 if (tag != OF_DT_PROP)
191 noff = *((u32 *)(p + 4));
193 if (initial_boot_params->version < 0x10)
194 p = _ALIGN(p, sz >= 8 ? 8 : 4);
196 nstr = find_flat_dt_string(noff);
198 printk(KERN_WARNING "Can't find property index"
202 if (strcmp(name, nstr) == 0) {
212 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
215 unsigned long cplen, l;
217 cp = of_get_flat_dt_prop(node, "compatible", &cplen);
221 if (strncasecmp(cp, compat, strlen(compat)) == 0)
231 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
236 *mem = _ALIGN(*mem, align);
243 static unsigned long __init unflatten_dt_node(unsigned long mem,
245 struct device_node *dad,
246 struct device_node ***allnextpp,
247 unsigned long fpsize)
249 struct device_node *np;
250 struct property *pp, **prev_pp = NULL;
253 unsigned int l, allocl;
257 tag = *((u32 *)(*p));
258 if (tag != OF_DT_BEGIN_NODE) {
259 printk("Weird tag at start of node: %x\n", tag);
264 l = allocl = strlen(pathp) + 1;
265 *p = _ALIGN(*p + l, 4);
267 /* version 0x10 has a more compact unit name here instead of the full
268 * path. we accumulate the full path size using "fpsize", we'll rebuild
269 * it later. We detect this because the first character of the name is
272 if ((*pathp) != '/') {
275 /* root node: special case. fpsize accounts for path
276 * plus terminating zero. root node only has '/', so
277 * fpsize should be 2, but we want to avoid the first
278 * level nodes to have two '/' so we use fpsize 1 here
283 /* account for '/' and path size minus terminal 0
292 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
293 __alignof__(struct device_node));
295 memset(np, 0, sizeof(*np));
296 np->full_name = ((char*)np) + sizeof(struct device_node);
298 char *p = np->full_name;
299 /* rebuild full path for new format */
300 if (dad && dad->parent) {
301 strcpy(p, dad->full_name);
303 if ((strlen(p) + l + 1) != allocl) {
304 DBG("%s: p: %d, l: %d, a: %d\n",
305 pathp, (int)strlen(p), l, allocl);
313 memcpy(np->full_name, pathp, l);
314 prev_pp = &np->properties;
316 *allnextpp = &np->allnext;
319 /* we temporarily use the next field as `last_child'*/
323 dad->next->sibling = np;
326 kref_init(&np->kref);
332 tag = *((u32 *)(*p));
333 if (tag == OF_DT_NOP) {
337 if (tag != OF_DT_PROP)
341 noff = *((u32 *)((*p) + 4));
343 if (initial_boot_params->version < 0x10)
344 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
346 pname = find_flat_dt_string(noff);
348 printk("Can't find property name in list !\n");
351 if (strcmp(pname, "name") == 0)
353 l = strlen(pname) + 1;
354 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
355 __alignof__(struct property));
357 if (strcmp(pname, "linux,phandle") == 0) {
358 np->node = *((u32 *)*p);
359 if (np->linux_phandle == 0)
360 np->linux_phandle = np->node;
362 if (strcmp(pname, "ibm,phandle") == 0)
363 np->linux_phandle = *((u32 *)*p);
366 pp->value = (void *)*p;
370 *p = _ALIGN((*p) + sz, 4);
372 /* with version 0x10 we may not have the name property, recreate
373 * it here from the unit name if absent
376 char *p = pathp, *ps = pathp, *pa = NULL;
389 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
390 __alignof__(struct property));
397 memcpy(pp->value, ps, sz - 1);
398 ((char *)pp->value)[sz - 1] = 0;
399 DBG("fixed up name for %s -> %s\n", pathp,
405 np->name = of_get_property(np, "name", NULL);
406 np->type = of_get_property(np, "device_type", NULL);
413 while (tag == OF_DT_BEGIN_NODE) {
414 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
415 tag = *((u32 *)(*p));
417 if (tag != OF_DT_END_NODE) {
418 printk("Weird tag at end of node: %x\n", tag);
425 static int __init early_parse_mem(char *p)
430 memory_limit = PAGE_ALIGN(memparse(p, &p));
431 DBG("memory limit = 0x%lx\n", memory_limit);
435 early_param("mem", early_parse_mem);
438 * The device tree may be allocated below our memory limit, or inside the
439 * crash kernel region for kdump. If so, move it out now.
441 static void 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},
537 /* put this back once we know how to test if firmware does 64k IO */
538 {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
540 {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
543 static void __init scan_features(unsigned long node, unsigned char *ftrs,
544 unsigned long tablelen,
545 struct ibm_pa_feature *fp,
546 unsigned long ft_size)
548 unsigned long i, len, bit;
550 /* find descriptor with type == 0 */
556 return; /* descriptor 0 not found */
563 /* loop over bits we know about */
564 for (i = 0; i < ft_size; ++i, ++fp) {
565 if (fp->pabyte >= ftrs[0])
567 bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
568 if (bit ^ fp->invert) {
569 cur_cpu_spec->cpu_features |= fp->cpu_features;
570 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
572 cur_cpu_spec->cpu_features &= ~fp->cpu_features;
573 cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
578 static void __init check_cpu_pa_features(unsigned long node)
580 unsigned char *pa_ftrs;
581 unsigned long tablelen;
583 pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
587 scan_features(node, pa_ftrs, tablelen,
588 ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
591 static struct feature_property {
594 unsigned long cpu_feature;
595 unsigned long cpu_user_ftr;
596 } feature_properties[] __initdata = {
597 #ifdef CONFIG_ALTIVEC
598 {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
599 {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
600 #endif /* CONFIG_ALTIVEC */
602 {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
603 {"ibm,purr", 1, CPU_FTR_PURR, 0},
604 {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
605 #endif /* CONFIG_PPC64 */
608 static void __init check_cpu_feature_properties(unsigned long node)
611 struct feature_property *fp = feature_properties;
614 for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
615 prop = of_get_flat_dt_prop(node, fp->name, NULL);
616 if (prop && *prop >= fp->min_value) {
617 cur_cpu_spec->cpu_features |= fp->cpu_feature;
618 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
623 static int __init early_init_dt_scan_cpus(unsigned long node,
624 const char *uname, int depth,
627 static int logical_cpuid = 0;
628 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
635 /* We are scanning "cpu" nodes only */
636 if (type == NULL || strcmp(type, "cpu") != 0)
639 /* Get physical cpuid */
640 intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
642 nthreads = len / sizeof(int);
644 intserv = of_get_flat_dt_prop(node, "reg", NULL);
649 * Now see if any of these threads match our boot cpu.
650 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
652 for (i = 0; i < nthreads; i++) {
654 * version 2 of the kexec param format adds the phys cpuid of
657 if (initial_boot_params && initial_boot_params->version >= 2) {
659 initial_boot_params->boot_cpuid_phys) {
665 * Check if it's the boot-cpu, set it's hw index now,
666 * unfortunately this format did not support booting
667 * off secondary threads.
669 if (of_get_flat_dt_prop(node,
670 "linux,boot-cpu", NULL) != NULL) {
677 /* logical cpu id is always 0 on UP kernels */
683 DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
685 boot_cpuid = logical_cpuid;
686 set_hard_smp_processor_id(boot_cpuid, intserv[i]);
689 * PAPR defines "logical" PVR values for cpus that
690 * meet various levels of the architecture:
691 * 0x0f000001 Architecture version 2.04
692 * 0x0f000002 Architecture version 2.05
693 * If the cpu-version property in the cpu node contains
694 * such a value, we call identify_cpu again with the
695 * logical PVR value in order to use the cpu feature
696 * bits appropriate for the architecture level.
698 * A POWER6 partition in "POWER6 architected" mode
699 * uses the 0x0f000002 PVR value; in POWER5+ mode
700 * it uses 0x0f000001.
702 prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
703 if (prop && (*prop & 0xff000000) == 0x0f000000)
704 identify_cpu(0, *prop);
707 check_cpu_feature_properties(node);
708 check_cpu_pa_features(node);
710 #ifdef CONFIG_PPC_PSERIES
712 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
714 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
720 #ifdef CONFIG_BLK_DEV_INITRD
721 static void __init early_init_dt_check_for_initrd(unsigned long node)
726 DBG("Looking for initrd properties... ");
728 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
730 initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
732 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
734 initrd_end = (unsigned long)
735 __va(of_read_ulong(prop, l/4));
736 initrd_below_start_ok = 1;
742 DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end);
745 static inline void early_init_dt_check_for_initrd(unsigned long node)
748 #endif /* CONFIG_BLK_DEV_INITRD */
750 static int __init early_init_dt_scan_chosen(unsigned long node,
751 const char *uname, int depth, void *data)
753 unsigned long *lprop;
757 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
760 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
764 /* check if iommu is forced on or off */
765 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
767 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
771 /* mem=x on the command line is the preferred mechanism */
772 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
774 memory_limit = *lprop;
777 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
779 tce_alloc_start = *lprop;
780 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
782 tce_alloc_end = *lprop;
786 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
788 crashk_res.start = *lprop;
790 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
792 crashk_res.end = crashk_res.start + *lprop - 1;
795 early_init_dt_check_for_initrd(node);
797 /* Retreive command line */
798 p = of_get_flat_dt_prop(node, "bootargs", &l);
799 if (p != NULL && l > 0)
800 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
802 #ifdef CONFIG_CMDLINE
803 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
804 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
805 #endif /* CONFIG_CMDLINE */
807 DBG("Command line is: %s\n", cmd_line);
813 static int __init early_init_dt_scan_root(unsigned long node,
814 const char *uname, int depth, void *data)
821 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
822 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
823 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
825 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
826 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
827 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
833 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
838 return of_read_ulong(p, s);
841 #ifdef CONFIG_PPC_PSERIES
843 * Interpret the ibm,dynamic-memory property in the
844 * /ibm,dynamic-reconfiguration-memory node.
845 * This contains a list of memory blocks along with NUMA affinity
848 static int __init early_init_dt_scan_drconf_memory(unsigned long node)
852 unsigned long base, size, lmb_size, flags;
854 ls = (cell_t *)of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
855 if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
857 lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
859 dm = (cell_t *)of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
860 if (dm == NULL || l < sizeof(cell_t))
863 n = *dm++; /* number of entries */
864 if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
867 for (; n != 0; --n) {
868 base = dt_mem_next_cell(dt_root_addr_cells, &dm);
870 /* skip DRC index, pad, assoc. list index, flags */
872 /* skip this block if the reserved bit is set in flags (0x80)
873 or if the block is not assigned to this partition (0x8) */
874 if ((flags & 0x80) || !(flags & 0x8))
878 if (base >= 0x80000000ul)
880 if ((base + size) > 0x80000000ul)
881 size = 0x80000000ul - base;
889 #define early_init_dt_scan_drconf_memory(node) 0
890 #endif /* CONFIG_PPC_PSERIES */
892 static int __init early_init_dt_scan_memory(unsigned long node,
893 const char *uname, int depth, void *data)
895 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
899 /* Look for the ibm,dynamic-reconfiguration-memory node */
901 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
902 return early_init_dt_scan_drconf_memory(node);
904 /* We are scanning "memory" nodes only */
907 * The longtrail doesn't have a device_type on the
908 * /memory node, so look for the node called /memory@0.
910 if (depth != 1 || strcmp(uname, "memory@0") != 0)
912 } else if (strcmp(type, "memory") != 0)
915 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
917 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
921 endp = reg + (l / sizeof(cell_t));
923 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
924 uname, l, reg[0], reg[1], reg[2], reg[3]);
926 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
927 unsigned long base, size;
929 base = dt_mem_next_cell(dt_root_addr_cells, ®);
930 size = dt_mem_next_cell(dt_root_size_cells, ®);
934 DBG(" - %lx , %lx\n", base, size);
937 if (base >= 0x80000000ul)
939 if ((base + size) > 0x80000000ul)
940 size = 0x80000000ul - base;
948 static void __init early_reserve_mem(void)
952 unsigned long self_base;
953 unsigned long self_size;
955 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
956 initial_boot_params->off_mem_rsvmap);
958 /* before we do anything, lets reserve the dt blob */
959 self_base = __pa((unsigned long)initial_boot_params);
960 self_size = initial_boot_params->totalsize;
961 lmb_reserve(self_base, self_size);
963 #ifdef CONFIG_BLK_DEV_INITRD
964 /* then reserve the initrd, if any */
965 if (initrd_start && (initrd_end > initrd_start))
966 lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
967 #endif /* CONFIG_BLK_DEV_INITRD */
971 * Handle the case where we might be booting from an old kexec
972 * image that setup the mem_rsvmap as pairs of 32-bit values
974 if (*reserve_map > 0xffffffffull) {
975 u32 base_32, size_32;
976 u32 *reserve_map_32 = (u32 *)reserve_map;
979 base_32 = *(reserve_map_32++);
980 size_32 = *(reserve_map_32++);
983 /* skip if the reservation is for the blob */
984 if (base_32 == self_base && size_32 == self_size)
986 DBG("reserving: %x -> %x\n", base_32, size_32);
987 lmb_reserve(base_32, size_32);
993 base = *(reserve_map++);
994 size = *(reserve_map++);
997 DBG("reserving: %llx -> %llx\n", base, size);
998 lmb_reserve(base, size);
1002 DBG("memory reserved, lmbs :\n");
1007 void __init early_init_devtree(void *params)
1009 DBG(" -> early_init_devtree(%p)\n", params);
1011 /* Setup flat device-tree pointer */
1012 initial_boot_params = params;
1014 #ifdef CONFIG_PPC_RTAS
1015 /* Some machines might need RTAS info for debugging, grab it now. */
1016 of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
1019 /* Retrieve various informations from the /chosen node of the
1020 * device-tree, including the platform type, initrd location and
1021 * size, TCE reserve, and more ...
1023 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
1025 /* Scan memory nodes and rebuild LMBs */
1027 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1028 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1030 /* Save command line for /proc/cmdline and then parse parameters */
1031 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
1032 parse_early_param();
1034 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1035 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
1036 reserve_kdump_trampoline();
1037 reserve_crashkernel();
1038 early_reserve_mem();
1040 lmb_enforce_memory_limit(memory_limit);
1043 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1045 /* We may need to relocate the flat tree, do it now.
1046 * FIXME .. and the initrd too? */
1049 DBG("Scanning CPUs ...\n");
1051 /* Retreive CPU related informations from the flat tree
1052 * (altivec support, boot CPU ID, ...)
1054 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
1056 DBG(" <- early_init_devtree()\n");
1059 int of_n_addr_cells(struct device_node* np)
1065 ip = of_get_property(np, "#address-cells", NULL);
1068 } while (np->parent);
1069 /* No #address-cells property for the root node, default to 1 */
1072 EXPORT_SYMBOL(of_n_addr_cells);
1074 int of_n_size_cells(struct device_node* np)
1080 ip = of_get_property(np, "#size-cells", NULL);
1083 } while (np->parent);
1084 /* No #size-cells property for the root node, default to 1 */
1087 EXPORT_SYMBOL(of_n_size_cells);
1089 /** Checks if the given "compat" string matches one of the strings in
1090 * the device's "compatible" property
1092 int of_device_is_compatible(const struct device_node *device,
1098 cp = of_get_property(device, "compatible", &cplen);
1102 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1111 EXPORT_SYMBOL(of_device_is_compatible);
1115 * Indicates whether the root node has a given value in its
1116 * compatible property.
1118 int machine_is_compatible(const char *compat)
1120 struct device_node *root;
1123 root = of_find_node_by_path("/");
1125 rc = of_device_is_compatible(root, compat);
1130 EXPORT_SYMBOL(machine_is_compatible);
1134 * New implementation of the OF "find" APIs, return a refcounted
1135 * object, call of_node_put() when done. The device tree and list
1136 * are protected by a rw_lock.
1138 * Note that property management will need some locking as well,
1139 * this isn't dealt with yet.
1144 * of_find_node_by_name - Find a node by its "name" property
1145 * @from: The node to start searching from or NULL, the node
1146 * you pass will not be searched, only the next one
1147 * will; typically, you pass what the previous call
1148 * returned. of_node_put() will be called on it
1149 * @name: The name string to match against
1151 * Returns a node pointer with refcount incremented, use
1152 * of_node_put() on it when done.
1154 struct device_node *of_find_node_by_name(struct device_node *from,
1157 struct device_node *np;
1159 read_lock(&devtree_lock);
1160 np = from ? from->allnext : allnodes;
1161 for (; np != NULL; np = np->allnext)
1162 if (np->name != NULL && strcasecmp(np->name, name) == 0
1166 read_unlock(&devtree_lock);
1169 EXPORT_SYMBOL(of_find_node_by_name);
1172 * of_find_node_by_type - Find a node by its "device_type" property
1173 * @from: The node to start searching from, or NULL to start searching
1174 * the entire device tree. The node you pass will not be
1175 * searched, only the next one will; typically, you pass
1176 * what the previous call returned. of_node_put() will be
1177 * called on from for you.
1178 * @type: The type string to match against
1180 * Returns a node pointer with refcount incremented, use
1181 * of_node_put() on it when done.
1183 struct device_node *of_find_node_by_type(struct device_node *from,
1186 struct device_node *np;
1188 read_lock(&devtree_lock);
1189 np = from ? from->allnext : allnodes;
1190 for (; np != 0; np = np->allnext)
1191 if (np->type != 0 && strcasecmp(np->type, type) == 0
1195 read_unlock(&devtree_lock);
1198 EXPORT_SYMBOL(of_find_node_by_type);
1201 * of_find_compatible_node - Find a node based on type and one of the
1202 * tokens in its "compatible" property
1203 * @from: The node to start searching from or NULL, the node
1204 * you pass will not be searched, only the next one
1205 * will; typically, you pass what the previous call
1206 * returned. of_node_put() will be called on it
1207 * @type: The type string to match "device_type" or NULL to ignore
1208 * @compatible: The string to match to one of the tokens in the device
1209 * "compatible" list.
1211 * Returns a node pointer with refcount incremented, use
1212 * of_node_put() on it when done.
1214 struct device_node *of_find_compatible_node(struct device_node *from,
1215 const char *type, const char *compatible)
1217 struct device_node *np;
1219 read_lock(&devtree_lock);
1220 np = from ? from->allnext : allnodes;
1221 for (; np != 0; np = np->allnext) {
1223 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1225 if (of_device_is_compatible(np, compatible) && of_node_get(np))
1229 read_unlock(&devtree_lock);
1232 EXPORT_SYMBOL(of_find_compatible_node);
1235 * of_find_node_by_path - Find a node matching a full OF path
1236 * @path: The full path to match
1238 * Returns a node pointer with refcount incremented, use
1239 * of_node_put() on it when done.
1241 struct device_node *of_find_node_by_path(const char *path)
1243 struct device_node *np = allnodes;
1245 read_lock(&devtree_lock);
1246 for (; np != 0; np = np->allnext) {
1247 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1251 read_unlock(&devtree_lock);
1254 EXPORT_SYMBOL(of_find_node_by_path);
1257 * of_find_node_by_phandle - Find a node given a phandle
1258 * @handle: phandle of the node to find
1260 * Returns a node pointer with refcount incremented, use
1261 * of_node_put() on it when done.
1263 struct device_node *of_find_node_by_phandle(phandle handle)
1265 struct device_node *np;
1267 read_lock(&devtree_lock);
1268 for (np = allnodes; np != 0; np = np->allnext)
1269 if (np->linux_phandle == handle)
1272 read_unlock(&devtree_lock);
1275 EXPORT_SYMBOL(of_find_node_by_phandle);
1278 * of_find_all_nodes - Get next node in global list
1279 * @prev: Previous node or NULL to start iteration
1280 * of_node_put() will be called on it
1282 * Returns a node pointer with refcount incremented, use
1283 * of_node_put() on it when done.
1285 struct device_node *of_find_all_nodes(struct device_node *prev)
1287 struct device_node *np;
1289 read_lock(&devtree_lock);
1290 np = prev ? prev->allnext : allnodes;
1291 for (; np != 0; np = np->allnext)
1292 if (of_node_get(np))
1295 read_unlock(&devtree_lock);
1298 EXPORT_SYMBOL(of_find_all_nodes);
1301 * of_get_parent - Get a node's parent if any
1302 * @node: Node to get parent
1304 * Returns a node pointer with refcount incremented, use
1305 * of_node_put() on it when done.
1307 struct device_node *of_get_parent(const struct device_node *node)
1309 struct device_node *np;
1314 read_lock(&devtree_lock);
1315 np = of_node_get(node->parent);
1316 read_unlock(&devtree_lock);
1319 EXPORT_SYMBOL(of_get_parent);
1322 * of_get_next_child - Iterate a node childs
1323 * @node: parent node
1324 * @prev: previous child of the parent node, or NULL to get first
1326 * Returns a node pointer with refcount incremented, use
1327 * of_node_put() on it when done.
1329 struct device_node *of_get_next_child(const struct device_node *node,
1330 struct device_node *prev)
1332 struct device_node *next;
1334 read_lock(&devtree_lock);
1335 next = prev ? prev->sibling : node->child;
1336 for (; next != 0; next = next->sibling)
1337 if (of_node_get(next))
1340 read_unlock(&devtree_lock);
1343 EXPORT_SYMBOL(of_get_next_child);
1346 * of_node_get - Increment refcount of a node
1347 * @node: Node to inc refcount, NULL is supported to
1348 * simplify writing of callers
1352 struct device_node *of_node_get(struct device_node *node)
1355 kref_get(&node->kref);
1358 EXPORT_SYMBOL(of_node_get);
1360 static inline struct device_node * kref_to_device_node(struct kref *kref)
1362 return container_of(kref, struct device_node, kref);
1366 * of_node_release - release a dynamically allocated node
1367 * @kref: kref element of the node to be released
1369 * In of_node_put() this function is passed to kref_put()
1370 * as the destructor.
1372 static void of_node_release(struct kref *kref)
1374 struct device_node *node = kref_to_device_node(kref);
1375 struct property *prop = node->properties;
1377 /* We should never be releasing nodes that haven't been detached. */
1378 if (!of_node_check_flag(node, OF_DETACHED)) {
1379 printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
1381 kref_init(&node->kref);
1385 if (!of_node_check_flag(node, OF_DYNAMIC))
1389 struct property *next = prop->next;
1396 prop = node->deadprops;
1397 node->deadprops = NULL;
1400 kfree(node->full_name);
1406 * of_node_put - Decrement refcount of a node
1407 * @node: Node to dec refcount, NULL is supported to
1408 * simplify writing of callers
1411 void of_node_put(struct device_node *node)
1414 kref_put(&node->kref, of_node_release);
1416 EXPORT_SYMBOL(of_node_put);
1419 * Plug a device node into the tree and global list.
1421 void of_attach_node(struct device_node *np)
1423 write_lock(&devtree_lock);
1424 np->sibling = np->parent->child;
1425 np->allnext = allnodes;
1426 np->parent->child = np;
1428 write_unlock(&devtree_lock);
1432 * "Unplug" a node from the device tree. The caller must hold
1433 * a reference to the node. The memory associated with the node
1434 * is not freed until its refcount goes to zero.
1436 void of_detach_node(const struct device_node *np)
1438 struct device_node *parent;
1440 write_lock(&devtree_lock);
1442 parent = np->parent;
1447 allnodes = np->allnext;
1449 struct device_node *prev;
1450 for (prev = allnodes;
1451 prev->allnext != np;
1452 prev = prev->allnext)
1454 prev->allnext = np->allnext;
1457 if (parent->child == np)
1458 parent->child = np->sibling;
1460 struct device_node *prevsib;
1461 for (prevsib = np->parent->child;
1462 prevsib->sibling != np;
1463 prevsib = prevsib->sibling)
1465 prevsib->sibling = np->sibling;
1468 of_node_set_flag(np, OF_DETACHED);
1471 write_unlock(&devtree_lock);
1474 #ifdef CONFIG_PPC_PSERIES
1476 * Fix up the uninitialized fields in a new device node:
1477 * name, type and pci-specific fields
1480 static int of_finish_dynamic_node(struct device_node *node)
1482 struct device_node *parent = of_get_parent(node);
1484 const phandle *ibm_phandle;
1486 node->name = of_get_property(node, "name", NULL);
1487 node->type = of_get_property(node, "device_type", NULL);
1490 node->name = "<NULL>";
1492 node->type = "<NULL>";
1499 /* We don't support that function on PowerMac, at least
1502 if (machine_is(powermac))
1505 /* fix up new node's linux_phandle field */
1506 if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
1507 node->linux_phandle = *ibm_phandle;
1510 of_node_put(parent);
1514 static int prom_reconfig_notifier(struct notifier_block *nb,
1515 unsigned long action, void *node)
1520 case PSERIES_RECONFIG_ADD:
1521 err = of_finish_dynamic_node(node);
1523 printk(KERN_ERR "finish_node returned %d\n", err);
1534 static struct notifier_block prom_reconfig_nb = {
1535 .notifier_call = prom_reconfig_notifier,
1536 .priority = 10, /* This one needs to run first */
1539 static int __init prom_reconfig_setup(void)
1541 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1543 __initcall(prom_reconfig_setup);
1546 struct property *of_find_property(const struct device_node *np,
1550 struct property *pp;
1552 read_lock(&devtree_lock);
1553 for (pp = np->properties; pp != 0; pp = pp->next)
1554 if (strcmp(pp->name, name) == 0) {
1559 read_unlock(&devtree_lock);
1563 EXPORT_SYMBOL(of_find_property);
1566 * Find a property with a given name for a given node
1567 * and return the value.
1569 const void *of_get_property(const struct device_node *np, const char *name,
1572 struct property *pp = of_find_property(np,name,lenp);
1573 return pp ? pp->value : NULL;
1575 EXPORT_SYMBOL(of_get_property);
1578 * Add a property to a node
1580 int prom_add_property(struct device_node* np, struct property* prop)
1582 struct property **next;
1585 write_lock(&devtree_lock);
1586 next = &np->properties;
1588 if (strcmp(prop->name, (*next)->name) == 0) {
1589 /* duplicate ! don't insert it */
1590 write_unlock(&devtree_lock);
1593 next = &(*next)->next;
1596 write_unlock(&devtree_lock);
1598 #ifdef CONFIG_PROC_DEVICETREE
1599 /* try to add to proc as well if it was initialized */
1601 proc_device_tree_add_prop(np->pde, prop);
1602 #endif /* CONFIG_PROC_DEVICETREE */
1608 * Remove a property from a node. Note that we don't actually
1609 * remove it, since we have given out who-knows-how-many pointers
1610 * to the data using get-property. Instead we just move the property
1611 * to the "dead properties" list, so it won't be found any more.
1613 int prom_remove_property(struct device_node *np, struct property *prop)
1615 struct property **next;
1618 write_lock(&devtree_lock);
1619 next = &np->properties;
1621 if (*next == prop) {
1622 /* found the node */
1624 prop->next = np->deadprops;
1625 np->deadprops = prop;
1629 next = &(*next)->next;
1631 write_unlock(&devtree_lock);
1636 #ifdef CONFIG_PROC_DEVICETREE
1637 /* try to remove the proc node as well */
1639 proc_device_tree_remove_prop(np->pde, prop);
1640 #endif /* CONFIG_PROC_DEVICETREE */
1646 * Update a property in a node. Note that we don't actually
1647 * remove it, since we have given out who-knows-how-many pointers
1648 * to the data using get-property. Instead we just move the property
1649 * to the "dead properties" list, and add the new property to the
1652 int prom_update_property(struct device_node *np,
1653 struct property *newprop,
1654 struct property *oldprop)
1656 struct property **next;
1659 write_lock(&devtree_lock);
1660 next = &np->properties;
1662 if (*next == oldprop) {
1663 /* found the node */
1664 newprop->next = oldprop->next;
1666 oldprop->next = np->deadprops;
1667 np->deadprops = oldprop;
1671 next = &(*next)->next;
1673 write_unlock(&devtree_lock);
1678 #ifdef CONFIG_PROC_DEVICETREE
1679 /* try to add to proc as well if it was initialized */
1681 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1682 #endif /* CONFIG_PROC_DEVICETREE */
1688 /* Find the device node for a given logical cpu number, also returns the cpu
1689 * local thread number (index in ibm,interrupt-server#s) if relevant and
1690 * asked for (non NULL)
1692 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
1695 struct device_node *np;
1697 hardid = get_hard_smp_processor_id(cpu);
1699 for_each_node_by_type(np, "cpu") {
1701 unsigned int plen, t;
1703 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1704 * fallback to "reg" property and assume no threads
1706 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
1708 if (intserv == NULL) {
1709 const u32 *reg = of_get_property(np, "reg", NULL);
1712 if (*reg == hardid) {
1718 plen /= sizeof(u32);
1719 for (t = 0; t < plen; t++) {
1720 if (hardid == intserv[t]) {
1730 EXPORT_SYMBOL(of_get_cpu_node);
1732 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1733 static struct debugfs_blob_wrapper flat_dt_blob;
1735 static int __init export_flat_device_tree(void)
1739 flat_dt_blob.data = initial_boot_params;
1740 flat_dt_blob.size = initial_boot_params->totalsize;
1742 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1743 powerpc_debugfs_root, &flat_dt_blob);
1749 __initcall(export_flat_device_tree);