blob: 9369287aa8c2a4518b4db304c869742d3c55a233 [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>
38#include <asm/system.h>
39#include <asm/mmu.h>
40#include <asm/pgtable.h>
41#include <asm/pci.h>
42#include <asm/iommu.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100043#include <asm/btext.h>
44#include <asm/sections.h>
45#include <asm/machdep.h>
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +000046#include <asm/opal.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100047
Paul Mackerras9b6b5632005-10-06 12:06:20 +100048#include <linux/linux_logo.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100049
50/*
51 * Properties whose value is longer than this get excluded from our
52 * copy of the device tree. This value does need to be big enough to
53 * ensure that we don't lose things like the interrupt-map property
54 * on a PCI-PCI bridge.
55 */
56#define MAX_PROPERTY_LENGTH (1UL * 1024 * 1024)
57
58/*
59 * Eventually bump that one up
60 */
61#define DEVTREE_CHUNK_SIZE 0x100000
62
63/*
64 * This is the size of the local memory reserve map that gets copied
65 * into the boot params passed to the kernel. That size is totally
66 * flexible as the kernel just reads the list until it encounters an
67 * entry with size 0, so it can be changed without breaking binary
68 * compatibility
69 */
70#define MEM_RESERVE_MAP_SIZE 8
71
72/*
73 * prom_init() is called very early on, before the kernel text
74 * and data have been mapped to KERNELBASE. At this point the code
75 * is running at whatever address it has been loaded at.
76 * On ppc32 we compile with -mrelocatable, which means that references
77 * to extern and static variables get relocated automatically.
78 * On ppc64 we have to relocate the references explicitly with
79 * RELOC. (Note that strings count as static variables.)
80 *
81 * Because OF may have mapped I/O devices into the area starting at
82 * KERNELBASE, particularly on CHRP machines, we can't safely call
83 * OF once the kernel has been mapped to KERNELBASE. Therefore all
84 * OF calls must be done within prom_init().
85 *
86 * ADDR is used in calls to call_prom. The 4th and following
87 * arguments to call_prom should be 32-bit values.
88 * On ppc64, 64 bit values are truncated to 32 bits (and
89 * fortunately don't get interpreted as two arguments).
90 */
91#ifdef CONFIG_PPC64
92#define RELOC(x) (*PTRRELOC(&(x)))
93#define ADDR(x) (u32) add_reloc_offset((unsigned long)(x))
Paul Mackerrasa23414b2005-11-10 12:00:55 +110094#define OF_WORKAROUNDS 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +100095#else
96#define RELOC(x) (x)
97#define ADDR(x) (u32) (x)
Paul Mackerrasa23414b2005-11-10 12:00:55 +110098#define OF_WORKAROUNDS of_workarounds
99int of_workarounds;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000100#endif
101
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100102#define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */
103#define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */
104
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000105#define PROM_BUG() do { \
106 prom_printf("kernel BUG at %s line 0x%x!\n", \
107 RELOC(__FILE__), __LINE__); \
108 __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
109} while (0)
110
111#ifdef DEBUG_PROM
112#define prom_debug(x...) prom_printf(x)
113#else
114#define prom_debug(x...)
115#endif
116
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000117
118typedef u32 prom_arg_t;
119
120struct prom_args {
121 u32 service;
122 u32 nargs;
123 u32 nret;
124 prom_arg_t args[10];
125};
126
127struct prom_t {
128 ihandle root;
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100129 phandle chosen;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000130 int cpu;
131 ihandle stdout;
Paul Mackerrasa575b802005-10-23 17:23:21 +1000132 ihandle mmumap;
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100133 ihandle memory;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000134};
135
136struct mem_map_entry {
Kumar Galacbbcf342006-01-11 17:57:13 -0600137 u64 base;
138 u64 size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000139};
140
141typedef u32 cell_t;
142
143extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
144
145#ifdef CONFIG_PPC64
Paul Mackerrasc49888202005-10-26 21:52:53 +1000146extern int enter_prom(struct prom_args *args, unsigned long entry);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000147#else
Paul Mackerrasc49888202005-10-26 21:52:53 +1000148static inline int enter_prom(struct prom_args *args, unsigned long entry)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000149{
Paul Mackerrasc49888202005-10-26 21:52:53 +1000150 return ((int (*)(struct prom_args *))entry)(args);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000151}
152#endif
153
154extern void copy_and_flush(unsigned long dest, unsigned long src,
155 unsigned long size, unsigned long offset);
156
157/* prom structure */
158static struct prom_t __initdata prom;
159
160static unsigned long prom_entry __initdata;
161
162#define PROM_SCRATCH_SIZE 256
163
164static char __initdata of_stdout_device[256];
165static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
166
167static unsigned long __initdata dt_header_start;
168static unsigned long __initdata dt_struct_start, dt_struct_end;
169static unsigned long __initdata dt_string_start, dt_string_end;
170
171static unsigned long __initdata prom_initrd_start, prom_initrd_end;
172
173#ifdef CONFIG_PPC64
Jeremy Kerr165785e2006-11-11 17:25:18 +1100174static int __initdata prom_iommu_force_on;
175static int __initdata prom_iommu_off;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000176static unsigned long __initdata prom_tce_alloc_start;
177static unsigned long __initdata prom_tce_alloc_end;
178#endif
179
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100180/* Platforms codes are now obsolete in the kernel. Now only used within this
181 * file and ultimately gone too. Feel free to change them if you need, they
182 * are not shared with anything outside of this file anymore
183 */
184#define PLATFORM_PSERIES 0x0100
185#define PLATFORM_PSERIES_LPAR 0x0101
186#define PLATFORM_LPAR 0x0001
187#define PLATFORM_POWERMAC 0x0400
188#define PLATFORM_GENERIC 0x0500
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +0000189#define PLATFORM_OPAL 0x0600
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100190
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000191static int __initdata of_platform;
192
193static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
194
Benjamin Krillcf687872009-07-27 22:02:39 +0000195static unsigned long __initdata prom_memory_limit;
196
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000197static unsigned long __initdata alloc_top;
198static unsigned long __initdata alloc_top_high;
199static unsigned long __initdata alloc_bottom;
200static unsigned long __initdata rmo_top;
201static unsigned long __initdata ram_top;
202
203static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
204static int __initdata mem_reserve_cnt;
205
206static cell_t __initdata regbuf[1024];
207
208
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000209/*
210 * Error results ... some OF calls will return "-1" on error, some
211 * will return 0, some will return either. To simplify, here are
212 * macros to use with any ihandle or phandle return value to check if
213 * it is valid
214 */
215
216#define PROM_ERROR (-1u)
217#define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
218#define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
219
220
221/* This is the one and *ONLY* place where we actually call open
222 * firmware.
223 */
224
225static int __init call_prom(const char *service, int nargs, int nret, ...)
226{
227 int i;
228 struct prom_args args;
229 va_list list;
230
231 args.service = ADDR(service);
232 args.nargs = nargs;
233 args.nret = nret;
234
235 va_start(list, nret);
236 for (i = 0; i < nargs; i++)
237 args.args[i] = va_arg(list, prom_arg_t);
238 va_end(list);
239
240 for (i = 0; i < nret; i++)
241 args.args[nargs+i] = 0;
242
Paul Mackerrasc49888202005-10-26 21:52:53 +1000243 if (enter_prom(&args, RELOC(prom_entry)) < 0)
244 return PROM_ERROR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000245
246 return (nret > 0) ? args.args[nargs] : 0;
247}
248
249static int __init call_prom_ret(const char *service, int nargs, int nret,
250 prom_arg_t *rets, ...)
251{
252 int i;
253 struct prom_args args;
254 va_list list;
255
256 args.service = ADDR(service);
257 args.nargs = nargs;
258 args.nret = nret;
259
260 va_start(list, rets);
261 for (i = 0; i < nargs; i++)
262 args.args[i] = va_arg(list, prom_arg_t);
263 va_end(list);
264
265 for (i = 0; i < nret; i++)
Olaf Heringed1189b2005-11-29 14:04:05 +0100266 args.args[nargs+i] = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000267
Paul Mackerrasc49888202005-10-26 21:52:53 +1000268 if (enter_prom(&args, RELOC(prom_entry)) < 0)
269 return PROM_ERROR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000270
271 if (rets != NULL)
272 for (i = 1; i < nret; ++i)
Paul Mackerrasc5200c92005-10-10 22:57:03 +1000273 rets[i-1] = args.args[nargs+i];
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000274
275 return (nret > 0) ? args.args[nargs] : 0;
276}
277
278
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000279static void __init prom_print(const char *msg)
280{
281 const char *p, *q;
282 struct prom_t *_prom = &RELOC(prom);
283
284 if (_prom->stdout == 0)
285 return;
286
287 for (p = msg; *p != 0; p = q) {
288 for (q = p; *q != 0 && *q != '\n'; ++q)
289 ;
290 if (q > p)
291 call_prom("write", 3, 1, _prom->stdout, p, q - p);
292 if (*q == 0)
293 break;
294 ++q;
295 call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
296 }
297}
298
299
300static void __init prom_print_hex(unsigned long val)
301{
302 int i, nibbles = sizeof(val)*2;
303 char buf[sizeof(val)*2+1];
304 struct prom_t *_prom = &RELOC(prom);
305
306 for (i = nibbles-1; i >= 0; i--) {
307 buf[i] = (val & 0xf) + '0';
308 if (buf[i] > '9')
309 buf[i] += ('a'-'0'-10);
310 val >>= 4;
311 }
312 buf[nibbles] = '\0';
313 call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
314}
315
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000316/* max number of decimal digits in an unsigned long */
317#define UL_DIGITS 21
318static void __init prom_print_dec(unsigned long val)
319{
320 int i, size;
321 char buf[UL_DIGITS+1];
322 struct prom_t *_prom = &RELOC(prom);
323
324 for (i = UL_DIGITS-1; i >= 0; i--) {
325 buf[i] = (val % 10) + '0';
326 val = val/10;
327 if (val == 0)
328 break;
329 }
330 /* shift stuff down */
331 size = UL_DIGITS - i;
332 call_prom("write", 3, 1, _prom->stdout, buf+i, size);
333}
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000334
335static void __init prom_printf(const char *format, ...)
336{
337 const char *p, *q, *s;
338 va_list args;
339 unsigned long v;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000340 long vs;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000341 struct prom_t *_prom = &RELOC(prom);
342
343 va_start(args, format);
344#ifdef CONFIG_PPC64
345 format = PTRRELOC(format);
346#endif
347 for (p = format; *p != 0; p = q) {
348 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
349 ;
350 if (q > p)
351 call_prom("write", 3, 1, _prom->stdout, p, q - p);
352 if (*q == 0)
353 break;
354 if (*q == '\n') {
355 ++q;
356 call_prom("write", 3, 1, _prom->stdout,
357 ADDR("\r\n"), 2);
358 continue;
359 }
360 ++q;
361 if (*q == 0)
362 break;
363 switch (*q) {
364 case 's':
365 ++q;
366 s = va_arg(args, const char *);
367 prom_print(s);
368 break;
369 case 'x':
370 ++q;
371 v = va_arg(args, unsigned long);
372 prom_print_hex(v);
373 break;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000374 case 'd':
375 ++q;
376 vs = va_arg(args, int);
377 if (vs < 0) {
378 prom_print(RELOC("-"));
379 vs = -vs;
380 }
381 prom_print_dec(vs);
382 break;
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000383 case 'l':
384 ++q;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000385 if (*q == 0)
386 break;
387 else if (*q == 'x') {
388 ++q;
389 v = va_arg(args, unsigned long);
390 prom_print_hex(v);
391 } else if (*q == 'u') { /* '%lu' */
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000392 ++q;
393 v = va_arg(args, unsigned long);
394 prom_print_dec(v);
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000395 } else if (*q == 'd') { /* %ld */
396 ++q;
397 vs = va_arg(args, long);
398 if (vs < 0) {
399 prom_print(RELOC("-"));
400 vs = -vs;
401 }
402 prom_print_dec(vs);
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000403 }
404 break;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000405 }
406 }
407}
408
409
Paul Mackerrasa575b802005-10-23 17:23:21 +1000410static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
411 unsigned long align)
412{
Paul Mackerrasa575b802005-10-23 17:23:21 +1000413 struct prom_t *_prom = &RELOC(prom);
414
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100415 if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
416 /*
417 * Old OF requires we claim physical and virtual separately
418 * and then map explicitly (assuming virtual mode)
419 */
420 int ret;
421 prom_arg_t result;
422
423 ret = call_prom_ret("call-method", 5, 2, &result,
424 ADDR("claim"), _prom->memory,
425 align, size, virt);
426 if (ret != 0 || result == -1)
427 return -1;
428 ret = call_prom_ret("call-method", 5, 2, &result,
429 ADDR("claim"), _prom->mmumap,
430 align, size, virt);
431 if (ret != 0) {
432 call_prom("call-method", 4, 1, ADDR("release"),
433 _prom->memory, size, virt);
434 return -1;
435 }
436 /* the 0x12 is M (coherence) + PP == read/write */
Paul Mackerrasa575b802005-10-23 17:23:21 +1000437 call_prom("call-method", 6, 1,
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100438 ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
439 return virt;
440 }
441 return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
442 (prom_arg_t)align);
Paul Mackerrasa575b802005-10-23 17:23:21 +1000443}
444
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000445static void __init __attribute__((noreturn)) prom_panic(const char *reason)
446{
447#ifdef CONFIG_PPC64
448 reason = PTRRELOC(reason);
449#endif
450 prom_print(reason);
Olaf Heringadd60ef2006-03-23 22:03:57 +0100451 /* Do not call exit because it clears the screen on pmac
452 * it also causes some sort of double-fault on early pmacs */
453 if (RELOC(of_platform) == PLATFORM_POWERMAC)
454 asm("trap\n");
455
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000456 /* ToDo: should put up an SRC here on p/iSeries */
457 call_prom("exit", 0, 0);
458
459 for (;;) /* should never get here */
460 ;
461}
462
463
464static int __init prom_next_node(phandle *nodep)
465{
466 phandle node;
467
468 if ((node = *nodep) != 0
469 && (*nodep = call_prom("child", 1, 1, node)) != 0)
470 return 1;
471 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
472 return 1;
473 for (;;) {
474 if ((node = call_prom("parent", 1, 1, node)) == 0)
475 return 0;
476 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
477 return 1;
478 }
479}
480
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +1100481static int inline prom_getprop(phandle node, const char *pname,
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000482 void *value, size_t valuelen)
483{
484 return call_prom("getprop", 4, 1, node, ADDR(pname),
485 (u32)(unsigned long) value, (u32) valuelen);
486}
487
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +1100488static int inline prom_getproplen(phandle node, const char *pname)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000489{
490 return call_prom("getproplen", 2, 1, node, ADDR(pname));
491}
492
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100493static void add_string(char **str, const char *q)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000494{
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100495 char *p = *str;
496
497 while (*q)
498 *p++ = *q++;
499 *p++ = ' ';
500 *str = p;
501}
502
503static char *tohex(unsigned int x)
504{
505 static char digits[] = "0123456789abcdef";
506 static char result[9];
507 int i;
508
509 result[8] = 0;
510 i = 8;
511 do {
512 --i;
513 result[i] = digits[x & 0xf];
514 x >>= 4;
515 } while (x != 0 && i > 0);
516 return &result[i];
517}
518
519static int __init prom_setprop(phandle node, const char *nodename,
520 const char *pname, void *value, size_t valuelen)
521{
522 char cmd[256], *p;
523
524 if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
525 return call_prom("setprop", 4, 1, node, ADDR(pname),
526 (u32)(unsigned long) value, (u32) valuelen);
527
528 /* gah... setprop doesn't work on longtrail, have to use interpret */
529 p = cmd;
530 add_string(&p, "dev");
531 add_string(&p, nodename);
532 add_string(&p, tohex((u32)(unsigned long) value));
533 add_string(&p, tohex(valuelen));
534 add_string(&p, tohex(ADDR(pname)));
535 add_string(&p, tohex(strlen(RELOC(pname))));
536 add_string(&p, "property");
537 *p = 0;
538 return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000539}
540
Benjamin Krillcf687872009-07-27 22:02:39 +0000541/* We can't use the standard versions because of RELOC headaches. */
542#define isxdigit(c) (('0' <= (c) && (c) <= '9') \
543 || ('a' <= (c) && (c) <= 'f') \
544 || ('A' <= (c) && (c) <= 'F'))
545
546#define isdigit(c) ('0' <= (c) && (c) <= '9')
547#define islower(c) ('a' <= (c) && (c) <= 'z')
548#define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c))
549
550unsigned long prom_strtoul(const char *cp, const char **endp)
551{
552 unsigned long result = 0, base = 10, value;
553
554 if (*cp == '0') {
555 base = 8;
556 cp++;
557 if (toupper(*cp) == 'X') {
558 cp++;
559 base = 16;
560 }
561 }
562
563 while (isxdigit(*cp) &&
564 (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
565 result = result * base + value;
566 cp++;
567 }
568
569 if (endp)
570 *endp = cp;
571
572 return result;
573}
574
575unsigned long prom_memparse(const char *ptr, const char **retptr)
576{
577 unsigned long ret = prom_strtoul(ptr, retptr);
578 int shift = 0;
579
580 /*
581 * We can't use a switch here because GCC *may* generate a
582 * jump table which won't work, because we're not running at
583 * the address we're linked at.
584 */
585 if ('G' == **retptr || 'g' == **retptr)
586 shift = 30;
587
588 if ('M' == **retptr || 'm' == **retptr)
589 shift = 20;
590
591 if ('K' == **retptr || 'k' == **retptr)
592 shift = 10;
593
594 if (shift) {
595 ret <<= shift;
596 (*retptr)++;
597 }
598
599 return ret;
600}
601
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000602/*
603 * Early parsing of the command line passed to the kernel, used for
604 * "mem=x" and the options that affect the iommu
605 */
606static void __init early_cmdline_parse(void)
607{
608 struct prom_t *_prom = &RELOC(prom);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100609 const char *opt;
Benjamin Krillcf687872009-07-27 22:02:39 +0000610
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100611 char *p;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000612 int l = 0;
613
614 RELOC(prom_cmd_line[0]) = 0;
615 p = RELOC(prom_cmd_line);
616 if ((long)_prom->chosen > 0)
617 l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
618#ifdef CONFIG_CMDLINE
Amos Waterland0e4aa9c2006-06-12 23:45:02 -0400619 if (l <= 0 || p[0] == '\0') /* dbl check */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000620 strlcpy(RELOC(prom_cmd_line),
621 RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
622#endif /* CONFIG_CMDLINE */
623 prom_printf("command line: %s\n", RELOC(prom_cmd_line));
624
625#ifdef CONFIG_PPC64
626 opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
627 if (opt) {
628 prom_printf("iommu opt is: %s\n", opt);
629 opt += 6;
630 while (*opt && *opt == ' ')
631 opt++;
632 if (!strncmp(opt, RELOC("off"), 3))
Jeremy Kerr165785e2006-11-11 17:25:18 +1100633 RELOC(prom_iommu_off) = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000634 else if (!strncmp(opt, RELOC("force"), 5))
Jeremy Kerr165785e2006-11-11 17:25:18 +1100635 RELOC(prom_iommu_force_on) = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000636 }
637#endif
Benjamin Krillcf687872009-07-27 22:02:39 +0000638 opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
639 if (opt) {
640 opt += 4;
641 RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
642#ifdef CONFIG_PPC64
643 /* Align to 16 MB == size of ppc64 large page */
644 RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
645#endif
646 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000647}
648
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +0000649#if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000650/*
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000651 * There are two methods for telling firmware what our capabilities are.
652 * Newer machines have an "ibm,client-architecture-support" method on the
653 * root node. For older machines, we have to call the "process-elf-header"
654 * method in the /packages/elf-loader node, passing it a fake 32-bit
655 * ELF header containing a couple of PT_NOTE sections that contain
656 * structures that contain various information.
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000657 */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000658
659/*
660 * New method - extensible architecture description vector.
661 *
662 * Because the description vector contains a mix of byte and word
663 * values, we declare it as an unsigned char array, and use this
664 * macro to put word values in.
665 */
666#define W(x) ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
667 ((x) >> 8) & 0xff, (x) & 0xff
668
669/* Option vector bits - generic bits in byte 1 */
670#define OV_IGNORE 0x80 /* ignore this vector */
671#define OV_CESSATION_POLICY 0x40 /* halt if unsupported option present*/
672
673/* Option vector 1: processor architectures supported */
674#define OV1_PPC_2_00 0x80 /* set if we support PowerPC 2.00 */
675#define OV1_PPC_2_01 0x40 /* set if we support PowerPC 2.01 */
676#define OV1_PPC_2_02 0x20 /* set if we support PowerPC 2.02 */
677#define OV1_PPC_2_03 0x10 /* set if we support PowerPC 2.03 */
678#define OV1_PPC_2_04 0x08 /* set if we support PowerPC 2.04 */
679#define OV1_PPC_2_05 0x04 /* set if we support PowerPC 2.05 */
Joel Schopp0cb99012008-06-19 06:23:23 +1000680#define OV1_PPC_2_06 0x02 /* set if we support PowerPC 2.06 */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000681
682/* Option vector 2: Open Firmware options supported */
683#define OV2_REAL_MODE 0x20 /* set if we want OF in real mode */
684
685/* Option vector 3: processor options supported */
686#define OV3_FP 0x80 /* floating point */
687#define OV3_VMX 0x40 /* VMX/Altivec */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100688#define OV3_DFP 0x20 /* decimal FP */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000689
690/* Option vector 5: PAPR/OF options supported */
691#define OV5_LPAR 0x80 /* logical partitioning supported */
692#define OV5_SPLPAR 0x40 /* shared-processor LPAR supported */
693/* ibm,dynamic-reconfiguration-memory property supported */
694#define OV5_DRCONF_MEMORY 0x20
695#define OV5_LARGE_PAGES 0x10 /* large pages supported */
Jake Moilanend8c391a2007-06-08 07:27:11 +1000696#define OV5_DONATE_DEDICATE_CPU 0x02 /* donate dedicated CPU support */
Michael Ellerman014dad92007-05-08 12:58:35 +1000697/* PCIe/MSI support. Without MSI full PCIe is not supported */
698#ifdef CONFIG_PCI_MSI
699#define OV5_MSI 0x01 /* PCIe/MSI support */
700#else
701#define OV5_MSI 0x00
702#endif /* CONFIG_PCI_MSI */
Nathan Fontenot8391e422008-07-24 04:36:38 +1000703#ifdef CONFIG_PPC_SMLPAR
704#define OV5_CMO 0x80 /* Cooperative Memory Overcommitment */
Brian King9ee820f2011-05-04 16:01:20 +1000705#define OV5_XCMO 0x40 /* Page Coalescing */
Nathan Fontenot8391e422008-07-24 04:36:38 +1000706#else
707#define OV5_CMO 0x00
Brian King9ee820f2011-05-04 16:01:20 +1000708#define OV5_XCMO 0x00
Nathan Fontenot8391e422008-07-24 04:36:38 +1000709#endif
Anton Blanchard4b83c332010-04-07 15:33:44 +0000710#define OV5_TYPE1_AFFINITY 0x80 /* Type 1 NUMA affinity */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000711
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000712/* Option Vector 6: IBM PAPR hints */
713#define OV6_LINUX 0x02 /* Linux is our OS */
714
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000715/*
716 * The architecture vector has an array of PVR mask/value pairs,
717 * followed by # option vectors - 1, followed by the option vectors.
718 */
719static unsigned char ibm_architecture_vec[] = {
720 W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */
Anton Blanchard03054d52006-04-29 09:51:06 +1000721 W(0xffff0000), W(0x003e0000), /* POWER6 */
Michael Neulinge952e6c2008-06-18 10:47:26 +1000722 W(0xffff0000), W(0x003f0000), /* POWER7 */
Joel Schopp0cb99012008-06-19 06:23:23 +1000723 W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */
Paul Mackerras0efbc182006-11-29 22:31:47 +1100724 W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000725 W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000726 6 - 1, /* 6 option vectors */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000727
728 /* option vector 1: processor architectures supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500729 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000730 0, /* don't ignore, don't halt */
731 OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
Joel Schopp0cb99012008-06-19 06:23:23 +1000732 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000733
734 /* option vector 2: Open Firmware options supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500735 34 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000736 OV2_REAL_MODE,
737 0, 0,
738 W(0xffffffff), /* real_base */
739 W(0xffffffff), /* real_size */
740 W(0xffffffff), /* virt_base */
741 W(0xffffffff), /* virt_size */
742 W(0xffffffff), /* load_base */
Anton Blanchard856cc2f2009-03-31 20:14:01 +0000743 W(64), /* 64MB min RMA */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000744 W(0xffffffff), /* full client load */
745 0, /* min RMA percentage of total RAM */
746 48, /* max log_2(hash table size) */
747
748 /* option vector 3: processor options supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500749 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000750 0, /* don't ignore, don't halt */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100751 OV3_FP | OV3_VMX | OV3_DFP,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000752
753 /* option vector 4: IBM PAPR implementation */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500754 2 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000755 0, /* don't halt */
756
757 /* option vector 5: PAPR/OF options */
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000758 13 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000759 0, /* don't ignore, don't halt */
Jake Moilanend8c391a2007-06-08 07:27:11 +1000760 OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
761 OV5_DONATE_DEDICATE_CPU | OV5_MSI,
Nathan Fontenot8391e422008-07-24 04:36:38 +1000762 0,
Brian King9ee820f2011-05-04 16:01:20 +1000763 OV5_CMO | OV5_XCMO,
Anton Blanchard4b83c332010-04-07 15:33:44 +0000764 OV5_TYPE1_AFFINITY,
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000765 0,
766 0,
767 0,
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100768 /* WARNING: The offset of the "number of cores" field below
769 * must match by the macro below. Update the definition if
770 * the structure layout changes.
771 */
772#define IBM_ARCH_VEC_NRCORES_OFFSET 100
773 W(NR_CPUS), /* number of cores supported */
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000774
775 /* option vector 6: IBM PAPR hints */
776 4 - 2, /* length */
777 0,
778 0,
779 OV6_LINUX,
780
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000781};
782
783/* Old method - ELF header with PT_NOTE sections */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000784static struct fake_elf {
785 Elf32_Ehdr elfhdr;
786 Elf32_Phdr phdr[2];
787 struct chrpnote {
788 u32 namesz;
789 u32 descsz;
790 u32 type;
791 char name[8]; /* "PowerPC" */
792 struct chrpdesc {
793 u32 real_mode;
794 u32 real_base;
795 u32 real_size;
796 u32 virt_base;
797 u32 virt_size;
798 u32 load_base;
799 } chrpdesc;
800 } chrpnote;
801 struct rpanote {
802 u32 namesz;
803 u32 descsz;
804 u32 type;
805 char name[24]; /* "IBM,RPA-Client-Config" */
806 struct rpadesc {
807 u32 lpar_affinity;
808 u32 min_rmo_size;
809 u32 min_rmo_percent;
810 u32 max_pft_size;
811 u32 splpar;
812 u32 min_load;
813 u32 new_mem_def;
814 u32 ignore_me;
815 } rpadesc;
816 } rpanote;
Paul Mackerras5663a122008-10-31 22:27:17 +1100817} fake_elf = {
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000818 .elfhdr = {
819 .e_ident = { 0x7f, 'E', 'L', 'F',
820 ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
821 .e_type = ET_EXEC, /* yeah right */
822 .e_machine = EM_PPC,
823 .e_version = EV_CURRENT,
824 .e_phoff = offsetof(struct fake_elf, phdr),
825 .e_phentsize = sizeof(Elf32_Phdr),
826 .e_phnum = 2
827 },
828 .phdr = {
829 [0] = {
830 .p_type = PT_NOTE,
831 .p_offset = offsetof(struct fake_elf, chrpnote),
832 .p_filesz = sizeof(struct chrpnote)
833 }, [1] = {
834 .p_type = PT_NOTE,
835 .p_offset = offsetof(struct fake_elf, rpanote),
836 .p_filesz = sizeof(struct rpanote)
837 }
838 },
839 .chrpnote = {
840 .namesz = sizeof("PowerPC"),
841 .descsz = sizeof(struct chrpdesc),
842 .type = 0x1275,
843 .name = "PowerPC",
844 .chrpdesc = {
845 .real_mode = ~0U, /* ~0 means "don't care" */
846 .real_base = ~0U,
847 .real_size = ~0U,
848 .virt_base = ~0U,
849 .virt_size = ~0U,
850 .load_base = ~0U
851 },
852 },
853 .rpanote = {
854 .namesz = sizeof("IBM,RPA-Client-Config"),
855 .descsz = sizeof(struct rpadesc),
856 .type = 0x12759999,
857 .name = "IBM,RPA-Client-Config",
858 .rpadesc = {
Paul Mackerras5663a122008-10-31 22:27:17 +1100859 .lpar_affinity = 0,
860 .min_rmo_size = 64, /* in megabytes */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000861 .min_rmo_percent = 0,
Paul Mackerras5663a122008-10-31 22:27:17 +1100862 .max_pft_size = 48, /* 2^48 bytes max PFT size */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000863 .splpar = 1,
864 .min_load = ~0U,
Paul Mackerras5663a122008-10-31 22:27:17 +1100865 .new_mem_def = 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000866 }
867 }
868};
869
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100870static int __init prom_count_smt_threads(void)
871{
872 phandle node;
873 char type[64];
874 unsigned int plen;
875
876 /* Pick up th first CPU node we can find */
877 for (node = 0; prom_next_node(&node); ) {
878 type[0] = 0;
879 prom_getprop(node, "device_type", type, sizeof(type));
880
881 if (strcmp(type, RELOC("cpu")))
882 continue;
883 /*
884 * There is an entry for each smt thread, each entry being
885 * 4 bytes long. All cpus should have the same number of
886 * smt threads, so return after finding the first.
887 */
888 plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
889 if (plen == PROM_ERROR)
890 break;
891 plen >>= 2;
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000892 prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100893
894 /* Sanity check */
895 if (plen < 1 || plen > 64) {
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000896 prom_printf("Threads per core %lu out of bounds, assuming 1\n",
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100897 (unsigned long)plen);
898 return 1;
899 }
900 return plen;
901 }
902 prom_debug("No threads found, assuming 1 per core\n");
903
904 return 1;
905
906}
907
908
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000909static void __init prom_send_capabilities(void)
910{
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000911 ihandle elfloader, root;
912 prom_arg_t ret;
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100913 u32 *cores;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000914
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000915 root = call_prom("open", 1, 1, ADDR("/"));
916 if (root != 0) {
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100917 /* We need to tell the FW about the number of cores we support.
918 *
919 * To do that, we count the number of threads on the first core
920 * (we assume this is the same for all cores) and use it to
921 * divide NR_CPUS.
922 */
923 cores = (u32 *)PTRRELOC(&ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET]);
924 if (*cores != NR_CPUS) {
925 prom_printf("WARNING ! "
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000926 "ibm_architecture_vec structure inconsistent: %lu!\n",
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100927 *cores);
928 } else {
Anton Blanchard33ad5e42010-06-17 14:33:06 +0000929 *cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000930 prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
931 *cores, NR_CPUS);
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100932 }
933
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000934 /* try calling the ibm,client-architecture-support method */
Anton Blanchard049d0492009-09-21 20:47:39 +0000935 prom_printf("Calling ibm,client-architecture-support...");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000936 if (call_prom_ret("call-method", 3, 2, &ret,
937 ADDR("ibm,client-architecture-support"),
Benjamin Herrenschmidt33b74972006-06-07 12:01:32 +1000938 root,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000939 ADDR(ibm_architecture_vec)) == 0) {
940 /* the call exists... */
941 if (ret)
Anton Blanchard4da727a2009-03-31 20:06:14 +0000942 prom_printf("\nWARNING: ibm,client-architecture"
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000943 "-support call FAILED!\n");
944 call_prom("close", 1, 0, root);
Anton Blanchard4da727a2009-03-31 20:06:14 +0000945 prom_printf(" done\n");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000946 return;
947 }
948 call_prom("close", 1, 0, root);
Anton Blanchard049d0492009-09-21 20:47:39 +0000949 prom_printf(" not implemented\n");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000950 }
951
952 /* no ibm,client-architecture-support call, try the old way */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000953 elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
954 if (elfloader == 0) {
955 prom_printf("couldn't open /packages/elf-loader\n");
956 return;
957 }
958 call_prom("call-method", 3, 1, ADDR("process-elf-header"),
959 elfloader, ADDR(&fake_elf));
960 call_prom("close", 1, 0, elfloader);
961}
962#endif
963
964/*
965 * Memory allocation strategy... our layout is normally:
966 *
967 * at 14Mb or more we have vmlinux, then a gap and initrd. In some
968 * rare cases, initrd might end up being before the kernel though.
969 * We assume this won't override the final kernel at 0, we have no
970 * provision to handle that in this version, but it should hopefully
971 * never happen.
972 *
973 * alloc_top is set to the top of RMO, eventually shrink down if the
974 * TCEs overlap
975 *
976 * alloc_bottom is set to the top of kernel/initrd
977 *
978 * from there, allocations are done this way : rtas is allocated
979 * topmost, and the device-tree is allocated from the bottom. We try
980 * to grow the device-tree allocation as we progress. If we can't,
981 * then we fail, we don't currently have a facility to restart
982 * elsewhere, but that shouldn't be necessary.
983 *
984 * Note that calls to reserve_mem have to be done explicitly, memory
985 * allocated with either alloc_up or alloc_down isn't automatically
986 * reserved.
987 */
988
989
990/*
991 * Allocates memory in the RMO upward from the kernel/initrd
992 *
993 * When align is 0, this is a special case, it means to allocate in place
994 * at the current location of alloc_bottom or fail (that is basically
995 * extending the previous allocation). Used for the device-tree flattening
996 */
997static unsigned long __init alloc_up(unsigned long size, unsigned long align)
998{
Paul Mackerrasc49888202005-10-26 21:52:53 +1000999 unsigned long base = RELOC(alloc_bottom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001000 unsigned long addr = 0;
1001
Paul Mackerrasc49888202005-10-26 21:52:53 +10001002 if (align)
1003 base = _ALIGN_UP(base, align);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001004 prom_debug("alloc_up(%x, %x)\n", size, align);
1005 if (RELOC(ram_top) == 0)
1006 prom_panic("alloc_up() called with mem not initialized\n");
1007
1008 if (align)
1009 base = _ALIGN_UP(RELOC(alloc_bottom), align);
1010 else
1011 base = RELOC(alloc_bottom);
1012
1013 for(; (base + size) <= RELOC(alloc_top);
1014 base = _ALIGN_UP(base + 0x100000, align)) {
1015 prom_debug(" trying: 0x%x\n\r", base);
1016 addr = (unsigned long)prom_claim(base, size, 0);
Paul Mackerrasc49888202005-10-26 21:52:53 +10001017 if (addr != PROM_ERROR && addr != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001018 break;
1019 addr = 0;
1020 if (align == 0)
1021 break;
1022 }
1023 if (addr == 0)
1024 return 0;
Anton Blanchard966728d2011-07-25 20:47:07 +00001025 RELOC(alloc_bottom) = addr + size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001026
1027 prom_debug(" -> %x\n", addr);
1028 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1029 prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
1030 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1031 prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
1032 prom_debug(" ram_top : %x\n", RELOC(ram_top));
1033
1034 return addr;
1035}
1036
1037/*
1038 * Allocates memory downward, either from top of RMO, or if highmem
1039 * is set, from the top of RAM. Note that this one doesn't handle
1040 * failures. It does claim memory if highmem is not set.
1041 */
1042static unsigned long __init alloc_down(unsigned long size, unsigned long align,
1043 int highmem)
1044{
1045 unsigned long base, addr = 0;
1046
1047 prom_debug("alloc_down(%x, %x, %s)\n", size, align,
1048 highmem ? RELOC("(high)") : RELOC("(low)"));
1049 if (RELOC(ram_top) == 0)
1050 prom_panic("alloc_down() called with mem not initialized\n");
1051
1052 if (highmem) {
1053 /* Carve out storage for the TCE table. */
1054 addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
1055 if (addr <= RELOC(alloc_bottom))
1056 return 0;
1057 /* Will we bump into the RMO ? If yes, check out that we
1058 * didn't overlap existing allocations there, if we did,
1059 * we are dead, we must be the first in town !
1060 */
1061 if (addr < RELOC(rmo_top)) {
1062 /* Good, we are first */
1063 if (RELOC(alloc_top) == RELOC(rmo_top))
1064 RELOC(alloc_top) = RELOC(rmo_top) = addr;
1065 else
1066 return 0;
1067 }
1068 RELOC(alloc_top_high) = addr;
1069 goto bail;
1070 }
1071
1072 base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
1073 for (; base > RELOC(alloc_bottom);
1074 base = _ALIGN_DOWN(base - 0x100000, align)) {
1075 prom_debug(" trying: 0x%x\n\r", base);
1076 addr = (unsigned long)prom_claim(base, size, 0);
Paul Mackerrasc49888202005-10-26 21:52:53 +10001077 if (addr != PROM_ERROR && addr != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001078 break;
1079 addr = 0;
1080 }
1081 if (addr == 0)
1082 return 0;
1083 RELOC(alloc_top) = addr;
1084
1085 bail:
1086 prom_debug(" -> %x\n", addr);
1087 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1088 prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
1089 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1090 prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
1091 prom_debug(" ram_top : %x\n", RELOC(ram_top));
1092
1093 return addr;
1094}
1095
1096/*
1097 * Parse a "reg" cell
1098 */
1099static unsigned long __init prom_next_cell(int s, cell_t **cellp)
1100{
1101 cell_t *p = *cellp;
1102 unsigned long r = 0;
1103
1104 /* Ignore more than 2 cells */
1105 while (s > sizeof(unsigned long) / 4) {
1106 p++;
1107 s--;
1108 }
1109 r = *p++;
1110#ifdef CONFIG_PPC64
Paul Mackerras35499c02005-10-22 16:02:39 +10001111 if (s > 1) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001112 r <<= 32;
1113 r |= *(p++);
1114 }
1115#endif
1116 *cellp = p;
1117 return r;
1118}
1119
1120/*
1121 * Very dumb function for adding to the memory reserve list, but
1122 * we don't need anything smarter at this point
1123 *
1124 * XXX Eventually check for collisions. They should NEVER happen.
1125 * If problems seem to show up, it would be a good start to track
1126 * them down.
1127 */
Michael Ellerman0108d3f2007-05-07 15:58:28 +10001128static void __init reserve_mem(u64 base, u64 size)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001129{
Kumar Galacbbcf342006-01-11 17:57:13 -06001130 u64 top = base + size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001131 unsigned long cnt = RELOC(mem_reserve_cnt);
1132
1133 if (size == 0)
1134 return;
1135
1136 /* We need to always keep one empty entry so that we
1137 * have our terminator with "size" set to 0 since we are
1138 * dumb and just copy this entire array to the boot params
1139 */
1140 base = _ALIGN_DOWN(base, PAGE_SIZE);
1141 top = _ALIGN_UP(top, PAGE_SIZE);
1142 size = top - base;
1143
1144 if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
1145 prom_panic("Memory reserve map exhausted !\n");
1146 RELOC(mem_reserve_map)[cnt].base = base;
1147 RELOC(mem_reserve_map)[cnt].size = size;
1148 RELOC(mem_reserve_cnt) = cnt + 1;
1149}
1150
1151/*
Adrian Bunkb3c2ffd2006-06-30 18:20:44 +02001152 * Initialize memory allocation mechanism, parse "memory" nodes and
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001153 * obtain that way the top of memory and RMO to setup out local allocator
1154 */
1155static void __init prom_init_mem(void)
1156{
1157 phandle node;
1158 char *path, type[64];
1159 unsigned int plen;
1160 cell_t *p, *endp;
1161 struct prom_t *_prom = &RELOC(prom);
1162 u32 rac, rsc;
1163
1164 /*
1165 * We iterate the memory nodes to find
1166 * 1) top of RMO (first node)
1167 * 2) top of memory
1168 */
1169 rac = 2;
1170 prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
1171 rsc = 1;
1172 prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
1173 prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
1174 prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
1175
1176 prom_debug("scanning memory:\n");
1177 path = RELOC(prom_scratch);
1178
1179 for (node = 0; prom_next_node(&node); ) {
1180 type[0] = 0;
1181 prom_getprop(node, "device_type", type, sizeof(type));
1182
Paul Mackerrasc49888202005-10-26 21:52:53 +10001183 if (type[0] == 0) {
1184 /*
1185 * CHRP Longtrail machines have no device_type
1186 * on the memory node, so check the name instead...
1187 */
1188 prom_getprop(node, "name", type, sizeof(type));
1189 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001190 if (strcmp(type, RELOC("memory")))
1191 continue;
Paul Mackerrasc49888202005-10-26 21:52:53 +10001192
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001193 plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
1194 if (plen > sizeof(regbuf)) {
1195 prom_printf("memory node too large for buffer !\n");
1196 plen = sizeof(regbuf);
1197 }
1198 p = RELOC(regbuf);
1199 endp = p + (plen / sizeof(cell_t));
1200
1201#ifdef DEBUG_PROM
1202 memset(path, 0, PROM_SCRATCH_SIZE);
1203 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1204 prom_debug(" node %s :\n", path);
1205#endif /* DEBUG_PROM */
1206
1207 while ((endp - p) >= (rac + rsc)) {
1208 unsigned long base, size;
1209
1210 base = prom_next_cell(rac, &p);
1211 size = prom_next_cell(rsc, &p);
1212
1213 if (size == 0)
1214 continue;
1215 prom_debug(" %x %x\n", base, size);
Benjamin Herrenschmidtab1b55e2006-03-03 10:35:40 +11001216 if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001217 RELOC(rmo_top) = size;
1218 if ((base + size) > RELOC(ram_top))
1219 RELOC(ram_top) = base + size;
1220 }
1221 }
1222
1223 RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
1224
1225 /* Check if we have an initrd after the kernel, if we do move our bottom
1226 * point to after it
1227 */
1228 if (RELOC(prom_initrd_start)) {
1229 if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
1230 RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
1231 }
1232
1233 /*
Benjamin Krillcf687872009-07-27 22:02:39 +00001234 * If prom_memory_limit is set we reduce the upper limits *except* for
1235 * alloc_top_high. This must be the real top of RAM so we can put
1236 * TCE's up there.
1237 */
1238
1239 RELOC(alloc_top_high) = RELOC(ram_top);
1240
1241 if (RELOC(prom_memory_limit)) {
1242 if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
1243 prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
1244 RELOC(prom_memory_limit));
1245 RELOC(prom_memory_limit) = 0;
1246 } else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
1247 prom_printf("Ignoring mem=%x >= ram_top.\n",
1248 RELOC(prom_memory_limit));
1249 RELOC(prom_memory_limit) = 0;
1250 } else {
1251 RELOC(ram_top) = RELOC(prom_memory_limit);
1252 RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
1253 }
1254 }
1255
1256 /*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001257 * Setup our top alloc point, that is top of RMO or top of
1258 * segment 0 when running non-LPAR.
1259 * Some RS64 machines have buggy firmware where claims up at
1260 * 1GB fail. Cap at 768MB as a workaround.
1261 * Since 768MB is plenty of room, and we need to cap to something
1262 * reasonable on 32-bit, cap at 768MB on all machines.
1263 */
1264 if (!RELOC(rmo_top))
1265 RELOC(rmo_top) = RELOC(ram_top);
1266 RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
1267 RELOC(alloc_top) = RELOC(rmo_top);
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001268 RELOC(alloc_top_high) = RELOC(ram_top);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001269
1270 prom_printf("memory layout at init:\n");
Benjamin Krillcf687872009-07-27 22:02:39 +00001271 prom_printf(" memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001272 prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1273 prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
1274 prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1275 prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
1276 prom_printf(" ram_top : %x\n", RELOC(ram_top));
1277}
1278
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001279static void __init prom_close_stdin(void)
1280{
1281 struct prom_t *_prom = &RELOC(prom);
1282 ihandle val;
1283
1284 if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1285 call_prom("close", 1, 0, val);
1286}
1287
1288#ifdef CONFIG_PPC_POWERNV
1289
1290static u64 __initdata prom_opal_size;
1291static u64 __initdata prom_opal_align;
1292static int __initdata prom_rtas_start_cpu;
1293static u64 __initdata prom_rtas_data;
1294static u64 __initdata prom_rtas_entry;
1295
1296/* XXX Don't change this structure without updating opal-takeover.S */
1297static struct opal_secondary_data {
1298 s64 ack; /* 0 */
1299 u64 go; /* 8 */
1300 struct opal_takeover_args args; /* 16 */
1301} opal_secondary_data;
1302
1303extern char opal_secondary_entry;
1304
1305static void prom_query_opal(void)
1306{
1307 long rc;
1308
1309 prom_printf("Querying for OPAL presence... ");
1310 rc = opal_query_takeover(&RELOC(prom_opal_size),
1311 &RELOC(prom_opal_align));
1312 prom_debug("(rc = %ld) ", rc);
1313 if (rc != 0) {
1314 prom_printf("not there.\n");
1315 return;
1316 }
1317 RELOC(of_platform) = PLATFORM_OPAL;
1318 prom_printf(" there !\n");
1319 prom_debug(" opal_size = 0x%lx\n", RELOC(prom_opal_size));
1320 prom_debug(" opal_align = 0x%lx\n", RELOC(prom_opal_align));
1321 if (RELOC(prom_opal_align) < 0x10000)
1322 RELOC(prom_opal_align) = 0x10000;
1323}
1324
1325static int prom_rtas_call(int token, int nargs, int nret, int *outputs, ...)
1326{
1327 struct rtas_args rtas_args;
1328 va_list list;
1329 int i;
1330
1331 rtas_args.token = token;
1332 rtas_args.nargs = nargs;
1333 rtas_args.nret = nret;
1334 rtas_args.rets = (rtas_arg_t *)&(rtas_args.args[nargs]);
1335 va_start(list, outputs);
1336 for (i = 0; i < nargs; ++i)
1337 rtas_args.args[i] = va_arg(list, rtas_arg_t);
1338 va_end(list);
1339
1340 for (i = 0; i < nret; ++i)
1341 rtas_args.rets[i] = 0;
1342
1343 opal_enter_rtas(&rtas_args, RELOC(prom_rtas_data),
1344 RELOC(prom_rtas_entry));
1345
1346 if (nret > 1 && outputs != NULL)
1347 for (i = 0; i < nret-1; ++i)
1348 outputs[i] = rtas_args.rets[i+1];
1349 return (nret > 0)? rtas_args.rets[0]: 0;
1350}
1351
1352static void __init prom_opal_hold_cpus(void)
1353{
1354 int i, cnt, cpu, rc;
1355 long j;
1356 phandle node;
1357 char type[64];
1358 u32 servers[8];
1359 struct prom_t *_prom = &RELOC(prom);
1360 void *entry = (unsigned long *)&RELOC(opal_secondary_entry);
1361 struct opal_secondary_data *data = &RELOC(opal_secondary_data);
1362
1363 prom_debug("prom_opal_hold_cpus: start...\n");
1364 prom_debug(" - entry = 0x%x\n", entry);
1365 prom_debug(" - data = 0x%x\n", data);
1366
1367 data->ack = -1;
1368 data->go = 0;
1369
1370 /* look for cpus */
1371 for (node = 0; prom_next_node(&node); ) {
1372 type[0] = 0;
1373 prom_getprop(node, "device_type", type, sizeof(type));
1374 if (strcmp(type, RELOC("cpu")) != 0)
1375 continue;
1376
1377 /* Skip non-configured cpus. */
1378 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1379 if (strcmp(type, RELOC("okay")) != 0)
1380 continue;
1381
1382 cnt = prom_getprop(node, "ibm,ppc-interrupt-server#s", servers,
1383 sizeof(servers));
1384 if (cnt == PROM_ERROR)
1385 break;
1386 cnt >>= 2;
1387 for (i = 0; i < cnt; i++) {
1388 cpu = servers[i];
1389 prom_debug("CPU %d ... ", cpu);
1390 if (cpu == _prom->cpu) {
1391 prom_debug("booted !\n");
1392 continue;
1393 }
1394 prom_debug("starting ... ");
1395
1396 /* Init the acknowledge var which will be reset by
1397 * the secondary cpu when it awakens from its OF
1398 * spinloop.
1399 */
1400 data->ack = -1;
1401 rc = prom_rtas_call(RELOC(prom_rtas_start_cpu), 3, 1,
1402 NULL, cpu, entry, data);
1403 prom_debug("rtas rc=%d ...", rc);
1404
1405 for (j = 0; j < 100000000 && data->ack == -1; j++) {
1406 HMT_low();
1407 mb();
1408 }
1409 HMT_medium();
1410 if (data->ack != -1)
1411 prom_debug("done, PIR=0x%x\n", data->ack);
1412 else
1413 prom_debug("timeout !\n");
1414 }
1415 }
1416 prom_debug("prom_opal_hold_cpus: end...\n");
1417}
1418
1419static void prom_opal_takeover(void)
1420{
1421 struct opal_secondary_data *data = &RELOC(opal_secondary_data);
1422 struct opal_takeover_args *args = &data->args;
1423 u64 align = RELOC(prom_opal_align);
1424 u64 top_addr, opal_addr;
1425
1426 args->k_image = (u64)RELOC(_stext);
1427 args->k_size = _end - _stext;
1428 args->k_entry = 0;
1429 args->k_entry2 = 0x60;
1430
1431 top_addr = _ALIGN_UP(args->k_size, align);
1432
1433 if (RELOC(prom_initrd_start) != 0) {
1434 args->rd_image = RELOC(prom_initrd_start);
1435 args->rd_size = RELOC(prom_initrd_end) - args->rd_image;
1436 args->rd_loc = top_addr;
1437 top_addr = _ALIGN_UP(args->rd_loc + args->rd_size, align);
1438 }
1439
1440 /* Pickup an address for the HAL. We want to go really high
1441 * up to avoid problem with future kexecs. On the other hand
1442 * we don't want to be all over the TCEs on P5IOC2 machines
1443 * which are going to be up there too. We assume the machine
1444 * has plenty of memory, and we ask for the HAL for now to
1445 * be just below the 1G point, or above the initrd
1446 */
1447 opal_addr = _ALIGN_DOWN(0x40000000 - RELOC(prom_opal_size), align);
1448 if (opal_addr < top_addr)
1449 opal_addr = top_addr;
1450 args->hal_addr = opal_addr;
1451
1452 prom_debug(" k_image = 0x%lx\n", args->k_image);
1453 prom_debug(" k_size = 0x%lx\n", args->k_size);
1454 prom_debug(" k_entry = 0x%lx\n", args->k_entry);
1455 prom_debug(" k_entry2 = 0x%lx\n", args->k_entry2);
1456 prom_debug(" hal_addr = 0x%lx\n", args->hal_addr);
1457 prom_debug(" rd_image = 0x%lx\n", args->rd_image);
1458 prom_debug(" rd_size = 0x%lx\n", args->rd_size);
1459 prom_debug(" rd_loc = 0x%lx\n", args->rd_loc);
1460 prom_printf("Performing OPAL takeover,this can take a few minutes..\n");
1461 prom_close_stdin();
1462 mb();
1463 data->go = 1;
1464 for (;;)
1465 opal_do_takeover(args);
1466}
1467#endif /* CONFIG_PPC_POWERNV */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001468
1469/*
1470 * Allocate room for and instantiate RTAS
1471 */
1472static void __init prom_instantiate_rtas(void)
1473{
1474 phandle rtas_node;
1475 ihandle rtas_inst;
1476 u32 base, entry = 0;
1477 u32 size = 0;
1478
1479 prom_debug("prom_instantiate_rtas: start...\n");
1480
1481 rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1482 prom_debug("rtas_node: %x\n", rtas_node);
1483 if (!PHANDLE_VALID(rtas_node))
1484 return;
1485
1486 prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1487 if (size == 0)
1488 return;
1489
1490 base = alloc_down(size, PAGE_SIZE, 0);
1491 if (base == 0) {
1492 prom_printf("RTAS allocation failed !\n");
1493 return;
1494 }
1495
1496 rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1497 if (!IHANDLE_VALID(rtas_inst)) {
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001498 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001499 return;
1500 }
1501
Anton Blanchard1f8737a2009-03-31 20:06:15 +00001502 prom_printf("instantiating rtas at 0x%x...", base);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001503
1504 if (call_prom_ret("call-method", 3, 2, &entry,
1505 ADDR("instantiate-rtas"),
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001506 rtas_inst, base) != 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001507 || entry == 0) {
1508 prom_printf(" failed\n");
1509 return;
1510 }
1511 prom_printf(" done\n");
1512
1513 reserve_mem(base, size);
1514
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001515 prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1516 &base, sizeof(base));
1517 prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1518 &entry, sizeof(entry));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001519
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001520#ifdef CONFIG_PPC_POWERNV
1521 /* PowerVN takeover hack */
1522 RELOC(prom_rtas_data) = base;
1523 RELOC(prom_rtas_entry) = entry;
1524 prom_getprop(rtas_node, "start-cpu", &RELOC(prom_rtas_start_cpu), 4);
1525#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001526 prom_debug("rtas base = 0x%x\n", base);
1527 prom_debug("rtas entry = 0x%x\n", entry);
1528 prom_debug("rtas size = 0x%x\n", (long)size);
1529
1530 prom_debug("prom_instantiate_rtas: end...\n");
1531}
1532
1533#ifdef CONFIG_PPC64
1534/*
1535 * Allocate room for and initialize TCE tables
1536 */
1537static void __init prom_initialize_tce_table(void)
1538{
1539 phandle node;
1540 ihandle phb_node;
1541 char compatible[64], type[64], model[64];
1542 char *path = RELOC(prom_scratch);
1543 u64 base, align;
1544 u32 minalign, minsize;
1545 u64 tce_entry, *tce_entryp;
1546 u64 local_alloc_top, local_alloc_bottom;
1547 u64 i;
1548
Jeremy Kerr165785e2006-11-11 17:25:18 +11001549 if (RELOC(prom_iommu_off))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001550 return;
1551
1552 prom_debug("starting prom_initialize_tce_table\n");
1553
1554 /* Cache current top of allocs so we reserve a single block */
1555 local_alloc_top = RELOC(alloc_top_high);
1556 local_alloc_bottom = local_alloc_top;
1557
1558 /* Search all nodes looking for PHBs. */
1559 for (node = 0; prom_next_node(&node); ) {
1560 compatible[0] = 0;
1561 type[0] = 0;
1562 model[0] = 0;
1563 prom_getprop(node, "compatible",
1564 compatible, sizeof(compatible));
1565 prom_getprop(node, "device_type", type, sizeof(type));
1566 prom_getprop(node, "model", model, sizeof(model));
1567
1568 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1569 continue;
1570
Linas Vepstase788ff12007-09-07 03:45:21 +10001571 /* Keep the old logic intact to avoid regression. */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001572 if (compatible[0] != 0) {
1573 if ((strstr(compatible, RELOC("python")) == NULL) &&
1574 (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1575 (strstr(compatible, RELOC("Winnipeg")) == NULL))
1576 continue;
1577 } else if (model[0] != 0) {
1578 if ((strstr(model, RELOC("ython")) == NULL) &&
1579 (strstr(model, RELOC("peedwagon")) == NULL) &&
1580 (strstr(model, RELOC("innipeg")) == NULL))
1581 continue;
1582 }
1583
1584 if (prom_getprop(node, "tce-table-minalign", &minalign,
1585 sizeof(minalign)) == PROM_ERROR)
1586 minalign = 0;
1587 if (prom_getprop(node, "tce-table-minsize", &minsize,
1588 sizeof(minsize)) == PROM_ERROR)
1589 minsize = 4UL << 20;
1590
1591 /*
1592 * Even though we read what OF wants, we just set the table
1593 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
1594 * By doing this, we avoid the pitfalls of trying to DMA to
1595 * MMIO space and the DMA alias hole.
1596 *
1597 * On POWER4, firmware sets the TCE region by assuming
1598 * each TCE table is 8MB. Using this memory for anything
1599 * else will impact performance, so we always allocate 8MB.
1600 * Anton
1601 */
1602 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1603 minsize = 8UL << 20;
1604 else
1605 minsize = 4UL << 20;
1606
1607 /* Align to the greater of the align or size */
1608 align = max(minalign, minsize);
1609 base = alloc_down(minsize, align, 1);
1610 if (base == 0)
1611 prom_panic("ERROR, cannot find space for TCE table.\n");
1612 if (base < local_alloc_bottom)
1613 local_alloc_bottom = base;
1614
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001615 /* It seems OF doesn't null-terminate the path :-( */
Li Zefanaca71ef2007-11-05 13:21:56 +11001616 memset(path, 0, PROM_SCRATCH_SIZE);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001617 /* Call OF to setup the TCE hardware */
1618 if (call_prom("package-to-path", 3, 1, node,
1619 path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1620 prom_printf("package-to-path failed\n");
1621 }
1622
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001623 /* Save away the TCE table attributes for later use. */
1624 prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1625 prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1626
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001627 prom_debug("TCE table: %s\n", path);
1628 prom_debug("\tnode = 0x%x\n", node);
1629 prom_debug("\tbase = 0x%x\n", base);
1630 prom_debug("\tsize = 0x%x\n", minsize);
1631
1632 /* Initialize the table to have a one-to-one mapping
1633 * over the allocated size.
1634 */
Ingo Molnar2b931fb2009-01-06 13:56:52 +00001635 tce_entryp = (u64 *)base;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001636 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1637 tce_entry = (i << PAGE_SHIFT);
1638 tce_entry |= 0x3;
1639 *tce_entryp = tce_entry;
1640 }
1641
1642 prom_printf("opening PHB %s", path);
1643 phb_node = call_prom("open", 1, 1, path);
1644 if (phb_node == 0)
1645 prom_printf("... failed\n");
1646 else
1647 prom_printf("... done\n");
1648
1649 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1650 phb_node, -1, minsize,
1651 (u32) base, (u32) (base >> 32));
1652 call_prom("close", 1, 0, phb_node);
1653 }
1654
1655 reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1656
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001657 /* These are only really needed if there is a memory limit in
1658 * effect, but we don't know so export them always. */
1659 RELOC(prom_tce_alloc_start) = local_alloc_bottom;
1660 RELOC(prom_tce_alloc_end) = local_alloc_top;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001661
1662 /* Flag the first invalid entry */
1663 prom_debug("ending prom_initialize_tce_table\n");
1664}
1665#endif
1666
1667/*
1668 * With CHRP SMP we need to use the OF to start the other processors.
1669 * We can't wait until smp_boot_cpus (the OF is trashed by then)
1670 * so we have to put the processors into a holding pattern controlled
1671 * by the kernel (not OF) before we destroy the OF.
1672 *
1673 * This uses a chunk of low memory, puts some holding pattern
1674 * code there and sends the other processors off to there until
1675 * smp_boot_cpus tells them to do something. The holding pattern
1676 * checks that address until its cpu # is there, when it is that
1677 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
1678 * of setting those values.
1679 *
1680 * We also use physical address 0x4 here to tell when a cpu
1681 * is in its holding pattern code.
1682 *
1683 * -- Cort
1684 */
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001685/*
1686 * We want to reference the copy of __secondary_hold_* in the
1687 * 0 - 0x100 address range
1688 */
1689#define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
1690
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001691static void __init prom_hold_cpus(void)
1692{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001693 unsigned long i;
1694 unsigned int reg;
1695 phandle node;
1696 char type[64];
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001697 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001698 unsigned long *spinloop
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001699 = (void *) LOW_ADDR(__secondary_hold_spinloop);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001700 unsigned long *acknowledge
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001701 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001702 unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001703
1704 prom_debug("prom_hold_cpus: start...\n");
1705 prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
1706 prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
1707 prom_debug(" 1) acknowledge = 0x%x\n",
1708 (unsigned long)acknowledge);
1709 prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
1710 prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
1711
1712 /* Set the common spinloop variable, so all of the secondary cpus
1713 * will block when they are awakened from their OF spinloop.
1714 * This must occur for both SMP and non SMP kernels, since OF will
1715 * be trashed when we move the kernel.
1716 */
1717 *spinloop = 0;
1718
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001719 /* look for cpus */
1720 for (node = 0; prom_next_node(&node); ) {
1721 type[0] = 0;
1722 prom_getprop(node, "device_type", type, sizeof(type));
1723 if (strcmp(type, RELOC("cpu")) != 0)
1724 continue;
1725
1726 /* Skip non-configured cpus. */
1727 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1728 if (strcmp(type, RELOC("okay")) != 0)
1729 continue;
1730
1731 reg = -1;
1732 prom_getprop(node, "reg", &reg, sizeof(reg));
1733
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001734 prom_debug("cpu hw idx = %lu\n", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001735
1736 /* Init the acknowledge var which will be reset by
1737 * the secondary cpu when it awakens from its OF
1738 * spinloop.
1739 */
1740 *acknowledge = (unsigned long)-1;
1741
Nathan Lynch7d2f6072008-07-27 15:24:50 +10001742 if (reg != _prom->cpu) {
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001743 /* Primary Thread of non-boot cpu or any thread */
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001744 prom_printf("starting cpu hw idx %lu... ", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001745 call_prom("start-cpu", 3, 0, node,
1746 secondary_hold, reg);
1747
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001748 for (i = 0; (i < 100000000) &&
1749 (*acknowledge == ((unsigned long)-1)); i++ )
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001750 mb();
1751
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001752 if (*acknowledge == reg)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001753 prom_printf("done\n");
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001754 else
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001755 prom_printf("failed: %x\n", *acknowledge);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001756 }
1757#ifdef CONFIG_SMP
1758 else
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001759 prom_printf("boot cpu hw idx %lu\n", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001760#endif /* CONFIG_SMP */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001761 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001762
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001763 prom_debug("prom_hold_cpus: end...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001764}
1765
1766
1767static void __init prom_init_client_services(unsigned long pp)
1768{
1769 struct prom_t *_prom = &RELOC(prom);
1770
1771 /* Get a handle to the prom entry point before anything else */
1772 RELOC(prom_entry) = pp;
1773
1774 /* get a handle for the stdout device */
1775 _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1776 if (!PHANDLE_VALID(_prom->chosen))
1777 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1778
1779 /* get device tree root */
1780 _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1781 if (!PHANDLE_VALID(_prom->root))
1782 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001783
1784 _prom->mmumap = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001785}
1786
Paul Mackerrasa575b802005-10-23 17:23:21 +10001787#ifdef CONFIG_PPC32
1788/*
1789 * For really old powermacs, we need to map things we claim.
1790 * For that, we need the ihandle of the mmu.
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001791 * Also, on the longtrail, we need to work around other bugs.
Paul Mackerrasa575b802005-10-23 17:23:21 +10001792 */
1793static void __init prom_find_mmu(void)
1794{
1795 struct prom_t *_prom = &RELOC(prom);
1796 phandle oprom;
1797 char version[64];
1798
1799 oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1800 if (!PHANDLE_VALID(oprom))
1801 return;
1802 if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1803 return;
1804 version[sizeof(version) - 1] = 0;
Paul Mackerrasa575b802005-10-23 17:23:21 +10001805 /* XXX might need to add other versions here */
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001806 if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1807 of_workarounds = OF_WA_CLAIM;
1808 else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1809 of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1810 call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1811 } else
Paul Mackerrasa575b802005-10-23 17:23:21 +10001812 return;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001813 _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
Paul Mackerrasa575b802005-10-23 17:23:21 +10001814 prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1815 sizeof(_prom->mmumap));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001816 if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1817 of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001818}
1819#else
1820#define prom_find_mmu()
1821#endif
1822
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001823static void __init prom_init_stdout(void)
1824{
1825 struct prom_t *_prom = &RELOC(prom);
1826 char *path = RELOC(of_stdout_device);
1827 char type[16];
1828 u32 val;
1829
1830 if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1831 prom_panic("cannot find stdout");
1832
1833 _prom->stdout = val;
1834
1835 /* Get the full OF pathname of the stdout device */
1836 memset(path, 0, 256);
1837 call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1838 val = call_prom("instance-to-package", 1, 1, _prom->stdout);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001839 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1840 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001841 prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001842 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1843 path, strlen(path) + 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001844
1845 /* If it's a display, note it */
1846 memset(type, 0, sizeof(type));
1847 prom_getprop(val, "device_type", type, sizeof(type));
1848 if (strcmp(type, RELOC("display")) == 0)
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001849 prom_setprop(val, path, "linux,boot-display", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001850}
1851
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001852static int __init prom_find_machine_type(void)
1853{
1854 struct prom_t *_prom = &RELOC(prom);
1855 char compat[256];
1856 int len, i = 0;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001857#ifdef CONFIG_PPC64
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001858 phandle rtas;
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001859 int x;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001860#endif
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001861
1862 /* Look for a PowerMac */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001863 len = prom_getprop(_prom->root, "compatible",
1864 compat, sizeof(compat)-1);
1865 if (len > 0) {
1866 compat[len] = 0;
1867 while (i < len) {
1868 char *p = &compat[i];
1869 int sl = strlen(p);
1870 if (sl == 0)
1871 break;
1872 if (strstr(p, RELOC("Power Macintosh")) ||
Paul Mackerrasa575b802005-10-23 17:23:21 +10001873 strstr(p, RELOC("MacRISC")))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001874 return PLATFORM_POWERMAC;
Arnd Bergmann133dda12006-06-07 12:04:18 +10001875#ifdef CONFIG_PPC64
1876 /* We must make sure we don't detect the IBM Cell
1877 * blades as pSeries due to some firmware issues,
1878 * so we do it here.
1879 */
1880 if (strstr(p, RELOC("IBM,CBEA")) ||
1881 strstr(p, RELOC("IBM,CPBW-1.0")))
1882 return PLATFORM_GENERIC;
1883#endif /* CONFIG_PPC64 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001884 i += sl + 1;
1885 }
1886 }
1887#ifdef CONFIG_PPC64
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001888 /* If not a mac, try to figure out if it's an IBM pSeries or any other
1889 * PAPR compliant platform. We assume it is if :
1890 * - /device_type is "chrp" (please, do NOT use that for future
1891 * non-IBM designs !
1892 * - it has /rtas
1893 */
Michael Ellerman6f806ce2006-04-07 13:56:21 +10001894 len = prom_getprop(_prom->root, "device_type",
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001895 compat, sizeof(compat)-1);
1896 if (len <= 0)
1897 return PLATFORM_GENERIC;
Benjamin Herrenschmidtcb6b2eb2006-05-15 15:46:03 +10001898 if (strcmp(compat, RELOC("chrp")))
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001899 return PLATFORM_GENERIC;
1900
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001901 /* Default to pSeries. We need to know if we are running LPAR */
1902 rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001903 if (!PHANDLE_VALID(rtas))
1904 return PLATFORM_GENERIC;
1905 x = prom_getproplen(rtas, "ibm,hypertas-functions");
1906 if (x != PROM_ERROR) {
Anton Blanchard4da727a2009-03-31 20:06:14 +00001907 prom_debug("Hypertas detected, assuming LPAR !\n");
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001908 return PLATFORM_PSERIES_LPAR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001909 }
1910 return PLATFORM_PSERIES;
1911#else
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001912 return PLATFORM_GENERIC;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001913#endif
1914}
1915
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001916static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1917{
1918 return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1919}
1920
1921/*
1922 * If we have a display that we don't know how to drive,
1923 * we will want to try to execute OF's open method for it
1924 * later. However, OF will probably fall over if we do that
1925 * we've taken over the MMU.
1926 * So we check whether we will need to open the display,
1927 * and if so, open it now.
1928 */
1929static void __init prom_check_displays(void)
1930{
1931 char type[16], *path;
1932 phandle node;
1933 ihandle ih;
1934 int i;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001935
1936 static unsigned char default_colors[] = {
1937 0x00, 0x00, 0x00,
1938 0x00, 0x00, 0xaa,
1939 0x00, 0xaa, 0x00,
1940 0x00, 0xaa, 0xaa,
1941 0xaa, 0x00, 0x00,
1942 0xaa, 0x00, 0xaa,
1943 0xaa, 0xaa, 0x00,
1944 0xaa, 0xaa, 0xaa,
1945 0x55, 0x55, 0x55,
1946 0x55, 0x55, 0xff,
1947 0x55, 0xff, 0x55,
1948 0x55, 0xff, 0xff,
1949 0xff, 0x55, 0x55,
1950 0xff, 0x55, 0xff,
1951 0xff, 0xff, 0x55,
1952 0xff, 0xff, 0xff
1953 };
1954 const unsigned char *clut;
1955
Anton Blanchard4da727a2009-03-31 20:06:14 +00001956 prom_debug("Looking for displays\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001957 for (node = 0; prom_next_node(&node); ) {
1958 memset(type, 0, sizeof(type));
1959 prom_getprop(node, "device_type", type, sizeof(type));
1960 if (strcmp(type, RELOC("display")) != 0)
1961 continue;
1962
1963 /* It seems OF doesn't null-terminate the path :-( */
1964 path = RELOC(prom_scratch);
1965 memset(path, 0, PROM_SCRATCH_SIZE);
1966
1967 /*
1968 * leave some room at the end of the path for appending extra
1969 * arguments
1970 */
1971 if (call_prom("package-to-path", 3, 1, node, path,
1972 PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1973 continue;
Anton Blanchard1f8737a2009-03-31 20:06:15 +00001974 prom_printf("found display : %s, opening... ", path);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001975
1976 ih = call_prom("open", 1, 1, path);
1977 if (ih == 0) {
1978 prom_printf("failed\n");
1979 continue;
1980 }
1981
1982 /* Success */
1983 prom_printf("done\n");
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001984 prom_setprop(node, path, "linux,opened", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001985
1986 /* Setup a usable color table when the appropriate
1987 * method is available. Should update this to set-colors */
1988 clut = RELOC(default_colors);
1989 for (i = 0; i < 32; i++, clut += 3)
1990 if (prom_set_color(ih, i, clut[0], clut[1],
1991 clut[2]) != 0)
1992 break;
1993
1994#ifdef CONFIG_LOGO_LINUX_CLUT224
1995 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1996 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1997 if (prom_set_color(ih, i + 32, clut[0], clut[1],
1998 clut[2]) != 0)
1999 break;
2000#endif /* CONFIG_LOGO_LINUX_CLUT224 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002001 }
2002}
2003
2004
2005/* Return (relocated) pointer to this much memory: moves initrd if reqd. */
2006static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
2007 unsigned long needed, unsigned long align)
2008{
2009 void *ret;
2010
2011 *mem_start = _ALIGN(*mem_start, align);
2012 while ((*mem_start + needed) > *mem_end) {
2013 unsigned long room, chunk;
2014
2015 prom_debug("Chunk exhausted, claiming more at %x...\n",
2016 RELOC(alloc_bottom));
2017 room = RELOC(alloc_top) - RELOC(alloc_bottom);
2018 if (room > DEVTREE_CHUNK_SIZE)
2019 room = DEVTREE_CHUNK_SIZE;
2020 if (room < PAGE_SIZE)
Anton Blanchardfbafd722011-07-25 20:47:51 +00002021 prom_panic("No memory for flatten_device_tree "
2022 "(no room)\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002023 chunk = alloc_up(room, 0);
2024 if (chunk == 0)
Anton Blanchardfbafd722011-07-25 20:47:51 +00002025 prom_panic("No memory for flatten_device_tree "
2026 "(claim failed)\n");
Anton Blanchard966728d2011-07-25 20:47:07 +00002027 *mem_end = chunk + room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002028 }
2029
2030 ret = (void *)*mem_start;
2031 *mem_start += needed;
2032
2033 return ret;
2034}
2035
2036#define dt_push_token(token, mem_start, mem_end) \
2037 do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
2038
2039static unsigned long __init dt_find_string(char *str)
2040{
2041 char *s, *os;
2042
2043 s = os = (char *)RELOC(dt_string_start);
2044 s += 4;
2045 while (s < (char *)RELOC(dt_string_end)) {
2046 if (strcmp(s, str) == 0)
2047 return s - os;
2048 s += strlen(s) + 1;
2049 }
2050 return 0;
2051}
2052
2053/*
2054 * The Open Firmware 1275 specification states properties must be 31 bytes or
2055 * less, however not all firmwares obey this. Make it 64 bytes to be safe.
2056 */
2057#define MAX_PROPERTY_NAME 64
2058
2059static void __init scan_dt_build_strings(phandle node,
2060 unsigned long *mem_start,
2061 unsigned long *mem_end)
2062{
2063 char *prev_name, *namep, *sstart;
2064 unsigned long soff;
2065 phandle child;
2066
2067 sstart = (char *)RELOC(dt_string_start);
2068
2069 /* get and store all property names */
2070 prev_name = RELOC("");
2071 for (;;) {
2072 /* 64 is max len of name including nul. */
2073 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
2074 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
2075 /* No more nodes: unwind alloc */
2076 *mem_start = (unsigned long)namep;
2077 break;
2078 }
2079
2080 /* skip "name" */
2081 if (strcmp(namep, RELOC("name")) == 0) {
2082 *mem_start = (unsigned long)namep;
2083 prev_name = RELOC("name");
2084 continue;
2085 }
2086 /* get/create string entry */
2087 soff = dt_find_string(namep);
2088 if (soff != 0) {
2089 *mem_start = (unsigned long)namep;
2090 namep = sstart + soff;
2091 } else {
2092 /* Trim off some if we can */
2093 *mem_start = (unsigned long)namep + strlen(namep) + 1;
2094 RELOC(dt_string_end) = *mem_start;
2095 }
2096 prev_name = namep;
2097 }
2098
2099 /* do all our children */
2100 child = call_prom("child", 1, 1, node);
2101 while (child != 0) {
2102 scan_dt_build_strings(child, mem_start, mem_end);
2103 child = call_prom("peer", 1, 1, child);
2104 }
2105}
2106
2107static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
2108 unsigned long *mem_end)
2109{
2110 phandle child;
2111 char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
2112 unsigned long soff;
2113 unsigned char *valp;
2114 static char pname[MAX_PROPERTY_NAME];
Paul Mackerrasc49888202005-10-26 21:52:53 +10002115 int l, room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002116
2117 dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
2118
2119 /* get the node's full name */
2120 namep = (char *)*mem_start;
Paul Mackerrasc49888202005-10-26 21:52:53 +10002121 room = *mem_end - *mem_start;
2122 if (room > 255)
2123 room = 255;
2124 l = call_prom("package-to-path", 3, 1, node, namep, room);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002125 if (l >= 0) {
2126 /* Didn't fit? Get more room. */
Paul Mackerrasc49888202005-10-26 21:52:53 +10002127 if (l >= room) {
2128 if (l >= *mem_end - *mem_start)
2129 namep = make_room(mem_start, mem_end, l+1, 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002130 call_prom("package-to-path", 3, 1, node, namep, l);
2131 }
2132 namep[l] = '\0';
2133
2134 /* Fixup an Apple bug where they have bogus \0 chars in the
Paul Mackerrasa575b802005-10-23 17:23:21 +10002135 * middle of the path in some properties, and extract
2136 * the unit name (everything after the last '/').
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002137 */
Paul Mackerrasa575b802005-10-23 17:23:21 +10002138 for (lp = p = namep, ep = namep + l; p < ep; p++) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002139 if (*p == '/')
Paul Mackerrasa575b802005-10-23 17:23:21 +10002140 lp = namep;
2141 else if (*p != 0)
2142 *lp++ = *p;
2143 }
2144 *lp = 0;
2145 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002146 }
2147
2148 /* get it again for debugging */
2149 path = RELOC(prom_scratch);
2150 memset(path, 0, PROM_SCRATCH_SIZE);
2151 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
2152
2153 /* get and store all properties */
2154 prev_name = RELOC("");
2155 sstart = (char *)RELOC(dt_string_start);
2156 for (;;) {
2157 if (call_prom("nextprop", 3, 1, node, prev_name,
2158 RELOC(pname)) != 1)
2159 break;
2160
2161 /* skip "name" */
2162 if (strcmp(RELOC(pname), RELOC("name")) == 0) {
2163 prev_name = RELOC("name");
2164 continue;
2165 }
2166
2167 /* find string offset */
2168 soff = dt_find_string(RELOC(pname));
2169 if (soff == 0) {
2170 prom_printf("WARNING: Can't find string index for"
2171 " <%s>, node %s\n", RELOC(pname), path);
2172 break;
2173 }
2174 prev_name = sstart + soff;
2175
2176 /* get length */
2177 l = call_prom("getproplen", 2, 1, node, RELOC(pname));
2178
2179 /* sanity checks */
2180 if (l == PROM_ERROR)
2181 continue;
2182 if (l > MAX_PROPERTY_LENGTH) {
2183 prom_printf("WARNING: ignoring large property ");
2184 /* It seems OF doesn't null-terminate the path :-( */
2185 prom_printf("[%s] ", path);
2186 prom_printf("%s length 0x%x\n", RELOC(pname), l);
2187 continue;
2188 }
2189
2190 /* push property head */
2191 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2192 dt_push_token(l, mem_start, mem_end);
2193 dt_push_token(soff, mem_start, mem_end);
2194
2195 /* push property content */
2196 valp = make_room(mem_start, mem_end, l, 4);
2197 call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
2198 *mem_start = _ALIGN(*mem_start, 4);
2199 }
2200
2201 /* Add a "linux,phandle" property. */
2202 soff = dt_find_string(RELOC("linux,phandle"));
2203 if (soff == 0)
2204 prom_printf("WARNING: Can't find string index for"
2205 " <linux-phandle> node %s\n", path);
2206 else {
2207 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2208 dt_push_token(4, mem_start, mem_end);
2209 dt_push_token(soff, mem_start, mem_end);
2210 valp = make_room(mem_start, mem_end, 4, 4);
2211 *(u32 *)valp = node;
2212 }
2213
2214 /* do all our children */
2215 child = call_prom("child", 1, 1, node);
2216 while (child != 0) {
2217 scan_dt_build_struct(child, mem_start, mem_end);
2218 child = call_prom("peer", 1, 1, child);
2219 }
2220
2221 dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
2222}
2223
2224static void __init flatten_device_tree(void)
2225{
2226 phandle root;
2227 unsigned long mem_start, mem_end, room;
2228 struct boot_param_header *hdr;
2229 struct prom_t *_prom = &RELOC(prom);
2230 char *namep;
2231 u64 *rsvmap;
2232
2233 /*
2234 * Check how much room we have between alloc top & bottom (+/- a
Anton Blanchardfbafd722011-07-25 20:47:51 +00002235 * few pages), crop to 1MB, as this is our "chunk" size
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002236 */
2237 room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
2238 if (room > DEVTREE_CHUNK_SIZE)
2239 room = DEVTREE_CHUNK_SIZE;
2240 prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
2241
2242 /* Now try to claim that */
2243 mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
2244 if (mem_start == 0)
2245 prom_panic("Can't allocate initial device-tree chunk\n");
Anton Blanchard966728d2011-07-25 20:47:07 +00002246 mem_end = mem_start + room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002247
2248 /* Get root of tree */
2249 root = call_prom("peer", 1, 1, (phandle)0);
2250 if (root == (phandle)0)
2251 prom_panic ("couldn't get device tree root\n");
2252
2253 /* Build header and make room for mem rsv map */
2254 mem_start = _ALIGN(mem_start, 4);
2255 hdr = make_room(&mem_start, &mem_end,
2256 sizeof(struct boot_param_header), 4);
2257 RELOC(dt_header_start) = (unsigned long)hdr;
2258 rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
2259
2260 /* Start of strings */
2261 mem_start = PAGE_ALIGN(mem_start);
2262 RELOC(dt_string_start) = mem_start;
2263 mem_start += 4; /* hole */
2264
2265 /* Add "linux,phandle" in there, we'll need it */
2266 namep = make_room(&mem_start, &mem_end, 16, 1);
2267 strcpy(namep, RELOC("linux,phandle"));
2268 mem_start = (unsigned long)namep + strlen(namep) + 1;
2269
2270 /* Build string array */
2271 prom_printf("Building dt strings...\n");
2272 scan_dt_build_strings(root, &mem_start, &mem_end);
2273 RELOC(dt_string_end) = mem_start;
2274
2275 /* Build structure */
2276 mem_start = PAGE_ALIGN(mem_start);
2277 RELOC(dt_struct_start) = mem_start;
2278 prom_printf("Building dt structure...\n");
2279 scan_dt_build_struct(root, &mem_start, &mem_end);
2280 dt_push_token(OF_DT_END, &mem_start, &mem_end);
2281 RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
2282
2283 /* Finish header */
2284 hdr->boot_cpuid_phys = _prom->cpu;
2285 hdr->magic = OF_DT_HEADER;
2286 hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
2287 hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
2288 hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
2289 hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
2290 hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
2291 hdr->version = OF_DT_VERSION;
2292 /* Version 16 is not backward compatible */
2293 hdr->last_comp_version = 0x10;
2294
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05002295 /* Copy the reserve map in */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002296 memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
2297
2298#ifdef DEBUG_PROM
2299 {
2300 int i;
2301 prom_printf("reserved memory map:\n");
2302 for (i = 0; i < RELOC(mem_reserve_cnt); i++)
2303 prom_printf(" %x - %x\n",
2304 RELOC(mem_reserve_map)[i].base,
2305 RELOC(mem_reserve_map)[i].size);
2306 }
2307#endif
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05002308 /* Bump mem_reserve_cnt to cause further reservations to fail
2309 * since it's too late.
2310 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002311 RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
2312
2313 prom_printf("Device tree strings 0x%x -> 0x%x\n",
2314 RELOC(dt_string_start), RELOC(dt_string_end));
2315 prom_printf("Device tree struct 0x%x -> 0x%x\n",
2316 RELOC(dt_struct_start), RELOC(dt_struct_end));
2317
2318}
2319
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002320#ifdef CONFIG_PPC_MAPLE
2321/* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2322 * The values are bad, and it doesn't even have the right number of cells. */
2323static void __init fixup_device_tree_maple(void)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002324{
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002325 phandle isa;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002326 u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002327 u32 isa_ranges[6];
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002328 char *name;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002329
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002330 name = "/ht@0/isa@4";
2331 isa = call_prom("finddevice", 1, 1, ADDR(name));
2332 if (!PHANDLE_VALID(isa)) {
2333 name = "/ht@0/isa@6";
2334 isa = call_prom("finddevice", 1, 1, ADDR(name));
2335 rloc = 0x01003000; /* IO space; PCI device = 6 */
2336 }
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002337 if (!PHANDLE_VALID(isa))
2338 return;
2339
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002340 if (prom_getproplen(isa, "ranges") != 12)
2341 return;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002342 if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2343 == PROM_ERROR)
2344 return;
2345
2346 if (isa_ranges[0] != 0x1 ||
2347 isa_ranges[1] != 0xf4000000 ||
2348 isa_ranges[2] != 0x00010000)
2349 return;
2350
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002351 prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002352
2353 isa_ranges[0] = 0x1;
2354 isa_ranges[1] = 0x0;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002355 isa_ranges[2] = rloc;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002356 isa_ranges[3] = 0x0;
2357 isa_ranges[4] = 0x0;
2358 isa_ranges[5] = 0x00010000;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002359 prom_setprop(isa, name, "ranges",
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002360 isa_ranges, sizeof(isa_ranges));
2361}
Harry Ciao8f101a02009-06-17 16:28:00 -07002362
2363#define CPC925_MC_START 0xf8000000
2364#define CPC925_MC_LENGTH 0x1000000
2365/* The values for memory-controller don't have right number of cells */
2366static void __init fixup_device_tree_maple_memory_controller(void)
2367{
2368 phandle mc;
2369 u32 mc_reg[4];
2370 char *name = "/hostbridge@f8000000";
2371 struct prom_t *_prom = &RELOC(prom);
2372 u32 ac, sc;
2373
2374 mc = call_prom("finddevice", 1, 1, ADDR(name));
2375 if (!PHANDLE_VALID(mc))
2376 return;
2377
2378 if (prom_getproplen(mc, "reg") != 8)
2379 return;
2380
2381 prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
2382 prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
2383 if ((ac != 2) || (sc != 2))
2384 return;
2385
2386 if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
2387 return;
2388
2389 if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
2390 return;
2391
2392 prom_printf("Fixing up bogus hostbridge on Maple...\n");
2393
2394 mc_reg[0] = 0x0;
2395 mc_reg[1] = CPC925_MC_START;
2396 mc_reg[2] = 0x0;
2397 mc_reg[3] = CPC925_MC_LENGTH;
2398 prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
2399}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002400#else
2401#define fixup_device_tree_maple()
Harry Ciao8f101a02009-06-17 16:28:00 -07002402#define fixup_device_tree_maple_memory_controller()
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002403#endif
2404
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002405#ifdef CONFIG_PPC_CHRP
Olaf Heringe4805922007-04-04 18:20:04 +02002406/*
2407 * Pegasos and BriQ lacks the "ranges" property in the isa node
2408 * Pegasos needs decimal IRQ 14/15, not hexadecimal
Olaf Hering556ecf92007-08-18 04:27:17 +10002409 * Pegasos has the IDE configured in legacy mode, but advertised as native
Olaf Heringe4805922007-04-04 18:20:04 +02002410 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002411static void __init fixup_device_tree_chrp(void)
2412{
Olaf Heringe4805922007-04-04 18:20:04 +02002413 phandle ph;
2414 u32 prop[6];
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002415 u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002416 char *name;
2417 int rc;
2418
2419 name = "/pci@80000000/isa@c";
Olaf Heringe4805922007-04-04 18:20:04 +02002420 ph = call_prom("finddevice", 1, 1, ADDR(name));
2421 if (!PHANDLE_VALID(ph)) {
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002422 name = "/pci@ff500000/isa@6";
Olaf Heringe4805922007-04-04 18:20:04 +02002423 ph = call_prom("finddevice", 1, 1, ADDR(name));
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002424 rloc = 0x01003000; /* IO space; PCI device = 6 */
2425 }
Olaf Heringe4805922007-04-04 18:20:04 +02002426 if (PHANDLE_VALID(ph)) {
2427 rc = prom_getproplen(ph, "ranges");
2428 if (rc == 0 || rc == PROM_ERROR) {
2429 prom_printf("Fixing up missing ISA range on Pegasos...\n");
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002430
Olaf Heringe4805922007-04-04 18:20:04 +02002431 prop[0] = 0x1;
2432 prop[1] = 0x0;
2433 prop[2] = rloc;
2434 prop[3] = 0x0;
2435 prop[4] = 0x0;
2436 prop[5] = 0x00010000;
2437 prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2438 }
2439 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002440
Olaf Heringe4805922007-04-04 18:20:04 +02002441 name = "/pci@80000000/ide@C,1";
2442 ph = call_prom("finddevice", 1, 1, ADDR(name));
2443 if (PHANDLE_VALID(ph)) {
2444 prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2445 prop[0] = 14;
2446 prop[1] = 0x0;
Olaf Hering556ecf92007-08-18 04:27:17 +10002447 prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2448 prom_printf("Fixing up IDE class-code on Pegasos...\n");
2449 rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2450 if (rc == sizeof(u32)) {
2451 prop[0] &= ~0x5;
2452 prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2453 }
Olaf Heringe4805922007-04-04 18:20:04 +02002454 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002455}
2456#else
2457#define fixup_device_tree_chrp()
2458#endif
2459
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002460#if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002461static void __init fixup_device_tree_pmac(void)
2462{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002463 phandle u3, i2c, mpic;
2464 u32 u3_rev;
2465 u32 interrupts[2];
2466 u32 parent;
2467
2468 /* Some G5s have a missing interrupt definition, fix it up here */
2469 u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2470 if (!PHANDLE_VALID(u3))
2471 return;
2472 i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2473 if (!PHANDLE_VALID(i2c))
2474 return;
2475 mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2476 if (!PHANDLE_VALID(mpic))
2477 return;
2478
2479 /* check if proper rev of u3 */
2480 if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2481 == PROM_ERROR)
2482 return;
Benjamin Herrenschmidt7d496972005-11-07 14:36:21 +11002483 if (u3_rev < 0x35 || u3_rev > 0x39)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002484 return;
2485 /* does it need fixup ? */
2486 if (prom_getproplen(i2c, "interrupts") > 0)
2487 return;
2488
2489 prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2490
2491 /* interrupt on this revision of u3 is number 0 and level */
2492 interrupts[0] = 0;
2493 interrupts[1] = 1;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002494 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2495 &interrupts, sizeof(interrupts));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002496 parent = (u32)mpic;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002497 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2498 &parent, sizeof(parent));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002499}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002500#else
2501#define fixup_device_tree_pmac()
2502#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002503
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002504#ifdef CONFIG_PPC_EFIKA
Grant Likely94d2dde2008-01-24 22:25:32 -07002505/*
2506 * The MPC5200 FEC driver requires an phy-handle property to tell it how
2507 * to talk to the phy. If the phy-handle property is missing, then this
2508 * function is called to add the appropriate nodes and link it to the
2509 * ethernet node.
2510 */
2511static void __init fixup_device_tree_efika_add_phy(void)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002512{
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002513 u32 node;
2514 char prop[64];
Grant Likely94d2dde2008-01-24 22:25:32 -07002515 int rv;
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002516
Grant Likely94d2dde2008-01-24 22:25:32 -07002517 /* Check if /builtin/ethernet exists - bail if it doesn't */
2518 node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002519 if (!PHANDLE_VALID(node))
2520 return;
2521
Grant Likely94d2dde2008-01-24 22:25:32 -07002522 /* Check if the phy-handle property exists - bail if it does */
2523 rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2524 if (!rv)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002525 return;
2526
Grant Likely94d2dde2008-01-24 22:25:32 -07002527 /*
2528 * At this point the ethernet device doesn't have a phy described.
2529 * Now we need to add the missing phy node and linkage
2530 */
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002531
Grant Likely94d2dde2008-01-24 22:25:32 -07002532 /* Check for an MDIO bus node - if missing then create one */
Olaf Hering6f4347c2008-01-10 01:06:08 +11002533 node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2534 if (!PHANDLE_VALID(node)) {
2535 prom_printf("Adding Ethernet MDIO node\n");
2536 call_prom("interpret", 1, 1,
2537 " s\" /builtin\" find-device"
2538 " new-device"
2539 " 1 encode-int s\" #address-cells\" property"
2540 " 0 encode-int s\" #size-cells\" property"
Grant Likely94d2dde2008-01-24 22:25:32 -07002541 " s\" mdio\" device-name"
2542 " s\" fsl,mpc5200b-mdio\" encode-string"
Olaf Hering6f4347c2008-01-10 01:06:08 +11002543 " s\" compatible\" property"
2544 " 0xf0003000 0x400 reg"
2545 " 0x2 encode-int"
2546 " 0x5 encode-int encode+"
2547 " 0x3 encode-int encode+"
2548 " s\" interrupts\" property"
2549 " finish-device");
2550 };
2551
Grant Likely94d2dde2008-01-24 22:25:32 -07002552 /* Check for a PHY device node - if missing then create one and
2553 * give it's phandle to the ethernet node */
2554 node = call_prom("finddevice", 1, 1,
2555 ADDR("/builtin/mdio/ethernet-phy"));
Olaf Hering6f4347c2008-01-10 01:06:08 +11002556 if (!PHANDLE_VALID(node)) {
2557 prom_printf("Adding Ethernet PHY node\n");
2558 call_prom("interpret", 1, 1,
2559 " s\" /builtin/mdio\" find-device"
2560 " new-device"
2561 " s\" ethernet-phy\" device-name"
2562 " 0x10 encode-int s\" reg\" property"
2563 " my-self"
2564 " ihandle>phandle"
2565 " finish-device"
2566 " s\" /builtin/ethernet\" find-device"
2567 " encode-int"
2568 " s\" phy-handle\" property"
2569 " device-end");
2570 }
Grant Likely94d2dde2008-01-24 22:25:32 -07002571}
Olaf Hering6f4347c2008-01-10 01:06:08 +11002572
Grant Likely94d2dde2008-01-24 22:25:32 -07002573static void __init fixup_device_tree_efika(void)
2574{
2575 int sound_irq[3] = { 2, 2, 0 };
2576 int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2577 3,4,0, 3,5,0, 3,6,0, 3,7,0,
2578 3,8,0, 3,9,0, 3,10,0, 3,11,0,
2579 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2580 u32 node;
2581 char prop[64];
2582 int rv, len;
2583
2584 /* Check if we're really running on a EFIKA */
2585 node = call_prom("finddevice", 1, 1, ADDR("/"));
2586 if (!PHANDLE_VALID(node))
2587 return;
2588
2589 rv = prom_getprop(node, "model", prop, sizeof(prop));
2590 if (rv == PROM_ERROR)
2591 return;
2592 if (strcmp(prop, "EFIKA5K2"))
2593 return;
2594
2595 prom_printf("Applying EFIKA device tree fixups\n");
2596
2597 /* Claiming to be 'chrp' is death */
2598 node = call_prom("finddevice", 1, 1, ADDR("/"));
2599 rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2600 if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2601 prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2602
David Woodhouse7f4392c2008-04-14 02:52:38 +10002603 /* CODEGEN,description is exposed in /proc/cpuinfo so
2604 fix that too */
2605 rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2606 if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2607 prom_setprop(node, "/", "CODEGEN,description",
2608 "Efika 5200B PowerPC System",
2609 sizeof("Efika 5200B PowerPC System"));
2610
Grant Likely94d2dde2008-01-24 22:25:32 -07002611 /* Fixup bestcomm interrupts property */
2612 node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2613 if (PHANDLE_VALID(node)) {
2614 len = prom_getproplen(node, "interrupts");
2615 if (len == 12) {
2616 prom_printf("Fixing bestcomm interrupts property\n");
2617 prom_setprop(node, "/builtin/bestcom", "interrupts",
2618 bcomm_irq, sizeof(bcomm_irq));
2619 }
2620 }
2621
2622 /* Fixup sound interrupts property */
2623 node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2624 if (PHANDLE_VALID(node)) {
2625 rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2626 if (rv == PROM_ERROR) {
2627 prom_printf("Adding sound interrupts property\n");
2628 prom_setprop(node, "/builtin/sound", "interrupts",
2629 sound_irq, sizeof(sound_irq));
2630 }
2631 }
2632
2633 /* Make sure ethernet phy-handle property exists */
2634 fixup_device_tree_efika_add_phy();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002635}
2636#else
2637#define fixup_device_tree_efika()
2638#endif
2639
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002640static void __init fixup_device_tree(void)
2641{
2642 fixup_device_tree_maple();
Harry Ciao8f101a02009-06-17 16:28:00 -07002643 fixup_device_tree_maple_memory_controller();
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002644 fixup_device_tree_chrp();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002645 fixup_device_tree_pmac();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002646 fixup_device_tree_efika();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002647}
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002648
2649static void __init prom_find_boot_cpu(void)
2650{
Linas Vepstase788ff12007-09-07 03:45:21 +10002651 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002652 u32 getprop_rval;
2653 ihandle prom_cpu;
2654 phandle cpu_pkg;
2655
Paul Mackerrasa575b802005-10-23 17:23:21 +10002656 _prom->cpu = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002657 if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
Paul Mackerrasa575b802005-10-23 17:23:21 +10002658 return;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002659
2660 cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2661
2662 prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
2663 _prom->cpu = getprop_rval;
2664
Michael Neuling2c48a7d2010-07-27 18:26:21 +00002665 prom_debug("Booting CPU hw index = %lu\n", _prom->cpu);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002666}
2667
2668static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2669{
2670#ifdef CONFIG_BLK_DEV_INITRD
Linas Vepstase788ff12007-09-07 03:45:21 +10002671 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002672
2673 if (r3 && r4 && r4 != 0xdeadbeef) {
2674 unsigned long val;
2675
Michael Ellerman51fae6de2005-12-04 18:39:15 +11002676 RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002677 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2678
2679 val = RELOC(prom_initrd_start);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002680 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2681 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002682 val = RELOC(prom_initrd_end);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002683 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2684 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002685
2686 reserve_mem(RELOC(prom_initrd_start),
2687 RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
2688
2689 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
2690 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
2691 }
2692#endif /* CONFIG_BLK_DEV_INITRD */
2693}
2694
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002695
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002696/*
2697 * We enter here early on, when the Open Firmware prom is still
2698 * handling exceptions and the MMU hash table for us.
2699 */
2700
2701unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2702 unsigned long pp,
Paul Mackerras549e8152008-08-30 11:43:47 +10002703 unsigned long r6, unsigned long r7,
2704 unsigned long kbase)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002705{
Linas Vepstase788ff12007-09-07 03:45:21 +10002706 struct prom_t *_prom;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002707 unsigned long hdr;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002708
2709#ifdef CONFIG_PPC32
Paul Mackerras549e8152008-08-30 11:43:47 +10002710 unsigned long offset = reloc_offset();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002711 reloc_got2(offset);
2712#endif
2713
2714 _prom = &RELOC(prom);
2715
2716 /*
2717 * First zero the BSS
2718 */
2719 memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
2720
2721 /*
2722 * Init interface to Open Firmware, get some node references,
2723 * like /chosen
2724 */
2725 prom_init_client_services(pp);
2726
2727 /*
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002728 * See if this OF is old enough that we need to do explicit maps
2729 * and other workarounds
2730 */
2731 prom_find_mmu();
2732
2733 /*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002734 * Init prom stdout device
2735 */
2736 prom_init_stdout();
2737
Benjamin Herrenschmidt151a9f42009-03-22 16:04:53 +00002738 prom_printf("Preparing to boot %s", RELOC(linux_banner));
Michael Ellermane7943fb2009-03-04 19:02:01 +00002739
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002740 /*
2741 * Get default machine type. At this point, we do not differentiate
2742 * between pSeries SMP and pSeries LPAR
2743 */
2744 RELOC(of_platform) = prom_find_machine_type();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002745
Paul Mackerras549e8152008-08-30 11:43:47 +10002746#ifndef CONFIG_RELOCATABLE
Olaf Heringadd60ef2006-03-23 22:03:57 +01002747 /* Bail if this is a kdump kernel. */
2748 if (PHYSICAL_START > 0)
2749 prom_panic("Error: You can't boot a kdump kernel from OF!\n");
Paul Mackerras549e8152008-08-30 11:43:47 +10002750#endif
Olaf Heringadd60ef2006-03-23 22:03:57 +01002751
2752 /*
2753 * Check for an initrd
2754 */
2755 prom_check_initrd(r3, r4);
2756
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002757#if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002758 /*
2759 * On pSeries, inform the firmware about our capabilities
2760 */
Paul Mackerras799d6042005-11-10 13:37:51 +11002761 if (RELOC(of_platform) == PLATFORM_PSERIES ||
2762 RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002763 prom_send_capabilities();
2764#endif
2765
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002766 /*
Arnd Bergmannf3f66f52005-10-31 20:08:37 -05002767 * Copy the CPU hold code
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002768 */
Linas Vepstase788ff12007-09-07 03:45:21 +10002769 if (RELOC(of_platform) != PLATFORM_POWERMAC)
Paul Mackerras549e8152008-08-30 11:43:47 +10002770 copy_and_flush(0, kbase, 0x100, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002771
2772 /*
2773 * Do early parsing of command line
2774 */
2775 early_cmdline_parse();
2776
2777 /*
2778 * Initialize memory management within prom_init
2779 */
2780 prom_init_mem();
2781
2782 /*
2783 * Determine which cpu is actually running right _now_
2784 */
2785 prom_find_boot_cpu();
2786
2787 /*
2788 * Initialize display devices
2789 */
2790 prom_check_displays();
2791
2792#ifdef CONFIG_PPC64
2793 /*
2794 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2795 * that uses the allocator, we need to make sure we get the top of memory
2796 * available for us here...
2797 */
2798 if (RELOC(of_platform) == PLATFORM_PSERIES)
2799 prom_initialize_tce_table();
2800#endif
2801
2802 /*
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002803 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
2804 * have a usable RTAS implementation.
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002805 */
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002806 if (RELOC(of_platform) != PLATFORM_POWERMAC)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002807 prom_instantiate_rtas();
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002808
2809#ifdef CONFIG_PPC_POWERNV
2810 /* Detect HAL and try instanciating it & doing takeover */
2811 if (RELOC(of_platform) == PLATFORM_PSERIES_LPAR) {
2812 prom_query_opal();
2813 if (RELOC(of_platform) == PLATFORM_OPAL) {
2814 prom_opal_hold_cpus();
2815 prom_opal_takeover();
2816 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002817 }
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002818#endif
2819
2820 /*
2821 * On non-powermacs, put all CPUs in spin-loops.
2822 *
2823 * PowerMacs use a different mechanism to spin CPUs
2824 */
2825 if (RELOC(of_platform) != PLATFORM_POWERMAC)
2826 prom_hold_cpus();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002827
2828 /*
2829 * Fill in some infos for use by the kernel later on
2830 */
Benjamin Krillcf687872009-07-27 22:02:39 +00002831 if (RELOC(prom_memory_limit))
2832 prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
2833 &RELOC(prom_memory_limit),
2834 sizeof(prom_memory_limit));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002835#ifdef CONFIG_PPC64
Jeremy Kerr165785e2006-11-11 17:25:18 +11002836 if (RELOC(prom_iommu_off))
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002837 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2838 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002839
Jeremy Kerr165785e2006-11-11 17:25:18 +11002840 if (RELOC(prom_iommu_force_on))
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002841 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2842 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002843
2844 if (RELOC(prom_tce_alloc_start)) {
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002845 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002846 &RELOC(prom_tce_alloc_start),
2847 sizeof(prom_tce_alloc_start));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002848 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002849 &RELOC(prom_tce_alloc_end),
2850 sizeof(prom_tce_alloc_end));
2851 }
2852#endif
2853
2854 /*
2855 * Fixup any known bugs in the device-tree
2856 */
2857 fixup_device_tree();
2858
2859 /*
2860 * Now finally create the flattened device-tree
2861 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00002862 prom_printf("copying OF device tree...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002863 flatten_device_tree();
2864
Paul Mackerras3825ac02005-11-08 22:48:08 +11002865 /*
2866 * in case stdin is USB and still active on IBM machines...
2867 * Unfortunately quiesce crashes on some powermacs if we have
2868 * closed stdin already (in particular the powerbook 101).
2869 */
2870 if (RELOC(of_platform) != PLATFORM_POWERMAC)
2871 prom_close_stdin();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002872
2873 /*
2874 * Call OF "quiesce" method to shut down pending DMA's from
2875 * devices etc...
2876 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00002877 prom_printf("Calling quiesce...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002878 call_prom("quiesce", 0, 0);
2879
2880 /*
2881 * And finally, call the kernel passing it the flattened device
2882 * tree and NULL as r5, thus triggering the new entry point which
2883 * is common to us and kexec
2884 */
2885 hdr = RELOC(dt_header_start);
2886 prom_printf("returning from prom_init\n");
2887 prom_debug("->dt_header_start=0x%x\n", hdr);
2888
2889#ifdef CONFIG_PPC32
2890 reloc_got2(-offset);
2891#endif
2892
Paul Mackerras549e8152008-08-30 11:43:47 +10002893 __start(hdr, kbase, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002894
2895 return 0;
2896}