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