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