Merge to Fedora kernel-2.6.7-1.492
[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 = { 0, };
137 phandle prom_display_nodes[FB_MAX] __initdata;
138 unsigned int prom_num_displays = 0;
139 char *of_stdout_device = 0;
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 = 0;
152 char *bootdevice = 0;
153
154 int boot_cpuid = 0;
155 #define MAX_CPU_THREADS 2
156
157 struct device_node *allnodes = 0;
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         unsigned long offset = reloc_offset();
1416
1417         *mem_start = ALIGN(*mem_start, align);
1418         if (*mem_start + needed > *mem_end) {
1419 #ifdef CONFIG_BLK_DEV_INITRD
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, 0, &mem_start, &mem_end, &allnextp);
1586         *allnextp = 0;
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 static int __devinit
1885 prom_n_intr_cells(struct device_node *np)
1886 {
1887         struct device_node *p;
1888         unsigned int *icp;
1889
1890         for (p = np; (p = intr_parent(p)) != NULL; ) {
1891                 icp = (unsigned int *)
1892                         get_property(p, "#interrupt-cells", NULL);
1893                 if (icp != NULL)
1894                         return *icp;
1895                 if (get_property(p, "interrupt-controller", NULL) != NULL
1896                     || get_property(p, "interrupt-map", NULL) != NULL) {
1897                         printk("oops, node %s doesn't have #interrupt-cells\n",
1898                                p->full_name);
1899                 return 1;
1900                 }
1901         }
1902 #ifdef DEBUG_IRQ
1903         printk("prom_n_intr_cells failed for %s\n", np->full_name);
1904 #endif
1905         return 1;
1906 }
1907
1908 /*
1909  * Map an interrupt from a device up to the platform interrupt
1910  * descriptor.
1911  */
1912 static int __devinit
1913 map_interrupt(unsigned int **irq, struct device_node **ictrler,
1914               struct device_node *np, unsigned int *ints, int nintrc)
1915 {
1916         struct device_node *p, *ipar;
1917         unsigned int *imap, *imask, *ip;
1918         int i, imaplen, match;
1919         int newintrc, newaddrc;
1920         unsigned int *reg;
1921         int naddrc;
1922
1923         reg = (unsigned int *) get_property(np, "reg", NULL);
1924         naddrc = prom_n_addr_cells(np);
1925         p = intr_parent(np);
1926         while (p != NULL) {
1927                 if (get_property(p, "interrupt-controller", NULL) != NULL)
1928                         /* this node is an interrupt controller, stop here */
1929                         break;
1930                 imap = (unsigned int *)
1931                         get_property(p, "interrupt-map", &imaplen);
1932                 if (imap == NULL) {
1933                         p = intr_parent(p);
1934                         continue;
1935                 }
1936                 imask = (unsigned int *)
1937                         get_property(p, "interrupt-map-mask", NULL);
1938                 if (imask == NULL) {
1939                         printk("oops, %s has interrupt-map but no mask\n",
1940                                p->full_name);
1941                         return 0;
1942                 }
1943                 imaplen /= sizeof(unsigned int);
1944                 match = 0;
1945                 ipar = NULL;
1946                 while (imaplen > 0 && !match) {
1947                         /* check the child-interrupt field */
1948                         match = 1;
1949                         for (i = 0; i < naddrc && match; ++i)
1950                                 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
1951                         for (; i < naddrc + nintrc && match; ++i)
1952                                 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
1953                         imap += naddrc + nintrc;
1954                         imaplen -= naddrc + nintrc;
1955                         /* grab the interrupt parent */
1956                         ipar = find_phandle((phandle) *imap++);
1957                         --imaplen;
1958                         if (ipar == NULL) {
1959                                 printk("oops, no int parent %x in map of %s\n",
1960                                        imap[-1], p->full_name);
1961                                 return 0;
1962                         }
1963                         /* find the parent's # addr and intr cells */
1964                         ip = (unsigned int *)
1965                                 get_property(ipar, "#interrupt-cells", NULL);
1966                         if (ip == NULL) {
1967                                 printk("oops, no #interrupt-cells on %s\n",
1968                                        ipar->full_name);
1969                                 return 0;
1970                         }
1971                         newintrc = *ip;
1972                         ip = (unsigned int *)
1973                                 get_property(ipar, "#address-cells", NULL);
1974                         newaddrc = (ip == NULL)? 0: *ip;
1975                         imap += newaddrc + newintrc;
1976                         imaplen -= newaddrc + newintrc;
1977                 }
1978                 if (imaplen < 0) {
1979                         printk("oops, error decoding int-map on %s, len=%d\n",
1980                                p->full_name, imaplen);
1981                         return 0;
1982                 }
1983                 if (!match) {
1984 #ifdef DEBUG_IRQ
1985                         printk("oops, no match in %s int-map for %s\n",
1986                                p->full_name, np->full_name);
1987 #endif
1988                         return 0;
1989                 }
1990                 p = ipar;
1991                 naddrc = newaddrc;
1992                 nintrc = newintrc;
1993                 ints = imap - nintrc;
1994                 reg = ints - naddrc;
1995         }
1996         if (p == NULL) {
1997 #ifdef DEBUG_IRQ
1998                 printk("hmmm, int tree for %s doesn't have ctrler\n",
1999                        np->full_name);
2000 #endif
2001                 return 0;
2002         }
2003         *irq = ints;
2004         *ictrler = p;
2005         return nintrc;
2006 }
2007
2008 static unsigned long __init
2009 finish_node_interrupts(struct device_node *np, unsigned long mem_start,
2010                        int measure_only)
2011 {
2012         unsigned int *ints;
2013         int intlen, intrcells, intrcount;
2014         int i, j, n;
2015         unsigned int *irq, virq;
2016         struct device_node *ic;
2017
2018         ints = (unsigned int *) get_property(np, "interrupts", &intlen);
2019         if (ints == NULL)
2020                 return mem_start;
2021         intrcells = prom_n_intr_cells(np);
2022         intlen /= intrcells * sizeof(unsigned int);
2023         np->intrs = (struct interrupt_info *) mem_start;
2024         mem_start += intlen * sizeof(struct interrupt_info);
2025
2026         if (measure_only)
2027                 return mem_start;
2028
2029         intrcount = 0;
2030         for (i = 0; i < intlen; ++i, ints += intrcells) {
2031                 n = map_interrupt(&irq, &ic, np, ints, intrcells);
2032                 if (n <= 0)
2033                         continue;
2034
2035                 /* don't map IRQ numbers under a cascaded 8259 controller */
2036                 if (ic && device_is_compatible(ic, "chrp,iic")) {
2037                         np->intrs[intrcount].line = irq[0];
2038                 } else {
2039                         virq = virt_irq_create_mapping(irq[0]);
2040                         if (virq == NO_IRQ) {
2041                                 printk(KERN_CRIT "Could not allocate interrupt"
2042                                        " number for %s\n", np->full_name);
2043                                 continue;
2044                         }
2045                         np->intrs[intrcount].line = irq_offset_up(virq);
2046                 }
2047
2048                 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
2049                 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
2050                         char *name = get_property(ic->parent, "name", NULL);
2051                         if (name && !strcmp(name, "u3"))
2052                                 np->intrs[intrcount].line += 128;
2053                 }
2054                 np->intrs[intrcount].sense = 1;
2055                 if (n > 1)
2056                         np->intrs[intrcount].sense = irq[1];
2057                 if (n > 2) {
2058                         printk("hmmm, got %d intr cells for %s:", n,
2059                                np->full_name);
2060                         for (j = 0; j < n; ++j)
2061                                 printk(" %d", irq[j]);
2062                         printk("\n");
2063                 }
2064                 ++intrcount;
2065         }
2066         np->n_intrs = intrcount;
2067
2068         return mem_start;
2069 }
2070
2071 static unsigned long __init
2072 interpret_pci_props(struct device_node *np, unsigned long mem_start,
2073                     int naddrc, int nsizec, int measure_only)
2074 {
2075         struct address_range *adr;
2076         struct pci_reg_property *pci_addrs;
2077         int i, l;
2078
2079         pci_addrs = (struct pci_reg_property *)
2080                 get_property(np, "assigned-addresses", &l);
2081         if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
2082                 i = 0;
2083                 adr = (struct address_range *) mem_start;
2084                 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
2085                         if (!measure_only) {
2086                                 adr[i].space = pci_addrs[i].addr.a_hi;
2087                                 adr[i].address = pci_addrs[i].addr.a_lo;
2088                                 adr[i].size = pci_addrs[i].size_lo;
2089                         }
2090                         ++i;
2091                 }
2092                 np->addrs = adr;
2093                 np->n_addrs = i;
2094                 mem_start += i * sizeof(struct address_range);
2095         }
2096         return mem_start;
2097 }
2098
2099 static unsigned long __init
2100 interpret_dbdma_props(struct device_node *np, unsigned long mem_start,
2101                       int naddrc, int nsizec, int measure_only)
2102 {
2103         struct reg_property32 *rp;
2104         struct address_range *adr;
2105         unsigned long base_address;
2106         int i, l;
2107         struct device_node *db;
2108
2109         base_address = 0;
2110         if (!measure_only) {
2111                 for (db = np->parent; db != NULL; db = db->parent) {
2112                         if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
2113                                 base_address = db->addrs[0].address;
2114                                 break;
2115                         }
2116                 }
2117         }
2118
2119         rp = (struct reg_property32 *) get_property(np, "reg", &l);
2120         if (rp != 0 && l >= sizeof(struct reg_property32)) {
2121                 i = 0;
2122                 adr = (struct address_range *) mem_start;
2123                 while ((l -= sizeof(struct reg_property32)) >= 0) {
2124                         if (!measure_only) {
2125                                 adr[i].space = 2;
2126                                 adr[i].address = rp[i].address + base_address;
2127                                 adr[i].size = rp[i].size;
2128                         }
2129                         ++i;
2130                 }
2131                 np->addrs = adr;
2132                 np->n_addrs = i;
2133                 mem_start += i * sizeof(struct address_range);
2134         }
2135
2136         return mem_start;
2137 }
2138
2139 static unsigned long __init
2140 interpret_macio_props(struct device_node *np, unsigned long mem_start,
2141                       int naddrc, int nsizec, int measure_only)
2142 {
2143         struct reg_property32 *rp;
2144         struct address_range *adr;
2145         unsigned long base_address;
2146         int i, l;
2147         struct device_node *db;
2148
2149         base_address = 0;
2150         if (!measure_only) {
2151                 for (db = np->parent; db != NULL; db = db->parent) {
2152                         if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
2153                                 base_address = db->addrs[0].address;
2154                                 break;
2155                         }
2156                 }
2157         }
2158
2159         rp = (struct reg_property32 *) get_property(np, "reg", &l);
2160         if (rp != 0 && l >= sizeof(struct reg_property32)) {
2161                 i = 0;
2162                 adr = (struct address_range *) mem_start;
2163                 while ((l -= sizeof(struct reg_property32)) >= 0) {
2164                         if (!measure_only) {
2165                                 adr[i].space = 2;
2166                                 adr[i].address = rp[i].address + base_address;
2167                                 adr[i].size = rp[i].size;
2168                         }
2169                         ++i;
2170                 }
2171                 np->addrs = adr;
2172                 np->n_addrs = i;
2173                 mem_start += i * sizeof(struct address_range);
2174         }
2175
2176         return mem_start;
2177 }
2178
2179 static unsigned long __init
2180 interpret_isa_props(struct device_node *np, unsigned long mem_start,
2181                     int naddrc, int nsizec, int measure_only)
2182 {
2183         struct isa_reg_property *rp;
2184         struct address_range *adr;
2185         int i, l;
2186
2187         rp = (struct isa_reg_property *) get_property(np, "reg", &l);
2188         if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
2189                 i = 0;
2190                 adr = (struct address_range *) mem_start;
2191                 while ((l -= sizeof(struct reg_property)) >= 0) {
2192                         if (!measure_only) {
2193                                 adr[i].space = rp[i].space;
2194                                 adr[i].address = rp[i].address;
2195                                 adr[i].size = rp[i].size;
2196                         }
2197                         ++i;
2198                 }
2199                 np->addrs = adr;
2200                 np->n_addrs = i;
2201                 mem_start += i * sizeof(struct address_range);
2202         }
2203
2204         return mem_start;
2205 }
2206
2207 static unsigned long __init
2208 interpret_root_props(struct device_node *np, unsigned long mem_start,
2209                      int naddrc, int nsizec, int measure_only)
2210 {
2211         struct address_range *adr;
2212         int i, l;
2213         unsigned int *rp;
2214         int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
2215
2216         rp = (unsigned int *) get_property(np, "reg", &l);
2217         if (rp != 0 && l >= rpsize) {
2218                 i = 0;
2219                 adr = (struct address_range *) mem_start;
2220                 while ((l -= rpsize) >= 0) {
2221                         if (!measure_only) {
2222                                 adr[i].space = 0;
2223                                 adr[i].address = rp[naddrc - 1];
2224                                 adr[i].size = rp[naddrc + nsizec - 1];
2225                         }
2226                         ++i;
2227                         rp += naddrc + nsizec;
2228                 }
2229                 np->addrs = adr;
2230                 np->n_addrs = i;
2231                 mem_start += i * sizeof(struct address_range);
2232         }
2233
2234         return mem_start;
2235 }
2236
2237 static unsigned long __init
2238 finish_node(struct device_node *np, unsigned long mem_start,
2239             interpret_func *ifunc, int naddrc, int nsizec, int measure_only)
2240 {
2241         struct device_node *child;
2242         int *ip;
2243
2244         np->name = get_property(np, "name", 0);
2245         np->type = get_property(np, "device_type", 0);
2246
2247         if (!np->name)
2248                 np->name = "<NULL>";
2249         if (!np->type)
2250                 np->type = "<NULL>";
2251
2252         /* get the device addresses and interrupts */
2253         if (ifunc != NULL)
2254                 mem_start = ifunc(np, mem_start, naddrc, nsizec, measure_only);
2255
2256         mem_start = finish_node_interrupts(np, mem_start, measure_only);
2257
2258         /* Look for #address-cells and #size-cells properties. */
2259         ip = (int *) get_property(np, "#address-cells", 0);
2260         if (ip != NULL)
2261                 naddrc = *ip;
2262         ip = (int *) get_property(np, "#size-cells", 0);
2263         if (ip != NULL)
2264                 nsizec = *ip;
2265
2266         /* the f50 sets the name to 'display' and 'compatible' to what we
2267          * expect for the name -- Cort
2268          */
2269         if (!strcmp(np->name, "display"))
2270                 np->name = get_property(np, "compatible", 0);
2271
2272         if (!strcmp(np->name, "device-tree") || np->parent == NULL)
2273                 ifunc = interpret_root_props;
2274         else if (np->type == 0)
2275                 ifunc = NULL;
2276         else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
2277                 ifunc = interpret_pci_props;
2278         else if (!strcmp(np->type, "dbdma"))
2279                 ifunc = interpret_dbdma_props;
2280         else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
2281                 ifunc = interpret_macio_props;
2282         else if (!strcmp(np->type, "isa"))
2283                 ifunc = interpret_isa_props;
2284         else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
2285                 ifunc = interpret_root_props;
2286         else if (!((ifunc == interpret_dbdma_props
2287                     || ifunc == interpret_macio_props)
2288                    && (!strcmp(np->type, "escc")
2289                        || !strcmp(np->type, "media-bay"))))
2290                 ifunc = NULL;
2291
2292         for (child = np->child; child != NULL; child = child->sibling)
2293                 mem_start = finish_node(child, mem_start, ifunc,
2294                                         naddrc, nsizec, measure_only);
2295
2296         return mem_start;
2297 }
2298
2299 /*
2300  * finish_device_tree is called once things are running normally
2301  * (i.e. with text and data mapped to the address they were linked at).
2302  * It traverses the device tree and fills in the name, type,
2303  * {n_}addrs and {n_}intrs fields of each node.
2304  */
2305 void __init
2306 finish_device_tree(void)
2307 {
2308         unsigned long mem = klimit;
2309
2310         virt_irq_init();
2311
2312         dev_tree_size = finish_node(allnodes, 0, NULL, 0, 0, 1);
2313         mem = (long)abs_to_virt(lmb_alloc(dev_tree_size,
2314                                           __alignof__(struct device_node)));
2315         if (finish_node(allnodes, mem, NULL, 0, 0, 0) != mem + dev_tree_size)
2316                 BUG();
2317         rtas.dev = of_find_node_by_name(NULL, "rtas");
2318 }
2319
2320 int
2321 prom_n_addr_cells(struct device_node* np)
2322 {
2323         int* ip;
2324         do {
2325                 if (np->parent)
2326                         np = np->parent;
2327                 ip = (int *) get_property(np, "#address-cells", 0);
2328                 if (ip != NULL)
2329                         return *ip;
2330         } while (np->parent);
2331         /* No #address-cells property for the root node, default to 1 */
2332         return 1;
2333 }
2334
2335 int
2336 prom_n_size_cells(struct device_node* np)
2337 {
2338         int* ip;
2339         do {
2340                 if (np->parent)
2341                         np = np->parent;
2342                 ip = (int *) get_property(np, "#size-cells", 0);
2343                 if (ip != NULL)
2344                         return *ip;
2345         } while (np->parent);
2346         /* No #size-cells property for the root node, default to 1 */
2347         return 1;
2348 }
2349
2350 /*
2351  * Work out the sense (active-low level / active-high edge)
2352  * of each interrupt from the device tree.
2353  */
2354 void __init
2355 prom_get_irq_senses(unsigned char *senses, int off, int max)
2356 {
2357         struct device_node *np;
2358         int i, j;
2359
2360         /* default to level-triggered */
2361         memset(senses, 1, max - off);
2362
2363         for (np = allnodes; np != 0; np = np->allnext) {
2364                 for (j = 0; j < np->n_intrs; j++) {
2365                         i = np->intrs[j].line;
2366                         if (i >= off && i < max)
2367                                 senses[i-off] = np->intrs[j].sense;
2368                 }
2369         }
2370 }
2371
2372 /*
2373  * Construct and return a list of the device_nodes with a given name.
2374  */
2375 struct device_node *
2376 find_devices(const char *name)
2377 {
2378         struct device_node *head, **prevp, *np;
2379
2380         prevp = &head;
2381         for (np = allnodes; np != 0; np = np->allnext) {
2382                 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
2383                         *prevp = np;
2384                         prevp = &np->next;
2385                 }
2386         }
2387         *prevp = 0;
2388         return head;
2389 }
2390
2391 /*
2392  * Construct and return a list of the device_nodes with a given type.
2393  */
2394 struct device_node *
2395 find_type_devices(const char *type)
2396 {
2397         struct device_node *head, **prevp, *np;
2398
2399         prevp = &head;
2400         for (np = allnodes; np != 0; np = np->allnext) {
2401                 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
2402                         *prevp = np;
2403                         prevp = &np->next;
2404                 }
2405         }
2406         *prevp = 0;
2407         return head;
2408 }
2409
2410 /*
2411  * Returns all nodes linked together
2412  */
2413 struct device_node *
2414 find_all_nodes(void)
2415 {
2416         struct device_node *head, **prevp, *np;
2417
2418         prevp = &head;
2419         for (np = allnodes; np != 0; np = np->allnext) {
2420                 *prevp = np;
2421                 prevp = &np->next;
2422         }
2423         *prevp = 0;
2424         return head;
2425 }
2426
2427 /* Checks if the given "compat" string matches one of the strings in
2428  * the device's "compatible" property
2429  */
2430 int
2431 device_is_compatible(struct device_node *device, const char *compat)
2432 {
2433         const char* cp;
2434         int cplen, l;
2435
2436         cp = (char *) get_property(device, "compatible", &cplen);
2437         if (cp == NULL)
2438                 return 0;
2439         while (cplen > 0) {
2440                 if (strncasecmp(cp, compat, strlen(compat)) == 0)
2441                         return 1;
2442                 l = strlen(cp) + 1;
2443                 cp += l;
2444                 cplen -= l;
2445         }
2446
2447         return 0;
2448 }
2449
2450
2451 /*
2452  * Indicates whether the root node has a given value in its
2453  * compatible property.
2454  */
2455 int
2456 machine_is_compatible(const char *compat)
2457 {
2458         struct device_node *root;
2459         int rc = 0;
2460   
2461         root = of_find_node_by_path("/");
2462         if (root) {
2463                 rc = device_is_compatible(root, compat);
2464                 of_node_put(root);
2465         }
2466         return rc;
2467 }
2468
2469 /*
2470  * Construct and return a list of the device_nodes with a given type
2471  * and compatible property.
2472  */
2473 struct device_node *
2474 find_compatible_devices(const char *type, const char *compat)
2475 {
2476         struct device_node *head, **prevp, *np;
2477
2478         prevp = &head;
2479         for (np = allnodes; np != 0; np = np->allnext) {
2480                 if (type != NULL
2481                     && !(np->type != 0 && strcasecmp(np->type, type) == 0))
2482                         continue;
2483                 if (device_is_compatible(np, compat)) {
2484                         *prevp = np;
2485                         prevp = &np->next;
2486                 }
2487         }
2488         *prevp = 0;
2489         return head;
2490 }
2491
2492 /*
2493  * Find the device_node with a given full_name.
2494  */
2495 struct device_node *
2496 find_path_device(const char *path)
2497 {
2498         struct device_node *np;
2499
2500         for (np = allnodes; np != 0; np = np->allnext)
2501                 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
2502                         return np;
2503         return NULL;
2504 }
2505
2506 /*******
2507  *
2508  * New implementation of the OF "find" APIs, return a refcounted
2509  * object, call of_node_put() when done.  The device tree and list
2510  * are protected by a rw_lock.
2511  *
2512  * Note that property management will need some locking as well,
2513  * this isn't dealt with yet.
2514  *
2515  *******/
2516
2517 /**
2518  *      of_find_node_by_name - Find a node by its "name" property
2519  *      @from:  The node to start searching from or NULL, the node
2520  *              you pass will not be searched, only the next one
2521  *              will; typically, you pass what the previous call
2522  *              returned. of_node_put() will be called on it
2523  *      @name:  The name string to match against
2524  *
2525  *      Returns a node pointer with refcount incremented, use
2526  *      of_node_put() on it when done.
2527  */
2528 struct device_node *of_find_node_by_name(struct device_node *from,
2529         const char *name)
2530 {
2531         struct device_node *np;
2532
2533         read_lock(&devtree_lock);
2534         np = from ? from->allnext : allnodes;
2535         for (; np != 0; np = np->allnext)
2536                 if (np->name != 0 && strcasecmp(np->name, name) == 0
2537                     && of_node_get(np))
2538                         break;
2539         if (from)
2540                 of_node_put(from);
2541         read_unlock(&devtree_lock);
2542         return np;
2543 }
2544 EXPORT_SYMBOL(of_find_node_by_name);
2545
2546 /**
2547  *      of_find_node_by_type - Find a node by its "device_type" property
2548  *      @from:  The node to start searching from or NULL, the node
2549  *              you pass will not be searched, only the next one
2550  *              will; typically, you pass what the previous call
2551  *              returned. of_node_put() will be called on it
2552  *      @name:  The type string to match against
2553  *
2554  *      Returns a node pointer with refcount incremented, use
2555  *      of_node_put() on it when done.
2556  */
2557 struct device_node *of_find_node_by_type(struct device_node *from,
2558         const char *type)
2559 {
2560         struct device_node *np;
2561
2562         read_lock(&devtree_lock);
2563         np = from ? from->allnext : allnodes;
2564         for (; np != 0; np = np->allnext)
2565                 if (np->type != 0 && strcasecmp(np->type, type) == 0
2566                     && of_node_get(np))
2567                         break;
2568         if (from)
2569                 of_node_put(from);
2570         read_unlock(&devtree_lock);
2571         return np;
2572 }
2573 EXPORT_SYMBOL(of_find_node_by_type);
2574
2575 /**
2576  *      of_find_compatible_node - Find a node based on type and one of the
2577  *                                tokens in its "compatible" property
2578  *      @from:          The node to start searching from or NULL, the node
2579  *                      you pass will not be searched, only the next one
2580  *                      will; typically, you pass what the previous call
2581  *                      returned. of_node_put() will be called on it
2582  *      @type:          The type string to match "device_type" or NULL to ignore
2583  *      @compatible:    The string to match to one of the tokens in the device
2584  *                      "compatible" list.
2585  *
2586  *      Returns a node pointer with refcount incremented, use
2587  *      of_node_put() on it when done.
2588  */
2589 struct device_node *of_find_compatible_node(struct device_node *from,
2590         const char *type, const char *compatible)
2591 {
2592         struct device_node *np;
2593
2594         read_lock(&devtree_lock);
2595         np = from ? from->allnext : allnodes;
2596         for (; np != 0; np = np->allnext) {
2597                 if (type != NULL
2598                     && !(np->type != 0 && strcasecmp(np->type, type) == 0))
2599                         continue;
2600                 if (device_is_compatible(np, compatible) && of_node_get(np))
2601                         break;
2602         }
2603         if (from)
2604                 of_node_put(from);
2605         read_unlock(&devtree_lock);
2606         return np;
2607 }
2608 EXPORT_SYMBOL(of_find_compatible_node);
2609
2610 /**
2611  *      of_find_node_by_path - Find a node matching a full OF path
2612  *      @path:  The full path to match
2613  *
2614  *      Returns a node pointer with refcount incremented, use
2615  *      of_node_put() on it when done.
2616  */
2617 struct device_node *of_find_node_by_path(const char *path)
2618 {
2619         struct device_node *np = allnodes;
2620
2621         read_lock(&devtree_lock);
2622         for (; np != 0; np = np->allnext)
2623                 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
2624                     && of_node_get(np))
2625                         break;
2626         read_unlock(&devtree_lock);
2627         return np;
2628 }
2629 EXPORT_SYMBOL(of_find_node_by_path);
2630
2631 /**
2632  *      of_find_all_nodes - Get next node in global list
2633  *      @prev:  Previous node or NULL to start iteration
2634  *              of_node_put() will be called on it
2635  *
2636  *      Returns a node pointer with refcount incremented, use
2637  *      of_node_put() on it when done.
2638  */
2639 struct device_node *of_find_all_nodes(struct device_node *prev)
2640 {
2641         struct device_node *np;
2642
2643         read_lock(&devtree_lock);
2644         np = prev ? prev->allnext : allnodes;
2645         for (; np != 0; np = np->allnext)
2646                 if (of_node_get(np))
2647                         break;
2648         if (prev)
2649                 of_node_put(prev);
2650         read_unlock(&devtree_lock);
2651         return np;
2652 }
2653 EXPORT_SYMBOL(of_find_all_nodes);
2654
2655 /**
2656  *      of_get_parent - Get a node's parent if any
2657  *      @node:  Node to get parent
2658  *
2659  *      Returns a node pointer with refcount incremented, use
2660  *      of_node_put() on it when done.
2661  */
2662 struct device_node *of_get_parent(const struct device_node *node)
2663 {
2664         struct device_node *np;
2665
2666         if (!node)
2667                 return NULL;
2668
2669         read_lock(&devtree_lock);
2670         np = of_node_get(node->parent);
2671         read_unlock(&devtree_lock);
2672         return np;
2673 }
2674 EXPORT_SYMBOL(of_get_parent);
2675
2676 /**
2677  *      of_get_next_child - Iterate a node childs
2678  *      @node:  parent node
2679  *      @prev:  previous child of the parent node, or NULL to get first
2680  *
2681  *      Returns a node pointer with refcount incremented, use
2682  *      of_node_put() on it when done.
2683  */
2684 struct device_node *of_get_next_child(const struct device_node *node,
2685         struct device_node *prev)
2686 {
2687         struct device_node *next;
2688
2689         read_lock(&devtree_lock);
2690         next = prev ? prev->sibling : node->child;
2691         for (; next != 0; next = next->sibling)
2692                 if (of_node_get(next))
2693                         break;
2694         if (prev)
2695                 of_node_put(prev);
2696         read_unlock(&devtree_lock);
2697         return next;
2698 }
2699 EXPORT_SYMBOL(of_get_next_child);
2700
2701 /**
2702  *      of_node_get - Increment refcount of a node
2703  *      @node:  Node to inc refcount, NULL is supported to
2704  *              simplify writing of callers
2705  *
2706  *      Returns the node itself or NULL if gone.
2707  */
2708 struct device_node *of_node_get(struct device_node *node)
2709 {
2710         if (node && !OF_IS_STALE(node)) {
2711                 atomic_inc(&node->_users);
2712                 return node;
2713         }
2714         return NULL;
2715 }
2716 EXPORT_SYMBOL(of_node_get);
2717
2718 /**
2719  *      of_node_cleanup - release a dynamically allocated node
2720  *      @arg:  Node to be released
2721  */
2722 static void of_node_cleanup(struct device_node *node)
2723 {
2724         struct property *prop = node->properties;
2725
2726         if (!OF_IS_DYNAMIC(node))
2727                 return;
2728         while (prop) {
2729                 struct property *next = prop->next;
2730                 kfree(prop->name);
2731                 kfree(prop->value);
2732                 kfree(prop);
2733                 prop = next;
2734         }
2735         kfree(node->intrs);
2736         kfree(node->addrs);
2737         kfree(node->full_name);
2738         kfree(node);
2739 }
2740
2741 /**
2742  *      of_node_put - Decrement refcount of a node
2743  *      @node:  Node to dec refcount, NULL is supported to
2744  *              simplify writing of callers
2745  *
2746  */
2747 void of_node_put(struct device_node *node)
2748 {
2749         if (!node)
2750                 return;
2751
2752         WARN_ON(0 == atomic_read(&node->_users));
2753
2754         if (OF_IS_STALE(node)) {
2755                 if (atomic_dec_and_test(&node->_users)) {
2756                         of_node_cleanup(node);
2757                         return;
2758                 }
2759         }
2760         else
2761                 atomic_dec(&node->_users);
2762 }
2763 EXPORT_SYMBOL(of_node_put);
2764
2765 /**
2766  *      derive_parent - basically like dirname(1)
2767  *      @path:  the full_name of a node to be added to the tree
2768  *
2769  *      Returns the node which should be the parent of the node
2770  *      described by path.  E.g., for path = "/foo/bar", returns
2771  *      the node with full_name = "/foo".
2772  */
2773 static struct device_node *derive_parent(const char *path)
2774 {
2775         struct device_node *parent = NULL;
2776         char *parent_path = "/";
2777         size_t parent_path_len = strrchr(path, '/') - path + 1;
2778
2779         /* reject if path is "/" */
2780         if (!strcmp(path, "/"))
2781                 return NULL;
2782
2783         if (strrchr(path, '/') != path) {
2784                 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
2785                 if (!parent_path)
2786                         return NULL;
2787                 strlcpy(parent_path, path, parent_path_len);
2788         }
2789         parent = of_find_node_by_path(parent_path);
2790         if (strcmp(parent_path, "/"))
2791                 kfree(parent_path);
2792         return parent;
2793 }
2794
2795 /*
2796  * Routines for "runtime" addition and removal of device tree nodes.
2797  */
2798 #ifdef CONFIG_PROC_DEVICETREE
2799 /*
2800  * Add a node to /proc/device-tree.
2801  */
2802 static void add_node_proc_entries(struct device_node *np)
2803 {
2804         struct proc_dir_entry *ent;
2805
2806         ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
2807         if (ent)
2808                 proc_device_tree_add_node(np, ent);
2809 }
2810
2811 static void remove_node_proc_entries(struct device_node *np)
2812 {
2813         struct property *pp = np->properties;
2814         struct device_node *parent = np->parent;
2815
2816         while (pp) {
2817                 remove_proc_entry(pp->name, np->pde);
2818                 pp = pp->next;
2819         }
2820
2821         /* Assuming that symlinks have the same parent directory as
2822          * np->pde.
2823          */
2824         if (np->name_link)
2825                 remove_proc_entry(np->name_link->name, parent->pde);
2826         if (np->addr_link)
2827                 remove_proc_entry(np->addr_link->name, parent->pde);
2828         if (np->pde)
2829                 remove_proc_entry(np->pde->name, parent->pde);
2830 }
2831 #else /* !CONFIG_PROC_DEVICETREE */
2832 static void add_node_proc_entries(struct device_node *np)
2833 {
2834         return;
2835 }
2836
2837 static void remove_node_proc_entries(struct device_node *np)
2838 {
2839         return;
2840 }
2841 #endif /* CONFIG_PROC_DEVICETREE */
2842
2843 /*
2844  * Fix up n_intrs and intrs fields in a new device node
2845  *
2846  */
2847 static int of_finish_dynamic_node_interrupts(struct device_node *node)
2848 {
2849         int intrcells, intlen, i;
2850         unsigned *irq, *ints, virq;
2851         struct device_node *ic;
2852
2853         ints = (unsigned int *)get_property(node, "interrupts", &intlen);
2854         intrcells = prom_n_intr_cells(node);
2855         intlen /= intrcells * sizeof(unsigned int);
2856         node->n_intrs = intlen;
2857         node->intrs = kmalloc(sizeof(struct interrupt_info) * intlen,
2858                               GFP_KERNEL);
2859         if (!node->intrs)
2860                 return -ENOMEM;
2861
2862         for (i = 0; i < intlen; ++i) {
2863                 int n, j;
2864                 node->intrs[i].line = 0;
2865                 node->intrs[i].sense = 1;
2866                 n = map_interrupt(&irq, &ic, node, ints, intrcells);
2867                 if (n <= 0)
2868                         continue;
2869                 virq = virt_irq_create_mapping(irq[0]);
2870                 if (virq == NO_IRQ) {
2871                         printk(KERN_CRIT "Could not allocate interrupt "
2872                                "number for %s\n", node->full_name);
2873                         return -ENOMEM;
2874                 }
2875                 node->intrs[i].line = irq_offset_up(virq);
2876                 if (n > 1)
2877                         node->intrs[i].sense = irq[1];
2878                 if (n > 2) {
2879                         printk(KERN_DEBUG "hmmm, got %d intr cells for %s:", n,
2880                                node->full_name);
2881                         for (j = 0; j < n; ++j)
2882                                 printk(" %d", irq[j]);
2883                         printk("\n");
2884                 }
2885                 ints += intrcells;
2886         }
2887         return 0;
2888 }
2889
2890 /*
2891  * Fix up the uninitialized fields in a new device node:
2892  * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
2893  *
2894  * A lot of boot-time code is duplicated here, because functions such
2895  * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
2896  * slab allocator.
2897  *
2898  * This should probably be split up into smaller chunks.
2899  */
2900
2901 static int of_finish_dynamic_node(struct device_node *node)
2902 {
2903         struct device_node *parent = of_get_parent(node);
2904         u32 *regs;
2905         int err = 0;
2906         phandle *ibm_phandle;
2907  
2908         node->name = get_property(node, "name", 0);
2909         node->type = get_property(node, "device_type", 0);
2910
2911         if (!parent) {
2912                 err = -ENODEV;
2913                 goto out;
2914         }
2915
2916         /* We don't support that function on PowerMac, at least
2917          * not yet
2918          */
2919         if (systemcfg->platform == PLATFORM_POWERMAC)
2920                 return -ENODEV;
2921
2922         /* fix up new node's linux_phandle field */
2923         if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
2924                 node->linux_phandle = *ibm_phandle;
2925
2926         /* do the work of interpret_pci_props */
2927         if (parent->type && !strcmp(parent->type, "pci")) {
2928                 struct address_range *adr;
2929                 struct pci_reg_property *pci_addrs;
2930                 int i, l;
2931
2932                 pci_addrs = (struct pci_reg_property *)
2933                         get_property(node, "assigned-addresses", &l);
2934                 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
2935                         i = 0;
2936                         adr = kmalloc(sizeof(struct address_range) * 
2937                                       (l / sizeof(struct pci_reg_property)),
2938                                       GFP_KERNEL);
2939                         if (!adr) {
2940                                 err = -ENOMEM;
2941                                 goto out;
2942                         }
2943                         while ((l -= sizeof(struct pci_reg_property)) >= 0) {
2944                                 adr[i].space = pci_addrs[i].addr.a_hi;
2945                                 adr[i].address = pci_addrs[i].addr.a_lo;
2946                                 adr[i].size = pci_addrs[i].size_lo;
2947                                 ++i;
2948                         }
2949                         node->addrs = adr;
2950                         node->n_addrs = i;
2951                 }
2952         }
2953
2954         /* now do the work of finish_node_interrupts */
2955         if (get_property(node, "interrupts", 0)) {
2956                 err = of_finish_dynamic_node_interrupts(node);
2957                 if (err) goto out;
2958         }
2959
2960        /* now do the rough equivalent of update_dn_pci_info, this
2961         * probably is not correct for phb's, but should work for
2962         * IOAs and slots.
2963         */
2964
2965        node->phb = parent->phb;
2966
2967        regs = (u32 *)get_property(node, "reg", 0);
2968        if (regs) {
2969                node->busno = (regs[0] >> 16) & 0xff;
2970                node->devfn = (regs[0] >> 8) & 0xff;
2971        }
2972
2973         /* fixing up iommu_table */
2974
2975         if(strcmp(node->name, "pci") == 0 &&
2976                 get_property(node, "ibm,dma-window", NULL)) {
2977                 node->bussubno = node->busno;
2978                 iommu_devnode_init(node);
2979         }
2980         else
2981                 node->iommu_table = parent->iommu_table;
2982
2983 out:
2984         of_node_put(parent);
2985         return err;
2986 }
2987
2988 /*
2989  * Given a path and a property list, construct an OF device node, add
2990  * it to the device tree and global list, and place it in
2991  * /proc/device-tree.  This function may sleep.
2992  */
2993 int of_add_node(const char *path, struct property *proplist)
2994 {
2995         struct device_node *np;
2996         int err = 0;
2997
2998         np = kmalloc(sizeof(struct device_node), GFP_KERNEL);
2999         if (!np)
3000                 return -ENOMEM;
3001
3002         memset(np, 0, sizeof(*np));
3003
3004         np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
3005         if (!np->full_name) {
3006                 kfree(np);
3007                 return -ENOMEM;
3008         }
3009         strcpy(np->full_name, path);
3010
3011         np->properties = proplist;
3012         OF_MARK_DYNAMIC(np);
3013         of_node_get(np);
3014         np->parent = derive_parent(path);
3015         if (!np->parent) {
3016                 kfree(np);
3017                 return -EINVAL; /* could also be ENOMEM, though */
3018         }
3019
3020         if (0 != (err = of_finish_dynamic_node(np))) {
3021                 kfree(np);
3022                 return err;
3023         }
3024
3025         write_lock(&devtree_lock);
3026         np->sibling = np->parent->child;
3027         np->allnext = allnodes;
3028         np->parent->child = np;
3029         allnodes = np;
3030         write_unlock(&devtree_lock);
3031
3032         add_node_proc_entries(np);
3033
3034         of_node_put(np->parent);
3035         of_node_put(np);
3036         return 0;
3037 }
3038
3039 /*
3040  * Remove an OF device node from the system.
3041  * Caller should have already "gotten" np.
3042  */
3043 int of_remove_node(struct device_node *np)
3044 {
3045         struct device_node *parent, *child;
3046
3047         parent = of_get_parent(np);
3048         if (!parent)
3049                 return -EINVAL;
3050
3051         if ((child = of_get_next_child(np, NULL))) {
3052                 of_node_put(child);
3053                 return -EBUSY;
3054         }
3055
3056         write_lock(&devtree_lock);
3057         OF_MARK_STALE(np);
3058         remove_node_proc_entries(np);
3059         if (allnodes == np)
3060                 allnodes = np->allnext;
3061         else {
3062                 struct device_node *prev;
3063                 for (prev = allnodes;
3064                      prev->allnext != np;
3065                      prev = prev->allnext)
3066                         ;
3067                 prev->allnext = np->allnext;
3068         }
3069
3070         if (parent->child == np)
3071                 parent->child = np->sibling;
3072         else {
3073                 struct device_node *prevsib;
3074                 for (prevsib = np->parent->child;
3075                      prevsib->sibling != np;
3076                      prevsib = prevsib->sibling)
3077                         ;
3078                 prevsib->sibling = np->sibling;
3079         }
3080         write_unlock(&devtree_lock);
3081         of_node_put(parent);
3082         return 0;
3083 }
3084
3085 /*
3086  * Find a property with a given name for a given node
3087  * and return the value.
3088  */
3089 unsigned char *
3090 get_property(struct device_node *np, const char *name, int *lenp)
3091 {
3092         struct property *pp;
3093
3094         for (pp = np->properties; pp != 0; pp = pp->next)
3095                 if (strcmp(pp->name, name) == 0) {
3096                         if (lenp != 0)
3097                                 *lenp = pp->length;
3098                         return pp->value;
3099                 }
3100         return 0;
3101 }
3102
3103 /*
3104  * Add a property to a node
3105  */
3106 void
3107 prom_add_property(struct device_node* np, struct property* prop)
3108 {
3109         struct property **next = &np->properties;
3110
3111         prop->next = NULL;      
3112         while (*next)
3113                 next = &(*next)->next;
3114         *next = prop;
3115 }
3116
3117 #if 0
3118 void
3119 print_properties(struct device_node *np)
3120 {
3121         struct property *pp;
3122         char *cp;
3123         int i, n;
3124
3125         for (pp = np->properties; pp != 0; pp = pp->next) {
3126                 printk(KERN_INFO "%s", pp->name);
3127                 for (i = strlen(pp->name); i < 16; ++i)
3128                         printk(" ");
3129                 cp = (char *) pp->value;
3130                 for (i = pp->length; i > 0; --i, ++cp)
3131                         if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
3132                             || (i == 1 && *cp != 0))
3133                                 break;
3134                 if (i == 0 && pp->length > 1) {
3135                         /* looks like a string */
3136                         printk(" %s\n", (char *) pp->value);
3137                 } else {
3138                         /* dump it in hex */
3139                         n = pp->length;
3140                         if (n > 64)
3141                                 n = 64;
3142                         if (pp->length % 4 == 0) {
3143                                 unsigned int *p = (unsigned int *) pp->value;
3144
3145                                 n /= 4;
3146                                 for (i = 0; i < n; ++i) {
3147                                         if (i != 0 && (i % 4) == 0)
3148                                                 printk("\n                ");
3149                                         printk(" %08x", *p++);
3150                                 }
3151                         } else {
3152                                 unsigned char *bp = pp->value;
3153
3154                                 for (i = 0; i < n; ++i) {
3155                                         if (i != 0 && (i % 16) == 0)
3156                                                 printk("\n                ");
3157                                         printk(" %02x", *bp++);
3158                                 }
3159                         }
3160                         printk("\n");
3161                         if (pp->length > 64)
3162                                 printk("                 ... (length = %d)\n",
3163                                        pp->length);
3164                 }
3165         }
3166 }
3167 #endif