blob: c78ac5698b9998731e84f8e21b81c10a6ab85f41 [file] [log] [blame]
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001/*
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>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100019#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>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100038#include <asm/mmu.h>
39#include <asm/pgtable.h>
40#include <asm/pci.h>
41#include <asm/iommu.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100042#include <asm/btext.h>
43#include <asm/sections.h>
44#include <asm/machdep.h>
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +000045#include <asm/opal.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100046
Paul Mackerras9b6b5632005-10-06 12:06:20 +100047#include <linux/linux_logo.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100048
49/*
Paul Mackerras9b6b5632005-10-06 12:06:20 +100050 * 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.
Anton Blanchard5ac47f72012-11-26 17:39:03 +000069 * ppc64 objects are always relocatable, we just need to relocate the
70 * TOC.
Paul Mackerras9b6b5632005-10-06 12:06:20 +100071 *
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 */
Anton Blanchard5ac47f72012-11-26 17:39:03 +000082#define RELOC(x) (x)
83#define ADDR(x) (u32)(unsigned long)(x)
84
Paul Mackerras9b6b5632005-10-06 12:06:20 +100085#ifdef CONFIG_PPC64
Paul Mackerrasa23414b2005-11-10 12:00:55 +110086#define OF_WORKAROUNDS 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +100087#else
Paul Mackerrasa23414b2005-11-10 12:00:55 +110088#define OF_WORKAROUNDS of_workarounds
89int of_workarounds;
Paul Mackerras9b6b5632005-10-06 12:06:20 +100090#endif
91
Paul Mackerrasa23414b2005-11-10 12:00:55 +110092#define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */
93#define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */
94
Paul Mackerras9b6b5632005-10-06 12:06:20 +100095#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
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000107
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;
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100119 phandle chosen;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000120 int cpu;
121 ihandle stdout;
Paul Mackerrasa575b802005-10-23 17:23:21 +1000122 ihandle mmumap;
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100123 ihandle memory;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000124};
125
126struct mem_map_entry {
Kumar Galacbbcf342006-01-11 17:57:13 -0600127 u64 base;
128 u64 size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000129};
130
131typedef u32 cell_t;
132
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +0000133extern 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);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000136
137#ifdef CONFIG_PPC64
Paul Mackerrasc49888202005-10-26 21:52:53 +1000138extern int enter_prom(struct prom_args *args, unsigned long entry);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000139#else
Paul Mackerrasc49888202005-10-26 21:52:53 +1000140static inline int enter_prom(struct prom_args *args, unsigned long entry)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000141{
Paul Mackerrasc49888202005-10-26 21:52:53 +1000142 return ((int (*)(struct prom_args *))entry)(args);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000143}
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
Jeremy Kerr165785e2006-11-11 17:25:18 +1100166static int __initdata prom_iommu_force_on;
167static int __initdata prom_iommu_off;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000168static unsigned long __initdata prom_tce_alloc_start;
169static unsigned long __initdata prom_tce_alloc_end;
170#endif
171
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100172/* 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
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +0000181#define PLATFORM_OPAL 0x0600
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100182
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000183static int __initdata of_platform;
184
185static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
186
Benjamin Krillcf687872009-07-27 22:02:39 +0000187static unsigned long __initdata prom_memory_limit;
188
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000189static 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
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000201/*
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
Paul Mackerrasc49888202005-10-26 21:52:53 +1000235 if (enter_prom(&args, RELOC(prom_entry)) < 0)
236 return PROM_ERROR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000237
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++)
Olaf Heringed1189b2005-11-29 14:04:05 +0100258 args.args[nargs+i] = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000259
Paul Mackerrasc49888202005-10-26 21:52:53 +1000260 if (enter_prom(&args, RELOC(prom_entry)) < 0)
261 return PROM_ERROR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000262
263 if (rets != NULL)
264 for (i = 1; i < nret; ++i)
Paul Mackerrasc5200c92005-10-10 22:57:03 +1000265 rets[i-1] = args.args[nargs+i];
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000266
267 return (nret > 0) ? args.args[nargs] : 0;
268}
269
270
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000271static 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
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000308/* 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}
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000326
327static void __init prom_printf(const char *format, ...)
328{
329 const char *p, *q, *s;
330 va_list args;
331 unsigned long v;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000332 long vs;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000333 struct prom_t *_prom = &RELOC(prom);
334
335 va_start(args, format);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000336 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;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000363 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;
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000372 case 'l':
373 ++q;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000374 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' */
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000381 ++q;
382 v = va_arg(args, unsigned long);
383 prom_print_dec(v);
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000384 } 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);
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000392 }
393 break;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000394 }
395 }
396}
397
398
Paul Mackerrasa575b802005-10-23 17:23:21 +1000399static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
400 unsigned long align)
401{
Paul Mackerrasa575b802005-10-23 17:23:21 +1000402 struct prom_t *_prom = &RELOC(prom);
403
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100404 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 */
Paul Mackerrasa575b802005-10-23 17:23:21 +1000426 call_prom("call-method", 6, 1,
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100427 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);
Paul Mackerrasa575b802005-10-23 17:23:21 +1000432}
433
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000434static void __init __attribute__((noreturn)) prom_panic(const char *reason)
435{
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000436 prom_print(reason);
Olaf Heringadd60ef2006-03-23 22:03:57 +0100437 /* 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
Stephen Rothwell1d9a4732012-03-21 18:23:27 +0000442 /* ToDo: should put up an SRC here on pSeries */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000443 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
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +1100467static int inline prom_getprop(phandle node, const char *pname,
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000468 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
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +1100474static int inline prom_getproplen(phandle node, const char *pname)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000475{
476 return call_prom("getproplen", 2, 1, node, ADDR(pname));
477}
478
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100479static void add_string(char **str, const char *q)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000480{
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100481 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);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000525}
526
Benjamin Krillcf687872009-07-27 22:02:39 +0000527/* 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
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000588/*
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);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100595 const char *opt;
Benjamin Krillcf687872009-07-27 22:02:39 +0000596
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100597 char *p;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000598 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
Amos Waterland0e4aa9c2006-06-12 23:45:02 -0400605 if (l <= 0 || p[0] == '\0') /* dbl check */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000606 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))
Jeremy Kerr165785e2006-11-11 17:25:18 +1100619 RELOC(prom_iommu_off) = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000620 else if (!strncmp(opt, RELOC("force"), 5))
Jeremy Kerr165785e2006-11-11 17:25:18 +1100621 RELOC(prom_iommu_force_on) = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000622 }
623#endif
Benjamin Krillcf687872009-07-27 22:02:39 +0000624 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 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000633}
634
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +0000635#if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000636/*
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000637 * 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.
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000643 */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000644
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 */
Joel Schopp0cb99012008-06-19 06:23:23 +1000666#define OV1_PPC_2_06 0x02 /* set if we support PowerPC 2.06 */
Michael Neulingdf77c792012-11-08 20:23:11 +0000667#define OV1_PPC_2_07 0x01 /* set if we support PowerPC 2.07 */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000668
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 */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100675#define OV3_DFP 0x20 /* decimal FP */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000676
Robert Jennings404e32e2012-05-10 08:55:49 +0000677/* Option vector 4: IBM PAPR implementation */
678#define OV4_MIN_ENT_CAP 0x01 /* minimum VP entitled capacity */
679
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000680/* 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 */
Jake Moilanend8c391a2007-06-08 07:27:11 +1000686#define OV5_DONATE_DEDICATE_CPU 0x02 /* donate dedicated CPU support */
Michael Ellerman014dad92007-05-08 12:58:35 +1000687/* 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 */
Nathan Fontenot8391e422008-07-24 04:36:38 +1000693#ifdef CONFIG_PPC_SMLPAR
694#define OV5_CMO 0x80 /* Cooperative Memory Overcommitment */
Brian King9ee820f2011-05-04 16:01:20 +1000695#define OV5_XCMO 0x40 /* Page Coalescing */
Nathan Fontenot8391e422008-07-24 04:36:38 +1000696#else
697#define OV5_CMO 0x00
Brian King9ee820f2011-05-04 16:01:20 +1000698#define OV5_XCMO 0x00
Nathan Fontenot8391e422008-07-24 04:36:38 +1000699#endif
Anton Blanchard4b83c332010-04-07 15:33:44 +0000700#define OV5_TYPE1_AFFINITY 0x80 /* Type 1 NUMA affinity */
Kent Yoder828d2b52012-04-12 05:17:51 +0000701#define OV5_PFO_HW_RNG 0x80 /* PFO Random Number Generator */
Seth Jenningsda29aa82012-07-19 09:42:39 -0500702#define OV5_PFO_HW_842 0x40 /* PFO Compression Accelerator */
Kent Yoder7e3a4fa2012-04-12 05:39:35 +0000703#define OV5_PFO_HW_ENCR 0x20 /* PFO Encryption Accelerator */
Michael Neulingdf77c792012-11-08 20:23:11 +0000704#define OV5_SUB_PROCESSORS 0x01 /* 1,2,or 4 Sub-Processors supported */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000705
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000706/* Option Vector 6: IBM PAPR hints */
707#define OV6_LINUX 0x02 /* Linux is our OS */
708
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000709/*
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+ */
Anton Blanchard03054d52006-04-29 09:51:06 +1000715 W(0xffff0000), W(0x003e0000), /* POWER6 */
Michael Neulinge952e6c2008-06-18 10:47:26 +1000716 W(0xffff0000), W(0x003f0000), /* POWER7 */
Michael Neulingdf77c792012-11-08 20:23:11 +0000717 W(0xffff0000), W(0x004b0000), /* POWER8 */
718 W(0xffffffff), W(0x0f000004), /* all 2.07-compliant */
Joel Schopp0cb99012008-06-19 06:23:23 +1000719 W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */
Paul Mackerras0efbc182006-11-29 22:31:47 +1100720 W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000721 W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000722 6 - 1, /* 6 option vectors */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000723
724 /* option vector 1: processor architectures supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500725 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000726 0, /* don't ignore, don't halt */
727 OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
Michael Neulingdf77c792012-11-08 20:23:11 +0000728 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000729
730 /* option vector 2: Open Firmware options supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500731 34 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000732 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 */
Anton Blanchard33392642011-12-04 13:13:58 +0000739 W(256), /* 256MB min RMA */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000740 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 */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500745 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000746 0, /* don't ignore, don't halt */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100747 OV3_FP | OV3_VMX | OV3_DFP,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000748
749 /* option vector 4: IBM PAPR implementation */
Robert Jennings404e32e2012-05-10 08:55:49 +0000750 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000751 0, /* don't halt */
Robert Jennings404e32e2012-05-10 08:55:49 +0000752 OV4_MIN_ENT_CAP, /* minimum VP entitled capacity */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000753
754 /* option vector 5: PAPR/OF options */
Michael Neulingdf77c792012-11-08 20:23:11 +0000755 19 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000756 0, /* don't ignore, don't halt */
Jake Moilanend8c391a2007-06-08 07:27:11 +1000757 OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
758 OV5_DONATE_DEDICATE_CPU | OV5_MSI,
Nathan Fontenot8391e422008-07-24 04:36:38 +1000759 0,
Brian King9ee820f2011-05-04 16:01:20 +1000760 OV5_CMO | OV5_XCMO,
Anton Blanchard4b83c332010-04-07 15:33:44 +0000761 OV5_TYPE1_AFFINITY,
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000762 0,
763 0,
764 0,
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100765 /* 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 */
Michael Neulingdf77c792012-11-08 20:23:11 +0000769#define IBM_ARCH_VEC_NRCORES_OFFSET 117
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100770 W(NR_CPUS), /* number of cores supported */
Kent Yoder828d2b52012-04-12 05:17:51 +0000771 0,
772 0,
773 0,
774 0,
Seth Jenningsda29aa82012-07-19 09:42:39 -0500775 OV5_PFO_HW_RNG | OV5_PFO_HW_ENCR | OV5_PFO_HW_842,
Michael Neulingdf77c792012-11-08 20:23:11 +0000776 OV5_SUB_PROCESSORS,
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000777 /* option vector 6: IBM PAPR hints */
778 4 - 2, /* length */
779 0,
780 0,
781 OV6_LINUX,
782
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000783};
784
785/* Old method - ELF header with PT_NOTE sections */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000786static 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;
Paul Mackerras5663a122008-10-31 22:27:17 +1100819} fake_elf = {
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000820 .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 = {
Paul Mackerras5663a122008-10-31 22:27:17 +1100861 .lpar_affinity = 0,
862 .min_rmo_size = 64, /* in megabytes */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000863 .min_rmo_percent = 0,
Paul Mackerras5663a122008-10-31 22:27:17 +1100864 .max_pft_size = 48, /* 2^48 bytes max PFT size */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000865 .splpar = 1,
866 .min_load = ~0U,
Paul Mackerras5663a122008-10-31 22:27:17 +1100867 .new_mem_def = 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000868 }
869 }
870};
871
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100872static 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;
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000894 prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100895
896 /* Sanity check */
897 if (plen < 1 || plen > 64) {
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000898 prom_printf("Threads per core %lu out of bounds, assuming 1\n",
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100899 (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
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000911static void __init prom_send_capabilities(void)
912{
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000913 ihandle elfloader, root;
914 prom_arg_t ret;
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100915 u32 *cores;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000916
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000917 root = call_prom("open", 1, 1, ADDR("/"));
918 if (root != 0) {
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100919 /* 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 */
Anton Blanchard5ac47f72012-11-26 17:39:03 +0000925 cores = (u32 *)&ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET];
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100926 if (*cores != NR_CPUS) {
927 prom_printf("WARNING ! "
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000928 "ibm_architecture_vec structure inconsistent: %lu!\n",
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100929 *cores);
930 } else {
Anton Blanchard33ad5e42010-06-17 14:33:06 +0000931 *cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000932 prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
933 *cores, NR_CPUS);
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100934 }
935
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000936 /* try calling the ibm,client-architecture-support method */
Anton Blanchard049d0492009-09-21 20:47:39 +0000937 prom_printf("Calling ibm,client-architecture-support...");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000938 if (call_prom_ret("call-method", 3, 2, &ret,
939 ADDR("ibm,client-architecture-support"),
Benjamin Herrenschmidt33b74972006-06-07 12:01:32 +1000940 root,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000941 ADDR(ibm_architecture_vec)) == 0) {
942 /* the call exists... */
943 if (ret)
Anton Blanchard4da727a2009-03-31 20:06:14 +0000944 prom_printf("\nWARNING: ibm,client-architecture"
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000945 "-support call FAILED!\n");
946 call_prom("close", 1, 0, root);
Anton Blanchard4da727a2009-03-31 20:06:14 +0000947 prom_printf(" done\n");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000948 return;
949 }
950 call_prom("close", 1, 0, root);
Anton Blanchard049d0492009-09-21 20:47:39 +0000951 prom_printf(" not implemented\n");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000952 }
953
954 /* no ibm,client-architecture-support call, try the old way */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000955 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{
Paul Mackerrasc49888202005-10-26 21:52:53 +10001001 unsigned long base = RELOC(alloc_bottom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001002 unsigned long addr = 0;
1003
Paul Mackerrasc49888202005-10-26 21:52:53 +10001004 if (align)
1005 base = _ALIGN_UP(base, align);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001006 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);
Paul Mackerrasc49888202005-10-26 21:52:53 +10001019 if (addr != PROM_ERROR && addr != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001020 break;
1021 addr = 0;
1022 if (align == 0)
1023 break;
1024 }
1025 if (addr == 0)
1026 return 0;
Anton Blanchard966728d2011-07-25 20:47:07 +00001027 RELOC(alloc_bottom) = addr + size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001028
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);
Paul Mackerrasc49888202005-10-26 21:52:53 +10001079 if (addr != PROM_ERROR && addr != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001080 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
Paul Mackerras35499c02005-10-22 16:02:39 +10001113 if (s > 1) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001114 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 */
Michael Ellerman0108d3f2007-05-07 15:58:28 +10001130static void __init reserve_mem(u64 base, u64 size)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001131{
Kumar Galacbbcf342006-01-11 17:57:13 -06001132 u64 top = base + size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001133 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/*
Adrian Bunkb3c2ffd2006-06-30 18:20:44 +02001154 * Initialize memory allocation mechanism, parse "memory" nodes and
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001155 * 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
Paul Mackerrasc49888202005-10-26 21:52:53 +10001185 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 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001192 if (strcmp(type, RELOC("memory")))
1193 continue;
Paul Mackerrasc49888202005-10-26 21:52:53 +10001194
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001195 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);
Benjamin Herrenschmidtab1b55e2006-03-03 10:35:40 +11001218 if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001219 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
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001227 /*
Benjamin Krillcf687872009-07-27 22:02:39 +00001228 * 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
1250 /*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001251 * 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);
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001262 RELOC(alloc_top_high) = RELOC(ram_top);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001263
Paul Mackerras64968f62011-12-13 17:54:13 +00001264 /*
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
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001273 prom_printf("memory layout at init:\n");
Benjamin Krillcf687872009-07-27 22:02:39 +00001274 prom_printf(" memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001275 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
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001282static 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
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00001299#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
1300static u64 __initdata prom_opal_base;
1301static u64 __initdata prom_opal_entry;
1302#endif
1303
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001304/* 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
Li Zhong2cb387a2012-06-07 17:44:23 +00001313static void __init prom_query_opal(void)
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001314{
1315 long rc;
1316
Benjamin Herrenschmidt76800572011-09-28 20:51:46 +00001317 /* 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
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001327 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
Li Zhong2cb387a2012-06-07 17:44:23 +00001437static void __init prom_opal_takeover(void)
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001438{
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
Benjamin Herrenschmidt817c21a2011-09-19 17:44:56 +00001470 /* Copy the command line to the kernel image */
1471 strlcpy(RELOC(boot_command_line), RELOC(prom_cmd_line),
1472 COMMAND_LINE_SIZE);
1473
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001474 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}
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00001489
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
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001559#endif /* CONFIG_PPC_POWERNV */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001560
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);
Anton Blanchard6d1e2c62011-11-14 12:55:47 +00001583 if (base == 0)
1584 prom_panic("Could not allocate memory for RTAS\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001585
1586 rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1587 if (!IHANDLE_VALID(rtas_inst)) {
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001588 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001589 return;
1590 }
1591
Anton Blanchard1f8737a2009-03-31 20:06:15 +00001592 prom_printf("instantiating rtas at 0x%x...", base);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001593
1594 if (call_prom_ret("call-method", 3, 2, &entry,
1595 ADDR("instantiate-rtas"),
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001596 rtas_inst, base) != 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001597 || entry == 0) {
1598 prom_printf(" failed\n");
1599 return;
1600 }
1601 prom_printf(" done\n");
1602
1603 reserve_mem(base, size);
1604
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001605 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));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001609
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001610#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
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001616 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
1624/*
Ashley Lai4a727422012-08-14 18:34:57 -05001625 * 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
1681/*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001682 * 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
Jeremy Kerr165785e2006-11-11 17:25:18 +11001696 if (RELOC(prom_iommu_off))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001697 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
Linas Vepstase788ff12007-09-07 03:45:21 +10001718 /* Keep the old logic intact to avoid regression. */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001719 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 */
Michael Ellermand3dbeef2012-08-19 21:44:01 +00001749 if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001750 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
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001762 /* It seems OF doesn't null-terminate the path :-( */
Li Zefanaca71ef2007-11-05 13:21:56 +11001763 memset(path, 0, PROM_SCRATCH_SIZE);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001764 /* 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
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001770 /* 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
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001774 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 */
Ingo Molnar2b931fb2009-01-06 13:56:52 +00001782 tce_entryp = (u64 *)base;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001783 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
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001804 /* 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;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001808
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 */
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001832/*
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
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001838static void __init prom_hold_cpus(void)
1839{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001840 unsigned long i;
1841 unsigned int reg;
1842 phandle node;
1843 char type[64];
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001844 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001845 unsigned long *spinloop
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001846 = (void *) LOW_ADDR(__secondary_hold_spinloop);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001847 unsigned long *acknowledge
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001848 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001849 unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001850
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
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001866 /* 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
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001881 prom_debug("cpu hw idx = %lu\n", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001882
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
Nathan Lynch7d2f6072008-07-27 15:24:50 +10001889 if (reg != _prom->cpu) {
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001890 /* Primary Thread of non-boot cpu or any thread */
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001891 prom_printf("starting cpu hw idx %lu... ", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001892 call_prom("start-cpu", 3, 0, node,
1893 secondary_hold, reg);
1894
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001895 for (i = 0; (i < 100000000) &&
1896 (*acknowledge == ((unsigned long)-1)); i++ )
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001897 mb();
1898
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001899 if (*acknowledge == reg)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001900 prom_printf("done\n");
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001901 else
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001902 prom_printf("failed: %x\n", *acknowledge);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001903 }
1904#ifdef CONFIG_SMP
1905 else
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001906 prom_printf("boot cpu hw idx %lu\n", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001907#endif /* CONFIG_SMP */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001908 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001909
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001910 prom_debug("prom_hold_cpus: end...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001911}
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 :( */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001930
1931 _prom->mmumap = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001932}
1933
Paul Mackerrasa575b802005-10-23 17:23:21 +10001934#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.
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001938 * Also, on the longtrail, we need to work around other bugs.
Paul Mackerrasa575b802005-10-23 17:23:21 +10001939 */
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;
Paul Mackerrasa575b802005-10-23 17:23:21 +10001952 /* XXX might need to add other versions here */
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001953 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
Paul Mackerrasa575b802005-10-23 17:23:21 +10001959 return;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001960 _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
Paul Mackerrasa575b802005-10-23 17:23:21 +10001961 prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1962 sizeof(_prom->mmumap));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001963 if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1964 of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001965}
1966#else
1967#define prom_find_mmu()
1968#endif
1969
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001970static 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);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001986 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1987 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001988 prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001989 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1990 path, strlen(path) + 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001991
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)
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001996 prom_setprop(val, path, "linux,boot-display", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001997}
1998
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001999static int __init prom_find_machine_type(void)
2000{
2001 struct prom_t *_prom = &RELOC(prom);
2002 char compat[256];
2003 int len, i = 0;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11002004#ifdef CONFIG_PPC64
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002005 phandle rtas;
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11002006 int x;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11002007#endif
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11002008
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002009 /* Look for a PowerMac or a Cell */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002010 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")) ||
Paul Mackerrasa575b802005-10-23 17:23:21 +10002020 strstr(p, RELOC("MacRISC")))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002021 return PLATFORM_POWERMAC;
Arnd Bergmann133dda12006-06-07 12:04:18 +10002022#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 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002031 i += sl + 1;
2032 }
2033 }
2034#ifdef CONFIG_PPC64
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002035 /* 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
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11002040 * 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 */
Michael Ellerman6f806ce2006-04-07 13:56:21 +10002045 len = prom_getprop(_prom->root, "device_type",
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11002046 compat, sizeof(compat)-1);
2047 if (len <= 0)
2048 return PLATFORM_GENERIC;
Benjamin Herrenschmidtcb6b2eb2006-05-15 15:46:03 +10002049 if (strcmp(compat, RELOC("chrp")))
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11002050 return PLATFORM_GENERIC;
2051
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002052 /* Default to pSeries. We need to know if we are running LPAR */
2053 rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11002054 if (!PHANDLE_VALID(rtas))
2055 return PLATFORM_GENERIC;
2056 x = prom_getproplen(rtas, "ibm,hypertas-functions");
2057 if (x != PROM_ERROR) {
Anton Blanchard4da727a2009-03-31 20:06:14 +00002058 prom_debug("Hypertas detected, assuming LPAR !\n");
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11002059 return PLATFORM_PSERIES_LPAR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002060 }
2061 return PLATFORM_PSERIES;
2062#else
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11002063 return PLATFORM_GENERIC;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002064#endif
2065}
2066
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002067static 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;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002086
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
Anton Blanchard4da727a2009-03-31 20:06:14 +00002107 prom_debug("Looking for displays\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002108 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;
Anton Blanchard1f8737a2009-03-31 20:06:15 +00002125 prom_printf("found display : %s, opening... ", path);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002126
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");
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002135 prom_setprop(node, path, "linux,opened", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002136
2137 /* Setup a usable color table when the appropriate
2138 * method is available. Should update this to set-colors */
2139 clut = RELOC(default_colors);
Benjamin Herrenschmidt3f536382011-12-14 13:55:11 +00002140 for (i = 0; i < 16; i++, clut += 3)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002141 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 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002152 }
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)
Anton Blanchardfbafd722011-07-25 20:47:51 +00002172 prom_panic("No memory for flatten_device_tree "
2173 "(no room)\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002174 chunk = alloc_up(room, 0);
2175 if (chunk == 0)
Anton Blanchardfbafd722011-07-25 20:47:51 +00002176 prom_panic("No memory for flatten_device_tree "
2177 "(claim failed)\n");
Anton Blanchard966728d2011-07-25 20:47:07 +00002178 *mem_end = chunk + room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002179 }
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];
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002266 int l, room, has_phandle = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002267
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;
Paul Mackerrasc49888202005-10-26 21:52:53 +10002272 room = *mem_end - *mem_start;
2273 if (room > 255)
2274 room = 255;
2275 l = call_prom("package-to-path", 3, 1, node, namep, room);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002276 if (l >= 0) {
2277 /* Didn't fit? Get more room. */
Paul Mackerrasc49888202005-10-26 21:52:53 +10002278 if (l >= room) {
2279 if (l >= *mem_end - *mem_start)
2280 namep = make_room(mem_start, mem_end, l+1, 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002281 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
Paul Mackerrasa575b802005-10-23 17:23:21 +10002286 * middle of the path in some properties, and extract
2287 * the unit name (everything after the last '/').
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002288 */
Paul Mackerrasa575b802005-10-23 17:23:21 +10002289 for (lp = p = namep, ep = namep + l; p < ep; p++) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002290 if (*p == '/')
Paul Mackerrasa575b802005-10-23 17:23:21 +10002291 lp = namep;
2292 else if (*p != 0)
2293 *lp++ = *p;
2294 }
2295 *lp = 0;
2296 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002297 }
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;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002333
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);
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002343
2344 if (!strcmp(RELOC(pname), RELOC("phandle")))
2345 has_phandle = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002346 }
2347
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002348 /* 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 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002363 }
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
Anton Blanchardfbafd722011-07-25 20:47:51 +00002386 * few pages), crop to 1MB, as this is our "chunk" size
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002387 */
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");
Anton Blanchard966728d2011-07-25 20:47:07 +00002397 mem_end = mem_start + room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002398
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
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05002446 /* Copy the reserve map in */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002447 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
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05002459 /* Bump mem_reserve_cnt to cause further reservations to fail
2460 * since it's too late.
2461 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002462 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
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002471#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)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002475{
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002476 phandle isa;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002477 u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002478 u32 isa_ranges[6];
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002479 char *name;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002480
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002481 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 }
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002488 if (!PHANDLE_VALID(isa))
2489 return;
2490
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002491 if (prom_getproplen(isa, "ranges") != 12)
2492 return;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002493 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
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002502 prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002503
2504 isa_ranges[0] = 0x1;
2505 isa_ranges[1] = 0x0;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002506 isa_ranges[2] = rloc;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002507 isa_ranges[3] = 0x0;
2508 isa_ranges[4] = 0x0;
2509 isa_ranges[5] = 0x00010000;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002510 prom_setprop(isa, name, "ranges",
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002511 isa_ranges, sizeof(isa_ranges));
2512}
Harry Ciao8f101a02009-06-17 16:28:00 -07002513
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}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002551#else
2552#define fixup_device_tree_maple()
Harry Ciao8f101a02009-06-17 16:28:00 -07002553#define fixup_device_tree_maple_memory_controller()
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002554#endif
2555
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002556#ifdef CONFIG_PPC_CHRP
Olaf Heringe4805922007-04-04 18:20:04 +02002557/*
2558 * Pegasos and BriQ lacks the "ranges" property in the isa node
2559 * Pegasos needs decimal IRQ 14/15, not hexadecimal
Olaf Hering556ecf92007-08-18 04:27:17 +10002560 * Pegasos has the IDE configured in legacy mode, but advertised as native
Olaf Heringe4805922007-04-04 18:20:04 +02002561 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002562static void __init fixup_device_tree_chrp(void)
2563{
Olaf Heringe4805922007-04-04 18:20:04 +02002564 phandle ph;
2565 u32 prop[6];
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002566 u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002567 char *name;
2568 int rc;
2569
2570 name = "/pci@80000000/isa@c";
Olaf Heringe4805922007-04-04 18:20:04 +02002571 ph = call_prom("finddevice", 1, 1, ADDR(name));
2572 if (!PHANDLE_VALID(ph)) {
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002573 name = "/pci@ff500000/isa@6";
Olaf Heringe4805922007-04-04 18:20:04 +02002574 ph = call_prom("finddevice", 1, 1, ADDR(name));
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002575 rloc = 0x01003000; /* IO space; PCI device = 6 */
2576 }
Olaf Heringe4805922007-04-04 18:20:04 +02002577 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");
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002581
Olaf Heringe4805922007-04-04 18:20:04 +02002582 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 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002591
Olaf Heringe4805922007-04-04 18:20:04 +02002592 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;
Olaf Hering556ecf92007-08-18 04:27:17 +10002598 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 }
Olaf Heringe4805922007-04-04 18:20:04 +02002605 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002606}
2607#else
2608#define fixup_device_tree_chrp()
2609#endif
2610
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002611#if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002612static void __init fixup_device_tree_pmac(void)
2613{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002614 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;
Benjamin Herrenschmidt7d496972005-11-07 14:36:21 +11002634 if (u3_rev < 0x35 || u3_rev > 0x39)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002635 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;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002645 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2646 &interrupts, sizeof(interrupts));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002647 parent = (u32)mpic;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002648 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2649 &parent, sizeof(parent));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002650}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002651#else
2652#define fixup_device_tree_pmac()
2653#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002654
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002655#ifdef CONFIG_PPC_EFIKA
Grant Likely94d2dde2008-01-24 22:25:32 -07002656/*
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)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002663{
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002664 u32 node;
2665 char prop[64];
Grant Likely94d2dde2008-01-24 22:25:32 -07002666 int rv;
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002667
Grant Likely94d2dde2008-01-24 22:25:32 -07002668 /* Check if /builtin/ethernet exists - bail if it doesn't */
2669 node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002670 if (!PHANDLE_VALID(node))
2671 return;
2672
Grant Likely94d2dde2008-01-24 22:25:32 -07002673 /* Check if the phy-handle property exists - bail if it does */
2674 rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2675 if (!rv)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002676 return;
2677
Grant Likely94d2dde2008-01-24 22:25:32 -07002678 /*
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 */
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002682
Grant Likely94d2dde2008-01-24 22:25:32 -07002683 /* Check for an MDIO bus node - if missing then create one */
Olaf Hering6f4347c2008-01-10 01:06:08 +11002684 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"
Grant Likely94d2dde2008-01-24 22:25:32 -07002692 " s\" mdio\" device-name"
2693 " s\" fsl,mpc5200b-mdio\" encode-string"
Olaf Hering6f4347c2008-01-10 01:06:08 +11002694 " 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
Grant Likely94d2dde2008-01-24 22:25:32 -07002703 /* 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"));
Olaf Hering6f4347c2008-01-10 01:06:08 +11002707 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 }
Grant Likely94d2dde2008-01-24 22:25:32 -07002722}
Olaf Hering6f4347c2008-01-10 01:06:08 +11002723
Grant Likely94d2dde2008-01-24 22:25:32 -07002724static 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
David Woodhouse7f4392c2008-04-14 02:52:38 +10002754 /* 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
Grant Likely94d2dde2008-01-24 22:25:32 -07002762 /* 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 }
2783
2784 /* Make sure ethernet phy-handle property exists */
2785 fixup_device_tree_efika_add_phy();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002786}
2787#else
2788#define fixup_device_tree_efika()
2789#endif
2790
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002791static void __init fixup_device_tree(void)
2792{
2793 fixup_device_tree_maple();
Harry Ciao8f101a02009-06-17 16:28:00 -07002794 fixup_device_tree_maple_memory_controller();
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002795 fixup_device_tree_chrp();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002796 fixup_device_tree_pmac();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002797 fixup_device_tree_efika();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002798}
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002799
2800static void __init prom_find_boot_cpu(void)
2801{
Linas Vepstase788ff12007-09-07 03:45:21 +10002802 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002803 u32 getprop_rval;
2804 ihandle prom_cpu;
2805 phandle cpu_pkg;
2806
Paul Mackerrasa575b802005-10-23 17:23:21 +10002807 _prom->cpu = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002808 if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
Paul Mackerrasa575b802005-10-23 17:23:21 +10002809 return;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002810
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
Michael Neuling2c48a7d2010-07-27 18:26:21 +00002816 prom_debug("Booting CPU hw index = %lu\n", _prom->cpu);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002817}
2818
2819static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2820{
2821#ifdef CONFIG_BLK_DEV_INITRD
Linas Vepstase788ff12007-09-07 03:45:21 +10002822 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002823
2824 if (r3 && r4 && r4 != 0xdeadbeef) {
2825 unsigned long val;
2826
Michael Ellerman51fae6de2005-12-04 18:39:15 +11002827 RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002828 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2829
2830 val = RELOC(prom_initrd_start);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002831 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2832 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002833 val = RELOC(prom_initrd_end);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002834 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2835 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002836
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
Anton Blanchard5ac47f72012-11-26 17:39:03 +00002846#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
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002893
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002894/*
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,
Paul Mackerras549e8152008-08-30 11:43:47 +10002901 unsigned long r6, unsigned long r7,
2902 unsigned long kbase)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002903{
Linas Vepstase788ff12007-09-07 03:45:21 +10002904 struct prom_t *_prom;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002905 unsigned long hdr;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002906
2907#ifdef CONFIG_PPC32
Paul Mackerras549e8152008-08-30 11:43:47 +10002908 unsigned long offset = reloc_offset();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002909 reloc_got2(offset);
Anton Blanchard5ac47f72012-11-26 17:39:03 +00002910#else
2911 reloc_toc();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002912#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 /*
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002928 * See if this OF is old enough that we need to do explicit maps
2929 * and other workarounds
2930 */
2931 prom_find_mmu();
2932
2933 /*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002934 * Init prom stdout device
2935 */
2936 prom_init_stdout();
2937
Benjamin Herrenschmidt151a9f42009-03-22 16:04:53 +00002938 prom_printf("Preparing to boot %s", RELOC(linux_banner));
Michael Ellermane7943fb2009-03-04 19:02:01 +00002939
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002940 /*
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();
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002945 prom_printf("Detected machine type: %x\n", RELOC(of_platform));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002946
Suzuki Poulose0f890c82011-12-14 22:57:15 +00002947#ifndef CONFIG_NONSTATIC_KERNEL
Olaf Heringadd60ef2006-03-23 22:03:57 +01002948 /* 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");
Paul Mackerras549e8152008-08-30 11:43:47 +10002951#endif
Olaf Heringadd60ef2006-03-23 22:03:57 +01002952
2953 /*
2954 * Check for an initrd
2955 */
2956 prom_check_initrd(r3, r4);
2957
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002958#if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002959 /*
2960 * On pSeries, inform the firmware about our capabilities
2961 */
Paul Mackerras799d6042005-11-10 13:37:51 +11002962 if (RELOC(of_platform) == PLATFORM_PSERIES ||
2963 RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002964 prom_send_capabilities();
2965#endif
2966
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002967 /*
Arnd Bergmannf3f66f52005-10-31 20:08:37 -05002968 * Copy the CPU hold code
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002969 */
Linas Vepstase788ff12007-09-07 03:45:21 +10002970 if (RELOC(of_platform) != PLATFORM_POWERMAC)
Paul Mackerras549e8152008-08-30 11:43:47 +10002971 copy_and_flush(0, kbase, 0x100, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002972
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 /*
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00003004 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
3005 * have a usable RTAS implementation.
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003006 */
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00003007 if (RELOC(of_platform) != PLATFORM_POWERMAC &&
3008 RELOC(of_platform) != PLATFORM_OPAL)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003009 prom_instantiate_rtas();
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00003010
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 }
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00003019 } else if (RELOC(of_platform) == PLATFORM_OPAL)
3020 prom_instantiate_opal();
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00003021#endif
3022
Ashley Lai4a727422012-08-14 18:34:57 -05003023#ifdef CONFIG_PPC64
3024 /* instantiate sml */
3025 prom_instantiate_sml();
3026#endif
3027
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00003028 /*
3029 * On non-powermacs, put all CPUs in spin-loops.
3030 *
3031 * PowerMacs use a different mechanism to spin CPUs
3032 */
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00003033 if (RELOC(of_platform) != PLATFORM_POWERMAC &&
3034 RELOC(of_platform) != PLATFORM_OPAL)
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00003035 prom_hold_cpus();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003036
3037 /*
3038 * Fill in some infos for use by the kernel later on
3039 */
Benjamin Krillcf687872009-07-27 22:02:39 +00003040 if (RELOC(prom_memory_limit))
3041 prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
3042 &RELOC(prom_memory_limit),
3043 sizeof(prom_memory_limit));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003044#ifdef CONFIG_PPC64
Jeremy Kerr165785e2006-11-11 17:25:18 +11003045 if (RELOC(prom_iommu_off))
Paul Mackerrasa23414b2005-11-10 12:00:55 +11003046 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
3047 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003048
Jeremy Kerr165785e2006-11-11 17:25:18 +11003049 if (RELOC(prom_iommu_force_on))
Paul Mackerrasa23414b2005-11-10 12:00:55 +11003050 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
3051 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003052
3053 if (RELOC(prom_tce_alloc_start)) {
Paul Mackerrasa23414b2005-11-10 12:00:55 +11003054 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003055 &RELOC(prom_tce_alloc_start),
3056 sizeof(prom_tce_alloc_start));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11003057 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003058 &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 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00003071 prom_printf("copying OF device tree...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003072 flatten_device_tree();
3073
Paul Mackerras3825ac02005-11-08 22:48:08 +11003074 /*
3075 * in case stdin is USB and still active on IBM machines...
3076 * Unfortunately quiesce crashes on some powermacs if we have
Benjamin Herrenschmidt40dfef62011-11-29 18:22:56 +00003077 * closed stdin already (in particular the powerbook 101). It
3078 * appears that the OPAL version of OFW doesn't like it either.
Paul Mackerras3825ac02005-11-08 22:48:08 +11003079 */
Benjamin Herrenschmidt40dfef62011-11-29 18:22:56 +00003080 if (RELOC(of_platform) != PLATFORM_POWERMAC &&
3081 RELOC(of_platform) != PLATFORM_OPAL)
Paul Mackerras3825ac02005-11-08 22:48:08 +11003082 prom_close_stdin();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003083
3084 /*
3085 * Call OF "quiesce" method to shut down pending DMA's from
3086 * devices etc...
3087 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00003088 prom_printf("Calling quiesce...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003089 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);
Benjamin Herrenschmidt40dfef62011-11-29 18:22:56 +00003097
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 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003103
3104#ifdef CONFIG_PPC32
3105 reloc_got2(-offset);
Anton Blanchard5ac47f72012-11-26 17:39:03 +00003106#else
3107 unreloc_toc();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003108#endif
3109
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00003110#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
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003117
3118 return 0;
3119}