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