powerpc: Make prom_init.c suitable for both 32-bit and 64-bit
[powerpc.git] / arch / powerpc / kernel / prom_init.c
1 /*
2  * Procedures for interfacing to Open Firmware.
3  *
4  * Paul Mackerras       August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  * 
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com 
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #undef DEBUG_PROM
17
18 #include <stdarg.h>
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/threads.h>
24 #include <linux/spinlock.h>
25 #include <linux/types.h>
26 #include <linux/pci.h>
27 #include <linux/proc_fs.h>
28 #include <linux/stringify.h>
29 #include <linux/delay.h>
30 #include <linux/initrd.h>
31 #include <linux/bitops.h>
32 #include <asm/prom.h>
33 #include <asm/rtas.h>
34 #include <asm/page.h>
35 #include <asm/processor.h>
36 #include <asm/irq.h>
37 #include <asm/io.h>
38 #include <asm/smp.h>
39 #include <asm/system.h>
40 #include <asm/mmu.h>
41 #include <asm/pgtable.h>
42 #include <asm/pci.h>
43 #include <asm/iommu.h>
44 #include <asm/bootinfo.h>
45 #include <asm/btext.h>
46 #include <asm/sections.h>
47 #include <asm/machdep.h>
48
49 #ifdef CONFIG_LOGO_LINUX_CLUT224
50 #include <linux/linux_logo.h>
51 extern const struct linux_logo logo_linux_clut224;
52 #endif
53
54 /*
55  * Properties whose value is longer than this get excluded from our
56  * copy of the device tree. This value does need to be big enough to
57  * ensure that we don't lose things like the interrupt-map property
58  * on a PCI-PCI bridge.
59  */
60 #define MAX_PROPERTY_LENGTH     (1UL * 1024 * 1024)
61
62 /*
63  * Eventually bump that one up
64  */
65 #define DEVTREE_CHUNK_SIZE      0x100000
66
67 /*
68  * This is the size of the local memory reserve map that gets copied
69  * into the boot params passed to the kernel. That size is totally
70  * flexible as the kernel just reads the list until it encounters an
71  * entry with size 0, so it can be changed without breaking binary
72  * compatibility
73  */
74 #define MEM_RESERVE_MAP_SIZE    8
75
76 /*
77  * prom_init() is called very early on, before the kernel text
78  * and data have been mapped to KERNELBASE.  At this point the code
79  * is running at whatever address it has been loaded at.
80  * On ppc32 we compile with -mrelocatable, which means that references
81  * to extern and static variables get relocated automatically.
82  * On ppc64 we have to relocate the references explicitly with
83  * RELOC.  (Note that strings count as static variables.)
84  *
85  * Because OF may have mapped I/O devices into the area starting at
86  * KERNELBASE, particularly on CHRP machines, we can't safely call
87  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
88  * OF calls must be done within prom_init().
89  *
90  * ADDR is used in calls to call_prom.  The 4th and following
91  * arguments to call_prom should be 32-bit values.
92  * On ppc64, 64 bit values are truncated to 32 bits (and
93  * fortunately don't get interpreted as two arguments).
94  */
95 #ifdef CONFIG_PPC64
96 #define RELOC(x)        (*PTRRELOC(&(x)))
97 #define ADDR(x)         (u32) add_reloc_offset((unsigned long)(x))
98 #else
99 #define RELOC(x)        (x)
100 #define ADDR(x)         (u32) (x)
101 #endif
102
103 #define PROM_BUG() do {                                         \
104         prom_printf("kernel BUG at %s line 0x%x!\n",            \
105                     RELOC(__FILE__), __LINE__);                 \
106         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);       \
107 } while (0)
108
109 #ifdef DEBUG_PROM
110 #define prom_debug(x...)        prom_printf(x)
111 #else
112 #define prom_debug(x...)
113 #endif
114
115 #ifdef CONFIG_PPC32
116 #define PLATFORM_POWERMAC       _MACH_Pmac
117 #define PLATFORM_CHRP           _MACH_chrp
118 #endif
119
120
121 typedef u32 prom_arg_t;
122
123 struct prom_args {
124         u32 service;
125         u32 nargs;
126         u32 nret;
127         prom_arg_t args[10];
128 };
129
130 struct prom_t {
131         ihandle root;
132         ihandle chosen;
133         int cpu;
134         ihandle stdout;
135 };
136
137 struct mem_map_entry {
138         unsigned long   base;
139         unsigned long   size;
140 };
141
142 typedef u32 cell_t;
143
144 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
145
146 #ifdef CONFIG_PPC64
147 extern void enter_prom(struct prom_args *args, unsigned long entry);
148 #else
149 static inline void enter_prom(struct prom_args *args, unsigned long entry)
150 {
151         ((void (*)(struct prom_args *))entry)(args);
152 }
153 #endif
154
155 extern void copy_and_flush(unsigned long dest, unsigned long src,
156                            unsigned long size, unsigned long offset);
157
158 /* prom structure */
159 static struct prom_t __initdata prom;
160
161 static unsigned long prom_entry __initdata;
162
163 #define PROM_SCRATCH_SIZE 256
164
165 static char __initdata of_stdout_device[256];
166 static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
167
168 static unsigned long __initdata dt_header_start;
169 static unsigned long __initdata dt_struct_start, dt_struct_end;
170 static unsigned long __initdata dt_string_start, dt_string_end;
171
172 static unsigned long __initdata prom_initrd_start, prom_initrd_end;
173
174 #ifdef CONFIG_PPC64
175 static int __initdata iommu_force_on;
176 static int __initdata ppc64_iommu_off;
177 static unsigned long __initdata prom_tce_alloc_start;
178 static unsigned long __initdata prom_tce_alloc_end;
179 #endif
180
181 static int __initdata of_platform;
182
183 static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
184
185 static unsigned long __initdata prom_memory_limit;
186
187 static unsigned long __initdata alloc_top;
188 static unsigned long __initdata alloc_top_high;
189 static unsigned long __initdata alloc_bottom;
190 static unsigned long __initdata rmo_top;
191 static unsigned long __initdata ram_top;
192
193 static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
194 static int __initdata mem_reserve_cnt;
195
196 static cell_t __initdata regbuf[1024];
197
198
199 #define MAX_CPU_THREADS 2
200
201 /* TO GO */
202 #ifdef CONFIG_HMT
203 struct {
204         unsigned int pir;
205         unsigned int threadid;
206 } hmt_thread_data[NR_CPUS];
207 #endif /* CONFIG_HMT */
208
209 /*
210  * Error results ... some OF calls will return "-1" on error, some
211  * will return 0, some will return either. To simplify, here are
212  * macros to use with any ihandle or phandle return value to check if
213  * it is valid
214  */
215
216 #define PROM_ERROR              (-1u)
217 #define PHANDLE_VALID(p)        ((p) != 0 && (p) != PROM_ERROR)
218 #define IHANDLE_VALID(i)        ((i) != 0 && (i) != PROM_ERROR)
219
220
221 /* This is the one and *ONLY* place where we actually call open
222  * firmware.
223  */
224
225 static int __init call_prom(const char *service, int nargs, int nret, ...)
226 {
227         int i;
228         struct prom_args args;
229         va_list list;
230
231         args.service = ADDR(service);
232         args.nargs = nargs;
233         args.nret = nret;
234
235         va_start(list, nret);
236         for (i = 0; i < nargs; i++)
237                 args.args[i] = va_arg(list, prom_arg_t);
238         va_end(list);
239
240         for (i = 0; i < nret; i++)
241                 args.args[nargs+i] = 0;
242
243         enter_prom(&args, RELOC(prom_entry));
244
245         return (nret > 0) ? args.args[nargs] : 0;
246 }
247
248 static int __init call_prom_ret(const char *service, int nargs, int nret,
249                                 prom_arg_t *rets, ...)
250 {
251         int i;
252         struct prom_args args;
253         va_list list;
254
255         args.service = ADDR(service);
256         args.nargs = nargs;
257         args.nret = nret;
258
259         va_start(list, rets);
260         for (i = 0; i < nargs; i++)
261                 args.args[i] = va_arg(list, prom_arg_t);
262         va_end(list);
263
264         for (i = 0; i < nret; i++)
265                 rets[nargs+i] = 0;
266
267         enter_prom(&args, RELOC(prom_entry));
268
269         if (rets != NULL)
270                 for (i = 1; i < nret; ++i)
271                         rets[i] = args.args[nargs+i];
272
273         return (nret > 0) ? args.args[nargs] : 0;
274 }
275
276
277 static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
278                                 unsigned long align)
279 {
280         return (unsigned int)call_prom("claim", 3, 1,
281                                        (prom_arg_t)virt, (prom_arg_t)size,
282                                        (prom_arg_t)align);
283 }
284
285 static void __init prom_print(const char *msg)
286 {
287         const char *p, *q;
288         struct prom_t *_prom = &RELOC(prom);
289
290         if (_prom->stdout == 0)
291                 return;
292
293         for (p = msg; *p != 0; p = q) {
294                 for (q = p; *q != 0 && *q != '\n'; ++q)
295                         ;
296                 if (q > p)
297                         call_prom("write", 3, 1, _prom->stdout, p, q - p);
298                 if (*q == 0)
299                         break;
300                 ++q;
301                 call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
302         }
303 }
304
305
306 static void __init prom_print_hex(unsigned long val)
307 {
308         int i, nibbles = sizeof(val)*2;
309         char buf[sizeof(val)*2+1];
310         struct prom_t *_prom = &RELOC(prom);
311
312         for (i = nibbles-1;  i >= 0;  i--) {
313                 buf[i] = (val & 0xf) + '0';
314                 if (buf[i] > '9')
315                         buf[i] += ('a'-'0'-10);
316                 val >>= 4;
317         }
318         buf[nibbles] = '\0';
319         call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
320 }
321
322
323 static void __init prom_printf(const char *format, ...)
324 {
325         const char *p, *q, *s;
326         va_list args;
327         unsigned long v;
328         struct prom_t *_prom = &RELOC(prom);
329
330         va_start(args, format);
331 #ifdef CONFIG_PPC64
332         format = PTRRELOC(format);
333 #endif
334         for (p = format; *p != 0; p = q) {
335                 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
336                         ;
337                 if (q > p)
338                         call_prom("write", 3, 1, _prom->stdout, p, q - p);
339                 if (*q == 0)
340                         break;
341                 if (*q == '\n') {
342                         ++q;
343                         call_prom("write", 3, 1, _prom->stdout,
344                                   ADDR("\r\n"), 2);
345                         continue;
346                 }
347                 ++q;
348                 if (*q == 0)
349                         break;
350                 switch (*q) {
351                 case 's':
352                         ++q;
353                         s = va_arg(args, const char *);
354                         prom_print(s);
355                         break;
356                 case 'x':
357                         ++q;
358                         v = va_arg(args, unsigned long);
359                         prom_print_hex(v);
360                         break;
361                 }
362         }
363 }
364
365
366 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
367 {
368 #ifdef CONFIG_PPC64
369         reason = PTRRELOC(reason);
370 #endif
371         prom_print(reason);
372         /* ToDo: should put up an SRC here on p/iSeries */
373         call_prom("exit", 0, 0);
374
375         for (;;)                        /* should never get here */
376                 ;
377 }
378
379
380 static int __init prom_next_node(phandle *nodep)
381 {
382         phandle node;
383
384         if ((node = *nodep) != 0
385             && (*nodep = call_prom("child", 1, 1, node)) != 0)
386                 return 1;
387         if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
388                 return 1;
389         for (;;) {
390                 if ((node = call_prom("parent", 1, 1, node)) == 0)
391                         return 0;
392                 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
393                         return 1;
394         }
395 }
396
397 static int __init prom_getprop(phandle node, const char *pname,
398                                void *value, size_t valuelen)
399 {
400         return call_prom("getprop", 4, 1, node, ADDR(pname),
401                          (u32)(unsigned long) value, (u32) valuelen);
402 }
403
404 static int __init prom_getproplen(phandle node, const char *pname)
405 {
406         return call_prom("getproplen", 2, 1, node, ADDR(pname));
407 }
408
409 static int __init prom_setprop(phandle node, const char *pname,
410                                void *value, size_t valuelen)
411 {
412         return call_prom("setprop", 4, 1, node, ADDR(pname),
413                          (u32)(unsigned long) value, (u32) valuelen);
414 }
415
416 /* We can't use the standard versions because of RELOC headaches. */
417 #define isxdigit(c)     (('0' <= (c) && (c) <= '9') \
418                          || ('a' <= (c) && (c) <= 'f') \
419                          || ('A' <= (c) && (c) <= 'F'))
420
421 #define isdigit(c)      ('0' <= (c) && (c) <= '9')
422 #define islower(c)      ('a' <= (c) && (c) <= 'z')
423 #define toupper(c)      (islower(c) ? ((c) - 'a' + 'A') : (c))
424
425 unsigned long prom_strtoul(const char *cp, const char **endp)
426 {
427         unsigned long result = 0, base = 10, value;
428
429         if (*cp == '0') {
430                 base = 8;
431                 cp++;
432                 if (toupper(*cp) == 'X') {
433                         cp++;
434                         base = 16;
435                 }
436         }
437
438         while (isxdigit(*cp) &&
439                (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
440                 result = result * base + value;
441                 cp++;
442         }
443
444         if (endp)
445                 *endp = cp;
446
447         return result;
448 }
449
450 unsigned long prom_memparse(const char *ptr, const char **retptr)
451 {
452         unsigned long ret = prom_strtoul(ptr, retptr);
453         int shift = 0;
454
455         /*
456          * We can't use a switch here because GCC *may* generate a
457          * jump table which won't work, because we're not running at
458          * the address we're linked at.
459          */
460         if ('G' == **retptr || 'g' == **retptr)
461                 shift = 30;
462
463         if ('M' == **retptr || 'm' == **retptr)
464                 shift = 20;
465
466         if ('K' == **retptr || 'k' == **retptr)
467                 shift = 10;
468
469         if (shift) {
470                 ret <<= shift;
471                 (*retptr)++;
472         }
473
474         return ret;
475 }
476
477 /*
478  * Early parsing of the command line passed to the kernel, used for
479  * "mem=x" and the options that affect the iommu
480  */
481 static void __init early_cmdline_parse(void)
482 {
483         struct prom_t *_prom = &RELOC(prom);
484         char *opt, *p;
485         int l = 0;
486
487         RELOC(prom_cmd_line[0]) = 0;
488         p = RELOC(prom_cmd_line);
489         if ((long)_prom->chosen > 0)
490                 l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
491 #ifdef CONFIG_CMDLINE
492         if (l == 0) /* dbl check */
493                 strlcpy(RELOC(prom_cmd_line),
494                         RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
495 #endif /* CONFIG_CMDLINE */
496         prom_printf("command line: %s\n", RELOC(prom_cmd_line));
497
498 #ifdef CONFIG_PPC64
499         opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
500         if (opt) {
501                 prom_printf("iommu opt is: %s\n", opt);
502                 opt += 6;
503                 while (*opt && *opt == ' ')
504                         opt++;
505                 if (!strncmp(opt, RELOC("off"), 3))
506                         RELOC(ppc64_iommu_off) = 1;
507                 else if (!strncmp(opt, RELOC("force"), 5))
508                         RELOC(iommu_force_on) = 1;
509         }
510 #endif
511
512         opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
513         if (opt) {
514                 opt += 4;
515                 RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
516 #ifdef CONFIG_PPC64
517                 /* Align to 16 MB == size of ppc64 large page */
518                 RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
519 #endif
520         }
521 }
522
523 #ifdef CONFIG_PPC_PSERIES
524 /*
525  * To tell the firmware what our capabilities are, we have to pass
526  * it a fake 32-bit ELF header containing a couple of PT_NOTE sections
527  * that contain structures that contain the actual values.
528  */
529 static struct fake_elf {
530         Elf32_Ehdr      elfhdr;
531         Elf32_Phdr      phdr[2];
532         struct chrpnote {
533                 u32     namesz;
534                 u32     descsz;
535                 u32     type;
536                 char    name[8];        /* "PowerPC" */
537                 struct chrpdesc {
538                         u32     real_mode;
539                         u32     real_base;
540                         u32     real_size;
541                         u32     virt_base;
542                         u32     virt_size;
543                         u32     load_base;
544                 } chrpdesc;
545         } chrpnote;
546         struct rpanote {
547                 u32     namesz;
548                 u32     descsz;
549                 u32     type;
550                 char    name[24];       /* "IBM,RPA-Client-Config" */
551                 struct rpadesc {
552                         u32     lpar_affinity;
553                         u32     min_rmo_size;
554                         u32     min_rmo_percent;
555                         u32     max_pft_size;
556                         u32     splpar;
557                         u32     min_load;
558                         u32     new_mem_def;
559                         u32     ignore_me;
560                 } rpadesc;
561         } rpanote;
562 } fake_elf = {
563         .elfhdr = {
564                 .e_ident = { 0x7f, 'E', 'L', 'F',
565                              ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
566                 .e_type = ET_EXEC,      /* yeah right */
567                 .e_machine = EM_PPC,
568                 .e_version = EV_CURRENT,
569                 .e_phoff = offsetof(struct fake_elf, phdr),
570                 .e_phentsize = sizeof(Elf32_Phdr),
571                 .e_phnum = 2
572         },
573         .phdr = {
574                 [0] = {
575                         .p_type = PT_NOTE,
576                         .p_offset = offsetof(struct fake_elf, chrpnote),
577                         .p_filesz = sizeof(struct chrpnote)
578                 }, [1] = {
579                         .p_type = PT_NOTE,
580                         .p_offset = offsetof(struct fake_elf, rpanote),
581                         .p_filesz = sizeof(struct rpanote)
582                 }
583         },
584         .chrpnote = {
585                 .namesz = sizeof("PowerPC"),
586                 .descsz = sizeof(struct chrpdesc),
587                 .type = 0x1275,
588                 .name = "PowerPC",
589                 .chrpdesc = {
590                         .real_mode = ~0U,       /* ~0 means "don't care" */
591                         .real_base = ~0U,
592                         .real_size = ~0U,
593                         .virt_base = ~0U,
594                         .virt_size = ~0U,
595                         .load_base = ~0U
596                 },
597         },
598         .rpanote = {
599                 .namesz = sizeof("IBM,RPA-Client-Config"),
600                 .descsz = sizeof(struct rpadesc),
601                 .type = 0x12759999,
602                 .name = "IBM,RPA-Client-Config",
603                 .rpadesc = {
604                         .lpar_affinity = 0,
605                         .min_rmo_size = 64,     /* in megabytes */
606                         .min_rmo_percent = 0,
607                         .max_pft_size = 48,     /* 2^48 bytes max PFT size */
608                         .splpar = 1,
609                         .min_load = ~0U,
610                         .new_mem_def = 0
611                 }
612         }
613 };
614
615 static void __init prom_send_capabilities(void)
616 {
617         ihandle elfloader;
618
619         elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
620         if (elfloader == 0) {
621                 prom_printf("couldn't open /packages/elf-loader\n");
622                 return;
623         }
624         call_prom("call-method", 3, 1, ADDR("process-elf-header"),
625                         elfloader, ADDR(&fake_elf));
626         call_prom("close", 1, 0, elfloader);
627 }
628 #endif
629
630 /*
631  * Memory allocation strategy... our layout is normally:
632  *
633  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
634  *  rare cases, initrd might end up being before the kernel though.
635  *  We assume this won't override the final kernel at 0, we have no
636  *  provision to handle that in this version, but it should hopefully
637  *  never happen.
638  *
639  *  alloc_top is set to the top of RMO, eventually shrink down if the
640  *  TCEs overlap
641  *
642  *  alloc_bottom is set to the top of kernel/initrd
643  *
644  *  from there, allocations are done this way : rtas is allocated
645  *  topmost, and the device-tree is allocated from the bottom. We try
646  *  to grow the device-tree allocation as we progress. If we can't,
647  *  then we fail, we don't currently have a facility to restart
648  *  elsewhere, but that shouldn't be necessary.
649  *
650  *  Note that calls to reserve_mem have to be done explicitly, memory
651  *  allocated with either alloc_up or alloc_down isn't automatically
652  *  reserved.
653  */
654
655
656 /*
657  * Allocates memory in the RMO upward from the kernel/initrd
658  *
659  * When align is 0, this is a special case, it means to allocate in place
660  * at the current location of alloc_bottom or fail (that is basically
661  * extending the previous allocation). Used for the device-tree flattening
662  */
663 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
664 {
665         unsigned long base = _ALIGN_UP(RELOC(alloc_bottom), align);
666         unsigned long addr = 0;
667
668         prom_debug("alloc_up(%x, %x)\n", size, align);
669         if (RELOC(ram_top) == 0)
670                 prom_panic("alloc_up() called with mem not initialized\n");
671
672         if (align)
673                 base = _ALIGN_UP(RELOC(alloc_bottom), align);
674         else
675                 base = RELOC(alloc_bottom);
676
677         for(; (base + size) <= RELOC(alloc_top); 
678             base = _ALIGN_UP(base + 0x100000, align)) {
679                 prom_debug("    trying: 0x%x\n\r", base);
680                 addr = (unsigned long)prom_claim(base, size, 0);
681                 if (addr != PROM_ERROR)
682                         break;
683                 addr = 0;
684                 if (align == 0)
685                         break;
686         }
687         if (addr == 0)
688                 return 0;
689         RELOC(alloc_bottom) = addr;
690
691         prom_debug(" -> %x\n", addr);
692         prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
693         prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
694         prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
695         prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
696         prom_debug("  ram_top      : %x\n", RELOC(ram_top));
697
698         return addr;
699 }
700
701 /*
702  * Allocates memory downward, either from top of RMO, or if highmem
703  * is set, from the top of RAM.  Note that this one doesn't handle
704  * failures.  It does claim memory if highmem is not set.
705  */
706 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
707                                        int highmem)
708 {
709         unsigned long base, addr = 0;
710
711         prom_debug("alloc_down(%x, %x, %s)\n", size, align,
712                    highmem ? RELOC("(high)") : RELOC("(low)"));
713         if (RELOC(ram_top) == 0)
714                 prom_panic("alloc_down() called with mem not initialized\n");
715
716         if (highmem) {
717                 /* Carve out storage for the TCE table. */
718                 addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
719                 if (addr <= RELOC(alloc_bottom))
720                         return 0;
721                 /* Will we bump into the RMO ? If yes, check out that we
722                  * didn't overlap existing allocations there, if we did,
723                  * we are dead, we must be the first in town !
724                  */
725                 if (addr < RELOC(rmo_top)) {
726                         /* Good, we are first */
727                         if (RELOC(alloc_top) == RELOC(rmo_top))
728                                 RELOC(alloc_top) = RELOC(rmo_top) = addr;
729                         else
730                                 return 0;
731                 }
732                 RELOC(alloc_top_high) = addr;
733                 goto bail;
734         }
735
736         base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
737         for (; base > RELOC(alloc_bottom);
738              base = _ALIGN_DOWN(base - 0x100000, align))  {
739                 prom_debug("    trying: 0x%x\n\r", base);
740                 addr = (unsigned long)prom_claim(base, size, 0);
741                 if (addr != PROM_ERROR)
742                         break;
743                 addr = 0;
744         }
745         if (addr == 0)
746                 return 0;
747         RELOC(alloc_top) = addr;
748
749  bail:
750         prom_debug(" -> %x\n", addr);
751         prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
752         prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
753         prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
754         prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
755         prom_debug("  ram_top      : %x\n", RELOC(ram_top));
756
757         return addr;
758 }
759
760 /*
761  * Parse a "reg" cell
762  */
763 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
764 {
765         cell_t *p = *cellp;
766         unsigned long r = 0;
767
768         /* Ignore more than 2 cells */
769         while (s > sizeof(unsigned long) / 4) {
770                 p++;
771                 s--;
772         }
773         r = *p++;
774 #ifdef CONFIG_PPC64
775         if (s) {
776                 r <<= 32;
777                 r |= *(p++);
778         }
779 #endif
780         *cellp = p;
781         return r;
782 }
783
784 /*
785  * Very dumb function for adding to the memory reserve list, but
786  * we don't need anything smarter at this point
787  *
788  * XXX Eventually check for collisions.  They should NEVER happen.
789  * If problems seem to show up, it would be a good start to track
790  * them down.
791  */
792 static void reserve_mem(unsigned long base, unsigned long size)
793 {
794         unsigned long top = base + size;
795         unsigned long cnt = RELOC(mem_reserve_cnt);
796
797         if (size == 0)
798                 return;
799
800         /* We need to always keep one empty entry so that we
801          * have our terminator with "size" set to 0 since we are
802          * dumb and just copy this entire array to the boot params
803          */
804         base = _ALIGN_DOWN(base, PAGE_SIZE);
805         top = _ALIGN_UP(top, PAGE_SIZE);
806         size = top - base;
807
808         if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
809                 prom_panic("Memory reserve map exhausted !\n");
810         RELOC(mem_reserve_map)[cnt].base = base;
811         RELOC(mem_reserve_map)[cnt].size = size;
812         RELOC(mem_reserve_cnt) = cnt + 1;
813 }
814
815 /*
816  * Initialize memory allocation mecanism, parse "memory" nodes and
817  * obtain that way the top of memory and RMO to setup out local allocator
818  */
819 static void __init prom_init_mem(void)
820 {
821         phandle node;
822         char *path, type[64];
823         unsigned int plen;
824         cell_t *p, *endp;
825         struct prom_t *_prom = &RELOC(prom);
826         u32 rac, rsc;
827
828         /*
829          * We iterate the memory nodes to find
830          * 1) top of RMO (first node)
831          * 2) top of memory
832          */
833         rac = 2;
834         prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
835         rsc = 1;
836         prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
837         prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
838         prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
839
840         prom_debug("scanning memory:\n");
841         path = RELOC(prom_scratch);
842
843         for (node = 0; prom_next_node(&node); ) {
844                 type[0] = 0;
845                 prom_getprop(node, "device_type", type, sizeof(type));
846
847                 if (strcmp(type, RELOC("memory")))
848                         continue;
849         
850                 plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
851                 if (plen > sizeof(regbuf)) {
852                         prom_printf("memory node too large for buffer !\n");
853                         plen = sizeof(regbuf);
854                 }
855                 p = RELOC(regbuf);
856                 endp = p + (plen / sizeof(cell_t));
857
858 #ifdef DEBUG_PROM
859                 memset(path, 0, PROM_SCRATCH_SIZE);
860                 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
861                 prom_debug("  node %s :\n", path);
862 #endif /* DEBUG_PROM */
863
864                 while ((endp - p) >= (rac + rsc)) {
865                         unsigned long base, size;
866
867                         base = prom_next_cell(rac, &p);
868                         size = prom_next_cell(rsc, &p);
869
870                         if (size == 0)
871                                 continue;
872                         prom_debug("    %x %x\n", base, size);
873                         if (base == 0)
874                                 RELOC(rmo_top) = size;
875                         if ((base + size) > RELOC(ram_top))
876                                 RELOC(ram_top) = base + size;
877                 }
878         }
879
880         RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
881
882         /* Check if we have an initrd after the kernel, if we do move our bottom
883          * point to after it
884          */
885         if (RELOC(prom_initrd_start)) {
886                 if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
887                         RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
888         }
889
890         /*
891          * If prom_memory_limit is set we reduce the upper limits *except* for
892          * alloc_top_high. This must be the real top of RAM so we can put
893          * TCE's up there.
894          */
895
896         RELOC(alloc_top_high) = RELOC(ram_top);
897
898         if (RELOC(prom_memory_limit)) {
899                 if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
900                         prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
901                                 RELOC(prom_memory_limit));
902                         RELOC(prom_memory_limit) = 0;
903                 } else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
904                         prom_printf("Ignoring mem=%x >= ram_top.\n",
905                                 RELOC(prom_memory_limit));
906                         RELOC(prom_memory_limit) = 0;
907                 } else {
908                         RELOC(ram_top) = RELOC(prom_memory_limit);
909                         RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
910                 }
911         }
912
913         /*
914          * Setup our top alloc point, that is top of RMO or top of
915          * segment 0 when running non-LPAR.
916          * Some RS64 machines have buggy firmware where claims up at
917          * 1GB fail.  Cap at 768MB as a workaround.
918          * Since 768MB is plenty of room, and we need to cap to something
919          * reasonable on 32-bit, cap at 768MB on all machines.
920          */
921         if (!RELOC(rmo_top))
922                 RELOC(rmo_top) = RELOC(ram_top);
923         RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
924         RELOC(alloc_top) = RELOC(rmo_top);
925
926         prom_printf("memory layout at init:\n");
927         prom_printf("  memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
928         prom_printf("  alloc_bottom : %x\n", RELOC(alloc_bottom));
929         prom_printf("  alloc_top    : %x\n", RELOC(alloc_top));
930         prom_printf("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
931         prom_printf("  rmo_top      : %x\n", RELOC(rmo_top));
932         prom_printf("  ram_top      : %x\n", RELOC(ram_top));
933 }
934
935
936 /*
937  * Allocate room for and instantiate RTAS
938  */
939 static void __init prom_instantiate_rtas(void)
940 {
941         phandle rtas_node;
942         ihandle rtas_inst;
943         u32 base, entry = 0;
944         u32 size = 0;
945
946         prom_debug("prom_instantiate_rtas: start...\n");
947
948         rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
949         prom_debug("rtas_node: %x\n", rtas_node);
950         if (!PHANDLE_VALID(rtas_node))
951                 return;
952
953         prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
954         if (size == 0)
955                 return;
956
957         base = alloc_down(size, PAGE_SIZE, 0);
958         if (base == 0) {
959                 prom_printf("RTAS allocation failed !\n");
960                 return;
961         }
962
963         rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
964         if (!IHANDLE_VALID(rtas_inst)) {
965                 prom_printf("opening rtas package failed");
966                 return;
967         }
968
969         prom_printf("instantiating rtas at 0x%x ...", base);
970
971         if (call_prom_ret("call-method", 3, 2, &entry,
972                           ADDR("instantiate-rtas"),
973                           rtas_inst, base) == PROM_ERROR
974             || entry == 0) {
975                 prom_printf(" failed\n");
976                 return;
977         }
978         prom_printf(" done\n");
979
980         reserve_mem(base, size);
981
982         prom_setprop(rtas_node, "linux,rtas-base", &base, sizeof(base));
983         prom_setprop(rtas_node, "linux,rtas-entry", &entry, sizeof(entry));
984
985         prom_debug("rtas base     = 0x%x\n", base);
986         prom_debug("rtas entry    = 0x%x\n", entry);
987         prom_debug("rtas size     = 0x%x\n", (long)size);
988
989         prom_debug("prom_instantiate_rtas: end...\n");
990 }
991
992 #ifdef CONFIG_PPC64
993 /*
994  * Allocate room for and initialize TCE tables
995  */
996 static void __init prom_initialize_tce_table(void)
997 {
998         phandle node;
999         ihandle phb_node;
1000         char compatible[64], type[64], model[64];
1001         char *path = RELOC(prom_scratch);
1002         u64 base, align;
1003         u32 minalign, minsize;
1004         u64 tce_entry, *tce_entryp;
1005         u64 local_alloc_top, local_alloc_bottom;
1006         u64 i;
1007
1008         if (RELOC(ppc64_iommu_off))
1009                 return;
1010
1011         prom_debug("starting prom_initialize_tce_table\n");
1012
1013         /* Cache current top of allocs so we reserve a single block */
1014         local_alloc_top = RELOC(alloc_top_high);
1015         local_alloc_bottom = local_alloc_top;
1016
1017         /* Search all nodes looking for PHBs. */
1018         for (node = 0; prom_next_node(&node); ) {
1019                 compatible[0] = 0;
1020                 type[0] = 0;
1021                 model[0] = 0;
1022                 prom_getprop(node, "compatible",
1023                              compatible, sizeof(compatible));
1024                 prom_getprop(node, "device_type", type, sizeof(type));
1025                 prom_getprop(node, "model", model, sizeof(model));
1026
1027                 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1028                         continue;
1029
1030                 /* Keep the old logic in tack to avoid regression. */
1031                 if (compatible[0] != 0) {
1032                         if ((strstr(compatible, RELOC("python")) == NULL) &&
1033                             (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1034                             (strstr(compatible, RELOC("Winnipeg")) == NULL))
1035                                 continue;
1036                 } else if (model[0] != 0) {
1037                         if ((strstr(model, RELOC("ython")) == NULL) &&
1038                             (strstr(model, RELOC("peedwagon")) == NULL) &&
1039                             (strstr(model, RELOC("innipeg")) == NULL))
1040                                 continue;
1041                 }
1042
1043                 if (prom_getprop(node, "tce-table-minalign", &minalign,
1044                                  sizeof(minalign)) == PROM_ERROR)
1045                         minalign = 0;
1046                 if (prom_getprop(node, "tce-table-minsize", &minsize,
1047                                  sizeof(minsize)) == PROM_ERROR)
1048                         minsize = 4UL << 20;
1049
1050                 /*
1051                  * Even though we read what OF wants, we just set the table
1052                  * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
1053                  * By doing this, we avoid the pitfalls of trying to DMA to
1054                  * MMIO space and the DMA alias hole.
1055                  *
1056                  * On POWER4, firmware sets the TCE region by assuming
1057                  * each TCE table is 8MB. Using this memory for anything
1058                  * else will impact performance, so we always allocate 8MB.
1059                  * Anton
1060                  */
1061                 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1062                         minsize = 8UL << 20;
1063                 else
1064                         minsize = 4UL << 20;
1065
1066                 /* Align to the greater of the align or size */
1067                 align = max(minalign, minsize);
1068                 base = alloc_down(minsize, align, 1);
1069                 if (base == 0)
1070                         prom_panic("ERROR, cannot find space for TCE table.\n");
1071                 if (base < local_alloc_bottom)
1072                         local_alloc_bottom = base;
1073
1074                 /* Save away the TCE table attributes for later use. */
1075                 prom_setprop(node, "linux,tce-base", &base, sizeof(base));
1076                 prom_setprop(node, "linux,tce-size", &minsize, sizeof(minsize));
1077
1078                 /* It seems OF doesn't null-terminate the path :-( */
1079                 memset(path, 0, sizeof(path));
1080                 /* Call OF to setup the TCE hardware */
1081                 if (call_prom("package-to-path", 3, 1, node,
1082                               path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1083                         prom_printf("package-to-path failed\n");
1084                 }
1085
1086                 prom_debug("TCE table: %s\n", path);
1087                 prom_debug("\tnode = 0x%x\n", node);
1088                 prom_debug("\tbase = 0x%x\n", base);
1089                 prom_debug("\tsize = 0x%x\n", minsize);
1090
1091                 /* Initialize the table to have a one-to-one mapping
1092                  * over the allocated size.
1093                  */
1094                 tce_entryp = (unsigned long *)base;
1095                 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1096                         tce_entry = (i << PAGE_SHIFT);
1097                         tce_entry |= 0x3;
1098                         *tce_entryp = tce_entry;
1099                 }
1100
1101                 prom_printf("opening PHB %s", path);
1102                 phb_node = call_prom("open", 1, 1, path);
1103                 if (phb_node == 0)
1104                         prom_printf("... failed\n");
1105                 else
1106                         prom_printf("... done\n");
1107
1108                 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1109                           phb_node, -1, minsize,
1110                           (u32) base, (u32) (base >> 32));
1111                 call_prom("close", 1, 0, phb_node);
1112         }
1113
1114         reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1115
1116         if (RELOC(prom_memory_limit)) {
1117                 /*
1118                  * We align the start to a 16MB boundary so we can map
1119                  * the TCE area using large pages if possible.
1120                  * The end should be the top of RAM so no need to align it.
1121                  */
1122                 RELOC(prom_tce_alloc_start) = _ALIGN_DOWN(local_alloc_bottom,
1123                                                           0x1000000);
1124                 RELOC(prom_tce_alloc_end) = local_alloc_top;
1125         }
1126
1127         /* Flag the first invalid entry */
1128         prom_debug("ending prom_initialize_tce_table\n");
1129 }
1130 #endif
1131
1132 /*
1133  * With CHRP SMP we need to use the OF to start the other processors.
1134  * We can't wait until smp_boot_cpus (the OF is trashed by then)
1135  * so we have to put the processors into a holding pattern controlled
1136  * by the kernel (not OF) before we destroy the OF.
1137  *
1138  * This uses a chunk of low memory, puts some holding pattern
1139  * code there and sends the other processors off to there until
1140  * smp_boot_cpus tells them to do something.  The holding pattern
1141  * checks that address until its cpu # is there, when it is that
1142  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
1143  * of setting those values.
1144  *
1145  * We also use physical address 0x4 here to tell when a cpu
1146  * is in its holding pattern code.
1147  *
1148  * -- Cort
1149  */
1150 static void __init prom_hold_cpus(void)
1151 {
1152 #ifdef CONFIG_PPC64
1153         unsigned long i;
1154         unsigned int reg;
1155         phandle node;
1156         char type[64];
1157         int cpuid = 0;
1158         unsigned int interrupt_server[MAX_CPU_THREADS];
1159         unsigned int cpu_threads, hw_cpu_num;
1160         int propsize;
1161         extern void __secondary_hold(void);
1162         extern unsigned long __secondary_hold_spinloop;
1163         extern unsigned long __secondary_hold_acknowledge;
1164         unsigned long *spinloop
1165                 = (void *) __pa(&__secondary_hold_spinloop);
1166         unsigned long *acknowledge
1167                 = (void *) __pa(&__secondary_hold_acknowledge);
1168 #ifdef CONFIG_PPC64
1169         unsigned long secondary_hold
1170                 = __pa(*PTRRELOC((unsigned long *)__secondary_hold));
1171 #else
1172         unsigned long secondary_hold = __pa(&__secondary_hold);
1173 #endif
1174         struct prom_t *_prom = &RELOC(prom);
1175
1176         prom_debug("prom_hold_cpus: start...\n");
1177         prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
1178         prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
1179         prom_debug("    1) acknowledge    = 0x%x\n",
1180                    (unsigned long)acknowledge);
1181         prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
1182         prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
1183
1184         /* Set the common spinloop variable, so all of the secondary cpus
1185          * will block when they are awakened from their OF spinloop.
1186          * This must occur for both SMP and non SMP kernels, since OF will
1187          * be trashed when we move the kernel.
1188          */
1189         *spinloop = 0;
1190
1191 #ifdef CONFIG_HMT
1192         for (i = 0; i < NR_CPUS; i++) {
1193                 RELOC(hmt_thread_data)[i].pir = 0xdeadbeef;
1194         }
1195 #endif
1196         /* look for cpus */
1197         for (node = 0; prom_next_node(&node); ) {
1198                 type[0] = 0;
1199                 prom_getprop(node, "device_type", type, sizeof(type));
1200                 if (strcmp(type, RELOC("cpu")) != 0)
1201                         continue;
1202
1203                 /* Skip non-configured cpus. */
1204                 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1205                         if (strcmp(type, RELOC("okay")) != 0)
1206                                 continue;
1207
1208                 reg = -1;
1209                 prom_getprop(node, "reg", &reg, sizeof(reg));
1210
1211                 prom_debug("\ncpuid        = 0x%x\n", cpuid);
1212                 prom_debug("cpu hw idx   = 0x%x\n", reg);
1213
1214                 /* Init the acknowledge var which will be reset by
1215                  * the secondary cpu when it awakens from its OF
1216                  * spinloop.
1217                  */
1218                 *acknowledge = (unsigned long)-1;
1219
1220                 propsize = prom_getprop(node, "ibm,ppc-interrupt-server#s",
1221                                         &interrupt_server,
1222                                         sizeof(interrupt_server));
1223                 if (propsize < 0) {
1224                         /* no property.  old hardware has no SMT */
1225                         cpu_threads = 1;
1226                         interrupt_server[0] = reg; /* fake it with phys id */
1227                 } else {
1228                         /* We have a threaded processor */
1229                         cpu_threads = propsize / sizeof(u32);
1230                         if (cpu_threads > MAX_CPU_THREADS) {
1231                                 prom_printf("SMT: too many threads!\n"
1232                                             "SMT: found %x, max is %x\n",
1233                                             cpu_threads, MAX_CPU_THREADS);
1234                                 cpu_threads = 1; /* ToDo: panic? */
1235                         }
1236                 }
1237
1238                 hw_cpu_num = interrupt_server[0];
1239                 if (hw_cpu_num != _prom->cpu) {
1240                         /* Primary Thread of non-boot cpu */
1241                         prom_printf("%x : starting cpu hw idx %x... ", cpuid, reg);
1242                         call_prom("start-cpu", 3, 0, node,
1243                                   secondary_hold, reg);
1244
1245                         for ( i = 0 ; (i < 100000000) && 
1246                               (*acknowledge == ((unsigned long)-1)); i++ )
1247                                 mb();
1248
1249                         if (*acknowledge == reg) {
1250                                 prom_printf("done\n");
1251                                 /* We have to get every CPU out of OF,
1252                                  * even if we never start it. */
1253                                 if (cpuid >= NR_CPUS)
1254                                         goto next;
1255                         } else {
1256                                 prom_printf("failed: %x\n", *acknowledge);
1257                         }
1258                 }
1259 #ifdef CONFIG_SMP
1260                 else
1261                         prom_printf("%x : boot cpu     %x\n", cpuid, reg);
1262 #endif
1263 next:
1264 #ifdef CONFIG_SMP
1265                 /* Init paca for secondary threads.   They start later. */
1266                 for (i=1; i < cpu_threads; i++) {
1267                         cpuid++;
1268                         if (cpuid >= NR_CPUS)
1269                                 continue;
1270                 }
1271 #endif /* CONFIG_SMP */
1272                 cpuid++;
1273         }
1274 #ifdef CONFIG_HMT
1275         /* Only enable HMT on processors that provide support. */
1276         if (__is_processor(PV_PULSAR) || 
1277             __is_processor(PV_ICESTAR) ||
1278             __is_processor(PV_SSTAR)) {
1279                 prom_printf("    starting secondary threads\n");
1280
1281                 for (i = 0; i < NR_CPUS; i += 2) {
1282                         if (!cpu_online(i))
1283                                 continue;
1284
1285                         if (i == 0) {
1286                                 unsigned long pir = mfspr(SPRN_PIR);
1287                                 if (__is_processor(PV_PULSAR)) {
1288                                         RELOC(hmt_thread_data)[i].pir = 
1289                                                 pir & 0x1f;
1290                                 } else {
1291                                         RELOC(hmt_thread_data)[i].pir = 
1292                                                 pir & 0x3ff;
1293                                 }
1294                         }
1295                 }
1296         } else {
1297                 prom_printf("Processor is not HMT capable\n");
1298         }
1299 #endif
1300
1301         if (cpuid > NR_CPUS)
1302                 prom_printf("WARNING: maximum CPUs (" __stringify(NR_CPUS)
1303                             ") exceeded: ignoring extras\n");
1304
1305         prom_debug("prom_hold_cpus: end...\n");
1306 #endif
1307 }
1308
1309
1310 static void __init prom_init_client_services(unsigned long pp)
1311 {
1312         struct prom_t *_prom = &RELOC(prom);
1313
1314         /* Get a handle to the prom entry point before anything else */
1315         RELOC(prom_entry) = pp;
1316
1317         /* get a handle for the stdout device */
1318         _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1319         if (!PHANDLE_VALID(_prom->chosen))
1320                 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1321
1322         /* get device tree root */
1323         _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1324         if (!PHANDLE_VALID(_prom->root))
1325                 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1326 }
1327
1328 static void __init prom_init_stdout(void)
1329 {
1330         struct prom_t *_prom = &RELOC(prom);
1331         char *path = RELOC(of_stdout_device);
1332         char type[16];
1333         u32 val;
1334
1335         if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1336                 prom_panic("cannot find stdout");
1337
1338         _prom->stdout = val;
1339
1340         /* Get the full OF pathname of the stdout device */
1341         memset(path, 0, 256);
1342         call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1343         val = call_prom("instance-to-package", 1, 1, _prom->stdout);
1344         prom_setprop(_prom->chosen, "linux,stdout-package", &val, sizeof(val));
1345         prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
1346         prom_setprop(_prom->chosen, "linux,stdout-path",
1347                      RELOC(of_stdout_device), strlen(RELOC(of_stdout_device))+1);
1348
1349         /* If it's a display, note it */
1350         memset(type, 0, sizeof(type));
1351         prom_getprop(val, "device_type", type, sizeof(type));
1352         if (strcmp(type, RELOC("display")) == 0)
1353                 prom_setprop(val, "linux,boot-display", NULL, 0);
1354 }
1355
1356 static void __init prom_close_stdin(void)
1357 {
1358         struct prom_t *_prom = &RELOC(prom);
1359         ihandle val;
1360
1361         if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1362                 call_prom("close", 1, 0, val);
1363 }
1364
1365 static int __init prom_find_machine_type(void)
1366 {
1367         struct prom_t *_prom = &RELOC(prom);
1368         char compat[256];
1369         int len, i = 0;
1370         phandle rtas;
1371
1372         len = prom_getprop(_prom->root, "compatible",
1373                            compat, sizeof(compat)-1);
1374         if (len > 0) {
1375                 compat[len] = 0;
1376                 while (i < len) {
1377                         char *p = &compat[i];
1378                         int sl = strlen(p);
1379                         if (sl == 0)
1380                                 break;
1381                         if (strstr(p, RELOC("Power Macintosh")) ||
1382                             strstr(p, RELOC("MacRISC4")))
1383                                 return PLATFORM_POWERMAC;
1384 #ifdef CONFIG_PPC64
1385                         if (strstr(p, RELOC("Momentum,Maple")))
1386                                 return PLATFORM_MAPLE;
1387 #endif
1388                         i += sl + 1;
1389                 }
1390         }
1391 #ifdef CONFIG_PPC64
1392         /* Default to pSeries. We need to know if we are running LPAR */
1393         rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1394         if (PHANDLE_VALID(rtas)) {
1395                 int x = prom_getproplen(rtas, "ibm,hypertas-functions");
1396                 if (x != PROM_ERROR) {
1397                         prom_printf("Hypertas detected, assuming LPAR !\n");
1398                         return PLATFORM_PSERIES_LPAR;
1399                 }
1400         }
1401         return PLATFORM_PSERIES;
1402 #else
1403         return PLATFORM_CHRP;
1404 #endif
1405 }
1406
1407 static int __init setup_disp(phandle dp)
1408 {
1409 #if defined(CONFIG_BOOTX_TEXT) && defined(CONFIG_PPC32)
1410         int width = 640, height = 480, depth = 8, pitch;
1411         unsigned address;
1412         u32 addrs[8][5];
1413         int i, naddrs;
1414         char name[32];
1415         char *getprop = "getprop";
1416
1417         prom_printf("Initializing screen: ");
1418
1419         memset(name, 0, sizeof(name));
1420         call_prom(getprop, 4, 1, dp, "name", name, sizeof(name));
1421         name[sizeof(name)-1] = 0;
1422         prom_print(name);
1423         prom_print("\n");
1424         call_prom(getprop, 4, 1, dp, "width", &width, sizeof(width));
1425         call_prom(getprop, 4, 1, dp, "height", &height, sizeof(height));
1426         call_prom(getprop, 4, 1, dp, "depth", &depth, sizeof(depth));
1427         pitch = width * ((depth + 7) / 8);
1428         call_prom(getprop, 4, 1, dp, "linebytes",
1429                   &pitch, sizeof(pitch));
1430         if (pitch == 1)
1431                 pitch = 0x1000;         /* for strange IBM display */
1432         address = 0;
1433         call_prom(getprop, 4, 1, dp, "address", &address, sizeof(address));
1434         if (address == 0) {
1435                 /* look for an assigned address with a size of >= 1MB */
1436                 naddrs = call_prom(getprop, 4, 1, dp, "assigned-addresses",
1437                                    addrs, sizeof(addrs));
1438                 naddrs /= 20;
1439                 for (i = 0; i < naddrs; ++i) {
1440                         if (addrs[i][4] >= (1 << 20)) {
1441                                 address = addrs[i][2];
1442                                 /* use the BE aperture if possible */
1443                                 if (addrs[i][4] >= (16 << 20))
1444                                         address += (8 << 20);
1445                                 break;
1446                         }
1447                 }
1448                 if (address == 0) {
1449                         prom_print("Failed to get address\n");
1450                         return 0;
1451                 }
1452         }
1453         /* kludge for valkyrie */
1454         if (strcmp(name, "valkyrie") == 0)
1455                 address += 0x1000;
1456
1457         prom_printf("\n\n\n\naddress = %x\n", address);
1458         btext_setup_display(width, height, depth, pitch, address);
1459 #endif /* CONFIG_BOOTX_TEXT && CONFIG_PPC32 */
1460         return 1;
1461 }
1462
1463 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1464 {
1465         return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1466 }
1467
1468 /*
1469  * If we have a display that we don't know how to drive,
1470  * we will want to try to execute OF's open method for it
1471  * later.  However, OF will probably fall over if we do that
1472  * we've taken over the MMU.
1473  * So we check whether we will need to open the display,
1474  * and if so, open it now.
1475  */
1476 static void __init prom_check_displays(void)
1477 {
1478         char type[16], *path;
1479         phandle node;
1480         ihandle ih;
1481         int i;
1482         int got_display = 0;
1483
1484         static unsigned char default_colors[] = {
1485                 0x00, 0x00, 0x00,
1486                 0x00, 0x00, 0xaa,
1487                 0x00, 0xaa, 0x00,
1488                 0x00, 0xaa, 0xaa,
1489                 0xaa, 0x00, 0x00,
1490                 0xaa, 0x00, 0xaa,
1491                 0xaa, 0xaa, 0x00,
1492                 0xaa, 0xaa, 0xaa,
1493                 0x55, 0x55, 0x55,
1494                 0x55, 0x55, 0xff,
1495                 0x55, 0xff, 0x55,
1496                 0x55, 0xff, 0xff,
1497                 0xff, 0x55, 0x55,
1498                 0xff, 0x55, 0xff,
1499                 0xff, 0xff, 0x55,
1500                 0xff, 0xff, 0xff
1501         };
1502         const unsigned char *clut;
1503
1504         prom_printf("Looking for displays\n");
1505         for (node = 0; prom_next_node(&node); ) {
1506                 memset(type, 0, sizeof(type));
1507                 prom_getprop(node, "device_type", type, sizeof(type));
1508                 if (strcmp(type, RELOC("display")) != 0)
1509                         continue;
1510
1511                 /* It seems OF doesn't null-terminate the path :-( */
1512                 path = RELOC(prom_scratch);
1513                 memset(path, 0, PROM_SCRATCH_SIZE);
1514
1515                 /*
1516                  * leave some room at the end of the path for appending extra
1517                  * arguments
1518                  */
1519                 if (call_prom("package-to-path", 3, 1, node, path,
1520                               PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1521                         continue;
1522                 prom_printf("found display   : %s, opening ... ", path);
1523                 
1524                 ih = call_prom("open", 1, 1, path);
1525                 if (ih == 0) {
1526                         prom_printf("failed\n");
1527                         continue;
1528                 }
1529
1530                 /* Success */
1531                 prom_printf("done\n");
1532                 prom_setprop(node, "linux,opened", NULL, 0);
1533
1534                 /* Setup a usable color table when the appropriate
1535                  * method is available. Should update this to set-colors */
1536                 clut = RELOC(default_colors);
1537                 for (i = 0; i < 32; i++, clut += 3)
1538                         if (prom_set_color(ih, i, clut[0], clut[1],
1539                                            clut[2]) != 0)
1540                                 break;
1541
1542 #ifdef CONFIG_LOGO_LINUX_CLUT224
1543                 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1544                 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1545                         if (prom_set_color(ih, i + 32, clut[0], clut[1],
1546                                            clut[2]) != 0)
1547                                 break;
1548 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
1549                 if (!got_display)
1550                         got_display = setup_disp(node);
1551         }
1552 }
1553
1554
1555 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1556 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
1557                               unsigned long needed, unsigned long align)
1558 {
1559         void *ret;
1560
1561         *mem_start = _ALIGN(*mem_start, align);
1562         while ((*mem_start + needed) > *mem_end) {
1563                 unsigned long room, chunk;
1564
1565                 prom_debug("Chunk exhausted, claiming more at %x...\n",
1566                            RELOC(alloc_bottom));
1567                 room = RELOC(alloc_top) - RELOC(alloc_bottom);
1568                 if (room > DEVTREE_CHUNK_SIZE)
1569                         room = DEVTREE_CHUNK_SIZE;
1570                 if (room < PAGE_SIZE)
1571                         prom_panic("No memory for flatten_device_tree (no room)");
1572                 chunk = alloc_up(room, 0);
1573                 if (chunk == 0)
1574                         prom_panic("No memory for flatten_device_tree (claim failed)");
1575                 *mem_end = RELOC(alloc_top);
1576         }
1577
1578         ret = (void *)*mem_start;
1579         *mem_start += needed;
1580
1581         return ret;
1582 }
1583
1584 #define dt_push_token(token, mem_start, mem_end) \
1585         do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
1586
1587 static unsigned long __init dt_find_string(char *str)
1588 {
1589         char *s, *os;
1590
1591         s = os = (char *)RELOC(dt_string_start);
1592         s += 4;
1593         while (s <  (char *)RELOC(dt_string_end)) {
1594                 if (strcmp(s, str) == 0)
1595                         return s - os;
1596                 s += strlen(s) + 1;
1597         }
1598         return 0;
1599 }
1600
1601 /*
1602  * The Open Firmware 1275 specification states properties must be 31 bytes or
1603  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
1604  */
1605 #define MAX_PROPERTY_NAME 64
1606
1607 static void __init scan_dt_build_strings(phandle node,
1608                                          unsigned long *mem_start,
1609                                          unsigned long *mem_end)
1610 {
1611         char *prev_name, *namep, *sstart;
1612         unsigned long soff;
1613         phandle child;
1614
1615         sstart =  (char *)RELOC(dt_string_start);
1616
1617         /* get and store all property names */
1618         prev_name = RELOC("");
1619         for (;;) {
1620                 /* 64 is max len of name including nul. */
1621                 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
1622                 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
1623                         /* No more nodes: unwind alloc */
1624                         *mem_start = (unsigned long)namep;
1625                         break;
1626                 }
1627
1628                 /* skip "name" */
1629                 if (strcmp(namep, RELOC("name")) == 0) {
1630                         *mem_start = (unsigned long)namep;
1631                         prev_name = RELOC("name");
1632                         continue;
1633                 }
1634                 /* get/create string entry */
1635                 soff = dt_find_string(namep);
1636                 if (soff != 0) {
1637                         *mem_start = (unsigned long)namep;
1638                         namep = sstart + soff;
1639                 } else {
1640                         /* Trim off some if we can */
1641                         *mem_start = (unsigned long)namep + strlen(namep) + 1;
1642                         RELOC(dt_string_end) = *mem_start;
1643                 }
1644                 prev_name = namep;
1645         }
1646
1647         /* do all our children */
1648         child = call_prom("child", 1, 1, node);
1649         while (child != 0) {
1650                 scan_dt_build_strings(child, mem_start, mem_end);
1651                 child = call_prom("peer", 1, 1, child);
1652         }
1653 }
1654
1655 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
1656                                         unsigned long *mem_end)
1657 {
1658         phandle child;
1659         char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
1660         unsigned long soff;
1661         unsigned char *valp;
1662         static char pname[MAX_PROPERTY_NAME];
1663         int l;
1664
1665         dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
1666
1667         /* get the node's full name */
1668         namep = (char *)*mem_start;
1669         l = call_prom("package-to-path", 3, 1, node,
1670                       namep, *mem_end - *mem_start);
1671         if (l >= 0) {
1672                 /* Didn't fit?  Get more room. */
1673                 if ((l+1) > (*mem_end - *mem_start)) {
1674                         namep = make_room(mem_start, mem_end, l+1, 1);
1675                         call_prom("package-to-path", 3, 1, node, namep, l);
1676                 }
1677                 namep[l] = '\0';
1678
1679                 /* Fixup an Apple bug where they have bogus \0 chars in the
1680                  * middle of the path in some properties
1681                  */
1682                 for (p = namep, ep = namep + l; p < ep; p++)
1683                         if (*p == '\0') {
1684                                 memmove(p, p+1, ep - p);
1685                                 ep--; l--; p--;
1686                         }
1687
1688                 /* now try to extract the unit name in that mess */
1689                 for (p = namep, lp = NULL; *p; p++)
1690                         if (*p == '/')
1691                                 lp = p + 1;
1692                 if (lp != NULL)
1693                         memmove(namep, lp, strlen(lp) + 1);
1694                 *mem_start = _ALIGN(((unsigned long) namep) +
1695                                     strlen(namep) + 1, 4);
1696         }
1697
1698         /* get it again for debugging */
1699         path = RELOC(prom_scratch);
1700         memset(path, 0, PROM_SCRATCH_SIZE);
1701         call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1702
1703         /* get and store all properties */
1704         prev_name = RELOC("");
1705         sstart = (char *)RELOC(dt_string_start);
1706         for (;;) {
1707                 if (call_prom("nextprop", 3, 1, node, prev_name,
1708                               RELOC(pname)) != 1)
1709                         break;
1710
1711                 /* skip "name" */
1712                 if (strcmp(RELOC(pname), RELOC("name")) == 0) {
1713                         prev_name = RELOC("name");
1714                         continue;
1715                 }
1716
1717                 /* find string offset */
1718                 soff = dt_find_string(RELOC(pname));
1719                 if (soff == 0) {
1720                         prom_printf("WARNING: Can't find string index for"
1721                                     " <%s>, node %s\n", RELOC(pname), path);
1722                         break;
1723                 }
1724                 prev_name = sstart + soff;
1725
1726                 /* get length */
1727                 l = call_prom("getproplen", 2, 1, node, RELOC(pname));
1728
1729                 /* sanity checks */
1730                 if (l == PROM_ERROR)
1731                         continue;
1732                 if (l > MAX_PROPERTY_LENGTH) {
1733                         prom_printf("WARNING: ignoring large property ");
1734                         /* It seems OF doesn't null-terminate the path :-( */
1735                         prom_printf("[%s] ", path);
1736                         prom_printf("%s length 0x%x\n", RELOC(pname), l);
1737                         continue;
1738                 }
1739
1740                 /* push property head */
1741                 dt_push_token(OF_DT_PROP, mem_start, mem_end);
1742                 dt_push_token(l, mem_start, mem_end);
1743                 dt_push_token(soff, mem_start, mem_end);
1744
1745                 /* push property content */
1746                 valp = make_room(mem_start, mem_end, l, 4);
1747                 call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
1748                 *mem_start = _ALIGN(*mem_start, 4);
1749         }
1750
1751         /* Add a "linux,phandle" property. */
1752         soff = dt_find_string(RELOC("linux,phandle"));
1753         if (soff == 0)
1754                 prom_printf("WARNING: Can't find string index for"
1755                             " <linux-phandle> node %s\n", path);
1756         else {
1757                 dt_push_token(OF_DT_PROP, mem_start, mem_end);
1758                 dt_push_token(4, mem_start, mem_end);
1759                 dt_push_token(soff, mem_start, mem_end);
1760                 valp = make_room(mem_start, mem_end, 4, 4);
1761                 *(u32 *)valp = node;
1762         }
1763
1764         /* do all our children */
1765         child = call_prom("child", 1, 1, node);
1766         while (child != 0) {
1767                 scan_dt_build_struct(child, mem_start, mem_end);
1768                 child = call_prom("peer", 1, 1, child);
1769         }
1770
1771         dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
1772 }
1773
1774 static void __init flatten_device_tree(void)
1775 {
1776         phandle root;
1777         unsigned long mem_start, mem_end, room;
1778         struct boot_param_header *hdr;
1779         struct prom_t *_prom = &RELOC(prom);
1780         char *namep;
1781         u64 *rsvmap;
1782
1783         /*
1784          * Check how much room we have between alloc top & bottom (+/- a
1785          * few pages), crop to 4Mb, as this is our "chuck" size
1786          */
1787         room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
1788         if (room > DEVTREE_CHUNK_SIZE)
1789                 room = DEVTREE_CHUNK_SIZE;
1790         prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
1791
1792         /* Now try to claim that */
1793         mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
1794         if (mem_start == 0)
1795                 prom_panic("Can't allocate initial device-tree chunk\n");
1796         mem_end = RELOC(alloc_top);
1797
1798         /* Get root of tree */
1799         root = call_prom("peer", 1, 1, (phandle)0);
1800         if (root == (phandle)0)
1801                 prom_panic ("couldn't get device tree root\n");
1802
1803         /* Build header and make room for mem rsv map */ 
1804         mem_start = _ALIGN(mem_start, 4);
1805         hdr = make_room(&mem_start, &mem_end,
1806                         sizeof(struct boot_param_header), 4);
1807         RELOC(dt_header_start) = (unsigned long)hdr;
1808         rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
1809
1810         /* Start of strings */
1811         mem_start = PAGE_ALIGN(mem_start);
1812         RELOC(dt_string_start) = mem_start;
1813         mem_start += 4; /* hole */
1814
1815         /* Add "linux,phandle" in there, we'll need it */
1816         namep = make_room(&mem_start, &mem_end, 16, 1);
1817         strcpy(namep, RELOC("linux,phandle"));
1818         mem_start = (unsigned long)namep + strlen(namep) + 1;
1819
1820         /* Build string array */
1821         prom_printf("Building dt strings...\n"); 
1822         scan_dt_build_strings(root, &mem_start, &mem_end);
1823         RELOC(dt_string_end) = mem_start;
1824
1825         /* Build structure */
1826         mem_start = PAGE_ALIGN(mem_start);
1827         RELOC(dt_struct_start) = mem_start;
1828         prom_printf("Building dt structure...\n"); 
1829         scan_dt_build_struct(root, &mem_start, &mem_end);
1830         dt_push_token(OF_DT_END, &mem_start, &mem_end);
1831         RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
1832
1833         /* Finish header */
1834         hdr->boot_cpuid_phys = _prom->cpu;
1835         hdr->magic = OF_DT_HEADER;
1836         hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
1837         hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
1838         hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
1839         hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
1840         hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
1841         hdr->version = OF_DT_VERSION;
1842         /* Version 16 is not backward compatible */
1843         hdr->last_comp_version = 0x10;
1844
1845         /* Reserve the whole thing and copy the reserve map in, we
1846          * also bump mem_reserve_cnt to cause further reservations to
1847          * fail since it's too late.
1848          */
1849         reserve_mem(RELOC(dt_header_start), hdr->totalsize);
1850         memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
1851
1852 #ifdef DEBUG_PROM
1853         {
1854                 int i;
1855                 prom_printf("reserved memory map:\n");
1856                 for (i = 0; i < RELOC(mem_reserve_cnt); i++)
1857                         prom_printf("  %x - %x\n",
1858                                     RELOC(mem_reserve_map)[i].base,
1859                                     RELOC(mem_reserve_map)[i].size);
1860         }
1861 #endif
1862         RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
1863
1864         prom_printf("Device tree strings 0x%x -> 0x%x\n",
1865                     RELOC(dt_string_start), RELOC(dt_string_end)); 
1866         prom_printf("Device tree struct  0x%x -> 0x%x\n",
1867                     RELOC(dt_struct_start), RELOC(dt_struct_end));
1868
1869 }
1870
1871
1872 static void __init fixup_device_tree(void)
1873 {
1874 #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
1875         phandle u3, i2c, mpic;
1876         u32 u3_rev;
1877         u32 interrupts[2];
1878         u32 parent;
1879
1880         /* Some G5s have a missing interrupt definition, fix it up here */
1881         u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
1882         if (!PHANDLE_VALID(u3))
1883                 return;
1884         i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
1885         if (!PHANDLE_VALID(i2c))
1886                 return;
1887         mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
1888         if (!PHANDLE_VALID(mpic))
1889                 return;
1890
1891         /* check if proper rev of u3 */
1892         if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
1893             == PROM_ERROR)
1894                 return;
1895         if (u3_rev != 0x35 && u3_rev != 0x37)
1896                 return;
1897         /* does it need fixup ? */
1898         if (prom_getproplen(i2c, "interrupts") > 0)
1899                 return;
1900
1901         prom_printf("fixing up bogus interrupts for u3 i2c...\n");
1902
1903         /* interrupt on this revision of u3 is number 0 and level */
1904         interrupts[0] = 0;
1905         interrupts[1] = 1;
1906         prom_setprop(i2c, "interrupts", &interrupts, sizeof(interrupts));
1907         parent = (u32)mpic;
1908         prom_setprop(i2c, "interrupt-parent", &parent, sizeof(parent));
1909 #endif
1910 }
1911
1912
1913 static void __init prom_find_boot_cpu(void)
1914 {
1915         struct prom_t *_prom = &RELOC(prom);
1916         u32 getprop_rval;
1917         ihandle prom_cpu;
1918         phandle cpu_pkg;
1919
1920         if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
1921                 prom_panic("cannot find boot cpu");
1922
1923         cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
1924
1925         prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
1926         _prom->cpu = getprop_rval;
1927
1928         prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
1929 }
1930
1931 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
1932 {
1933 #ifdef CONFIG_BLK_DEV_INITRD
1934         struct prom_t *_prom = &RELOC(prom);
1935
1936         if (r3 && r4 && r4 != 0xdeadbeef) {
1937                 unsigned long val;
1938
1939                 RELOC(prom_initrd_start) = (r3 >= KERNELBASE) ? __pa(r3) : r3;
1940                 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
1941
1942                 val = RELOC(prom_initrd_start);
1943                 prom_setprop(_prom->chosen, "linux,initrd-start", &val,
1944                              sizeof(val));
1945                 val = RELOC(prom_initrd_end);
1946                 prom_setprop(_prom->chosen, "linux,initrd-end", &val,
1947                              sizeof(val));
1948
1949                 reserve_mem(RELOC(prom_initrd_start),
1950                             RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
1951
1952                 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
1953                 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
1954         }
1955 #endif /* CONFIG_BLK_DEV_INITRD */
1956 }
1957
1958 /*
1959  * We enter here early on, when the Open Firmware prom is still
1960  * handling exceptions and the MMU hash table for us.
1961  */
1962
1963 unsigned long __init prom_init(unsigned long r3, unsigned long r4,
1964                                unsigned long pp,
1965                                unsigned long r6, unsigned long r7)
1966 {       
1967         struct prom_t *_prom;
1968         extern char _stext[];
1969         unsigned long hdr;
1970         u32 getprop_rval;
1971         unsigned long offset = reloc_offset();
1972
1973 #ifdef CONFIG_PPC32
1974         reloc_got2(offset);
1975 #endif
1976
1977         _prom = &RELOC(prom);
1978
1979         /*
1980          * First zero the BSS
1981          */
1982         memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
1983
1984         /*
1985          * Init interface to Open Firmware, get some node references,
1986          * like /chosen
1987          */
1988         prom_init_client_services(pp);
1989
1990         /*
1991          * Init prom stdout device
1992          */
1993         prom_init_stdout();
1994
1995         /*
1996          * Check for an initrd
1997          */
1998         prom_check_initrd(r3, r4);
1999
2000         /*
2001          * Get default machine type. At this point, we do not differentiate
2002          * between pSeries SMP and pSeries LPAR
2003          */
2004         RELOC(of_platform) = prom_find_machine_type();
2005         getprop_rval = RELOC(of_platform);
2006         prom_setprop(_prom->chosen, "linux,platform",
2007                      &getprop_rval, sizeof(getprop_rval));
2008
2009 #ifdef CONFIG_PPC_PSERIES
2010         /*
2011          * On pSeries, inform the firmware about our capabilities
2012          */
2013         if (RELOC(of_platform) & PLATFORM_PSERIES)
2014                 prom_send_capabilities();
2015 #endif
2016
2017 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_BPA)
2018         /*
2019          * On pSeries and BPA, copy the CPU hold code
2020          */
2021         if (RELOC(of_platform) & (PLATFORM_PSERIES | PLATFORM_BPA))
2022                 copy_and_flush(0, KERNELBASE - offset, 0x100, 0);
2023 #endif
2024
2025         /*
2026          * Do early parsing of command line
2027          */
2028         early_cmdline_parse();
2029
2030         /*
2031          * Initialize memory management within prom_init
2032          */
2033         prom_init_mem();
2034
2035         /*
2036          * Determine which cpu is actually running right _now_
2037          */
2038         prom_find_boot_cpu();
2039
2040         /* 
2041          * Initialize display devices
2042          */
2043         prom_check_displays();
2044
2045 #ifdef CONFIG_PPC64
2046         /*
2047          * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2048          * that uses the allocator, we need to make sure we get the top of memory
2049          * available for us here...
2050          */
2051         if (RELOC(of_platform) == PLATFORM_PSERIES)
2052                 prom_initialize_tce_table();
2053 #endif
2054
2055         /*
2056          * On non-powermacs, try to instantiate RTAS and puts all CPUs
2057          * in spin-loops. PowerMacs don't have a working RTAS and use
2058          * a different way to spin CPUs
2059          */
2060         if (RELOC(of_platform) != PLATFORM_POWERMAC) {
2061                 prom_instantiate_rtas();
2062                 prom_hold_cpus();
2063         }
2064
2065         /*
2066          * Fill in some infos for use by the kernel later on
2067          */
2068         if (RELOC(prom_memory_limit))
2069                 prom_setprop(_prom->chosen, "linux,memory-limit",
2070                              &RELOC(prom_memory_limit),
2071                              sizeof(prom_memory_limit));
2072 #ifdef CONFIG_PPC64
2073         if (RELOC(ppc64_iommu_off))
2074                 prom_setprop(_prom->chosen, "linux,iommu-off", NULL, 0);
2075
2076         if (RELOC(iommu_force_on))
2077                 prom_setprop(_prom->chosen, "linux,iommu-force-on", NULL, 0);
2078
2079         if (RELOC(prom_tce_alloc_start)) {
2080                 prom_setprop(_prom->chosen, "linux,tce-alloc-start",
2081                              &RELOC(prom_tce_alloc_start),
2082                              sizeof(prom_tce_alloc_start));
2083                 prom_setprop(_prom->chosen, "linux,tce-alloc-end",
2084                              &RELOC(prom_tce_alloc_end),
2085                              sizeof(prom_tce_alloc_end));
2086         }
2087 #endif
2088
2089         /*
2090          * Fixup any known bugs in the device-tree
2091          */
2092         fixup_device_tree();
2093
2094         /*
2095          * Now finally create the flattened device-tree
2096          */
2097         prom_printf("copying OF device tree ...\n");
2098         flatten_device_tree();
2099
2100         /* in case stdin is USB and still active on IBM machines... */
2101         prom_close_stdin();
2102
2103         /*
2104          * Call OF "quiesce" method to shut down pending DMA's from
2105          * devices etc...
2106          */
2107         prom_printf("Calling quiesce ...\n");
2108         call_prom("quiesce", 0, 0);
2109
2110         /*
2111          * And finally, call the kernel passing it the flattened device
2112          * tree and NULL as r5, thus triggering the new entry point which
2113          * is common to us and kexec
2114          */
2115         hdr = RELOC(dt_header_start);
2116         prom_printf("returning from prom_init\n");
2117         prom_debug("->dt_header_start=0x%x\n", hdr);
2118
2119 #ifdef CONFIG_PPC32
2120         reloc_got2(-offset);
2121 #endif
2122
2123         __start(hdr, 0, 0);
2124
2125         return 0;
2126 }