Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[pandora-kernel.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/kernel.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
22 #include <linux/threads.h>
23 #include <linux/spinlock.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/proc_fs.h>
27 #include <linux/stringify.h>
28 #include <linux/delay.h>
29 #include <linux/initrd.h>
30 #include <linux/bitops.h>
31 #include <asm/prom.h>
32 #include <asm/rtas.h>
33 #include <asm/page.h>
34 #include <asm/processor.h>
35 #include <asm/irq.h>
36 #include <asm/io.h>
37 #include <asm/smp.h>
38 #include <asm/system.h>
39 #include <asm/mmu.h>
40 #include <asm/pgtable.h>
41 #include <asm/pci.h>
42 #include <asm/iommu.h>
43 #include <asm/btext.h>
44 #include <asm/sections.h>
45 #include <asm/machdep.h>
46
47 #include <linux/linux_logo.h>
48
49 /*
50  * Properties whose value is longer than this get excluded from our
51  * copy of the device tree. This value does need to be big enough to
52  * ensure that we don't lose things like the interrupt-map property
53  * on a PCI-PCI bridge.
54  */
55 #define MAX_PROPERTY_LENGTH     (1UL * 1024 * 1024)
56
57 /*
58  * Eventually bump that one up
59  */
60 #define DEVTREE_CHUNK_SIZE      0x100000
61
62 /*
63  * This is the size of the local memory reserve map that gets copied
64  * into the boot params passed to the kernel. That size is totally
65  * flexible as the kernel just reads the list until it encounters an
66  * entry with size 0, so it can be changed without breaking binary
67  * compatibility
68  */
69 #define MEM_RESERVE_MAP_SIZE    8
70
71 /*
72  * prom_init() is called very early on, before the kernel text
73  * and data have been mapped to KERNELBASE.  At this point the code
74  * is running at whatever address it has been loaded at.
75  * On ppc32 we compile with -mrelocatable, which means that references
76  * to extern and static variables get relocated automatically.
77  * On ppc64 we have to relocate the references explicitly with
78  * RELOC.  (Note that strings count as static variables.)
79  *
80  * Because OF may have mapped I/O devices into the area starting at
81  * KERNELBASE, particularly on CHRP machines, we can't safely call
82  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
83  * OF calls must be done within prom_init().
84  *
85  * ADDR is used in calls to call_prom.  The 4th and following
86  * arguments to call_prom should be 32-bit values.
87  * On ppc64, 64 bit values are truncated to 32 bits (and
88  * fortunately don't get interpreted as two arguments).
89  */
90 #ifdef CONFIG_PPC64
91 #define RELOC(x)        (*PTRRELOC(&(x)))
92 #define ADDR(x)         (u32) add_reloc_offset((unsigned long)(x))
93 #define OF_WORKAROUNDS  0
94 #else
95 #define RELOC(x)        (x)
96 #define ADDR(x)         (u32) (x)
97 #define OF_WORKAROUNDS  of_workarounds
98 int of_workarounds;
99 #endif
100
101 #define OF_WA_CLAIM     1       /* do phys/virt claim separately, then map */
102 #define OF_WA_LONGTRAIL 2       /* work around longtrail bugs */
103
104 #define PROM_BUG() do {                                         \
105         prom_printf("kernel BUG at %s line 0x%x!\n",            \
106                     RELOC(__FILE__), __LINE__);                 \
107         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);       \
108 } while (0)
109
110 #ifdef DEBUG_PROM
111 #define prom_debug(x...)        prom_printf(x)
112 #else
113 #define prom_debug(x...)
114 #endif
115
116
117 typedef u32 prom_arg_t;
118
119 struct prom_args {
120         u32 service;
121         u32 nargs;
122         u32 nret;
123         prom_arg_t args[10];
124 };
125
126 struct prom_t {
127         ihandle root;
128         phandle chosen;
129         int cpu;
130         ihandle stdout;
131         ihandle mmumap;
132         ihandle memory;
133 };
134
135 struct mem_map_entry {
136         u64     base;
137         u64     size;
138 };
139
140 typedef u32 cell_t;
141
142 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
143
144 #ifdef CONFIG_PPC64
145 extern int enter_prom(struct prom_args *args, unsigned long entry);
146 #else
147 static inline int enter_prom(struct prom_args *args, unsigned long entry)
148 {
149         return ((int (*)(struct prom_args *))entry)(args);
150 }
151 #endif
152
153 extern void copy_and_flush(unsigned long dest, unsigned long src,
154                            unsigned long size, unsigned long offset);
155
156 /* prom structure */
157 static struct prom_t __initdata prom;
158
159 static unsigned long prom_entry __initdata;
160
161 #define PROM_SCRATCH_SIZE 256
162
163 static char __initdata of_stdout_device[256];
164 static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
165
166 static unsigned long __initdata dt_header_start;
167 static unsigned long __initdata dt_struct_start, dt_struct_end;
168 static unsigned long __initdata dt_string_start, dt_string_end;
169
170 static unsigned long __initdata prom_initrd_start, prom_initrd_end;
171
172 #ifdef CONFIG_PPC64
173 static int __initdata prom_iommu_force_on;
174 static int __initdata prom_iommu_off;
175 static unsigned long __initdata prom_tce_alloc_start;
176 static unsigned long __initdata prom_tce_alloc_end;
177 #endif
178
179 /* Platforms codes are now obsolete in the kernel. Now only used within this
180  * file and ultimately gone too. Feel free to change them if you need, they
181  * are not shared with anything outside of this file anymore
182  */
183 #define PLATFORM_PSERIES        0x0100
184 #define PLATFORM_PSERIES_LPAR   0x0101
185 #define PLATFORM_LPAR           0x0001
186 #define PLATFORM_POWERMAC       0x0400
187 #define PLATFORM_GENERIC        0x0500
188
189 static int __initdata of_platform;
190
191 static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
192
193 static unsigned long __initdata alloc_top;
194 static unsigned long __initdata alloc_top_high;
195 static unsigned long __initdata alloc_bottom;
196 static unsigned long __initdata rmo_top;
197 static unsigned long __initdata ram_top;
198
199 static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
200 static int __initdata mem_reserve_cnt;
201
202 static cell_t __initdata regbuf[1024];
203
204
205 /*
206  * Error results ... some OF calls will return "-1" on error, some
207  * will return 0, some will return either. To simplify, here are
208  * macros to use with any ihandle or phandle return value to check if
209  * it is valid
210  */
211
212 #define PROM_ERROR              (-1u)
213 #define PHANDLE_VALID(p)        ((p) != 0 && (p) != PROM_ERROR)
214 #define IHANDLE_VALID(i)        ((i) != 0 && (i) != PROM_ERROR)
215
216
217 /* This is the one and *ONLY* place where we actually call open
218  * firmware.
219  */
220
221 static int __init call_prom(const char *service, int nargs, int nret, ...)
222 {
223         int i;
224         struct prom_args args;
225         va_list list;
226
227         args.service = ADDR(service);
228         args.nargs = nargs;
229         args.nret = nret;
230
231         va_start(list, nret);
232         for (i = 0; i < nargs; i++)
233                 args.args[i] = va_arg(list, prom_arg_t);
234         va_end(list);
235
236         for (i = 0; i < nret; i++)
237                 args.args[nargs+i] = 0;
238
239         if (enter_prom(&args, RELOC(prom_entry)) < 0)
240                 return PROM_ERROR;
241
242         return (nret > 0) ? args.args[nargs] : 0;
243 }
244
245 static int __init call_prom_ret(const char *service, int nargs, int nret,
246                                 prom_arg_t *rets, ...)
247 {
248         int i;
249         struct prom_args args;
250         va_list list;
251
252         args.service = ADDR(service);
253         args.nargs = nargs;
254         args.nret = nret;
255
256         va_start(list, rets);
257         for (i = 0; i < nargs; i++)
258                 args.args[i] = va_arg(list, prom_arg_t);
259         va_end(list);
260
261         for (i = 0; i < nret; i++)
262                 args.args[nargs+i] = 0;
263
264         if (enter_prom(&args, RELOC(prom_entry)) < 0)
265                 return PROM_ERROR;
266
267         if (rets != NULL)
268                 for (i = 1; i < nret; ++i)
269                         rets[i-1] = args.args[nargs+i];
270
271         return (nret > 0) ? args.args[nargs] : 0;
272 }
273
274
275 static void __init prom_print(const char *msg)
276 {
277         const char *p, *q;
278         struct prom_t *_prom = &RELOC(prom);
279
280         if (_prom->stdout == 0)
281                 return;
282
283         for (p = msg; *p != 0; p = q) {
284                 for (q = p; *q != 0 && *q != '\n'; ++q)
285                         ;
286                 if (q > p)
287                         call_prom("write", 3, 1, _prom->stdout, p, q - p);
288                 if (*q == 0)
289                         break;
290                 ++q;
291                 call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
292         }
293 }
294
295
296 static void __init prom_print_hex(unsigned long val)
297 {
298         int i, nibbles = sizeof(val)*2;
299         char buf[sizeof(val)*2+1];
300         struct prom_t *_prom = &RELOC(prom);
301
302         for (i = nibbles-1;  i >= 0;  i--) {
303                 buf[i] = (val & 0xf) + '0';
304                 if (buf[i] > '9')
305                         buf[i] += ('a'-'0'-10);
306                 val >>= 4;
307         }
308         buf[nibbles] = '\0';
309         call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
310 }
311
312
313 static void __init prom_printf(const char *format, ...)
314 {
315         const char *p, *q, *s;
316         va_list args;
317         unsigned long v;
318         struct prom_t *_prom = &RELOC(prom);
319
320         va_start(args, format);
321 #ifdef CONFIG_PPC64
322         format = PTRRELOC(format);
323 #endif
324         for (p = format; *p != 0; p = q) {
325                 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
326                         ;
327                 if (q > p)
328                         call_prom("write", 3, 1, _prom->stdout, p, q - p);
329                 if (*q == 0)
330                         break;
331                 if (*q == '\n') {
332                         ++q;
333                         call_prom("write", 3, 1, _prom->stdout,
334                                   ADDR("\r\n"), 2);
335                         continue;
336                 }
337                 ++q;
338                 if (*q == 0)
339                         break;
340                 switch (*q) {
341                 case 's':
342                         ++q;
343                         s = va_arg(args, const char *);
344                         prom_print(s);
345                         break;
346                 case 'x':
347                         ++q;
348                         v = va_arg(args, unsigned long);
349                         prom_print_hex(v);
350                         break;
351                 }
352         }
353 }
354
355
356 static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
357                                 unsigned long align)
358 {
359         struct prom_t *_prom = &RELOC(prom);
360
361         if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
362                 /*
363                  * Old OF requires we claim physical and virtual separately
364                  * and then map explicitly (assuming virtual mode)
365                  */
366                 int ret;
367                 prom_arg_t result;
368
369                 ret = call_prom_ret("call-method", 5, 2, &result,
370                                     ADDR("claim"), _prom->memory,
371                                     align, size, virt);
372                 if (ret != 0 || result == -1)
373                         return -1;
374                 ret = call_prom_ret("call-method", 5, 2, &result,
375                                     ADDR("claim"), _prom->mmumap,
376                                     align, size, virt);
377                 if (ret != 0) {
378                         call_prom("call-method", 4, 1, ADDR("release"),
379                                   _prom->memory, size, virt);
380                         return -1;
381                 }
382                 /* the 0x12 is M (coherence) + PP == read/write */
383                 call_prom("call-method", 6, 1,
384                           ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
385                 return virt;
386         }
387         return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
388                          (prom_arg_t)align);
389 }
390
391 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
392 {
393 #ifdef CONFIG_PPC64
394         reason = PTRRELOC(reason);
395 #endif
396         prom_print(reason);
397         /* Do not call exit because it clears the screen on pmac
398          * it also causes some sort of double-fault on early pmacs */
399         if (RELOC(of_platform) == PLATFORM_POWERMAC)
400                 asm("trap\n");
401
402         /* ToDo: should put up an SRC here on p/iSeries */
403         call_prom("exit", 0, 0);
404
405         for (;;)                        /* should never get here */
406                 ;
407 }
408
409
410 static int __init prom_next_node(phandle *nodep)
411 {
412         phandle node;
413
414         if ((node = *nodep) != 0
415             && (*nodep = call_prom("child", 1, 1, node)) != 0)
416                 return 1;
417         if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
418                 return 1;
419         for (;;) {
420                 if ((node = call_prom("parent", 1, 1, node)) == 0)
421                         return 0;
422                 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
423                         return 1;
424         }
425 }
426
427 static int inline prom_getprop(phandle node, const char *pname,
428                                void *value, size_t valuelen)
429 {
430         return call_prom("getprop", 4, 1, node, ADDR(pname),
431                          (u32)(unsigned long) value, (u32) valuelen);
432 }
433
434 static int inline prom_getproplen(phandle node, const char *pname)
435 {
436         return call_prom("getproplen", 2, 1, node, ADDR(pname));
437 }
438
439 static void add_string(char **str, const char *q)
440 {
441         char *p = *str;
442
443         while (*q)
444                 *p++ = *q++;
445         *p++ = ' ';
446         *str = p;
447 }
448
449 static char *tohex(unsigned int x)
450 {
451         static char digits[] = "0123456789abcdef";
452         static char result[9];
453         int i;
454
455         result[8] = 0;
456         i = 8;
457         do {
458                 --i;
459                 result[i] = digits[x & 0xf];
460                 x >>= 4;
461         } while (x != 0 && i > 0);
462         return &result[i];
463 }
464
465 static int __init prom_setprop(phandle node, const char *nodename,
466                                const char *pname, void *value, size_t valuelen)
467 {
468         char cmd[256], *p;
469
470         if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
471                 return call_prom("setprop", 4, 1, node, ADDR(pname),
472                                  (u32)(unsigned long) value, (u32) valuelen);
473
474         /* gah... setprop doesn't work on longtrail, have to use interpret */
475         p = cmd;
476         add_string(&p, "dev");
477         add_string(&p, nodename);
478         add_string(&p, tohex((u32)(unsigned long) value));
479         add_string(&p, tohex(valuelen));
480         add_string(&p, tohex(ADDR(pname)));
481         add_string(&p, tohex(strlen(RELOC(pname))));
482         add_string(&p, "property");
483         *p = 0;
484         return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
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 #ifdef CONFIG_PPC64
495         const char *opt;
496 #endif
497         char *p;
498         int l = 0;
499
500         RELOC(prom_cmd_line[0]) = 0;
501         p = RELOC(prom_cmd_line);
502         if ((long)_prom->chosen > 0)
503                 l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
504 #ifdef CONFIG_CMDLINE
505         if (l <= 0 || p[0] == '\0') /* dbl check */
506                 strlcpy(RELOC(prom_cmd_line),
507                         RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
508 #endif /* CONFIG_CMDLINE */
509         prom_printf("command line: %s\n", RELOC(prom_cmd_line));
510
511 #ifdef CONFIG_PPC64
512         opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
513         if (opt) {
514                 prom_printf("iommu opt is: %s\n", opt);
515                 opt += 6;
516                 while (*opt && *opt == ' ')
517                         opt++;
518                 if (!strncmp(opt, RELOC("off"), 3))
519                         RELOC(prom_iommu_off) = 1;
520                 else if (!strncmp(opt, RELOC("force"), 5))
521                         RELOC(prom_iommu_force_on) = 1;
522         }
523 #endif
524 }
525
526 #ifdef CONFIG_PPC_PSERIES
527 /*
528  * There are two methods for telling firmware what our capabilities are.
529  * Newer machines have an "ibm,client-architecture-support" method on the
530  * root node.  For older machines, we have to call the "process-elf-header"
531  * method in the /packages/elf-loader node, passing it a fake 32-bit
532  * ELF header containing a couple of PT_NOTE sections that contain
533  * structures that contain various information.
534  */
535
536 /*
537  * New method - extensible architecture description vector.
538  *
539  * Because the description vector contains a mix of byte and word
540  * values, we declare it as an unsigned char array, and use this
541  * macro to put word values in.
542  */
543 #define W(x)    ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
544                 ((x) >> 8) & 0xff, (x) & 0xff
545
546 /* Option vector bits - generic bits in byte 1 */
547 #define OV_IGNORE               0x80    /* ignore this vector */
548 #define OV_CESSATION_POLICY     0x40    /* halt if unsupported option present*/
549
550 /* Option vector 1: processor architectures supported */
551 #define OV1_PPC_2_00            0x80    /* set if we support PowerPC 2.00 */
552 #define OV1_PPC_2_01            0x40    /* set if we support PowerPC 2.01 */
553 #define OV1_PPC_2_02            0x20    /* set if we support PowerPC 2.02 */
554 #define OV1_PPC_2_03            0x10    /* set if we support PowerPC 2.03 */
555 #define OV1_PPC_2_04            0x08    /* set if we support PowerPC 2.04 */
556 #define OV1_PPC_2_05            0x04    /* set if we support PowerPC 2.05 */
557 #define OV1_PPC_2_06            0x02    /* set if we support PowerPC 2.06 */
558
559 /* Option vector 2: Open Firmware options supported */
560 #define OV2_REAL_MODE           0x20    /* set if we want OF in real mode */
561
562 /* Option vector 3: processor options supported */
563 #define OV3_FP                  0x80    /* floating point */
564 #define OV3_VMX                 0x40    /* VMX/Altivec */
565 #define OV3_DFP                 0x20    /* decimal FP */
566
567 /* Option vector 5: PAPR/OF options supported */
568 #define OV5_LPAR                0x80    /* logical partitioning supported */
569 #define OV5_SPLPAR              0x40    /* shared-processor LPAR supported */
570 /* ibm,dynamic-reconfiguration-memory property supported */
571 #define OV5_DRCONF_MEMORY       0x20
572 #define OV5_LARGE_PAGES         0x10    /* large pages supported */
573 #define OV5_DONATE_DEDICATE_CPU 0x02    /* donate dedicated CPU support */
574 /* PCIe/MSI support.  Without MSI full PCIe is not supported */
575 #ifdef CONFIG_PCI_MSI
576 #define OV5_MSI                 0x01    /* PCIe/MSI support */
577 #else
578 #define OV5_MSI                 0x00
579 #endif /* CONFIG_PCI_MSI */
580 #ifdef CONFIG_PPC_SMLPAR
581 #define OV5_CMO                 0x80    /* Cooperative Memory Overcommitment */
582 #else
583 #define OV5_CMO                 0x00
584 #endif
585
586 /*
587  * The architecture vector has an array of PVR mask/value pairs,
588  * followed by # option vectors - 1, followed by the option vectors.
589  */
590 static unsigned char ibm_architecture_vec[] = {
591         W(0xfffe0000), W(0x003a0000),   /* POWER5/POWER5+ */
592         W(0xffff0000), W(0x003e0000),   /* POWER6 */
593         W(0xffff0000), W(0x003f0000),   /* POWER7 */
594         W(0xffffffff), W(0x0f000003),   /* all 2.06-compliant */
595         W(0xffffffff), W(0x0f000002),   /* all 2.05-compliant */
596         W(0xfffffffe), W(0x0f000001),   /* all 2.04-compliant and earlier */
597         5 - 1,                          /* 5 option vectors */
598
599         /* option vector 1: processor architectures supported */
600         3 - 2,                          /* length */
601         0,                              /* don't ignore, don't halt */
602         OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
603         OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
604
605         /* option vector 2: Open Firmware options supported */
606         34 - 2,                         /* length */
607         OV2_REAL_MODE,
608         0, 0,
609         W(0xffffffff),                  /* real_base */
610         W(0xffffffff),                  /* real_size */
611         W(0xffffffff),                  /* virt_base */
612         W(0xffffffff),                  /* virt_size */
613         W(0xffffffff),                  /* load_base */
614         W(64),                          /* 64MB min RMA */
615         W(0xffffffff),                  /* full client load */
616         0,                              /* min RMA percentage of total RAM */
617         48,                             /* max log_2(hash table size) */
618
619         /* option vector 3: processor options supported */
620         3 - 2,                          /* length */
621         0,                              /* don't ignore, don't halt */
622         OV3_FP | OV3_VMX | OV3_DFP,
623
624         /* option vector 4: IBM PAPR implementation */
625         2 - 2,                          /* length */
626         0,                              /* don't halt */
627
628         /* option vector 5: PAPR/OF options */
629         5 - 2,                          /* length */
630         0,                              /* don't ignore, don't halt */
631         OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
632         OV5_DONATE_DEDICATE_CPU | OV5_MSI,
633         0,
634         OV5_CMO,
635 };
636
637 /* Old method - ELF header with PT_NOTE sections */
638 static struct fake_elf {
639         Elf32_Ehdr      elfhdr;
640         Elf32_Phdr      phdr[2];
641         struct chrpnote {
642                 u32     namesz;
643                 u32     descsz;
644                 u32     type;
645                 char    name[8];        /* "PowerPC" */
646                 struct chrpdesc {
647                         u32     real_mode;
648                         u32     real_base;
649                         u32     real_size;
650                         u32     virt_base;
651                         u32     virt_size;
652                         u32     load_base;
653                 } chrpdesc;
654         } chrpnote;
655         struct rpanote {
656                 u32     namesz;
657                 u32     descsz;
658                 u32     type;
659                 char    name[24];       /* "IBM,RPA-Client-Config" */
660                 struct rpadesc {
661                         u32     lpar_affinity;
662                         u32     min_rmo_size;
663                         u32     min_rmo_percent;
664                         u32     max_pft_size;
665                         u32     splpar;
666                         u32     min_load;
667                         u32     new_mem_def;
668                         u32     ignore_me;
669                 } rpadesc;
670         } rpanote;
671 } fake_elf = {
672         .elfhdr = {
673                 .e_ident = { 0x7f, 'E', 'L', 'F',
674                              ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
675                 .e_type = ET_EXEC,      /* yeah right */
676                 .e_machine = EM_PPC,
677                 .e_version = EV_CURRENT,
678                 .e_phoff = offsetof(struct fake_elf, phdr),
679                 .e_phentsize = sizeof(Elf32_Phdr),
680                 .e_phnum = 2
681         },
682         .phdr = {
683                 [0] = {
684                         .p_type = PT_NOTE,
685                         .p_offset = offsetof(struct fake_elf, chrpnote),
686                         .p_filesz = sizeof(struct chrpnote)
687                 }, [1] = {
688                         .p_type = PT_NOTE,
689                         .p_offset = offsetof(struct fake_elf, rpanote),
690                         .p_filesz = sizeof(struct rpanote)
691                 }
692         },
693         .chrpnote = {
694                 .namesz = sizeof("PowerPC"),
695                 .descsz = sizeof(struct chrpdesc),
696                 .type = 0x1275,
697                 .name = "PowerPC",
698                 .chrpdesc = {
699                         .real_mode = ~0U,       /* ~0 means "don't care" */
700                         .real_base = ~0U,
701                         .real_size = ~0U,
702                         .virt_base = ~0U,
703                         .virt_size = ~0U,
704                         .load_base = ~0U
705                 },
706         },
707         .rpanote = {
708                 .namesz = sizeof("IBM,RPA-Client-Config"),
709                 .descsz = sizeof(struct rpadesc),
710                 .type = 0x12759999,
711                 .name = "IBM,RPA-Client-Config",
712                 .rpadesc = {
713                         .lpar_affinity = 0,
714                         .min_rmo_size = 64,     /* in megabytes */
715                         .min_rmo_percent = 0,
716                         .max_pft_size = 48,     /* 2^48 bytes max PFT size */
717                         .splpar = 1,
718                         .min_load = ~0U,
719                         .new_mem_def = 0
720                 }
721         }
722 };
723
724 static void __init prom_send_capabilities(void)
725 {
726         ihandle elfloader, root;
727         prom_arg_t ret;
728
729         root = call_prom("open", 1, 1, ADDR("/"));
730         if (root != 0) {
731                 /* try calling the ibm,client-architecture-support method */
732                 prom_printf("Calling ibm,client-architecture...");
733                 if (call_prom_ret("call-method", 3, 2, &ret,
734                                   ADDR("ibm,client-architecture-support"),
735                                   root,
736                                   ADDR(ibm_architecture_vec)) == 0) {
737                         /* the call exists... */
738                         if (ret)
739                                 prom_printf("\nWARNING: ibm,client-architecture"
740                                             "-support call FAILED!\n");
741                         call_prom("close", 1, 0, root);
742                         prom_printf(" done\n");
743                         return;
744                 }
745                 call_prom("close", 1, 0, root);
746         }
747
748         /* no ibm,client-architecture-support call, try the old way */
749         elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
750         if (elfloader == 0) {
751                 prom_printf("couldn't open /packages/elf-loader\n");
752                 return;
753         }
754         call_prom("call-method", 3, 1, ADDR("process-elf-header"),
755                         elfloader, ADDR(&fake_elf));
756         call_prom("close", 1, 0, elfloader);
757 }
758 #endif
759
760 /*
761  * Memory allocation strategy... our layout is normally:
762  *
763  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
764  *  rare cases, initrd might end up being before the kernel though.
765  *  We assume this won't override the final kernel at 0, we have no
766  *  provision to handle that in this version, but it should hopefully
767  *  never happen.
768  *
769  *  alloc_top is set to the top of RMO, eventually shrink down if the
770  *  TCEs overlap
771  *
772  *  alloc_bottom is set to the top of kernel/initrd
773  *
774  *  from there, allocations are done this way : rtas is allocated
775  *  topmost, and the device-tree is allocated from the bottom. We try
776  *  to grow the device-tree allocation as we progress. If we can't,
777  *  then we fail, we don't currently have a facility to restart
778  *  elsewhere, but that shouldn't be necessary.
779  *
780  *  Note that calls to reserve_mem have to be done explicitly, memory
781  *  allocated with either alloc_up or alloc_down isn't automatically
782  *  reserved.
783  */
784
785
786 /*
787  * Allocates memory in the RMO upward from the kernel/initrd
788  *
789  * When align is 0, this is a special case, it means to allocate in place
790  * at the current location of alloc_bottom or fail (that is basically
791  * extending the previous allocation). Used for the device-tree flattening
792  */
793 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
794 {
795         unsigned long base = RELOC(alloc_bottom);
796         unsigned long addr = 0;
797
798         if (align)
799                 base = _ALIGN_UP(base, align);
800         prom_debug("alloc_up(%x, %x)\n", size, align);
801         if (RELOC(ram_top) == 0)
802                 prom_panic("alloc_up() called with mem not initialized\n");
803
804         if (align)
805                 base = _ALIGN_UP(RELOC(alloc_bottom), align);
806         else
807                 base = RELOC(alloc_bottom);
808
809         for(; (base + size) <= RELOC(alloc_top); 
810             base = _ALIGN_UP(base + 0x100000, align)) {
811                 prom_debug("    trying: 0x%x\n\r", base);
812                 addr = (unsigned long)prom_claim(base, size, 0);
813                 if (addr != PROM_ERROR && addr != 0)
814                         break;
815                 addr = 0;
816                 if (align == 0)
817                         break;
818         }
819         if (addr == 0)
820                 return 0;
821         RELOC(alloc_bottom) = addr;
822
823         prom_debug(" -> %x\n", addr);
824         prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
825         prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
826         prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
827         prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
828         prom_debug("  ram_top      : %x\n", RELOC(ram_top));
829
830         return addr;
831 }
832
833 /*
834  * Allocates memory downward, either from top of RMO, or if highmem
835  * is set, from the top of RAM.  Note that this one doesn't handle
836  * failures.  It does claim memory if highmem is not set.
837  */
838 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
839                                        int highmem)
840 {
841         unsigned long base, addr = 0;
842
843         prom_debug("alloc_down(%x, %x, %s)\n", size, align,
844                    highmem ? RELOC("(high)") : RELOC("(low)"));
845         if (RELOC(ram_top) == 0)
846                 prom_panic("alloc_down() called with mem not initialized\n");
847
848         if (highmem) {
849                 /* Carve out storage for the TCE table. */
850                 addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
851                 if (addr <= RELOC(alloc_bottom))
852                         return 0;
853                 /* Will we bump into the RMO ? If yes, check out that we
854                  * didn't overlap existing allocations there, if we did,
855                  * we are dead, we must be the first in town !
856                  */
857                 if (addr < RELOC(rmo_top)) {
858                         /* Good, we are first */
859                         if (RELOC(alloc_top) == RELOC(rmo_top))
860                                 RELOC(alloc_top) = RELOC(rmo_top) = addr;
861                         else
862                                 return 0;
863                 }
864                 RELOC(alloc_top_high) = addr;
865                 goto bail;
866         }
867
868         base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
869         for (; base > RELOC(alloc_bottom);
870              base = _ALIGN_DOWN(base - 0x100000, align))  {
871                 prom_debug("    trying: 0x%x\n\r", base);
872                 addr = (unsigned long)prom_claim(base, size, 0);
873                 if (addr != PROM_ERROR && addr != 0)
874                         break;
875                 addr = 0;
876         }
877         if (addr == 0)
878                 return 0;
879         RELOC(alloc_top) = addr;
880
881  bail:
882         prom_debug(" -> %x\n", addr);
883         prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
884         prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
885         prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
886         prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
887         prom_debug("  ram_top      : %x\n", RELOC(ram_top));
888
889         return addr;
890 }
891
892 /*
893  * Parse a "reg" cell
894  */
895 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
896 {
897         cell_t *p = *cellp;
898         unsigned long r = 0;
899
900         /* Ignore more than 2 cells */
901         while (s > sizeof(unsigned long) / 4) {
902                 p++;
903                 s--;
904         }
905         r = *p++;
906 #ifdef CONFIG_PPC64
907         if (s > 1) {
908                 r <<= 32;
909                 r |= *(p++);
910         }
911 #endif
912         *cellp = p;
913         return r;
914 }
915
916 /*
917  * Very dumb function for adding to the memory reserve list, but
918  * we don't need anything smarter at this point
919  *
920  * XXX Eventually check for collisions.  They should NEVER happen.
921  * If problems seem to show up, it would be a good start to track
922  * them down.
923  */
924 static void __init reserve_mem(u64 base, u64 size)
925 {
926         u64 top = base + size;
927         unsigned long cnt = RELOC(mem_reserve_cnt);
928
929         if (size == 0)
930                 return;
931
932         /* We need to always keep one empty entry so that we
933          * have our terminator with "size" set to 0 since we are
934          * dumb and just copy this entire array to the boot params
935          */
936         base = _ALIGN_DOWN(base, PAGE_SIZE);
937         top = _ALIGN_UP(top, PAGE_SIZE);
938         size = top - base;
939
940         if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
941                 prom_panic("Memory reserve map exhausted !\n");
942         RELOC(mem_reserve_map)[cnt].base = base;
943         RELOC(mem_reserve_map)[cnt].size = size;
944         RELOC(mem_reserve_cnt) = cnt + 1;
945 }
946
947 /*
948  * Initialize memory allocation mechanism, parse "memory" nodes and
949  * obtain that way the top of memory and RMO to setup out local allocator
950  */
951 static void __init prom_init_mem(void)
952 {
953         phandle node;
954         char *path, type[64];
955         unsigned int plen;
956         cell_t *p, *endp;
957         struct prom_t *_prom = &RELOC(prom);
958         u32 rac, rsc;
959
960         /*
961          * We iterate the memory nodes to find
962          * 1) top of RMO (first node)
963          * 2) top of memory
964          */
965         rac = 2;
966         prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
967         rsc = 1;
968         prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
969         prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
970         prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
971
972         prom_debug("scanning memory:\n");
973         path = RELOC(prom_scratch);
974
975         for (node = 0; prom_next_node(&node); ) {
976                 type[0] = 0;
977                 prom_getprop(node, "device_type", type, sizeof(type));
978
979                 if (type[0] == 0) {
980                         /*
981                          * CHRP Longtrail machines have no device_type
982                          * on the memory node, so check the name instead...
983                          */
984                         prom_getprop(node, "name", type, sizeof(type));
985                 }
986                 if (strcmp(type, RELOC("memory")))
987                         continue;
988
989                 plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
990                 if (plen > sizeof(regbuf)) {
991                         prom_printf("memory node too large for buffer !\n");
992                         plen = sizeof(regbuf);
993                 }
994                 p = RELOC(regbuf);
995                 endp = p + (plen / sizeof(cell_t));
996
997 #ifdef DEBUG_PROM
998                 memset(path, 0, PROM_SCRATCH_SIZE);
999                 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1000                 prom_debug("  node %s :\n", path);
1001 #endif /* DEBUG_PROM */
1002
1003                 while ((endp - p) >= (rac + rsc)) {
1004                         unsigned long base, size;
1005
1006                         base = prom_next_cell(rac, &p);
1007                         size = prom_next_cell(rsc, &p);
1008
1009                         if (size == 0)
1010                                 continue;
1011                         prom_debug("    %x %x\n", base, size);
1012                         if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
1013                                 RELOC(rmo_top) = size;
1014                         if ((base + size) > RELOC(ram_top))
1015                                 RELOC(ram_top) = base + size;
1016                 }
1017         }
1018
1019         RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
1020
1021         /* Check if we have an initrd after the kernel, if we do move our bottom
1022          * point to after it
1023          */
1024         if (RELOC(prom_initrd_start)) {
1025                 if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
1026                         RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
1027         }
1028
1029         /*
1030          * Setup our top alloc point, that is top of RMO or top of
1031          * segment 0 when running non-LPAR.
1032          * Some RS64 machines have buggy firmware where claims up at
1033          * 1GB fail.  Cap at 768MB as a workaround.
1034          * Since 768MB is plenty of room, and we need to cap to something
1035          * reasonable on 32-bit, cap at 768MB on all machines.
1036          */
1037         if (!RELOC(rmo_top))
1038                 RELOC(rmo_top) = RELOC(ram_top);
1039         RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
1040         RELOC(alloc_top) = RELOC(rmo_top);
1041         RELOC(alloc_top_high) = RELOC(ram_top);
1042
1043         prom_printf("memory layout at init:\n");
1044         prom_printf("  alloc_bottom : %x\n", RELOC(alloc_bottom));
1045         prom_printf("  alloc_top    : %x\n", RELOC(alloc_top));
1046         prom_printf("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
1047         prom_printf("  rmo_top      : %x\n", RELOC(rmo_top));
1048         prom_printf("  ram_top      : %x\n", RELOC(ram_top));
1049 }
1050
1051
1052 /*
1053  * Allocate room for and instantiate RTAS
1054  */
1055 static void __init prom_instantiate_rtas(void)
1056 {
1057         phandle rtas_node;
1058         ihandle rtas_inst;
1059         u32 base, entry = 0;
1060         u32 size = 0;
1061
1062         prom_debug("prom_instantiate_rtas: start...\n");
1063
1064         rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1065         prom_debug("rtas_node: %x\n", rtas_node);
1066         if (!PHANDLE_VALID(rtas_node))
1067                 return;
1068
1069         prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1070         if (size == 0)
1071                 return;
1072
1073         base = alloc_down(size, PAGE_SIZE, 0);
1074         if (base == 0) {
1075                 prom_printf("RTAS allocation failed !\n");
1076                 return;
1077         }
1078
1079         rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1080         if (!IHANDLE_VALID(rtas_inst)) {
1081                 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
1082                 return;
1083         }
1084
1085         prom_printf("instantiating rtas at 0x%x...", base);
1086
1087         if (call_prom_ret("call-method", 3, 2, &entry,
1088                           ADDR("instantiate-rtas"),
1089                           rtas_inst, base) != 0
1090             || entry == 0) {
1091                 prom_printf(" failed\n");
1092                 return;
1093         }
1094         prom_printf(" done\n");
1095
1096         reserve_mem(base, size);
1097
1098         prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1099                      &base, sizeof(base));
1100         prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1101                      &entry, sizeof(entry));
1102
1103         prom_debug("rtas base     = 0x%x\n", base);
1104         prom_debug("rtas entry    = 0x%x\n", entry);
1105         prom_debug("rtas size     = 0x%x\n", (long)size);
1106
1107         prom_debug("prom_instantiate_rtas: end...\n");
1108 }
1109
1110 #ifdef CONFIG_PPC64
1111 /*
1112  * Allocate room for and initialize TCE tables
1113  */
1114 static void __init prom_initialize_tce_table(void)
1115 {
1116         phandle node;
1117         ihandle phb_node;
1118         char compatible[64], type[64], model[64];
1119         char *path = RELOC(prom_scratch);
1120         u64 base, align;
1121         u32 minalign, minsize;
1122         u64 tce_entry, *tce_entryp;
1123         u64 local_alloc_top, local_alloc_bottom;
1124         u64 i;
1125
1126         if (RELOC(prom_iommu_off))
1127                 return;
1128
1129         prom_debug("starting prom_initialize_tce_table\n");
1130
1131         /* Cache current top of allocs so we reserve a single block */
1132         local_alloc_top = RELOC(alloc_top_high);
1133         local_alloc_bottom = local_alloc_top;
1134
1135         /* Search all nodes looking for PHBs. */
1136         for (node = 0; prom_next_node(&node); ) {
1137                 compatible[0] = 0;
1138                 type[0] = 0;
1139                 model[0] = 0;
1140                 prom_getprop(node, "compatible",
1141                              compatible, sizeof(compatible));
1142                 prom_getprop(node, "device_type", type, sizeof(type));
1143                 prom_getprop(node, "model", model, sizeof(model));
1144
1145                 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1146                         continue;
1147
1148                 /* Keep the old logic intact to avoid regression. */
1149                 if (compatible[0] != 0) {
1150                         if ((strstr(compatible, RELOC("python")) == NULL) &&
1151                             (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1152                             (strstr(compatible, RELOC("Winnipeg")) == NULL))
1153                                 continue;
1154                 } else if (model[0] != 0) {
1155                         if ((strstr(model, RELOC("ython")) == NULL) &&
1156                             (strstr(model, RELOC("peedwagon")) == NULL) &&
1157                             (strstr(model, RELOC("innipeg")) == NULL))
1158                                 continue;
1159                 }
1160
1161                 if (prom_getprop(node, "tce-table-minalign", &minalign,
1162                                  sizeof(minalign)) == PROM_ERROR)
1163                         minalign = 0;
1164                 if (prom_getprop(node, "tce-table-minsize", &minsize,
1165                                  sizeof(minsize)) == PROM_ERROR)
1166                         minsize = 4UL << 20;
1167
1168                 /*
1169                  * Even though we read what OF wants, we just set the table
1170                  * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
1171                  * By doing this, we avoid the pitfalls of trying to DMA to
1172                  * MMIO space and the DMA alias hole.
1173                  *
1174                  * On POWER4, firmware sets the TCE region by assuming
1175                  * each TCE table is 8MB. Using this memory for anything
1176                  * else will impact performance, so we always allocate 8MB.
1177                  * Anton
1178                  */
1179                 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1180                         minsize = 8UL << 20;
1181                 else
1182                         minsize = 4UL << 20;
1183
1184                 /* Align to the greater of the align or size */
1185                 align = max(minalign, minsize);
1186                 base = alloc_down(minsize, align, 1);
1187                 if (base == 0)
1188                         prom_panic("ERROR, cannot find space for TCE table.\n");
1189                 if (base < local_alloc_bottom)
1190                         local_alloc_bottom = base;
1191
1192                 /* It seems OF doesn't null-terminate the path :-( */
1193                 memset(path, 0, PROM_SCRATCH_SIZE);
1194                 /* Call OF to setup the TCE hardware */
1195                 if (call_prom("package-to-path", 3, 1, node,
1196                               path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1197                         prom_printf("package-to-path failed\n");
1198                 }
1199
1200                 /* Save away the TCE table attributes for later use. */
1201                 prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1202                 prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1203
1204                 prom_debug("TCE table: %s\n", path);
1205                 prom_debug("\tnode = 0x%x\n", node);
1206                 prom_debug("\tbase = 0x%x\n", base);
1207                 prom_debug("\tsize = 0x%x\n", minsize);
1208
1209                 /* Initialize the table to have a one-to-one mapping
1210                  * over the allocated size.
1211                  */
1212                 tce_entryp = (u64 *)base;
1213                 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1214                         tce_entry = (i << PAGE_SHIFT);
1215                         tce_entry |= 0x3;
1216                         *tce_entryp = tce_entry;
1217                 }
1218
1219                 prom_printf("opening PHB %s", path);
1220                 phb_node = call_prom("open", 1, 1, path);
1221                 if (phb_node == 0)
1222                         prom_printf("... failed\n");
1223                 else
1224                         prom_printf("... done\n");
1225
1226                 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1227                           phb_node, -1, minsize,
1228                           (u32) base, (u32) (base >> 32));
1229                 call_prom("close", 1, 0, phb_node);
1230         }
1231
1232         reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1233
1234         /* These are only really needed if there is a memory limit in
1235          * effect, but we don't know so export them always. */
1236         RELOC(prom_tce_alloc_start) = local_alloc_bottom;
1237         RELOC(prom_tce_alloc_end) = local_alloc_top;
1238
1239         /* Flag the first invalid entry */
1240         prom_debug("ending prom_initialize_tce_table\n");
1241 }
1242 #endif
1243
1244 /*
1245  * With CHRP SMP we need to use the OF to start the other processors.
1246  * We can't wait until smp_boot_cpus (the OF is trashed by then)
1247  * so we have to put the processors into a holding pattern controlled
1248  * by the kernel (not OF) before we destroy the OF.
1249  *
1250  * This uses a chunk of low memory, puts some holding pattern
1251  * code there and sends the other processors off to there until
1252  * smp_boot_cpus tells them to do something.  The holding pattern
1253  * checks that address until its cpu # is there, when it is that
1254  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
1255  * of setting those values.
1256  *
1257  * We also use physical address 0x4 here to tell when a cpu
1258  * is in its holding pattern code.
1259  *
1260  * -- Cort
1261  */
1262 extern char __secondary_hold;
1263 extern unsigned long __secondary_hold_spinloop;
1264 extern unsigned long __secondary_hold_acknowledge;
1265
1266 /*
1267  * We want to reference the copy of __secondary_hold_* in the
1268  * 0 - 0x100 address range
1269  */
1270 #define LOW_ADDR(x)     (((unsigned long) &(x)) & 0xff)
1271
1272 static void __init prom_hold_cpus(void)
1273 {
1274         unsigned long i;
1275         unsigned int reg;
1276         phandle node;
1277         char type[64];
1278         struct prom_t *_prom = &RELOC(prom);
1279         unsigned long *spinloop
1280                 = (void *) LOW_ADDR(__secondary_hold_spinloop);
1281         unsigned long *acknowledge
1282                 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
1283         unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
1284
1285         prom_debug("prom_hold_cpus: start...\n");
1286         prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
1287         prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
1288         prom_debug("    1) acknowledge    = 0x%x\n",
1289                    (unsigned long)acknowledge);
1290         prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
1291         prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
1292
1293         /* Set the common spinloop variable, so all of the secondary cpus
1294          * will block when they are awakened from their OF spinloop.
1295          * This must occur for both SMP and non SMP kernels, since OF will
1296          * be trashed when we move the kernel.
1297          */
1298         *spinloop = 0;
1299
1300         /* look for cpus */
1301         for (node = 0; prom_next_node(&node); ) {
1302                 type[0] = 0;
1303                 prom_getprop(node, "device_type", type, sizeof(type));
1304                 if (strcmp(type, RELOC("cpu")) != 0)
1305                         continue;
1306
1307                 /* Skip non-configured cpus. */
1308                 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1309                         if (strcmp(type, RELOC("okay")) != 0)
1310                                 continue;
1311
1312                 reg = -1;
1313                 prom_getprop(node, "reg", &reg, sizeof(reg));
1314
1315                 prom_debug("cpu hw idx   = 0x%x\n", reg);
1316
1317                 /* Init the acknowledge var which will be reset by
1318                  * the secondary cpu when it awakens from its OF
1319                  * spinloop.
1320                  */
1321                 *acknowledge = (unsigned long)-1;
1322
1323                 if (reg != _prom->cpu) {
1324                         /* Primary Thread of non-boot cpu */
1325                         prom_printf("starting cpu hw idx %x... ", reg);
1326                         call_prom("start-cpu", 3, 0, node,
1327                                   secondary_hold, reg);
1328
1329                         for (i = 0; (i < 100000000) && 
1330                              (*acknowledge == ((unsigned long)-1)); i++ )
1331                                 mb();
1332
1333                         if (*acknowledge == reg)
1334                                 prom_printf("done\n");
1335                         else
1336                                 prom_printf("failed: %x\n", *acknowledge);
1337                 }
1338 #ifdef CONFIG_SMP
1339                 else
1340                         prom_printf("boot cpu hw idx %x\n", reg);
1341 #endif /* CONFIG_SMP */
1342         }
1343
1344         prom_debug("prom_hold_cpus: end...\n");
1345 }
1346
1347
1348 static void __init prom_init_client_services(unsigned long pp)
1349 {
1350         struct prom_t *_prom = &RELOC(prom);
1351
1352         /* Get a handle to the prom entry point before anything else */
1353         RELOC(prom_entry) = pp;
1354
1355         /* get a handle for the stdout device */
1356         _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1357         if (!PHANDLE_VALID(_prom->chosen))
1358                 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1359
1360         /* get device tree root */
1361         _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1362         if (!PHANDLE_VALID(_prom->root))
1363                 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1364
1365         _prom->mmumap = 0;
1366 }
1367
1368 #ifdef CONFIG_PPC32
1369 /*
1370  * For really old powermacs, we need to map things we claim.
1371  * For that, we need the ihandle of the mmu.
1372  * Also, on the longtrail, we need to work around other bugs.
1373  */
1374 static void __init prom_find_mmu(void)
1375 {
1376         struct prom_t *_prom = &RELOC(prom);
1377         phandle oprom;
1378         char version[64];
1379
1380         oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1381         if (!PHANDLE_VALID(oprom))
1382                 return;
1383         if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1384                 return;
1385         version[sizeof(version) - 1] = 0;
1386         /* XXX might need to add other versions here */
1387         if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1388                 of_workarounds = OF_WA_CLAIM;
1389         else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1390                 of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1391                 call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1392         } else
1393                 return;
1394         _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
1395         prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1396                      sizeof(_prom->mmumap));
1397         if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1398                 of_workarounds &= ~OF_WA_CLAIM;         /* hmmm */
1399 }
1400 #else
1401 #define prom_find_mmu()
1402 #endif
1403
1404 static void __init prom_init_stdout(void)
1405 {
1406         struct prom_t *_prom = &RELOC(prom);
1407         char *path = RELOC(of_stdout_device);
1408         char type[16];
1409         u32 val;
1410
1411         if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1412                 prom_panic("cannot find stdout");
1413
1414         _prom->stdout = val;
1415
1416         /* Get the full OF pathname of the stdout device */
1417         memset(path, 0, 256);
1418         call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1419         val = call_prom("instance-to-package", 1, 1, _prom->stdout);
1420         prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1421                      &val, sizeof(val));
1422         prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
1423         prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1424                      path, strlen(path) + 1);
1425
1426         /* If it's a display, note it */
1427         memset(type, 0, sizeof(type));
1428         prom_getprop(val, "device_type", type, sizeof(type));
1429         if (strcmp(type, RELOC("display")) == 0)
1430                 prom_setprop(val, path, "linux,boot-display", NULL, 0);
1431 }
1432
1433 static void __init prom_close_stdin(void)
1434 {
1435         struct prom_t *_prom = &RELOC(prom);
1436         ihandle val;
1437
1438         if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1439                 call_prom("close", 1, 0, val);
1440 }
1441
1442 static int __init prom_find_machine_type(void)
1443 {
1444         struct prom_t *_prom = &RELOC(prom);
1445         char compat[256];
1446         int len, i = 0;
1447 #ifdef CONFIG_PPC64
1448         phandle rtas;
1449         int x;
1450 #endif
1451
1452         /* Look for a PowerMac */
1453         len = prom_getprop(_prom->root, "compatible",
1454                            compat, sizeof(compat)-1);
1455         if (len > 0) {
1456                 compat[len] = 0;
1457                 while (i < len) {
1458                         char *p = &compat[i];
1459                         int sl = strlen(p);
1460                         if (sl == 0)
1461                                 break;
1462                         if (strstr(p, RELOC("Power Macintosh")) ||
1463                             strstr(p, RELOC("MacRISC")))
1464                                 return PLATFORM_POWERMAC;
1465 #ifdef CONFIG_PPC64
1466                         /* We must make sure we don't detect the IBM Cell
1467                          * blades as pSeries due to some firmware issues,
1468                          * so we do it here.
1469                          */
1470                         if (strstr(p, RELOC("IBM,CBEA")) ||
1471                             strstr(p, RELOC("IBM,CPBW-1.0")))
1472                                 return PLATFORM_GENERIC;
1473 #endif /* CONFIG_PPC64 */
1474                         i += sl + 1;
1475                 }
1476         }
1477 #ifdef CONFIG_PPC64
1478         /* If not a mac, try to figure out if it's an IBM pSeries or any other
1479          * PAPR compliant platform. We assume it is if :
1480          *  - /device_type is "chrp" (please, do NOT use that for future
1481          *    non-IBM designs !
1482          *  - it has /rtas
1483          */
1484         len = prom_getprop(_prom->root, "device_type",
1485                            compat, sizeof(compat)-1);
1486         if (len <= 0)
1487                 return PLATFORM_GENERIC;
1488         if (strcmp(compat, RELOC("chrp")))
1489                 return PLATFORM_GENERIC;
1490
1491         /* Default to pSeries. We need to know if we are running LPAR */
1492         rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1493         if (!PHANDLE_VALID(rtas))
1494                 return PLATFORM_GENERIC;
1495         x = prom_getproplen(rtas, "ibm,hypertas-functions");
1496         if (x != PROM_ERROR) {
1497                 prom_debug("Hypertas detected, assuming LPAR !\n");
1498                 return PLATFORM_PSERIES_LPAR;
1499         }
1500         return PLATFORM_PSERIES;
1501 #else
1502         return PLATFORM_GENERIC;
1503 #endif
1504 }
1505
1506 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1507 {
1508         return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1509 }
1510
1511 /*
1512  * If we have a display that we don't know how to drive,
1513  * we will want to try to execute OF's open method for it
1514  * later.  However, OF will probably fall over if we do that
1515  * we've taken over the MMU.
1516  * So we check whether we will need to open the display,
1517  * and if so, open it now.
1518  */
1519 static void __init prom_check_displays(void)
1520 {
1521         char type[16], *path;
1522         phandle node;
1523         ihandle ih;
1524         int i;
1525
1526         static unsigned char default_colors[] = {
1527                 0x00, 0x00, 0x00,
1528                 0x00, 0x00, 0xaa,
1529                 0x00, 0xaa, 0x00,
1530                 0x00, 0xaa, 0xaa,
1531                 0xaa, 0x00, 0x00,
1532                 0xaa, 0x00, 0xaa,
1533                 0xaa, 0xaa, 0x00,
1534                 0xaa, 0xaa, 0xaa,
1535                 0x55, 0x55, 0x55,
1536                 0x55, 0x55, 0xff,
1537                 0x55, 0xff, 0x55,
1538                 0x55, 0xff, 0xff,
1539                 0xff, 0x55, 0x55,
1540                 0xff, 0x55, 0xff,
1541                 0xff, 0xff, 0x55,
1542                 0xff, 0xff, 0xff
1543         };
1544         const unsigned char *clut;
1545
1546         prom_debug("Looking for displays\n");
1547         for (node = 0; prom_next_node(&node); ) {
1548                 memset(type, 0, sizeof(type));
1549                 prom_getprop(node, "device_type", type, sizeof(type));
1550                 if (strcmp(type, RELOC("display")) != 0)
1551                         continue;
1552
1553                 /* It seems OF doesn't null-terminate the path :-( */
1554                 path = RELOC(prom_scratch);
1555                 memset(path, 0, PROM_SCRATCH_SIZE);
1556
1557                 /*
1558                  * leave some room at the end of the path for appending extra
1559                  * arguments
1560                  */
1561                 if (call_prom("package-to-path", 3, 1, node, path,
1562                               PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1563                         continue;
1564                 prom_printf("found display   : %s, opening... ", path);
1565                 
1566                 ih = call_prom("open", 1, 1, path);
1567                 if (ih == 0) {
1568                         prom_printf("failed\n");
1569                         continue;
1570                 }
1571
1572                 /* Success */
1573                 prom_printf("done\n");
1574                 prom_setprop(node, path, "linux,opened", NULL, 0);
1575
1576                 /* Setup a usable color table when the appropriate
1577                  * method is available. Should update this to set-colors */
1578                 clut = RELOC(default_colors);
1579                 for (i = 0; i < 32; i++, clut += 3)
1580                         if (prom_set_color(ih, i, clut[0], clut[1],
1581                                            clut[2]) != 0)
1582                                 break;
1583
1584 #ifdef CONFIG_LOGO_LINUX_CLUT224
1585                 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1586                 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1587                         if (prom_set_color(ih, i + 32, clut[0], clut[1],
1588                                            clut[2]) != 0)
1589                                 break;
1590 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
1591         }
1592 }
1593
1594
1595 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1596 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
1597                               unsigned long needed, unsigned long align)
1598 {
1599         void *ret;
1600
1601         *mem_start = _ALIGN(*mem_start, align);
1602         while ((*mem_start + needed) > *mem_end) {
1603                 unsigned long room, chunk;
1604
1605                 prom_debug("Chunk exhausted, claiming more at %x...\n",
1606                            RELOC(alloc_bottom));
1607                 room = RELOC(alloc_top) - RELOC(alloc_bottom);
1608                 if (room > DEVTREE_CHUNK_SIZE)
1609                         room = DEVTREE_CHUNK_SIZE;
1610                 if (room < PAGE_SIZE)
1611                         prom_panic("No memory for flatten_device_tree (no room)");
1612                 chunk = alloc_up(room, 0);
1613                 if (chunk == 0)
1614                         prom_panic("No memory for flatten_device_tree (claim failed)");
1615                 *mem_end = RELOC(alloc_top);
1616         }
1617
1618         ret = (void *)*mem_start;
1619         *mem_start += needed;
1620
1621         return ret;
1622 }
1623
1624 #define dt_push_token(token, mem_start, mem_end) \
1625         do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
1626
1627 static unsigned long __init dt_find_string(char *str)
1628 {
1629         char *s, *os;
1630
1631         s = os = (char *)RELOC(dt_string_start);
1632         s += 4;
1633         while (s <  (char *)RELOC(dt_string_end)) {
1634                 if (strcmp(s, str) == 0)
1635                         return s - os;
1636                 s += strlen(s) + 1;
1637         }
1638         return 0;
1639 }
1640
1641 /*
1642  * The Open Firmware 1275 specification states properties must be 31 bytes or
1643  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
1644  */
1645 #define MAX_PROPERTY_NAME 64
1646
1647 static void __init scan_dt_build_strings(phandle node,
1648                                          unsigned long *mem_start,
1649                                          unsigned long *mem_end)
1650 {
1651         char *prev_name, *namep, *sstart;
1652         unsigned long soff;
1653         phandle child;
1654
1655         sstart =  (char *)RELOC(dt_string_start);
1656
1657         /* get and store all property names */
1658         prev_name = RELOC("");
1659         for (;;) {
1660                 /* 64 is max len of name including nul. */
1661                 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
1662                 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
1663                         /* No more nodes: unwind alloc */
1664                         *mem_start = (unsigned long)namep;
1665                         break;
1666                 }
1667
1668                 /* skip "name" */
1669                 if (strcmp(namep, RELOC("name")) == 0) {
1670                         *mem_start = (unsigned long)namep;
1671                         prev_name = RELOC("name");
1672                         continue;
1673                 }
1674                 /* get/create string entry */
1675                 soff = dt_find_string(namep);
1676                 if (soff != 0) {
1677                         *mem_start = (unsigned long)namep;
1678                         namep = sstart + soff;
1679                 } else {
1680                         /* Trim off some if we can */
1681                         *mem_start = (unsigned long)namep + strlen(namep) + 1;
1682                         RELOC(dt_string_end) = *mem_start;
1683                 }
1684                 prev_name = namep;
1685         }
1686
1687         /* do all our children */
1688         child = call_prom("child", 1, 1, node);
1689         while (child != 0) {
1690                 scan_dt_build_strings(child, mem_start, mem_end);
1691                 child = call_prom("peer", 1, 1, child);
1692         }
1693 }
1694
1695 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
1696                                         unsigned long *mem_end)
1697 {
1698         phandle child;
1699         char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
1700         unsigned long soff;
1701         unsigned char *valp;
1702         static char pname[MAX_PROPERTY_NAME];
1703         int l, room;
1704
1705         dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
1706
1707         /* get the node's full name */
1708         namep = (char *)*mem_start;
1709         room = *mem_end - *mem_start;
1710         if (room > 255)
1711                 room = 255;
1712         l = call_prom("package-to-path", 3, 1, node, namep, room);
1713         if (l >= 0) {
1714                 /* Didn't fit?  Get more room. */
1715                 if (l >= room) {
1716                         if (l >= *mem_end - *mem_start)
1717                                 namep = make_room(mem_start, mem_end, l+1, 1);
1718                         call_prom("package-to-path", 3, 1, node, namep, l);
1719                 }
1720                 namep[l] = '\0';
1721
1722                 /* Fixup an Apple bug where they have bogus \0 chars in the
1723                  * middle of the path in some properties, and extract
1724                  * the unit name (everything after the last '/').
1725                  */
1726                 for (lp = p = namep, ep = namep + l; p < ep; p++) {
1727                         if (*p == '/')
1728                                 lp = namep;
1729                         else if (*p != 0)
1730                                 *lp++ = *p;
1731                 }
1732                 *lp = 0;
1733                 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
1734         }
1735
1736         /* get it again for debugging */
1737         path = RELOC(prom_scratch);
1738         memset(path, 0, PROM_SCRATCH_SIZE);
1739         call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1740
1741         /* get and store all properties */
1742         prev_name = RELOC("");
1743         sstart = (char *)RELOC(dt_string_start);
1744         for (;;) {
1745                 if (call_prom("nextprop", 3, 1, node, prev_name,
1746                               RELOC(pname)) != 1)
1747                         break;
1748
1749                 /* skip "name" */
1750                 if (strcmp(RELOC(pname), RELOC("name")) == 0) {
1751                         prev_name = RELOC("name");
1752                         continue;
1753                 }
1754
1755                 /* find string offset */
1756                 soff = dt_find_string(RELOC(pname));
1757                 if (soff == 0) {
1758                         prom_printf("WARNING: Can't find string index for"
1759                                     " <%s>, node %s\n", RELOC(pname), path);
1760                         break;
1761                 }
1762                 prev_name = sstart + soff;
1763
1764                 /* get length */
1765                 l = call_prom("getproplen", 2, 1, node, RELOC(pname));
1766
1767                 /* sanity checks */
1768                 if (l == PROM_ERROR)
1769                         continue;
1770                 if (l > MAX_PROPERTY_LENGTH) {
1771                         prom_printf("WARNING: ignoring large property ");
1772                         /* It seems OF doesn't null-terminate the path :-( */
1773                         prom_printf("[%s] ", path);
1774                         prom_printf("%s length 0x%x\n", RELOC(pname), l);
1775                         continue;
1776                 }
1777
1778                 /* push property head */
1779                 dt_push_token(OF_DT_PROP, mem_start, mem_end);
1780                 dt_push_token(l, mem_start, mem_end);
1781                 dt_push_token(soff, mem_start, mem_end);
1782
1783                 /* push property content */
1784                 valp = make_room(mem_start, mem_end, l, 4);
1785                 call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
1786                 *mem_start = _ALIGN(*mem_start, 4);
1787         }
1788
1789         /* Add a "linux,phandle" property. */
1790         soff = dt_find_string(RELOC("linux,phandle"));
1791         if (soff == 0)
1792                 prom_printf("WARNING: Can't find string index for"
1793                             " <linux-phandle> node %s\n", path);
1794         else {
1795                 dt_push_token(OF_DT_PROP, mem_start, mem_end);
1796                 dt_push_token(4, mem_start, mem_end);
1797                 dt_push_token(soff, mem_start, mem_end);
1798                 valp = make_room(mem_start, mem_end, 4, 4);
1799                 *(u32 *)valp = node;
1800         }
1801
1802         /* do all our children */
1803         child = call_prom("child", 1, 1, node);
1804         while (child != 0) {
1805                 scan_dt_build_struct(child, mem_start, mem_end);
1806                 child = call_prom("peer", 1, 1, child);
1807         }
1808
1809         dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
1810 }
1811
1812 static void __init flatten_device_tree(void)
1813 {
1814         phandle root;
1815         unsigned long mem_start, mem_end, room;
1816         struct boot_param_header *hdr;
1817         struct prom_t *_prom = &RELOC(prom);
1818         char *namep;
1819         u64 *rsvmap;
1820
1821         /*
1822          * Check how much room we have between alloc top & bottom (+/- a
1823          * few pages), crop to 4Mb, as this is our "chuck" size
1824          */
1825         room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
1826         if (room > DEVTREE_CHUNK_SIZE)
1827                 room = DEVTREE_CHUNK_SIZE;
1828         prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
1829
1830         /* Now try to claim that */
1831         mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
1832         if (mem_start == 0)
1833                 prom_panic("Can't allocate initial device-tree chunk\n");
1834         mem_end = RELOC(alloc_top);
1835
1836         /* Get root of tree */
1837         root = call_prom("peer", 1, 1, (phandle)0);
1838         if (root == (phandle)0)
1839                 prom_panic ("couldn't get device tree root\n");
1840
1841         /* Build header and make room for mem rsv map */ 
1842         mem_start = _ALIGN(mem_start, 4);
1843         hdr = make_room(&mem_start, &mem_end,
1844                         sizeof(struct boot_param_header), 4);
1845         RELOC(dt_header_start) = (unsigned long)hdr;
1846         rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
1847
1848         /* Start of strings */
1849         mem_start = PAGE_ALIGN(mem_start);
1850         RELOC(dt_string_start) = mem_start;
1851         mem_start += 4; /* hole */
1852
1853         /* Add "linux,phandle" in there, we'll need it */
1854         namep = make_room(&mem_start, &mem_end, 16, 1);
1855         strcpy(namep, RELOC("linux,phandle"));
1856         mem_start = (unsigned long)namep + strlen(namep) + 1;
1857
1858         /* Build string array */
1859         prom_printf("Building dt strings...\n"); 
1860         scan_dt_build_strings(root, &mem_start, &mem_end);
1861         RELOC(dt_string_end) = mem_start;
1862
1863         /* Build structure */
1864         mem_start = PAGE_ALIGN(mem_start);
1865         RELOC(dt_struct_start) = mem_start;
1866         prom_printf("Building dt structure...\n"); 
1867         scan_dt_build_struct(root, &mem_start, &mem_end);
1868         dt_push_token(OF_DT_END, &mem_start, &mem_end);
1869         RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
1870
1871         /* Finish header */
1872         hdr->boot_cpuid_phys = _prom->cpu;
1873         hdr->magic = OF_DT_HEADER;
1874         hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
1875         hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
1876         hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
1877         hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
1878         hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
1879         hdr->version = OF_DT_VERSION;
1880         /* Version 16 is not backward compatible */
1881         hdr->last_comp_version = 0x10;
1882
1883         /* Copy the reserve map in */
1884         memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
1885
1886 #ifdef DEBUG_PROM
1887         {
1888                 int i;
1889                 prom_printf("reserved memory map:\n");
1890                 for (i = 0; i < RELOC(mem_reserve_cnt); i++)
1891                         prom_printf("  %x - %x\n",
1892                                     RELOC(mem_reserve_map)[i].base,
1893                                     RELOC(mem_reserve_map)[i].size);
1894         }
1895 #endif
1896         /* Bump mem_reserve_cnt to cause further reservations to fail
1897          * since it's too late.
1898          */
1899         RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
1900
1901         prom_printf("Device tree strings 0x%x -> 0x%x\n",
1902                     RELOC(dt_string_start), RELOC(dt_string_end)); 
1903         prom_printf("Device tree struct  0x%x -> 0x%x\n",
1904                     RELOC(dt_struct_start), RELOC(dt_struct_end));
1905
1906 }
1907
1908 #ifdef CONFIG_PPC_MAPLE
1909 /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
1910  * The values are bad, and it doesn't even have the right number of cells. */
1911 static void __init fixup_device_tree_maple(void)
1912 {
1913         phandle isa;
1914         u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
1915         u32 isa_ranges[6];
1916         char *name;
1917
1918         name = "/ht@0/isa@4";
1919         isa = call_prom("finddevice", 1, 1, ADDR(name));
1920         if (!PHANDLE_VALID(isa)) {
1921                 name = "/ht@0/isa@6";
1922                 isa = call_prom("finddevice", 1, 1, ADDR(name));
1923                 rloc = 0x01003000; /* IO space; PCI device = 6 */
1924         }
1925         if (!PHANDLE_VALID(isa))
1926                 return;
1927
1928         if (prom_getproplen(isa, "ranges") != 12)
1929                 return;
1930         if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
1931                 == PROM_ERROR)
1932                 return;
1933
1934         if (isa_ranges[0] != 0x1 ||
1935                 isa_ranges[1] != 0xf4000000 ||
1936                 isa_ranges[2] != 0x00010000)
1937                 return;
1938
1939         prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
1940
1941         isa_ranges[0] = 0x1;
1942         isa_ranges[1] = 0x0;
1943         isa_ranges[2] = rloc;
1944         isa_ranges[3] = 0x0;
1945         isa_ranges[4] = 0x0;
1946         isa_ranges[5] = 0x00010000;
1947         prom_setprop(isa, name, "ranges",
1948                         isa_ranges, sizeof(isa_ranges));
1949 }
1950
1951 #define CPC925_MC_START         0xf8000000
1952 #define CPC925_MC_LENGTH        0x1000000
1953 /* The values for memory-controller don't have right number of cells */
1954 static void __init fixup_device_tree_maple_memory_controller(void)
1955 {
1956         phandle mc;
1957         u32 mc_reg[4];
1958         char *name = "/hostbridge@f8000000";
1959         struct prom_t *_prom = &RELOC(prom);
1960         u32 ac, sc;
1961
1962         mc = call_prom("finddevice", 1, 1, ADDR(name));
1963         if (!PHANDLE_VALID(mc))
1964                 return;
1965
1966         if (prom_getproplen(mc, "reg") != 8)
1967                 return;
1968
1969         prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
1970         prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
1971         if ((ac != 2) || (sc != 2))
1972                 return;
1973
1974         if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
1975                 return;
1976
1977         if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
1978                 return;
1979
1980         prom_printf("Fixing up bogus hostbridge on Maple...\n");
1981
1982         mc_reg[0] = 0x0;
1983         mc_reg[1] = CPC925_MC_START;
1984         mc_reg[2] = 0x0;
1985         mc_reg[3] = CPC925_MC_LENGTH;
1986         prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
1987 }
1988 #else
1989 #define fixup_device_tree_maple()
1990 #define fixup_device_tree_maple_memory_controller()
1991 #endif
1992
1993 #ifdef CONFIG_PPC_CHRP
1994 /*
1995  * Pegasos and BriQ lacks the "ranges" property in the isa node
1996  * Pegasos needs decimal IRQ 14/15, not hexadecimal
1997  * Pegasos has the IDE configured in legacy mode, but advertised as native
1998  */
1999 static void __init fixup_device_tree_chrp(void)
2000 {
2001         phandle ph;
2002         u32 prop[6];
2003         u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2004         char *name;
2005         int rc;
2006
2007         name = "/pci@80000000/isa@c";
2008         ph = call_prom("finddevice", 1, 1, ADDR(name));
2009         if (!PHANDLE_VALID(ph)) {
2010                 name = "/pci@ff500000/isa@6";
2011                 ph = call_prom("finddevice", 1, 1, ADDR(name));
2012                 rloc = 0x01003000; /* IO space; PCI device = 6 */
2013         }
2014         if (PHANDLE_VALID(ph)) {
2015                 rc = prom_getproplen(ph, "ranges");
2016                 if (rc == 0 || rc == PROM_ERROR) {
2017                         prom_printf("Fixing up missing ISA range on Pegasos...\n");
2018
2019                         prop[0] = 0x1;
2020                         prop[1] = 0x0;
2021                         prop[2] = rloc;
2022                         prop[3] = 0x0;
2023                         prop[4] = 0x0;
2024                         prop[5] = 0x00010000;
2025                         prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2026                 }
2027         }
2028
2029         name = "/pci@80000000/ide@C,1";
2030         ph = call_prom("finddevice", 1, 1, ADDR(name));
2031         if (PHANDLE_VALID(ph)) {
2032                 prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2033                 prop[0] = 14;
2034                 prop[1] = 0x0;
2035                 prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2036                 prom_printf("Fixing up IDE class-code on Pegasos...\n");
2037                 rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2038                 if (rc == sizeof(u32)) {
2039                         prop[0] &= ~0x5;
2040                         prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2041                 }
2042         }
2043 }
2044 #else
2045 #define fixup_device_tree_chrp()
2046 #endif
2047
2048 #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
2049 static void __init fixup_device_tree_pmac(void)
2050 {
2051         phandle u3, i2c, mpic;
2052         u32 u3_rev;
2053         u32 interrupts[2];
2054         u32 parent;
2055
2056         /* Some G5s have a missing interrupt definition, fix it up here */
2057         u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2058         if (!PHANDLE_VALID(u3))
2059                 return;
2060         i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2061         if (!PHANDLE_VALID(i2c))
2062                 return;
2063         mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2064         if (!PHANDLE_VALID(mpic))
2065                 return;
2066
2067         /* check if proper rev of u3 */
2068         if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2069             == PROM_ERROR)
2070                 return;
2071         if (u3_rev < 0x35 || u3_rev > 0x39)
2072                 return;
2073         /* does it need fixup ? */
2074         if (prom_getproplen(i2c, "interrupts") > 0)
2075                 return;
2076
2077         prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2078
2079         /* interrupt on this revision of u3 is number 0 and level */
2080         interrupts[0] = 0;
2081         interrupts[1] = 1;
2082         prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2083                      &interrupts, sizeof(interrupts));
2084         parent = (u32)mpic;
2085         prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2086                      &parent, sizeof(parent));
2087 }
2088 #else
2089 #define fixup_device_tree_pmac()
2090 #endif
2091
2092 #ifdef CONFIG_PPC_EFIKA
2093 /*
2094  * The MPC5200 FEC driver requires an phy-handle property to tell it how
2095  * to talk to the phy.  If the phy-handle property is missing, then this
2096  * function is called to add the appropriate nodes and link it to the
2097  * ethernet node.
2098  */
2099 static void __init fixup_device_tree_efika_add_phy(void)
2100 {
2101         u32 node;
2102         char prop[64];
2103         int rv;
2104
2105         /* Check if /builtin/ethernet exists - bail if it doesn't */
2106         node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
2107         if (!PHANDLE_VALID(node))
2108                 return;
2109
2110         /* Check if the phy-handle property exists - bail if it does */
2111         rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2112         if (!rv)
2113                 return;
2114
2115         /*
2116          * At this point the ethernet device doesn't have a phy described.
2117          * Now we need to add the missing phy node and linkage
2118          */
2119
2120         /* Check for an MDIO bus node - if missing then create one */
2121         node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2122         if (!PHANDLE_VALID(node)) {
2123                 prom_printf("Adding Ethernet MDIO node\n");
2124                 call_prom("interpret", 1, 1,
2125                         " s\" /builtin\" find-device"
2126                         " new-device"
2127                                 " 1 encode-int s\" #address-cells\" property"
2128                                 " 0 encode-int s\" #size-cells\" property"
2129                                 " s\" mdio\" device-name"
2130                                 " s\" fsl,mpc5200b-mdio\" encode-string"
2131                                 " s\" compatible\" property"
2132                                 " 0xf0003000 0x400 reg"
2133                                 " 0x2 encode-int"
2134                                 " 0x5 encode-int encode+"
2135                                 " 0x3 encode-int encode+"
2136                                 " s\" interrupts\" property"
2137                         " finish-device");
2138         };
2139
2140         /* Check for a PHY device node - if missing then create one and
2141          * give it's phandle to the ethernet node */
2142         node = call_prom("finddevice", 1, 1,
2143                          ADDR("/builtin/mdio/ethernet-phy"));
2144         if (!PHANDLE_VALID(node)) {
2145                 prom_printf("Adding Ethernet PHY node\n");
2146                 call_prom("interpret", 1, 1,
2147                         " s\" /builtin/mdio\" find-device"
2148                         " new-device"
2149                                 " s\" ethernet-phy\" device-name"
2150                                 " 0x10 encode-int s\" reg\" property"
2151                                 " my-self"
2152                                 " ihandle>phandle"
2153                         " finish-device"
2154                         " s\" /builtin/ethernet\" find-device"
2155                                 " encode-int"
2156                                 " s\" phy-handle\" property"
2157                         " device-end");
2158         }
2159 }
2160
2161 static void __init fixup_device_tree_efika(void)
2162 {
2163         int sound_irq[3] = { 2, 2, 0 };
2164         int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2165                                 3,4,0, 3,5,0, 3,6,0, 3,7,0,
2166                                 3,8,0, 3,9,0, 3,10,0, 3,11,0,
2167                                 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2168         u32 node;
2169         char prop[64];
2170         int rv, len;
2171
2172         /* Check if we're really running on a EFIKA */
2173         node = call_prom("finddevice", 1, 1, ADDR("/"));
2174         if (!PHANDLE_VALID(node))
2175                 return;
2176
2177         rv = prom_getprop(node, "model", prop, sizeof(prop));
2178         if (rv == PROM_ERROR)
2179                 return;
2180         if (strcmp(prop, "EFIKA5K2"))
2181                 return;
2182
2183         prom_printf("Applying EFIKA device tree fixups\n");
2184
2185         /* Claiming to be 'chrp' is death */
2186         node = call_prom("finddevice", 1, 1, ADDR("/"));
2187         rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2188         if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2189                 prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2190
2191         /* CODEGEN,description is exposed in /proc/cpuinfo so
2192            fix that too */
2193         rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2194         if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2195                 prom_setprop(node, "/", "CODEGEN,description",
2196                              "Efika 5200B PowerPC System",
2197                              sizeof("Efika 5200B PowerPC System"));
2198
2199         /* Fixup bestcomm interrupts property */
2200         node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2201         if (PHANDLE_VALID(node)) {
2202                 len = prom_getproplen(node, "interrupts");
2203                 if (len == 12) {
2204                         prom_printf("Fixing bestcomm interrupts property\n");
2205                         prom_setprop(node, "/builtin/bestcom", "interrupts",
2206                                      bcomm_irq, sizeof(bcomm_irq));
2207                 }
2208         }
2209
2210         /* Fixup sound interrupts property */
2211         node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2212         if (PHANDLE_VALID(node)) {
2213                 rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2214                 if (rv == PROM_ERROR) {
2215                         prom_printf("Adding sound interrupts property\n");
2216                         prom_setprop(node, "/builtin/sound", "interrupts",
2217                                      sound_irq, sizeof(sound_irq));
2218                 }
2219         }
2220
2221         /* Make sure ethernet phy-handle property exists */
2222         fixup_device_tree_efika_add_phy();
2223 }
2224 #else
2225 #define fixup_device_tree_efika()
2226 #endif
2227
2228 static void __init fixup_device_tree(void)
2229 {
2230         fixup_device_tree_maple();
2231         fixup_device_tree_maple_memory_controller();
2232         fixup_device_tree_chrp();
2233         fixup_device_tree_pmac();
2234         fixup_device_tree_efika();
2235 }
2236
2237 static void __init prom_find_boot_cpu(void)
2238 {
2239         struct prom_t *_prom = &RELOC(prom);
2240         u32 getprop_rval;
2241         ihandle prom_cpu;
2242         phandle cpu_pkg;
2243
2244         _prom->cpu = 0;
2245         if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
2246                 return;
2247
2248         cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2249
2250         prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
2251         _prom->cpu = getprop_rval;
2252
2253         prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
2254 }
2255
2256 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2257 {
2258 #ifdef CONFIG_BLK_DEV_INITRD
2259         struct prom_t *_prom = &RELOC(prom);
2260
2261         if (r3 && r4 && r4 != 0xdeadbeef) {
2262                 unsigned long val;
2263
2264                 RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
2265                 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2266
2267                 val = RELOC(prom_initrd_start);
2268                 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2269                              &val, sizeof(val));
2270                 val = RELOC(prom_initrd_end);
2271                 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2272                              &val, sizeof(val));
2273
2274                 reserve_mem(RELOC(prom_initrd_start),
2275                             RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
2276
2277                 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
2278                 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
2279         }
2280 #endif /* CONFIG_BLK_DEV_INITRD */
2281 }
2282
2283 /*
2284  * We enter here early on, when the Open Firmware prom is still
2285  * handling exceptions and the MMU hash table for us.
2286  */
2287
2288 unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2289                                unsigned long pp,
2290                                unsigned long r6, unsigned long r7,
2291                                unsigned long kbase)
2292 {       
2293         struct prom_t *_prom;
2294         unsigned long hdr;
2295
2296 #ifdef CONFIG_PPC32
2297         unsigned long offset = reloc_offset();
2298         reloc_got2(offset);
2299 #endif
2300
2301         _prom = &RELOC(prom);
2302
2303         /*
2304          * First zero the BSS
2305          */
2306         memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
2307
2308         /*
2309          * Init interface to Open Firmware, get some node references,
2310          * like /chosen
2311          */
2312         prom_init_client_services(pp);
2313
2314         /*
2315          * See if this OF is old enough that we need to do explicit maps
2316          * and other workarounds
2317          */
2318         prom_find_mmu();
2319
2320         /*
2321          * Init prom stdout device
2322          */
2323         prom_init_stdout();
2324
2325         prom_printf("Preparing to boot %s", RELOC(linux_banner));
2326
2327         /*
2328          * Get default machine type. At this point, we do not differentiate
2329          * between pSeries SMP and pSeries LPAR
2330          */
2331         RELOC(of_platform) = prom_find_machine_type();
2332
2333 #ifndef CONFIG_RELOCATABLE
2334         /* Bail if this is a kdump kernel. */
2335         if (PHYSICAL_START > 0)
2336                 prom_panic("Error: You can't boot a kdump kernel from OF!\n");
2337 #endif
2338
2339         /*
2340          * Check for an initrd
2341          */
2342         prom_check_initrd(r3, r4);
2343
2344 #ifdef CONFIG_PPC_PSERIES
2345         /*
2346          * On pSeries, inform the firmware about our capabilities
2347          */
2348         if (RELOC(of_platform) == PLATFORM_PSERIES ||
2349             RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
2350                 prom_send_capabilities();
2351 #endif
2352
2353         /*
2354          * Copy the CPU hold code
2355          */
2356         if (RELOC(of_platform) != PLATFORM_POWERMAC)
2357                 copy_and_flush(0, kbase, 0x100, 0);
2358
2359         /*
2360          * Do early parsing of command line
2361          */
2362         early_cmdline_parse();
2363
2364         /*
2365          * Initialize memory management within prom_init
2366          */
2367         prom_init_mem();
2368
2369         /*
2370          * Determine which cpu is actually running right _now_
2371          */
2372         prom_find_boot_cpu();
2373
2374         /* 
2375          * Initialize display devices
2376          */
2377         prom_check_displays();
2378
2379 #ifdef CONFIG_PPC64
2380         /*
2381          * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2382          * that uses the allocator, we need to make sure we get the top of memory
2383          * available for us here...
2384          */
2385         if (RELOC(of_platform) == PLATFORM_PSERIES)
2386                 prom_initialize_tce_table();
2387 #endif
2388
2389         /*
2390          * On non-powermacs, try to instantiate RTAS and puts all CPUs
2391          * in spin-loops. PowerMacs don't have a working RTAS and use
2392          * a different way to spin CPUs
2393          */
2394         if (RELOC(of_platform) != PLATFORM_POWERMAC) {
2395                 prom_instantiate_rtas();
2396                 prom_hold_cpus();
2397         }
2398
2399         /*
2400          * Fill in some infos for use by the kernel later on
2401          */
2402 #ifdef CONFIG_PPC64
2403         if (RELOC(prom_iommu_off))
2404                 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2405                              NULL, 0);
2406
2407         if (RELOC(prom_iommu_force_on))
2408                 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2409                              NULL, 0);
2410
2411         if (RELOC(prom_tce_alloc_start)) {
2412                 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
2413                              &RELOC(prom_tce_alloc_start),
2414                              sizeof(prom_tce_alloc_start));
2415                 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
2416                              &RELOC(prom_tce_alloc_end),
2417                              sizeof(prom_tce_alloc_end));
2418         }
2419 #endif
2420
2421         /*
2422          * Fixup any known bugs in the device-tree
2423          */
2424         fixup_device_tree();
2425
2426         /*
2427          * Now finally create the flattened device-tree
2428          */
2429         prom_printf("copying OF device tree...\n");
2430         flatten_device_tree();
2431
2432         /*
2433          * in case stdin is USB and still active on IBM machines...
2434          * Unfortunately quiesce crashes on some powermacs if we have
2435          * closed stdin already (in particular the powerbook 101).
2436          */
2437         if (RELOC(of_platform) != PLATFORM_POWERMAC)
2438                 prom_close_stdin();
2439
2440         /*
2441          * Call OF "quiesce" method to shut down pending DMA's from
2442          * devices etc...
2443          */
2444         prom_printf("Calling quiesce...\n");
2445         call_prom("quiesce", 0, 0);
2446
2447         /*
2448          * And finally, call the kernel passing it the flattened device
2449          * tree and NULL as r5, thus triggering the new entry point which
2450          * is common to us and kexec
2451          */
2452         hdr = RELOC(dt_header_start);
2453         prom_printf("returning from prom_init\n");
2454         prom_debug("->dt_header_start=0x%x\n", hdr);
2455
2456 #ifdef CONFIG_PPC32
2457         reloc_got2(-offset);
2458 #endif
2459
2460         __start(hdr, kbase, 0);
2461
2462         return 0;
2463 }