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