vserver 2.0 rc7
[linux-2.6.git] / arch / ppc64 / kernel / prom.c
1 /*
2  * 
3  *
4  * Procedures for interfacing to Open Firmware.
5  *
6  * Paul Mackerras       August 1996.
7  * Copyright (C) 1996 Paul Mackerras.
8  * 
9  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
10  *    {engebret|bergner}@us.ibm.com 
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  */
17
18 #undef DEBUG
19
20 #include <stdarg.h>
21 #include <linux/config.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/init.h>
25 #include <linux/version.h>
26 #include <linux/threads.h>
27 #include <linux/spinlock.h>
28 #include <linux/types.h>
29 #include <linux/pci.h>
30 #include <linux/stringify.h>
31 #include <linux/delay.h>
32 #include <linux/initrd.h>
33 #include <linux/bitops.h>
34 #include <linux/module.h>
35
36 #include <asm/prom.h>
37 #include <asm/rtas.h>
38 #include <asm/lmb.h>
39 #include <asm/abs_addr.h>
40 #include <asm/page.h>
41 #include <asm/processor.h>
42 #include <asm/irq.h>
43 #include <asm/io.h>
44 #include <asm/smp.h>
45 #include <asm/system.h>
46 #include <asm/mmu.h>
47 #include <asm/pgtable.h>
48 #include <asm/pci.h>
49 #include <asm/iommu.h>
50 #include <asm/bootinfo.h>
51 #include <asm/ppcdebug.h>
52 #include <asm/btext.h>
53 #include <asm/sections.h>
54 #include <asm/machdep.h>
55 #include <asm/pSeries_reconfig.h>
56
57 #ifdef DEBUG
58 #define DBG(fmt...) udbg_printf(fmt)
59 #else
60 #define DBG(fmt...)
61 #endif
62
63 struct pci_reg_property {
64         struct pci_address addr;
65         u32 size_hi;
66         u32 size_lo;
67 };
68
69 struct isa_reg_property {
70         u32 space;
71         u32 address;
72         u32 size;
73 };
74
75
76 typedef int interpret_func(struct device_node *, unsigned long *,
77                            int, int, int);
78
79 extern struct rtas_t rtas;
80 extern struct lmb lmb;
81 extern unsigned long klimit;
82
83 static int __initdata dt_root_addr_cells;
84 static int __initdata dt_root_size_cells;
85 static int __initdata iommu_is_off;
86 int __initdata iommu_force_on;
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 /*
106  * Wrapper for allocating memory for various data that needs to be
107  * attached to device nodes as they are processed at boot or when
108  * added to the device tree later (e.g. DLPAR).  At boot there is
109  * already a region reserved so we just increment *mem_start by size;
110  * otherwise we call kmalloc.
111  */
112 static void * prom_alloc(unsigned long size, unsigned long *mem_start)
113 {
114         unsigned long tmp;
115
116         if (!mem_start)
117                 return kmalloc(size, GFP_KERNEL);
118
119         tmp = *mem_start;
120         *mem_start += size;
121         return (void *)tmp;
122 }
123
124 /*
125  * Find the device_node with a given phandle.
126  */
127 static struct device_node * find_phandle(phandle ph)
128 {
129         struct device_node *np;
130
131         for (np = allnodes; np != 0; np = np->allnext)
132                 if (np->linux_phandle == ph)
133                         return np;
134         return NULL;
135 }
136
137 /*
138  * Find the interrupt parent of a node.
139  */
140 static struct device_node * __devinit intr_parent(struct device_node *p)
141 {
142         phandle *parp;
143
144         parp = (phandle *) get_property(p, "interrupt-parent", NULL);
145         if (parp == NULL)
146                 return p->parent;
147         return find_phandle(*parp);
148 }
149
150 /*
151  * Find out the size of each entry of the interrupts property
152  * for a node.
153  */
154 int __devinit prom_n_intr_cells(struct device_node *np)
155 {
156         struct device_node *p;
157         unsigned int *icp;
158
159         for (p = np; (p = intr_parent(p)) != NULL; ) {
160                 icp = (unsigned int *)
161                         get_property(p, "#interrupt-cells", NULL);
162                 if (icp != NULL)
163                         return *icp;
164                 if (get_property(p, "interrupt-controller", NULL) != NULL
165                     || get_property(p, "interrupt-map", NULL) != NULL) {
166                         printk("oops, node %s doesn't have #interrupt-cells\n",
167                                p->full_name);
168                         return 1;
169                 }
170         }
171 #ifdef DEBUG_IRQ
172         printk("prom_n_intr_cells failed for %s\n", np->full_name);
173 #endif
174         return 1;
175 }
176
177 /*
178  * Map an interrupt from a device up to the platform interrupt
179  * descriptor.
180  */
181 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
182                                    struct device_node *np, unsigned int *ints,
183                                    int nintrc)
184 {
185         struct device_node *p, *ipar;
186         unsigned int *imap, *imask, *ip;
187         int i, imaplen, match;
188         int newintrc = 0, newaddrc = 0;
189         unsigned int *reg;
190         int naddrc;
191
192         reg = (unsigned int *) get_property(np, "reg", NULL);
193         naddrc = prom_n_addr_cells(np);
194         p = intr_parent(np);
195         while (p != NULL) {
196                 if (get_property(p, "interrupt-controller", NULL) != NULL)
197                         /* this node is an interrupt controller, stop here */
198                         break;
199                 imap = (unsigned int *)
200                         get_property(p, "interrupt-map", &imaplen);
201                 if (imap == NULL) {
202                         p = intr_parent(p);
203                         continue;
204                 }
205                 imask = (unsigned int *)
206                         get_property(p, "interrupt-map-mask", NULL);
207                 if (imask == NULL) {
208                         printk("oops, %s has interrupt-map but no mask\n",
209                                p->full_name);
210                         return 0;
211                 }
212                 imaplen /= sizeof(unsigned int);
213                 match = 0;
214                 ipar = NULL;
215                 while (imaplen > 0 && !match) {
216                         /* check the child-interrupt field */
217                         match = 1;
218                         for (i = 0; i < naddrc && match; ++i)
219                                 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
220                         for (; i < naddrc + nintrc && match; ++i)
221                                 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
222                         imap += naddrc + nintrc;
223                         imaplen -= naddrc + nintrc;
224                         /* grab the interrupt parent */
225                         ipar = find_phandle((phandle) *imap++);
226                         --imaplen;
227                         if (ipar == NULL) {
228                                 printk("oops, no int parent %x in map of %s\n",
229                                        imap[-1], p->full_name);
230                                 return 0;
231                         }
232                         /* find the parent's # addr and intr cells */
233                         ip = (unsigned int *)
234                                 get_property(ipar, "#interrupt-cells", NULL);
235                         if (ip == NULL) {
236                                 printk("oops, no #interrupt-cells on %s\n",
237                                        ipar->full_name);
238                                 return 0;
239                         }
240                         newintrc = *ip;
241                         ip = (unsigned int *)
242                                 get_property(ipar, "#address-cells", NULL);
243                         newaddrc = (ip == NULL)? 0: *ip;
244                         imap += newaddrc + newintrc;
245                         imaplen -= newaddrc + newintrc;
246                 }
247                 if (imaplen < 0) {
248                         printk("oops, error decoding int-map on %s, len=%d\n",
249                                p->full_name, imaplen);
250                         return 0;
251                 }
252                 if (!match) {
253 #ifdef DEBUG_IRQ
254                         printk("oops, no match in %s int-map for %s\n",
255                                p->full_name, np->full_name);
256 #endif
257                         return 0;
258                 }
259                 p = ipar;
260                 naddrc = newaddrc;
261                 nintrc = newintrc;
262                 ints = imap - nintrc;
263                 reg = ints - naddrc;
264         }
265         if (p == NULL) {
266 #ifdef DEBUG_IRQ
267                 printk("hmmm, int tree for %s doesn't have ctrler\n",
268                        np->full_name);
269 #endif
270                 return 0;
271         }
272         *irq = ints;
273         *ictrler = p;
274         return nintrc;
275 }
276
277 static int __devinit finish_node_interrupts(struct device_node *np,
278                                             unsigned long *mem_start,
279                                             int measure_only)
280 {
281         unsigned int *ints;
282         int intlen, intrcells, intrcount;
283         int i, j, n;
284         unsigned int *irq, virq;
285         struct device_node *ic;
286
287         ints = (unsigned int *) get_property(np, "interrupts", &intlen);
288         if (ints == NULL)
289                 return 0;
290         intrcells = prom_n_intr_cells(np);
291         intlen /= intrcells * sizeof(unsigned int);
292
293         np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
294         if (!np->intrs)
295                 return -ENOMEM;
296
297         if (measure_only)
298                 return 0;
299
300         intrcount = 0;
301         for (i = 0; i < intlen; ++i, ints += intrcells) {
302                 n = map_interrupt(&irq, &ic, np, ints, intrcells);
303                 if (n <= 0)
304                         continue;
305
306                 /* don't map IRQ numbers under a cascaded 8259 controller */
307                 if (ic && device_is_compatible(ic, "chrp,iic")) {
308                         np->intrs[intrcount].line = irq[0];
309                 } else {
310                         virq = virt_irq_create_mapping(irq[0]);
311                         if (virq == NO_IRQ) {
312                                 printk(KERN_CRIT "Could not allocate interrupt"
313                                        " number for %s\n", np->full_name);
314                                 continue;
315                         }
316                         np->intrs[intrcount].line = irq_offset_up(virq);
317                 }
318
319                 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
320                 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
321                         char *name = get_property(ic->parent, "name", NULL);
322                         if (name && !strcmp(name, "u3"))
323                                 np->intrs[intrcount].line += 128;
324                         else if (!(name && !strcmp(name, "mac-io")))
325                                 /* ignore other cascaded controllers, such as
326                                    the k2-sata-root */
327                                 break;
328                 }
329                 np->intrs[intrcount].sense = 1;
330                 if (n > 1)
331                         np->intrs[intrcount].sense = irq[1];
332                 if (n > 2) {
333                         printk("hmmm, got %d intr cells for %s:", n,
334                                np->full_name);
335                         for (j = 0; j < n; ++j)
336                                 printk(" %d", irq[j]);
337                         printk("\n");
338                 }
339                 ++intrcount;
340         }
341         np->n_intrs = intrcount;
342
343         return 0;
344 }
345
346 static int __devinit interpret_pci_props(struct device_node *np,
347                                          unsigned long *mem_start,
348                                          int naddrc, int nsizec,
349                                          int measure_only)
350 {
351         struct address_range *adr;
352         struct pci_reg_property *pci_addrs;
353         int i, l, n_addrs;
354
355         pci_addrs = (struct pci_reg_property *)
356                 get_property(np, "assigned-addresses", &l);
357         if (!pci_addrs)
358                 return 0;
359
360         n_addrs = l / sizeof(*pci_addrs);
361
362         adr = prom_alloc(n_addrs * sizeof(*adr), mem_start);
363         if (!adr)
364                 return -ENOMEM;
365
366         if (measure_only)
367                 return 0;
368
369         np->addrs = adr;
370         np->n_addrs = n_addrs;
371
372         for (i = 0; i < n_addrs; i++) {
373                 adr[i].space = pci_addrs[i].addr.a_hi;
374                 adr[i].address = pci_addrs[i].addr.a_lo |
375                         ((u64)pci_addrs[i].addr.a_mid << 32);
376                 adr[i].size = pci_addrs[i].size_lo;
377         }
378
379         return 0;
380 }
381
382 static int __init interpret_dbdma_props(struct device_node *np,
383                                         unsigned long *mem_start,
384                                         int naddrc, int nsizec,
385                                         int measure_only)
386 {
387         struct reg_property32 *rp;
388         struct address_range *adr;
389         unsigned long base_address;
390         int i, l;
391         struct device_node *db;
392
393         base_address = 0;
394         if (!measure_only) {
395                 for (db = np->parent; db != NULL; db = db->parent) {
396                         if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
397                                 base_address = db->addrs[0].address;
398                                 break;
399                         }
400                 }
401         }
402
403         rp = (struct reg_property32 *) get_property(np, "reg", &l);
404         if (rp != 0 && l >= sizeof(struct reg_property32)) {
405                 i = 0;
406                 adr = (struct address_range *) (*mem_start);
407                 while ((l -= sizeof(struct reg_property32)) >= 0) {
408                         if (!measure_only) {
409                                 adr[i].space = 2;
410                                 adr[i].address = rp[i].address + base_address;
411                                 adr[i].size = rp[i].size;
412                         }
413                         ++i;
414                 }
415                 np->addrs = adr;
416                 np->n_addrs = i;
417                 (*mem_start) += i * sizeof(struct address_range);
418         }
419
420         return 0;
421 }
422
423 static int __init interpret_macio_props(struct device_node *np,
424                                         unsigned long *mem_start,
425                                         int naddrc, int nsizec,
426                                         int measure_only)
427 {
428         struct reg_property32 *rp;
429         struct address_range *adr;
430         unsigned long base_address;
431         int i, l;
432         struct device_node *db;
433
434         base_address = 0;
435         if (!measure_only) {
436                 for (db = np->parent; db != NULL; db = db->parent) {
437                         if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
438                                 base_address = db->addrs[0].address;
439                                 break;
440                         }
441                 }
442         }
443
444         rp = (struct reg_property32 *) get_property(np, "reg", &l);
445         if (rp != 0 && l >= sizeof(struct reg_property32)) {
446                 i = 0;
447                 adr = (struct address_range *) (*mem_start);
448                 while ((l -= sizeof(struct reg_property32)) >= 0) {
449                         if (!measure_only) {
450                                 adr[i].space = 2;
451                                 adr[i].address = rp[i].address + base_address;
452                                 adr[i].size = rp[i].size;
453                         }
454                         ++i;
455                 }
456                 np->addrs = adr;
457                 np->n_addrs = i;
458                 (*mem_start) += i * sizeof(struct address_range);
459         }
460
461         return 0;
462 }
463
464 static int __init interpret_isa_props(struct device_node *np,
465                                       unsigned long *mem_start,
466                                       int naddrc, int nsizec,
467                                       int measure_only)
468 {
469         struct isa_reg_property *rp;
470         struct address_range *adr;
471         int i, l;
472
473         rp = (struct isa_reg_property *) get_property(np, "reg", &l);
474         if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
475                 i = 0;
476                 adr = (struct address_range *) (*mem_start);
477                 while ((l -= sizeof(struct isa_reg_property)) >= 0) {
478                         if (!measure_only) {
479                                 adr[i].space = rp[i].space;
480                                 adr[i].address = rp[i].address;
481                                 adr[i].size = rp[i].size;
482                         }
483                         ++i;
484                 }
485                 np->addrs = adr;
486                 np->n_addrs = i;
487                 (*mem_start) += i * sizeof(struct address_range);
488         }
489
490         return 0;
491 }
492
493 static int __init interpret_root_props(struct device_node *np,
494                                        unsigned long *mem_start,
495                                        int naddrc, int nsizec,
496                                        int measure_only)
497 {
498         struct address_range *adr;
499         int i, l;
500         unsigned int *rp;
501         int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
502
503         rp = (unsigned int *) get_property(np, "reg", &l);
504         if (rp != 0 && l >= rpsize) {
505                 i = 0;
506                 adr = (struct address_range *) (*mem_start);
507                 while ((l -= rpsize) >= 0) {
508                         if (!measure_only) {
509                                 adr[i].space = 0;
510                                 adr[i].address = rp[naddrc - 1];
511                                 adr[i].size = rp[naddrc + nsizec - 1];
512                         }
513                         ++i;
514                         rp += naddrc + nsizec;
515                 }
516                 np->addrs = adr;
517                 np->n_addrs = i;
518                 (*mem_start) += i * sizeof(struct address_range);
519         }
520
521         return 0;
522 }
523
524 static int __devinit finish_node(struct device_node *np,
525                                  unsigned long *mem_start,
526                                  interpret_func *ifunc,
527                                  int naddrc, int nsizec,
528                                  int measure_only)
529 {
530         struct device_node *child;
531         int *ip, rc = 0;
532
533         /* get the device addresses and interrupts */
534         if (ifunc != NULL)
535                 rc = ifunc(np, mem_start, naddrc, nsizec, measure_only);
536         if (rc)
537                 goto out;
538
539         rc = finish_node_interrupts(np, mem_start, measure_only);
540         if (rc)
541                 goto out;
542
543         /* Look for #address-cells and #size-cells properties. */
544         ip = (int *) get_property(np, "#address-cells", NULL);
545         if (ip != NULL)
546                 naddrc = *ip;
547         ip = (int *) get_property(np, "#size-cells", NULL);
548         if (ip != NULL)
549                 nsizec = *ip;
550
551         if (!strcmp(np->name, "device-tree") || np->parent == NULL)
552                 ifunc = interpret_root_props;
553         else if (np->type == 0)
554                 ifunc = NULL;
555         else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
556                 ifunc = interpret_pci_props;
557         else if (!strcmp(np->type, "dbdma"))
558                 ifunc = interpret_dbdma_props;
559         else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
560                 ifunc = interpret_macio_props;
561         else if (!strcmp(np->type, "isa"))
562                 ifunc = interpret_isa_props;
563         else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
564                 ifunc = interpret_root_props;
565         else if (!((ifunc == interpret_dbdma_props
566                     || ifunc == interpret_macio_props)
567                    && (!strcmp(np->type, "escc")
568                        || !strcmp(np->type, "media-bay"))))
569                 ifunc = NULL;
570
571         for (child = np->child; child != NULL; child = child->sibling) {
572                 rc = finish_node(child, mem_start, ifunc,
573                                  naddrc, nsizec, measure_only);
574                 if (rc)
575                         goto out;
576         }
577 out:
578         return rc;
579 }
580
581 /**
582  * finish_device_tree is called once things are running normally
583  * (i.e. with text and data mapped to the address they were linked at).
584  * It traverses the device tree and fills in some of the additional,
585  * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
586  * mapping is also initialized at this point.
587  */
588 void __init finish_device_tree(void)
589 {
590         unsigned long start, end, size = 0;
591
592         DBG(" -> finish_device_tree\n");
593
594         if (ppc64_interrupt_controller == IC_INVALID) {
595                 DBG("failed to configure interrupt controller type\n");
596                 panic("failed to configure interrupt controller type\n");
597         }
598         
599         /* Initialize virtual IRQ map */
600         virt_irq_init();
601
602         /*
603          * Finish device-tree (pre-parsing some properties etc...)
604          * We do this in 2 passes. One with "measure_only" set, which
605          * will only measure the amount of memory needed, then we can
606          * allocate that memory, and call finish_node again. However,
607          * we must be careful as most routines will fail nowadays when
608          * prom_alloc() returns 0, so we must make sure our first pass
609          * doesn't start at 0. We pre-initialize size to 16 for that
610          * reason and then remove those additional 16 bytes
611          */
612         size = 16;
613         finish_node(allnodes, &size, NULL, 0, 0, 1);
614         size -= 16;
615         end = start = (unsigned long)abs_to_virt(lmb_alloc(size, 128));
616         finish_node(allnodes, &end, NULL, 0, 0, 0);
617         BUG_ON(end != start + size);
618
619         DBG(" <- finish_device_tree\n");
620 }
621
622 #ifdef DEBUG
623 #define printk udbg_printf
624 #endif
625
626 static inline char *find_flat_dt_string(u32 offset)
627 {
628         return ((char *)initial_boot_params) + initial_boot_params->off_dt_strings
629                 + offset;
630 }
631
632 /**
633  * This function is used to scan the flattened device-tree, it is
634  * used to extract the memory informations at boot before we can
635  * unflatten the tree
636  */
637 static int __init scan_flat_dt(int (*it)(unsigned long node,
638                                          const char *full_path, void *data),
639                                void *data)
640 {
641         unsigned long p = ((unsigned long)initial_boot_params) +
642                 initial_boot_params->off_dt_struct;
643         int rc = 0;
644
645         do {
646                 u32 tag = *((u32 *)p);
647                 char *pathp;
648                 
649                 p += 4;
650                 if (tag == OF_DT_END_NODE)
651                         continue;
652                 if (tag == OF_DT_END)
653                         break;
654                 if (tag == OF_DT_PROP) {
655                         u32 sz = *((u32 *)p);
656                         p += 8;
657                         p = _ALIGN(p, sz >= 8 ? 8 : 4);
658                         p += sz;
659                         p = _ALIGN(p, 4);
660                         continue;
661                 }
662                 if (tag != OF_DT_BEGIN_NODE) {
663                         printk(KERN_WARNING "Invalid tag %x scanning flattened"
664                                " device tree !\n", tag);
665                         return -EINVAL;
666                 }
667                 pathp = (char *)p;
668                 p = _ALIGN(p + strlen(pathp) + 1, 4);
669                 rc = it(p, pathp, data);
670                 if (rc != 0)
671                         break;          
672         } while(1);
673
674         return rc;
675 }
676
677 /**
678  * This  function can be used within scan_flattened_dt callback to get
679  * access to properties
680  */
681 static void* __init get_flat_dt_prop(unsigned long node, const char *name,
682                                      unsigned long *size)
683 {
684         unsigned long p = node;
685
686         do {
687                 u32 tag = *((u32 *)p);
688                 u32 sz, noff;
689                 const char *nstr;
690
691                 p += 4;
692                 if (tag != OF_DT_PROP)
693                         return NULL;
694
695                 sz = *((u32 *)p);
696                 noff = *((u32 *)(p + 4));
697                 p += 8;
698                 p = _ALIGN(p, sz >= 8 ? 8 : 4);
699
700                 nstr = find_flat_dt_string(noff);
701                 if (nstr == NULL) {
702                         printk(KERN_WARNING "Can't find property index name !\n");
703                         return NULL;
704                 }
705                 if (strcmp(name, nstr) == 0) {
706                         if (size)
707                                 *size = sz;
708                         return (void *)p;
709                 }
710                 p += sz;
711                 p = _ALIGN(p, 4);
712         } while(1);
713 }
714
715 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
716                                                unsigned long align)
717 {
718         void *res;
719
720         *mem = _ALIGN(*mem, align);
721         res = (void *)*mem;
722         *mem += size;
723
724         return res;
725 }
726
727 static unsigned long __init unflatten_dt_node(unsigned long mem,
728                                               unsigned long *p,
729                                               struct device_node *dad,
730                                               struct device_node ***allnextpp)
731 {
732         struct device_node *np;
733         struct property *pp, **prev_pp = NULL;
734         char *pathp;
735         u32 tag;
736         unsigned int l;
737
738         tag = *((u32 *)(*p));
739         if (tag != OF_DT_BEGIN_NODE) {
740                 printk("Weird tag at start of node: %x\n", tag);
741                 return mem;
742         }
743         *p += 4;
744         pathp = (char *)*p;
745         l = strlen(pathp) + 1;
746         *p = _ALIGN(*p + l, 4);
747
748         np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + l,
749                                 __alignof__(struct device_node));
750         if (allnextpp) {
751                 memset(np, 0, sizeof(*np));
752                 np->full_name = ((char*)np) + sizeof(struct device_node);
753                 memcpy(np->full_name, pathp, l);
754                 prev_pp = &np->properties;
755                 **allnextpp = np;
756                 *allnextpp = &np->allnext;
757                 if (dad != NULL) {
758                         np->parent = dad;
759                         /* we temporarily use the `next' field as `last_child'. */
760                         if (dad->next == 0)
761                                 dad->child = np;
762                         else
763                                 dad->next->sibling = np;
764                         dad->next = np;
765                 }
766                 kref_init(&np->kref);
767         }
768         while(1) {
769                 u32 sz, noff;
770                 char *pname;
771
772                 tag = *((u32 *)(*p));
773                 if (tag != OF_DT_PROP)
774                         break;
775                 *p += 4;
776                 sz = *((u32 *)(*p));
777                 noff = *((u32 *)((*p) + 4));
778                 *p = _ALIGN((*p) + 8, sz >= 8 ? 8 : 4);
779
780                 pname = find_flat_dt_string(noff);
781                 if (pname == NULL) {
782                         printk("Can't find property name in list !\n");
783                         break;
784                 }
785                 l = strlen(pname) + 1;
786                 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
787                                         __alignof__(struct property));
788                 if (allnextpp) {
789                         if (strcmp(pname, "linux,phandle") == 0) {
790                                 np->node = *((u32 *)*p);
791                                 if (np->linux_phandle == 0)
792                                         np->linux_phandle = np->node;
793                         }
794                         if (strcmp(pname, "ibm,phandle") == 0)
795                                 np->linux_phandle = *((u32 *)*p);
796                         pp->name = pname;
797                         pp->length = sz;
798                         pp->value = (void *)*p;
799                         *prev_pp = pp;
800                         prev_pp = &pp->next;
801                 }
802                 *p = _ALIGN((*p) + sz, 4);
803         }
804         if (allnextpp) {
805                 *prev_pp = NULL;
806                 np->name = get_property(np, "name", NULL);
807                 np->type = get_property(np, "device_type", NULL);
808
809                 if (!np->name)
810                         np->name = "<NULL>";
811                 if (!np->type)
812                         np->type = "<NULL>";
813         }
814         while (tag == OF_DT_BEGIN_NODE) {
815                 mem = unflatten_dt_node(mem, p, np, allnextpp);
816                 tag = *((u32 *)(*p));
817         }
818         if (tag != OF_DT_END_NODE) {
819                 printk("Weird tag at start of node: %x\n", tag);
820                 return mem;
821         }
822         *p += 4;
823         return mem;
824 }
825
826
827 /**
828  * unflattens the device-tree passed by the firmware, creating the
829  * tree of struct device_node. It also fills the "name" and "type"
830  * pointers of the nodes so the normal device-tree walking functions
831  * can be used (this used to be done by finish_device_tree)
832  */
833 void __init unflatten_device_tree(void)
834 {
835         unsigned long start, mem, size;
836         struct device_node **allnextp = &allnodes;
837         char *p = NULL;
838         int l = 0;
839
840         DBG(" -> unflatten_device_tree()\n");
841
842         /* First pass, scan for size */
843         start = ((unsigned long)initial_boot_params) +
844                 initial_boot_params->off_dt_struct;
845         size = unflatten_dt_node(0, &start, NULL, NULL);
846
847         DBG("  size is %lx, allocating...\n", size);
848
849         /* Allocate memory for the expanded device tree */
850         mem = (unsigned long)abs_to_virt(lmb_alloc(size,
851                                                    __alignof__(struct device_node)));
852         DBG("  unflattening...\n", mem);
853
854         /* Second pass, do actual unflattening */
855         start = ((unsigned long)initial_boot_params) +
856                 initial_boot_params->off_dt_struct;
857         unflatten_dt_node(mem, &start, NULL, &allnextp);
858         if (*((u32 *)start) != OF_DT_END)
859                 printk(KERN_WARNING "Weird tag at end of tree: %x\n", *((u32 *)start));
860         *allnextp = NULL;
861
862         /* Get pointer to OF "/chosen" node for use everywhere */
863         of_chosen = of_find_node_by_path("/chosen");
864
865         /* Retreive command line */
866         if (of_chosen != NULL) {
867                 p = (char *)get_property(of_chosen, "bootargs", &l);
868                 if (p != NULL && l > 0)
869                         strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
870         }
871 #ifdef CONFIG_CMDLINE
872         if (l == 0 || (l == 1 && (*p) == 0))
873                 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
874 #endif /* CONFIG_CMDLINE */
875
876         DBG("Command line is: %s\n", cmd_line);
877
878         DBG(" <- unflatten_device_tree()\n");
879 }
880
881
882 static int __init early_init_dt_scan_cpus(unsigned long node,
883                                           const char *full_path, void *data)
884 {
885         char *type = get_flat_dt_prop(node, "device_type", NULL);
886         u32 *prop;
887
888         /* We are scanning "cpu" nodes only */
889         if (type == NULL || strcmp(type, "cpu") != 0)
890                 return 0;
891
892         /* On LPAR, look for the first ibm,pft-size property for the  hash table size
893          */
894         if (systemcfg->platform == PLATFORM_PSERIES_LPAR && ppc64_pft_size == 0) {
895                 u32 *pft_size;
896                 pft_size = (u32 *)get_flat_dt_prop(node, "ibm,pft-size", NULL);
897                 if (pft_size != NULL) {
898                         /* pft_size[0] is the NUMA CEC cookie */
899                         ppc64_pft_size = pft_size[1];
900                 }
901         }
902
903         if (initial_boot_params && initial_boot_params->version >= 2) {
904                 /* version 2 of the kexec param format adds the phys cpuid
905                  * of booted proc.
906                  */
907                 boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
908                 boot_cpuid = 0;
909         } else {
910                 /* Check if it's the boot-cpu, set it's hw index in paca now */
911                 if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
912                         u32 *prop = get_flat_dt_prop(node, "reg", NULL);
913                         set_hard_smp_processor_id(0, prop == NULL ? 0 : *prop);
914                         boot_cpuid_phys = get_hard_smp_processor_id(0);
915                 }
916         }
917
918         /* Check if we have a VMX and eventually update CPU features */
919         prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", NULL);
920         if (prop && (*prop) > 0) {
921                 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
922                 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
923         }
924
925         /* Same goes for Apple's "altivec" property */
926         prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL);
927         if (prop) {
928                 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
929                 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
930         }
931
932         return 0;
933 }
934
935 static int __init early_init_dt_scan_chosen(unsigned long node,
936                                             const char *full_path, void *data)
937 {
938         u32 *prop;
939         u64 *prop64;
940         extern unsigned long memory_limit, tce_alloc_start, tce_alloc_end;
941
942         if (strcmp(full_path, "/chosen") != 0)
943                 return 0;
944
945         /* get platform type */
946         prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
947         if (prop == NULL)
948                 return 0;
949         systemcfg->platform = *prop;
950
951         /* check if iommu is forced on or off */
952         if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
953                 iommu_is_off = 1;
954         if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
955                 iommu_force_on = 1;
956
957         prop64 = (u64*)get_flat_dt_prop(node, "linux,memory-limit", NULL);
958         if (prop64)
959                 memory_limit = *prop64;
960
961         prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
962         if (prop64)
963                 tce_alloc_start = *prop64;
964
965         prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
966         if (prop64)
967                 tce_alloc_end = *prop64;
968
969 #ifdef CONFIG_PPC_RTAS
970         /* To help early debugging via the front panel, we retreive a minimal
971          * set of RTAS infos now if available
972          */
973         {
974                 u64 *basep, *entryp;
975
976                 basep = (u64*)get_flat_dt_prop(node, "linux,rtas-base", NULL);
977                 entryp = (u64*)get_flat_dt_prop(node, "linux,rtas-entry", NULL);
978                 prop = (u32*)get_flat_dt_prop(node, "linux,rtas-size", NULL);
979                 if (basep && entryp && prop) {
980                         rtas.base = *basep;
981                         rtas.entry = *entryp;
982                         rtas.size = *prop;
983                 }
984         }
985 #endif /* CONFIG_PPC_RTAS */
986
987         /* break now */
988         return 1;
989 }
990
991 static int __init early_init_dt_scan_root(unsigned long node,
992                                           const char *full_path, void *data)
993 {
994         u32 *prop;
995
996         if (strcmp(full_path, "/") != 0)
997                 return 0;
998
999         prop = (u32 *)get_flat_dt_prop(node, "#size-cells", NULL);
1000         dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1001                 
1002         prop = (u32 *)get_flat_dt_prop(node, "#address-cells", NULL);
1003         dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1004         
1005         /* break now */
1006         return 1;
1007 }
1008
1009 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1010 {
1011         cell_t *p = *cellp;
1012         unsigned long r = 0;
1013
1014         /* Ignore more than 2 cells */
1015         while (s > 2) {
1016                 p++;
1017                 s--;
1018         }
1019         while (s) {
1020                 r <<= 32;
1021                 r |= *(p++);
1022                 s--;
1023         }
1024
1025         *cellp = p;
1026         return r;
1027 }
1028
1029
1030 static int __init early_init_dt_scan_memory(unsigned long node,
1031                                             const char *full_path, void *data)
1032 {
1033         char *type = get_flat_dt_prop(node, "device_type", NULL);
1034         cell_t *reg, *endp;
1035         unsigned long l;
1036
1037         /* We are scanning "memory" nodes only */
1038         if (type == NULL || strcmp(type, "memory") != 0)
1039                 return 0;
1040
1041         reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
1042         if (reg == NULL)
1043                 return 0;
1044
1045         endp = reg + (l / sizeof(cell_t));
1046
1047         DBG("memory scan node %s ...\n", full_path);
1048         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1049                 unsigned long base, size;
1050
1051                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
1052                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
1053
1054                 if (size == 0)
1055                         continue;
1056                 DBG(" - %lx ,  %lx\n", base, size);
1057                 if (iommu_is_off) {
1058                         if (base >= 0x80000000ul)
1059                                 continue;
1060                         if ((base + size) > 0x80000000ul)
1061                                 size = 0x80000000ul - base;
1062                 }
1063                 lmb_add(base, size);
1064         }
1065         return 0;
1066 }
1067
1068 static void __init early_reserve_mem(void)
1069 {
1070         u64 base, size;
1071         u64 *reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
1072                                    initial_boot_params->off_mem_rsvmap);
1073         while (1) {
1074                 base = *(reserve_map++);
1075                 size = *(reserve_map++);
1076                 if (size == 0)
1077                         break;
1078                 DBG("reserving: %lx -> %lx\n", base, size);
1079                 lmb_reserve(base, size);
1080         }
1081
1082 #if 0
1083         DBG("memory reserved, lmbs :\n");
1084         lmb_dump_all();
1085 #endif
1086 }
1087
1088 void __init early_init_devtree(void *params)
1089 {
1090         DBG(" -> early_init_devtree()\n");
1091
1092         /* Setup flat device-tree pointer */
1093         initial_boot_params = params;
1094
1095         /* By default, hash size is not set */
1096         ppc64_pft_size = 0;
1097
1098         /* Retreive various informations from the /chosen node of the
1099          * device-tree, including the platform type, initrd location and
1100          * size, TCE reserve, and more ...
1101          */
1102         scan_flat_dt(early_init_dt_scan_chosen, NULL);
1103
1104         /* Scan memory nodes and rebuild LMBs */
1105         lmb_init();
1106         scan_flat_dt(early_init_dt_scan_root, NULL);
1107         scan_flat_dt(early_init_dt_scan_memory, NULL);
1108         lmb_enforce_memory_limit();
1109         lmb_analyze();
1110         systemcfg->physicalMemorySize = lmb_phys_mem_size();
1111         lmb_reserve(0, __pa(klimit));
1112
1113         DBG("Phys. mem: %lx\n", systemcfg->physicalMemorySize);
1114
1115         /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1116         early_reserve_mem();
1117
1118         DBG("Scanning CPUs ...\n");
1119
1120         /* Retreive hash table size from flattened tree plus other
1121          * CPU related informations (altivec support, boot CPU ID, ...)
1122          */
1123         scan_flat_dt(early_init_dt_scan_cpus, NULL);
1124
1125         /* If hash size wasn't obtained above, we calculate it now based on
1126          * the total RAM size
1127          */
1128         if (ppc64_pft_size == 0) {
1129                 unsigned long rnd_mem_size, pteg_count;
1130
1131                 /* round mem_size up to next power of 2 */
1132                 rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
1133                 if (rnd_mem_size < systemcfg->physicalMemorySize)
1134                         rnd_mem_size <<= 1;
1135
1136                 /* # pages / 2 */
1137                 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
1138
1139                 ppc64_pft_size = __ilog2(pteg_count << 7);
1140         }
1141
1142         DBG("Hash pftSize: %x\n", (int)ppc64_pft_size);
1143         DBG(" <- early_init_devtree()\n");
1144 }
1145
1146 #undef printk
1147
1148 int
1149 prom_n_addr_cells(struct device_node* np)
1150 {
1151         int* ip;
1152         do {
1153                 if (np->parent)
1154                         np = np->parent;
1155                 ip = (int *) get_property(np, "#address-cells", NULL);
1156                 if (ip != NULL)
1157                         return *ip;
1158         } while (np->parent);
1159         /* No #address-cells property for the root node, default to 1 */
1160         return 1;
1161 }
1162
1163 int
1164 prom_n_size_cells(struct device_node* np)
1165 {
1166         int* ip;
1167         do {
1168                 if (np->parent)
1169                         np = np->parent;
1170                 ip = (int *) get_property(np, "#size-cells", NULL);
1171                 if (ip != NULL)
1172                         return *ip;
1173         } while (np->parent);
1174         /* No #size-cells property for the root node, default to 1 */
1175         return 1;
1176 }
1177
1178 /**
1179  * Work out the sense (active-low level / active-high edge)
1180  * of each interrupt from the device tree.
1181  */
1182 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1183 {
1184         struct device_node *np;
1185         int i, j;
1186
1187         /* default to level-triggered */
1188         memset(senses, 1, max - off);
1189
1190         for (np = allnodes; np != 0; np = np->allnext) {
1191                 for (j = 0; j < np->n_intrs; j++) {
1192                         i = np->intrs[j].line;
1193                         if (i >= off && i < max)
1194                                 senses[i-off] = np->intrs[j].sense ?
1195                                         IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE :
1196                                         IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE;
1197                 }
1198         }
1199 }
1200
1201 /**
1202  * Construct and return a list of the device_nodes with a given name.
1203  */
1204 struct device_node *
1205 find_devices(const char *name)
1206 {
1207         struct device_node *head, **prevp, *np;
1208
1209         prevp = &head;
1210         for (np = allnodes; np != 0; np = np->allnext) {
1211                 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1212                         *prevp = np;
1213                         prevp = &np->next;
1214                 }
1215         }
1216         *prevp = NULL;
1217         return head;
1218 }
1219 EXPORT_SYMBOL(find_devices);
1220
1221 /**
1222  * Construct and return a list of the device_nodes with a given type.
1223  */
1224 struct device_node *
1225 find_type_devices(const char *type)
1226 {
1227         struct device_node *head, **prevp, *np;
1228
1229         prevp = &head;
1230         for (np = allnodes; np != 0; np = np->allnext) {
1231                 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1232                         *prevp = np;
1233                         prevp = &np->next;
1234                 }
1235         }
1236         *prevp = NULL;
1237         return head;
1238 }
1239 EXPORT_SYMBOL(find_type_devices);
1240
1241 /**
1242  * Returns all nodes linked together
1243  */
1244 struct device_node *
1245 find_all_nodes(void)
1246 {
1247         struct device_node *head, **prevp, *np;
1248
1249         prevp = &head;
1250         for (np = allnodes; np != 0; np = np->allnext) {
1251                 *prevp = np;
1252                 prevp = &np->next;
1253         }
1254         *prevp = NULL;
1255         return head;
1256 }
1257 EXPORT_SYMBOL(find_all_nodes);
1258
1259 /** Checks if the given "compat" string matches one of the strings in
1260  * the device's "compatible" property
1261  */
1262 int
1263 device_is_compatible(struct device_node *device, const char *compat)
1264 {
1265         const char* cp;
1266         int cplen, l;
1267
1268         cp = (char *) get_property(device, "compatible", &cplen);
1269         if (cp == NULL)
1270                 return 0;
1271         while (cplen > 0) {
1272                 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1273                         return 1;
1274                 l = strlen(cp) + 1;
1275                 cp += l;
1276                 cplen -= l;
1277         }
1278
1279         return 0;
1280 }
1281 EXPORT_SYMBOL(device_is_compatible);
1282
1283
1284 /**
1285  * Indicates whether the root node has a given value in its
1286  * compatible property.
1287  */
1288 int
1289 machine_is_compatible(const char *compat)
1290 {
1291         struct device_node *root;
1292         int rc = 0;
1293
1294         root = of_find_node_by_path("/");
1295         if (root) {
1296                 rc = device_is_compatible(root, compat);
1297                 of_node_put(root);
1298         }
1299         return rc;
1300 }
1301 EXPORT_SYMBOL(machine_is_compatible);
1302
1303 /**
1304  * Construct and return a list of the device_nodes with a given type
1305  * and compatible property.
1306  */
1307 struct device_node *
1308 find_compatible_devices(const char *type, const char *compat)
1309 {
1310         struct device_node *head, **prevp, *np;
1311
1312         prevp = &head;
1313         for (np = allnodes; np != 0; np = np->allnext) {
1314                 if (type != NULL
1315                     && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1316                         continue;
1317                 if (device_is_compatible(np, compat)) {
1318                         *prevp = np;
1319                         prevp = &np->next;
1320                 }
1321         }
1322         *prevp = NULL;
1323         return head;
1324 }
1325 EXPORT_SYMBOL(find_compatible_devices);
1326
1327 /**
1328  * Find the device_node with a given full_name.
1329  */
1330 struct device_node *
1331 find_path_device(const char *path)
1332 {
1333         struct device_node *np;
1334
1335         for (np = allnodes; np != 0; np = np->allnext)
1336                 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1337                         return np;
1338         return NULL;
1339 }
1340 EXPORT_SYMBOL(find_path_device);
1341
1342 /*******
1343  *
1344  * New implementation of the OF "find" APIs, return a refcounted
1345  * object, call of_node_put() when done.  The device tree and list
1346  * are protected by a rw_lock.
1347  *
1348  * Note that property management will need some locking as well,
1349  * this isn't dealt with yet.
1350  *
1351  *******/
1352
1353 /**
1354  *      of_find_node_by_name - Find a node by its "name" property
1355  *      @from:  The node to start searching from or NULL, the node
1356  *              you pass will not be searched, only the next one
1357  *              will; typically, you pass what the previous call
1358  *              returned. of_node_put() will be called on it
1359  *      @name:  The name string to match against
1360  *
1361  *      Returns a node pointer with refcount incremented, use
1362  *      of_node_put() on it when done.
1363  */
1364 struct device_node *of_find_node_by_name(struct device_node *from,
1365         const char *name)
1366 {
1367         struct device_node *np;
1368
1369         read_lock(&devtree_lock);
1370         np = from ? from->allnext : allnodes;
1371         for (; np != 0; np = np->allnext)
1372                 if (np->name != 0 && strcasecmp(np->name, name) == 0
1373                     && of_node_get(np))
1374                         break;
1375         if (from)
1376                 of_node_put(from);
1377         read_unlock(&devtree_lock);
1378         return np;
1379 }
1380 EXPORT_SYMBOL(of_find_node_by_name);
1381
1382 /**
1383  *      of_find_node_by_type - Find a node by its "device_type" property
1384  *      @from:  The node to start searching from or NULL, the node
1385  *              you pass will not be searched, only the next one
1386  *              will; typically, you pass what the previous call
1387  *              returned. of_node_put() will be called on it
1388  *      @name:  The type string to match against
1389  *
1390  *      Returns a node pointer with refcount incremented, use
1391  *      of_node_put() on it when done.
1392  */
1393 struct device_node *of_find_node_by_type(struct device_node *from,
1394         const char *type)
1395 {
1396         struct device_node *np;
1397
1398         read_lock(&devtree_lock);
1399         np = from ? from->allnext : allnodes;
1400         for (; np != 0; np = np->allnext)
1401                 if (np->type != 0 && strcasecmp(np->type, type) == 0
1402                     && of_node_get(np))
1403                         break;
1404         if (from)
1405                 of_node_put(from);
1406         read_unlock(&devtree_lock);
1407         return np;
1408 }
1409 EXPORT_SYMBOL(of_find_node_by_type);
1410
1411 /**
1412  *      of_find_compatible_node - Find a node based on type and one of the
1413  *                                tokens in its "compatible" property
1414  *      @from:          The node to start searching from or NULL, the node
1415  *                      you pass will not be searched, only the next one
1416  *                      will; typically, you pass what the previous call
1417  *                      returned. of_node_put() will be called on it
1418  *      @type:          The type string to match "device_type" or NULL to ignore
1419  *      @compatible:    The string to match to one of the tokens in the device
1420  *                      "compatible" list.
1421  *
1422  *      Returns a node pointer with refcount incremented, use
1423  *      of_node_put() on it when done.
1424  */
1425 struct device_node *of_find_compatible_node(struct device_node *from,
1426         const char *type, const char *compatible)
1427 {
1428         struct device_node *np;
1429
1430         read_lock(&devtree_lock);
1431         np = from ? from->allnext : allnodes;
1432         for (; np != 0; np = np->allnext) {
1433                 if (type != NULL
1434                     && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1435                         continue;
1436                 if (device_is_compatible(np, compatible) && of_node_get(np))
1437                         break;
1438         }
1439         if (from)
1440                 of_node_put(from);
1441         read_unlock(&devtree_lock);
1442         return np;
1443 }
1444 EXPORT_SYMBOL(of_find_compatible_node);
1445
1446 /**
1447  *      of_find_node_by_path - Find a node matching a full OF path
1448  *      @path:  The full path to match
1449  *
1450  *      Returns a node pointer with refcount incremented, use
1451  *      of_node_put() on it when done.
1452  */
1453 struct device_node *of_find_node_by_path(const char *path)
1454 {
1455         struct device_node *np = allnodes;
1456
1457         read_lock(&devtree_lock);
1458         for (; np != 0; np = np->allnext)
1459                 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1460                     && of_node_get(np))
1461                         break;
1462         read_unlock(&devtree_lock);
1463         return np;
1464 }
1465 EXPORT_SYMBOL(of_find_node_by_path);
1466
1467 /**
1468  *      of_find_node_by_phandle - Find a node given a phandle
1469  *      @handle:        phandle of the node to find
1470  *
1471  *      Returns a node pointer with refcount incremented, use
1472  *      of_node_put() on it when done.
1473  */
1474 struct device_node *of_find_node_by_phandle(phandle handle)
1475 {
1476         struct device_node *np;
1477
1478         read_lock(&devtree_lock);
1479         for (np = allnodes; np != 0; np = np->allnext)
1480                 if (np->linux_phandle == handle)
1481                         break;
1482         if (np)
1483                 of_node_get(np);
1484         read_unlock(&devtree_lock);
1485         return np;
1486 }
1487 EXPORT_SYMBOL(of_find_node_by_phandle);
1488
1489 /**
1490  *      of_find_all_nodes - Get next node in global list
1491  *      @prev:  Previous node or NULL to start iteration
1492  *              of_node_put() will be called on it
1493  *
1494  *      Returns a node pointer with refcount incremented, use
1495  *      of_node_put() on it when done.
1496  */
1497 struct device_node *of_find_all_nodes(struct device_node *prev)
1498 {
1499         struct device_node *np;
1500
1501         read_lock(&devtree_lock);
1502         np = prev ? prev->allnext : allnodes;
1503         for (; np != 0; np = np->allnext)
1504                 if (of_node_get(np))
1505                         break;
1506         if (prev)
1507                 of_node_put(prev);
1508         read_unlock(&devtree_lock);
1509         return np;
1510 }
1511 EXPORT_SYMBOL(of_find_all_nodes);
1512
1513 /**
1514  *      of_get_parent - Get a node's parent if any
1515  *      @node:  Node to get parent
1516  *
1517  *      Returns a node pointer with refcount incremented, use
1518  *      of_node_put() on it when done.
1519  */
1520 struct device_node *of_get_parent(const struct device_node *node)
1521 {
1522         struct device_node *np;
1523
1524         if (!node)
1525                 return NULL;
1526
1527         read_lock(&devtree_lock);
1528         np = of_node_get(node->parent);
1529         read_unlock(&devtree_lock);
1530         return np;
1531 }
1532 EXPORT_SYMBOL(of_get_parent);
1533
1534 /**
1535  *      of_get_next_child - Iterate a node childs
1536  *      @node:  parent node
1537  *      @prev:  previous child of the parent node, or NULL to get first
1538  *
1539  *      Returns a node pointer with refcount incremented, use
1540  *      of_node_put() on it when done.
1541  */
1542 struct device_node *of_get_next_child(const struct device_node *node,
1543         struct device_node *prev)
1544 {
1545         struct device_node *next;
1546
1547         read_lock(&devtree_lock);
1548         next = prev ? prev->sibling : node->child;
1549         for (; next != 0; next = next->sibling)
1550                 if (of_node_get(next))
1551                         break;
1552         if (prev)
1553                 of_node_put(prev);
1554         read_unlock(&devtree_lock);
1555         return next;
1556 }
1557 EXPORT_SYMBOL(of_get_next_child);
1558
1559 /**
1560  *      of_node_get - Increment refcount of a node
1561  *      @node:  Node to inc refcount, NULL is supported to
1562  *              simplify writing of callers
1563  *
1564  *      Returns node.
1565  */
1566 struct device_node *of_node_get(struct device_node *node)
1567 {
1568         if (node)
1569                 kref_get(&node->kref);
1570         return node;
1571 }
1572 EXPORT_SYMBOL(of_node_get);
1573
1574 static inline struct device_node * kref_to_device_node(struct kref *kref)
1575 {
1576         return container_of(kref, struct device_node, kref);
1577 }
1578
1579 /**
1580  *      of_node_release - release a dynamically allocated node
1581  *      @kref:  kref element of the node to be released
1582  *
1583  *      In of_node_put() this function is passed to kref_put()
1584  *      as the destructor.
1585  */
1586 static void of_node_release(struct kref *kref)
1587 {
1588         struct device_node *node = kref_to_device_node(kref);
1589         struct property *prop = node->properties;
1590
1591         if (!OF_IS_DYNAMIC(node))
1592                 return;
1593         while (prop) {
1594                 struct property *next = prop->next;
1595                 kfree(prop->name);
1596                 kfree(prop->value);
1597                 kfree(prop);
1598                 prop = next;
1599         }
1600         kfree(node->intrs);
1601         kfree(node->addrs);
1602         kfree(node->full_name);
1603         kfree(node);
1604 }
1605
1606 /**
1607  *      of_node_put - Decrement refcount of a node
1608  *      @node:  Node to dec refcount, NULL is supported to
1609  *              simplify writing of callers
1610  *
1611  */
1612 void of_node_put(struct device_node *node)
1613 {
1614         if (node)
1615                 kref_put(&node->kref, of_node_release);
1616 }
1617 EXPORT_SYMBOL(of_node_put);
1618
1619 /*
1620  * Fix up the uninitialized fields in a new device node:
1621  * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1622  *
1623  * A lot of boot-time code is duplicated here, because functions such
1624  * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1625  * slab allocator.
1626  *
1627  * This should probably be split up into smaller chunks.
1628  */
1629
1630 static int of_finish_dynamic_node(struct device_node *node,
1631                                   unsigned long *unused1, int unused2,
1632                                   int unused3, int unused4)
1633 {
1634         struct device_node *parent = of_get_parent(node);
1635         int err = 0;
1636         phandle *ibm_phandle;
1637
1638         node->name = get_property(node, "name", NULL);
1639         node->type = get_property(node, "device_type", NULL);
1640
1641         if (!parent) {
1642                 err = -ENODEV;
1643                 goto out;
1644         }
1645
1646         /* We don't support that function on PowerMac, at least
1647          * not yet
1648          */
1649         if (systemcfg->platform == PLATFORM_POWERMAC)
1650                 return -ENODEV;
1651
1652         /* fix up new node's linux_phandle field */
1653         if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
1654                 node->linux_phandle = *ibm_phandle;
1655
1656 out:
1657         of_node_put(parent);
1658         return err;
1659 }
1660
1661 /*
1662  * Plug a device node into the tree and global list.
1663  */
1664 void of_attach_node(struct device_node *np)
1665 {
1666         write_lock(&devtree_lock);
1667         np->sibling = np->parent->child;
1668         np->allnext = allnodes;
1669         np->parent->child = np;
1670         allnodes = np;
1671         write_unlock(&devtree_lock);
1672 }
1673
1674 /*
1675  * "Unplug" a node from the device tree.  The caller must hold
1676  * a reference to the node.  The memory associated with the node
1677  * is not freed until its refcount goes to zero.
1678  */
1679 void of_detach_node(const struct device_node *np)
1680 {
1681         struct device_node *parent;
1682
1683         write_lock(&devtree_lock);
1684
1685         parent = np->parent;
1686
1687         if (allnodes == np)
1688                 allnodes = np->allnext;
1689         else {
1690                 struct device_node *prev;
1691                 for (prev = allnodes;
1692                      prev->allnext != np;
1693                      prev = prev->allnext)
1694                         ;
1695                 prev->allnext = np->allnext;
1696         }
1697
1698         if (parent->child == np)
1699                 parent->child = np->sibling;
1700         else {
1701                 struct device_node *prevsib;
1702                 for (prevsib = np->parent->child;
1703                      prevsib->sibling != np;
1704                      prevsib = prevsib->sibling)
1705                         ;
1706                 prevsib->sibling = np->sibling;
1707         }
1708
1709         write_unlock(&devtree_lock);
1710 }
1711
1712 static int prom_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
1713 {
1714         int err;
1715
1716         switch (action) {
1717         case PSERIES_RECONFIG_ADD:
1718                 err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
1719                 if (err < 0) {
1720                         printk(KERN_ERR "finish_node returned %d\n", err);
1721                         err = NOTIFY_BAD;
1722                 }
1723                 break;
1724         default:
1725                 err = NOTIFY_DONE;
1726                 break;
1727         }
1728         return err;
1729 }
1730
1731 static struct notifier_block prom_reconfig_nb = {
1732         .notifier_call = prom_reconfig_notifier,
1733         .priority = 10, /* This one needs to run first */
1734 };
1735
1736 static int __init prom_reconfig_setup(void)
1737 {
1738         return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1739 }
1740 __initcall(prom_reconfig_setup);
1741
1742 /*
1743  * Find a property with a given name for a given node
1744  * and return the value.
1745  */
1746 unsigned char *
1747 get_property(struct device_node *np, const char *name, int *lenp)
1748 {
1749         struct property *pp;
1750
1751         for (pp = np->properties; pp != 0; pp = pp->next)
1752                 if (strcmp(pp->name, name) == 0) {
1753                         if (lenp != 0)
1754                                 *lenp = pp->length;
1755                         return pp->value;
1756                 }
1757         return NULL;
1758 }
1759 EXPORT_SYMBOL(get_property);
1760
1761 /*
1762  * Add a property to a node
1763  */
1764 void
1765 prom_add_property(struct device_node* np, struct property* prop)
1766 {
1767         struct property **next = &np->properties;
1768
1769         prop->next = NULL;      
1770         while (*next)
1771                 next = &(*next)->next;
1772         *next = prop;
1773 }
1774
1775 #if 0
1776 void
1777 print_properties(struct device_node *np)
1778 {
1779         struct property *pp;
1780         char *cp;
1781         int i, n;
1782
1783         for (pp = np->properties; pp != 0; pp = pp->next) {
1784                 printk(KERN_INFO "%s", pp->name);
1785                 for (i = strlen(pp->name); i < 16; ++i)
1786                         printk(" ");
1787                 cp = (char *) pp->value;
1788                 for (i = pp->length; i > 0; --i, ++cp)
1789                         if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
1790                             || (i == 1 && *cp != 0))
1791                                 break;
1792                 if (i == 0 && pp->length > 1) {
1793                         /* looks like a string */
1794                         printk(" %s\n", (char *) pp->value);
1795                 } else {
1796                         /* dump it in hex */
1797                         n = pp->length;
1798                         if (n > 64)
1799                                 n = 64;
1800                         if (pp->length % 4 == 0) {
1801                                 unsigned int *p = (unsigned int *) pp->value;
1802
1803                                 n /= 4;
1804                                 for (i = 0; i < n; ++i) {
1805                                         if (i != 0 && (i % 4) == 0)
1806                                                 printk("\n                ");
1807                                         printk(" %08x", *p++);
1808                                 }
1809                         } else {
1810                                 unsigned char *bp = pp->value;
1811
1812                                 for (i = 0; i < n; ++i) {
1813                                         if (i != 0 && (i % 16) == 0)
1814                                                 printk("\n                ");
1815                                         printk(" %02x", *bp++);
1816                                 }
1817                         }
1818                         printk("\n");
1819                         if (pp->length > 64)
1820                                 printk("                 ... (length = %d)\n",
1821                                        pp->length);
1822                 }
1823         }
1824 }
1825 #endif
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835