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 extern struct rtas_t rtas;
75 extern struct lmb lmb;
76 extern unsigned long klimit;
78 static int __initdata dt_root_addr_cells;
79 static int __initdata dt_root_size_cells;
82 static int __initdata iommu_is_off;
83 int __initdata iommu_force_on;
84 unsigned long tce_alloc_start, tce_alloc_end;
90 static struct boot_param_header *initial_boot_params __initdata;
92 struct boot_param_header *initial_boot_params;
95 static struct device_node *allnodes = NULL;
97 /* use when traversing tree through the allnext, child, sibling,
98 * or parent members of struct device_node.
100 static DEFINE_RWLOCK(devtree_lock);
102 /* export that to outside world */
103 struct device_node *of_chosen;
105 struct device_node *dflt_interrupt_controller;
106 int num_interrupt_controllers;
109 * Wrapper for allocating memory for various data that needs to be
110 * attached to device nodes as they are processed at boot or when
111 * added to the device tree later (e.g. DLPAR). At boot there is
112 * already a region reserved so we just increment *mem_start by size;
113 * otherwise we call kmalloc.
115 static void * prom_alloc(unsigned long size, unsigned long *mem_start)
120 return kmalloc(size, GFP_KERNEL);
128 * Find the device_node with a given phandle.
130 static struct device_node * find_phandle(phandle ph)
132 struct device_node *np;
134 for (np = allnodes; np != 0; np = np->allnext)
135 if (np->linux_phandle == ph)
141 * Find the interrupt parent of a node.
143 static struct device_node * __devinit intr_parent(struct device_node *p)
147 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
150 p = find_phandle(*parp);
154 * On a powermac booted with BootX, we don't get to know the
155 * phandles for any nodes, so find_phandle will return NULL.
156 * Fortunately these machines only have one interrupt controller
157 * so there isn't in fact any ambiguity. -- paulus
159 if (num_interrupt_controllers == 1)
160 p = dflt_interrupt_controller;
165 * Find out the size of each entry of the interrupts property
168 int __devinit prom_n_intr_cells(struct device_node *np)
170 struct device_node *p;
173 for (p = np; (p = intr_parent(p)) != NULL; ) {
174 icp = (unsigned int *)
175 get_property(p, "#interrupt-cells", NULL);
178 if (get_property(p, "interrupt-controller", NULL) != NULL
179 || get_property(p, "interrupt-map", NULL) != NULL) {
180 printk("oops, node %s doesn't have #interrupt-cells\n",
186 printk("prom_n_intr_cells failed for %s\n", np->full_name);
192 * Map an interrupt from a device up to the platform interrupt
195 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
196 struct device_node *np, unsigned int *ints,
199 struct device_node *p, *ipar;
200 unsigned int *imap, *imask, *ip;
201 int i, imaplen, match;
202 int newintrc = 0, newaddrc = 0;
206 reg = (unsigned int *) get_property(np, "reg", NULL);
207 naddrc = prom_n_addr_cells(np);
210 if (get_property(p, "interrupt-controller", NULL) != NULL)
211 /* this node is an interrupt controller, stop here */
213 imap = (unsigned int *)
214 get_property(p, "interrupt-map", &imaplen);
219 imask = (unsigned int *)
220 get_property(p, "interrupt-map-mask", NULL);
222 printk("oops, %s has interrupt-map but no mask\n",
226 imaplen /= sizeof(unsigned int);
229 while (imaplen > 0 && !match) {
230 /* check the child-interrupt field */
232 for (i = 0; i < naddrc && match; ++i)
233 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
234 for (; i < naddrc + nintrc && match; ++i)
235 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
236 imap += naddrc + nintrc;
237 imaplen -= naddrc + nintrc;
238 /* grab the interrupt parent */
239 ipar = find_phandle((phandle) *imap++);
241 if (ipar == NULL && num_interrupt_controllers == 1)
242 /* cope with BootX not giving us phandles */
243 ipar = dflt_interrupt_controller;
245 printk("oops, no int parent %x in map of %s\n",
246 imap[-1], p->full_name);
249 /* find the parent's # addr and intr cells */
250 ip = (unsigned int *)
251 get_property(ipar, "#interrupt-cells", NULL);
253 printk("oops, no #interrupt-cells on %s\n",
258 ip = (unsigned int *)
259 get_property(ipar, "#address-cells", NULL);
260 newaddrc = (ip == NULL)? 0: *ip;
261 imap += newaddrc + newintrc;
262 imaplen -= newaddrc + newintrc;
265 printk("oops, error decoding int-map on %s, len=%d\n",
266 p->full_name, imaplen);
271 printk("oops, no match in %s int-map for %s\n",
272 p->full_name, np->full_name);
279 ints = imap - nintrc;
284 printk("hmmm, int tree for %s doesn't have ctrler\n",
294 static unsigned char map_isa_senses[4] = {
295 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
296 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
297 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
298 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE
301 static unsigned char map_mpic_senses[4] = {
302 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE,
303 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
304 /* 2 seems to be used for the 8259 cascade... */
305 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
306 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
309 static int __devinit finish_node_interrupts(struct device_node *np,
310 unsigned long *mem_start,
314 int intlen, intrcells, intrcount;
316 unsigned int *irq, virq;
317 struct device_node *ic;
319 if (num_interrupt_controllers == 0) {
321 * Old machines just have a list of interrupt numbers
322 * and no interrupt-controller nodes.
324 ints = (unsigned int *) get_property(np, "AAPL,interrupts",
326 /* XXX old interpret_pci_props looked in parent too */
327 /* XXX old interpret_macio_props looked for interrupts
328 before AAPL,interrupts */
330 ints = (unsigned int *) get_property(np, "interrupts",
335 np->n_intrs = intlen / sizeof(unsigned int);
336 np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]),
343 for (i = 0; i < np->n_intrs; ++i) {
344 np->intrs[i].line = *ints++;
345 np->intrs[i].sense = IRQ_SENSE_LEVEL
346 | IRQ_POLARITY_NEGATIVE;
351 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
354 intrcells = prom_n_intr_cells(np);
355 intlen /= intrcells * sizeof(unsigned int);
357 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
365 for (i = 0; i < intlen; ++i, ints += intrcells) {
366 n = map_interrupt(&irq, &ic, np, ints, intrcells);
370 /* don't map IRQ numbers under a cascaded 8259 controller */
371 if (ic && device_is_compatible(ic, "chrp,iic")) {
372 np->intrs[intrcount].line = irq[0];
373 sense = (n > 1)? (irq[1] & 3): 3;
374 np->intrs[intrcount].sense = map_isa_senses[sense];
376 virq = virt_irq_create_mapping(irq[0]);
378 if (virq == NO_IRQ) {
379 printk(KERN_CRIT "Could not allocate interrupt"
380 " number for %s\n", np->full_name);
384 np->intrs[intrcount].line = irq_offset_up(virq);
385 sense = (n > 1)? (irq[1] & 3): 1;
386 np->intrs[intrcount].sense = map_mpic_senses[sense];
390 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
391 if (_machine == PLATFORM_POWERMAC && ic && ic->parent) {
392 char *name = get_property(ic->parent, "name", NULL);
393 if (name && !strcmp(name, "u3"))
394 np->intrs[intrcount].line += 128;
395 else if (!(name && !strcmp(name, "mac-io")))
396 /* ignore other cascaded controllers, such as
402 printk("hmmm, got %d intr cells for %s:", n,
404 for (j = 0; j < n; ++j)
405 printk(" %d", irq[j]);
410 np->n_intrs = intrcount;
415 static int __devinit interpret_pci_props(struct device_node *np,
416 unsigned long *mem_start,
417 int naddrc, int nsizec,
420 struct address_range *adr;
421 struct pci_reg_property *pci_addrs;
424 pci_addrs = (struct pci_reg_property *)
425 get_property(np, "assigned-addresses", &l);
429 n_addrs = l / sizeof(*pci_addrs);
431 adr = prom_alloc(n_addrs * sizeof(*adr), mem_start);
439 np->n_addrs = n_addrs;
441 for (i = 0; i < n_addrs; i++) {
442 adr[i].space = pci_addrs[i].addr.a_hi;
443 adr[i].address = pci_addrs[i].addr.a_lo |
444 ((u64)pci_addrs[i].addr.a_mid << 32);
445 adr[i].size = pci_addrs[i].size_lo;
451 static int __init interpret_dbdma_props(struct device_node *np,
452 unsigned long *mem_start,
453 int naddrc, int nsizec,
456 struct reg_property32 *rp;
457 struct address_range *adr;
458 unsigned long base_address;
460 struct device_node *db;
464 for (db = np->parent; db != NULL; db = db->parent) {
465 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
466 base_address = db->addrs[0].address;
472 rp = (struct reg_property32 *) get_property(np, "reg", &l);
473 if (rp != 0 && l >= sizeof(struct reg_property32)) {
475 adr = (struct address_range *) (*mem_start);
476 while ((l -= sizeof(struct reg_property32)) >= 0) {
479 adr[i].address = rp[i].address + base_address;
480 adr[i].size = rp[i].size;
486 (*mem_start) += i * sizeof(struct address_range);
492 static int __init interpret_macio_props(struct device_node *np,
493 unsigned long *mem_start,
494 int naddrc, int nsizec,
497 struct reg_property32 *rp;
498 struct address_range *adr;
499 unsigned long base_address;
501 struct device_node *db;
505 for (db = np->parent; db != NULL; db = db->parent) {
506 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
507 base_address = db->addrs[0].address;
513 rp = (struct reg_property32 *) get_property(np, "reg", &l);
514 if (rp != 0 && l >= sizeof(struct reg_property32)) {
516 adr = (struct address_range *) (*mem_start);
517 while ((l -= sizeof(struct reg_property32)) >= 0) {
520 adr[i].address = rp[i].address + base_address;
521 adr[i].size = rp[i].size;
527 (*mem_start) += i * sizeof(struct address_range);
533 static int __init interpret_isa_props(struct device_node *np,
534 unsigned long *mem_start,
535 int naddrc, int nsizec,
538 struct isa_reg_property *rp;
539 struct address_range *adr;
542 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
543 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
545 adr = (struct address_range *) (*mem_start);
546 while ((l -= sizeof(struct isa_reg_property)) >= 0) {
548 adr[i].space = rp[i].space;
549 adr[i].address = rp[i].address;
550 adr[i].size = rp[i].size;
556 (*mem_start) += i * sizeof(struct address_range);
562 static int __init interpret_root_props(struct device_node *np,
563 unsigned long *mem_start,
564 int naddrc, int nsizec,
567 struct address_range *adr;
570 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
572 rp = (unsigned int *) get_property(np, "reg", &l);
573 if (rp != 0 && l >= rpsize) {
575 adr = (struct address_range *) (*mem_start);
576 while ((l -= rpsize) >= 0) {
579 adr[i].address = rp[naddrc - 1];
580 adr[i].size = rp[naddrc + nsizec - 1];
583 rp += naddrc + nsizec;
587 (*mem_start) += i * sizeof(struct address_range);
593 static int __devinit finish_node(struct device_node *np,
594 unsigned long *mem_start,
595 interpret_func *ifunc,
596 int naddrc, int nsizec,
599 struct device_node *child;
602 /* get the device addresses and interrupts */
604 rc = ifunc(np, mem_start, naddrc, nsizec, measure_only);
608 rc = finish_node_interrupts(np, mem_start, measure_only);
612 /* Look for #address-cells and #size-cells properties. */
613 ip = (int *) get_property(np, "#address-cells", NULL);
616 ip = (int *) get_property(np, "#size-cells", NULL);
620 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
621 ifunc = interpret_root_props;
622 else if (np->type == 0)
624 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
625 ifunc = interpret_pci_props;
626 else if (!strcmp(np->type, "dbdma"))
627 ifunc = interpret_dbdma_props;
628 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
629 ifunc = interpret_macio_props;
630 else if (!strcmp(np->type, "isa"))
631 ifunc = interpret_isa_props;
632 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
633 ifunc = interpret_root_props;
634 else if (!((ifunc == interpret_dbdma_props
635 || ifunc == interpret_macio_props)
636 && (!strcmp(np->type, "escc")
637 || !strcmp(np->type, "media-bay"))))
640 for (child = np->child; child != NULL; child = child->sibling) {
641 rc = finish_node(child, mem_start, ifunc,
642 naddrc, nsizec, measure_only);
650 static void __init scan_interrupt_controllers(void)
652 struct device_node *np;
657 for (np = allnodes; np != NULL; np = np->allnext) {
658 ic = get_property(np, "interrupt-controller", &iclen);
659 name = get_property(np, "name", NULL);
660 /* checking iclen makes sure we don't get a false
661 match on /chosen.interrupt_controller */
663 && strcmp(name, "interrupt-controller") == 0)
664 || (ic != NULL && iclen == 0
665 && strcmp(name, "AppleKiwi"))) {
667 dflt_interrupt_controller = np;
671 num_interrupt_controllers = n;
675 * finish_device_tree is called once things are running normally
676 * (i.e. with text and data mapped to the address they were linked at).
677 * It traverses the device tree and fills in some of the additional,
678 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
679 * mapping is also initialized at this point.
681 void __init finish_device_tree(void)
683 unsigned long start, end, size = 0;
685 DBG(" -> finish_device_tree\n");
688 /* Initialize virtual IRQ map */
691 scan_interrupt_controllers();
694 * Finish device-tree (pre-parsing some properties etc...)
695 * We do this in 2 passes. One with "measure_only" set, which
696 * will only measure the amount of memory needed, then we can
697 * allocate that memory, and call finish_node again. However,
698 * we must be careful as most routines will fail nowadays when
699 * prom_alloc() returns 0, so we must make sure our first pass
700 * doesn't start at 0. We pre-initialize size to 16 for that
701 * reason and then remove those additional 16 bytes
704 finish_node(allnodes, &size, NULL, 0, 0, 1);
706 end = start = (unsigned long) __va(lmb_alloc(size, 128));
707 finish_node(allnodes, &end, NULL, 0, 0, 0);
708 BUG_ON(end != start + size);
710 DBG(" <- finish_device_tree\n");
713 static inline char *find_flat_dt_string(u32 offset)
715 return ((char *)initial_boot_params) +
716 initial_boot_params->off_dt_strings + offset;
720 * This function is used to scan the flattened device-tree, it is
721 * used to extract the memory informations at boot before we can
724 int __init of_scan_flat_dt(int (*it)(unsigned long node,
725 const char *uname, int depth,
729 unsigned long p = ((unsigned long)initial_boot_params) +
730 initial_boot_params->off_dt_struct;
735 u32 tag = *((u32 *)p);
739 if (tag == OF_DT_END_NODE) {
743 if (tag == OF_DT_NOP)
745 if (tag == OF_DT_END)
747 if (tag == OF_DT_PROP) {
748 u32 sz = *((u32 *)p);
750 if (initial_boot_params->version < 0x10)
751 p = _ALIGN(p, sz >= 8 ? 8 : 4);
756 if (tag != OF_DT_BEGIN_NODE) {
757 printk(KERN_WARNING "Invalid tag %x scanning flattened"
758 " device tree !\n", tag);
763 p = _ALIGN(p + strlen(pathp) + 1, 4);
764 if ((*pathp) == '/') {
766 for (lp = NULL, np = pathp; *np; np++)
772 rc = it(p, pathp, depth, data);
781 * This function can be used within scan_flattened_dt callback to get
782 * access to properties
784 void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
787 unsigned long p = node;
790 u32 tag = *((u32 *)p);
795 if (tag == OF_DT_NOP)
797 if (tag != OF_DT_PROP)
801 noff = *((u32 *)(p + 4));
803 if (initial_boot_params->version < 0x10)
804 p = _ALIGN(p, sz >= 8 ? 8 : 4);
806 nstr = find_flat_dt_string(noff);
808 printk(KERN_WARNING "Can't find property index"
812 if (strcmp(name, nstr) == 0) {
822 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
827 *mem = _ALIGN(*mem, align);
834 static unsigned long __init unflatten_dt_node(unsigned long mem,
836 struct device_node *dad,
837 struct device_node ***allnextpp,
838 unsigned long fpsize)
840 struct device_node *np;
841 struct property *pp, **prev_pp = NULL;
844 unsigned int l, allocl;
848 tag = *((u32 *)(*p));
849 if (tag != OF_DT_BEGIN_NODE) {
850 printk("Weird tag at start of node: %x\n", tag);
855 l = allocl = strlen(pathp) + 1;
856 *p = _ALIGN(*p + l, 4);
858 /* version 0x10 has a more compact unit name here instead of the full
859 * path. we accumulate the full path size using "fpsize", we'll rebuild
860 * it later. We detect this because the first character of the name is
863 if ((*pathp) != '/') {
866 /* root node: special case. fpsize accounts for path
867 * plus terminating zero. root node only has '/', so
868 * fpsize should be 2, but we want to avoid the first
869 * level nodes to have two '/' so we use fpsize 1 here
874 /* account for '/' and path size minus terminal 0
883 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
884 __alignof__(struct device_node));
886 memset(np, 0, sizeof(*np));
887 np->full_name = ((char*)np) + sizeof(struct device_node);
889 char *p = np->full_name;
890 /* rebuild full path for new format */
891 if (dad && dad->parent) {
892 strcpy(p, dad->full_name);
894 if ((strlen(p) + l + 1) != allocl) {
895 DBG("%s: p: %d, l: %d, a: %d\n",
896 pathp, strlen(p), l, allocl);
904 memcpy(np->full_name, pathp, l);
905 prev_pp = &np->properties;
907 *allnextpp = &np->allnext;
910 /* we temporarily use the next field as `last_child'*/
914 dad->next->sibling = np;
917 kref_init(&np->kref);
923 tag = *((u32 *)(*p));
924 if (tag == OF_DT_NOP) {
928 if (tag != OF_DT_PROP)
932 noff = *((u32 *)((*p) + 4));
934 if (initial_boot_params->version < 0x10)
935 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
937 pname = find_flat_dt_string(noff);
939 printk("Can't find property name in list !\n");
942 if (strcmp(pname, "name") == 0)
944 l = strlen(pname) + 1;
945 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
946 __alignof__(struct property));
948 if (strcmp(pname, "linux,phandle") == 0) {
949 np->node = *((u32 *)*p);
950 if (np->linux_phandle == 0)
951 np->linux_phandle = np->node;
953 if (strcmp(pname, "ibm,phandle") == 0)
954 np->linux_phandle = *((u32 *)*p);
957 pp->value = (void *)*p;
961 *p = _ALIGN((*p) + sz, 4);
963 /* with version 0x10 we may not have the name property, recreate
964 * it here from the unit name if absent
967 char *p = pathp, *ps = pathp, *pa = NULL;
980 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
981 __alignof__(struct property));
985 pp->value = (unsigned char *)(pp + 1);
988 memcpy(pp->value, ps, sz - 1);
989 ((char *)pp->value)[sz - 1] = 0;
990 DBG("fixed up name for %s -> %s\n", pathp, pp->value);
995 np->name = get_property(np, "name", NULL);
996 np->type = get_property(np, "device_type", NULL);
1001 np->type = "<NULL>";
1003 while (tag == OF_DT_BEGIN_NODE) {
1004 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
1005 tag = *((u32 *)(*p));
1007 if (tag != OF_DT_END_NODE) {
1008 printk("Weird tag at end of node: %x\n", tag);
1017 * unflattens the device-tree passed by the firmware, creating the
1018 * tree of struct device_node. It also fills the "name" and "type"
1019 * pointers of the nodes so the normal device-tree walking functions
1020 * can be used (this used to be done by finish_device_tree)
1022 void __init unflatten_device_tree(void)
1024 unsigned long start, mem, size;
1025 struct device_node **allnextp = &allnodes;
1029 DBG(" -> unflatten_device_tree()\n");
1031 /* First pass, scan for size */
1032 start = ((unsigned long)initial_boot_params) +
1033 initial_boot_params->off_dt_struct;
1034 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
1035 size = (size | 3) + 1;
1037 DBG(" size is %lx, allocating...\n", size);
1039 /* Allocate memory for the expanded device tree */
1040 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
1042 DBG("Couldn't allocate memory with lmb_alloc()!\n");
1043 panic("Couldn't allocate memory with lmb_alloc()!\n");
1045 mem = (unsigned long) __va(mem);
1047 ((u32 *)mem)[size / 4] = 0xdeadbeef;
1049 DBG(" unflattening %lx...\n", mem);
1051 /* Second pass, do actual unflattening */
1052 start = ((unsigned long)initial_boot_params) +
1053 initial_boot_params->off_dt_struct;
1054 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
1055 if (*((u32 *)start) != OF_DT_END)
1056 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
1057 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
1058 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
1059 ((u32 *)mem)[size / 4] );
1062 /* Get pointer to OF "/chosen" node for use everywhere */
1063 of_chosen = of_find_node_by_path("/chosen");
1064 if (of_chosen == NULL)
1065 of_chosen = of_find_node_by_path("/chosen@0");
1067 /* Retreive command line */
1068 if (of_chosen != NULL) {
1069 p = (char *)get_property(of_chosen, "bootargs", &l);
1070 if (p != NULL && l > 0)
1071 strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
1073 #ifdef CONFIG_CMDLINE
1074 if (l == 0 || (l == 1 && (*p) == 0))
1075 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1076 #endif /* CONFIG_CMDLINE */
1078 DBG("Command line is: %s\n", cmd_line);
1080 DBG(" <- unflatten_device_tree()\n");
1084 static int __init early_init_dt_scan_cpus(unsigned long node,
1085 const char *uname, int depth, void *data)
1087 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1089 unsigned long size = 0;
1091 /* We are scanning "cpu" nodes only */
1092 if (type == NULL || strcmp(type, "cpu") != 0)
1096 boot_cpuid_phys = 0;
1097 if (initial_boot_params && initial_boot_params->version >= 2) {
1098 /* version 2 of the kexec param format adds the phys cpuid
1101 boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
1103 /* Check if it's the boot-cpu, set it's hw index now */
1104 if (of_get_flat_dt_prop(node,
1105 "linux,boot-cpu", NULL) != NULL) {
1106 prop = of_get_flat_dt_prop(node, "reg", NULL);
1108 boot_cpuid_phys = *prop;
1111 set_hard_smp_processor_id(0, boot_cpuid_phys);
1113 #ifdef CONFIG_ALTIVEC
1114 /* Check if we have a VMX and eventually update CPU features */
1115 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", &size);
1116 if (prop && (*prop) > 0) {
1117 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1118 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1121 /* Same goes for Apple's "altivec" property */
1122 prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
1124 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1125 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1127 #endif /* CONFIG_ALTIVEC */
1129 #ifdef CONFIG_PPC_PSERIES
1131 * Check for an SMT capable CPU and set the CPU feature. We do
1132 * this by looking at the size of the ibm,ppc-interrupt-server#s
1135 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
1137 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
1138 if (prop && ((size / sizeof(u32)) > 1))
1139 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
1145 static int __init early_init_dt_scan_chosen(unsigned long node,
1146 const char *uname, int depth, void *data)
1149 unsigned long *lprop;
1151 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1154 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
1157 /* get platform type */
1158 prop = (u32 *)of_get_flat_dt_prop(node, "linux,platform", NULL);
1161 #ifdef CONFIG_PPC_MULTIPLATFORM
1166 /* check if iommu is forced on or off */
1167 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
1169 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
1173 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
1175 memory_limit = *lprop;
1178 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
1180 tce_alloc_start = *lprop;
1181 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
1183 tce_alloc_end = *lprop;
1186 #ifdef CONFIG_PPC_RTAS
1187 /* To help early debugging via the front panel, we retreive a minimal
1188 * set of RTAS infos now if available
1191 u64 *basep, *entryp;
1193 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
1194 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1195 prop = of_get_flat_dt_prop(node, "linux,rtas-size", NULL);
1196 if (basep && entryp && prop) {
1198 rtas.entry = *entryp;
1202 #endif /* CONFIG_PPC_RTAS */
1208 static int __init early_init_dt_scan_root(unsigned long node,
1209 const char *uname, int depth, void *data)
1216 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
1217 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1218 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
1220 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
1221 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1222 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1228 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1233 /* Ignore more than 2 cells */
1234 while (s > sizeof(unsigned long) / 4) {
1252 static int __init early_init_dt_scan_memory(unsigned long node,
1253 const char *uname, int depth, void *data)
1255 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1259 /* We are scanning "memory" nodes only */
1262 * The longtrail doesn't have a device_type on the
1263 * /memory node, so look for the node called /memory@0.
1265 if (depth != 1 || strcmp(uname, "memory@0") != 0)
1267 } else if (strcmp(type, "memory") != 0)
1270 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
1274 endp = reg + (l / sizeof(cell_t));
1276 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
1277 uname, l, reg[0], reg[1], reg[2], reg[3]);
1279 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1280 unsigned long base, size;
1282 base = dt_mem_next_cell(dt_root_addr_cells, ®);
1283 size = dt_mem_next_cell(dt_root_size_cells, ®);
1287 DBG(" - %lx , %lx\n", base, size);
1290 if (base >= 0x80000000ul)
1292 if ((base + size) > 0x80000000ul)
1293 size = 0x80000000ul - base;
1296 lmb_add(base, size);
1301 static void __init early_reserve_mem(void)
1303 unsigned long base, size;
1304 unsigned long *reserve_map;
1306 reserve_map = (unsigned long *)(((unsigned long)initial_boot_params) +
1307 initial_boot_params->off_mem_rsvmap);
1309 base = *(reserve_map++);
1310 size = *(reserve_map++);
1313 DBG("reserving: %lx -> %lx\n", base, size);
1314 lmb_reserve(base, size);
1318 DBG("memory reserved, lmbs :\n");
1323 void __init early_init_devtree(void *params)
1325 DBG(" -> early_init_devtree()\n");
1327 /* Setup flat device-tree pointer */
1328 initial_boot_params = params;
1330 /* Retrieve various informations from the /chosen node of the
1331 * device-tree, including the platform type, initrd location and
1332 * size, TCE reserve, and more ...
1334 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
1336 /* Scan memory nodes and rebuild LMBs */
1338 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1339 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1340 lmb_enforce_memory_limit(memory_limit);
1342 lmb_reserve(0, __pa(klimit));
1344 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1346 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1347 early_reserve_mem();
1349 DBG("Scanning CPUs ...\n");
1351 /* Retreive CPU related informations from the flat tree
1352 * (altivec support, boot CPU ID, ...)
1354 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
1356 DBG(" <- early_init_devtree()\n");
1362 prom_n_addr_cells(struct device_node* np)
1368 ip = (int *) get_property(np, "#address-cells", NULL);
1371 } while (np->parent);
1372 /* No #address-cells property for the root node, default to 1 */
1377 prom_n_size_cells(struct device_node* np)
1383 ip = (int *) get_property(np, "#size-cells", NULL);
1386 } while (np->parent);
1387 /* No #size-cells property for the root node, default to 1 */
1392 * Work out the sense (active-low level / active-high edge)
1393 * of each interrupt from the device tree.
1395 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1397 struct device_node *np;
1400 /* default to level-triggered */
1401 memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
1403 for (np = allnodes; np != 0; np = np->allnext) {
1404 for (j = 0; j < np->n_intrs; j++) {
1405 i = np->intrs[j].line;
1406 if (i >= off && i < max)
1407 senses[i-off] = np->intrs[j].sense;
1413 * Construct and return a list of the device_nodes with a given name.
1415 struct device_node *find_devices(const char *name)
1417 struct device_node *head, **prevp, *np;
1420 for (np = allnodes; np != 0; np = np->allnext) {
1421 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1429 EXPORT_SYMBOL(find_devices);
1432 * Construct and return a list of the device_nodes with a given type.
1434 struct device_node *find_type_devices(const char *type)
1436 struct device_node *head, **prevp, *np;
1439 for (np = allnodes; np != 0; np = np->allnext) {
1440 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1448 EXPORT_SYMBOL(find_type_devices);
1451 * Returns all nodes linked together
1453 struct device_node *find_all_nodes(void)
1455 struct device_node *head, **prevp, *np;
1458 for (np = allnodes; np != 0; np = np->allnext) {
1465 EXPORT_SYMBOL(find_all_nodes);
1467 /** Checks if the given "compat" string matches one of the strings in
1468 * the device's "compatible" property
1470 int device_is_compatible(struct device_node *device, const char *compat)
1475 cp = (char *) get_property(device, "compatible", &cplen);
1479 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1488 EXPORT_SYMBOL(device_is_compatible);
1492 * Indicates whether the root node has a given value in its
1493 * compatible property.
1495 int machine_is_compatible(const char *compat)
1497 struct device_node *root;
1500 root = of_find_node_by_path("/");
1502 rc = device_is_compatible(root, compat);
1507 EXPORT_SYMBOL(machine_is_compatible);
1510 * Construct and return a list of the device_nodes with a given type
1511 * and compatible property.
1513 struct device_node *find_compatible_devices(const char *type,
1516 struct device_node *head, **prevp, *np;
1519 for (np = allnodes; np != 0; np = np->allnext) {
1521 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1523 if (device_is_compatible(np, compat)) {
1531 EXPORT_SYMBOL(find_compatible_devices);
1534 * Find the device_node with a given full_name.
1536 struct device_node *find_path_device(const char *path)
1538 struct device_node *np;
1540 for (np = allnodes; np != 0; np = np->allnext)
1541 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1545 EXPORT_SYMBOL(find_path_device);
1549 * New implementation of the OF "find" APIs, return a refcounted
1550 * object, call of_node_put() when done. The device tree and list
1551 * are protected by a rw_lock.
1553 * Note that property management will need some locking as well,
1554 * this isn't dealt with yet.
1559 * of_find_node_by_name - Find a node by its "name" property
1560 * @from: The node to start searching from or NULL, the node
1561 * you pass will not be searched, only the next one
1562 * will; typically, you pass what the previous call
1563 * returned. of_node_put() will be called on it
1564 * @name: The name string to match against
1566 * Returns a node pointer with refcount incremented, use
1567 * of_node_put() on it when done.
1569 struct device_node *of_find_node_by_name(struct device_node *from,
1572 struct device_node *np;
1574 read_lock(&devtree_lock);
1575 np = from ? from->allnext : allnodes;
1576 for (; np != 0; np = np->allnext)
1577 if (np->name != 0 && strcasecmp(np->name, name) == 0
1582 read_unlock(&devtree_lock);
1585 EXPORT_SYMBOL(of_find_node_by_name);
1588 * of_find_node_by_type - Find a node by its "device_type" property
1589 * @from: The node to start searching from or NULL, the node
1590 * you pass will not be searched, only the next one
1591 * will; typically, you pass what the previous call
1592 * returned. of_node_put() will be called on it
1593 * @name: The type string to match against
1595 * Returns a node pointer with refcount incremented, use
1596 * of_node_put() on it when done.
1598 struct device_node *of_find_node_by_type(struct device_node *from,
1601 struct device_node *np;
1603 read_lock(&devtree_lock);
1604 np = from ? from->allnext : allnodes;
1605 for (; np != 0; np = np->allnext)
1606 if (np->type != 0 && strcasecmp(np->type, type) == 0
1611 read_unlock(&devtree_lock);
1614 EXPORT_SYMBOL(of_find_node_by_type);
1617 * of_find_compatible_node - Find a node based on type and one of the
1618 * tokens in its "compatible" property
1619 * @from: The node to start searching from or NULL, the node
1620 * you pass will not be searched, only the next one
1621 * will; typically, you pass what the previous call
1622 * returned. of_node_put() will be called on it
1623 * @type: The type string to match "device_type" or NULL to ignore
1624 * @compatible: The string to match to one of the tokens in the device
1625 * "compatible" list.
1627 * Returns a node pointer with refcount incremented, use
1628 * of_node_put() on it when done.
1630 struct device_node *of_find_compatible_node(struct device_node *from,
1631 const char *type, const char *compatible)
1633 struct device_node *np;
1635 read_lock(&devtree_lock);
1636 np = from ? from->allnext : allnodes;
1637 for (; np != 0; np = np->allnext) {
1639 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1641 if (device_is_compatible(np, compatible) && of_node_get(np))
1646 read_unlock(&devtree_lock);
1649 EXPORT_SYMBOL(of_find_compatible_node);
1652 * of_find_node_by_path - Find a node matching a full OF path
1653 * @path: The full path to match
1655 * Returns a node pointer with refcount incremented, use
1656 * of_node_put() on it when done.
1658 struct device_node *of_find_node_by_path(const char *path)
1660 struct device_node *np = allnodes;
1662 read_lock(&devtree_lock);
1663 for (; np != 0; np = np->allnext) {
1664 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1668 read_unlock(&devtree_lock);
1671 EXPORT_SYMBOL(of_find_node_by_path);
1674 * of_find_node_by_phandle - Find a node given a phandle
1675 * @handle: phandle of the node to find
1677 * Returns a node pointer with refcount incremented, use
1678 * of_node_put() on it when done.
1680 struct device_node *of_find_node_by_phandle(phandle handle)
1682 struct device_node *np;
1684 read_lock(&devtree_lock);
1685 for (np = allnodes; np != 0; np = np->allnext)
1686 if (np->linux_phandle == handle)
1690 read_unlock(&devtree_lock);
1693 EXPORT_SYMBOL(of_find_node_by_phandle);
1696 * of_find_all_nodes - Get next node in global list
1697 * @prev: Previous node or NULL to start iteration
1698 * of_node_put() will be called on it
1700 * Returns a node pointer with refcount incremented, use
1701 * of_node_put() on it when done.
1703 struct device_node *of_find_all_nodes(struct device_node *prev)
1705 struct device_node *np;
1707 read_lock(&devtree_lock);
1708 np = prev ? prev->allnext : allnodes;
1709 for (; np != 0; np = np->allnext)
1710 if (of_node_get(np))
1714 read_unlock(&devtree_lock);
1717 EXPORT_SYMBOL(of_find_all_nodes);
1720 * of_get_parent - Get a node's parent if any
1721 * @node: Node to get parent
1723 * Returns a node pointer with refcount incremented, use
1724 * of_node_put() on it when done.
1726 struct device_node *of_get_parent(const struct device_node *node)
1728 struct device_node *np;
1733 read_lock(&devtree_lock);
1734 np = of_node_get(node->parent);
1735 read_unlock(&devtree_lock);
1738 EXPORT_SYMBOL(of_get_parent);
1741 * of_get_next_child - Iterate a node childs
1742 * @node: parent node
1743 * @prev: previous child of the parent node, or NULL to get first
1745 * Returns a node pointer with refcount incremented, use
1746 * of_node_put() on it when done.
1748 struct device_node *of_get_next_child(const struct device_node *node,
1749 struct device_node *prev)
1751 struct device_node *next;
1753 read_lock(&devtree_lock);
1754 next = prev ? prev->sibling : node->child;
1755 for (; next != 0; next = next->sibling)
1756 if (of_node_get(next))
1760 read_unlock(&devtree_lock);
1763 EXPORT_SYMBOL(of_get_next_child);
1766 * of_node_get - Increment refcount of a node
1767 * @node: Node to inc refcount, NULL is supported to
1768 * simplify writing of callers
1772 struct device_node *of_node_get(struct device_node *node)
1775 kref_get(&node->kref);
1778 EXPORT_SYMBOL(of_node_get);
1780 static inline struct device_node * kref_to_device_node(struct kref *kref)
1782 return container_of(kref, struct device_node, kref);
1786 * of_node_release - release a dynamically allocated node
1787 * @kref: kref element of the node to be released
1789 * In of_node_put() this function is passed to kref_put()
1790 * as the destructor.
1792 static void of_node_release(struct kref *kref)
1794 struct device_node *node = kref_to_device_node(kref);
1795 struct property *prop = node->properties;
1797 if (!OF_IS_DYNAMIC(node))
1800 struct property *next = prop->next;
1808 kfree(node->full_name);
1814 * of_node_put - Decrement refcount of a node
1815 * @node: Node to dec refcount, NULL is supported to
1816 * simplify writing of callers
1819 void of_node_put(struct device_node *node)
1822 kref_put(&node->kref, of_node_release);
1824 EXPORT_SYMBOL(of_node_put);
1827 * Plug a device node into the tree and global list.
1829 void of_attach_node(struct device_node *np)
1831 write_lock(&devtree_lock);
1832 np->sibling = np->parent->child;
1833 np->allnext = allnodes;
1834 np->parent->child = np;
1836 write_unlock(&devtree_lock);
1840 * "Unplug" a node from the device tree. The caller must hold
1841 * a reference to the node. The memory associated with the node
1842 * is not freed until its refcount goes to zero.
1844 void of_detach_node(const struct device_node *np)
1846 struct device_node *parent;
1848 write_lock(&devtree_lock);
1850 parent = np->parent;
1853 allnodes = np->allnext;
1855 struct device_node *prev;
1856 for (prev = allnodes;
1857 prev->allnext != np;
1858 prev = prev->allnext)
1860 prev->allnext = np->allnext;
1863 if (parent->child == np)
1864 parent->child = np->sibling;
1866 struct device_node *prevsib;
1867 for (prevsib = np->parent->child;
1868 prevsib->sibling != np;
1869 prevsib = prevsib->sibling)
1871 prevsib->sibling = np->sibling;
1874 write_unlock(&devtree_lock);
1877 #ifdef CONFIG_PPC_PSERIES
1879 * Fix up the uninitialized fields in a new device node:
1880 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1882 * A lot of boot-time code is duplicated here, because functions such
1883 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1886 * This should probably be split up into smaller chunks.
1889 static int of_finish_dynamic_node(struct device_node *node,
1890 unsigned long *unused1, int unused2,
1891 int unused3, int unused4)
1893 struct device_node *parent = of_get_parent(node);
1895 phandle *ibm_phandle;
1897 node->name = get_property(node, "name", NULL);
1898 node->type = get_property(node, "device_type", NULL);
1905 /* We don't support that function on PowerMac, at least
1908 if (_machine == PLATFORM_POWERMAC)
1911 /* fix up new node's linux_phandle field */
1912 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
1913 node->linux_phandle = *ibm_phandle;
1916 of_node_put(parent);
1920 static int prom_reconfig_notifier(struct notifier_block *nb,
1921 unsigned long action, void *node)
1926 case PSERIES_RECONFIG_ADD:
1927 err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
1929 printk(KERN_ERR "finish_node returned %d\n", err);
1940 static struct notifier_block prom_reconfig_nb = {
1941 .notifier_call = prom_reconfig_notifier,
1942 .priority = 10, /* This one needs to run first */
1945 static int __init prom_reconfig_setup(void)
1947 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1949 __initcall(prom_reconfig_setup);
1953 * Find a property with a given name for a given node
1954 * and return the value.
1956 unsigned char *get_property(struct device_node *np, const char *name,
1959 struct property *pp;
1961 for (pp = np->properties; pp != 0; pp = pp->next)
1962 if (strcmp(pp->name, name) == 0) {
1969 EXPORT_SYMBOL(get_property);
1972 * Add a property to a node
1974 int prom_add_property(struct device_node* np, struct property* prop)
1976 struct property **next;
1979 write_lock(&devtree_lock);
1980 next = &np->properties;
1982 if (strcmp(prop->name, (*next)->name) == 0) {
1983 /* duplicate ! don't insert it */
1984 write_unlock(&devtree_lock);
1987 next = &(*next)->next;
1990 write_unlock(&devtree_lock);
1992 #ifdef CONFIG_PROC_DEVICETREE
1993 /* try to add to proc as well if it was initialized */
1995 proc_device_tree_add_prop(np->pde, prop);
1996 #endif /* CONFIG_PROC_DEVICETREE */
2001 /* I quickly hacked that one, check against spec ! */
2002 static inline unsigned long
2003 bus_space_to_resource_flags(unsigned int bus_space)
2005 u8 space = (bus_space >> 24) & 0xf;
2009 return IORESOURCE_MEM;
2010 else if (space == 0x01)
2011 return IORESOURCE_IO;
2013 printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n",
2020 static struct resource *find_parent_pci_resource(struct pci_dev* pdev,
2021 struct address_range *range)
2026 /* Check this one */
2027 mask = bus_space_to_resource_flags(range->space);
2028 for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
2029 if ((pdev->resource[i].flags & mask) == mask &&
2030 pdev->resource[i].start <= range->address &&
2031 pdev->resource[i].end > range->address) {
2032 if ((range->address + range->size - 1) > pdev->resource[i].end) {
2033 /* Add better message */
2034 printk(KERN_WARNING "PCI/OF resource overlap !\n");
2040 if (i == DEVICE_COUNT_RESOURCE)
2042 return &pdev->resource[i];
2046 * Request an OF device resource. Currently handles child of PCI devices,
2047 * or other nodes attached to the root node. Ultimately, put some
2048 * link to resources in the OF node.
2050 struct resource *request_OF_resource(struct device_node* node, int index,
2051 const char* name_postfix)
2053 struct pci_dev* pcidev;
2054 u8 pci_bus, pci_devfn;
2055 unsigned long iomask;
2056 struct device_node* nd;
2057 struct resource* parent;
2058 struct resource *res = NULL;
2061 if (index >= node->n_addrs)
2064 /* Sanity check on bus space */
2065 iomask = bus_space_to_resource_flags(node->addrs[index].space);
2066 if (iomask & IORESOURCE_MEM)
2067 parent = &iomem_resource;
2068 else if (iomask & IORESOURCE_IO)
2069 parent = &ioport_resource;
2073 /* Find a PCI parent if any */
2077 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
2078 pcidev = pci_find_slot(pci_bus, pci_devfn);
2083 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
2085 printk(KERN_WARNING "request_OF_resource(%s), parent not found\n",
2090 res = __request_region(parent, node->addrs[index].address,
2091 node->addrs[index].size, NULL);
2094 nlen = strlen(node->name);
2095 plen = name_postfix ? strlen(name_postfix) : 0;
2096 res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL);
2098 strcpy((char *)res->name, node->name);
2100 strcpy((char *)res->name+nlen, name_postfix);
2106 EXPORT_SYMBOL(request_OF_resource);
2108 int release_OF_resource(struct device_node *node, int index)
2110 struct pci_dev* pcidev;
2111 u8 pci_bus, pci_devfn;
2112 unsigned long iomask, start, end;
2113 struct device_node* nd;
2114 struct resource* parent;
2115 struct resource *res = NULL;
2117 if (index >= node->n_addrs)
2120 /* Sanity check on bus space */
2121 iomask = bus_space_to_resource_flags(node->addrs[index].space);
2122 if (iomask & IORESOURCE_MEM)
2123 parent = &iomem_resource;
2124 else if (iomask & IORESOURCE_IO)
2125 parent = &ioport_resource;
2129 /* Find a PCI parent if any */
2133 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
2134 pcidev = pci_find_slot(pci_bus, pci_devfn);
2139 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
2141 printk(KERN_WARNING "release_OF_resource(%s), parent not found\n",
2146 /* Find us in the parent and its childs */
2147 res = parent->child;
2148 start = node->addrs[index].address;
2149 end = start + node->addrs[index].size - 1;
2151 if (res->start == start && res->end == end &&
2152 (res->flags & IORESOURCE_BUSY))
2154 if (res->start <= start && res->end >= end)
2166 release_resource(res);
2171 EXPORT_SYMBOL(release_OF_resource);
2172 #endif /* CONFIG_PCI */