blob: e3f390427216a399377ea720aa7874ff1d183a95 [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
Benjamin Herrenschmidt817c21a2011-09-19 17:44:56 +00001452 /* Copy the command line to the kernel image */
1453 strlcpy(RELOC(boot_command_line), RELOC(prom_cmd_line),
1454 COMMAND_LINE_SIZE);
1455
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001456 prom_debug(" k_image = 0x%lx\n", args->k_image);
1457 prom_debug(" k_size = 0x%lx\n", args->k_size);
1458 prom_debug(" k_entry = 0x%lx\n", args->k_entry);
1459 prom_debug(" k_entry2 = 0x%lx\n", args->k_entry2);
1460 prom_debug(" hal_addr = 0x%lx\n", args->hal_addr);
1461 prom_debug(" rd_image = 0x%lx\n", args->rd_image);
1462 prom_debug(" rd_size = 0x%lx\n", args->rd_size);
1463 prom_debug(" rd_loc = 0x%lx\n", args->rd_loc);
1464 prom_printf("Performing OPAL takeover,this can take a few minutes..\n");
1465 prom_close_stdin();
1466 mb();
1467 data->go = 1;
1468 for (;;)
1469 opal_do_takeover(args);
1470}
1471#endif /* CONFIG_PPC_POWERNV */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001472
1473/*
1474 * Allocate room for and instantiate RTAS
1475 */
1476static void __init prom_instantiate_rtas(void)
1477{
1478 phandle rtas_node;
1479 ihandle rtas_inst;
1480 u32 base, entry = 0;
1481 u32 size = 0;
1482
1483 prom_debug("prom_instantiate_rtas: start...\n");
1484
1485 rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1486 prom_debug("rtas_node: %x\n", rtas_node);
1487 if (!PHANDLE_VALID(rtas_node))
1488 return;
1489
1490 prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1491 if (size == 0)
1492 return;
1493
1494 base = alloc_down(size, PAGE_SIZE, 0);
1495 if (base == 0) {
1496 prom_printf("RTAS allocation failed !\n");
1497 return;
1498 }
1499
1500 rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1501 if (!IHANDLE_VALID(rtas_inst)) {
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001502 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001503 return;
1504 }
1505
Anton Blanchard1f8737a2009-03-31 20:06:15 +00001506 prom_printf("instantiating rtas at 0x%x...", base);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001507
1508 if (call_prom_ret("call-method", 3, 2, &entry,
1509 ADDR("instantiate-rtas"),
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001510 rtas_inst, base) != 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001511 || entry == 0) {
1512 prom_printf(" failed\n");
1513 return;
1514 }
1515 prom_printf(" done\n");
1516
1517 reserve_mem(base, size);
1518
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001519 prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1520 &base, sizeof(base));
1521 prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1522 &entry, sizeof(entry));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001523
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001524#ifdef CONFIG_PPC_POWERNV
1525 /* PowerVN takeover hack */
1526 RELOC(prom_rtas_data) = base;
1527 RELOC(prom_rtas_entry) = entry;
1528 prom_getprop(rtas_node, "start-cpu", &RELOC(prom_rtas_start_cpu), 4);
1529#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001530 prom_debug("rtas base = 0x%x\n", base);
1531 prom_debug("rtas entry = 0x%x\n", entry);
1532 prom_debug("rtas size = 0x%x\n", (long)size);
1533
1534 prom_debug("prom_instantiate_rtas: end...\n");
1535}
1536
1537#ifdef CONFIG_PPC64
1538/*
1539 * Allocate room for and initialize TCE tables
1540 */
1541static void __init prom_initialize_tce_table(void)
1542{
1543 phandle node;
1544 ihandle phb_node;
1545 char compatible[64], type[64], model[64];
1546 char *path = RELOC(prom_scratch);
1547 u64 base, align;
1548 u32 minalign, minsize;
1549 u64 tce_entry, *tce_entryp;
1550 u64 local_alloc_top, local_alloc_bottom;
1551 u64 i;
1552
Jeremy Kerr165785e2006-11-11 17:25:18 +11001553 if (RELOC(prom_iommu_off))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001554 return;
1555
1556 prom_debug("starting prom_initialize_tce_table\n");
1557
1558 /* Cache current top of allocs so we reserve a single block */
1559 local_alloc_top = RELOC(alloc_top_high);
1560 local_alloc_bottom = local_alloc_top;
1561
1562 /* Search all nodes looking for PHBs. */
1563 for (node = 0; prom_next_node(&node); ) {
1564 compatible[0] = 0;
1565 type[0] = 0;
1566 model[0] = 0;
1567 prom_getprop(node, "compatible",
1568 compatible, sizeof(compatible));
1569 prom_getprop(node, "device_type", type, sizeof(type));
1570 prom_getprop(node, "model", model, sizeof(model));
1571
1572 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1573 continue;
1574
Linas Vepstase788ff12007-09-07 03:45:21 +10001575 /* Keep the old logic intact to avoid regression. */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001576 if (compatible[0] != 0) {
1577 if ((strstr(compatible, RELOC("python")) == NULL) &&
1578 (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1579 (strstr(compatible, RELOC("Winnipeg")) == NULL))
1580 continue;
1581 } else if (model[0] != 0) {
1582 if ((strstr(model, RELOC("ython")) == NULL) &&
1583 (strstr(model, RELOC("peedwagon")) == NULL) &&
1584 (strstr(model, RELOC("innipeg")) == NULL))
1585 continue;
1586 }
1587
1588 if (prom_getprop(node, "tce-table-minalign", &minalign,
1589 sizeof(minalign)) == PROM_ERROR)
1590 minalign = 0;
1591 if (prom_getprop(node, "tce-table-minsize", &minsize,
1592 sizeof(minsize)) == PROM_ERROR)
1593 minsize = 4UL << 20;
1594
1595 /*
1596 * Even though we read what OF wants, we just set the table
1597 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
1598 * By doing this, we avoid the pitfalls of trying to DMA to
1599 * MMIO space and the DMA alias hole.
1600 *
1601 * On POWER4, firmware sets the TCE region by assuming
1602 * each TCE table is 8MB. Using this memory for anything
1603 * else will impact performance, so we always allocate 8MB.
1604 * Anton
1605 */
1606 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1607 minsize = 8UL << 20;
1608 else
1609 minsize = 4UL << 20;
1610
1611 /* Align to the greater of the align or size */
1612 align = max(minalign, minsize);
1613 base = alloc_down(minsize, align, 1);
1614 if (base == 0)
1615 prom_panic("ERROR, cannot find space for TCE table.\n");
1616 if (base < local_alloc_bottom)
1617 local_alloc_bottom = base;
1618
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001619 /* It seems OF doesn't null-terminate the path :-( */
Li Zefanaca71ef2007-11-05 13:21:56 +11001620 memset(path, 0, PROM_SCRATCH_SIZE);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001621 /* Call OF to setup the TCE hardware */
1622 if (call_prom("package-to-path", 3, 1, node,
1623 path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1624 prom_printf("package-to-path failed\n");
1625 }
1626
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001627 /* Save away the TCE table attributes for later use. */
1628 prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1629 prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1630
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001631 prom_debug("TCE table: %s\n", path);
1632 prom_debug("\tnode = 0x%x\n", node);
1633 prom_debug("\tbase = 0x%x\n", base);
1634 prom_debug("\tsize = 0x%x\n", minsize);
1635
1636 /* Initialize the table to have a one-to-one mapping
1637 * over the allocated size.
1638 */
Ingo Molnar2b931fb2009-01-06 13:56:52 +00001639 tce_entryp = (u64 *)base;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001640 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1641 tce_entry = (i << PAGE_SHIFT);
1642 tce_entry |= 0x3;
1643 *tce_entryp = tce_entry;
1644 }
1645
1646 prom_printf("opening PHB %s", path);
1647 phb_node = call_prom("open", 1, 1, path);
1648 if (phb_node == 0)
1649 prom_printf("... failed\n");
1650 else
1651 prom_printf("... done\n");
1652
1653 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1654 phb_node, -1, minsize,
1655 (u32) base, (u32) (base >> 32));
1656 call_prom("close", 1, 0, phb_node);
1657 }
1658
1659 reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1660
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001661 /* These are only really needed if there is a memory limit in
1662 * effect, but we don't know so export them always. */
1663 RELOC(prom_tce_alloc_start) = local_alloc_bottom;
1664 RELOC(prom_tce_alloc_end) = local_alloc_top;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001665
1666 /* Flag the first invalid entry */
1667 prom_debug("ending prom_initialize_tce_table\n");
1668}
1669#endif
1670
1671/*
1672 * With CHRP SMP we need to use the OF to start the other processors.
1673 * We can't wait until smp_boot_cpus (the OF is trashed by then)
1674 * so we have to put the processors into a holding pattern controlled
1675 * by the kernel (not OF) before we destroy the OF.
1676 *
1677 * This uses a chunk of low memory, puts some holding pattern
1678 * code there and sends the other processors off to there until
1679 * smp_boot_cpus tells them to do something. The holding pattern
1680 * checks that address until its cpu # is there, when it is that
1681 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
1682 * of setting those values.
1683 *
1684 * We also use physical address 0x4 here to tell when a cpu
1685 * is in its holding pattern code.
1686 *
1687 * -- Cort
1688 */
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001689/*
1690 * We want to reference the copy of __secondary_hold_* in the
1691 * 0 - 0x100 address range
1692 */
1693#define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
1694
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001695static void __init prom_hold_cpus(void)
1696{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001697 unsigned long i;
1698 unsigned int reg;
1699 phandle node;
1700 char type[64];
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001701 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001702 unsigned long *spinloop
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001703 = (void *) LOW_ADDR(__secondary_hold_spinloop);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001704 unsigned long *acknowledge
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001705 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001706 unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001707
1708 prom_debug("prom_hold_cpus: start...\n");
1709 prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
1710 prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
1711 prom_debug(" 1) acknowledge = 0x%x\n",
1712 (unsigned long)acknowledge);
1713 prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
1714 prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
1715
1716 /* Set the common spinloop variable, so all of the secondary cpus
1717 * will block when they are awakened from their OF spinloop.
1718 * This must occur for both SMP and non SMP kernels, since OF will
1719 * be trashed when we move the kernel.
1720 */
1721 *spinloop = 0;
1722
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001723 /* look for cpus */
1724 for (node = 0; prom_next_node(&node); ) {
1725 type[0] = 0;
1726 prom_getprop(node, "device_type", type, sizeof(type));
1727 if (strcmp(type, RELOC("cpu")) != 0)
1728 continue;
1729
1730 /* Skip non-configured cpus. */
1731 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1732 if (strcmp(type, RELOC("okay")) != 0)
1733 continue;
1734
1735 reg = -1;
1736 prom_getprop(node, "reg", &reg, sizeof(reg));
1737
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001738 prom_debug("cpu hw idx = %lu\n", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001739
1740 /* Init the acknowledge var which will be reset by
1741 * the secondary cpu when it awakens from its OF
1742 * spinloop.
1743 */
1744 *acknowledge = (unsigned long)-1;
1745
Nathan Lynch7d2f6072008-07-27 15:24:50 +10001746 if (reg != _prom->cpu) {
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00001747 /* Primary Thread of non-boot cpu or any thread */
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001748 prom_printf("starting cpu hw idx %lu... ", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001749 call_prom("start-cpu", 3, 0, node,
1750 secondary_hold, reg);
1751
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001752 for (i = 0; (i < 100000000) &&
1753 (*acknowledge == ((unsigned long)-1)); i++ )
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001754 mb();
1755
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001756 if (*acknowledge == reg)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001757 prom_printf("done\n");
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001758 else
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001759 prom_printf("failed: %x\n", *acknowledge);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001760 }
1761#ifdef CONFIG_SMP
1762 else
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001763 prom_printf("boot cpu hw idx %lu\n", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001764#endif /* CONFIG_SMP */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001765 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001766
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001767 prom_debug("prom_hold_cpus: end...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001768}
1769
1770
1771static void __init prom_init_client_services(unsigned long pp)
1772{
1773 struct prom_t *_prom = &RELOC(prom);
1774
1775 /* Get a handle to the prom entry point before anything else */
1776 RELOC(prom_entry) = pp;
1777
1778 /* get a handle for the stdout device */
1779 _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1780 if (!PHANDLE_VALID(_prom->chosen))
1781 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1782
1783 /* get device tree root */
1784 _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1785 if (!PHANDLE_VALID(_prom->root))
1786 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001787
1788 _prom->mmumap = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001789}
1790
Paul Mackerrasa575b802005-10-23 17:23:21 +10001791#ifdef CONFIG_PPC32
1792/*
1793 * For really old powermacs, we need to map things we claim.
1794 * For that, we need the ihandle of the mmu.
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001795 * Also, on the longtrail, we need to work around other bugs.
Paul Mackerrasa575b802005-10-23 17:23:21 +10001796 */
1797static void __init prom_find_mmu(void)
1798{
1799 struct prom_t *_prom = &RELOC(prom);
1800 phandle oprom;
1801 char version[64];
1802
1803 oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1804 if (!PHANDLE_VALID(oprom))
1805 return;
1806 if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1807 return;
1808 version[sizeof(version) - 1] = 0;
Paul Mackerrasa575b802005-10-23 17:23:21 +10001809 /* XXX might need to add other versions here */
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001810 if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1811 of_workarounds = OF_WA_CLAIM;
1812 else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1813 of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1814 call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1815 } else
Paul Mackerrasa575b802005-10-23 17:23:21 +10001816 return;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001817 _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
Paul Mackerrasa575b802005-10-23 17:23:21 +10001818 prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1819 sizeof(_prom->mmumap));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001820 if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1821 of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001822}
1823#else
1824#define prom_find_mmu()
1825#endif
1826
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001827static void __init prom_init_stdout(void)
1828{
1829 struct prom_t *_prom = &RELOC(prom);
1830 char *path = RELOC(of_stdout_device);
1831 char type[16];
1832 u32 val;
1833
1834 if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1835 prom_panic("cannot find stdout");
1836
1837 _prom->stdout = val;
1838
1839 /* Get the full OF pathname of the stdout device */
1840 memset(path, 0, 256);
1841 call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1842 val = call_prom("instance-to-package", 1, 1, _prom->stdout);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001843 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1844 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001845 prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001846 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1847 path, strlen(path) + 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001848
1849 /* If it's a display, note it */
1850 memset(type, 0, sizeof(type));
1851 prom_getprop(val, "device_type", type, sizeof(type));
1852 if (strcmp(type, RELOC("display")) == 0)
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001853 prom_setprop(val, path, "linux,boot-display", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001854}
1855
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001856static int __init prom_find_machine_type(void)
1857{
1858 struct prom_t *_prom = &RELOC(prom);
1859 char compat[256];
1860 int len, i = 0;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001861#ifdef CONFIG_PPC64
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001862 phandle rtas;
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001863 int x;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001864#endif
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001865
1866 /* Look for a PowerMac */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001867 len = prom_getprop(_prom->root, "compatible",
1868 compat, sizeof(compat)-1);
1869 if (len > 0) {
1870 compat[len] = 0;
1871 while (i < len) {
1872 char *p = &compat[i];
1873 int sl = strlen(p);
1874 if (sl == 0)
1875 break;
1876 if (strstr(p, RELOC("Power Macintosh")) ||
Paul Mackerrasa575b802005-10-23 17:23:21 +10001877 strstr(p, RELOC("MacRISC")))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001878 return PLATFORM_POWERMAC;
Arnd Bergmann133dda12006-06-07 12:04:18 +10001879#ifdef CONFIG_PPC64
1880 /* We must make sure we don't detect the IBM Cell
1881 * blades as pSeries due to some firmware issues,
1882 * so we do it here.
1883 */
1884 if (strstr(p, RELOC("IBM,CBEA")) ||
1885 strstr(p, RELOC("IBM,CPBW-1.0")))
1886 return PLATFORM_GENERIC;
1887#endif /* CONFIG_PPC64 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001888 i += sl + 1;
1889 }
1890 }
1891#ifdef CONFIG_PPC64
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001892 /* If not a mac, try to figure out if it's an IBM pSeries or any other
1893 * PAPR compliant platform. We assume it is if :
1894 * - /device_type is "chrp" (please, do NOT use that for future
1895 * non-IBM designs !
1896 * - it has /rtas
1897 */
Michael Ellerman6f806ce2006-04-07 13:56:21 +10001898 len = prom_getprop(_prom->root, "device_type",
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001899 compat, sizeof(compat)-1);
1900 if (len <= 0)
1901 return PLATFORM_GENERIC;
Benjamin Herrenschmidtcb6b2eb2006-05-15 15:46:03 +10001902 if (strcmp(compat, RELOC("chrp")))
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001903 return PLATFORM_GENERIC;
1904
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001905 /* Default to pSeries. We need to know if we are running LPAR */
1906 rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001907 if (!PHANDLE_VALID(rtas))
1908 return PLATFORM_GENERIC;
1909 x = prom_getproplen(rtas, "ibm,hypertas-functions");
1910 if (x != PROM_ERROR) {
Anton Blanchard4da727a2009-03-31 20:06:14 +00001911 prom_debug("Hypertas detected, assuming LPAR !\n");
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001912 return PLATFORM_PSERIES_LPAR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001913 }
1914 return PLATFORM_PSERIES;
1915#else
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001916 return PLATFORM_GENERIC;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001917#endif
1918}
1919
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001920static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1921{
1922 return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1923}
1924
1925/*
1926 * If we have a display that we don't know how to drive,
1927 * we will want to try to execute OF's open method for it
1928 * later. However, OF will probably fall over if we do that
1929 * we've taken over the MMU.
1930 * So we check whether we will need to open the display,
1931 * and if so, open it now.
1932 */
1933static void __init prom_check_displays(void)
1934{
1935 char type[16], *path;
1936 phandle node;
1937 ihandle ih;
1938 int i;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001939
1940 static unsigned char default_colors[] = {
1941 0x00, 0x00, 0x00,
1942 0x00, 0x00, 0xaa,
1943 0x00, 0xaa, 0x00,
1944 0x00, 0xaa, 0xaa,
1945 0xaa, 0x00, 0x00,
1946 0xaa, 0x00, 0xaa,
1947 0xaa, 0xaa, 0x00,
1948 0xaa, 0xaa, 0xaa,
1949 0x55, 0x55, 0x55,
1950 0x55, 0x55, 0xff,
1951 0x55, 0xff, 0x55,
1952 0x55, 0xff, 0xff,
1953 0xff, 0x55, 0x55,
1954 0xff, 0x55, 0xff,
1955 0xff, 0xff, 0x55,
1956 0xff, 0xff, 0xff
1957 };
1958 const unsigned char *clut;
1959
Anton Blanchard4da727a2009-03-31 20:06:14 +00001960 prom_debug("Looking for displays\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001961 for (node = 0; prom_next_node(&node); ) {
1962 memset(type, 0, sizeof(type));
1963 prom_getprop(node, "device_type", type, sizeof(type));
1964 if (strcmp(type, RELOC("display")) != 0)
1965 continue;
1966
1967 /* It seems OF doesn't null-terminate the path :-( */
1968 path = RELOC(prom_scratch);
1969 memset(path, 0, PROM_SCRATCH_SIZE);
1970
1971 /*
1972 * leave some room at the end of the path for appending extra
1973 * arguments
1974 */
1975 if (call_prom("package-to-path", 3, 1, node, path,
1976 PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1977 continue;
Anton Blanchard1f8737a2009-03-31 20:06:15 +00001978 prom_printf("found display : %s, opening... ", path);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001979
1980 ih = call_prom("open", 1, 1, path);
1981 if (ih == 0) {
1982 prom_printf("failed\n");
1983 continue;
1984 }
1985
1986 /* Success */
1987 prom_printf("done\n");
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001988 prom_setprop(node, path, "linux,opened", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001989
1990 /* Setup a usable color table when the appropriate
1991 * method is available. Should update this to set-colors */
1992 clut = RELOC(default_colors);
1993 for (i = 0; i < 32; i++, clut += 3)
1994 if (prom_set_color(ih, i, clut[0], clut[1],
1995 clut[2]) != 0)
1996 break;
1997
1998#ifdef CONFIG_LOGO_LINUX_CLUT224
1999 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
2000 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
2001 if (prom_set_color(ih, i + 32, clut[0], clut[1],
2002 clut[2]) != 0)
2003 break;
2004#endif /* CONFIG_LOGO_LINUX_CLUT224 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002005 }
2006}
2007
2008
2009/* Return (relocated) pointer to this much memory: moves initrd if reqd. */
2010static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
2011 unsigned long needed, unsigned long align)
2012{
2013 void *ret;
2014
2015 *mem_start = _ALIGN(*mem_start, align);
2016 while ((*mem_start + needed) > *mem_end) {
2017 unsigned long room, chunk;
2018
2019 prom_debug("Chunk exhausted, claiming more at %x...\n",
2020 RELOC(alloc_bottom));
2021 room = RELOC(alloc_top) - RELOC(alloc_bottom);
2022 if (room > DEVTREE_CHUNK_SIZE)
2023 room = DEVTREE_CHUNK_SIZE;
2024 if (room < PAGE_SIZE)
Anton Blanchardfbafd722011-07-25 20:47:51 +00002025 prom_panic("No memory for flatten_device_tree "
2026 "(no room)\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002027 chunk = alloc_up(room, 0);
2028 if (chunk == 0)
Anton Blanchardfbafd722011-07-25 20:47:51 +00002029 prom_panic("No memory for flatten_device_tree "
2030 "(claim failed)\n");
Anton Blanchard966728d2011-07-25 20:47:07 +00002031 *mem_end = chunk + room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002032 }
2033
2034 ret = (void *)*mem_start;
2035 *mem_start += needed;
2036
2037 return ret;
2038}
2039
2040#define dt_push_token(token, mem_start, mem_end) \
2041 do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
2042
2043static unsigned long __init dt_find_string(char *str)
2044{
2045 char *s, *os;
2046
2047 s = os = (char *)RELOC(dt_string_start);
2048 s += 4;
2049 while (s < (char *)RELOC(dt_string_end)) {
2050 if (strcmp(s, str) == 0)
2051 return s - os;
2052 s += strlen(s) + 1;
2053 }
2054 return 0;
2055}
2056
2057/*
2058 * The Open Firmware 1275 specification states properties must be 31 bytes or
2059 * less, however not all firmwares obey this. Make it 64 bytes to be safe.
2060 */
2061#define MAX_PROPERTY_NAME 64
2062
2063static void __init scan_dt_build_strings(phandle node,
2064 unsigned long *mem_start,
2065 unsigned long *mem_end)
2066{
2067 char *prev_name, *namep, *sstart;
2068 unsigned long soff;
2069 phandle child;
2070
2071 sstart = (char *)RELOC(dt_string_start);
2072
2073 /* get and store all property names */
2074 prev_name = RELOC("");
2075 for (;;) {
2076 /* 64 is max len of name including nul. */
2077 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
2078 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
2079 /* No more nodes: unwind alloc */
2080 *mem_start = (unsigned long)namep;
2081 break;
2082 }
2083
2084 /* skip "name" */
2085 if (strcmp(namep, RELOC("name")) == 0) {
2086 *mem_start = (unsigned long)namep;
2087 prev_name = RELOC("name");
2088 continue;
2089 }
2090 /* get/create string entry */
2091 soff = dt_find_string(namep);
2092 if (soff != 0) {
2093 *mem_start = (unsigned long)namep;
2094 namep = sstart + soff;
2095 } else {
2096 /* Trim off some if we can */
2097 *mem_start = (unsigned long)namep + strlen(namep) + 1;
2098 RELOC(dt_string_end) = *mem_start;
2099 }
2100 prev_name = namep;
2101 }
2102
2103 /* do all our children */
2104 child = call_prom("child", 1, 1, node);
2105 while (child != 0) {
2106 scan_dt_build_strings(child, mem_start, mem_end);
2107 child = call_prom("peer", 1, 1, child);
2108 }
2109}
2110
2111static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
2112 unsigned long *mem_end)
2113{
2114 phandle child;
2115 char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
2116 unsigned long soff;
2117 unsigned char *valp;
2118 static char pname[MAX_PROPERTY_NAME];
Paul Mackerrasc49888202005-10-26 21:52:53 +10002119 int l, room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002120
2121 dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
2122
2123 /* get the node's full name */
2124 namep = (char *)*mem_start;
Paul Mackerrasc49888202005-10-26 21:52:53 +10002125 room = *mem_end - *mem_start;
2126 if (room > 255)
2127 room = 255;
2128 l = call_prom("package-to-path", 3, 1, node, namep, room);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002129 if (l >= 0) {
2130 /* Didn't fit? Get more room. */
Paul Mackerrasc49888202005-10-26 21:52:53 +10002131 if (l >= room) {
2132 if (l >= *mem_end - *mem_start)
2133 namep = make_room(mem_start, mem_end, l+1, 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002134 call_prom("package-to-path", 3, 1, node, namep, l);
2135 }
2136 namep[l] = '\0';
2137
2138 /* Fixup an Apple bug where they have bogus \0 chars in the
Paul Mackerrasa575b802005-10-23 17:23:21 +10002139 * middle of the path in some properties, and extract
2140 * the unit name (everything after the last '/').
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002141 */
Paul Mackerrasa575b802005-10-23 17:23:21 +10002142 for (lp = p = namep, ep = namep + l; p < ep; p++) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002143 if (*p == '/')
Paul Mackerrasa575b802005-10-23 17:23:21 +10002144 lp = namep;
2145 else if (*p != 0)
2146 *lp++ = *p;
2147 }
2148 *lp = 0;
2149 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002150 }
2151
2152 /* get it again for debugging */
2153 path = RELOC(prom_scratch);
2154 memset(path, 0, PROM_SCRATCH_SIZE);
2155 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
2156
2157 /* get and store all properties */
2158 prev_name = RELOC("");
2159 sstart = (char *)RELOC(dt_string_start);
2160 for (;;) {
2161 if (call_prom("nextprop", 3, 1, node, prev_name,
2162 RELOC(pname)) != 1)
2163 break;
2164
2165 /* skip "name" */
2166 if (strcmp(RELOC(pname), RELOC("name")) == 0) {
2167 prev_name = RELOC("name");
2168 continue;
2169 }
2170
2171 /* find string offset */
2172 soff = dt_find_string(RELOC(pname));
2173 if (soff == 0) {
2174 prom_printf("WARNING: Can't find string index for"
2175 " <%s>, node %s\n", RELOC(pname), path);
2176 break;
2177 }
2178 prev_name = sstart + soff;
2179
2180 /* get length */
2181 l = call_prom("getproplen", 2, 1, node, RELOC(pname));
2182
2183 /* sanity checks */
2184 if (l == PROM_ERROR)
2185 continue;
2186 if (l > MAX_PROPERTY_LENGTH) {
2187 prom_printf("WARNING: ignoring large property ");
2188 /* It seems OF doesn't null-terminate the path :-( */
2189 prom_printf("[%s] ", path);
2190 prom_printf("%s length 0x%x\n", RELOC(pname), l);
2191 continue;
2192 }
2193
2194 /* push property head */
2195 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2196 dt_push_token(l, mem_start, mem_end);
2197 dt_push_token(soff, mem_start, mem_end);
2198
2199 /* push property content */
2200 valp = make_room(mem_start, mem_end, l, 4);
2201 call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
2202 *mem_start = _ALIGN(*mem_start, 4);
2203 }
2204
2205 /* Add a "linux,phandle" property. */
2206 soff = dt_find_string(RELOC("linux,phandle"));
2207 if (soff == 0)
2208 prom_printf("WARNING: Can't find string index for"
2209 " <linux-phandle> node %s\n", path);
2210 else {
2211 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2212 dt_push_token(4, mem_start, mem_end);
2213 dt_push_token(soff, mem_start, mem_end);
2214 valp = make_room(mem_start, mem_end, 4, 4);
2215 *(u32 *)valp = node;
2216 }
2217
2218 /* do all our children */
2219 child = call_prom("child", 1, 1, node);
2220 while (child != 0) {
2221 scan_dt_build_struct(child, mem_start, mem_end);
2222 child = call_prom("peer", 1, 1, child);
2223 }
2224
2225 dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
2226}
2227
2228static void __init flatten_device_tree(void)
2229{
2230 phandle root;
2231 unsigned long mem_start, mem_end, room;
2232 struct boot_param_header *hdr;
2233 struct prom_t *_prom = &RELOC(prom);
2234 char *namep;
2235 u64 *rsvmap;
2236
2237 /*
2238 * Check how much room we have between alloc top & bottom (+/- a
Anton Blanchardfbafd722011-07-25 20:47:51 +00002239 * few pages), crop to 1MB, as this is our "chunk" size
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002240 */
2241 room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
2242 if (room > DEVTREE_CHUNK_SIZE)
2243 room = DEVTREE_CHUNK_SIZE;
2244 prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
2245
2246 /* Now try to claim that */
2247 mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
2248 if (mem_start == 0)
2249 prom_panic("Can't allocate initial device-tree chunk\n");
Anton Blanchard966728d2011-07-25 20:47:07 +00002250 mem_end = mem_start + room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002251
2252 /* Get root of tree */
2253 root = call_prom("peer", 1, 1, (phandle)0);
2254 if (root == (phandle)0)
2255 prom_panic ("couldn't get device tree root\n");
2256
2257 /* Build header and make room for mem rsv map */
2258 mem_start = _ALIGN(mem_start, 4);
2259 hdr = make_room(&mem_start, &mem_end,
2260 sizeof(struct boot_param_header), 4);
2261 RELOC(dt_header_start) = (unsigned long)hdr;
2262 rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
2263
2264 /* Start of strings */
2265 mem_start = PAGE_ALIGN(mem_start);
2266 RELOC(dt_string_start) = mem_start;
2267 mem_start += 4; /* hole */
2268
2269 /* Add "linux,phandle" in there, we'll need it */
2270 namep = make_room(&mem_start, &mem_end, 16, 1);
2271 strcpy(namep, RELOC("linux,phandle"));
2272 mem_start = (unsigned long)namep + strlen(namep) + 1;
2273
2274 /* Build string array */
2275 prom_printf("Building dt strings...\n");
2276 scan_dt_build_strings(root, &mem_start, &mem_end);
2277 RELOC(dt_string_end) = mem_start;
2278
2279 /* Build structure */
2280 mem_start = PAGE_ALIGN(mem_start);
2281 RELOC(dt_struct_start) = mem_start;
2282 prom_printf("Building dt structure...\n");
2283 scan_dt_build_struct(root, &mem_start, &mem_end);
2284 dt_push_token(OF_DT_END, &mem_start, &mem_end);
2285 RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
2286
2287 /* Finish header */
2288 hdr->boot_cpuid_phys = _prom->cpu;
2289 hdr->magic = OF_DT_HEADER;
2290 hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
2291 hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
2292 hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
2293 hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
2294 hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
2295 hdr->version = OF_DT_VERSION;
2296 /* Version 16 is not backward compatible */
2297 hdr->last_comp_version = 0x10;
2298
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05002299 /* Copy the reserve map in */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002300 memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
2301
2302#ifdef DEBUG_PROM
2303 {
2304 int i;
2305 prom_printf("reserved memory map:\n");
2306 for (i = 0; i < RELOC(mem_reserve_cnt); i++)
2307 prom_printf(" %x - %x\n",
2308 RELOC(mem_reserve_map)[i].base,
2309 RELOC(mem_reserve_map)[i].size);
2310 }
2311#endif
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05002312 /* Bump mem_reserve_cnt to cause further reservations to fail
2313 * since it's too late.
2314 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002315 RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
2316
2317 prom_printf("Device tree strings 0x%x -> 0x%x\n",
2318 RELOC(dt_string_start), RELOC(dt_string_end));
2319 prom_printf("Device tree struct 0x%x -> 0x%x\n",
2320 RELOC(dt_struct_start), RELOC(dt_struct_end));
2321
2322}
2323
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002324#ifdef CONFIG_PPC_MAPLE
2325/* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2326 * The values are bad, and it doesn't even have the right number of cells. */
2327static void __init fixup_device_tree_maple(void)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002328{
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002329 phandle isa;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002330 u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002331 u32 isa_ranges[6];
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002332 char *name;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002333
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002334 name = "/ht@0/isa@4";
2335 isa = call_prom("finddevice", 1, 1, ADDR(name));
2336 if (!PHANDLE_VALID(isa)) {
2337 name = "/ht@0/isa@6";
2338 isa = call_prom("finddevice", 1, 1, ADDR(name));
2339 rloc = 0x01003000; /* IO space; PCI device = 6 */
2340 }
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002341 if (!PHANDLE_VALID(isa))
2342 return;
2343
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002344 if (prom_getproplen(isa, "ranges") != 12)
2345 return;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002346 if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2347 == PROM_ERROR)
2348 return;
2349
2350 if (isa_ranges[0] != 0x1 ||
2351 isa_ranges[1] != 0xf4000000 ||
2352 isa_ranges[2] != 0x00010000)
2353 return;
2354
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002355 prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002356
2357 isa_ranges[0] = 0x1;
2358 isa_ranges[1] = 0x0;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002359 isa_ranges[2] = rloc;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002360 isa_ranges[3] = 0x0;
2361 isa_ranges[4] = 0x0;
2362 isa_ranges[5] = 0x00010000;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002363 prom_setprop(isa, name, "ranges",
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002364 isa_ranges, sizeof(isa_ranges));
2365}
Harry Ciao8f101a02009-06-17 16:28:00 -07002366
2367#define CPC925_MC_START 0xf8000000
2368#define CPC925_MC_LENGTH 0x1000000
2369/* The values for memory-controller don't have right number of cells */
2370static void __init fixup_device_tree_maple_memory_controller(void)
2371{
2372 phandle mc;
2373 u32 mc_reg[4];
2374 char *name = "/hostbridge@f8000000";
2375 struct prom_t *_prom = &RELOC(prom);
2376 u32 ac, sc;
2377
2378 mc = call_prom("finddevice", 1, 1, ADDR(name));
2379 if (!PHANDLE_VALID(mc))
2380 return;
2381
2382 if (prom_getproplen(mc, "reg") != 8)
2383 return;
2384
2385 prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
2386 prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
2387 if ((ac != 2) || (sc != 2))
2388 return;
2389
2390 if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
2391 return;
2392
2393 if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
2394 return;
2395
2396 prom_printf("Fixing up bogus hostbridge on Maple...\n");
2397
2398 mc_reg[0] = 0x0;
2399 mc_reg[1] = CPC925_MC_START;
2400 mc_reg[2] = 0x0;
2401 mc_reg[3] = CPC925_MC_LENGTH;
2402 prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
2403}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002404#else
2405#define fixup_device_tree_maple()
Harry Ciao8f101a02009-06-17 16:28:00 -07002406#define fixup_device_tree_maple_memory_controller()
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002407#endif
2408
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002409#ifdef CONFIG_PPC_CHRP
Olaf Heringe4805922007-04-04 18:20:04 +02002410/*
2411 * Pegasos and BriQ lacks the "ranges" property in the isa node
2412 * Pegasos needs decimal IRQ 14/15, not hexadecimal
Olaf Hering556ecf92007-08-18 04:27:17 +10002413 * Pegasos has the IDE configured in legacy mode, but advertised as native
Olaf Heringe4805922007-04-04 18:20:04 +02002414 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002415static void __init fixup_device_tree_chrp(void)
2416{
Olaf Heringe4805922007-04-04 18:20:04 +02002417 phandle ph;
2418 u32 prop[6];
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002419 u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002420 char *name;
2421 int rc;
2422
2423 name = "/pci@80000000/isa@c";
Olaf Heringe4805922007-04-04 18:20:04 +02002424 ph = call_prom("finddevice", 1, 1, ADDR(name));
2425 if (!PHANDLE_VALID(ph)) {
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002426 name = "/pci@ff500000/isa@6";
Olaf Heringe4805922007-04-04 18:20:04 +02002427 ph = call_prom("finddevice", 1, 1, ADDR(name));
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002428 rloc = 0x01003000; /* IO space; PCI device = 6 */
2429 }
Olaf Heringe4805922007-04-04 18:20:04 +02002430 if (PHANDLE_VALID(ph)) {
2431 rc = prom_getproplen(ph, "ranges");
2432 if (rc == 0 || rc == PROM_ERROR) {
2433 prom_printf("Fixing up missing ISA range on Pegasos...\n");
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002434
Olaf Heringe4805922007-04-04 18:20:04 +02002435 prop[0] = 0x1;
2436 prop[1] = 0x0;
2437 prop[2] = rloc;
2438 prop[3] = 0x0;
2439 prop[4] = 0x0;
2440 prop[5] = 0x00010000;
2441 prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2442 }
2443 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002444
Olaf Heringe4805922007-04-04 18:20:04 +02002445 name = "/pci@80000000/ide@C,1";
2446 ph = call_prom("finddevice", 1, 1, ADDR(name));
2447 if (PHANDLE_VALID(ph)) {
2448 prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2449 prop[0] = 14;
2450 prop[1] = 0x0;
Olaf Hering556ecf92007-08-18 04:27:17 +10002451 prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2452 prom_printf("Fixing up IDE class-code on Pegasos...\n");
2453 rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2454 if (rc == sizeof(u32)) {
2455 prop[0] &= ~0x5;
2456 prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2457 }
Olaf Heringe4805922007-04-04 18:20:04 +02002458 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002459}
2460#else
2461#define fixup_device_tree_chrp()
2462#endif
2463
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002464#if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002465static void __init fixup_device_tree_pmac(void)
2466{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002467 phandle u3, i2c, mpic;
2468 u32 u3_rev;
2469 u32 interrupts[2];
2470 u32 parent;
2471
2472 /* Some G5s have a missing interrupt definition, fix it up here */
2473 u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2474 if (!PHANDLE_VALID(u3))
2475 return;
2476 i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2477 if (!PHANDLE_VALID(i2c))
2478 return;
2479 mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2480 if (!PHANDLE_VALID(mpic))
2481 return;
2482
2483 /* check if proper rev of u3 */
2484 if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2485 == PROM_ERROR)
2486 return;
Benjamin Herrenschmidt7d496972005-11-07 14:36:21 +11002487 if (u3_rev < 0x35 || u3_rev > 0x39)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002488 return;
2489 /* does it need fixup ? */
2490 if (prom_getproplen(i2c, "interrupts") > 0)
2491 return;
2492
2493 prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2494
2495 /* interrupt on this revision of u3 is number 0 and level */
2496 interrupts[0] = 0;
2497 interrupts[1] = 1;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002498 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2499 &interrupts, sizeof(interrupts));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002500 parent = (u32)mpic;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002501 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2502 &parent, sizeof(parent));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002503}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002504#else
2505#define fixup_device_tree_pmac()
2506#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002507
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002508#ifdef CONFIG_PPC_EFIKA
Grant Likely94d2dde2008-01-24 22:25:32 -07002509/*
2510 * The MPC5200 FEC driver requires an phy-handle property to tell it how
2511 * to talk to the phy. If the phy-handle property is missing, then this
2512 * function is called to add the appropriate nodes and link it to the
2513 * ethernet node.
2514 */
2515static void __init fixup_device_tree_efika_add_phy(void)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002516{
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002517 u32 node;
2518 char prop[64];
Grant Likely94d2dde2008-01-24 22:25:32 -07002519 int rv;
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002520
Grant Likely94d2dde2008-01-24 22:25:32 -07002521 /* Check if /builtin/ethernet exists - bail if it doesn't */
2522 node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002523 if (!PHANDLE_VALID(node))
2524 return;
2525
Grant Likely94d2dde2008-01-24 22:25:32 -07002526 /* Check if the phy-handle property exists - bail if it does */
2527 rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2528 if (!rv)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002529 return;
2530
Grant Likely94d2dde2008-01-24 22:25:32 -07002531 /*
2532 * At this point the ethernet device doesn't have a phy described.
2533 * Now we need to add the missing phy node and linkage
2534 */
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002535
Grant Likely94d2dde2008-01-24 22:25:32 -07002536 /* Check for an MDIO bus node - if missing then create one */
Olaf Hering6f4347c2008-01-10 01:06:08 +11002537 node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2538 if (!PHANDLE_VALID(node)) {
2539 prom_printf("Adding Ethernet MDIO node\n");
2540 call_prom("interpret", 1, 1,
2541 " s\" /builtin\" find-device"
2542 " new-device"
2543 " 1 encode-int s\" #address-cells\" property"
2544 " 0 encode-int s\" #size-cells\" property"
Grant Likely94d2dde2008-01-24 22:25:32 -07002545 " s\" mdio\" device-name"
2546 " s\" fsl,mpc5200b-mdio\" encode-string"
Olaf Hering6f4347c2008-01-10 01:06:08 +11002547 " s\" compatible\" property"
2548 " 0xf0003000 0x400 reg"
2549 " 0x2 encode-int"
2550 " 0x5 encode-int encode+"
2551 " 0x3 encode-int encode+"
2552 " s\" interrupts\" property"
2553 " finish-device");
2554 };
2555
Grant Likely94d2dde2008-01-24 22:25:32 -07002556 /* Check for a PHY device node - if missing then create one and
2557 * give it's phandle to the ethernet node */
2558 node = call_prom("finddevice", 1, 1,
2559 ADDR("/builtin/mdio/ethernet-phy"));
Olaf Hering6f4347c2008-01-10 01:06:08 +11002560 if (!PHANDLE_VALID(node)) {
2561 prom_printf("Adding Ethernet PHY node\n");
2562 call_prom("interpret", 1, 1,
2563 " s\" /builtin/mdio\" find-device"
2564 " new-device"
2565 " s\" ethernet-phy\" device-name"
2566 " 0x10 encode-int s\" reg\" property"
2567 " my-self"
2568 " ihandle>phandle"
2569 " finish-device"
2570 " s\" /builtin/ethernet\" find-device"
2571 " encode-int"
2572 " s\" phy-handle\" property"
2573 " device-end");
2574 }
Grant Likely94d2dde2008-01-24 22:25:32 -07002575}
Olaf Hering6f4347c2008-01-10 01:06:08 +11002576
Grant Likely94d2dde2008-01-24 22:25:32 -07002577static void __init fixup_device_tree_efika(void)
2578{
2579 int sound_irq[3] = { 2, 2, 0 };
2580 int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2581 3,4,0, 3,5,0, 3,6,0, 3,7,0,
2582 3,8,0, 3,9,0, 3,10,0, 3,11,0,
2583 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2584 u32 node;
2585 char prop[64];
2586 int rv, len;
2587
2588 /* Check if we're really running on a EFIKA */
2589 node = call_prom("finddevice", 1, 1, ADDR("/"));
2590 if (!PHANDLE_VALID(node))
2591 return;
2592
2593 rv = prom_getprop(node, "model", prop, sizeof(prop));
2594 if (rv == PROM_ERROR)
2595 return;
2596 if (strcmp(prop, "EFIKA5K2"))
2597 return;
2598
2599 prom_printf("Applying EFIKA device tree fixups\n");
2600
2601 /* Claiming to be 'chrp' is death */
2602 node = call_prom("finddevice", 1, 1, ADDR("/"));
2603 rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2604 if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2605 prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2606
David Woodhouse7f4392c2008-04-14 02:52:38 +10002607 /* CODEGEN,description is exposed in /proc/cpuinfo so
2608 fix that too */
2609 rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2610 if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2611 prom_setprop(node, "/", "CODEGEN,description",
2612 "Efika 5200B PowerPC System",
2613 sizeof("Efika 5200B PowerPC System"));
2614
Grant Likely94d2dde2008-01-24 22:25:32 -07002615 /* Fixup bestcomm interrupts property */
2616 node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2617 if (PHANDLE_VALID(node)) {
2618 len = prom_getproplen(node, "interrupts");
2619 if (len == 12) {
2620 prom_printf("Fixing bestcomm interrupts property\n");
2621 prom_setprop(node, "/builtin/bestcom", "interrupts",
2622 bcomm_irq, sizeof(bcomm_irq));
2623 }
2624 }
2625
2626 /* Fixup sound interrupts property */
2627 node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2628 if (PHANDLE_VALID(node)) {
2629 rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2630 if (rv == PROM_ERROR) {
2631 prom_printf("Adding sound interrupts property\n");
2632 prom_setprop(node, "/builtin/sound", "interrupts",
2633 sound_irq, sizeof(sound_irq));
2634 }
2635 }
2636
2637 /* Make sure ethernet phy-handle property exists */
2638 fixup_device_tree_efika_add_phy();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002639}
2640#else
2641#define fixup_device_tree_efika()
2642#endif
2643
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002644static void __init fixup_device_tree(void)
2645{
2646 fixup_device_tree_maple();
Harry Ciao8f101a02009-06-17 16:28:00 -07002647 fixup_device_tree_maple_memory_controller();
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002648 fixup_device_tree_chrp();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002649 fixup_device_tree_pmac();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002650 fixup_device_tree_efika();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002651}
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002652
2653static void __init prom_find_boot_cpu(void)
2654{
Linas Vepstase788ff12007-09-07 03:45:21 +10002655 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002656 u32 getprop_rval;
2657 ihandle prom_cpu;
2658 phandle cpu_pkg;
2659
Paul Mackerrasa575b802005-10-23 17:23:21 +10002660 _prom->cpu = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002661 if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
Paul Mackerrasa575b802005-10-23 17:23:21 +10002662 return;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002663
2664 cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2665
2666 prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
2667 _prom->cpu = getprop_rval;
2668
Michael Neuling2c48a7d2010-07-27 18:26:21 +00002669 prom_debug("Booting CPU hw index = %lu\n", _prom->cpu);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002670}
2671
2672static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2673{
2674#ifdef CONFIG_BLK_DEV_INITRD
Linas Vepstase788ff12007-09-07 03:45:21 +10002675 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002676
2677 if (r3 && r4 && r4 != 0xdeadbeef) {
2678 unsigned long val;
2679
Michael Ellerman51fae6de2005-12-04 18:39:15 +11002680 RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002681 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2682
2683 val = RELOC(prom_initrd_start);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002684 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2685 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002686 val = RELOC(prom_initrd_end);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002687 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2688 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002689
2690 reserve_mem(RELOC(prom_initrd_start),
2691 RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
2692
2693 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
2694 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
2695 }
2696#endif /* CONFIG_BLK_DEV_INITRD */
2697}
2698
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002699
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002700/*
2701 * We enter here early on, when the Open Firmware prom is still
2702 * handling exceptions and the MMU hash table for us.
2703 */
2704
2705unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2706 unsigned long pp,
Paul Mackerras549e8152008-08-30 11:43:47 +10002707 unsigned long r6, unsigned long r7,
2708 unsigned long kbase)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002709{
Linas Vepstase788ff12007-09-07 03:45:21 +10002710 struct prom_t *_prom;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002711 unsigned long hdr;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002712
2713#ifdef CONFIG_PPC32
Paul Mackerras549e8152008-08-30 11:43:47 +10002714 unsigned long offset = reloc_offset();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002715 reloc_got2(offset);
2716#endif
2717
2718 _prom = &RELOC(prom);
2719
2720 /*
2721 * First zero the BSS
2722 */
2723 memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
2724
2725 /*
2726 * Init interface to Open Firmware, get some node references,
2727 * like /chosen
2728 */
2729 prom_init_client_services(pp);
2730
2731 /*
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002732 * See if this OF is old enough that we need to do explicit maps
2733 * and other workarounds
2734 */
2735 prom_find_mmu();
2736
2737 /*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002738 * Init prom stdout device
2739 */
2740 prom_init_stdout();
2741
Benjamin Herrenschmidt151a9f42009-03-22 16:04:53 +00002742 prom_printf("Preparing to boot %s", RELOC(linux_banner));
Michael Ellermane7943fb2009-03-04 19:02:01 +00002743
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002744 /*
2745 * Get default machine type. At this point, we do not differentiate
2746 * between pSeries SMP and pSeries LPAR
2747 */
2748 RELOC(of_platform) = prom_find_machine_type();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002749
Paul Mackerras549e8152008-08-30 11:43:47 +10002750#ifndef CONFIG_RELOCATABLE
Olaf Heringadd60ef2006-03-23 22:03:57 +01002751 /* Bail if this is a kdump kernel. */
2752 if (PHYSICAL_START > 0)
2753 prom_panic("Error: You can't boot a kdump kernel from OF!\n");
Paul Mackerras549e8152008-08-30 11:43:47 +10002754#endif
Olaf Heringadd60ef2006-03-23 22:03:57 +01002755
2756 /*
2757 * Check for an initrd
2758 */
2759 prom_check_initrd(r3, r4);
2760
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002761#if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002762 /*
2763 * On pSeries, inform the firmware about our capabilities
2764 */
Paul Mackerras799d6042005-11-10 13:37:51 +11002765 if (RELOC(of_platform) == PLATFORM_PSERIES ||
2766 RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002767 prom_send_capabilities();
2768#endif
2769
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002770 /*
Arnd Bergmannf3f66f52005-10-31 20:08:37 -05002771 * Copy the CPU hold code
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002772 */
Linas Vepstase788ff12007-09-07 03:45:21 +10002773 if (RELOC(of_platform) != PLATFORM_POWERMAC)
Paul Mackerras549e8152008-08-30 11:43:47 +10002774 copy_and_flush(0, kbase, 0x100, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002775
2776 /*
2777 * Do early parsing of command line
2778 */
2779 early_cmdline_parse();
2780
2781 /*
2782 * Initialize memory management within prom_init
2783 */
2784 prom_init_mem();
2785
2786 /*
2787 * Determine which cpu is actually running right _now_
2788 */
2789 prom_find_boot_cpu();
2790
2791 /*
2792 * Initialize display devices
2793 */
2794 prom_check_displays();
2795
2796#ifdef CONFIG_PPC64
2797 /*
2798 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2799 * that uses the allocator, we need to make sure we get the top of memory
2800 * available for us here...
2801 */
2802 if (RELOC(of_platform) == PLATFORM_PSERIES)
2803 prom_initialize_tce_table();
2804#endif
2805
2806 /*
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002807 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
2808 * have a usable RTAS implementation.
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002809 */
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002810 if (RELOC(of_platform) != PLATFORM_POWERMAC)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002811 prom_instantiate_rtas();
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002812
2813#ifdef CONFIG_PPC_POWERNV
2814 /* Detect HAL and try instanciating it & doing takeover */
2815 if (RELOC(of_platform) == PLATFORM_PSERIES_LPAR) {
2816 prom_query_opal();
2817 if (RELOC(of_platform) == PLATFORM_OPAL) {
2818 prom_opal_hold_cpus();
2819 prom_opal_takeover();
2820 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002821 }
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +00002822#endif
2823
2824 /*
2825 * On non-powermacs, put all CPUs in spin-loops.
2826 *
2827 * PowerMacs use a different mechanism to spin CPUs
2828 */
2829 if (RELOC(of_platform) != PLATFORM_POWERMAC)
2830 prom_hold_cpus();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002831
2832 /*
2833 * Fill in some infos for use by the kernel later on
2834 */
Benjamin Krillcf687872009-07-27 22:02:39 +00002835 if (RELOC(prom_memory_limit))
2836 prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
2837 &RELOC(prom_memory_limit),
2838 sizeof(prom_memory_limit));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002839#ifdef CONFIG_PPC64
Jeremy Kerr165785e2006-11-11 17:25:18 +11002840 if (RELOC(prom_iommu_off))
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002841 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2842 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002843
Jeremy Kerr165785e2006-11-11 17:25:18 +11002844 if (RELOC(prom_iommu_force_on))
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002845 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2846 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002847
2848 if (RELOC(prom_tce_alloc_start)) {
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002849 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002850 &RELOC(prom_tce_alloc_start),
2851 sizeof(prom_tce_alloc_start));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002852 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002853 &RELOC(prom_tce_alloc_end),
2854 sizeof(prom_tce_alloc_end));
2855 }
2856#endif
2857
2858 /*
2859 * Fixup any known bugs in the device-tree
2860 */
2861 fixup_device_tree();
2862
2863 /*
2864 * Now finally create the flattened device-tree
2865 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00002866 prom_printf("copying OF device tree...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002867 flatten_device_tree();
2868
Paul Mackerras3825ac02005-11-08 22:48:08 +11002869 /*
2870 * in case stdin is USB and still active on IBM machines...
2871 * Unfortunately quiesce crashes on some powermacs if we have
2872 * closed stdin already (in particular the powerbook 101).
2873 */
2874 if (RELOC(of_platform) != PLATFORM_POWERMAC)
2875 prom_close_stdin();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002876
2877 /*
2878 * Call OF "quiesce" method to shut down pending DMA's from
2879 * devices etc...
2880 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00002881 prom_printf("Calling quiesce...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002882 call_prom("quiesce", 0, 0);
2883
2884 /*
2885 * And finally, call the kernel passing it the flattened device
2886 * tree and NULL as r5, thus triggering the new entry point which
2887 * is common to us and kexec
2888 */
2889 hdr = RELOC(dt_header_start);
2890 prom_printf("returning from prom_init\n");
2891 prom_debug("->dt_header_start=0x%x\n", hdr);
2892
2893#ifdef CONFIG_PPC32
2894 reloc_got2(-offset);
2895#endif
2896
Paul Mackerras549e8152008-08-30 11:43:47 +10002897 __start(hdr, kbase, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002898
2899 return 0;
2900}