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