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/config.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/threads.h>
24 #include <linux/spinlock.h>
25 #include <linux/types.h>
26 #include <linux/pci.h>
27 #include <linux/stringify.h>
28 #include <linux/delay.h>
29 #include <linux/initrd.h>
30 #include <linux/bitops.h>
31 #include <linux/module.h>
32 #include <linux/kexec.h>
38 #include <asm/processor.h>
41 #include <asm/kdump.h>
43 #include <asm/system.h>
45 #include <asm/pgtable.h>
47 #include <asm/iommu.h>
48 #include <asm/btext.h>
49 #include <asm/sections.h>
50 #include <asm/machdep.h>
51 #include <asm/pSeries_reconfig.h>
52 #include <asm/pci-bridge.h>
55 #define DBG(fmt...) printk(KERN_ERR fmt)
61 static int __initdata dt_root_addr_cells;
62 static int __initdata dt_root_size_cells;
65 int __initdata iommu_is_off;
66 int __initdata iommu_force_on;
67 unsigned long tce_alloc_start, tce_alloc_end;
73 static struct boot_param_header *initial_boot_params __initdata;
75 struct boot_param_header *initial_boot_params;
78 static struct device_node *allnodes = NULL;
80 /* use when traversing tree through the allnext, child, sibling,
81 * or parent members of struct device_node.
83 static DEFINE_RWLOCK(devtree_lock);
85 /* export that to outside world */
86 struct device_node *of_chosen;
88 struct device_node *dflt_interrupt_controller;
89 int num_interrupt_controllers;
92 * Wrapper for allocating memory for various data that needs to be
93 * attached to device nodes as they are processed at boot or when
94 * added to the device tree later (e.g. DLPAR). At boot there is
95 * already a region reserved so we just increment *mem_start by size;
96 * otherwise we call kmalloc.
98 static void * prom_alloc(unsigned long size, unsigned long *mem_start)
103 return kmalloc(size, GFP_KERNEL);
111 * Find the device_node with a given phandle.
113 static struct device_node * find_phandle(phandle ph)
115 struct device_node *np;
117 for (np = allnodes; np != 0; np = np->allnext)
118 if (np->linux_phandle == ph)
124 * Find the interrupt parent of a node.
126 static struct device_node * __devinit intr_parent(struct device_node *p)
130 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
133 p = find_phandle(*parp);
137 * On a powermac booted with BootX, we don't get to know the
138 * phandles for any nodes, so find_phandle will return NULL.
139 * Fortunately these machines only have one interrupt controller
140 * so there isn't in fact any ambiguity. -- paulus
142 if (num_interrupt_controllers == 1)
143 p = dflt_interrupt_controller;
148 * Find out the size of each entry of the interrupts property
151 int __devinit prom_n_intr_cells(struct device_node *np)
153 struct device_node *p;
156 for (p = np; (p = intr_parent(p)) != NULL; ) {
157 icp = (unsigned int *)
158 get_property(p, "#interrupt-cells", NULL);
161 if (get_property(p, "interrupt-controller", NULL) != NULL
162 || get_property(p, "interrupt-map", NULL) != NULL) {
163 printk("oops, node %s doesn't have #interrupt-cells\n",
169 printk("prom_n_intr_cells failed for %s\n", np->full_name);
175 * Map an interrupt from a device up to the platform interrupt
178 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
179 struct device_node *np, unsigned int *ints,
182 struct device_node *p, *ipar;
183 unsigned int *imap, *imask, *ip;
184 int i, imaplen, match;
185 int newintrc = 0, newaddrc = 0;
189 reg = (unsigned int *) get_property(np, "reg", NULL);
190 naddrc = prom_n_addr_cells(np);
193 if (get_property(p, "interrupt-controller", NULL) != NULL)
194 /* this node is an interrupt controller, stop here */
196 imap = (unsigned int *)
197 get_property(p, "interrupt-map", &imaplen);
202 imask = (unsigned int *)
203 get_property(p, "interrupt-map-mask", NULL);
205 printk("oops, %s has interrupt-map but no mask\n",
209 imaplen /= sizeof(unsigned int);
212 while (imaplen > 0 && !match) {
213 /* check the child-interrupt field */
215 for (i = 0; i < naddrc && match; ++i)
216 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
217 for (; i < naddrc + nintrc && match; ++i)
218 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
219 imap += naddrc + nintrc;
220 imaplen -= naddrc + nintrc;
221 /* grab the interrupt parent */
222 ipar = find_phandle((phandle) *imap++);
224 if (ipar == NULL && num_interrupt_controllers == 1)
225 /* cope with BootX not giving us phandles */
226 ipar = dflt_interrupt_controller;
228 printk("oops, no int parent %x in map of %s\n",
229 imap[-1], p->full_name);
232 /* find the parent's # addr and intr cells */
233 ip = (unsigned int *)
234 get_property(ipar, "#interrupt-cells", NULL);
236 printk("oops, no #interrupt-cells on %s\n",
241 ip = (unsigned int *)
242 get_property(ipar, "#address-cells", NULL);
243 newaddrc = (ip == NULL)? 0: *ip;
244 imap += newaddrc + newintrc;
245 imaplen -= newaddrc + newintrc;
248 printk("oops, error decoding int-map on %s, len=%d\n",
249 p->full_name, imaplen);
254 printk("oops, no match in %s int-map for %s\n",
255 p->full_name, np->full_name);
262 ints = imap - nintrc;
267 printk("hmmm, int tree for %s doesn't have ctrler\n",
277 static unsigned char map_isa_senses[4] = {
278 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
279 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
280 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
281 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE
284 static unsigned char map_mpic_senses[4] = {
285 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE,
286 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
287 /* 2 seems to be used for the 8259 cascade... */
288 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
289 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
292 static int __devinit finish_node_interrupts(struct device_node *np,
293 unsigned long *mem_start,
297 int intlen, intrcells, intrcount;
299 unsigned int *irq, virq;
300 struct device_node *ic;
303 //#define TRACE(fmt...) do { if (trace) { printk(fmt); mdelay(1000); } } while(0)
304 #define TRACE(fmt...)
306 if (!strcmp(np->name, "smu-doorbell"))
309 TRACE("Finishing SMU doorbell ! num_interrupt_controllers = %d\n",
310 num_interrupt_controllers);
312 if (num_interrupt_controllers == 0) {
314 * Old machines just have a list of interrupt numbers
315 * and no interrupt-controller nodes.
317 ints = (unsigned int *) get_property(np, "AAPL,interrupts",
319 /* XXX old interpret_pci_props looked in parent too */
320 /* XXX old interpret_macio_props looked for interrupts
321 before AAPL,interrupts */
323 ints = (unsigned int *) get_property(np, "interrupts",
328 np->n_intrs = intlen / sizeof(unsigned int);
329 np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]),
336 for (i = 0; i < np->n_intrs; ++i) {
337 np->intrs[i].line = *ints++;
338 np->intrs[i].sense = IRQ_SENSE_LEVEL
339 | IRQ_POLARITY_NEGATIVE;
344 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
345 TRACE("ints=%p, intlen=%d\n", ints, intlen);
348 intrcells = prom_n_intr_cells(np);
349 intlen /= intrcells * sizeof(unsigned int);
350 TRACE("intrcells=%d, new intlen=%d\n", intrcells, intlen);
351 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
359 for (i = 0; i < intlen; ++i, ints += intrcells) {
360 n = map_interrupt(&irq, &ic, np, ints, intrcells);
361 TRACE("map, irq=%d, ic=%p, n=%d\n", irq, ic, n);
365 /* don't map IRQ numbers under a cascaded 8259 controller */
366 if (ic && device_is_compatible(ic, "chrp,iic")) {
367 np->intrs[intrcount].line = irq[0];
368 sense = (n > 1)? (irq[1] & 3): 3;
369 np->intrs[intrcount].sense = map_isa_senses[sense];
371 virq = virt_irq_create_mapping(irq[0]);
372 TRACE("virq=%d\n", virq);
374 if (virq == NO_IRQ) {
375 printk(KERN_CRIT "Could not allocate interrupt"
376 " number for %s\n", np->full_name);
380 np->intrs[intrcount].line = irq_offset_up(virq);
381 sense = (n > 1)? (irq[1] & 3): 1;
383 /* Apple uses bits in there in a different way, let's
384 * only keep the real sense bit on macs
386 if (machine_is(powermac))
388 np->intrs[intrcount].sense = map_mpic_senses[sense];
392 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
393 if (machine_is(powermac) && ic && ic->parent) {
394 char *name = get_property(ic->parent, "name", NULL);
395 if (name && !strcmp(name, "u3"))
396 np->intrs[intrcount].line += 128;
397 else if (!(name && (!strcmp(name, "mac-io") ||
398 !strcmp(name, "u4"))))
399 /* ignore other cascaded controllers, such as
403 #endif /* CONFIG_PPC64 */
405 printk("hmmm, got %d intr cells for %s:", n,
407 for (j = 0; j < n; ++j)
408 printk(" %d", irq[j]);
413 np->n_intrs = intrcount;
418 static int __devinit finish_node(struct device_node *np,
419 unsigned long *mem_start,
422 struct device_node *child;
425 rc = finish_node_interrupts(np, mem_start, measure_only);
429 for (child = np->child; child != NULL; child = child->sibling) {
430 rc = finish_node(child, mem_start, measure_only);
438 static void __init scan_interrupt_controllers(void)
440 struct device_node *np;
445 for (np = allnodes; np != NULL; np = np->allnext) {
446 ic = get_property(np, "interrupt-controller", &iclen);
447 name = get_property(np, "name", NULL);
448 /* checking iclen makes sure we don't get a false
449 match on /chosen.interrupt_controller */
451 && strcmp(name, "interrupt-controller") == 0)
452 || (ic != NULL && iclen == 0
453 && strcmp(name, "AppleKiwi"))) {
455 dflt_interrupt_controller = np;
459 num_interrupt_controllers = n;
463 * finish_device_tree is called once things are running normally
464 * (i.e. with text and data mapped to the address they were linked at).
465 * It traverses the device tree and fills in some of the additional,
466 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
467 * mapping is also initialized at this point.
469 void __init finish_device_tree(void)
471 unsigned long start, end, size = 0;
473 DBG(" -> finish_device_tree\n");
476 /* Initialize virtual IRQ map */
479 scan_interrupt_controllers();
482 * Finish device-tree (pre-parsing some properties etc...)
483 * We do this in 2 passes. One with "measure_only" set, which
484 * will only measure the amount of memory needed, then we can
485 * allocate that memory, and call finish_node again. However,
486 * we must be careful as most routines will fail nowadays when
487 * prom_alloc() returns 0, so we must make sure our first pass
488 * doesn't start at 0. We pre-initialize size to 16 for that
489 * reason and then remove those additional 16 bytes
492 finish_node(allnodes, &size, 1);
498 end = start = (unsigned long)__va(lmb_alloc(size, 128));
500 finish_node(allnodes, &end, 0);
501 BUG_ON(end != start + size);
503 DBG(" <- finish_device_tree\n");
506 static inline char *find_flat_dt_string(u32 offset)
508 return ((char *)initial_boot_params) +
509 initial_boot_params->off_dt_strings + offset;
513 * This function is used to scan the flattened device-tree, it is
514 * used to extract the memory informations at boot before we can
517 int __init of_scan_flat_dt(int (*it)(unsigned long node,
518 const char *uname, int depth,
522 unsigned long p = ((unsigned long)initial_boot_params) +
523 initial_boot_params->off_dt_struct;
528 u32 tag = *((u32 *)p);
532 if (tag == OF_DT_END_NODE) {
536 if (tag == OF_DT_NOP)
538 if (tag == OF_DT_END)
540 if (tag == OF_DT_PROP) {
541 u32 sz = *((u32 *)p);
543 if (initial_boot_params->version < 0x10)
544 p = _ALIGN(p, sz >= 8 ? 8 : 4);
549 if (tag != OF_DT_BEGIN_NODE) {
550 printk(KERN_WARNING "Invalid tag %x scanning flattened"
551 " device tree !\n", tag);
556 p = _ALIGN(p + strlen(pathp) + 1, 4);
557 if ((*pathp) == '/') {
559 for (lp = NULL, np = pathp; *np; np++)
565 rc = it(p, pathp, depth, data);
573 unsigned long __init of_get_flat_dt_root(void)
575 unsigned long p = ((unsigned long)initial_boot_params) +
576 initial_boot_params->off_dt_struct;
578 while(*((u32 *)p) == OF_DT_NOP)
580 BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
582 return _ALIGN(p + strlen((char *)p) + 1, 4);
586 * This function can be used within scan_flattened_dt callback to get
587 * access to properties
589 void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
592 unsigned long p = node;
595 u32 tag = *((u32 *)p);
600 if (tag == OF_DT_NOP)
602 if (tag != OF_DT_PROP)
606 noff = *((u32 *)(p + 4));
608 if (initial_boot_params->version < 0x10)
609 p = _ALIGN(p, sz >= 8 ? 8 : 4);
611 nstr = find_flat_dt_string(noff);
613 printk(KERN_WARNING "Can't find property index"
617 if (strcmp(name, nstr) == 0) {
627 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
630 unsigned long cplen, l;
632 cp = of_get_flat_dt_prop(node, "compatible", &cplen);
636 if (strncasecmp(cp, compat, strlen(compat)) == 0)
646 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
651 *mem = _ALIGN(*mem, align);
658 static unsigned long __init unflatten_dt_node(unsigned long mem,
660 struct device_node *dad,
661 struct device_node ***allnextpp,
662 unsigned long fpsize)
664 struct device_node *np;
665 struct property *pp, **prev_pp = NULL;
668 unsigned int l, allocl;
672 tag = *((u32 *)(*p));
673 if (tag != OF_DT_BEGIN_NODE) {
674 printk("Weird tag at start of node: %x\n", tag);
679 l = allocl = strlen(pathp) + 1;
680 *p = _ALIGN(*p + l, 4);
682 /* version 0x10 has a more compact unit name here instead of the full
683 * path. we accumulate the full path size using "fpsize", we'll rebuild
684 * it later. We detect this because the first character of the name is
687 if ((*pathp) != '/') {
690 /* root node: special case. fpsize accounts for path
691 * plus terminating zero. root node only has '/', so
692 * fpsize should be 2, but we want to avoid the first
693 * level nodes to have two '/' so we use fpsize 1 here
698 /* account for '/' and path size minus terminal 0
707 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
708 __alignof__(struct device_node));
710 memset(np, 0, sizeof(*np));
711 np->full_name = ((char*)np) + sizeof(struct device_node);
713 char *p = np->full_name;
714 /* rebuild full path for new format */
715 if (dad && dad->parent) {
716 strcpy(p, dad->full_name);
718 if ((strlen(p) + l + 1) != allocl) {
719 DBG("%s: p: %d, l: %d, a: %d\n",
720 pathp, (int)strlen(p), l, allocl);
728 memcpy(np->full_name, pathp, l);
729 prev_pp = &np->properties;
731 *allnextpp = &np->allnext;
734 /* we temporarily use the next field as `last_child'*/
738 dad->next->sibling = np;
741 kref_init(&np->kref);
747 tag = *((u32 *)(*p));
748 if (tag == OF_DT_NOP) {
752 if (tag != OF_DT_PROP)
756 noff = *((u32 *)((*p) + 4));
758 if (initial_boot_params->version < 0x10)
759 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
761 pname = find_flat_dt_string(noff);
763 printk("Can't find property name in list !\n");
766 if (strcmp(pname, "name") == 0)
768 l = strlen(pname) + 1;
769 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
770 __alignof__(struct property));
772 if (strcmp(pname, "linux,phandle") == 0) {
773 np->node = *((u32 *)*p);
774 if (np->linux_phandle == 0)
775 np->linux_phandle = np->node;
777 if (strcmp(pname, "ibm,phandle") == 0)
778 np->linux_phandle = *((u32 *)*p);
781 pp->value = (void *)*p;
785 *p = _ALIGN((*p) + sz, 4);
787 /* with version 0x10 we may not have the name property, recreate
788 * it here from the unit name if absent
791 char *p = pathp, *ps = pathp, *pa = NULL;
804 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
805 __alignof__(struct property));
809 pp->value = (unsigned char *)(pp + 1);
812 memcpy(pp->value, ps, sz - 1);
813 ((char *)pp->value)[sz - 1] = 0;
814 DBG("fixed up name for %s -> %s\n", pathp, pp->value);
819 np->name = get_property(np, "name", NULL);
820 np->type = get_property(np, "device_type", NULL);
827 while (tag == OF_DT_BEGIN_NODE) {
828 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
829 tag = *((u32 *)(*p));
831 if (tag != OF_DT_END_NODE) {
832 printk("Weird tag at end of node: %x\n", tag);
841 * unflattens the device-tree passed by the firmware, creating the
842 * tree of struct device_node. It also fills the "name" and "type"
843 * pointers of the nodes so the normal device-tree walking functions
844 * can be used (this used to be done by finish_device_tree)
846 void __init unflatten_device_tree(void)
848 unsigned long start, mem, size;
849 struct device_node **allnextp = &allnodes;
851 DBG(" -> unflatten_device_tree()\n");
853 /* First pass, scan for size */
854 start = ((unsigned long)initial_boot_params) +
855 initial_boot_params->off_dt_struct;
856 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
857 size = (size | 3) + 1;
859 DBG(" size is %lx, allocating...\n", size);
861 /* Allocate memory for the expanded device tree */
862 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
863 mem = (unsigned long) __va(mem);
865 ((u32 *)mem)[size / 4] = 0xdeadbeef;
867 DBG(" unflattening %lx...\n", mem);
869 /* Second pass, do actual unflattening */
870 start = ((unsigned long)initial_boot_params) +
871 initial_boot_params->off_dt_struct;
872 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
873 if (*((u32 *)start) != OF_DT_END)
874 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
875 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
876 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
877 ((u32 *)mem)[size / 4] );
880 /* Get pointer to OF "/chosen" node for use everywhere */
881 of_chosen = of_find_node_by_path("/chosen");
882 if (of_chosen == NULL)
883 of_chosen = of_find_node_by_path("/chosen@0");
885 DBG(" <- unflatten_device_tree()\n");
888 static int __init early_init_dt_scan_cpus(unsigned long node,
889 const char *uname, int depth,
892 static int logical_cpuid = 0;
893 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
894 #ifdef CONFIG_ALTIVEC
902 /* We are scanning "cpu" nodes only */
903 if (type == NULL || strcmp(type, "cpu") != 0)
906 /* Get physical cpuid */
907 intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
909 nthreads = len / sizeof(int);
911 intserv = of_get_flat_dt_prop(node, "reg", NULL);
916 * Now see if any of these threads match our boot cpu.
917 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
919 for (i = 0; i < nthreads; i++) {
921 * version 2 of the kexec param format adds the phys cpuid of
924 if (initial_boot_params && initial_boot_params->version >= 2) {
926 initial_boot_params->boot_cpuid_phys) {
932 * Check if it's the boot-cpu, set it's hw index now,
933 * unfortunately this format did not support booting
934 * off secondary threads.
936 if (of_get_flat_dt_prop(node,
937 "linux,boot-cpu", NULL) != NULL) {
944 /* logical cpu id is always 0 on UP kernels */
950 DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
952 boot_cpuid = logical_cpuid;
953 set_hard_smp_processor_id(boot_cpuid, intserv[i]);
956 #ifdef CONFIG_ALTIVEC
957 /* Check if we have a VMX and eventually update CPU features */
958 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL);
959 if (prop && (*prop) > 0) {
960 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
961 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
964 /* Same goes for Apple's "altivec" property */
965 prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
967 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
968 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
970 #endif /* CONFIG_ALTIVEC */
972 #ifdef CONFIG_PPC_PSERIES
974 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
976 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
982 static int __init early_init_dt_scan_chosen(unsigned long node,
983 const char *uname, int depth, void *data)
985 unsigned long *lprop;
989 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
992 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
996 /* check if iommu is forced on or off */
997 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
999 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
1003 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
1005 memory_limit = *lprop;
1008 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
1010 tce_alloc_start = *lprop;
1011 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
1013 tce_alloc_end = *lprop;
1016 #ifdef CONFIG_PPC_RTAS
1017 /* To help early debugging via the front panel, we retrieve a minimal
1018 * set of RTAS infos now if available
1021 u64 *basep, *entryp, *sizep;
1023 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
1024 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1025 sizep = of_get_flat_dt_prop(node, "linux,rtas-size", NULL);
1026 if (basep && entryp && sizep) {
1028 rtas.entry = *entryp;
1032 #endif /* CONFIG_PPC_RTAS */
1035 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
1037 crashk_res.start = *lprop;
1039 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
1041 crashk_res.end = crashk_res.start + *lprop - 1;
1044 /* Retreive command line */
1045 p = of_get_flat_dt_prop(node, "bootargs", &l);
1046 if (p != NULL && l > 0)
1047 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
1049 #ifdef CONFIG_CMDLINE
1050 if (l == 0 || (l == 1 && (*p) == 0))
1051 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1052 #endif /* CONFIG_CMDLINE */
1054 DBG("Command line is: %s\n", cmd_line);
1056 if (strstr(cmd_line, "mem=")) {
1059 for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
1061 if (p > cmd_line && p[-1] != ' ')
1063 memory_limit = memparse(q, &q);
1071 static int __init early_init_dt_scan_root(unsigned long node,
1072 const char *uname, int depth, void *data)
1079 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
1080 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1081 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
1083 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
1084 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1085 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1091 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1096 /* Ignore more than 2 cells */
1097 while (s > sizeof(unsigned long) / 4) {
1115 static int __init early_init_dt_scan_memory(unsigned long node,
1116 const char *uname, int depth, void *data)
1118 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1122 /* We are scanning "memory" nodes only */
1125 * The longtrail doesn't have a device_type on the
1126 * /memory node, so look for the node called /memory@0.
1128 if (depth != 1 || strcmp(uname, "memory@0") != 0)
1130 } else if (strcmp(type, "memory") != 0)
1133 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1135 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
1139 endp = reg + (l / sizeof(cell_t));
1141 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
1142 uname, l, reg[0], reg[1], reg[2], reg[3]);
1144 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1145 unsigned long base, size;
1147 base = dt_mem_next_cell(dt_root_addr_cells, ®);
1148 size = dt_mem_next_cell(dt_root_size_cells, ®);
1152 DBG(" - %lx , %lx\n", base, size);
1155 if (base >= 0x80000000ul)
1157 if ((base + size) > 0x80000000ul)
1158 size = 0x80000000ul - base;
1161 lmb_add(base, size);
1166 static void __init early_reserve_mem(void)
1171 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
1172 initial_boot_params->off_mem_rsvmap);
1175 * Handle the case where we might be booting from an old kexec
1176 * image that setup the mem_rsvmap as pairs of 32-bit values
1178 if (*reserve_map > 0xffffffffull) {
1179 u32 base_32, size_32;
1180 u32 *reserve_map_32 = (u32 *)reserve_map;
1183 base_32 = *(reserve_map_32++);
1184 size_32 = *(reserve_map_32++);
1187 DBG("reserving: %x -> %x\n", base_32, size_32);
1188 lmb_reserve(base_32, size_32);
1194 base = *(reserve_map++);
1195 size = *(reserve_map++);
1198 DBG("reserving: %llx -> %llx\n", base, size);
1199 lmb_reserve(base, size);
1203 DBG("memory reserved, lmbs :\n");
1208 void __init early_init_devtree(void *params)
1210 DBG(" -> early_init_devtree()\n");
1212 /* Setup flat device-tree pointer */
1213 initial_boot_params = params;
1215 /* Retrieve various informations from the /chosen node of the
1216 * device-tree, including the platform type, initrd location and
1217 * size, TCE reserve, and more ...
1219 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
1221 /* Scan memory nodes and rebuild LMBs */
1223 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1224 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1225 lmb_enforce_memory_limit(memory_limit);
1228 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1230 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1231 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
1232 #ifdef CONFIG_CRASH_DUMP
1233 lmb_reserve(0, KDUMP_RESERVE_LIMIT);
1235 early_reserve_mem();
1237 DBG("Scanning CPUs ...\n");
1239 /* Retreive CPU related informations from the flat tree
1240 * (altivec support, boot CPU ID, ...)
1242 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
1244 DBG(" <- early_init_devtree()\n");
1250 prom_n_addr_cells(struct device_node* np)
1256 ip = (int *) get_property(np, "#address-cells", NULL);
1259 } while (np->parent);
1260 /* No #address-cells property for the root node, default to 1 */
1263 EXPORT_SYMBOL(prom_n_addr_cells);
1266 prom_n_size_cells(struct device_node* np)
1272 ip = (int *) get_property(np, "#size-cells", NULL);
1275 } while (np->parent);
1276 /* No #size-cells property for the root node, default to 1 */
1279 EXPORT_SYMBOL(prom_n_size_cells);
1282 * Work out the sense (active-low level / active-high edge)
1283 * of each interrupt from the device tree.
1285 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1287 struct device_node *np;
1290 /* default to level-triggered */
1291 memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
1293 for (np = allnodes; np != 0; np = np->allnext) {
1294 for (j = 0; j < np->n_intrs; j++) {
1295 i = np->intrs[j].line;
1296 if (i >= off && i < max)
1297 senses[i-off] = np->intrs[j].sense;
1303 * Construct and return a list of the device_nodes with a given name.
1305 struct device_node *find_devices(const char *name)
1307 struct device_node *head, **prevp, *np;
1310 for (np = allnodes; np != 0; np = np->allnext) {
1311 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1319 EXPORT_SYMBOL(find_devices);
1322 * Construct and return a list of the device_nodes with a given type.
1324 struct device_node *find_type_devices(const char *type)
1326 struct device_node *head, **prevp, *np;
1329 for (np = allnodes; np != 0; np = np->allnext) {
1330 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1338 EXPORT_SYMBOL(find_type_devices);
1341 * Returns all nodes linked together
1343 struct device_node *find_all_nodes(void)
1345 struct device_node *head, **prevp, *np;
1348 for (np = allnodes; np != 0; np = np->allnext) {
1355 EXPORT_SYMBOL(find_all_nodes);
1357 /** Checks if the given "compat" string matches one of the strings in
1358 * the device's "compatible" property
1360 int device_is_compatible(struct device_node *device, const char *compat)
1365 cp = (char *) get_property(device, "compatible", &cplen);
1369 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1378 EXPORT_SYMBOL(device_is_compatible);
1382 * Indicates whether the root node has a given value in its
1383 * compatible property.
1385 int machine_is_compatible(const char *compat)
1387 struct device_node *root;
1390 root = of_find_node_by_path("/");
1392 rc = device_is_compatible(root, compat);
1397 EXPORT_SYMBOL(machine_is_compatible);
1400 * Construct and return a list of the device_nodes with a given type
1401 * and compatible property.
1403 struct device_node *find_compatible_devices(const char *type,
1406 struct device_node *head, **prevp, *np;
1409 for (np = allnodes; np != 0; np = np->allnext) {
1411 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1413 if (device_is_compatible(np, compat)) {
1421 EXPORT_SYMBOL(find_compatible_devices);
1424 * Find the device_node with a given full_name.
1426 struct device_node *find_path_device(const char *path)
1428 struct device_node *np;
1430 for (np = allnodes; np != 0; np = np->allnext)
1431 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1435 EXPORT_SYMBOL(find_path_device);
1439 * New implementation of the OF "find" APIs, return a refcounted
1440 * object, call of_node_put() when done. The device tree and list
1441 * are protected by a rw_lock.
1443 * Note that property management will need some locking as well,
1444 * this isn't dealt with yet.
1449 * of_find_node_by_name - Find a node by its "name" property
1450 * @from: The node to start searching from or NULL, the node
1451 * you pass will not be searched, only the next one
1452 * will; typically, you pass what the previous call
1453 * returned. of_node_put() will be called on it
1454 * @name: The name string to match against
1456 * Returns a node pointer with refcount incremented, use
1457 * of_node_put() on it when done.
1459 struct device_node *of_find_node_by_name(struct device_node *from,
1462 struct device_node *np;
1464 read_lock(&devtree_lock);
1465 np = from ? from->allnext : allnodes;
1466 for (; np != NULL; np = np->allnext)
1467 if (np->name != NULL && strcasecmp(np->name, name) == 0
1472 read_unlock(&devtree_lock);
1475 EXPORT_SYMBOL(of_find_node_by_name);
1478 * of_find_node_by_type - Find a node by its "device_type" property
1479 * @from: The node to start searching from or NULL, the node
1480 * you pass will not be searched, only the next one
1481 * will; typically, you pass what the previous call
1482 * returned. of_node_put() will be called on it
1483 * @name: The type string to match against
1485 * Returns a node pointer with refcount incremented, use
1486 * of_node_put() on it when done.
1488 struct device_node *of_find_node_by_type(struct device_node *from,
1491 struct device_node *np;
1493 read_lock(&devtree_lock);
1494 np = from ? from->allnext : allnodes;
1495 for (; np != 0; np = np->allnext)
1496 if (np->type != 0 && strcasecmp(np->type, type) == 0
1501 read_unlock(&devtree_lock);
1504 EXPORT_SYMBOL(of_find_node_by_type);
1507 * of_find_compatible_node - Find a node based on type and one of the
1508 * tokens in its "compatible" property
1509 * @from: The node to start searching from or NULL, the node
1510 * you pass will not be searched, only the next one
1511 * will; typically, you pass what the previous call
1512 * returned. of_node_put() will be called on it
1513 * @type: The type string to match "device_type" or NULL to ignore
1514 * @compatible: The string to match to one of the tokens in the device
1515 * "compatible" list.
1517 * Returns a node pointer with refcount incremented, use
1518 * of_node_put() on it when done.
1520 struct device_node *of_find_compatible_node(struct device_node *from,
1521 const char *type, const char *compatible)
1523 struct device_node *np;
1525 read_lock(&devtree_lock);
1526 np = from ? from->allnext : allnodes;
1527 for (; np != 0; np = np->allnext) {
1529 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1531 if (device_is_compatible(np, compatible) && of_node_get(np))
1536 read_unlock(&devtree_lock);
1539 EXPORT_SYMBOL(of_find_compatible_node);
1542 * of_find_node_by_path - Find a node matching a full OF path
1543 * @path: The full path to match
1545 * Returns a node pointer with refcount incremented, use
1546 * of_node_put() on it when done.
1548 struct device_node *of_find_node_by_path(const char *path)
1550 struct device_node *np = allnodes;
1552 read_lock(&devtree_lock);
1553 for (; np != 0; np = np->allnext) {
1554 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1558 read_unlock(&devtree_lock);
1561 EXPORT_SYMBOL(of_find_node_by_path);
1564 * of_find_node_by_phandle - Find a node given a phandle
1565 * @handle: phandle of the node to find
1567 * Returns a node pointer with refcount incremented, use
1568 * of_node_put() on it when done.
1570 struct device_node *of_find_node_by_phandle(phandle handle)
1572 struct device_node *np;
1574 read_lock(&devtree_lock);
1575 for (np = allnodes; np != 0; np = np->allnext)
1576 if (np->linux_phandle == handle)
1580 read_unlock(&devtree_lock);
1583 EXPORT_SYMBOL(of_find_node_by_phandle);
1586 * of_find_all_nodes - Get next node in global list
1587 * @prev: Previous node or NULL to start iteration
1588 * of_node_put() will be called on it
1590 * Returns a node pointer with refcount incremented, use
1591 * of_node_put() on it when done.
1593 struct device_node *of_find_all_nodes(struct device_node *prev)
1595 struct device_node *np;
1597 read_lock(&devtree_lock);
1598 np = prev ? prev->allnext : allnodes;
1599 for (; np != 0; np = np->allnext)
1600 if (of_node_get(np))
1604 read_unlock(&devtree_lock);
1607 EXPORT_SYMBOL(of_find_all_nodes);
1610 * of_get_parent - Get a node's parent if any
1611 * @node: Node to get parent
1613 * Returns a node pointer with refcount incremented, use
1614 * of_node_put() on it when done.
1616 struct device_node *of_get_parent(const struct device_node *node)
1618 struct device_node *np;
1623 read_lock(&devtree_lock);
1624 np = of_node_get(node->parent);
1625 read_unlock(&devtree_lock);
1628 EXPORT_SYMBOL(of_get_parent);
1631 * of_get_next_child - Iterate a node childs
1632 * @node: parent node
1633 * @prev: previous child of the parent node, or NULL to get first
1635 * Returns a node pointer with refcount incremented, use
1636 * of_node_put() on it when done.
1638 struct device_node *of_get_next_child(const struct device_node *node,
1639 struct device_node *prev)
1641 struct device_node *next;
1643 read_lock(&devtree_lock);
1644 next = prev ? prev->sibling : node->child;
1645 for (; next != 0; next = next->sibling)
1646 if (of_node_get(next))
1650 read_unlock(&devtree_lock);
1653 EXPORT_SYMBOL(of_get_next_child);
1656 * of_node_get - Increment refcount of a node
1657 * @node: Node to inc refcount, NULL is supported to
1658 * simplify writing of callers
1662 struct device_node *of_node_get(struct device_node *node)
1665 kref_get(&node->kref);
1668 EXPORT_SYMBOL(of_node_get);
1670 static inline struct device_node * kref_to_device_node(struct kref *kref)
1672 return container_of(kref, struct device_node, kref);
1676 * of_node_release - release a dynamically allocated node
1677 * @kref: kref element of the node to be released
1679 * In of_node_put() this function is passed to kref_put()
1680 * as the destructor.
1682 static void of_node_release(struct kref *kref)
1684 struct device_node *node = kref_to_device_node(kref);
1685 struct property *prop = node->properties;
1687 if (!OF_IS_DYNAMIC(node))
1690 struct property *next = prop->next;
1697 prop = node->deadprops;
1698 node->deadprops = NULL;
1702 kfree(node->full_name);
1708 * of_node_put - Decrement refcount of a node
1709 * @node: Node to dec refcount, NULL is supported to
1710 * simplify writing of callers
1713 void of_node_put(struct device_node *node)
1716 kref_put(&node->kref, of_node_release);
1718 EXPORT_SYMBOL(of_node_put);
1721 * Plug a device node into the tree and global list.
1723 void of_attach_node(struct device_node *np)
1725 write_lock(&devtree_lock);
1726 np->sibling = np->parent->child;
1727 np->allnext = allnodes;
1728 np->parent->child = np;
1730 write_unlock(&devtree_lock);
1734 * "Unplug" a node from the device tree. The caller must hold
1735 * a reference to the node. The memory associated with the node
1736 * is not freed until its refcount goes to zero.
1738 void of_detach_node(const struct device_node *np)
1740 struct device_node *parent;
1742 write_lock(&devtree_lock);
1744 parent = np->parent;
1747 allnodes = np->allnext;
1749 struct device_node *prev;
1750 for (prev = allnodes;
1751 prev->allnext != np;
1752 prev = prev->allnext)
1754 prev->allnext = np->allnext;
1757 if (parent->child == np)
1758 parent->child = np->sibling;
1760 struct device_node *prevsib;
1761 for (prevsib = np->parent->child;
1762 prevsib->sibling != np;
1763 prevsib = prevsib->sibling)
1765 prevsib->sibling = np->sibling;
1768 write_unlock(&devtree_lock);
1771 #ifdef CONFIG_PPC_PSERIES
1773 * Fix up the uninitialized fields in a new device node:
1774 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1776 * A lot of boot-time code is duplicated here, because functions such
1777 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1780 * This should probably be split up into smaller chunks.
1783 static int of_finish_dynamic_node(struct device_node *node)
1785 struct device_node *parent = of_get_parent(node);
1787 phandle *ibm_phandle;
1789 node->name = get_property(node, "name", NULL);
1790 node->type = get_property(node, "device_type", NULL);
1797 /* We don't support that function on PowerMac, at least
1800 if (machine_is(powermac))
1803 /* fix up new node's linux_phandle field */
1804 if ((ibm_phandle = (unsigned int *)get_property(node,
1805 "ibm,phandle", NULL)))
1806 node->linux_phandle = *ibm_phandle;
1809 of_node_put(parent);
1813 static int prom_reconfig_notifier(struct notifier_block *nb,
1814 unsigned long action, void *node)
1819 case PSERIES_RECONFIG_ADD:
1820 err = of_finish_dynamic_node(node);
1822 finish_node(node, NULL, 0);
1824 printk(KERN_ERR "finish_node returned %d\n", err);
1835 static struct notifier_block prom_reconfig_nb = {
1836 .notifier_call = prom_reconfig_notifier,
1837 .priority = 10, /* This one needs to run first */
1840 static int __init prom_reconfig_setup(void)
1842 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1844 __initcall(prom_reconfig_setup);
1847 struct property *of_find_property(struct device_node *np, const char *name,
1850 struct property *pp;
1852 read_lock(&devtree_lock);
1853 for (pp = np->properties; pp != 0; pp = pp->next)
1854 if (strcmp(pp->name, name) == 0) {
1859 read_unlock(&devtree_lock);
1865 * Find a property with a given name for a given node
1866 * and return the value.
1868 unsigned char *get_property(struct device_node *np, const char *name,
1871 struct property *pp = of_find_property(np,name,lenp);
1872 return pp ? pp->value : NULL;
1874 EXPORT_SYMBOL(get_property);
1877 * Add a property to a node
1879 int prom_add_property(struct device_node* np, struct property* prop)
1881 struct property **next;
1884 write_lock(&devtree_lock);
1885 next = &np->properties;
1887 if (strcmp(prop->name, (*next)->name) == 0) {
1888 /* duplicate ! don't insert it */
1889 write_unlock(&devtree_lock);
1892 next = &(*next)->next;
1895 write_unlock(&devtree_lock);
1897 #ifdef CONFIG_PROC_DEVICETREE
1898 /* try to add to proc as well if it was initialized */
1900 proc_device_tree_add_prop(np->pde, prop);
1901 #endif /* CONFIG_PROC_DEVICETREE */
1907 * Remove a property from a node. Note that we don't actually
1908 * remove it, since we have given out who-knows-how-many pointers
1909 * to the data using get-property. Instead we just move the property
1910 * to the "dead properties" list, so it won't be found any more.
1912 int prom_remove_property(struct device_node *np, struct property *prop)
1914 struct property **next;
1917 write_lock(&devtree_lock);
1918 next = &np->properties;
1920 if (*next == prop) {
1921 /* found the node */
1923 prop->next = np->deadprops;
1924 np->deadprops = prop;
1928 next = &(*next)->next;
1930 write_unlock(&devtree_lock);
1935 #ifdef CONFIG_PROC_DEVICETREE
1936 /* try to remove the proc node as well */
1938 proc_device_tree_remove_prop(np->pde, prop);
1939 #endif /* CONFIG_PROC_DEVICETREE */
1945 * Update a property in a node. Note that we don't actually
1946 * remove it, since we have given out who-knows-how-many pointers
1947 * to the data using get-property. Instead we just move the property
1948 * to the "dead properties" list, and add the new property to the
1951 int prom_update_property(struct device_node *np,
1952 struct property *newprop,
1953 struct property *oldprop)
1955 struct property **next;
1958 write_lock(&devtree_lock);
1959 next = &np->properties;
1961 if (*next == oldprop) {
1962 /* found the node */
1963 newprop->next = oldprop->next;
1965 oldprop->next = np->deadprops;
1966 np->deadprops = oldprop;
1970 next = &(*next)->next;
1972 write_unlock(&devtree_lock);
1977 #ifdef CONFIG_PROC_DEVICETREE
1978 /* try to add to proc as well if it was initialized */
1980 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1981 #endif /* CONFIG_PROC_DEVICETREE */
1987 /* We may have allocated the flat device tree inside the crash kernel region
1988 * in prom_init. If so we need to move it out into regular memory. */
1989 void kdump_move_device_tree(void)
1991 unsigned long start, end;
1992 struct boot_param_header *new;
1994 start = __pa((unsigned long)initial_boot_params);
1995 end = start + initial_boot_params->totalsize;
1997 if (end < crashk_res.start || start > crashk_res.end)
2000 new = (struct boot_param_header*)
2001 __va(lmb_alloc(initial_boot_params->totalsize, PAGE_SIZE));
2003 memcpy(new, initial_boot_params, initial_boot_params->totalsize);
2005 initial_boot_params = new;
2007 DBG("Flat device tree blob moved to %p\n", initial_boot_params);
2009 /* XXX should we unreserve the old DT? */
2011 #endif /* CONFIG_KEXEC */