Merge branch 'core/softlockup-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / arch / powerpc / kernel / prom_init.c
1 /*
2  * Procedures for interfacing to Open Firmware.
3  *
4  * Paul Mackerras       August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  * 
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com 
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #undef DEBUG_PROM
17
18 #include <stdarg.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
22 #include <linux/threads.h>
23 #include <linux/spinlock.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/proc_fs.h>
27 #include <linux/stringify.h>
28 #include <linux/delay.h>
29 #include <linux/initrd.h>
30 #include <linux/bitops.h>
31 #include <asm/prom.h>
32 #include <asm/rtas.h>
33 #include <asm/page.h>
34 #include <asm/processor.h>
35 #include <asm/irq.h>
36 #include <asm/io.h>
37 #include <asm/smp.h>
38 #include <asm/system.h>
39 #include <asm/mmu.h>
40 #include <asm/pgtable.h>
41 #include <asm/pci.h>
42 #include <asm/iommu.h>
43 #include <asm/btext.h>
44 #include <asm/sections.h>
45 #include <asm/machdep.h>
46
47 #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 #define OV1_PPC_2_06            0x02    /* set if we support PowerPC 2.06 */
624
625 /* Option vector 2: Open Firmware options supported */
626 #define OV2_REAL_MODE           0x20    /* set if we want OF in real mode */
627
628 /* Option vector 3: processor options supported */
629 #define OV3_FP                  0x80    /* floating point */
630 #define OV3_VMX                 0x40    /* VMX/Altivec */
631 #define OV3_DFP                 0x20    /* decimal FP */
632
633 /* Option vector 5: PAPR/OF options supported */
634 #define OV5_LPAR                0x80    /* logical partitioning supported */
635 #define OV5_SPLPAR              0x40    /* shared-processor LPAR supported */
636 /* ibm,dynamic-reconfiguration-memory property supported */
637 #define OV5_DRCONF_MEMORY       0x20
638 #define OV5_LARGE_PAGES         0x10    /* large pages supported */
639 #define OV5_DONATE_DEDICATE_CPU 0x02    /* donate dedicated CPU support */
640 /* PCIe/MSI support.  Without MSI full PCIe is not supported */
641 #ifdef CONFIG_PCI_MSI
642 #define OV5_MSI                 0x01    /* PCIe/MSI support */
643 #else
644 #define OV5_MSI                 0x00
645 #endif /* CONFIG_PCI_MSI */
646
647 /*
648  * The architecture vector has an array of PVR mask/value pairs,
649  * followed by # option vectors - 1, followed by the option vectors.
650  */
651 static unsigned char ibm_architecture_vec[] = {
652         W(0xfffe0000), W(0x003a0000),   /* POWER5/POWER5+ */
653         W(0xffff0000), W(0x003e0000),   /* POWER6 */
654         W(0xffff0000), W(0x003f0000),   /* POWER7 */
655         W(0xffffffff), W(0x0f000003),   /* all 2.06-compliant */
656         W(0xffffffff), W(0x0f000002),   /* all 2.05-compliant */
657         W(0xfffffffe), W(0x0f000001),   /* all 2.04-compliant and earlier */
658         5 - 1,                          /* 5 option vectors */
659
660         /* option vector 1: processor architectures supported */
661         3 - 2,                          /* length */
662         0,                              /* don't ignore, don't halt */
663         OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
664         OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
665
666         /* option vector 2: Open Firmware options supported */
667         34 - 2,                         /* length */
668         OV2_REAL_MODE,
669         0, 0,
670         W(0xffffffff),                  /* real_base */
671         W(0xffffffff),                  /* real_size */
672         W(0xffffffff),                  /* virt_base */
673         W(0xffffffff),                  /* virt_size */
674         W(0xffffffff),                  /* load_base */
675         W(64),                          /* 128MB min RMA */
676         W(0xffffffff),                  /* full client load */
677         0,                              /* min RMA percentage of total RAM */
678         48,                             /* max log_2(hash table size) */
679
680         /* option vector 3: processor options supported */
681         3 - 2,                          /* length */
682         0,                              /* don't ignore, don't halt */
683         OV3_FP | OV3_VMX | OV3_DFP,
684
685         /* option vector 4: IBM PAPR implementation */
686         2 - 2,                          /* length */
687         0,                              /* don't halt */
688
689         /* option vector 5: PAPR/OF options */
690         3 - 2,                          /* length */
691         0,                              /* don't ignore, don't halt */
692         OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
693         OV5_DONATE_DEDICATE_CPU | OV5_MSI,
694 };
695
696 /* Old method - ELF header with PT_NOTE sections */
697 static struct fake_elf {
698         Elf32_Ehdr      elfhdr;
699         Elf32_Phdr      phdr[2];
700         struct chrpnote {
701                 u32     namesz;
702                 u32     descsz;
703                 u32     type;
704                 char    name[8];        /* "PowerPC" */
705                 struct chrpdesc {
706                         u32     real_mode;
707                         u32     real_base;
708                         u32     real_size;
709                         u32     virt_base;
710                         u32     virt_size;
711                         u32     load_base;
712                 } chrpdesc;
713         } chrpnote;
714         struct rpanote {
715                 u32     namesz;
716                 u32     descsz;
717                 u32     type;
718                 char    name[24];       /* "IBM,RPA-Client-Config" */
719                 struct rpadesc {
720                         u32     lpar_affinity;
721                         u32     min_rmo_size;
722                         u32     min_rmo_percent;
723                         u32     max_pft_size;
724                         u32     splpar;
725                         u32     min_load;
726                         u32     new_mem_def;
727                         u32     ignore_me;
728                 } rpadesc;
729         } rpanote;
730 } fake_elf = {
731         .elfhdr = {
732                 .e_ident = { 0x7f, 'E', 'L', 'F',
733                              ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
734                 .e_type = ET_EXEC,      /* yeah right */
735                 .e_machine = EM_PPC,
736                 .e_version = EV_CURRENT,
737                 .e_phoff = offsetof(struct fake_elf, phdr),
738                 .e_phentsize = sizeof(Elf32_Phdr),
739                 .e_phnum = 2
740         },
741         .phdr = {
742                 [0] = {
743                         .p_type = PT_NOTE,
744                         .p_offset = offsetof(struct fake_elf, chrpnote),
745                         .p_filesz = sizeof(struct chrpnote)
746                 }, [1] = {
747                         .p_type = PT_NOTE,
748                         .p_offset = offsetof(struct fake_elf, rpanote),
749                         .p_filesz = sizeof(struct rpanote)
750                 }
751         },
752         .chrpnote = {
753                 .namesz = sizeof("PowerPC"),
754                 .descsz = sizeof(struct chrpdesc),
755                 .type = 0x1275,
756                 .name = "PowerPC",
757                 .chrpdesc = {
758                         .real_mode = ~0U,       /* ~0 means "don't care" */
759                         .real_base = ~0U,
760                         .real_size = ~0U,
761                         .virt_base = ~0U,
762                         .virt_size = ~0U,
763                         .load_base = ~0U
764                 },
765         },
766         .rpanote = {
767                 .namesz = sizeof("IBM,RPA-Client-Config"),
768                 .descsz = sizeof(struct rpadesc),
769                 .type = 0x12759999,
770                 .name = "IBM,RPA-Client-Config",
771                 .rpadesc = {
772                         .lpar_affinity = 0,
773                         .min_rmo_size = 64,     /* in megabytes */
774                         .min_rmo_percent = 0,
775                         .max_pft_size = 48,     /* 2^48 bytes max PFT size */
776                         .splpar = 1,
777                         .min_load = ~0U,
778                         .new_mem_def = 0
779                 }
780         }
781 };
782
783 static void __init prom_send_capabilities(void)
784 {
785         ihandle elfloader, root;
786         prom_arg_t ret;
787
788         root = call_prom("open", 1, 1, ADDR("/"));
789         if (root != 0) {
790                 /* try calling the ibm,client-architecture-support method */
791                 if (call_prom_ret("call-method", 3, 2, &ret,
792                                   ADDR("ibm,client-architecture-support"),
793                                   root,
794                                   ADDR(ibm_architecture_vec)) == 0) {
795                         /* the call exists... */
796                         if (ret)
797                                 prom_printf("WARNING: ibm,client-architecture"
798                                             "-support call FAILED!\n");
799                         call_prom("close", 1, 0, root);
800                         return;
801                 }
802                 call_prom("close", 1, 0, root);
803         }
804
805         /* no ibm,client-architecture-support call, try the old way */
806         elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
807         if (elfloader == 0) {
808                 prom_printf("couldn't open /packages/elf-loader\n");
809                 return;
810         }
811         call_prom("call-method", 3, 1, ADDR("process-elf-header"),
812                         elfloader, ADDR(&fake_elf));
813         call_prom("close", 1, 0, elfloader);
814 }
815 #endif
816
817 /*
818  * Memory allocation strategy... our layout is normally:
819  *
820  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
821  *  rare cases, initrd might end up being before the kernel though.
822  *  We assume this won't override the final kernel at 0, we have no
823  *  provision to handle that in this version, but it should hopefully
824  *  never happen.
825  *
826  *  alloc_top is set to the top of RMO, eventually shrink down if the
827  *  TCEs overlap
828  *
829  *  alloc_bottom is set to the top of kernel/initrd
830  *
831  *  from there, allocations are done this way : rtas is allocated
832  *  topmost, and the device-tree is allocated from the bottom. We try
833  *  to grow the device-tree allocation as we progress. If we can't,
834  *  then we fail, we don't currently have a facility to restart
835  *  elsewhere, but that shouldn't be necessary.
836  *
837  *  Note that calls to reserve_mem have to be done explicitly, memory
838  *  allocated with either alloc_up or alloc_down isn't automatically
839  *  reserved.
840  */
841
842
843 /*
844  * Allocates memory in the RMO upward from the kernel/initrd
845  *
846  * When align is 0, this is a special case, it means to allocate in place
847  * at the current location of alloc_bottom or fail (that is basically
848  * extending the previous allocation). Used for the device-tree flattening
849  */
850 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
851 {
852         unsigned long base = RELOC(alloc_bottom);
853         unsigned long addr = 0;
854
855         if (align)
856                 base = _ALIGN_UP(base, align);
857         prom_debug("alloc_up(%x, %x)\n", size, align);
858         if (RELOC(ram_top) == 0)
859                 prom_panic("alloc_up() called with mem not initialized\n");
860
861         if (align)
862                 base = _ALIGN_UP(RELOC(alloc_bottom), align);
863         else
864                 base = RELOC(alloc_bottom);
865
866         for(; (base + size) <= RELOC(alloc_top); 
867             base = _ALIGN_UP(base + 0x100000, align)) {
868                 prom_debug("    trying: 0x%x\n\r", base);
869                 addr = (unsigned long)prom_claim(base, size, 0);
870                 if (addr != PROM_ERROR && addr != 0)
871                         break;
872                 addr = 0;
873                 if (align == 0)
874                         break;
875         }
876         if (addr == 0)
877                 return 0;
878         RELOC(alloc_bottom) = addr;
879
880         prom_debug(" -> %x\n", addr);
881         prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
882         prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
883         prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
884         prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
885         prom_debug("  ram_top      : %x\n", RELOC(ram_top));
886
887         return addr;
888 }
889
890 /*
891  * Allocates memory downward, either from top of RMO, or if highmem
892  * is set, from the top of RAM.  Note that this one doesn't handle
893  * failures.  It does claim memory if highmem is not set.
894  */
895 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
896                                        int highmem)
897 {
898         unsigned long base, addr = 0;
899
900         prom_debug("alloc_down(%x, %x, %s)\n", size, align,
901                    highmem ? RELOC("(high)") : RELOC("(low)"));
902         if (RELOC(ram_top) == 0)
903                 prom_panic("alloc_down() called with mem not initialized\n");
904
905         if (highmem) {
906                 /* Carve out storage for the TCE table. */
907                 addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
908                 if (addr <= RELOC(alloc_bottom))
909                         return 0;
910                 /* Will we bump into the RMO ? If yes, check out that we
911                  * didn't overlap existing allocations there, if we did,
912                  * we are dead, we must be the first in town !
913                  */
914                 if (addr < RELOC(rmo_top)) {
915                         /* Good, we are first */
916                         if (RELOC(alloc_top) == RELOC(rmo_top))
917                                 RELOC(alloc_top) = RELOC(rmo_top) = addr;
918                         else
919                                 return 0;
920                 }
921                 RELOC(alloc_top_high) = addr;
922                 goto bail;
923         }
924
925         base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
926         for (; base > RELOC(alloc_bottom);
927              base = _ALIGN_DOWN(base - 0x100000, align))  {
928                 prom_debug("    trying: 0x%x\n\r", base);
929                 addr = (unsigned long)prom_claim(base, size, 0);
930                 if (addr != PROM_ERROR && addr != 0)
931                         break;
932                 addr = 0;
933         }
934         if (addr == 0)
935                 return 0;
936         RELOC(alloc_top) = addr;
937
938  bail:
939         prom_debug(" -> %x\n", addr);
940         prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
941         prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
942         prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
943         prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
944         prom_debug("  ram_top      : %x\n", RELOC(ram_top));
945
946         return addr;
947 }
948
949 /*
950  * Parse a "reg" cell
951  */
952 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
953 {
954         cell_t *p = *cellp;
955         unsigned long r = 0;
956
957         /* Ignore more than 2 cells */
958         while (s > sizeof(unsigned long) / 4) {
959                 p++;
960                 s--;
961         }
962         r = *p++;
963 #ifdef CONFIG_PPC64
964         if (s > 1) {
965                 r <<= 32;
966                 r |= *(p++);
967         }
968 #endif
969         *cellp = p;
970         return r;
971 }
972
973 /*
974  * Very dumb function for adding to the memory reserve list, but
975  * we don't need anything smarter at this point
976  *
977  * XXX Eventually check for collisions.  They should NEVER happen.
978  * If problems seem to show up, it would be a good start to track
979  * them down.
980  */
981 static void __init reserve_mem(u64 base, u64 size)
982 {
983         u64 top = base + size;
984         unsigned long cnt = RELOC(mem_reserve_cnt);
985
986         if (size == 0)
987                 return;
988
989         /* We need to always keep one empty entry so that we
990          * have our terminator with "size" set to 0 since we are
991          * dumb and just copy this entire array to the boot params
992          */
993         base = _ALIGN_DOWN(base, PAGE_SIZE);
994         top = _ALIGN_UP(top, PAGE_SIZE);
995         size = top - base;
996
997         if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
998                 prom_panic("Memory reserve map exhausted !\n");
999         RELOC(mem_reserve_map)[cnt].base = base;
1000         RELOC(mem_reserve_map)[cnt].size = size;
1001         RELOC(mem_reserve_cnt) = cnt + 1;
1002 }
1003
1004 /*
1005  * Initialize memory allocation mechanism, parse "memory" nodes and
1006  * obtain that way the top of memory and RMO to setup out local allocator
1007  */
1008 static void __init prom_init_mem(void)
1009 {
1010         phandle node;
1011         char *path, type[64];
1012         unsigned int plen;
1013         cell_t *p, *endp;
1014         struct prom_t *_prom = &RELOC(prom);
1015         u32 rac, rsc;
1016
1017         /*
1018          * We iterate the memory nodes to find
1019          * 1) top of RMO (first node)
1020          * 2) top of memory
1021          */
1022         rac = 2;
1023         prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
1024         rsc = 1;
1025         prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
1026         prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
1027         prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
1028
1029         prom_debug("scanning memory:\n");
1030         path = RELOC(prom_scratch);
1031
1032         for (node = 0; prom_next_node(&node); ) {
1033                 type[0] = 0;
1034                 prom_getprop(node, "device_type", type, sizeof(type));
1035
1036                 if (type[0] == 0) {
1037                         /*
1038                          * CHRP Longtrail machines have no device_type
1039                          * on the memory node, so check the name instead...
1040                          */
1041                         prom_getprop(node, "name", type, sizeof(type));
1042                 }
1043                 if (strcmp(type, RELOC("memory")))
1044                         continue;
1045
1046                 plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
1047                 if (plen > sizeof(regbuf)) {
1048                         prom_printf("memory node too large for buffer !\n");
1049                         plen = sizeof(regbuf);
1050                 }
1051                 p = RELOC(regbuf);
1052                 endp = p + (plen / sizeof(cell_t));
1053
1054 #ifdef DEBUG_PROM
1055                 memset(path, 0, PROM_SCRATCH_SIZE);
1056                 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1057                 prom_debug("  node %s :\n", path);
1058 #endif /* DEBUG_PROM */
1059
1060                 while ((endp - p) >= (rac + rsc)) {
1061                         unsigned long base, size;
1062
1063                         base = prom_next_cell(rac, &p);
1064                         size = prom_next_cell(rsc, &p);
1065
1066                         if (size == 0)
1067                                 continue;
1068                         prom_debug("    %x %x\n", base, size);
1069                         if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
1070                                 RELOC(rmo_top) = size;
1071                         if ((base + size) > RELOC(ram_top))
1072                                 RELOC(ram_top) = base + size;
1073                 }
1074         }
1075
1076         RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
1077
1078         /* Check if we have an initrd after the kernel, if we do move our bottom
1079          * point to after it
1080          */
1081         if (RELOC(prom_initrd_start)) {
1082                 if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
1083                         RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
1084         }
1085
1086         /*
1087          * Setup our top alloc point, that is top of RMO or top of
1088          * segment 0 when running non-LPAR.
1089          * Some RS64 machines have buggy firmware where claims up at
1090          * 1GB fail.  Cap at 768MB as a workaround.
1091          * Since 768MB is plenty of room, and we need to cap to something
1092          * reasonable on 32-bit, cap at 768MB on all machines.
1093          */
1094         if (!RELOC(rmo_top))
1095                 RELOC(rmo_top) = RELOC(ram_top);
1096         RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
1097         RELOC(alloc_top) = RELOC(rmo_top);
1098         RELOC(alloc_top_high) = RELOC(ram_top);
1099
1100         prom_printf("memory layout at init:\n");
1101         prom_printf("  alloc_bottom : %x\n", RELOC(alloc_bottom));
1102         prom_printf("  alloc_top    : %x\n", RELOC(alloc_top));
1103         prom_printf("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
1104         prom_printf("  rmo_top      : %x\n", RELOC(rmo_top));
1105         prom_printf("  ram_top      : %x\n", RELOC(ram_top));
1106 }
1107
1108
1109 /*
1110  * Allocate room for and instantiate RTAS
1111  */
1112 static void __init prom_instantiate_rtas(void)
1113 {
1114         phandle rtas_node;
1115         ihandle rtas_inst;
1116         u32 base, entry = 0;
1117         u32 size = 0;
1118
1119         prom_debug("prom_instantiate_rtas: start...\n");
1120
1121         rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1122         prom_debug("rtas_node: %x\n", rtas_node);
1123         if (!PHANDLE_VALID(rtas_node))
1124                 return;
1125
1126         prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1127         if (size == 0)
1128                 return;
1129
1130         base = alloc_down(size, PAGE_SIZE, 0);
1131         if (base == 0) {
1132                 prom_printf("RTAS allocation failed !\n");
1133                 return;
1134         }
1135
1136         rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1137         if (!IHANDLE_VALID(rtas_inst)) {
1138                 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
1139                 return;
1140         }
1141
1142         prom_printf("instantiating rtas at 0x%x ...", base);
1143
1144         if (call_prom_ret("call-method", 3, 2, &entry,
1145                           ADDR("instantiate-rtas"),
1146                           rtas_inst, base) != 0
1147             || entry == 0) {
1148                 prom_printf(" failed\n");
1149                 return;
1150         }
1151         prom_printf(" done\n");
1152
1153         reserve_mem(base, size);
1154
1155         prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1156                      &base, sizeof(base));
1157         prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1158                      &entry, sizeof(entry));
1159
1160         prom_debug("rtas base     = 0x%x\n", base);
1161         prom_debug("rtas entry    = 0x%x\n", entry);
1162         prom_debug("rtas size     = 0x%x\n", (long)size);
1163
1164         prom_debug("prom_instantiate_rtas: end...\n");
1165 }
1166
1167 #ifdef CONFIG_PPC64
1168 /*
1169  * Allocate room for and initialize TCE tables
1170  */
1171 static void __init prom_initialize_tce_table(void)
1172 {
1173         phandle node;
1174         ihandle phb_node;
1175         char compatible[64], type[64], model[64];
1176         char *path = RELOC(prom_scratch);
1177         u64 base, align;
1178         u32 minalign, minsize;
1179         u64 tce_entry, *tce_entryp;
1180         u64 local_alloc_top, local_alloc_bottom;
1181         u64 i;
1182
1183         if (RELOC(prom_iommu_off))
1184                 return;
1185
1186         prom_debug("starting prom_initialize_tce_table\n");
1187
1188         /* Cache current top of allocs so we reserve a single block */
1189         local_alloc_top = RELOC(alloc_top_high);
1190         local_alloc_bottom = local_alloc_top;
1191
1192         /* Search all nodes looking for PHBs. */
1193         for (node = 0; prom_next_node(&node); ) {
1194                 compatible[0] = 0;
1195                 type[0] = 0;
1196                 model[0] = 0;
1197                 prom_getprop(node, "compatible",
1198                              compatible, sizeof(compatible));
1199                 prom_getprop(node, "device_type", type, sizeof(type));
1200                 prom_getprop(node, "model", model, sizeof(model));
1201
1202                 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1203                         continue;
1204
1205                 /* Keep the old logic intact to avoid regression. */
1206                 if (compatible[0] != 0) {
1207                         if ((strstr(compatible, RELOC("python")) == NULL) &&
1208                             (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1209                             (strstr(compatible, RELOC("Winnipeg")) == NULL))
1210                                 continue;
1211                 } else if (model[0] != 0) {
1212                         if ((strstr(model, RELOC("ython")) == NULL) &&
1213                             (strstr(model, RELOC("peedwagon")) == NULL) &&
1214                             (strstr(model, RELOC("innipeg")) == NULL))
1215                                 continue;
1216                 }
1217
1218                 if (prom_getprop(node, "tce-table-minalign", &minalign,
1219                                  sizeof(minalign)) == PROM_ERROR)
1220                         minalign = 0;
1221                 if (prom_getprop(node, "tce-table-minsize", &minsize,
1222                                  sizeof(minsize)) == PROM_ERROR)
1223                         minsize = 4UL << 20;
1224
1225                 /*
1226                  * Even though we read what OF wants, we just set the table
1227                  * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
1228                  * By doing this, we avoid the pitfalls of trying to DMA to
1229                  * MMIO space and the DMA alias hole.
1230                  *
1231                  * On POWER4, firmware sets the TCE region by assuming
1232                  * each TCE table is 8MB. Using this memory for anything
1233                  * else will impact performance, so we always allocate 8MB.
1234                  * Anton
1235                  */
1236                 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1237                         minsize = 8UL << 20;
1238                 else
1239                         minsize = 4UL << 20;
1240
1241                 /* Align to the greater of the align or size */
1242                 align = max(minalign, minsize);
1243                 base = alloc_down(minsize, align, 1);
1244                 if (base == 0)
1245                         prom_panic("ERROR, cannot find space for TCE table.\n");
1246                 if (base < local_alloc_bottom)
1247                         local_alloc_bottom = base;
1248
1249                 /* It seems OF doesn't null-terminate the path :-( */
1250                 memset(path, 0, PROM_SCRATCH_SIZE);
1251                 /* Call OF to setup the TCE hardware */
1252                 if (call_prom("package-to-path", 3, 1, node,
1253                               path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1254                         prom_printf("package-to-path failed\n");
1255                 }
1256
1257                 /* Save away the TCE table attributes for later use. */
1258                 prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1259                 prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1260
1261                 prom_debug("TCE table: %s\n", path);
1262                 prom_debug("\tnode = 0x%x\n", node);
1263                 prom_debug("\tbase = 0x%x\n", base);
1264                 prom_debug("\tsize = 0x%x\n", minsize);
1265
1266                 /* Initialize the table to have a one-to-one mapping
1267                  * over the allocated size.
1268                  */
1269                 tce_entryp = (unsigned long *)base;
1270                 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1271                         tce_entry = (i << PAGE_SHIFT);
1272                         tce_entry |= 0x3;
1273                         *tce_entryp = tce_entry;
1274                 }
1275
1276                 prom_printf("opening PHB %s", path);
1277                 phb_node = call_prom("open", 1, 1, path);
1278                 if (phb_node == 0)
1279                         prom_printf("... failed\n");
1280                 else
1281                         prom_printf("... done\n");
1282
1283                 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1284                           phb_node, -1, minsize,
1285                           (u32) base, (u32) (base >> 32));
1286                 call_prom("close", 1, 0, phb_node);
1287         }
1288
1289         reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1290
1291         /* These are only really needed if there is a memory limit in
1292          * effect, but we don't know so export them always. */
1293         RELOC(prom_tce_alloc_start) = local_alloc_bottom;
1294         RELOC(prom_tce_alloc_end) = local_alloc_top;
1295
1296         /* Flag the first invalid entry */
1297         prom_debug("ending prom_initialize_tce_table\n");
1298 }
1299 #endif
1300
1301 /*
1302  * With CHRP SMP we need to use the OF to start the other processors.
1303  * We can't wait until smp_boot_cpus (the OF is trashed by then)
1304  * so we have to put the processors into a holding pattern controlled
1305  * by the kernel (not OF) before we destroy the OF.
1306  *
1307  * This uses a chunk of low memory, puts some holding pattern
1308  * code there and sends the other processors off to there until
1309  * smp_boot_cpus tells them to do something.  The holding pattern
1310  * checks that address until its cpu # is there, when it is that
1311  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
1312  * of setting those values.
1313  *
1314  * We also use physical address 0x4 here to tell when a cpu
1315  * is in its holding pattern code.
1316  *
1317  * -- Cort
1318  */
1319 extern void __secondary_hold(void);
1320 extern unsigned long __secondary_hold_spinloop;
1321 extern unsigned long __secondary_hold_acknowledge;
1322
1323 /*
1324  * We want to reference the copy of __secondary_hold_* in the
1325  * 0 - 0x100 address range
1326  */
1327 #define LOW_ADDR(x)     (((unsigned long) &(x)) & 0xff)
1328
1329 static void __init prom_hold_cpus(void)
1330 {
1331         unsigned long i;
1332         unsigned int reg;
1333         phandle node;
1334         char type[64];
1335         int cpuid = 0;
1336         unsigned int interrupt_server[MAX_CPU_THREADS];
1337         unsigned int cpu_threads, hw_cpu_num;
1338         int propsize;
1339         struct prom_t *_prom = &RELOC(prom);
1340         unsigned long *spinloop
1341                 = (void *) LOW_ADDR(__secondary_hold_spinloop);
1342         unsigned long *acknowledge
1343                 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
1344 #ifdef CONFIG_PPC64
1345         /* __secondary_hold is actually a descriptor, not the text address */
1346         unsigned long secondary_hold
1347                 = __pa(*PTRRELOC((unsigned long *)__secondary_hold));
1348 #else
1349         unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
1350 #endif
1351
1352         prom_debug("prom_hold_cpus: start...\n");
1353         prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
1354         prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
1355         prom_debug("    1) acknowledge    = 0x%x\n",
1356                    (unsigned long)acknowledge);
1357         prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
1358         prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
1359
1360         /* Set the common spinloop variable, so all of the secondary cpus
1361          * will block when they are awakened from their OF spinloop.
1362          * This must occur for both SMP and non SMP kernels, since OF will
1363          * be trashed when we move the kernel.
1364          */
1365         *spinloop = 0;
1366
1367         /* look for cpus */
1368         for (node = 0; prom_next_node(&node); ) {
1369                 type[0] = 0;
1370                 prom_getprop(node, "device_type", type, sizeof(type));
1371                 if (strcmp(type, RELOC("cpu")) != 0)
1372                         continue;
1373
1374                 /* Skip non-configured cpus. */
1375                 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1376                         if (strcmp(type, RELOC("okay")) != 0)
1377                                 continue;
1378
1379                 reg = -1;
1380                 prom_getprop(node, "reg", &reg, sizeof(reg));
1381
1382                 prom_debug("\ncpuid        = 0x%x\n", cpuid);
1383                 prom_debug("cpu hw idx   = 0x%x\n", reg);
1384
1385                 /* Init the acknowledge var which will be reset by
1386                  * the secondary cpu when it awakens from its OF
1387                  * spinloop.
1388                  */
1389                 *acknowledge = (unsigned long)-1;
1390
1391                 propsize = prom_getprop(node, "ibm,ppc-interrupt-server#s",
1392                                         &interrupt_server,
1393                                         sizeof(interrupt_server));
1394                 if (propsize < 0) {
1395                         /* no property.  old hardware has no SMT */
1396                         cpu_threads = 1;
1397                         interrupt_server[0] = reg; /* fake it with phys id */
1398                 } else {
1399                         /* We have a threaded processor */
1400                         cpu_threads = propsize / sizeof(u32);
1401                         if (cpu_threads > MAX_CPU_THREADS) {
1402                                 prom_printf("SMT: too many threads!\n"
1403                                             "SMT: found %x, max is %x\n",
1404                                             cpu_threads, MAX_CPU_THREADS);
1405                                 cpu_threads = 1; /* ToDo: panic? */
1406                         }
1407                 }
1408
1409                 hw_cpu_num = interrupt_server[0];
1410                 if (hw_cpu_num != _prom->cpu) {
1411                         /* Primary Thread of non-boot cpu */
1412                         prom_printf("%x : starting cpu hw idx %x... ", cpuid, reg);
1413                         call_prom("start-cpu", 3, 0, node,
1414                                   secondary_hold, reg);
1415
1416                         for (i = 0; (i < 100000000) && 
1417                              (*acknowledge == ((unsigned long)-1)); i++ )
1418                                 mb();
1419
1420                         if (*acknowledge == reg)
1421                                 prom_printf("done\n");
1422                         else
1423                                 prom_printf("failed: %x\n", *acknowledge);
1424                 }
1425 #ifdef CONFIG_SMP
1426                 else
1427                         prom_printf("%x : boot cpu     %x\n", cpuid, reg);
1428 #endif /* CONFIG_SMP */
1429
1430                 /* Reserve cpu #s for secondary threads.   They start later. */
1431                 cpuid += cpu_threads;
1432         }
1433
1434         if (cpuid > NR_CPUS)
1435                 prom_printf("WARNING: maximum CPUs (" __stringify(NR_CPUS)
1436                             ") exceeded: ignoring extras\n");
1437
1438         prom_debug("prom_hold_cpus: end...\n");
1439 }
1440
1441
1442 static void __init prom_init_client_services(unsigned long pp)
1443 {
1444         struct prom_t *_prom = &RELOC(prom);
1445
1446         /* Get a handle to the prom entry point before anything else */
1447         RELOC(prom_entry) = pp;
1448
1449         /* get a handle for the stdout device */
1450         _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1451         if (!PHANDLE_VALID(_prom->chosen))
1452                 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1453
1454         /* get device tree root */
1455         _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1456         if (!PHANDLE_VALID(_prom->root))
1457                 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1458
1459         _prom->mmumap = 0;
1460 }
1461
1462 #ifdef CONFIG_PPC32
1463 /*
1464  * For really old powermacs, we need to map things we claim.
1465  * For that, we need the ihandle of the mmu.
1466  * Also, on the longtrail, we need to work around other bugs.
1467  */
1468 static void __init prom_find_mmu(void)
1469 {
1470         struct prom_t *_prom = &RELOC(prom);
1471         phandle oprom;
1472         char version[64];
1473
1474         oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1475         if (!PHANDLE_VALID(oprom))
1476                 return;
1477         if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1478                 return;
1479         version[sizeof(version) - 1] = 0;
1480         /* XXX might need to add other versions here */
1481         if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1482                 of_workarounds = OF_WA_CLAIM;
1483         else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1484                 of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1485                 call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1486         } else
1487                 return;
1488         _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
1489         prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1490                      sizeof(_prom->mmumap));
1491         if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1492                 of_workarounds &= ~OF_WA_CLAIM;         /* hmmm */
1493 }
1494 #else
1495 #define prom_find_mmu()
1496 #endif
1497
1498 static void __init prom_init_stdout(void)
1499 {
1500         struct prom_t *_prom = &RELOC(prom);
1501         char *path = RELOC(of_stdout_device);
1502         char type[16];
1503         u32 val;
1504
1505         if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1506                 prom_panic("cannot find stdout");
1507
1508         _prom->stdout = val;
1509
1510         /* Get the full OF pathname of the stdout device */
1511         memset(path, 0, 256);
1512         call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1513         val = call_prom("instance-to-package", 1, 1, _prom->stdout);
1514         prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1515                      &val, sizeof(val));
1516         prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
1517         prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1518                      path, strlen(path) + 1);
1519
1520         /* If it's a display, note it */
1521         memset(type, 0, sizeof(type));
1522         prom_getprop(val, "device_type", type, sizeof(type));
1523         if (strcmp(type, RELOC("display")) == 0)
1524                 prom_setprop(val, path, "linux,boot-display", NULL, 0);
1525 }
1526
1527 static void __init prom_close_stdin(void)
1528 {
1529         struct prom_t *_prom = &RELOC(prom);
1530         ihandle val;
1531
1532         if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1533                 call_prom("close", 1, 0, val);
1534 }
1535
1536 static int __init prom_find_machine_type(void)
1537 {
1538         struct prom_t *_prom = &RELOC(prom);
1539         char compat[256];
1540         int len, i = 0;
1541 #ifdef CONFIG_PPC64
1542         phandle rtas;
1543         int x;
1544 #endif
1545
1546         /* Look for a PowerMac */
1547         len = prom_getprop(_prom->root, "compatible",
1548                            compat, sizeof(compat)-1);
1549         if (len > 0) {
1550                 compat[len] = 0;
1551                 while (i < len) {
1552                         char *p = &compat[i];
1553                         int sl = strlen(p);
1554                         if (sl == 0)
1555                                 break;
1556                         if (strstr(p, RELOC("Power Macintosh")) ||
1557                             strstr(p, RELOC("MacRISC")))
1558                                 return PLATFORM_POWERMAC;
1559 #ifdef CONFIG_PPC64
1560                         /* We must make sure we don't detect the IBM Cell
1561                          * blades as pSeries due to some firmware issues,
1562                          * so we do it here.
1563                          */
1564                         if (strstr(p, RELOC("IBM,CBEA")) ||
1565                             strstr(p, RELOC("IBM,CPBW-1.0")))
1566                                 return PLATFORM_GENERIC;
1567 #endif /* CONFIG_PPC64 */
1568                         i += sl + 1;
1569                 }
1570         }
1571 #ifdef CONFIG_PPC64
1572         /* If not a mac, try to figure out if it's an IBM pSeries or any other
1573          * PAPR compliant platform. We assume it is if :
1574          *  - /device_type is "chrp" (please, do NOT use that for future
1575          *    non-IBM designs !
1576          *  - it has /rtas
1577          */
1578         len = prom_getprop(_prom->root, "device_type",
1579                            compat, sizeof(compat)-1);
1580         if (len <= 0)
1581                 return PLATFORM_GENERIC;
1582         if (strcmp(compat, RELOC("chrp")))
1583                 return PLATFORM_GENERIC;
1584
1585         /* Default to pSeries. We need to know if we are running LPAR */
1586         rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1587         if (!PHANDLE_VALID(rtas))
1588                 return PLATFORM_GENERIC;
1589         x = prom_getproplen(rtas, "ibm,hypertas-functions");
1590         if (x != PROM_ERROR) {
1591                 prom_printf("Hypertas detected, assuming LPAR !\n");
1592                 return PLATFORM_PSERIES_LPAR;
1593         }
1594         return PLATFORM_PSERIES;
1595 #else
1596         return PLATFORM_GENERIC;
1597 #endif
1598 }
1599
1600 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1601 {
1602         return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1603 }
1604
1605 /*
1606  * If we have a display that we don't know how to drive,
1607  * we will want to try to execute OF's open method for it
1608  * later.  However, OF will probably fall over if we do that
1609  * we've taken over the MMU.
1610  * So we check whether we will need to open the display,
1611  * and if so, open it now.
1612  */
1613 static void __init prom_check_displays(void)
1614 {
1615         char type[16], *path;
1616         phandle node;
1617         ihandle ih;
1618         int i;
1619
1620         static unsigned char default_colors[] = {
1621                 0x00, 0x00, 0x00,
1622                 0x00, 0x00, 0xaa,
1623                 0x00, 0xaa, 0x00,
1624                 0x00, 0xaa, 0xaa,
1625                 0xaa, 0x00, 0x00,
1626                 0xaa, 0x00, 0xaa,
1627                 0xaa, 0xaa, 0x00,
1628                 0xaa, 0xaa, 0xaa,
1629                 0x55, 0x55, 0x55,
1630                 0x55, 0x55, 0xff,
1631                 0x55, 0xff, 0x55,
1632                 0x55, 0xff, 0xff,
1633                 0xff, 0x55, 0x55,
1634                 0xff, 0x55, 0xff,
1635                 0xff, 0xff, 0x55,
1636                 0xff, 0xff, 0xff
1637         };
1638         const unsigned char *clut;
1639
1640         prom_printf("Looking for displays\n");
1641         for (node = 0; prom_next_node(&node); ) {
1642                 memset(type, 0, sizeof(type));
1643                 prom_getprop(node, "device_type", type, sizeof(type));
1644                 if (strcmp(type, RELOC("display")) != 0)
1645                         continue;
1646
1647                 /* It seems OF doesn't null-terminate the path :-( */
1648                 path = RELOC(prom_scratch);
1649                 memset(path, 0, PROM_SCRATCH_SIZE);
1650
1651                 /*
1652                  * leave some room at the end of the path for appending extra
1653                  * arguments
1654                  */
1655                 if (call_prom("package-to-path", 3, 1, node, path,
1656                               PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1657                         continue;
1658                 prom_printf("found display   : %s, opening ... ", path);
1659                 
1660                 ih = call_prom("open", 1, 1, path);
1661                 if (ih == 0) {
1662                         prom_printf("failed\n");
1663                         continue;
1664                 }
1665
1666                 /* Success */
1667                 prom_printf("done\n");
1668                 prom_setprop(node, path, "linux,opened", NULL, 0);
1669
1670                 /* Setup a usable color table when the appropriate
1671                  * method is available. Should update this to set-colors */
1672                 clut = RELOC(default_colors);
1673                 for (i = 0; i < 32; i++, clut += 3)
1674                         if (prom_set_color(ih, i, clut[0], clut[1],
1675                                            clut[2]) != 0)
1676                                 break;
1677
1678 #ifdef CONFIG_LOGO_LINUX_CLUT224
1679                 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1680                 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1681                         if (prom_set_color(ih, i + 32, clut[0], clut[1],
1682                                            clut[2]) != 0)
1683                                 break;
1684 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
1685         }
1686 }
1687
1688
1689 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1690 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
1691                               unsigned long needed, unsigned long align)
1692 {
1693         void *ret;
1694
1695         *mem_start = _ALIGN(*mem_start, align);
1696         while ((*mem_start + needed) > *mem_end) {
1697                 unsigned long room, chunk;
1698
1699                 prom_debug("Chunk exhausted, claiming more at %x...\n",
1700                            RELOC(alloc_bottom));
1701                 room = RELOC(alloc_top) - RELOC(alloc_bottom);
1702                 if (room > DEVTREE_CHUNK_SIZE)
1703                         room = DEVTREE_CHUNK_SIZE;
1704                 if (room < PAGE_SIZE)
1705                         prom_panic("No memory for flatten_device_tree (no room)");
1706                 chunk = alloc_up(room, 0);
1707                 if (chunk == 0)
1708                         prom_panic("No memory for flatten_device_tree (claim failed)");
1709                 *mem_end = RELOC(alloc_top);
1710         }
1711
1712         ret = (void *)*mem_start;
1713         *mem_start += needed;
1714
1715         return ret;
1716 }
1717
1718 #define dt_push_token(token, mem_start, mem_end) \
1719         do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
1720
1721 static unsigned long __init dt_find_string(char *str)
1722 {
1723         char *s, *os;
1724
1725         s = os = (char *)RELOC(dt_string_start);
1726         s += 4;
1727         while (s <  (char *)RELOC(dt_string_end)) {
1728                 if (strcmp(s, str) == 0)
1729                         return s - os;
1730                 s += strlen(s) + 1;
1731         }
1732         return 0;
1733 }
1734
1735 /*
1736  * The Open Firmware 1275 specification states properties must be 31 bytes or
1737  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
1738  */
1739 #define MAX_PROPERTY_NAME 64
1740
1741 static void __init scan_dt_build_strings(phandle node,
1742                                          unsigned long *mem_start,
1743                                          unsigned long *mem_end)
1744 {
1745         char *prev_name, *namep, *sstart;
1746         unsigned long soff;
1747         phandle child;
1748
1749         sstart =  (char *)RELOC(dt_string_start);
1750
1751         /* get and store all property names */
1752         prev_name = RELOC("");
1753         for (;;) {
1754                 /* 64 is max len of name including nul. */
1755                 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
1756                 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
1757                         /* No more nodes: unwind alloc */
1758                         *mem_start = (unsigned long)namep;
1759                         break;
1760                 }
1761
1762                 /* skip "name" */
1763                 if (strcmp(namep, RELOC("name")) == 0) {
1764                         *mem_start = (unsigned long)namep;
1765                         prev_name = RELOC("name");
1766                         continue;
1767                 }
1768                 /* get/create string entry */
1769                 soff = dt_find_string(namep);
1770                 if (soff != 0) {
1771                         *mem_start = (unsigned long)namep;
1772                         namep = sstart + soff;
1773                 } else {
1774                         /* Trim off some if we can */
1775                         *mem_start = (unsigned long)namep + strlen(namep) + 1;
1776                         RELOC(dt_string_end) = *mem_start;
1777                 }
1778                 prev_name = namep;
1779         }
1780
1781         /* do all our children */
1782         child = call_prom("child", 1, 1, node);
1783         while (child != 0) {
1784                 scan_dt_build_strings(child, mem_start, mem_end);
1785                 child = call_prom("peer", 1, 1, child);
1786         }
1787 }
1788
1789 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
1790                                         unsigned long *mem_end)
1791 {
1792         phandle child;
1793         char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
1794         unsigned long soff;
1795         unsigned char *valp;
1796         static char pname[MAX_PROPERTY_NAME];
1797         int l, room;
1798
1799         dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
1800
1801         /* get the node's full name */
1802         namep = (char *)*mem_start;
1803         room = *mem_end - *mem_start;
1804         if (room > 255)
1805                 room = 255;
1806         l = call_prom("package-to-path", 3, 1, node, namep, room);
1807         if (l >= 0) {
1808                 /* Didn't fit?  Get more room. */
1809                 if (l >= room) {
1810                         if (l >= *mem_end - *mem_start)
1811                                 namep = make_room(mem_start, mem_end, l+1, 1);
1812                         call_prom("package-to-path", 3, 1, node, namep, l);
1813                 }
1814                 namep[l] = '\0';
1815
1816                 /* Fixup an Apple bug where they have bogus \0 chars in the
1817                  * middle of the path in some properties, and extract
1818                  * the unit name (everything after the last '/').
1819                  */
1820                 for (lp = p = namep, ep = namep + l; p < ep; p++) {
1821                         if (*p == '/')
1822                                 lp = namep;
1823                         else if (*p != 0)
1824                                 *lp++ = *p;
1825                 }
1826                 *lp = 0;
1827                 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
1828         }
1829
1830         /* get it again for debugging */
1831         path = RELOC(prom_scratch);
1832         memset(path, 0, PROM_SCRATCH_SIZE);
1833         call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1834
1835         /* get and store all properties */
1836         prev_name = RELOC("");
1837         sstart = (char *)RELOC(dt_string_start);
1838         for (;;) {
1839                 if (call_prom("nextprop", 3, 1, node, prev_name,
1840                               RELOC(pname)) != 1)
1841                         break;
1842
1843                 /* skip "name" */
1844                 if (strcmp(RELOC(pname), RELOC("name")) == 0) {
1845                         prev_name = RELOC("name");
1846                         continue;
1847                 }
1848
1849                 /* find string offset */
1850                 soff = dt_find_string(RELOC(pname));
1851                 if (soff == 0) {
1852                         prom_printf("WARNING: Can't find string index for"
1853                                     " <%s>, node %s\n", RELOC(pname), path);
1854                         break;
1855                 }
1856                 prev_name = sstart + soff;
1857
1858                 /* get length */
1859                 l = call_prom("getproplen", 2, 1, node, RELOC(pname));
1860
1861                 /* sanity checks */
1862                 if (l == PROM_ERROR)
1863                         continue;
1864                 if (l > MAX_PROPERTY_LENGTH) {
1865                         prom_printf("WARNING: ignoring large property ");
1866                         /* It seems OF doesn't null-terminate the path :-( */
1867                         prom_printf("[%s] ", path);
1868                         prom_printf("%s length 0x%x\n", RELOC(pname), l);
1869                         continue;
1870                 }
1871
1872                 /* push property head */
1873                 dt_push_token(OF_DT_PROP, mem_start, mem_end);
1874                 dt_push_token(l, mem_start, mem_end);
1875                 dt_push_token(soff, mem_start, mem_end);
1876
1877                 /* push property content */
1878                 valp = make_room(mem_start, mem_end, l, 4);
1879                 call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
1880                 *mem_start = _ALIGN(*mem_start, 4);
1881         }
1882
1883         /* Add a "linux,phandle" property. */
1884         soff = dt_find_string(RELOC("linux,phandle"));
1885         if (soff == 0)
1886                 prom_printf("WARNING: Can't find string index for"
1887                             " <linux-phandle> node %s\n", path);
1888         else {
1889                 dt_push_token(OF_DT_PROP, mem_start, mem_end);
1890                 dt_push_token(4, mem_start, mem_end);
1891                 dt_push_token(soff, mem_start, mem_end);
1892                 valp = make_room(mem_start, mem_end, 4, 4);
1893                 *(u32 *)valp = node;
1894         }
1895
1896         /* do all our children */
1897         child = call_prom("child", 1, 1, node);
1898         while (child != 0) {
1899                 scan_dt_build_struct(child, mem_start, mem_end);
1900                 child = call_prom("peer", 1, 1, child);
1901         }
1902
1903         dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
1904 }
1905
1906 static void __init flatten_device_tree(void)
1907 {
1908         phandle root;
1909         unsigned long mem_start, mem_end, room;
1910         struct boot_param_header *hdr;
1911         struct prom_t *_prom = &RELOC(prom);
1912         char *namep;
1913         u64 *rsvmap;
1914
1915         /*
1916          * Check how much room we have between alloc top & bottom (+/- a
1917          * few pages), crop to 4Mb, as this is our "chuck" size
1918          */
1919         room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
1920         if (room > DEVTREE_CHUNK_SIZE)
1921                 room = DEVTREE_CHUNK_SIZE;
1922         prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
1923
1924         /* Now try to claim that */
1925         mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
1926         if (mem_start == 0)
1927                 prom_panic("Can't allocate initial device-tree chunk\n");
1928         mem_end = RELOC(alloc_top);
1929
1930         /* Get root of tree */
1931         root = call_prom("peer", 1, 1, (phandle)0);
1932         if (root == (phandle)0)
1933                 prom_panic ("couldn't get device tree root\n");
1934
1935         /* Build header and make room for mem rsv map */ 
1936         mem_start = _ALIGN(mem_start, 4);
1937         hdr = make_room(&mem_start, &mem_end,
1938                         sizeof(struct boot_param_header), 4);
1939         RELOC(dt_header_start) = (unsigned long)hdr;
1940         rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
1941
1942         /* Start of strings */
1943         mem_start = PAGE_ALIGN(mem_start);
1944         RELOC(dt_string_start) = mem_start;
1945         mem_start += 4; /* hole */
1946
1947         /* Add "linux,phandle" in there, we'll need it */
1948         namep = make_room(&mem_start, &mem_end, 16, 1);
1949         strcpy(namep, RELOC("linux,phandle"));
1950         mem_start = (unsigned long)namep + strlen(namep) + 1;
1951
1952         /* Build string array */
1953         prom_printf("Building dt strings...\n"); 
1954         scan_dt_build_strings(root, &mem_start, &mem_end);
1955         RELOC(dt_string_end) = mem_start;
1956
1957         /* Build structure */
1958         mem_start = PAGE_ALIGN(mem_start);
1959         RELOC(dt_struct_start) = mem_start;
1960         prom_printf("Building dt structure...\n"); 
1961         scan_dt_build_struct(root, &mem_start, &mem_end);
1962         dt_push_token(OF_DT_END, &mem_start, &mem_end);
1963         RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
1964
1965         /* Finish header */
1966         hdr->boot_cpuid_phys = _prom->cpu;
1967         hdr->magic = OF_DT_HEADER;
1968         hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
1969         hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
1970         hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
1971         hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
1972         hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
1973         hdr->version = OF_DT_VERSION;
1974         /* Version 16 is not backward compatible */
1975         hdr->last_comp_version = 0x10;
1976
1977         /* Copy the reserve map in */
1978         memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
1979
1980 #ifdef DEBUG_PROM
1981         {
1982                 int i;
1983                 prom_printf("reserved memory map:\n");
1984                 for (i = 0; i < RELOC(mem_reserve_cnt); i++)
1985                         prom_printf("  %x - %x\n",
1986                                     RELOC(mem_reserve_map)[i].base,
1987                                     RELOC(mem_reserve_map)[i].size);
1988         }
1989 #endif
1990         /* Bump mem_reserve_cnt to cause further reservations to fail
1991          * since it's too late.
1992          */
1993         RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
1994
1995         prom_printf("Device tree strings 0x%x -> 0x%x\n",
1996                     RELOC(dt_string_start), RELOC(dt_string_end)); 
1997         prom_printf("Device tree struct  0x%x -> 0x%x\n",
1998                     RELOC(dt_struct_start), RELOC(dt_struct_end));
1999
2000 }
2001
2002 #ifdef CONFIG_PPC_MAPLE
2003 /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2004  * The values are bad, and it doesn't even have the right number of cells. */
2005 static void __init fixup_device_tree_maple(void)
2006 {
2007         phandle isa;
2008         u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
2009         u32 isa_ranges[6];
2010         char *name;
2011
2012         name = "/ht@0/isa@4";
2013         isa = call_prom("finddevice", 1, 1, ADDR(name));
2014         if (!PHANDLE_VALID(isa)) {
2015                 name = "/ht@0/isa@6";
2016                 isa = call_prom("finddevice", 1, 1, ADDR(name));
2017                 rloc = 0x01003000; /* IO space; PCI device = 6 */
2018         }
2019         if (!PHANDLE_VALID(isa))
2020                 return;
2021
2022         if (prom_getproplen(isa, "ranges") != 12)
2023                 return;
2024         if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2025                 == PROM_ERROR)
2026                 return;
2027
2028         if (isa_ranges[0] != 0x1 ||
2029                 isa_ranges[1] != 0xf4000000 ||
2030                 isa_ranges[2] != 0x00010000)
2031                 return;
2032
2033         prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
2034
2035         isa_ranges[0] = 0x1;
2036         isa_ranges[1] = 0x0;
2037         isa_ranges[2] = rloc;
2038         isa_ranges[3] = 0x0;
2039         isa_ranges[4] = 0x0;
2040         isa_ranges[5] = 0x00010000;
2041         prom_setprop(isa, name, "ranges",
2042                         isa_ranges, sizeof(isa_ranges));
2043 }
2044 #else
2045 #define fixup_device_tree_maple()
2046 #endif
2047
2048 #ifdef CONFIG_PPC_CHRP
2049 /*
2050  * Pegasos and BriQ lacks the "ranges" property in the isa node
2051  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2052  * Pegasos has the IDE configured in legacy mode, but advertised as native
2053  */
2054 static void __init fixup_device_tree_chrp(void)
2055 {
2056         phandle ph;
2057         u32 prop[6];
2058         u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2059         char *name;
2060         int rc;
2061
2062         name = "/pci@80000000/isa@c";
2063         ph = call_prom("finddevice", 1, 1, ADDR(name));
2064         if (!PHANDLE_VALID(ph)) {
2065                 name = "/pci@ff500000/isa@6";
2066                 ph = call_prom("finddevice", 1, 1, ADDR(name));
2067                 rloc = 0x01003000; /* IO space; PCI device = 6 */
2068         }
2069         if (PHANDLE_VALID(ph)) {
2070                 rc = prom_getproplen(ph, "ranges");
2071                 if (rc == 0 || rc == PROM_ERROR) {
2072                         prom_printf("Fixing up missing ISA range on Pegasos...\n");
2073
2074                         prop[0] = 0x1;
2075                         prop[1] = 0x0;
2076                         prop[2] = rloc;
2077                         prop[3] = 0x0;
2078                         prop[4] = 0x0;
2079                         prop[5] = 0x00010000;
2080                         prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2081                 }
2082         }
2083
2084         name = "/pci@80000000/ide@C,1";
2085         ph = call_prom("finddevice", 1, 1, ADDR(name));
2086         if (PHANDLE_VALID(ph)) {
2087                 prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2088                 prop[0] = 14;
2089                 prop[1] = 0x0;
2090                 prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2091                 prom_printf("Fixing up IDE class-code on Pegasos...\n");
2092                 rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2093                 if (rc == sizeof(u32)) {
2094                         prop[0] &= ~0x5;
2095                         prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2096                 }
2097         }
2098 }
2099 #else
2100 #define fixup_device_tree_chrp()
2101 #endif
2102
2103 #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
2104 static void __init fixup_device_tree_pmac(void)
2105 {
2106         phandle u3, i2c, mpic;
2107         u32 u3_rev;
2108         u32 interrupts[2];
2109         u32 parent;
2110
2111         /* Some G5s have a missing interrupt definition, fix it up here */
2112         u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2113         if (!PHANDLE_VALID(u3))
2114                 return;
2115         i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2116         if (!PHANDLE_VALID(i2c))
2117                 return;
2118         mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2119         if (!PHANDLE_VALID(mpic))
2120                 return;
2121
2122         /* check if proper rev of u3 */
2123         if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2124             == PROM_ERROR)
2125                 return;
2126         if (u3_rev < 0x35 || u3_rev > 0x39)
2127                 return;
2128         /* does it need fixup ? */
2129         if (prom_getproplen(i2c, "interrupts") > 0)
2130                 return;
2131
2132         prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2133
2134         /* interrupt on this revision of u3 is number 0 and level */
2135         interrupts[0] = 0;
2136         interrupts[1] = 1;
2137         prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2138                      &interrupts, sizeof(interrupts));
2139         parent = (u32)mpic;
2140         prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2141                      &parent, sizeof(parent));
2142 }
2143 #else
2144 #define fixup_device_tree_pmac()
2145 #endif
2146
2147 #ifdef CONFIG_PPC_EFIKA
2148 /*
2149  * The MPC5200 FEC driver requires an phy-handle property to tell it how
2150  * to talk to the phy.  If the phy-handle property is missing, then this
2151  * function is called to add the appropriate nodes and link it to the
2152  * ethernet node.
2153  */
2154 static void __init fixup_device_tree_efika_add_phy(void)
2155 {
2156         u32 node;
2157         char prop[64];
2158         int rv;
2159
2160         /* Check if /builtin/ethernet exists - bail if it doesn't */
2161         node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
2162         if (!PHANDLE_VALID(node))
2163                 return;
2164
2165         /* Check if the phy-handle property exists - bail if it does */
2166         rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2167         if (!rv)
2168                 return;
2169
2170         /*
2171          * At this point the ethernet device doesn't have a phy described.
2172          * Now we need to add the missing phy node and linkage
2173          */
2174
2175         /* Check for an MDIO bus node - if missing then create one */
2176         node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2177         if (!PHANDLE_VALID(node)) {
2178                 prom_printf("Adding Ethernet MDIO node\n");
2179                 call_prom("interpret", 1, 1,
2180                         " s\" /builtin\" find-device"
2181                         " new-device"
2182                                 " 1 encode-int s\" #address-cells\" property"
2183                                 " 0 encode-int s\" #size-cells\" property"
2184                                 " s\" mdio\" device-name"
2185                                 " s\" fsl,mpc5200b-mdio\" encode-string"
2186                                 " s\" compatible\" property"
2187                                 " 0xf0003000 0x400 reg"
2188                                 " 0x2 encode-int"
2189                                 " 0x5 encode-int encode+"
2190                                 " 0x3 encode-int encode+"
2191                                 " s\" interrupts\" property"
2192                         " finish-device");
2193         };
2194
2195         /* Check for a PHY device node - if missing then create one and
2196          * give it's phandle to the ethernet node */
2197         node = call_prom("finddevice", 1, 1,
2198                          ADDR("/builtin/mdio/ethernet-phy"));
2199         if (!PHANDLE_VALID(node)) {
2200                 prom_printf("Adding Ethernet PHY node\n");
2201                 call_prom("interpret", 1, 1,
2202                         " s\" /builtin/mdio\" find-device"
2203                         " new-device"
2204                                 " s\" ethernet-phy\" device-name"
2205                                 " 0x10 encode-int s\" reg\" property"
2206                                 " my-self"
2207                                 " ihandle>phandle"
2208                         " finish-device"
2209                         " s\" /builtin/ethernet\" find-device"
2210                                 " encode-int"
2211                                 " s\" phy-handle\" property"
2212                         " device-end");
2213         }
2214 }
2215
2216 static void __init fixup_device_tree_efika(void)
2217 {
2218         int sound_irq[3] = { 2, 2, 0 };
2219         int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2220                                 3,4,0, 3,5,0, 3,6,0, 3,7,0,
2221                                 3,8,0, 3,9,0, 3,10,0, 3,11,0,
2222                                 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2223         u32 node;
2224         char prop[64];
2225         int rv, len;
2226
2227         /* Check if we're really running on a EFIKA */
2228         node = call_prom("finddevice", 1, 1, ADDR("/"));
2229         if (!PHANDLE_VALID(node))
2230                 return;
2231
2232         rv = prom_getprop(node, "model", prop, sizeof(prop));
2233         if (rv == PROM_ERROR)
2234                 return;
2235         if (strcmp(prop, "EFIKA5K2"))
2236                 return;
2237
2238         prom_printf("Applying EFIKA device tree fixups\n");
2239
2240         /* Claiming to be 'chrp' is death */
2241         node = call_prom("finddevice", 1, 1, ADDR("/"));
2242         rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2243         if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2244                 prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2245
2246         /* CODEGEN,description is exposed in /proc/cpuinfo so
2247            fix that too */
2248         rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2249         if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2250                 prom_setprop(node, "/", "CODEGEN,description",
2251                              "Efika 5200B PowerPC System",
2252                              sizeof("Efika 5200B PowerPC System"));
2253
2254         /* Fixup bestcomm interrupts property */
2255         node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2256         if (PHANDLE_VALID(node)) {
2257                 len = prom_getproplen(node, "interrupts");
2258                 if (len == 12) {
2259                         prom_printf("Fixing bestcomm interrupts property\n");
2260                         prom_setprop(node, "/builtin/bestcom", "interrupts",
2261                                      bcomm_irq, sizeof(bcomm_irq));
2262                 }
2263         }
2264
2265         /* Fixup sound interrupts property */
2266         node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2267         if (PHANDLE_VALID(node)) {
2268                 rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2269                 if (rv == PROM_ERROR) {
2270                         prom_printf("Adding sound interrupts property\n");
2271                         prom_setprop(node, "/builtin/sound", "interrupts",
2272                                      sound_irq, sizeof(sound_irq));
2273                 }
2274         }
2275
2276         /* Make sure ethernet phy-handle property exists */
2277         fixup_device_tree_efika_add_phy();
2278 }
2279 #else
2280 #define fixup_device_tree_efika()
2281 #endif
2282
2283 static void __init fixup_device_tree(void)
2284 {
2285         fixup_device_tree_maple();
2286         fixup_device_tree_chrp();
2287         fixup_device_tree_pmac();
2288         fixup_device_tree_efika();
2289 }
2290
2291 static void __init prom_find_boot_cpu(void)
2292 {
2293         struct prom_t *_prom = &RELOC(prom);
2294         u32 getprop_rval;
2295         ihandle prom_cpu;
2296         phandle cpu_pkg;
2297
2298         _prom->cpu = 0;
2299         if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
2300                 return;
2301
2302         cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2303
2304         prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
2305         _prom->cpu = getprop_rval;
2306
2307         prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
2308 }
2309
2310 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2311 {
2312 #ifdef CONFIG_BLK_DEV_INITRD
2313         struct prom_t *_prom = &RELOC(prom);
2314
2315         if (r3 && r4 && r4 != 0xdeadbeef) {
2316                 unsigned long val;
2317
2318                 RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
2319                 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2320
2321                 val = RELOC(prom_initrd_start);
2322                 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2323                              &val, sizeof(val));
2324                 val = RELOC(prom_initrd_end);
2325                 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2326                              &val, sizeof(val));
2327
2328                 reserve_mem(RELOC(prom_initrd_start),
2329                             RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
2330
2331                 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
2332                 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
2333         }
2334 #endif /* CONFIG_BLK_DEV_INITRD */
2335 }
2336
2337 /*
2338  * We enter here early on, when the Open Firmware prom is still
2339  * handling exceptions and the MMU hash table for us.
2340  */
2341
2342 unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2343                                unsigned long pp,
2344                                unsigned long r6, unsigned long r7)
2345 {       
2346         struct prom_t *_prom;
2347         unsigned long hdr;
2348         unsigned long offset = reloc_offset();
2349
2350 #ifdef CONFIG_PPC32
2351         reloc_got2(offset);
2352 #endif
2353
2354         _prom = &RELOC(prom);
2355
2356         /*
2357          * First zero the BSS
2358          */
2359         memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
2360
2361         /*
2362          * Init interface to Open Firmware, get some node references,
2363          * like /chosen
2364          */
2365         prom_init_client_services(pp);
2366
2367         /*
2368          * See if this OF is old enough that we need to do explicit maps
2369          * and other workarounds
2370          */
2371         prom_find_mmu();
2372
2373         /*
2374          * Init prom stdout device
2375          */
2376         prom_init_stdout();
2377
2378         /*
2379          * Get default machine type. At this point, we do not differentiate
2380          * between pSeries SMP and pSeries LPAR
2381          */
2382         RELOC(of_platform) = prom_find_machine_type();
2383
2384         /* Bail if this is a kdump kernel. */
2385         if (PHYSICAL_START > 0)
2386                 prom_panic("Error: You can't boot a kdump kernel from OF!\n");
2387
2388         /*
2389          * Check for an initrd
2390          */
2391         prom_check_initrd(r3, r4);
2392
2393 #ifdef CONFIG_PPC_PSERIES
2394         /*
2395          * On pSeries, inform the firmware about our capabilities
2396          */
2397         if (RELOC(of_platform) == PLATFORM_PSERIES ||
2398             RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
2399                 prom_send_capabilities();
2400 #endif
2401
2402         /*
2403          * Copy the CPU hold code
2404          */
2405         if (RELOC(of_platform) != PLATFORM_POWERMAC)
2406                 copy_and_flush(0, KERNELBASE + offset, 0x100, 0);
2407
2408         /*
2409          * Do early parsing of command line
2410          */
2411         early_cmdline_parse();
2412
2413         /*
2414          * Initialize memory management within prom_init
2415          */
2416         prom_init_mem();
2417
2418         /*
2419          * Determine which cpu is actually running right _now_
2420          */
2421         prom_find_boot_cpu();
2422
2423         /* 
2424          * Initialize display devices
2425          */
2426         prom_check_displays();
2427
2428 #ifdef CONFIG_PPC64
2429         /*
2430          * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2431          * that uses the allocator, we need to make sure we get the top of memory
2432          * available for us here...
2433          */
2434         if (RELOC(of_platform) == PLATFORM_PSERIES)
2435                 prom_initialize_tce_table();
2436 #endif
2437
2438         /*
2439          * On non-powermacs, try to instantiate RTAS and puts all CPUs
2440          * in spin-loops. PowerMacs don't have a working RTAS and use
2441          * a different way to spin CPUs
2442          */
2443         if (RELOC(of_platform) != PLATFORM_POWERMAC) {
2444                 prom_instantiate_rtas();
2445                 prom_hold_cpus();
2446         }
2447
2448         /*
2449          * Fill in some infos for use by the kernel later on
2450          */
2451 #ifdef CONFIG_PPC64
2452         if (RELOC(prom_iommu_off))
2453                 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2454                              NULL, 0);
2455
2456         if (RELOC(prom_iommu_force_on))
2457                 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2458                              NULL, 0);
2459
2460         if (RELOC(prom_tce_alloc_start)) {
2461                 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
2462                              &RELOC(prom_tce_alloc_start),
2463                              sizeof(prom_tce_alloc_start));
2464                 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
2465                              &RELOC(prom_tce_alloc_end),
2466                              sizeof(prom_tce_alloc_end));
2467         }
2468 #endif
2469
2470         /*
2471          * Fixup any known bugs in the device-tree
2472          */
2473         fixup_device_tree();
2474
2475         /*
2476          * Now finally create the flattened device-tree
2477          */
2478         prom_printf("copying OF device tree ...\n");
2479         flatten_device_tree();
2480
2481         /*
2482          * in case stdin is USB and still active on IBM machines...
2483          * Unfortunately quiesce crashes on some powermacs if we have
2484          * closed stdin already (in particular the powerbook 101).
2485          */
2486         if (RELOC(of_platform) != PLATFORM_POWERMAC)
2487                 prom_close_stdin();
2488
2489         /*
2490          * Call OF "quiesce" method to shut down pending DMA's from
2491          * devices etc...
2492          */
2493         prom_printf("Calling quiesce ...\n");
2494         call_prom("quiesce", 0, 0);
2495
2496         /*
2497          * And finally, call the kernel passing it the flattened device
2498          * tree and NULL as r5, thus triggering the new entry point which
2499          * is common to us and kexec
2500          */
2501         hdr = RELOC(dt_header_start);
2502         prom_printf("returning from prom_init\n");
2503         prom_debug("->dt_header_start=0x%x\n", hdr);
2504
2505 #ifdef CONFIG_PPC32
2506         reloc_got2(-offset);
2507 #endif
2508
2509         __start(hdr, KERNELBASE + offset, 0);
2510
2511         return 0;
2512 }