blob: 7839bd7bfd1587f9a621d3fe8dc876e3a0f0af19 [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>
46
Paul Mackerras9b6b5632005-10-06 12:06:20 +100047#include <linux/linux_logo.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100048
49/*
50 * Properties whose value is longer than this get excluded from our
51 * copy of the device tree. This value does need to be big enough to
52 * ensure that we don't lose things like the interrupt-map property
53 * on a PCI-PCI bridge.
54 */
55#define MAX_PROPERTY_LENGTH (1UL * 1024 * 1024)
56
57/*
58 * Eventually bump that one up
59 */
60#define DEVTREE_CHUNK_SIZE 0x100000
61
62/*
63 * This is the size of the local memory reserve map that gets copied
64 * into the boot params passed to the kernel. That size is totally
65 * flexible as the kernel just reads the list until it encounters an
66 * entry with size 0, so it can be changed without breaking binary
67 * compatibility
68 */
69#define MEM_RESERVE_MAP_SIZE 8
70
71/*
72 * prom_init() is called very early on, before the kernel text
73 * and data have been mapped to KERNELBASE. At this point the code
74 * is running at whatever address it has been loaded at.
75 * On ppc32 we compile with -mrelocatable, which means that references
76 * to extern and static variables get relocated automatically.
77 * On ppc64 we have to relocate the references explicitly with
78 * RELOC. (Note that strings count as static variables.)
79 *
80 * Because OF may have mapped I/O devices into the area starting at
81 * KERNELBASE, particularly on CHRP machines, we can't safely call
82 * OF once the kernel has been mapped to KERNELBASE. Therefore all
83 * OF calls must be done within prom_init().
84 *
85 * ADDR is used in calls to call_prom. The 4th and following
86 * arguments to call_prom should be 32-bit values.
87 * On ppc64, 64 bit values are truncated to 32 bits (and
88 * fortunately don't get interpreted as two arguments).
89 */
90#ifdef CONFIG_PPC64
91#define RELOC(x) (*PTRRELOC(&(x)))
92#define ADDR(x) (u32) add_reloc_offset((unsigned long)(x))
Paul Mackerrasa23414b2005-11-10 12:00:55 +110093#define OF_WORKAROUNDS 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +100094#else
95#define RELOC(x) (x)
96#define ADDR(x) (u32) (x)
Paul Mackerrasa23414b2005-11-10 12:00:55 +110097#define OF_WORKAROUNDS of_workarounds
98int of_workarounds;
Paul Mackerras9b6b5632005-10-06 12:06:20 +100099#endif
100
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100101#define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */
102#define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */
103
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000104#define PROM_BUG() do { \
105 prom_printf("kernel BUG at %s line 0x%x!\n", \
106 RELOC(__FILE__), __LINE__); \
107 __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
108} while (0)
109
110#ifdef DEBUG_PROM
111#define prom_debug(x...) prom_printf(x)
112#else
113#define prom_debug(x...)
114#endif
115
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000116
117typedef u32 prom_arg_t;
118
119struct prom_args {
120 u32 service;
121 u32 nargs;
122 u32 nret;
123 prom_arg_t args[10];
124};
125
126struct prom_t {
127 ihandle root;
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100128 phandle chosen;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000129 int cpu;
130 ihandle stdout;
Paul Mackerrasa575b802005-10-23 17:23:21 +1000131 ihandle mmumap;
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100132 ihandle memory;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000133};
134
135struct mem_map_entry {
Kumar Galacbbcf342006-01-11 17:57:13 -0600136 u64 base;
137 u64 size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000138};
139
140typedef u32 cell_t;
141
142extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
143
144#ifdef CONFIG_PPC64
Paul Mackerrasc49888202005-10-26 21:52:53 +1000145extern int enter_prom(struct prom_args *args, unsigned long entry);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000146#else
Paul Mackerrasc49888202005-10-26 21:52:53 +1000147static inline int enter_prom(struct prom_args *args, unsigned long entry)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000148{
Paul Mackerrasc49888202005-10-26 21:52:53 +1000149 return ((int (*)(struct prom_args *))entry)(args);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000150}
151#endif
152
153extern void copy_and_flush(unsigned long dest, unsigned long src,
154 unsigned long size, unsigned long offset);
155
156/* prom structure */
157static struct prom_t __initdata prom;
158
159static unsigned long prom_entry __initdata;
160
161#define PROM_SCRATCH_SIZE 256
162
163static char __initdata of_stdout_device[256];
164static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
165
166static unsigned long __initdata dt_header_start;
167static unsigned long __initdata dt_struct_start, dt_struct_end;
168static unsigned long __initdata dt_string_start, dt_string_end;
169
170static unsigned long __initdata prom_initrd_start, prom_initrd_end;
171
172#ifdef CONFIG_PPC64
Jeremy Kerr165785e2006-11-11 17:25:18 +1100173static int __initdata prom_iommu_force_on;
174static int __initdata prom_iommu_off;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000175static unsigned long __initdata prom_tce_alloc_start;
176static unsigned long __initdata prom_tce_alloc_end;
177#endif
178
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100179/* Platforms codes are now obsolete in the kernel. Now only used within this
180 * file and ultimately gone too. Feel free to change them if you need, they
181 * are not shared with anything outside of this file anymore
182 */
183#define PLATFORM_PSERIES 0x0100
184#define PLATFORM_PSERIES_LPAR 0x0101
185#define PLATFORM_LPAR 0x0001
186#define PLATFORM_POWERMAC 0x0400
187#define PLATFORM_GENERIC 0x0500
188
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000189static int __initdata of_platform;
190
191static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
192
Benjamin Krillcf687872009-07-27 22:02:39 +0000193static unsigned long __initdata prom_memory_limit;
194
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000195static unsigned long __initdata alloc_top;
196static unsigned long __initdata alloc_top_high;
197static unsigned long __initdata alloc_bottom;
198static unsigned long __initdata rmo_top;
199static unsigned long __initdata ram_top;
200
201static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
202static int __initdata mem_reserve_cnt;
203
204static cell_t __initdata regbuf[1024];
205
206
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000207/*
208 * Error results ... some OF calls will return "-1" on error, some
209 * will return 0, some will return either. To simplify, here are
210 * macros to use with any ihandle or phandle return value to check if
211 * it is valid
212 */
213
214#define PROM_ERROR (-1u)
215#define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
216#define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
217
218
219/* This is the one and *ONLY* place where we actually call open
220 * firmware.
221 */
222
223static int __init call_prom(const char *service, int nargs, int nret, ...)
224{
225 int i;
226 struct prom_args args;
227 va_list list;
228
229 args.service = ADDR(service);
230 args.nargs = nargs;
231 args.nret = nret;
232
233 va_start(list, nret);
234 for (i = 0; i < nargs; i++)
235 args.args[i] = va_arg(list, prom_arg_t);
236 va_end(list);
237
238 for (i = 0; i < nret; i++)
239 args.args[nargs+i] = 0;
240
Paul Mackerrasc49888202005-10-26 21:52:53 +1000241 if (enter_prom(&args, RELOC(prom_entry)) < 0)
242 return PROM_ERROR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000243
244 return (nret > 0) ? args.args[nargs] : 0;
245}
246
247static int __init call_prom_ret(const char *service, int nargs, int nret,
248 prom_arg_t *rets, ...)
249{
250 int i;
251 struct prom_args args;
252 va_list list;
253
254 args.service = ADDR(service);
255 args.nargs = nargs;
256 args.nret = nret;
257
258 va_start(list, rets);
259 for (i = 0; i < nargs; i++)
260 args.args[i] = va_arg(list, prom_arg_t);
261 va_end(list);
262
263 for (i = 0; i < nret; i++)
Olaf Heringed1189b2005-11-29 14:04:05 +0100264 args.args[nargs+i] = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000265
Paul Mackerrasc49888202005-10-26 21:52:53 +1000266 if (enter_prom(&args, RELOC(prom_entry)) < 0)
267 return PROM_ERROR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000268
269 if (rets != NULL)
270 for (i = 1; i < nret; ++i)
Paul Mackerrasc5200c92005-10-10 22:57:03 +1000271 rets[i-1] = args.args[nargs+i];
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000272
273 return (nret > 0) ? args.args[nargs] : 0;
274}
275
276
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000277static void __init prom_print(const char *msg)
278{
279 const char *p, *q;
280 struct prom_t *_prom = &RELOC(prom);
281
282 if (_prom->stdout == 0)
283 return;
284
285 for (p = msg; *p != 0; p = q) {
286 for (q = p; *q != 0 && *q != '\n'; ++q)
287 ;
288 if (q > p)
289 call_prom("write", 3, 1, _prom->stdout, p, q - p);
290 if (*q == 0)
291 break;
292 ++q;
293 call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
294 }
295}
296
297
298static void __init prom_print_hex(unsigned long val)
299{
300 int i, nibbles = sizeof(val)*2;
301 char buf[sizeof(val)*2+1];
302 struct prom_t *_prom = &RELOC(prom);
303
304 for (i = nibbles-1; i >= 0; i--) {
305 buf[i] = (val & 0xf) + '0';
306 if (buf[i] > '9')
307 buf[i] += ('a'-'0'-10);
308 val >>= 4;
309 }
310 buf[nibbles] = '\0';
311 call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
312}
313
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000314/* max number of decimal digits in an unsigned long */
315#define UL_DIGITS 21
316static void __init prom_print_dec(unsigned long val)
317{
318 int i, size;
319 char buf[UL_DIGITS+1];
320 struct prom_t *_prom = &RELOC(prom);
321
322 for (i = UL_DIGITS-1; i >= 0; i--) {
323 buf[i] = (val % 10) + '0';
324 val = val/10;
325 if (val == 0)
326 break;
327 }
328 /* shift stuff down */
329 size = UL_DIGITS - i;
330 call_prom("write", 3, 1, _prom->stdout, buf+i, size);
331}
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000332
333static void __init prom_printf(const char *format, ...)
334{
335 const char *p, *q, *s;
336 va_list args;
337 unsigned long v;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000338 long vs;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000339 struct prom_t *_prom = &RELOC(prom);
340
341 va_start(args, format);
342#ifdef CONFIG_PPC64
343 format = PTRRELOC(format);
344#endif
345 for (p = format; *p != 0; p = q) {
346 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
347 ;
348 if (q > p)
349 call_prom("write", 3, 1, _prom->stdout, p, q - p);
350 if (*q == 0)
351 break;
352 if (*q == '\n') {
353 ++q;
354 call_prom("write", 3, 1, _prom->stdout,
355 ADDR("\r\n"), 2);
356 continue;
357 }
358 ++q;
359 if (*q == 0)
360 break;
361 switch (*q) {
362 case 's':
363 ++q;
364 s = va_arg(args, const char *);
365 prom_print(s);
366 break;
367 case 'x':
368 ++q;
369 v = va_arg(args, unsigned long);
370 prom_print_hex(v);
371 break;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000372 case 'd':
373 ++q;
374 vs = va_arg(args, int);
375 if (vs < 0) {
376 prom_print(RELOC("-"));
377 vs = -vs;
378 }
379 prom_print_dec(vs);
380 break;
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000381 case 'l':
382 ++q;
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000383 if (*q == 0)
384 break;
385 else if (*q == 'x') {
386 ++q;
387 v = va_arg(args, unsigned long);
388 prom_print_hex(v);
389 } else if (*q == 'u') { /* '%lu' */
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000390 ++q;
391 v = va_arg(args, unsigned long);
392 prom_print_dec(v);
Benjamin Herrenschmidtaf277142011-04-06 10:51:17 +1000393 } else if (*q == 'd') { /* %ld */
394 ++q;
395 vs = va_arg(args, long);
396 if (vs < 0) {
397 prom_print(RELOC("-"));
398 vs = -vs;
399 }
400 prom_print_dec(vs);
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000401 }
402 break;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000403 }
404 }
405}
406
407
Paul Mackerrasa575b802005-10-23 17:23:21 +1000408static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
409 unsigned long align)
410{
Paul Mackerrasa575b802005-10-23 17:23:21 +1000411 struct prom_t *_prom = &RELOC(prom);
412
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100413 if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
414 /*
415 * Old OF requires we claim physical and virtual separately
416 * and then map explicitly (assuming virtual mode)
417 */
418 int ret;
419 prom_arg_t result;
420
421 ret = call_prom_ret("call-method", 5, 2, &result,
422 ADDR("claim"), _prom->memory,
423 align, size, virt);
424 if (ret != 0 || result == -1)
425 return -1;
426 ret = call_prom_ret("call-method", 5, 2, &result,
427 ADDR("claim"), _prom->mmumap,
428 align, size, virt);
429 if (ret != 0) {
430 call_prom("call-method", 4, 1, ADDR("release"),
431 _prom->memory, size, virt);
432 return -1;
433 }
434 /* the 0x12 is M (coherence) + PP == read/write */
Paul Mackerrasa575b802005-10-23 17:23:21 +1000435 call_prom("call-method", 6, 1,
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100436 ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
437 return virt;
438 }
439 return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
440 (prom_arg_t)align);
Paul Mackerrasa575b802005-10-23 17:23:21 +1000441}
442
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000443static void __init __attribute__((noreturn)) prom_panic(const char *reason)
444{
445#ifdef CONFIG_PPC64
446 reason = PTRRELOC(reason);
447#endif
448 prom_print(reason);
Olaf Heringadd60ef2006-03-23 22:03:57 +0100449 /* Do not call exit because it clears the screen on pmac
450 * it also causes some sort of double-fault on early pmacs */
451 if (RELOC(of_platform) == PLATFORM_POWERMAC)
452 asm("trap\n");
453
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000454 /* ToDo: should put up an SRC here on p/iSeries */
455 call_prom("exit", 0, 0);
456
457 for (;;) /* should never get here */
458 ;
459}
460
461
462static int __init prom_next_node(phandle *nodep)
463{
464 phandle node;
465
466 if ((node = *nodep) != 0
467 && (*nodep = call_prom("child", 1, 1, node)) != 0)
468 return 1;
469 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
470 return 1;
471 for (;;) {
472 if ((node = call_prom("parent", 1, 1, node)) == 0)
473 return 0;
474 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
475 return 1;
476 }
477}
478
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +1100479static int inline prom_getprop(phandle node, const char *pname,
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000480 void *value, size_t valuelen)
481{
482 return call_prom("getprop", 4, 1, node, ADDR(pname),
483 (u32)(unsigned long) value, (u32) valuelen);
484}
485
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +1100486static int inline prom_getproplen(phandle node, const char *pname)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000487{
488 return call_prom("getproplen", 2, 1, node, ADDR(pname));
489}
490
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100491static void add_string(char **str, const char *q)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000492{
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100493 char *p = *str;
494
495 while (*q)
496 *p++ = *q++;
497 *p++ = ' ';
498 *str = p;
499}
500
501static char *tohex(unsigned int x)
502{
503 static char digits[] = "0123456789abcdef";
504 static char result[9];
505 int i;
506
507 result[8] = 0;
508 i = 8;
509 do {
510 --i;
511 result[i] = digits[x & 0xf];
512 x >>= 4;
513 } while (x != 0 && i > 0);
514 return &result[i];
515}
516
517static int __init prom_setprop(phandle node, const char *nodename,
518 const char *pname, void *value, size_t valuelen)
519{
520 char cmd[256], *p;
521
522 if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
523 return call_prom("setprop", 4, 1, node, ADDR(pname),
524 (u32)(unsigned long) value, (u32) valuelen);
525
526 /* gah... setprop doesn't work on longtrail, have to use interpret */
527 p = cmd;
528 add_string(&p, "dev");
529 add_string(&p, nodename);
530 add_string(&p, tohex((u32)(unsigned long) value));
531 add_string(&p, tohex(valuelen));
532 add_string(&p, tohex(ADDR(pname)));
533 add_string(&p, tohex(strlen(RELOC(pname))));
534 add_string(&p, "property");
535 *p = 0;
536 return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000537}
538
Benjamin Krillcf687872009-07-27 22:02:39 +0000539/* We can't use the standard versions because of RELOC headaches. */
540#define isxdigit(c) (('0' <= (c) && (c) <= '9') \
541 || ('a' <= (c) && (c) <= 'f') \
542 || ('A' <= (c) && (c) <= 'F'))
543
544#define isdigit(c) ('0' <= (c) && (c) <= '9')
545#define islower(c) ('a' <= (c) && (c) <= 'z')
546#define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c))
547
548unsigned long prom_strtoul(const char *cp, const char **endp)
549{
550 unsigned long result = 0, base = 10, value;
551
552 if (*cp == '0') {
553 base = 8;
554 cp++;
555 if (toupper(*cp) == 'X') {
556 cp++;
557 base = 16;
558 }
559 }
560
561 while (isxdigit(*cp) &&
562 (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
563 result = result * base + value;
564 cp++;
565 }
566
567 if (endp)
568 *endp = cp;
569
570 return result;
571}
572
573unsigned long prom_memparse(const char *ptr, const char **retptr)
574{
575 unsigned long ret = prom_strtoul(ptr, retptr);
576 int shift = 0;
577
578 /*
579 * We can't use a switch here because GCC *may* generate a
580 * jump table which won't work, because we're not running at
581 * the address we're linked at.
582 */
583 if ('G' == **retptr || 'g' == **retptr)
584 shift = 30;
585
586 if ('M' == **retptr || 'm' == **retptr)
587 shift = 20;
588
589 if ('K' == **retptr || 'k' == **retptr)
590 shift = 10;
591
592 if (shift) {
593 ret <<= shift;
594 (*retptr)++;
595 }
596
597 return ret;
598}
599
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000600/*
601 * Early parsing of the command line passed to the kernel, used for
602 * "mem=x" and the options that affect the iommu
603 */
604static void __init early_cmdline_parse(void)
605{
606 struct prom_t *_prom = &RELOC(prom);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100607 const char *opt;
Benjamin Krillcf687872009-07-27 22:02:39 +0000608
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100609 char *p;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000610 int l = 0;
611
612 RELOC(prom_cmd_line[0]) = 0;
613 p = RELOC(prom_cmd_line);
614 if ((long)_prom->chosen > 0)
615 l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
616#ifdef CONFIG_CMDLINE
Amos Waterland0e4aa9c2006-06-12 23:45:02 -0400617 if (l <= 0 || p[0] == '\0') /* dbl check */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000618 strlcpy(RELOC(prom_cmd_line),
619 RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
620#endif /* CONFIG_CMDLINE */
621 prom_printf("command line: %s\n", RELOC(prom_cmd_line));
622
623#ifdef CONFIG_PPC64
624 opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
625 if (opt) {
626 prom_printf("iommu opt is: %s\n", opt);
627 opt += 6;
628 while (*opt && *opt == ' ')
629 opt++;
630 if (!strncmp(opt, RELOC("off"), 3))
Jeremy Kerr165785e2006-11-11 17:25:18 +1100631 RELOC(prom_iommu_off) = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000632 else if (!strncmp(opt, RELOC("force"), 5))
Jeremy Kerr165785e2006-11-11 17:25:18 +1100633 RELOC(prom_iommu_force_on) = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000634 }
635#endif
Benjamin Krillcf687872009-07-27 22:02:39 +0000636 opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
637 if (opt) {
638 opt += 4;
639 RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
640#ifdef CONFIG_PPC64
641 /* Align to 16 MB == size of ppc64 large page */
642 RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
643#endif
644 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000645}
646
647#ifdef CONFIG_PPC_PSERIES
648/*
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000649 * There are two methods for telling firmware what our capabilities are.
650 * Newer machines have an "ibm,client-architecture-support" method on the
651 * root node. For older machines, we have to call the "process-elf-header"
652 * method in the /packages/elf-loader node, passing it a fake 32-bit
653 * ELF header containing a couple of PT_NOTE sections that contain
654 * structures that contain various information.
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000655 */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000656
657/*
658 * New method - extensible architecture description vector.
659 *
660 * Because the description vector contains a mix of byte and word
661 * values, we declare it as an unsigned char array, and use this
662 * macro to put word values in.
663 */
664#define W(x) ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
665 ((x) >> 8) & 0xff, (x) & 0xff
666
667/* Option vector bits - generic bits in byte 1 */
668#define OV_IGNORE 0x80 /* ignore this vector */
669#define OV_CESSATION_POLICY 0x40 /* halt if unsupported option present*/
670
671/* Option vector 1: processor architectures supported */
672#define OV1_PPC_2_00 0x80 /* set if we support PowerPC 2.00 */
673#define OV1_PPC_2_01 0x40 /* set if we support PowerPC 2.01 */
674#define OV1_PPC_2_02 0x20 /* set if we support PowerPC 2.02 */
675#define OV1_PPC_2_03 0x10 /* set if we support PowerPC 2.03 */
676#define OV1_PPC_2_04 0x08 /* set if we support PowerPC 2.04 */
677#define OV1_PPC_2_05 0x04 /* set if we support PowerPC 2.05 */
Joel Schopp0cb99012008-06-19 06:23:23 +1000678#define OV1_PPC_2_06 0x02 /* set if we support PowerPC 2.06 */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000679
680/* Option vector 2: Open Firmware options supported */
681#define OV2_REAL_MODE 0x20 /* set if we want OF in real mode */
682
683/* Option vector 3: processor options supported */
684#define OV3_FP 0x80 /* floating point */
685#define OV3_VMX 0x40 /* VMX/Altivec */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100686#define OV3_DFP 0x20 /* decimal FP */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000687
688/* Option vector 5: PAPR/OF options supported */
689#define OV5_LPAR 0x80 /* logical partitioning supported */
690#define OV5_SPLPAR 0x40 /* shared-processor LPAR supported */
691/* ibm,dynamic-reconfiguration-memory property supported */
692#define OV5_DRCONF_MEMORY 0x20
693#define OV5_LARGE_PAGES 0x10 /* large pages supported */
Jake Moilanend8c391a2007-06-08 07:27:11 +1000694#define OV5_DONATE_DEDICATE_CPU 0x02 /* donate dedicated CPU support */
Michael Ellerman014dad92007-05-08 12:58:35 +1000695/* PCIe/MSI support. Without MSI full PCIe is not supported */
696#ifdef CONFIG_PCI_MSI
697#define OV5_MSI 0x01 /* PCIe/MSI support */
698#else
699#define OV5_MSI 0x00
700#endif /* CONFIG_PCI_MSI */
Nathan Fontenot8391e422008-07-24 04:36:38 +1000701#ifdef CONFIG_PPC_SMLPAR
702#define OV5_CMO 0x80 /* Cooperative Memory Overcommitment */
703#else
704#define OV5_CMO 0x00
705#endif
Anton Blanchard4b83c332010-04-07 15:33:44 +0000706#define OV5_TYPE1_AFFINITY 0x80 /* Type 1 NUMA affinity */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000707
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000708/* Option Vector 6: IBM PAPR hints */
709#define OV6_LINUX 0x02 /* Linux is our OS */
710
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000711/*
712 * The architecture vector has an array of PVR mask/value pairs,
713 * followed by # option vectors - 1, followed by the option vectors.
714 */
715static unsigned char ibm_architecture_vec[] = {
716 W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */
Anton Blanchard03054d52006-04-29 09:51:06 +1000717 W(0xffff0000), W(0x003e0000), /* POWER6 */
Michael Neulinge952e6c2008-06-18 10:47:26 +1000718 W(0xffff0000), W(0x003f0000), /* POWER7 */
Joel Schopp0cb99012008-06-19 06:23:23 +1000719 W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */
Paul Mackerras0efbc182006-11-29 22:31:47 +1100720 W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000721 W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000722 6 - 1, /* 6 option vectors */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000723
724 /* option vector 1: processor architectures supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500725 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000726 0, /* don't ignore, don't halt */
727 OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
Joel Schopp0cb99012008-06-19 06:23:23 +1000728 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000729
730 /* option vector 2: Open Firmware options supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500731 34 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000732 OV2_REAL_MODE,
733 0, 0,
734 W(0xffffffff), /* real_base */
735 W(0xffffffff), /* real_size */
736 W(0xffffffff), /* virt_base */
737 W(0xffffffff), /* virt_size */
738 W(0xffffffff), /* load_base */
Anton Blanchard856cc2f2009-03-31 20:14:01 +0000739 W(64), /* 64MB min RMA */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000740 W(0xffffffff), /* full client load */
741 0, /* min RMA percentage of total RAM */
742 48, /* max log_2(hash table size) */
743
744 /* option vector 3: processor options supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500745 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000746 0, /* don't ignore, don't halt */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100747 OV3_FP | OV3_VMX | OV3_DFP,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000748
749 /* option vector 4: IBM PAPR implementation */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500750 2 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000751 0, /* don't halt */
752
753 /* option vector 5: PAPR/OF options */
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000754 13 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000755 0, /* don't ignore, don't halt */
Jake Moilanend8c391a2007-06-08 07:27:11 +1000756 OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
757 OV5_DONATE_DEDICATE_CPU | OV5_MSI,
Nathan Fontenot8391e422008-07-24 04:36:38 +1000758 0,
759 OV5_CMO,
Anton Blanchard4b83c332010-04-07 15:33:44 +0000760 OV5_TYPE1_AFFINITY,
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000761 0,
762 0,
763 0,
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100764 /* WARNING: The offset of the "number of cores" field below
765 * must match by the macro below. Update the definition if
766 * the structure layout changes.
767 */
768#define IBM_ARCH_VEC_NRCORES_OFFSET 100
769 W(NR_CPUS), /* number of cores supported */
jschopp@austin.ibm.com28bb9ee2010-02-01 12:50:48 +0000770
771 /* option vector 6: IBM PAPR hints */
772 4 - 2, /* length */
773 0,
774 0,
775 OV6_LINUX,
776
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000777};
778
779/* Old method - ELF header with PT_NOTE sections */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000780static struct fake_elf {
781 Elf32_Ehdr elfhdr;
782 Elf32_Phdr phdr[2];
783 struct chrpnote {
784 u32 namesz;
785 u32 descsz;
786 u32 type;
787 char name[8]; /* "PowerPC" */
788 struct chrpdesc {
789 u32 real_mode;
790 u32 real_base;
791 u32 real_size;
792 u32 virt_base;
793 u32 virt_size;
794 u32 load_base;
795 } chrpdesc;
796 } chrpnote;
797 struct rpanote {
798 u32 namesz;
799 u32 descsz;
800 u32 type;
801 char name[24]; /* "IBM,RPA-Client-Config" */
802 struct rpadesc {
803 u32 lpar_affinity;
804 u32 min_rmo_size;
805 u32 min_rmo_percent;
806 u32 max_pft_size;
807 u32 splpar;
808 u32 min_load;
809 u32 new_mem_def;
810 u32 ignore_me;
811 } rpadesc;
812 } rpanote;
Paul Mackerras5663a122008-10-31 22:27:17 +1100813} fake_elf = {
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000814 .elfhdr = {
815 .e_ident = { 0x7f, 'E', 'L', 'F',
816 ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
817 .e_type = ET_EXEC, /* yeah right */
818 .e_machine = EM_PPC,
819 .e_version = EV_CURRENT,
820 .e_phoff = offsetof(struct fake_elf, phdr),
821 .e_phentsize = sizeof(Elf32_Phdr),
822 .e_phnum = 2
823 },
824 .phdr = {
825 [0] = {
826 .p_type = PT_NOTE,
827 .p_offset = offsetof(struct fake_elf, chrpnote),
828 .p_filesz = sizeof(struct chrpnote)
829 }, [1] = {
830 .p_type = PT_NOTE,
831 .p_offset = offsetof(struct fake_elf, rpanote),
832 .p_filesz = sizeof(struct rpanote)
833 }
834 },
835 .chrpnote = {
836 .namesz = sizeof("PowerPC"),
837 .descsz = sizeof(struct chrpdesc),
838 .type = 0x1275,
839 .name = "PowerPC",
840 .chrpdesc = {
841 .real_mode = ~0U, /* ~0 means "don't care" */
842 .real_base = ~0U,
843 .real_size = ~0U,
844 .virt_base = ~0U,
845 .virt_size = ~0U,
846 .load_base = ~0U
847 },
848 },
849 .rpanote = {
850 .namesz = sizeof("IBM,RPA-Client-Config"),
851 .descsz = sizeof(struct rpadesc),
852 .type = 0x12759999,
853 .name = "IBM,RPA-Client-Config",
854 .rpadesc = {
Paul Mackerras5663a122008-10-31 22:27:17 +1100855 .lpar_affinity = 0,
856 .min_rmo_size = 64, /* in megabytes */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000857 .min_rmo_percent = 0,
Paul Mackerras5663a122008-10-31 22:27:17 +1100858 .max_pft_size = 48, /* 2^48 bytes max PFT size */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000859 .splpar = 1,
860 .min_load = ~0U,
Paul Mackerras5663a122008-10-31 22:27:17 +1100861 .new_mem_def = 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000862 }
863 }
864};
865
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100866static int __init prom_count_smt_threads(void)
867{
868 phandle node;
869 char type[64];
870 unsigned int plen;
871
872 /* Pick up th first CPU node we can find */
873 for (node = 0; prom_next_node(&node); ) {
874 type[0] = 0;
875 prom_getprop(node, "device_type", type, sizeof(type));
876
877 if (strcmp(type, RELOC("cpu")))
878 continue;
879 /*
880 * There is an entry for each smt thread, each entry being
881 * 4 bytes long. All cpus should have the same number of
882 * smt threads, so return after finding the first.
883 */
884 plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
885 if (plen == PROM_ERROR)
886 break;
887 plen >>= 2;
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000888 prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100889
890 /* Sanity check */
891 if (plen < 1 || plen > 64) {
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000892 prom_printf("Threads per core %lu out of bounds, assuming 1\n",
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100893 (unsigned long)plen);
894 return 1;
895 }
896 return plen;
897 }
898 prom_debug("No threads found, assuming 1 per core\n");
899
900 return 1;
901
902}
903
904
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000905static void __init prom_send_capabilities(void)
906{
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000907 ihandle elfloader, root;
908 prom_arg_t ret;
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100909 u32 *cores;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000910
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000911 root = call_prom("open", 1, 1, ADDR("/"));
912 if (root != 0) {
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100913 /* We need to tell the FW about the number of cores we support.
914 *
915 * To do that, we count the number of threads on the first core
916 * (we assume this is the same for all cores) and use it to
917 * divide NR_CPUS.
918 */
919 cores = (u32 *)PTRRELOC(&ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET]);
920 if (*cores != NR_CPUS) {
921 prom_printf("WARNING ! "
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000922 "ibm_architecture_vec structure inconsistent: %lu!\n",
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100923 *cores);
924 } else {
Anton Blanchard33ad5e42010-06-17 14:33:06 +0000925 *cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
Michael Neuling2c48a7d2010-07-27 18:26:21 +0000926 prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
927 *cores, NR_CPUS);
Benjamin Herrenschmidtefec9592010-02-04 14:33:54 +1100928 }
929
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000930 /* try calling the ibm,client-architecture-support method */
Anton Blanchard049d0492009-09-21 20:47:39 +0000931 prom_printf("Calling ibm,client-architecture-support...");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000932 if (call_prom_ret("call-method", 3, 2, &ret,
933 ADDR("ibm,client-architecture-support"),
Benjamin Herrenschmidt33b74972006-06-07 12:01:32 +1000934 root,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000935 ADDR(ibm_architecture_vec)) == 0) {
936 /* the call exists... */
937 if (ret)
Anton Blanchard4da727a2009-03-31 20:06:14 +0000938 prom_printf("\nWARNING: ibm,client-architecture"
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000939 "-support call FAILED!\n");
940 call_prom("close", 1, 0, root);
Anton Blanchard4da727a2009-03-31 20:06:14 +0000941 prom_printf(" done\n");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000942 return;
943 }
944 call_prom("close", 1, 0, root);
Anton Blanchard049d0492009-09-21 20:47:39 +0000945 prom_printf(" not implemented\n");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000946 }
947
948 /* no ibm,client-architecture-support call, try the old way */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000949 elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
950 if (elfloader == 0) {
951 prom_printf("couldn't open /packages/elf-loader\n");
952 return;
953 }
954 call_prom("call-method", 3, 1, ADDR("process-elf-header"),
955 elfloader, ADDR(&fake_elf));
956 call_prom("close", 1, 0, elfloader);
957}
958#endif
959
960/*
961 * Memory allocation strategy... our layout is normally:
962 *
963 * at 14Mb or more we have vmlinux, then a gap and initrd. In some
964 * rare cases, initrd might end up being before the kernel though.
965 * We assume this won't override the final kernel at 0, we have no
966 * provision to handle that in this version, but it should hopefully
967 * never happen.
968 *
969 * alloc_top is set to the top of RMO, eventually shrink down if the
970 * TCEs overlap
971 *
972 * alloc_bottom is set to the top of kernel/initrd
973 *
974 * from there, allocations are done this way : rtas is allocated
975 * topmost, and the device-tree is allocated from the bottom. We try
976 * to grow the device-tree allocation as we progress. If we can't,
977 * then we fail, we don't currently have a facility to restart
978 * elsewhere, but that shouldn't be necessary.
979 *
980 * Note that calls to reserve_mem have to be done explicitly, memory
981 * allocated with either alloc_up or alloc_down isn't automatically
982 * reserved.
983 */
984
985
986/*
987 * Allocates memory in the RMO upward from the kernel/initrd
988 *
989 * When align is 0, this is a special case, it means to allocate in place
990 * at the current location of alloc_bottom or fail (that is basically
991 * extending the previous allocation). Used for the device-tree flattening
992 */
993static unsigned long __init alloc_up(unsigned long size, unsigned long align)
994{
Paul Mackerrasc49888202005-10-26 21:52:53 +1000995 unsigned long base = RELOC(alloc_bottom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000996 unsigned long addr = 0;
997
Paul Mackerrasc49888202005-10-26 21:52:53 +1000998 if (align)
999 base = _ALIGN_UP(base, align);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001000 prom_debug("alloc_up(%x, %x)\n", size, align);
1001 if (RELOC(ram_top) == 0)
1002 prom_panic("alloc_up() called with mem not initialized\n");
1003
1004 if (align)
1005 base = _ALIGN_UP(RELOC(alloc_bottom), align);
1006 else
1007 base = RELOC(alloc_bottom);
1008
1009 for(; (base + size) <= RELOC(alloc_top);
1010 base = _ALIGN_UP(base + 0x100000, align)) {
1011 prom_debug(" trying: 0x%x\n\r", base);
1012 addr = (unsigned long)prom_claim(base, size, 0);
Paul Mackerrasc49888202005-10-26 21:52:53 +10001013 if (addr != PROM_ERROR && addr != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001014 break;
1015 addr = 0;
1016 if (align == 0)
1017 break;
1018 }
1019 if (addr == 0)
1020 return 0;
1021 RELOC(alloc_bottom) = addr;
1022
1023 prom_debug(" -> %x\n", addr);
1024 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1025 prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
1026 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1027 prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
1028 prom_debug(" ram_top : %x\n", RELOC(ram_top));
1029
1030 return addr;
1031}
1032
1033/*
1034 * Allocates memory downward, either from top of RMO, or if highmem
1035 * is set, from the top of RAM. Note that this one doesn't handle
1036 * failures. It does claim memory if highmem is not set.
1037 */
1038static unsigned long __init alloc_down(unsigned long size, unsigned long align,
1039 int highmem)
1040{
1041 unsigned long base, addr = 0;
1042
1043 prom_debug("alloc_down(%x, %x, %s)\n", size, align,
1044 highmem ? RELOC("(high)") : RELOC("(low)"));
1045 if (RELOC(ram_top) == 0)
1046 prom_panic("alloc_down() called with mem not initialized\n");
1047
1048 if (highmem) {
1049 /* Carve out storage for the TCE table. */
1050 addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
1051 if (addr <= RELOC(alloc_bottom))
1052 return 0;
1053 /* Will we bump into the RMO ? If yes, check out that we
1054 * didn't overlap existing allocations there, if we did,
1055 * we are dead, we must be the first in town !
1056 */
1057 if (addr < RELOC(rmo_top)) {
1058 /* Good, we are first */
1059 if (RELOC(alloc_top) == RELOC(rmo_top))
1060 RELOC(alloc_top) = RELOC(rmo_top) = addr;
1061 else
1062 return 0;
1063 }
1064 RELOC(alloc_top_high) = addr;
1065 goto bail;
1066 }
1067
1068 base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
1069 for (; base > RELOC(alloc_bottom);
1070 base = _ALIGN_DOWN(base - 0x100000, align)) {
1071 prom_debug(" trying: 0x%x\n\r", base);
1072 addr = (unsigned long)prom_claim(base, size, 0);
Paul Mackerrasc49888202005-10-26 21:52:53 +10001073 if (addr != PROM_ERROR && addr != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001074 break;
1075 addr = 0;
1076 }
1077 if (addr == 0)
1078 return 0;
1079 RELOC(alloc_top) = addr;
1080
1081 bail:
1082 prom_debug(" -> %x\n", addr);
1083 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1084 prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
1085 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1086 prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
1087 prom_debug(" ram_top : %x\n", RELOC(ram_top));
1088
1089 return addr;
1090}
1091
1092/*
1093 * Parse a "reg" cell
1094 */
1095static unsigned long __init prom_next_cell(int s, cell_t **cellp)
1096{
1097 cell_t *p = *cellp;
1098 unsigned long r = 0;
1099
1100 /* Ignore more than 2 cells */
1101 while (s > sizeof(unsigned long) / 4) {
1102 p++;
1103 s--;
1104 }
1105 r = *p++;
1106#ifdef CONFIG_PPC64
Paul Mackerras35499c02005-10-22 16:02:39 +10001107 if (s > 1) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001108 r <<= 32;
1109 r |= *(p++);
1110 }
1111#endif
1112 *cellp = p;
1113 return r;
1114}
1115
1116/*
1117 * Very dumb function for adding to the memory reserve list, but
1118 * we don't need anything smarter at this point
1119 *
1120 * XXX Eventually check for collisions. They should NEVER happen.
1121 * If problems seem to show up, it would be a good start to track
1122 * them down.
1123 */
Michael Ellerman0108d3f2007-05-07 15:58:28 +10001124static void __init reserve_mem(u64 base, u64 size)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001125{
Kumar Galacbbcf342006-01-11 17:57:13 -06001126 u64 top = base + size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001127 unsigned long cnt = RELOC(mem_reserve_cnt);
1128
1129 if (size == 0)
1130 return;
1131
1132 /* We need to always keep one empty entry so that we
1133 * have our terminator with "size" set to 0 since we are
1134 * dumb and just copy this entire array to the boot params
1135 */
1136 base = _ALIGN_DOWN(base, PAGE_SIZE);
1137 top = _ALIGN_UP(top, PAGE_SIZE);
1138 size = top - base;
1139
1140 if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
1141 prom_panic("Memory reserve map exhausted !\n");
1142 RELOC(mem_reserve_map)[cnt].base = base;
1143 RELOC(mem_reserve_map)[cnt].size = size;
1144 RELOC(mem_reserve_cnt) = cnt + 1;
1145}
1146
1147/*
Adrian Bunkb3c2ffd2006-06-30 18:20:44 +02001148 * Initialize memory allocation mechanism, parse "memory" nodes and
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001149 * obtain that way the top of memory and RMO to setup out local allocator
1150 */
1151static void __init prom_init_mem(void)
1152{
1153 phandle node;
1154 char *path, type[64];
1155 unsigned int plen;
1156 cell_t *p, *endp;
1157 struct prom_t *_prom = &RELOC(prom);
1158 u32 rac, rsc;
1159
1160 /*
1161 * We iterate the memory nodes to find
1162 * 1) top of RMO (first node)
1163 * 2) top of memory
1164 */
1165 rac = 2;
1166 prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
1167 rsc = 1;
1168 prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
1169 prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
1170 prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
1171
1172 prom_debug("scanning memory:\n");
1173 path = RELOC(prom_scratch);
1174
1175 for (node = 0; prom_next_node(&node); ) {
1176 type[0] = 0;
1177 prom_getprop(node, "device_type", type, sizeof(type));
1178
Paul Mackerrasc49888202005-10-26 21:52:53 +10001179 if (type[0] == 0) {
1180 /*
1181 * CHRP Longtrail machines have no device_type
1182 * on the memory node, so check the name instead...
1183 */
1184 prom_getprop(node, "name", type, sizeof(type));
1185 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001186 if (strcmp(type, RELOC("memory")))
1187 continue;
Paul Mackerrasc49888202005-10-26 21:52:53 +10001188
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001189 plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
1190 if (plen > sizeof(regbuf)) {
1191 prom_printf("memory node too large for buffer !\n");
1192 plen = sizeof(regbuf);
1193 }
1194 p = RELOC(regbuf);
1195 endp = p + (plen / sizeof(cell_t));
1196
1197#ifdef DEBUG_PROM
1198 memset(path, 0, PROM_SCRATCH_SIZE);
1199 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1200 prom_debug(" node %s :\n", path);
1201#endif /* DEBUG_PROM */
1202
1203 while ((endp - p) >= (rac + rsc)) {
1204 unsigned long base, size;
1205
1206 base = prom_next_cell(rac, &p);
1207 size = prom_next_cell(rsc, &p);
1208
1209 if (size == 0)
1210 continue;
1211 prom_debug(" %x %x\n", base, size);
Benjamin Herrenschmidtab1b55e2006-03-03 10:35:40 +11001212 if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001213 RELOC(rmo_top) = size;
1214 if ((base + size) > RELOC(ram_top))
1215 RELOC(ram_top) = base + size;
1216 }
1217 }
1218
1219 RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
1220
1221 /* Check if we have an initrd after the kernel, if we do move our bottom
1222 * point to after it
1223 */
1224 if (RELOC(prom_initrd_start)) {
1225 if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
1226 RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
1227 }
1228
1229 /*
Benjamin Krillcf687872009-07-27 22:02:39 +00001230 * If prom_memory_limit is set we reduce the upper limits *except* for
1231 * alloc_top_high. This must be the real top of RAM so we can put
1232 * TCE's up there.
1233 */
1234
1235 RELOC(alloc_top_high) = RELOC(ram_top);
1236
1237 if (RELOC(prom_memory_limit)) {
1238 if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
1239 prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
1240 RELOC(prom_memory_limit));
1241 RELOC(prom_memory_limit) = 0;
1242 } else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
1243 prom_printf("Ignoring mem=%x >= ram_top.\n",
1244 RELOC(prom_memory_limit));
1245 RELOC(prom_memory_limit) = 0;
1246 } else {
1247 RELOC(ram_top) = RELOC(prom_memory_limit);
1248 RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
1249 }
1250 }
1251
1252 /*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001253 * Setup our top alloc point, that is top of RMO or top of
1254 * segment 0 when running non-LPAR.
1255 * Some RS64 machines have buggy firmware where claims up at
1256 * 1GB fail. Cap at 768MB as a workaround.
1257 * Since 768MB is plenty of room, and we need to cap to something
1258 * reasonable on 32-bit, cap at 768MB on all machines.
1259 */
1260 if (!RELOC(rmo_top))
1261 RELOC(rmo_top) = RELOC(ram_top);
1262 RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
1263 RELOC(alloc_top) = RELOC(rmo_top);
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001264 RELOC(alloc_top_high) = RELOC(ram_top);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001265
1266 prom_printf("memory layout at init:\n");
Benjamin Krillcf687872009-07-27 22:02:39 +00001267 prom_printf(" memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001268 prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1269 prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
1270 prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1271 prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
1272 prom_printf(" ram_top : %x\n", RELOC(ram_top));
1273}
1274
1275
1276/*
1277 * Allocate room for and instantiate RTAS
1278 */
1279static void __init prom_instantiate_rtas(void)
1280{
1281 phandle rtas_node;
1282 ihandle rtas_inst;
1283 u32 base, entry = 0;
1284 u32 size = 0;
1285
1286 prom_debug("prom_instantiate_rtas: start...\n");
1287
1288 rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1289 prom_debug("rtas_node: %x\n", rtas_node);
1290 if (!PHANDLE_VALID(rtas_node))
1291 return;
1292
1293 prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1294 if (size == 0)
1295 return;
1296
1297 base = alloc_down(size, PAGE_SIZE, 0);
1298 if (base == 0) {
1299 prom_printf("RTAS allocation failed !\n");
1300 return;
1301 }
1302
1303 rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1304 if (!IHANDLE_VALID(rtas_inst)) {
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001305 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001306 return;
1307 }
1308
Anton Blanchard1f8737a2009-03-31 20:06:15 +00001309 prom_printf("instantiating rtas at 0x%x...", base);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001310
1311 if (call_prom_ret("call-method", 3, 2, &entry,
1312 ADDR("instantiate-rtas"),
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001313 rtas_inst, base) != 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001314 || entry == 0) {
1315 prom_printf(" failed\n");
1316 return;
1317 }
1318 prom_printf(" done\n");
1319
1320 reserve_mem(base, size);
1321
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001322 prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1323 &base, sizeof(base));
1324 prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1325 &entry, sizeof(entry));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001326
1327 prom_debug("rtas base = 0x%x\n", base);
1328 prom_debug("rtas entry = 0x%x\n", entry);
1329 prom_debug("rtas size = 0x%x\n", (long)size);
1330
1331 prom_debug("prom_instantiate_rtas: end...\n");
1332}
1333
1334#ifdef CONFIG_PPC64
1335/*
1336 * Allocate room for and initialize TCE tables
1337 */
1338static void __init prom_initialize_tce_table(void)
1339{
1340 phandle node;
1341 ihandle phb_node;
1342 char compatible[64], type[64], model[64];
1343 char *path = RELOC(prom_scratch);
1344 u64 base, align;
1345 u32 minalign, minsize;
1346 u64 tce_entry, *tce_entryp;
1347 u64 local_alloc_top, local_alloc_bottom;
1348 u64 i;
1349
Jeremy Kerr165785e2006-11-11 17:25:18 +11001350 if (RELOC(prom_iommu_off))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001351 return;
1352
1353 prom_debug("starting prom_initialize_tce_table\n");
1354
1355 /* Cache current top of allocs so we reserve a single block */
1356 local_alloc_top = RELOC(alloc_top_high);
1357 local_alloc_bottom = local_alloc_top;
1358
1359 /* Search all nodes looking for PHBs. */
1360 for (node = 0; prom_next_node(&node); ) {
1361 compatible[0] = 0;
1362 type[0] = 0;
1363 model[0] = 0;
1364 prom_getprop(node, "compatible",
1365 compatible, sizeof(compatible));
1366 prom_getprop(node, "device_type", type, sizeof(type));
1367 prom_getprop(node, "model", model, sizeof(model));
1368
1369 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1370 continue;
1371
Linas Vepstase788ff12007-09-07 03:45:21 +10001372 /* Keep the old logic intact to avoid regression. */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001373 if (compatible[0] != 0) {
1374 if ((strstr(compatible, RELOC("python")) == NULL) &&
1375 (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1376 (strstr(compatible, RELOC("Winnipeg")) == NULL))
1377 continue;
1378 } else if (model[0] != 0) {
1379 if ((strstr(model, RELOC("ython")) == NULL) &&
1380 (strstr(model, RELOC("peedwagon")) == NULL) &&
1381 (strstr(model, RELOC("innipeg")) == NULL))
1382 continue;
1383 }
1384
1385 if (prom_getprop(node, "tce-table-minalign", &minalign,
1386 sizeof(minalign)) == PROM_ERROR)
1387 minalign = 0;
1388 if (prom_getprop(node, "tce-table-minsize", &minsize,
1389 sizeof(minsize)) == PROM_ERROR)
1390 minsize = 4UL << 20;
1391
1392 /*
1393 * Even though we read what OF wants, we just set the table
1394 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
1395 * By doing this, we avoid the pitfalls of trying to DMA to
1396 * MMIO space and the DMA alias hole.
1397 *
1398 * On POWER4, firmware sets the TCE region by assuming
1399 * each TCE table is 8MB. Using this memory for anything
1400 * else will impact performance, so we always allocate 8MB.
1401 * Anton
1402 */
1403 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1404 minsize = 8UL << 20;
1405 else
1406 minsize = 4UL << 20;
1407
1408 /* Align to the greater of the align or size */
1409 align = max(minalign, minsize);
1410 base = alloc_down(minsize, align, 1);
1411 if (base == 0)
1412 prom_panic("ERROR, cannot find space for TCE table.\n");
1413 if (base < local_alloc_bottom)
1414 local_alloc_bottom = base;
1415
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001416 /* It seems OF doesn't null-terminate the path :-( */
Li Zefanaca71ef2007-11-05 13:21:56 +11001417 memset(path, 0, PROM_SCRATCH_SIZE);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001418 /* Call OF to setup the TCE hardware */
1419 if (call_prom("package-to-path", 3, 1, node,
1420 path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1421 prom_printf("package-to-path failed\n");
1422 }
1423
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001424 /* Save away the TCE table attributes for later use. */
1425 prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1426 prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1427
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001428 prom_debug("TCE table: %s\n", path);
1429 prom_debug("\tnode = 0x%x\n", node);
1430 prom_debug("\tbase = 0x%x\n", base);
1431 prom_debug("\tsize = 0x%x\n", minsize);
1432
1433 /* Initialize the table to have a one-to-one mapping
1434 * over the allocated size.
1435 */
Ingo Molnar2b931fb2009-01-06 13:56:52 +00001436 tce_entryp = (u64 *)base;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001437 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1438 tce_entry = (i << PAGE_SHIFT);
1439 tce_entry |= 0x3;
1440 *tce_entryp = tce_entry;
1441 }
1442
1443 prom_printf("opening PHB %s", path);
1444 phb_node = call_prom("open", 1, 1, path);
1445 if (phb_node == 0)
1446 prom_printf("... failed\n");
1447 else
1448 prom_printf("... done\n");
1449
1450 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1451 phb_node, -1, minsize,
1452 (u32) base, (u32) (base >> 32));
1453 call_prom("close", 1, 0, phb_node);
1454 }
1455
1456 reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1457
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001458 /* These are only really needed if there is a memory limit in
1459 * effect, but we don't know so export them always. */
1460 RELOC(prom_tce_alloc_start) = local_alloc_bottom;
1461 RELOC(prom_tce_alloc_end) = local_alloc_top;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001462
1463 /* Flag the first invalid entry */
1464 prom_debug("ending prom_initialize_tce_table\n");
1465}
1466#endif
1467
1468/*
1469 * With CHRP SMP we need to use the OF to start the other processors.
1470 * We can't wait until smp_boot_cpus (the OF is trashed by then)
1471 * so we have to put the processors into a holding pattern controlled
1472 * by the kernel (not OF) before we destroy the OF.
1473 *
1474 * This uses a chunk of low memory, puts some holding pattern
1475 * code there and sends the other processors off to there until
1476 * smp_boot_cpus tells them to do something. The holding pattern
1477 * checks that address until its cpu # is there, when it is that
1478 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
1479 * of setting those values.
1480 *
1481 * We also use physical address 0x4 here to tell when a cpu
1482 * is in its holding pattern code.
1483 *
1484 * -- Cort
1485 */
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001486/*
1487 * We want to reference the copy of __secondary_hold_* in the
1488 * 0 - 0x100 address range
1489 */
1490#define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
1491
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001492static void __init prom_hold_cpus(void)
1493{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001494 unsigned long i;
1495 unsigned int reg;
1496 phandle node;
1497 char type[64];
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001498 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001499 unsigned long *spinloop
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001500 = (void *) LOW_ADDR(__secondary_hold_spinloop);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001501 unsigned long *acknowledge
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001502 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001503 unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001504
1505 prom_debug("prom_hold_cpus: start...\n");
1506 prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
1507 prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
1508 prom_debug(" 1) acknowledge = 0x%x\n",
1509 (unsigned long)acknowledge);
1510 prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
1511 prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
1512
1513 /* Set the common spinloop variable, so all of the secondary cpus
1514 * will block when they are awakened from their OF spinloop.
1515 * This must occur for both SMP and non SMP kernels, since OF will
1516 * be trashed when we move the kernel.
1517 */
1518 *spinloop = 0;
1519
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001520 /* look for cpus */
1521 for (node = 0; prom_next_node(&node); ) {
1522 type[0] = 0;
1523 prom_getprop(node, "device_type", type, sizeof(type));
1524 if (strcmp(type, RELOC("cpu")) != 0)
1525 continue;
1526
1527 /* Skip non-configured cpus. */
1528 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1529 if (strcmp(type, RELOC("okay")) != 0)
1530 continue;
1531
1532 reg = -1;
1533 prom_getprop(node, "reg", &reg, sizeof(reg));
1534
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001535 prom_debug("cpu hw idx = %lu\n", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001536
1537 /* Init the acknowledge var which will be reset by
1538 * the secondary cpu when it awakens from its OF
1539 * spinloop.
1540 */
1541 *acknowledge = (unsigned long)-1;
1542
Nathan Lynch7d2f6072008-07-27 15:24:50 +10001543 if (reg != _prom->cpu) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001544 /* Primary Thread of non-boot cpu */
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001545 prom_printf("starting cpu hw idx %lu... ", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001546 call_prom("start-cpu", 3, 0, node,
1547 secondary_hold, reg);
1548
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001549 for (i = 0; (i < 100000000) &&
1550 (*acknowledge == ((unsigned long)-1)); i++ )
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001551 mb();
1552
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001553 if (*acknowledge == reg)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001554 prom_printf("done\n");
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001555 else
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001556 prom_printf("failed: %x\n", *acknowledge);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001557 }
1558#ifdef CONFIG_SMP
1559 else
Michael Neuling2c48a7d2010-07-27 18:26:21 +00001560 prom_printf("boot cpu hw idx %lu\n", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001561#endif /* CONFIG_SMP */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001562 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001563
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001564 prom_debug("prom_hold_cpus: end...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001565}
1566
1567
1568static void __init prom_init_client_services(unsigned long pp)
1569{
1570 struct prom_t *_prom = &RELOC(prom);
1571
1572 /* Get a handle to the prom entry point before anything else */
1573 RELOC(prom_entry) = pp;
1574
1575 /* get a handle for the stdout device */
1576 _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1577 if (!PHANDLE_VALID(_prom->chosen))
1578 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1579
1580 /* get device tree root */
1581 _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1582 if (!PHANDLE_VALID(_prom->root))
1583 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001584
1585 _prom->mmumap = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001586}
1587
Paul Mackerrasa575b802005-10-23 17:23:21 +10001588#ifdef CONFIG_PPC32
1589/*
1590 * For really old powermacs, we need to map things we claim.
1591 * For that, we need the ihandle of the mmu.
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001592 * Also, on the longtrail, we need to work around other bugs.
Paul Mackerrasa575b802005-10-23 17:23:21 +10001593 */
1594static void __init prom_find_mmu(void)
1595{
1596 struct prom_t *_prom = &RELOC(prom);
1597 phandle oprom;
1598 char version[64];
1599
1600 oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1601 if (!PHANDLE_VALID(oprom))
1602 return;
1603 if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1604 return;
1605 version[sizeof(version) - 1] = 0;
Paul Mackerrasa575b802005-10-23 17:23:21 +10001606 /* XXX might need to add other versions here */
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001607 if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1608 of_workarounds = OF_WA_CLAIM;
1609 else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1610 of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1611 call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1612 } else
Paul Mackerrasa575b802005-10-23 17:23:21 +10001613 return;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001614 _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
Paul Mackerrasa575b802005-10-23 17:23:21 +10001615 prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1616 sizeof(_prom->mmumap));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001617 if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1618 of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001619}
1620#else
1621#define prom_find_mmu()
1622#endif
1623
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001624static void __init prom_init_stdout(void)
1625{
1626 struct prom_t *_prom = &RELOC(prom);
1627 char *path = RELOC(of_stdout_device);
1628 char type[16];
1629 u32 val;
1630
1631 if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1632 prom_panic("cannot find stdout");
1633
1634 _prom->stdout = val;
1635
1636 /* Get the full OF pathname of the stdout device */
1637 memset(path, 0, 256);
1638 call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1639 val = call_prom("instance-to-package", 1, 1, _prom->stdout);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001640 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1641 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001642 prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001643 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1644 path, strlen(path) + 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001645
1646 /* If it's a display, note it */
1647 memset(type, 0, sizeof(type));
1648 prom_getprop(val, "device_type", type, sizeof(type));
1649 if (strcmp(type, RELOC("display")) == 0)
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001650 prom_setprop(val, path, "linux,boot-display", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001651}
1652
1653static void __init prom_close_stdin(void)
1654{
1655 struct prom_t *_prom = &RELOC(prom);
1656 ihandle val;
1657
1658 if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1659 call_prom("close", 1, 0, val);
1660}
1661
1662static int __init prom_find_machine_type(void)
1663{
1664 struct prom_t *_prom = &RELOC(prom);
1665 char compat[256];
1666 int len, i = 0;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001667#ifdef CONFIG_PPC64
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001668 phandle rtas;
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001669 int x;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001670#endif
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001671
1672 /* Look for a PowerMac */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001673 len = prom_getprop(_prom->root, "compatible",
1674 compat, sizeof(compat)-1);
1675 if (len > 0) {
1676 compat[len] = 0;
1677 while (i < len) {
1678 char *p = &compat[i];
1679 int sl = strlen(p);
1680 if (sl == 0)
1681 break;
1682 if (strstr(p, RELOC("Power Macintosh")) ||
Paul Mackerrasa575b802005-10-23 17:23:21 +10001683 strstr(p, RELOC("MacRISC")))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001684 return PLATFORM_POWERMAC;
Arnd Bergmann133dda12006-06-07 12:04:18 +10001685#ifdef CONFIG_PPC64
1686 /* We must make sure we don't detect the IBM Cell
1687 * blades as pSeries due to some firmware issues,
1688 * so we do it here.
1689 */
1690 if (strstr(p, RELOC("IBM,CBEA")) ||
1691 strstr(p, RELOC("IBM,CPBW-1.0")))
1692 return PLATFORM_GENERIC;
1693#endif /* CONFIG_PPC64 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001694 i += sl + 1;
1695 }
1696 }
1697#ifdef CONFIG_PPC64
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001698 /* If not a mac, try to figure out if it's an IBM pSeries or any other
1699 * PAPR compliant platform. We assume it is if :
1700 * - /device_type is "chrp" (please, do NOT use that for future
1701 * non-IBM designs !
1702 * - it has /rtas
1703 */
Michael Ellerman6f806ce2006-04-07 13:56:21 +10001704 len = prom_getprop(_prom->root, "device_type",
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001705 compat, sizeof(compat)-1);
1706 if (len <= 0)
1707 return PLATFORM_GENERIC;
Benjamin Herrenschmidtcb6b2eb2006-05-15 15:46:03 +10001708 if (strcmp(compat, RELOC("chrp")))
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001709 return PLATFORM_GENERIC;
1710
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001711 /* Default to pSeries. We need to know if we are running LPAR */
1712 rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001713 if (!PHANDLE_VALID(rtas))
1714 return PLATFORM_GENERIC;
1715 x = prom_getproplen(rtas, "ibm,hypertas-functions");
1716 if (x != PROM_ERROR) {
Anton Blanchard4da727a2009-03-31 20:06:14 +00001717 prom_debug("Hypertas detected, assuming LPAR !\n");
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001718 return PLATFORM_PSERIES_LPAR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001719 }
1720 return PLATFORM_PSERIES;
1721#else
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001722 return PLATFORM_GENERIC;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001723#endif
1724}
1725
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001726static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1727{
1728 return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1729}
1730
1731/*
1732 * If we have a display that we don't know how to drive,
1733 * we will want to try to execute OF's open method for it
1734 * later. However, OF will probably fall over if we do that
1735 * we've taken over the MMU.
1736 * So we check whether we will need to open the display,
1737 * and if so, open it now.
1738 */
1739static void __init prom_check_displays(void)
1740{
1741 char type[16], *path;
1742 phandle node;
1743 ihandle ih;
1744 int i;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001745
1746 static unsigned char default_colors[] = {
1747 0x00, 0x00, 0x00,
1748 0x00, 0x00, 0xaa,
1749 0x00, 0xaa, 0x00,
1750 0x00, 0xaa, 0xaa,
1751 0xaa, 0x00, 0x00,
1752 0xaa, 0x00, 0xaa,
1753 0xaa, 0xaa, 0x00,
1754 0xaa, 0xaa, 0xaa,
1755 0x55, 0x55, 0x55,
1756 0x55, 0x55, 0xff,
1757 0x55, 0xff, 0x55,
1758 0x55, 0xff, 0xff,
1759 0xff, 0x55, 0x55,
1760 0xff, 0x55, 0xff,
1761 0xff, 0xff, 0x55,
1762 0xff, 0xff, 0xff
1763 };
1764 const unsigned char *clut;
1765
Anton Blanchard4da727a2009-03-31 20:06:14 +00001766 prom_debug("Looking for displays\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001767 for (node = 0; prom_next_node(&node); ) {
1768 memset(type, 0, sizeof(type));
1769 prom_getprop(node, "device_type", type, sizeof(type));
1770 if (strcmp(type, RELOC("display")) != 0)
1771 continue;
1772
1773 /* It seems OF doesn't null-terminate the path :-( */
1774 path = RELOC(prom_scratch);
1775 memset(path, 0, PROM_SCRATCH_SIZE);
1776
1777 /*
1778 * leave some room at the end of the path for appending extra
1779 * arguments
1780 */
1781 if (call_prom("package-to-path", 3, 1, node, path,
1782 PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1783 continue;
Anton Blanchard1f8737a2009-03-31 20:06:15 +00001784 prom_printf("found display : %s, opening... ", path);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001785
1786 ih = call_prom("open", 1, 1, path);
1787 if (ih == 0) {
1788 prom_printf("failed\n");
1789 continue;
1790 }
1791
1792 /* Success */
1793 prom_printf("done\n");
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001794 prom_setprop(node, path, "linux,opened", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001795
1796 /* Setup a usable color table when the appropriate
1797 * method is available. Should update this to set-colors */
1798 clut = RELOC(default_colors);
1799 for (i = 0; i < 32; i++, clut += 3)
1800 if (prom_set_color(ih, i, clut[0], clut[1],
1801 clut[2]) != 0)
1802 break;
1803
1804#ifdef CONFIG_LOGO_LINUX_CLUT224
1805 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1806 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1807 if (prom_set_color(ih, i + 32, clut[0], clut[1],
1808 clut[2]) != 0)
1809 break;
1810#endif /* CONFIG_LOGO_LINUX_CLUT224 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001811 }
1812}
1813
1814
1815/* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1816static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
1817 unsigned long needed, unsigned long align)
1818{
1819 void *ret;
1820
1821 *mem_start = _ALIGN(*mem_start, align);
1822 while ((*mem_start + needed) > *mem_end) {
1823 unsigned long room, chunk;
1824
1825 prom_debug("Chunk exhausted, claiming more at %x...\n",
1826 RELOC(alloc_bottom));
1827 room = RELOC(alloc_top) - RELOC(alloc_bottom);
1828 if (room > DEVTREE_CHUNK_SIZE)
1829 room = DEVTREE_CHUNK_SIZE;
1830 if (room < PAGE_SIZE)
1831 prom_panic("No memory for flatten_device_tree (no room)");
1832 chunk = alloc_up(room, 0);
1833 if (chunk == 0)
1834 prom_panic("No memory for flatten_device_tree (claim failed)");
1835 *mem_end = RELOC(alloc_top);
1836 }
1837
1838 ret = (void *)*mem_start;
1839 *mem_start += needed;
1840
1841 return ret;
1842}
1843
1844#define dt_push_token(token, mem_start, mem_end) \
1845 do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
1846
1847static unsigned long __init dt_find_string(char *str)
1848{
1849 char *s, *os;
1850
1851 s = os = (char *)RELOC(dt_string_start);
1852 s += 4;
1853 while (s < (char *)RELOC(dt_string_end)) {
1854 if (strcmp(s, str) == 0)
1855 return s - os;
1856 s += strlen(s) + 1;
1857 }
1858 return 0;
1859}
1860
1861/*
1862 * The Open Firmware 1275 specification states properties must be 31 bytes or
1863 * less, however not all firmwares obey this. Make it 64 bytes to be safe.
1864 */
1865#define MAX_PROPERTY_NAME 64
1866
1867static void __init scan_dt_build_strings(phandle node,
1868 unsigned long *mem_start,
1869 unsigned long *mem_end)
1870{
1871 char *prev_name, *namep, *sstart;
1872 unsigned long soff;
1873 phandle child;
1874
1875 sstart = (char *)RELOC(dt_string_start);
1876
1877 /* get and store all property names */
1878 prev_name = RELOC("");
1879 for (;;) {
1880 /* 64 is max len of name including nul. */
1881 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
1882 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
1883 /* No more nodes: unwind alloc */
1884 *mem_start = (unsigned long)namep;
1885 break;
1886 }
1887
1888 /* skip "name" */
1889 if (strcmp(namep, RELOC("name")) == 0) {
1890 *mem_start = (unsigned long)namep;
1891 prev_name = RELOC("name");
1892 continue;
1893 }
1894 /* get/create string entry */
1895 soff = dt_find_string(namep);
1896 if (soff != 0) {
1897 *mem_start = (unsigned long)namep;
1898 namep = sstart + soff;
1899 } else {
1900 /* Trim off some if we can */
1901 *mem_start = (unsigned long)namep + strlen(namep) + 1;
1902 RELOC(dt_string_end) = *mem_start;
1903 }
1904 prev_name = namep;
1905 }
1906
1907 /* do all our children */
1908 child = call_prom("child", 1, 1, node);
1909 while (child != 0) {
1910 scan_dt_build_strings(child, mem_start, mem_end);
1911 child = call_prom("peer", 1, 1, child);
1912 }
1913}
1914
1915static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
1916 unsigned long *mem_end)
1917{
1918 phandle child;
1919 char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
1920 unsigned long soff;
1921 unsigned char *valp;
1922 static char pname[MAX_PROPERTY_NAME];
Paul Mackerrasc49888202005-10-26 21:52:53 +10001923 int l, room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001924
1925 dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
1926
1927 /* get the node's full name */
1928 namep = (char *)*mem_start;
Paul Mackerrasc49888202005-10-26 21:52:53 +10001929 room = *mem_end - *mem_start;
1930 if (room > 255)
1931 room = 255;
1932 l = call_prom("package-to-path", 3, 1, node, namep, room);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001933 if (l >= 0) {
1934 /* Didn't fit? Get more room. */
Paul Mackerrasc49888202005-10-26 21:52:53 +10001935 if (l >= room) {
1936 if (l >= *mem_end - *mem_start)
1937 namep = make_room(mem_start, mem_end, l+1, 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001938 call_prom("package-to-path", 3, 1, node, namep, l);
1939 }
1940 namep[l] = '\0';
1941
1942 /* Fixup an Apple bug where they have bogus \0 chars in the
Paul Mackerrasa575b802005-10-23 17:23:21 +10001943 * middle of the path in some properties, and extract
1944 * the unit name (everything after the last '/').
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001945 */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001946 for (lp = p = namep, ep = namep + l; p < ep; p++) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001947 if (*p == '/')
Paul Mackerrasa575b802005-10-23 17:23:21 +10001948 lp = namep;
1949 else if (*p != 0)
1950 *lp++ = *p;
1951 }
1952 *lp = 0;
1953 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001954 }
1955
1956 /* get it again for debugging */
1957 path = RELOC(prom_scratch);
1958 memset(path, 0, PROM_SCRATCH_SIZE);
1959 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1960
1961 /* get and store all properties */
1962 prev_name = RELOC("");
1963 sstart = (char *)RELOC(dt_string_start);
1964 for (;;) {
1965 if (call_prom("nextprop", 3, 1, node, prev_name,
1966 RELOC(pname)) != 1)
1967 break;
1968
1969 /* skip "name" */
1970 if (strcmp(RELOC(pname), RELOC("name")) == 0) {
1971 prev_name = RELOC("name");
1972 continue;
1973 }
1974
1975 /* find string offset */
1976 soff = dt_find_string(RELOC(pname));
1977 if (soff == 0) {
1978 prom_printf("WARNING: Can't find string index for"
1979 " <%s>, node %s\n", RELOC(pname), path);
1980 break;
1981 }
1982 prev_name = sstart + soff;
1983
1984 /* get length */
1985 l = call_prom("getproplen", 2, 1, node, RELOC(pname));
1986
1987 /* sanity checks */
1988 if (l == PROM_ERROR)
1989 continue;
1990 if (l > MAX_PROPERTY_LENGTH) {
1991 prom_printf("WARNING: ignoring large property ");
1992 /* It seems OF doesn't null-terminate the path :-( */
1993 prom_printf("[%s] ", path);
1994 prom_printf("%s length 0x%x\n", RELOC(pname), l);
1995 continue;
1996 }
1997
1998 /* push property head */
1999 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2000 dt_push_token(l, mem_start, mem_end);
2001 dt_push_token(soff, mem_start, mem_end);
2002
2003 /* push property content */
2004 valp = make_room(mem_start, mem_end, l, 4);
2005 call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
2006 *mem_start = _ALIGN(*mem_start, 4);
2007 }
2008
2009 /* Add a "linux,phandle" property. */
2010 soff = dt_find_string(RELOC("linux,phandle"));
2011 if (soff == 0)
2012 prom_printf("WARNING: Can't find string index for"
2013 " <linux-phandle> node %s\n", path);
2014 else {
2015 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2016 dt_push_token(4, mem_start, mem_end);
2017 dt_push_token(soff, mem_start, mem_end);
2018 valp = make_room(mem_start, mem_end, 4, 4);
2019 *(u32 *)valp = node;
2020 }
2021
2022 /* do all our children */
2023 child = call_prom("child", 1, 1, node);
2024 while (child != 0) {
2025 scan_dt_build_struct(child, mem_start, mem_end);
2026 child = call_prom("peer", 1, 1, child);
2027 }
2028
2029 dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
2030}
2031
2032static void __init flatten_device_tree(void)
2033{
2034 phandle root;
2035 unsigned long mem_start, mem_end, room;
2036 struct boot_param_header *hdr;
2037 struct prom_t *_prom = &RELOC(prom);
2038 char *namep;
2039 u64 *rsvmap;
2040
2041 /*
2042 * Check how much room we have between alloc top & bottom (+/- a
2043 * few pages), crop to 4Mb, as this is our "chuck" size
2044 */
2045 room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
2046 if (room > DEVTREE_CHUNK_SIZE)
2047 room = DEVTREE_CHUNK_SIZE;
2048 prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
2049
2050 /* Now try to claim that */
2051 mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
2052 if (mem_start == 0)
2053 prom_panic("Can't allocate initial device-tree chunk\n");
2054 mem_end = RELOC(alloc_top);
2055
2056 /* Get root of tree */
2057 root = call_prom("peer", 1, 1, (phandle)0);
2058 if (root == (phandle)0)
2059 prom_panic ("couldn't get device tree root\n");
2060
2061 /* Build header and make room for mem rsv map */
2062 mem_start = _ALIGN(mem_start, 4);
2063 hdr = make_room(&mem_start, &mem_end,
2064 sizeof(struct boot_param_header), 4);
2065 RELOC(dt_header_start) = (unsigned long)hdr;
2066 rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
2067
2068 /* Start of strings */
2069 mem_start = PAGE_ALIGN(mem_start);
2070 RELOC(dt_string_start) = mem_start;
2071 mem_start += 4; /* hole */
2072
2073 /* Add "linux,phandle" in there, we'll need it */
2074 namep = make_room(&mem_start, &mem_end, 16, 1);
2075 strcpy(namep, RELOC("linux,phandle"));
2076 mem_start = (unsigned long)namep + strlen(namep) + 1;
2077
2078 /* Build string array */
2079 prom_printf("Building dt strings...\n");
2080 scan_dt_build_strings(root, &mem_start, &mem_end);
2081 RELOC(dt_string_end) = mem_start;
2082
2083 /* Build structure */
2084 mem_start = PAGE_ALIGN(mem_start);
2085 RELOC(dt_struct_start) = mem_start;
2086 prom_printf("Building dt structure...\n");
2087 scan_dt_build_struct(root, &mem_start, &mem_end);
2088 dt_push_token(OF_DT_END, &mem_start, &mem_end);
2089 RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
2090
2091 /* Finish header */
2092 hdr->boot_cpuid_phys = _prom->cpu;
2093 hdr->magic = OF_DT_HEADER;
2094 hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
2095 hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
2096 hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
2097 hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
2098 hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
2099 hdr->version = OF_DT_VERSION;
2100 /* Version 16 is not backward compatible */
2101 hdr->last_comp_version = 0x10;
2102
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05002103 /* Copy the reserve map in */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002104 memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
2105
2106#ifdef DEBUG_PROM
2107 {
2108 int i;
2109 prom_printf("reserved memory map:\n");
2110 for (i = 0; i < RELOC(mem_reserve_cnt); i++)
2111 prom_printf(" %x - %x\n",
2112 RELOC(mem_reserve_map)[i].base,
2113 RELOC(mem_reserve_map)[i].size);
2114 }
2115#endif
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05002116 /* Bump mem_reserve_cnt to cause further reservations to fail
2117 * since it's too late.
2118 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002119 RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
2120
2121 prom_printf("Device tree strings 0x%x -> 0x%x\n",
2122 RELOC(dt_string_start), RELOC(dt_string_end));
2123 prom_printf("Device tree struct 0x%x -> 0x%x\n",
2124 RELOC(dt_struct_start), RELOC(dt_struct_end));
2125
2126}
2127
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002128#ifdef CONFIG_PPC_MAPLE
2129/* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2130 * The values are bad, and it doesn't even have the right number of cells. */
2131static void __init fixup_device_tree_maple(void)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002132{
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002133 phandle isa;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002134 u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002135 u32 isa_ranges[6];
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002136 char *name;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002137
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002138 name = "/ht@0/isa@4";
2139 isa = call_prom("finddevice", 1, 1, ADDR(name));
2140 if (!PHANDLE_VALID(isa)) {
2141 name = "/ht@0/isa@6";
2142 isa = call_prom("finddevice", 1, 1, ADDR(name));
2143 rloc = 0x01003000; /* IO space; PCI device = 6 */
2144 }
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002145 if (!PHANDLE_VALID(isa))
2146 return;
2147
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002148 if (prom_getproplen(isa, "ranges") != 12)
2149 return;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002150 if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2151 == PROM_ERROR)
2152 return;
2153
2154 if (isa_ranges[0] != 0x1 ||
2155 isa_ranges[1] != 0xf4000000 ||
2156 isa_ranges[2] != 0x00010000)
2157 return;
2158
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002159 prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002160
2161 isa_ranges[0] = 0x1;
2162 isa_ranges[1] = 0x0;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002163 isa_ranges[2] = rloc;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002164 isa_ranges[3] = 0x0;
2165 isa_ranges[4] = 0x0;
2166 isa_ranges[5] = 0x00010000;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10002167 prom_setprop(isa, name, "ranges",
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002168 isa_ranges, sizeof(isa_ranges));
2169}
Harry Ciao8f101a02009-06-17 16:28:00 -07002170
2171#define CPC925_MC_START 0xf8000000
2172#define CPC925_MC_LENGTH 0x1000000
2173/* The values for memory-controller don't have right number of cells */
2174static void __init fixup_device_tree_maple_memory_controller(void)
2175{
2176 phandle mc;
2177 u32 mc_reg[4];
2178 char *name = "/hostbridge@f8000000";
2179 struct prom_t *_prom = &RELOC(prom);
2180 u32 ac, sc;
2181
2182 mc = call_prom("finddevice", 1, 1, ADDR(name));
2183 if (!PHANDLE_VALID(mc))
2184 return;
2185
2186 if (prom_getproplen(mc, "reg") != 8)
2187 return;
2188
2189 prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
2190 prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
2191 if ((ac != 2) || (sc != 2))
2192 return;
2193
2194 if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
2195 return;
2196
2197 if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
2198 return;
2199
2200 prom_printf("Fixing up bogus hostbridge on Maple...\n");
2201
2202 mc_reg[0] = 0x0;
2203 mc_reg[1] = CPC925_MC_START;
2204 mc_reg[2] = 0x0;
2205 mc_reg[3] = CPC925_MC_LENGTH;
2206 prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
2207}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002208#else
2209#define fixup_device_tree_maple()
Harry Ciao8f101a02009-06-17 16:28:00 -07002210#define fixup_device_tree_maple_memory_controller()
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002211#endif
2212
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002213#ifdef CONFIG_PPC_CHRP
Olaf Heringe4805922007-04-04 18:20:04 +02002214/*
2215 * Pegasos and BriQ lacks the "ranges" property in the isa node
2216 * Pegasos needs decimal IRQ 14/15, not hexadecimal
Olaf Hering556ecf92007-08-18 04:27:17 +10002217 * Pegasos has the IDE configured in legacy mode, but advertised as native
Olaf Heringe4805922007-04-04 18:20:04 +02002218 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002219static void __init fixup_device_tree_chrp(void)
2220{
Olaf Heringe4805922007-04-04 18:20:04 +02002221 phandle ph;
2222 u32 prop[6];
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002223 u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002224 char *name;
2225 int rc;
2226
2227 name = "/pci@80000000/isa@c";
Olaf Heringe4805922007-04-04 18:20:04 +02002228 ph = call_prom("finddevice", 1, 1, ADDR(name));
2229 if (!PHANDLE_VALID(ph)) {
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002230 name = "/pci@ff500000/isa@6";
Olaf Heringe4805922007-04-04 18:20:04 +02002231 ph = call_prom("finddevice", 1, 1, ADDR(name));
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002232 rloc = 0x01003000; /* IO space; PCI device = 6 */
2233 }
Olaf Heringe4805922007-04-04 18:20:04 +02002234 if (PHANDLE_VALID(ph)) {
2235 rc = prom_getproplen(ph, "ranges");
2236 if (rc == 0 || rc == PROM_ERROR) {
2237 prom_printf("Fixing up missing ISA range on Pegasos...\n");
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002238
Olaf Heringe4805922007-04-04 18:20:04 +02002239 prop[0] = 0x1;
2240 prop[1] = 0x0;
2241 prop[2] = rloc;
2242 prop[3] = 0x0;
2243 prop[4] = 0x0;
2244 prop[5] = 0x00010000;
2245 prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2246 }
2247 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002248
Olaf Heringe4805922007-04-04 18:20:04 +02002249 name = "/pci@80000000/ide@C,1";
2250 ph = call_prom("finddevice", 1, 1, ADDR(name));
2251 if (PHANDLE_VALID(ph)) {
2252 prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2253 prop[0] = 14;
2254 prop[1] = 0x0;
Olaf Hering556ecf92007-08-18 04:27:17 +10002255 prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2256 prom_printf("Fixing up IDE class-code on Pegasos...\n");
2257 rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2258 if (rc == sizeof(u32)) {
2259 prop[0] &= ~0x5;
2260 prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2261 }
Olaf Heringe4805922007-04-04 18:20:04 +02002262 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002263}
2264#else
2265#define fixup_device_tree_chrp()
2266#endif
2267
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002268#if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002269static void __init fixup_device_tree_pmac(void)
2270{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002271 phandle u3, i2c, mpic;
2272 u32 u3_rev;
2273 u32 interrupts[2];
2274 u32 parent;
2275
2276 /* Some G5s have a missing interrupt definition, fix it up here */
2277 u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2278 if (!PHANDLE_VALID(u3))
2279 return;
2280 i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2281 if (!PHANDLE_VALID(i2c))
2282 return;
2283 mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2284 if (!PHANDLE_VALID(mpic))
2285 return;
2286
2287 /* check if proper rev of u3 */
2288 if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2289 == PROM_ERROR)
2290 return;
Benjamin Herrenschmidt7d496972005-11-07 14:36:21 +11002291 if (u3_rev < 0x35 || u3_rev > 0x39)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002292 return;
2293 /* does it need fixup ? */
2294 if (prom_getproplen(i2c, "interrupts") > 0)
2295 return;
2296
2297 prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2298
2299 /* interrupt on this revision of u3 is number 0 and level */
2300 interrupts[0] = 0;
2301 interrupts[1] = 1;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002302 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2303 &interrupts, sizeof(interrupts));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002304 parent = (u32)mpic;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002305 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2306 &parent, sizeof(parent));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002307}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002308#else
2309#define fixup_device_tree_pmac()
2310#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002311
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002312#ifdef CONFIG_PPC_EFIKA
Grant Likely94d2dde2008-01-24 22:25:32 -07002313/*
2314 * The MPC5200 FEC driver requires an phy-handle property to tell it how
2315 * to talk to the phy. If the phy-handle property is missing, then this
2316 * function is called to add the appropriate nodes and link it to the
2317 * ethernet node.
2318 */
2319static void __init fixup_device_tree_efika_add_phy(void)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002320{
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002321 u32 node;
2322 char prop[64];
Grant Likely94d2dde2008-01-24 22:25:32 -07002323 int rv;
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002324
Grant Likely94d2dde2008-01-24 22:25:32 -07002325 /* Check if /builtin/ethernet exists - bail if it doesn't */
2326 node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002327 if (!PHANDLE_VALID(node))
2328 return;
2329
Grant Likely94d2dde2008-01-24 22:25:32 -07002330 /* Check if the phy-handle property exists - bail if it does */
2331 rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2332 if (!rv)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002333 return;
2334
Grant Likely94d2dde2008-01-24 22:25:32 -07002335 /*
2336 * At this point the ethernet device doesn't have a phy described.
2337 * Now we need to add the missing phy node and linkage
2338 */
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002339
Grant Likely94d2dde2008-01-24 22:25:32 -07002340 /* Check for an MDIO bus node - if missing then create one */
Olaf Hering6f4347c2008-01-10 01:06:08 +11002341 node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2342 if (!PHANDLE_VALID(node)) {
2343 prom_printf("Adding Ethernet MDIO node\n");
2344 call_prom("interpret", 1, 1,
2345 " s\" /builtin\" find-device"
2346 " new-device"
2347 " 1 encode-int s\" #address-cells\" property"
2348 " 0 encode-int s\" #size-cells\" property"
Grant Likely94d2dde2008-01-24 22:25:32 -07002349 " s\" mdio\" device-name"
2350 " s\" fsl,mpc5200b-mdio\" encode-string"
Olaf Hering6f4347c2008-01-10 01:06:08 +11002351 " s\" compatible\" property"
2352 " 0xf0003000 0x400 reg"
2353 " 0x2 encode-int"
2354 " 0x5 encode-int encode+"
2355 " 0x3 encode-int encode+"
2356 " s\" interrupts\" property"
2357 " finish-device");
2358 };
2359
Grant Likely94d2dde2008-01-24 22:25:32 -07002360 /* Check for a PHY device node - if missing then create one and
2361 * give it's phandle to the ethernet node */
2362 node = call_prom("finddevice", 1, 1,
2363 ADDR("/builtin/mdio/ethernet-phy"));
Olaf Hering6f4347c2008-01-10 01:06:08 +11002364 if (!PHANDLE_VALID(node)) {
2365 prom_printf("Adding Ethernet PHY node\n");
2366 call_prom("interpret", 1, 1,
2367 " s\" /builtin/mdio\" find-device"
2368 " new-device"
2369 " s\" ethernet-phy\" device-name"
2370 " 0x10 encode-int s\" reg\" property"
2371 " my-self"
2372 " ihandle>phandle"
2373 " finish-device"
2374 " s\" /builtin/ethernet\" find-device"
2375 " encode-int"
2376 " s\" phy-handle\" property"
2377 " device-end");
2378 }
Grant Likely94d2dde2008-01-24 22:25:32 -07002379}
Olaf Hering6f4347c2008-01-10 01:06:08 +11002380
Grant Likely94d2dde2008-01-24 22:25:32 -07002381static void __init fixup_device_tree_efika(void)
2382{
2383 int sound_irq[3] = { 2, 2, 0 };
2384 int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2385 3,4,0, 3,5,0, 3,6,0, 3,7,0,
2386 3,8,0, 3,9,0, 3,10,0, 3,11,0,
2387 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2388 u32 node;
2389 char prop[64];
2390 int rv, len;
2391
2392 /* Check if we're really running on a EFIKA */
2393 node = call_prom("finddevice", 1, 1, ADDR("/"));
2394 if (!PHANDLE_VALID(node))
2395 return;
2396
2397 rv = prom_getprop(node, "model", prop, sizeof(prop));
2398 if (rv == PROM_ERROR)
2399 return;
2400 if (strcmp(prop, "EFIKA5K2"))
2401 return;
2402
2403 prom_printf("Applying EFIKA device tree fixups\n");
2404
2405 /* Claiming to be 'chrp' is death */
2406 node = call_prom("finddevice", 1, 1, ADDR("/"));
2407 rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2408 if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2409 prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2410
David Woodhouse7f4392c2008-04-14 02:52:38 +10002411 /* CODEGEN,description is exposed in /proc/cpuinfo so
2412 fix that too */
2413 rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2414 if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2415 prom_setprop(node, "/", "CODEGEN,description",
2416 "Efika 5200B PowerPC System",
2417 sizeof("Efika 5200B PowerPC System"));
2418
Grant Likely94d2dde2008-01-24 22:25:32 -07002419 /* Fixup bestcomm interrupts property */
2420 node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2421 if (PHANDLE_VALID(node)) {
2422 len = prom_getproplen(node, "interrupts");
2423 if (len == 12) {
2424 prom_printf("Fixing bestcomm interrupts property\n");
2425 prom_setprop(node, "/builtin/bestcom", "interrupts",
2426 bcomm_irq, sizeof(bcomm_irq));
2427 }
2428 }
2429
2430 /* Fixup sound interrupts property */
2431 node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2432 if (PHANDLE_VALID(node)) {
2433 rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2434 if (rv == PROM_ERROR) {
2435 prom_printf("Adding sound interrupts property\n");
2436 prom_setprop(node, "/builtin/sound", "interrupts",
2437 sound_irq, sizeof(sound_irq));
2438 }
2439 }
2440
2441 /* Make sure ethernet phy-handle property exists */
2442 fixup_device_tree_efika_add_phy();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002443}
2444#else
2445#define fixup_device_tree_efika()
2446#endif
2447
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002448static void __init fixup_device_tree(void)
2449{
2450 fixup_device_tree_maple();
Harry Ciao8f101a02009-06-17 16:28:00 -07002451 fixup_device_tree_maple_memory_controller();
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002452 fixup_device_tree_chrp();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002453 fixup_device_tree_pmac();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002454 fixup_device_tree_efika();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002455}
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002456
2457static void __init prom_find_boot_cpu(void)
2458{
Linas Vepstase788ff12007-09-07 03:45:21 +10002459 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002460 u32 getprop_rval;
2461 ihandle prom_cpu;
2462 phandle cpu_pkg;
2463
Paul Mackerrasa575b802005-10-23 17:23:21 +10002464 _prom->cpu = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002465 if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
Paul Mackerrasa575b802005-10-23 17:23:21 +10002466 return;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002467
2468 cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2469
2470 prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
2471 _prom->cpu = getprop_rval;
2472
Michael Neuling2c48a7d2010-07-27 18:26:21 +00002473 prom_debug("Booting CPU hw index = %lu\n", _prom->cpu);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002474}
2475
2476static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2477{
2478#ifdef CONFIG_BLK_DEV_INITRD
Linas Vepstase788ff12007-09-07 03:45:21 +10002479 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002480
2481 if (r3 && r4 && r4 != 0xdeadbeef) {
2482 unsigned long val;
2483
Michael Ellerman51fae6de2005-12-04 18:39:15 +11002484 RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002485 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2486
2487 val = RELOC(prom_initrd_start);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002488 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2489 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002490 val = RELOC(prom_initrd_end);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002491 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2492 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002493
2494 reserve_mem(RELOC(prom_initrd_start),
2495 RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
2496
2497 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
2498 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
2499 }
2500#endif /* CONFIG_BLK_DEV_INITRD */
2501}
2502
2503/*
2504 * We enter here early on, when the Open Firmware prom is still
2505 * handling exceptions and the MMU hash table for us.
2506 */
2507
2508unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2509 unsigned long pp,
Paul Mackerras549e8152008-08-30 11:43:47 +10002510 unsigned long r6, unsigned long r7,
2511 unsigned long kbase)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002512{
Linas Vepstase788ff12007-09-07 03:45:21 +10002513 struct prom_t *_prom;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002514 unsigned long hdr;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002515
2516#ifdef CONFIG_PPC32
Paul Mackerras549e8152008-08-30 11:43:47 +10002517 unsigned long offset = reloc_offset();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002518 reloc_got2(offset);
2519#endif
2520
2521 _prom = &RELOC(prom);
2522
2523 /*
2524 * First zero the BSS
2525 */
2526 memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
2527
2528 /*
2529 * Init interface to Open Firmware, get some node references,
2530 * like /chosen
2531 */
2532 prom_init_client_services(pp);
2533
2534 /*
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002535 * See if this OF is old enough that we need to do explicit maps
2536 * and other workarounds
2537 */
2538 prom_find_mmu();
2539
2540 /*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002541 * Init prom stdout device
2542 */
2543 prom_init_stdout();
2544
Benjamin Herrenschmidt151a9f42009-03-22 16:04:53 +00002545 prom_printf("Preparing to boot %s", RELOC(linux_banner));
Michael Ellermane7943fb2009-03-04 19:02:01 +00002546
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002547 /*
2548 * Get default machine type. At this point, we do not differentiate
2549 * between pSeries SMP and pSeries LPAR
2550 */
2551 RELOC(of_platform) = prom_find_machine_type();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002552
Paul Mackerras549e8152008-08-30 11:43:47 +10002553#ifndef CONFIG_RELOCATABLE
Olaf Heringadd60ef2006-03-23 22:03:57 +01002554 /* Bail if this is a kdump kernel. */
2555 if (PHYSICAL_START > 0)
2556 prom_panic("Error: You can't boot a kdump kernel from OF!\n");
Paul Mackerras549e8152008-08-30 11:43:47 +10002557#endif
Olaf Heringadd60ef2006-03-23 22:03:57 +01002558
2559 /*
2560 * Check for an initrd
2561 */
2562 prom_check_initrd(r3, r4);
2563
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002564#ifdef CONFIG_PPC_PSERIES
2565 /*
2566 * On pSeries, inform the firmware about our capabilities
2567 */
Paul Mackerras799d6042005-11-10 13:37:51 +11002568 if (RELOC(of_platform) == PLATFORM_PSERIES ||
2569 RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002570 prom_send_capabilities();
2571#endif
2572
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002573 /*
Arnd Bergmannf3f66f52005-10-31 20:08:37 -05002574 * Copy the CPU hold code
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002575 */
Linas Vepstase788ff12007-09-07 03:45:21 +10002576 if (RELOC(of_platform) != PLATFORM_POWERMAC)
Paul Mackerras549e8152008-08-30 11:43:47 +10002577 copy_and_flush(0, kbase, 0x100, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002578
2579 /*
2580 * Do early parsing of command line
2581 */
2582 early_cmdline_parse();
2583
2584 /*
2585 * Initialize memory management within prom_init
2586 */
2587 prom_init_mem();
2588
2589 /*
2590 * Determine which cpu is actually running right _now_
2591 */
2592 prom_find_boot_cpu();
2593
2594 /*
2595 * Initialize display devices
2596 */
2597 prom_check_displays();
2598
2599#ifdef CONFIG_PPC64
2600 /*
2601 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2602 * that uses the allocator, we need to make sure we get the top of memory
2603 * available for us here...
2604 */
2605 if (RELOC(of_platform) == PLATFORM_PSERIES)
2606 prom_initialize_tce_table();
2607#endif
2608
2609 /*
2610 * On non-powermacs, try to instantiate RTAS and puts all CPUs
2611 * in spin-loops. PowerMacs don't have a working RTAS and use
2612 * a different way to spin CPUs
2613 */
2614 if (RELOC(of_platform) != PLATFORM_POWERMAC) {
2615 prom_instantiate_rtas();
2616 prom_hold_cpus();
2617 }
2618
2619 /*
2620 * Fill in some infos for use by the kernel later on
2621 */
Benjamin Krillcf687872009-07-27 22:02:39 +00002622 if (RELOC(prom_memory_limit))
2623 prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
2624 &RELOC(prom_memory_limit),
2625 sizeof(prom_memory_limit));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002626#ifdef CONFIG_PPC64
Jeremy Kerr165785e2006-11-11 17:25:18 +11002627 if (RELOC(prom_iommu_off))
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002628 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2629 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002630
Jeremy Kerr165785e2006-11-11 17:25:18 +11002631 if (RELOC(prom_iommu_force_on))
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002632 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2633 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002634
2635 if (RELOC(prom_tce_alloc_start)) {
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002636 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002637 &RELOC(prom_tce_alloc_start),
2638 sizeof(prom_tce_alloc_start));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002639 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002640 &RELOC(prom_tce_alloc_end),
2641 sizeof(prom_tce_alloc_end));
2642 }
2643#endif
2644
2645 /*
2646 * Fixup any known bugs in the device-tree
2647 */
2648 fixup_device_tree();
2649
2650 /*
2651 * Now finally create the flattened device-tree
2652 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00002653 prom_printf("copying OF device tree...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002654 flatten_device_tree();
2655
Paul Mackerras3825ac02005-11-08 22:48:08 +11002656 /*
2657 * in case stdin is USB and still active on IBM machines...
2658 * Unfortunately quiesce crashes on some powermacs if we have
2659 * closed stdin already (in particular the powerbook 101).
2660 */
2661 if (RELOC(of_platform) != PLATFORM_POWERMAC)
2662 prom_close_stdin();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002663
2664 /*
2665 * Call OF "quiesce" method to shut down pending DMA's from
2666 * devices etc...
2667 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00002668 prom_printf("Calling quiesce...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002669 call_prom("quiesce", 0, 0);
2670
2671 /*
2672 * And finally, call the kernel passing it the flattened device
2673 * tree and NULL as r5, thus triggering the new entry point which
2674 * is common to us and kexec
2675 */
2676 hdr = RELOC(dt_header_start);
2677 prom_printf("returning from prom_init\n");
2678 prom_debug("->dt_header_start=0x%x\n", hdr);
2679
2680#ifdef CONFIG_PPC32
2681 reloc_got2(-offset);
2682#endif
2683
Paul Mackerras549e8152008-08-30 11:43:47 +10002684 __start(hdr, kbase, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002685
2686 return 0;
2687}