]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/powerpc/kernel/prom.c
powerpc: remove some warnings when building iSeries
[karo-tx-linux.git] / arch / powerpc / kernel / prom.c
1 /*
2  * Procedures for creating, accessing and interpreting the device tree.
3  *
4  * Paul Mackerras       August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  * 
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com 
9  *
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.
14  */
15
16 #undef DEBUG
17
18 #include <stdarg.h>
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/threads.h>
24 #include <linux/spinlock.h>
25 #include <linux/types.h>
26 #include <linux/pci.h>
27 #include <linux/stringify.h>
28 #include <linux/delay.h>
29 #include <linux/initrd.h>
30 #include <linux/bitops.h>
31 #include <linux/module.h>
32
33 #include <asm/prom.h>
34 #include <asm/rtas.h>
35 #include <asm/lmb.h>
36 #include <asm/page.h>
37 #include <asm/processor.h>
38 #include <asm/irq.h>
39 #include <asm/io.h>
40 #include <asm/smp.h>
41 #include <asm/system.h>
42 #include <asm/mmu.h>
43 #include <asm/pgtable.h>
44 #include <asm/pci.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>
51
52 #ifdef DEBUG
53 #define DBG(fmt...) printk(KERN_ERR fmt)
54 #else
55 #define DBG(fmt...)
56 #endif
57
58 struct pci_reg_property {
59         struct pci_address addr;
60         u32 size_hi;
61         u32 size_lo;
62 };
63
64 struct isa_reg_property {
65         u32 space;
66         u32 address;
67         u32 size;
68 };
69
70
71 typedef int interpret_func(struct device_node *, unsigned long *,
72                            int, int, int);
73
74 extern struct rtas_t rtas;
75 extern struct lmb lmb;
76 extern unsigned long klimit;
77
78 static int __initdata dt_root_addr_cells;
79 static int __initdata dt_root_size_cells;
80
81 #ifdef CONFIG_PPC64
82 static int __initdata iommu_is_off;
83 int __initdata iommu_force_on;
84 unsigned long tce_alloc_start, tce_alloc_end;
85 #endif
86
87 typedef u32 cell_t;
88
89 #if 0
90 static struct boot_param_header *initial_boot_params __initdata;
91 #else
92 struct boot_param_header *initial_boot_params;
93 #endif
94
95 static struct device_node *allnodes = NULL;
96
97 /* use when traversing tree through the allnext, child, sibling,
98  * or parent members of struct device_node.
99  */
100 static DEFINE_RWLOCK(devtree_lock);
101
102 /* export that to outside world */
103 struct device_node *of_chosen;
104
105 struct device_node *dflt_interrupt_controller;
106 int num_interrupt_controllers;
107
108 /*
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.
114  */
115 static void * prom_alloc(unsigned long size, unsigned long *mem_start)
116 {
117         unsigned long tmp;
118
119         if (!mem_start)
120                 return kmalloc(size, GFP_KERNEL);
121
122         tmp = *mem_start;
123         *mem_start += size;
124         return (void *)tmp;
125 }
126
127 /*
128  * Find the device_node with a given phandle.
129  */
130 static struct device_node * find_phandle(phandle ph)
131 {
132         struct device_node *np;
133
134         for (np = allnodes; np != 0; np = np->allnext)
135                 if (np->linux_phandle == ph)
136                         return np;
137         return NULL;
138 }
139
140 /*
141  * Find the interrupt parent of a node.
142  */
143 static struct device_node * __devinit intr_parent(struct device_node *p)
144 {
145         phandle *parp;
146
147         parp = (phandle *) get_property(p, "interrupt-parent", NULL);
148         if (parp == NULL)
149                 return p->parent;
150         p = find_phandle(*parp);
151         if (p != NULL)
152                 return p;
153         /*
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
158          */
159         if (num_interrupt_controllers == 1)
160                 p = dflt_interrupt_controller;
161         return p;
162 }
163
164 /*
165  * Find out the size of each entry of the interrupts property
166  * for a node.
167  */
168 int __devinit prom_n_intr_cells(struct device_node *np)
169 {
170         struct device_node *p;
171         unsigned int *icp;
172
173         for (p = np; (p = intr_parent(p)) != NULL; ) {
174                 icp = (unsigned int *)
175                         get_property(p, "#interrupt-cells", NULL);
176                 if (icp != NULL)
177                         return *icp;
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",
181                                p->full_name);
182                         return 1;
183                 }
184         }
185 #ifdef DEBUG_IRQ
186         printk("prom_n_intr_cells failed for %s\n", np->full_name);
187 #endif
188         return 1;
189 }
190
191 /*
192  * Map an interrupt from a device up to the platform interrupt
193  * descriptor.
194  */
195 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
196                                    struct device_node *np, unsigned int *ints,
197                                    int nintrc)
198 {
199         struct device_node *p, *ipar;
200         unsigned int *imap, *imask, *ip;
201         int i, imaplen, match;
202         int newintrc = 0, newaddrc = 0;
203         unsigned int *reg;
204         int naddrc;
205
206         reg = (unsigned int *) get_property(np, "reg", NULL);
207         naddrc = prom_n_addr_cells(np);
208         p = intr_parent(np);
209         while (p != NULL) {
210                 if (get_property(p, "interrupt-controller", NULL) != NULL)
211                         /* this node is an interrupt controller, stop here */
212                         break;
213                 imap = (unsigned int *)
214                         get_property(p, "interrupt-map", &imaplen);
215                 if (imap == NULL) {
216                         p = intr_parent(p);
217                         continue;
218                 }
219                 imask = (unsigned int *)
220                         get_property(p, "interrupt-map-mask", NULL);
221                 if (imask == NULL) {
222                         printk("oops, %s has interrupt-map but no mask\n",
223                                p->full_name);
224                         return 0;
225                 }
226                 imaplen /= sizeof(unsigned int);
227                 match = 0;
228                 ipar = NULL;
229                 while (imaplen > 0 && !match) {
230                         /* check the child-interrupt field */
231                         match = 1;
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++);
240                         --imaplen;
241                         if (ipar == NULL && num_interrupt_controllers == 1)
242                                 /* cope with BootX not giving us phandles */
243                                 ipar = dflt_interrupt_controller;
244                         if (ipar == NULL) {
245                                 printk("oops, no int parent %x in map of %s\n",
246                                        imap[-1], p->full_name);
247                                 return 0;
248                         }
249                         /* find the parent's # addr and intr cells */
250                         ip = (unsigned int *)
251                                 get_property(ipar, "#interrupt-cells", NULL);
252                         if (ip == NULL) {
253                                 printk("oops, no #interrupt-cells on %s\n",
254                                        ipar->full_name);
255                                 return 0;
256                         }
257                         newintrc = *ip;
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;
263                 }
264                 if (imaplen < 0) {
265                         printk("oops, error decoding int-map on %s, len=%d\n",
266                                p->full_name, imaplen);
267                         return 0;
268                 }
269                 if (!match) {
270 #ifdef DEBUG_IRQ
271                         printk("oops, no match in %s int-map for %s\n",
272                                p->full_name, np->full_name);
273 #endif
274                         return 0;
275                 }
276                 p = ipar;
277                 naddrc = newaddrc;
278                 nintrc = newintrc;
279                 ints = imap - nintrc;
280                 reg = ints - naddrc;
281         }
282         if (p == NULL) {
283 #ifdef DEBUG_IRQ
284                 printk("hmmm, int tree for %s doesn't have ctrler\n",
285                        np->full_name);
286 #endif
287                 return 0;
288         }
289         *irq = ints;
290         *ictrler = p;
291         return nintrc;
292 }
293
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
299 };
300
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,
307 };
308
309 static int __devinit finish_node_interrupts(struct device_node *np,
310                                             unsigned long *mem_start,
311                                             int measure_only)
312 {
313         unsigned int *ints;
314         int intlen, intrcells, intrcount;
315         int i, j, n, sense;
316         unsigned int *irq, virq;
317         struct device_node *ic;
318
319         if (num_interrupt_controllers == 0) {
320                 /*
321                  * Old machines just have a list of interrupt numbers
322                  * and no interrupt-controller nodes.
323                  */
324                 ints = (unsigned int *) get_property(np, "AAPL,interrupts",
325                                                      &intlen);
326                 /* XXX old interpret_pci_props looked in parent too */
327                 /* XXX old interpret_macio_props looked for interrupts
328                    before AAPL,interrupts */
329                 if (ints == NULL)
330                         ints = (unsigned int *) get_property(np, "interrupts",
331                                                              &intlen);
332                 if (ints == NULL)
333                         return 0;
334
335                 np->n_intrs = intlen / sizeof(unsigned int);
336                 np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]),
337                                        mem_start);
338                 if (!np->intrs)
339                         return -ENOMEM;
340                 if (measure_only)
341                         return 0;
342
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;
347                 }
348                 return 0;
349         }
350
351         ints = (unsigned int *) get_property(np, "interrupts", &intlen);
352         if (ints == NULL)
353                 return 0;
354         intrcells = prom_n_intr_cells(np);
355         intlen /= intrcells * sizeof(unsigned int);
356
357         np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
358         if (!np->intrs)
359                 return -ENOMEM;
360
361         if (measure_only)
362                 return 0;
363
364         intrcount = 0;
365         for (i = 0; i < intlen; ++i, ints += intrcells) {
366                 n = map_interrupt(&irq, &ic, np, ints, intrcells);
367                 if (n <= 0)
368                         continue;
369
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];
375                 } else {
376                         virq = virt_irq_create_mapping(irq[0]);
377 #ifdef CONFIG_PPC64
378                         if (virq == NO_IRQ) {
379                                 printk(KERN_CRIT "Could not allocate interrupt"
380                                        " number for %s\n", np->full_name);
381                                 continue;
382                         }
383 #endif
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];
387                 }
388
389 #ifdef CONFIG_PPC64
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
397                                    the k2-sata-root */
398                                 break;
399                 }
400 #endif
401                 if (n > 2) {
402                         printk("hmmm, got %d intr cells for %s:", n,
403                                np->full_name);
404                         for (j = 0; j < n; ++j)
405                                 printk(" %d", irq[j]);
406                         printk("\n");
407                 }
408                 ++intrcount;
409         }
410         np->n_intrs = intrcount;
411
412         return 0;
413 }
414
415 static int __devinit interpret_pci_props(struct device_node *np,
416                                          unsigned long *mem_start,
417                                          int naddrc, int nsizec,
418                                          int measure_only)
419 {
420         struct address_range *adr;
421         struct pci_reg_property *pci_addrs;
422         int i, l, n_addrs;
423
424         pci_addrs = (struct pci_reg_property *)
425                 get_property(np, "assigned-addresses", &l);
426         if (!pci_addrs)
427                 return 0;
428
429         n_addrs = l / sizeof(*pci_addrs);
430
431         adr = prom_alloc(n_addrs * sizeof(*adr), mem_start);
432         if (!adr)
433                 return -ENOMEM;
434
435         if (measure_only)
436                 return 0;
437
438         np->addrs = adr;
439         np->n_addrs = n_addrs;
440
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;
446         }
447
448         return 0;
449 }
450
451 static int __init interpret_dbdma_props(struct device_node *np,
452                                         unsigned long *mem_start,
453                                         int naddrc, int nsizec,
454                                         int measure_only)
455 {
456         struct reg_property32 *rp;
457         struct address_range *adr;
458         unsigned long base_address;
459         int i, l;
460         struct device_node *db;
461
462         base_address = 0;
463         if (!measure_only) {
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;
467                                 break;
468                         }
469                 }
470         }
471
472         rp = (struct reg_property32 *) get_property(np, "reg", &l);
473         if (rp != 0 && l >= sizeof(struct reg_property32)) {
474                 i = 0;
475                 adr = (struct address_range *) (*mem_start);
476                 while ((l -= sizeof(struct reg_property32)) >= 0) {
477                         if (!measure_only) {
478                                 adr[i].space = 2;
479                                 adr[i].address = rp[i].address + base_address;
480                                 adr[i].size = rp[i].size;
481                         }
482                         ++i;
483                 }
484                 np->addrs = adr;
485                 np->n_addrs = i;
486                 (*mem_start) += i * sizeof(struct address_range);
487         }
488
489         return 0;
490 }
491
492 static int __init interpret_macio_props(struct device_node *np,
493                                         unsigned long *mem_start,
494                                         int naddrc, int nsizec,
495                                         int measure_only)
496 {
497         struct reg_property32 *rp;
498         struct address_range *adr;
499         unsigned long base_address;
500         int i, l;
501         struct device_node *db;
502
503         base_address = 0;
504         if (!measure_only) {
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;
508                                 break;
509                         }
510                 }
511         }
512
513         rp = (struct reg_property32 *) get_property(np, "reg", &l);
514         if (rp != 0 && l >= sizeof(struct reg_property32)) {
515                 i = 0;
516                 adr = (struct address_range *) (*mem_start);
517                 while ((l -= sizeof(struct reg_property32)) >= 0) {
518                         if (!measure_only) {
519                                 adr[i].space = 2;
520                                 adr[i].address = rp[i].address + base_address;
521                                 adr[i].size = rp[i].size;
522                         }
523                         ++i;
524                 }
525                 np->addrs = adr;
526                 np->n_addrs = i;
527                 (*mem_start) += i * sizeof(struct address_range);
528         }
529
530         return 0;
531 }
532
533 static int __init interpret_isa_props(struct device_node *np,
534                                       unsigned long *mem_start,
535                                       int naddrc, int nsizec,
536                                       int measure_only)
537 {
538         struct isa_reg_property *rp;
539         struct address_range *adr;
540         int i, l;
541
542         rp = (struct isa_reg_property *) get_property(np, "reg", &l);
543         if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
544                 i = 0;
545                 adr = (struct address_range *) (*mem_start);
546                 while ((l -= sizeof(struct isa_reg_property)) >= 0) {
547                         if (!measure_only) {
548                                 adr[i].space = rp[i].space;
549                                 adr[i].address = rp[i].address;
550                                 adr[i].size = rp[i].size;
551                         }
552                         ++i;
553                 }
554                 np->addrs = adr;
555                 np->n_addrs = i;
556                 (*mem_start) += i * sizeof(struct address_range);
557         }
558
559         return 0;
560 }
561
562 static int __init interpret_root_props(struct device_node *np,
563                                        unsigned long *mem_start,
564                                        int naddrc, int nsizec,
565                                        int measure_only)
566 {
567         struct address_range *adr;
568         int i, l;
569         unsigned int *rp;
570         int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
571
572         rp = (unsigned int *) get_property(np, "reg", &l);
573         if (rp != 0 && l >= rpsize) {
574                 i = 0;
575                 adr = (struct address_range *) (*mem_start);
576                 while ((l -= rpsize) >= 0) {
577                         if (!measure_only) {
578                                 adr[i].space = 0;
579                                 adr[i].address = rp[naddrc - 1];
580                                 adr[i].size = rp[naddrc + nsizec - 1];
581                         }
582                         ++i;
583                         rp += naddrc + nsizec;
584                 }
585                 np->addrs = adr;
586                 np->n_addrs = i;
587                 (*mem_start) += i * sizeof(struct address_range);
588         }
589
590         return 0;
591 }
592
593 static int __devinit finish_node(struct device_node *np,
594                                  unsigned long *mem_start,
595                                  interpret_func *ifunc,
596                                  int naddrc, int nsizec,
597                                  int measure_only)
598 {
599         struct device_node *child;
600         int *ip, rc = 0;
601
602         /* get the device addresses and interrupts */
603         if (ifunc != NULL)
604                 rc = ifunc(np, mem_start, naddrc, nsizec, measure_only);
605         if (rc)
606                 goto out;
607
608         rc = finish_node_interrupts(np, mem_start, measure_only);
609         if (rc)
610                 goto out;
611
612         /* Look for #address-cells and #size-cells properties. */
613         ip = (int *) get_property(np, "#address-cells", NULL);
614         if (ip != NULL)
615                 naddrc = *ip;
616         ip = (int *) get_property(np, "#size-cells", NULL);
617         if (ip != NULL)
618                 nsizec = *ip;
619
620         if (!strcmp(np->name, "device-tree") || np->parent == NULL)
621                 ifunc = interpret_root_props;
622         else if (np->type == 0)
623                 ifunc = NULL;
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"))))
638                 ifunc = NULL;
639
640         for (child = np->child; child != NULL; child = child->sibling) {
641                 rc = finish_node(child, mem_start, ifunc,
642                                  naddrc, nsizec, measure_only);
643                 if (rc)
644                         goto out;
645         }
646 out:
647         return rc;
648 }
649
650 static void __init scan_interrupt_controllers(void)
651 {
652         struct device_node *np;
653         int n = 0;
654         char *name, *ic;
655         int iclen;
656
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 */
662                 if ((name != NULL
663                      && strcmp(name, "interrupt-controller") == 0)
664                     || (ic != NULL && iclen == 0
665                         && strcmp(name, "AppleKiwi"))) {
666                         if (n == 0)
667                                 dflt_interrupt_controller = np;
668                         ++n;
669                 }
670         }
671         num_interrupt_controllers = n;
672 }
673
674 /**
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.
680  */
681 void __init finish_device_tree(void)
682 {
683         unsigned long start, end, size = 0;
684
685         DBG(" -> finish_device_tree\n");
686
687 #ifdef CONFIG_PPC64
688         /* Initialize virtual IRQ map */
689         virt_irq_init();
690 #endif
691         scan_interrupt_controllers();
692
693         /*
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
702          */
703         size = 16;
704         finish_node(allnodes, &size, NULL, 0, 0, 1);
705         size -= 16;
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);
709
710         DBG(" <- finish_device_tree\n");
711 }
712
713 static inline char *find_flat_dt_string(u32 offset)
714 {
715         return ((char *)initial_boot_params) +
716                 initial_boot_params->off_dt_strings + offset;
717 }
718
719 /**
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
722  * unflatten the tree
723  */
724 int __init of_scan_flat_dt(int (*it)(unsigned long node,
725                                      const char *uname, int depth,
726                                      void *data),
727                            void *data)
728 {
729         unsigned long p = ((unsigned long)initial_boot_params) +
730                 initial_boot_params->off_dt_struct;
731         int rc = 0;
732         int depth = -1;
733
734         do {
735                 u32 tag = *((u32 *)p);
736                 char *pathp;
737                 
738                 p += 4;
739                 if (tag == OF_DT_END_NODE) {
740                         depth --;
741                         continue;
742                 }
743                 if (tag == OF_DT_NOP)
744                         continue;
745                 if (tag == OF_DT_END)
746                         break;
747                 if (tag == OF_DT_PROP) {
748                         u32 sz = *((u32 *)p);
749                         p += 8;
750                         if (initial_boot_params->version < 0x10)
751                                 p = _ALIGN(p, sz >= 8 ? 8 : 4);
752                         p += sz;
753                         p = _ALIGN(p, 4);
754                         continue;
755                 }
756                 if (tag != OF_DT_BEGIN_NODE) {
757                         printk(KERN_WARNING "Invalid tag %x scanning flattened"
758                                " device tree !\n", tag);
759                         return -EINVAL;
760                 }
761                 depth++;
762                 pathp = (char *)p;
763                 p = _ALIGN(p + strlen(pathp) + 1, 4);
764                 if ((*pathp) == '/') {
765                         char *lp, *np;
766                         for (lp = NULL, np = pathp; *np; np++)
767                                 if ((*np) == '/')
768                                         lp = np+1;
769                         if (lp != NULL)
770                                 pathp = lp;
771                 }
772                 rc = it(p, pathp, depth, data);
773                 if (rc != 0)
774                         break;          
775         } while(1);
776
777         return rc;
778 }
779
780 /**
781  * This  function can be used within scan_flattened_dt callback to get
782  * access to properties
783  */
784 void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
785                                  unsigned long *size)
786 {
787         unsigned long p = node;
788
789         do {
790                 u32 tag = *((u32 *)p);
791                 u32 sz, noff;
792                 const char *nstr;
793
794                 p += 4;
795                 if (tag == OF_DT_NOP)
796                         continue;
797                 if (tag != OF_DT_PROP)
798                         return NULL;
799
800                 sz = *((u32 *)p);
801                 noff = *((u32 *)(p + 4));
802                 p += 8;
803                 if (initial_boot_params->version < 0x10)
804                         p = _ALIGN(p, sz >= 8 ? 8 : 4);
805
806                 nstr = find_flat_dt_string(noff);
807                 if (nstr == NULL) {
808                         printk(KERN_WARNING "Can't find property index"
809                                " name !\n");
810                         return NULL;
811                 }
812                 if (strcmp(name, nstr) == 0) {
813                         if (size)
814                                 *size = sz;
815                         return (void *)p;
816                 }
817                 p += sz;
818                 p = _ALIGN(p, 4);
819         } while(1);
820 }
821
822 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
823                                        unsigned long align)
824 {
825         void *res;
826
827         *mem = _ALIGN(*mem, align);
828         res = (void *)*mem;
829         *mem += size;
830
831         return res;
832 }
833
834 static unsigned long __init unflatten_dt_node(unsigned long mem,
835                                               unsigned long *p,
836                                               struct device_node *dad,
837                                               struct device_node ***allnextpp,
838                                               unsigned long fpsize)
839 {
840         struct device_node *np;
841         struct property *pp, **prev_pp = NULL;
842         char *pathp;
843         u32 tag;
844         unsigned int l, allocl;
845         int has_name = 0;
846         int new_format = 0;
847
848         tag = *((u32 *)(*p));
849         if (tag != OF_DT_BEGIN_NODE) {
850                 printk("Weird tag at start of node: %x\n", tag);
851                 return mem;
852         }
853         *p += 4;
854         pathp = (char *)*p;
855         l = allocl = strlen(pathp) + 1;
856         *p = _ALIGN(*p + l, 4);
857
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
861          * not '/'.
862          */
863         if ((*pathp) != '/') {
864                 new_format = 1;
865                 if (fpsize == 0) {
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
870                          */
871                         fpsize = 1;
872                         allocl = 2;
873                 } else {
874                         /* account for '/' and path size minus terminal 0
875                          * already in 'l'
876                          */
877                         fpsize += l;
878                         allocl = fpsize;
879                 }
880         }
881
882
883         np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
884                                 __alignof__(struct device_node));
885         if (allnextpp) {
886                 memset(np, 0, sizeof(*np));
887                 np->full_name = ((char*)np) + sizeof(struct device_node);
888                 if (new_format) {
889                         char *p = np->full_name;
890                         /* rebuild full path for new format */
891                         if (dad && dad->parent) {
892                                 strcpy(p, dad->full_name);
893 #ifdef DEBUG
894                                 if ((strlen(p) + l + 1) != allocl) {
895                                         DBG("%s: p: %d, l: %d, a: %d\n",
896                                             pathp, strlen(p), l, allocl);
897                                 }
898 #endif
899                                 p += strlen(p);
900                         }
901                         *(p++) = '/';
902                         memcpy(p, pathp, l);
903                 } else
904                         memcpy(np->full_name, pathp, l);
905                 prev_pp = &np->properties;
906                 **allnextpp = np;
907                 *allnextpp = &np->allnext;
908                 if (dad != NULL) {
909                         np->parent = dad;
910                         /* we temporarily use the next field as `last_child'*/
911                         if (dad->next == 0)
912                                 dad->child = np;
913                         else
914                                 dad->next->sibling = np;
915                         dad->next = np;
916                 }
917                 kref_init(&np->kref);
918         }
919         while(1) {
920                 u32 sz, noff;
921                 char *pname;
922
923                 tag = *((u32 *)(*p));
924                 if (tag == OF_DT_NOP) {
925                         *p += 4;
926                         continue;
927                 }
928                 if (tag != OF_DT_PROP)
929                         break;
930                 *p += 4;
931                 sz = *((u32 *)(*p));
932                 noff = *((u32 *)((*p) + 4));
933                 *p += 8;
934                 if (initial_boot_params->version < 0x10)
935                         *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
936
937                 pname = find_flat_dt_string(noff);
938                 if (pname == NULL) {
939                         printk("Can't find property name in list !\n");
940                         break;
941                 }
942                 if (strcmp(pname, "name") == 0)
943                         has_name = 1;
944                 l = strlen(pname) + 1;
945                 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
946                                         __alignof__(struct property));
947                 if (allnextpp) {
948                         if (strcmp(pname, "linux,phandle") == 0) {
949                                 np->node = *((u32 *)*p);
950                                 if (np->linux_phandle == 0)
951                                         np->linux_phandle = np->node;
952                         }
953                         if (strcmp(pname, "ibm,phandle") == 0)
954                                 np->linux_phandle = *((u32 *)*p);
955                         pp->name = pname;
956                         pp->length = sz;
957                         pp->value = (void *)*p;
958                         *prev_pp = pp;
959                         prev_pp = &pp->next;
960                 }
961                 *p = _ALIGN((*p) + sz, 4);
962         }
963         /* with version 0x10 we may not have the name property, recreate
964          * it here from the unit name if absent
965          */
966         if (!has_name) {
967                 char *p = pathp, *ps = pathp, *pa = NULL;
968                 int sz;
969
970                 while (*p) {
971                         if ((*p) == '@')
972                                 pa = p;
973                         if ((*p) == '/')
974                                 ps = p + 1;
975                         p++;
976                 }
977                 if (pa < ps)
978                         pa = p;
979                 sz = (pa - ps) + 1;
980                 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
981                                         __alignof__(struct property));
982                 if (allnextpp) {
983                         pp->name = "name";
984                         pp->length = sz;
985                         pp->value = (unsigned char *)(pp + 1);
986                         *prev_pp = pp;
987                         prev_pp = &pp->next;
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);
991                 }
992         }
993         if (allnextpp) {
994                 *prev_pp = NULL;
995                 np->name = get_property(np, "name", NULL);
996                 np->type = get_property(np, "device_type", NULL);
997
998                 if (!np->name)
999                         np->name = "<NULL>";
1000                 if (!np->type)
1001                         np->type = "<NULL>";
1002         }
1003         while (tag == OF_DT_BEGIN_NODE) {
1004                 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
1005                 tag = *((u32 *)(*p));
1006         }
1007         if (tag != OF_DT_END_NODE) {
1008                 printk("Weird tag at end of node: %x\n", tag);
1009                 return mem;
1010         }
1011         *p += 4;
1012         return mem;
1013 }
1014
1015
1016 /**
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)
1021  */
1022 void __init unflatten_device_tree(void)
1023 {
1024         unsigned long start, mem, size;
1025         struct device_node **allnextp = &allnodes;
1026         char *p = NULL;
1027         int l = 0;
1028
1029         DBG(" -> unflatten_device_tree()\n");
1030
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;
1036
1037         DBG("  size is %lx, allocating...\n", size);
1038
1039         /* Allocate memory for the expanded device tree */
1040         mem = lmb_alloc(size + 4, __alignof__(struct device_node));
1041         if (!mem) {
1042                 DBG("Couldn't allocate memory with lmb_alloc()!\n");
1043                 panic("Couldn't allocate memory with lmb_alloc()!\n");
1044         }
1045         mem = (unsigned long) __va(mem);
1046
1047         ((u32 *)mem)[size / 4] = 0xdeadbeef;
1048
1049         DBG("  unflattening %lx...\n", mem);
1050
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] );
1060         *allnextp = NULL;
1061
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");
1066
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));
1072         }
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 */
1077
1078         DBG("Command line is: %s\n", cmd_line);
1079
1080         DBG(" <- unflatten_device_tree()\n");
1081 }
1082
1083
1084 static int __init early_init_dt_scan_cpus(unsigned long node,
1085                                           const char *uname, int depth, void *data)
1086 {
1087         u32 *prop;
1088         unsigned long size;
1089         char *type = of_get_flat_dt_prop(node, "device_type", &size);
1090
1091         /* We are scanning "cpu" nodes only */
1092         if (type == NULL || strcmp(type, "cpu") != 0)
1093                 return 0;
1094
1095         boot_cpuid = 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
1099                  * of booted proc.
1100                  */
1101                 boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
1102         } else {
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);
1107                         if (prop != NULL)
1108                                 boot_cpuid_phys = *prop;
1109                 }
1110         }
1111         set_hard_smp_processor_id(0, boot_cpuid_phys);
1112
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", NULL);
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;
1119         }
1120
1121         /* Same goes for Apple's "altivec" property */
1122         prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
1123         if (prop) {
1124                 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1125                 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1126         }
1127 #endif /* CONFIG_ALTIVEC */
1128
1129 #ifdef CONFIG_PPC_PSERIES
1130         /*
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
1133          * property
1134          */
1135         prop = (u32 *)of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
1136                                        &size);
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;
1140 #endif
1141
1142         return 0;
1143 }
1144
1145 static int __init early_init_dt_scan_chosen(unsigned long node,
1146                                             const char *uname, int depth, void *data)
1147 {
1148         u32 *prop;
1149         unsigned long *lprop;
1150
1151         DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1152
1153         if (depth != 1 ||
1154             (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
1155                 return 0;
1156
1157         /* get platform type */
1158         prop = (u32 *)of_get_flat_dt_prop(node, "linux,platform", NULL);
1159         if (prop == NULL)
1160                 return 0;
1161 #ifdef CONFIG_PPC_MULTIPLATFORM
1162         _machine = *prop;
1163 #endif
1164
1165 #ifdef CONFIG_PPC64
1166         /* check if iommu is forced on or off */
1167         if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
1168                 iommu_is_off = 1;
1169         if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
1170                 iommu_force_on = 1;
1171 #endif
1172
1173         lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
1174         if (lprop)
1175                 memory_limit = *lprop;
1176
1177 #ifdef CONFIG_PPC64
1178         lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
1179         if (lprop)
1180                 tce_alloc_start = *lprop;
1181         lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
1182         if (lprop)
1183                 tce_alloc_end = *lprop;
1184 #endif
1185
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
1189          */
1190         {
1191                 u64 *basep, *entryp;
1192
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) {
1197                         rtas.base = *basep;
1198                         rtas.entry = *entryp;
1199                         rtas.size = *prop;
1200                 }
1201         }
1202 #endif /* CONFIG_PPC_RTAS */
1203
1204         /* break now */
1205         return 1;
1206 }
1207
1208 static int __init early_init_dt_scan_root(unsigned long node,
1209                                           const char *uname, int depth, void *data)
1210 {
1211         u32 *prop;
1212
1213         if (depth != 0)
1214                 return 0;
1215
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);
1219
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);
1223         
1224         /* break now */
1225         return 1;
1226 }
1227
1228 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1229 {
1230         cell_t *p = *cellp;
1231         unsigned long r;
1232
1233         /* Ignore more than 2 cells */
1234         while (s > sizeof(unsigned long) / 4) {
1235                 p++;
1236                 s--;
1237         }
1238         r = *p++;
1239 #ifdef CONFIG_PPC64
1240         if (s > 1) {
1241                 r <<= 32;
1242                 r |= *(p++);
1243                 s--;
1244         }
1245 #endif
1246
1247         *cellp = p;
1248         return r;
1249 }
1250
1251
1252 static int __init early_init_dt_scan_memory(unsigned long node,
1253                                             const char *uname, int depth, void *data)
1254 {
1255         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1256         cell_t *reg, *endp;
1257         unsigned long l;
1258
1259         /* We are scanning "memory" nodes only */
1260         if (type == NULL) {
1261                 /*
1262                  * The longtrail doesn't have a device_type on the
1263                  * /memory node, so look for the node called /memory@0.
1264                  */
1265                 if (depth != 1 || strcmp(uname, "memory@0") != 0)
1266                         return 0;
1267         } else if (strcmp(type, "memory") != 0)
1268                 return 0;
1269
1270         reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
1271         if (reg == NULL)
1272                 return 0;
1273
1274         endp = reg + (l / sizeof(cell_t));
1275
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]);
1278
1279         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1280                 unsigned long base, size;
1281
1282                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
1283                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
1284
1285                 if (size == 0)
1286                         continue;
1287                 DBG(" - %lx ,  %lx\n", base, size);
1288 #ifdef CONFIG_PPC64
1289                 if (iommu_is_off) {
1290                         if (base >= 0x80000000ul)
1291                                 continue;
1292                         if ((base + size) > 0x80000000ul)
1293                                 size = 0x80000000ul - base;
1294                 }
1295 #endif
1296                 lmb_add(base, size);
1297         }
1298         return 0;
1299 }
1300
1301 static void __init early_reserve_mem(void)
1302 {
1303         unsigned long base, size;
1304         unsigned long *reserve_map;
1305
1306         reserve_map = (unsigned long *)(((unsigned long)initial_boot_params) +
1307                                         initial_boot_params->off_mem_rsvmap);
1308         while (1) {
1309                 base = *(reserve_map++);
1310                 size = *(reserve_map++);
1311                 if (size == 0)
1312                         break;
1313                 DBG("reserving: %lx -> %lx\n", base, size);
1314                 lmb_reserve(base, size);
1315         }
1316
1317 #if 0
1318         DBG("memory reserved, lmbs :\n");
1319         lmb_dump_all();
1320 #endif
1321 }
1322
1323 void __init early_init_devtree(void *params)
1324 {
1325         DBG(" -> early_init_devtree()\n");
1326
1327         /* Setup flat device-tree pointer */
1328         initial_boot_params = params;
1329
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 ...
1333          */
1334         of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
1335
1336         /* Scan memory nodes and rebuild LMBs */
1337         lmb_init();
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);
1341         lmb_analyze();
1342         lmb_reserve(0, __pa(klimit));
1343
1344         DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1345
1346         /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1347         early_reserve_mem();
1348
1349         DBG("Scanning CPUs ...\n");
1350
1351         /* Retreive CPU related informations from the flat tree
1352          * (altivec support, boot CPU ID, ...)
1353          */
1354         of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
1355
1356         DBG(" <- early_init_devtree()\n");
1357 }
1358
1359 #undef printk
1360
1361 int
1362 prom_n_addr_cells(struct device_node* np)
1363 {
1364         int* ip;
1365         do {
1366                 if (np->parent)
1367                         np = np->parent;
1368                 ip = (int *) get_property(np, "#address-cells", NULL);
1369                 if (ip != NULL)
1370                         return *ip;
1371         } while (np->parent);
1372         /* No #address-cells property for the root node, default to 1 */
1373         return 1;
1374 }
1375
1376 int
1377 prom_n_size_cells(struct device_node* np)
1378 {
1379         int* ip;
1380         do {
1381                 if (np->parent)
1382                         np = np->parent;
1383                 ip = (int *) get_property(np, "#size-cells", NULL);
1384                 if (ip != NULL)
1385                         return *ip;
1386         } while (np->parent);
1387         /* No #size-cells property for the root node, default to 1 */
1388         return 1;
1389 }
1390
1391 /**
1392  * Work out the sense (active-low level / active-high edge)
1393  * of each interrupt from the device tree.
1394  */
1395 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1396 {
1397         struct device_node *np;
1398         int i, j;
1399
1400         /* default to level-triggered */
1401         memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
1402
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;
1408                 }
1409         }
1410 }
1411
1412 /**
1413  * Construct and return a list of the device_nodes with a given name.
1414  */
1415 struct device_node *find_devices(const char *name)
1416 {
1417         struct device_node *head, **prevp, *np;
1418
1419         prevp = &head;
1420         for (np = allnodes; np != 0; np = np->allnext) {
1421                 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1422                         *prevp = np;
1423                         prevp = &np->next;
1424                 }
1425         }
1426         *prevp = NULL;
1427         return head;
1428 }
1429 EXPORT_SYMBOL(find_devices);
1430
1431 /**
1432  * Construct and return a list of the device_nodes with a given type.
1433  */
1434 struct device_node *find_type_devices(const char *type)
1435 {
1436         struct device_node *head, **prevp, *np;
1437
1438         prevp = &head;
1439         for (np = allnodes; np != 0; np = np->allnext) {
1440                 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1441                         *prevp = np;
1442                         prevp = &np->next;
1443                 }
1444         }
1445         *prevp = NULL;
1446         return head;
1447 }
1448 EXPORT_SYMBOL(find_type_devices);
1449
1450 /**
1451  * Returns all nodes linked together
1452  */
1453 struct device_node *find_all_nodes(void)
1454 {
1455         struct device_node *head, **prevp, *np;
1456
1457         prevp = &head;
1458         for (np = allnodes; np != 0; np = np->allnext) {
1459                 *prevp = np;
1460                 prevp = &np->next;
1461         }
1462         *prevp = NULL;
1463         return head;
1464 }
1465 EXPORT_SYMBOL(find_all_nodes);
1466
1467 /** Checks if the given "compat" string matches one of the strings in
1468  * the device's "compatible" property
1469  */
1470 int device_is_compatible(struct device_node *device, const char *compat)
1471 {
1472         const char* cp;
1473         int cplen, l;
1474
1475         cp = (char *) get_property(device, "compatible", &cplen);
1476         if (cp == NULL)
1477                 return 0;
1478         while (cplen > 0) {
1479                 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1480                         return 1;
1481                 l = strlen(cp) + 1;
1482                 cp += l;
1483                 cplen -= l;
1484         }
1485
1486         return 0;
1487 }
1488 EXPORT_SYMBOL(device_is_compatible);
1489
1490
1491 /**
1492  * Indicates whether the root node has a given value in its
1493  * compatible property.
1494  */
1495 int machine_is_compatible(const char *compat)
1496 {
1497         struct device_node *root;
1498         int rc = 0;
1499
1500         root = of_find_node_by_path("/");
1501         if (root) {
1502                 rc = device_is_compatible(root, compat);
1503                 of_node_put(root);
1504         }
1505         return rc;
1506 }
1507 EXPORT_SYMBOL(machine_is_compatible);
1508
1509 /**
1510  * Construct and return a list of the device_nodes with a given type
1511  * and compatible property.
1512  */
1513 struct device_node *find_compatible_devices(const char *type,
1514                                             const char *compat)
1515 {
1516         struct device_node *head, **prevp, *np;
1517
1518         prevp = &head;
1519         for (np = allnodes; np != 0; np = np->allnext) {
1520                 if (type != NULL
1521                     && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1522                         continue;
1523                 if (device_is_compatible(np, compat)) {
1524                         *prevp = np;
1525                         prevp = &np->next;
1526                 }
1527         }
1528         *prevp = NULL;
1529         return head;
1530 }
1531 EXPORT_SYMBOL(find_compatible_devices);
1532
1533 /**
1534  * Find the device_node with a given full_name.
1535  */
1536 struct device_node *find_path_device(const char *path)
1537 {
1538         struct device_node *np;
1539
1540         for (np = allnodes; np != 0; np = np->allnext)
1541                 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1542                         return np;
1543         return NULL;
1544 }
1545 EXPORT_SYMBOL(find_path_device);
1546
1547 /*******
1548  *
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.
1552  *
1553  * Note that property management will need some locking as well,
1554  * this isn't dealt with yet.
1555  *
1556  *******/
1557
1558 /**
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
1565  *
1566  *      Returns a node pointer with refcount incremented, use
1567  *      of_node_put() on it when done.
1568  */
1569 struct device_node *of_find_node_by_name(struct device_node *from,
1570         const char *name)
1571 {
1572         struct device_node *np;
1573
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
1578                     && of_node_get(np))
1579                         break;
1580         if (from)
1581                 of_node_put(from);
1582         read_unlock(&devtree_lock);
1583         return np;
1584 }
1585 EXPORT_SYMBOL(of_find_node_by_name);
1586
1587 /**
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
1594  *
1595  *      Returns a node pointer with refcount incremented, use
1596  *      of_node_put() on it when done.
1597  */
1598 struct device_node *of_find_node_by_type(struct device_node *from,
1599         const char *type)
1600 {
1601         struct device_node *np;
1602
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
1607                     && of_node_get(np))
1608                         break;
1609         if (from)
1610                 of_node_put(from);
1611         read_unlock(&devtree_lock);
1612         return np;
1613 }
1614 EXPORT_SYMBOL(of_find_node_by_type);
1615
1616 /**
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.
1626  *
1627  *      Returns a node pointer with refcount incremented, use
1628  *      of_node_put() on it when done.
1629  */
1630 struct device_node *of_find_compatible_node(struct device_node *from,
1631         const char *type, const char *compatible)
1632 {
1633         struct device_node *np;
1634
1635         read_lock(&devtree_lock);
1636         np = from ? from->allnext : allnodes;
1637         for (; np != 0; np = np->allnext) {
1638                 if (type != NULL
1639                     && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1640                         continue;
1641                 if (device_is_compatible(np, compatible) && of_node_get(np))
1642                         break;
1643         }
1644         if (from)
1645                 of_node_put(from);
1646         read_unlock(&devtree_lock);
1647         return np;
1648 }
1649 EXPORT_SYMBOL(of_find_compatible_node);
1650
1651 /**
1652  *      of_find_node_by_path - Find a node matching a full OF path
1653  *      @path:  The full path to match
1654  *
1655  *      Returns a node pointer with refcount incremented, use
1656  *      of_node_put() on it when done.
1657  */
1658 struct device_node *of_find_node_by_path(const char *path)
1659 {
1660         struct device_node *np = allnodes;
1661
1662         read_lock(&devtree_lock);
1663         for (; np != 0; np = np->allnext) {
1664                 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1665                     && of_node_get(np))
1666                         break;
1667         }
1668         read_unlock(&devtree_lock);
1669         return np;
1670 }
1671 EXPORT_SYMBOL(of_find_node_by_path);
1672
1673 /**
1674  *      of_find_node_by_phandle - Find a node given a phandle
1675  *      @handle:        phandle of the node to find
1676  *
1677  *      Returns a node pointer with refcount incremented, use
1678  *      of_node_put() on it when done.
1679  */
1680 struct device_node *of_find_node_by_phandle(phandle handle)
1681 {
1682         struct device_node *np;
1683
1684         read_lock(&devtree_lock);
1685         for (np = allnodes; np != 0; np = np->allnext)
1686                 if (np->linux_phandle == handle)
1687                         break;
1688         if (np)
1689                 of_node_get(np);
1690         read_unlock(&devtree_lock);
1691         return np;
1692 }
1693 EXPORT_SYMBOL(of_find_node_by_phandle);
1694
1695 /**
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
1699  *
1700  *      Returns a node pointer with refcount incremented, use
1701  *      of_node_put() on it when done.
1702  */
1703 struct device_node *of_find_all_nodes(struct device_node *prev)
1704 {
1705         struct device_node *np;
1706
1707         read_lock(&devtree_lock);
1708         np = prev ? prev->allnext : allnodes;
1709         for (; np != 0; np = np->allnext)
1710                 if (of_node_get(np))
1711                         break;
1712         if (prev)
1713                 of_node_put(prev);
1714         read_unlock(&devtree_lock);
1715         return np;
1716 }
1717 EXPORT_SYMBOL(of_find_all_nodes);
1718
1719 /**
1720  *      of_get_parent - Get a node's parent if any
1721  *      @node:  Node to get parent
1722  *
1723  *      Returns a node pointer with refcount incremented, use
1724  *      of_node_put() on it when done.
1725  */
1726 struct device_node *of_get_parent(const struct device_node *node)
1727 {
1728         struct device_node *np;
1729
1730         if (!node)
1731                 return NULL;
1732
1733         read_lock(&devtree_lock);
1734         np = of_node_get(node->parent);
1735         read_unlock(&devtree_lock);
1736         return np;
1737 }
1738 EXPORT_SYMBOL(of_get_parent);
1739
1740 /**
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
1744  *
1745  *      Returns a node pointer with refcount incremented, use
1746  *      of_node_put() on it when done.
1747  */
1748 struct device_node *of_get_next_child(const struct device_node *node,
1749         struct device_node *prev)
1750 {
1751         struct device_node *next;
1752
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))
1757                         break;
1758         if (prev)
1759                 of_node_put(prev);
1760         read_unlock(&devtree_lock);
1761         return next;
1762 }
1763 EXPORT_SYMBOL(of_get_next_child);
1764
1765 /**
1766  *      of_node_get - Increment refcount of a node
1767  *      @node:  Node to inc refcount, NULL is supported to
1768  *              simplify writing of callers
1769  *
1770  *      Returns node.
1771  */
1772 struct device_node *of_node_get(struct device_node *node)
1773 {
1774         if (node)
1775                 kref_get(&node->kref);
1776         return node;
1777 }
1778 EXPORT_SYMBOL(of_node_get);
1779
1780 static inline struct device_node * kref_to_device_node(struct kref *kref)
1781 {
1782         return container_of(kref, struct device_node, kref);
1783 }
1784
1785 /**
1786  *      of_node_release - release a dynamically allocated node
1787  *      @kref:  kref element of the node to be released
1788  *
1789  *      In of_node_put() this function is passed to kref_put()
1790  *      as the destructor.
1791  */
1792 static void of_node_release(struct kref *kref)
1793 {
1794         struct device_node *node = kref_to_device_node(kref);
1795         struct property *prop = node->properties;
1796
1797         if (!OF_IS_DYNAMIC(node))
1798                 return;
1799         while (prop) {
1800                 struct property *next = prop->next;
1801                 kfree(prop->name);
1802                 kfree(prop->value);
1803                 kfree(prop);
1804                 prop = next;
1805         }
1806         kfree(node->intrs);
1807         kfree(node->addrs);
1808         kfree(node->full_name);
1809         kfree(node->data);
1810         kfree(node);
1811 }
1812
1813 /**
1814  *      of_node_put - Decrement refcount of a node
1815  *      @node:  Node to dec refcount, NULL is supported to
1816  *              simplify writing of callers
1817  *
1818  */
1819 void of_node_put(struct device_node *node)
1820 {
1821         if (node)
1822                 kref_put(&node->kref, of_node_release);
1823 }
1824 EXPORT_SYMBOL(of_node_put);
1825
1826 /*
1827  * Plug a device node into the tree and global list.
1828  */
1829 void of_attach_node(struct device_node *np)
1830 {
1831         write_lock(&devtree_lock);
1832         np->sibling = np->parent->child;
1833         np->allnext = allnodes;
1834         np->parent->child = np;
1835         allnodes = np;
1836         write_unlock(&devtree_lock);
1837 }
1838
1839 /*
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.
1843  */
1844 void of_detach_node(const struct device_node *np)
1845 {
1846         struct device_node *parent;
1847
1848         write_lock(&devtree_lock);
1849
1850         parent = np->parent;
1851
1852         if (allnodes == np)
1853                 allnodes = np->allnext;
1854         else {
1855                 struct device_node *prev;
1856                 for (prev = allnodes;
1857                      prev->allnext != np;
1858                      prev = prev->allnext)
1859                         ;
1860                 prev->allnext = np->allnext;
1861         }
1862
1863         if (parent->child == np)
1864                 parent->child = np->sibling;
1865         else {
1866                 struct device_node *prevsib;
1867                 for (prevsib = np->parent->child;
1868                      prevsib->sibling != np;
1869                      prevsib = prevsib->sibling)
1870                         ;
1871                 prevsib->sibling = np->sibling;
1872         }
1873
1874         write_unlock(&devtree_lock);
1875 }
1876
1877 #ifdef CONFIG_PPC_PSERIES
1878 /*
1879  * Fix up the uninitialized fields in a new device node:
1880  * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1881  *
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
1884  * slab allocator.
1885  *
1886  * This should probably be split up into smaller chunks.
1887  */
1888
1889 static int of_finish_dynamic_node(struct device_node *node,
1890                                   unsigned long *unused1, int unused2,
1891                                   int unused3, int unused4)
1892 {
1893         struct device_node *parent = of_get_parent(node);
1894         int err = 0;
1895         phandle *ibm_phandle;
1896
1897         node->name = get_property(node, "name", NULL);
1898         node->type = get_property(node, "device_type", NULL);
1899
1900         if (!parent) {
1901                 err = -ENODEV;
1902                 goto out;
1903         }
1904
1905         /* We don't support that function on PowerMac, at least
1906          * not yet
1907          */
1908         if (_machine == PLATFORM_POWERMAC)
1909                 return -ENODEV;
1910
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;
1914
1915 out:
1916         of_node_put(parent);
1917         return err;
1918 }
1919
1920 static int prom_reconfig_notifier(struct notifier_block *nb,
1921                                   unsigned long action, void *node)
1922 {
1923         int err;
1924
1925         switch (action) {
1926         case PSERIES_RECONFIG_ADD:
1927                 err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
1928                 if (err < 0) {
1929                         printk(KERN_ERR "finish_node returned %d\n", err);
1930                         err = NOTIFY_BAD;
1931                 }
1932                 break;
1933         default:
1934                 err = NOTIFY_DONE;
1935                 break;
1936         }
1937         return err;
1938 }
1939
1940 static struct notifier_block prom_reconfig_nb = {
1941         .notifier_call = prom_reconfig_notifier,
1942         .priority = 10, /* This one needs to run first */
1943 };
1944
1945 static int __init prom_reconfig_setup(void)
1946 {
1947         return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1948 }
1949 __initcall(prom_reconfig_setup);
1950 #endif
1951
1952 /*
1953  * Find a property with a given name for a given node
1954  * and return the value.
1955  */
1956 unsigned char *get_property(struct device_node *np, const char *name,
1957                             int *lenp)
1958 {
1959         struct property *pp;
1960
1961         for (pp = np->properties; pp != 0; pp = pp->next)
1962                 if (strcmp(pp->name, name) == 0) {
1963                         if (lenp != 0)
1964                                 *lenp = pp->length;
1965                         return pp->value;
1966                 }
1967         return NULL;
1968 }
1969 EXPORT_SYMBOL(get_property);
1970
1971 /*
1972  * Add a property to a node
1973  */
1974 int prom_add_property(struct device_node* np, struct property* prop)
1975 {
1976         struct property **next;
1977
1978         prop->next = NULL;      
1979         write_lock(&devtree_lock);
1980         next = &np->properties;
1981         while (*next) {
1982                 if (strcmp(prop->name, (*next)->name) == 0) {
1983                         /* duplicate ! don't insert it */
1984                         write_unlock(&devtree_lock);
1985                         return -1;
1986                 }
1987                 next = &(*next)->next;
1988         }
1989         *next = prop;
1990         write_unlock(&devtree_lock);
1991
1992 #ifdef CONFIG_PROC_DEVICETREE
1993         /* try to add to proc as well if it was initialized */
1994         if (np->pde)
1995                 proc_device_tree_add_prop(np->pde, prop);
1996 #endif /* CONFIG_PROC_DEVICETREE */
1997
1998         return 0;
1999 }
2000
2001 /* I quickly hacked that one, check against spec ! */
2002 static inline unsigned long
2003 bus_space_to_resource_flags(unsigned int bus_space)
2004 {
2005         u8 space = (bus_space >> 24) & 0xf;
2006         if (space == 0)
2007                 space = 0x02;
2008         if (space == 0x02)
2009                 return IORESOURCE_MEM;
2010         else if (space == 0x01)
2011                 return IORESOURCE_IO;
2012         else {
2013                 printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n",
2014                         bus_space);
2015                 return 0;
2016         }
2017 }
2018
2019 #ifdef CONFIG_PCI
2020 static struct resource *find_parent_pci_resource(struct pci_dev* pdev,
2021                                                  struct address_range *range)
2022 {
2023         unsigned long mask;
2024         int i;
2025
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");
2035                                         return NULL;
2036                                 }
2037                                 break;
2038                         }
2039         }
2040         if (i == DEVICE_COUNT_RESOURCE)
2041                 return NULL;
2042         return &pdev->resource[i];
2043 }
2044
2045 /*
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.
2049  */
2050 struct resource *request_OF_resource(struct device_node* node, int index,
2051                                      const char* name_postfix)
2052 {
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;
2059         int nlen, plen;
2060
2061         if (index >= node->n_addrs)
2062                 goto fail;
2063
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;
2070         else
2071                 goto fail;
2072
2073         /* Find a PCI parent if any */
2074         nd = node;
2075         pcidev = NULL;
2076         while (nd) {
2077                 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
2078                         pcidev = pci_find_slot(pci_bus, pci_devfn);
2079                 if (pcidev) break;
2080                 nd = nd->parent;
2081         }
2082         if (pcidev)
2083                 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
2084         if (!parent) {
2085                 printk(KERN_WARNING "request_OF_resource(%s), parent not found\n",
2086                         node->name);
2087                 goto fail;
2088         }
2089
2090         res = __request_region(parent, node->addrs[index].address,
2091                                node->addrs[index].size, NULL);
2092         if (!res)
2093                 goto fail;
2094         nlen = strlen(node->name);
2095         plen = name_postfix ? strlen(name_postfix) : 0;
2096         res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL);
2097         if (res->name) {
2098                 strcpy((char *)res->name, node->name);
2099                 if (plen)
2100                         strcpy((char *)res->name+nlen, name_postfix);
2101         }
2102         return res;
2103 fail:
2104         return NULL;
2105 }
2106 EXPORT_SYMBOL(request_OF_resource);
2107
2108 int release_OF_resource(struct device_node *node, int index)
2109 {
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;
2116
2117         if (index >= node->n_addrs)
2118                 return -EINVAL;
2119
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;
2126         else
2127                 return -EINVAL;
2128
2129         /* Find a PCI parent if any */
2130         nd = node;
2131         pcidev = NULL;
2132         while(nd) {
2133                 if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
2134                         pcidev = pci_find_slot(pci_bus, pci_devfn);
2135                 if (pcidev) break;
2136                 nd = nd->parent;
2137         }
2138         if (pcidev)
2139                 parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
2140         if (!parent) {
2141                 printk(KERN_WARNING "release_OF_resource(%s), parent not found\n",
2142                         node->name);
2143                 return -ENODEV;
2144         }
2145
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;
2150         while (res) {
2151                 if (res->start == start && res->end == end &&
2152                     (res->flags & IORESOURCE_BUSY))
2153                         break;
2154                 if (res->start <= start && res->end >= end)
2155                         res = res->child;
2156                 else
2157                         res = res->sibling;
2158         }
2159         if (!res)
2160                 return -ENODEV;
2161
2162         if (res->name) {
2163                 kfree(res->name);
2164                 res->name = NULL;
2165         }
2166         release_resource(res);
2167         kfree(res);
2168
2169         return 0;
2170 }
2171 EXPORT_SYMBOL(release_OF_resource);
2172 #endif /* CONFIG_PCI */