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