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 struct device_node *allnodes; /* temporary while merging */
78 extern rwlock_t devtree_lock; /* temporary while merging */
80 /* export that to outside world */
81 struct device_node *of_chosen;
84 * This function is used to scan the flattened device-tree, it is
85 * used to extract the memory informations at boot before we can
88 int __init of_scan_flat_dt(int (*it)(unsigned long node,
89 const char *uname, int depth,
93 unsigned long p = ((unsigned long)initial_boot_params) +
94 initial_boot_params->off_dt_struct;
99 u32 tag = *((u32 *)p);
103 if (tag == OF_DT_END_NODE) {
107 if (tag == OF_DT_NOP)
109 if (tag == OF_DT_END)
111 if (tag == OF_DT_PROP) {
112 u32 sz = *((u32 *)p);
114 if (initial_boot_params->version < 0x10)
115 p = _ALIGN(p, sz >= 8 ? 8 : 4);
120 if (tag != OF_DT_BEGIN_NODE) {
121 printk(KERN_WARNING "Invalid tag %x scanning flattened"
122 " device tree !\n", tag);
127 p = _ALIGN(p + strlen(pathp) + 1, 4);
128 if ((*pathp) == '/') {
130 for (lp = NULL, np = pathp; *np; np++)
136 rc = it(p, pathp, depth, data);
144 unsigned long __init of_get_flat_dt_root(void)
146 unsigned long p = ((unsigned long)initial_boot_params) +
147 initial_boot_params->off_dt_struct;
149 while(*((u32 *)p) == OF_DT_NOP)
151 BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
153 return _ALIGN(p + strlen((char *)p) + 1, 4);
157 * This function can be used within scan_flattened_dt callback to get
158 * access to properties
160 void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
163 unsigned long p = node;
166 u32 tag = *((u32 *)p);
171 if (tag == OF_DT_NOP)
173 if (tag != OF_DT_PROP)
177 noff = *((u32 *)(p + 4));
179 if (initial_boot_params->version < 0x10)
180 p = _ALIGN(p, sz >= 8 ? 8 : 4);
182 nstr = find_flat_dt_string(noff);
184 printk(KERN_WARNING "Can't find property index"
188 if (strcmp(name, nstr) == 0) {
198 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
201 unsigned long cplen, l;
203 cp = of_get_flat_dt_prop(node, "compatible", &cplen);
207 if (strncasecmp(cp, compat, strlen(compat)) == 0)
217 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
222 *mem = _ALIGN(*mem, align);
229 static unsigned long __init unflatten_dt_node(unsigned long mem,
231 struct device_node *dad,
232 struct device_node ***allnextpp,
233 unsigned long fpsize)
235 struct device_node *np;
236 struct property *pp, **prev_pp = NULL;
239 unsigned int l, allocl;
243 tag = *((u32 *)(*p));
244 if (tag != OF_DT_BEGIN_NODE) {
245 printk("Weird tag at start of node: %x\n", tag);
250 l = allocl = strlen(pathp) + 1;
251 *p = _ALIGN(*p + l, 4);
253 /* version 0x10 has a more compact unit name here instead of the full
254 * path. we accumulate the full path size using "fpsize", we'll rebuild
255 * it later. We detect this because the first character of the name is
258 if ((*pathp) != '/') {
261 /* root node: special case. fpsize accounts for path
262 * plus terminating zero. root node only has '/', so
263 * fpsize should be 2, but we want to avoid the first
264 * level nodes to have two '/' so we use fpsize 1 here
269 /* account for '/' and path size minus terminal 0
278 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
279 __alignof__(struct device_node));
281 memset(np, 0, sizeof(*np));
282 np->full_name = ((char*)np) + sizeof(struct device_node);
284 char *p = np->full_name;
285 /* rebuild full path for new format */
286 if (dad && dad->parent) {
287 strcpy(p, dad->full_name);
289 if ((strlen(p) + l + 1) != allocl) {
290 DBG("%s: p: %d, l: %d, a: %d\n",
291 pathp, (int)strlen(p), l, allocl);
299 memcpy(np->full_name, pathp, l);
300 prev_pp = &np->properties;
302 *allnextpp = &np->allnext;
305 /* we temporarily use the next field as `last_child'*/
309 dad->next->sibling = np;
312 kref_init(&np->kref);
318 tag = *((u32 *)(*p));
319 if (tag == OF_DT_NOP) {
323 if (tag != OF_DT_PROP)
327 noff = *((u32 *)((*p) + 4));
329 if (initial_boot_params->version < 0x10)
330 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
332 pname = find_flat_dt_string(noff);
334 printk("Can't find property name in list !\n");
337 if (strcmp(pname, "name") == 0)
339 l = strlen(pname) + 1;
340 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
341 __alignof__(struct property));
343 if (strcmp(pname, "linux,phandle") == 0) {
344 np->node = *((u32 *)*p);
345 if (np->linux_phandle == 0)
346 np->linux_phandle = np->node;
348 if (strcmp(pname, "ibm,phandle") == 0)
349 np->linux_phandle = *((u32 *)*p);
352 pp->value = (void *)*p;
356 *p = _ALIGN((*p) + sz, 4);
358 /* with version 0x10 we may not have the name property, recreate
359 * it here from the unit name if absent
362 char *p = pathp, *ps = pathp, *pa = NULL;
375 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
376 __alignof__(struct property));
383 memcpy(pp->value, ps, sz - 1);
384 ((char *)pp->value)[sz - 1] = 0;
385 DBG("fixed up name for %s -> %s\n", pathp,
391 np->name = of_get_property(np, "name", NULL);
392 np->type = of_get_property(np, "device_type", NULL);
399 while (tag == OF_DT_BEGIN_NODE) {
400 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
401 tag = *((u32 *)(*p));
403 if (tag != OF_DT_END_NODE) {
404 printk("Weird tag at end of node: %x\n", tag);
411 static int __init early_parse_mem(char *p)
416 memory_limit = PAGE_ALIGN(memparse(p, &p));
417 DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit);
421 early_param("mem", early_parse_mem);
424 * move_device_tree - move tree to an unused area, if needed.
426 * The device tree may be allocated beyond our memory limit, or inside the
427 * crash kernel region for kdump. If so, move it out of the way.
429 static void __init move_device_tree(void)
431 unsigned long start, size;
434 DBG("-> move_device_tree\n");
436 start = __pa(initial_boot_params);
437 size = initial_boot_params->totalsize;
439 if ((memory_limit && (start + size) > memory_limit) ||
440 overlaps_crashkernel(start, size)) {
441 p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
442 memcpy(p, initial_boot_params, size);
443 initial_boot_params = (struct boot_param_header *)p;
444 DBG("Moved device tree to 0x%p\n", p);
447 DBG("<- move_device_tree\n");
451 * unflattens the device-tree passed by the firmware, creating the
452 * tree of struct device_node. It also fills the "name" and "type"
453 * pointers of the nodes so the normal device-tree walking functions
454 * can be used (this used to be done by finish_device_tree)
456 void __init unflatten_device_tree(void)
458 unsigned long start, mem, size;
459 struct device_node **allnextp = &allnodes;
461 DBG(" -> unflatten_device_tree()\n");
463 /* First pass, scan for size */
464 start = ((unsigned long)initial_boot_params) +
465 initial_boot_params->off_dt_struct;
466 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
467 size = (size | 3) + 1;
469 DBG(" size is %lx, allocating...\n", size);
471 /* Allocate memory for the expanded device tree */
472 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
473 mem = (unsigned long) __va(mem);
475 ((u32 *)mem)[size / 4] = 0xdeadbeef;
477 DBG(" unflattening %lx...\n", mem);
479 /* Second pass, do actual unflattening */
480 start = ((unsigned long)initial_boot_params) +
481 initial_boot_params->off_dt_struct;
482 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
483 if (*((u32 *)start) != OF_DT_END)
484 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
485 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
486 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
487 ((u32 *)mem)[size / 4] );
490 /* Get pointer to OF "/chosen" node for use everywhere */
491 of_chosen = of_find_node_by_path("/chosen");
492 if (of_chosen == NULL)
493 of_chosen = of_find_node_by_path("/chosen@0");
495 DBG(" <- unflatten_device_tree()\n");
499 * ibm,pa-features is a per-cpu property that contains a string of
500 * attribute descriptors, each of which has a 2 byte header plus up
501 * to 254 bytes worth of processor attribute bits. First header
502 * byte specifies the number of bytes following the header.
503 * Second header byte is an "attribute-specifier" type, of which
504 * zero is the only currently-defined value.
505 * Implementation: Pass in the byte and bit offset for the feature
506 * that we are interested in. The function will return -1 if the
507 * pa-features property is missing, or a 1/0 to indicate if the feature
508 * is supported/not supported. Note that the bit numbers are
509 * big-endian to match the definition in PAPR.
511 static struct ibm_pa_feature {
512 unsigned long cpu_features; /* CPU_FTR_xxx bit */
513 unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
514 unsigned char pabyte; /* byte number in ibm,pa-features */
515 unsigned char pabit; /* bit number (big-endian) */
516 unsigned char invert; /* if 1, pa bit set => clear feature */
517 } ibm_pa_features[] __initdata = {
518 {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
519 {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
520 {CPU_FTR_SLB, 0, 0, 2, 0},
521 {CPU_FTR_CTRL, 0, 0, 3, 0},
522 {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
523 {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
524 {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
525 {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
528 static void __init scan_features(unsigned long node, unsigned char *ftrs,
529 unsigned long tablelen,
530 struct ibm_pa_feature *fp,
531 unsigned long ft_size)
533 unsigned long i, len, bit;
535 /* find descriptor with type == 0 */
541 return; /* descriptor 0 not found */
548 /* loop over bits we know about */
549 for (i = 0; i < ft_size; ++i, ++fp) {
550 if (fp->pabyte >= ftrs[0])
552 bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
553 if (bit ^ fp->invert) {
554 cur_cpu_spec->cpu_features |= fp->cpu_features;
555 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
557 cur_cpu_spec->cpu_features &= ~fp->cpu_features;
558 cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
563 static void __init check_cpu_pa_features(unsigned long node)
565 unsigned char *pa_ftrs;
566 unsigned long tablelen;
568 pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
572 scan_features(node, pa_ftrs, tablelen,
573 ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
576 #ifdef CONFIG_PPC_STD_MMU_64
577 static void __init check_cpu_slb_size(unsigned long node)
581 slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
582 if (slb_size_ptr != NULL) {
583 mmu_slb_size = *slb_size_ptr;
586 slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
587 if (slb_size_ptr != NULL) {
588 mmu_slb_size = *slb_size_ptr;
592 #define check_cpu_slb_size(node) do { } while(0)
595 static struct feature_property {
598 unsigned long cpu_feature;
599 unsigned long cpu_user_ftr;
600 } feature_properties[] __initdata = {
601 #ifdef CONFIG_ALTIVEC
602 {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
603 {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
604 #endif /* CONFIG_ALTIVEC */
606 /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
607 {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
608 #endif /* CONFIG_VSX */
610 {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
611 {"ibm,purr", 1, CPU_FTR_PURR, 0},
612 {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
613 #endif /* CONFIG_PPC64 */
616 #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
617 static inline void identical_pvr_fixup(unsigned long node)
620 char *model = of_get_flat_dt_prop(node, "model", NULL);
623 * Since 440GR(x)/440EP(x) processors have the same pvr,
624 * we check the node path and set bit 28 in the cur_cpu_spec
625 * pvr for EP(x) processor version. This bit is always 0 in
626 * the "real" pvr. Then we call identify_cpu again with
627 * the new logical pvr to enable FPU support.
629 if (model && strstr(model, "440EP")) {
630 pvr = cur_cpu_spec->pvr_value | 0x8;
631 identify_cpu(0, pvr);
632 DBG("Using logical pvr %x for %s\n", pvr, model);
636 #define identical_pvr_fixup(node) do { } while(0)
639 static void __init check_cpu_feature_properties(unsigned long node)
642 struct feature_property *fp = feature_properties;
645 for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
646 prop = of_get_flat_dt_prop(node, fp->name, NULL);
647 if (prop && *prop >= fp->min_value) {
648 cur_cpu_spec->cpu_features |= fp->cpu_feature;
649 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
654 static int __init early_init_dt_scan_cpus(unsigned long node,
655 const char *uname, int depth,
658 static int logical_cpuid = 0;
659 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
666 /* We are scanning "cpu" nodes only */
667 if (type == NULL || strcmp(type, "cpu") != 0)
670 /* Get physical cpuid */
671 intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
673 nthreads = len / sizeof(int);
675 intserv = of_get_flat_dt_prop(node, "reg", NULL);
680 * Now see if any of these threads match our boot cpu.
681 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
683 for (i = 0; i < nthreads; i++) {
685 * version 2 of the kexec param format adds the phys cpuid of
688 if (initial_boot_params && initial_boot_params->version >= 2) {
690 initial_boot_params->boot_cpuid_phys) {
696 * Check if it's the boot-cpu, set it's hw index now,
697 * unfortunately this format did not support booting
698 * off secondary threads.
700 if (of_get_flat_dt_prop(node,
701 "linux,boot-cpu", NULL) != NULL) {
708 /* logical cpu id is always 0 on UP kernels */
714 DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
716 boot_cpuid = logical_cpuid;
717 set_hard_smp_processor_id(boot_cpuid, intserv[i]);
720 * PAPR defines "logical" PVR values for cpus that
721 * meet various levels of the architecture:
722 * 0x0f000001 Architecture version 2.04
723 * 0x0f000002 Architecture version 2.05
724 * If the cpu-version property in the cpu node contains
725 * such a value, we call identify_cpu again with the
726 * logical PVR value in order to use the cpu feature
727 * bits appropriate for the architecture level.
729 * A POWER6 partition in "POWER6 architected" mode
730 * uses the 0x0f000002 PVR value; in POWER5+ mode
731 * it uses 0x0f000001.
733 prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
734 if (prop && (*prop & 0xff000000) == 0x0f000000)
735 identify_cpu(0, *prop);
737 identical_pvr_fixup(node);
740 check_cpu_feature_properties(node);
741 check_cpu_pa_features(node);
742 check_cpu_slb_size(node);
744 #ifdef CONFIG_PPC_PSERIES
746 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
748 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
754 #ifdef CONFIG_BLK_DEV_INITRD
755 static void __init early_init_dt_check_for_initrd(unsigned long node)
760 DBG("Looking for initrd properties... ");
762 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
764 initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
766 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
768 initrd_end = (unsigned long)
769 __va(of_read_ulong(prop, l/4));
770 initrd_below_start_ok = 1;
776 DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end);
779 static inline void early_init_dt_check_for_initrd(unsigned long node)
782 #endif /* CONFIG_BLK_DEV_INITRD */
784 static int __init early_init_dt_scan_chosen(unsigned long node,
785 const char *uname, int depth, void *data)
787 unsigned long *lprop;
791 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
794 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
798 /* check if iommu is forced on or off */
799 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
801 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
805 /* mem=x on the command line is the preferred mechanism */
806 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
808 memory_limit = *lprop;
811 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
813 tce_alloc_start = *lprop;
814 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
816 tce_alloc_end = *lprop;
820 lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
822 crashk_res.start = *lprop;
824 lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
826 crashk_res.end = crashk_res.start + *lprop - 1;
829 early_init_dt_check_for_initrd(node);
831 /* Retreive command line */
832 p = of_get_flat_dt_prop(node, "bootargs", &l);
833 if (p != NULL && l > 0)
834 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
836 #ifdef CONFIG_CMDLINE
837 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
838 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
839 #endif /* CONFIG_CMDLINE */
841 DBG("Command line is: %s\n", cmd_line);
847 static int __init early_init_dt_scan_root(unsigned long node,
848 const char *uname, int depth, void *data)
855 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
856 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
857 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
859 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
860 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
861 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
867 static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
872 return of_read_number(p, s);
875 #ifdef CONFIG_PPC_PSERIES
877 * Interpret the ibm,dynamic-memory property in the
878 * /ibm,dynamic-reconfiguration-memory node.
879 * This contains a list of memory blocks along with NUMA affinity
882 static int __init early_init_dt_scan_drconf_memory(unsigned long node)
884 cell_t *dm, *ls, *usm;
885 unsigned long l, n, flags;
886 u64 base, size, lmb_size;
887 unsigned int is_kexec_kdump = 0, rngs;
889 ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
890 if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
892 lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
894 dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
895 if (dm == NULL || l < sizeof(cell_t))
898 n = *dm++; /* number of entries */
899 if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
902 /* check if this is a kexec/kdump kernel. */
903 usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
908 for (; n != 0; --n) {
909 base = dt_mem_next_cell(dt_root_addr_cells, &dm);
911 /* skip DRC index, pad, assoc. list index, flags */
913 /* skip this block if the reserved bit is set in flags (0x80)
914 or if the block is not assigned to this partition (0x8) */
915 if ((flags & 0x80) || !(flags & 0x8))
919 if (is_kexec_kdump) {
921 * For each lmb in ibm,dynamic-memory, a corresponding
922 * entry in linux,drconf-usable-memory property contains
923 * a counter 'p' followed by 'p' (base, size) duple.
924 * Now read the counter from
925 * linux,drconf-usable-memory property
927 rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
928 if (!rngs) /* there are no (base, size) duple */
932 if (is_kexec_kdump) {
933 base = dt_mem_next_cell(dt_root_addr_cells,
935 size = dt_mem_next_cell(dt_root_size_cells,
939 if (base >= 0x80000000ul)
941 if ((base + size) > 0x80000000ul)
942 size = 0x80000000ul - base;
951 #define early_init_dt_scan_drconf_memory(node) 0
952 #endif /* CONFIG_PPC_PSERIES */
954 static int __init early_init_dt_scan_memory(unsigned long node,
955 const char *uname, int depth, void *data)
957 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
961 /* Look for the ibm,dynamic-reconfiguration-memory node */
963 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
964 return early_init_dt_scan_drconf_memory(node);
966 /* We are scanning "memory" nodes only */
969 * The longtrail doesn't have a device_type on the
970 * /memory node, so look for the node called /memory@0.
972 if (depth != 1 || strcmp(uname, "memory@0") != 0)
974 } else if (strcmp(type, "memory") != 0)
977 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
979 reg = of_get_flat_dt_prop(node, "reg", &l);
983 endp = reg + (l / sizeof(cell_t));
985 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
986 uname, l, reg[0], reg[1], reg[2], reg[3]);
988 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
991 base = dt_mem_next_cell(dt_root_addr_cells, ®);
992 size = dt_mem_next_cell(dt_root_size_cells, ®);
996 DBG(" - %llx , %llx\n", (unsigned long long)base,
997 (unsigned long long)size);
1000 if (base >= 0x80000000ul)
1002 if ((base + size) > 0x80000000ul)
1003 size = 0x80000000ul - base;
1006 lmb_add(base, size);
1008 memstart_addr = min((u64)memstart_addr, base);
1014 static void __init early_reserve_mem(void)
1018 unsigned long self_base;
1019 unsigned long self_size;
1021 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
1022 initial_boot_params->off_mem_rsvmap);
1024 /* before we do anything, lets reserve the dt blob */
1025 self_base = __pa((unsigned long)initial_boot_params);
1026 self_size = initial_boot_params->totalsize;
1027 lmb_reserve(self_base, self_size);
1029 #ifdef CONFIG_BLK_DEV_INITRD
1030 /* then reserve the initrd, if any */
1031 if (initrd_start && (initrd_end > initrd_start))
1032 lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
1033 #endif /* CONFIG_BLK_DEV_INITRD */
1037 * Handle the case where we might be booting from an old kexec
1038 * image that setup the mem_rsvmap as pairs of 32-bit values
1040 if (*reserve_map > 0xffffffffull) {
1041 u32 base_32, size_32;
1042 u32 *reserve_map_32 = (u32 *)reserve_map;
1045 base_32 = *(reserve_map_32++);
1046 size_32 = *(reserve_map_32++);
1049 /* skip if the reservation is for the blob */
1050 if (base_32 == self_base && size_32 == self_size)
1052 DBG("reserving: %x -> %x\n", base_32, size_32);
1053 lmb_reserve(base_32, size_32);
1059 base = *(reserve_map++);
1060 size = *(reserve_map++);
1063 DBG("reserving: %llx -> %llx\n", base, size);
1064 lmb_reserve(base, size);
1068 #ifdef CONFIG_PHYP_DUMP
1070 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
1072 * Function to find the largest size we need to reserve
1073 * during early boot process.
1075 * It either looks for boot param and returns that OR
1076 * returns larger of 256 or 5% rounded down to multiples of 256MB.
1079 static inline unsigned long phyp_dump_calculate_reserve_size(void)
1083 if (phyp_dump_info->reserve_bootvar)
1084 return phyp_dump_info->reserve_bootvar;
1086 /* divide by 20 to get 5% of value */
1087 tmp = lmb_end_of_DRAM();
1090 /* round it down in multiples of 256 */
1091 tmp = tmp & ~0x0FFFFFFFUL;
1093 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
1097 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
1099 * This routine may reserve memory regions in the kernel only
1100 * if the system is supported and a dump was taken in last
1101 * boot instance or if the hardware is supported and the
1102 * scratch area needs to be setup. In other instances it returns
1103 * without reserving anything. The memory in case of dump being
1104 * active is freed when the dump is collected (by userland tools).
1106 static void __init phyp_dump_reserve_mem(void)
1108 unsigned long base, size;
1109 unsigned long variable_reserve_size;
1111 if (!phyp_dump_info->phyp_dump_configured) {
1112 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
1116 if (!phyp_dump_info->phyp_dump_at_boot) {
1117 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
1121 variable_reserve_size = phyp_dump_calculate_reserve_size();
1123 if (phyp_dump_info->phyp_dump_is_active) {
1124 /* Reserve *everything* above RMR.Area freed by userland tools*/
1125 base = variable_reserve_size;
1126 size = lmb_end_of_DRAM() - base;
1128 /* XXX crashed_ram_end is wrong, since it may be beyond
1129 * the memory_limit, it will need to be adjusted. */
1130 lmb_reserve(base, size);
1132 phyp_dump_info->init_reserve_start = base;
1133 phyp_dump_info->init_reserve_size = size;
1135 size = phyp_dump_info->cpu_state_size +
1136 phyp_dump_info->hpte_region_size +
1137 variable_reserve_size;
1138 base = lmb_end_of_DRAM() - size;
1139 lmb_reserve(base, size);
1140 phyp_dump_info->init_reserve_start = base;
1141 phyp_dump_info->init_reserve_size = size;
1145 static inline void __init phyp_dump_reserve_mem(void) {}
1146 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
1149 void __init early_init_devtree(void *params)
1153 DBG(" -> early_init_devtree(%p)\n", params);
1155 /* Setup flat device-tree pointer */
1156 initial_boot_params = params;
1158 #ifdef CONFIG_PPC_RTAS
1159 /* Some machines might need RTAS info for debugging, grab it now. */
1160 of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
1163 #ifdef CONFIG_PHYP_DUMP
1164 /* scan tree to see if dump occured during last boot */
1165 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
1168 /* Retrieve various informations from the /chosen node of the
1169 * device-tree, including the platform type, initrd location and
1170 * size, TCE reserve, and more ...
1172 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
1174 /* Scan memory nodes and rebuild LMBs */
1176 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1177 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1179 /* Save command line for /proc/cmdline and then parse parameters */
1180 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
1181 parse_early_param();
1183 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1184 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
1185 /* If relocatable, reserve first 32k for interrupt vectors etc. */
1186 if (PHYSICAL_START > MEMORY_START)
1187 lmb_reserve(MEMORY_START, 0x8000);
1188 reserve_kdump_trampoline();
1189 reserve_crashkernel();
1190 early_reserve_mem();
1191 phyp_dump_reserve_mem();
1193 limit = memory_limit;
1195 phys_addr_t memsize;
1197 /* Ensure that total memory size is page-aligned, because
1198 * otherwise mark_bootmem() gets upset. */
1200 memsize = lmb_phys_mem_size();
1201 if ((memsize & PAGE_MASK) != memsize)
1202 limit = memsize & PAGE_MASK;
1204 lmb_enforce_memory_limit(limit);
1209 DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
1211 /* We may need to relocate the flat tree, do it now.
1212 * FIXME .. and the initrd too? */
1215 DBG("Scanning CPUs ...\n");
1217 /* Retreive CPU related informations from the flat tree
1218 * (altivec support, boot CPU ID, ...)
1220 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
1222 DBG(" <- early_init_devtree()\n");
1227 * Indicates whether the root node has a given value in its
1228 * compatible property.
1230 int machine_is_compatible(const char *compat)
1232 struct device_node *root;
1235 root = of_find_node_by_path("/");
1237 rc = of_device_is_compatible(root, compat);
1242 EXPORT_SYMBOL(machine_is_compatible);
1246 * New implementation of the OF "find" APIs, return a refcounted
1247 * object, call of_node_put() when done. The device tree and list
1248 * are protected by a rw_lock.
1250 * Note that property management will need some locking as well,
1251 * this isn't dealt with yet.
1256 * of_find_node_by_phandle - Find a node given a phandle
1257 * @handle: phandle of the node to find
1259 * Returns a node pointer with refcount incremented, use
1260 * of_node_put() on it when done.
1262 struct device_node *of_find_node_by_phandle(phandle handle)
1264 struct device_node *np;
1266 read_lock(&devtree_lock);
1267 for (np = allnodes; np != 0; np = np->allnext)
1268 if (np->linux_phandle == handle)
1271 read_unlock(&devtree_lock);
1274 EXPORT_SYMBOL(of_find_node_by_phandle);
1277 * of_find_next_cache_node - Find a node's subsidiary cache
1278 * @np: node of type "cpu" or "cache"
1280 * Returns a node pointer with refcount incremented, use
1281 * of_node_put() on it when done. Caller should hold a reference
1284 struct device_node *of_find_next_cache_node(struct device_node *np)
1286 struct device_node *child;
1287 const phandle *handle;
1289 handle = of_get_property(np, "l2-cache", NULL);
1291 handle = of_get_property(np, "next-level-cache", NULL);
1294 return of_find_node_by_phandle(*handle);
1296 /* OF on pmac has nodes instead of properties named "l2-cache"
1297 * beneath CPU nodes.
1299 if (!strcmp(np->type, "cpu"))
1300 for_each_child_of_node(np, child)
1301 if (!strcmp(child->type, "cache"))
1308 * of_node_get - Increment refcount of a node
1309 * @node: Node to inc refcount, NULL is supported to
1310 * simplify writing of callers
1314 struct device_node *of_node_get(struct device_node *node)
1317 kref_get(&node->kref);
1320 EXPORT_SYMBOL(of_node_get);
1322 static inline struct device_node * kref_to_device_node(struct kref *kref)
1324 return container_of(kref, struct device_node, kref);
1328 * of_node_release - release a dynamically allocated node
1329 * @kref: kref element of the node to be released
1331 * In of_node_put() this function is passed to kref_put()
1332 * as the destructor.
1334 static void of_node_release(struct kref *kref)
1336 struct device_node *node = kref_to_device_node(kref);
1337 struct property *prop = node->properties;
1339 /* We should never be releasing nodes that haven't been detached. */
1340 if (!of_node_check_flag(node, OF_DETACHED)) {
1341 printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
1343 kref_init(&node->kref);
1347 if (!of_node_check_flag(node, OF_DYNAMIC))
1351 struct property *next = prop->next;
1358 prop = node->deadprops;
1359 node->deadprops = NULL;
1362 kfree(node->full_name);
1368 * of_node_put - Decrement refcount of a node
1369 * @node: Node to dec refcount, NULL is supported to
1370 * simplify writing of callers
1373 void of_node_put(struct device_node *node)
1376 kref_put(&node->kref, of_node_release);
1378 EXPORT_SYMBOL(of_node_put);
1381 * Plug a device node into the tree and global list.
1383 void of_attach_node(struct device_node *np)
1385 unsigned long flags;
1387 write_lock_irqsave(&devtree_lock, flags);
1388 np->sibling = np->parent->child;
1389 np->allnext = allnodes;
1390 np->parent->child = np;
1392 write_unlock_irqrestore(&devtree_lock, flags);
1396 * "Unplug" a node from the device tree. The caller must hold
1397 * a reference to the node. The memory associated with the node
1398 * is not freed until its refcount goes to zero.
1400 void of_detach_node(struct device_node *np)
1402 struct device_node *parent;
1403 unsigned long flags;
1405 write_lock_irqsave(&devtree_lock, flags);
1407 parent = np->parent;
1412 allnodes = np->allnext;
1414 struct device_node *prev;
1415 for (prev = allnodes;
1416 prev->allnext != np;
1417 prev = prev->allnext)
1419 prev->allnext = np->allnext;
1422 if (parent->child == np)
1423 parent->child = np->sibling;
1425 struct device_node *prevsib;
1426 for (prevsib = np->parent->child;
1427 prevsib->sibling != np;
1428 prevsib = prevsib->sibling)
1430 prevsib->sibling = np->sibling;
1433 of_node_set_flag(np, OF_DETACHED);
1436 write_unlock_irqrestore(&devtree_lock, flags);
1439 #ifdef CONFIG_PPC_PSERIES
1441 * Fix up the uninitialized fields in a new device node:
1442 * name, type and pci-specific fields
1445 static int of_finish_dynamic_node(struct device_node *node)
1447 struct device_node *parent = of_get_parent(node);
1449 const phandle *ibm_phandle;
1451 node->name = of_get_property(node, "name", NULL);
1452 node->type = of_get_property(node, "device_type", NULL);
1455 node->name = "<NULL>";
1457 node->type = "<NULL>";
1464 /* We don't support that function on PowerMac, at least
1467 if (machine_is(powermac))
1470 /* fix up new node's linux_phandle field */
1471 if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
1472 node->linux_phandle = *ibm_phandle;
1475 of_node_put(parent);
1479 static int prom_reconfig_notifier(struct notifier_block *nb,
1480 unsigned long action, void *node)
1485 case PSERIES_RECONFIG_ADD:
1486 err = of_finish_dynamic_node(node);
1488 printk(KERN_ERR "finish_node returned %d\n", err);
1499 static struct notifier_block prom_reconfig_nb = {
1500 .notifier_call = prom_reconfig_notifier,
1501 .priority = 10, /* This one needs to run first */
1504 static int __init prom_reconfig_setup(void)
1506 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1508 __initcall(prom_reconfig_setup);
1512 * Add a property to a node
1514 int prom_add_property(struct device_node* np, struct property* prop)
1516 struct property **next;
1517 unsigned long flags;
1520 write_lock_irqsave(&devtree_lock, flags);
1521 next = &np->properties;
1523 if (strcmp(prop->name, (*next)->name) == 0) {
1524 /* duplicate ! don't insert it */
1525 write_unlock_irqrestore(&devtree_lock, flags);
1528 next = &(*next)->next;
1531 write_unlock_irqrestore(&devtree_lock, flags);
1533 #ifdef CONFIG_PROC_DEVICETREE
1534 /* try to add to proc as well if it was initialized */
1536 proc_device_tree_add_prop(np->pde, prop);
1537 #endif /* CONFIG_PROC_DEVICETREE */
1543 * Remove a property from a node. Note that we don't actually
1544 * remove it, since we have given out who-knows-how-many pointers
1545 * to the data using get-property. Instead we just move the property
1546 * to the "dead properties" list, so it won't be found any more.
1548 int prom_remove_property(struct device_node *np, struct property *prop)
1550 struct property **next;
1551 unsigned long flags;
1554 write_lock_irqsave(&devtree_lock, flags);
1555 next = &np->properties;
1557 if (*next == prop) {
1558 /* found the node */
1560 prop->next = np->deadprops;
1561 np->deadprops = prop;
1565 next = &(*next)->next;
1567 write_unlock_irqrestore(&devtree_lock, flags);
1572 #ifdef CONFIG_PROC_DEVICETREE
1573 /* try to remove the proc node as well */
1575 proc_device_tree_remove_prop(np->pde, prop);
1576 #endif /* CONFIG_PROC_DEVICETREE */
1582 * Update a property in a node. Note that we don't actually
1583 * remove it, since we have given out who-knows-how-many pointers
1584 * to the data using get-property. Instead we just move the property
1585 * to the "dead properties" list, and add the new property to the
1588 int prom_update_property(struct device_node *np,
1589 struct property *newprop,
1590 struct property *oldprop)
1592 struct property **next;
1593 unsigned long flags;
1596 write_lock_irqsave(&devtree_lock, flags);
1597 next = &np->properties;
1599 if (*next == oldprop) {
1600 /* found the node */
1601 newprop->next = oldprop->next;
1603 oldprop->next = np->deadprops;
1604 np->deadprops = oldprop;
1608 next = &(*next)->next;
1610 write_unlock_irqrestore(&devtree_lock, flags);
1615 #ifdef CONFIG_PROC_DEVICETREE
1616 /* try to add to proc as well if it was initialized */
1618 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1619 #endif /* CONFIG_PROC_DEVICETREE */
1625 /* Find the device node for a given logical cpu number, also returns the cpu
1626 * local thread number (index in ibm,interrupt-server#s) if relevant and
1627 * asked for (non NULL)
1629 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
1632 struct device_node *np;
1634 hardid = get_hard_smp_processor_id(cpu);
1636 for_each_node_by_type(np, "cpu") {
1638 unsigned int plen, t;
1640 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1641 * fallback to "reg" property and assume no threads
1643 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
1645 if (intserv == NULL) {
1646 const u32 *reg = of_get_property(np, "reg", NULL);
1649 if (*reg == hardid) {
1655 plen /= sizeof(u32);
1656 for (t = 0; t < plen; t++) {
1657 if (hardid == intserv[t]) {
1667 EXPORT_SYMBOL(of_get_cpu_node);
1669 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1670 static struct debugfs_blob_wrapper flat_dt_blob;
1672 static int __init export_flat_device_tree(void)
1676 flat_dt_blob.data = initial_boot_params;
1677 flat_dt_blob.size = initial_boot_params->totalsize;
1679 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1680 powerpc_debugfs_root, &flat_dt_blob);
1686 __initcall(export_flat_device_tree);