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;
114 * Wrapper for allocating memory for various data that needs to be
115 * attached to device nodes as they are processed at boot or when
116 * added to the device tree later (e.g. DLPAR). At boot there is
117 * already a region reserved so we just increment *mem_start by size;
118 * otherwise we call kmalloc.
120 static void * prom_alloc(unsigned long size, unsigned long *mem_start)
125 return kmalloc(size, GFP_KERNEL);
133 * Find the device_node with a given phandle.
135 static struct device_node * find_phandle(phandle ph)
137 struct device_node *np;
139 for (np = allnodes; np != 0; np = np->allnext)
140 if (np->linux_phandle == ph)
146 * Find the interrupt parent of a node.
148 static struct device_node * __devinit intr_parent(struct device_node *p)
152 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
155 p = find_phandle(*parp);
159 * On a powermac booted with BootX, we don't get to know the
160 * phandles for any nodes, so find_phandle will return NULL.
161 * Fortunately these machines only have one interrupt controller
162 * so there isn't in fact any ambiguity. -- paulus
164 if (num_interrupt_controllers == 1)
165 p = dflt_interrupt_controller;
170 * Find out the size of each entry of the interrupts property
173 int __devinit prom_n_intr_cells(struct device_node *np)
175 struct device_node *p;
178 for (p = np; (p = intr_parent(p)) != NULL; ) {
179 icp = (unsigned int *)
180 get_property(p, "#interrupt-cells", NULL);
183 if (get_property(p, "interrupt-controller", NULL) != NULL
184 || get_property(p, "interrupt-map", NULL) != NULL) {
185 printk("oops, node %s doesn't have #interrupt-cells\n",
191 printk("prom_n_intr_cells failed for %s\n", np->full_name);
197 * Map an interrupt from a device up to the platform interrupt
200 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
201 struct device_node *np, unsigned int *ints,
204 struct device_node *p, *ipar;
205 unsigned int *imap, *imask, *ip;
206 int i, imaplen, match;
207 int newintrc = 0, newaddrc = 0;
211 reg = (unsigned int *) get_property(np, "reg", NULL);
212 naddrc = prom_n_addr_cells(np);
215 if (get_property(p, "interrupt-controller", NULL) != NULL)
216 /* this node is an interrupt controller, stop here */
218 imap = (unsigned int *)
219 get_property(p, "interrupt-map", &imaplen);
224 imask = (unsigned int *)
225 get_property(p, "interrupt-map-mask", NULL);
227 printk("oops, %s has interrupt-map but no mask\n",
231 imaplen /= sizeof(unsigned int);
234 while (imaplen > 0 && !match) {
235 /* check the child-interrupt field */
237 for (i = 0; i < naddrc && match; ++i)
238 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
239 for (; i < naddrc + nintrc && match; ++i)
240 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
241 imap += naddrc + nintrc;
242 imaplen -= naddrc + nintrc;
243 /* grab the interrupt parent */
244 ipar = find_phandle((phandle) *imap++);
246 if (ipar == NULL && num_interrupt_controllers == 1)
247 /* cope with BootX not giving us phandles */
248 ipar = dflt_interrupt_controller;
250 printk("oops, no int parent %x in map of %s\n",
251 imap[-1], p->full_name);
254 /* find the parent's # addr and intr cells */
255 ip = (unsigned int *)
256 get_property(ipar, "#interrupt-cells", NULL);
258 printk("oops, no #interrupt-cells on %s\n",
263 ip = (unsigned int *)
264 get_property(ipar, "#address-cells", NULL);
265 newaddrc = (ip == NULL)? 0: *ip;
266 imap += newaddrc + newintrc;
267 imaplen -= newaddrc + newintrc;
270 printk("oops, error decoding int-map on %s, len=%d\n",
271 p->full_name, imaplen);
276 printk("oops, no match in %s int-map for %s\n",
277 p->full_name, np->full_name);
284 ints = imap - nintrc;
289 printk("hmmm, int tree for %s doesn't have ctrler\n",
299 static unsigned char map_isa_senses[4] = {
300 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
301 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
302 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
303 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE
306 static unsigned char map_mpic_senses[4] = {
307 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE,
308 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
309 /* 2 seems to be used for the 8259 cascade... */
310 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
311 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
314 static int __devinit finish_node_interrupts(struct device_node *np,
315 unsigned long *mem_start,
319 int intlen, intrcells, intrcount;
321 unsigned int *irq, virq;
322 struct device_node *ic;
324 if (num_interrupt_controllers == 0) {
326 * Old machines just have a list of interrupt numbers
327 * and no interrupt-controller nodes.
329 ints = (unsigned int *) get_property(np, "AAPL,interrupts",
331 /* XXX old interpret_pci_props looked in parent too */
332 /* XXX old interpret_macio_props looked for interrupts
333 before AAPL,interrupts */
335 ints = (unsigned int *) get_property(np, "interrupts",
340 np->n_intrs = intlen / sizeof(unsigned int);
341 np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]),
348 for (i = 0; i < np->n_intrs; ++i) {
349 np->intrs[i].line = *ints++;
350 np->intrs[i].sense = IRQ_SENSE_LEVEL
351 | IRQ_POLARITY_NEGATIVE;
356 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
359 intrcells = prom_n_intr_cells(np);
360 intlen /= intrcells * sizeof(unsigned int);
362 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
370 for (i = 0; i < intlen; ++i, ints += intrcells) {
371 n = map_interrupt(&irq, &ic, np, ints, intrcells);
375 /* don't map IRQ numbers under a cascaded 8259 controller */
376 if (ic && device_is_compatible(ic, "chrp,iic")) {
377 np->intrs[intrcount].line = irq[0];
378 sense = (n > 1)? (irq[1] & 3): 3;
379 np->intrs[intrcount].sense = map_isa_senses[sense];
381 virq = virt_irq_create_mapping(irq[0]);
383 if (virq == NO_IRQ) {
384 printk(KERN_CRIT "Could not allocate interrupt"
385 " number for %s\n", np->full_name);
389 np->intrs[intrcount].line = irq_offset_up(virq);
390 sense = (n > 1)? (irq[1] & 3): 1;
391 np->intrs[intrcount].sense = map_mpic_senses[sense];
395 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
396 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
397 char *name = get_property(ic->parent, "name", NULL);
398 if (name && !strcmp(name, "u3"))
399 np->intrs[intrcount].line += 128;
400 else if (!(name && !strcmp(name, "mac-io")))
401 /* ignore other cascaded controllers, such as
407 printk("hmmm, got %d intr cells for %s:", n,
409 for (j = 0; j < n; ++j)
410 printk(" %d", irq[j]);
415 np->n_intrs = intrcount;
420 static int __devinit interpret_pci_props(struct device_node *np,
421 unsigned long *mem_start,
422 int naddrc, int nsizec,
425 struct address_range *adr;
426 struct pci_reg_property *pci_addrs;
429 pci_addrs = (struct pci_reg_property *)
430 get_property(np, "assigned-addresses", &l);
434 n_addrs = l / sizeof(*pci_addrs);
436 adr = prom_alloc(n_addrs * sizeof(*adr), mem_start);
444 np->n_addrs = n_addrs;
446 for (i = 0; i < n_addrs; i++) {
447 adr[i].space = pci_addrs[i].addr.a_hi;
448 adr[i].address = pci_addrs[i].addr.a_lo |
449 ((u64)pci_addrs[i].addr.a_mid << 32);
450 adr[i].size = pci_addrs[i].size_lo;
456 static int __init interpret_dbdma_props(struct device_node *np,
457 unsigned long *mem_start,
458 int naddrc, int nsizec,
461 struct reg_property32 *rp;
462 struct address_range *adr;
463 unsigned long base_address;
465 struct device_node *db;
469 for (db = np->parent; db != NULL; db = db->parent) {
470 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
471 base_address = db->addrs[0].address;
477 rp = (struct reg_property32 *) get_property(np, "reg", &l);
478 if (rp != 0 && l >= sizeof(struct reg_property32)) {
480 adr = (struct address_range *) (*mem_start);
481 while ((l -= sizeof(struct reg_property32)) >= 0) {
484 adr[i].address = rp[i].address + base_address;
485 adr[i].size = rp[i].size;
491 (*mem_start) += i * sizeof(struct address_range);
497 static int __init interpret_macio_props(struct device_node *np,
498 unsigned long *mem_start,
499 int naddrc, int nsizec,
502 struct reg_property32 *rp;
503 struct address_range *adr;
504 unsigned long base_address;
506 struct device_node *db;
510 for (db = np->parent; db != NULL; db = db->parent) {
511 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
512 base_address = db->addrs[0].address;
518 rp = (struct reg_property32 *) get_property(np, "reg", &l);
519 if (rp != 0 && l >= sizeof(struct reg_property32)) {
521 adr = (struct address_range *) (*mem_start);
522 while ((l -= sizeof(struct reg_property32)) >= 0) {
525 adr[i].address = rp[i].address + base_address;
526 adr[i].size = rp[i].size;
532 (*mem_start) += i * sizeof(struct address_range);
538 static int __init interpret_isa_props(struct device_node *np,
539 unsigned long *mem_start,
540 int naddrc, int nsizec,
543 struct isa_reg_property *rp;
544 struct address_range *adr;
547 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
548 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
550 adr = (struct address_range *) (*mem_start);
551 while ((l -= sizeof(struct isa_reg_property)) >= 0) {
553 adr[i].space = rp[i].space;
554 adr[i].address = rp[i].address;
555 adr[i].size = rp[i].size;
561 (*mem_start) += i * sizeof(struct address_range);
567 static int __init interpret_root_props(struct device_node *np,
568 unsigned long *mem_start,
569 int naddrc, int nsizec,
572 struct address_range *adr;
575 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
577 rp = (unsigned int *) get_property(np, "reg", &l);
578 if (rp != 0 && l >= rpsize) {
580 adr = (struct address_range *) (*mem_start);
581 while ((l -= rpsize) >= 0) {
584 adr[i].address = rp[naddrc - 1];
585 adr[i].size = rp[naddrc + nsizec - 1];
588 rp += naddrc + nsizec;
592 (*mem_start) += i * sizeof(struct address_range);
598 static int __devinit finish_node(struct device_node *np,
599 unsigned long *mem_start,
600 interpret_func *ifunc,
601 int naddrc, int nsizec,
604 struct device_node *child;
607 /* get the device addresses and interrupts */
609 rc = ifunc(np, mem_start, naddrc, nsizec, measure_only);
613 rc = finish_node_interrupts(np, mem_start, measure_only);
617 /* Look for #address-cells and #size-cells properties. */
618 ip = (int *) get_property(np, "#address-cells", NULL);
621 ip = (int *) get_property(np, "#size-cells", NULL);
625 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
626 ifunc = interpret_root_props;
627 else if (np->type == 0)
629 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
630 ifunc = interpret_pci_props;
631 else if (!strcmp(np->type, "dbdma"))
632 ifunc = interpret_dbdma_props;
633 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
634 ifunc = interpret_macio_props;
635 else if (!strcmp(np->type, "isa"))
636 ifunc = interpret_isa_props;
637 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
638 ifunc = interpret_root_props;
639 else if (!((ifunc == interpret_dbdma_props
640 || ifunc == interpret_macio_props)
641 && (!strcmp(np->type, "escc")
642 || !strcmp(np->type, "media-bay"))))
645 for (child = np->child; child != NULL; child = child->sibling) {
646 rc = finish_node(child, mem_start, ifunc,
647 naddrc, nsizec, measure_only);
655 static void __init scan_interrupt_controllers(void)
657 struct device_node *np;
662 for (np = allnodes; np != NULL; np = np->allnext) {
663 ic = get_property(np, "interrupt-controller", &iclen);
664 name = get_property(np, "name", NULL);
665 /* checking iclen makes sure we don't get a false
666 match on /chosen.interrupt_controller */
668 && strcmp(name, "interrupt-controller") == 0)
669 || (ic != NULL && iclen == 0
670 && strcmp(name, "AppleKiwi"))) {
672 dflt_interrupt_controller = np;
676 num_interrupt_controllers = n;
680 * finish_device_tree is called once things are running normally
681 * (i.e. with text and data mapped to the address they were linked at).
682 * It traverses the device tree and fills in some of the additional,
683 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
684 * mapping is also initialized at this point.
686 void __init finish_device_tree(void)
688 unsigned long start, end, size = 0;
690 DBG(" -> finish_device_tree\n");
693 /* Initialize virtual IRQ map */
696 scan_interrupt_controllers();
699 * Finish device-tree (pre-parsing some properties etc...)
700 * We do this in 2 passes. One with "measure_only" set, which
701 * will only measure the amount of memory needed, then we can
702 * allocate that memory, and call finish_node again. However,
703 * we must be careful as most routines will fail nowadays when
704 * prom_alloc() returns 0, so we must make sure our first pass
705 * doesn't start at 0. We pre-initialize size to 16 for that
706 * reason and then remove those additional 16 bytes
709 finish_node(allnodes, &size, NULL, 0, 0, 1);
711 end = start = (unsigned long) __va(lmb_alloc(size, 128));
712 finish_node(allnodes, &end, NULL, 0, 0, 0);
713 BUG_ON(end != start + size);
715 DBG(" <- finish_device_tree\n");
718 static inline char *find_flat_dt_string(u32 offset)
720 return ((char *)initial_boot_params) +
721 initial_boot_params->off_dt_strings + offset;
725 * This function is used to scan the flattened device-tree, it is
726 * used to extract the memory informations at boot before we can
729 static int __init scan_flat_dt(int (*it)(unsigned long node,
730 const char *uname, int depth,
734 unsigned long p = ((unsigned long)initial_boot_params) +
735 initial_boot_params->off_dt_struct;
740 u32 tag = *((u32 *)p);
744 if (tag == OF_DT_END_NODE) {
748 if (tag == OF_DT_NOP)
750 if (tag == OF_DT_END)
752 if (tag == OF_DT_PROP) {
753 u32 sz = *((u32 *)p);
755 if (initial_boot_params->version < 0x10)
756 p = _ALIGN(p, sz >= 8 ? 8 : 4);
761 if (tag != OF_DT_BEGIN_NODE) {
762 printk(KERN_WARNING "Invalid tag %x scanning flattened"
763 " device tree !\n", tag);
768 p = _ALIGN(p + strlen(pathp) + 1, 4);
769 if ((*pathp) == '/') {
771 for (lp = NULL, np = pathp; *np; np++)
777 rc = it(p, pathp, depth, data);
786 * This function can be used within scan_flattened_dt callback to get
787 * access to properties
789 static void* __init get_flat_dt_prop(unsigned long node, const char *name,
792 unsigned long p = node;
795 u32 tag = *((u32 *)p);
800 if (tag == OF_DT_NOP)
802 if (tag != OF_DT_PROP)
806 noff = *((u32 *)(p + 4));
808 if (initial_boot_params->version < 0x10)
809 p = _ALIGN(p, sz >= 8 ? 8 : 4);
811 nstr = find_flat_dt_string(noff);
813 printk(KERN_WARNING "Can't find property index"
817 if (strcmp(name, nstr) == 0) {
827 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
832 *mem = _ALIGN(*mem, align);
839 static unsigned long __init unflatten_dt_node(unsigned long mem,
841 struct device_node *dad,
842 struct device_node ***allnextpp,
843 unsigned long fpsize)
845 struct device_node *np;
846 struct property *pp, **prev_pp = NULL;
849 unsigned int l, allocl;
853 tag = *((u32 *)(*p));
854 if (tag != OF_DT_BEGIN_NODE) {
855 printk("Weird tag at start of node: %x\n", tag);
860 l = allocl = strlen(pathp) + 1;
861 *p = _ALIGN(*p + l, 4);
863 /* version 0x10 has a more compact unit name here instead of the full
864 * path. we accumulate the full path size using "fpsize", we'll rebuild
865 * it later. We detect this because the first character of the name is
868 if ((*pathp) != '/') {
871 /* root node: special case. fpsize accounts for path
872 * plus terminating zero. root node only has '/', so
873 * fpsize should be 2, but we want to avoid the first
874 * level nodes to have two '/' so we use fpsize 1 here
879 /* account for '/' and path size minus terminal 0
888 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
889 __alignof__(struct device_node));
891 memset(np, 0, sizeof(*np));
892 np->full_name = ((char*)np) + sizeof(struct device_node);
894 char *p = np->full_name;
895 /* rebuild full path for new format */
896 if (dad && dad->parent) {
897 strcpy(p, dad->full_name);
899 if ((strlen(p) + l + 1) != allocl) {
900 DBG("%s: p: %d, l: %d, a: %d\n",
901 pathp, strlen(p), l, allocl);
909 memcpy(np->full_name, pathp, l);
910 prev_pp = &np->properties;
912 *allnextpp = &np->allnext;
915 /* we temporarily use the next field as `last_child'*/
919 dad->next->sibling = np;
922 kref_init(&np->kref);
928 tag = *((u32 *)(*p));
929 if (tag == OF_DT_NOP) {
933 if (tag != OF_DT_PROP)
937 noff = *((u32 *)((*p) + 4));
939 if (initial_boot_params->version < 0x10)
940 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
942 pname = find_flat_dt_string(noff);
944 printk("Can't find property name in list !\n");
947 if (strcmp(pname, "name") == 0)
949 l = strlen(pname) + 1;
950 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
951 __alignof__(struct property));
953 if (strcmp(pname, "linux,phandle") == 0) {
954 np->node = *((u32 *)*p);
955 if (np->linux_phandle == 0)
956 np->linux_phandle = np->node;
958 if (strcmp(pname, "ibm,phandle") == 0)
959 np->linux_phandle = *((u32 *)*p);
962 pp->value = (void *)*p;
966 *p = _ALIGN((*p) + sz, 4);
968 /* with version 0x10 we may not have the name property, recreate
969 * it here from the unit name if absent
972 char *p = pathp, *ps = pathp, *pa = NULL;
985 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
986 __alignof__(struct property));
990 pp->value = (unsigned char *)(pp + 1);
993 memcpy(pp->value, ps, sz - 1);
994 ((char *)pp->value)[sz - 1] = 0;
995 DBG("fixed up name for %s -> %s\n", pathp, pp->value);
1000 np->name = get_property(np, "name", NULL);
1001 np->type = get_property(np, "device_type", NULL);
1004 np->name = "<NULL>";
1006 np->type = "<NULL>";
1008 while (tag == OF_DT_BEGIN_NODE) {
1009 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
1010 tag = *((u32 *)(*p));
1012 if (tag != OF_DT_END_NODE) {
1013 printk("Weird tag at end of node: %x\n", tag);
1022 * unflattens the device-tree passed by the firmware, creating the
1023 * tree of struct device_node. It also fills the "name" and "type"
1024 * pointers of the nodes so the normal device-tree walking functions
1025 * can be used (this used to be done by finish_device_tree)
1027 void __init unflatten_device_tree(void)
1029 unsigned long start, mem, size;
1030 struct device_node **allnextp = &allnodes;
1034 DBG(" -> unflatten_device_tree()\n");
1036 /* First pass, scan for size */
1037 start = ((unsigned long)initial_boot_params) +
1038 initial_boot_params->off_dt_struct;
1039 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
1040 size = (size | 3) + 1;
1042 DBG(" size is %lx, allocating...\n", size);
1044 /* Allocate memory for the expanded device tree */
1045 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
1047 DBG("Couldn't allocate memory with lmb_alloc()!\n");
1048 panic("Couldn't allocate memory with lmb_alloc()!\n");
1050 mem = (unsigned long) __va(mem);
1052 ((u32 *)mem)[size / 4] = 0xdeadbeef;
1054 DBG(" unflattening %lx...\n", mem);
1056 /* Second pass, do actual unflattening */
1057 start = ((unsigned long)initial_boot_params) +
1058 initial_boot_params->off_dt_struct;
1059 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
1060 if (*((u32 *)start) != OF_DT_END)
1061 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
1062 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
1063 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
1064 ((u32 *)mem)[size / 4] );
1067 /* Get pointer to OF "/chosen" node for use everywhere */
1068 of_chosen = of_find_node_by_path("/chosen");
1069 if (of_chosen == NULL)
1070 of_chosen = of_find_node_by_path("/chosen@0");
1072 /* Retreive command line */
1073 if (of_chosen != NULL) {
1074 p = (char *)get_property(of_chosen, "bootargs", &l);
1075 if (p != NULL && l > 0)
1076 strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
1078 #ifdef CONFIG_CMDLINE
1079 if (l == 0 || (l == 1 && (*p) == 0))
1080 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1081 #endif /* CONFIG_CMDLINE */
1083 DBG("Command line is: %s\n", cmd_line);
1085 DBG(" <- unflatten_device_tree()\n");
1089 static int __init early_init_dt_scan_cpus(unsigned long node,
1090 const char *uname, int depth, void *data)
1092 char *type = get_flat_dt_prop(node, "device_type", NULL);
1094 unsigned long size = 0;
1096 /* We are scanning "cpu" nodes only */
1097 if (type == NULL || strcmp(type, "cpu") != 0)
1100 #ifdef CONFIG_PPC_PSERIES
1101 /* On LPAR, look for the first ibm,pft-size property for the hash table size
1103 if (systemcfg->platform == PLATFORM_PSERIES_LPAR && ppc64_pft_size == 0) {
1105 pft_size = get_flat_dt_prop(node, "ibm,pft-size", NULL);
1106 if (pft_size != NULL) {
1107 /* pft_size[0] is the NUMA CEC cookie */
1108 ppc64_pft_size = pft_size[1];
1114 boot_cpuid_phys = 0;
1115 if (initial_boot_params && initial_boot_params->version >= 2) {
1116 /* version 2 of the kexec param format adds the phys cpuid
1119 boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
1121 /* Check if it's the boot-cpu, set it's hw index now */
1122 if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
1123 prop = get_flat_dt_prop(node, "reg", NULL);
1125 boot_cpuid_phys = *prop;
1128 set_hard_smp_processor_id(0, boot_cpuid_phys);
1130 #ifdef CONFIG_ALTIVEC
1131 /* Check if we have a VMX and eventually update CPU features */
1132 prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", &size);
1133 if (prop && (*prop) > 0) {
1134 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1135 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1138 /* Same goes for Apple's "altivec" property */
1139 prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL);
1141 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1142 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1144 #endif /* CONFIG_ALTIVEC */
1146 #ifdef CONFIG_PPC_PSERIES
1148 * Check for an SMT capable CPU and set the CPU feature. We do
1149 * this by looking at the size of the ibm,ppc-interrupt-server#s
1152 prop = (u32 *)get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
1154 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
1155 if (prop && ((size / sizeof(u32)) > 1))
1156 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
1162 static int __init early_init_dt_scan_chosen(unsigned long node,
1163 const char *uname, int depth, void *data)
1166 unsigned long *lprop;
1168 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1171 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
1174 /* get platform type */
1175 prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
1179 systemcfg->platform = *prop;
1181 #ifdef CONFIG_PPC_MULTIPLATFORM
1187 /* check if iommu is forced on or off */
1188 if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
1190 if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
1194 lprop = get_flat_dt_prop(node, "linux,memory-limit", NULL);
1196 memory_limit = *lprop;
1199 lprop = get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
1201 tce_alloc_start = *lprop;
1202 lprop = get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
1204 tce_alloc_end = *lprop;
1207 #ifdef CONFIG_PPC_RTAS
1208 /* To help early debugging via the front panel, we retreive a minimal
1209 * set of RTAS infos now if available
1212 u64 *basep, *entryp;
1214 basep = get_flat_dt_prop(node, "linux,rtas-base", NULL);
1215 entryp = get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1216 prop = get_flat_dt_prop(node, "linux,rtas-size", NULL);
1217 if (basep && entryp && prop) {
1219 rtas.entry = *entryp;
1223 #endif /* CONFIG_PPC_RTAS */
1229 static int __init early_init_dt_scan_root(unsigned long node,
1230 const char *uname, int depth, void *data)
1237 prop = get_flat_dt_prop(node, "#size-cells", NULL);
1238 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1239 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
1241 prop = get_flat_dt_prop(node, "#address-cells", NULL);
1242 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1243 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1249 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1254 /* Ignore more than 2 cells */
1255 while (s > sizeof(unsigned long) / 4) {
1273 static int __init early_init_dt_scan_memory(unsigned long node,
1274 const char *uname, int depth, void *data)
1276 char *type = get_flat_dt_prop(node, "device_type", NULL);
1280 /* We are scanning "memory" nodes only */
1281 if (type == NULL || strcmp(type, "memory") != 0)
1284 reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
1288 endp = reg + (l / sizeof(cell_t));
1290 DBG("memory scan node %s ..., reg size %ld, data: %x %x %x %x, ...\n",
1291 uname, l, reg[0], reg[1], reg[2], reg[3]);
1293 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1294 unsigned long base, size;
1296 base = dt_mem_next_cell(dt_root_addr_cells, ®);
1297 size = dt_mem_next_cell(dt_root_size_cells, ®);
1301 DBG(" - %lx , %lx\n", base, size);
1304 if (base >= 0x80000000ul)
1306 if ((base + size) > 0x80000000ul)
1307 size = 0x80000000ul - base;
1310 lmb_add(base, size);
1315 static void __init early_reserve_mem(void)
1317 unsigned long base, size;
1318 unsigned long *reserve_map;
1320 reserve_map = (unsigned long *)(((unsigned long)initial_boot_params) +
1321 initial_boot_params->off_mem_rsvmap);
1323 base = *(reserve_map++);
1324 size = *(reserve_map++);
1327 DBG("reserving: %lx -> %lx\n", base, size);
1328 lmb_reserve(base, size);
1332 DBG("memory reserved, lmbs :\n");
1337 void __init early_init_devtree(void *params)
1339 DBG(" -> early_init_devtree()\n");
1341 /* Setup flat device-tree pointer */
1342 initial_boot_params = params;
1344 /* Retrieve various informations from the /chosen node of the
1345 * device-tree, including the platform type, initrd location and
1346 * size, TCE reserve, and more ...
1348 scan_flat_dt(early_init_dt_scan_chosen, NULL);
1350 /* Scan memory nodes and rebuild LMBs */
1352 scan_flat_dt(early_init_dt_scan_root, NULL);
1353 scan_flat_dt(early_init_dt_scan_memory, NULL);
1354 lmb_enforce_memory_limit(memory_limit);
1357 systemcfg->physicalMemorySize = lmb_phys_mem_size();
1359 lmb_reserve(0, __pa(klimit));
1361 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1363 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1364 early_reserve_mem();
1366 DBG("Scanning CPUs ...\n");
1368 /* Retreive hash table size from flattened tree plus other
1369 * CPU related informations (altivec support, boot CPU ID, ...)
1371 scan_flat_dt(early_init_dt_scan_cpus, NULL);
1373 DBG(" <- early_init_devtree()\n");
1379 prom_n_addr_cells(struct device_node* np)
1385 ip = (int *) get_property(np, "#address-cells", NULL);
1388 } while (np->parent);
1389 /* No #address-cells property for the root node, default to 1 */
1394 prom_n_size_cells(struct device_node* np)
1400 ip = (int *) get_property(np, "#size-cells", NULL);
1403 } while (np->parent);
1404 /* No #size-cells property for the root node, default to 1 */
1409 * Work out the sense (active-low level / active-high edge)
1410 * of each interrupt from the device tree.
1412 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1414 struct device_node *np;
1417 /* default to level-triggered */
1418 memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
1420 for (np = allnodes; np != 0; np = np->allnext) {
1421 for (j = 0; j < np->n_intrs; j++) {
1422 i = np->intrs[j].line;
1423 if (i >= off && i < max)
1424 senses[i-off] = np->intrs[j].sense;
1430 * Construct and return a list of the device_nodes with a given name.
1432 struct device_node *find_devices(const char *name)
1434 struct device_node *head, **prevp, *np;
1437 for (np = allnodes; np != 0; np = np->allnext) {
1438 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1446 EXPORT_SYMBOL(find_devices);
1449 * Construct and return a list of the device_nodes with a given type.
1451 struct device_node *find_type_devices(const char *type)
1453 struct device_node *head, **prevp, *np;
1456 for (np = allnodes; np != 0; np = np->allnext) {
1457 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1465 EXPORT_SYMBOL(find_type_devices);
1468 * Returns all nodes linked together
1470 struct device_node *find_all_nodes(void)
1472 struct device_node *head, **prevp, *np;
1475 for (np = allnodes; np != 0; np = np->allnext) {
1482 EXPORT_SYMBOL(find_all_nodes);
1484 /** Checks if the given "compat" string matches one of the strings in
1485 * the device's "compatible" property
1487 int device_is_compatible(struct device_node *device, const char *compat)
1492 cp = (char *) get_property(device, "compatible", &cplen);
1496 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1505 EXPORT_SYMBOL(device_is_compatible);
1509 * Indicates whether the root node has a given value in its
1510 * compatible property.
1512 int machine_is_compatible(const char *compat)
1514 struct device_node *root;
1517 root = of_find_node_by_path("/");
1519 rc = device_is_compatible(root, compat);
1524 EXPORT_SYMBOL(machine_is_compatible);
1527 * Construct and return a list of the device_nodes with a given type
1528 * and compatible property.
1530 struct device_node *find_compatible_devices(const char *type,
1533 struct device_node *head, **prevp, *np;
1536 for (np = allnodes; np != 0; np = np->allnext) {
1538 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1540 if (device_is_compatible(np, compat)) {
1548 EXPORT_SYMBOL(find_compatible_devices);
1551 * Find the device_node with a given full_name.
1553 struct device_node *find_path_device(const char *path)
1555 struct device_node *np;
1557 for (np = allnodes; np != 0; np = np->allnext)
1558 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1562 EXPORT_SYMBOL(find_path_device);
1566 * New implementation of the OF "find" APIs, return a refcounted
1567 * object, call of_node_put() when done. The device tree and list
1568 * are protected by a rw_lock.
1570 * Note that property management will need some locking as well,
1571 * this isn't dealt with yet.
1576 * of_find_node_by_name - Find a node by its "name" property
1577 * @from: The node to start searching from or NULL, the node
1578 * you pass will not be searched, only the next one
1579 * will; typically, you pass what the previous call
1580 * returned. of_node_put() will be called on it
1581 * @name: The name string to match against
1583 * Returns a node pointer with refcount incremented, use
1584 * of_node_put() on it when done.
1586 struct device_node *of_find_node_by_name(struct device_node *from,
1589 struct device_node *np;
1591 read_lock(&devtree_lock);
1592 np = from ? from->allnext : allnodes;
1593 for (; np != 0; np = np->allnext)
1594 if (np->name != 0 && strcasecmp(np->name, name) == 0
1599 read_unlock(&devtree_lock);
1602 EXPORT_SYMBOL(of_find_node_by_name);
1605 * of_find_node_by_type - Find a node by its "device_type" property
1606 * @from: The node to start searching from or NULL, the node
1607 * you pass will not be searched, only the next one
1608 * will; typically, you pass what the previous call
1609 * returned. of_node_put() will be called on it
1610 * @name: The type string to match against
1612 * Returns a node pointer with refcount incremented, use
1613 * of_node_put() on it when done.
1615 struct device_node *of_find_node_by_type(struct device_node *from,
1618 struct device_node *np;
1620 read_lock(&devtree_lock);
1621 np = from ? from->allnext : allnodes;
1622 for (; np != 0; np = np->allnext)
1623 if (np->type != 0 && strcasecmp(np->type, type) == 0
1628 read_unlock(&devtree_lock);
1631 EXPORT_SYMBOL(of_find_node_by_type);
1634 * of_find_compatible_node - Find a node based on type and one of the
1635 * tokens in its "compatible" property
1636 * @from: The node to start searching from or NULL, the node
1637 * you pass will not be searched, only the next one
1638 * will; typically, you pass what the previous call
1639 * returned. of_node_put() will be called on it
1640 * @type: The type string to match "device_type" or NULL to ignore
1641 * @compatible: The string to match to one of the tokens in the device
1642 * "compatible" list.
1644 * Returns a node pointer with refcount incremented, use
1645 * of_node_put() on it when done.
1647 struct device_node *of_find_compatible_node(struct device_node *from,
1648 const char *type, const char *compatible)
1650 struct device_node *np;
1652 read_lock(&devtree_lock);
1653 np = from ? from->allnext : allnodes;
1654 for (; np != 0; np = np->allnext) {
1656 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1658 if (device_is_compatible(np, compatible) && of_node_get(np))
1663 read_unlock(&devtree_lock);
1666 EXPORT_SYMBOL(of_find_compatible_node);
1669 * of_find_node_by_path - Find a node matching a full OF path
1670 * @path: The full path to match
1672 * Returns a node pointer with refcount incremented, use
1673 * of_node_put() on it when done.
1675 struct device_node *of_find_node_by_path(const char *path)
1677 struct device_node *np = allnodes;
1679 read_lock(&devtree_lock);
1680 for (; np != 0; np = np->allnext) {
1681 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1685 read_unlock(&devtree_lock);
1688 EXPORT_SYMBOL(of_find_node_by_path);
1691 * of_find_node_by_phandle - Find a node given a phandle
1692 * @handle: phandle of the node to find
1694 * Returns a node pointer with refcount incremented, use
1695 * of_node_put() on it when done.
1697 struct device_node *of_find_node_by_phandle(phandle handle)
1699 struct device_node *np;
1701 read_lock(&devtree_lock);
1702 for (np = allnodes; np != 0; np = np->allnext)
1703 if (np->linux_phandle == handle)
1707 read_unlock(&devtree_lock);
1710 EXPORT_SYMBOL(of_find_node_by_phandle);
1713 * of_find_all_nodes - Get next node in global list
1714 * @prev: Previous node or NULL to start iteration
1715 * of_node_put() will be called on it
1717 * Returns a node pointer with refcount incremented, use
1718 * of_node_put() on it when done.
1720 struct device_node *of_find_all_nodes(struct device_node *prev)
1722 struct device_node *np;
1724 read_lock(&devtree_lock);
1725 np = prev ? prev->allnext : allnodes;
1726 for (; np != 0; np = np->allnext)
1727 if (of_node_get(np))
1731 read_unlock(&devtree_lock);
1734 EXPORT_SYMBOL(of_find_all_nodes);
1737 * of_get_parent - Get a node's parent if any
1738 * @node: Node to get parent
1740 * Returns a node pointer with refcount incremented, use
1741 * of_node_put() on it when done.
1743 struct device_node *of_get_parent(const struct device_node *node)
1745 struct device_node *np;
1750 read_lock(&devtree_lock);
1751 np = of_node_get(node->parent);
1752 read_unlock(&devtree_lock);
1755 EXPORT_SYMBOL(of_get_parent);
1758 * of_get_next_child - Iterate a node childs
1759 * @node: parent node
1760 * @prev: previous child of the parent node, or NULL to get first
1762 * Returns a node pointer with refcount incremented, use
1763 * of_node_put() on it when done.
1765 struct device_node *of_get_next_child(const struct device_node *node,
1766 struct device_node *prev)
1768 struct device_node *next;
1770 read_lock(&devtree_lock);
1771 next = prev ? prev->sibling : node->child;
1772 for (; next != 0; next = next->sibling)
1773 if (of_node_get(next))
1777 read_unlock(&devtree_lock);
1780 EXPORT_SYMBOL(of_get_next_child);
1783 * of_node_get - Increment refcount of a node
1784 * @node: Node to inc refcount, NULL is supported to
1785 * simplify writing of callers
1789 struct device_node *of_node_get(struct device_node *node)
1792 kref_get(&node->kref);
1795 EXPORT_SYMBOL(of_node_get);
1797 static inline struct device_node * kref_to_device_node(struct kref *kref)
1799 return container_of(kref, struct device_node, kref);
1803 * of_node_release - release a dynamically allocated node
1804 * @kref: kref element of the node to be released
1806 * In of_node_put() this function is passed to kref_put()
1807 * as the destructor.
1809 static void of_node_release(struct kref *kref)
1811 struct device_node *node = kref_to_device_node(kref);
1812 struct property *prop = node->properties;
1814 if (!OF_IS_DYNAMIC(node))
1817 struct property *next = prop->next;
1825 kfree(node->full_name);
1831 * of_node_put - Decrement refcount of a node
1832 * @node: Node to dec refcount, NULL is supported to
1833 * simplify writing of callers
1836 void of_node_put(struct device_node *node)
1839 kref_put(&node->kref, of_node_release);
1841 EXPORT_SYMBOL(of_node_put);
1844 * Plug a device node into the tree and global list.
1846 void of_attach_node(struct device_node *np)
1848 write_lock(&devtree_lock);
1849 np->sibling = np->parent->child;
1850 np->allnext = allnodes;
1851 np->parent->child = np;
1853 write_unlock(&devtree_lock);
1857 * "Unplug" a node from the device tree. The caller must hold
1858 * a reference to the node. The memory associated with the node
1859 * is not freed until its refcount goes to zero.
1861 void of_detach_node(const struct device_node *np)
1863 struct device_node *parent;
1865 write_lock(&devtree_lock);
1867 parent = np->parent;
1870 allnodes = np->allnext;
1872 struct device_node *prev;
1873 for (prev = allnodes;
1874 prev->allnext != np;
1875 prev = prev->allnext)
1877 prev->allnext = np->allnext;
1880 if (parent->child == np)
1881 parent->child = np->sibling;
1883 struct device_node *prevsib;
1884 for (prevsib = np->parent->child;
1885 prevsib->sibling != np;
1886 prevsib = prevsib->sibling)
1888 prevsib->sibling = np->sibling;
1891 write_unlock(&devtree_lock);
1894 #ifdef CONFIG_PPC_PSERIES
1896 * Fix up the uninitialized fields in a new device node:
1897 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1899 * A lot of boot-time code is duplicated here, because functions such
1900 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1903 * This should probably be split up into smaller chunks.
1906 static int of_finish_dynamic_node(struct device_node *node,
1907 unsigned long *unused1, int unused2,
1908 int unused3, int unused4)
1910 struct device_node *parent = of_get_parent(node);
1912 phandle *ibm_phandle;
1914 node->name = get_property(node, "name", NULL);
1915 node->type = get_property(node, "device_type", NULL);
1922 /* We don't support that function on PowerMac, at least
1925 if (systemcfg->platform == PLATFORM_POWERMAC)
1928 /* fix up new node's linux_phandle field */
1929 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
1930 node->linux_phandle = *ibm_phandle;
1933 of_node_put(parent);
1937 static int prom_reconfig_notifier(struct notifier_block *nb,
1938 unsigned long action, void *node)
1943 case PSERIES_RECONFIG_ADD:
1944 err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
1946 printk(KERN_ERR "finish_node returned %d\n", err);
1957 static struct notifier_block prom_reconfig_nb = {
1958 .notifier_call = prom_reconfig_notifier,
1959 .priority = 10, /* This one needs to run first */
1962 static int __init prom_reconfig_setup(void)
1964 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1966 __initcall(prom_reconfig_setup);
1970 * Find a property with a given name for a given node
1971 * and return the value.
1973 unsigned char *get_property(struct device_node *np, const char *name,
1976 struct property *pp;
1978 for (pp = np->properties; pp != 0; pp = pp->next)
1979 if (strcmp(pp->name, name) == 0) {
1986 EXPORT_SYMBOL(get_property);
1989 * Add a property to a node
1991 void prom_add_property(struct device_node* np, struct property* prop)
1993 struct property **next = &np->properties;
1997 next = &(*next)->next;
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 */