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>
37 #include <asm/processor.h>
41 #include <asm/system.h>
43 #include <asm/pgtable.h>
45 #include <asm/iommu.h>
46 #include <asm/btext.h>
47 #include <asm/sections.h>
48 #include <asm/machdep.h>
49 #include <asm/pSeries_reconfig.h>
50 #include <asm/pci-bridge.h>
52 #include <asm/systemcfg.h>
56 #define DBG(fmt...) printk(KERN_ERR fmt)
61 struct pci_reg_property {
62 struct pci_address addr;
67 struct isa_reg_property {
74 typedef int interpret_func(struct device_node *, unsigned long *,
77 extern struct rtas_t rtas;
78 extern struct lmb lmb;
79 extern unsigned long klimit;
81 static unsigned long memory_limit;
83 static int __initdata dt_root_addr_cells;
84 static int __initdata dt_root_size_cells;
87 static int __initdata iommu_is_off;
88 int __initdata iommu_force_on;
89 extern unsigned long tce_alloc_start, tce_alloc_end;
95 static struct boot_param_header *initial_boot_params __initdata;
97 struct boot_param_header *initial_boot_params;
100 static struct device_node *allnodes = NULL;
102 /* use when traversing tree through the allnext, child, sibling,
103 * or parent members of struct device_node.
105 static DEFINE_RWLOCK(devtree_lock);
107 /* export that to outside world */
108 struct device_node *of_chosen;
110 struct device_node *dflt_interrupt_controller;
111 int num_interrupt_controllers;
117 * Wrapper for allocating memory for various data that needs to be
118 * attached to device nodes as they are processed at boot or when
119 * added to the device tree later (e.g. DLPAR). At boot there is
120 * already a region reserved so we just increment *mem_start by size;
121 * otherwise we call kmalloc.
123 static void * prom_alloc(unsigned long size, unsigned long *mem_start)
128 return kmalloc(size, GFP_KERNEL);
136 * Find the device_node with a given phandle.
138 static struct device_node * find_phandle(phandle ph)
140 struct device_node *np;
142 for (np = allnodes; np != 0; np = np->allnext)
143 if (np->linux_phandle == ph)
149 * Find the interrupt parent of a node.
151 static struct device_node * __devinit intr_parent(struct device_node *p)
155 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
158 p = find_phandle(*parp);
162 * On a powermac booted with BootX, we don't get to know the
163 * phandles for any nodes, so find_phandle will return NULL.
164 * Fortunately these machines only have one interrupt controller
165 * so there isn't in fact any ambiguity. -- paulus
167 if (num_interrupt_controllers == 1)
168 p = dflt_interrupt_controller;
173 * Find out the size of each entry of the interrupts property
176 int __devinit prom_n_intr_cells(struct device_node *np)
178 struct device_node *p;
181 for (p = np; (p = intr_parent(p)) != NULL; ) {
182 icp = (unsigned int *)
183 get_property(p, "#interrupt-cells", NULL);
186 if (get_property(p, "interrupt-controller", NULL) != NULL
187 || get_property(p, "interrupt-map", NULL) != NULL) {
188 printk("oops, node %s doesn't have #interrupt-cells\n",
194 printk("prom_n_intr_cells failed for %s\n", np->full_name);
200 * Map an interrupt from a device up to the platform interrupt
203 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
204 struct device_node *np, unsigned int *ints,
207 struct device_node *p, *ipar;
208 unsigned int *imap, *imask, *ip;
209 int i, imaplen, match;
210 int newintrc = 0, newaddrc = 0;
214 reg = (unsigned int *) get_property(np, "reg", NULL);
215 naddrc = prom_n_addr_cells(np);
218 if (get_property(p, "interrupt-controller", NULL) != NULL)
219 /* this node is an interrupt controller, stop here */
221 imap = (unsigned int *)
222 get_property(p, "interrupt-map", &imaplen);
227 imask = (unsigned int *)
228 get_property(p, "interrupt-map-mask", NULL);
230 printk("oops, %s has interrupt-map but no mask\n",
234 imaplen /= sizeof(unsigned int);
237 while (imaplen > 0 && !match) {
238 /* check the child-interrupt field */
240 for (i = 0; i < naddrc && match; ++i)
241 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
242 for (; i < naddrc + nintrc && match; ++i)
243 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
244 imap += naddrc + nintrc;
245 imaplen -= naddrc + nintrc;
246 /* grab the interrupt parent */
247 ipar = find_phandle((phandle) *imap++);
249 if (ipar == NULL && num_interrupt_controllers == 1)
250 /* cope with BootX not giving us phandles */
251 ipar = dflt_interrupt_controller;
253 printk("oops, no int parent %x in map of %s\n",
254 imap[-1], p->full_name);
257 /* find the parent's # addr and intr cells */
258 ip = (unsigned int *)
259 get_property(ipar, "#interrupt-cells", NULL);
261 printk("oops, no #interrupt-cells on %s\n",
266 ip = (unsigned int *)
267 get_property(ipar, "#address-cells", NULL);
268 newaddrc = (ip == NULL)? 0: *ip;
269 imap += newaddrc + newintrc;
270 imaplen -= newaddrc + newintrc;
273 printk("oops, error decoding int-map on %s, len=%d\n",
274 p->full_name, imaplen);
279 printk("oops, no match in %s int-map for %s\n",
280 p->full_name, np->full_name);
287 ints = imap - nintrc;
292 printk("hmmm, int tree for %s doesn't have ctrler\n",
302 static int __devinit finish_node_interrupts(struct device_node *np,
303 unsigned long *mem_start,
307 int intlen, intrcells, intrcount;
309 unsigned int *irq, virq;
310 struct device_node *ic;
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 = 1;
343 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
346 intrcells = prom_n_intr_cells(np);
347 intlen /= intrcells * sizeof(unsigned int);
349 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
357 for (i = 0; i < intlen; ++i, ints += intrcells) {
358 n = map_interrupt(&irq, &ic, np, ints, intrcells);
362 /* don't map IRQ numbers under a cascaded 8259 controller */
363 if (ic && device_is_compatible(ic, "chrp,iic")) {
364 np->intrs[intrcount].line = irq[0];
367 virq = virt_irq_create_mapping(irq[0]);
368 if (virq == NO_IRQ) {
369 printk(KERN_CRIT "Could not allocate interrupt"
370 " number for %s\n", np->full_name);
373 virq = irq_offset_up(virq);
377 np->intrs[intrcount].line = virq;
381 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
382 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
383 char *name = get_property(ic->parent, "name", NULL);
384 if (name && !strcmp(name, "u3"))
385 np->intrs[intrcount].line += 128;
386 else if (!(name && !strcmp(name, "mac-io")))
387 /* ignore other cascaded controllers, such as
392 np->intrs[intrcount].sense = 1;
394 np->intrs[intrcount].sense = irq[1];
396 printk("hmmm, got %d intr cells for %s:", n,
398 for (j = 0; j < n; ++j)
399 printk(" %d", irq[j]);
404 np->n_intrs = intrcount;
409 static int __devinit interpret_pci_props(struct device_node *np,
410 unsigned long *mem_start,
411 int naddrc, int nsizec,
414 struct address_range *adr;
415 struct pci_reg_property *pci_addrs;
418 pci_addrs = (struct pci_reg_property *)
419 get_property(np, "assigned-addresses", &l);
423 n_addrs = l / sizeof(*pci_addrs);
425 adr = prom_alloc(n_addrs * sizeof(*adr), mem_start);
433 np->n_addrs = n_addrs;
435 for (i = 0; i < n_addrs; i++) {
436 adr[i].space = pci_addrs[i].addr.a_hi;
437 adr[i].address = pci_addrs[i].addr.a_lo |
438 ((u64)pci_addrs[i].addr.a_mid << 32);
439 adr[i].size = pci_addrs[i].size_lo;
445 static int __init interpret_dbdma_props(struct device_node *np,
446 unsigned long *mem_start,
447 int naddrc, int nsizec,
450 struct reg_property32 *rp;
451 struct address_range *adr;
452 unsigned long base_address;
454 struct device_node *db;
458 for (db = np->parent; db != NULL; db = db->parent) {
459 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
460 base_address = db->addrs[0].address;
466 rp = (struct reg_property32 *) get_property(np, "reg", &l);
467 if (rp != 0 && l >= sizeof(struct reg_property32)) {
469 adr = (struct address_range *) (*mem_start);
470 while ((l -= sizeof(struct reg_property32)) >= 0) {
473 adr[i].address = rp[i].address + base_address;
474 adr[i].size = rp[i].size;
480 (*mem_start) += i * sizeof(struct address_range);
486 static int __init interpret_macio_props(struct device_node *np,
487 unsigned long *mem_start,
488 int naddrc, int nsizec,
491 struct reg_property32 *rp;
492 struct address_range *adr;
493 unsigned long base_address;
495 struct device_node *db;
499 for (db = np->parent; db != NULL; db = db->parent) {
500 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
501 base_address = db->addrs[0].address;
507 rp = (struct reg_property32 *) get_property(np, "reg", &l);
508 if (rp != 0 && l >= sizeof(struct reg_property32)) {
510 adr = (struct address_range *) (*mem_start);
511 while ((l -= sizeof(struct reg_property32)) >= 0) {
514 adr[i].address = rp[i].address + base_address;
515 adr[i].size = rp[i].size;
521 (*mem_start) += i * sizeof(struct address_range);
527 static int __init interpret_isa_props(struct device_node *np,
528 unsigned long *mem_start,
529 int naddrc, int nsizec,
532 struct isa_reg_property *rp;
533 struct address_range *adr;
536 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
537 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
539 adr = (struct address_range *) (*mem_start);
540 while ((l -= sizeof(struct isa_reg_property)) >= 0) {
542 adr[i].space = rp[i].space;
543 adr[i].address = rp[i].address;
544 adr[i].size = rp[i].size;
550 (*mem_start) += i * sizeof(struct address_range);
556 static int __init interpret_root_props(struct device_node *np,
557 unsigned long *mem_start,
558 int naddrc, int nsizec,
561 struct address_range *adr;
564 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
566 rp = (unsigned int *) get_property(np, "reg", &l);
567 if (rp != 0 && l >= rpsize) {
569 adr = (struct address_range *) (*mem_start);
570 while ((l -= rpsize) >= 0) {
573 adr[i].address = rp[naddrc - 1];
574 adr[i].size = rp[naddrc + nsizec - 1];
577 rp += naddrc + nsizec;
581 (*mem_start) += i * sizeof(struct address_range);
587 static int __devinit finish_node(struct device_node *np,
588 unsigned long *mem_start,
589 interpret_func *ifunc,
590 int naddrc, int nsizec,
593 struct device_node *child;
596 /* get the device addresses and interrupts */
598 rc = ifunc(np, mem_start, naddrc, nsizec, measure_only);
602 rc = finish_node_interrupts(np, mem_start, measure_only);
606 /* Look for #address-cells and #size-cells properties. */
607 ip = (int *) get_property(np, "#address-cells", NULL);
610 ip = (int *) get_property(np, "#size-cells", NULL);
614 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
615 ifunc = interpret_root_props;
616 else if (np->type == 0)
618 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
619 ifunc = interpret_pci_props;
620 else if (!strcmp(np->type, "dbdma"))
621 ifunc = interpret_dbdma_props;
622 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
623 ifunc = interpret_macio_props;
624 else if (!strcmp(np->type, "isa"))
625 ifunc = interpret_isa_props;
626 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
627 ifunc = interpret_root_props;
628 else if (!((ifunc == interpret_dbdma_props
629 || ifunc == interpret_macio_props)
630 && (!strcmp(np->type, "escc")
631 || !strcmp(np->type, "media-bay"))))
634 for (child = np->child; child != NULL; child = child->sibling) {
635 rc = finish_node(child, mem_start, ifunc,
636 naddrc, nsizec, measure_only);
644 static void __init scan_interrupt_controllers(void)
646 struct device_node *np;
651 for (np = allnodes; np != NULL; np = np->allnext) {
652 ic = get_property(np, "interrupt-controller", &iclen);
653 name = get_property(np, "name", NULL);
654 /* checking iclen makes sure we don't get a false
655 match on /chosen.interrupt_controller */
657 && strcmp(name, "interrupt-controller") == 0)
658 || (ic != NULL && iclen == 0
659 && strcmp(name, "AppleKiwi"))) {
661 dflt_interrupt_controller = np;
665 num_interrupt_controllers = n;
669 * finish_device_tree is called once things are running normally
670 * (i.e. with text and data mapped to the address they were linked at).
671 * It traverses the device tree and fills in some of the additional,
672 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
673 * mapping is also initialized at this point.
675 void __init finish_device_tree(void)
677 unsigned long start, end, size = 0;
679 DBG(" -> finish_device_tree\n");
682 /* Initialize virtual IRQ map */
685 scan_interrupt_controllers();
688 * Finish device-tree (pre-parsing some properties etc...)
689 * We do this in 2 passes. One with "measure_only" set, which
690 * will only measure the amount of memory needed, then we can
691 * allocate that memory, and call finish_node again. However,
692 * we must be careful as most routines will fail nowadays when
693 * prom_alloc() returns 0, so we must make sure our first pass
694 * doesn't start at 0. We pre-initialize size to 16 for that
695 * reason and then remove those additional 16 bytes
698 finish_node(allnodes, &size, NULL, 0, 0, 1);
700 end = start = (unsigned long) __va(lmb_alloc(size, 128));
701 finish_node(allnodes, &end, NULL, 0, 0, 0);
702 BUG_ON(end != start + size);
704 DBG(" <- finish_device_tree\n");
707 static inline char *find_flat_dt_string(u32 offset)
709 return ((char *)initial_boot_params) +
710 initial_boot_params->off_dt_strings + offset;
714 * This function is used to scan the flattened device-tree, it is
715 * used to extract the memory informations at boot before we can
718 static int __init scan_flat_dt(int (*it)(unsigned long node,
719 const char *uname, int depth,
723 unsigned long p = ((unsigned long)initial_boot_params) +
724 initial_boot_params->off_dt_struct;
729 u32 tag = *((u32 *)p);
733 if (tag == OF_DT_END_NODE) {
737 if (tag == OF_DT_NOP)
739 if (tag == OF_DT_END)
741 if (tag == OF_DT_PROP) {
742 u32 sz = *((u32 *)p);
744 if (initial_boot_params->version < 0x10)
745 p = _ALIGN(p, sz >= 8 ? 8 : 4);
750 if (tag != OF_DT_BEGIN_NODE) {
751 printk(KERN_WARNING "Invalid tag %x scanning flattened"
752 " device tree !\n", tag);
757 p = _ALIGN(p + strlen(pathp) + 1, 4);
758 if ((*pathp) == '/') {
760 for (lp = NULL, np = pathp; *np; np++)
766 rc = it(p, pathp, depth, data);
775 * This function can be used within scan_flattened_dt callback to get
776 * access to properties
778 static void* __init get_flat_dt_prop(unsigned long node, const char *name,
781 unsigned long p = node;
784 u32 tag = *((u32 *)p);
789 if (tag == OF_DT_NOP)
791 if (tag != OF_DT_PROP)
795 noff = *((u32 *)(p + 4));
797 if (initial_boot_params->version < 0x10)
798 p = _ALIGN(p, sz >= 8 ? 8 : 4);
800 nstr = find_flat_dt_string(noff);
802 printk(KERN_WARNING "Can't find property index"
806 if (strcmp(name, nstr) == 0) {
816 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
821 *mem = _ALIGN(*mem, align);
828 static unsigned long __init unflatten_dt_node(unsigned long mem,
830 struct device_node *dad,
831 struct device_node ***allnextpp,
832 unsigned long fpsize)
834 struct device_node *np;
835 struct property *pp, **prev_pp = NULL;
838 unsigned int l, allocl;
842 tag = *((u32 *)(*p));
843 if (tag != OF_DT_BEGIN_NODE) {
844 printk("Weird tag at start of node: %x\n", tag);
849 l = allocl = strlen(pathp) + 1;
850 *p = _ALIGN(*p + l, 4);
852 /* version 0x10 has a more compact unit name here instead of the full
853 * path. we accumulate the full path size using "fpsize", we'll rebuild
854 * it later. We detect this because the first character of the name is
857 if ((*pathp) != '/') {
860 /* root node: special case. fpsize accounts for path
861 * plus terminating zero. root node only has '/', so
862 * fpsize should be 2, but we want to avoid the first
863 * level nodes to have two '/' so we use fpsize 1 here
868 /* account for '/' and path size minus terminal 0
877 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
878 __alignof__(struct device_node));
880 memset(np, 0, sizeof(*np));
881 np->full_name = ((char*)np) + sizeof(struct device_node);
883 char *p = np->full_name;
884 /* rebuild full path for new format */
885 if (dad && dad->parent) {
886 strcpy(p, dad->full_name);
888 if ((strlen(p) + l + 1) != allocl) {
889 DBG("%s: p: %d, l: %d, a: %d\n",
890 pathp, strlen(p), l, allocl);
898 memcpy(np->full_name, pathp, l);
899 prev_pp = &np->properties;
901 *allnextpp = &np->allnext;
904 /* we temporarily use the next field as `last_child'*/
908 dad->next->sibling = np;
911 kref_init(&np->kref);
917 tag = *((u32 *)(*p));
918 if (tag == OF_DT_NOP) {
922 if (tag != OF_DT_PROP)
926 noff = *((u32 *)((*p) + 4));
928 if (initial_boot_params->version < 0x10)
929 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
931 pname = find_flat_dt_string(noff);
933 printk("Can't find property name in list !\n");
936 if (strcmp(pname, "name") == 0)
938 l = strlen(pname) + 1;
939 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
940 __alignof__(struct property));
942 if (strcmp(pname, "linux,phandle") == 0) {
943 np->node = *((u32 *)*p);
944 if (np->linux_phandle == 0)
945 np->linux_phandle = np->node;
947 if (strcmp(pname, "ibm,phandle") == 0)
948 np->linux_phandle = *((u32 *)*p);
951 pp->value = (void *)*p;
955 *p = _ALIGN((*p) + sz, 4);
957 /* with version 0x10 we may not have the name property, recreate
958 * it here from the unit name if absent
961 char *p = pathp, *ps = pathp, *pa = NULL;
974 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
975 __alignof__(struct property));
979 pp->value = (unsigned char *)(pp + 1);
982 memcpy(pp->value, ps, sz - 1);
983 ((char *)pp->value)[sz - 1] = 0;
984 DBG("fixed up name for %s -> %s\n", pathp, pp->value);
989 np->name = get_property(np, "name", NULL);
990 np->type = get_property(np, "device_type", NULL);
997 while (tag == OF_DT_BEGIN_NODE) {
998 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
999 tag = *((u32 *)(*p));
1001 if (tag != OF_DT_END_NODE) {
1002 printk("Weird tag at end of node: %x\n", tag);
1011 * unflattens the device-tree passed by the firmware, creating the
1012 * tree of struct device_node. It also fills the "name" and "type"
1013 * pointers of the nodes so the normal device-tree walking functions
1014 * can be used (this used to be done by finish_device_tree)
1016 void __init unflatten_device_tree(void)
1018 unsigned long start, mem, size;
1019 struct device_node **allnextp = &allnodes;
1023 DBG(" -> unflatten_device_tree()\n");
1025 /* First pass, scan for size */
1026 start = ((unsigned long)initial_boot_params) +
1027 initial_boot_params->off_dt_struct;
1028 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
1029 size = (size | 3) + 1;
1031 DBG(" size is %lx, allocating...\n", size);
1033 /* Allocate memory for the expanded device tree */
1034 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
1036 DBG("Couldn't allocate memory with lmb_alloc()!\n");
1037 panic("Couldn't allocate memory with lmb_alloc()!\n");
1039 mem = (unsigned long) __va(mem);
1041 ((u32 *)mem)[size / 4] = 0xdeadbeef;
1043 DBG(" unflattening %lx...\n", mem);
1045 /* Second pass, do actual unflattening */
1046 start = ((unsigned long)initial_boot_params) +
1047 initial_boot_params->off_dt_struct;
1048 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
1049 if (*((u32 *)start) != OF_DT_END)
1050 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
1051 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
1052 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
1053 ((u32 *)mem)[size / 4] );
1056 /* Get pointer to OF "/chosen" node for use everywhere */
1057 of_chosen = of_find_node_by_path("/chosen");
1058 if (of_chosen == NULL)
1059 of_chosen = of_find_node_by_path("/chosen@0");
1061 /* Retreive command line */
1062 if (of_chosen != NULL) {
1063 p = (char *)get_property(of_chosen, "bootargs", &l);
1064 if (p != NULL && l > 0)
1065 strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
1067 #ifdef CONFIG_CMDLINE
1068 if (l == 0 || (l == 1 && (*p) == 0))
1069 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1070 #endif /* CONFIG_CMDLINE */
1072 DBG("Command line is: %s\n", cmd_line);
1074 DBG(" <- unflatten_device_tree()\n");
1078 static int __init early_init_dt_scan_cpus(unsigned long node,
1079 const char *uname, int depth, void *data)
1081 char *type = get_flat_dt_prop(node, "device_type", NULL);
1083 unsigned long size = 0;
1085 /* We are scanning "cpu" nodes only */
1086 if (type == NULL || strcmp(type, "cpu") != 0)
1089 #ifdef CONFIG_PPC_PSERIES
1090 /* On LPAR, look for the first ibm,pft-size property for the hash table size
1092 if (systemcfg->platform == PLATFORM_PSERIES_LPAR && ppc64_pft_size == 0) {
1094 pft_size = get_flat_dt_prop(node, "ibm,pft-size", NULL);
1095 if (pft_size != NULL) {
1096 /* pft_size[0] is the NUMA CEC cookie */
1097 ppc64_pft_size = pft_size[1];
1103 if (initial_boot_params && initial_boot_params->version >= 2) {
1104 /* version 2 of the kexec param format adds the phys cpuid
1107 boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
1110 /* Check if it's the boot-cpu, set it's hw index in paca now */
1111 if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
1112 prop = get_flat_dt_prop(node, "reg", NULL);
1113 set_hard_smp_processor_id(0, prop == NULL ? 0 : *prop);
1114 boot_cpuid_phys = get_hard_smp_processor_id(0);
1119 #ifdef CONFIG_ALTIVEC
1120 /* Check if we have a VMX and eventually update CPU features */
1121 prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", &size);
1122 if (prop && (*prop) > 0) {
1123 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1124 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1127 /* Same goes for Apple's "altivec" property */
1128 prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL);
1130 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1131 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1133 #endif /* CONFIG_ALTIVEC */
1135 #ifdef CONFIG_PPC_PSERIES
1137 * Check for an SMT capable CPU and set the CPU feature. We do
1138 * this by looking at the size of the ibm,ppc-interrupt-server#s
1141 prop = (u32 *)get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
1143 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
1144 if (prop && ((size / sizeof(u32)) > 1))
1145 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
1151 static int __init early_init_dt_scan_chosen(unsigned long node,
1152 const char *uname, int depth, void *data)
1155 unsigned long *lprop;
1157 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1160 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
1163 /* get platform type */
1164 prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
1168 systemcfg->platform = *prop;
1174 /* check if iommu is forced on or off */
1175 if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
1177 if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
1181 lprop = get_flat_dt_prop(node, "linux,memory-limit", NULL);
1183 memory_limit = *lprop;
1186 lprop = get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
1188 tce_alloc_start = *lprop;
1189 lprop = get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
1191 tce_alloc_end = *lprop;
1194 #ifdef CONFIG_PPC_RTAS
1195 /* To help early debugging via the front panel, we retreive a minimal
1196 * set of RTAS infos now if available
1199 u64 *basep, *entryp;
1201 basep = get_flat_dt_prop(node, "linux,rtas-base", NULL);
1202 entryp = get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1203 prop = get_flat_dt_prop(node, "linux,rtas-size", NULL);
1204 if (basep && entryp && prop) {
1206 rtas.entry = *entryp;
1210 #endif /* CONFIG_PPC_RTAS */
1216 static int __init early_init_dt_scan_root(unsigned long node,
1217 const char *uname, int depth, void *data)
1224 prop = get_flat_dt_prop(node, "#size-cells", NULL);
1225 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1226 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
1228 prop = get_flat_dt_prop(node, "#address-cells", NULL);
1229 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1230 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1236 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1241 /* Ignore more than 2 cells */
1242 while (s > sizeof(unsigned long) / 4) {
1260 static int __init early_init_dt_scan_memory(unsigned long node,
1261 const char *uname, int depth, void *data)
1263 char *type = get_flat_dt_prop(node, "device_type", NULL);
1267 /* We are scanning "memory" nodes only */
1268 if (type == NULL || strcmp(type, "memory") != 0)
1271 reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
1275 endp = reg + (l / sizeof(cell_t));
1277 DBG("memory scan node %s ..., reg size %ld, data: %x %x %x %x, ...\n",
1278 uname, l, reg[0], reg[1], reg[2], reg[3]);
1280 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1281 unsigned long base, size;
1283 base = dt_mem_next_cell(dt_root_addr_cells, ®);
1284 size = dt_mem_next_cell(dt_root_size_cells, ®);
1288 DBG(" - %lx , %lx\n", base, size);
1291 if (base >= 0x80000000ul)
1293 if ((base + size) > 0x80000000ul)
1294 size = 0x80000000ul - base;
1297 lmb_add(base, size);
1302 static void __init early_reserve_mem(void)
1304 unsigned long base, size;
1305 unsigned long *reserve_map;
1307 reserve_map = (unsigned long *)(((unsigned long)initial_boot_params) +
1308 initial_boot_params->off_mem_rsvmap);
1310 base = *(reserve_map++);
1311 size = *(reserve_map++);
1314 DBG("reserving: %lx -> %lx\n", base, size);
1315 lmb_reserve(base, size);
1319 DBG("memory reserved, lmbs :\n");
1324 void __init early_init_devtree(void *params)
1326 DBG(" -> early_init_devtree()\n");
1328 /* Setup flat device-tree pointer */
1329 initial_boot_params = params;
1331 /* Retrieve various informations from the /chosen node of the
1332 * device-tree, including the platform type, initrd location and
1333 * size, TCE reserve, and more ...
1335 scan_flat_dt(early_init_dt_scan_chosen, NULL);
1337 /* Scan memory nodes and rebuild LMBs */
1339 scan_flat_dt(early_init_dt_scan_root, NULL);
1340 scan_flat_dt(early_init_dt_scan_memory, NULL);
1341 lmb_enforce_memory_limit(memory_limit);
1344 systemcfg->physicalMemorySize = lmb_phys_mem_size();
1346 lmb_reserve(0, __pa(klimit));
1348 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1350 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1351 early_reserve_mem();
1353 DBG("Scanning CPUs ...\n");
1355 /* Retreive hash table size from flattened tree plus other
1356 * CPU related informations (altivec support, boot CPU ID, ...)
1358 scan_flat_dt(early_init_dt_scan_cpus, NULL);
1360 DBG(" <- early_init_devtree()\n");
1366 prom_n_addr_cells(struct device_node* np)
1372 ip = (int *) get_property(np, "#address-cells", NULL);
1375 } while (np->parent);
1376 /* No #address-cells property for the root node, default to 1 */
1381 prom_n_size_cells(struct device_node* np)
1387 ip = (int *) get_property(np, "#size-cells", NULL);
1390 } while (np->parent);
1391 /* No #size-cells property for the root node, default to 1 */
1396 * Work out the sense (active-low level / active-high edge)
1397 * of each interrupt from the device tree.
1399 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1401 struct device_node *np;
1404 /* default to level-triggered */
1405 memset(senses, 1, max - off);
1407 for (np = allnodes; np != 0; np = np->allnext) {
1408 for (j = 0; j < np->n_intrs; j++) {
1409 i = np->intrs[j].line;
1410 if (i >= off && i < max)
1411 senses[i-off] = np->intrs[j].sense ?
1412 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE :
1413 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE;
1419 * Construct and return a list of the device_nodes with a given name.
1421 struct device_node *find_devices(const char *name)
1423 struct device_node *head, **prevp, *np;
1426 for (np = allnodes; np != 0; np = np->allnext) {
1427 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1435 EXPORT_SYMBOL(find_devices);
1438 * Construct and return a list of the device_nodes with a given type.
1440 struct device_node *find_type_devices(const char *type)
1442 struct device_node *head, **prevp, *np;
1445 for (np = allnodes; np != 0; np = np->allnext) {
1446 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1454 EXPORT_SYMBOL(find_type_devices);
1457 * Returns all nodes linked together
1459 struct device_node *find_all_nodes(void)
1461 struct device_node *head, **prevp, *np;
1464 for (np = allnodes; np != 0; np = np->allnext) {
1471 EXPORT_SYMBOL(find_all_nodes);
1473 /** Checks if the given "compat" string matches one of the strings in
1474 * the device's "compatible" property
1476 int device_is_compatible(struct device_node *device, const char *compat)
1481 cp = (char *) get_property(device, "compatible", &cplen);
1485 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1494 EXPORT_SYMBOL(device_is_compatible);
1498 * Indicates whether the root node has a given value in its
1499 * compatible property.
1501 int machine_is_compatible(const char *compat)
1503 struct device_node *root;
1506 root = of_find_node_by_path("/");
1508 rc = device_is_compatible(root, compat);
1513 EXPORT_SYMBOL(machine_is_compatible);
1516 * Construct and return a list of the device_nodes with a given type
1517 * and compatible property.
1519 struct device_node *find_compatible_devices(const char *type,
1522 struct device_node *head, **prevp, *np;
1525 for (np = allnodes; np != 0; np = np->allnext) {
1527 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1529 if (device_is_compatible(np, compat)) {
1537 EXPORT_SYMBOL(find_compatible_devices);
1540 * Find the device_node with a given full_name.
1542 struct device_node *find_path_device(const char *path)
1544 struct device_node *np;
1546 for (np = allnodes; np != 0; np = np->allnext)
1547 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1551 EXPORT_SYMBOL(find_path_device);
1555 * New implementation of the OF "find" APIs, return a refcounted
1556 * object, call of_node_put() when done. The device tree and list
1557 * are protected by a rw_lock.
1559 * Note that property management will need some locking as well,
1560 * this isn't dealt with yet.
1565 * of_find_node_by_name - Find a node by its "name" property
1566 * @from: The node to start searching from or NULL, the node
1567 * you pass will not be searched, only the next one
1568 * will; typically, you pass what the previous call
1569 * returned. of_node_put() will be called on it
1570 * @name: The name string to match against
1572 * Returns a node pointer with refcount incremented, use
1573 * of_node_put() on it when done.
1575 struct device_node *of_find_node_by_name(struct device_node *from,
1578 struct device_node *np;
1580 read_lock(&devtree_lock);
1581 np = from ? from->allnext : allnodes;
1582 for (; np != 0; np = np->allnext)
1583 if (np->name != 0 && strcasecmp(np->name, name) == 0
1588 read_unlock(&devtree_lock);
1591 EXPORT_SYMBOL(of_find_node_by_name);
1594 * of_find_node_by_type - Find a node by its "device_type" property
1595 * @from: The node to start searching from or NULL, the node
1596 * you pass will not be searched, only the next one
1597 * will; typically, you pass what the previous call
1598 * returned. of_node_put() will be called on it
1599 * @name: The type string to match against
1601 * Returns a node pointer with refcount incremented, use
1602 * of_node_put() on it when done.
1604 struct device_node *of_find_node_by_type(struct device_node *from,
1607 struct device_node *np;
1609 read_lock(&devtree_lock);
1610 np = from ? from->allnext : allnodes;
1611 for (; np != 0; np = np->allnext)
1612 if (np->type != 0 && strcasecmp(np->type, type) == 0
1617 read_unlock(&devtree_lock);
1620 EXPORT_SYMBOL(of_find_node_by_type);
1623 * of_find_compatible_node - Find a node based on type and one of the
1624 * tokens in its "compatible" property
1625 * @from: The node to start searching from or NULL, the node
1626 * you pass will not be searched, only the next one
1627 * will; typically, you pass what the previous call
1628 * returned. of_node_put() will be called on it
1629 * @type: The type string to match "device_type" or NULL to ignore
1630 * @compatible: The string to match to one of the tokens in the device
1631 * "compatible" list.
1633 * Returns a node pointer with refcount incremented, use
1634 * of_node_put() on it when done.
1636 struct device_node *of_find_compatible_node(struct device_node *from,
1637 const char *type, const char *compatible)
1639 struct device_node *np;
1641 read_lock(&devtree_lock);
1642 np = from ? from->allnext : allnodes;
1643 for (; np != 0; np = np->allnext) {
1645 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1647 if (device_is_compatible(np, compatible) && of_node_get(np))
1652 read_unlock(&devtree_lock);
1655 EXPORT_SYMBOL(of_find_compatible_node);
1658 * of_find_node_by_path - Find a node matching a full OF path
1659 * @path: The full path to match
1661 * Returns a node pointer with refcount incremented, use
1662 * of_node_put() on it when done.
1664 struct device_node *of_find_node_by_path(const char *path)
1666 struct device_node *np = allnodes;
1668 read_lock(&devtree_lock);
1669 for (; np != 0; np = np->allnext) {
1670 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1674 read_unlock(&devtree_lock);
1677 EXPORT_SYMBOL(of_find_node_by_path);
1680 * of_find_node_by_phandle - Find a node given a phandle
1681 * @handle: phandle of the node to find
1683 * Returns a node pointer with refcount incremented, use
1684 * of_node_put() on it when done.
1686 struct device_node *of_find_node_by_phandle(phandle handle)
1688 struct device_node *np;
1690 read_lock(&devtree_lock);
1691 for (np = allnodes; np != 0; np = np->allnext)
1692 if (np->linux_phandle == handle)
1696 read_unlock(&devtree_lock);
1699 EXPORT_SYMBOL(of_find_node_by_phandle);
1702 * of_find_all_nodes - Get next node in global list
1703 * @prev: Previous node or NULL to start iteration
1704 * of_node_put() will be called on it
1706 * Returns a node pointer with refcount incremented, use
1707 * of_node_put() on it when done.
1709 struct device_node *of_find_all_nodes(struct device_node *prev)
1711 struct device_node *np;
1713 read_lock(&devtree_lock);
1714 np = prev ? prev->allnext : allnodes;
1715 for (; np != 0; np = np->allnext)
1716 if (of_node_get(np))
1720 read_unlock(&devtree_lock);
1723 EXPORT_SYMBOL(of_find_all_nodes);
1726 * of_get_parent - Get a node's parent if any
1727 * @node: Node to get parent
1729 * Returns a node pointer with refcount incremented, use
1730 * of_node_put() on it when done.
1732 struct device_node *of_get_parent(const struct device_node *node)
1734 struct device_node *np;
1739 read_lock(&devtree_lock);
1740 np = of_node_get(node->parent);
1741 read_unlock(&devtree_lock);
1744 EXPORT_SYMBOL(of_get_parent);
1747 * of_get_next_child - Iterate a node childs
1748 * @node: parent node
1749 * @prev: previous child of the parent node, or NULL to get first
1751 * Returns a node pointer with refcount incremented, use
1752 * of_node_put() on it when done.
1754 struct device_node *of_get_next_child(const struct device_node *node,
1755 struct device_node *prev)
1757 struct device_node *next;
1759 read_lock(&devtree_lock);
1760 next = prev ? prev->sibling : node->child;
1761 for (; next != 0; next = next->sibling)
1762 if (of_node_get(next))
1766 read_unlock(&devtree_lock);
1769 EXPORT_SYMBOL(of_get_next_child);
1772 * of_node_get - Increment refcount of a node
1773 * @node: Node to inc refcount, NULL is supported to
1774 * simplify writing of callers
1778 struct device_node *of_node_get(struct device_node *node)
1781 kref_get(&node->kref);
1784 EXPORT_SYMBOL(of_node_get);
1786 static inline struct device_node * kref_to_device_node(struct kref *kref)
1788 return container_of(kref, struct device_node, kref);
1792 * of_node_release - release a dynamically allocated node
1793 * @kref: kref element of the node to be released
1795 * In of_node_put() this function is passed to kref_put()
1796 * as the destructor.
1798 static void of_node_release(struct kref *kref)
1800 struct device_node *node = kref_to_device_node(kref);
1801 struct property *prop = node->properties;
1803 if (!OF_IS_DYNAMIC(node))
1806 struct property *next = prop->next;
1814 kfree(node->full_name);
1820 * of_node_put - Decrement refcount of a node
1821 * @node: Node to dec refcount, NULL is supported to
1822 * simplify writing of callers
1825 void of_node_put(struct device_node *node)
1828 kref_put(&node->kref, of_node_release);
1830 EXPORT_SYMBOL(of_node_put);
1833 * Plug a device node into the tree and global list.
1835 void of_attach_node(struct device_node *np)
1837 write_lock(&devtree_lock);
1838 np->sibling = np->parent->child;
1839 np->allnext = allnodes;
1840 np->parent->child = np;
1842 write_unlock(&devtree_lock);
1846 * "Unplug" a node from the device tree. The caller must hold
1847 * a reference to the node. The memory associated with the node
1848 * is not freed until its refcount goes to zero.
1850 void of_detach_node(const struct device_node *np)
1852 struct device_node *parent;
1854 write_lock(&devtree_lock);
1856 parent = np->parent;
1859 allnodes = np->allnext;
1861 struct device_node *prev;
1862 for (prev = allnodes;
1863 prev->allnext != np;
1864 prev = prev->allnext)
1866 prev->allnext = np->allnext;
1869 if (parent->child == np)
1870 parent->child = np->sibling;
1872 struct device_node *prevsib;
1873 for (prevsib = np->parent->child;
1874 prevsib->sibling != np;
1875 prevsib = prevsib->sibling)
1877 prevsib->sibling = np->sibling;
1880 write_unlock(&devtree_lock);
1883 #ifdef CONFIG_PPC_PSERIES
1885 * Fix up the uninitialized fields in a new device node:
1886 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1888 * A lot of boot-time code is duplicated here, because functions such
1889 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1892 * This should probably be split up into smaller chunks.
1895 static int of_finish_dynamic_node(struct device_node *node,
1896 unsigned long *unused1, int unused2,
1897 int unused3, int unused4)
1899 struct device_node *parent = of_get_parent(node);
1901 phandle *ibm_phandle;
1903 node->name = get_property(node, "name", NULL);
1904 node->type = get_property(node, "device_type", NULL);
1911 /* We don't support that function on PowerMac, at least
1914 if (systemcfg->platform == PLATFORM_POWERMAC)
1917 /* fix up new node's linux_phandle field */
1918 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
1919 node->linux_phandle = *ibm_phandle;
1922 of_node_put(parent);
1926 static int prom_reconfig_notifier(struct notifier_block *nb,
1927 unsigned long action, void *node)
1932 case PSERIES_RECONFIG_ADD:
1933 err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
1935 printk(KERN_ERR "finish_node returned %d\n", err);
1946 static struct notifier_block prom_reconfig_nb = {
1947 .notifier_call = prom_reconfig_notifier,
1948 .priority = 10, /* This one needs to run first */
1951 static int __init prom_reconfig_setup(void)
1953 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1955 __initcall(prom_reconfig_setup);
1959 * Find a property with a given name for a given node
1960 * and return the value.
1962 unsigned char *get_property(struct device_node *np, const char *name,
1965 struct property *pp;
1967 for (pp = np->properties; pp != 0; pp = pp->next)
1968 if (strcmp(pp->name, name) == 0) {
1975 EXPORT_SYMBOL(get_property);
1978 * Add a property to a node
1980 void prom_add_property(struct device_node* np, struct property* prop)
1982 struct property **next = &np->properties;
1986 next = &(*next)->next;
1990 /* I quickly hacked that one, check against spec ! */
1991 static inline unsigned long
1992 bus_space_to_resource_flags(unsigned int bus_space)
1994 u8 space = (bus_space >> 24) & 0xf;
1998 return IORESOURCE_MEM;
1999 else if (space == 0x01)
2000 return IORESOURCE_IO;
2002 printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n",
2008 static struct resource *find_parent_pci_resource(struct pci_dev* pdev,
2009 struct address_range *range)
2014 /* Check this one */
2015 mask = bus_space_to_resource_flags(range->space);
2016 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
2017 if ((pdev->resource[i].flags & mask) == mask &&
2018 pdev->resource[i].start <= range->address &&
2019 pdev->resource[i].end > range->address) {
2020 if ((range->address + range->size - 1) > pdev->resource[i].end) {
2021 /* Add better message */
2022 printk(KERN_WARNING "PCI/OF resource overlap !\n");
2028 if (i == DEVICE_COUNT_RESOURCE)
2030 return &pdev->resource[i];
2034 * Request an OF device resource. Currently handles child of PCI devices,
2035 * or other nodes attached to the root node. Ultimately, put some
2036 * link to resources in the OF node.
2038 struct resource *request_OF_resource(struct device_node* node, int index,
2039 const char* name_postfix)
2041 struct pci_dev* pcidev;
2042 u8 pci_bus, pci_devfn;
2043 unsigned long iomask;
2044 struct device_node* nd;
2045 struct resource* parent;
2046 struct resource *res = NULL;
2049 if (index >= node->n_addrs)
2052 /* Sanity check on bus space */
2053 iomask = bus_space_to_resource_flags(node->addrs[index].space);
2054 if (iomask & IORESOURCE_MEM)
2055 parent = &iomem_resource;
2056 else if (iomask & IORESOURCE_IO)
2057 parent = &ioport_resource;
2061 /* Find a PCI parent if any */
2065 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
2066 pcidev = pci_find_slot(pci_bus, pci_devfn);
2071 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
2073 printk(KERN_WARNING "request_OF_resource(%s), parent not found\n",
2078 res = __request_region(parent, node->addrs[index].address,
2079 node->addrs[index].size, NULL);
2082 nlen = strlen(node->name);
2083 plen = name_postfix ? strlen(name_postfix) : 0;
2084 res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL);
2086 strcpy((char *)res->name, node->name);
2088 strcpy((char *)res->name+nlen, name_postfix);
2094 EXPORT_SYMBOL(request_OF_resource);
2096 int release_OF_resource(struct device_node *node, int index)
2098 struct pci_dev* pcidev;
2099 u8 pci_bus, pci_devfn;
2100 unsigned long iomask, start, end;
2101 struct device_node* nd;
2102 struct resource* parent;
2103 struct resource *res = NULL;
2105 if (index >= node->n_addrs)
2108 /* Sanity check on bus space */
2109 iomask = bus_space_to_resource_flags(node->addrs[index].space);
2110 if (iomask & IORESOURCE_MEM)
2111 parent = &iomem_resource;
2112 else if (iomask & IORESOURCE_IO)
2113 parent = &ioport_resource;
2117 /* Find a PCI parent if any */
2121 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
2122 pcidev = pci_find_slot(pci_bus, pci_devfn);
2127 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
2129 printk(KERN_WARNING "release_OF_resource(%s), parent not found\n",
2134 /* Find us in the parent and its childs */
2135 res = parent->child;
2136 start = node->addrs[index].address;
2137 end = start + node->addrs[index].size - 1;
2139 if (res->start == start && res->end == end &&
2140 (res->flags & IORESOURCE_BUSY))
2142 if (res->start <= start && res->end >= end)
2154 release_resource(res);
2159 EXPORT_SYMBOL(release_OF_resource);