powerpc/kexec: Fix build failure on 32-bit SMP
[linux-2.6-block.git] / arch / powerpc / kernel / prom_init.c
CommitLineData
9b6b563c
PM
1/*
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>
9b6b563c
PM
19#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>
9b6b563c
PM
43#include <asm/btext.h>
44#include <asm/sections.h>
45#include <asm/machdep.h>
46
9b6b563c 47#include <linux/linux_logo.h>
9b6b563c
PM
48
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))
a23414be 93#define OF_WORKAROUNDS 0
9b6b563c
PM
94#else
95#define RELOC(x) (x)
96#define ADDR(x) (u32) (x)
a23414be
PM
97#define OF_WORKAROUNDS of_workarounds
98int of_workarounds;
9b6b563c
PM
99#endif
100
a23414be
PM
101#define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */
102#define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */
103
9b6b563c
PM
104#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
9b6b563c
PM
116
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;
a23414be 128 phandle chosen;
9b6b563c
PM
129 int cpu;
130 ihandle stdout;
a575b807 131 ihandle mmumap;
a23414be 132 ihandle memory;
9b6b563c
PM
133};
134
135struct mem_map_entry {
cbbcf340
KG
136 u64 base;
137 u64 size;
9b6b563c
PM
138};
139
140typedef u32 cell_t;
141
142extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
143
144#ifdef CONFIG_PPC64
c4988820 145extern int enter_prom(struct prom_args *args, unsigned long entry);
9b6b563c 146#else
c4988820 147static inline int enter_prom(struct prom_args *args, unsigned long entry)
9b6b563c 148{
c4988820 149 return ((int (*)(struct prom_args *))entry)(args);
9b6b563c
PM
150}
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
165785e5
JK
173static int __initdata prom_iommu_force_on;
174static int __initdata prom_iommu_off;
9b6b563c
PM
175static unsigned long __initdata prom_tce_alloc_start;
176static unsigned long __initdata prom_tce_alloc_end;
177#endif
178
e8222502
BH
179/* 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
9b6b563c
PM
189static int __initdata of_platform;
190
191static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
192
cf68787b
BK
193static unsigned long __initdata prom_memory_limit;
194
9b6b563c
PM
195static unsigned long __initdata alloc_top;
196static unsigned long __initdata alloc_top_high;
197static unsigned long __initdata alloc_bottom;
198static unsigned long __initdata rmo_top;
199static unsigned long __initdata ram_top;
200
201static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
202static int __initdata mem_reserve_cnt;
203
204static cell_t __initdata regbuf[1024];
205
206
9b6b563c
PM
207/*
208 * Error results ... some OF calls will return "-1" on error, some
209 * will return 0, some will return either. To simplify, here are
210 * macros to use with any ihandle or phandle return value to check if
211 * it is valid
212 */
213
214#define PROM_ERROR (-1u)
215#define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
216#define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
217
218
219/* This is the one and *ONLY* place where we actually call open
220 * firmware.
221 */
222
223static int __init call_prom(const char *service, int nargs, int nret, ...)
224{
225 int i;
226 struct prom_args args;
227 va_list list;
228
229 args.service = ADDR(service);
230 args.nargs = nargs;
231 args.nret = nret;
232
233 va_start(list, nret);
234 for (i = 0; i < nargs; i++)
235 args.args[i] = va_arg(list, prom_arg_t);
236 va_end(list);
237
238 for (i = 0; i < nret; i++)
239 args.args[nargs+i] = 0;
240
c4988820
PM
241 if (enter_prom(&args, RELOC(prom_entry)) < 0)
242 return PROM_ERROR;
9b6b563c
PM
243
244 return (nret > 0) ? args.args[nargs] : 0;
245}
246
247static int __init call_prom_ret(const char *service, int nargs, int nret,
248 prom_arg_t *rets, ...)
249{
250 int i;
251 struct prom_args args;
252 va_list list;
253
254 args.service = ADDR(service);
255 args.nargs = nargs;
256 args.nret = nret;
257
258 va_start(list, rets);
259 for (i = 0; i < nargs; i++)
260 args.args[i] = va_arg(list, prom_arg_t);
261 va_end(list);
262
263 for (i = 0; i < nret; i++)
ed1189b7 264 args.args[nargs+i] = 0;
9b6b563c 265
c4988820
PM
266 if (enter_prom(&args, RELOC(prom_entry)) < 0)
267 return PROM_ERROR;
9b6b563c
PM
268
269 if (rets != NULL)
270 for (i = 1; i < nret; ++i)
c5200c90 271 rets[i-1] = args.args[nargs+i];
9b6b563c
PM
272
273 return (nret > 0) ? args.args[nargs] : 0;
274}
275
276
9b6b563c
PM
277static void __init prom_print(const char *msg)
278{
279 const char *p, *q;
280 struct prom_t *_prom = &RELOC(prom);
281
282 if (_prom->stdout == 0)
283 return;
284
285 for (p = msg; *p != 0; p = q) {
286 for (q = p; *q != 0 && *q != '\n'; ++q)
287 ;
288 if (q > p)
289 call_prom("write", 3, 1, _prom->stdout, p, q - p);
290 if (*q == 0)
291 break;
292 ++q;
293 call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
294 }
295}
296
297
298static void __init prom_print_hex(unsigned long val)
299{
300 int i, nibbles = sizeof(val)*2;
301 char buf[sizeof(val)*2+1];
302 struct prom_t *_prom = &RELOC(prom);
303
304 for (i = nibbles-1; i >= 0; i--) {
305 buf[i] = (val & 0xf) + '0';
306 if (buf[i] > '9')
307 buf[i] += ('a'-'0'-10);
308 val >>= 4;
309 }
310 buf[nibbles] = '\0';
311 call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
312}
313
2c48a7d6
MN
314/* max number of decimal digits in an unsigned long */
315#define UL_DIGITS 21
316static void __init prom_print_dec(unsigned long val)
317{
318 int i, size;
319 char buf[UL_DIGITS+1];
320 struct prom_t *_prom = &RELOC(prom);
321
322 for (i = UL_DIGITS-1; i >= 0; i--) {
323 buf[i] = (val % 10) + '0';
324 val = val/10;
325 if (val == 0)
326 break;
327 }
328 /* shift stuff down */
329 size = UL_DIGITS - i;
330 call_prom("write", 3, 1, _prom->stdout, buf+i, size);
331}
9b6b563c
PM
332
333static void __init prom_printf(const char *format, ...)
334{
335 const char *p, *q, *s;
336 va_list args;
337 unsigned long v;
af277149 338 long vs;
9b6b563c
PM
339 struct prom_t *_prom = &RELOC(prom);
340
341 va_start(args, format);
342#ifdef CONFIG_PPC64
343 format = PTRRELOC(format);
344#endif
345 for (p = format; *p != 0; p = q) {
346 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
347 ;
348 if (q > p)
349 call_prom("write", 3, 1, _prom->stdout, p, q - p);
350 if (*q == 0)
351 break;
352 if (*q == '\n') {
353 ++q;
354 call_prom("write", 3, 1, _prom->stdout,
355 ADDR("\r\n"), 2);
356 continue;
357 }
358 ++q;
359 if (*q == 0)
360 break;
361 switch (*q) {
362 case 's':
363 ++q;
364 s = va_arg(args, const char *);
365 prom_print(s);
366 break;
367 case 'x':
368 ++q;
369 v = va_arg(args, unsigned long);
370 prom_print_hex(v);
371 break;
af277149
BH
372 case 'd':
373 ++q;
374 vs = va_arg(args, int);
375 if (vs < 0) {
376 prom_print(RELOC("-"));
377 vs = -vs;
378 }
379 prom_print_dec(vs);
380 break;
2c48a7d6
MN
381 case 'l':
382 ++q;
af277149
BH
383 if (*q == 0)
384 break;
385 else if (*q == 'x') {
386 ++q;
387 v = va_arg(args, unsigned long);
388 prom_print_hex(v);
389 } else if (*q == 'u') { /* '%lu' */
2c48a7d6
MN
390 ++q;
391 v = va_arg(args, unsigned long);
392 prom_print_dec(v);
af277149
BH
393 } else if (*q == 'd') { /* %ld */
394 ++q;
395 vs = va_arg(args, long);
396 if (vs < 0) {
397 prom_print(RELOC("-"));
398 vs = -vs;
399 }
400 prom_print_dec(vs);
2c48a7d6
MN
401 }
402 break;
9b6b563c
PM
403 }
404 }
405}
406
407
a575b807
PM
408static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
409 unsigned long align)
410{
a575b807
PM
411 struct prom_t *_prom = &RELOC(prom);
412
a23414be
PM
413 if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
414 /*
415 * Old OF requires we claim physical and virtual separately
416 * and then map explicitly (assuming virtual mode)
417 */
418 int ret;
419 prom_arg_t result;
420
421 ret = call_prom_ret("call-method", 5, 2, &result,
422 ADDR("claim"), _prom->memory,
423 align, size, virt);
424 if (ret != 0 || result == -1)
425 return -1;
426 ret = call_prom_ret("call-method", 5, 2, &result,
427 ADDR("claim"), _prom->mmumap,
428 align, size, virt);
429 if (ret != 0) {
430 call_prom("call-method", 4, 1, ADDR("release"),
431 _prom->memory, size, virt);
432 return -1;
433 }
434 /* the 0x12 is M (coherence) + PP == read/write */
a575b807 435 call_prom("call-method", 6, 1,
a23414be
PM
436 ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
437 return virt;
438 }
439 return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
440 (prom_arg_t)align);
a575b807
PM
441}
442
9b6b563c
PM
443static void __init __attribute__((noreturn)) prom_panic(const char *reason)
444{
445#ifdef CONFIG_PPC64
446 reason = PTRRELOC(reason);
447#endif
448 prom_print(reason);
add60ef3
OH
449 /* Do not call exit because it clears the screen on pmac
450 * it also causes some sort of double-fault on early pmacs */
451 if (RELOC(of_platform) == PLATFORM_POWERMAC)
452 asm("trap\n");
453
9b6b563c
PM
454 /* ToDo: should put up an SRC here on p/iSeries */
455 call_prom("exit", 0, 0);
456
457 for (;;) /* should never get here */
458 ;
459}
460
461
462static int __init prom_next_node(phandle *nodep)
463{
464 phandle node;
465
466 if ((node = *nodep) != 0
467 && (*nodep = call_prom("child", 1, 1, node)) != 0)
468 return 1;
469 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
470 return 1;
471 for (;;) {
472 if ((node = call_prom("parent", 1, 1, node)) == 0)
473 return 0;
474 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
475 return 1;
476 }
477}
478
21fe3301 479static int inline prom_getprop(phandle node, const char *pname,
9b6b563c
PM
480 void *value, size_t valuelen)
481{
482 return call_prom("getprop", 4, 1, node, ADDR(pname),
483 (u32)(unsigned long) value, (u32) valuelen);
484}
485
21fe3301 486static int inline prom_getproplen(phandle node, const char *pname)
9b6b563c
PM
487{
488 return call_prom("getproplen", 2, 1, node, ADDR(pname));
489}
490
a23414be 491static void add_string(char **str, const char *q)
9b6b563c 492{
a23414be
PM
493 char *p = *str;
494
495 while (*q)
496 *p++ = *q++;
497 *p++ = ' ';
498 *str = p;
499}
500
501static char *tohex(unsigned int x)
502{
503 static char digits[] = "0123456789abcdef";
504 static char result[9];
505 int i;
506
507 result[8] = 0;
508 i = 8;
509 do {
510 --i;
511 result[i] = digits[x & 0xf];
512 x >>= 4;
513 } while (x != 0 && i > 0);
514 return &result[i];
515}
516
517static int __init prom_setprop(phandle node, const char *nodename,
518 const char *pname, void *value, size_t valuelen)
519{
520 char cmd[256], *p;
521
522 if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
523 return call_prom("setprop", 4, 1, node, ADDR(pname),
524 (u32)(unsigned long) value, (u32) valuelen);
525
526 /* gah... setprop doesn't work on longtrail, have to use interpret */
527 p = cmd;
528 add_string(&p, "dev");
529 add_string(&p, nodename);
530 add_string(&p, tohex((u32)(unsigned long) value));
531 add_string(&p, tohex(valuelen));
532 add_string(&p, tohex(ADDR(pname)));
533 add_string(&p, tohex(strlen(RELOC(pname))));
534 add_string(&p, "property");
535 *p = 0;
536 return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
9b6b563c
PM
537}
538
cf68787b
BK
539/* We can't use the standard versions because of RELOC headaches. */
540#define isxdigit(c) (('0' <= (c) && (c) <= '9') \
541 || ('a' <= (c) && (c) <= 'f') \
542 || ('A' <= (c) && (c) <= 'F'))
543
544#define isdigit(c) ('0' <= (c) && (c) <= '9')
545#define islower(c) ('a' <= (c) && (c) <= 'z')
546#define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c))
547
548unsigned long prom_strtoul(const char *cp, const char **endp)
549{
550 unsigned long result = 0, base = 10, value;
551
552 if (*cp == '0') {
553 base = 8;
554 cp++;
555 if (toupper(*cp) == 'X') {
556 cp++;
557 base = 16;
558 }
559 }
560
561 while (isxdigit(*cp) &&
562 (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
563 result = result * base + value;
564 cp++;
565 }
566
567 if (endp)
568 *endp = cp;
569
570 return result;
571}
572
573unsigned long prom_memparse(const char *ptr, const char **retptr)
574{
575 unsigned long ret = prom_strtoul(ptr, retptr);
576 int shift = 0;
577
578 /*
579 * We can't use a switch here because GCC *may* generate a
580 * jump table which won't work, because we're not running at
581 * the address we're linked at.
582 */
583 if ('G' == **retptr || 'g' == **retptr)
584 shift = 30;
585
586 if ('M' == **retptr || 'm' == **retptr)
587 shift = 20;
588
589 if ('K' == **retptr || 'k' == **retptr)
590 shift = 10;
591
592 if (shift) {
593 ret <<= shift;
594 (*retptr)++;
595 }
596
597 return ret;
598}
599
9b6b563c
PM
600/*
601 * Early parsing of the command line passed to the kernel, used for
602 * "mem=x" and the options that affect the iommu
603 */
604static void __init early_cmdline_parse(void)
605{
606 struct prom_t *_prom = &RELOC(prom);
cc5d0189 607 const char *opt;
cf68787b 608
cc5d0189 609 char *p;
9b6b563c
PM
610 int l = 0;
611
612 RELOC(prom_cmd_line[0]) = 0;
613 p = RELOC(prom_cmd_line);
614 if ((long)_prom->chosen > 0)
615 l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
616#ifdef CONFIG_CMDLINE
0e4aa9c2 617 if (l <= 0 || p[0] == '\0') /* dbl check */
9b6b563c
PM
618 strlcpy(RELOC(prom_cmd_line),
619 RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
620#endif /* CONFIG_CMDLINE */
621 prom_printf("command line: %s\n", RELOC(prom_cmd_line));
622
623#ifdef CONFIG_PPC64
624 opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
625 if (opt) {
626 prom_printf("iommu opt is: %s\n", opt);
627 opt += 6;
628 while (*opt && *opt == ' ')
629 opt++;
630 if (!strncmp(opt, RELOC("off"), 3))
165785e5 631 RELOC(prom_iommu_off) = 1;
9b6b563c 632 else if (!strncmp(opt, RELOC("force"), 5))
165785e5 633 RELOC(prom_iommu_force_on) = 1;
9b6b563c
PM
634 }
635#endif
cf68787b
BK
636 opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
637 if (opt) {
638 opt += 4;
639 RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
640#ifdef CONFIG_PPC64
641 /* Align to 16 MB == size of ppc64 large page */
642 RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
643#endif
644 }
9b6b563c
PM
645}
646
647#ifdef CONFIG_PPC_PSERIES
648/*
f709bfac
PM
649 * There are two methods for telling firmware what our capabilities are.
650 * Newer machines have an "ibm,client-architecture-support" method on the
651 * root node. For older machines, we have to call the "process-elf-header"
652 * method in the /packages/elf-loader node, passing it a fake 32-bit
653 * ELF header containing a couple of PT_NOTE sections that contain
654 * structures that contain various information.
9b6b563c 655 */
f709bfac
PM
656
657/*
658 * New method - extensible architecture description vector.
659 *
660 * Because the description vector contains a mix of byte and word
661 * values, we declare it as an unsigned char array, and use this
662 * macro to put word values in.
663 */
664#define W(x) ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
665 ((x) >> 8) & 0xff, (x) & 0xff
666
667/* Option vector bits - generic bits in byte 1 */
668#define OV_IGNORE 0x80 /* ignore this vector */
669#define OV_CESSATION_POLICY 0x40 /* halt if unsupported option present*/
670
671/* Option vector 1: processor architectures supported */
672#define OV1_PPC_2_00 0x80 /* set if we support PowerPC 2.00 */
673#define OV1_PPC_2_01 0x40 /* set if we support PowerPC 2.01 */
674#define OV1_PPC_2_02 0x20 /* set if we support PowerPC 2.02 */
675#define OV1_PPC_2_03 0x10 /* set if we support PowerPC 2.03 */
676#define OV1_PPC_2_04 0x08 /* set if we support PowerPC 2.04 */
677#define OV1_PPC_2_05 0x04 /* set if we support PowerPC 2.05 */
0cb99013 678#define OV1_PPC_2_06 0x02 /* set if we support PowerPC 2.06 */
f709bfac
PM
679
680/* Option vector 2: Open Firmware options supported */
681#define OV2_REAL_MODE 0x20 /* set if we want OF in real mode */
682
683/* Option vector 3: processor options supported */
684#define OV3_FP 0x80 /* floating point */
685#define OV3_VMX 0x40 /* VMX/Altivec */
974a76f5 686#define OV3_DFP 0x20 /* decimal FP */
f709bfac
PM
687
688/* Option vector 5: PAPR/OF options supported */
689#define OV5_LPAR 0x80 /* logical partitioning supported */
690#define OV5_SPLPAR 0x40 /* shared-processor LPAR supported */
691/* ibm,dynamic-reconfiguration-memory property supported */
692#define OV5_DRCONF_MEMORY 0x20
693#define OV5_LARGE_PAGES 0x10 /* large pages supported */
d8c391a5 694#define OV5_DONATE_DEDICATE_CPU 0x02 /* donate dedicated CPU support */
014dad90
ME
695/* PCIe/MSI support. Without MSI full PCIe is not supported */
696#ifdef CONFIG_PCI_MSI
697#define OV5_MSI 0x01 /* PCIe/MSI support */
698#else
699#define OV5_MSI 0x00
700#endif /* CONFIG_PCI_MSI */
8391e42a
NF
701#ifdef CONFIG_PPC_SMLPAR
702#define OV5_CMO 0x80 /* Cooperative Memory Overcommitment */
703#else
704#define OV5_CMO 0x00
705#endif
4b83c330 706#define OV5_TYPE1_AFFINITY 0x80 /* Type 1 NUMA affinity */
f709bfac 707
28bb9ee1 708/* Option Vector 6: IBM PAPR hints */
709#define OV6_LINUX 0x02 /* Linux is our OS */
710
f709bfac
PM
711/*
712 * The architecture vector has an array of PVR mask/value pairs,
713 * followed by # option vectors - 1, followed by the option vectors.
714 */
715static unsigned char ibm_architecture_vec[] = {
716 W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */
03054d51 717 W(0xffff0000), W(0x003e0000), /* POWER6 */
e952e6c4 718 W(0xffff0000), W(0x003f0000), /* POWER7 */
0cb99013 719 W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */
0efbc18a 720 W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
f709bfac 721 W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
28bb9ee1 722 6 - 1, /* 6 option vectors */
f709bfac
PM
723
724 /* option vector 1: processor architectures supported */
11e9ed43 725 3 - 2, /* length */
f709bfac
PM
726 0, /* don't ignore, don't halt */
727 OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
0cb99013 728 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
f709bfac
PM
729
730 /* option vector 2: Open Firmware options supported */
11e9ed43 731 34 - 2, /* length */
f709bfac
PM
732 OV2_REAL_MODE,
733 0, 0,
734 W(0xffffffff), /* real_base */
735 W(0xffffffff), /* real_size */
736 W(0xffffffff), /* virt_base */
737 W(0xffffffff), /* virt_size */
738 W(0xffffffff), /* load_base */
856cc2f0 739 W(64), /* 64MB min RMA */
f709bfac
PM
740 W(0xffffffff), /* full client load */
741 0, /* min RMA percentage of total RAM */
742 48, /* max log_2(hash table size) */
743
744 /* option vector 3: processor options supported */
11e9ed43 745 3 - 2, /* length */
f709bfac 746 0, /* don't ignore, don't halt */
974a76f5 747 OV3_FP | OV3_VMX | OV3_DFP,
f709bfac
PM
748
749 /* option vector 4: IBM PAPR implementation */
11e9ed43 750 2 - 2, /* length */
f709bfac
PM
751 0, /* don't halt */
752
753 /* option vector 5: PAPR/OF options */
28bb9ee1 754 13 - 2, /* length */
f709bfac 755 0, /* don't ignore, don't halt */
d8c391a5
JM
756 OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
757 OV5_DONATE_DEDICATE_CPU | OV5_MSI,
8391e42a
NF
758 0,
759 OV5_CMO,
4b83c330 760 OV5_TYPE1_AFFINITY,
28bb9ee1 761 0,
762 0,
763 0,
efec959f
BH
764 /* WARNING: The offset of the "number of cores" field below
765 * must match by the macro below. Update the definition if
766 * the structure layout changes.
767 */
768#define IBM_ARCH_VEC_NRCORES_OFFSET 100
769 W(NR_CPUS), /* number of cores supported */
28bb9ee1 770
771 /* option vector 6: IBM PAPR hints */
772 4 - 2, /* length */
773 0,
774 0,
775 OV6_LINUX,
776
f709bfac
PM
777};
778
779/* Old method - ELF header with PT_NOTE sections */
9b6b563c
PM
780static struct fake_elf {
781 Elf32_Ehdr elfhdr;
782 Elf32_Phdr phdr[2];
783 struct chrpnote {
784 u32 namesz;
785 u32 descsz;
786 u32 type;
787 char name[8]; /* "PowerPC" */
788 struct chrpdesc {
789 u32 real_mode;
790 u32 real_base;
791 u32 real_size;
792 u32 virt_base;
793 u32 virt_size;
794 u32 load_base;
795 } chrpdesc;
796 } chrpnote;
797 struct rpanote {
798 u32 namesz;
799 u32 descsz;
800 u32 type;
801 char name[24]; /* "IBM,RPA-Client-Config" */
802 struct rpadesc {
803 u32 lpar_affinity;
804 u32 min_rmo_size;
805 u32 min_rmo_percent;
806 u32 max_pft_size;
807 u32 splpar;
808 u32 min_load;
809 u32 new_mem_def;
810 u32 ignore_me;
811 } rpadesc;
812 } rpanote;
5663a123 813} fake_elf = {
9b6b563c
PM
814 .elfhdr = {
815 .e_ident = { 0x7f, 'E', 'L', 'F',
816 ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
817 .e_type = ET_EXEC, /* yeah right */
818 .e_machine = EM_PPC,
819 .e_version = EV_CURRENT,
820 .e_phoff = offsetof(struct fake_elf, phdr),
821 .e_phentsize = sizeof(Elf32_Phdr),
822 .e_phnum = 2
823 },
824 .phdr = {
825 [0] = {
826 .p_type = PT_NOTE,
827 .p_offset = offsetof(struct fake_elf, chrpnote),
828 .p_filesz = sizeof(struct chrpnote)
829 }, [1] = {
830 .p_type = PT_NOTE,
831 .p_offset = offsetof(struct fake_elf, rpanote),
832 .p_filesz = sizeof(struct rpanote)
833 }
834 },
835 .chrpnote = {
836 .namesz = sizeof("PowerPC"),
837 .descsz = sizeof(struct chrpdesc),
838 .type = 0x1275,
839 .name = "PowerPC",
840 .chrpdesc = {
841 .real_mode = ~0U, /* ~0 means "don't care" */
842 .real_base = ~0U,
843 .real_size = ~0U,
844 .virt_base = ~0U,
845 .virt_size = ~0U,
846 .load_base = ~0U
847 },
848 },
849 .rpanote = {
850 .namesz = sizeof("IBM,RPA-Client-Config"),
851 .descsz = sizeof(struct rpadesc),
852 .type = 0x12759999,
853 .name = "IBM,RPA-Client-Config",
854 .rpadesc = {
5663a123
PM
855 .lpar_affinity = 0,
856 .min_rmo_size = 64, /* in megabytes */
9b6b563c 857 .min_rmo_percent = 0,
5663a123 858 .max_pft_size = 48, /* 2^48 bytes max PFT size */
9b6b563c
PM
859 .splpar = 1,
860 .min_load = ~0U,
5663a123 861 .new_mem_def = 0
9b6b563c
PM
862 }
863 }
864};
865
efec959f
BH
866static int __init prom_count_smt_threads(void)
867{
868 phandle node;
869 char type[64];
870 unsigned int plen;
871
872 /* Pick up th first CPU node we can find */
873 for (node = 0; prom_next_node(&node); ) {
874 type[0] = 0;
875 prom_getprop(node, "device_type", type, sizeof(type));
876
877 if (strcmp(type, RELOC("cpu")))
878 continue;
879 /*
880 * There is an entry for each smt thread, each entry being
881 * 4 bytes long. All cpus should have the same number of
882 * smt threads, so return after finding the first.
883 */
884 plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
885 if (plen == PROM_ERROR)
886 break;
887 plen >>= 2;
2c48a7d6 888 prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
efec959f
BH
889
890 /* Sanity check */
891 if (plen < 1 || plen > 64) {
2c48a7d6 892 prom_printf("Threads per core %lu out of bounds, assuming 1\n",
efec959f
BH
893 (unsigned long)plen);
894 return 1;
895 }
896 return plen;
897 }
898 prom_debug("No threads found, assuming 1 per core\n");
899
900 return 1;
901
902}
903
904
9b6b563c
PM
905static void __init prom_send_capabilities(void)
906{
f709bfac
PM
907 ihandle elfloader, root;
908 prom_arg_t ret;
efec959f 909 u32 *cores;
f709bfac
PM
910
911 root = call_prom("open", 1, 1, ADDR("/"));
912 if (root != 0) {
efec959f
BH
913 /* We need to tell the FW about the number of cores we support.
914 *
915 * To do that, we count the number of threads on the first core
916 * (we assume this is the same for all cores) and use it to
917 * divide NR_CPUS.
918 */
919 cores = (u32 *)PTRRELOC(&ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET]);
920 if (*cores != NR_CPUS) {
921 prom_printf("WARNING ! "
2c48a7d6 922 "ibm_architecture_vec structure inconsistent: %lu!\n",
efec959f
BH
923 *cores);
924 } else {
33ad5e4b 925 *cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
2c48a7d6
MN
926 prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
927 *cores, NR_CPUS);
efec959f
BH
928 }
929
f709bfac 930 /* try calling the ibm,client-architecture-support method */
049d0497 931 prom_printf("Calling ibm,client-architecture-support...");
f709bfac
PM
932 if (call_prom_ret("call-method", 3, 2, &ret,
933 ADDR("ibm,client-architecture-support"),
33b74977 934 root,
f709bfac
PM
935 ADDR(ibm_architecture_vec)) == 0) {
936 /* the call exists... */
937 if (ret)
4da727ae 938 prom_printf("\nWARNING: ibm,client-architecture"
f709bfac
PM
939 "-support call FAILED!\n");
940 call_prom("close", 1, 0, root);
4da727ae 941 prom_printf(" done\n");
f709bfac
PM
942 return;
943 }
944 call_prom("close", 1, 0, root);
049d0497 945 prom_printf(" not implemented\n");
f709bfac 946 }
9b6b563c 947
f709bfac 948 /* no ibm,client-architecture-support call, try the old way */
9b6b563c
PM
949 elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
950 if (elfloader == 0) {
951 prom_printf("couldn't open /packages/elf-loader\n");
952 return;
953 }
954 call_prom("call-method", 3, 1, ADDR("process-elf-header"),
955 elfloader, ADDR(&fake_elf));
956 call_prom("close", 1, 0, elfloader);
957}
958#endif
959
960/*
961 * Memory allocation strategy... our layout is normally:
962 *
963 * at 14Mb or more we have vmlinux, then a gap and initrd. In some
964 * rare cases, initrd might end up being before the kernel though.
965 * We assume this won't override the final kernel at 0, we have no
966 * provision to handle that in this version, but it should hopefully
967 * never happen.
968 *
969 * alloc_top is set to the top of RMO, eventually shrink down if the
970 * TCEs overlap
971 *
972 * alloc_bottom is set to the top of kernel/initrd
973 *
974 * from there, allocations are done this way : rtas is allocated
975 * topmost, and the device-tree is allocated from the bottom. We try
976 * to grow the device-tree allocation as we progress. If we can't,
977 * then we fail, we don't currently have a facility to restart
978 * elsewhere, but that shouldn't be necessary.
979 *
980 * Note that calls to reserve_mem have to be done explicitly, memory
981 * allocated with either alloc_up or alloc_down isn't automatically
982 * reserved.
983 */
984
985
986/*
987 * Allocates memory in the RMO upward from the kernel/initrd
988 *
989 * When align is 0, this is a special case, it means to allocate in place
990 * at the current location of alloc_bottom or fail (that is basically
991 * extending the previous allocation). Used for the device-tree flattening
992 */
993static unsigned long __init alloc_up(unsigned long size, unsigned long align)
994{
c4988820 995 unsigned long base = RELOC(alloc_bottom);
9b6b563c
PM
996 unsigned long addr = 0;
997
c4988820
PM
998 if (align)
999 base = _ALIGN_UP(base, align);
9b6b563c
PM
1000 prom_debug("alloc_up(%x, %x)\n", size, align);
1001 if (RELOC(ram_top) == 0)
1002 prom_panic("alloc_up() called with mem not initialized\n");
1003
1004 if (align)
1005 base = _ALIGN_UP(RELOC(alloc_bottom), align);
1006 else
1007 base = RELOC(alloc_bottom);
1008
1009 for(; (base + size) <= RELOC(alloc_top);
1010 base = _ALIGN_UP(base + 0x100000, align)) {
1011 prom_debug(" trying: 0x%x\n\r", base);
1012 addr = (unsigned long)prom_claim(base, size, 0);
c4988820 1013 if (addr != PROM_ERROR && addr != 0)
9b6b563c
PM
1014 break;
1015 addr = 0;
1016 if (align == 0)
1017 break;
1018 }
1019 if (addr == 0)
1020 return 0;
1021 RELOC(alloc_bottom) = addr;
1022
1023 prom_debug(" -> %x\n", addr);
1024 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1025 prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
1026 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1027 prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
1028 prom_debug(" ram_top : %x\n", RELOC(ram_top));
1029
1030 return addr;
1031}
1032
1033/*
1034 * Allocates memory downward, either from top of RMO, or if highmem
1035 * is set, from the top of RAM. Note that this one doesn't handle
1036 * failures. It does claim memory if highmem is not set.
1037 */
1038static unsigned long __init alloc_down(unsigned long size, unsigned long align,
1039 int highmem)
1040{
1041 unsigned long base, addr = 0;
1042
1043 prom_debug("alloc_down(%x, %x, %s)\n", size, align,
1044 highmem ? RELOC("(high)") : RELOC("(low)"));
1045 if (RELOC(ram_top) == 0)
1046 prom_panic("alloc_down() called with mem not initialized\n");
1047
1048 if (highmem) {
1049 /* Carve out storage for the TCE table. */
1050 addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
1051 if (addr <= RELOC(alloc_bottom))
1052 return 0;
1053 /* Will we bump into the RMO ? If yes, check out that we
1054 * didn't overlap existing allocations there, if we did,
1055 * we are dead, we must be the first in town !
1056 */
1057 if (addr < RELOC(rmo_top)) {
1058 /* Good, we are first */
1059 if (RELOC(alloc_top) == RELOC(rmo_top))
1060 RELOC(alloc_top) = RELOC(rmo_top) = addr;
1061 else
1062 return 0;
1063 }
1064 RELOC(alloc_top_high) = addr;
1065 goto bail;
1066 }
1067
1068 base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
1069 for (; base > RELOC(alloc_bottom);
1070 base = _ALIGN_DOWN(base - 0x100000, align)) {
1071 prom_debug(" trying: 0x%x\n\r", base);
1072 addr = (unsigned long)prom_claim(base, size, 0);
c4988820 1073 if (addr != PROM_ERROR && addr != 0)
9b6b563c
PM
1074 break;
1075 addr = 0;
1076 }
1077 if (addr == 0)
1078 return 0;
1079 RELOC(alloc_top) = addr;
1080
1081 bail:
1082 prom_debug(" -> %x\n", addr);
1083 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1084 prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
1085 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1086 prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
1087 prom_debug(" ram_top : %x\n", RELOC(ram_top));
1088
1089 return addr;
1090}
1091
1092/*
1093 * Parse a "reg" cell
1094 */
1095static unsigned long __init prom_next_cell(int s, cell_t **cellp)
1096{
1097 cell_t *p = *cellp;
1098 unsigned long r = 0;
1099
1100 /* Ignore more than 2 cells */
1101 while (s > sizeof(unsigned long) / 4) {
1102 p++;
1103 s--;
1104 }
1105 r = *p++;
1106#ifdef CONFIG_PPC64
35499c01 1107 if (s > 1) {
9b6b563c
PM
1108 r <<= 32;
1109 r |= *(p++);
1110 }
1111#endif
1112 *cellp = p;
1113 return r;
1114}
1115
1116/*
1117 * Very dumb function for adding to the memory reserve list, but
1118 * we don't need anything smarter at this point
1119 *
1120 * XXX Eventually check for collisions. They should NEVER happen.
1121 * If problems seem to show up, it would be a good start to track
1122 * them down.
1123 */
0108d3fe 1124static void __init reserve_mem(u64 base, u64 size)
9b6b563c 1125{
cbbcf340 1126 u64 top = base + size;
9b6b563c
PM
1127 unsigned long cnt = RELOC(mem_reserve_cnt);
1128
1129 if (size == 0)
1130 return;
1131
1132 /* We need to always keep one empty entry so that we
1133 * have our terminator with "size" set to 0 since we are
1134 * dumb and just copy this entire array to the boot params
1135 */
1136 base = _ALIGN_DOWN(base, PAGE_SIZE);
1137 top = _ALIGN_UP(top, PAGE_SIZE);
1138 size = top - base;
1139
1140 if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
1141 prom_panic("Memory reserve map exhausted !\n");
1142 RELOC(mem_reserve_map)[cnt].base = base;
1143 RELOC(mem_reserve_map)[cnt].size = size;
1144 RELOC(mem_reserve_cnt) = cnt + 1;
1145}
1146
1147/*
b3c2ffd5 1148 * Initialize memory allocation mechanism, parse "memory" nodes and
9b6b563c
PM
1149 * obtain that way the top of memory and RMO to setup out local allocator
1150 */
1151static void __init prom_init_mem(void)
1152{
1153 phandle node;
1154 char *path, type[64];
1155 unsigned int plen;
1156 cell_t *p, *endp;
1157 struct prom_t *_prom = &RELOC(prom);
1158 u32 rac, rsc;
1159
1160 /*
1161 * We iterate the memory nodes to find
1162 * 1) top of RMO (first node)
1163 * 2) top of memory
1164 */
1165 rac = 2;
1166 prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
1167 rsc = 1;
1168 prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
1169 prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
1170 prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
1171
1172 prom_debug("scanning memory:\n");
1173 path = RELOC(prom_scratch);
1174
1175 for (node = 0; prom_next_node(&node); ) {
1176 type[0] = 0;
1177 prom_getprop(node, "device_type", type, sizeof(type));
1178
c4988820
PM
1179 if (type[0] == 0) {
1180 /*
1181 * CHRP Longtrail machines have no device_type
1182 * on the memory node, so check the name instead...
1183 */
1184 prom_getprop(node, "name", type, sizeof(type));
1185 }
9b6b563c
PM
1186 if (strcmp(type, RELOC("memory")))
1187 continue;
c4988820 1188
9b6b563c
PM
1189 plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
1190 if (plen > sizeof(regbuf)) {
1191 prom_printf("memory node too large for buffer !\n");
1192 plen = sizeof(regbuf);
1193 }
1194 p = RELOC(regbuf);
1195 endp = p + (plen / sizeof(cell_t));
1196
1197#ifdef DEBUG_PROM
1198 memset(path, 0, PROM_SCRATCH_SIZE);
1199 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1200 prom_debug(" node %s :\n", path);
1201#endif /* DEBUG_PROM */
1202
1203 while ((endp - p) >= (rac + rsc)) {
1204 unsigned long base, size;
1205
1206 base = prom_next_cell(rac, &p);
1207 size = prom_next_cell(rsc, &p);
1208
1209 if (size == 0)
1210 continue;
1211 prom_debug(" %x %x\n", base, size);
ab1b55e2 1212 if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
9b6b563c
PM
1213 RELOC(rmo_top) = size;
1214 if ((base + size) > RELOC(ram_top))
1215 RELOC(ram_top) = base + size;
1216 }
1217 }
1218
1219 RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
1220
1221 /* Check if we have an initrd after the kernel, if we do move our bottom
1222 * point to after it
1223 */
1224 if (RELOC(prom_initrd_start)) {
1225 if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
1226 RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
1227 }
1228
cf68787b
BK
1229 /*
1230 * If prom_memory_limit is set we reduce the upper limits *except* for
1231 * alloc_top_high. This must be the real top of RAM so we can put
1232 * TCE's up there.
1233 */
1234
1235 RELOC(alloc_top_high) = RELOC(ram_top);
1236
1237 if (RELOC(prom_memory_limit)) {
1238 if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
1239 prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
1240 RELOC(prom_memory_limit));
1241 RELOC(prom_memory_limit) = 0;
1242 } else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
1243 prom_printf("Ignoring mem=%x >= ram_top.\n",
1244 RELOC(prom_memory_limit));
1245 RELOC(prom_memory_limit) = 0;
1246 } else {
1247 RELOC(ram_top) = RELOC(prom_memory_limit);
1248 RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
1249 }
1250 }
1251
9b6b563c
PM
1252 /*
1253 * Setup our top alloc point, that is top of RMO or top of
1254 * segment 0 when running non-LPAR.
1255 * Some RS64 machines have buggy firmware where claims up at
1256 * 1GB fail. Cap at 768MB as a workaround.
1257 * Since 768MB is plenty of room, and we need to cap to something
1258 * reasonable on 32-bit, cap at 768MB on all machines.
1259 */
1260 if (!RELOC(rmo_top))
1261 RELOC(rmo_top) = RELOC(ram_top);
1262 RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
1263 RELOC(alloc_top) = RELOC(rmo_top);
2babf5c2 1264 RELOC(alloc_top_high) = RELOC(ram_top);
9b6b563c
PM
1265
1266 prom_printf("memory layout at init:\n");
cf68787b 1267 prom_printf(" memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
9b6b563c
PM
1268 prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1269 prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
1270 prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1271 prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
1272 prom_printf(" ram_top : %x\n", RELOC(ram_top));
1273}
1274
1275
1276/*
1277 * Allocate room for and instantiate RTAS
1278 */
1279static void __init prom_instantiate_rtas(void)
1280{
1281 phandle rtas_node;
1282 ihandle rtas_inst;
1283 u32 base, entry = 0;
1284 u32 size = 0;
1285
1286 prom_debug("prom_instantiate_rtas: start...\n");
1287
1288 rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1289 prom_debug("rtas_node: %x\n", rtas_node);
1290 if (!PHANDLE_VALID(rtas_node))
1291 return;
1292
1293 prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1294 if (size == 0)
1295 return;
1296
1297 base = alloc_down(size, PAGE_SIZE, 0);
1298 if (base == 0) {
1299 prom_printf("RTAS allocation failed !\n");
1300 return;
1301 }
1302
1303 rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1304 if (!IHANDLE_VALID(rtas_inst)) {
a23414be 1305 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
9b6b563c
PM
1306 return;
1307 }
1308
1f8737aa 1309 prom_printf("instantiating rtas at 0x%x...", base);
9b6b563c
PM
1310
1311 if (call_prom_ret("call-method", 3, 2, &entry,
1312 ADDR("instantiate-rtas"),
a23414be 1313 rtas_inst, base) != 0
9b6b563c
PM
1314 || entry == 0) {
1315 prom_printf(" failed\n");
1316 return;
1317 }
1318 prom_printf(" done\n");
1319
1320 reserve_mem(base, size);
1321
a23414be
PM
1322 prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1323 &base, sizeof(base));
1324 prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1325 &entry, sizeof(entry));
9b6b563c
PM
1326
1327 prom_debug("rtas base = 0x%x\n", base);
1328 prom_debug("rtas entry = 0x%x\n", entry);
1329 prom_debug("rtas size = 0x%x\n", (long)size);
1330
1331 prom_debug("prom_instantiate_rtas: end...\n");
1332}
1333
1334#ifdef CONFIG_PPC64
1335/*
1336 * Allocate room for and initialize TCE tables
1337 */
1338static void __init prom_initialize_tce_table(void)
1339{
1340 phandle node;
1341 ihandle phb_node;
1342 char compatible[64], type[64], model[64];
1343 char *path = RELOC(prom_scratch);
1344 u64 base, align;
1345 u32 minalign, minsize;
1346 u64 tce_entry, *tce_entryp;
1347 u64 local_alloc_top, local_alloc_bottom;
1348 u64 i;
1349
165785e5 1350 if (RELOC(prom_iommu_off))
9b6b563c
PM
1351 return;
1352
1353 prom_debug("starting prom_initialize_tce_table\n");
1354
1355 /* Cache current top of allocs so we reserve a single block */
1356 local_alloc_top = RELOC(alloc_top_high);
1357 local_alloc_bottom = local_alloc_top;
1358
1359 /* Search all nodes looking for PHBs. */
1360 for (node = 0; prom_next_node(&node); ) {
1361 compatible[0] = 0;
1362 type[0] = 0;
1363 model[0] = 0;
1364 prom_getprop(node, "compatible",
1365 compatible, sizeof(compatible));
1366 prom_getprop(node, "device_type", type, sizeof(type));
1367 prom_getprop(node, "model", model, sizeof(model));
1368
1369 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1370 continue;
1371
e788ff13 1372 /* Keep the old logic intact to avoid regression. */
9b6b563c
PM
1373 if (compatible[0] != 0) {
1374 if ((strstr(compatible, RELOC("python")) == NULL) &&
1375 (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1376 (strstr(compatible, RELOC("Winnipeg")) == NULL))
1377 continue;
1378 } else if (model[0] != 0) {
1379 if ((strstr(model, RELOC("ython")) == NULL) &&
1380 (strstr(model, RELOC("peedwagon")) == NULL) &&
1381 (strstr(model, RELOC("innipeg")) == NULL))
1382 continue;
1383 }
1384
1385 if (prom_getprop(node, "tce-table-minalign", &minalign,
1386 sizeof(minalign)) == PROM_ERROR)
1387 minalign = 0;
1388 if (prom_getprop(node, "tce-table-minsize", &minsize,
1389 sizeof(minsize)) == PROM_ERROR)
1390 minsize = 4UL << 20;
1391
1392 /*
1393 * Even though we read what OF wants, we just set the table
1394 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
1395 * By doing this, we avoid the pitfalls of trying to DMA to
1396 * MMIO space and the DMA alias hole.
1397 *
1398 * On POWER4, firmware sets the TCE region by assuming
1399 * each TCE table is 8MB. Using this memory for anything
1400 * else will impact performance, so we always allocate 8MB.
1401 * Anton
1402 */
1403 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1404 minsize = 8UL << 20;
1405 else
1406 minsize = 4UL << 20;
1407
1408 /* Align to the greater of the align or size */
1409 align = max(minalign, minsize);
1410 base = alloc_down(minsize, align, 1);
1411 if (base == 0)
1412 prom_panic("ERROR, cannot find space for TCE table.\n");
1413 if (base < local_alloc_bottom)
1414 local_alloc_bottom = base;
1415
9b6b563c 1416 /* It seems OF doesn't null-terminate the path :-( */
aca71ef8 1417 memset(path, 0, PROM_SCRATCH_SIZE);
9b6b563c
PM
1418 /* Call OF to setup the TCE hardware */
1419 if (call_prom("package-to-path", 3, 1, node,
1420 path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1421 prom_printf("package-to-path failed\n");
1422 }
1423
a23414be
PM
1424 /* Save away the TCE table attributes for later use. */
1425 prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1426 prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1427
9b6b563c
PM
1428 prom_debug("TCE table: %s\n", path);
1429 prom_debug("\tnode = 0x%x\n", node);
1430 prom_debug("\tbase = 0x%x\n", base);
1431 prom_debug("\tsize = 0x%x\n", minsize);
1432
1433 /* Initialize the table to have a one-to-one mapping
1434 * over the allocated size.
1435 */
2b931fb6 1436 tce_entryp = (u64 *)base;
9b6b563c
PM
1437 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1438 tce_entry = (i << PAGE_SHIFT);
1439 tce_entry |= 0x3;
1440 *tce_entryp = tce_entry;
1441 }
1442
1443 prom_printf("opening PHB %s", path);
1444 phb_node = call_prom("open", 1, 1, path);
1445 if (phb_node == 0)
1446 prom_printf("... failed\n");
1447 else
1448 prom_printf("... done\n");
1449
1450 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1451 phb_node, -1, minsize,
1452 (u32) base, (u32) (base >> 32));
1453 call_prom("close", 1, 0, phb_node);
1454 }
1455
1456 reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1457
2babf5c2
ME
1458 /* These are only really needed if there is a memory limit in
1459 * effect, but we don't know so export them always. */
1460 RELOC(prom_tce_alloc_start) = local_alloc_bottom;
1461 RELOC(prom_tce_alloc_end) = local_alloc_top;
9b6b563c
PM
1462
1463 /* Flag the first invalid entry */
1464 prom_debug("ending prom_initialize_tce_table\n");
1465}
1466#endif
1467
1468/*
1469 * With CHRP SMP we need to use the OF to start the other processors.
1470 * We can't wait until smp_boot_cpus (the OF is trashed by then)
1471 * so we have to put the processors into a holding pattern controlled
1472 * by the kernel (not OF) before we destroy the OF.
1473 *
1474 * This uses a chunk of low memory, puts some holding pattern
1475 * code there and sends the other processors off to there until
1476 * smp_boot_cpus tells them to do something. The holding pattern
1477 * checks that address until its cpu # is there, when it is that
1478 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
1479 * of setting those values.
1480 *
1481 * We also use physical address 0x4 here to tell when a cpu
1482 * is in its holding pattern code.
1483 *
1484 * -- Cort
1485 */
bbd0abda
PM
1486/*
1487 * We want to reference the copy of __secondary_hold_* in the
1488 * 0 - 0x100 address range
1489 */
1490#define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
1491
9b6b563c
PM
1492static void __init prom_hold_cpus(void)
1493{
9b6b563c
PM
1494 unsigned long i;
1495 unsigned int reg;
1496 phandle node;
1497 char type[64];
bbd0abda 1498 struct prom_t *_prom = &RELOC(prom);
9b6b563c 1499 unsigned long *spinloop
bbd0abda 1500 = (void *) LOW_ADDR(__secondary_hold_spinloop);
9b6b563c 1501 unsigned long *acknowledge
bbd0abda 1502 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
bbd0abda 1503 unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
9b6b563c
PM
1504
1505 prom_debug("prom_hold_cpus: start...\n");
1506 prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
1507 prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
1508 prom_debug(" 1) acknowledge = 0x%x\n",
1509 (unsigned long)acknowledge);
1510 prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
1511 prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
1512
1513 /* Set the common spinloop variable, so all of the secondary cpus
1514 * will block when they are awakened from their OF spinloop.
1515 * This must occur for both SMP and non SMP kernels, since OF will
1516 * be trashed when we move the kernel.
1517 */
1518 *spinloop = 0;
1519
9b6b563c
PM
1520 /* look for cpus */
1521 for (node = 0; prom_next_node(&node); ) {
1522 type[0] = 0;
1523 prom_getprop(node, "device_type", type, sizeof(type));
1524 if (strcmp(type, RELOC("cpu")) != 0)
1525 continue;
1526
1527 /* Skip non-configured cpus. */
1528 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1529 if (strcmp(type, RELOC("okay")) != 0)
1530 continue;
1531
1532 reg = -1;
1533 prom_getprop(node, "reg", &reg, sizeof(reg));
1534
2c48a7d6 1535 prom_debug("cpu hw idx = %lu\n", reg);
9b6b563c
PM
1536
1537 /* Init the acknowledge var which will be reset by
1538 * the secondary cpu when it awakens from its OF
1539 * spinloop.
1540 */
1541 *acknowledge = (unsigned long)-1;
1542
7d2f6075 1543 if (reg != _prom->cpu) {
9b6b563c 1544 /* Primary Thread of non-boot cpu */
2c48a7d6 1545 prom_printf("starting cpu hw idx %lu... ", reg);
9b6b563c
PM
1546 call_prom("start-cpu", 3, 0, node,
1547 secondary_hold, reg);
1548
bbd0abda
PM
1549 for (i = 0; (i < 100000000) &&
1550 (*acknowledge == ((unsigned long)-1)); i++ )
9b6b563c
PM
1551 mb();
1552
bbd0abda 1553 if (*acknowledge == reg)
9b6b563c 1554 prom_printf("done\n");
bbd0abda 1555 else
9b6b563c 1556 prom_printf("failed: %x\n", *acknowledge);
9b6b563c
PM
1557 }
1558#ifdef CONFIG_SMP
1559 else
2c48a7d6 1560 prom_printf("boot cpu hw idx %lu\n", reg);
9b6b563c 1561#endif /* CONFIG_SMP */
9b6b563c 1562 }
9b6b563c 1563
9b6b563c 1564 prom_debug("prom_hold_cpus: end...\n");
9b6b563c
PM
1565}
1566
1567
1568static void __init prom_init_client_services(unsigned long pp)
1569{
1570 struct prom_t *_prom = &RELOC(prom);
1571
1572 /* Get a handle to the prom entry point before anything else */
1573 RELOC(prom_entry) = pp;
1574
1575 /* get a handle for the stdout device */
1576 _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1577 if (!PHANDLE_VALID(_prom->chosen))
1578 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1579
1580 /* get device tree root */
1581 _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1582 if (!PHANDLE_VALID(_prom->root))
1583 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
a575b807
PM
1584
1585 _prom->mmumap = 0;
1586}
1587
1588#ifdef CONFIG_PPC32
1589/*
1590 * For really old powermacs, we need to map things we claim.
1591 * For that, we need the ihandle of the mmu.
a23414be 1592 * Also, on the longtrail, we need to work around other bugs.
a575b807
PM
1593 */
1594static void __init prom_find_mmu(void)
1595{
1596 struct prom_t *_prom = &RELOC(prom);
1597 phandle oprom;
1598 char version[64];
1599
1600 oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1601 if (!PHANDLE_VALID(oprom))
1602 return;
1603 if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1604 return;
1605 version[sizeof(version) - 1] = 0;
a575b807 1606 /* XXX might need to add other versions here */
a23414be
PM
1607 if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1608 of_workarounds = OF_WA_CLAIM;
1609 else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1610 of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1611 call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1612 } else
a575b807 1613 return;
a23414be 1614 _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
a575b807
PM
1615 prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1616 sizeof(_prom->mmumap));
a23414be
PM
1617 if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1618 of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
9b6b563c 1619}
a575b807
PM
1620#else
1621#define prom_find_mmu()
1622#endif
9b6b563c
PM
1623
1624static void __init prom_init_stdout(void)
1625{
1626 struct prom_t *_prom = &RELOC(prom);
1627 char *path = RELOC(of_stdout_device);
1628 char type[16];
1629 u32 val;
1630
1631 if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1632 prom_panic("cannot find stdout");
1633
1634 _prom->stdout = val;
1635
1636 /* Get the full OF pathname of the stdout device */
1637 memset(path, 0, 256);
1638 call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1639 val = call_prom("instance-to-package", 1, 1, _prom->stdout);
a23414be
PM
1640 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1641 &val, sizeof(val));
9b6b563c 1642 prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
a23414be
PM
1643 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1644 path, strlen(path) + 1);
9b6b563c
PM
1645
1646 /* If it's a display, note it */
1647 memset(type, 0, sizeof(type));
1648 prom_getprop(val, "device_type", type, sizeof(type));
1649 if (strcmp(type, RELOC("display")) == 0)
a23414be 1650 prom_setprop(val, path, "linux,boot-display", NULL, 0);
9b6b563c
PM
1651}
1652
1653static void __init prom_close_stdin(void)
1654{
1655 struct prom_t *_prom = &RELOC(prom);
1656 ihandle val;
1657
1658 if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1659 call_prom("close", 1, 0, val);
1660}
1661
1662static int __init prom_find_machine_type(void)
1663{
1664 struct prom_t *_prom = &RELOC(prom);
1665 char compat[256];
1666 int len, i = 0;
21fe3301 1667#ifdef CONFIG_PPC64
9b6b563c 1668 phandle rtas;
e8222502 1669 int x;
21fe3301 1670#endif
e8222502
BH
1671
1672 /* Look for a PowerMac */
9b6b563c
PM
1673 len = prom_getprop(_prom->root, "compatible",
1674 compat, sizeof(compat)-1);
1675 if (len > 0) {
1676 compat[len] = 0;
1677 while (i < len) {
1678 char *p = &compat[i];
1679 int sl = strlen(p);
1680 if (sl == 0)
1681 break;
1682 if (strstr(p, RELOC("Power Macintosh")) ||
a575b807 1683 strstr(p, RELOC("MacRISC")))
9b6b563c 1684 return PLATFORM_POWERMAC;
133dda1e
AB
1685#ifdef CONFIG_PPC64
1686 /* We must make sure we don't detect the IBM Cell
1687 * blades as pSeries due to some firmware issues,
1688 * so we do it here.
1689 */
1690 if (strstr(p, RELOC("IBM,CBEA")) ||
1691 strstr(p, RELOC("IBM,CPBW-1.0")))
1692 return PLATFORM_GENERIC;
1693#endif /* CONFIG_PPC64 */
9b6b563c
PM
1694 i += sl + 1;
1695 }
1696 }
1697#ifdef CONFIG_PPC64
e8222502
BH
1698 /* If not a mac, try to figure out if it's an IBM pSeries or any other
1699 * PAPR compliant platform. We assume it is if :
1700 * - /device_type is "chrp" (please, do NOT use that for future
1701 * non-IBM designs !
1702 * - it has /rtas
1703 */
6f806cee 1704 len = prom_getprop(_prom->root, "device_type",
e8222502
BH
1705 compat, sizeof(compat)-1);
1706 if (len <= 0)
1707 return PLATFORM_GENERIC;
cb6b2eb9 1708 if (strcmp(compat, RELOC("chrp")))
e8222502
BH
1709 return PLATFORM_GENERIC;
1710
9b6b563c
PM
1711 /* Default to pSeries. We need to know if we are running LPAR */
1712 rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
e8222502
BH
1713 if (!PHANDLE_VALID(rtas))
1714 return PLATFORM_GENERIC;
1715 x = prom_getproplen(rtas, "ibm,hypertas-functions");
1716 if (x != PROM_ERROR) {
4da727ae 1717 prom_debug("Hypertas detected, assuming LPAR !\n");
e8222502 1718 return PLATFORM_PSERIES_LPAR;
9b6b563c
PM
1719 }
1720 return PLATFORM_PSERIES;
1721#else
e8222502 1722 return PLATFORM_GENERIC;
9b6b563c
PM
1723#endif
1724}
1725
9b6b563c
PM
1726static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1727{
1728 return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1729}
1730
1731/*
1732 * If we have a display that we don't know how to drive,
1733 * we will want to try to execute OF's open method for it
1734 * later. However, OF will probably fall over if we do that
1735 * we've taken over the MMU.
1736 * So we check whether we will need to open the display,
1737 * and if so, open it now.
1738 */
1739static void __init prom_check_displays(void)
1740{
1741 char type[16], *path;
1742 phandle node;
1743 ihandle ih;
1744 int i;
9b6b563c
PM
1745
1746 static unsigned char default_colors[] = {
1747 0x00, 0x00, 0x00,
1748 0x00, 0x00, 0xaa,
1749 0x00, 0xaa, 0x00,
1750 0x00, 0xaa, 0xaa,
1751 0xaa, 0x00, 0x00,
1752 0xaa, 0x00, 0xaa,
1753 0xaa, 0xaa, 0x00,
1754 0xaa, 0xaa, 0xaa,
1755 0x55, 0x55, 0x55,
1756 0x55, 0x55, 0xff,
1757 0x55, 0xff, 0x55,
1758 0x55, 0xff, 0xff,
1759 0xff, 0x55, 0x55,
1760 0xff, 0x55, 0xff,
1761 0xff, 0xff, 0x55,
1762 0xff, 0xff, 0xff
1763 };
1764 const unsigned char *clut;
1765
4da727ae 1766 prom_debug("Looking for displays\n");
9b6b563c
PM
1767 for (node = 0; prom_next_node(&node); ) {
1768 memset(type, 0, sizeof(type));
1769 prom_getprop(node, "device_type", type, sizeof(type));
1770 if (strcmp(type, RELOC("display")) != 0)
1771 continue;
1772
1773 /* It seems OF doesn't null-terminate the path :-( */
1774 path = RELOC(prom_scratch);
1775 memset(path, 0, PROM_SCRATCH_SIZE);
1776
1777 /*
1778 * leave some room at the end of the path for appending extra
1779 * arguments
1780 */
1781 if (call_prom("package-to-path", 3, 1, node, path,
1782 PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1783 continue;
1f8737aa 1784 prom_printf("found display : %s, opening... ", path);
9b6b563c
PM
1785
1786 ih = call_prom("open", 1, 1, path);
1787 if (ih == 0) {
1788 prom_printf("failed\n");
1789 continue;
1790 }
1791
1792 /* Success */
1793 prom_printf("done\n");
a23414be 1794 prom_setprop(node, path, "linux,opened", NULL, 0);
9b6b563c
PM
1795
1796 /* Setup a usable color table when the appropriate
1797 * method is available. Should update this to set-colors */
1798 clut = RELOC(default_colors);
1799 for (i = 0; i < 32; i++, clut += 3)
1800 if (prom_set_color(ih, i, clut[0], clut[1],
1801 clut[2]) != 0)
1802 break;
1803
1804#ifdef CONFIG_LOGO_LINUX_CLUT224
1805 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1806 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1807 if (prom_set_color(ih, i + 32, clut[0], clut[1],
1808 clut[2]) != 0)
1809 break;
1810#endif /* CONFIG_LOGO_LINUX_CLUT224 */
9b6b563c
PM
1811 }
1812}
1813
1814
1815/* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1816static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
1817 unsigned long needed, unsigned long align)
1818{
1819 void *ret;
1820
1821 *mem_start = _ALIGN(*mem_start, align);
1822 while ((*mem_start + needed) > *mem_end) {
1823 unsigned long room, chunk;
1824
1825 prom_debug("Chunk exhausted, claiming more at %x...\n",
1826 RELOC(alloc_bottom));
1827 room = RELOC(alloc_top) - RELOC(alloc_bottom);
1828 if (room > DEVTREE_CHUNK_SIZE)
1829 room = DEVTREE_CHUNK_SIZE;
1830 if (room < PAGE_SIZE)
1831 prom_panic("No memory for flatten_device_tree (no room)");
1832 chunk = alloc_up(room, 0);
1833 if (chunk == 0)
1834 prom_panic("No memory for flatten_device_tree (claim failed)");
1835 *mem_end = RELOC(alloc_top);
1836 }
1837
1838 ret = (void *)*mem_start;
1839 *mem_start += needed;
1840
1841 return ret;
1842}
1843
1844#define dt_push_token(token, mem_start, mem_end) \
1845 do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
1846
1847static unsigned long __init dt_find_string(char *str)
1848{
1849 char *s, *os;
1850
1851 s = os = (char *)RELOC(dt_string_start);
1852 s += 4;
1853 while (s < (char *)RELOC(dt_string_end)) {
1854 if (strcmp(s, str) == 0)
1855 return s - os;
1856 s += strlen(s) + 1;
1857 }
1858 return 0;
1859}
1860
1861/*
1862 * The Open Firmware 1275 specification states properties must be 31 bytes or
1863 * less, however not all firmwares obey this. Make it 64 bytes to be safe.
1864 */
1865#define MAX_PROPERTY_NAME 64
1866
1867static void __init scan_dt_build_strings(phandle node,
1868 unsigned long *mem_start,
1869 unsigned long *mem_end)
1870{
1871 char *prev_name, *namep, *sstart;
1872 unsigned long soff;
1873 phandle child;
1874
1875 sstart = (char *)RELOC(dt_string_start);
1876
1877 /* get and store all property names */
1878 prev_name = RELOC("");
1879 for (;;) {
1880 /* 64 is max len of name including nul. */
1881 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
1882 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
1883 /* No more nodes: unwind alloc */
1884 *mem_start = (unsigned long)namep;
1885 break;
1886 }
1887
1888 /* skip "name" */
1889 if (strcmp(namep, RELOC("name")) == 0) {
1890 *mem_start = (unsigned long)namep;
1891 prev_name = RELOC("name");
1892 continue;
1893 }
1894 /* get/create string entry */
1895 soff = dt_find_string(namep);
1896 if (soff != 0) {
1897 *mem_start = (unsigned long)namep;
1898 namep = sstart + soff;
1899 } else {
1900 /* Trim off some if we can */
1901 *mem_start = (unsigned long)namep + strlen(namep) + 1;
1902 RELOC(dt_string_end) = *mem_start;
1903 }
1904 prev_name = namep;
1905 }
1906
1907 /* do all our children */
1908 child = call_prom("child", 1, 1, node);
1909 while (child != 0) {
1910 scan_dt_build_strings(child, mem_start, mem_end);
1911 child = call_prom("peer", 1, 1, child);
1912 }
1913}
1914
1915static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
1916 unsigned long *mem_end)
1917{
1918 phandle child;
1919 char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
1920 unsigned long soff;
1921 unsigned char *valp;
1922 static char pname[MAX_PROPERTY_NAME];
c4988820 1923 int l, room;
9b6b563c
PM
1924
1925 dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
1926
1927 /* get the node's full name */
1928 namep = (char *)*mem_start;
c4988820
PM
1929 room = *mem_end - *mem_start;
1930 if (room > 255)
1931 room = 255;
1932 l = call_prom("package-to-path", 3, 1, node, namep, room);
9b6b563c
PM
1933 if (l >= 0) {
1934 /* Didn't fit? Get more room. */
c4988820
PM
1935 if (l >= room) {
1936 if (l >= *mem_end - *mem_start)
1937 namep = make_room(mem_start, mem_end, l+1, 1);
9b6b563c
PM
1938 call_prom("package-to-path", 3, 1, node, namep, l);
1939 }
1940 namep[l] = '\0';
1941
1942 /* Fixup an Apple bug where they have bogus \0 chars in the
a575b807
PM
1943 * middle of the path in some properties, and extract
1944 * the unit name (everything after the last '/').
9b6b563c 1945 */
a575b807 1946 for (lp = p = namep, ep = namep + l; p < ep; p++) {
9b6b563c 1947 if (*p == '/')
a575b807
PM
1948 lp = namep;
1949 else if (*p != 0)
1950 *lp++ = *p;
1951 }
1952 *lp = 0;
1953 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
9b6b563c
PM
1954 }
1955
1956 /* get it again for debugging */
1957 path = RELOC(prom_scratch);
1958 memset(path, 0, PROM_SCRATCH_SIZE);
1959 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1960
1961 /* get and store all properties */
1962 prev_name = RELOC("");
1963 sstart = (char *)RELOC(dt_string_start);
1964 for (;;) {
1965 if (call_prom("nextprop", 3, 1, node, prev_name,
1966 RELOC(pname)) != 1)
1967 break;
1968
1969 /* skip "name" */
1970 if (strcmp(RELOC(pname), RELOC("name")) == 0) {
1971 prev_name = RELOC("name");
1972 continue;
1973 }
1974
1975 /* find string offset */
1976 soff = dt_find_string(RELOC(pname));
1977 if (soff == 0) {
1978 prom_printf("WARNING: Can't find string index for"
1979 " <%s>, node %s\n", RELOC(pname), path);
1980 break;
1981 }
1982 prev_name = sstart + soff;
1983
1984 /* get length */
1985 l = call_prom("getproplen", 2, 1, node, RELOC(pname));
1986
1987 /* sanity checks */
1988 if (l == PROM_ERROR)
1989 continue;
1990 if (l > MAX_PROPERTY_LENGTH) {
1991 prom_printf("WARNING: ignoring large property ");
1992 /* It seems OF doesn't null-terminate the path :-( */
1993 prom_printf("[%s] ", path);
1994 prom_printf("%s length 0x%x\n", RELOC(pname), l);
1995 continue;
1996 }
1997
1998 /* push property head */
1999 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2000 dt_push_token(l, mem_start, mem_end);
2001 dt_push_token(soff, mem_start, mem_end);
2002
2003 /* push property content */
2004 valp = make_room(mem_start, mem_end, l, 4);
2005 call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
2006 *mem_start = _ALIGN(*mem_start, 4);
2007 }
2008
2009 /* Add a "linux,phandle" property. */
2010 soff = dt_find_string(RELOC("linux,phandle"));
2011 if (soff == 0)
2012 prom_printf("WARNING: Can't find string index for"
2013 " <linux-phandle> node %s\n", path);
2014 else {
2015 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2016 dt_push_token(4, mem_start, mem_end);
2017 dt_push_token(soff, mem_start, mem_end);
2018 valp = make_room(mem_start, mem_end, 4, 4);
2019 *(u32 *)valp = node;
2020 }
2021
2022 /* do all our children */
2023 child = call_prom("child", 1, 1, node);
2024 while (child != 0) {
2025 scan_dt_build_struct(child, mem_start, mem_end);
2026 child = call_prom("peer", 1, 1, child);
2027 }
2028
2029 dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
2030}
2031
2032static void __init flatten_device_tree(void)
2033{
2034 phandle root;
2035 unsigned long mem_start, mem_end, room;
2036 struct boot_param_header *hdr;
2037 struct prom_t *_prom = &RELOC(prom);
2038 char *namep;
2039 u64 *rsvmap;
2040
2041 /*
2042 * Check how much room we have between alloc top & bottom (+/- a
2043 * few pages), crop to 4Mb, as this is our "chuck" size
2044 */
2045 room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
2046 if (room > DEVTREE_CHUNK_SIZE)
2047 room = DEVTREE_CHUNK_SIZE;
2048 prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
2049
2050 /* Now try to claim that */
2051 mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
2052 if (mem_start == 0)
2053 prom_panic("Can't allocate initial device-tree chunk\n");
2054 mem_end = RELOC(alloc_top);
2055
2056 /* Get root of tree */
2057 root = call_prom("peer", 1, 1, (phandle)0);
2058 if (root == (phandle)0)
2059 prom_panic ("couldn't get device tree root\n");
2060
2061 /* Build header and make room for mem rsv map */
2062 mem_start = _ALIGN(mem_start, 4);
2063 hdr = make_room(&mem_start, &mem_end,
2064 sizeof(struct boot_param_header), 4);
2065 RELOC(dt_header_start) = (unsigned long)hdr;
2066 rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
2067
2068 /* Start of strings */
2069 mem_start = PAGE_ALIGN(mem_start);
2070 RELOC(dt_string_start) = mem_start;
2071 mem_start += 4; /* hole */
2072
2073 /* Add "linux,phandle" in there, we'll need it */
2074 namep = make_room(&mem_start, &mem_end, 16, 1);
2075 strcpy(namep, RELOC("linux,phandle"));
2076 mem_start = (unsigned long)namep + strlen(namep) + 1;
2077
2078 /* Build string array */
2079 prom_printf("Building dt strings...\n");
2080 scan_dt_build_strings(root, &mem_start, &mem_end);
2081 RELOC(dt_string_end) = mem_start;
2082
2083 /* Build structure */
2084 mem_start = PAGE_ALIGN(mem_start);
2085 RELOC(dt_struct_start) = mem_start;
2086 prom_printf("Building dt structure...\n");
2087 scan_dt_build_struct(root, &mem_start, &mem_end);
2088 dt_push_token(OF_DT_END, &mem_start, &mem_end);
2089 RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
2090
2091 /* Finish header */
2092 hdr->boot_cpuid_phys = _prom->cpu;
2093 hdr->magic = OF_DT_HEADER;
2094 hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
2095 hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
2096 hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
2097 hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
2098 hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
2099 hdr->version = OF_DT_VERSION;
2100 /* Version 16 is not backward compatible */
2101 hdr->last_comp_version = 0x10;
2102
4d1f3f25 2103 /* Copy the reserve map in */
9b6b563c
PM
2104 memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
2105
2106#ifdef DEBUG_PROM
2107 {
2108 int i;
2109 prom_printf("reserved memory map:\n");
2110 for (i = 0; i < RELOC(mem_reserve_cnt); i++)
2111 prom_printf(" %x - %x\n",
2112 RELOC(mem_reserve_map)[i].base,
2113 RELOC(mem_reserve_map)[i].size);
2114 }
2115#endif
4d1f3f25
JX
2116 /* Bump mem_reserve_cnt to cause further reservations to fail
2117 * since it's too late.
2118 */
9b6b563c
PM
2119 RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
2120
2121 prom_printf("Device tree strings 0x%x -> 0x%x\n",
2122 RELOC(dt_string_start), RELOC(dt_string_end));
2123 prom_printf("Device tree struct 0x%x -> 0x%x\n",
2124 RELOC(dt_struct_start), RELOC(dt_struct_end));
2125
2126}
2127
54f4ee18
HB
2128#ifdef CONFIG_PPC_MAPLE
2129/* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2130 * The values are bad, and it doesn't even have the right number of cells. */
2131static void __init fixup_device_tree_maple(void)
9b6b563c 2132{
54f4ee18 2133 phandle isa;
980a6513 2134 u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
54f4ee18 2135 u32 isa_ranges[6];
980a6513
BH
2136 char *name;
2137
2138 name = "/ht@0/isa@4";
2139 isa = call_prom("finddevice", 1, 1, ADDR(name));
2140 if (!PHANDLE_VALID(isa)) {
2141 name = "/ht@0/isa@6";
2142 isa = call_prom("finddevice", 1, 1, ADDR(name));
2143 rloc = 0x01003000; /* IO space; PCI device = 6 */
2144 }
54f4ee18
HB
2145 if (!PHANDLE_VALID(isa))
2146 return;
2147
980a6513
BH
2148 if (prom_getproplen(isa, "ranges") != 12)
2149 return;
54f4ee18
HB
2150 if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2151 == PROM_ERROR)
2152 return;
2153
2154 if (isa_ranges[0] != 0x1 ||
2155 isa_ranges[1] != 0xf4000000 ||
2156 isa_ranges[2] != 0x00010000)
2157 return;
2158
980a6513 2159 prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
54f4ee18
HB
2160
2161 isa_ranges[0] = 0x1;
2162 isa_ranges[1] = 0x0;
980a6513 2163 isa_ranges[2] = rloc;
54f4ee18
HB
2164 isa_ranges[3] = 0x0;
2165 isa_ranges[4] = 0x0;
2166 isa_ranges[5] = 0x00010000;
980a6513 2167 prom_setprop(isa, name, "ranges",
54f4ee18
HB
2168 isa_ranges, sizeof(isa_ranges));
2169}
8f101a05
HC
2170
2171#define CPC925_MC_START 0xf8000000
2172#define CPC925_MC_LENGTH 0x1000000
2173/* The values for memory-controller don't have right number of cells */
2174static void __init fixup_device_tree_maple_memory_controller(void)
2175{
2176 phandle mc;
2177 u32 mc_reg[4];
2178 char *name = "/hostbridge@f8000000";
2179 struct prom_t *_prom = &RELOC(prom);
2180 u32 ac, sc;
2181
2182 mc = call_prom("finddevice", 1, 1, ADDR(name));
2183 if (!PHANDLE_VALID(mc))
2184 return;
2185
2186 if (prom_getproplen(mc, "reg") != 8)
2187 return;
2188
2189 prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
2190 prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
2191 if ((ac != 2) || (sc != 2))
2192 return;
2193
2194 if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
2195 return;
2196
2197 if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
2198 return;
2199
2200 prom_printf("Fixing up bogus hostbridge on Maple...\n");
2201
2202 mc_reg[0] = 0x0;
2203 mc_reg[1] = CPC925_MC_START;
2204 mc_reg[2] = 0x0;
2205 mc_reg[3] = CPC925_MC_LENGTH;
2206 prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
2207}
54f4ee18
HB
2208#else
2209#define fixup_device_tree_maple()
8f101a05 2210#define fixup_device_tree_maple_memory_controller()
54f4ee18
HB
2211#endif
2212
e8c0acf9 2213#ifdef CONFIG_PPC_CHRP
e4805922
OH
2214/*
2215 * Pegasos and BriQ lacks the "ranges" property in the isa node
2216 * Pegasos needs decimal IRQ 14/15, not hexadecimal
556ecf9b 2217 * Pegasos has the IDE configured in legacy mode, but advertised as native
e4805922 2218 */
e8c0acf9
BH
2219static void __init fixup_device_tree_chrp(void)
2220{
e4805922
OH
2221 phandle ph;
2222 u32 prop[6];
26c5032e 2223 u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
e8c0acf9
BH
2224 char *name;
2225 int rc;
2226
2227 name = "/pci@80000000/isa@c";
e4805922
OH
2228 ph = call_prom("finddevice", 1, 1, ADDR(name));
2229 if (!PHANDLE_VALID(ph)) {
26c5032e 2230 name = "/pci@ff500000/isa@6";
e4805922 2231 ph = call_prom("finddevice", 1, 1, ADDR(name));
26c5032e
BH
2232 rloc = 0x01003000; /* IO space; PCI device = 6 */
2233 }
e4805922
OH
2234 if (PHANDLE_VALID(ph)) {
2235 rc = prom_getproplen(ph, "ranges");
2236 if (rc == 0 || rc == PROM_ERROR) {
2237 prom_printf("Fixing up missing ISA range on Pegasos...\n");
2238
2239 prop[0] = 0x1;
2240 prop[1] = 0x0;
2241 prop[2] = rloc;
2242 prop[3] = 0x0;
2243 prop[4] = 0x0;
2244 prop[5] = 0x00010000;
2245 prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2246 }
2247 }
e8c0acf9 2248
e4805922
OH
2249 name = "/pci@80000000/ide@C,1";
2250 ph = call_prom("finddevice", 1, 1, ADDR(name));
2251 if (PHANDLE_VALID(ph)) {
2252 prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2253 prop[0] = 14;
2254 prop[1] = 0x0;
556ecf9b
OH
2255 prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2256 prom_printf("Fixing up IDE class-code on Pegasos...\n");
2257 rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2258 if (rc == sizeof(u32)) {
2259 prop[0] &= ~0x5;
2260 prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2261 }
e4805922 2262 }
e8c0acf9
BH
2263}
2264#else
2265#define fixup_device_tree_chrp()
2266#endif
2267
9b6b563c 2268#if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
54f4ee18
HB
2269static void __init fixup_device_tree_pmac(void)
2270{
9b6b563c
PM
2271 phandle u3, i2c, mpic;
2272 u32 u3_rev;
2273 u32 interrupts[2];
2274 u32 parent;
2275
2276 /* Some G5s have a missing interrupt definition, fix it up here */
2277 u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2278 if (!PHANDLE_VALID(u3))
2279 return;
2280 i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2281 if (!PHANDLE_VALID(i2c))
2282 return;
2283 mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2284 if (!PHANDLE_VALID(mpic))
2285 return;
2286
2287 /* check if proper rev of u3 */
2288 if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2289 == PROM_ERROR)
2290 return;
7d49697e 2291 if (u3_rev < 0x35 || u3_rev > 0x39)
9b6b563c
PM
2292 return;
2293 /* does it need fixup ? */
2294 if (prom_getproplen(i2c, "interrupts") > 0)
2295 return;
2296
2297 prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2298
2299 /* interrupt on this revision of u3 is number 0 and level */
2300 interrupts[0] = 0;
2301 interrupts[1] = 1;
a23414be
PM
2302 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2303 &interrupts, sizeof(interrupts));
9b6b563c 2304 parent = (u32)mpic;
a23414be
PM
2305 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2306 &parent, sizeof(parent));
9b6b563c 2307}
54f4ee18
HB
2308#else
2309#define fixup_device_tree_pmac()
2310#endif
9b6b563c 2311
88fd2a9d 2312#ifdef CONFIG_PPC_EFIKA
94d2dde7
GL
2313/*
2314 * The MPC5200 FEC driver requires an phy-handle property to tell it how
2315 * to talk to the phy. If the phy-handle property is missing, then this
2316 * function is called to add the appropriate nodes and link it to the
2317 * ethernet node.
2318 */
2319static void __init fixup_device_tree_efika_add_phy(void)
88fd2a9d 2320{
88fd2a9d
SM
2321 u32 node;
2322 char prop[64];
94d2dde7 2323 int rv;
88fd2a9d 2324
94d2dde7
GL
2325 /* Check if /builtin/ethernet exists - bail if it doesn't */
2326 node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
88fd2a9d
SM
2327 if (!PHANDLE_VALID(node))
2328 return;
2329
94d2dde7
GL
2330 /* Check if the phy-handle property exists - bail if it does */
2331 rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2332 if (!rv)
88fd2a9d
SM
2333 return;
2334
94d2dde7
GL
2335 /*
2336 * At this point the ethernet device doesn't have a phy described.
2337 * Now we need to add the missing phy node and linkage
2338 */
6f4347c9 2339
94d2dde7 2340 /* Check for an MDIO bus node - if missing then create one */
6f4347c9
OH
2341 node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2342 if (!PHANDLE_VALID(node)) {
2343 prom_printf("Adding Ethernet MDIO node\n");
2344 call_prom("interpret", 1, 1,
2345 " s\" /builtin\" find-device"
2346 " new-device"
2347 " 1 encode-int s\" #address-cells\" property"
2348 " 0 encode-int s\" #size-cells\" property"
94d2dde7
GL
2349 " s\" mdio\" device-name"
2350 " s\" fsl,mpc5200b-mdio\" encode-string"
6f4347c9
OH
2351 " s\" compatible\" property"
2352 " 0xf0003000 0x400 reg"
2353 " 0x2 encode-int"
2354 " 0x5 encode-int encode+"
2355 " 0x3 encode-int encode+"
2356 " s\" interrupts\" property"
2357 " finish-device");
2358 };
2359
94d2dde7
GL
2360 /* Check for a PHY device node - if missing then create one and
2361 * give it's phandle to the ethernet node */
2362 node = call_prom("finddevice", 1, 1,
2363 ADDR("/builtin/mdio/ethernet-phy"));
6f4347c9
OH
2364 if (!PHANDLE_VALID(node)) {
2365 prom_printf("Adding Ethernet PHY node\n");
2366 call_prom("interpret", 1, 1,
2367 " s\" /builtin/mdio\" find-device"
2368 " new-device"
2369 " s\" ethernet-phy\" device-name"
2370 " 0x10 encode-int s\" reg\" property"
2371 " my-self"
2372 " ihandle>phandle"
2373 " finish-device"
2374 " s\" /builtin/ethernet\" find-device"
2375 " encode-int"
2376 " s\" phy-handle\" property"
2377 " device-end");
2378 }
94d2dde7
GL
2379}
2380
2381static void __init fixup_device_tree_efika(void)
2382{
2383 int sound_irq[3] = { 2, 2, 0 };
2384 int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2385 3,4,0, 3,5,0, 3,6,0, 3,7,0,
2386 3,8,0, 3,9,0, 3,10,0, 3,11,0,
2387 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2388 u32 node;
2389 char prop[64];
2390 int rv, len;
2391
2392 /* Check if we're really running on a EFIKA */
2393 node = call_prom("finddevice", 1, 1, ADDR("/"));
2394 if (!PHANDLE_VALID(node))
2395 return;
2396
2397 rv = prom_getprop(node, "model", prop, sizeof(prop));
2398 if (rv == PROM_ERROR)
2399 return;
2400 if (strcmp(prop, "EFIKA5K2"))
2401 return;
2402
2403 prom_printf("Applying EFIKA device tree fixups\n");
2404
2405 /* Claiming to be 'chrp' is death */
2406 node = call_prom("finddevice", 1, 1, ADDR("/"));
2407 rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2408 if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2409 prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2410
7f4392cd
DW
2411 /* CODEGEN,description is exposed in /proc/cpuinfo so
2412 fix that too */
2413 rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2414 if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2415 prom_setprop(node, "/", "CODEGEN,description",
2416 "Efika 5200B PowerPC System",
2417 sizeof("Efika 5200B PowerPC System"));
2418
94d2dde7
GL
2419 /* Fixup bestcomm interrupts property */
2420 node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2421 if (PHANDLE_VALID(node)) {
2422 len = prom_getproplen(node, "interrupts");
2423 if (len == 12) {
2424 prom_printf("Fixing bestcomm interrupts property\n");
2425 prom_setprop(node, "/builtin/bestcom", "interrupts",
2426 bcomm_irq, sizeof(bcomm_irq));
2427 }
2428 }
2429
2430 /* Fixup sound interrupts property */
2431 node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2432 if (PHANDLE_VALID(node)) {
2433 rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2434 if (rv == PROM_ERROR) {
2435 prom_printf("Adding sound interrupts property\n");
2436 prom_setprop(node, "/builtin/sound", "interrupts",
2437 sound_irq, sizeof(sound_irq));
2438 }
2439 }
6f4347c9 2440
94d2dde7
GL
2441 /* Make sure ethernet phy-handle property exists */
2442 fixup_device_tree_efika_add_phy();
88fd2a9d
SM
2443}
2444#else
2445#define fixup_device_tree_efika()
2446#endif
2447
54f4ee18
HB
2448static void __init fixup_device_tree(void)
2449{
2450 fixup_device_tree_maple();
8f101a05 2451 fixup_device_tree_maple_memory_controller();
e8c0acf9 2452 fixup_device_tree_chrp();
54f4ee18 2453 fixup_device_tree_pmac();
88fd2a9d 2454 fixup_device_tree_efika();
54f4ee18 2455}
9b6b563c
PM
2456
2457static void __init prom_find_boot_cpu(void)
2458{
e788ff13 2459 struct prom_t *_prom = &RELOC(prom);
9b6b563c
PM
2460 u32 getprop_rval;
2461 ihandle prom_cpu;
2462 phandle cpu_pkg;
2463
a575b807 2464 _prom->cpu = 0;
9b6b563c 2465 if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
a575b807 2466 return;
9b6b563c
PM
2467
2468 cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2469
2470 prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
2471 _prom->cpu = getprop_rval;
2472
2c48a7d6 2473 prom_debug("Booting CPU hw index = %lu\n", _prom->cpu);
9b6b563c
PM
2474}
2475
2476static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2477{
2478#ifdef CONFIG_BLK_DEV_INITRD
e788ff13 2479 struct prom_t *_prom = &RELOC(prom);
9b6b563c
PM
2480
2481 if (r3 && r4 && r4 != 0xdeadbeef) {
2482 unsigned long val;
2483
51fae6de 2484 RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
9b6b563c
PM
2485 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2486
2487 val = RELOC(prom_initrd_start);
a23414be
PM
2488 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2489 &val, sizeof(val));
9b6b563c 2490 val = RELOC(prom_initrd_end);
a23414be
PM
2491 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2492 &val, sizeof(val));
9b6b563c
PM
2493
2494 reserve_mem(RELOC(prom_initrd_start),
2495 RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
2496
2497 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
2498 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
2499 }
2500#endif /* CONFIG_BLK_DEV_INITRD */
2501}
2502
2503/*
2504 * We enter here early on, when the Open Firmware prom is still
2505 * handling exceptions and the MMU hash table for us.
2506 */
2507
2508unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2509 unsigned long pp,
549e8152
PM
2510 unsigned long r6, unsigned long r7,
2511 unsigned long kbase)
9b6b563c 2512{
e788ff13 2513 struct prom_t *_prom;
9b6b563c 2514 unsigned long hdr;
9b6b563c
PM
2515
2516#ifdef CONFIG_PPC32
549e8152 2517 unsigned long offset = reloc_offset();
9b6b563c
PM
2518 reloc_got2(offset);
2519#endif
2520
2521 _prom = &RELOC(prom);
2522
2523 /*
2524 * First zero the BSS
2525 */
2526 memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
2527
2528 /*
2529 * Init interface to Open Firmware, get some node references,
2530 * like /chosen
2531 */
2532 prom_init_client_services(pp);
2533
2534 /*
a23414be
PM
2535 * See if this OF is old enough that we need to do explicit maps
2536 * and other workarounds
9b6b563c 2537 */
a23414be 2538 prom_find_mmu();
9b6b563c 2539
a575b807 2540 /*
a23414be 2541 * Init prom stdout device
a575b807 2542 */
a23414be 2543 prom_init_stdout();
a575b807 2544
151a9f4a 2545 prom_printf("Preparing to boot %s", RELOC(linux_banner));
e7943fbb 2546
9b6b563c
PM
2547 /*
2548 * Get default machine type. At this point, we do not differentiate
2549 * between pSeries SMP and pSeries LPAR
2550 */
2551 RELOC(of_platform) = prom_find_machine_type();
9b6b563c 2552
549e8152 2553#ifndef CONFIG_RELOCATABLE
add60ef3
OH
2554 /* Bail if this is a kdump kernel. */
2555 if (PHYSICAL_START > 0)
2556 prom_panic("Error: You can't boot a kdump kernel from OF!\n");
549e8152 2557#endif
add60ef3
OH
2558
2559 /*
2560 * Check for an initrd
2561 */
2562 prom_check_initrd(r3, r4);
2563
9b6b563c
PM
2564#ifdef CONFIG_PPC_PSERIES
2565 /*
2566 * On pSeries, inform the firmware about our capabilities
2567 */
799d6046
PM
2568 if (RELOC(of_platform) == PLATFORM_PSERIES ||
2569 RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
9b6b563c
PM
2570 prom_send_capabilities();
2571#endif
2572
9b6b563c 2573 /*
f3f66f59 2574 * Copy the CPU hold code
9b6b563c 2575 */
e788ff13 2576 if (RELOC(of_platform) != PLATFORM_POWERMAC)
549e8152 2577 copy_and_flush(0, kbase, 0x100, 0);
9b6b563c
PM
2578
2579 /*
2580 * Do early parsing of command line
2581 */
2582 early_cmdline_parse();
2583
2584 /*
2585 * Initialize memory management within prom_init
2586 */
2587 prom_init_mem();
2588
2589 /*
2590 * Determine which cpu is actually running right _now_
2591 */
2592 prom_find_boot_cpu();
2593
2594 /*
2595 * Initialize display devices
2596 */
2597 prom_check_displays();
2598
2599#ifdef CONFIG_PPC64
2600 /*
2601 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2602 * that uses the allocator, we need to make sure we get the top of memory
2603 * available for us here...
2604 */
2605 if (RELOC(of_platform) == PLATFORM_PSERIES)
2606 prom_initialize_tce_table();
2607#endif
2608
2609 /*
2610 * On non-powermacs, try to instantiate RTAS and puts all CPUs
2611 * in spin-loops. PowerMacs don't have a working RTAS and use
2612 * a different way to spin CPUs
2613 */
2614 if (RELOC(of_platform) != PLATFORM_POWERMAC) {
2615 prom_instantiate_rtas();
2616 prom_hold_cpus();
2617 }
2618
2619 /*
2620 * Fill in some infos for use by the kernel later on
2621 */
cf68787b
BK
2622 if (RELOC(prom_memory_limit))
2623 prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
2624 &RELOC(prom_memory_limit),
2625 sizeof(prom_memory_limit));
9b6b563c 2626#ifdef CONFIG_PPC64
165785e5 2627 if (RELOC(prom_iommu_off))
a23414be
PM
2628 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2629 NULL, 0);
9b6b563c 2630
165785e5 2631 if (RELOC(prom_iommu_force_on))
a23414be
PM
2632 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2633 NULL, 0);
9b6b563c
PM
2634
2635 if (RELOC(prom_tce_alloc_start)) {
a23414be 2636 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
9b6b563c
PM
2637 &RELOC(prom_tce_alloc_start),
2638 sizeof(prom_tce_alloc_start));
a23414be 2639 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
9b6b563c
PM
2640 &RELOC(prom_tce_alloc_end),
2641 sizeof(prom_tce_alloc_end));
2642 }
2643#endif
2644
2645 /*
2646 * Fixup any known bugs in the device-tree
2647 */
2648 fixup_device_tree();
2649
2650 /*
2651 * Now finally create the flattened device-tree
2652 */
1f8737aa 2653 prom_printf("copying OF device tree...\n");
9b6b563c
PM
2654 flatten_device_tree();
2655
3825ac0e
PM
2656 /*
2657 * in case stdin is USB and still active on IBM machines...
2658 * Unfortunately quiesce crashes on some powermacs if we have
2659 * closed stdin already (in particular the powerbook 101).
2660 */
2661 if (RELOC(of_platform) != PLATFORM_POWERMAC)
2662 prom_close_stdin();
9b6b563c
PM
2663
2664 /*
2665 * Call OF "quiesce" method to shut down pending DMA's from
2666 * devices etc...
2667 */
1f8737aa 2668 prom_printf("Calling quiesce...\n");
9b6b563c
PM
2669 call_prom("quiesce", 0, 0);
2670
2671 /*
2672 * And finally, call the kernel passing it the flattened device
2673 * tree and NULL as r5, thus triggering the new entry point which
2674 * is common to us and kexec
2675 */
2676 hdr = RELOC(dt_header_start);
2677 prom_printf("returning from prom_init\n");
2678 prom_debug("->dt_header_start=0x%x\n", hdr);
2679
2680#ifdef CONFIG_PPC32
2681 reloc_got2(-offset);
2682#endif
2683
549e8152 2684 __start(hdr, kbase, 0);
9b6b563c
PM
2685
2686 return 0;
2687}