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