blob: d942404779c1d2d871cd7024ae045e4448a2aa44 [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
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000193static unsigned long __initdata alloc_top;
194static unsigned long __initdata alloc_top_high;
195static unsigned long __initdata alloc_bottom;
196static unsigned long __initdata rmo_top;
197static unsigned long __initdata ram_top;
198
199static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
200static int __initdata mem_reserve_cnt;
201
202static cell_t __initdata regbuf[1024];
203
204
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000205/*
206 * Error results ... some OF calls will return "-1" on error, some
207 * will return 0, some will return either. To simplify, here are
208 * macros to use with any ihandle or phandle return value to check if
209 * it is valid
210 */
211
212#define PROM_ERROR (-1u)
213#define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
214#define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
215
216
217/* This is the one and *ONLY* place where we actually call open
218 * firmware.
219 */
220
221static int __init call_prom(const char *service, int nargs, int nret, ...)
222{
223 int i;
224 struct prom_args args;
225 va_list list;
226
227 args.service = ADDR(service);
228 args.nargs = nargs;
229 args.nret = nret;
230
231 va_start(list, nret);
232 for (i = 0; i < nargs; i++)
233 args.args[i] = va_arg(list, prom_arg_t);
234 va_end(list);
235
236 for (i = 0; i < nret; i++)
237 args.args[nargs+i] = 0;
238
Paul Mackerrasc49888202005-10-26 21:52:53 +1000239 if (enter_prom(&args, RELOC(prom_entry)) < 0)
240 return PROM_ERROR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000241
242 return (nret > 0) ? args.args[nargs] : 0;
243}
244
245static int __init call_prom_ret(const char *service, int nargs, int nret,
246 prom_arg_t *rets, ...)
247{
248 int i;
249 struct prom_args args;
250 va_list list;
251
252 args.service = ADDR(service);
253 args.nargs = nargs;
254 args.nret = nret;
255
256 va_start(list, rets);
257 for (i = 0; i < nargs; i++)
258 args.args[i] = va_arg(list, prom_arg_t);
259 va_end(list);
260
261 for (i = 0; i < nret; i++)
Olaf Heringed1189b2005-11-29 14:04:05 +0100262 args.args[nargs+i] = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000263
Paul Mackerrasc49888202005-10-26 21:52:53 +1000264 if (enter_prom(&args, RELOC(prom_entry)) < 0)
265 return PROM_ERROR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000266
267 if (rets != NULL)
268 for (i = 1; i < nret; ++i)
Paul Mackerrasc5200c92005-10-10 22:57:03 +1000269 rets[i-1] = args.args[nargs+i];
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000270
271 return (nret > 0) ? args.args[nargs] : 0;
272}
273
274
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000275static void __init prom_print(const char *msg)
276{
277 const char *p, *q;
278 struct prom_t *_prom = &RELOC(prom);
279
280 if (_prom->stdout == 0)
281 return;
282
283 for (p = msg; *p != 0; p = q) {
284 for (q = p; *q != 0 && *q != '\n'; ++q)
285 ;
286 if (q > p)
287 call_prom("write", 3, 1, _prom->stdout, p, q - p);
288 if (*q == 0)
289 break;
290 ++q;
291 call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
292 }
293}
294
295
296static void __init prom_print_hex(unsigned long val)
297{
298 int i, nibbles = sizeof(val)*2;
299 char buf[sizeof(val)*2+1];
300 struct prom_t *_prom = &RELOC(prom);
301
302 for (i = nibbles-1; i >= 0; i--) {
303 buf[i] = (val & 0xf) + '0';
304 if (buf[i] > '9')
305 buf[i] += ('a'-'0'-10);
306 val >>= 4;
307 }
308 buf[nibbles] = '\0';
309 call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
310}
311
312
313static void __init prom_printf(const char *format, ...)
314{
315 const char *p, *q, *s;
316 va_list args;
317 unsigned long v;
318 struct prom_t *_prom = &RELOC(prom);
319
320 va_start(args, format);
321#ifdef CONFIG_PPC64
322 format = PTRRELOC(format);
323#endif
324 for (p = format; *p != 0; p = q) {
325 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
326 ;
327 if (q > p)
328 call_prom("write", 3, 1, _prom->stdout, p, q - p);
329 if (*q == 0)
330 break;
331 if (*q == '\n') {
332 ++q;
333 call_prom("write", 3, 1, _prom->stdout,
334 ADDR("\r\n"), 2);
335 continue;
336 }
337 ++q;
338 if (*q == 0)
339 break;
340 switch (*q) {
341 case 's':
342 ++q;
343 s = va_arg(args, const char *);
344 prom_print(s);
345 break;
346 case 'x':
347 ++q;
348 v = va_arg(args, unsigned long);
349 prom_print_hex(v);
350 break;
351 }
352 }
353}
354
355
Paul Mackerrasa575b802005-10-23 17:23:21 +1000356static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
357 unsigned long align)
358{
Paul Mackerrasa575b802005-10-23 17:23:21 +1000359 struct prom_t *_prom = &RELOC(prom);
360
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100361 if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
362 /*
363 * Old OF requires we claim physical and virtual separately
364 * and then map explicitly (assuming virtual mode)
365 */
366 int ret;
367 prom_arg_t result;
368
369 ret = call_prom_ret("call-method", 5, 2, &result,
370 ADDR("claim"), _prom->memory,
371 align, size, virt);
372 if (ret != 0 || result == -1)
373 return -1;
374 ret = call_prom_ret("call-method", 5, 2, &result,
375 ADDR("claim"), _prom->mmumap,
376 align, size, virt);
377 if (ret != 0) {
378 call_prom("call-method", 4, 1, ADDR("release"),
379 _prom->memory, size, virt);
380 return -1;
381 }
382 /* the 0x12 is M (coherence) + PP == read/write */
Paul Mackerrasa575b802005-10-23 17:23:21 +1000383 call_prom("call-method", 6, 1,
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100384 ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
385 return virt;
386 }
387 return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
388 (prom_arg_t)align);
Paul Mackerrasa575b802005-10-23 17:23:21 +1000389}
390
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000391static void __init __attribute__((noreturn)) prom_panic(const char *reason)
392{
393#ifdef CONFIG_PPC64
394 reason = PTRRELOC(reason);
395#endif
396 prom_print(reason);
Olaf Heringadd60ef2006-03-23 22:03:57 +0100397 /* Do not call exit because it clears the screen on pmac
398 * it also causes some sort of double-fault on early pmacs */
399 if (RELOC(of_platform) == PLATFORM_POWERMAC)
400 asm("trap\n");
401
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000402 /* ToDo: should put up an SRC here on p/iSeries */
403 call_prom("exit", 0, 0);
404
405 for (;;) /* should never get here */
406 ;
407}
408
409
410static int __init prom_next_node(phandle *nodep)
411{
412 phandle node;
413
414 if ((node = *nodep) != 0
415 && (*nodep = call_prom("child", 1, 1, node)) != 0)
416 return 1;
417 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
418 return 1;
419 for (;;) {
420 if ((node = call_prom("parent", 1, 1, node)) == 0)
421 return 0;
422 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
423 return 1;
424 }
425}
426
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +1100427static int inline prom_getprop(phandle node, const char *pname,
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000428 void *value, size_t valuelen)
429{
430 return call_prom("getprop", 4, 1, node, ADDR(pname),
431 (u32)(unsigned long) value, (u32) valuelen);
432}
433
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +1100434static int inline prom_getproplen(phandle node, const char *pname)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000435{
436 return call_prom("getproplen", 2, 1, node, ADDR(pname));
437}
438
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100439static void add_string(char **str, const char *q)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000440{
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100441 char *p = *str;
442
443 while (*q)
444 *p++ = *q++;
445 *p++ = ' ';
446 *str = p;
447}
448
449static char *tohex(unsigned int x)
450{
451 static char digits[] = "0123456789abcdef";
452 static char result[9];
453 int i;
454
455 result[8] = 0;
456 i = 8;
457 do {
458 --i;
459 result[i] = digits[x & 0xf];
460 x >>= 4;
461 } while (x != 0 && i > 0);
462 return &result[i];
463}
464
465static int __init prom_setprop(phandle node, const char *nodename,
466 const char *pname, void *value, size_t valuelen)
467{
468 char cmd[256], *p;
469
470 if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
471 return call_prom("setprop", 4, 1, node, ADDR(pname),
472 (u32)(unsigned long) value, (u32) valuelen);
473
474 /* gah... setprop doesn't work on longtrail, have to use interpret */
475 p = cmd;
476 add_string(&p, "dev");
477 add_string(&p, nodename);
478 add_string(&p, tohex((u32)(unsigned long) value));
479 add_string(&p, tohex(valuelen));
480 add_string(&p, tohex(ADDR(pname)));
481 add_string(&p, tohex(strlen(RELOC(pname))));
482 add_string(&p, "property");
483 *p = 0;
484 return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000485}
486
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000487/*
488 * Early parsing of the command line passed to the kernel, used for
489 * "mem=x" and the options that affect the iommu
490 */
491static void __init early_cmdline_parse(void)
492{
493 struct prom_t *_prom = &RELOC(prom);
Benjamin Herrenschmidt470407a2006-07-04 14:07:42 +1000494#ifdef CONFIG_PPC64
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100495 const char *opt;
Benjamin Herrenschmidt470407a2006-07-04 14:07:42 +1000496#endif
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100497 char *p;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000498 int l = 0;
499
500 RELOC(prom_cmd_line[0]) = 0;
501 p = RELOC(prom_cmd_line);
502 if ((long)_prom->chosen > 0)
503 l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
504#ifdef CONFIG_CMDLINE
Amos Waterland0e4aa9c2006-06-12 23:45:02 -0400505 if (l <= 0 || p[0] == '\0') /* dbl check */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000506 strlcpy(RELOC(prom_cmd_line),
507 RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
508#endif /* CONFIG_CMDLINE */
509 prom_printf("command line: %s\n", RELOC(prom_cmd_line));
510
511#ifdef CONFIG_PPC64
512 opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
513 if (opt) {
514 prom_printf("iommu opt is: %s\n", opt);
515 opt += 6;
516 while (*opt && *opt == ' ')
517 opt++;
518 if (!strncmp(opt, RELOC("off"), 3))
Jeremy Kerr165785e2006-11-11 17:25:18 +1100519 RELOC(prom_iommu_off) = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000520 else if (!strncmp(opt, RELOC("force"), 5))
Jeremy Kerr165785e2006-11-11 17:25:18 +1100521 RELOC(prom_iommu_force_on) = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000522 }
523#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000524}
525
526#ifdef CONFIG_PPC_PSERIES
527/*
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000528 * There are two methods for telling firmware what our capabilities are.
529 * Newer machines have an "ibm,client-architecture-support" method on the
530 * root node. For older machines, we have to call the "process-elf-header"
531 * method in the /packages/elf-loader node, passing it a fake 32-bit
532 * ELF header containing a couple of PT_NOTE sections that contain
533 * structures that contain various information.
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000534 */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000535
536/*
537 * New method - extensible architecture description vector.
538 *
539 * Because the description vector contains a mix of byte and word
540 * values, we declare it as an unsigned char array, and use this
541 * macro to put word values in.
542 */
543#define W(x) ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
544 ((x) >> 8) & 0xff, (x) & 0xff
545
546/* Option vector bits - generic bits in byte 1 */
547#define OV_IGNORE 0x80 /* ignore this vector */
548#define OV_CESSATION_POLICY 0x40 /* halt if unsupported option present*/
549
550/* Option vector 1: processor architectures supported */
551#define OV1_PPC_2_00 0x80 /* set if we support PowerPC 2.00 */
552#define OV1_PPC_2_01 0x40 /* set if we support PowerPC 2.01 */
553#define OV1_PPC_2_02 0x20 /* set if we support PowerPC 2.02 */
554#define OV1_PPC_2_03 0x10 /* set if we support PowerPC 2.03 */
555#define OV1_PPC_2_04 0x08 /* set if we support PowerPC 2.04 */
556#define OV1_PPC_2_05 0x04 /* set if we support PowerPC 2.05 */
Joel Schopp0cb99012008-06-19 06:23:23 +1000557#define OV1_PPC_2_06 0x02 /* set if we support PowerPC 2.06 */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000558
559/* Option vector 2: Open Firmware options supported */
560#define OV2_REAL_MODE 0x20 /* set if we want OF in real mode */
561
562/* Option vector 3: processor options supported */
563#define OV3_FP 0x80 /* floating point */
564#define OV3_VMX 0x40 /* VMX/Altivec */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100565#define OV3_DFP 0x20 /* decimal FP */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000566
567/* Option vector 5: PAPR/OF options supported */
568#define OV5_LPAR 0x80 /* logical partitioning supported */
569#define OV5_SPLPAR 0x40 /* shared-processor LPAR supported */
570/* ibm,dynamic-reconfiguration-memory property supported */
571#define OV5_DRCONF_MEMORY 0x20
572#define OV5_LARGE_PAGES 0x10 /* large pages supported */
Jake Moilanend8c391a2007-06-08 07:27:11 +1000573#define OV5_DONATE_DEDICATE_CPU 0x02 /* donate dedicated CPU support */
Michael Ellerman014dad92007-05-08 12:58:35 +1000574/* PCIe/MSI support. Without MSI full PCIe is not supported */
575#ifdef CONFIG_PCI_MSI
576#define OV5_MSI 0x01 /* PCIe/MSI support */
577#else
578#define OV5_MSI 0x00
579#endif /* CONFIG_PCI_MSI */
Nathan Fontenot8391e422008-07-24 04:36:38 +1000580#ifdef CONFIG_PPC_SMLPAR
581#define OV5_CMO 0x80 /* Cooperative Memory Overcommitment */
582#else
583#define OV5_CMO 0x00
584#endif
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000585
586/*
587 * The architecture vector has an array of PVR mask/value pairs,
588 * followed by # option vectors - 1, followed by the option vectors.
589 */
590static unsigned char ibm_architecture_vec[] = {
591 W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */
Anton Blanchard03054d52006-04-29 09:51:06 +1000592 W(0xffff0000), W(0x003e0000), /* POWER6 */
Michael Neulinge952e6c2008-06-18 10:47:26 +1000593 W(0xffff0000), W(0x003f0000), /* POWER7 */
Joel Schopp0cb99012008-06-19 06:23:23 +1000594 W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */
Paul Mackerras0efbc182006-11-29 22:31:47 +1100595 W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000596 W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
597 5 - 1, /* 5 option vectors */
598
599 /* option vector 1: processor architectures supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500600 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000601 0, /* don't ignore, don't halt */
602 OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
Joel Schopp0cb99012008-06-19 06:23:23 +1000603 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000604
605 /* option vector 2: Open Firmware options supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500606 34 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000607 OV2_REAL_MODE,
608 0, 0,
609 W(0xffffffff), /* real_base */
610 W(0xffffffff), /* real_size */
611 W(0xffffffff), /* virt_base */
612 W(0xffffffff), /* virt_size */
613 W(0xffffffff), /* load_base */
Anton Blanchard856cc2f2009-03-31 20:14:01 +0000614 W(64), /* 64MB min RMA */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000615 W(0xffffffff), /* full client load */
616 0, /* min RMA percentage of total RAM */
617 48, /* max log_2(hash table size) */
618
619 /* option vector 3: processor options supported */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500620 3 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000621 0, /* don't ignore, don't halt */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100622 OV3_FP | OV3_VMX | OV3_DFP,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000623
624 /* option vector 4: IBM PAPR implementation */
Will Schmidt11e9ed42006-08-25 15:46:59 -0500625 2 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000626 0, /* don't halt */
627
628 /* option vector 5: PAPR/OF options */
Nathan Fontenot8391e422008-07-24 04:36:38 +1000629 5 - 2, /* length */
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000630 0, /* don't ignore, don't halt */
Jake Moilanend8c391a2007-06-08 07:27:11 +1000631 OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
632 OV5_DONATE_DEDICATE_CPU | OV5_MSI,
Nathan Fontenot8391e422008-07-24 04:36:38 +1000633 0,
634 OV5_CMO,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000635};
636
637/* Old method - ELF header with PT_NOTE sections */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000638static struct fake_elf {
639 Elf32_Ehdr elfhdr;
640 Elf32_Phdr phdr[2];
641 struct chrpnote {
642 u32 namesz;
643 u32 descsz;
644 u32 type;
645 char name[8]; /* "PowerPC" */
646 struct chrpdesc {
647 u32 real_mode;
648 u32 real_base;
649 u32 real_size;
650 u32 virt_base;
651 u32 virt_size;
652 u32 load_base;
653 } chrpdesc;
654 } chrpnote;
655 struct rpanote {
656 u32 namesz;
657 u32 descsz;
658 u32 type;
659 char name[24]; /* "IBM,RPA-Client-Config" */
660 struct rpadesc {
661 u32 lpar_affinity;
662 u32 min_rmo_size;
663 u32 min_rmo_percent;
664 u32 max_pft_size;
665 u32 splpar;
666 u32 min_load;
667 u32 new_mem_def;
668 u32 ignore_me;
669 } rpadesc;
670 } rpanote;
Paul Mackerras5663a122008-10-31 22:27:17 +1100671} fake_elf = {
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000672 .elfhdr = {
673 .e_ident = { 0x7f, 'E', 'L', 'F',
674 ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
675 .e_type = ET_EXEC, /* yeah right */
676 .e_machine = EM_PPC,
677 .e_version = EV_CURRENT,
678 .e_phoff = offsetof(struct fake_elf, phdr),
679 .e_phentsize = sizeof(Elf32_Phdr),
680 .e_phnum = 2
681 },
682 .phdr = {
683 [0] = {
684 .p_type = PT_NOTE,
685 .p_offset = offsetof(struct fake_elf, chrpnote),
686 .p_filesz = sizeof(struct chrpnote)
687 }, [1] = {
688 .p_type = PT_NOTE,
689 .p_offset = offsetof(struct fake_elf, rpanote),
690 .p_filesz = sizeof(struct rpanote)
691 }
692 },
693 .chrpnote = {
694 .namesz = sizeof("PowerPC"),
695 .descsz = sizeof(struct chrpdesc),
696 .type = 0x1275,
697 .name = "PowerPC",
698 .chrpdesc = {
699 .real_mode = ~0U, /* ~0 means "don't care" */
700 .real_base = ~0U,
701 .real_size = ~0U,
702 .virt_base = ~0U,
703 .virt_size = ~0U,
704 .load_base = ~0U
705 },
706 },
707 .rpanote = {
708 .namesz = sizeof("IBM,RPA-Client-Config"),
709 .descsz = sizeof(struct rpadesc),
710 .type = 0x12759999,
711 .name = "IBM,RPA-Client-Config",
712 .rpadesc = {
Paul Mackerras5663a122008-10-31 22:27:17 +1100713 .lpar_affinity = 0,
714 .min_rmo_size = 64, /* in megabytes */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000715 .min_rmo_percent = 0,
Paul Mackerras5663a122008-10-31 22:27:17 +1100716 .max_pft_size = 48, /* 2^48 bytes max PFT size */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000717 .splpar = 1,
718 .min_load = ~0U,
Paul Mackerras5663a122008-10-31 22:27:17 +1100719 .new_mem_def = 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000720 }
721 }
722};
723
724static void __init prom_send_capabilities(void)
725{
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000726 ihandle elfloader, root;
727 prom_arg_t ret;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000728
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000729 root = call_prom("open", 1, 1, ADDR("/"));
730 if (root != 0) {
731 /* try calling the ibm,client-architecture-support method */
Anton Blanchard4da727a2009-03-31 20:06:14 +0000732 prom_printf("Calling ibm,client-architecture...");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000733 if (call_prom_ret("call-method", 3, 2, &ret,
734 ADDR("ibm,client-architecture-support"),
Benjamin Herrenschmidt33b74972006-06-07 12:01:32 +1000735 root,
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000736 ADDR(ibm_architecture_vec)) == 0) {
737 /* the call exists... */
738 if (ret)
Anton Blanchard4da727a2009-03-31 20:06:14 +0000739 prom_printf("\nWARNING: ibm,client-architecture"
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000740 "-support call FAILED!\n");
741 call_prom("close", 1, 0, root);
Anton Blanchard4da727a2009-03-31 20:06:14 +0000742 prom_printf(" done\n");
Paul Mackerrasf709bfa2006-04-28 16:28:35 +1000743 return;
744 }
745 call_prom("close", 1, 0, root);
746 }
747
748 /* no ibm,client-architecture-support call, try the old way */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000749 elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
750 if (elfloader == 0) {
751 prom_printf("couldn't open /packages/elf-loader\n");
752 return;
753 }
754 call_prom("call-method", 3, 1, ADDR("process-elf-header"),
755 elfloader, ADDR(&fake_elf));
756 call_prom("close", 1, 0, elfloader);
757}
758#endif
759
760/*
761 * Memory allocation strategy... our layout is normally:
762 *
763 * at 14Mb or more we have vmlinux, then a gap and initrd. In some
764 * rare cases, initrd might end up being before the kernel though.
765 * We assume this won't override the final kernel at 0, we have no
766 * provision to handle that in this version, but it should hopefully
767 * never happen.
768 *
769 * alloc_top is set to the top of RMO, eventually shrink down if the
770 * TCEs overlap
771 *
772 * alloc_bottom is set to the top of kernel/initrd
773 *
774 * from there, allocations are done this way : rtas is allocated
775 * topmost, and the device-tree is allocated from the bottom. We try
776 * to grow the device-tree allocation as we progress. If we can't,
777 * then we fail, we don't currently have a facility to restart
778 * elsewhere, but that shouldn't be necessary.
779 *
780 * Note that calls to reserve_mem have to be done explicitly, memory
781 * allocated with either alloc_up or alloc_down isn't automatically
782 * reserved.
783 */
784
785
786/*
787 * Allocates memory in the RMO upward from the kernel/initrd
788 *
789 * When align is 0, this is a special case, it means to allocate in place
790 * at the current location of alloc_bottom or fail (that is basically
791 * extending the previous allocation). Used for the device-tree flattening
792 */
793static unsigned long __init alloc_up(unsigned long size, unsigned long align)
794{
Paul Mackerrasc49888202005-10-26 21:52:53 +1000795 unsigned long base = RELOC(alloc_bottom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000796 unsigned long addr = 0;
797
Paul Mackerrasc49888202005-10-26 21:52:53 +1000798 if (align)
799 base = _ALIGN_UP(base, align);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000800 prom_debug("alloc_up(%x, %x)\n", size, align);
801 if (RELOC(ram_top) == 0)
802 prom_panic("alloc_up() called with mem not initialized\n");
803
804 if (align)
805 base = _ALIGN_UP(RELOC(alloc_bottom), align);
806 else
807 base = RELOC(alloc_bottom);
808
809 for(; (base + size) <= RELOC(alloc_top);
810 base = _ALIGN_UP(base + 0x100000, align)) {
811 prom_debug(" trying: 0x%x\n\r", base);
812 addr = (unsigned long)prom_claim(base, size, 0);
Paul Mackerrasc49888202005-10-26 21:52:53 +1000813 if (addr != PROM_ERROR && addr != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000814 break;
815 addr = 0;
816 if (align == 0)
817 break;
818 }
819 if (addr == 0)
820 return 0;
821 RELOC(alloc_bottom) = addr;
822
823 prom_debug(" -> %x\n", addr);
824 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
825 prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
826 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
827 prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
828 prom_debug(" ram_top : %x\n", RELOC(ram_top));
829
830 return addr;
831}
832
833/*
834 * Allocates memory downward, either from top of RMO, or if highmem
835 * is set, from the top of RAM. Note that this one doesn't handle
836 * failures. It does claim memory if highmem is not set.
837 */
838static unsigned long __init alloc_down(unsigned long size, unsigned long align,
839 int highmem)
840{
841 unsigned long base, addr = 0;
842
843 prom_debug("alloc_down(%x, %x, %s)\n", size, align,
844 highmem ? RELOC("(high)") : RELOC("(low)"));
845 if (RELOC(ram_top) == 0)
846 prom_panic("alloc_down() called with mem not initialized\n");
847
848 if (highmem) {
849 /* Carve out storage for the TCE table. */
850 addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
851 if (addr <= RELOC(alloc_bottom))
852 return 0;
853 /* Will we bump into the RMO ? If yes, check out that we
854 * didn't overlap existing allocations there, if we did,
855 * we are dead, we must be the first in town !
856 */
857 if (addr < RELOC(rmo_top)) {
858 /* Good, we are first */
859 if (RELOC(alloc_top) == RELOC(rmo_top))
860 RELOC(alloc_top) = RELOC(rmo_top) = addr;
861 else
862 return 0;
863 }
864 RELOC(alloc_top_high) = addr;
865 goto bail;
866 }
867
868 base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
869 for (; base > RELOC(alloc_bottom);
870 base = _ALIGN_DOWN(base - 0x100000, align)) {
871 prom_debug(" trying: 0x%x\n\r", base);
872 addr = (unsigned long)prom_claim(base, size, 0);
Paul Mackerrasc49888202005-10-26 21:52:53 +1000873 if (addr != PROM_ERROR && addr != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000874 break;
875 addr = 0;
876 }
877 if (addr == 0)
878 return 0;
879 RELOC(alloc_top) = addr;
880
881 bail:
882 prom_debug(" -> %x\n", addr);
883 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
884 prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
885 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
886 prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
887 prom_debug(" ram_top : %x\n", RELOC(ram_top));
888
889 return addr;
890}
891
892/*
893 * Parse a "reg" cell
894 */
895static unsigned long __init prom_next_cell(int s, cell_t **cellp)
896{
897 cell_t *p = *cellp;
898 unsigned long r = 0;
899
900 /* Ignore more than 2 cells */
901 while (s > sizeof(unsigned long) / 4) {
902 p++;
903 s--;
904 }
905 r = *p++;
906#ifdef CONFIG_PPC64
Paul Mackerras35499c02005-10-22 16:02:39 +1000907 if (s > 1) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000908 r <<= 32;
909 r |= *(p++);
910 }
911#endif
912 *cellp = p;
913 return r;
914}
915
916/*
917 * Very dumb function for adding to the memory reserve list, but
918 * we don't need anything smarter at this point
919 *
920 * XXX Eventually check for collisions. They should NEVER happen.
921 * If problems seem to show up, it would be a good start to track
922 * them down.
923 */
Michael Ellerman0108d3f2007-05-07 15:58:28 +1000924static void __init reserve_mem(u64 base, u64 size)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000925{
Kumar Galacbbcf342006-01-11 17:57:13 -0600926 u64 top = base + size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000927 unsigned long cnt = RELOC(mem_reserve_cnt);
928
929 if (size == 0)
930 return;
931
932 /* We need to always keep one empty entry so that we
933 * have our terminator with "size" set to 0 since we are
934 * dumb and just copy this entire array to the boot params
935 */
936 base = _ALIGN_DOWN(base, PAGE_SIZE);
937 top = _ALIGN_UP(top, PAGE_SIZE);
938 size = top - base;
939
940 if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
941 prom_panic("Memory reserve map exhausted !\n");
942 RELOC(mem_reserve_map)[cnt].base = base;
943 RELOC(mem_reserve_map)[cnt].size = size;
944 RELOC(mem_reserve_cnt) = cnt + 1;
945}
946
947/*
Adrian Bunkb3c2ffd2006-06-30 18:20:44 +0200948 * Initialize memory allocation mechanism, parse "memory" nodes and
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000949 * obtain that way the top of memory and RMO to setup out local allocator
950 */
951static void __init prom_init_mem(void)
952{
953 phandle node;
954 char *path, type[64];
955 unsigned int plen;
956 cell_t *p, *endp;
957 struct prom_t *_prom = &RELOC(prom);
958 u32 rac, rsc;
959
960 /*
961 * We iterate the memory nodes to find
962 * 1) top of RMO (first node)
963 * 2) top of memory
964 */
965 rac = 2;
966 prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
967 rsc = 1;
968 prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
969 prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
970 prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
971
972 prom_debug("scanning memory:\n");
973 path = RELOC(prom_scratch);
974
975 for (node = 0; prom_next_node(&node); ) {
976 type[0] = 0;
977 prom_getprop(node, "device_type", type, sizeof(type));
978
Paul Mackerrasc49888202005-10-26 21:52:53 +1000979 if (type[0] == 0) {
980 /*
981 * CHRP Longtrail machines have no device_type
982 * on the memory node, so check the name instead...
983 */
984 prom_getprop(node, "name", type, sizeof(type));
985 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000986 if (strcmp(type, RELOC("memory")))
987 continue;
Paul Mackerrasc49888202005-10-26 21:52:53 +1000988
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000989 plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
990 if (plen > sizeof(regbuf)) {
991 prom_printf("memory node too large for buffer !\n");
992 plen = sizeof(regbuf);
993 }
994 p = RELOC(regbuf);
995 endp = p + (plen / sizeof(cell_t));
996
997#ifdef DEBUG_PROM
998 memset(path, 0, PROM_SCRATCH_SIZE);
999 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1000 prom_debug(" node %s :\n", path);
1001#endif /* DEBUG_PROM */
1002
1003 while ((endp - p) >= (rac + rsc)) {
1004 unsigned long base, size;
1005
1006 base = prom_next_cell(rac, &p);
1007 size = prom_next_cell(rsc, &p);
1008
1009 if (size == 0)
1010 continue;
1011 prom_debug(" %x %x\n", base, size);
Benjamin Herrenschmidtab1b55e2006-03-03 10:35:40 +11001012 if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001013 RELOC(rmo_top) = size;
1014 if ((base + size) > RELOC(ram_top))
1015 RELOC(ram_top) = base + size;
1016 }
1017 }
1018
1019 RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
1020
1021 /* Check if we have an initrd after the kernel, if we do move our bottom
1022 * point to after it
1023 */
1024 if (RELOC(prom_initrd_start)) {
1025 if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
1026 RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
1027 }
1028
1029 /*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001030 * Setup our top alloc point, that is top of RMO or top of
1031 * segment 0 when running non-LPAR.
1032 * Some RS64 machines have buggy firmware where claims up at
1033 * 1GB fail. Cap at 768MB as a workaround.
1034 * Since 768MB is plenty of room, and we need to cap to something
1035 * reasonable on 32-bit, cap at 768MB on all machines.
1036 */
1037 if (!RELOC(rmo_top))
1038 RELOC(rmo_top) = RELOC(ram_top);
1039 RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
1040 RELOC(alloc_top) = RELOC(rmo_top);
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001041 RELOC(alloc_top_high) = RELOC(ram_top);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001042
1043 prom_printf("memory layout at init:\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001044 prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1045 prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
1046 prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1047 prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
1048 prom_printf(" ram_top : %x\n", RELOC(ram_top));
1049}
1050
1051
1052/*
1053 * Allocate room for and instantiate RTAS
1054 */
1055static void __init prom_instantiate_rtas(void)
1056{
1057 phandle rtas_node;
1058 ihandle rtas_inst;
1059 u32 base, entry = 0;
1060 u32 size = 0;
1061
1062 prom_debug("prom_instantiate_rtas: start...\n");
1063
1064 rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1065 prom_debug("rtas_node: %x\n", rtas_node);
1066 if (!PHANDLE_VALID(rtas_node))
1067 return;
1068
1069 prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1070 if (size == 0)
1071 return;
1072
1073 base = alloc_down(size, PAGE_SIZE, 0);
1074 if (base == 0) {
1075 prom_printf("RTAS allocation failed !\n");
1076 return;
1077 }
1078
1079 rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1080 if (!IHANDLE_VALID(rtas_inst)) {
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001081 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001082 return;
1083 }
1084
Anton Blanchard1f8737a2009-03-31 20:06:15 +00001085 prom_printf("instantiating rtas at 0x%x...", base);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001086
1087 if (call_prom_ret("call-method", 3, 2, &entry,
1088 ADDR("instantiate-rtas"),
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001089 rtas_inst, base) != 0
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001090 || entry == 0) {
1091 prom_printf(" failed\n");
1092 return;
1093 }
1094 prom_printf(" done\n");
1095
1096 reserve_mem(base, size);
1097
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001098 prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1099 &base, sizeof(base));
1100 prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1101 &entry, sizeof(entry));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001102
1103 prom_debug("rtas base = 0x%x\n", base);
1104 prom_debug("rtas entry = 0x%x\n", entry);
1105 prom_debug("rtas size = 0x%x\n", (long)size);
1106
1107 prom_debug("prom_instantiate_rtas: end...\n");
1108}
1109
1110#ifdef CONFIG_PPC64
1111/*
1112 * Allocate room for and initialize TCE tables
1113 */
1114static void __init prom_initialize_tce_table(void)
1115{
1116 phandle node;
1117 ihandle phb_node;
1118 char compatible[64], type[64], model[64];
1119 char *path = RELOC(prom_scratch);
1120 u64 base, align;
1121 u32 minalign, minsize;
1122 u64 tce_entry, *tce_entryp;
1123 u64 local_alloc_top, local_alloc_bottom;
1124 u64 i;
1125
Jeremy Kerr165785e2006-11-11 17:25:18 +11001126 if (RELOC(prom_iommu_off))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001127 return;
1128
1129 prom_debug("starting prom_initialize_tce_table\n");
1130
1131 /* Cache current top of allocs so we reserve a single block */
1132 local_alloc_top = RELOC(alloc_top_high);
1133 local_alloc_bottom = local_alloc_top;
1134
1135 /* Search all nodes looking for PHBs. */
1136 for (node = 0; prom_next_node(&node); ) {
1137 compatible[0] = 0;
1138 type[0] = 0;
1139 model[0] = 0;
1140 prom_getprop(node, "compatible",
1141 compatible, sizeof(compatible));
1142 prom_getprop(node, "device_type", type, sizeof(type));
1143 prom_getprop(node, "model", model, sizeof(model));
1144
1145 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1146 continue;
1147
Linas Vepstase788ff12007-09-07 03:45:21 +10001148 /* Keep the old logic intact to avoid regression. */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001149 if (compatible[0] != 0) {
1150 if ((strstr(compatible, RELOC("python")) == NULL) &&
1151 (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1152 (strstr(compatible, RELOC("Winnipeg")) == NULL))
1153 continue;
1154 } else if (model[0] != 0) {
1155 if ((strstr(model, RELOC("ython")) == NULL) &&
1156 (strstr(model, RELOC("peedwagon")) == NULL) &&
1157 (strstr(model, RELOC("innipeg")) == NULL))
1158 continue;
1159 }
1160
1161 if (prom_getprop(node, "tce-table-minalign", &minalign,
1162 sizeof(minalign)) == PROM_ERROR)
1163 minalign = 0;
1164 if (prom_getprop(node, "tce-table-minsize", &minsize,
1165 sizeof(minsize)) == PROM_ERROR)
1166 minsize = 4UL << 20;
1167
1168 /*
1169 * Even though we read what OF wants, we just set the table
1170 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
1171 * By doing this, we avoid the pitfalls of trying to DMA to
1172 * MMIO space and the DMA alias hole.
1173 *
1174 * On POWER4, firmware sets the TCE region by assuming
1175 * each TCE table is 8MB. Using this memory for anything
1176 * else will impact performance, so we always allocate 8MB.
1177 * Anton
1178 */
1179 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1180 minsize = 8UL << 20;
1181 else
1182 minsize = 4UL << 20;
1183
1184 /* Align to the greater of the align or size */
1185 align = max(minalign, minsize);
1186 base = alloc_down(minsize, align, 1);
1187 if (base == 0)
1188 prom_panic("ERROR, cannot find space for TCE table.\n");
1189 if (base < local_alloc_bottom)
1190 local_alloc_bottom = base;
1191
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001192 /* It seems OF doesn't null-terminate the path :-( */
Li Zefanaca71ef2007-11-05 13:21:56 +11001193 memset(path, 0, PROM_SCRATCH_SIZE);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001194 /* Call OF to setup the TCE hardware */
1195 if (call_prom("package-to-path", 3, 1, node,
1196 path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1197 prom_printf("package-to-path failed\n");
1198 }
1199
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001200 /* Save away the TCE table attributes for later use. */
1201 prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1202 prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1203
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001204 prom_debug("TCE table: %s\n", path);
1205 prom_debug("\tnode = 0x%x\n", node);
1206 prom_debug("\tbase = 0x%x\n", base);
1207 prom_debug("\tsize = 0x%x\n", minsize);
1208
1209 /* Initialize the table to have a one-to-one mapping
1210 * over the allocated size.
1211 */
Ingo Molnar2b931fb2009-01-06 13:56:52 +00001212 tce_entryp = (u64 *)base;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001213 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1214 tce_entry = (i << PAGE_SHIFT);
1215 tce_entry |= 0x3;
1216 *tce_entryp = tce_entry;
1217 }
1218
1219 prom_printf("opening PHB %s", path);
1220 phb_node = call_prom("open", 1, 1, path);
1221 if (phb_node == 0)
1222 prom_printf("... failed\n");
1223 else
1224 prom_printf("... done\n");
1225
1226 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1227 phb_node, -1, minsize,
1228 (u32) base, (u32) (base >> 32));
1229 call_prom("close", 1, 0, phb_node);
1230 }
1231
1232 reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1233
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001234 /* These are only really needed if there is a memory limit in
1235 * effect, but we don't know so export them always. */
1236 RELOC(prom_tce_alloc_start) = local_alloc_bottom;
1237 RELOC(prom_tce_alloc_end) = local_alloc_top;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001238
1239 /* Flag the first invalid entry */
1240 prom_debug("ending prom_initialize_tce_table\n");
1241}
1242#endif
1243
1244/*
1245 * With CHRP SMP we need to use the OF to start the other processors.
1246 * We can't wait until smp_boot_cpus (the OF is trashed by then)
1247 * so we have to put the processors into a holding pattern controlled
1248 * by the kernel (not OF) before we destroy the OF.
1249 *
1250 * This uses a chunk of low memory, puts some holding pattern
1251 * code there and sends the other processors off to there until
1252 * smp_boot_cpus tells them to do something. The holding pattern
1253 * checks that address until its cpu # is there, when it is that
1254 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
1255 * of setting those values.
1256 *
1257 * We also use physical address 0x4 here to tell when a cpu
1258 * is in its holding pattern code.
1259 *
1260 * -- Cort
1261 */
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001262/*
1263 * We want to reference the copy of __secondary_hold_* in the
1264 * 0 - 0x100 address range
1265 */
1266#define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
1267
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001268static void __init prom_hold_cpus(void)
1269{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001270 unsigned long i;
1271 unsigned int reg;
1272 phandle node;
1273 char type[64];
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001274 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001275 unsigned long *spinloop
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001276 = (void *) LOW_ADDR(__secondary_hold_spinloop);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001277 unsigned long *acknowledge
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001278 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001279 unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001280
1281 prom_debug("prom_hold_cpus: start...\n");
1282 prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
1283 prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
1284 prom_debug(" 1) acknowledge = 0x%x\n",
1285 (unsigned long)acknowledge);
1286 prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
1287 prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
1288
1289 /* Set the common spinloop variable, so all of the secondary cpus
1290 * will block when they are awakened from their OF spinloop.
1291 * This must occur for both SMP and non SMP kernels, since OF will
1292 * be trashed when we move the kernel.
1293 */
1294 *spinloop = 0;
1295
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001296 /* look for cpus */
1297 for (node = 0; prom_next_node(&node); ) {
1298 type[0] = 0;
1299 prom_getprop(node, "device_type", type, sizeof(type));
1300 if (strcmp(type, RELOC("cpu")) != 0)
1301 continue;
1302
1303 /* Skip non-configured cpus. */
1304 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1305 if (strcmp(type, RELOC("okay")) != 0)
1306 continue;
1307
1308 reg = -1;
1309 prom_getprop(node, "reg", &reg, sizeof(reg));
1310
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001311 prom_debug("cpu hw idx = 0x%x\n", reg);
1312
1313 /* Init the acknowledge var which will be reset by
1314 * the secondary cpu when it awakens from its OF
1315 * spinloop.
1316 */
1317 *acknowledge = (unsigned long)-1;
1318
Nathan Lynch7d2f6072008-07-27 15:24:50 +10001319 if (reg != _prom->cpu) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001320 /* Primary Thread of non-boot cpu */
Nathan Lynch7d2f6072008-07-27 15:24:50 +10001321 prom_printf("starting cpu hw idx %x... ", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001322 call_prom("start-cpu", 3, 0, node,
1323 secondary_hold, reg);
1324
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001325 for (i = 0; (i < 100000000) &&
1326 (*acknowledge == ((unsigned long)-1)); i++ )
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001327 mb();
1328
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001329 if (*acknowledge == reg)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001330 prom_printf("done\n");
Paul Mackerrasbbd0abd2005-10-26 21:45:56 +10001331 else
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001332 prom_printf("failed: %x\n", *acknowledge);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001333 }
1334#ifdef CONFIG_SMP
1335 else
Nathan Lynch7d2f6072008-07-27 15:24:50 +10001336 prom_printf("boot cpu hw idx %x\n", reg);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001337#endif /* CONFIG_SMP */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001338 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001339
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001340 prom_debug("prom_hold_cpus: end...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001341}
1342
1343
1344static void __init prom_init_client_services(unsigned long pp)
1345{
1346 struct prom_t *_prom = &RELOC(prom);
1347
1348 /* Get a handle to the prom entry point before anything else */
1349 RELOC(prom_entry) = pp;
1350
1351 /* get a handle for the stdout device */
1352 _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1353 if (!PHANDLE_VALID(_prom->chosen))
1354 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1355
1356 /* get device tree root */
1357 _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1358 if (!PHANDLE_VALID(_prom->root))
1359 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001360
1361 _prom->mmumap = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001362}
1363
Paul Mackerrasa575b802005-10-23 17:23:21 +10001364#ifdef CONFIG_PPC32
1365/*
1366 * For really old powermacs, we need to map things we claim.
1367 * For that, we need the ihandle of the mmu.
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001368 * Also, on the longtrail, we need to work around other bugs.
Paul Mackerrasa575b802005-10-23 17:23:21 +10001369 */
1370static void __init prom_find_mmu(void)
1371{
1372 struct prom_t *_prom = &RELOC(prom);
1373 phandle oprom;
1374 char version[64];
1375
1376 oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1377 if (!PHANDLE_VALID(oprom))
1378 return;
1379 if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1380 return;
1381 version[sizeof(version) - 1] = 0;
Paul Mackerrasa575b802005-10-23 17:23:21 +10001382 /* XXX might need to add other versions here */
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001383 if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1384 of_workarounds = OF_WA_CLAIM;
1385 else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1386 of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1387 call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1388 } else
Paul Mackerrasa575b802005-10-23 17:23:21 +10001389 return;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001390 _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
Paul Mackerrasa575b802005-10-23 17:23:21 +10001391 prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1392 sizeof(_prom->mmumap));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001393 if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1394 of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001395}
1396#else
1397#define prom_find_mmu()
1398#endif
1399
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001400static void __init prom_init_stdout(void)
1401{
1402 struct prom_t *_prom = &RELOC(prom);
1403 char *path = RELOC(of_stdout_device);
1404 char type[16];
1405 u32 val;
1406
1407 if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1408 prom_panic("cannot find stdout");
1409
1410 _prom->stdout = val;
1411
1412 /* Get the full OF pathname of the stdout device */
1413 memset(path, 0, 256);
1414 call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1415 val = call_prom("instance-to-package", 1, 1, _prom->stdout);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001416 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1417 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001418 prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001419 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1420 path, strlen(path) + 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001421
1422 /* If it's a display, note it */
1423 memset(type, 0, sizeof(type));
1424 prom_getprop(val, "device_type", type, sizeof(type));
1425 if (strcmp(type, RELOC("display")) == 0)
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001426 prom_setprop(val, path, "linux,boot-display", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001427}
1428
1429static void __init prom_close_stdin(void)
1430{
1431 struct prom_t *_prom = &RELOC(prom);
1432 ihandle val;
1433
1434 if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1435 call_prom("close", 1, 0, val);
1436}
1437
1438static int __init prom_find_machine_type(void)
1439{
1440 struct prom_t *_prom = &RELOC(prom);
1441 char compat[256];
1442 int len, i = 0;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001443#ifdef CONFIG_PPC64
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001444 phandle rtas;
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001445 int x;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001446#endif
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001447
1448 /* Look for a PowerMac */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001449 len = prom_getprop(_prom->root, "compatible",
1450 compat, sizeof(compat)-1);
1451 if (len > 0) {
1452 compat[len] = 0;
1453 while (i < len) {
1454 char *p = &compat[i];
1455 int sl = strlen(p);
1456 if (sl == 0)
1457 break;
1458 if (strstr(p, RELOC("Power Macintosh")) ||
Paul Mackerrasa575b802005-10-23 17:23:21 +10001459 strstr(p, RELOC("MacRISC")))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001460 return PLATFORM_POWERMAC;
Arnd Bergmann133dda12006-06-07 12:04:18 +10001461#ifdef CONFIG_PPC64
1462 /* We must make sure we don't detect the IBM Cell
1463 * blades as pSeries due to some firmware issues,
1464 * so we do it here.
1465 */
1466 if (strstr(p, RELOC("IBM,CBEA")) ||
1467 strstr(p, RELOC("IBM,CPBW-1.0")))
1468 return PLATFORM_GENERIC;
1469#endif /* CONFIG_PPC64 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001470 i += sl + 1;
1471 }
1472 }
1473#ifdef CONFIG_PPC64
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001474 /* If not a mac, try to figure out if it's an IBM pSeries or any other
1475 * PAPR compliant platform. We assume it is if :
1476 * - /device_type is "chrp" (please, do NOT use that for future
1477 * non-IBM designs !
1478 * - it has /rtas
1479 */
Michael Ellerman6f806ce2006-04-07 13:56:21 +10001480 len = prom_getprop(_prom->root, "device_type",
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001481 compat, sizeof(compat)-1);
1482 if (len <= 0)
1483 return PLATFORM_GENERIC;
Benjamin Herrenschmidtcb6b2eb2006-05-15 15:46:03 +10001484 if (strcmp(compat, RELOC("chrp")))
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001485 return PLATFORM_GENERIC;
1486
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001487 /* Default to pSeries. We need to know if we are running LPAR */
1488 rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001489 if (!PHANDLE_VALID(rtas))
1490 return PLATFORM_GENERIC;
1491 x = prom_getproplen(rtas, "ibm,hypertas-functions");
1492 if (x != PROM_ERROR) {
Anton Blanchard4da727a2009-03-31 20:06:14 +00001493 prom_debug("Hypertas detected, assuming LPAR !\n");
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001494 return PLATFORM_PSERIES_LPAR;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001495 }
1496 return PLATFORM_PSERIES;
1497#else
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001498 return PLATFORM_GENERIC;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001499#endif
1500}
1501
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001502static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1503{
1504 return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1505}
1506
1507/*
1508 * If we have a display that we don't know how to drive,
1509 * we will want to try to execute OF's open method for it
1510 * later. However, OF will probably fall over if we do that
1511 * we've taken over the MMU.
1512 * So we check whether we will need to open the display,
1513 * and if so, open it now.
1514 */
1515static void __init prom_check_displays(void)
1516{
1517 char type[16], *path;
1518 phandle node;
1519 ihandle ih;
1520 int i;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001521
1522 static unsigned char default_colors[] = {
1523 0x00, 0x00, 0x00,
1524 0x00, 0x00, 0xaa,
1525 0x00, 0xaa, 0x00,
1526 0x00, 0xaa, 0xaa,
1527 0xaa, 0x00, 0x00,
1528 0xaa, 0x00, 0xaa,
1529 0xaa, 0xaa, 0x00,
1530 0xaa, 0xaa, 0xaa,
1531 0x55, 0x55, 0x55,
1532 0x55, 0x55, 0xff,
1533 0x55, 0xff, 0x55,
1534 0x55, 0xff, 0xff,
1535 0xff, 0x55, 0x55,
1536 0xff, 0x55, 0xff,
1537 0xff, 0xff, 0x55,
1538 0xff, 0xff, 0xff
1539 };
1540 const unsigned char *clut;
1541
Anton Blanchard4da727a2009-03-31 20:06:14 +00001542 prom_debug("Looking for displays\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001543 for (node = 0; prom_next_node(&node); ) {
1544 memset(type, 0, sizeof(type));
1545 prom_getprop(node, "device_type", type, sizeof(type));
1546 if (strcmp(type, RELOC("display")) != 0)
1547 continue;
1548
1549 /* It seems OF doesn't null-terminate the path :-( */
1550 path = RELOC(prom_scratch);
1551 memset(path, 0, PROM_SCRATCH_SIZE);
1552
1553 /*
1554 * leave some room at the end of the path for appending extra
1555 * arguments
1556 */
1557 if (call_prom("package-to-path", 3, 1, node, path,
1558 PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1559 continue;
Anton Blanchard1f8737a2009-03-31 20:06:15 +00001560 prom_printf("found display : %s, opening... ", path);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001561
1562 ih = call_prom("open", 1, 1, path);
1563 if (ih == 0) {
1564 prom_printf("failed\n");
1565 continue;
1566 }
1567
1568 /* Success */
1569 prom_printf("done\n");
Paul Mackerrasa23414b2005-11-10 12:00:55 +11001570 prom_setprop(node, path, "linux,opened", NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001571
1572 /* Setup a usable color table when the appropriate
1573 * method is available. Should update this to set-colors */
1574 clut = RELOC(default_colors);
1575 for (i = 0; i < 32; i++, clut += 3)
1576 if (prom_set_color(ih, i, clut[0], clut[1],
1577 clut[2]) != 0)
1578 break;
1579
1580#ifdef CONFIG_LOGO_LINUX_CLUT224
1581 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1582 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1583 if (prom_set_color(ih, i + 32, clut[0], clut[1],
1584 clut[2]) != 0)
1585 break;
1586#endif /* CONFIG_LOGO_LINUX_CLUT224 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001587 }
1588}
1589
1590
1591/* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1592static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
1593 unsigned long needed, unsigned long align)
1594{
1595 void *ret;
1596
1597 *mem_start = _ALIGN(*mem_start, align);
1598 while ((*mem_start + needed) > *mem_end) {
1599 unsigned long room, chunk;
1600
1601 prom_debug("Chunk exhausted, claiming more at %x...\n",
1602 RELOC(alloc_bottom));
1603 room = RELOC(alloc_top) - RELOC(alloc_bottom);
1604 if (room > DEVTREE_CHUNK_SIZE)
1605 room = DEVTREE_CHUNK_SIZE;
1606 if (room < PAGE_SIZE)
1607 prom_panic("No memory for flatten_device_tree (no room)");
1608 chunk = alloc_up(room, 0);
1609 if (chunk == 0)
1610 prom_panic("No memory for flatten_device_tree (claim failed)");
1611 *mem_end = RELOC(alloc_top);
1612 }
1613
1614 ret = (void *)*mem_start;
1615 *mem_start += needed;
1616
1617 return ret;
1618}
1619
1620#define dt_push_token(token, mem_start, mem_end) \
1621 do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
1622
1623static unsigned long __init dt_find_string(char *str)
1624{
1625 char *s, *os;
1626
1627 s = os = (char *)RELOC(dt_string_start);
1628 s += 4;
1629 while (s < (char *)RELOC(dt_string_end)) {
1630 if (strcmp(s, str) == 0)
1631 return s - os;
1632 s += strlen(s) + 1;
1633 }
1634 return 0;
1635}
1636
1637/*
1638 * The Open Firmware 1275 specification states properties must be 31 bytes or
1639 * less, however not all firmwares obey this. Make it 64 bytes to be safe.
1640 */
1641#define MAX_PROPERTY_NAME 64
1642
1643static void __init scan_dt_build_strings(phandle node,
1644 unsigned long *mem_start,
1645 unsigned long *mem_end)
1646{
1647 char *prev_name, *namep, *sstart;
1648 unsigned long soff;
1649 phandle child;
1650
1651 sstart = (char *)RELOC(dt_string_start);
1652
1653 /* get and store all property names */
1654 prev_name = RELOC("");
1655 for (;;) {
1656 /* 64 is max len of name including nul. */
1657 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
1658 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
1659 /* No more nodes: unwind alloc */
1660 *mem_start = (unsigned long)namep;
1661 break;
1662 }
1663
1664 /* skip "name" */
1665 if (strcmp(namep, RELOC("name")) == 0) {
1666 *mem_start = (unsigned long)namep;
1667 prev_name = RELOC("name");
1668 continue;
1669 }
1670 /* get/create string entry */
1671 soff = dt_find_string(namep);
1672 if (soff != 0) {
1673 *mem_start = (unsigned long)namep;
1674 namep = sstart + soff;
1675 } else {
1676 /* Trim off some if we can */
1677 *mem_start = (unsigned long)namep + strlen(namep) + 1;
1678 RELOC(dt_string_end) = *mem_start;
1679 }
1680 prev_name = namep;
1681 }
1682
1683 /* do all our children */
1684 child = call_prom("child", 1, 1, node);
1685 while (child != 0) {
1686 scan_dt_build_strings(child, mem_start, mem_end);
1687 child = call_prom("peer", 1, 1, child);
1688 }
1689}
1690
1691static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
1692 unsigned long *mem_end)
1693{
1694 phandle child;
1695 char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
1696 unsigned long soff;
1697 unsigned char *valp;
1698 static char pname[MAX_PROPERTY_NAME];
Paul Mackerrasc49888202005-10-26 21:52:53 +10001699 int l, room;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001700
1701 dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
1702
1703 /* get the node's full name */
1704 namep = (char *)*mem_start;
Paul Mackerrasc49888202005-10-26 21:52:53 +10001705 room = *mem_end - *mem_start;
1706 if (room > 255)
1707 room = 255;
1708 l = call_prom("package-to-path", 3, 1, node, namep, room);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001709 if (l >= 0) {
1710 /* Didn't fit? Get more room. */
Paul Mackerrasc49888202005-10-26 21:52:53 +10001711 if (l >= room) {
1712 if (l >= *mem_end - *mem_start)
1713 namep = make_room(mem_start, mem_end, l+1, 1);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001714 call_prom("package-to-path", 3, 1, node, namep, l);
1715 }
1716 namep[l] = '\0';
1717
1718 /* Fixup an Apple bug where they have bogus \0 chars in the
Paul Mackerrasa575b802005-10-23 17:23:21 +10001719 * middle of the path in some properties, and extract
1720 * the unit name (everything after the last '/').
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001721 */
Paul Mackerrasa575b802005-10-23 17:23:21 +10001722 for (lp = p = namep, ep = namep + l; p < ep; p++) {
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001723 if (*p == '/')
Paul Mackerrasa575b802005-10-23 17:23:21 +10001724 lp = namep;
1725 else if (*p != 0)
1726 *lp++ = *p;
1727 }
1728 *lp = 0;
1729 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001730 }
1731
1732 /* get it again for debugging */
1733 path = RELOC(prom_scratch);
1734 memset(path, 0, PROM_SCRATCH_SIZE);
1735 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1736
1737 /* get and store all properties */
1738 prev_name = RELOC("");
1739 sstart = (char *)RELOC(dt_string_start);
1740 for (;;) {
1741 if (call_prom("nextprop", 3, 1, node, prev_name,
1742 RELOC(pname)) != 1)
1743 break;
1744
1745 /* skip "name" */
1746 if (strcmp(RELOC(pname), RELOC("name")) == 0) {
1747 prev_name = RELOC("name");
1748 continue;
1749 }
1750
1751 /* find string offset */
1752 soff = dt_find_string(RELOC(pname));
1753 if (soff == 0) {
1754 prom_printf("WARNING: Can't find string index for"
1755 " <%s>, node %s\n", RELOC(pname), path);
1756 break;
1757 }
1758 prev_name = sstart + soff;
1759
1760 /* get length */
1761 l = call_prom("getproplen", 2, 1, node, RELOC(pname));
1762
1763 /* sanity checks */
1764 if (l == PROM_ERROR)
1765 continue;
1766 if (l > MAX_PROPERTY_LENGTH) {
1767 prom_printf("WARNING: ignoring large property ");
1768 /* It seems OF doesn't null-terminate the path :-( */
1769 prom_printf("[%s] ", path);
1770 prom_printf("%s length 0x%x\n", RELOC(pname), l);
1771 continue;
1772 }
1773
1774 /* push property head */
1775 dt_push_token(OF_DT_PROP, mem_start, mem_end);
1776 dt_push_token(l, mem_start, mem_end);
1777 dt_push_token(soff, mem_start, mem_end);
1778
1779 /* push property content */
1780 valp = make_room(mem_start, mem_end, l, 4);
1781 call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
1782 *mem_start = _ALIGN(*mem_start, 4);
1783 }
1784
1785 /* Add a "linux,phandle" property. */
1786 soff = dt_find_string(RELOC("linux,phandle"));
1787 if (soff == 0)
1788 prom_printf("WARNING: Can't find string index for"
1789 " <linux-phandle> node %s\n", path);
1790 else {
1791 dt_push_token(OF_DT_PROP, mem_start, mem_end);
1792 dt_push_token(4, mem_start, mem_end);
1793 dt_push_token(soff, mem_start, mem_end);
1794 valp = make_room(mem_start, mem_end, 4, 4);
1795 *(u32 *)valp = node;
1796 }
1797
1798 /* do all our children */
1799 child = call_prom("child", 1, 1, node);
1800 while (child != 0) {
1801 scan_dt_build_struct(child, mem_start, mem_end);
1802 child = call_prom("peer", 1, 1, child);
1803 }
1804
1805 dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
1806}
1807
1808static void __init flatten_device_tree(void)
1809{
1810 phandle root;
1811 unsigned long mem_start, mem_end, room;
1812 struct boot_param_header *hdr;
1813 struct prom_t *_prom = &RELOC(prom);
1814 char *namep;
1815 u64 *rsvmap;
1816
1817 /*
1818 * Check how much room we have between alloc top & bottom (+/- a
1819 * few pages), crop to 4Mb, as this is our "chuck" size
1820 */
1821 room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
1822 if (room > DEVTREE_CHUNK_SIZE)
1823 room = DEVTREE_CHUNK_SIZE;
1824 prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
1825
1826 /* Now try to claim that */
1827 mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
1828 if (mem_start == 0)
1829 prom_panic("Can't allocate initial device-tree chunk\n");
1830 mem_end = RELOC(alloc_top);
1831
1832 /* Get root of tree */
1833 root = call_prom("peer", 1, 1, (phandle)0);
1834 if (root == (phandle)0)
1835 prom_panic ("couldn't get device tree root\n");
1836
1837 /* Build header and make room for mem rsv map */
1838 mem_start = _ALIGN(mem_start, 4);
1839 hdr = make_room(&mem_start, &mem_end,
1840 sizeof(struct boot_param_header), 4);
1841 RELOC(dt_header_start) = (unsigned long)hdr;
1842 rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
1843
1844 /* Start of strings */
1845 mem_start = PAGE_ALIGN(mem_start);
1846 RELOC(dt_string_start) = mem_start;
1847 mem_start += 4; /* hole */
1848
1849 /* Add "linux,phandle" in there, we'll need it */
1850 namep = make_room(&mem_start, &mem_end, 16, 1);
1851 strcpy(namep, RELOC("linux,phandle"));
1852 mem_start = (unsigned long)namep + strlen(namep) + 1;
1853
1854 /* Build string array */
1855 prom_printf("Building dt strings...\n");
1856 scan_dt_build_strings(root, &mem_start, &mem_end);
1857 RELOC(dt_string_end) = mem_start;
1858
1859 /* Build structure */
1860 mem_start = PAGE_ALIGN(mem_start);
1861 RELOC(dt_struct_start) = mem_start;
1862 prom_printf("Building dt structure...\n");
1863 scan_dt_build_struct(root, &mem_start, &mem_end);
1864 dt_push_token(OF_DT_END, &mem_start, &mem_end);
1865 RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
1866
1867 /* Finish header */
1868 hdr->boot_cpuid_phys = _prom->cpu;
1869 hdr->magic = OF_DT_HEADER;
1870 hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
1871 hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
1872 hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
1873 hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
1874 hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
1875 hdr->version = OF_DT_VERSION;
1876 /* Version 16 is not backward compatible */
1877 hdr->last_comp_version = 0x10;
1878
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05001879 /* Copy the reserve map in */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001880 memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
1881
1882#ifdef DEBUG_PROM
1883 {
1884 int i;
1885 prom_printf("reserved memory map:\n");
1886 for (i = 0; i < RELOC(mem_reserve_cnt); i++)
1887 prom_printf(" %x - %x\n",
1888 RELOC(mem_reserve_map)[i].base,
1889 RELOC(mem_reserve_map)[i].size);
1890 }
1891#endif
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05001892 /* Bump mem_reserve_cnt to cause further reservations to fail
1893 * since it's too late.
1894 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001895 RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
1896
1897 prom_printf("Device tree strings 0x%x -> 0x%x\n",
1898 RELOC(dt_string_start), RELOC(dt_string_end));
1899 prom_printf("Device tree struct 0x%x -> 0x%x\n",
1900 RELOC(dt_struct_start), RELOC(dt_struct_end));
1901
1902}
1903
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05001904#ifdef CONFIG_PPC_MAPLE
1905/* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
1906 * The values are bad, and it doesn't even have the right number of cells. */
1907static void __init fixup_device_tree_maple(void)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001908{
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05001909 phandle isa;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10001910 u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05001911 u32 isa_ranges[6];
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10001912 char *name;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05001913
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10001914 name = "/ht@0/isa@4";
1915 isa = call_prom("finddevice", 1, 1, ADDR(name));
1916 if (!PHANDLE_VALID(isa)) {
1917 name = "/ht@0/isa@6";
1918 isa = call_prom("finddevice", 1, 1, ADDR(name));
1919 rloc = 0x01003000; /* IO space; PCI device = 6 */
1920 }
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05001921 if (!PHANDLE_VALID(isa))
1922 return;
1923
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10001924 if (prom_getproplen(isa, "ranges") != 12)
1925 return;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05001926 if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
1927 == PROM_ERROR)
1928 return;
1929
1930 if (isa_ranges[0] != 0x1 ||
1931 isa_ranges[1] != 0xf4000000 ||
1932 isa_ranges[2] != 0x00010000)
1933 return;
1934
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10001935 prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05001936
1937 isa_ranges[0] = 0x1;
1938 isa_ranges[1] = 0x0;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10001939 isa_ranges[2] = rloc;
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05001940 isa_ranges[3] = 0x0;
1941 isa_ranges[4] = 0x0;
1942 isa_ranges[5] = 0x00010000;
Benjamin Herrenschmidt980a6512006-07-03 17:22:05 +10001943 prom_setprop(isa, name, "ranges",
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05001944 isa_ranges, sizeof(isa_ranges));
1945}
Harry Ciao8f101a02009-06-17 16:28:00 -07001946
1947#define CPC925_MC_START 0xf8000000
1948#define CPC925_MC_LENGTH 0x1000000
1949/* The values for memory-controller don't have right number of cells */
1950static void __init fixup_device_tree_maple_memory_controller(void)
1951{
1952 phandle mc;
1953 u32 mc_reg[4];
1954 char *name = "/hostbridge@f8000000";
1955 struct prom_t *_prom = &RELOC(prom);
1956 u32 ac, sc;
1957
1958 mc = call_prom("finddevice", 1, 1, ADDR(name));
1959 if (!PHANDLE_VALID(mc))
1960 return;
1961
1962 if (prom_getproplen(mc, "reg") != 8)
1963 return;
1964
1965 prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
1966 prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
1967 if ((ac != 2) || (sc != 2))
1968 return;
1969
1970 if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
1971 return;
1972
1973 if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
1974 return;
1975
1976 prom_printf("Fixing up bogus hostbridge on Maple...\n");
1977
1978 mc_reg[0] = 0x0;
1979 mc_reg[1] = CPC925_MC_START;
1980 mc_reg[2] = 0x0;
1981 mc_reg[3] = CPC925_MC_LENGTH;
1982 prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
1983}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05001984#else
1985#define fixup_device_tree_maple()
Harry Ciao8f101a02009-06-17 16:28:00 -07001986#define fixup_device_tree_maple_memory_controller()
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05001987#endif
1988
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10001989#ifdef CONFIG_PPC_CHRP
Olaf Heringe4805922007-04-04 18:20:04 +02001990/*
1991 * Pegasos and BriQ lacks the "ranges" property in the isa node
1992 * Pegasos needs decimal IRQ 14/15, not hexadecimal
Olaf Hering556ecf92007-08-18 04:27:17 +10001993 * Pegasos has the IDE configured in legacy mode, but advertised as native
Olaf Heringe4805922007-04-04 18:20:04 +02001994 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10001995static void __init fixup_device_tree_chrp(void)
1996{
Olaf Heringe4805922007-04-04 18:20:04 +02001997 phandle ph;
1998 u32 prop[6];
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10001999 u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002000 char *name;
2001 int rc;
2002
2003 name = "/pci@80000000/isa@c";
Olaf Heringe4805922007-04-04 18:20:04 +02002004 ph = call_prom("finddevice", 1, 1, ADDR(name));
2005 if (!PHANDLE_VALID(ph)) {
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002006 name = "/pci@ff500000/isa@6";
Olaf Heringe4805922007-04-04 18:20:04 +02002007 ph = call_prom("finddevice", 1, 1, ADDR(name));
Benjamin Herrenschmidt26c50322006-07-04 14:16:28 +10002008 rloc = 0x01003000; /* IO space; PCI device = 6 */
2009 }
Olaf Heringe4805922007-04-04 18:20:04 +02002010 if (PHANDLE_VALID(ph)) {
2011 rc = prom_getproplen(ph, "ranges");
2012 if (rc == 0 || rc == PROM_ERROR) {
2013 prom_printf("Fixing up missing ISA range on Pegasos...\n");
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002014
Olaf Heringe4805922007-04-04 18:20:04 +02002015 prop[0] = 0x1;
2016 prop[1] = 0x0;
2017 prop[2] = rloc;
2018 prop[3] = 0x0;
2019 prop[4] = 0x0;
2020 prop[5] = 0x00010000;
2021 prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2022 }
2023 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002024
Olaf Heringe4805922007-04-04 18:20:04 +02002025 name = "/pci@80000000/ide@C,1";
2026 ph = call_prom("finddevice", 1, 1, ADDR(name));
2027 if (PHANDLE_VALID(ph)) {
2028 prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2029 prop[0] = 14;
2030 prop[1] = 0x0;
Olaf Hering556ecf92007-08-18 04:27:17 +10002031 prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2032 prom_printf("Fixing up IDE class-code on Pegasos...\n");
2033 rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2034 if (rc == sizeof(u32)) {
2035 prop[0] &= ~0x5;
2036 prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2037 }
Olaf Heringe4805922007-04-04 18:20:04 +02002038 }
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002039}
2040#else
2041#define fixup_device_tree_chrp()
2042#endif
2043
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002044#if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002045static void __init fixup_device_tree_pmac(void)
2046{
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002047 phandle u3, i2c, mpic;
2048 u32 u3_rev;
2049 u32 interrupts[2];
2050 u32 parent;
2051
2052 /* Some G5s have a missing interrupt definition, fix it up here */
2053 u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2054 if (!PHANDLE_VALID(u3))
2055 return;
2056 i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2057 if (!PHANDLE_VALID(i2c))
2058 return;
2059 mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2060 if (!PHANDLE_VALID(mpic))
2061 return;
2062
2063 /* check if proper rev of u3 */
2064 if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2065 == PROM_ERROR)
2066 return;
Benjamin Herrenschmidt7d496972005-11-07 14:36:21 +11002067 if (u3_rev < 0x35 || u3_rev > 0x39)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002068 return;
2069 /* does it need fixup ? */
2070 if (prom_getproplen(i2c, "interrupts") > 0)
2071 return;
2072
2073 prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2074
2075 /* interrupt on this revision of u3 is number 0 and level */
2076 interrupts[0] = 0;
2077 interrupts[1] = 1;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002078 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2079 &interrupts, sizeof(interrupts));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002080 parent = (u32)mpic;
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002081 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2082 &parent, sizeof(parent));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002083}
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002084#else
2085#define fixup_device_tree_pmac()
2086#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002087
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002088#ifdef CONFIG_PPC_EFIKA
Grant Likely94d2dde2008-01-24 22:25:32 -07002089/*
2090 * The MPC5200 FEC driver requires an phy-handle property to tell it how
2091 * to talk to the phy. If the phy-handle property is missing, then this
2092 * function is called to add the appropriate nodes and link it to the
2093 * ethernet node.
2094 */
2095static void __init fixup_device_tree_efika_add_phy(void)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002096{
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002097 u32 node;
2098 char prop[64];
Grant Likely94d2dde2008-01-24 22:25:32 -07002099 int rv;
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002100
Grant Likely94d2dde2008-01-24 22:25:32 -07002101 /* Check if /builtin/ethernet exists - bail if it doesn't */
2102 node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002103 if (!PHANDLE_VALID(node))
2104 return;
2105
Grant Likely94d2dde2008-01-24 22:25:32 -07002106 /* Check if the phy-handle property exists - bail if it does */
2107 rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2108 if (!rv)
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002109 return;
2110
Grant Likely94d2dde2008-01-24 22:25:32 -07002111 /*
2112 * At this point the ethernet device doesn't have a phy described.
2113 * Now we need to add the missing phy node and linkage
2114 */
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002115
Grant Likely94d2dde2008-01-24 22:25:32 -07002116 /* Check for an MDIO bus node - if missing then create one */
Olaf Hering6f4347c2008-01-10 01:06:08 +11002117 node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2118 if (!PHANDLE_VALID(node)) {
2119 prom_printf("Adding Ethernet MDIO node\n");
2120 call_prom("interpret", 1, 1,
2121 " s\" /builtin\" find-device"
2122 " new-device"
2123 " 1 encode-int s\" #address-cells\" property"
2124 " 0 encode-int s\" #size-cells\" property"
Grant Likely94d2dde2008-01-24 22:25:32 -07002125 " s\" mdio\" device-name"
2126 " s\" fsl,mpc5200b-mdio\" encode-string"
Olaf Hering6f4347c2008-01-10 01:06:08 +11002127 " s\" compatible\" property"
2128 " 0xf0003000 0x400 reg"
2129 " 0x2 encode-int"
2130 " 0x5 encode-int encode+"
2131 " 0x3 encode-int encode+"
2132 " s\" interrupts\" property"
2133 " finish-device");
2134 };
2135
Grant Likely94d2dde2008-01-24 22:25:32 -07002136 /* Check for a PHY device node - if missing then create one and
2137 * give it's phandle to the ethernet node */
2138 node = call_prom("finddevice", 1, 1,
2139 ADDR("/builtin/mdio/ethernet-phy"));
Olaf Hering6f4347c2008-01-10 01:06:08 +11002140 if (!PHANDLE_VALID(node)) {
2141 prom_printf("Adding Ethernet PHY node\n");
2142 call_prom("interpret", 1, 1,
2143 " s\" /builtin/mdio\" find-device"
2144 " new-device"
2145 " s\" ethernet-phy\" device-name"
2146 " 0x10 encode-int s\" reg\" property"
2147 " my-self"
2148 " ihandle>phandle"
2149 " finish-device"
2150 " s\" /builtin/ethernet\" find-device"
2151 " encode-int"
2152 " s\" phy-handle\" property"
2153 " device-end");
2154 }
Grant Likely94d2dde2008-01-24 22:25:32 -07002155}
Olaf Hering6f4347c2008-01-10 01:06:08 +11002156
Grant Likely94d2dde2008-01-24 22:25:32 -07002157static void __init fixup_device_tree_efika(void)
2158{
2159 int sound_irq[3] = { 2, 2, 0 };
2160 int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2161 3,4,0, 3,5,0, 3,6,0, 3,7,0,
2162 3,8,0, 3,9,0, 3,10,0, 3,11,0,
2163 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2164 u32 node;
2165 char prop[64];
2166 int rv, len;
2167
2168 /* Check if we're really running on a EFIKA */
2169 node = call_prom("finddevice", 1, 1, ADDR("/"));
2170 if (!PHANDLE_VALID(node))
2171 return;
2172
2173 rv = prom_getprop(node, "model", prop, sizeof(prop));
2174 if (rv == PROM_ERROR)
2175 return;
2176 if (strcmp(prop, "EFIKA5K2"))
2177 return;
2178
2179 prom_printf("Applying EFIKA device tree fixups\n");
2180
2181 /* Claiming to be 'chrp' is death */
2182 node = call_prom("finddevice", 1, 1, ADDR("/"));
2183 rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2184 if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2185 prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2186
David Woodhouse7f4392c2008-04-14 02:52:38 +10002187 /* CODEGEN,description is exposed in /proc/cpuinfo so
2188 fix that too */
2189 rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2190 if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2191 prom_setprop(node, "/", "CODEGEN,description",
2192 "Efika 5200B PowerPC System",
2193 sizeof("Efika 5200B PowerPC System"));
2194
Grant Likely94d2dde2008-01-24 22:25:32 -07002195 /* Fixup bestcomm interrupts property */
2196 node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2197 if (PHANDLE_VALID(node)) {
2198 len = prom_getproplen(node, "interrupts");
2199 if (len == 12) {
2200 prom_printf("Fixing bestcomm interrupts property\n");
2201 prom_setprop(node, "/builtin/bestcom", "interrupts",
2202 bcomm_irq, sizeof(bcomm_irq));
2203 }
2204 }
2205
2206 /* Fixup sound interrupts property */
2207 node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2208 if (PHANDLE_VALID(node)) {
2209 rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2210 if (rv == PROM_ERROR) {
2211 prom_printf("Adding sound interrupts property\n");
2212 prom_setprop(node, "/builtin/sound", "interrupts",
2213 sound_irq, sizeof(sound_irq));
2214 }
2215 }
2216
2217 /* Make sure ethernet phy-handle property exists */
2218 fixup_device_tree_efika_add_phy();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002219}
2220#else
2221#define fixup_device_tree_efika()
2222#endif
2223
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002224static void __init fixup_device_tree(void)
2225{
2226 fixup_device_tree_maple();
Harry Ciao8f101a02009-06-17 16:28:00 -07002227 fixup_device_tree_maple_memory_controller();
Benjamin Herrenschmidte8c0acf2006-07-04 14:06:29 +10002228 fixup_device_tree_chrp();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002229 fixup_device_tree_pmac();
Sylvain Munaut88fd2a92007-02-12 23:13:20 +01002230 fixup_device_tree_efika();
Hollis Blanchard54f4ee12006-05-25 16:36:53 -05002231}
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002232
2233static void __init prom_find_boot_cpu(void)
2234{
Linas Vepstase788ff12007-09-07 03:45:21 +10002235 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002236 u32 getprop_rval;
2237 ihandle prom_cpu;
2238 phandle cpu_pkg;
2239
Paul Mackerrasa575b802005-10-23 17:23:21 +10002240 _prom->cpu = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002241 if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
Paul Mackerrasa575b802005-10-23 17:23:21 +10002242 return;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002243
2244 cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2245
2246 prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
2247 _prom->cpu = getprop_rval;
2248
2249 prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
2250}
2251
2252static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2253{
2254#ifdef CONFIG_BLK_DEV_INITRD
Linas Vepstase788ff12007-09-07 03:45:21 +10002255 struct prom_t *_prom = &RELOC(prom);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002256
2257 if (r3 && r4 && r4 != 0xdeadbeef) {
2258 unsigned long val;
2259
Michael Ellerman51fae6de2005-12-04 18:39:15 +11002260 RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002261 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2262
2263 val = RELOC(prom_initrd_start);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002264 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2265 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002266 val = RELOC(prom_initrd_end);
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002267 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2268 &val, sizeof(val));
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002269
2270 reserve_mem(RELOC(prom_initrd_start),
2271 RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
2272
2273 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
2274 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
2275 }
2276#endif /* CONFIG_BLK_DEV_INITRD */
2277}
2278
2279/*
2280 * We enter here early on, when the Open Firmware prom is still
2281 * handling exceptions and the MMU hash table for us.
2282 */
2283
2284unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2285 unsigned long pp,
Paul Mackerras549e8152008-08-30 11:43:47 +10002286 unsigned long r6, unsigned long r7,
2287 unsigned long kbase)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002288{
Linas Vepstase788ff12007-09-07 03:45:21 +10002289 struct prom_t *_prom;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002290 unsigned long hdr;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002291
2292#ifdef CONFIG_PPC32
Paul Mackerras549e8152008-08-30 11:43:47 +10002293 unsigned long offset = reloc_offset();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002294 reloc_got2(offset);
2295#endif
2296
2297 _prom = &RELOC(prom);
2298
2299 /*
2300 * First zero the BSS
2301 */
2302 memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
2303
2304 /*
2305 * Init interface to Open Firmware, get some node references,
2306 * like /chosen
2307 */
2308 prom_init_client_services(pp);
2309
2310 /*
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002311 * See if this OF is old enough that we need to do explicit maps
2312 * and other workarounds
2313 */
2314 prom_find_mmu();
2315
2316 /*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002317 * Init prom stdout device
2318 */
2319 prom_init_stdout();
2320
Benjamin Herrenschmidt151a9f42009-03-22 16:04:53 +00002321 prom_printf("Preparing to boot %s", RELOC(linux_banner));
Michael Ellermane7943fb2009-03-04 19:02:01 +00002322
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002323 /*
2324 * Get default machine type. At this point, we do not differentiate
2325 * between pSeries SMP and pSeries LPAR
2326 */
2327 RELOC(of_platform) = prom_find_machine_type();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002328
Paul Mackerras549e8152008-08-30 11:43:47 +10002329#ifndef CONFIG_RELOCATABLE
Olaf Heringadd60ef2006-03-23 22:03:57 +01002330 /* Bail if this is a kdump kernel. */
2331 if (PHYSICAL_START > 0)
2332 prom_panic("Error: You can't boot a kdump kernel from OF!\n");
Paul Mackerras549e8152008-08-30 11:43:47 +10002333#endif
Olaf Heringadd60ef2006-03-23 22:03:57 +01002334
2335 /*
2336 * Check for an initrd
2337 */
2338 prom_check_initrd(r3, r4);
2339
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002340#ifdef CONFIG_PPC_PSERIES
2341 /*
2342 * On pSeries, inform the firmware about our capabilities
2343 */
Paul Mackerras799d6042005-11-10 13:37:51 +11002344 if (RELOC(of_platform) == PLATFORM_PSERIES ||
2345 RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002346 prom_send_capabilities();
2347#endif
2348
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002349 /*
Arnd Bergmannf3f66f52005-10-31 20:08:37 -05002350 * Copy the CPU hold code
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002351 */
Linas Vepstase788ff12007-09-07 03:45:21 +10002352 if (RELOC(of_platform) != PLATFORM_POWERMAC)
Paul Mackerras549e8152008-08-30 11:43:47 +10002353 copy_and_flush(0, kbase, 0x100, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002354
2355 /*
2356 * Do early parsing of command line
2357 */
2358 early_cmdline_parse();
2359
2360 /*
2361 * Initialize memory management within prom_init
2362 */
2363 prom_init_mem();
2364
2365 /*
2366 * Determine which cpu is actually running right _now_
2367 */
2368 prom_find_boot_cpu();
2369
2370 /*
2371 * Initialize display devices
2372 */
2373 prom_check_displays();
2374
2375#ifdef CONFIG_PPC64
2376 /*
2377 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2378 * that uses the allocator, we need to make sure we get the top of memory
2379 * available for us here...
2380 */
2381 if (RELOC(of_platform) == PLATFORM_PSERIES)
2382 prom_initialize_tce_table();
2383#endif
2384
2385 /*
2386 * On non-powermacs, try to instantiate RTAS and puts all CPUs
2387 * in spin-loops. PowerMacs don't have a working RTAS and use
2388 * a different way to spin CPUs
2389 */
2390 if (RELOC(of_platform) != PLATFORM_POWERMAC) {
2391 prom_instantiate_rtas();
2392 prom_hold_cpus();
2393 }
2394
2395 /*
2396 * Fill in some infos for use by the kernel later on
2397 */
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002398#ifdef CONFIG_PPC64
Jeremy Kerr165785e2006-11-11 17:25:18 +11002399 if (RELOC(prom_iommu_off))
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002400 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2401 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002402
Jeremy Kerr165785e2006-11-11 17:25:18 +11002403 if (RELOC(prom_iommu_force_on))
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002404 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2405 NULL, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002406
2407 if (RELOC(prom_tce_alloc_start)) {
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002408 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002409 &RELOC(prom_tce_alloc_start),
2410 sizeof(prom_tce_alloc_start));
Paul Mackerrasa23414b2005-11-10 12:00:55 +11002411 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002412 &RELOC(prom_tce_alloc_end),
2413 sizeof(prom_tce_alloc_end));
2414 }
2415#endif
2416
2417 /*
2418 * Fixup any known bugs in the device-tree
2419 */
2420 fixup_device_tree();
2421
2422 /*
2423 * Now finally create the flattened device-tree
2424 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00002425 prom_printf("copying OF device tree...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002426 flatten_device_tree();
2427
Paul Mackerras3825ac02005-11-08 22:48:08 +11002428 /*
2429 * in case stdin is USB and still active on IBM machines...
2430 * Unfortunately quiesce crashes on some powermacs if we have
2431 * closed stdin already (in particular the powerbook 101).
2432 */
2433 if (RELOC(of_platform) != PLATFORM_POWERMAC)
2434 prom_close_stdin();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002435
2436 /*
2437 * Call OF "quiesce" method to shut down pending DMA's from
2438 * devices etc...
2439 */
Anton Blanchard1f8737a2009-03-31 20:06:15 +00002440 prom_printf("Calling quiesce...\n");
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002441 call_prom("quiesce", 0, 0);
2442
2443 /*
2444 * And finally, call the kernel passing it the flattened device
2445 * tree and NULL as r5, thus triggering the new entry point which
2446 * is common to us and kexec
2447 */
2448 hdr = RELOC(dt_header_start);
2449 prom_printf("returning from prom_init\n");
2450 prom_debug("->dt_header_start=0x%x\n", hdr);
2451
2452#ifdef CONFIG_PPC32
2453 reloc_got2(-offset);
2454#endif
2455
Paul Mackerras549e8152008-08-30 11:43:47 +10002456 __start(hdr, kbase, 0);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10002457
2458 return 0;
2459}