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