powerpc: Add PACA fields specific to 64-bit Book3E processors
[linux-2.6/linux-2.6-openrd.git] / arch / powerpc / kernel / prom_init.c
blobd942404779c1d2d871cd7024ae045e4448a2aa44
1 /*
2 * Procedures for interfacing to Open Firmware.
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
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.
16 #undef DEBUG_PROM
18 #include <stdarg.h>
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>
43 #include <asm/btext.h>
44 #include <asm/sections.h>
45 #include <asm/machdep.h>
47 #include <linux/linux_logo.h>
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.
55 #define MAX_PROPERTY_LENGTH (1UL * 1024 * 1024)
58 * Eventually bump that one up
60 #define DEVTREE_CHUNK_SIZE 0x100000
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
69 #define MEM_RESERVE_MAP_SIZE 8
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.)
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().
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).
90 #ifdef CONFIG_PPC64
91 #define RELOC(x) (*PTRRELOC(&(x)))
92 #define ADDR(x) (u32) add_reloc_offset((unsigned long)(x))
93 #define OF_WORKAROUNDS 0
94 #else
95 #define RELOC(x) (x)
96 #define ADDR(x) (u32) (x)
97 #define OF_WORKAROUNDS of_workarounds
98 int of_workarounds;
99 #endif
101 #define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */
102 #define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */
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)
110 #ifdef DEBUG_PROM
111 #define prom_debug(x...) prom_printf(x)
112 #else
113 #define prom_debug(x...)
114 #endif
117 typedef u32 prom_arg_t;
119 struct prom_args {
120 u32 service;
121 u32 nargs;
122 u32 nret;
123 prom_arg_t args[10];
126 struct prom_t {
127 ihandle root;
128 phandle chosen;
129 int cpu;
130 ihandle stdout;
131 ihandle mmumap;
132 ihandle memory;
135 struct mem_map_entry {
136 u64 base;
137 u64 size;
140 typedef u32 cell_t;
142 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
144 #ifdef CONFIG_PPC64
145 extern int enter_prom(struct prom_args *args, unsigned long entry);
146 #else
147 static inline int enter_prom(struct prom_args *args, unsigned long entry)
149 return ((int (*)(struct prom_args *))entry)(args);
151 #endif
153 extern void copy_and_flush(unsigned long dest, unsigned long src,
154 unsigned long size, unsigned long offset);
156 /* prom structure */
157 static struct prom_t __initdata prom;
159 static unsigned long prom_entry __initdata;
161 #define PROM_SCRATCH_SIZE 256
163 static char __initdata of_stdout_device[256];
164 static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
166 static unsigned long __initdata dt_header_start;
167 static unsigned long __initdata dt_struct_start, dt_struct_end;
168 static unsigned long __initdata dt_string_start, dt_string_end;
170 static unsigned long __initdata prom_initrd_start, prom_initrd_end;
172 #ifdef CONFIG_PPC64
173 static int __initdata prom_iommu_force_on;
174 static int __initdata prom_iommu_off;
175 static unsigned long __initdata prom_tce_alloc_start;
176 static unsigned long __initdata prom_tce_alloc_end;
177 #endif
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
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
189 static int __initdata of_platform;
191 static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
193 static unsigned long __initdata alloc_top;
194 static unsigned long __initdata alloc_top_high;
195 static unsigned long __initdata alloc_bottom;
196 static unsigned long __initdata rmo_top;
197 static unsigned long __initdata ram_top;
199 static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
200 static int __initdata mem_reserve_cnt;
202 static cell_t __initdata regbuf[1024];
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
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)
217 /* This is the one and *ONLY* place where we actually call open
218 * firmware.
221 static int __init call_prom(const char *service, int nargs, int nret, ...)
223 int i;
224 struct prom_args args;
225 va_list list;
227 args.service = ADDR(service);
228 args.nargs = nargs;
229 args.nret = nret;
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);
236 for (i = 0; i < nret; i++)
237 args.args[nargs+i] = 0;
239 if (enter_prom(&args, RELOC(prom_entry)) < 0)
240 return PROM_ERROR;
242 return (nret > 0) ? args.args[nargs] : 0;
245 static int __init call_prom_ret(const char *service, int nargs, int nret,
246 prom_arg_t *rets, ...)
248 int i;
249 struct prom_args args;
250 va_list list;
252 args.service = ADDR(service);
253 args.nargs = nargs;
254 args.nret = nret;
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);
261 for (i = 0; i < nret; i++)
262 args.args[nargs+i] = 0;
264 if (enter_prom(&args, RELOC(prom_entry)) < 0)
265 return PROM_ERROR;
267 if (rets != NULL)
268 for (i = 1; i < nret; ++i)
269 rets[i-1] = args.args[nargs+i];
271 return (nret > 0) ? args.args[nargs] : 0;
275 static void __init prom_print(const char *msg)
277 const char *p, *q;
278 struct prom_t *_prom = &RELOC(prom);
280 if (_prom->stdout == 0)
281 return;
283 for (p = msg; *p != 0; p = q) {
284 for (q = p; *q != 0 && *q != '\n'; ++q)
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);
296 static void __init prom_print_hex(unsigned long val)
298 int i, nibbles = sizeof(val)*2;
299 char buf[sizeof(val)*2+1];
300 struct prom_t *_prom = &RELOC(prom);
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;
308 buf[nibbles] = '\0';
309 call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
313 static void __init prom_printf(const char *format, ...)
315 const char *p, *q, *s;
316 va_list args;
317 unsigned long v;
318 struct prom_t *_prom = &RELOC(prom);
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)
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;
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;
356 static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
357 unsigned long align)
359 struct prom_t *_prom = &RELOC(prom);
361 if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
363 * Old OF requires we claim physical and virtual separately
364 * and then map explicitly (assuming virtual mode)
366 int ret;
367 prom_arg_t result;
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;
382 /* the 0x12 is M (coherence) + PP == read/write */
383 call_prom("call-method", 6, 1,
384 ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
385 return virt;
387 return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
388 (prom_arg_t)align);
391 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
393 #ifdef CONFIG_PPC64
394 reason = PTRRELOC(reason);
395 #endif
396 prom_print(reason);
397 /* 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");
402 /* ToDo: should put up an SRC here on p/iSeries */
403 call_prom("exit", 0, 0);
405 for (;;) /* should never get here */
410 static int __init prom_next_node(phandle *nodep)
412 phandle node;
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;
427 static int inline prom_getprop(phandle node, const char *pname,
428 void *value, size_t valuelen)
430 return call_prom("getprop", 4, 1, node, ADDR(pname),
431 (u32)(unsigned long) value, (u32) valuelen);
434 static int inline prom_getproplen(phandle node, const char *pname)
436 return call_prom("getproplen", 2, 1, node, ADDR(pname));
439 static void add_string(char **str, const char *q)
441 char *p = *str;
443 while (*q)
444 *p++ = *q++;
445 *p++ = ' ';
446 *str = p;
449 static char *tohex(unsigned int x)
451 static char digits[] = "0123456789abcdef";
452 static char result[9];
453 int i;
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];
465 static int __init prom_setprop(phandle node, const char *nodename,
466 const char *pname, void *value, size_t valuelen)
468 char cmd[256], *p;
470 if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
471 return call_prom("setprop", 4, 1, node, ADDR(pname),
472 (u32)(unsigned long) value, (u32) valuelen);
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);
488 * Early parsing of the command line passed to the kernel, used for
489 * "mem=x" and the options that affect the iommu
491 static void __init early_cmdline_parse(void)
493 struct prom_t *_prom = &RELOC(prom);
494 #ifdef CONFIG_PPC64
495 const char *opt;
496 #endif
497 char *p;
498 int l = 0;
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
505 if (l <= 0 || p[0] == '\0') /* dbl check */
506 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));
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))
519 RELOC(prom_iommu_off) = 1;
520 else if (!strncmp(opt, RELOC("force"), 5))
521 RELOC(prom_iommu_force_on) = 1;
523 #endif
526 #ifdef CONFIG_PPC_PSERIES
528 * 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.
537 * New method - extensible architecture description vector.
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.
543 #define W(x) ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
544 ((x) >> 8) & 0xff, (x) & 0xff
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*/
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 */
557 #define OV1_PPC_2_06 0x02 /* set if we support PowerPC 2.06 */
559 /* Option vector 2: Open Firmware options supported */
560 #define OV2_REAL_MODE 0x20 /* set if we want OF in real mode */
562 /* Option vector 3: processor options supported */
563 #define OV3_FP 0x80 /* floating point */
564 #define OV3_VMX 0x40 /* VMX/Altivec */
565 #define OV3_DFP 0x20 /* decimal FP */
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 */
573 #define OV5_DONATE_DEDICATE_CPU 0x02 /* donate dedicated CPU support */
574 /* 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 */
580 #ifdef CONFIG_PPC_SMLPAR
581 #define OV5_CMO 0x80 /* Cooperative Memory Overcommitment */
582 #else
583 #define OV5_CMO 0x00
584 #endif
587 * The architecture vector has an array of PVR mask/value pairs,
588 * followed by # option vectors - 1, followed by the option vectors.
590 static unsigned char ibm_architecture_vec[] = {
591 W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */
592 W(0xffff0000), W(0x003e0000), /* POWER6 */
593 W(0xffff0000), W(0x003f0000), /* POWER7 */
594 W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */
595 W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
596 W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
597 5 - 1, /* 5 option vectors */
599 /* option vector 1: processor architectures supported */
600 3 - 2, /* length */
601 0, /* don't ignore, don't halt */
602 OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
603 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
605 /* option vector 2: Open Firmware options supported */
606 34 - 2, /* length */
607 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 */
614 W(64), /* 64MB min RMA */
615 W(0xffffffff), /* full client load */
616 0, /* min RMA percentage of total RAM */
617 48, /* max log_2(hash table size) */
619 /* option vector 3: processor options supported */
620 3 - 2, /* length */
621 0, /* don't ignore, don't halt */
622 OV3_FP | OV3_VMX | OV3_DFP,
624 /* option vector 4: IBM PAPR implementation */
625 2 - 2, /* length */
626 0, /* don't halt */
628 /* option vector 5: PAPR/OF options */
629 5 - 2, /* length */
630 0, /* don't ignore, don't halt */
631 OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
632 OV5_DONATE_DEDICATE_CPU | OV5_MSI,
634 OV5_CMO,
637 /* Old method - ELF header with PT_NOTE sections */
638 static 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;
671 } fake_elf = {
672 .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
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)
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
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 = {
713 .lpar_affinity = 0,
714 .min_rmo_size = 64, /* in megabytes */
715 .min_rmo_percent = 0,
716 .max_pft_size = 48, /* 2^48 bytes max PFT size */
717 .splpar = 1,
718 .min_load = ~0U,
719 .new_mem_def = 0
724 static void __init prom_send_capabilities(void)
726 ihandle elfloader, root;
727 prom_arg_t ret;
729 root = call_prom("open", 1, 1, ADDR("/"));
730 if (root != 0) {
731 /* try calling the ibm,client-architecture-support method */
732 prom_printf("Calling ibm,client-architecture...");
733 if (call_prom_ret("call-method", 3, 2, &ret,
734 ADDR("ibm,client-architecture-support"),
735 root,
736 ADDR(ibm_architecture_vec)) == 0) {
737 /* the call exists... */
738 if (ret)
739 prom_printf("\nWARNING: ibm,client-architecture"
740 "-support call FAILED!\n");
741 call_prom("close", 1, 0, root);
742 prom_printf(" done\n");
743 return;
745 call_prom("close", 1, 0, root);
748 /* no ibm,client-architecture-support call, try the old way */
749 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;
754 call_prom("call-method", 3, 1, ADDR("process-elf-header"),
755 elfloader, ADDR(&fake_elf));
756 call_prom("close", 1, 0, elfloader);
758 #endif
761 * Memory allocation strategy... our layout is normally:
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.
769 * alloc_top is set to the top of RMO, eventually shrink down if the
770 * TCEs overlap
772 * alloc_bottom is set to the top of kernel/initrd
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.
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.
787 * Allocates memory in the RMO upward from the kernel/initrd
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
793 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
795 unsigned long base = RELOC(alloc_bottom);
796 unsigned long addr = 0;
798 if (align)
799 base = _ALIGN_UP(base, align);
800 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");
804 if (align)
805 base = _ALIGN_UP(RELOC(alloc_bottom), align);
806 else
807 base = RELOC(alloc_bottom);
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);
813 if (addr != PROM_ERROR && addr != 0)
814 break;
815 addr = 0;
816 if (align == 0)
817 break;
819 if (addr == 0)
820 return 0;
821 RELOC(alloc_bottom) = addr;
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));
830 return addr;
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.
838 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
839 int highmem)
841 unsigned long base, addr = 0;
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");
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 !
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;
864 RELOC(alloc_top_high) = addr;
865 goto bail;
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);
873 if (addr != PROM_ERROR && addr != 0)
874 break;
875 addr = 0;
877 if (addr == 0)
878 return 0;
879 RELOC(alloc_top) = addr;
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));
889 return addr;
893 * Parse a "reg" cell
895 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
897 cell_t *p = *cellp;
898 unsigned long r = 0;
900 /* Ignore more than 2 cells */
901 while (s > sizeof(unsigned long) / 4) {
902 p++;
903 s--;
905 r = *p++;
906 #ifdef CONFIG_PPC64
907 if (s > 1) {
908 r <<= 32;
909 r |= *(p++);
911 #endif
912 *cellp = p;
913 return r;
917 * Very dumb function for adding to the memory reserve list, but
918 * we don't need anything smarter at this point
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.
924 static void __init reserve_mem(u64 base, u64 size)
926 u64 top = base + size;
927 unsigned long cnt = RELOC(mem_reserve_cnt);
929 if (size == 0)
930 return;
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
936 base = _ALIGN_DOWN(base, PAGE_SIZE);
937 top = _ALIGN_UP(top, PAGE_SIZE);
938 size = top - base;
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;
948 * Initialize memory allocation mechanism, parse "memory" nodes and
949 * obtain that way the top of memory and RMO to setup out local allocator
951 static void __init prom_init_mem(void)
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;
961 * We iterate the memory nodes to find
962 * 1) top of RMO (first node)
963 * 2) top of memory
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);
972 prom_debug("scanning memory:\n");
973 path = RELOC(prom_scratch);
975 for (node = 0; prom_next_node(&node); ) {
976 type[0] = 0;
977 prom_getprop(node, "device_type", type, sizeof(type));
979 if (type[0] == 0) {
981 * CHRP Longtrail machines have no device_type
982 * on the memory node, so check the name instead...
984 prom_getprop(node, "name", type, sizeof(type));
986 if (strcmp(type, RELOC("memory")))
987 continue;
989 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);
994 p = RELOC(regbuf);
995 endp = p + (plen / sizeof(cell_t));
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 */
1003 while ((endp - p) >= (rac + rsc)) {
1004 unsigned long base, size;
1006 base = prom_next_cell(rac, &p);
1007 size = prom_next_cell(rsc, &p);
1009 if (size == 0)
1010 continue;
1011 prom_debug(" %x %x\n", base, size);
1012 if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
1013 RELOC(rmo_top) = size;
1014 if ((base + size) > RELOC(ram_top))
1015 RELOC(ram_top) = base + size;
1019 RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
1021 /* Check if we have an initrd after the kernel, if we do move our bottom
1022 * point to after it
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));
1030 * 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.
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);
1041 RELOC(alloc_top_high) = RELOC(ram_top);
1043 prom_printf("memory layout at init:\n");
1044 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));
1053 * Allocate room for and instantiate RTAS
1055 static void __init prom_instantiate_rtas(void)
1057 phandle rtas_node;
1058 ihandle rtas_inst;
1059 u32 base, entry = 0;
1060 u32 size = 0;
1062 prom_debug("prom_instantiate_rtas: start...\n");
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;
1069 prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1070 if (size == 0)
1071 return;
1073 base = alloc_down(size, PAGE_SIZE, 0);
1074 if (base == 0) {
1075 prom_printf("RTAS allocation failed !\n");
1076 return;
1079 rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1080 if (!IHANDLE_VALID(rtas_inst)) {
1081 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
1082 return;
1085 prom_printf("instantiating rtas at 0x%x...", base);
1087 if (call_prom_ret("call-method", 3, 2, &entry,
1088 ADDR("instantiate-rtas"),
1089 rtas_inst, base) != 0
1090 || entry == 0) {
1091 prom_printf(" failed\n");
1092 return;
1094 prom_printf(" done\n");
1096 reserve_mem(base, size);
1098 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));
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);
1107 prom_debug("prom_instantiate_rtas: end...\n");
1110 #ifdef CONFIG_PPC64
1112 * Allocate room for and initialize TCE tables
1114 static void __init prom_initialize_tce_table(void)
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;
1126 if (RELOC(prom_iommu_off))
1127 return;
1129 prom_debug("starting prom_initialize_tce_table\n");
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;
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));
1145 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1146 continue;
1148 /* Keep the old logic intact to avoid regression. */
1149 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;
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;
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.
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
1179 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1180 minsize = 8UL << 20;
1181 else
1182 minsize = 4UL << 20;
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;
1192 /* It seems OF doesn't null-terminate the path :-( */
1193 memset(path, 0, PROM_SCRATCH_SIZE);
1194 /* 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");
1200 /* 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));
1204 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);
1209 /* Initialize the table to have a one-to-one mapping
1210 * over the allocated size.
1212 tce_entryp = (u64 *)base;
1213 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1214 tce_entry = (i << PAGE_SHIFT);
1215 tce_entry |= 0x3;
1216 *tce_entryp = tce_entry;
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");
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);
1232 reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1234 /* 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;
1239 /* Flag the first invalid entry */
1240 prom_debug("ending prom_initialize_tce_table\n");
1242 #endif
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.
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.
1257 * We also use physical address 0x4 here to tell when a cpu
1258 * is in its holding pattern code.
1260 * -- Cort
1263 * We want to reference the copy of __secondary_hold_* in the
1264 * 0 - 0x100 address range
1266 #define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
1268 static void __init prom_hold_cpus(void)
1270 unsigned long i;
1271 unsigned int reg;
1272 phandle node;
1273 char type[64];
1274 struct prom_t *_prom = &RELOC(prom);
1275 unsigned long *spinloop
1276 = (void *) LOW_ADDR(__secondary_hold_spinloop);
1277 unsigned long *acknowledge
1278 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
1279 unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
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);
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.
1294 *spinloop = 0;
1296 /* 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;
1303 /* Skip non-configured cpus. */
1304 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1305 if (strcmp(type, RELOC("okay")) != 0)
1306 continue;
1308 reg = -1;
1309 prom_getprop(node, "reg", &reg, sizeof(reg));
1311 prom_debug("cpu hw idx = 0x%x\n", reg);
1313 /* Init the acknowledge var which will be reset by
1314 * the secondary cpu when it awakens from its OF
1315 * spinloop.
1317 *acknowledge = (unsigned long)-1;
1319 if (reg != _prom->cpu) {
1320 /* Primary Thread of non-boot cpu */
1321 prom_printf("starting cpu hw idx %x... ", reg);
1322 call_prom("start-cpu", 3, 0, node,
1323 secondary_hold, reg);
1325 for (i = 0; (i < 100000000) &&
1326 (*acknowledge == ((unsigned long)-1)); i++ )
1327 mb();
1329 if (*acknowledge == reg)
1330 prom_printf("done\n");
1331 else
1332 prom_printf("failed: %x\n", *acknowledge);
1334 #ifdef CONFIG_SMP
1335 else
1336 prom_printf("boot cpu hw idx %x\n", reg);
1337 #endif /* CONFIG_SMP */
1340 prom_debug("prom_hold_cpus: end...\n");
1344 static void __init prom_init_client_services(unsigned long pp)
1346 struct prom_t *_prom = &RELOC(prom);
1348 /* Get a handle to the prom entry point before anything else */
1349 RELOC(prom_entry) = pp;
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 :( */
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 :( */
1361 _prom->mmumap = 0;
1364 #ifdef CONFIG_PPC32
1366 * For really old powermacs, we need to map things we claim.
1367 * For that, we need the ihandle of the mmu.
1368 * Also, on the longtrail, we need to work around other bugs.
1370 static void __init prom_find_mmu(void)
1372 struct prom_t *_prom = &RELOC(prom);
1373 phandle oprom;
1374 char version[64];
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;
1382 /* XXX might need to add other versions here */
1383 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
1389 return;
1390 _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
1391 prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1392 sizeof(_prom->mmumap));
1393 if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1394 of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
1396 #else
1397 #define prom_find_mmu()
1398 #endif
1400 static void __init prom_init_stdout(void)
1402 struct prom_t *_prom = &RELOC(prom);
1403 char *path = RELOC(of_stdout_device);
1404 char type[16];
1405 u32 val;
1407 if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1408 prom_panic("cannot find stdout");
1410 _prom->stdout = val;
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);
1416 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1417 &val, sizeof(val));
1418 prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
1419 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1420 path, strlen(path) + 1);
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)
1426 prom_setprop(val, path, "linux,boot-display", NULL, 0);
1429 static void __init prom_close_stdin(void)
1431 struct prom_t *_prom = &RELOC(prom);
1432 ihandle val;
1434 if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1435 call_prom("close", 1, 0, val);
1438 static int __init prom_find_machine_type(void)
1440 struct prom_t *_prom = &RELOC(prom);
1441 char compat[256];
1442 int len, i = 0;
1443 #ifdef CONFIG_PPC64
1444 phandle rtas;
1445 int x;
1446 #endif
1448 /* Look for a PowerMac */
1449 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")) ||
1459 strstr(p, RELOC("MacRISC")))
1460 return PLATFORM_POWERMAC;
1461 #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.
1466 if (strstr(p, RELOC("IBM,CBEA")) ||
1467 strstr(p, RELOC("IBM,CPBW-1.0")))
1468 return PLATFORM_GENERIC;
1469 #endif /* CONFIG_PPC64 */
1470 i += sl + 1;
1473 #ifdef CONFIG_PPC64
1474 /* 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
1480 len = prom_getprop(_prom->root, "device_type",
1481 compat, sizeof(compat)-1);
1482 if (len <= 0)
1483 return PLATFORM_GENERIC;
1484 if (strcmp(compat, RELOC("chrp")))
1485 return PLATFORM_GENERIC;
1487 /* Default to pSeries. We need to know if we are running LPAR */
1488 rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1489 if (!PHANDLE_VALID(rtas))
1490 return PLATFORM_GENERIC;
1491 x = prom_getproplen(rtas, "ibm,hypertas-functions");
1492 if (x != PROM_ERROR) {
1493 prom_debug("Hypertas detected, assuming LPAR !\n");
1494 return PLATFORM_PSERIES_LPAR;
1496 return PLATFORM_PSERIES;
1497 #else
1498 return PLATFORM_GENERIC;
1499 #endif
1502 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1504 return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
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.
1515 static void __init prom_check_displays(void)
1517 char type[16], *path;
1518 phandle node;
1519 ihandle ih;
1520 int i;
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
1540 const unsigned char *clut;
1542 prom_debug("Looking for displays\n");
1543 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;
1549 /* It seems OF doesn't null-terminate the path :-( */
1550 path = RELOC(prom_scratch);
1551 memset(path, 0, PROM_SCRATCH_SIZE);
1554 * leave some room at the end of the path for appending extra
1555 * arguments
1557 if (call_prom("package-to-path", 3, 1, node, path,
1558 PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1559 continue;
1560 prom_printf("found display : %s, opening... ", path);
1562 ih = call_prom("open", 1, 1, path);
1563 if (ih == 0) {
1564 prom_printf("failed\n");
1565 continue;
1568 /* Success */
1569 prom_printf("done\n");
1570 prom_setprop(node, path, "linux,opened", NULL, 0);
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;
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 */
1591 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1592 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
1593 unsigned long needed, unsigned long align)
1595 void *ret;
1597 *mem_start = _ALIGN(*mem_start, align);
1598 while ((*mem_start + needed) > *mem_end) {
1599 unsigned long room, chunk;
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);
1614 ret = (void *)*mem_start;
1615 *mem_start += needed;
1617 return ret;
1620 #define dt_push_token(token, mem_start, mem_end) \
1621 do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
1623 static unsigned long __init dt_find_string(char *str)
1625 char *s, *os;
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;
1634 return 0;
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.
1641 #define MAX_PROPERTY_NAME 64
1643 static void __init scan_dt_build_strings(phandle node,
1644 unsigned long *mem_start,
1645 unsigned long *mem_end)
1647 char *prev_name, *namep, *sstart;
1648 unsigned long soff;
1649 phandle child;
1651 sstart = (char *)RELOC(dt_string_start);
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;
1664 /* skip "name" */
1665 if (strcmp(namep, RELOC("name")) == 0) {
1666 *mem_start = (unsigned long)namep;
1667 prev_name = RELOC("name");
1668 continue;
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;
1680 prev_name = namep;
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);
1691 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
1692 unsigned long *mem_end)
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];
1699 int l, room;
1701 dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
1703 /* get the node's full name */
1704 namep = (char *)*mem_start;
1705 room = *mem_end - *mem_start;
1706 if (room > 255)
1707 room = 255;
1708 l = call_prom("package-to-path", 3, 1, node, namep, room);
1709 if (l >= 0) {
1710 /* Didn't fit? Get more room. */
1711 if (l >= room) {
1712 if (l >= *mem_end - *mem_start)
1713 namep = make_room(mem_start, mem_end, l+1, 1);
1714 call_prom("package-to-path", 3, 1, node, namep, l);
1716 namep[l] = '\0';
1718 /* Fixup an Apple bug where they have bogus \0 chars in the
1719 * middle of the path in some properties, and extract
1720 * the unit name (everything after the last '/').
1722 for (lp = p = namep, ep = namep + l; p < ep; p++) {
1723 if (*p == '/')
1724 lp = namep;
1725 else if (*p != 0)
1726 *lp++ = *p;
1728 *lp = 0;
1729 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
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);
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;
1745 /* skip "name" */
1746 if (strcmp(RELOC(pname), RELOC("name")) == 0) {
1747 prev_name = RELOC("name");
1748 continue;
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;
1758 prev_name = sstart + soff;
1760 /* get length */
1761 l = call_prom("getproplen", 2, 1, node, RELOC(pname));
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;
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);
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);
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;
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);
1805 dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
1808 static void __init flatten_device_tree(void)
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;
1818 * Check how much room we have between alloc top & bottom (+/- a
1819 * few pages), crop to 4Mb, as this is our "chuck" size
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));
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);
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");
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);
1844 /* Start of strings */
1845 mem_start = PAGE_ALIGN(mem_start);
1846 RELOC(dt_string_start) = mem_start;
1847 mem_start += 4; /* hole */
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;
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;
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);
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;
1879 /* Copy the reserve map in */
1880 memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
1882 #ifdef DEBUG_PROM
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);
1891 #endif
1892 /* Bump mem_reserve_cnt to cause further reservations to fail
1893 * since it's too late.
1895 RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
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));
1904 #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. */
1907 static void __init fixup_device_tree_maple(void)
1909 phandle isa;
1910 u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
1911 u32 isa_ranges[6];
1912 char *name;
1914 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 */
1921 if (!PHANDLE_VALID(isa))
1922 return;
1924 if (prom_getproplen(isa, "ranges") != 12)
1925 return;
1926 if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
1927 == PROM_ERROR)
1928 return;
1930 if (isa_ranges[0] != 0x1 ||
1931 isa_ranges[1] != 0xf4000000 ||
1932 isa_ranges[2] != 0x00010000)
1933 return;
1935 prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
1937 isa_ranges[0] = 0x1;
1938 isa_ranges[1] = 0x0;
1939 isa_ranges[2] = rloc;
1940 isa_ranges[3] = 0x0;
1941 isa_ranges[4] = 0x0;
1942 isa_ranges[5] = 0x00010000;
1943 prom_setprop(isa, name, "ranges",
1944 isa_ranges, sizeof(isa_ranges));
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 */
1950 static void __init fixup_device_tree_maple_memory_controller(void)
1952 phandle mc;
1953 u32 mc_reg[4];
1954 char *name = "/hostbridge@f8000000";
1955 struct prom_t *_prom = &RELOC(prom);
1956 u32 ac, sc;
1958 mc = call_prom("finddevice", 1, 1, ADDR(name));
1959 if (!PHANDLE_VALID(mc))
1960 return;
1962 if (prom_getproplen(mc, "reg") != 8)
1963 return;
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;
1970 if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
1971 return;
1973 if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
1974 return;
1976 prom_printf("Fixing up bogus hostbridge on Maple...\n");
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));
1984 #else
1985 #define fixup_device_tree_maple()
1986 #define fixup_device_tree_maple_memory_controller()
1987 #endif
1989 #ifdef CONFIG_PPC_CHRP
1991 * Pegasos and BriQ lacks the "ranges" property in the isa node
1992 * Pegasos needs decimal IRQ 14/15, not hexadecimal
1993 * Pegasos has the IDE configured in legacy mode, but advertised as native
1995 static void __init fixup_device_tree_chrp(void)
1997 phandle ph;
1998 u32 prop[6];
1999 u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2000 char *name;
2001 int rc;
2003 name = "/pci@80000000/isa@c";
2004 ph = call_prom("finddevice", 1, 1, ADDR(name));
2005 if (!PHANDLE_VALID(ph)) {
2006 name = "/pci@ff500000/isa@6";
2007 ph = call_prom("finddevice", 1, 1, ADDR(name));
2008 rloc = 0x01003000; /* IO space; PCI device = 6 */
2010 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");
2015 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));
2025 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;
2031 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));
2040 #else
2041 #define fixup_device_tree_chrp()
2042 #endif
2044 #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
2045 static void __init fixup_device_tree_pmac(void)
2047 phandle u3, i2c, mpic;
2048 u32 u3_rev;
2049 u32 interrupts[2];
2050 u32 parent;
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;
2063 /* check if proper rev of u3 */
2064 if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2065 == PROM_ERROR)
2066 return;
2067 if (u3_rev < 0x35 || u3_rev > 0x39)
2068 return;
2069 /* does it need fixup ? */
2070 if (prom_getproplen(i2c, "interrupts") > 0)
2071 return;
2073 prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2075 /* interrupt on this revision of u3 is number 0 and level */
2076 interrupts[0] = 0;
2077 interrupts[1] = 1;
2078 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2079 &interrupts, sizeof(interrupts));
2080 parent = (u32)mpic;
2081 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2082 &parent, sizeof(parent));
2084 #else
2085 #define fixup_device_tree_pmac()
2086 #endif
2088 #ifdef CONFIG_PPC_EFIKA
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.
2095 static void __init fixup_device_tree_efika_add_phy(void)
2097 u32 node;
2098 char prop[64];
2099 int rv;
2101 /* Check if /builtin/ethernet exists - bail if it doesn't */
2102 node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
2103 if (!PHANDLE_VALID(node))
2104 return;
2106 /* Check if the phy-handle property exists - bail if it does */
2107 rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2108 if (!rv)
2109 return;
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
2116 /* Check for an MDIO bus node - if missing then create one */
2117 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"
2125 " s\" mdio\" device-name"
2126 " s\" fsl,mpc5200b-mdio\" encode-string"
2127 " 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");
2136 /* 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"));
2140 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");
2157 static void __init fixup_device_tree_efika(void)
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;
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;
2173 rv = prom_getprop(node, "model", prop, sizeof(prop));
2174 if (rv == PROM_ERROR)
2175 return;
2176 if (strcmp(prop, "EFIKA5K2"))
2177 return;
2179 prom_printf("Applying EFIKA device tree fixups\n");
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"));
2187 /* 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"));
2195 /* 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));
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));
2217 /* Make sure ethernet phy-handle property exists */
2218 fixup_device_tree_efika_add_phy();
2220 #else
2221 #define fixup_device_tree_efika()
2222 #endif
2224 static void __init fixup_device_tree(void)
2226 fixup_device_tree_maple();
2227 fixup_device_tree_maple_memory_controller();
2228 fixup_device_tree_chrp();
2229 fixup_device_tree_pmac();
2230 fixup_device_tree_efika();
2233 static void __init prom_find_boot_cpu(void)
2235 struct prom_t *_prom = &RELOC(prom);
2236 u32 getprop_rval;
2237 ihandle prom_cpu;
2238 phandle cpu_pkg;
2240 _prom->cpu = 0;
2241 if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
2242 return;
2244 cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2246 prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
2247 _prom->cpu = getprop_rval;
2249 prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
2252 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2254 #ifdef CONFIG_BLK_DEV_INITRD
2255 struct prom_t *_prom = &RELOC(prom);
2257 if (r3 && r4 && r4 != 0xdeadbeef) {
2258 unsigned long val;
2260 RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
2261 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2263 val = RELOC(prom_initrd_start);
2264 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2265 &val, sizeof(val));
2266 val = RELOC(prom_initrd_end);
2267 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2268 &val, sizeof(val));
2270 reserve_mem(RELOC(prom_initrd_start),
2271 RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
2273 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
2274 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
2276 #endif /* CONFIG_BLK_DEV_INITRD */
2280 * We enter here early on, when the Open Firmware prom is still
2281 * handling exceptions and the MMU hash table for us.
2284 unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2285 unsigned long pp,
2286 unsigned long r6, unsigned long r7,
2287 unsigned long kbase)
2289 struct prom_t *_prom;
2290 unsigned long hdr;
2292 #ifdef CONFIG_PPC32
2293 unsigned long offset = reloc_offset();
2294 reloc_got2(offset);
2295 #endif
2297 _prom = &RELOC(prom);
2300 * First zero the BSS
2302 memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
2305 * Init interface to Open Firmware, get some node references,
2306 * like /chosen
2308 prom_init_client_services(pp);
2311 * See if this OF is old enough that we need to do explicit maps
2312 * and other workarounds
2314 prom_find_mmu();
2317 * Init prom stdout device
2319 prom_init_stdout();
2321 prom_printf("Preparing to boot %s", RELOC(linux_banner));
2324 * Get default machine type. At this point, we do not differentiate
2325 * between pSeries SMP and pSeries LPAR
2327 RELOC(of_platform) = prom_find_machine_type();
2329 #ifndef CONFIG_RELOCATABLE
2330 /* 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");
2333 #endif
2336 * Check for an initrd
2338 prom_check_initrd(r3, r4);
2340 #ifdef CONFIG_PPC_PSERIES
2342 * On pSeries, inform the firmware about our capabilities
2344 if (RELOC(of_platform) == PLATFORM_PSERIES ||
2345 RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
2346 prom_send_capabilities();
2347 #endif
2350 * Copy the CPU hold code
2352 if (RELOC(of_platform) != PLATFORM_POWERMAC)
2353 copy_and_flush(0, kbase, 0x100, 0);
2356 * Do early parsing of command line
2358 early_cmdline_parse();
2361 * Initialize memory management within prom_init
2363 prom_init_mem();
2366 * Determine which cpu is actually running right _now_
2368 prom_find_boot_cpu();
2371 * Initialize display devices
2373 prom_check_displays();
2375 #ifdef CONFIG_PPC64
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...
2381 if (RELOC(of_platform) == PLATFORM_PSERIES)
2382 prom_initialize_tce_table();
2383 #endif
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
2390 if (RELOC(of_platform) != PLATFORM_POWERMAC) {
2391 prom_instantiate_rtas();
2392 prom_hold_cpus();
2396 * Fill in some infos for use by the kernel later on
2398 #ifdef CONFIG_PPC64
2399 if (RELOC(prom_iommu_off))
2400 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2401 NULL, 0);
2403 if (RELOC(prom_iommu_force_on))
2404 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2405 NULL, 0);
2407 if (RELOC(prom_tce_alloc_start)) {
2408 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
2409 &RELOC(prom_tce_alloc_start),
2410 sizeof(prom_tce_alloc_start));
2411 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
2412 &RELOC(prom_tce_alloc_end),
2413 sizeof(prom_tce_alloc_end));
2415 #endif
2418 * Fixup any known bugs in the device-tree
2420 fixup_device_tree();
2423 * Now finally create the flattened device-tree
2425 prom_printf("copying OF device tree...\n");
2426 flatten_device_tree();
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).
2433 if (RELOC(of_platform) != PLATFORM_POWERMAC)
2434 prom_close_stdin();
2437 * Call OF "quiesce" method to shut down pending DMA's from
2438 * devices etc...
2440 prom_printf("Calling quiesce...\n");
2441 call_prom("quiesce", 0, 0);
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
2448 hdr = RELOC(dt_header_start);
2449 prom_printf("returning from prom_init\n");
2450 prom_debug("->dt_header_start=0x%x\n", hdr);
2452 #ifdef CONFIG_PPC32
2453 reloc_got2(-offset);
2454 #endif
2456 __start(hdr, kbase, 0);
2458 return 0;