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>
53 #define DBG(fmt...) printk(KERN_ERR fmt)
58 struct pci_reg_property {
59 struct pci_address addr;
64 struct isa_reg_property {
71 typedef int interpret_func(struct device_node *, unsigned long *,
74 static int __initdata dt_root_addr_cells;
75 static int __initdata dt_root_size_cells;
78 static int __initdata iommu_is_off;
79 int __initdata iommu_force_on;
80 unsigned long tce_alloc_start, tce_alloc_end;
86 static struct boot_param_header *initial_boot_params __initdata;
88 struct boot_param_header *initial_boot_params;
91 static struct device_node *allnodes = NULL;
93 /* use when traversing tree through the allnext, child, sibling,
94 * or parent members of struct device_node.
96 static DEFINE_RWLOCK(devtree_lock);
98 /* export that to outside world */
99 struct device_node *of_chosen;
101 struct device_node *dflt_interrupt_controller;
102 int num_interrupt_controllers;
105 * Wrapper for allocating memory for various data that needs to be
106 * attached to device nodes as they are processed at boot or when
107 * added to the device tree later (e.g. DLPAR). At boot there is
108 * already a region reserved so we just increment *mem_start by size;
109 * otherwise we call kmalloc.
111 static void * prom_alloc(unsigned long size, unsigned long *mem_start)
116 return kmalloc(size, GFP_KERNEL);
124 * Find the device_node with a given phandle.
126 static struct device_node * find_phandle(phandle ph)
128 struct device_node *np;
130 for (np = allnodes; np != 0; np = np->allnext)
131 if (np->linux_phandle == ph)
137 * Find the interrupt parent of a node.
139 static struct device_node * __devinit intr_parent(struct device_node *p)
143 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
146 p = find_phandle(*parp);
150 * On a powermac booted with BootX, we don't get to know the
151 * phandles for any nodes, so find_phandle will return NULL.
152 * Fortunately these machines only have one interrupt controller
153 * so there isn't in fact any ambiguity. -- paulus
155 if (num_interrupt_controllers == 1)
156 p = dflt_interrupt_controller;
161 * Find out the size of each entry of the interrupts property
164 int __devinit prom_n_intr_cells(struct device_node *np)
166 struct device_node *p;
169 for (p = np; (p = intr_parent(p)) != NULL; ) {
170 icp = (unsigned int *)
171 get_property(p, "#interrupt-cells", NULL);
174 if (get_property(p, "interrupt-controller", NULL) != NULL
175 || get_property(p, "interrupt-map", NULL) != NULL) {
176 printk("oops, node %s doesn't have #interrupt-cells\n",
182 printk("prom_n_intr_cells failed for %s\n", np->full_name);
188 * Map an interrupt from a device up to the platform interrupt
191 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
192 struct device_node *np, unsigned int *ints,
195 struct device_node *p, *ipar;
196 unsigned int *imap, *imask, *ip;
197 int i, imaplen, match;
198 int newintrc = 0, newaddrc = 0;
202 reg = (unsigned int *) get_property(np, "reg", NULL);
203 naddrc = prom_n_addr_cells(np);
206 if (get_property(p, "interrupt-controller", NULL) != NULL)
207 /* this node is an interrupt controller, stop here */
209 imap = (unsigned int *)
210 get_property(p, "interrupt-map", &imaplen);
215 imask = (unsigned int *)
216 get_property(p, "interrupt-map-mask", NULL);
218 printk("oops, %s has interrupt-map but no mask\n",
222 imaplen /= sizeof(unsigned int);
225 while (imaplen > 0 && !match) {
226 /* check the child-interrupt field */
228 for (i = 0; i < naddrc && match; ++i)
229 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
230 for (; i < naddrc + nintrc && match; ++i)
231 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
232 imap += naddrc + nintrc;
233 imaplen -= naddrc + nintrc;
234 /* grab the interrupt parent */
235 ipar = find_phandle((phandle) *imap++);
237 if (ipar == NULL && num_interrupt_controllers == 1)
238 /* cope with BootX not giving us phandles */
239 ipar = dflt_interrupt_controller;
241 printk("oops, no int parent %x in map of %s\n",
242 imap[-1], p->full_name);
245 /* find the parent's # addr and intr cells */
246 ip = (unsigned int *)
247 get_property(ipar, "#interrupt-cells", NULL);
249 printk("oops, no #interrupt-cells on %s\n",
254 ip = (unsigned int *)
255 get_property(ipar, "#address-cells", NULL);
256 newaddrc = (ip == NULL)? 0: *ip;
257 imap += newaddrc + newintrc;
258 imaplen -= newaddrc + newintrc;
261 printk("oops, error decoding int-map on %s, len=%d\n",
262 p->full_name, imaplen);
267 printk("oops, no match in %s int-map for %s\n",
268 p->full_name, np->full_name);
275 ints = imap - nintrc;
280 printk("hmmm, int tree for %s doesn't have ctrler\n",
290 static unsigned char map_isa_senses[4] = {
291 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
292 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
293 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
294 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE
297 static unsigned char map_mpic_senses[4] = {
298 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE,
299 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
300 /* 2 seems to be used for the 8259 cascade... */
301 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
302 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
305 static int __devinit finish_node_interrupts(struct device_node *np,
306 unsigned long *mem_start,
310 int intlen, intrcells, intrcount;
312 unsigned int *irq, virq;
313 struct device_node *ic;
315 if (num_interrupt_controllers == 0) {
317 * Old machines just have a list of interrupt numbers
318 * and no interrupt-controller nodes.
320 ints = (unsigned int *) get_property(np, "AAPL,interrupts",
322 /* XXX old interpret_pci_props looked in parent too */
323 /* XXX old interpret_macio_props looked for interrupts
324 before AAPL,interrupts */
326 ints = (unsigned int *) get_property(np, "interrupts",
331 np->n_intrs = intlen / sizeof(unsigned int);
332 np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]),
339 for (i = 0; i < np->n_intrs; ++i) {
340 np->intrs[i].line = *ints++;
341 np->intrs[i].sense = IRQ_SENSE_LEVEL
342 | IRQ_POLARITY_NEGATIVE;
347 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
350 intrcells = prom_n_intr_cells(np);
351 intlen /= intrcells * sizeof(unsigned int);
353 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
361 for (i = 0; i < intlen; ++i, ints += intrcells) {
362 n = map_interrupt(&irq, &ic, np, ints, intrcells);
366 /* don't map IRQ numbers under a cascaded 8259 controller */
367 if (ic && device_is_compatible(ic, "chrp,iic")) {
368 np->intrs[intrcount].line = irq[0];
369 sense = (n > 1)? (irq[1] & 3): 3;
370 np->intrs[intrcount].sense = map_isa_senses[sense];
372 virq = virt_irq_create_mapping(irq[0]);
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;
382 np->intrs[intrcount].sense = map_mpic_senses[sense];
386 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
387 if (_machine == PLATFORM_POWERMAC && ic && ic->parent) {
388 char *name = get_property(ic->parent, "name", NULL);
389 if (name && !strcmp(name, "u3"))
390 np->intrs[intrcount].line += 128;
391 else if (!(name && !strcmp(name, "mac-io")))
392 /* ignore other cascaded controllers, such as
398 printk("hmmm, got %d intr cells for %s:", n,
400 for (j = 0; j < n; ++j)
401 printk(" %d", irq[j]);
406 np->n_intrs = intrcount;
411 static int __devinit interpret_pci_props(struct device_node *np,
412 unsigned long *mem_start,
413 int naddrc, int nsizec,
416 struct address_range *adr;
417 struct pci_reg_property *pci_addrs;
420 pci_addrs = (struct pci_reg_property *)
421 get_property(np, "assigned-addresses", &l);
425 n_addrs = l / sizeof(*pci_addrs);
427 adr = prom_alloc(n_addrs * sizeof(*adr), mem_start);
435 np->n_addrs = n_addrs;
437 for (i = 0; i < n_addrs; i++) {
438 adr[i].space = pci_addrs[i].addr.a_hi;
439 adr[i].address = pci_addrs[i].addr.a_lo |
440 ((u64)pci_addrs[i].addr.a_mid << 32);
441 adr[i].size = pci_addrs[i].size_lo;
447 static int __init interpret_dbdma_props(struct device_node *np,
448 unsigned long *mem_start,
449 int naddrc, int nsizec,
452 struct reg_property32 *rp;
453 struct address_range *adr;
454 unsigned long base_address;
456 struct device_node *db;
460 for (db = np->parent; db != NULL; db = db->parent) {
461 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
462 base_address = db->addrs[0].address;
468 rp = (struct reg_property32 *) get_property(np, "reg", &l);
469 if (rp != 0 && l >= sizeof(struct reg_property32)) {
471 adr = (struct address_range *) (*mem_start);
472 while ((l -= sizeof(struct reg_property32)) >= 0) {
475 adr[i].address = rp[i].address + base_address;
476 adr[i].size = rp[i].size;
482 (*mem_start) += i * sizeof(struct address_range);
488 static int __init interpret_macio_props(struct device_node *np,
489 unsigned long *mem_start,
490 int naddrc, int nsizec,
493 struct reg_property32 *rp;
494 struct address_range *adr;
495 unsigned long base_address;
497 struct device_node *db;
501 for (db = np->parent; db != NULL; db = db->parent) {
502 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
503 base_address = db->addrs[0].address;
509 rp = (struct reg_property32 *) get_property(np, "reg", &l);
510 if (rp != 0 && l >= sizeof(struct reg_property32)) {
512 adr = (struct address_range *) (*mem_start);
513 while ((l -= sizeof(struct reg_property32)) >= 0) {
516 adr[i].address = rp[i].address + base_address;
517 adr[i].size = rp[i].size;
523 (*mem_start) += i * sizeof(struct address_range);
529 static int __init interpret_isa_props(struct device_node *np,
530 unsigned long *mem_start,
531 int naddrc, int nsizec,
534 struct isa_reg_property *rp;
535 struct address_range *adr;
538 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
539 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
541 adr = (struct address_range *) (*mem_start);
542 while ((l -= sizeof(struct isa_reg_property)) >= 0) {
544 adr[i].space = rp[i].space;
545 adr[i].address = rp[i].address;
546 adr[i].size = rp[i].size;
552 (*mem_start) += i * sizeof(struct address_range);
558 static int __init interpret_root_props(struct device_node *np,
559 unsigned long *mem_start,
560 int naddrc, int nsizec,
563 struct address_range *adr;
566 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
568 rp = (unsigned int *) get_property(np, "reg", &l);
569 if (rp != 0 && l >= rpsize) {
571 adr = (struct address_range *) (*mem_start);
572 while ((l -= rpsize) >= 0) {
575 adr[i].address = rp[naddrc - 1];
576 adr[i].size = rp[naddrc + nsizec - 1];
579 rp += naddrc + nsizec;
583 (*mem_start) += i * sizeof(struct address_range);
589 static int __devinit finish_node(struct device_node *np,
590 unsigned long *mem_start,
591 interpret_func *ifunc,
592 int naddrc, int nsizec,
595 struct device_node *child;
598 /* get the device addresses and interrupts */
600 rc = ifunc(np, mem_start, naddrc, nsizec, measure_only);
604 rc = finish_node_interrupts(np, mem_start, measure_only);
608 /* Look for #address-cells and #size-cells properties. */
609 ip = (int *) get_property(np, "#address-cells", NULL);
612 ip = (int *) get_property(np, "#size-cells", NULL);
616 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
617 ifunc = interpret_root_props;
618 else if (np->type == 0)
620 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
621 ifunc = interpret_pci_props;
622 else if (!strcmp(np->type, "dbdma"))
623 ifunc = interpret_dbdma_props;
624 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
625 ifunc = interpret_macio_props;
626 else if (!strcmp(np->type, "isa"))
627 ifunc = interpret_isa_props;
628 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
629 ifunc = interpret_root_props;
630 else if (!((ifunc == interpret_dbdma_props
631 || ifunc == interpret_macio_props)
632 && (!strcmp(np->type, "escc")
633 || !strcmp(np->type, "media-bay"))))
636 for (child = np->child; child != NULL; child = child->sibling) {
637 rc = finish_node(child, mem_start, ifunc,
638 naddrc, nsizec, measure_only);
646 static void __init scan_interrupt_controllers(void)
648 struct device_node *np;
653 for (np = allnodes; np != NULL; np = np->allnext) {
654 ic = get_property(np, "interrupt-controller", &iclen);
655 name = get_property(np, "name", NULL);
656 /* checking iclen makes sure we don't get a false
657 match on /chosen.interrupt_controller */
659 && strcmp(name, "interrupt-controller") == 0)
660 || (ic != NULL && iclen == 0
661 && strcmp(name, "AppleKiwi"))) {
663 dflt_interrupt_controller = np;
667 num_interrupt_controllers = n;
671 * finish_device_tree is called once things are running normally
672 * (i.e. with text and data mapped to the address they were linked at).
673 * It traverses the device tree and fills in some of the additional,
674 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
675 * mapping is also initialized at this point.
677 void __init finish_device_tree(void)
679 unsigned long start, end, size = 0;
681 DBG(" -> finish_device_tree\n");
684 /* Initialize virtual IRQ map */
687 scan_interrupt_controllers();
690 * Finish device-tree (pre-parsing some properties etc...)
691 * We do this in 2 passes. One with "measure_only" set, which
692 * will only measure the amount of memory needed, then we can
693 * allocate that memory, and call finish_node again. However,
694 * we must be careful as most routines will fail nowadays when
695 * prom_alloc() returns 0, so we must make sure our first pass
696 * doesn't start at 0. We pre-initialize size to 16 for that
697 * reason and then remove those additional 16 bytes
700 finish_node(allnodes, &size, NULL, 0, 0, 1);
702 end = start = (unsigned long) __va(lmb_alloc(size, 128));
703 finish_node(allnodes, &end, NULL, 0, 0, 0);
704 BUG_ON(end != start + size);
706 DBG(" <- finish_device_tree\n");
709 static inline char *find_flat_dt_string(u32 offset)
711 return ((char *)initial_boot_params) +
712 initial_boot_params->off_dt_strings + offset;
716 * This function is used to scan the flattened device-tree, it is
717 * used to extract the memory informations at boot before we can
720 int __init of_scan_flat_dt(int (*it)(unsigned long node,
721 const char *uname, int depth,
725 unsigned long p = ((unsigned long)initial_boot_params) +
726 initial_boot_params->off_dt_struct;
731 u32 tag = *((u32 *)p);
735 if (tag == OF_DT_END_NODE) {
739 if (tag == OF_DT_NOP)
741 if (tag == OF_DT_END)
743 if (tag == OF_DT_PROP) {
744 u32 sz = *((u32 *)p);
746 if (initial_boot_params->version < 0x10)
747 p = _ALIGN(p, sz >= 8 ? 8 : 4);
752 if (tag != OF_DT_BEGIN_NODE) {
753 printk(KERN_WARNING "Invalid tag %x scanning flattened"
754 " device tree !\n", tag);
759 p = _ALIGN(p + strlen(pathp) + 1, 4);
760 if ((*pathp) == '/') {
762 for (lp = NULL, np = pathp; *np; np++)
768 rc = it(p, pathp, depth, data);
777 * This function can be used within scan_flattened_dt callback to get
778 * access to properties
780 void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
783 unsigned long p = node;
786 u32 tag = *((u32 *)p);
791 if (tag == OF_DT_NOP)
793 if (tag != OF_DT_PROP)
797 noff = *((u32 *)(p + 4));
799 if (initial_boot_params->version < 0x10)
800 p = _ALIGN(p, sz >= 8 ? 8 : 4);
802 nstr = find_flat_dt_string(noff);
804 printk(KERN_WARNING "Can't find property index"
808 if (strcmp(name, nstr) == 0) {
818 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
823 *mem = _ALIGN(*mem, align);
830 static unsigned long __init unflatten_dt_node(unsigned long mem,
832 struct device_node *dad,
833 struct device_node ***allnextpp,
834 unsigned long fpsize)
836 struct device_node *np;
837 struct property *pp, **prev_pp = NULL;
840 unsigned int l, allocl;
844 tag = *((u32 *)(*p));
845 if (tag != OF_DT_BEGIN_NODE) {
846 printk("Weird tag at start of node: %x\n", tag);
851 l = allocl = strlen(pathp) + 1;
852 *p = _ALIGN(*p + l, 4);
854 /* version 0x10 has a more compact unit name here instead of the full
855 * path. we accumulate the full path size using "fpsize", we'll rebuild
856 * it later. We detect this because the first character of the name is
859 if ((*pathp) != '/') {
862 /* root node: special case. fpsize accounts for path
863 * plus terminating zero. root node only has '/', so
864 * fpsize should be 2, but we want to avoid the first
865 * level nodes to have two '/' so we use fpsize 1 here
870 /* account for '/' and path size minus terminal 0
879 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
880 __alignof__(struct device_node));
882 memset(np, 0, sizeof(*np));
883 np->full_name = ((char*)np) + sizeof(struct device_node);
885 char *p = np->full_name;
886 /* rebuild full path for new format */
887 if (dad && dad->parent) {
888 strcpy(p, dad->full_name);
890 if ((strlen(p) + l + 1) != allocl) {
891 DBG("%s: p: %d, l: %d, a: %d\n",
892 pathp, strlen(p), l, allocl);
900 memcpy(np->full_name, pathp, l);
901 prev_pp = &np->properties;
903 *allnextpp = &np->allnext;
906 /* we temporarily use the next field as `last_child'*/
910 dad->next->sibling = np;
913 kref_init(&np->kref);
919 tag = *((u32 *)(*p));
920 if (tag == OF_DT_NOP) {
924 if (tag != OF_DT_PROP)
928 noff = *((u32 *)((*p) + 4));
930 if (initial_boot_params->version < 0x10)
931 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
933 pname = find_flat_dt_string(noff);
935 printk("Can't find property name in list !\n");
938 if (strcmp(pname, "name") == 0)
940 l = strlen(pname) + 1;
941 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
942 __alignof__(struct property));
944 if (strcmp(pname, "linux,phandle") == 0) {
945 np->node = *((u32 *)*p);
946 if (np->linux_phandle == 0)
947 np->linux_phandle = np->node;
949 if (strcmp(pname, "ibm,phandle") == 0)
950 np->linux_phandle = *((u32 *)*p);
953 pp->value = (void *)*p;
957 *p = _ALIGN((*p) + sz, 4);
959 /* with version 0x10 we may not have the name property, recreate
960 * it here from the unit name if absent
963 char *p = pathp, *ps = pathp, *pa = NULL;
976 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
977 __alignof__(struct property));
981 pp->value = (unsigned char *)(pp + 1);
984 memcpy(pp->value, ps, sz - 1);
985 ((char *)pp->value)[sz - 1] = 0;
986 DBG("fixed up name for %s -> %s\n", pathp, pp->value);
991 np->name = get_property(np, "name", NULL);
992 np->type = get_property(np, "device_type", NULL);
999 while (tag == OF_DT_BEGIN_NODE) {
1000 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
1001 tag = *((u32 *)(*p));
1003 if (tag != OF_DT_END_NODE) {
1004 printk("Weird tag at end of node: %x\n", tag);
1013 * unflattens the device-tree passed by the firmware, creating the
1014 * tree of struct device_node. It also fills the "name" and "type"
1015 * pointers of the nodes so the normal device-tree walking functions
1016 * can be used (this used to be done by finish_device_tree)
1018 void __init unflatten_device_tree(void)
1020 unsigned long start, mem, size;
1021 struct device_node **allnextp = &allnodes;
1025 DBG(" -> unflatten_device_tree()\n");
1027 /* First pass, scan for size */
1028 start = ((unsigned long)initial_boot_params) +
1029 initial_boot_params->off_dt_struct;
1030 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
1031 size = (size | 3) + 1;
1033 DBG(" size is %lx, allocating...\n", size);
1035 /* Allocate memory for the expanded device tree */
1036 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
1038 DBG("Couldn't allocate memory with lmb_alloc()!\n");
1039 panic("Couldn't allocate memory with lmb_alloc()!\n");
1041 mem = (unsigned long) __va(mem);
1043 ((u32 *)mem)[size / 4] = 0xdeadbeef;
1045 DBG(" unflattening %lx...\n", mem);
1047 /* Second pass, do actual unflattening */
1048 start = ((unsigned long)initial_boot_params) +
1049 initial_boot_params->off_dt_struct;
1050 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
1051 if (*((u32 *)start) != OF_DT_END)
1052 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
1053 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
1054 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
1055 ((u32 *)mem)[size / 4] );
1058 /* Get pointer to OF "/chosen" node for use everywhere */
1059 of_chosen = of_find_node_by_path("/chosen");
1060 if (of_chosen == NULL)
1061 of_chosen = of_find_node_by_path("/chosen@0");
1063 /* Retreive command line */
1064 if (of_chosen != NULL) {
1065 p = (char *)get_property(of_chosen, "bootargs", &l);
1066 if (p != NULL && l > 0)
1067 strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
1069 #ifdef CONFIG_CMDLINE
1070 if (l == 0 || (l == 1 && (*p) == 0))
1071 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1072 #endif /* CONFIG_CMDLINE */
1074 DBG("Command line is: %s\n", cmd_line);
1076 DBG(" <- unflatten_device_tree()\n");
1080 static int __init early_init_dt_scan_cpus(unsigned long node,
1081 const char *uname, int depth, void *data)
1085 char *type = of_get_flat_dt_prop(node, "device_type", &size);
1087 /* We are scanning "cpu" nodes only */
1088 if (type == NULL || strcmp(type, "cpu") != 0)
1092 boot_cpuid_phys = 0;
1093 if (initial_boot_params && initial_boot_params->version >= 2) {
1094 /* version 2 of the kexec param format adds the phys cpuid
1097 boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
1099 /* Check if it's the boot-cpu, set it's hw index now */
1100 if (of_get_flat_dt_prop(node,
1101 "linux,boot-cpu", NULL) != NULL) {
1102 prop = of_get_flat_dt_prop(node, "reg", NULL);
1104 boot_cpuid_phys = *prop;
1107 set_hard_smp_processor_id(0, boot_cpuid_phys);
1109 #ifdef CONFIG_ALTIVEC
1110 /* Check if we have a VMX and eventually update CPU features */
1111 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL);
1112 if (prop && (*prop) > 0) {
1113 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1114 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1117 /* Same goes for Apple's "altivec" property */
1118 prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
1120 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1121 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1123 #endif /* CONFIG_ALTIVEC */
1125 #ifdef CONFIG_PPC_PSERIES
1127 * Check for an SMT capable CPU and set the CPU feature. We do
1128 * this by looking at the size of the ibm,ppc-interrupt-server#s
1131 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
1133 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
1134 if (prop && ((size / sizeof(u32)) > 1))
1135 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
1141 static int __init early_init_dt_scan_chosen(unsigned long node,
1142 const char *uname, int depth, void *data)
1145 unsigned long *lprop;
1147 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1150 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
1153 /* get platform type */
1154 prop = (u32 *)of_get_flat_dt_prop(node, "linux,platform", NULL);
1157 #ifdef CONFIG_PPC_MULTIPLATFORM
1162 /* check if iommu is forced on or off */
1163 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
1165 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
1169 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
1171 memory_limit = *lprop;
1174 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
1176 tce_alloc_start = *lprop;
1177 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
1179 tce_alloc_end = *lprop;
1182 #ifdef CONFIG_PPC_RTAS
1183 /* To help early debugging via the front panel, we retreive a minimal
1184 * set of RTAS infos now if available
1187 u64 *basep, *entryp;
1189 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
1190 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1191 prop = of_get_flat_dt_prop(node, "linux,rtas-size", NULL);
1192 if (basep && entryp && prop) {
1194 rtas.entry = *entryp;
1198 #endif /* CONFIG_PPC_RTAS */
1204 static int __init early_init_dt_scan_root(unsigned long node,
1205 const char *uname, int depth, void *data)
1212 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
1213 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1214 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
1216 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
1217 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1218 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1224 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1229 /* Ignore more than 2 cells */
1230 while (s > sizeof(unsigned long) / 4) {
1248 static int __init early_init_dt_scan_memory(unsigned long node,
1249 const char *uname, int depth, void *data)
1251 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1255 /* We are scanning "memory" nodes only */
1258 * The longtrail doesn't have a device_type on the
1259 * /memory node, so look for the node called /memory@0.
1261 if (depth != 1 || strcmp(uname, "memory@0") != 0)
1263 } else if (strcmp(type, "memory") != 0)
1266 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
1270 endp = reg + (l / sizeof(cell_t));
1272 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
1273 uname, l, reg[0], reg[1], reg[2], reg[3]);
1275 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1276 unsigned long base, size;
1278 base = dt_mem_next_cell(dt_root_addr_cells, ®);
1279 size = dt_mem_next_cell(dt_root_size_cells, ®);
1283 DBG(" - %lx , %lx\n", base, size);
1286 if (base >= 0x80000000ul)
1288 if ((base + size) > 0x80000000ul)
1289 size = 0x80000000ul - base;
1292 lmb_add(base, size);
1297 static void __init early_reserve_mem(void)
1299 unsigned long base, size;
1300 unsigned long *reserve_map;
1302 reserve_map = (unsigned long *)(((unsigned long)initial_boot_params) +
1303 initial_boot_params->off_mem_rsvmap);
1305 base = *(reserve_map++);
1306 size = *(reserve_map++);
1309 DBG("reserving: %lx -> %lx\n", base, size);
1310 lmb_reserve(base, size);
1314 DBG("memory reserved, lmbs :\n");
1319 void __init early_init_devtree(void *params)
1321 DBG(" -> early_init_devtree()\n");
1323 /* Setup flat device-tree pointer */
1324 initial_boot_params = params;
1326 /* Retrieve various informations from the /chosen node of the
1327 * device-tree, including the platform type, initrd location and
1328 * size, TCE reserve, and more ...
1330 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
1332 /* Scan memory nodes and rebuild LMBs */
1334 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1335 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1336 lmb_enforce_memory_limit(memory_limit);
1338 lmb_reserve(0, __pa(klimit));
1340 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1342 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1343 early_reserve_mem();
1345 DBG("Scanning CPUs ...\n");
1347 /* Retreive CPU related informations from the flat tree
1348 * (altivec support, boot CPU ID, ...)
1350 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
1352 DBG(" <- early_init_devtree()\n");
1358 prom_n_addr_cells(struct device_node* np)
1364 ip = (int *) get_property(np, "#address-cells", NULL);
1367 } while (np->parent);
1368 /* No #address-cells property for the root node, default to 1 */
1373 prom_n_size_cells(struct device_node* np)
1379 ip = (int *) get_property(np, "#size-cells", NULL);
1382 } while (np->parent);
1383 /* No #size-cells property for the root node, default to 1 */
1388 * Work out the sense (active-low level / active-high edge)
1389 * of each interrupt from the device tree.
1391 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1393 struct device_node *np;
1396 /* default to level-triggered */
1397 memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
1399 for (np = allnodes; np != 0; np = np->allnext) {
1400 for (j = 0; j < np->n_intrs; j++) {
1401 i = np->intrs[j].line;
1402 if (i >= off && i < max)
1403 senses[i-off] = np->intrs[j].sense;
1409 * Construct and return a list of the device_nodes with a given name.
1411 struct device_node *find_devices(const char *name)
1413 struct device_node *head, **prevp, *np;
1416 for (np = allnodes; np != 0; np = np->allnext) {
1417 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1425 EXPORT_SYMBOL(find_devices);
1428 * Construct and return a list of the device_nodes with a given type.
1430 struct device_node *find_type_devices(const char *type)
1432 struct device_node *head, **prevp, *np;
1435 for (np = allnodes; np != 0; np = np->allnext) {
1436 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1444 EXPORT_SYMBOL(find_type_devices);
1447 * Returns all nodes linked together
1449 struct device_node *find_all_nodes(void)
1451 struct device_node *head, **prevp, *np;
1454 for (np = allnodes; np != 0; np = np->allnext) {
1461 EXPORT_SYMBOL(find_all_nodes);
1463 /** Checks if the given "compat" string matches one of the strings in
1464 * the device's "compatible" property
1466 int device_is_compatible(struct device_node *device, const char *compat)
1471 cp = (char *) get_property(device, "compatible", &cplen);
1475 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1484 EXPORT_SYMBOL(device_is_compatible);
1488 * Indicates whether the root node has a given value in its
1489 * compatible property.
1491 int machine_is_compatible(const char *compat)
1493 struct device_node *root;
1496 root = of_find_node_by_path("/");
1498 rc = device_is_compatible(root, compat);
1503 EXPORT_SYMBOL(machine_is_compatible);
1506 * Construct and return a list of the device_nodes with a given type
1507 * and compatible property.
1509 struct device_node *find_compatible_devices(const char *type,
1512 struct device_node *head, **prevp, *np;
1515 for (np = allnodes; np != 0; np = np->allnext) {
1517 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1519 if (device_is_compatible(np, compat)) {
1527 EXPORT_SYMBOL(find_compatible_devices);
1530 * Find the device_node with a given full_name.
1532 struct device_node *find_path_device(const char *path)
1534 struct device_node *np;
1536 for (np = allnodes; np != 0; np = np->allnext)
1537 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1541 EXPORT_SYMBOL(find_path_device);
1545 * New implementation of the OF "find" APIs, return a refcounted
1546 * object, call of_node_put() when done. The device tree and list
1547 * are protected by a rw_lock.
1549 * Note that property management will need some locking as well,
1550 * this isn't dealt with yet.
1555 * of_find_node_by_name - Find a node by its "name" property
1556 * @from: The node to start searching from or NULL, the node
1557 * you pass will not be searched, only the next one
1558 * will; typically, you pass what the previous call
1559 * returned. of_node_put() will be called on it
1560 * @name: The name string to match against
1562 * Returns a node pointer with refcount incremented, use
1563 * of_node_put() on it when done.
1565 struct device_node *of_find_node_by_name(struct device_node *from,
1568 struct device_node *np;
1570 read_lock(&devtree_lock);
1571 np = from ? from->allnext : allnodes;
1572 for (; np != 0; np = np->allnext)
1573 if (np->name != 0 && strcasecmp(np->name, name) == 0
1578 read_unlock(&devtree_lock);
1581 EXPORT_SYMBOL(of_find_node_by_name);
1584 * of_find_node_by_type - Find a node by its "device_type" property
1585 * @from: The node to start searching from or NULL, the node
1586 * you pass will not be searched, only the next one
1587 * will; typically, you pass what the previous call
1588 * returned. of_node_put() will be called on it
1589 * @name: The type string to match against
1591 * Returns a node pointer with refcount incremented, use
1592 * of_node_put() on it when done.
1594 struct device_node *of_find_node_by_type(struct device_node *from,
1597 struct device_node *np;
1599 read_lock(&devtree_lock);
1600 np = from ? from->allnext : allnodes;
1601 for (; np != 0; np = np->allnext)
1602 if (np->type != 0 && strcasecmp(np->type, type) == 0
1607 read_unlock(&devtree_lock);
1610 EXPORT_SYMBOL(of_find_node_by_type);
1613 * of_find_compatible_node - Find a node based on type and one of the
1614 * tokens in its "compatible" property
1615 * @from: The node to start searching from or NULL, the node
1616 * you pass will not be searched, only the next one
1617 * will; typically, you pass what the previous call
1618 * returned. of_node_put() will be called on it
1619 * @type: The type string to match "device_type" or NULL to ignore
1620 * @compatible: The string to match to one of the tokens in the device
1621 * "compatible" list.
1623 * Returns a node pointer with refcount incremented, use
1624 * of_node_put() on it when done.
1626 struct device_node *of_find_compatible_node(struct device_node *from,
1627 const char *type, const char *compatible)
1629 struct device_node *np;
1631 read_lock(&devtree_lock);
1632 np = from ? from->allnext : allnodes;
1633 for (; np != 0; np = np->allnext) {
1635 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1637 if (device_is_compatible(np, compatible) && of_node_get(np))
1642 read_unlock(&devtree_lock);
1645 EXPORT_SYMBOL(of_find_compatible_node);
1648 * of_find_node_by_path - Find a node matching a full OF path
1649 * @path: The full path to match
1651 * Returns a node pointer with refcount incremented, use
1652 * of_node_put() on it when done.
1654 struct device_node *of_find_node_by_path(const char *path)
1656 struct device_node *np = allnodes;
1658 read_lock(&devtree_lock);
1659 for (; np != 0; np = np->allnext) {
1660 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1664 read_unlock(&devtree_lock);
1667 EXPORT_SYMBOL(of_find_node_by_path);
1670 * of_find_node_by_phandle - Find a node given a phandle
1671 * @handle: phandle of the node to find
1673 * Returns a node pointer with refcount incremented, use
1674 * of_node_put() on it when done.
1676 struct device_node *of_find_node_by_phandle(phandle handle)
1678 struct device_node *np;
1680 read_lock(&devtree_lock);
1681 for (np = allnodes; np != 0; np = np->allnext)
1682 if (np->linux_phandle == handle)
1686 read_unlock(&devtree_lock);
1689 EXPORT_SYMBOL(of_find_node_by_phandle);
1692 * of_find_all_nodes - Get next node in global list
1693 * @prev: Previous node or NULL to start iteration
1694 * of_node_put() will be called on it
1696 * Returns a node pointer with refcount incremented, use
1697 * of_node_put() on it when done.
1699 struct device_node *of_find_all_nodes(struct device_node *prev)
1701 struct device_node *np;
1703 read_lock(&devtree_lock);
1704 np = prev ? prev->allnext : allnodes;
1705 for (; np != 0; np = np->allnext)
1706 if (of_node_get(np))
1710 read_unlock(&devtree_lock);
1713 EXPORT_SYMBOL(of_find_all_nodes);
1716 * of_get_parent - Get a node's parent if any
1717 * @node: Node to get parent
1719 * Returns a node pointer with refcount incremented, use
1720 * of_node_put() on it when done.
1722 struct device_node *of_get_parent(const struct device_node *node)
1724 struct device_node *np;
1729 read_lock(&devtree_lock);
1730 np = of_node_get(node->parent);
1731 read_unlock(&devtree_lock);
1734 EXPORT_SYMBOL(of_get_parent);
1737 * of_get_next_child - Iterate a node childs
1738 * @node: parent node
1739 * @prev: previous child of the parent node, or NULL to get first
1741 * Returns a node pointer with refcount incremented, use
1742 * of_node_put() on it when done.
1744 struct device_node *of_get_next_child(const struct device_node *node,
1745 struct device_node *prev)
1747 struct device_node *next;
1749 read_lock(&devtree_lock);
1750 next = prev ? prev->sibling : node->child;
1751 for (; next != 0; next = next->sibling)
1752 if (of_node_get(next))
1756 read_unlock(&devtree_lock);
1759 EXPORT_SYMBOL(of_get_next_child);
1762 * of_node_get - Increment refcount of a node
1763 * @node: Node to inc refcount, NULL is supported to
1764 * simplify writing of callers
1768 struct device_node *of_node_get(struct device_node *node)
1771 kref_get(&node->kref);
1774 EXPORT_SYMBOL(of_node_get);
1776 static inline struct device_node * kref_to_device_node(struct kref *kref)
1778 return container_of(kref, struct device_node, kref);
1782 * of_node_release - release a dynamically allocated node
1783 * @kref: kref element of the node to be released
1785 * In of_node_put() this function is passed to kref_put()
1786 * as the destructor.
1788 static void of_node_release(struct kref *kref)
1790 struct device_node *node = kref_to_device_node(kref);
1791 struct property *prop = node->properties;
1793 if (!OF_IS_DYNAMIC(node))
1796 struct property *next = prop->next;
1804 kfree(node->full_name);
1810 * of_node_put - Decrement refcount of a node
1811 * @node: Node to dec refcount, NULL is supported to
1812 * simplify writing of callers
1815 void of_node_put(struct device_node *node)
1818 kref_put(&node->kref, of_node_release);
1820 EXPORT_SYMBOL(of_node_put);
1823 * Plug a device node into the tree and global list.
1825 void of_attach_node(struct device_node *np)
1827 write_lock(&devtree_lock);
1828 np->sibling = np->parent->child;
1829 np->allnext = allnodes;
1830 np->parent->child = np;
1832 write_unlock(&devtree_lock);
1836 * "Unplug" a node from the device tree. The caller must hold
1837 * a reference to the node. The memory associated with the node
1838 * is not freed until its refcount goes to zero.
1840 void of_detach_node(const struct device_node *np)
1842 struct device_node *parent;
1844 write_lock(&devtree_lock);
1846 parent = np->parent;
1849 allnodes = np->allnext;
1851 struct device_node *prev;
1852 for (prev = allnodes;
1853 prev->allnext != np;
1854 prev = prev->allnext)
1856 prev->allnext = np->allnext;
1859 if (parent->child == np)
1860 parent->child = np->sibling;
1862 struct device_node *prevsib;
1863 for (prevsib = np->parent->child;
1864 prevsib->sibling != np;
1865 prevsib = prevsib->sibling)
1867 prevsib->sibling = np->sibling;
1870 write_unlock(&devtree_lock);
1873 #ifdef CONFIG_PPC_PSERIES
1875 * Fix up the uninitialized fields in a new device node:
1876 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1878 * A lot of boot-time code is duplicated here, because functions such
1879 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1882 * This should probably be split up into smaller chunks.
1885 static int of_finish_dynamic_node(struct device_node *node,
1886 unsigned long *unused1, int unused2,
1887 int unused3, int unused4)
1889 struct device_node *parent = of_get_parent(node);
1891 phandle *ibm_phandle;
1893 node->name = get_property(node, "name", NULL);
1894 node->type = get_property(node, "device_type", NULL);
1901 /* We don't support that function on PowerMac, at least
1904 if (_machine == PLATFORM_POWERMAC)
1907 /* fix up new node's linux_phandle field */
1908 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
1909 node->linux_phandle = *ibm_phandle;
1912 of_node_put(parent);
1916 static int prom_reconfig_notifier(struct notifier_block *nb,
1917 unsigned long action, void *node)
1922 case PSERIES_RECONFIG_ADD:
1923 err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
1925 printk(KERN_ERR "finish_node returned %d\n", err);
1936 static struct notifier_block prom_reconfig_nb = {
1937 .notifier_call = prom_reconfig_notifier,
1938 .priority = 10, /* This one needs to run first */
1941 static int __init prom_reconfig_setup(void)
1943 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1945 __initcall(prom_reconfig_setup);
1949 * Find a property with a given name for a given node
1950 * and return the value.
1952 unsigned char *get_property(struct device_node *np, const char *name,
1955 struct property *pp;
1957 for (pp = np->properties; pp != 0; pp = pp->next)
1958 if (strcmp(pp->name, name) == 0) {
1965 EXPORT_SYMBOL(get_property);
1968 * Add a property to a node
1970 int prom_add_property(struct device_node* np, struct property* prop)
1972 struct property **next;
1975 write_lock(&devtree_lock);
1976 next = &np->properties;
1978 if (strcmp(prop->name, (*next)->name) == 0) {
1979 /* duplicate ! don't insert it */
1980 write_unlock(&devtree_lock);
1983 next = &(*next)->next;
1986 write_unlock(&devtree_lock);
1988 #ifdef CONFIG_PROC_DEVICETREE
1989 /* try to add to proc as well if it was initialized */
1991 proc_device_tree_add_prop(np->pde, prop);
1992 #endif /* CONFIG_PROC_DEVICETREE */
1997 /* I quickly hacked that one, check against spec ! */
1998 static inline unsigned long
1999 bus_space_to_resource_flags(unsigned int bus_space)
2001 u8 space = (bus_space >> 24) & 0xf;
2005 return IORESOURCE_MEM;
2006 else if (space == 0x01)
2007 return IORESOURCE_IO;
2009 printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n",
2016 static struct resource *find_parent_pci_resource(struct pci_dev* pdev,
2017 struct address_range *range)
2022 /* Check this one */
2023 mask = bus_space_to_resource_flags(range->space);
2024 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
2025 if ((pdev->resource[i].flags & mask) == mask &&
2026 pdev->resource[i].start <= range->address &&
2027 pdev->resource[i].end > range->address) {
2028 if ((range->address + range->size - 1) > pdev->resource[i].end) {
2029 /* Add better message */
2030 printk(KERN_WARNING "PCI/OF resource overlap !\n");
2036 if (i == DEVICE_COUNT_RESOURCE)
2038 return &pdev->resource[i];
2042 * Request an OF device resource. Currently handles child of PCI devices,
2043 * or other nodes attached to the root node. Ultimately, put some
2044 * link to resources in the OF node.
2046 struct resource *request_OF_resource(struct device_node* node, int index,
2047 const char* name_postfix)
2049 struct pci_dev* pcidev;
2050 u8 pci_bus, pci_devfn;
2051 unsigned long iomask;
2052 struct device_node* nd;
2053 struct resource* parent;
2054 struct resource *res = NULL;
2057 if (index >= node->n_addrs)
2060 /* Sanity check on bus space */
2061 iomask = bus_space_to_resource_flags(node->addrs[index].space);
2062 if (iomask & IORESOURCE_MEM)
2063 parent = &iomem_resource;
2064 else if (iomask & IORESOURCE_IO)
2065 parent = &ioport_resource;
2069 /* Find a PCI parent if any */
2073 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
2074 pcidev = pci_find_slot(pci_bus, pci_devfn);
2079 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
2081 printk(KERN_WARNING "request_OF_resource(%s), parent not found\n",
2086 res = __request_region(parent, node->addrs[index].address,
2087 node->addrs[index].size, NULL);
2090 nlen = strlen(node->name);
2091 plen = name_postfix ? strlen(name_postfix) : 0;
2092 res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL);
2094 strcpy((char *)res->name, node->name);
2096 strcpy((char *)res->name+nlen, name_postfix);
2102 EXPORT_SYMBOL(request_OF_resource);
2104 int release_OF_resource(struct device_node *node, int index)
2106 struct pci_dev* pcidev;
2107 u8 pci_bus, pci_devfn;
2108 unsigned long iomask, start, end;
2109 struct device_node* nd;
2110 struct resource* parent;
2111 struct resource *res = NULL;
2113 if (index >= node->n_addrs)
2116 /* Sanity check on bus space */
2117 iomask = bus_space_to_resource_flags(node->addrs[index].space);
2118 if (iomask & IORESOURCE_MEM)
2119 parent = &iomem_resource;
2120 else if (iomask & IORESOURCE_IO)
2121 parent = &ioport_resource;
2125 /* Find a PCI parent if any */
2129 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
2130 pcidev = pci_find_slot(pci_bus, pci_devfn);
2135 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
2137 printk(KERN_WARNING "release_OF_resource(%s), parent not found\n",
2142 /* Find us in the parent and its childs */
2143 res = parent->child;
2144 start = node->addrs[index].address;
2145 end = start + node->addrs[index].size - 1;
2147 if (res->start == start && res->end == end &&
2148 (res->flags & IORESOURCE_BUSY))
2150 if (res->start <= start && res->end >= end)
2162 release_resource(res);
2167 EXPORT_SYMBOL(release_OF_resource);
2168 #endif /* CONFIG_PCI */