blob: 5eccda9fd33f5396b5a5c231db1c8051bcacf747 [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 ADDR(x) (u32)(unsigned long)(x)
83
Paul Mackerras9b6b5632005-10-06 12:06:20 +100084#ifdef CONFIG_PPC64
Paul Mackerrasa23414b2005-11-10 12:00:55 +110085#define OF_WORKAROUNDS 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +100086#else
Paul Mackerrasa23414b2005-11-10 12:00:55 +110087#define OF_WORKAROUNDS of_workarounds
88int of_workarounds;
Paul Mackerras9b6b5632005-10-06 12:06:20 +100089#endif
90
Paul Mackerrasa23414b2005-11-10 12:00:55 +110091#define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */
92#define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */
93
Paul Mackerras9b6b5632005-10-06 12:06:20 +100094#define PROM_BUG() do { \
95 prom_printf("kernel BUG at %s line 0x%x!\n", \
Anton Blanchard5827d412012-11-26 17:40:03 +000096 __FILE__, __LINE__); \
Paul Mackerras9b6b5632005-10-06 12:06:20 +100097 __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
98} while (0)
99
100#ifdef DEBUG_PROM
101#define prom_debug(x...) prom_printf(x)
102#else
103#define prom_debug(x...)
104#endif
105
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000106
107typedef u32 prom_arg_t;
108
109struct prom_args {
110 u32 service;
111 u32 nargs;
112 u32 nret;
113 prom_arg_t args[10];
114};
115
116struct prom_t {
117 ihandle root;
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100118 phandle chosen;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000119 int cpu;
120 ihandle stdout;
Paul Mackerrasa575b802005-10-23 17:23:21 +1000121 ihandle mmumap;
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100122 ihandle memory;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000123};
124
125struct mem_map_entry {
Kumar Galacbbcf342006-01-11 17:57:13 -0600126 u64 base;
127 u64 size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000128};
129
130typedef u32 cell_t;
131
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +0000132extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
133 unsigned long r6, unsigned long r7, unsigned long r8,
134 unsigned long r9);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000135
136#ifdef CONFIG_PPC64
Paul Mackerrasc49888202005-10-26 21:52:53 +1000137extern int enter_prom(struct prom_args *args, unsigned long entry);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000138#else
Paul Mackerrasc49888202005-10-26 21:52:53 +1000139static inline int enter_prom(struct prom_args *args, unsigned long entry)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000140{
Paul Mackerrasc49888202005-10-26 21:52:53 +1000141 return ((int (*)(struct prom_args *))entry)(args);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000142}
143#endif
144
145extern void copy_and_flush(unsigned long dest, unsigned long src,
146 unsigned long size, unsigned long offset);
147
148/* prom structure */
149static struct prom_t __initdata prom;
150
151static unsigned long prom_entry __initdata;
152
153#define PROM_SCRATCH_SIZE 256
154
155static char __initdata of_stdout_device[256];
156static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
157
158static unsigned long __initdata dt_header_start;
159static unsigned long __initdata dt_struct_start, dt_struct_end;
160static unsigned long __initdata dt_string_start, dt_string_end;
161
162static unsigned long __initdata prom_initrd_start, prom_initrd_end;
163
164#ifdef CONFIG_PPC64
Jeremy Kerr165785e2006-11-11 17:25:18 +1100165static int __initdata prom_iommu_force_on;
166static int __initdata prom_iommu_off;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000167static unsigned long __initdata prom_tce_alloc_start;
168static unsigned long __initdata prom_tce_alloc_end;
169#endif
170
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100171/* Platforms codes are now obsolete in the kernel. Now only used within this
172 * file and ultimately gone too. Feel free to change them if you need, they
173 * are not shared with anything outside of this file anymore
174 */
175#define PLATFORM_PSERIES 0x0100
176#define PLATFORM_PSERIES_LPAR 0x0101
177#define PLATFORM_LPAR 0x0001
178#define PLATFORM_POWERMAC 0x0400
179#define PLATFORM_GENERIC 0x0500
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +0000180#define PLATFORM_OPAL 0x0600
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100181
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000182static int __initdata of_platform;
183
184static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
185
Benjamin Krillcf687872009-07-27 22:02:39 +0000186static unsigned long __initdata prom_memory_limit;
187
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000188static unsigned long __initdata alloc_top;
189static unsigned long __initdata alloc_top_high;
190static unsigned long __initdata alloc_bottom;
191static unsigned long __initdata rmo_top;
192static unsigned long __initdata ram_top;
193
194static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
195static int __initdata mem_reserve_cnt;
196
197static cell_t __initdata regbuf[1024];
198
199
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000200/*
201 * Error results ... some OF calls will return "-1" on error, some
202 * will return 0, some will return either. To simplify, here are
203 * macros to use with any ihandle or phandle return value to check if
204 * it is valid
205 */
206
207#define PROM_ERROR (-1u)
208#define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
209#define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
210
211
212/* This is the one and *ONLY* place where we actually call open
213 * firmware.
214 */
215
216static int __init call_prom(const char *service, int nargs, int nret, ...)
217{
218 int i;
219 struct prom_args args;
220 va_list list;
221
222 args.service = ADDR(service);
223 args.nargs = nargs;
224 args.nret = nret;
225
226 va_start(list, nret);
227 for (i = 0; i < nargs; i++)
228 args.args[i] = va_arg(list, prom_arg_t);
229 va_end(list);
230
231 for (i = 0; i < nret; i++)
232 args.args[nargs+i] = 0;
233
Anton Blanchard5827d412012-11-26 17:40:03 +0000234 if (enter_prom(&args, prom_entry) < 0)
Paul Mackerrasc49888202005-10-26 21:52:53 +1000235 return PROM_ERROR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000236
237 return (nret > 0) ? args.args[nargs] : 0;
238}
239
240static int __init call_prom_ret(const char *service, int nargs, int nret,
241 prom_arg_t *rets, ...)
242{
243 int i;
244 struct prom_args args;
245 va_list list;
246
247 args.service = ADDR(service);
248 args.nargs = nargs;
249 args.nret = nret;
250
251 va_start(list, rets);
252 for (i = 0; i < nargs; i++)
253 args.args[i] = va_arg(list, prom_arg_t);
254 va_end(list);
255
256 for (i = 0; i < nret; i++)
Olaf Heringed1189b2005-11-29 14:04:05 +0100257 args.args[nargs+i] = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000258
Anton Blanchard5827d412012-11-26 17:40:03 +0000259 if (enter_prom(&args, prom_entry) < 0)
Paul Mackerrasc49888202005-10-26 21:52:53 +1000260 return PROM_ERROR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000261
262 if (rets != NULL)
263 for (i = 1; i < nret; ++i)
Paul Mackerrasc5200c92005-10-10 22:57:03 +1000264 rets[i-1] = args.args[nargs+i];
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000265
266 return (nret > 0) ? args.args[nargs] : 0;
267}
268
269
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000270static void __init prom_print(const char *msg)
271{
272 const char *p, *q;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000273
Anton Blanchard5827d412012-11-26 17:40:03 +0000274 if (prom.stdout == 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000275 return;
276
277 for (p = msg; *p != 0; p = q) {
278 for (q = p; *q != 0 && *q != '\n'; ++q)
279 ;
280 if (q > p)
Anton Blanchard5827d412012-11-26 17:40:03 +0000281 call_prom("write", 3, 1, prom.stdout, p, q - p);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000282 if (*q == 0)
283 break;
284 ++q;
Anton Blanchard5827d412012-11-26 17:40:03 +0000285 call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000286 }
287}
288
289
290static void __init prom_print_hex(unsigned long val)
291{
292 int i, nibbles = sizeof(val)*2;
293 char buf[sizeof(val)*2+1];
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000294
295 for (i = nibbles-1; i >= 0; i--) {
296 buf[i] = (val & 0xf) + '0';
297 if (buf[i] > '9')
298 buf[i] += ('a'-'0'-10);
299 val >>= 4;
300 }
301 buf[nibbles] = '\0';
Anton Blanchard5827d412012-11-26 17:40:03 +0000302 call_prom("write", 3, 1, prom.stdout, buf, nibbles);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000303}
304
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000305/* max number of decimal digits in an unsigned long */
306#define UL_DIGITS 21
307static void __init prom_print_dec(unsigned long val)
308{
309 int i, size;
310 char buf[UL_DIGITS+1];
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000311
312 for (i = UL_DIGITS-1; i >= 0; i--) {
313 buf[i] = (val % 10) + '0';
314 val = val/10;
315 if (val == 0)
316 break;
317 }
318 /* shift stuff down */
319 size = UL_DIGITS - i;
Anton Blanchard5827d412012-11-26 17:40:03 +0000320 call_prom("write", 3, 1, prom.stdout, buf+i, size);
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000321}
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000322
323static void __init prom_printf(const char *format, ...)
324{
325 const char *p, *q, *s;
326 va_list args;
327 unsigned long v;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000328 long vs;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000329
330 va_start(args, format);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000331 for (p = format; *p != 0; p = q) {
332 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
333 ;
334 if (q > p)
Anton Blanchard5827d412012-11-26 17:40:03 +0000335 call_prom("write", 3, 1, prom.stdout, p, q - p);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000336 if (*q == 0)
337 break;
338 if (*q == '\n') {
339 ++q;
Anton Blanchard5827d412012-11-26 17:40:03 +0000340 call_prom("write", 3, 1, prom.stdout,
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000341 ADDR("\r\n"), 2);
342 continue;
343 }
344 ++q;
345 if (*q == 0)
346 break;
347 switch (*q) {
348 case 's':
349 ++q;
350 s = va_arg(args, const char *);
351 prom_print(s);
352 break;
353 case 'x':
354 ++q;
355 v = va_arg(args, unsigned long);
356 prom_print_hex(v);
357 break;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000358 case 'd':
359 ++q;
360 vs = va_arg(args, int);
361 if (vs < 0) {
Anton Blanchard5827d412012-11-26 17:40:03 +0000362 prom_print("-");
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000363 vs = -vs;
364 }
365 prom_print_dec(vs);
366 break;
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000367 case 'l':
368 ++q;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000369 if (*q == 0)
370 break;
371 else if (*q == 'x') {
372 ++q;
373 v = va_arg(args, unsigned long);
374 prom_print_hex(v);
375 } else if (*q == 'u') { /* '%lu' */
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000376 ++q;
377 v = va_arg(args, unsigned long);
378 prom_print_dec(v);
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000379 } else if (*q == 'd') { /* %ld */
380 ++q;
381 vs = va_arg(args, long);
382 if (vs < 0) {
Anton Blanchard5827d412012-11-26 17:40:03 +0000383 prom_print("-");
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000384 vs = -vs;
385 }
386 prom_print_dec(vs);
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000387 }
388 break;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000389 }
390 }
391}
392
393
Paul Mackerrasa575b802005-10-23 17:23:21 +1000394static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
395 unsigned long align)
396{
Paul Mackerrasa575b802005-10-23 17:23:21 +1000397
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100398 if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
399 /*
400 * Old OF requires we claim physical and virtual separately
401 * and then map explicitly (assuming virtual mode)
402 */
403 int ret;
404 prom_arg_t result;
405
406 ret = call_prom_ret("call-method", 5, 2, &result,
Anton Blanchard5827d412012-11-26 17:40:03 +0000407 ADDR("claim"), prom.memory,
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100408 align, size, virt);
409 if (ret != 0 || result == -1)
410 return -1;
411 ret = call_prom_ret("call-method", 5, 2, &result,
Anton Blanchard5827d412012-11-26 17:40:03 +0000412 ADDR("claim"), prom.mmumap,
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100413 align, size, virt);
414 if (ret != 0) {
415 call_prom("call-method", 4, 1, ADDR("release"),
Anton Blanchard5827d412012-11-26 17:40:03 +0000416 prom.memory, size, virt);
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100417 return -1;
418 }
419 /* the 0x12 is M (coherence) + PP == read/write */
Paul Mackerrasa575b802005-10-23 17:23:21 +1000420 call_prom("call-method", 6, 1,
Anton Blanchard5827d412012-11-26 17:40:03 +0000421 ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100422 return virt;
423 }
424 return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
425 (prom_arg_t)align);
Paul Mackerrasa575b802005-10-23 17:23:21 +1000426}
427
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000428static void __init __attribute__((noreturn)) prom_panic(const char *reason)
429{
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000430 prom_print(reason);
Olaf Heringadd60ef2006-03-23 22:03:57 +0100431 /* Do not call exit because it clears the screen on pmac
432 * it also causes some sort of double-fault on early pmacs */
Anton Blanchard5827d412012-11-26 17:40:03 +0000433 if (of_platform == PLATFORM_POWERMAC)
Olaf Heringadd60ef2006-03-23 22:03:57 +0100434 asm("trap\n");
435
Stephen Rothwell1d9a4732012-03-21 18:23:27 +0000436 /* ToDo: should put up an SRC here on pSeries */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000437 call_prom("exit", 0, 0);
438
439 for (;;) /* should never get here */
440 ;
441}
442
443
444static int __init prom_next_node(phandle *nodep)
445{
446 phandle node;
447
448 if ((node = *nodep) != 0
449 && (*nodep = call_prom("child", 1, 1, node)) != 0)
450 return 1;
451 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
452 return 1;
453 for (;;) {
454 if ((node = call_prom("parent", 1, 1, node)) == 0)
455 return 0;
456 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
457 return 1;
458 }
459}
460
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +1100461static int inline prom_getprop(phandle node, const char *pname,
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000462 void *value, size_t valuelen)
463{
464 return call_prom("getprop", 4, 1, node, ADDR(pname),
465 (u32)(unsigned long) value, (u32) valuelen);
466}
467
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +1100468static int inline prom_getproplen(phandle node, const char *pname)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000469{
470 return call_prom("getproplen", 2, 1, node, ADDR(pname));
471}
472
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100473static void add_string(char **str, const char *q)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000474{
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100475 char *p = *str;
476
477 while (*q)
478 *p++ = *q++;
479 *p++ = ' ';
480 *str = p;
481}
482
483static char *tohex(unsigned int x)
484{
485 static char digits[] = "0123456789abcdef";
486 static char result[9];
487 int i;
488
489 result[8] = 0;
490 i = 8;
491 do {
492 --i;
493 result[i] = digits[x & 0xf];
494 x >>= 4;
495 } while (x != 0 && i > 0);
496 return &result[i];
497}
498
499static int __init prom_setprop(phandle node, const char *nodename,
500 const char *pname, void *value, size_t valuelen)
501{
502 char cmd[256], *p;
503
504 if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
505 return call_prom("setprop", 4, 1, node, ADDR(pname),
506 (u32)(unsigned long) value, (u32) valuelen);
507
508 /* gah... setprop doesn't work on longtrail, have to use interpret */
509 p = cmd;
510 add_string(&p, "dev");
511 add_string(&p, nodename);
512 add_string(&p, tohex((u32)(unsigned long) value));
513 add_string(&p, tohex(valuelen));
514 add_string(&p, tohex(ADDR(pname)));
Anton Blanchard5827d412012-11-26 17:40:03 +0000515 add_string(&p, tohex(strlen(pname)));
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100516 add_string(&p, "property");
517 *p = 0;
518 return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000519}
520
Anton Blanchard5827d412012-11-26 17:40:03 +0000521/* We can't use the standard versions because of relocation headaches. */
Benjamin Krillcf687872009-07-27 22:02:39 +0000522#define isxdigit(c) (('0' <= (c) && (c) <= '9') \
523 || ('a' <= (c) && (c) <= 'f') \
524 || ('A' <= (c) && (c) <= 'F'))
525
526#define isdigit(c) ('0' <= (c) && (c) <= '9')
527#define islower(c) ('a' <= (c) && (c) <= 'z')
528#define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c))
529
530unsigned long prom_strtoul(const char *cp, const char **endp)
531{
532 unsigned long result = 0, base = 10, value;
533
534 if (*cp == '0') {
535 base = 8;
536 cp++;
537 if (toupper(*cp) == 'X') {
538 cp++;
539 base = 16;
540 }
541 }
542
543 while (isxdigit(*cp) &&
544 (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
545 result = result * base + value;
546 cp++;
547 }
548
549 if (endp)
550 *endp = cp;
551
552 return result;
553}
554
555unsigned long prom_memparse(const char *ptr, const char **retptr)
556{
557 unsigned long ret = prom_strtoul(ptr, retptr);
558 int shift = 0;
559
560 /*
561 * We can't use a switch here because GCC *may* generate a
562 * jump table which won't work, because we're not running at
563 * the address we're linked at.
564 */
565 if ('G' == **retptr || 'g' == **retptr)
566 shift = 30;
567
568 if ('M' == **retptr || 'm' == **retptr)
569 shift = 20;
570
571 if ('K' == **retptr || 'k' == **retptr)
572 shift = 10;
573
574 if (shift) {
575 ret <<= shift;
576 (*retptr)++;
577 }
578
579 return ret;
580}
581
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000582/*
583 * Early parsing of the command line passed to the kernel, used for
584 * "mem=x" and the options that affect the iommu
585 */
586static void __init early_cmdline_parse(void)
587{
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100588 const char *opt;
Benjamin Krillcf687872009-07-27 22:02:39 +0000589
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100590 char *p;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000591 int l = 0;
592
Anton Blanchard5827d412012-11-26 17:40:03 +0000593 prom_cmd_line[0] = 0;
594 p = prom_cmd_line;
595 if ((long)prom.chosen > 0)
596 l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000597#ifdef CONFIG_CMDLINE
Amos Waterland0e4aa9c2006-06-12 23:45:02 -0400598 if (l <= 0 || p[0] == '\0') /* dbl check */
Anton Blanchard5827d412012-11-26 17:40:03 +0000599 strlcpy(prom_cmd_line,
600 CONFIG_CMDLINE, sizeof(prom_cmd_line));
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000601#endif /* CONFIG_CMDLINE */
Anton Blanchard5827d412012-11-26 17:40:03 +0000602 prom_printf("command line: %s\n", prom_cmd_line);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000603
604#ifdef CONFIG_PPC64
Anton Blanchard5827d412012-11-26 17:40:03 +0000605 opt = strstr(prom_cmd_line, "iommu=");
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000606 if (opt) {
607 prom_printf("iommu opt is: %s\n", opt);
608 opt += 6;
609 while (*opt && *opt == ' ')
610 opt++;
Anton Blanchard5827d412012-11-26 17:40:03 +0000611 if (!strncmp(opt, "off", 3))
612 prom_iommu_off = 1;
613 else if (!strncmp(opt, "force", 5))
614 prom_iommu_force_on = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000615 }
616#endif
Anton Blanchard5827d412012-11-26 17:40:03 +0000617 opt = strstr(prom_cmd_line, "mem=");
Benjamin Krillcf687872009-07-27 22:02:39 +0000618 if (opt) {
619 opt += 4;
Anton Blanchard5827d412012-11-26 17:40:03 +0000620 prom_memory_limit = prom_memparse(opt, (const char **)&opt);
Benjamin Krillcf687872009-07-27 22:02:39 +0000621#ifdef CONFIG_PPC64
622 /* Align to 16 MB == size of ppc64 large page */
Anton Blanchard5827d412012-11-26 17:40:03 +0000623 prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
Benjamin Krillcf687872009-07-27 22:02:39 +0000624#endif
625 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000626}
627
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +0000628#if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000629/*
Nathan Fontenot530b5e12013-04-24 05:53:10 +0000630 * The architecture vector has an array of PVR mask/value pairs,
631 * followed by # option vectors - 1, followed by the option vectors.
632 *
633 * See prom.h for the definition of the bits specified in the
634 * architecture vector.
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000635 *
636 * Because the description vector contains a mix of byte and word
637 * values, we declare it as an unsigned char array, and use this
638 * macro to put word values in.
639 */
640#define W(x) ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
641 ((x) >> 8) & 0xff, (x) & 0xff
642
Nathan Fontenot530b5e12013-04-24 05:53:10 +0000643unsigned char ibm_architecture_vec[] = {
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000644 W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */
Anton Blanchard03054d52006-04-29 09:51:06 +1000645 W(0xffff0000), W(0x003e0000), /* POWER6 */
Michael Neulinge952e6c2008-06-18 10:47:26 +1000646 W(0xffff0000), W(0x003f0000), /* POWER7 */
Michael Neulingdf77c792012-11-08 20:23:11 +0000647 W(0xffff0000), W(0x004b0000), /* POWER8 */
648 W(0xffffffff), W(0x0f000004), /* all 2.07-compliant */
Joel Schopp0cb99012008-06-19 06:23:23 +1000649 W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */
Paul Mackerras0efbc182006-11-29 22:31:47 +1100650 W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000651 W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000652 6 - 1, /* 6 option vectors */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000653
654 /* option vector 1: processor architectures supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500655 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000656 0, /* don't ignore, don't halt */
657 OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
Michael Neulingdf77c792012-11-08 20:23:11 +0000658 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000659
660 /* option vector 2: Open Firmware options supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500661 34 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000662 OV2_REAL_MODE,
663 0, 0,
664 W(0xffffffff), /* real_base */
665 W(0xffffffff), /* real_size */
666 W(0xffffffff), /* virt_base */
667 W(0xffffffff), /* virt_size */
668 W(0xffffffff), /* load_base */
Anton Blanchard33392642011-12-04 13:13:58 +0000669 W(256), /* 256MB min RMA */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000670 W(0xffffffff), /* full client load */
671 0, /* min RMA percentage of total RAM */
672 48, /* max log_2(hash table size) */
673
674 /* option vector 3: processor options supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500675 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000676 0, /* don't ignore, don't halt */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100677 OV3_FP | OV3_VMX | OV3_DFP,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000678
679 /* option vector 4: IBM PAPR implementation */
Robert Jennings404e32e2012-05-10 08:55:49 +0000680 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000681 0, /* don't halt */
Robert Jennings404e32e2012-05-10 08:55:49 +0000682 OV4_MIN_ENT_CAP, /* minimum VP entitled capacity */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000683
684 /* option vector 5: PAPR/OF options */
Michael Neulingdf77c792012-11-08 20:23:11 +0000685 19 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000686 0, /* don't ignore, don't halt */
Nathan Fontenotf0ff7eb2013-04-24 05:57:18 +0000687 OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
688 OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
689#ifdef CONFIG_PCI_MSI
690 /* PCIe/MSI support. Without MSI full PCIe is not supported */
691 OV5_FEAT(OV5_MSI),
692#else
Nathan Fontenot8391e422008-07-24 04:36:38 +1000693 0,
Nathan Fontenotf0ff7eb2013-04-24 05:57:18 +0000694#endif
695 0,
696#ifdef CONFIG_PPC_SMLPAR
697 OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
698#else
699 0,
700#endif
Nathan Fontenot1b1218d2013-04-24 06:06:17 +0000701 OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000702 0,
703 0,
704 0,
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100705 /* WARNING: The offset of the "number of cores" field below
706 * must match by the macro below. Update the definition if
707 * the structure layout changes.
708 */
Michael Neulingdf77c792012-11-08 20:23:11 +0000709#define IBM_ARCH_VEC_NRCORES_OFFSET 117
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100710 W(NR_CPUS), /* number of cores supported */
Kent Yoder828d2b52012-04-12 05:17:51 +0000711 0,
712 0,
713 0,
714 0,
Nathan Fontenotf0ff7eb2013-04-24 05:57:18 +0000715 OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) |
716 OV5_FEAT(OV5_PFO_HW_842),
717 OV5_FEAT(OV5_SUB_PROCESSORS),
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000718 /* option vector 6: IBM PAPR hints */
719 4 - 2, /* length */
720 0,
721 0,
722 OV6_LINUX,
723
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000724};
725
726/* Old method - ELF header with PT_NOTE sections */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000727static struct fake_elf {
728 Elf32_Ehdr elfhdr;
729 Elf32_Phdr phdr[2];
730 struct chrpnote {
731 u32 namesz;
732 u32 descsz;
733 u32 type;
734 char name[8]; /* "PowerPC" */
735 struct chrpdesc {
736 u32 real_mode;
737 u32 real_base;
738 u32 real_size;
739 u32 virt_base;
740 u32 virt_size;
741 u32 load_base;
742 } chrpdesc;
743 } chrpnote;
744 struct rpanote {
745 u32 namesz;
746 u32 descsz;
747 u32 type;
748 char name[24]; /* "IBM,RPA-Client-Config" */
749 struct rpadesc {
750 u32 lpar_affinity;
751 u32 min_rmo_size;
752 u32 min_rmo_percent;
753 u32 max_pft_size;
754 u32 splpar;
755 u32 min_load;
756 u32 new_mem_def;
757 u32 ignore_me;
758 } rpadesc;
759 } rpanote;
Paul Mackerras5663a122008-10-31 22:27:17 +1100760} fake_elf = {
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000761 .elfhdr = {
762 .e_ident = { 0x7f, 'E', 'L', 'F',
763 ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
764 .e_type = ET_EXEC, /* yeah right */
765 .e_machine = EM_PPC,
766 .e_version = EV_CURRENT,
767 .e_phoff = offsetof(struct fake_elf, phdr),
768 .e_phentsize = sizeof(Elf32_Phdr),
769 .e_phnum = 2
770 },
771 .phdr = {
772 [0] = {
773 .p_type = PT_NOTE,
774 .p_offset = offsetof(struct fake_elf, chrpnote),
775 .p_filesz = sizeof(struct chrpnote)
776 }, [1] = {
777 .p_type = PT_NOTE,
778 .p_offset = offsetof(struct fake_elf, rpanote),
779 .p_filesz = sizeof(struct rpanote)
780 }
781 },
782 .chrpnote = {
783 .namesz = sizeof("PowerPC"),
784 .descsz = sizeof(struct chrpdesc),
785 .type = 0x1275,
786 .name = "PowerPC",
787 .chrpdesc = {
788 .real_mode = ~0U, /* ~0 means "don't care" */
789 .real_base = ~0U,
790 .real_size = ~0U,
791 .virt_base = ~0U,
792 .virt_size = ~0U,
793 .load_base = ~0U
794 },
795 },
796 .rpanote = {
797 .namesz = sizeof("IBM,RPA-Client-Config"),
798 .descsz = sizeof(struct rpadesc),
799 .type = 0x12759999,
800 .name = "IBM,RPA-Client-Config",
801 .rpadesc = {
Paul Mackerras5663a122008-10-31 22:27:17 +1100802 .lpar_affinity = 0,
803 .min_rmo_size = 64, /* in megabytes */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000804 .min_rmo_percent = 0,
Paul Mackerras5663a122008-10-31 22:27:17 +1100805 .max_pft_size = 48, /* 2^48 bytes max PFT size */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000806 .splpar = 1,
807 .min_load = ~0U,
Paul Mackerras5663a122008-10-31 22:27:17 +1100808 .new_mem_def = 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000809 }
810 }
811};
812
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100813static int __init prom_count_smt_threads(void)
814{
815 phandle node;
816 char type[64];
817 unsigned int plen;
818
819 /* Pick up th first CPU node we can find */
820 for (node = 0; prom_next_node(&node); ) {
821 type[0] = 0;
822 prom_getprop(node, "device_type", type, sizeof(type));
823
Anton Blanchard5827d412012-11-26 17:40:03 +0000824 if (strcmp(type, "cpu"))
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100825 continue;
826 /*
827 * There is an entry for each smt thread, each entry being
828 * 4 bytes long. All cpus should have the same number of
829 * smt threads, so return after finding the first.
830 */
831 plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
832 if (plen == PROM_ERROR)
833 break;
834 plen >>= 2;
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000835 prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100836
837 /* Sanity check */
838 if (plen < 1 || plen > 64) {
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000839 prom_printf("Threads per core %lu out of bounds, assuming 1\n",
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100840 (unsigned long)plen);
841 return 1;
842 }
843 return plen;
844 }
845 prom_debug("No threads found, assuming 1 per core\n");
846
847 return 1;
848
849}
850
851
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000852static void __init prom_send_capabilities(void)
853{
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000854 ihandle elfloader, root;
855 prom_arg_t ret;
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100856 u32 *cores;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000857
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000858 root = call_prom("open", 1, 1, ADDR("/"));
859 if (root != 0) {
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100860 /* We need to tell the FW about the number of cores we support.
861 *
862 * To do that, we count the number of threads on the first core
863 * (we assume this is the same for all cores) and use it to
864 * divide NR_CPUS.
865 */
Anton Blanchard5ac47f72012-11-26 17:39:03 +0000866 cores = (u32 *)&ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET];
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100867 if (*cores != NR_CPUS) {
868 prom_printf("WARNING ! "
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000869 "ibm_architecture_vec structure inconsistent: %lu!\n",
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100870 *cores);
871 } else {
Anton Blanchard33ad5e42010-06-17 14:33:06 +0000872 *cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000873 prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
874 *cores, NR_CPUS);
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100875 }
876
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000877 /* try calling the ibm,client-architecture-support method */
Anton Blanchard049d0492009-09-21 20:47:39 +0000878 prom_printf("Calling ibm,client-architecture-support...");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000879 if (call_prom_ret("call-method", 3, 2, &ret,
880 ADDR("ibm,client-architecture-support"),
Benjamin Herrenschmidt33b74972006-06-07 12:01:32 +1000881 root,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000882 ADDR(ibm_architecture_vec)) == 0) {
883 /* the call exists... */
884 if (ret)
Anton Blanchard4da727a2009-03-31 20:06:14 +0000885 prom_printf("\nWARNING: ibm,client-architecture"
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000886 "-support call FAILED!\n");
887 call_prom("close", 1, 0, root);
Anton Blanchard4da727a2009-03-31 20:06:14 +0000888 prom_printf(" done\n");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000889 return;
890 }
891 call_prom("close", 1, 0, root);
Anton Blanchard049d0492009-09-21 20:47:39 +0000892 prom_printf(" not implemented\n");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000893 }
894
895 /* no ibm,client-architecture-support call, try the old way */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000896 elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
897 if (elfloader == 0) {
898 prom_printf("couldn't open /packages/elf-loader\n");
899 return;
900 }
901 call_prom("call-method", 3, 1, ADDR("process-elf-header"),
902 elfloader, ADDR(&fake_elf));
903 call_prom("close", 1, 0, elfloader);
904}
905#endif
906
907/*
908 * Memory allocation strategy... our layout is normally:
909 *
910 * at 14Mb or more we have vmlinux, then a gap and initrd. In some
911 * rare cases, initrd might end up being before the kernel though.
912 * We assume this won't override the final kernel at 0, we have no
913 * provision to handle that in this version, but it should hopefully
914 * never happen.
915 *
916 * alloc_top is set to the top of RMO, eventually shrink down if the
917 * TCEs overlap
918 *
919 * alloc_bottom is set to the top of kernel/initrd
920 *
921 * from there, allocations are done this way : rtas is allocated
922 * topmost, and the device-tree is allocated from the bottom. We try
923 * to grow the device-tree allocation as we progress. If we can't,
924 * then we fail, we don't currently have a facility to restart
925 * elsewhere, but that shouldn't be necessary.
926 *
927 * Note that calls to reserve_mem have to be done explicitly, memory
928 * allocated with either alloc_up or alloc_down isn't automatically
929 * reserved.
930 */
931
932
933/*
934 * Allocates memory in the RMO upward from the kernel/initrd
935 *
936 * When align is 0, this is a special case, it means to allocate in place
937 * at the current location of alloc_bottom or fail (that is basically
938 * extending the previous allocation). Used for the device-tree flattening
939 */
940static unsigned long __init alloc_up(unsigned long size, unsigned long align)
941{
Anton Blanchard5827d412012-11-26 17:40:03 +0000942 unsigned long base = alloc_bottom;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000943 unsigned long addr = 0;
944
Paul Mackerrasc49888202005-10-26 21:52:53 +1000945 if (align)
946 base = _ALIGN_UP(base, align);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000947 prom_debug("alloc_up(%x, %x)\n", size, align);
Anton Blanchard5827d412012-11-26 17:40:03 +0000948 if (ram_top == 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000949 prom_panic("alloc_up() called with mem not initialized\n");
950
951 if (align)
Anton Blanchard5827d412012-11-26 17:40:03 +0000952 base = _ALIGN_UP(alloc_bottom, align);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000953 else
Anton Blanchard5827d412012-11-26 17:40:03 +0000954 base = alloc_bottom;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000955
Anton Blanchard5827d412012-11-26 17:40:03 +0000956 for(; (base + size) <= alloc_top;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000957 base = _ALIGN_UP(base + 0x100000, align)) {
958 prom_debug(" trying: 0x%x\n\r", base);
959 addr = (unsigned long)prom_claim(base, size, 0);
Paul Mackerrasc49888202005-10-26 21:52:53 +1000960 if (addr != PROM_ERROR && addr != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000961 break;
962 addr = 0;
963 if (align == 0)
964 break;
965 }
966 if (addr == 0)
967 return 0;
Anton Blanchard5827d412012-11-26 17:40:03 +0000968 alloc_bottom = addr + size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000969
970 prom_debug(" -> %x\n", addr);
Anton Blanchard5827d412012-11-26 17:40:03 +0000971 prom_debug(" alloc_bottom : %x\n", alloc_bottom);
972 prom_debug(" alloc_top : %x\n", alloc_top);
973 prom_debug(" alloc_top_hi : %x\n", alloc_top_high);
974 prom_debug(" rmo_top : %x\n", rmo_top);
975 prom_debug(" ram_top : %x\n", ram_top);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000976
977 return addr;
978}
979
980/*
981 * Allocates memory downward, either from top of RMO, or if highmem
982 * is set, from the top of RAM. Note that this one doesn't handle
983 * failures. It does claim memory if highmem is not set.
984 */
985static unsigned long __init alloc_down(unsigned long size, unsigned long align,
986 int highmem)
987{
988 unsigned long base, addr = 0;
989
990 prom_debug("alloc_down(%x, %x, %s)\n", size, align,
Anton Blanchard5827d412012-11-26 17:40:03 +0000991 highmem ? "(high)" : "(low)");
992 if (ram_top == 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000993 prom_panic("alloc_down() called with mem not initialized\n");
994
995 if (highmem) {
996 /* Carve out storage for the TCE table. */
Anton Blanchard5827d412012-11-26 17:40:03 +0000997 addr = _ALIGN_DOWN(alloc_top_high - size, align);
998 if (addr <= alloc_bottom)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000999 return 0;
1000 /* Will we bump into the RMO ? If yes, check out that we
1001 * didn't overlap existing allocations there, if we did,
1002 * we are dead, we must be the first in town !
1003 */
Anton Blanchard5827d412012-11-26 17:40:03 +00001004 if (addr < rmo_top) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001005 /* Good, we are first */
Anton Blanchard5827d412012-11-26 17:40:03 +00001006 if (alloc_top == rmo_top)
1007 alloc_top = rmo_top = addr;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001008 else
1009 return 0;
1010 }
Anton Blanchard5827d412012-11-26 17:40:03 +00001011 alloc_top_high = addr;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001012 goto bail;
1013 }
1014
Anton Blanchard5827d412012-11-26 17:40:03 +00001015 base = _ALIGN_DOWN(alloc_top - size, align);
1016 for (; base > alloc_bottom;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001017 base = _ALIGN_DOWN(base - 0x100000, align)) {
1018 prom_debug(" trying: 0x%x\n\r", base);
1019 addr = (unsigned long)prom_claim(base, size, 0);
Paul Mackerrasc49888202005-10-26 21:52:53 +10001020 if (addr != PROM_ERROR && addr != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001021 break;
1022 addr = 0;
1023 }
1024 if (addr == 0)
1025 return 0;
Anton Blanchard5827d412012-11-26 17:40:03 +00001026 alloc_top = addr;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001027
1028 bail:
1029 prom_debug(" -> %x\n", addr);
Anton Blanchard5827d412012-11-26 17:40:03 +00001030 prom_debug(" alloc_bottom : %x\n", alloc_bottom);
1031 prom_debug(" alloc_top : %x\n", alloc_top);
1032 prom_debug(" alloc_top_hi : %x\n", alloc_top_high);
1033 prom_debug(" rmo_top : %x\n", rmo_top);
1034 prom_debug(" ram_top : %x\n", ram_top);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001035
1036 return addr;
1037}
1038
1039/*
1040 * Parse a "reg" cell
1041 */
1042static unsigned long __init prom_next_cell(int s, cell_t **cellp)
1043{
1044 cell_t *p = *cellp;
1045 unsigned long r = 0;
1046
1047 /* Ignore more than 2 cells */
1048 while (s > sizeof(unsigned long) / 4) {
1049 p++;
1050 s--;
1051 }
1052 r = *p++;
1053#ifdef CONFIG_PPC64
Paul Mackerras35499c02005-10-22 16:02:39 +10001054 if (s > 1) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001055 r <<= 32;
1056 r |= *(p++);
1057 }
1058#endif
1059 *cellp = p;
1060 return r;
1061}
1062
1063/*
1064 * Very dumb function for adding to the memory reserve list, but
1065 * we don't need anything smarter at this point
1066 *
1067 * XXX Eventually check for collisions. They should NEVER happen.
1068 * If problems seem to show up, it would be a good start to track
1069 * them down.
1070 */
Michael Ellerman0108d3f2007-05-07 15:58:28 +10001071static void __init reserve_mem(u64 base, u64 size)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001072{
Kumar Galacbbcf342006-01-11 17:57:13 -06001073 u64 top = base + size;
Anton Blanchard5827d412012-11-26 17:40:03 +00001074 unsigned long cnt = mem_reserve_cnt;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001075
1076 if (size == 0)
1077 return;
1078
1079 /* We need to always keep one empty entry so that we
1080 * have our terminator with "size" set to 0 since we are
1081 * dumb and just copy this entire array to the boot params
1082 */
1083 base = _ALIGN_DOWN(base, PAGE_SIZE);
1084 top = _ALIGN_UP(top, PAGE_SIZE);
1085 size = top - base;
1086
1087 if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
1088 prom_panic("Memory reserve map exhausted !\n");
Anton Blanchard5827d412012-11-26 17:40:03 +00001089 mem_reserve_map[cnt].base = base;
1090 mem_reserve_map[cnt].size = size;
1091 mem_reserve_cnt = cnt + 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001092}
1093
1094/*
Adrian Bunkb3c2ffd2006-06-30 18:20:44 +02001095 * Initialize memory allocation mechanism, parse "memory" nodes and
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001096 * obtain that way the top of memory and RMO to setup out local allocator
1097 */
1098static void __init prom_init_mem(void)
1099{
1100 phandle node;
1101 char *path, type[64];
1102 unsigned int plen;
1103 cell_t *p, *endp;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001104 u32 rac, rsc;
1105
1106 /*
1107 * We iterate the memory nodes to find
1108 * 1) top of RMO (first node)
1109 * 2) top of memory
1110 */
1111 rac = 2;
Anton Blanchard5827d412012-11-26 17:40:03 +00001112 prom_getprop(prom.root, "#address-cells", &rac, sizeof(rac));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001113 rsc = 1;
Anton Blanchard5827d412012-11-26 17:40:03 +00001114 prom_getprop(prom.root, "#size-cells", &rsc, sizeof(rsc));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001115 prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
1116 prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
1117
1118 prom_debug("scanning memory:\n");
Anton Blanchard5827d412012-11-26 17:40:03 +00001119 path = prom_scratch;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001120
1121 for (node = 0; prom_next_node(&node); ) {
1122 type[0] = 0;
1123 prom_getprop(node, "device_type", type, sizeof(type));
1124
Paul Mackerrasc49888202005-10-26 21:52:53 +10001125 if (type[0] == 0) {
1126 /*
1127 * CHRP Longtrail machines have no device_type
1128 * on the memory node, so check the name instead...
1129 */
1130 prom_getprop(node, "name", type, sizeof(type));
1131 }
Anton Blanchard5827d412012-11-26 17:40:03 +00001132 if (strcmp(type, "memory"))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001133 continue;
Paul Mackerrasc49888202005-10-26 21:52:53 +10001134
Anton Blanchard5827d412012-11-26 17:40:03 +00001135 plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001136 if (plen > sizeof(regbuf)) {
1137 prom_printf("memory node too large for buffer !\n");
1138 plen = sizeof(regbuf);
1139 }
Anton Blanchard5827d412012-11-26 17:40:03 +00001140 p = regbuf;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001141 endp = p + (plen / sizeof(cell_t));
1142
1143#ifdef DEBUG_PROM
1144 memset(path, 0, PROM_SCRATCH_SIZE);
1145 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1146 prom_debug(" node %s :\n", path);
1147#endif /* DEBUG_PROM */
1148
1149 while ((endp - p) >= (rac + rsc)) {
1150 unsigned long base, size;
1151
1152 base = prom_next_cell(rac, &p);
1153 size = prom_next_cell(rsc, &p);
1154
1155 if (size == 0)
1156 continue;
1157 prom_debug(" %x %x\n", base, size);
Anton Blanchard5827d412012-11-26 17:40:03 +00001158 if (base == 0 && (of_platform & PLATFORM_LPAR))
1159 rmo_top = size;
1160 if ((base + size) > ram_top)
1161 ram_top = base + size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001162 }
1163 }
1164
Anton Blanchard5827d412012-11-26 17:40:03 +00001165 alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001166
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001167 /*
Benjamin Krillcf687872009-07-27 22:02:39 +00001168 * If prom_memory_limit is set we reduce the upper limits *except* for
1169 * alloc_top_high. This must be the real top of RAM so we can put
1170 * TCE's up there.
1171 */
1172
Anton Blanchard5827d412012-11-26 17:40:03 +00001173 alloc_top_high = ram_top;
Benjamin Krillcf687872009-07-27 22:02:39 +00001174
Anton Blanchard5827d412012-11-26 17:40:03 +00001175 if (prom_memory_limit) {
1176 if (prom_memory_limit <= alloc_bottom) {
Benjamin Krillcf687872009-07-27 22:02:39 +00001177 prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
Anton Blanchard5827d412012-11-26 17:40:03 +00001178 prom_memory_limit);
1179 prom_memory_limit = 0;
1180 } else if (prom_memory_limit >= ram_top) {
Benjamin Krillcf687872009-07-27 22:02:39 +00001181 prom_printf("Ignoring mem=%x >= ram_top.\n",
Anton Blanchard5827d412012-11-26 17:40:03 +00001182 prom_memory_limit);
1183 prom_memory_limit = 0;
Benjamin Krillcf687872009-07-27 22:02:39 +00001184 } else {
Anton Blanchard5827d412012-11-26 17:40:03 +00001185 ram_top = prom_memory_limit;
1186 rmo_top = min(rmo_top, prom_memory_limit);
Benjamin Krillcf687872009-07-27 22:02:39 +00001187 }
1188 }
1189
1190 /*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001191 * Setup our top alloc point, that is top of RMO or top of
1192 * segment 0 when running non-LPAR.
1193 * Some RS64 machines have buggy firmware where claims up at
1194 * 1GB fail. Cap at 768MB as a workaround.
1195 * Since 768MB is plenty of room, and we need to cap to something
1196 * reasonable on 32-bit, cap at 768MB on all machines.
1197 */
Anton Blanchard5827d412012-11-26 17:40:03 +00001198 if (!rmo_top)
1199 rmo_top = ram_top;
1200 rmo_top = min(0x30000000ul, rmo_top);
1201 alloc_top = rmo_top;
1202 alloc_top_high = ram_top;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001203
Paul Mackerras64968f62011-12-13 17:54:13 +00001204 /*
1205 * Check if we have an initrd after the kernel but still inside
1206 * the RMO. If we do move our bottom point to after it.
1207 */
Anton Blanchard5827d412012-11-26 17:40:03 +00001208 if (prom_initrd_start &&
1209 prom_initrd_start < rmo_top &&
1210 prom_initrd_end > alloc_bottom)
1211 alloc_bottom = PAGE_ALIGN(prom_initrd_end);
Paul Mackerras64968f62011-12-13 17:54:13 +00001212
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001213 prom_printf("memory layout at init:\n");
Anton Blanchard5827d412012-11-26 17:40:03 +00001214 prom_printf(" memory_limit : %x (16 MB aligned)\n", prom_memory_limit);
1215 prom_printf(" alloc_bottom : %x\n", alloc_bottom);
1216 prom_printf(" alloc_top : %x\n", alloc_top);
1217 prom_printf(" alloc_top_hi : %x\n", alloc_top_high);
1218 prom_printf(" rmo_top : %x\n", rmo_top);
1219 prom_printf(" ram_top : %x\n", ram_top);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001220}
1221
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001222static void __init prom_close_stdin(void)
1223{
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001224 ihandle val;
1225
Anton Blanchard5827d412012-11-26 17:40:03 +00001226 if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0)
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001227 call_prom("close", 1, 0, val);
1228}
1229
1230#ifdef CONFIG_PPC_POWERNV
1231
1232static u64 __initdata prom_opal_size;
1233static u64 __initdata prom_opal_align;
1234static int __initdata prom_rtas_start_cpu;
1235static u64 __initdata prom_rtas_data;
1236static u64 __initdata prom_rtas_entry;
1237
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00001238#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
1239static u64 __initdata prom_opal_base;
1240static u64 __initdata prom_opal_entry;
1241#endif
1242
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001243/* XXX Don't change this structure without updating opal-takeover.S */
1244static struct opal_secondary_data {
1245 s64 ack; /* 0 */
1246 u64 go; /* 8 */
1247 struct opal_takeover_args args; /* 16 */
1248} opal_secondary_data;
1249
1250extern char opal_secondary_entry;
1251
Li Zhong2cb387a2012-06-07 17:44:23 +00001252static void __init prom_query_opal(void)
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001253{
1254 long rc;
1255
Benjamin Herrenschmidt76800572011-09-28 20:51:46 +00001256 /* We must not query for OPAL presence on a machine that
1257 * supports TNK takeover (970 blades), as this uses the same
1258 * h-call with different arguments and will crash
1259 */
1260 if (PHANDLE_VALID(call_prom("finddevice", 1, 1,
1261 ADDR("/tnk-memory-map")))) {
1262 prom_printf("TNK takeover detected, skipping OPAL check\n");
1263 return;
1264 }
1265
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001266 prom_printf("Querying for OPAL presence... ");
Anton Blanchard5827d412012-11-26 17:40:03 +00001267 rc = opal_query_takeover(&prom_opal_size,
1268 &prom_opal_align);
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001269 prom_debug("(rc = %ld) ", rc);
1270 if (rc != 0) {
1271 prom_printf("not there.\n");
1272 return;
1273 }
Anton Blanchard5827d412012-11-26 17:40:03 +00001274 of_platform = PLATFORM_OPAL;
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001275 prom_printf(" there !\n");
Anton Blanchard5827d412012-11-26 17:40:03 +00001276 prom_debug(" opal_size = 0x%lx\n", prom_opal_size);
1277 prom_debug(" opal_align = 0x%lx\n", prom_opal_align);
1278 if (prom_opal_align < 0x10000)
1279 prom_opal_align = 0x10000;
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001280}
1281
1282static int prom_rtas_call(int token, int nargs, int nret, int *outputs, ...)
1283{
1284 struct rtas_args rtas_args;
1285 va_list list;
1286 int i;
1287
1288 rtas_args.token = token;
1289 rtas_args.nargs = nargs;
1290 rtas_args.nret = nret;
1291 rtas_args.rets = (rtas_arg_t *)&(rtas_args.args[nargs]);
1292 va_start(list, outputs);
1293 for (i = 0; i < nargs; ++i)
1294 rtas_args.args[i] = va_arg(list, rtas_arg_t);
1295 va_end(list);
1296
1297 for (i = 0; i < nret; ++i)
1298 rtas_args.rets[i] = 0;
1299
Anton Blanchard5827d412012-11-26 17:40:03 +00001300 opal_enter_rtas(&rtas_args, prom_rtas_data,
1301 prom_rtas_entry);
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001302
1303 if (nret > 1 && outputs != NULL)
1304 for (i = 0; i < nret-1; ++i)
1305 outputs[i] = rtas_args.rets[i+1];
1306 return (nret > 0)? rtas_args.rets[0]: 0;
1307}
1308
1309static void __init prom_opal_hold_cpus(void)
1310{
1311 int i, cnt, cpu, rc;
1312 long j;
1313 phandle node;
1314 char type[64];
1315 u32 servers[8];
Anton Blanchard5827d412012-11-26 17:40:03 +00001316 void *entry = (unsigned long *)&opal_secondary_entry;
1317 struct opal_secondary_data *data = &opal_secondary_data;
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001318
1319 prom_debug("prom_opal_hold_cpus: start...\n");
1320 prom_debug(" - entry = 0x%x\n", entry);
1321 prom_debug(" - data = 0x%x\n", data);
1322
1323 data->ack = -1;
1324 data->go = 0;
1325
1326 /* look for cpus */
1327 for (node = 0; prom_next_node(&node); ) {
1328 type[0] = 0;
1329 prom_getprop(node, "device_type", type, sizeof(type));
Anton Blanchard5827d412012-11-26 17:40:03 +00001330 if (strcmp(type, "cpu") != 0)
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001331 continue;
1332
1333 /* Skip non-configured cpus. */
1334 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
Anton Blanchard5827d412012-11-26 17:40:03 +00001335 if (strcmp(type, "okay") != 0)
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001336 continue;
1337
1338 cnt = prom_getprop(node, "ibm,ppc-interrupt-server#s", servers,
1339 sizeof(servers));
1340 if (cnt == PROM_ERROR)
1341 break;
1342 cnt >>= 2;
1343 for (i = 0; i < cnt; i++) {
1344 cpu = servers[i];
1345 prom_debug("CPU %d ... ", cpu);
Anton Blanchard5827d412012-11-26 17:40:03 +00001346 if (cpu == prom.cpu) {
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001347 prom_debug("booted !\n");
1348 continue;
1349 }
1350 prom_debug("starting ... ");
1351
1352 /* Init the acknowledge var which will be reset by
1353 * the secondary cpu when it awakens from its OF
1354 * spinloop.
1355 */
1356 data->ack = -1;
Anton Blanchard5827d412012-11-26 17:40:03 +00001357 rc = prom_rtas_call(prom_rtas_start_cpu, 3, 1,
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001358 NULL, cpu, entry, data);
1359 prom_debug("rtas rc=%d ...", rc);
1360
1361 for (j = 0; j < 100000000 && data->ack == -1; j++) {
1362 HMT_low();
1363 mb();
1364 }
1365 HMT_medium();
1366 if (data->ack != -1)
1367 prom_debug("done, PIR=0x%x\n", data->ack);
1368 else
1369 prom_debug("timeout !\n");
1370 }
1371 }
1372 prom_debug("prom_opal_hold_cpus: end...\n");
1373}
1374
Li Zhong2cb387a2012-06-07 17:44:23 +00001375static void __init prom_opal_takeover(void)
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001376{
Anton Blanchard5827d412012-11-26 17:40:03 +00001377 struct opal_secondary_data *data = &opal_secondary_data;
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001378 struct opal_takeover_args *args = &data->args;
Anton Blanchard5827d412012-11-26 17:40:03 +00001379 u64 align = prom_opal_align;
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001380 u64 top_addr, opal_addr;
1381
Anton Blanchard5827d412012-11-26 17:40:03 +00001382 args->k_image = (u64)_stext;
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001383 args->k_size = _end - _stext;
1384 args->k_entry = 0;
1385 args->k_entry2 = 0x60;
1386
1387 top_addr = _ALIGN_UP(args->k_size, align);
1388
Anton Blanchard5827d412012-11-26 17:40:03 +00001389 if (prom_initrd_start != 0) {
1390 args->rd_image = prom_initrd_start;
1391 args->rd_size = prom_initrd_end - args->rd_image;
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001392 args->rd_loc = top_addr;
1393 top_addr = _ALIGN_UP(args->rd_loc + args->rd_size, align);
1394 }
1395
1396 /* Pickup an address for the HAL. We want to go really high
1397 * up to avoid problem with future kexecs. On the other hand
1398 * we don't want to be all over the TCEs on P5IOC2 machines
1399 * which are going to be up there too. We assume the machine
1400 * has plenty of memory, and we ask for the HAL for now to
1401 * be just below the 1G point, or above the initrd
1402 */
Anton Blanchard5827d412012-11-26 17:40:03 +00001403 opal_addr = _ALIGN_DOWN(0x40000000 - prom_opal_size, align);
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001404 if (opal_addr < top_addr)
1405 opal_addr = top_addr;
1406 args->hal_addr = opal_addr;
1407
Benjamin Herrenschmidt817c21a2011-09-19 17:44:56 +00001408 /* Copy the command line to the kernel image */
Anton Blanchard5827d412012-11-26 17:40:03 +00001409 strlcpy(boot_command_line, prom_cmd_line,
Benjamin Herrenschmidt817c21a2011-09-19 17:44:56 +00001410 COMMAND_LINE_SIZE);
1411
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001412 prom_debug(" k_image = 0x%lx\n", args->k_image);
1413 prom_debug(" k_size = 0x%lx\n", args->k_size);
1414 prom_debug(" k_entry = 0x%lx\n", args->k_entry);
1415 prom_debug(" k_entry2 = 0x%lx\n", args->k_entry2);
1416 prom_debug(" hal_addr = 0x%lx\n", args->hal_addr);
1417 prom_debug(" rd_image = 0x%lx\n", args->rd_image);
1418 prom_debug(" rd_size = 0x%lx\n", args->rd_size);
1419 prom_debug(" rd_loc = 0x%lx\n", args->rd_loc);
1420 prom_printf("Performing OPAL takeover,this can take a few minutes..\n");
1421 prom_close_stdin();
1422 mb();
1423 data->go = 1;
1424 for (;;)
1425 opal_do_takeover(args);
1426}
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00001427
1428/*
1429 * Allocate room for and instantiate OPAL
1430 */
1431static void __init prom_instantiate_opal(void)
1432{
1433 phandle opal_node;
1434 ihandle opal_inst;
1435 u64 base, entry;
1436 u64 size = 0, align = 0x10000;
1437 u32 rets[2];
1438
1439 prom_debug("prom_instantiate_opal: start...\n");
1440
1441 opal_node = call_prom("finddevice", 1, 1, ADDR("/ibm,opal"));
1442 prom_debug("opal_node: %x\n", opal_node);
1443 if (!PHANDLE_VALID(opal_node))
1444 return;
1445
1446 prom_getprop(opal_node, "opal-runtime-size", &size, sizeof(size));
1447 if (size == 0)
1448 return;
1449 prom_getprop(opal_node, "opal-runtime-alignment", &align,
1450 sizeof(align));
1451
1452 base = alloc_down(size, align, 0);
1453 if (base == 0) {
1454 prom_printf("OPAL allocation failed !\n");
1455 return;
1456 }
1457
1458 opal_inst = call_prom("open", 1, 1, ADDR("/ibm,opal"));
1459 if (!IHANDLE_VALID(opal_inst)) {
1460 prom_printf("opening opal package failed (%x)\n", opal_inst);
1461 return;
1462 }
1463
1464 prom_printf("instantiating opal at 0x%x...", base);
1465
1466 if (call_prom_ret("call-method", 4, 3, rets,
1467 ADDR("load-opal-runtime"),
1468 opal_inst,
1469 base >> 32, base & 0xffffffff) != 0
1470 || (rets[0] == 0 && rets[1] == 0)) {
1471 prom_printf(" failed\n");
1472 return;
1473 }
1474 entry = (((u64)rets[0]) << 32) | rets[1];
1475
1476 prom_printf(" done\n");
1477
1478 reserve_mem(base, size);
1479
1480 prom_debug("opal base = 0x%x\n", base);
1481 prom_debug("opal align = 0x%x\n", align);
1482 prom_debug("opal entry = 0x%x\n", entry);
1483 prom_debug("opal size = 0x%x\n", (long)size);
1484
1485 prom_setprop(opal_node, "/ibm,opal", "opal-base-address",
1486 &base, sizeof(base));
1487 prom_setprop(opal_node, "/ibm,opal", "opal-entry-address",
1488 &entry, sizeof(entry));
1489
1490#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
Anton Blanchard5827d412012-11-26 17:40:03 +00001491 prom_opal_base = base;
1492 prom_opal_entry = entry;
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00001493#endif
1494 prom_debug("prom_instantiate_opal: end...\n");
1495}
1496
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001497#endif /* CONFIG_PPC_POWERNV */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001498
1499/*
1500 * Allocate room for and instantiate RTAS
1501 */
1502static void __init prom_instantiate_rtas(void)
1503{
1504 phandle rtas_node;
1505 ihandle rtas_inst;
1506 u32 base, entry = 0;
1507 u32 size = 0;
1508
1509 prom_debug("prom_instantiate_rtas: start...\n");
1510
1511 rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1512 prom_debug("rtas_node: %x\n", rtas_node);
1513 if (!PHANDLE_VALID(rtas_node))
1514 return;
1515
1516 prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1517 if (size == 0)
1518 return;
1519
1520 base = alloc_down(size, PAGE_SIZE, 0);
Anton Blanchard6d1e2c62011-11-14 12:55:47 +00001521 if (base == 0)
1522 prom_panic("Could not allocate memory for RTAS\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001523
1524 rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1525 if (!IHANDLE_VALID(rtas_inst)) {
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001526 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001527 return;
1528 }
1529
Anton Blanchard1f8737a2009-03-31 20:06:15 +00001530 prom_printf("instantiating rtas at 0x%x...", base);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001531
1532 if (call_prom_ret("call-method", 3, 2, &entry,
1533 ADDR("instantiate-rtas"),
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001534 rtas_inst, base) != 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001535 || entry == 0) {
1536 prom_printf(" failed\n");
1537 return;
1538 }
1539 prom_printf(" done\n");
1540
1541 reserve_mem(base, size);
1542
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001543 prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1544 &base, sizeof(base));
1545 prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1546 &entry, sizeof(entry));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001547
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001548#ifdef CONFIG_PPC_POWERNV
1549 /* PowerVN takeover hack */
Anton Blanchard5827d412012-11-26 17:40:03 +00001550 prom_rtas_data = base;
1551 prom_rtas_entry = entry;
1552 prom_getprop(rtas_node, "start-cpu", &prom_rtas_start_cpu, 4);
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001553#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001554 prom_debug("rtas base = 0x%x\n", base);
1555 prom_debug("rtas entry = 0x%x\n", entry);
1556 prom_debug("rtas size = 0x%x\n", (long)size);
1557
1558 prom_debug("prom_instantiate_rtas: end...\n");
1559}
1560
1561#ifdef CONFIG_PPC64
1562/*
Ashley Lai4a727422012-08-14 18:34:57 -05001563 * Allocate room for and instantiate Stored Measurement Log (SML)
1564 */
1565static void __init prom_instantiate_sml(void)
1566{
1567 phandle ibmvtpm_node;
1568 ihandle ibmvtpm_inst;
1569 u32 entry = 0, size = 0;
1570 u64 base;
1571
1572 prom_debug("prom_instantiate_sml: start...\n");
1573
1574 ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/ibm,vtpm"));
1575 prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
1576 if (!PHANDLE_VALID(ibmvtpm_node))
1577 return;
1578
1579 ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/ibm,vtpm"));
1580 if (!IHANDLE_VALID(ibmvtpm_inst)) {
1581 prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
1582 return;
1583 }
1584
1585 if (call_prom_ret("call-method", 2, 2, &size,
1586 ADDR("sml-get-handover-size"),
1587 ibmvtpm_inst) != 0 || size == 0) {
1588 prom_printf("SML get handover size failed\n");
1589 return;
1590 }
1591
1592 base = alloc_down(size, PAGE_SIZE, 0);
1593 if (base == 0)
1594 prom_panic("Could not allocate memory for sml\n");
1595
1596 prom_printf("instantiating sml at 0x%x...", base);
1597
1598 if (call_prom_ret("call-method", 4, 2, &entry,
1599 ADDR("sml-handover"),
1600 ibmvtpm_inst, size, base) != 0 || entry == 0) {
1601 prom_printf("SML handover failed\n");
1602 return;
1603 }
1604 prom_printf(" done\n");
1605
1606 reserve_mem(base, size);
1607
1608 prom_setprop(ibmvtpm_node, "/ibm,vtpm", "linux,sml-base",
1609 &base, sizeof(base));
1610 prom_setprop(ibmvtpm_node, "/ibm,vtpm", "linux,sml-size",
1611 &size, sizeof(size));
1612
1613 prom_debug("sml base = 0x%x\n", base);
1614 prom_debug("sml size = 0x%x\n", (long)size);
1615
1616 prom_debug("prom_instantiate_sml: end...\n");
1617}
1618
1619/*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001620 * Allocate room for and initialize TCE tables
1621 */
1622static void __init prom_initialize_tce_table(void)
1623{
1624 phandle node;
1625 ihandle phb_node;
1626 char compatible[64], type[64], model[64];
Anton Blanchard5827d412012-11-26 17:40:03 +00001627 char *path = prom_scratch;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001628 u64 base, align;
1629 u32 minalign, minsize;
1630 u64 tce_entry, *tce_entryp;
1631 u64 local_alloc_top, local_alloc_bottom;
1632 u64 i;
1633
Anton Blanchard5827d412012-11-26 17:40:03 +00001634 if (prom_iommu_off)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001635 return;
1636
1637 prom_debug("starting prom_initialize_tce_table\n");
1638
1639 /* Cache current top of allocs so we reserve a single block */
Anton Blanchard5827d412012-11-26 17:40:03 +00001640 local_alloc_top = alloc_top_high;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001641 local_alloc_bottom = local_alloc_top;
1642
1643 /* Search all nodes looking for PHBs. */
1644 for (node = 0; prom_next_node(&node); ) {
1645 compatible[0] = 0;
1646 type[0] = 0;
1647 model[0] = 0;
1648 prom_getprop(node, "compatible",
1649 compatible, sizeof(compatible));
1650 prom_getprop(node, "device_type", type, sizeof(type));
1651 prom_getprop(node, "model", model, sizeof(model));
1652
Anton Blanchard5827d412012-11-26 17:40:03 +00001653 if ((type[0] == 0) || (strstr(type, "pci") == NULL))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001654 continue;
1655
Linas Vepstase788ff12007-09-07 03:45:21 +10001656 /* Keep the old logic intact to avoid regression. */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001657 if (compatible[0] != 0) {
Anton Blanchard5827d412012-11-26 17:40:03 +00001658 if ((strstr(compatible, "python") == NULL) &&
1659 (strstr(compatible, "Speedwagon") == NULL) &&
1660 (strstr(compatible, "Winnipeg") == NULL))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001661 continue;
1662 } else if (model[0] != 0) {
Anton Blanchard5827d412012-11-26 17:40:03 +00001663 if ((strstr(model, "ython") == NULL) &&
1664 (strstr(model, "peedwagon") == NULL) &&
1665 (strstr(model, "innipeg") == NULL))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001666 continue;
1667 }
1668
1669 if (prom_getprop(node, "tce-table-minalign", &minalign,
1670 sizeof(minalign)) == PROM_ERROR)
1671 minalign = 0;
1672 if (prom_getprop(node, "tce-table-minsize", &minsize,
1673 sizeof(minsize)) == PROM_ERROR)
1674 minsize = 4UL << 20;
1675
1676 /*
1677 * Even though we read what OF wants, we just set the table
1678 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
1679 * By doing this, we avoid the pitfalls of trying to DMA to
1680 * MMIO space and the DMA alias hole.
1681 *
1682 * On POWER4, firmware sets the TCE region by assuming
1683 * each TCE table is 8MB. Using this memory for anything
1684 * else will impact performance, so we always allocate 8MB.
1685 * Anton
1686 */
Michael Ellermand3dbeef2012-08-19 21:44:01 +00001687 if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001688 minsize = 8UL << 20;
1689 else
1690 minsize = 4UL << 20;
1691
1692 /* Align to the greater of the align or size */
1693 align = max(minalign, minsize);
1694 base = alloc_down(minsize, align, 1);
1695 if (base == 0)
1696 prom_panic("ERROR, cannot find space for TCE table.\n");
1697 if (base < local_alloc_bottom)
1698 local_alloc_bottom = base;
1699
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001700 /* It seems OF doesn't null-terminate the path :-( */
Li Zefanaca71ef2007-11-05 13:21:56 +11001701 memset(path, 0, PROM_SCRATCH_SIZE);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001702 /* Call OF to setup the TCE hardware */
1703 if (call_prom("package-to-path", 3, 1, node,
1704 path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1705 prom_printf("package-to-path failed\n");
1706 }
1707
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001708 /* Save away the TCE table attributes for later use. */
1709 prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1710 prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1711
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001712 prom_debug("TCE table: %s\n", path);
1713 prom_debug("\tnode = 0x%x\n", node);
1714 prom_debug("\tbase = 0x%x\n", base);
1715 prom_debug("\tsize = 0x%x\n", minsize);
1716
1717 /* Initialize the table to have a one-to-one mapping
1718 * over the allocated size.
1719 */
Ingo Molnar2b931fb2009-01-06 13:56:52 +00001720 tce_entryp = (u64 *)base;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001721 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1722 tce_entry = (i << PAGE_SHIFT);
1723 tce_entry |= 0x3;
1724 *tce_entryp = tce_entry;
1725 }
1726
1727 prom_printf("opening PHB %s", path);
1728 phb_node = call_prom("open", 1, 1, path);
1729 if (phb_node == 0)
1730 prom_printf("... failed\n");
1731 else
1732 prom_printf("... done\n");
1733
1734 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1735 phb_node, -1, minsize,
1736 (u32) base, (u32) (base >> 32));
1737 call_prom("close", 1, 0, phb_node);
1738 }
1739
1740 reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1741
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001742 /* These are only really needed if there is a memory limit in
1743 * effect, but we don't know so export them always. */
Anton Blanchard5827d412012-11-26 17:40:03 +00001744 prom_tce_alloc_start = local_alloc_bottom;
1745 prom_tce_alloc_end = local_alloc_top;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001746
1747 /* Flag the first invalid entry */
1748 prom_debug("ending prom_initialize_tce_table\n");
1749}
1750#endif
1751
1752/*
1753 * With CHRP SMP we need to use the OF to start the other processors.
1754 * We can't wait until smp_boot_cpus (the OF is trashed by then)
1755 * so we have to put the processors into a holding pattern controlled
1756 * by the kernel (not OF) before we destroy the OF.
1757 *
1758 * This uses a chunk of low memory, puts some holding pattern
1759 * code there and sends the other processors off to there until
1760 * smp_boot_cpus tells them to do something. The holding pattern
1761 * checks that address until its cpu # is there, when it is that
1762 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
1763 * of setting those values.
1764 *
1765 * We also use physical address 0x4 here to tell when a cpu
1766 * is in its holding pattern code.
1767 *
1768 * -- Cort
1769 */
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001770/*
1771 * We want to reference the copy of __secondary_hold_* in the
1772 * 0 - 0x100 address range
1773 */
1774#define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
1775
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001776static void __init prom_hold_cpus(void)
1777{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001778 unsigned long i;
1779 unsigned int reg;
1780 phandle node;
1781 char type[64];
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001782 unsigned long *spinloop
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001783 = (void *) LOW_ADDR(__secondary_hold_spinloop);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001784 unsigned long *acknowledge
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001785 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001786 unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001787
1788 prom_debug("prom_hold_cpus: start...\n");
1789 prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
1790 prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
1791 prom_debug(" 1) acknowledge = 0x%x\n",
1792 (unsigned long)acknowledge);
1793 prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
1794 prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
1795
1796 /* Set the common spinloop variable, so all of the secondary cpus
1797 * will block when they are awakened from their OF spinloop.
1798 * This must occur for both SMP and non SMP kernels, since OF will
1799 * be trashed when we move the kernel.
1800 */
1801 *spinloop = 0;
1802
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001803 /* look for cpus */
1804 for (node = 0; prom_next_node(&node); ) {
1805 type[0] = 0;
1806 prom_getprop(node, "device_type", type, sizeof(type));
Anton Blanchard5827d412012-11-26 17:40:03 +00001807 if (strcmp(type, "cpu") != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001808 continue;
1809
1810 /* Skip non-configured cpus. */
1811 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
Anton Blanchard5827d412012-11-26 17:40:03 +00001812 if (strcmp(type, "okay") != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001813 continue;
1814
1815 reg = -1;
1816 prom_getprop(node, "reg", &reg, sizeof(reg));
1817
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001818 prom_debug("cpu hw idx = %lu\n", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001819
1820 /* Init the acknowledge var which will be reset by
1821 * the secondary cpu when it awakens from its OF
1822 * spinloop.
1823 */
1824 *acknowledge = (unsigned long)-1;
1825
Anton Blanchard5827d412012-11-26 17:40:03 +00001826 if (reg != prom.cpu) {
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001827 /* Primary Thread of non-boot cpu or any thread */
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001828 prom_printf("starting cpu hw idx %lu... ", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001829 call_prom("start-cpu", 3, 0, node,
1830 secondary_hold, reg);
1831
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001832 for (i = 0; (i < 100000000) &&
1833 (*acknowledge == ((unsigned long)-1)); i++ )
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001834 mb();
1835
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001836 if (*acknowledge == reg)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001837 prom_printf("done\n");
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001838 else
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001839 prom_printf("failed: %x\n", *acknowledge);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001840 }
1841#ifdef CONFIG_SMP
1842 else
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001843 prom_printf("boot cpu hw idx %lu\n", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001844#endif /* CONFIG_SMP */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001845 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001846
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001847 prom_debug("prom_hold_cpus: end...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001848}
1849
1850
1851static void __init prom_init_client_services(unsigned long pp)
1852{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001853 /* Get a handle to the prom entry point before anything else */
Anton Blanchard5827d412012-11-26 17:40:03 +00001854 prom_entry = pp;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001855
1856 /* get a handle for the stdout device */
Anton Blanchard5827d412012-11-26 17:40:03 +00001857 prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1858 if (!PHANDLE_VALID(prom.chosen))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001859 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1860
1861 /* get device tree root */
Anton Blanchard5827d412012-11-26 17:40:03 +00001862 prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
1863 if (!PHANDLE_VALID(prom.root))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001864 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001865
Anton Blanchard5827d412012-11-26 17:40:03 +00001866 prom.mmumap = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001867}
1868
Paul Mackerrasa575b802005-10-23 17:23:21 +10001869#ifdef CONFIG_PPC32
1870/*
1871 * For really old powermacs, we need to map things we claim.
1872 * For that, we need the ihandle of the mmu.
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001873 * Also, on the longtrail, we need to work around other bugs.
Paul Mackerrasa575b802005-10-23 17:23:21 +10001874 */
1875static void __init prom_find_mmu(void)
1876{
Paul Mackerrasa575b802005-10-23 17:23:21 +10001877 phandle oprom;
1878 char version[64];
1879
1880 oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1881 if (!PHANDLE_VALID(oprom))
1882 return;
1883 if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1884 return;
1885 version[sizeof(version) - 1] = 0;
Paul Mackerrasa575b802005-10-23 17:23:21 +10001886 /* XXX might need to add other versions here */
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001887 if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1888 of_workarounds = OF_WA_CLAIM;
1889 else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1890 of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1891 call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1892 } else
Paul Mackerrasa575b802005-10-23 17:23:21 +10001893 return;
Anton Blanchard5827d412012-11-26 17:40:03 +00001894 prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
1895 prom_getprop(prom.chosen, "mmu", &prom.mmumap,
1896 sizeof(prom.mmumap));
1897 if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001898 of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001899}
1900#else
1901#define prom_find_mmu()
1902#endif
1903
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001904static void __init prom_init_stdout(void)
1905{
Anton Blanchard5827d412012-11-26 17:40:03 +00001906 char *path = of_stdout_device;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001907 char type[16];
1908 u32 val;
1909
Anton Blanchard5827d412012-11-26 17:40:03 +00001910 if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001911 prom_panic("cannot find stdout");
1912
Anton Blanchard5827d412012-11-26 17:40:03 +00001913 prom.stdout = val;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001914
1915 /* Get the full OF pathname of the stdout device */
1916 memset(path, 0, 256);
Anton Blanchard5827d412012-11-26 17:40:03 +00001917 call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
1918 val = call_prom("instance-to-package", 1, 1, prom.stdout);
1919 prom_setprop(prom.chosen, "/chosen", "linux,stdout-package",
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001920 &val, sizeof(val));
Anton Blanchard5827d412012-11-26 17:40:03 +00001921 prom_printf("OF stdout device is: %s\n", of_stdout_device);
1922 prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001923 path, strlen(path) + 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001924
1925 /* If it's a display, note it */
1926 memset(type, 0, sizeof(type));
1927 prom_getprop(val, "device_type", type, sizeof(type));
Anton Blanchard5827d412012-11-26 17:40:03 +00001928 if (strcmp(type, "display") == 0)
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001929 prom_setprop(val, path, "linux,boot-display", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001930}
1931
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001932static int __init prom_find_machine_type(void)
1933{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001934 char compat[256];
1935 int len, i = 0;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001936#ifdef CONFIG_PPC64
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001937 phandle rtas;
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001938 int x;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001939#endif
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001940
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00001941 /* Look for a PowerMac or a Cell */
Anton Blanchard5827d412012-11-26 17:40:03 +00001942 len = prom_getprop(prom.root, "compatible",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001943 compat, sizeof(compat)-1);
1944 if (len > 0) {
1945 compat[len] = 0;
1946 while (i < len) {
1947 char *p = &compat[i];
1948 int sl = strlen(p);
1949 if (sl == 0)
1950 break;
Anton Blanchard5827d412012-11-26 17:40:03 +00001951 if (strstr(p, "Power Macintosh") ||
1952 strstr(p, "MacRISC"))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001953 return PLATFORM_POWERMAC;
Arnd Bergmann133dda12006-06-07 12:04:18 +10001954#ifdef CONFIG_PPC64
1955 /* We must make sure we don't detect the IBM Cell
1956 * blades as pSeries due to some firmware issues,
1957 * so we do it here.
1958 */
Anton Blanchard5827d412012-11-26 17:40:03 +00001959 if (strstr(p, "IBM,CBEA") ||
1960 strstr(p, "IBM,CPBW-1.0"))
Arnd Bergmann133dda12006-06-07 12:04:18 +10001961 return PLATFORM_GENERIC;
1962#endif /* CONFIG_PPC64 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001963 i += sl + 1;
1964 }
1965 }
1966#ifdef CONFIG_PPC64
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00001967 /* Try to detect OPAL */
1968 if (PHANDLE_VALID(call_prom("finddevice", 1, 1, ADDR("/ibm,opal"))))
1969 return PLATFORM_OPAL;
1970
1971 /* Try to figure out if it's an IBM pSeries or any other
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001972 * PAPR compliant platform. We assume it is if :
1973 * - /device_type is "chrp" (please, do NOT use that for future
1974 * non-IBM designs !
1975 * - it has /rtas
1976 */
Anton Blanchard5827d412012-11-26 17:40:03 +00001977 len = prom_getprop(prom.root, "device_type",
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001978 compat, sizeof(compat)-1);
1979 if (len <= 0)
1980 return PLATFORM_GENERIC;
Anton Blanchard5827d412012-11-26 17:40:03 +00001981 if (strcmp(compat, "chrp"))
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001982 return PLATFORM_GENERIC;
1983
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001984 /* Default to pSeries. We need to know if we are running LPAR */
1985 rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001986 if (!PHANDLE_VALID(rtas))
1987 return PLATFORM_GENERIC;
1988 x = prom_getproplen(rtas, "ibm,hypertas-functions");
1989 if (x != PROM_ERROR) {
Anton Blanchard4da727a2009-03-31 20:06:14 +00001990 prom_debug("Hypertas detected, assuming LPAR !\n");
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001991 return PLATFORM_PSERIES_LPAR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001992 }
1993 return PLATFORM_PSERIES;
1994#else
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001995 return PLATFORM_GENERIC;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001996#endif
1997}
1998
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001999static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
2000{
2001 return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
2002}
2003
2004/*
2005 * If we have a display that we don't know how to drive,
2006 * we will want to try to execute OF's open method for it
2007 * later. However, OF will probably fall over if we do that
2008 * we've taken over the MMU.
2009 * So we check whether we will need to open the display,
2010 * and if so, open it now.
2011 */
2012static void __init prom_check_displays(void)
2013{
2014 char type[16], *path;
2015 phandle node;
2016 ihandle ih;
2017 int i;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002018
2019 static unsigned char default_colors[] = {
2020 0x00, 0x00, 0x00,
2021 0x00, 0x00, 0xaa,
2022 0x00, 0xaa, 0x00,
2023 0x00, 0xaa, 0xaa,
2024 0xaa, 0x00, 0x00,
2025 0xaa, 0x00, 0xaa,
2026 0xaa, 0xaa, 0x00,
2027 0xaa, 0xaa, 0xaa,
2028 0x55, 0x55, 0x55,
2029 0x55, 0x55, 0xff,
2030 0x55, 0xff, 0x55,
2031 0x55, 0xff, 0xff,
2032 0xff, 0x55, 0x55,
2033 0xff, 0x55, 0xff,
2034 0xff, 0xff, 0x55,
2035 0xff, 0xff, 0xff
2036 };
2037 const unsigned char *clut;
2038
Anton Blanchard4da727a2009-03-31 20:06:14 +00002039 prom_debug("Looking for displays\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002040 for (node = 0; prom_next_node(&node); ) {
2041 memset(type, 0, sizeof(type));
2042 prom_getprop(node, "device_type", type, sizeof(type));
Anton Blanchard5827d412012-11-26 17:40:03 +00002043 if (strcmp(type, "display") != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002044 continue;
2045
2046 /* It seems OF doesn't null-terminate the path :-( */
Anton Blanchard5827d412012-11-26 17:40:03 +00002047 path = prom_scratch;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002048 memset(path, 0, PROM_SCRATCH_SIZE);
2049
2050 /*
2051 * leave some room at the end of the path for appending extra
2052 * arguments
2053 */
2054 if (call_prom("package-to-path", 3, 1, node, path,
2055 PROM_SCRATCH_SIZE-10) == PROM_ERROR)
2056 continue;
Anton Blanchard1f8737a2009-03-31 20:06:15 +00002057 prom_printf("found display : %s, opening... ", path);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002058
2059 ih = call_prom("open", 1, 1, path);
2060 if (ih == 0) {
2061 prom_printf("failed\n");
2062 continue;
2063 }
2064
2065 /* Success */
2066 prom_printf("done\n");
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002067 prom_setprop(node, path, "linux,opened", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002068
2069 /* Setup a usable color table when the appropriate
2070 * method is available. Should update this to set-colors */
Anton Blanchard5827d412012-11-26 17:40:03 +00002071 clut = default_colors;
Benjamin Herrenschmidt3f536382011-12-14 13:55:11 +00002072 for (i = 0; i < 16; i++, clut += 3)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002073 if (prom_set_color(ih, i, clut[0], clut[1],
2074 clut[2]) != 0)
2075 break;
2076
2077#ifdef CONFIG_LOGO_LINUX_CLUT224
Anton Blanchard5827d412012-11-26 17:40:03 +00002078 clut = PTRRELOC(logo_linux_clut224.clut);
2079 for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002080 if (prom_set_color(ih, i + 32, clut[0], clut[1],
2081 clut[2]) != 0)
2082 break;
2083#endif /* CONFIG_LOGO_LINUX_CLUT224 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002084 }
2085}
2086
2087
2088/* Return (relocated) pointer to this much memory: moves initrd if reqd. */
2089static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
2090 unsigned long needed, unsigned long align)
2091{
2092 void *ret;
2093
2094 *mem_start = _ALIGN(*mem_start, align);
2095 while ((*mem_start + needed) > *mem_end) {
2096 unsigned long room, chunk;
2097
2098 prom_debug("Chunk exhausted, claiming more at %x...\n",
Anton Blanchard5827d412012-11-26 17:40:03 +00002099 alloc_bottom);
2100 room = alloc_top - alloc_bottom;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002101 if (room > DEVTREE_CHUNK_SIZE)
2102 room = DEVTREE_CHUNK_SIZE;
2103 if (room < PAGE_SIZE)
Anton Blanchardfbafd722011-07-25 20:47:51 +00002104 prom_panic("No memory for flatten_device_tree "
2105 "(no room)\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002106 chunk = alloc_up(room, 0);
2107 if (chunk == 0)
Anton Blanchardfbafd722011-07-25 20:47:51 +00002108 prom_panic("No memory for flatten_device_tree "
2109 "(claim failed)\n");
Anton Blanchard966728d2011-07-25 20:47:07 +00002110 *mem_end = chunk + room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002111 }
2112
2113 ret = (void *)*mem_start;
2114 *mem_start += needed;
2115
2116 return ret;
2117}
2118
2119#define dt_push_token(token, mem_start, mem_end) \
2120 do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
2121
2122static unsigned long __init dt_find_string(char *str)
2123{
2124 char *s, *os;
2125
Anton Blanchard5827d412012-11-26 17:40:03 +00002126 s = os = (char *)dt_string_start;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002127 s += 4;
Anton Blanchard5827d412012-11-26 17:40:03 +00002128 while (s < (char *)dt_string_end) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002129 if (strcmp(s, str) == 0)
2130 return s - os;
2131 s += strlen(s) + 1;
2132 }
2133 return 0;
2134}
2135
2136/*
2137 * The Open Firmware 1275 specification states properties must be 31 bytes or
2138 * less, however not all firmwares obey this. Make it 64 bytes to be safe.
2139 */
2140#define MAX_PROPERTY_NAME 64
2141
2142static void __init scan_dt_build_strings(phandle node,
2143 unsigned long *mem_start,
2144 unsigned long *mem_end)
2145{
2146 char *prev_name, *namep, *sstart;
2147 unsigned long soff;
2148 phandle child;
2149
Anton Blanchard5827d412012-11-26 17:40:03 +00002150 sstart = (char *)dt_string_start;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002151
2152 /* get and store all property names */
Anton Blanchard5827d412012-11-26 17:40:03 +00002153 prev_name = "";
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002154 for (;;) {
2155 /* 64 is max len of name including nul. */
2156 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
2157 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
2158 /* No more nodes: unwind alloc */
2159 *mem_start = (unsigned long)namep;
2160 break;
2161 }
2162
2163 /* skip "name" */
Anton Blanchard5827d412012-11-26 17:40:03 +00002164 if (strcmp(namep, "name") == 0) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002165 *mem_start = (unsigned long)namep;
Anton Blanchard5827d412012-11-26 17:40:03 +00002166 prev_name = "name";
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002167 continue;
2168 }
2169 /* get/create string entry */
2170 soff = dt_find_string(namep);
2171 if (soff != 0) {
2172 *mem_start = (unsigned long)namep;
2173 namep = sstart + soff;
2174 } else {
2175 /* Trim off some if we can */
2176 *mem_start = (unsigned long)namep + strlen(namep) + 1;
Anton Blanchard5827d412012-11-26 17:40:03 +00002177 dt_string_end = *mem_start;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002178 }
2179 prev_name = namep;
2180 }
2181
2182 /* do all our children */
2183 child = call_prom("child", 1, 1, node);
2184 while (child != 0) {
2185 scan_dt_build_strings(child, mem_start, mem_end);
2186 child = call_prom("peer", 1, 1, child);
2187 }
2188}
2189
2190static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
2191 unsigned long *mem_end)
2192{
2193 phandle child;
2194 char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
2195 unsigned long soff;
2196 unsigned char *valp;
2197 static char pname[MAX_PROPERTY_NAME];
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002198 int l, room, has_phandle = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002199
2200 dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
2201
2202 /* get the node's full name */
2203 namep = (char *)*mem_start;
Paul Mackerrasc49888202005-10-26 21:52:53 +10002204 room = *mem_end - *mem_start;
2205 if (room > 255)
2206 room = 255;
2207 l = call_prom("package-to-path", 3, 1, node, namep, room);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002208 if (l >= 0) {
2209 /* Didn't fit? Get more room. */
Paul Mackerrasc49888202005-10-26 21:52:53 +10002210 if (l >= room) {
2211 if (l >= *mem_end - *mem_start)
2212 namep = make_room(mem_start, mem_end, l+1, 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002213 call_prom("package-to-path", 3, 1, node, namep, l);
2214 }
2215 namep[l] = '\0';
2216
2217 /* Fixup an Apple bug where they have bogus \0 chars in the
Paul Mackerrasa575b802005-10-23 17:23:21 +10002218 * middle of the path in some properties, and extract
2219 * the unit name (everything after the last '/').
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002220 */
Paul Mackerrasa575b802005-10-23 17:23:21 +10002221 for (lp = p = namep, ep = namep + l; p < ep; p++) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002222 if (*p == '/')
Paul Mackerrasa575b802005-10-23 17:23:21 +10002223 lp = namep;
2224 else if (*p != 0)
2225 *lp++ = *p;
2226 }
2227 *lp = 0;
2228 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002229 }
2230
2231 /* get it again for debugging */
Anton Blanchard5827d412012-11-26 17:40:03 +00002232 path = prom_scratch;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002233 memset(path, 0, PROM_SCRATCH_SIZE);
2234 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
2235
2236 /* get and store all properties */
Anton Blanchard5827d412012-11-26 17:40:03 +00002237 prev_name = "";
2238 sstart = (char *)dt_string_start;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002239 for (;;) {
2240 if (call_prom("nextprop", 3, 1, node, prev_name,
Anton Blanchard5827d412012-11-26 17:40:03 +00002241 pname) != 1)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002242 break;
2243
2244 /* skip "name" */
Anton Blanchard5827d412012-11-26 17:40:03 +00002245 if (strcmp(pname, "name") == 0) {
2246 prev_name = "name";
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002247 continue;
2248 }
2249
2250 /* find string offset */
Anton Blanchard5827d412012-11-26 17:40:03 +00002251 soff = dt_find_string(pname);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002252 if (soff == 0) {
2253 prom_printf("WARNING: Can't find string index for"
Anton Blanchard5827d412012-11-26 17:40:03 +00002254 " <%s>, node %s\n", pname, path);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002255 break;
2256 }
2257 prev_name = sstart + soff;
2258
2259 /* get length */
Anton Blanchard5827d412012-11-26 17:40:03 +00002260 l = call_prom("getproplen", 2, 1, node, pname);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002261
2262 /* sanity checks */
2263 if (l == PROM_ERROR)
2264 continue;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002265
2266 /* push property head */
2267 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2268 dt_push_token(l, mem_start, mem_end);
2269 dt_push_token(soff, mem_start, mem_end);
2270
2271 /* push property content */
2272 valp = make_room(mem_start, mem_end, l, 4);
Anton Blanchard5827d412012-11-26 17:40:03 +00002273 call_prom("getprop", 4, 1, node, pname, valp, l);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002274 *mem_start = _ALIGN(*mem_start, 4);
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002275
Anton Blanchard5827d412012-11-26 17:40:03 +00002276 if (!strcmp(pname, "phandle"))
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002277 has_phandle = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002278 }
2279
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002280 /* Add a "linux,phandle" property if no "phandle" property already
2281 * existed (can happen with OPAL)
2282 */
2283 if (!has_phandle) {
Anton Blanchard5827d412012-11-26 17:40:03 +00002284 soff = dt_find_string("linux,phandle");
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002285 if (soff == 0)
2286 prom_printf("WARNING: Can't find string index for"
2287 " <linux-phandle> node %s\n", path);
2288 else {
2289 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2290 dt_push_token(4, mem_start, mem_end);
2291 dt_push_token(soff, mem_start, mem_end);
2292 valp = make_room(mem_start, mem_end, 4, 4);
2293 *(u32 *)valp = node;
2294 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002295 }
2296
2297 /* do all our children */
2298 child = call_prom("child", 1, 1, node);
2299 while (child != 0) {
2300 scan_dt_build_struct(child, mem_start, mem_end);
2301 child = call_prom("peer", 1, 1, child);
2302 }
2303
2304 dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
2305}
2306
2307static void __init flatten_device_tree(void)
2308{
2309 phandle root;
2310 unsigned long mem_start, mem_end, room;
2311 struct boot_param_header *hdr;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002312 char *namep;
2313 u64 *rsvmap;
2314
2315 /*
2316 * Check how much room we have between alloc top & bottom (+/- a
Anton Blanchardfbafd722011-07-25 20:47:51 +00002317 * few pages), crop to 1MB, as this is our "chunk" size
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002318 */
Anton Blanchard5827d412012-11-26 17:40:03 +00002319 room = alloc_top - alloc_bottom - 0x4000;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002320 if (room > DEVTREE_CHUNK_SIZE)
2321 room = DEVTREE_CHUNK_SIZE;
Anton Blanchard5827d412012-11-26 17:40:03 +00002322 prom_debug("starting device tree allocs at %x\n", alloc_bottom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002323
2324 /* Now try to claim that */
2325 mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
2326 if (mem_start == 0)
2327 prom_panic("Can't allocate initial device-tree chunk\n");
Anton Blanchard966728d2011-07-25 20:47:07 +00002328 mem_end = mem_start + room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002329
2330 /* Get root of tree */
2331 root = call_prom("peer", 1, 1, (phandle)0);
2332 if (root == (phandle)0)
2333 prom_panic ("couldn't get device tree root\n");
2334
2335 /* Build header and make room for mem rsv map */
2336 mem_start = _ALIGN(mem_start, 4);
2337 hdr = make_room(&mem_start, &mem_end,
2338 sizeof(struct boot_param_header), 4);
Anton Blanchard5827d412012-11-26 17:40:03 +00002339 dt_header_start = (unsigned long)hdr;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002340 rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
2341
2342 /* Start of strings */
2343 mem_start = PAGE_ALIGN(mem_start);
Anton Blanchard5827d412012-11-26 17:40:03 +00002344 dt_string_start = mem_start;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002345 mem_start += 4; /* hole */
2346
2347 /* Add "linux,phandle" in there, we'll need it */
2348 namep = make_room(&mem_start, &mem_end, 16, 1);
Anton Blanchard5827d412012-11-26 17:40:03 +00002349 strcpy(namep, "linux,phandle");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002350 mem_start = (unsigned long)namep + strlen(namep) + 1;
2351
2352 /* Build string array */
2353 prom_printf("Building dt strings...\n");
2354 scan_dt_build_strings(root, &mem_start, &mem_end);
Anton Blanchard5827d412012-11-26 17:40:03 +00002355 dt_string_end = mem_start;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002356
2357 /* Build structure */
2358 mem_start = PAGE_ALIGN(mem_start);
Anton Blanchard5827d412012-11-26 17:40:03 +00002359 dt_struct_start = mem_start;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002360 prom_printf("Building dt structure...\n");
2361 scan_dt_build_struct(root, &mem_start, &mem_end);
2362 dt_push_token(OF_DT_END, &mem_start, &mem_end);
Anton Blanchard5827d412012-11-26 17:40:03 +00002363 dt_struct_end = PAGE_ALIGN(mem_start);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002364
2365 /* Finish header */
Anton Blanchard5827d412012-11-26 17:40:03 +00002366 hdr->boot_cpuid_phys = prom.cpu;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002367 hdr->magic = OF_DT_HEADER;
Anton Blanchard5827d412012-11-26 17:40:03 +00002368 hdr->totalsize = dt_struct_end - dt_header_start;
2369 hdr->off_dt_struct = dt_struct_start - dt_header_start;
2370 hdr->off_dt_strings = dt_string_start - dt_header_start;
2371 hdr->dt_strings_size = dt_string_end - dt_string_start;
2372 hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - dt_header_start;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002373 hdr->version = OF_DT_VERSION;
2374 /* Version 16 is not backward compatible */
2375 hdr->last_comp_version = 0x10;
2376
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05002377 /* Copy the reserve map in */
Anton Blanchard5827d412012-11-26 17:40:03 +00002378 memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002379
2380#ifdef DEBUG_PROM
2381 {
2382 int i;
2383 prom_printf("reserved memory map:\n");
Anton Blanchard5827d412012-11-26 17:40:03 +00002384 for (i = 0; i < mem_reserve_cnt; i++)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002385 prom_printf(" %x - %x\n",
Anton Blanchard5827d412012-11-26 17:40:03 +00002386 mem_reserve_map[i].base,
2387 mem_reserve_map[i].size);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002388 }
2389#endif
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05002390 /* Bump mem_reserve_cnt to cause further reservations to fail
2391 * since it's too late.
2392 */
Anton Blanchard5827d412012-11-26 17:40:03 +00002393 mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002394
2395 prom_printf("Device tree strings 0x%x -> 0x%x\n",
Anton Blanchard5827d412012-11-26 17:40:03 +00002396 dt_string_start, dt_string_end);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002397 prom_printf("Device tree struct 0x%x -> 0x%x\n",
Anton Blanchard5827d412012-11-26 17:40:03 +00002398 dt_struct_start, dt_struct_end);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002399
2400}
2401
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002402#ifdef CONFIG_PPC_MAPLE
2403/* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2404 * The values are bad, and it doesn't even have the right number of cells. */
2405static void __init fixup_device_tree_maple(void)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002406{
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002407 phandle isa;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002408 u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002409 u32 isa_ranges[6];
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002410 char *name;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002411
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002412 name = "/ht@0/isa@4";
2413 isa = call_prom("finddevice", 1, 1, ADDR(name));
2414 if (!PHANDLE_VALID(isa)) {
2415 name = "/ht@0/isa@6";
2416 isa = call_prom("finddevice", 1, 1, ADDR(name));
2417 rloc = 0x01003000; /* IO space; PCI device = 6 */
2418 }
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002419 if (!PHANDLE_VALID(isa))
2420 return;
2421
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002422 if (prom_getproplen(isa, "ranges") != 12)
2423 return;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002424 if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2425 == PROM_ERROR)
2426 return;
2427
2428 if (isa_ranges[0] != 0x1 ||
2429 isa_ranges[1] != 0xf4000000 ||
2430 isa_ranges[2] != 0x00010000)
2431 return;
2432
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002433 prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002434
2435 isa_ranges[0] = 0x1;
2436 isa_ranges[1] = 0x0;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002437 isa_ranges[2] = rloc;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002438 isa_ranges[3] = 0x0;
2439 isa_ranges[4] = 0x0;
2440 isa_ranges[5] = 0x00010000;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002441 prom_setprop(isa, name, "ranges",
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002442 isa_ranges, sizeof(isa_ranges));
2443}
Harry Ciao8f101a02009-06-17 16:28:00 -07002444
2445#define CPC925_MC_START 0xf8000000
2446#define CPC925_MC_LENGTH 0x1000000
2447/* The values for memory-controller don't have right number of cells */
2448static void __init fixup_device_tree_maple_memory_controller(void)
2449{
2450 phandle mc;
2451 u32 mc_reg[4];
2452 char *name = "/hostbridge@f8000000";
Harry Ciao8f101a02009-06-17 16:28:00 -07002453 u32 ac, sc;
2454
2455 mc = call_prom("finddevice", 1, 1, ADDR(name));
2456 if (!PHANDLE_VALID(mc))
2457 return;
2458
2459 if (prom_getproplen(mc, "reg") != 8)
2460 return;
2461
Anton Blanchard5827d412012-11-26 17:40:03 +00002462 prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
2463 prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
Harry Ciao8f101a02009-06-17 16:28:00 -07002464 if ((ac != 2) || (sc != 2))
2465 return;
2466
2467 if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
2468 return;
2469
2470 if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
2471 return;
2472
2473 prom_printf("Fixing up bogus hostbridge on Maple...\n");
2474
2475 mc_reg[0] = 0x0;
2476 mc_reg[1] = CPC925_MC_START;
2477 mc_reg[2] = 0x0;
2478 mc_reg[3] = CPC925_MC_LENGTH;
2479 prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
2480}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002481#else
2482#define fixup_device_tree_maple()
Harry Ciao8f101a02009-06-17 16:28:00 -07002483#define fixup_device_tree_maple_memory_controller()
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002484#endif
2485
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002486#ifdef CONFIG_PPC_CHRP
Olaf Heringe4805922007-04-04 18:20:04 +02002487/*
2488 * Pegasos and BriQ lacks the "ranges" property in the isa node
2489 * Pegasos needs decimal IRQ 14/15, not hexadecimal
Olaf Hering556ecf92007-08-18 04:27:17 +10002490 * Pegasos has the IDE configured in legacy mode, but advertised as native
Olaf Heringe4805922007-04-04 18:20:04 +02002491 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002492static void __init fixup_device_tree_chrp(void)
2493{
Olaf Heringe4805922007-04-04 18:20:04 +02002494 phandle ph;
2495 u32 prop[6];
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002496 u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002497 char *name;
2498 int rc;
2499
2500 name = "/pci@80000000/isa@c";
Olaf Heringe4805922007-04-04 18:20:04 +02002501 ph = call_prom("finddevice", 1, 1, ADDR(name));
2502 if (!PHANDLE_VALID(ph)) {
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002503 name = "/pci@ff500000/isa@6";
Olaf Heringe4805922007-04-04 18:20:04 +02002504 ph = call_prom("finddevice", 1, 1, ADDR(name));
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002505 rloc = 0x01003000; /* IO space; PCI device = 6 */
2506 }
Olaf Heringe4805922007-04-04 18:20:04 +02002507 if (PHANDLE_VALID(ph)) {
2508 rc = prom_getproplen(ph, "ranges");
2509 if (rc == 0 || rc == PROM_ERROR) {
2510 prom_printf("Fixing up missing ISA range on Pegasos...\n");
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002511
Olaf Heringe4805922007-04-04 18:20:04 +02002512 prop[0] = 0x1;
2513 prop[1] = 0x0;
2514 prop[2] = rloc;
2515 prop[3] = 0x0;
2516 prop[4] = 0x0;
2517 prop[5] = 0x00010000;
2518 prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2519 }
2520 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002521
Olaf Heringe4805922007-04-04 18:20:04 +02002522 name = "/pci@80000000/ide@C,1";
2523 ph = call_prom("finddevice", 1, 1, ADDR(name));
2524 if (PHANDLE_VALID(ph)) {
2525 prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2526 prop[0] = 14;
2527 prop[1] = 0x0;
Olaf Hering556ecf92007-08-18 04:27:17 +10002528 prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2529 prom_printf("Fixing up IDE class-code on Pegasos...\n");
2530 rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2531 if (rc == sizeof(u32)) {
2532 prop[0] &= ~0x5;
2533 prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2534 }
Olaf Heringe4805922007-04-04 18:20:04 +02002535 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002536}
2537#else
2538#define fixup_device_tree_chrp()
2539#endif
2540
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002541#if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002542static void __init fixup_device_tree_pmac(void)
2543{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002544 phandle u3, i2c, mpic;
2545 u32 u3_rev;
2546 u32 interrupts[2];
2547 u32 parent;
2548
2549 /* Some G5s have a missing interrupt definition, fix it up here */
2550 u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2551 if (!PHANDLE_VALID(u3))
2552 return;
2553 i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2554 if (!PHANDLE_VALID(i2c))
2555 return;
2556 mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2557 if (!PHANDLE_VALID(mpic))
2558 return;
2559
2560 /* check if proper rev of u3 */
2561 if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2562 == PROM_ERROR)
2563 return;
Benjamin Herrenschmidt7d496972005-11-07 14:36:21 +11002564 if (u3_rev < 0x35 || u3_rev > 0x39)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002565 return;
2566 /* does it need fixup ? */
2567 if (prom_getproplen(i2c, "interrupts") > 0)
2568 return;
2569
2570 prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2571
2572 /* interrupt on this revision of u3 is number 0 and level */
2573 interrupts[0] = 0;
2574 interrupts[1] = 1;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002575 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2576 &interrupts, sizeof(interrupts));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002577 parent = (u32)mpic;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002578 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2579 &parent, sizeof(parent));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002580}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002581#else
2582#define fixup_device_tree_pmac()
2583#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002584
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002585#ifdef CONFIG_PPC_EFIKA
Grant Likely94d2dde2008-01-24 22:25:32 -07002586/*
2587 * The MPC5200 FEC driver requires an phy-handle property to tell it how
2588 * to talk to the phy. If the phy-handle property is missing, then this
2589 * function is called to add the appropriate nodes and link it to the
2590 * ethernet node.
2591 */
2592static void __init fixup_device_tree_efika_add_phy(void)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002593{
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002594 u32 node;
2595 char prop[64];
Grant Likely94d2dde2008-01-24 22:25:32 -07002596 int rv;
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002597
Grant Likely94d2dde2008-01-24 22:25:32 -07002598 /* Check if /builtin/ethernet exists - bail if it doesn't */
2599 node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002600 if (!PHANDLE_VALID(node))
2601 return;
2602
Grant Likely94d2dde2008-01-24 22:25:32 -07002603 /* Check if the phy-handle property exists - bail if it does */
2604 rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2605 if (!rv)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002606 return;
2607
Grant Likely94d2dde2008-01-24 22:25:32 -07002608 /*
2609 * At this point the ethernet device doesn't have a phy described.
2610 * Now we need to add the missing phy node and linkage
2611 */
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002612
Grant Likely94d2dde2008-01-24 22:25:32 -07002613 /* Check for an MDIO bus node - if missing then create one */
Olaf Hering6f4347c2008-01-10 01:06:08 +11002614 node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2615 if (!PHANDLE_VALID(node)) {
2616 prom_printf("Adding Ethernet MDIO node\n");
2617 call_prom("interpret", 1, 1,
2618 " s\" /builtin\" find-device"
2619 " new-device"
2620 " 1 encode-int s\" #address-cells\" property"
2621 " 0 encode-int s\" #size-cells\" property"
Grant Likely94d2dde2008-01-24 22:25:32 -07002622 " s\" mdio\" device-name"
2623 " s\" fsl,mpc5200b-mdio\" encode-string"
Olaf Hering6f4347c2008-01-10 01:06:08 +11002624 " s\" compatible\" property"
2625 " 0xf0003000 0x400 reg"
2626 " 0x2 encode-int"
2627 " 0x5 encode-int encode+"
2628 " 0x3 encode-int encode+"
2629 " s\" interrupts\" property"
2630 " finish-device");
2631 };
2632
Grant Likely94d2dde2008-01-24 22:25:32 -07002633 /* Check for a PHY device node - if missing then create one and
2634 * give it's phandle to the ethernet node */
2635 node = call_prom("finddevice", 1, 1,
2636 ADDR("/builtin/mdio/ethernet-phy"));
Olaf Hering6f4347c2008-01-10 01:06:08 +11002637 if (!PHANDLE_VALID(node)) {
2638 prom_printf("Adding Ethernet PHY node\n");
2639 call_prom("interpret", 1, 1,
2640 " s\" /builtin/mdio\" find-device"
2641 " new-device"
2642 " s\" ethernet-phy\" device-name"
2643 " 0x10 encode-int s\" reg\" property"
2644 " my-self"
2645 " ihandle>phandle"
2646 " finish-device"
2647 " s\" /builtin/ethernet\" find-device"
2648 " encode-int"
2649 " s\" phy-handle\" property"
2650 " device-end");
2651 }
Grant Likely94d2dde2008-01-24 22:25:32 -07002652}
Olaf Hering6f4347c2008-01-10 01:06:08 +11002653
Grant Likely94d2dde2008-01-24 22:25:32 -07002654static void __init fixup_device_tree_efika(void)
2655{
2656 int sound_irq[3] = { 2, 2, 0 };
2657 int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2658 3,4,0, 3,5,0, 3,6,0, 3,7,0,
2659 3,8,0, 3,9,0, 3,10,0, 3,11,0,
2660 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2661 u32 node;
2662 char prop[64];
2663 int rv, len;
2664
2665 /* Check if we're really running on a EFIKA */
2666 node = call_prom("finddevice", 1, 1, ADDR("/"));
2667 if (!PHANDLE_VALID(node))
2668 return;
2669
2670 rv = prom_getprop(node, "model", prop, sizeof(prop));
2671 if (rv == PROM_ERROR)
2672 return;
2673 if (strcmp(prop, "EFIKA5K2"))
2674 return;
2675
2676 prom_printf("Applying EFIKA device tree fixups\n");
2677
2678 /* Claiming to be 'chrp' is death */
2679 node = call_prom("finddevice", 1, 1, ADDR("/"));
2680 rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2681 if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2682 prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2683
David Woodhouse7f4392c2008-04-14 02:52:38 +10002684 /* CODEGEN,description is exposed in /proc/cpuinfo so
2685 fix that too */
2686 rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2687 if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2688 prom_setprop(node, "/", "CODEGEN,description",
2689 "Efika 5200B PowerPC System",
2690 sizeof("Efika 5200B PowerPC System"));
2691
Grant Likely94d2dde2008-01-24 22:25:32 -07002692 /* Fixup bestcomm interrupts property */
2693 node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2694 if (PHANDLE_VALID(node)) {
2695 len = prom_getproplen(node, "interrupts");
2696 if (len == 12) {
2697 prom_printf("Fixing bestcomm interrupts property\n");
2698 prom_setprop(node, "/builtin/bestcom", "interrupts",
2699 bcomm_irq, sizeof(bcomm_irq));
2700 }
2701 }
2702
2703 /* Fixup sound interrupts property */
2704 node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2705 if (PHANDLE_VALID(node)) {
2706 rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2707 if (rv == PROM_ERROR) {
2708 prom_printf("Adding sound interrupts property\n");
2709 prom_setprop(node, "/builtin/sound", "interrupts",
2710 sound_irq, sizeof(sound_irq));
2711 }
2712 }
2713
2714 /* Make sure ethernet phy-handle property exists */
2715 fixup_device_tree_efika_add_phy();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002716}
2717#else
2718#define fixup_device_tree_efika()
2719#endif
2720
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002721static void __init fixup_device_tree(void)
2722{
2723 fixup_device_tree_maple();
Harry Ciao8f101a02009-06-17 16:28:00 -07002724 fixup_device_tree_maple_memory_controller();
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002725 fixup_device_tree_chrp();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002726 fixup_device_tree_pmac();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002727 fixup_device_tree_efika();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002728}
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002729
2730static void __init prom_find_boot_cpu(void)
2731{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002732 u32 getprop_rval;
2733 ihandle prom_cpu;
2734 phandle cpu_pkg;
2735
Anton Blanchard5827d412012-11-26 17:40:03 +00002736 prom.cpu = 0;
2737 if (prom_getprop(prom.chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
Paul Mackerrasa575b802005-10-23 17:23:21 +10002738 return;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002739
2740 cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2741
2742 prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
Anton Blanchard5827d412012-11-26 17:40:03 +00002743 prom.cpu = getprop_rval;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002744
Anton Blanchard5827d412012-11-26 17:40:03 +00002745 prom_debug("Booting CPU hw index = %lu\n", prom.cpu);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002746}
2747
2748static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2749{
2750#ifdef CONFIG_BLK_DEV_INITRD
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002751 if (r3 && r4 && r4 != 0xdeadbeef) {
2752 unsigned long val;
2753
Anton Blanchard5827d412012-11-26 17:40:03 +00002754 prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
2755 prom_initrd_end = prom_initrd_start + r4;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002756
Anton Blanchard5827d412012-11-26 17:40:03 +00002757 val = prom_initrd_start;
2758 prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002759 &val, sizeof(val));
Anton Blanchard5827d412012-11-26 17:40:03 +00002760 val = prom_initrd_end;
2761 prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002762 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002763
Anton Blanchard5827d412012-11-26 17:40:03 +00002764 reserve_mem(prom_initrd_start,
2765 prom_initrd_end - prom_initrd_start);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002766
Anton Blanchard5827d412012-11-26 17:40:03 +00002767 prom_debug("initrd_start=0x%x\n", prom_initrd_start);
2768 prom_debug("initrd_end=0x%x\n", prom_initrd_end);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002769 }
2770#endif /* CONFIG_BLK_DEV_INITRD */
2771}
2772
Anton Blanchard5ac47f72012-11-26 17:39:03 +00002773#ifdef CONFIG_PPC64
2774#ifdef CONFIG_RELOCATABLE
2775static void reloc_toc(void)
2776{
2777}
2778
2779static void unreloc_toc(void)
2780{
2781}
2782#else
Anton Blanchard16744002013-03-12 01:51:51 +00002783static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
Anton Blanchard5ac47f72012-11-26 17:39:03 +00002784{
2785 unsigned long i;
Anton Blanchard16744002013-03-12 01:51:51 +00002786 unsigned long *toc_entry;
2787
2788 /* Get the start of the TOC by using r2 directly. */
2789 asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
Anton Blanchard5ac47f72012-11-26 17:39:03 +00002790
2791 for (i = 0; i < nr_entries; i++) {
2792 *toc_entry = *toc_entry + offset;
2793 toc_entry++;
2794 }
2795}
2796
2797static void reloc_toc(void)
2798{
2799 unsigned long offset = reloc_offset();
2800 unsigned long nr_entries =
2801 (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
2802
Anton Blanchard16744002013-03-12 01:51:51 +00002803 __reloc_toc(offset, nr_entries);
Anton Blanchard5ac47f72012-11-26 17:39:03 +00002804
2805 mb();
2806}
2807
2808static void unreloc_toc(void)
2809{
2810 unsigned long offset = reloc_offset();
2811 unsigned long nr_entries =
2812 (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
2813
2814 mb();
2815
Anton Blanchard16744002013-03-12 01:51:51 +00002816 __reloc_toc(-offset, nr_entries);
Anton Blanchard5ac47f72012-11-26 17:39:03 +00002817}
2818#endif
2819#endif
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002820
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002821/*
2822 * We enter here early on, when the Open Firmware prom is still
2823 * handling exceptions and the MMU hash table for us.
2824 */
2825
2826unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2827 unsigned long pp,
Paul Mackerras549e8152008-08-30 11:43:47 +10002828 unsigned long r6, unsigned long r7,
2829 unsigned long kbase)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002830{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002831 unsigned long hdr;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002832
2833#ifdef CONFIG_PPC32
Paul Mackerras549e8152008-08-30 11:43:47 +10002834 unsigned long offset = reloc_offset();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002835 reloc_got2(offset);
Anton Blanchard5ac47f72012-11-26 17:39:03 +00002836#else
2837 reloc_toc();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002838#endif
2839
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002840 /*
2841 * First zero the BSS
2842 */
Anton Blanchard5827d412012-11-26 17:40:03 +00002843 memset(&__bss_start, 0, __bss_stop - __bss_start);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002844
2845 /*
2846 * Init interface to Open Firmware, get some node references,
2847 * like /chosen
2848 */
2849 prom_init_client_services(pp);
2850
2851 /*
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002852 * See if this OF is old enough that we need to do explicit maps
2853 * and other workarounds
2854 */
2855 prom_find_mmu();
2856
2857 /*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002858 * Init prom stdout device
2859 */
2860 prom_init_stdout();
2861
Anton Blanchard5827d412012-11-26 17:40:03 +00002862 prom_printf("Preparing to boot %s", linux_banner);
Michael Ellermane7943fb2009-03-04 19:02:01 +00002863
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002864 /*
2865 * Get default machine type. At this point, we do not differentiate
2866 * between pSeries SMP and pSeries LPAR
2867 */
Anton Blanchard5827d412012-11-26 17:40:03 +00002868 of_platform = prom_find_machine_type();
2869 prom_printf("Detected machine type: %x\n", of_platform);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002870
Suzuki Poulose0f890c82011-12-14 22:57:15 +00002871#ifndef CONFIG_NONSTATIC_KERNEL
Olaf Heringadd60ef2006-03-23 22:03:57 +01002872 /* Bail if this is a kdump kernel. */
2873 if (PHYSICAL_START > 0)
2874 prom_panic("Error: You can't boot a kdump kernel from OF!\n");
Paul Mackerras549e8152008-08-30 11:43:47 +10002875#endif
Olaf Heringadd60ef2006-03-23 22:03:57 +01002876
2877 /*
2878 * Check for an initrd
2879 */
2880 prom_check_initrd(r3, r4);
2881
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002882#if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002883 /*
2884 * On pSeries, inform the firmware about our capabilities
2885 */
Anton Blanchard5827d412012-11-26 17:40:03 +00002886 if (of_platform == PLATFORM_PSERIES ||
2887 of_platform == PLATFORM_PSERIES_LPAR)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002888 prom_send_capabilities();
2889#endif
2890
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002891 /*
Arnd Bergmannf3f66f52005-10-31 20:08:37 -05002892 * Copy the CPU hold code
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002893 */
Anton Blanchard5827d412012-11-26 17:40:03 +00002894 if (of_platform != PLATFORM_POWERMAC)
Paul Mackerras549e8152008-08-30 11:43:47 +10002895 copy_and_flush(0, kbase, 0x100, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002896
2897 /*
2898 * Do early parsing of command line
2899 */
2900 early_cmdline_parse();
2901
2902 /*
2903 * Initialize memory management within prom_init
2904 */
2905 prom_init_mem();
2906
2907 /*
2908 * Determine which cpu is actually running right _now_
2909 */
2910 prom_find_boot_cpu();
2911
2912 /*
2913 * Initialize display devices
2914 */
2915 prom_check_displays();
2916
2917#ifdef CONFIG_PPC64
2918 /*
2919 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2920 * that uses the allocator, we need to make sure we get the top of memory
2921 * available for us here...
2922 */
Anton Blanchard5827d412012-11-26 17:40:03 +00002923 if (of_platform == PLATFORM_PSERIES)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002924 prom_initialize_tce_table();
2925#endif
2926
2927 /*
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002928 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
2929 * have a usable RTAS implementation.
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002930 */
Anton Blanchard5827d412012-11-26 17:40:03 +00002931 if (of_platform != PLATFORM_POWERMAC &&
2932 of_platform != PLATFORM_OPAL)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002933 prom_instantiate_rtas();
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002934
2935#ifdef CONFIG_PPC_POWERNV
2936 /* Detect HAL and try instanciating it & doing takeover */
Anton Blanchard5827d412012-11-26 17:40:03 +00002937 if (of_platform == PLATFORM_PSERIES_LPAR) {
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002938 prom_query_opal();
Anton Blanchard5827d412012-11-26 17:40:03 +00002939 if (of_platform == PLATFORM_OPAL) {
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002940 prom_opal_hold_cpus();
2941 prom_opal_takeover();
2942 }
Anton Blanchard5827d412012-11-26 17:40:03 +00002943 } else if (of_platform == PLATFORM_OPAL)
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00002944 prom_instantiate_opal();
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002945#endif
2946
Ashley Lai4a727422012-08-14 18:34:57 -05002947#ifdef CONFIG_PPC64
2948 /* instantiate sml */
2949 prom_instantiate_sml();
2950#endif
2951
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002952 /*
2953 * On non-powermacs, put all CPUs in spin-loops.
2954 *
2955 * PowerMacs use a different mechanism to spin CPUs
2956 */
Anton Blanchard5827d412012-11-26 17:40:03 +00002957 if (of_platform != PLATFORM_POWERMAC &&
2958 of_platform != PLATFORM_OPAL)
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002959 prom_hold_cpus();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002960
2961 /*
2962 * Fill in some infos for use by the kernel later on
2963 */
Anton Blanchard5827d412012-11-26 17:40:03 +00002964 if (prom_memory_limit)
2965 prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
2966 &prom_memory_limit,
Benjamin Krillcf687872009-07-27 22:02:39 +00002967 sizeof(prom_memory_limit));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002968#ifdef CONFIG_PPC64
Anton Blanchard5827d412012-11-26 17:40:03 +00002969 if (prom_iommu_off)
2970 prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002971 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002972
Anton Blanchard5827d412012-11-26 17:40:03 +00002973 if (prom_iommu_force_on)
2974 prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002975 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002976
Anton Blanchard5827d412012-11-26 17:40:03 +00002977 if (prom_tce_alloc_start) {
2978 prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
2979 &prom_tce_alloc_start,
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002980 sizeof(prom_tce_alloc_start));
Anton Blanchard5827d412012-11-26 17:40:03 +00002981 prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
2982 &prom_tce_alloc_end,
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002983 sizeof(prom_tce_alloc_end));
2984 }
2985#endif
2986
2987 /*
2988 * Fixup any known bugs in the device-tree
2989 */
2990 fixup_device_tree();
2991
2992 /*
2993 * Now finally create the flattened device-tree
2994 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00002995 prom_printf("copying OF device tree...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002996 flatten_device_tree();
2997
Paul Mackerras3825ac02005-11-08 22:48:08 +11002998 /*
2999 * in case stdin is USB and still active on IBM machines...
3000 * Unfortunately quiesce crashes on some powermacs if we have
Benjamin Herrenschmidt40dfef62011-11-29 18:22:56 +00003001 * closed stdin already (in particular the powerbook 101). It
3002 * appears that the OPAL version of OFW doesn't like it either.
Paul Mackerras3825ac02005-11-08 22:48:08 +11003003 */
Anton Blanchard5827d412012-11-26 17:40:03 +00003004 if (of_platform != PLATFORM_POWERMAC &&
3005 of_platform != PLATFORM_OPAL)
Paul Mackerras3825ac02005-11-08 22:48:08 +11003006 prom_close_stdin();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003007
3008 /*
3009 * Call OF "quiesce" method to shut down pending DMA's from
3010 * devices etc...
3011 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00003012 prom_printf("Calling quiesce...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003013 call_prom("quiesce", 0, 0);
3014
3015 /*
3016 * And finally, call the kernel passing it the flattened device
3017 * tree and NULL as r5, thus triggering the new entry point which
3018 * is common to us and kexec
3019 */
Anton Blanchard5827d412012-11-26 17:40:03 +00003020 hdr = dt_header_start;
Benjamin Herrenschmidt40dfef62011-11-29 18:22:56 +00003021
3022 /* Don't print anything after quiesce under OPAL, it crashes OFW */
Anton Blanchard5827d412012-11-26 17:40:03 +00003023 if (of_platform != PLATFORM_OPAL) {
Benjamin Herrenschmidt40dfef62011-11-29 18:22:56 +00003024 prom_printf("returning from prom_init\n");
3025 prom_debug("->dt_header_start=0x%x\n", hdr);
3026 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003027
3028#ifdef CONFIG_PPC32
3029 reloc_got2(-offset);
Anton Blanchard5ac47f72012-11-26 17:39:03 +00003030#else
3031 unreloc_toc();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003032#endif
3033
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00003034#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
3035 /* OPAL early debug gets the OPAL base & entry in r8 and r9 */
3036 __start(hdr, kbase, 0, 0, 0,
Anton Blanchard5827d412012-11-26 17:40:03 +00003037 prom_opal_base, prom_opal_entry);
Benjamin Herrenschmidt6e35d5d2011-09-19 18:28:01 +00003038#else
3039 __start(hdr, kbase, 0, 0, 0, 0, 0);
3040#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10003041
3042 return 0;
3043}