VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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_PROM
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 CONFIG_LOGO_LINUX_CLUT224
58 #include <linux/linux_logo.h>
59 extern const struct linux_logo logo_linux_clut224;
60 #endif
61
62 /*
63  * Properties whose value is longer than this get excluded from our
64  * copy of the device tree. This value does need to be big enough to
65  * ensure that we don't lose things like the interrupt-map property
66  * on a PCI-PCI bridge.
67  */
68 #define MAX_PROPERTY_LENGTH     (1UL * 1024 * 1024)
69
70 /*
71  * prom_init() is called very early on, before the kernel text
72  * and data have been mapped to KERNELBASE.  At this point the code
73  * is running at whatever address it has been loaded at, so
74  * references to extern and static variables must be relocated
75  * explicitly.  The procedure reloc_offset() returns the address
76  * we're currently running at minus the address we were linked at.
77  * (Note that strings count as static variables.)
78  *
79  * Because OF may have mapped I/O devices into the area starting at
80  * KERNELBASE, particularly on CHRP machines, we can't safely call
81  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
82  * OF calls should be done within prom_init(), and prom_init()
83  * and all routines called within it must be careful to relocate
84  * references as necessary.
85  *
86  * Note that the bss is cleared *after* prom_init runs, so we have
87  * to make sure that any static or extern variables it accesses
88  * are put in the data segment.
89  */
90
91
92 #define PROM_BUG() do {                                         \
93         prom_printf("kernel BUG at %s line 0x%x!\n",            \
94                     RELOC(__FILE__), __LINE__);                 \
95         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);       \
96 } while (0)
97
98 #ifdef DEBUG_PROM
99 #define prom_debug(x...)        prom_printf(x)
100 #else
101 #define prom_debug(x...)
102 #endif
103
104
105 struct pci_reg_property {
106         struct pci_address addr;
107         u32 size_hi;
108         u32 size_lo;
109 };
110
111
112 struct isa_reg_property {
113         u32 space;
114         u32 address;
115         u32 size;
116 };
117
118 struct pci_intr_map {
119         struct pci_address addr;
120         u32 dunno;
121         phandle int_ctrler;
122         u32 intr;
123 };
124
125
126 typedef unsigned long interpret_func(struct device_node *, unsigned long,
127                                      int, int, int);
128
129 #ifndef FB_MAX                  /* avoid pulling in all of the fb stuff */
130 #define FB_MAX  8
131 #endif
132
133 /* prom structure */
134 struct prom_t prom;
135
136 char *prom_display_paths[FB_MAX] __initdata = { NULL, };
137 phandle prom_display_nodes[FB_MAX] __initdata;
138 unsigned int prom_num_displays = 0;
139 char *of_stdout_device = NULL;
140
141 static int iommu_force_on;
142 int ppc64_iommu_off;
143
144 extern struct rtas_t rtas;
145 extern unsigned long klimit;
146 extern struct lmb lmb;
147
148 #define MAX_PHB (32 * 6)  /* 32 drawers * 6 PHBs/drawer */
149 struct of_tce_table of_tce_table[MAX_PHB + 1];
150
151 char *bootpath = NULL;
152 char *bootdevice = NULL;
153
154 int boot_cpuid = 0;
155 #define MAX_CPU_THREADS 2
156
157 struct device_node *allnodes = NULL;
158 /* use when traversing tree through the allnext, child, sibling,
159  * or parent members of struct device_node.
160  */
161 static rwlock_t devtree_lock = RW_LOCK_UNLOCKED;
162
163 extern unsigned long reloc_offset(void);
164
165 extern void enter_prom(struct prom_args *args);
166 extern void copy_and_flush(unsigned long dest, unsigned long src,
167                            unsigned long size, unsigned long offset);
168
169 unsigned long dev_tree_size;
170 unsigned long _get_PIR(void);
171
172 #ifdef CONFIG_HMT
173 struct {
174         unsigned int pir;
175         unsigned int threadid;
176 } hmt_thread_data[NR_CPUS];
177 #endif /* CONFIG_HMT */
178
179 char testString[] = "LINUX\n"; 
180
181 /*
182  * This are used in calls to call_prom.  The 4th and following
183  * arguments to call_prom should be 32-bit values.  64 bit values
184  * are truncated to 32 bits (and fortunately don't get interpreted
185  * as two arguments).
186  */
187 #define ADDR(x)         (u32) ((unsigned long)(x) - offset)
188
189 /* This is the one and *ONLY* place where we actually call open
190  * firmware from, since we need to make sure we're running in 32b
191  * mode when we do.  We switch back to 64b mode upon return.
192  */
193
194 #define PROM_ERROR      (-1)
195
196 static int __init call_prom(const char *service, int nargs, int nret, ...)
197 {
198         int i;
199         unsigned long offset = reloc_offset();
200         struct prom_t *_prom = PTRRELOC(&prom);
201         va_list list;
202
203         _prom->args.service = ADDR(service);
204         _prom->args.nargs = nargs;
205         _prom->args.nret = nret;
206         _prom->args.rets = (prom_arg_t *)&(_prom->args.args[nargs]);
207
208         va_start(list, nret);
209         for (i=0; i < nargs; i++)
210                 _prom->args.args[i] = va_arg(list, prom_arg_t);
211         va_end(list);
212
213         for (i=0; i < nret ;i++)
214                 _prom->args.rets[i] = 0;
215
216         enter_prom(&_prom->args);
217
218         return (nret > 0)? _prom->args.rets[0]: 0;
219 }
220
221
222 static void __init prom_print(const char *msg)
223 {
224         const char *p, *q;
225         unsigned long offset = reloc_offset();
226         struct prom_t *_prom = PTRRELOC(&prom);
227
228         if (_prom->stdout == 0)
229                 return;
230
231         for (p = msg; *p != 0; p = q) {
232                 for (q = p; *q != 0 && *q != '\n'; ++q)
233                         ;
234                 if (q > p)
235                         call_prom("write", 3, 1, _prom->stdout, p, q - p);
236                 if (*q == 0)
237                         break;
238                 ++q;
239                 call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
240         }
241 }
242
243
244 static void __init prom_print_hex(unsigned long val)
245 {
246         unsigned long offset = reloc_offset();
247         int i, nibbles = sizeof(val)*2;
248         char buf[sizeof(val)*2+1];
249         struct prom_t *_prom = PTRRELOC(&prom);
250
251         for (i = nibbles-1;  i >= 0;  i--) {
252                 buf[i] = (val & 0xf) + '0';
253                 if (buf[i] > '9')
254                         buf[i] += ('a'-'0'-10);
255                 val >>= 4;
256         }
257         buf[nibbles] = '\0';
258         call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
259 }
260
261
262 static void __init prom_printf(const char *format, ...)
263 {
264         unsigned long offset = reloc_offset();
265         const char *p, *q, *s;
266         va_list args;
267         unsigned long v;
268         struct prom_t *_prom = PTRRELOC(&prom);
269
270         va_start(args, format);
271         for (p = PTRRELOC(format); *p != 0; p = q) {
272                 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
273                         ;
274                 if (q > p)
275                         call_prom("write", 3, 1, _prom->stdout, p, q - p);
276                 if (*q == 0)
277                         break;
278                 if (*q == '\n') {
279                         ++q;
280                         call_prom("write", 3, 1, _prom->stdout,
281                                   ADDR("\r\n"), 2);
282                         continue;
283                 }
284                 ++q;
285                 if (*q == 0)
286                         break;
287                 switch (*q) {
288                 case 's':
289                         ++q;
290                         s = va_arg(args, const char *);
291                         prom_print(s);
292                         break;
293                 case 'x':
294                         ++q;
295                         v = va_arg(args, unsigned long);
296                         prom_print_hex(v);
297                         break;
298                 }
299         }
300 }
301
302
303 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
304 {
305         unsigned long offset = reloc_offset();
306
307         prom_print(PTRRELOC(reason));
308         /* ToDo: should put up an SRC here */
309         call_prom("exit", 0, 0);
310
311         for (;;)                        /* should never get here */
312                 ;
313 }
314
315
316 static int __init prom_next_node(phandle *nodep)
317 {
318         phandle node;
319
320         if ((node = *nodep) != 0
321             && (*nodep = call_prom("child", 1, 1, node)) != 0)
322                 return 1;
323         if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
324                 return 1;
325         for (;;) {
326                 if ((node = call_prom("parent", 1, 1, node)) == 0)
327                         return 0;
328                 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
329                         return 1;
330         }
331 }
332
333 static int __init prom_getprop(phandle node, const char *pname,
334                                void *value, size_t valuelen)
335 {
336         unsigned long offset = reloc_offset();
337
338         return call_prom("getprop", 4, 1, node, ADDR(pname),
339                          (u32)(unsigned long) value, (u32) valuelen);
340 }
341
342 static void __init prom_initialize_naca(void)
343 {
344         phandle node;
345         char type[64];
346         unsigned long num_cpus = 0;
347         unsigned long offset = reloc_offset();
348         struct prom_t *_prom = PTRRELOC(&prom);
349         struct naca_struct *_naca = RELOC(naca);
350         struct systemcfg *_systemcfg = RELOC(systemcfg);
351
352         /* NOTE: _naca->debug_switch is already initialized. */
353         prom_debug("prom_initialize_naca: start...\n");
354
355         _naca->pftSize = 0;     /* ilog2 of htab size.  computed below. */
356
357         for (node = 0; prom_next_node(&node); ) {
358                 type[0] = 0;
359                 prom_getprop(node, "device_type", type, sizeof(type));
360
361                 if (!strcmp(type, RELOC("cpu"))) {
362                         num_cpus += 1;
363
364                         /* We're assuming *all* of the CPUs have the same
365                          * d-cache and i-cache sizes... -Peter
366                          */
367                         if ( num_cpus == 1 ) {
368                                 u32 size, lsize;
369                                 const char *dc, *ic;
370
371                                 if (_systemcfg->platform == PLATFORM_POWERMAC){
372                                         dc = "d-cache-block-size";
373                                         ic = "i-cache-block-size";
374                                 } else {
375                                         dc = "d-cache-line-size";
376                                         ic = "i-cache-line-size";
377                                 }
378
379                                 prom_getprop(node, "d-cache-size",
380                                              &size, sizeof(size));
381
382                                 prom_getprop(node, dc, &lsize, sizeof(lsize));
383
384                                 _systemcfg->dCacheL1Size = size;
385                                 _systemcfg->dCacheL1LineSize = lsize;
386                                 _naca->dCacheL1LogLineSize = __ilog2(lsize);
387                                 _naca->dCacheL1LinesPerPage = PAGE_SIZE/lsize;
388
389                                 prom_getprop(node, "i-cache-size",
390                                              &size, sizeof(size));
391
392                                 prom_getprop(node, ic, &lsize, sizeof(lsize));
393
394                                 _systemcfg->iCacheL1Size = size;
395                                 _systemcfg->iCacheL1LineSize = lsize;
396                                 _naca->iCacheL1LogLineSize = __ilog2(lsize);
397                                 _naca->iCacheL1LinesPerPage = PAGE_SIZE/lsize;
398
399                                 if (_systemcfg->platform == PLATFORM_PSERIES_LPAR) {
400                                         u32 pft_size[2];
401                                         prom_getprop(node, "ibm,pft-size",
402                                                 &pft_size, sizeof(pft_size));
403                                 /* pft_size[0] is the NUMA CEC cookie */
404                                         _naca->pftSize = pft_size[1];
405                                 }
406                         }
407                 } else if (!strcmp(type, RELOC("serial"))) {
408                         phandle isa, pci;
409                         struct isa_reg_property reg;
410                         union pci_range ranges;
411
412                         if (_systemcfg->platform == PLATFORM_POWERMAC)
413                                 continue;
414                         type[0] = 0;
415                         prom_getprop(node, "ibm,aix-loc", type, sizeof(type));
416
417                         if (strcmp(type, RELOC("S1")))
418                                 continue;
419
420                         prom_getprop(node, "reg", &reg, sizeof(reg));
421
422                         isa = call_prom("parent", 1, 1, node);
423                         if (!isa)
424                                 PROM_BUG();
425                         pci = call_prom("parent", 1, 1, isa);
426                         if (!pci)
427                                 PROM_BUG();
428
429                         prom_getprop(pci, "ranges", &ranges, sizeof(ranges));
430
431                         if ( _prom->encode_phys_size == 32 )
432                                 _naca->serialPortAddr = ranges.pci32.phys+reg.address;
433                         else {
434                                 _naca->serialPortAddr = 
435                                         ((((unsigned long)ranges.pci64.phys_hi) << 32) |
436                                          (ranges.pci64.phys_lo)) + reg.address;
437                         }
438                 }
439         }
440
441         if (_systemcfg->platform == PLATFORM_POWERMAC)
442                 _naca->interrupt_controller = IC_OPEN_PIC;
443         else {
444                 _naca->interrupt_controller = IC_INVALID;
445                 for (node = 0; prom_next_node(&node); ) {
446                         type[0] = 0;
447                         prom_getprop(node, "name", type, sizeof(type));
448                         if (strcmp(type, RELOC("interrupt-controller")))
449                                 continue;
450                         prom_getprop(node, "compatible", type, sizeof(type));
451                         if (strstr(type, RELOC("open-pic")))
452                                 _naca->interrupt_controller = IC_OPEN_PIC;
453                         else if (strstr(type, RELOC("ppc-xicp")))
454                                 _naca->interrupt_controller = IC_PPC_XIC;
455                         else
456                                 prom_printf("prom: failed to recognize"
457                                             " interrupt-controller\n");
458                         break;
459                 }
460         }
461
462         if (_naca->interrupt_controller == IC_INVALID) {
463                 prom_printf("prom: failed to find interrupt-controller\n");
464                 PROM_BUG();
465         }
466
467         /* We gotta have at least 1 cpu... */
468         if ( (_systemcfg->processorCount = num_cpus) < 1 )
469                 PROM_BUG();
470
471         _systemcfg->physicalMemorySize = lmb_phys_mem_size();
472
473         if (_systemcfg->platform == PLATFORM_PSERIES ||
474             _systemcfg->platform == PLATFORM_POWERMAC) {
475                 unsigned long rnd_mem_size, pteg_count;
476
477                 /* round mem_size up to next power of 2 */
478                 rnd_mem_size = 1UL << __ilog2(_systemcfg->physicalMemorySize);
479                 if (rnd_mem_size < _systemcfg->physicalMemorySize)
480                         rnd_mem_size <<= 1;
481
482                 /* # pages / 2 */
483                 pteg_count = (rnd_mem_size >> (12 + 1));
484
485                 _naca->pftSize = __ilog2(pteg_count << 7);
486         }
487
488         if (_naca->pftSize == 0) {
489                 prom_printf("prom: failed to compute pftSize!\n");
490                 PROM_BUG();
491         }
492
493         /* Add an eye catcher and the systemcfg layout version number */
494         strcpy(_systemcfg->eye_catcher, RELOC("SYSTEMCFG:PPC64"));
495         _systemcfg->version.major = SYSTEMCFG_MAJOR;
496         _systemcfg->version.minor = SYSTEMCFG_MINOR;
497         _systemcfg->processor = _get_PVR();
498
499         prom_debug("systemcfg->processorCount       = 0x%x\n",
500                    _systemcfg->processorCount);
501         prom_debug("systemcfg->physicalMemorySize   = 0x%x\n",
502                    _systemcfg->physicalMemorySize);
503         prom_debug("naca->pftSize                   = 0x%x\n",
504                    _naca->pftSize);
505         prom_debug("systemcfg->dCacheL1LineSize     = 0x%x\n",
506                    _systemcfg->dCacheL1LineSize);
507         prom_debug("systemcfg->iCacheL1LineSize     = 0x%x\n",
508                    _systemcfg->iCacheL1LineSize);
509         prom_debug("naca->serialPortAddr            = 0x%x\n",
510                    _naca->serialPortAddr);
511         prom_debug("naca->interrupt_controller      = 0x%x\n",
512                    _naca->interrupt_controller);
513         prom_debug("systemcfg->platform             = 0x%x\n",
514                    _systemcfg->platform);
515         prom_debug("prom_initialize_naca: end...\n");
516 }
517
518
519 static void __init early_cmdline_parse(void)
520 {
521         unsigned long offset = reloc_offset();
522         char *opt;
523 #ifndef CONFIG_PMAC_DART
524         struct systemcfg *_systemcfg = RELOC(systemcfg);
525 #endif
526
527         opt = strstr(RELOC(cmd_line), RELOC("iommu="));
528         if (opt) {
529                 prom_printf("opt is:%s\n", opt);
530                 opt += 6;
531                 while (*opt && *opt == ' ')
532                         opt++;
533                 if (!strncmp(opt, RELOC("off"), 3))
534                         RELOC(ppc64_iommu_off) = 1;
535                 else if (!strncmp(opt, RELOC("force"), 5))
536                         RELOC(iommu_force_on) = 1;
537         }
538
539 #ifndef CONFIG_PMAC_DART
540         if (_systemcfg->platform == PLATFORM_POWERMAC) {
541                 RELOC(ppc64_iommu_off) = 1;
542                 prom_printf("DART disabled on PowerMac !\n");
543         }
544 #endif
545 }
546
547 #ifdef DEBUG_PROM
548 void prom_dump_lmb(void)
549 {
550         unsigned long i;
551         unsigned long offset = reloc_offset();
552         struct lmb *_lmb  = PTRRELOC(&lmb);
553
554         prom_printf("\nprom_dump_lmb:\n");
555         prom_printf("    memory.cnt               = 0x%x\n",
556                     _lmb->memory.cnt);
557         prom_printf("    memory.size             = 0x%x\n",
558                     _lmb->memory.size);
559         for (i=0; i < _lmb->memory.cnt ;i++) {
560                 prom_printf("    memory.region[0x%x].base       = 0x%x\n",
561                             i, _lmb->memory.region[i].base);
562                 prom_printf("                 .physbase = 0x%x\n",
563                             _lmb->memory.region[i].physbase);
564                 prom_printf("                 .size     = 0x%x\n",
565                             _lmb->memory.region[i].size);
566         }
567
568         prom_printf("\n    reserved.cnt           = 0x%x\n",
569                     _lmb->reserved.cnt);
570         prom_printf("    reserved.size           = 0x%x\n",
571                     _lmb->reserved.size);
572         for (i=0; i < _lmb->reserved.cnt ;i++) {
573                 prom_printf("    reserved.region[0x%x\n].base       = 0x%x\n",
574                             i, _lmb->reserved.region[i].base);
575                 prom_printf("                 .physbase = 0x%x\n",
576                             _lmb->reserved.region[i].physbase);
577                 prom_printf("                 .size     = 0x%x\n",
578                             _lmb->reserved.region[i].size);
579         }
580 }
581 #endif /* DEBUG_PROM */
582
583 static void __init prom_initialize_lmb(void)
584 {
585         phandle node;
586         char type[64];
587         unsigned long i, offset = reloc_offset();
588         struct prom_t *_prom = PTRRELOC(&prom);
589         struct systemcfg *_systemcfg = RELOC(systemcfg);
590         union lmb_reg_property reg;
591         unsigned long lmb_base, lmb_size;
592         unsigned long num_regs, bytes_per_reg = (_prom->encode_phys_size*2)/8;
593
594         lmb_init();
595
596         /* XXX Quick HACK. Proper fix is to drop those structures and properly use
597          * #address-cells. PowerMac has #size-cell set to 1 and #address-cells to 2
598          */
599         if (_systemcfg->platform == PLATFORM_POWERMAC)
600                 bytes_per_reg = 12;
601
602         for (node = 0; prom_next_node(&node); ) {
603                 type[0] = 0;
604                 prom_getprop(node, "device_type", type, sizeof(type));
605
606                 if (strcmp(type, RELOC("memory")))
607                         continue;
608
609                 num_regs = prom_getprop(node, "reg", &reg, sizeof(reg))
610                         / bytes_per_reg;
611
612                 for (i=0; i < num_regs ;i++) {
613                         if (_systemcfg->platform == PLATFORM_POWERMAC) {
614                                 lmb_base = ((unsigned long)reg.addrPM[i].address_hi) << 32;
615                                 lmb_base |= (unsigned long)reg.addrPM[i].address_lo;
616                                 lmb_size = reg.addrPM[i].size;
617                         } else if (_prom->encode_phys_size == 32) {
618                                 lmb_base = reg.addr32[i].address;
619                                 lmb_size = reg.addr32[i].size;
620                         } else {
621                                 lmb_base = reg.addr64[i].address;
622                                 lmb_size = reg.addr64[i].size;
623                         }
624
625                         /* We limit memory to 2GB if the IOMMU is off */
626                         if (RELOC(ppc64_iommu_off)) {
627                                 if (lmb_base >= 0x80000000UL)
628                                         continue;
629
630                                 if ((lmb_base + lmb_size) > 0x80000000UL)
631                                         lmb_size = 0x80000000UL - lmb_base;
632                         }
633
634                         if (lmb_add(lmb_base, lmb_size) < 0)
635                                 prom_printf("Too many LMB's, discarding this one...\n");
636                 }
637
638         }
639
640         lmb_analyze();
641 #ifdef DEBUG_PROM
642         prom_dump_lmb();
643 #endif /* DEBUG_PROM */
644 }
645
646 static void __init
647 prom_instantiate_rtas(void)
648 {
649         unsigned long offset = reloc_offset();
650         struct prom_t *_prom = PTRRELOC(&prom);
651         struct rtas_t *_rtas = PTRRELOC(&rtas);
652         struct systemcfg *_systemcfg = RELOC(systemcfg);
653         ihandle prom_rtas;
654         u32 getprop_rval;
655         char hypertas_funcs[4];
656
657         prom_debug("prom_instantiate_rtas: start...\n");
658
659         prom_rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
660         if (prom_rtas != (ihandle) -1) {
661                 unsigned long x;
662                 x = prom_getprop(prom_rtas, "ibm,hypertas-functions",
663                                  hypertas_funcs, sizeof(hypertas_funcs));
664
665                 if (x != PROM_ERROR) {
666                         prom_printf("Hypertas detected, assuming LPAR !\n");
667                         _systemcfg->platform = PLATFORM_PSERIES_LPAR;
668                 }
669
670                 prom_getprop(prom_rtas, "rtas-size",
671                              &getprop_rval, sizeof(getprop_rval));
672                 _rtas->size = getprop_rval;
673                 prom_printf("instantiating rtas");
674                 if (_rtas->size != 0) {
675                         unsigned long rtas_region = RTAS_INSTANTIATE_MAX;
676
677                         /* Grab some space within the first RTAS_INSTANTIATE_MAX bytes
678                          * of physical memory (or within the RMO region) because RTAS
679                          * runs in 32-bit mode and relocate off.
680                          */
681                         if ( _systemcfg->platform == PLATFORM_PSERIES_LPAR ) {
682                                 struct lmb *_lmb  = PTRRELOC(&lmb);
683                                 rtas_region = min(_lmb->rmo_size, RTAS_INSTANTIATE_MAX);
684                         }
685
686                         _rtas->base = lmb_alloc_base(_rtas->size, PAGE_SIZE, rtas_region);
687
688                         prom_printf(" at 0x%x", _rtas->base);
689
690                         prom_rtas = call_prom("open", 1, 1, ADDR("/rtas"));
691                         prom_printf("...");
692
693                         if (call_prom("call-method", 3, 2,
694                                       ADDR("instantiate-rtas"),
695                                       prom_rtas,
696                                       _rtas->base) != PROM_ERROR) {
697                                 _rtas->entry = (long)_prom->args.rets[1];
698                         }
699                         RELOC(rtas_rmo_buf)
700                                 = lmb_alloc_base(RTAS_RMOBUF_MAX, PAGE_SIZE,
701                                                         rtas_region);
702                 }
703
704                 if (_rtas->entry <= 0) {
705                         prom_printf(" failed\n");
706                 } else {
707                         prom_printf(" done\n");
708                 }
709
710                 prom_debug("rtas->base          = 0x%x\n", _rtas->base);
711                 prom_debug("rtas->entry        = 0x%x\n", _rtas->entry);
712                 prom_debug("rtas->size          = 0x%x\n", _rtas->size);
713         }
714         prom_debug("prom_instantiate_rtas: end...\n");
715 }
716
717
718 #ifdef CONFIG_PMAC_DART
719 static void __init prom_initialize_dart_table(void)
720 {
721         unsigned long offset = reloc_offset();
722         extern unsigned long dart_tablebase;
723         extern unsigned long dart_tablesize;
724
725         /* Only reserve DART space if machine has more than 2GB of RAM
726          * or if requested with iommu=on on cmdline.
727          */
728         if (lmb_end_of_DRAM() <= 0x80000000ull && !RELOC(iommu_force_on))
729                 return;
730
731         /* 512 pages (2MB) is max DART tablesize. */
732         RELOC(dart_tablesize) = 1UL << 21;
733         /* 16MB (1 << 24) alignment. We allocate a full 16Mb chuck since we
734          * will blow up an entire large page anyway in the kernel mapping
735          */
736         RELOC(dart_tablebase) = (unsigned long)
737                 abs_to_virt(lmb_alloc_base(1UL<<24, 1UL<<24, 0x80000000L));
738
739         prom_printf("Dart at: %x\n", RELOC(dart_tablebase));
740 }
741 #endif /* CONFIG_PMAC_DART */
742
743 static void __init prom_initialize_tce_table(void)
744 {
745         phandle node;
746         ihandle phb_node;
747         unsigned long offset = reloc_offset();
748         char compatible[64], path[64], type[64], model[64];
749         unsigned long i, table = 0;
750         unsigned long base, vbase, align;
751         unsigned int minalign, minsize;
752         struct of_tce_table *prom_tce_table = RELOC(of_tce_table);
753         unsigned long tce_entry, *tce_entryp;
754
755         if (RELOC(ppc64_iommu_off))
756                 return;
757
758         prom_debug("starting prom_initialize_tce_table\n");
759
760         /* Search all nodes looking for PHBs. */
761         for (node = 0; prom_next_node(&node); ) {
762                 if (table == MAX_PHB) {
763                         prom_printf("WARNING: PCI host bridge ignored, "
764                                     "need to increase MAX_PHB\n");
765                         continue;
766                 }
767
768                 compatible[0] = 0;
769                 type[0] = 0;
770                 model[0] = 0;
771                 prom_getprop(node, "compatible",
772                              compatible, sizeof(compatible));
773                 prom_getprop(node, "device_type", type, sizeof(type));
774                 prom_getprop(node, "model", model, sizeof(model));
775
776                 /* Keep the old logic in tack to avoid regression. */
777                 if (compatible[0] != 0) {
778                         if ((strstr(compatible, RELOC("python")) == NULL) &&
779                             (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
780                             (strstr(compatible, RELOC("Winnipeg")) == NULL))
781                                 continue;
782                 } else if (model[0] != 0) {
783                         if ((strstr(model, RELOC("ython")) == NULL) &&
784                             (strstr(model, RELOC("peedwagon")) == NULL) &&
785                             (strstr(model, RELOC("innipeg")) == NULL))
786                                 continue;
787                 }
788
789                 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL)) {
790                         continue;
791                 }
792
793                 if (prom_getprop(node, "tce-table-minalign", &minalign,
794                                  sizeof(minalign)) == PROM_ERROR) {
795                         minalign = 0;
796                 }
797
798                 if (prom_getprop(node, "tce-table-minsize", &minsize,
799                                  sizeof(minsize)) == PROM_ERROR) {
800                         minsize = 4UL << 20;
801                 }
802
803                 /*
804                  * Even though we read what OF wants, we just set the table
805                  * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
806                  * By doing this, we avoid the pitfalls of trying to DMA to
807                  * MMIO space and the DMA alias hole.
808                  *
809                  * On POWER4, firmware sets the TCE region by assuming
810                  * each TCE table is 8MB. Using this memory for anything
811                  * else will impact performance, so we always allocate 8MB.
812                  * Anton
813                  */
814                 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
815                         minsize = 8UL << 20;
816                 else
817                         minsize = 4UL << 20;
818
819                 /* Align to the greater of the align or size */
820                 align = max(minalign, minsize);
821
822                 /* Carve out storage for the TCE table. */
823                 base = lmb_alloc(minsize, align);
824
825                 if ( !base ) {
826                         prom_panic("ERROR, cannot find space for TCE table.\n");
827                 }
828
829                 vbase = (unsigned long)abs_to_virt(base);
830
831                 /* Save away the TCE table attributes for later use. */
832                 prom_tce_table[table].node = node;
833                 prom_tce_table[table].base = vbase;
834                 prom_tce_table[table].size = minsize;
835
836                 prom_debug("TCE table: 0x%x\n", table);
837                 prom_debug("\tnode = 0x%x\n", node);
838                 prom_debug("\tbase = 0x%x\n", vbase);
839                 prom_debug("\tsize = 0x%x\n", minsize);
840
841                 /* Initialize the table to have a one-to-one mapping
842                  * over the allocated size.
843                  */
844                 tce_entryp = (unsigned long *)base;
845                 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
846                         tce_entry = (i << PAGE_SHIFT);
847                         tce_entry |= 0x3;
848                         *tce_entryp = tce_entry;
849                 }
850
851                 /* It seems OF doesn't null-terminate the path :-( */
852                 memset(path, 0, sizeof(path));
853                 /* Call OF to setup the TCE hardware */
854                 if (call_prom("package-to-path", 3, 1, node,
855                               path, sizeof(path)-1) == PROM_ERROR) {
856                         prom_printf("package-to-path failed\n");
857                 } else {
858                         prom_printf("opening PHB %s", path);
859                 }
860
861                 phb_node = call_prom("open", 1, 1, path);
862                 if ( (long)phb_node <= 0) {
863                         prom_printf("... failed\n");
864                 } else {
865                         prom_printf("... done\n");
866                 }
867                 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
868                           phb_node, -1, minsize,
869                           (u32) base, (u32) (base >> 32));
870                 call_prom("close", 1, 0, phb_node);
871
872                 table++;
873         }
874
875         /* Flag the first invalid entry */
876         prom_tce_table[table].node = 0;
877         prom_debug("ending prom_initialize_tce_table\n");
878 }
879
880 /*
881  * With CHRP SMP we need to use the OF to start the other
882  * processors so we can't wait until smp_boot_cpus (the OF is
883  * trashed by then) so we have to put the processors into
884  * a holding pattern controlled by the kernel (not OF) before
885  * we destroy the OF.
886  *
887  * This uses a chunk of low memory, puts some holding pattern
888  * code there and sends the other processors off to there until
889  * smp_boot_cpus tells them to do something.  The holding pattern
890  * checks that address until its cpu # is there, when it is that
891  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
892  * of setting those values.
893  *
894  * We also use physical address 0x4 here to tell when a cpu
895  * is in its holding pattern code.
896  *
897  * Fixup comment... DRENG / PPPBBB - Peter
898  *
899  * -- Cort
900  */
901 static void __init prom_hold_cpus(unsigned long mem)
902 {
903         unsigned long i;
904         unsigned int reg;
905         phandle node;
906         unsigned long offset = reloc_offset();
907         char type[64], *path;
908         int cpuid = 0;
909         unsigned int interrupt_server[MAX_CPU_THREADS];
910         unsigned int cpu_threads, hw_cpu_num;
911         int propsize;
912         extern void __secondary_hold(void);
913         extern unsigned long __secondary_hold_spinloop;
914         extern unsigned long __secondary_hold_acknowledge;
915         unsigned long *spinloop
916                 = (void *)virt_to_abs(&__secondary_hold_spinloop);
917         unsigned long *acknowledge
918                 = (void *)virt_to_abs(&__secondary_hold_acknowledge);
919         unsigned long secondary_hold
920                 = virt_to_abs(*PTRRELOC((unsigned long *)__secondary_hold));
921         struct systemcfg *_systemcfg = RELOC(systemcfg);
922         struct paca_struct *lpaca = PTRRELOC(&paca[0]);
923         struct prom_t *_prom = PTRRELOC(&prom);
924 #ifdef CONFIG_SMP
925         struct naca_struct *_naca = RELOC(naca);
926 #endif
927
928         /* On pmac, we just fill out the various global bitmasks and
929          * arrays indicating our CPUs are here, they are actually started
930          * later on from pmac_smp
931          */
932         if (_systemcfg->platform == PLATFORM_POWERMAC) {
933                 for (node = 0; prom_next_node(&node); ) {
934                         type[0] = 0;
935                         prom_getprop(node, "device_type", type, sizeof(type));
936                         if (strcmp(type, RELOC("cpu")) != 0)
937                                 continue;
938                         reg = -1;
939                         prom_getprop(node, "reg", &reg, sizeof(reg));
940                         lpaca[cpuid].hw_cpu_id = reg;
941
942 #ifdef CONFIG_SMP
943                         cpu_set(cpuid, RELOC(cpu_available_map));
944                         cpu_set(cpuid, RELOC(cpu_possible_map));
945                         cpu_set(cpuid, RELOC(cpu_present_at_boot));
946                         if (reg == 0)
947                                 cpu_set(cpuid, RELOC(cpu_online_map));
948 #endif /* CONFIG_SMP */
949                         cpuid++;
950                 }
951                 return;
952         }
953
954         /* Initially, we must have one active CPU. */
955         _systemcfg->processorCount = 1;
956
957         prom_debug("prom_hold_cpus: start...\n");
958         prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
959         prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
960         prom_debug("    1) acknowledge    = 0x%x\n",
961                    (unsigned long)acknowledge);
962         prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
963         prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
964
965         /* Set the common spinloop variable, so all of the secondary cpus
966          * will block when they are awakened from their OF spinloop.
967          * This must occur for both SMP and non SMP kernels, since OF will
968          * be trashed when we move the kernel.
969          */
970         *spinloop = 0;
971
972 #ifdef CONFIG_HMT
973         for (i=0; i < NR_CPUS; i++) {
974                 RELOC(hmt_thread_data)[i].pir = 0xdeadbeef;
975         }
976 #endif
977         /* look for cpus */
978         for (node = 0; prom_next_node(&node); ) {
979                 type[0] = 0;
980                 prom_getprop(node, "device_type", type, sizeof(type));
981                 if (strcmp(type, RELOC("cpu")) != 0)
982                         continue;
983
984                 /* Skip non-configured cpus. */
985                 prom_getprop(node, "status", type, sizeof(type));
986                 if (strcmp(type, RELOC("okay")) != 0)
987                         continue;
988
989                 reg = -1;
990                 prom_getprop(node, "reg", &reg, sizeof(reg));
991
992                 path = (char *) mem;
993                 memset(path, 0, 256);
994                 if (call_prom("package-to-path", 3, 1,
995                               node, path, 255) == PROM_ERROR)
996                         continue;
997
998                 prom_debug("\ncpuid        = 0x%x\n", cpuid);
999                 prom_debug("cpu hw idx   = 0x%x\n", reg);
1000                 lpaca[cpuid].hw_cpu_id = reg;
1001
1002                 /* Init the acknowledge var which will be reset by
1003                  * the secondary cpu when it awakens from its OF
1004                  * spinloop.
1005                  */
1006                 *acknowledge = (unsigned long)-1;
1007
1008                 propsize = prom_getprop(node, "ibm,ppc-interrupt-server#s",
1009                                         &interrupt_server,
1010                                         sizeof(interrupt_server));
1011                 if (propsize < 0) {
1012                         /* no property.  old hardware has no SMT */
1013                         cpu_threads = 1;
1014                         interrupt_server[0] = reg; /* fake it with phys id */
1015                 } else {
1016                         /* We have a threaded processor */
1017                         cpu_threads = propsize / sizeof(u32);
1018                         if (cpu_threads > MAX_CPU_THREADS) {
1019                                 prom_printf("SMT: too many threads!\n"
1020                                             "SMT: found %x, max is %x\n",
1021                                             cpu_threads, MAX_CPU_THREADS);
1022                                 cpu_threads = 1; /* ToDo: panic? */
1023                         }
1024                 }
1025
1026                 hw_cpu_num = interrupt_server[0];
1027                 if (hw_cpu_num != _prom->cpu) {
1028                         /* Primary Thread of non-boot cpu */
1029                         prom_printf("%x : starting cpu %s... ", cpuid, path);
1030                         call_prom("start-cpu", 3, 0, node,
1031                                   secondary_hold, cpuid);
1032
1033                         for ( i = 0 ; (i < 100000000) && 
1034                               (*acknowledge == ((unsigned long)-1)); i++ ) ;
1035
1036                         if (*acknowledge == cpuid) {
1037                                 prom_printf("... done\n");
1038                                 /* We have to get every CPU out of OF,
1039                                  * even if we never start it. */
1040                                 if (cpuid >= NR_CPUS)
1041                                         goto next;
1042 #ifdef CONFIG_SMP
1043                                 /* Set the number of active processors. */
1044                                 _systemcfg->processorCount++;
1045                                 cpu_set(cpuid, RELOC(cpu_available_map));
1046                                 cpu_set(cpuid, RELOC(cpu_possible_map));
1047                                 cpu_set(cpuid, RELOC(cpu_present_at_boot));
1048 #endif
1049                         } else {
1050                                 prom_printf("... failed: %x\n", *acknowledge);
1051                         }
1052                 }
1053 #ifdef CONFIG_SMP
1054                 else {
1055                         prom_printf("%x : booting  cpu %s\n", cpuid, path);
1056                         cpu_set(cpuid, RELOC(cpu_available_map));
1057                         cpu_set(cpuid, RELOC(cpu_possible_map));
1058                         cpu_set(cpuid, RELOC(cpu_online_map));
1059                         cpu_set(cpuid, RELOC(cpu_present_at_boot));
1060                 }
1061 #endif
1062 next:
1063 #ifdef CONFIG_SMP
1064                 /* Init paca for secondary threads.   They start later. */
1065                 for (i=1; i < cpu_threads; i++) {
1066                         cpuid++;
1067                         if (cpuid >= NR_CPUS)
1068                                 continue;
1069                         lpaca[cpuid].hw_cpu_id = interrupt_server[i];
1070                         prom_printf("%x : preparing thread ... ",
1071                                     interrupt_server[i]);
1072                         if (_naca->smt_state) {
1073                                 cpu_set(cpuid, RELOC(cpu_available_map));
1074                                 cpu_set(cpuid, RELOC(cpu_present_at_boot));
1075                                 prom_printf("available\n");
1076                         } else {
1077                                 prom_printf("not available\n");
1078                         }
1079                 }
1080 #endif
1081                 cpuid++;
1082         }
1083 #ifdef CONFIG_HMT
1084         /* Only enable HMT on processors that provide support. */
1085         if (__is_processor(PV_PULSAR) || 
1086             __is_processor(PV_ICESTAR) ||
1087             __is_processor(PV_SSTAR)) {
1088                 prom_printf("    starting secondary threads\n");
1089
1090                 for (i = 0; i < NR_CPUS; i += 2) {
1091                         if (!cpu_online(i))
1092                                 continue;
1093
1094                         if (i == 0) {
1095                                 unsigned long pir = _get_PIR();
1096                                 if (__is_processor(PV_PULSAR)) {
1097                                         RELOC(hmt_thread_data)[i].pir = 
1098                                                 pir & 0x1f;
1099                                 } else {
1100                                         RELOC(hmt_thread_data)[i].pir = 
1101                                                 pir & 0x3ff;
1102                                 }
1103                         }
1104 /*                      cpu_set(i+1, cpu_online_map); */
1105                         cpu_set(i+1, RELOC(cpu_possible_map));
1106                 }
1107                 _systemcfg->processorCount *= 2;
1108         } else {
1109                 prom_printf("Processor is not HMT capable\n");
1110         }
1111 #endif
1112
1113         if (cpuid > NR_CPUS)
1114                 prom_printf("WARNING: maximum CPUs (" __stringify(NR_CPUS)
1115                             ") exceeded: ignoring extras\n");
1116
1117         prom_debug("prom_hold_cpus: end...\n");
1118 }
1119
1120 static void __init smt_setup(void)
1121 {
1122         char *p, *q;
1123         char my_smt_enabled = SMT_DYNAMIC;
1124         ihandle prom_options = 0;
1125         char option[9];
1126         unsigned long offset = reloc_offset();
1127         struct naca_struct *_naca = RELOC(naca);
1128         char found = 0;
1129
1130         if (strstr(RELOC(cmd_line), RELOC("smt-enabled="))) {
1131                 for (q = RELOC(cmd_line); (p = strstr(q, RELOC("smt-enabled="))) != 0; ) {
1132                         q = p + 12;
1133                         if (p > RELOC(cmd_line) && p[-1] != ' ')
1134                                 continue;
1135                         found = 1;
1136                         if (q[0] == 'o' && q[1] == 'f' && 
1137                             q[2] == 'f' && (q[3] == ' ' || q[3] == '\0')) {
1138                                 my_smt_enabled = SMT_OFF;
1139                         } else if (q[0]=='o' && q[1] == 'n' && 
1140                                    (q[2] == ' ' || q[2] == '\0')) {
1141                                 my_smt_enabled = SMT_ON;
1142                         } else {
1143                                 my_smt_enabled = SMT_DYNAMIC;
1144                         } 
1145                 }
1146         }
1147         if (!found) {
1148                 prom_options = call_prom("finddevice", 1, 1, ADDR("/options"));
1149                 if (prom_options != (ihandle) -1) {
1150                         prom_getprop(prom_options, "ibm,smt-enabled",
1151                                      option, sizeof(option));
1152                         if (option[0] != 0) {
1153                                 found = 1;
1154                                 if (!strcmp(option, RELOC("off")))
1155                                         my_smt_enabled = SMT_OFF;
1156                                 else if (!strcmp(option, RELOC("on")))
1157                                         my_smt_enabled = SMT_ON;
1158                                 else
1159                                         my_smt_enabled = SMT_DYNAMIC;
1160                         }
1161                 }
1162         }
1163
1164         if (!found )
1165                 my_smt_enabled = SMT_DYNAMIC; /* default to on */
1166
1167         _naca->smt_state = my_smt_enabled;
1168 }
1169
1170
1171 #ifdef CONFIG_BOOTX_TEXT
1172
1173 /* This function will enable the early boot text when doing OF booting. This
1174  * way, xmon output should work too
1175  */
1176 static void __init setup_disp_fake_bi(ihandle dp)
1177 {
1178         int width = 640, height = 480, depth = 8, pitch;
1179         unsigned address;
1180         struct pci_reg_property addrs[8];
1181         int i, naddrs;
1182         char name[64];
1183         unsigned long offset = reloc_offset();
1184
1185         memset(name, 0, sizeof(name));
1186         prom_getprop(dp, "name", name, sizeof(name));
1187         name[sizeof(name)-1] = 0;
1188         prom_printf("Initializing fake screen: %s\n", name);
1189
1190         prom_getprop(dp, "width", &width, sizeof(width));
1191         prom_getprop(dp, "height", &height, sizeof(height));
1192         prom_getprop(dp, "depth", &depth, sizeof(depth));
1193         pitch = width * ((depth + 7) / 8);
1194         prom_getprop(dp, "linebytes", &pitch, sizeof(pitch));
1195         if (pitch == 1)
1196                 pitch = 0x1000;         /* for strange IBM display */
1197         address = 0;
1198
1199         prom_printf("width %x height %x depth %x linebytes %x\n",
1200                     width, height, depth, depth);
1201
1202         prom_getprop(dp, "address", &address, sizeof(address));
1203         if (address == 0) {
1204                 /* look for an assigned address with a size of >= 1MB */
1205                 naddrs = prom_getprop(dp, "assigned-addresses",
1206                                       addrs, sizeof(addrs));
1207                 naddrs /= sizeof(struct pci_reg_property);
1208                 for (i = 0; i < naddrs; ++i) {
1209                         if (addrs[i].size_lo >= (1 << 20)) {
1210                                 address = addrs[i].addr.a_lo;
1211                                 /* use the BE aperture if possible */
1212                                 if (addrs[i].size_lo >= (16 << 20))
1213                                         address += (8 << 20);
1214                                 break;
1215                         }
1216                 }
1217                 if (address == 0) {
1218                         prom_printf("Failed to get address of frame buffer\n");
1219                         return;
1220                 }
1221         }
1222         btext_setup_display(width, height, depth, pitch, address);
1223         prom_printf("Addr of fb: %x\n", address);
1224         RELOC(boot_text_mapped) = 0;
1225 }
1226 #endif /* CONFIG_BOOTX_TEXT */
1227
1228 static void __init prom_init_client_services(unsigned long pp)
1229 {
1230         unsigned long offset = reloc_offset();
1231         struct prom_t *_prom = PTRRELOC(&prom);
1232
1233         /* Get a handle to the prom entry point before anything else */
1234         _prom->entry = pp;
1235
1236         /* Init default value for phys size */
1237         _prom->encode_phys_size = 32;
1238
1239         /* get a handle for the stdout device */
1240         _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1241         if ((long)_prom->chosen <= 0)
1242                 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1243
1244         /* get device tree root */
1245         _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1246         if ((long)_prom->root <= 0)
1247                 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1248 }
1249
1250 static void __init prom_init_stdout(void)
1251 {
1252         unsigned long offset = reloc_offset();
1253         struct prom_t *_prom = PTRRELOC(&prom);
1254         u32 val;
1255
1256         if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1257                 prom_panic("cannot find stdout");
1258
1259         _prom->stdout = val;
1260 }
1261
1262 static int __init prom_find_machine_type(void)
1263 {
1264         unsigned long offset = reloc_offset();
1265         struct prom_t *_prom = PTRRELOC(&prom);
1266         char compat[256];
1267         int len, i = 0;
1268
1269         len = prom_getprop(_prom->root, "compatible",
1270                            compat, sizeof(compat)-1);
1271         if (len > 0) {
1272                 compat[len] = 0;
1273                 while (i < len) {
1274                         char *p = &compat[i];
1275                         int sl = strlen(p);
1276                         if (sl == 0)
1277                                 break;
1278                         if (strstr(p, RELOC("Power Macintosh")) ||
1279                             strstr(p, RELOC("MacRISC4")))
1280                                 return PLATFORM_POWERMAC;
1281                         i += sl + 1;
1282                 }
1283         }
1284         /* Default to pSeries */
1285         return PLATFORM_PSERIES;
1286 }
1287
1288 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1289 {
1290         unsigned long offset = reloc_offset();
1291
1292         return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1293 }
1294
1295 /*
1296  * If we have a display that we don't know how to drive,
1297  * we will want to try to execute OF's open method for it
1298  * later.  However, OF will probably fall over if we do that
1299  * we've taken over the MMU.
1300  * So we check whether we will need to open the display,
1301  * and if so, open it now.
1302  */
1303 static unsigned long __init check_display(unsigned long mem)
1304 {
1305         phandle node;
1306         ihandle ih;
1307         int i, j;
1308         unsigned long offset = reloc_offset();
1309         struct prom_t *_prom = PTRRELOC(&prom);
1310         char type[16], *path;
1311         static unsigned char default_colors[] = {
1312                 0x00, 0x00, 0x00,
1313                 0x00, 0x00, 0xaa,
1314                 0x00, 0xaa, 0x00,
1315                 0x00, 0xaa, 0xaa,
1316                 0xaa, 0x00, 0x00,
1317                 0xaa, 0x00, 0xaa,
1318                 0xaa, 0xaa, 0x00,
1319                 0xaa, 0xaa, 0xaa,
1320                 0x55, 0x55, 0x55,
1321                 0x55, 0x55, 0xff,
1322                 0x55, 0xff, 0x55,
1323                 0x55, 0xff, 0xff,
1324                 0xff, 0x55, 0x55,
1325                 0xff, 0x55, 0xff,
1326                 0xff, 0xff, 0x55,
1327                 0xff, 0xff, 0xff
1328         };
1329         const unsigned char *clut;
1330
1331         _prom->disp_node = 0;
1332
1333         prom_printf("Looking for displays\n");
1334         if (RELOC(of_stdout_device) != 0)
1335                 prom_printf("OF stdout is    : %s\n",
1336                             PTRRELOC(RELOC(of_stdout_device)));
1337         for (node = 0; prom_next_node(&node); ) {
1338                 type[0] = 0;
1339                 prom_getprop(node, "device_type", type, sizeof(type));
1340                 if (strcmp(type, RELOC("display")) != 0)
1341                         continue;
1342                 /* It seems OF doesn't null-terminate the path :-( */
1343                 path = (char *) mem;
1344                 memset(path, 0, 256);
1345
1346                 /*
1347                  * leave some room at the end of the path for appending extra
1348                  * arguments
1349                  */
1350                 if (call_prom("package-to-path", 3, 1, node, path, 250) < 0)
1351                         continue;
1352                 prom_printf("found display   : %s\n", path);
1353                 
1354                 /*
1355                  * If this display is the device that OF is using for stdout,
1356                  * move it to the front of the list.
1357                  */
1358                 mem += strlen(path) + 1;
1359                 i = RELOC(prom_num_displays);
1360                 RELOC(prom_num_displays) = i + 1;
1361                 if (RELOC(of_stdout_device) != 0 && i > 0
1362                     && strcmp(PTRRELOC(RELOC(of_stdout_device)), path) == 0) {
1363                         for (; i > 0; --i) {
1364                                 RELOC(prom_display_paths[i])
1365                                         = RELOC(prom_display_paths[i-1]);
1366                                 RELOC(prom_display_nodes[i])
1367                                         = RELOC(prom_display_nodes[i-1]);
1368                         }
1369                         _prom->disp_node = node;
1370                 }
1371                 RELOC(prom_display_paths[i]) = PTRUNRELOC(path);
1372                 RELOC(prom_display_nodes[i]) = node;
1373                 if (_prom->disp_node == 0)
1374                         _prom->disp_node = node;
1375                 if (RELOC(prom_num_displays) >= FB_MAX)
1376                         break;
1377         }
1378         prom_printf("Opening displays...\n");
1379         for (j = RELOC(prom_num_displays) - 1; j >= 0; j--) {
1380                 path = PTRRELOC(RELOC(prom_display_paths[j]));
1381                 prom_printf("opening display : %s", path);
1382                 ih = call_prom("open", 1, 1, path);
1383                 if (ih == (ihandle)0 || ih == (ihandle)-1) {
1384                         prom_printf("... failed\n");
1385                         continue;
1386                 }
1387
1388                 prom_printf("... done\n");
1389
1390                 /* Setup a useable color table when the appropriate
1391                  * method is available. Should update this to set-colors */
1392                 clut = RELOC(default_colors);
1393                 for (i = 0; i < 32; i++, clut += 3)
1394                         if (prom_set_color(ih, i, clut[0], clut[1],
1395                                            clut[2]) != 0)
1396                                 break;
1397
1398 #ifdef CONFIG_LOGO_LINUX_CLUT224
1399                 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1400                 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1401                         if (prom_set_color(ih, i + 32, clut[0], clut[1],
1402                                            clut[2]) != 0)
1403                                 break;
1404 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
1405         }
1406
1407         return DOUBLEWORD_ALIGN(mem);
1408 }
1409
1410 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1411 static void __init *__make_room(unsigned long *mem_start, unsigned long *mem_end,
1412                                 unsigned long needed, unsigned long align)
1413 {
1414         void *ret;
1415
1416         *mem_start = ALIGN(*mem_start, align);
1417         if (*mem_start + needed > *mem_end) {
1418 #ifdef CONFIG_BLK_DEV_INITRD
1419                 unsigned long offset = reloc_offset();
1420                 /* FIXME: Apple OF doesn't map unclaimed mem.  If this
1421                  * ever happened on G5, we'd need to fix. */
1422                 unsigned long initrd_len;
1423
1424                 if (*mem_end != RELOC(initrd_start))
1425                         prom_panic("No memory for copy_device_tree");
1426
1427                 prom_printf("Huge device_tree: moving initrd\n");
1428                 /* Move by 4M. */
1429                 initrd_len = RELOC(initrd_end) - RELOC(initrd_start);
1430                 *mem_end = RELOC(initrd_start) + 4 * 1024 * 1024;
1431                 memmove((void *)*mem_end, (void *)RELOC(initrd_start),
1432                         initrd_len);
1433                 RELOC(initrd_start) = *mem_end;
1434                 RELOC(initrd_end) = RELOC(initrd_start) + initrd_len;
1435 #else
1436                 prom_panic("No memory for copy_device_tree");
1437 #endif
1438         }
1439
1440         ret = (void *)*mem_start;
1441         *mem_start += needed;
1442
1443         return ret;
1444 }
1445
1446 #define make_room(startp, endp, type) \
1447         __make_room(startp, endp, sizeof(type), __alignof__(type))
1448
1449 static void __init
1450 inspect_node(phandle node, struct device_node *dad,
1451              unsigned long *mem_start, unsigned long *mem_end,
1452              struct device_node ***allnextpp)
1453 {
1454         int l;
1455         phandle child;
1456         struct device_node *np;
1457         struct property *pp, **prev_propp;
1458         char *prev_name, *namep;
1459         unsigned char *valp;
1460         unsigned long offset = reloc_offset();
1461         phandle ibm_phandle;
1462
1463         np = make_room(mem_start, mem_end, struct device_node);
1464         memset(np, 0, sizeof(*np));
1465
1466         np->node = node;
1467         **allnextpp = PTRUNRELOC(np);
1468         *allnextpp = &np->allnext;
1469         if (dad != 0) {
1470                 np->parent = PTRUNRELOC(dad);
1471                 /* we temporarily use the `next' field as `last_child'. */
1472                 if (dad->next == 0)
1473                         dad->child = PTRUNRELOC(np);
1474                 else
1475                         dad->next->sibling = PTRUNRELOC(np);
1476                 dad->next = np;
1477         }
1478
1479         /* get and store all properties */
1480         prev_propp = &np->properties;
1481         prev_name = RELOC("");
1482         for (;;) {
1483                 /* 32 is max len of name including nul. */
1484                 namep = make_room(mem_start, mem_end, char[32]);
1485                 if (call_prom("nextprop", 3, 1, node, prev_name, namep) <= 0) {
1486                         /* No more nodes: unwind alloc */
1487                         *mem_start = (unsigned long)namep;
1488                         break;
1489                 }
1490                 /* Trim off some if we can */
1491                 *mem_start = DOUBLEWORD_ALIGN((unsigned long)namep
1492                                              + strlen(namep) + 1);
1493                 pp = make_room(mem_start, mem_end, struct property);
1494                 pp->name = PTRUNRELOC(namep);
1495                 prev_name = namep;
1496
1497                 pp->length = call_prom("getproplen", 2, 1, node, namep);
1498                 if (pp->length < 0)
1499                         continue;
1500                 if (pp->length > MAX_PROPERTY_LENGTH) {
1501                         char path[128];
1502
1503                         prom_printf("WARNING: ignoring large property ");
1504                         /* It seems OF doesn't null-terminate the path :-( */
1505                         memset(path, 0, sizeof(path));
1506                         if (call_prom("package-to-path", 3, 1, node,
1507                                       path, sizeof(path)-1) > 0)
1508                                 prom_printf("[%s] ", path);
1509                         prom_printf("%s length 0x%x\n", namep, pp->length);
1510                         continue;
1511                 }
1512                 valp = __make_room(mem_start, mem_end, pp->length, 1);
1513                 pp->value = PTRUNRELOC(valp);
1514                 call_prom("getprop", 4, 1, node, namep, valp, pp->length);
1515                 *prev_propp = PTRUNRELOC(pp);
1516                 prev_propp = &pp->next;
1517         }
1518
1519         /* Add a "linux,phandle" property. */
1520         namep = make_room(mem_start, mem_end, char[16]);
1521         strcpy(namep, RELOC("linux,phandle"));
1522         pp = make_room(mem_start, mem_end, struct property);
1523         pp->name = PTRUNRELOC(namep);
1524         pp->length = sizeof(phandle);
1525         valp = make_room(mem_start, mem_end, phandle);
1526         pp->value = PTRUNRELOC(valp);
1527         *(phandle *)valp = node;
1528         *prev_propp = PTRUNRELOC(pp);
1529         pp->next = NULL;
1530
1531         /* Set np->linux_phandle to the value of the ibm,phandle property
1532            if it exists, otherwise to the phandle for this node. */
1533         np->linux_phandle = node;
1534         if (prom_getprop(node, "ibm,phandle",
1535                          &ibm_phandle, sizeof(ibm_phandle)) > 0)
1536                 np->linux_phandle = ibm_phandle;
1537
1538         /* get the node's full name */
1539         namep = (char *)*mem_start;
1540         l = call_prom("package-to-path", 3, 1, node,
1541                       namep, *mem_end - *mem_start);
1542         if (l >= 0) {
1543                 /* Didn't fit?  Get more room. */
1544                 if (l+1 > *mem_end - *mem_start) {
1545                         namep = __make_room(mem_start, mem_end, l+1, 1);
1546                         call_prom("package-to-path", 3, 1, node, namep, l);
1547                 }
1548                 np->full_name = PTRUNRELOC(namep);
1549                 namep[l] = '\0';
1550                 *mem_start = DOUBLEWORD_ALIGN(*mem_start + l + 1);
1551         }
1552
1553         /* do all our children */
1554         child = call_prom("child", 1, 1, node);
1555         while (child != (phandle)0) {
1556                 inspect_node(child, np, mem_start, mem_end,
1557                                          allnextpp);
1558                 child = call_prom("peer", 1, 1, child);
1559         }
1560 }
1561
1562 /*
1563  * Make a copy of the device tree from the PROM.
1564  */
1565 static unsigned long __init
1566 copy_device_tree(unsigned long mem_start)
1567 {
1568         phandle root;
1569         struct device_node **allnextp;
1570         unsigned long offset = reloc_offset();
1571         unsigned long mem_end;
1572
1573         /* We pass mem_end-mem_start to OF: keep it well under 32-bit */
1574         mem_end = mem_start + 1024*1024*1024;
1575 #ifdef CONFIG_BLK_DEV_INITRD
1576         if (RELOC(initrd_start) && RELOC(initrd_start) > mem_start)
1577                 mem_end = RELOC(initrd_start);
1578 #endif /* CONFIG_BLK_DEV_INITRD */
1579
1580         root = call_prom("peer", 1, 1, (phandle)0);
1581         if (root == (phandle)0) {
1582                 prom_panic("couldn't get device tree root\n");
1583         }
1584         allnextp = &RELOC(allnodes);
1585         inspect_node(root, NULL, &mem_start, &mem_end, &allnextp);
1586         *allnextp = NULL;
1587         return mem_start;
1588 }
1589
1590 /* Verify bi_recs are good */
1591 static struct bi_record * __init prom_bi_rec_verify(struct bi_record *bi_recs)
1592 {
1593         struct bi_record *first, *last;
1594
1595         prom_debug("birec_verify: r6=0x%x\n", (unsigned long)bi_recs);
1596         if (bi_recs != NULL)
1597                 prom_debug("  tag=0x%x\n", bi_recs->tag);
1598
1599         if ( bi_recs == NULL || bi_recs->tag != BI_FIRST )
1600                 return NULL;
1601
1602         last = (struct bi_record *)(long)bi_recs->data[0];
1603
1604         prom_debug("  last=0x%x\n", (unsigned long)last);
1605         if (last != NULL)
1606                 prom_debug("  last_tag=0x%x\n", last->tag);
1607
1608         if ( last == NULL || last->tag != BI_LAST )
1609                 return NULL;
1610
1611         first = (struct bi_record *)(long)last->data[0];
1612         prom_debug("  first=0x%x\n", (unsigned long)first);
1613
1614         if ( first == NULL || first != bi_recs )
1615                 return NULL;
1616
1617         return bi_recs;
1618 }
1619
1620 static void __init prom_bi_rec_reserve(void)
1621 {
1622         unsigned long offset = reloc_offset();
1623         struct prom_t *_prom = PTRRELOC(&prom);
1624         struct bi_record *rec;
1625
1626         if ( _prom->bi_recs != NULL) {
1627
1628                 for ( rec=_prom->bi_recs;
1629                       rec->tag != BI_LAST;
1630                       rec=bi_rec_next(rec) ) {
1631                         prom_debug("bi: 0x%x\n", rec->tag);
1632                         switch (rec->tag) {
1633 #ifdef CONFIG_BLK_DEV_INITRD
1634                         case BI_INITRD:
1635                                 RELOC(initrd_start) = (unsigned long)(rec->data[0]);
1636                                 RELOC(initrd_end) = RELOC(initrd_start) + rec->data[1];
1637                                 break;
1638 #endif /* CONFIG_BLK_DEV_INITRD */
1639                         }
1640                 }
1641                 /* The next use of this field will be after relocation
1642                  * is enabled, so convert this physical address into a
1643                  * virtual address.
1644                  */
1645                 _prom->bi_recs = PTRUNRELOC(_prom->bi_recs);
1646         }
1647 }
1648
1649 /*
1650  * We enter here early on, when the Open Firmware prom is still
1651  * handling exceptions and the MMU hash table for us.
1652  */
1653
1654 unsigned long __init
1655 prom_init(unsigned long r3, unsigned long r4, unsigned long pp,
1656           unsigned long r6, unsigned long r7)
1657 {
1658         unsigned long mem;
1659         ihandle prom_cpu;
1660         phandle cpu_pkg;
1661         unsigned long offset = reloc_offset();
1662         long l;
1663         char *p, *d;
1664         unsigned long phys;
1665         u32 getprop_rval;
1666         struct systemcfg *_systemcfg;
1667         struct paca_struct *lpaca = PTRRELOC(&paca[0]);
1668         struct prom_t *_prom = PTRRELOC(&prom);
1669
1670         /* First zero the BSS -- use memset, some arches don't have
1671          * caches on yet */
1672         memset(PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
1673
1674         /* Setup systemcfg and NACA pointers now */
1675         RELOC(systemcfg) = _systemcfg = (struct systemcfg *)(SYSTEMCFG_VIRT_ADDR - offset);
1676         RELOC(naca) = (struct naca_struct *)(NACA_VIRT_ADDR - offset);
1677
1678         /* Init interface to Open Firmware and pickup bi-recs */
1679         prom_init_client_services(pp);
1680
1681         /* Init prom stdout device */
1682         prom_init_stdout();
1683
1684         prom_debug("klimit=0x%x\n", RELOC(klimit));
1685         prom_debug("offset=0x%x\n", offset);
1686         prom_debug("->mem=0x%x\n", RELOC(klimit) - offset);
1687
1688         /* check out if we have bi_recs */
1689         _prom->bi_recs = prom_bi_rec_verify((struct bi_record *)r6);
1690         if ( _prom->bi_recs != NULL ) {
1691                 RELOC(klimit) = PTRUNRELOC((unsigned long)_prom->bi_recs +
1692                                            _prom->bi_recs->data[1]);
1693                 prom_debug("bi_recs=0x%x\n", (unsigned long)_prom->bi_recs);
1694                 prom_debug("new mem=0x%x\n", RELOC(klimit) - offset);
1695         }
1696
1697         /* If we don't have birec's or didn't find them, check for an initrd
1698          * using the "yaboot" way
1699          */
1700 #ifdef CONFIG_BLK_DEV_INITRD
1701         if ( _prom->bi_recs == NULL && r3 && r4 && r4 != 0xdeadbeef) {
1702                 RELOC(initrd_start) = (r3 >= KERNELBASE) ? __pa(r3) : r3;
1703                 RELOC(initrd_end) = RELOC(initrd_start) + r4;
1704                 RELOC(initrd_below_start_ok) = 1;
1705         }
1706 #endif /* CONFIG_BLK_DEV_INITRD */
1707
1708         /* Default machine type. */
1709         _systemcfg->platform = prom_find_machine_type();
1710
1711         /* On pSeries, copy the CPU hold code */
1712         if (_systemcfg->platform == PLATFORM_PSERIES)
1713                 copy_and_flush(0, KERNELBASE - offset, 0x100, 0);
1714
1715         /* Start storing things at klimit */
1716         mem = RELOC(klimit) - offset;
1717
1718         /* Get the full OF pathname of the stdout device */
1719         p = (char *) mem;
1720         memset(p, 0, 256);
1721         call_prom("instance-to-path", 3, 1, _prom->stdout, p, 255);
1722         RELOC(of_stdout_device) = PTRUNRELOC(p);
1723         mem += strlen(p) + 1;
1724
1725         getprop_rval = 1;
1726         prom_getprop(_prom->root, "#size-cells",
1727                      &getprop_rval, sizeof(getprop_rval));
1728         _prom->encode_phys_size = (getprop_rval == 1) ? 32 : 64;
1729
1730         /* Determine which cpu is actually running right _now_ */
1731         if (prom_getprop(_prom->chosen, "cpu",
1732                          &prom_cpu, sizeof(prom_cpu)) <= 0)
1733                 prom_panic("cannot find boot cpu");
1734
1735         cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
1736         prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
1737         _prom->cpu = getprop_rval;
1738         lpaca[0].hw_cpu_id = _prom->cpu;
1739
1740         RELOC(boot_cpuid) = 0;
1741
1742         prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
1743
1744         /* Get the boot device and translate it to a full OF pathname. */
1745         p = (char *) mem;
1746         l = prom_getprop(_prom->chosen, "bootpath", p, 1<<20);
1747         if (l > 0) {
1748                 p[l] = 0;       /* should already be null-terminated */
1749                 RELOC(bootpath) = PTRUNRELOC(p);
1750                 mem += l + 1;
1751                 d = (char *) mem;
1752                 *d = 0;
1753                 call_prom("canon", 3, 1, p, d, 1<<20);
1754                 RELOC(bootdevice) = PTRUNRELOC(d);
1755                 mem = DOUBLEWORD_ALIGN(mem + strlen(d) + 1);
1756         }
1757
1758         RELOC(cmd_line[0]) = 0;
1759         if ((long)_prom->chosen > 0) {
1760                 prom_getprop(_prom->chosen, "bootargs", p, sizeof(cmd_line));
1761                 if (p != NULL && p[0] != 0)
1762                         strlcpy(RELOC(cmd_line), p, sizeof(cmd_line));
1763         }
1764
1765         early_cmdline_parse();
1766
1767         prom_initialize_lmb();
1768
1769         prom_bi_rec_reserve();
1770
1771         mem = check_display(mem);
1772
1773         if (_systemcfg->platform != PLATFORM_POWERMAC)
1774                 prom_instantiate_rtas();
1775
1776         /* Initialize some system info into the Naca early... */
1777         prom_initialize_naca();
1778
1779         smt_setup();
1780
1781         /* If we are on an SMP machine, then we *MUST* do the
1782          * following, regardless of whether we have an SMP
1783          * kernel or not.
1784          */
1785         prom_hold_cpus(mem);
1786
1787         prom_debug("after basic inits, mem=0x%x\n", mem);
1788 #ifdef CONFIG_BLK_DEV_INITRD
1789         prom_debug("initrd_start=0x%x\n", RELOC(initrd_start));
1790         prom_debug("initrd_end=0x%x\n", RELOC(initrd_end));
1791 #endif /* CONFIG_BLK_DEV_INITRD */
1792         prom_debug("copying OF device tree...\n");
1793
1794         mem = copy_device_tree(mem);
1795
1796         RELOC(klimit) = mem + offset;
1797
1798         prom_debug("new klimit is\n");
1799         prom_debug("klimit=0x%x\n", RELOC(klimit));
1800         prom_debug(" ->mem=0x%x\n", mem);
1801
1802         lmb_reserve(0, __pa(RELOC(klimit)));
1803
1804 #ifdef CONFIG_BLK_DEV_INITRD
1805         if (RELOC(initrd_start)) {
1806                 unsigned long initrd_len;
1807                 initrd_len = RELOC(initrd_end) - RELOC(initrd_start);
1808
1809                 /* Move initrd if it's where we're going to copy kernel. */
1810                 if (RELOC(initrd_start) < __pa(RELOC(klimit))) {
1811                         memmove((void *)mem, (void *)RELOC(initrd_start),
1812                                 initrd_len);
1813                         RELOC(initrd_start) = mem;
1814                         RELOC(initrd_end) = mem + initrd_len;
1815                 }
1816
1817                 lmb_reserve(RELOC(initrd_start), initrd_len);
1818         }
1819 #endif /* CONFIG_BLK_DEV_INITRD */
1820
1821         if (_systemcfg->platform == PLATFORM_PSERIES)
1822                 prom_initialize_tce_table();
1823
1824 #ifdef CONFIG_PMAC_DART
1825         if (_systemcfg->platform == PLATFORM_POWERMAC)
1826                 prom_initialize_dart_table();
1827 #endif
1828
1829 #ifdef CONFIG_BOOTX_TEXT
1830         if (_prom->disp_node) {
1831                 prom_printf("Setting up bi display...\n");
1832                 setup_disp_fake_bi(_prom->disp_node);
1833         }
1834 #endif /* CONFIG_BOOTX_TEXT */
1835
1836         prom_printf("Calling quiesce ...\n");
1837         call_prom("quiesce", 0, 0);
1838         phys = KERNELBASE - offset;
1839
1840 #ifdef CONFIG_BLK_DEV_INITRD
1841         /* If we had an initrd, we convert its address to virtual */
1842         if (RELOC(initrd_start)) {
1843                 RELOC(initrd_start) = (unsigned long)__va(RELOC(initrd_start));
1844                 RELOC(initrd_end) = (unsigned long)__va(RELOC(initrd_end));
1845         }
1846 #endif /* CONFIG_BLK_DEV_INITRD */
1847
1848         prom_printf("returning from prom_init\n");
1849         return phys;
1850 }
1851
1852 /*
1853  * Find the device_node with a given phandle.
1854  */
1855 static struct device_node * __devinit
1856 find_phandle(phandle ph)
1857 {
1858         struct device_node *np;
1859
1860         for (np = allnodes; np != 0; np = np->allnext)
1861                 if (np->linux_phandle == ph)
1862                         return np;
1863         return NULL;
1864 }
1865
1866 /*
1867  * Find the interrupt parent of a node.
1868  */
1869 static struct device_node * __devinit
1870 intr_parent(struct device_node *p)
1871 {
1872         phandle *parp;
1873
1874         parp = (phandle *) get_property(p, "interrupt-parent", NULL);
1875         if (parp == NULL)
1876                 return p->parent;
1877         return find_phandle(*parp);
1878 }
1879
1880 /*
1881  * Find out the size of each entry of the interrupts property
1882  * for a node.
1883  */
1884 int __devinit prom_n_intr_cells(struct device_node *np)
1885 {
1886         struct device_node *p;
1887         unsigned int *icp;
1888
1889         for (p = np; (p = intr_parent(p)) != NULL; ) {
1890                 icp = (unsigned int *)
1891                         get_property(p, "#interrupt-cells", NULL);
1892                 if (icp != NULL)
1893                         return *icp;
1894                 if (get_property(p, "interrupt-controller", NULL) != NULL
1895                     || get_property(p, "interrupt-map", NULL) != NULL) {
1896                         printk("oops, node %s doesn't have #interrupt-cells\n",
1897                                p->full_name);
1898                         return 1;
1899                 }
1900         }
1901 #ifdef DEBUG_IRQ
1902         printk("prom_n_intr_cells failed for %s\n", np->full_name);
1903 #endif
1904         return 1;
1905 }
1906
1907 /*
1908  * Map an interrupt from a device up to the platform interrupt
1909  * descriptor.
1910  */
1911 static int __devinit
1912 map_interrupt(unsigned int **irq, struct device_node **ictrler,
1913               struct device_node *np, unsigned int *ints, int nintrc)
1914 {
1915         struct device_node *p, *ipar;
1916         unsigned int *imap, *imask, *ip;
1917         int i, imaplen, match;
1918         int newintrc, newaddrc;
1919         unsigned int *reg;
1920         int naddrc;
1921
1922         reg = (unsigned int *) get_property(np, "reg", NULL);
1923         naddrc = prom_n_addr_cells(np);
1924         p = intr_parent(np);
1925         while (p != NULL) {
1926                 if (get_property(p, "interrupt-controller", NULL) != NULL)
1927                         /* this node is an interrupt controller, stop here */
1928                         break;
1929                 imap = (unsigned int *)
1930                         get_property(p, "interrupt-map", &imaplen);
1931                 if (imap == NULL) {
1932                         p = intr_parent(p);
1933                         continue;
1934                 }
1935                 imask = (unsigned int *)
1936                         get_property(p, "interrupt-map-mask", NULL);
1937                 if (imask == NULL) {
1938                         printk("oops, %s has interrupt-map but no mask\n",
1939                                p->full_name);
1940                         return 0;
1941                 }
1942                 imaplen /= sizeof(unsigned int);
1943                 match = 0;
1944                 ipar = NULL;
1945                 while (imaplen > 0 && !match) {
1946                         /* check the child-interrupt field */
1947                         match = 1;
1948                         for (i = 0; i < naddrc && match; ++i)
1949                                 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
1950                         for (; i < naddrc + nintrc && match; ++i)
1951                                 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
1952                         imap += naddrc + nintrc;
1953                         imaplen -= naddrc + nintrc;
1954                         /* grab the interrupt parent */
1955                         ipar = find_phandle((phandle) *imap++);
1956                         --imaplen;
1957                         if (ipar == NULL) {
1958                                 printk("oops, no int parent %x in map of %s\n",
1959                                        imap[-1], p->full_name);
1960                                 return 0;
1961                         }
1962                         /* find the parent's # addr and intr cells */
1963                         ip = (unsigned int *)
1964                                 get_property(ipar, "#interrupt-cells", NULL);
1965                         if (ip == NULL) {
1966                                 printk("oops, no #interrupt-cells on %s\n",
1967                                        ipar->full_name);
1968                                 return 0;
1969                         }
1970                         newintrc = *ip;
1971                         ip = (unsigned int *)
1972                                 get_property(ipar, "#address-cells", NULL);
1973                         newaddrc = (ip == NULL)? 0: *ip;
1974                         imap += newaddrc + newintrc;
1975                         imaplen -= newaddrc + newintrc;
1976                 }
1977                 if (imaplen < 0) {
1978                         printk("oops, error decoding int-map on %s, len=%d\n",
1979                                p->full_name, imaplen);
1980                         return 0;
1981                 }
1982                 if (!match) {
1983 #ifdef DEBUG_IRQ
1984                         printk("oops, no match in %s int-map for %s\n",
1985                                p->full_name, np->full_name);
1986 #endif
1987                         return 0;
1988                 }
1989                 p = ipar;
1990                 naddrc = newaddrc;
1991                 nintrc = newintrc;
1992                 ints = imap - nintrc;
1993                 reg = ints - naddrc;
1994         }
1995         if (p == NULL) {
1996 #ifdef DEBUG_IRQ
1997                 printk("hmmm, int tree for %s doesn't have ctrler\n",
1998                        np->full_name);
1999 #endif
2000                 return 0;
2001         }
2002         *irq = ints;
2003         *ictrler = p;
2004         return nintrc;
2005 }
2006
2007 static unsigned long __init
2008 finish_node_interrupts(struct device_node *np, unsigned long mem_start,
2009                        int measure_only)
2010 {
2011         unsigned int *ints;
2012         int intlen, intrcells, intrcount;
2013         int i, j, n;
2014         unsigned int *irq, virq;
2015         struct device_node *ic;
2016
2017         ints = (unsigned int *) get_property(np, "interrupts", &intlen);
2018         if (ints == NULL)
2019                 return mem_start;
2020         intrcells = prom_n_intr_cells(np);
2021         intlen /= intrcells * sizeof(unsigned int);
2022         np->intrs = (struct interrupt_info *) mem_start;
2023         mem_start += intlen * sizeof(struct interrupt_info);
2024
2025         if (measure_only)
2026                 return mem_start;
2027
2028         intrcount = 0;
2029         for (i = 0; i < intlen; ++i, ints += intrcells) {
2030                 n = map_interrupt(&irq, &ic, np, ints, intrcells);
2031                 if (n <= 0)
2032                         continue;
2033
2034                 /* don't map IRQ numbers under a cascaded 8259 controller */
2035                 if (ic && device_is_compatible(ic, "chrp,iic")) {
2036                         np->intrs[intrcount].line = irq[0];
2037                 } else {
2038                         virq = virt_irq_create_mapping(irq[0]);
2039                         if (virq == NO_IRQ) {
2040                                 printk(KERN_CRIT "Could not allocate interrupt"
2041                                        " number for %s\n", np->full_name);
2042                                 continue;
2043                         }
2044                         np->intrs[intrcount].line = irq_offset_up(virq);
2045                 }
2046
2047                 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
2048                 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
2049                         char *name = get_property(ic->parent, "name", NULL);
2050                         if (name && !strcmp(name, "u3"))
2051                                 np->intrs[intrcount].line += 128;
2052                 }
2053                 np->intrs[intrcount].sense = 1;
2054                 if (n > 1)
2055                         np->intrs[intrcount].sense = irq[1];
2056                 if (n > 2) {
2057                         printk("hmmm, got %d intr cells for %s:", n,
2058                                np->full_name);
2059                         for (j = 0; j < n; ++j)
2060                                 printk(" %d", irq[j]);
2061                         printk("\n");
2062                 }
2063                 ++intrcount;
2064         }
2065         np->n_intrs = intrcount;
2066
2067         return mem_start;
2068 }
2069
2070 static unsigned long __init
2071 interpret_pci_props(struct device_node *np, unsigned long mem_start,
2072                     int naddrc, int nsizec, int measure_only)
2073 {
2074         struct address_range *adr;
2075         struct pci_reg_property *pci_addrs;
2076         int i, l;
2077
2078         pci_addrs = (struct pci_reg_property *)
2079                 get_property(np, "assigned-addresses", &l);
2080         if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
2081                 i = 0;
2082                 adr = (struct address_range *) mem_start;
2083                 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
2084                         if (!measure_only) {
2085                                 adr[i].space = pci_addrs[i].addr.a_hi;
2086                                 adr[i].address = pci_addrs[i].addr.a_lo;
2087                                 adr[i].size = pci_addrs[i].size_lo;
2088                         }
2089                         ++i;
2090                 }
2091                 np->addrs = adr;
2092                 np->n_addrs = i;
2093                 mem_start += i * sizeof(struct address_range);
2094         }
2095         return mem_start;
2096 }
2097
2098 static unsigned long __init
2099 interpret_dbdma_props(struct device_node *np, unsigned long mem_start,
2100                       int naddrc, int nsizec, int measure_only)
2101 {
2102         struct reg_property32 *rp;
2103         struct address_range *adr;
2104         unsigned long base_address;
2105         int i, l;
2106         struct device_node *db;
2107
2108         base_address = 0;
2109         if (!measure_only) {
2110                 for (db = np->parent; db != NULL; db = db->parent) {
2111                         if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
2112                                 base_address = db->addrs[0].address;
2113                                 break;
2114                         }
2115                 }
2116         }
2117
2118         rp = (struct reg_property32 *) get_property(np, "reg", &l);
2119         if (rp != 0 && l >= sizeof(struct reg_property32)) {
2120                 i = 0;
2121                 adr = (struct address_range *) mem_start;
2122                 while ((l -= sizeof(struct reg_property32)) >= 0) {
2123                         if (!measure_only) {
2124                                 adr[i].space = 2;
2125                                 adr[i].address = rp[i].address + base_address;
2126                                 adr[i].size = rp[i].size;
2127                         }
2128                         ++i;
2129                 }
2130                 np->addrs = adr;
2131                 np->n_addrs = i;
2132                 mem_start += i * sizeof(struct address_range);
2133         }
2134
2135         return mem_start;
2136 }
2137
2138 static unsigned long __init
2139 interpret_macio_props(struct device_node *np, unsigned long mem_start,
2140                       int naddrc, int nsizec, int measure_only)
2141 {
2142         struct reg_property32 *rp;
2143         struct address_range *adr;
2144         unsigned long base_address;
2145         int i, l;
2146         struct device_node *db;
2147
2148         base_address = 0;
2149         if (!measure_only) {
2150                 for (db = np->parent; db != NULL; db = db->parent) {
2151                         if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
2152                                 base_address = db->addrs[0].address;
2153                                 break;
2154                         }
2155                 }
2156         }
2157
2158         rp = (struct reg_property32 *) get_property(np, "reg", &l);
2159         if (rp != 0 && l >= sizeof(struct reg_property32)) {
2160                 i = 0;
2161                 adr = (struct address_range *) mem_start;
2162                 while ((l -= sizeof(struct reg_property32)) >= 0) {
2163                         if (!measure_only) {
2164                                 adr[i].space = 2;
2165                                 adr[i].address = rp[i].address + base_address;
2166                                 adr[i].size = rp[i].size;
2167                         }
2168                         ++i;
2169                 }
2170                 np->addrs = adr;
2171                 np->n_addrs = i;
2172                 mem_start += i * sizeof(struct address_range);
2173         }
2174
2175         return mem_start;
2176 }
2177
2178 static unsigned long __init
2179 interpret_isa_props(struct device_node *np, unsigned long mem_start,
2180                     int naddrc, int nsizec, int measure_only)
2181 {
2182         struct isa_reg_property *rp;
2183         struct address_range *adr;
2184         int i, l;
2185
2186         rp = (struct isa_reg_property *) get_property(np, "reg", &l);
2187         if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
2188                 i = 0;
2189                 adr = (struct address_range *) mem_start;
2190                 while ((l -= sizeof(struct reg_property)) >= 0) {
2191                         if (!measure_only) {
2192                                 adr[i].space = rp[i].space;
2193                                 adr[i].address = rp[i].address;
2194                                 adr[i].size = rp[i].size;
2195                         }
2196                         ++i;
2197                 }
2198                 np->addrs = adr;
2199                 np->n_addrs = i;
2200                 mem_start += i * sizeof(struct address_range);
2201         }
2202
2203         return mem_start;
2204 }
2205
2206 static unsigned long __init
2207 interpret_root_props(struct device_node *np, unsigned long mem_start,
2208                      int naddrc, int nsizec, int measure_only)
2209 {
2210         struct address_range *adr;
2211         int i, l;
2212         unsigned int *rp;
2213         int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
2214
2215         rp = (unsigned int *) get_property(np, "reg", &l);
2216         if (rp != 0 && l >= rpsize) {
2217                 i = 0;
2218                 adr = (struct address_range *) mem_start;
2219                 while ((l -= rpsize) >= 0) {
2220                         if (!measure_only) {
2221                                 adr[i].space = 0;
2222                                 adr[i].address = rp[naddrc - 1];
2223                                 adr[i].size = rp[naddrc + nsizec - 1];
2224                         }
2225                         ++i;
2226                         rp += naddrc + nsizec;
2227                 }
2228                 np->addrs = adr;
2229                 np->n_addrs = i;
2230                 mem_start += i * sizeof(struct address_range);
2231         }
2232
2233         return mem_start;
2234 }
2235
2236 static unsigned long __init
2237 finish_node(struct device_node *np, unsigned long mem_start,
2238             interpret_func *ifunc, int naddrc, int nsizec, int measure_only)
2239 {
2240         struct device_node *child;
2241         int *ip;
2242
2243         np->name = get_property(np, "name", NULL);
2244         np->type = get_property(np, "device_type", NULL);
2245
2246         if (!np->name)
2247                 np->name = "<NULL>";
2248         if (!np->type)
2249                 np->type = "<NULL>";
2250
2251         /* get the device addresses and interrupts */
2252         if (ifunc != NULL)
2253                 mem_start = ifunc(np, mem_start, naddrc, nsizec, measure_only);
2254
2255         mem_start = finish_node_interrupts(np, mem_start, measure_only);
2256
2257         /* Look for #address-cells and #size-cells properties. */
2258         ip = (int *) get_property(np, "#address-cells", NULL);
2259         if (ip != NULL)
2260                 naddrc = *ip;
2261         ip = (int *) get_property(np, "#size-cells", NULL);
2262         if (ip != NULL)
2263                 nsizec = *ip;
2264
2265         /* the f50 sets the name to 'display' and 'compatible' to what we
2266          * expect for the name -- Cort
2267          */
2268         if (!strcmp(np->name, "display"))
2269                 np->name = get_property(np, "compatible", NULL);
2270
2271         if (!strcmp(np->name, "device-tree") || np->parent == NULL)
2272                 ifunc = interpret_root_props;
2273         else if (np->type == 0)
2274                 ifunc = NULL;
2275         else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
2276                 ifunc = interpret_pci_props;
2277         else if (!strcmp(np->type, "dbdma"))
2278                 ifunc = interpret_dbdma_props;
2279         else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
2280                 ifunc = interpret_macio_props;
2281         else if (!strcmp(np->type, "isa"))
2282                 ifunc = interpret_isa_props;
2283         else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
2284                 ifunc = interpret_root_props;
2285         else if (!((ifunc == interpret_dbdma_props
2286                     || ifunc == interpret_macio_props)
2287                    && (!strcmp(np->type, "escc")
2288                        || !strcmp(np->type, "media-bay"))))
2289                 ifunc = NULL;
2290
2291         for (child = np->child; child != NULL; child = child->sibling)
2292                 mem_start = finish_node(child, mem_start, ifunc,
2293                                         naddrc, nsizec, measure_only);
2294
2295         return mem_start;
2296 }
2297
2298 /**
2299  * finish_device_tree is called once things are running normally
2300  * (i.e. with text and data mapped to the address they were linked at).
2301  * It traverses the device tree and fills in the name, type,
2302  * {n_}addrs and {n_}intrs fields of each node.
2303  */
2304 void __init
2305 finish_device_tree(void)
2306 {
2307         unsigned long mem = klimit;
2308
2309         virt_irq_init();
2310
2311         dev_tree_size = finish_node(allnodes, 0, NULL, 0, 0, 1);
2312         mem = (long)abs_to_virt(lmb_alloc(dev_tree_size,
2313                                           __alignof__(struct device_node)));
2314         if (finish_node(allnodes, mem, NULL, 0, 0, 0) != mem + dev_tree_size)
2315                 BUG();
2316         rtas.dev = of_find_node_by_name(NULL, "rtas");
2317 }
2318
2319 int
2320 prom_n_addr_cells(struct device_node* np)
2321 {
2322         int* ip;
2323         do {
2324                 if (np->parent)
2325                         np = np->parent;
2326                 ip = (int *) get_property(np, "#address-cells", NULL);
2327                 if (ip != NULL)
2328                         return *ip;
2329         } while (np->parent);
2330         /* No #address-cells property for the root node, default to 1 */
2331         return 1;
2332 }
2333
2334 int
2335 prom_n_size_cells(struct device_node* np)
2336 {
2337         int* ip;
2338         do {
2339                 if (np->parent)
2340                         np = np->parent;
2341                 ip = (int *) get_property(np, "#size-cells", NULL);
2342                 if (ip != NULL)
2343                         return *ip;
2344         } while (np->parent);
2345         /* No #size-cells property for the root node, default to 1 */
2346         return 1;
2347 }
2348
2349 /**
2350  * Work out the sense (active-low level / active-high edge)
2351  * of each interrupt from the device tree.
2352  */
2353 void __init
2354 prom_get_irq_senses(unsigned char *senses, int off, int max)
2355 {
2356         struct device_node *np;
2357         int i, j;
2358
2359         /* default to level-triggered */
2360         memset(senses, 1, max - off);
2361
2362         for (np = allnodes; np != 0; np = np->allnext) {
2363                 for (j = 0; j < np->n_intrs; j++) {
2364                         i = np->intrs[j].line;
2365                         if (i >= off && i < max)
2366                                 senses[i-off] = np->intrs[j].sense;
2367                 }
2368         }
2369 }
2370
2371 /**
2372  * Construct and return a list of the device_nodes with a given name.
2373  */
2374 struct device_node *
2375 find_devices(const char *name)
2376 {
2377         struct device_node *head, **prevp, *np;
2378
2379         prevp = &head;
2380         for (np = allnodes; np != 0; np = np->allnext) {
2381                 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
2382                         *prevp = np;
2383                         prevp = &np->next;
2384                 }
2385         }
2386         *prevp = NULL;
2387         return head;
2388 }
2389
2390 /**
2391  * Construct and return a list of the device_nodes with a given type.
2392  */
2393 struct device_node *
2394 find_type_devices(const char *type)
2395 {
2396         struct device_node *head, **prevp, *np;
2397
2398         prevp = &head;
2399         for (np = allnodes; np != 0; np = np->allnext) {
2400                 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
2401                         *prevp = np;
2402                         prevp = &np->next;
2403                 }
2404         }
2405         *prevp = NULL;
2406         return head;
2407 }
2408
2409 /**
2410  * Returns all nodes linked together
2411  */
2412 struct device_node *
2413 find_all_nodes(void)
2414 {
2415         struct device_node *head, **prevp, *np;
2416
2417         prevp = &head;
2418         for (np = allnodes; np != 0; np = np->allnext) {
2419                 *prevp = np;
2420                 prevp = &np->next;
2421         }
2422         *prevp = NULL;
2423         return head;
2424 }
2425
2426 /** Checks if the given "compat" string matches one of the strings in
2427  * the device's "compatible" property
2428  */
2429 int
2430 device_is_compatible(struct device_node *device, const char *compat)
2431 {
2432         const char* cp;
2433         int cplen, l;
2434
2435         cp = (char *) get_property(device, "compatible", &cplen);
2436         if (cp == NULL)
2437                 return 0;
2438         while (cplen > 0) {
2439                 if (strncasecmp(cp, compat, strlen(compat)) == 0)
2440                         return 1;
2441                 l = strlen(cp) + 1;
2442                 cp += l;
2443                 cplen -= l;
2444         }
2445
2446         return 0;
2447 }
2448
2449
2450 /**
2451  * Indicates whether the root node has a given value in its
2452  * compatible property.
2453  */
2454 int
2455 machine_is_compatible(const char *compat)
2456 {
2457         struct device_node *root;
2458         int rc = 0;
2459
2460         root = of_find_node_by_path("/");
2461         if (root) {
2462                 rc = device_is_compatible(root, compat);
2463                 of_node_put(root);
2464         }
2465         return rc;
2466 }
2467
2468 /**
2469  * Construct and return a list of the device_nodes with a given type
2470  * and compatible property.
2471  */
2472 struct device_node *
2473 find_compatible_devices(const char *type, const char *compat)
2474 {
2475         struct device_node *head, **prevp, *np;
2476
2477         prevp = &head;
2478         for (np = allnodes; np != 0; np = np->allnext) {
2479                 if (type != NULL
2480                     && !(np->type != 0 && strcasecmp(np->type, type) == 0))
2481                         continue;
2482                 if (device_is_compatible(np, compat)) {
2483                         *prevp = np;
2484                         prevp = &np->next;
2485                 }
2486         }
2487         *prevp = NULL;
2488         return head;
2489 }
2490
2491 /**
2492  * Find the device_node with a given full_name.
2493  */
2494 struct device_node *
2495 find_path_device(const char *path)
2496 {
2497         struct device_node *np;
2498
2499         for (np = allnodes; np != 0; np = np->allnext)
2500                 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
2501                         return np;
2502         return NULL;
2503 }
2504
2505 /*******
2506  *
2507  * New implementation of the OF "find" APIs, return a refcounted
2508  * object, call of_node_put() when done.  The device tree and list
2509  * are protected by a rw_lock.
2510  *
2511  * Note that property management will need some locking as well,
2512  * this isn't dealt with yet.
2513  *
2514  *******/
2515
2516 /**
2517  *      of_find_node_by_name - Find a node by its "name" property
2518  *      @from:  The node to start searching from or NULL, the node
2519  *              you pass will not be searched, only the next one
2520  *              will; typically, you pass what the previous call
2521  *              returned. of_node_put() will be called on it
2522  *      @name:  The name string to match against
2523  *
2524  *      Returns a node pointer with refcount incremented, use
2525  *      of_node_put() on it when done.
2526  */
2527 struct device_node *of_find_node_by_name(struct device_node *from,
2528         const char *name)
2529 {
2530         struct device_node *np;
2531
2532         read_lock(&devtree_lock);
2533         np = from ? from->allnext : allnodes;
2534         for (; np != 0; np = np->allnext)
2535                 if (np->name != 0 && strcasecmp(np->name, name) == 0
2536                     && of_node_get(np))
2537                         break;
2538         if (from)
2539                 of_node_put(from);
2540         read_unlock(&devtree_lock);
2541         return np;
2542 }
2543 EXPORT_SYMBOL(of_find_node_by_name);
2544
2545 /**
2546  *      of_find_node_by_type - Find a node by its "device_type" property
2547  *      @from:  The node to start searching from or NULL, the node
2548  *              you pass will not be searched, only the next one
2549  *              will; typically, you pass what the previous call
2550  *              returned. of_node_put() will be called on it
2551  *      @name:  The type string to match against
2552  *
2553  *      Returns a node pointer with refcount incremented, use
2554  *      of_node_put() on it when done.
2555  */
2556 struct device_node *of_find_node_by_type(struct device_node *from,
2557         const char *type)
2558 {
2559         struct device_node *np;
2560
2561         read_lock(&devtree_lock);
2562         np = from ? from->allnext : allnodes;
2563         for (; np != 0; np = np->allnext)
2564                 if (np->type != 0 && strcasecmp(np->type, type) == 0
2565                     && of_node_get(np))
2566                         break;
2567         if (from)
2568                 of_node_put(from);
2569         read_unlock(&devtree_lock);
2570         return np;
2571 }
2572 EXPORT_SYMBOL(of_find_node_by_type);
2573
2574 /**
2575  *      of_find_compatible_node - Find a node based on type and one of the
2576  *                                tokens in its "compatible" property
2577  *      @from:          The node to start searching from or NULL, the node
2578  *                      you pass will not be searched, only the next one
2579  *                      will; typically, you pass what the previous call
2580  *                      returned. of_node_put() will be called on it
2581  *      @type:          The type string to match "device_type" or NULL to ignore
2582  *      @compatible:    The string to match to one of the tokens in the device
2583  *                      "compatible" list.
2584  *
2585  *      Returns a node pointer with refcount incremented, use
2586  *      of_node_put() on it when done.
2587  */
2588 struct device_node *of_find_compatible_node(struct device_node *from,
2589         const char *type, const char *compatible)
2590 {
2591         struct device_node *np;
2592
2593         read_lock(&devtree_lock);
2594         np = from ? from->allnext : allnodes;
2595         for (; np != 0; np = np->allnext) {
2596                 if (type != NULL
2597                     && !(np->type != 0 && strcasecmp(np->type, type) == 0))
2598                         continue;
2599                 if (device_is_compatible(np, compatible) && of_node_get(np))
2600                         break;
2601         }
2602         if (from)
2603                 of_node_put(from);
2604         read_unlock(&devtree_lock);
2605         return np;
2606 }
2607 EXPORT_SYMBOL(of_find_compatible_node);
2608
2609 /**
2610  *      of_find_node_by_path - Find a node matching a full OF path
2611  *      @path:  The full path to match
2612  *
2613  *      Returns a node pointer with refcount incremented, use
2614  *      of_node_put() on it when done.
2615  */
2616 struct device_node *of_find_node_by_path(const char *path)
2617 {
2618         struct device_node *np = allnodes;
2619
2620         read_lock(&devtree_lock);
2621         for (; np != 0; np = np->allnext)
2622                 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
2623                     && of_node_get(np))
2624                         break;
2625         read_unlock(&devtree_lock);
2626         return np;
2627 }
2628 EXPORT_SYMBOL(of_find_node_by_path);
2629
2630 /**
2631  *      of_find_all_nodes - Get next node in global list
2632  *      @prev:  Previous node or NULL to start iteration
2633  *              of_node_put() will be called on it
2634  *
2635  *      Returns a node pointer with refcount incremented, use
2636  *      of_node_put() on it when done.
2637  */
2638 struct device_node *of_find_all_nodes(struct device_node *prev)
2639 {
2640         struct device_node *np;
2641
2642         read_lock(&devtree_lock);
2643         np = prev ? prev->allnext : allnodes;
2644         for (; np != 0; np = np->allnext)
2645                 if (of_node_get(np))
2646                         break;
2647         if (prev)
2648                 of_node_put(prev);
2649         read_unlock(&devtree_lock);
2650         return np;
2651 }
2652 EXPORT_SYMBOL(of_find_all_nodes);
2653
2654 /**
2655  *      of_get_parent - Get a node's parent if any
2656  *      @node:  Node to get parent
2657  *
2658  *      Returns a node pointer with refcount incremented, use
2659  *      of_node_put() on it when done.
2660  */
2661 struct device_node *of_get_parent(const struct device_node *node)
2662 {
2663         struct device_node *np;
2664
2665         if (!node)
2666                 return NULL;
2667
2668         read_lock(&devtree_lock);
2669         np = of_node_get(node->parent);
2670         read_unlock(&devtree_lock);
2671         return np;
2672 }
2673 EXPORT_SYMBOL(of_get_parent);
2674
2675 /**
2676  *      of_get_next_child - Iterate a node childs
2677  *      @node:  parent node
2678  *      @prev:  previous child of the parent node, or NULL to get first
2679  *
2680  *      Returns a node pointer with refcount incremented, use
2681  *      of_node_put() on it when done.
2682  */
2683 struct device_node *of_get_next_child(const struct device_node *node,
2684         struct device_node *prev)
2685 {
2686         struct device_node *next;
2687
2688         read_lock(&devtree_lock);
2689         next = prev ? prev->sibling : node->child;
2690         for (; next != 0; next = next->sibling)
2691                 if (of_node_get(next))
2692                         break;
2693         if (prev)
2694                 of_node_put(prev);
2695         read_unlock(&devtree_lock);
2696         return next;
2697 }
2698 EXPORT_SYMBOL(of_get_next_child);
2699
2700 /**
2701  *      of_node_get - Increment refcount of a node
2702  *      @node:  Node to inc refcount, NULL is supported to
2703  *              simplify writing of callers
2704  *
2705  *      Returns the node itself or NULL if gone.
2706  */
2707 struct device_node *of_node_get(struct device_node *node)
2708 {
2709         if (node && !OF_IS_STALE(node)) {
2710                 atomic_inc(&node->_users);
2711                 return node;
2712         }
2713         return NULL;
2714 }
2715 EXPORT_SYMBOL(of_node_get);
2716
2717 /**
2718  *      of_node_cleanup - release a dynamically allocated node
2719  *      @arg:  Node to be released
2720  */
2721 static void of_node_cleanup(struct device_node *node)
2722 {
2723         struct property *prop = node->properties;
2724
2725         if (!OF_IS_DYNAMIC(node))
2726                 return;
2727         while (prop) {
2728                 struct property *next = prop->next;
2729                 kfree(prop->name);
2730                 kfree(prop->value);
2731                 kfree(prop);
2732                 prop = next;
2733         }
2734         kfree(node->intrs);
2735         kfree(node->addrs);
2736         kfree(node->full_name);
2737         kfree(node);
2738 }
2739
2740 /**
2741  *      of_node_put - Decrement refcount of a node
2742  *      @node:  Node to dec refcount, NULL is supported to
2743  *              simplify writing of callers
2744  *
2745  */
2746 void of_node_put(struct device_node *node)
2747 {
2748         if (!node)
2749                 return;
2750
2751         WARN_ON(0 == atomic_read(&node->_users));
2752
2753         if (OF_IS_STALE(node)) {
2754                 if (atomic_dec_and_test(&node->_users)) {
2755                         of_node_cleanup(node);
2756                         return;
2757                 }
2758         }
2759         else
2760                 atomic_dec(&node->_users);
2761 }
2762 EXPORT_SYMBOL(of_node_put);
2763
2764 /**
2765  *      derive_parent - basically like dirname(1)
2766  *      @path:  the full_name of a node to be added to the tree
2767  *
2768  *      Returns the node which should be the parent of the node
2769  *      described by path.  E.g., for path = "/foo/bar", returns
2770  *      the node with full_name = "/foo".
2771  */
2772 static struct device_node *derive_parent(const char *path)
2773 {
2774         struct device_node *parent = NULL;
2775         char *parent_path = "/";
2776         size_t parent_path_len = strrchr(path, '/') - path + 1;
2777
2778         /* reject if path is "/" */
2779         if (!strcmp(path, "/"))
2780                 return NULL;
2781
2782         if (strrchr(path, '/') != path) {
2783                 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
2784                 if (!parent_path)
2785                         return NULL;
2786                 strlcpy(parent_path, path, parent_path_len);
2787         }
2788         parent = of_find_node_by_path(parent_path);
2789         if (strcmp(parent_path, "/"))
2790                 kfree(parent_path);
2791         return parent;
2792 }
2793
2794 /*
2795  * Routines for "runtime" addition and removal of device tree nodes.
2796  */
2797 #ifdef CONFIG_PROC_DEVICETREE
2798 /*
2799  * Add a node to /proc/device-tree.
2800  */
2801 static void add_node_proc_entries(struct device_node *np)
2802 {
2803         struct proc_dir_entry *ent;
2804
2805         ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
2806         if (ent)
2807                 proc_device_tree_add_node(np, ent);
2808 }
2809
2810 static void remove_node_proc_entries(struct device_node *np)
2811 {
2812         struct property *pp = np->properties;
2813         struct device_node *parent = np->parent;
2814
2815         while (pp) {
2816                 remove_proc_entry(pp->name, np->pde);
2817                 pp = pp->next;
2818         }
2819
2820         /* Assuming that symlinks have the same parent directory as
2821          * np->pde.
2822          */
2823         if (np->name_link)
2824                 remove_proc_entry(np->name_link->name, parent->pde);
2825         if (np->addr_link)
2826                 remove_proc_entry(np->addr_link->name, parent->pde);
2827         if (np->pde)
2828                 remove_proc_entry(np->pde->name, parent->pde);
2829 }
2830 #else /* !CONFIG_PROC_DEVICETREE */
2831 static void add_node_proc_entries(struct device_node *np)
2832 {
2833         return;
2834 }
2835
2836 static void remove_node_proc_entries(struct device_node *np)
2837 {
2838         return;
2839 }
2840 #endif /* CONFIG_PROC_DEVICETREE */
2841
2842 /*
2843  * Fix up n_intrs and intrs fields in a new device node
2844  *
2845  */
2846 static int of_finish_dynamic_node_interrupts(struct device_node *node)
2847 {
2848         int intrcells, intlen, i;
2849         unsigned *irq, *ints, virq;
2850         struct device_node *ic;
2851
2852         ints = (unsigned int *)get_property(node, "interrupts", &intlen);
2853         intrcells = prom_n_intr_cells(node);
2854         intlen /= intrcells * sizeof(unsigned int);
2855         node->n_intrs = intlen;
2856         node->intrs = kmalloc(sizeof(struct interrupt_info) * intlen,
2857                               GFP_KERNEL);
2858         if (!node->intrs)
2859                 return -ENOMEM;
2860
2861         for (i = 0; i < intlen; ++i) {
2862                 int n, j;
2863                 node->intrs[i].line = 0;
2864                 node->intrs[i].sense = 1;
2865                 n = map_interrupt(&irq, &ic, node, ints, intrcells);
2866                 if (n <= 0)
2867                         continue;
2868                 virq = virt_irq_create_mapping(irq[0]);
2869                 if (virq == NO_IRQ) {
2870                         printk(KERN_CRIT "Could not allocate interrupt "
2871                                "number for %s\n", node->full_name);
2872                         return -ENOMEM;
2873                 }
2874                 node->intrs[i].line = irq_offset_up(virq);
2875                 if (n > 1)
2876                         node->intrs[i].sense = irq[1];
2877                 if (n > 2) {
2878                         printk(KERN_DEBUG "hmmm, got %d intr cells for %s:", n,
2879                                node->full_name);
2880                         for (j = 0; j < n; ++j)
2881                                 printk(" %d", irq[j]);
2882                         printk("\n");
2883                 }
2884                 ints += intrcells;
2885         }
2886         return 0;
2887 }
2888
2889 /*
2890  * Fix up the uninitialized fields in a new device node:
2891  * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
2892  *
2893  * A lot of boot-time code is duplicated here, because functions such
2894  * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
2895  * slab allocator.
2896  *
2897  * This should probably be split up into smaller chunks.
2898  */
2899
2900 static int of_finish_dynamic_node(struct device_node *node)
2901 {
2902         struct device_node *parent = of_get_parent(node);
2903         u32 *regs;
2904         int err = 0;
2905         phandle *ibm_phandle;
2906
2907         node->name = get_property(node, "name", NULL);
2908         node->type = get_property(node, "device_type", NULL);
2909
2910         if (!parent) {
2911                 err = -ENODEV;
2912                 goto out;
2913         }
2914
2915         /* We don't support that function on PowerMac, at least
2916          * not yet
2917          */
2918         if (systemcfg->platform == PLATFORM_POWERMAC)
2919                 return -ENODEV;
2920
2921         /* fix up new node's linux_phandle field */
2922         if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
2923                 node->linux_phandle = *ibm_phandle;
2924
2925         /* do the work of interpret_pci_props */
2926         if (parent->type && !strcmp(parent->type, "pci")) {
2927                 struct address_range *adr;
2928                 struct pci_reg_property *pci_addrs;
2929                 int i, l;
2930
2931                 pci_addrs = (struct pci_reg_property *)
2932                         get_property(node, "assigned-addresses", &l);
2933                 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
2934                         i = 0;
2935                         adr = kmalloc(sizeof(struct address_range) * 
2936                                       (l / sizeof(struct pci_reg_property)),
2937                                       GFP_KERNEL);
2938                         if (!adr) {
2939                                 err = -ENOMEM;
2940                                 goto out;
2941                         }
2942                         while ((l -= sizeof(struct pci_reg_property)) >= 0) {
2943                                 adr[i].space = pci_addrs[i].addr.a_hi;
2944                                 adr[i].address = pci_addrs[i].addr.a_lo;
2945                                 adr[i].size = pci_addrs[i].size_lo;
2946                                 ++i;
2947                         }
2948                         node->addrs = adr;
2949                         node->n_addrs = i;
2950                 }
2951         }
2952
2953         /* now do the work of finish_node_interrupts */
2954         if (get_property(node, "interrupts", NULL)) {
2955                 err = of_finish_dynamic_node_interrupts(node);
2956                 if (err) goto out;
2957         }
2958
2959         /* now do the rough equivalent of update_dn_pci_info, this
2960          * probably is not correct for phb's, but should work for
2961          * IOAs and slots.
2962          */
2963
2964         node->phb = parent->phb;
2965
2966         regs = (u32 *)get_property(node, "reg", NULL);
2967         if (regs) {
2968                 node->busno = (regs[0] >> 16) & 0xff;
2969                 node->devfn = (regs[0] >> 8) & 0xff;
2970         }
2971
2972         /* fixing up iommu_table */
2973
2974         if (strcmp(node->name, "pci") == 0 &&
2975             get_property(node, "ibm,dma-window", NULL)) {
2976                 node->bussubno = node->busno;
2977                 iommu_devnode_init(node);
2978         } else
2979                 node->iommu_table = parent->iommu_table;
2980
2981 out:
2982         of_node_put(parent);
2983         return err;
2984 }
2985
2986 /*
2987  * Given a path and a property list, construct an OF device node, add
2988  * it to the device tree and global list, and place it in
2989  * /proc/device-tree.  This function may sleep.
2990  */
2991 int of_add_node(const char *path, struct property *proplist)
2992 {
2993         struct device_node *np;
2994         int err = 0;
2995
2996         np = kmalloc(sizeof(struct device_node), GFP_KERNEL);
2997         if (!np)
2998                 return -ENOMEM;
2999
3000         memset(np, 0, sizeof(*np));
3001
3002         np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
3003         if (!np->full_name) {
3004                 kfree(np);
3005                 return -ENOMEM;
3006         }
3007         strcpy(np->full_name, path);
3008
3009         np->properties = proplist;
3010         OF_MARK_DYNAMIC(np);
3011         of_node_get(np);
3012         np->parent = derive_parent(path);
3013         if (!np->parent) {
3014                 kfree(np);
3015                 return -EINVAL; /* could also be ENOMEM, though */
3016         }
3017
3018         if (0 != (err = of_finish_dynamic_node(np))) {
3019                 kfree(np);
3020                 return err;
3021         }
3022
3023         write_lock(&devtree_lock);
3024         np->sibling = np->parent->child;
3025         np->allnext = allnodes;
3026         np->parent->child = np;
3027         allnodes = np;
3028         write_unlock(&devtree_lock);
3029
3030         add_node_proc_entries(np);
3031
3032         of_node_put(np->parent);
3033         of_node_put(np);
3034         return 0;
3035 }
3036
3037 /*
3038  * Remove an OF device node from the system.
3039  * Caller should have already "gotten" np.
3040  */
3041 int of_remove_node(struct device_node *np)
3042 {
3043         struct device_node *parent, *child;
3044
3045         parent = of_get_parent(np);
3046         if (!parent)
3047                 return -EINVAL;
3048
3049         if ((child = of_get_next_child(np, NULL))) {
3050                 of_node_put(child);
3051                 return -EBUSY;
3052         }
3053
3054         write_lock(&devtree_lock);
3055         OF_MARK_STALE(np);
3056         remove_node_proc_entries(np);
3057         if (allnodes == np)
3058                 allnodes = np->allnext;
3059         else {
3060                 struct device_node *prev;
3061                 for (prev = allnodes;
3062                      prev->allnext != np;
3063                      prev = prev->allnext)
3064                         ;
3065                 prev->allnext = np->allnext;
3066         }
3067
3068         if (parent->child == np)
3069                 parent->child = np->sibling;
3070         else {
3071                 struct device_node *prevsib;
3072                 for (prevsib = np->parent->child;
3073                      prevsib->sibling != np;
3074                      prevsib = prevsib->sibling)
3075                         ;
3076                 prevsib->sibling = np->sibling;
3077         }
3078         write_unlock(&devtree_lock);
3079         of_node_put(parent);
3080         return 0;
3081 }
3082
3083 /*
3084  * Find a property with a given name for a given node
3085  * and return the value.
3086  */
3087 unsigned char *
3088 get_property(struct device_node *np, const char *name, int *lenp)
3089 {
3090         struct property *pp;
3091
3092         for (pp = np->properties; pp != 0; pp = pp->next)
3093                 if (strcmp(pp->name, name) == 0) {
3094                         if (lenp != 0)
3095                                 *lenp = pp->length;
3096                         return pp->value;
3097                 }
3098         return NULL;
3099 }
3100
3101 /*
3102  * Add a property to a node
3103  */
3104 void
3105 prom_add_property(struct device_node* np, struct property* prop)
3106 {
3107         struct property **next = &np->properties;
3108
3109         prop->next = NULL;      
3110         while (*next)
3111                 next = &(*next)->next;
3112         *next = prop;
3113 }
3114
3115 #if 0
3116 void
3117 print_properties(struct device_node *np)
3118 {
3119         struct property *pp;
3120         char *cp;
3121         int i, n;
3122
3123         for (pp = np->properties; pp != 0; pp = pp->next) {
3124                 printk(KERN_INFO "%s", pp->name);
3125                 for (i = strlen(pp->name); i < 16; ++i)
3126                         printk(" ");
3127                 cp = (char *) pp->value;
3128                 for (i = pp->length; i > 0; --i, ++cp)
3129                         if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
3130                             || (i == 1 && *cp != 0))
3131                                 break;
3132                 if (i == 0 && pp->length > 1) {
3133                         /* looks like a string */
3134                         printk(" %s\n", (char *) pp->value);
3135                 } else {
3136                         /* dump it in hex */
3137                         n = pp->length;
3138                         if (n > 64)
3139                                 n = 64;
3140                         if (pp->length % 4 == 0) {
3141                                 unsigned int *p = (unsigned int *) pp->value;
3142
3143                                 n /= 4;
3144                                 for (i = 0; i < n; ++i) {
3145                                         if (i != 0 && (i % 4) == 0)
3146                                                 printk("\n                ");
3147                                         printk(" %08x", *p++);
3148                                 }
3149                         } else {
3150                                 unsigned char *bp = pp->value;
3151
3152                                 for (i = 0; i < n; ++i) {
3153                                         if (i != 0 && (i % 16) == 0)
3154                                                 printk("\n                ");
3155                                         printk(" %02x", *bp++);
3156                                 }
3157                         }
3158                         printk("\n");
3159                         if (pp->length > 64)
3160                                 printk("                 ... (length = %d)\n",
3161                                        pp->length);
3162                 }
3163         }
3164 }
3165 #endif