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