2 * Procedures for interfacing to Open Firmware.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
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.
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>
34 #include <asm/processor.h>
38 #include <asm/system.h>
40 #include <asm/pgtable.h>
42 #include <asm/iommu.h>
43 #include <asm/btext.h>
44 #include <asm/sections.h>
45 #include <asm/machdep.h>
48 #include <linux/linux_logo.h>
51 * Eventually bump that one up
53 #define DEVTREE_CHUNK_SIZE 0x100000
56 * This is the size of the local memory reserve map that gets copied
57 * into the boot params passed to the kernel. That size is totally
58 * flexible as the kernel just reads the list until it encounters an
59 * entry with size 0, so it can be changed without breaking binary
62 #define MEM_RESERVE_MAP_SIZE 8
65 * prom_init() is called very early on, before the kernel text
66 * and data have been mapped to KERNELBASE. At this point the code
67 * is running at whatever address it has been loaded at.
68 * On ppc32 we compile with -mrelocatable, which means that references
69 * to extern and static variables get relocated automatically.
70 * On ppc64 we have to relocate the references explicitly with
71 * RELOC. (Note that strings count as static variables.)
73 * Because OF may have mapped I/O devices into the area starting at
74 * KERNELBASE, particularly on CHRP machines, we can't safely call
75 * OF once the kernel has been mapped to KERNELBASE. Therefore all
76 * OF calls must be done within prom_init().
78 * ADDR is used in calls to call_prom. The 4th and following
79 * arguments to call_prom should be 32-bit values.
80 * On ppc64, 64 bit values are truncated to 32 bits (and
81 * fortunately don't get interpreted as two arguments).
84 #define RELOC(x) (*PTRRELOC(&(x)))
85 #define ADDR(x) (u32) add_reloc_offset((unsigned long)(x))
86 #define OF_WORKAROUNDS 0
89 #define ADDR(x) (u32) (x)
90 #define OF_WORKAROUNDS of_workarounds
94 #define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */
95 #define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */
97 #define PROM_BUG() do { \
98 prom_printf("kernel BUG at %s line 0x%x!\n", \
99 RELOC(__FILE__), __LINE__); \
100 __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
104 #define prom_debug(x...) prom_printf(x)
106 #define prom_debug(x...)
110 typedef u32 prom_arg_t;
128 struct mem_map_entry {
135 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
136 unsigned long r6, unsigned long r7, unsigned long r8,
140 extern int enter_prom(struct prom_args *args, unsigned long entry);
142 static inline int enter_prom(struct prom_args *args, unsigned long entry)
144 return ((int (*)(struct prom_args *))entry)(args);
148 extern void copy_and_flush(unsigned long dest, unsigned long src,
149 unsigned long size, unsigned long offset);
152 static struct prom_t __initdata prom;
154 static unsigned long prom_entry __initdata;
156 #define PROM_SCRATCH_SIZE 256
158 static char __initdata of_stdout_device[256];
159 static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
161 static unsigned long __initdata dt_header_start;
162 static unsigned long __initdata dt_struct_start, dt_struct_end;
163 static unsigned long __initdata dt_string_start, dt_string_end;
165 static unsigned long __initdata prom_initrd_start, prom_initrd_end;
168 static int __initdata prom_iommu_force_on;
169 static int __initdata prom_iommu_off;
170 static unsigned long __initdata prom_tce_alloc_start;
171 static unsigned long __initdata prom_tce_alloc_end;
174 /* Platforms codes are now obsolete in the kernel. Now only used within this
175 * file and ultimately gone too. Feel free to change them if you need, they
176 * are not shared with anything outside of this file anymore
178 #define PLATFORM_PSERIES 0x0100
179 #define PLATFORM_PSERIES_LPAR 0x0101
180 #define PLATFORM_LPAR 0x0001
181 #define PLATFORM_POWERMAC 0x0400
182 #define PLATFORM_GENERIC 0x0500
183 #define PLATFORM_OPAL 0x0600
185 static int __initdata of_platform;
187 static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
189 static unsigned long __initdata prom_memory_limit;
191 static unsigned long __initdata alloc_top;
192 static unsigned long __initdata alloc_top_high;
193 static unsigned long __initdata alloc_bottom;
194 static unsigned long __initdata rmo_top;
195 static unsigned long __initdata ram_top;
197 static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
198 static int __initdata mem_reserve_cnt;
200 static cell_t __initdata regbuf[1024];
204 * Error results ... some OF calls will return "-1" on error, some
205 * will return 0, some will return either. To simplify, here are
206 * macros to use with any ihandle or phandle return value to check if
210 #define PROM_ERROR (-1u)
211 #define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
212 #define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
215 /* This is the one and *ONLY* place where we actually call open
219 static int __init call_prom(const char *service, int nargs, int nret, ...)
222 struct prom_args args;
225 args.service = ADDR(service);
229 va_start(list, nret);
230 for (i = 0; i < nargs; i++)
231 args.args[i] = va_arg(list, prom_arg_t);
234 for (i = 0; i < nret; i++)
235 args.args[nargs+i] = 0;
237 if (enter_prom(&args, RELOC(prom_entry)) < 0)
240 return (nret > 0) ? args.args[nargs] : 0;
243 static int __init call_prom_ret(const char *service, int nargs, int nret,
244 prom_arg_t *rets, ...)
247 struct prom_args args;
250 args.service = ADDR(service);
254 va_start(list, rets);
255 for (i = 0; i < nargs; i++)
256 args.args[i] = va_arg(list, prom_arg_t);
259 for (i = 0; i < nret; i++)
260 args.args[nargs+i] = 0;
262 if (enter_prom(&args, RELOC(prom_entry)) < 0)
266 for (i = 1; i < nret; ++i)
267 rets[i-1] = args.args[nargs+i];
269 return (nret > 0) ? args.args[nargs] : 0;
273 static void __init prom_print(const char *msg)
276 struct prom_t *_prom = &RELOC(prom);
278 if (_prom->stdout == 0)
281 for (p = msg; *p != 0; p = q) {
282 for (q = p; *q != 0 && *q != '\n'; ++q)
285 call_prom("write", 3, 1, _prom->stdout, p, q - p);
289 call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
294 static void __init prom_print_hex(unsigned long val)
296 int i, nibbles = sizeof(val)*2;
297 char buf[sizeof(val)*2+1];
298 struct prom_t *_prom = &RELOC(prom);
300 for (i = nibbles-1; i >= 0; i--) {
301 buf[i] = (val & 0xf) + '0';
303 buf[i] += ('a'-'0'-10);
307 call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
310 /* max number of decimal digits in an unsigned long */
312 static void __init prom_print_dec(unsigned long val)
315 char buf[UL_DIGITS+1];
316 struct prom_t *_prom = &RELOC(prom);
318 for (i = UL_DIGITS-1; i >= 0; i--) {
319 buf[i] = (val % 10) + '0';
324 /* shift stuff down */
325 size = UL_DIGITS - i;
326 call_prom("write", 3, 1, _prom->stdout, buf+i, size);
329 static void __init prom_printf(const char *format, ...)
331 const char *p, *q, *s;
335 struct prom_t *_prom = &RELOC(prom);
337 va_start(args, format);
339 format = PTRRELOC(format);
341 for (p = format; *p != 0; p = q) {
342 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
345 call_prom("write", 3, 1, _prom->stdout, p, q - p);
350 call_prom("write", 3, 1, _prom->stdout,
360 s = va_arg(args, const char *);
365 v = va_arg(args, unsigned long);
370 vs = va_arg(args, int);
372 prom_print(RELOC("-"));
381 else if (*q == 'x') {
383 v = va_arg(args, unsigned long);
385 } else if (*q == 'u') { /* '%lu' */
387 v = va_arg(args, unsigned long);
389 } else if (*q == 'd') { /* %ld */
391 vs = va_arg(args, long);
393 prom_print(RELOC("-"));
404 static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
407 struct prom_t *_prom = &RELOC(prom);
409 if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
411 * Old OF requires we claim physical and virtual separately
412 * and then map explicitly (assuming virtual mode)
417 ret = call_prom_ret("call-method", 5, 2, &result,
418 ADDR("claim"), _prom->memory,
420 if (ret != 0 || result == -1)
422 ret = call_prom_ret("call-method", 5, 2, &result,
423 ADDR("claim"), _prom->mmumap,
426 call_prom("call-method", 4, 1, ADDR("release"),
427 _prom->memory, size, virt);
430 /* the 0x12 is M (coherence) + PP == read/write */
431 call_prom("call-method", 6, 1,
432 ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
435 return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
439 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
442 reason = PTRRELOC(reason);
445 /* Do not call exit because it clears the screen on pmac
446 * it also causes some sort of double-fault on early pmacs */
447 if (RELOC(of_platform) == PLATFORM_POWERMAC)
450 /* ToDo: should put up an SRC here on pSeries */
451 call_prom("exit", 0, 0);
453 for (;;) /* should never get here */
458 static int __init prom_next_node(phandle *nodep)
462 if ((node = *nodep) != 0
463 && (*nodep = call_prom("child", 1, 1, node)) != 0)
465 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
468 if ((node = call_prom("parent", 1, 1, node)) == 0)
470 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
475 static int inline prom_getprop(phandle node, const char *pname,
476 void *value, size_t valuelen)
478 return call_prom("getprop", 4, 1, node, ADDR(pname),
479 (u32)(unsigned long) value, (u32) valuelen);
482 static int inline prom_getproplen(phandle node, const char *pname)
484 return call_prom("getproplen", 2, 1, node, ADDR(pname));
487 static void add_string(char **str, const char *q)
497 static char *tohex(unsigned int x)
499 static char digits[] = "0123456789abcdef";
500 static char result[9];
507 result[i] = digits[x & 0xf];
509 } while (x != 0 && i > 0);
513 static int __init prom_setprop(phandle node, const char *nodename,
514 const char *pname, void *value, size_t valuelen)
518 if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
519 return call_prom("setprop", 4, 1, node, ADDR(pname),
520 (u32)(unsigned long) value, (u32) valuelen);
522 /* gah... setprop doesn't work on longtrail, have to use interpret */
524 add_string(&p, "dev");
525 add_string(&p, nodename);
526 add_string(&p, tohex((u32)(unsigned long) value));
527 add_string(&p, tohex(valuelen));
528 add_string(&p, tohex(ADDR(pname)));
529 add_string(&p, tohex(strlen(RELOC(pname))));
530 add_string(&p, "property");
532 return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
535 /* We can't use the standard versions because of RELOC headaches. */
536 #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
537 || ('a' <= (c) && (c) <= 'f') \
538 || ('A' <= (c) && (c) <= 'F'))
540 #define isdigit(c) ('0' <= (c) && (c) <= '9')
541 #define islower(c) ('a' <= (c) && (c) <= 'z')
542 #define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c))
544 unsigned long prom_strtoul(const char *cp, const char **endp)
546 unsigned long result = 0, base = 10, value;
551 if (toupper(*cp) == 'X') {
557 while (isxdigit(*cp) &&
558 (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
559 result = result * base + value;
569 unsigned long prom_memparse(const char *ptr, const char **retptr)
571 unsigned long ret = prom_strtoul(ptr, retptr);
575 * We can't use a switch here because GCC *may* generate a
576 * jump table which won't work, because we're not running at
577 * the address we're linked at.
579 if ('G' == **retptr || 'g' == **retptr)
582 if ('M' == **retptr || 'm' == **retptr)
585 if ('K' == **retptr || 'k' == **retptr)
597 * Early parsing of the command line passed to the kernel, used for
598 * "mem=x" and the options that affect the iommu
600 static void __init early_cmdline_parse(void)
602 struct prom_t *_prom = &RELOC(prom);
608 RELOC(prom_cmd_line[0]) = 0;
609 p = RELOC(prom_cmd_line);
610 if ((long)_prom->chosen > 0)
611 l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
612 #ifdef CONFIG_CMDLINE
613 if (l <= 0 || p[0] == '\0') /* dbl check */
614 strlcpy(RELOC(prom_cmd_line),
615 RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
616 #endif /* CONFIG_CMDLINE */
617 prom_printf("command line: %s\n", RELOC(prom_cmd_line));
620 opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
622 prom_printf("iommu opt is: %s\n", opt);
624 while (*opt && *opt == ' ')
626 if (!strncmp(opt, RELOC("off"), 3))
627 RELOC(prom_iommu_off) = 1;
628 else if (!strncmp(opt, RELOC("force"), 5))
629 RELOC(prom_iommu_force_on) = 1;
632 opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
635 RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
637 /* Align to 16 MB == size of ppc64 large page */
638 RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
643 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
645 * There are two methods for telling firmware what our capabilities are.
646 * Newer machines have an "ibm,client-architecture-support" method on the
647 * root node. For older machines, we have to call the "process-elf-header"
648 * method in the /packages/elf-loader node, passing it a fake 32-bit
649 * ELF header containing a couple of PT_NOTE sections that contain
650 * structures that contain various information.
654 * New method - extensible architecture description vector.
656 * Because the description vector contains a mix of byte and word
657 * values, we declare it as an unsigned char array, and use this
658 * macro to put word values in.
660 #define W(x) ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
661 ((x) >> 8) & 0xff, (x) & 0xff
663 /* Option vector bits - generic bits in byte 1 */
664 #define OV_IGNORE 0x80 /* ignore this vector */
665 #define OV_CESSATION_POLICY 0x40 /* halt if unsupported option present*/
667 /* Option vector 1: processor architectures supported */
668 #define OV1_PPC_2_00 0x80 /* set if we support PowerPC 2.00 */
669 #define OV1_PPC_2_01 0x40 /* set if we support PowerPC 2.01 */
670 #define OV1_PPC_2_02 0x20 /* set if we support PowerPC 2.02 */
671 #define OV1_PPC_2_03 0x10 /* set if we support PowerPC 2.03 */
672 #define OV1_PPC_2_04 0x08 /* set if we support PowerPC 2.04 */
673 #define OV1_PPC_2_05 0x04 /* set if we support PowerPC 2.05 */
674 #define OV1_PPC_2_06 0x02 /* set if we support PowerPC 2.06 */
676 /* Option vector 2: Open Firmware options supported */
677 #define OV2_REAL_MODE 0x20 /* set if we want OF in real mode */
679 /* Option vector 3: processor options supported */
680 #define OV3_FP 0x80 /* floating point */
681 #define OV3_VMX 0x40 /* VMX/Altivec */
682 #define OV3_DFP 0x20 /* decimal FP */
684 /* Option vector 5: PAPR/OF options supported */
685 #define OV5_LPAR 0x80 /* logical partitioning supported */
686 #define OV5_SPLPAR 0x40 /* shared-processor LPAR supported */
687 /* ibm,dynamic-reconfiguration-memory property supported */
688 #define OV5_DRCONF_MEMORY 0x20
689 #define OV5_LARGE_PAGES 0x10 /* large pages supported */
690 #define OV5_DONATE_DEDICATE_CPU 0x02 /* donate dedicated CPU support */
691 /* PCIe/MSI support. Without MSI full PCIe is not supported */
692 #ifdef CONFIG_PCI_MSI
693 #define OV5_MSI 0x01 /* PCIe/MSI support */
696 #endif /* CONFIG_PCI_MSI */
697 #ifdef CONFIG_PPC_SMLPAR
698 #define OV5_CMO 0x80 /* Cooperative Memory Overcommitment */
699 #define OV5_XCMO 0x40 /* Page Coalescing */
702 #define OV5_XCMO 0x00
704 #define OV5_TYPE1_AFFINITY 0x80 /* Type 1 NUMA affinity */
706 /* Option Vector 6: IBM PAPR hints */
707 #define OV6_LINUX 0x02 /* Linux is our OS */
710 * The architecture vector has an array of PVR mask/value pairs,
711 * followed by # option vectors - 1, followed by the option vectors.
713 static unsigned char ibm_architecture_vec[] = {
714 W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */
715 W(0xffff0000), W(0x003e0000), /* POWER6 */
716 W(0xffff0000), W(0x003f0000), /* POWER7 */
717 W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */
718 W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */
719 W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */
720 6 - 1, /* 6 option vectors */
722 /* option vector 1: processor architectures supported */
724 0, /* don't ignore, don't halt */
725 OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
726 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
728 /* option vector 2: Open Firmware options supported */
732 W(0xffffffff), /* real_base */
733 W(0xffffffff), /* real_size */
734 W(0xffffffff), /* virt_base */
735 W(0xffffffff), /* virt_size */
736 W(0xffffffff), /* load_base */
737 W(256), /* 256MB min RMA */
738 W(0xffffffff), /* full client load */
739 0, /* min RMA percentage of total RAM */
740 48, /* max log_2(hash table size) */
742 /* option vector 3: processor options supported */
744 0, /* don't ignore, don't halt */
745 OV3_FP | OV3_VMX | OV3_DFP,
747 /* option vector 4: IBM PAPR implementation */
751 /* option vector 5: PAPR/OF options */
753 0, /* don't ignore, don't halt */
754 OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
755 OV5_DONATE_DEDICATE_CPU | OV5_MSI,
762 /* WARNING: The offset of the "number of cores" field below
763 * must match by the macro below. Update the definition if
764 * the structure layout changes.
766 #define IBM_ARCH_VEC_NRCORES_OFFSET 100
767 W(NR_CPUS), /* number of cores supported */
769 /* option vector 6: IBM PAPR hints */
777 /* Old method - ELF header with PT_NOTE sections */
778 static struct fake_elf {
785 char name[8]; /* "PowerPC" */
799 char name[24]; /* "IBM,RPA-Client-Config" */
813 .e_ident = { 0x7f, 'E', 'L', 'F',
814 ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
815 .e_type = ET_EXEC, /* yeah right */
817 .e_version = EV_CURRENT,
818 .e_phoff = offsetof(struct fake_elf, phdr),
819 .e_phentsize = sizeof(Elf32_Phdr),
825 .p_offset = offsetof(struct fake_elf, chrpnote),
826 .p_filesz = sizeof(struct chrpnote)
829 .p_offset = offsetof(struct fake_elf, rpanote),
830 .p_filesz = sizeof(struct rpanote)
834 .namesz = sizeof("PowerPC"),
835 .descsz = sizeof(struct chrpdesc),
839 .real_mode = ~0U, /* ~0 means "don't care" */
848 .namesz = sizeof("IBM,RPA-Client-Config"),
849 .descsz = sizeof(struct rpadesc),
851 .name = "IBM,RPA-Client-Config",
854 .min_rmo_size = 64, /* in megabytes */
855 .min_rmo_percent = 0,
856 .max_pft_size = 48, /* 2^48 bytes max PFT size */
864 static int __init prom_count_smt_threads(void)
870 /* Pick up th first CPU node we can find */
871 for (node = 0; prom_next_node(&node); ) {
873 prom_getprop(node, "device_type", type, sizeof(type));
875 if (strcmp(type, RELOC("cpu")))
878 * There is an entry for each smt thread, each entry being
879 * 4 bytes long. All cpus should have the same number of
880 * smt threads, so return after finding the first.
882 plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
883 if (plen == PROM_ERROR)
886 prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
889 if (plen < 1 || plen > 64) {
890 prom_printf("Threads per core %lu out of bounds, assuming 1\n",
891 (unsigned long)plen);
896 prom_debug("No threads found, assuming 1 per core\n");
903 static void __init prom_send_capabilities(void)
905 ihandle elfloader, root;
909 root = call_prom("open", 1, 1, ADDR("/"));
911 /* We need to tell the FW about the number of cores we support.
913 * To do that, we count the number of threads on the first core
914 * (we assume this is the same for all cores) and use it to
917 cores = (u32 *)PTRRELOC(&ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET]);
918 if (*cores != NR_CPUS) {
919 prom_printf("WARNING ! "
920 "ibm_architecture_vec structure inconsistent: %lu!\n",
923 *cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
924 prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
928 /* try calling the ibm,client-architecture-support method */
929 prom_printf("Calling ibm,client-architecture-support...");
930 if (call_prom_ret("call-method", 3, 2, &ret,
931 ADDR("ibm,client-architecture-support"),
933 ADDR(ibm_architecture_vec)) == 0) {
934 /* the call exists... */
936 prom_printf("\nWARNING: ibm,client-architecture"
937 "-support call FAILED!\n");
938 call_prom("close", 1, 0, root);
939 prom_printf(" done\n");
942 call_prom("close", 1, 0, root);
943 prom_printf(" not implemented\n");
946 /* no ibm,client-architecture-support call, try the old way */
947 elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
948 if (elfloader == 0) {
949 prom_printf("couldn't open /packages/elf-loader\n");
952 call_prom("call-method", 3, 1, ADDR("process-elf-header"),
953 elfloader, ADDR(&fake_elf));
954 call_prom("close", 1, 0, elfloader);
959 * Memory allocation strategy... our layout is normally:
961 * at 14Mb or more we have vmlinux, then a gap and initrd. In some
962 * rare cases, initrd might end up being before the kernel though.
963 * We assume this won't override the final kernel at 0, we have no
964 * provision to handle that in this version, but it should hopefully
967 * alloc_top is set to the top of RMO, eventually shrink down if the
970 * alloc_bottom is set to the top of kernel/initrd
972 * from there, allocations are done this way : rtas is allocated
973 * topmost, and the device-tree is allocated from the bottom. We try
974 * to grow the device-tree allocation as we progress. If we can't,
975 * then we fail, we don't currently have a facility to restart
976 * elsewhere, but that shouldn't be necessary.
978 * Note that calls to reserve_mem have to be done explicitly, memory
979 * allocated with either alloc_up or alloc_down isn't automatically
985 * Allocates memory in the RMO upward from the kernel/initrd
987 * When align is 0, this is a special case, it means to allocate in place
988 * at the current location of alloc_bottom or fail (that is basically
989 * extending the previous allocation). Used for the device-tree flattening
991 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
993 unsigned long base = RELOC(alloc_bottom);
994 unsigned long addr = 0;
997 base = _ALIGN_UP(base, align);
998 prom_debug("alloc_up(%x, %x)\n", size, align);
999 if (RELOC(ram_top) == 0)
1000 prom_panic("alloc_up() called with mem not initialized\n");
1003 base = _ALIGN_UP(RELOC(alloc_bottom), align);
1005 base = RELOC(alloc_bottom);
1007 for(; (base + size) <= RELOC(alloc_top);
1008 base = _ALIGN_UP(base + 0x100000, align)) {
1009 prom_debug(" trying: 0x%x\n\r", base);
1010 addr = (unsigned long)prom_claim(base, size, 0);
1011 if (addr != PROM_ERROR && addr != 0)
1019 RELOC(alloc_bottom) = addr + size;
1021 prom_debug(" -> %x\n", addr);
1022 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1023 prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
1024 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1025 prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
1026 prom_debug(" ram_top : %x\n", RELOC(ram_top));
1032 * Allocates memory downward, either from top of RMO, or if highmem
1033 * is set, from the top of RAM. Note that this one doesn't handle
1034 * failures. It does claim memory if highmem is not set.
1036 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
1039 unsigned long base, addr = 0;
1041 prom_debug("alloc_down(%x, %x, %s)\n", size, align,
1042 highmem ? RELOC("(high)") : RELOC("(low)"));
1043 if (RELOC(ram_top) == 0)
1044 prom_panic("alloc_down() called with mem not initialized\n");
1047 /* Carve out storage for the TCE table. */
1048 addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
1049 if (addr <= RELOC(alloc_bottom))
1051 /* Will we bump into the RMO ? If yes, check out that we
1052 * didn't overlap existing allocations there, if we did,
1053 * we are dead, we must be the first in town !
1055 if (addr < RELOC(rmo_top)) {
1056 /* Good, we are first */
1057 if (RELOC(alloc_top) == RELOC(rmo_top))
1058 RELOC(alloc_top) = RELOC(rmo_top) = addr;
1062 RELOC(alloc_top_high) = addr;
1066 base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
1067 for (; base > RELOC(alloc_bottom);
1068 base = _ALIGN_DOWN(base - 0x100000, align)) {
1069 prom_debug(" trying: 0x%x\n\r", base);
1070 addr = (unsigned long)prom_claim(base, size, 0);
1071 if (addr != PROM_ERROR && addr != 0)
1077 RELOC(alloc_top) = addr;
1080 prom_debug(" -> %x\n", addr);
1081 prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1082 prom_debug(" alloc_top : %x\n", RELOC(alloc_top));
1083 prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1084 prom_debug(" rmo_top : %x\n", RELOC(rmo_top));
1085 prom_debug(" ram_top : %x\n", RELOC(ram_top));
1091 * Parse a "reg" cell
1093 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
1096 unsigned long r = 0;
1098 /* Ignore more than 2 cells */
1099 while (s > sizeof(unsigned long) / 4) {
1115 * Very dumb function for adding to the memory reserve list, but
1116 * we don't need anything smarter at this point
1118 * XXX Eventually check for collisions. They should NEVER happen.
1119 * If problems seem to show up, it would be a good start to track
1122 static void __init reserve_mem(u64 base, u64 size)
1124 u64 top = base + size;
1125 unsigned long cnt = RELOC(mem_reserve_cnt);
1130 /* We need to always keep one empty entry so that we
1131 * have our terminator with "size" set to 0 since we are
1132 * dumb and just copy this entire array to the boot params
1134 base = _ALIGN_DOWN(base, PAGE_SIZE);
1135 top = _ALIGN_UP(top, PAGE_SIZE);
1138 if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
1139 prom_panic("Memory reserve map exhausted !\n");
1140 RELOC(mem_reserve_map)[cnt].base = base;
1141 RELOC(mem_reserve_map)[cnt].size = size;
1142 RELOC(mem_reserve_cnt) = cnt + 1;
1146 * Initialize memory allocation mechanism, parse "memory" nodes and
1147 * obtain that way the top of memory and RMO to setup out local allocator
1149 static void __init prom_init_mem(void)
1152 char *path, type[64];
1155 struct prom_t *_prom = &RELOC(prom);
1159 * We iterate the memory nodes to find
1160 * 1) top of RMO (first node)
1164 prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
1166 prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
1167 prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
1168 prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
1170 prom_debug("scanning memory:\n");
1171 path = RELOC(prom_scratch);
1173 for (node = 0; prom_next_node(&node); ) {
1175 prom_getprop(node, "device_type", type, sizeof(type));
1179 * CHRP Longtrail machines have no device_type
1180 * on the memory node, so check the name instead...
1182 prom_getprop(node, "name", type, sizeof(type));
1184 if (strcmp(type, RELOC("memory")))
1187 plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
1188 if (plen > sizeof(regbuf)) {
1189 prom_printf("memory node too large for buffer !\n");
1190 plen = sizeof(regbuf);
1193 endp = p + (plen / sizeof(cell_t));
1196 memset(path, 0, PROM_SCRATCH_SIZE);
1197 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1198 prom_debug(" node %s :\n", path);
1199 #endif /* DEBUG_PROM */
1201 while ((endp - p) >= (rac + rsc)) {
1202 unsigned long base, size;
1204 base = prom_next_cell(rac, &p);
1205 size = prom_next_cell(rsc, &p);
1209 prom_debug(" %x %x\n", base, size);
1210 if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
1211 RELOC(rmo_top) = size;
1212 if ((base + size) > RELOC(ram_top))
1213 RELOC(ram_top) = base + size;
1217 RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
1220 * If prom_memory_limit is set we reduce the upper limits *except* for
1221 * alloc_top_high. This must be the real top of RAM so we can put
1225 RELOC(alloc_top_high) = RELOC(ram_top);
1227 if (RELOC(prom_memory_limit)) {
1228 if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
1229 prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
1230 RELOC(prom_memory_limit));
1231 RELOC(prom_memory_limit) = 0;
1232 } else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
1233 prom_printf("Ignoring mem=%x >= ram_top.\n",
1234 RELOC(prom_memory_limit));
1235 RELOC(prom_memory_limit) = 0;
1237 RELOC(ram_top) = RELOC(prom_memory_limit);
1238 RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
1243 * Setup our top alloc point, that is top of RMO or top of
1244 * segment 0 when running non-LPAR.
1245 * Some RS64 machines have buggy firmware where claims up at
1246 * 1GB fail. Cap at 768MB as a workaround.
1247 * Since 768MB is plenty of room, and we need to cap to something
1248 * reasonable on 32-bit, cap at 768MB on all machines.
1250 if (!RELOC(rmo_top))
1251 RELOC(rmo_top) = RELOC(ram_top);
1252 RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
1253 RELOC(alloc_top) = RELOC(rmo_top);
1254 RELOC(alloc_top_high) = RELOC(ram_top);
1257 * Check if we have an initrd after the kernel but still inside
1258 * the RMO. If we do move our bottom point to after it.
1260 if (RELOC(prom_initrd_start) &&
1261 RELOC(prom_initrd_start) < RELOC(rmo_top) &&
1262 RELOC(prom_initrd_end) > RELOC(alloc_bottom))
1263 RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
1265 prom_printf("memory layout at init:\n");
1266 prom_printf(" memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
1267 prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom));
1268 prom_printf(" alloc_top : %x\n", RELOC(alloc_top));
1269 prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high));
1270 prom_printf(" rmo_top : %x\n", RELOC(rmo_top));
1271 prom_printf(" ram_top : %x\n", RELOC(ram_top));
1274 static void __init prom_close_stdin(void)
1276 struct prom_t *_prom = &RELOC(prom);
1279 if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1280 call_prom("close", 1, 0, val);
1283 #ifdef CONFIG_PPC_POWERNV
1285 static u64 __initdata prom_opal_size;
1286 static u64 __initdata prom_opal_align;
1287 static int __initdata prom_rtas_start_cpu;
1288 static u64 __initdata prom_rtas_data;
1289 static u64 __initdata prom_rtas_entry;
1291 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
1292 static u64 __initdata prom_opal_base;
1293 static u64 __initdata prom_opal_entry;
1296 /* XXX Don't change this structure without updating opal-takeover.S */
1297 static struct opal_secondary_data {
1300 struct opal_takeover_args args; /* 16 */
1301 } opal_secondary_data;
1303 extern char opal_secondary_entry;
1305 static void prom_query_opal(void)
1309 /* We must not query for OPAL presence on a machine that
1310 * supports TNK takeover (970 blades), as this uses the same
1311 * h-call with different arguments and will crash
1313 if (PHANDLE_VALID(call_prom("finddevice", 1, 1,
1314 ADDR("/tnk-memory-map")))) {
1315 prom_printf("TNK takeover detected, skipping OPAL check\n");
1319 prom_printf("Querying for OPAL presence... ");
1320 rc = opal_query_takeover(&RELOC(prom_opal_size),
1321 &RELOC(prom_opal_align));
1322 prom_debug("(rc = %ld) ", rc);
1324 prom_printf("not there.\n");
1327 RELOC(of_platform) = PLATFORM_OPAL;
1328 prom_printf(" there !\n");
1329 prom_debug(" opal_size = 0x%lx\n", RELOC(prom_opal_size));
1330 prom_debug(" opal_align = 0x%lx\n", RELOC(prom_opal_align));
1331 if (RELOC(prom_opal_align) < 0x10000)
1332 RELOC(prom_opal_align) = 0x10000;
1335 static int prom_rtas_call(int token, int nargs, int nret, int *outputs, ...)
1337 struct rtas_args rtas_args;
1341 rtas_args.token = token;
1342 rtas_args.nargs = nargs;
1343 rtas_args.nret = nret;
1344 rtas_args.rets = (rtas_arg_t *)&(rtas_args.args[nargs]);
1345 va_start(list, outputs);
1346 for (i = 0; i < nargs; ++i)
1347 rtas_args.args[i] = va_arg(list, rtas_arg_t);
1350 for (i = 0; i < nret; ++i)
1351 rtas_args.rets[i] = 0;
1353 opal_enter_rtas(&rtas_args, RELOC(prom_rtas_data),
1354 RELOC(prom_rtas_entry));
1356 if (nret > 1 && outputs != NULL)
1357 for (i = 0; i < nret-1; ++i)
1358 outputs[i] = rtas_args.rets[i+1];
1359 return (nret > 0)? rtas_args.rets[0]: 0;
1362 static void __init prom_opal_hold_cpus(void)
1364 int i, cnt, cpu, rc;
1369 struct prom_t *_prom = &RELOC(prom);
1370 void *entry = (unsigned long *)&RELOC(opal_secondary_entry);
1371 struct opal_secondary_data *data = &RELOC(opal_secondary_data);
1373 prom_debug("prom_opal_hold_cpus: start...\n");
1374 prom_debug(" - entry = 0x%x\n", entry);
1375 prom_debug(" - data = 0x%x\n", data);
1381 for (node = 0; prom_next_node(&node); ) {
1383 prom_getprop(node, "device_type", type, sizeof(type));
1384 if (strcmp(type, RELOC("cpu")) != 0)
1387 /* Skip non-configured cpus. */
1388 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1389 if (strcmp(type, RELOC("okay")) != 0)
1392 cnt = prom_getprop(node, "ibm,ppc-interrupt-server#s", servers,
1394 if (cnt == PROM_ERROR)
1397 for (i = 0; i < cnt; i++) {
1399 prom_debug("CPU %d ... ", cpu);
1400 if (cpu == _prom->cpu) {
1401 prom_debug("booted !\n");
1404 prom_debug("starting ... ");
1406 /* Init the acknowledge var which will be reset by
1407 * the secondary cpu when it awakens from its OF
1411 rc = prom_rtas_call(RELOC(prom_rtas_start_cpu), 3, 1,
1412 NULL, cpu, entry, data);
1413 prom_debug("rtas rc=%d ...", rc);
1415 for (j = 0; j < 100000000 && data->ack == -1; j++) {
1420 if (data->ack != -1)
1421 prom_debug("done, PIR=0x%x\n", data->ack);
1423 prom_debug("timeout !\n");
1426 prom_debug("prom_opal_hold_cpus: end...\n");
1429 static void prom_opal_takeover(void)
1431 struct opal_secondary_data *data = &RELOC(opal_secondary_data);
1432 struct opal_takeover_args *args = &data->args;
1433 u64 align = RELOC(prom_opal_align);
1434 u64 top_addr, opal_addr;
1436 args->k_image = (u64)RELOC(_stext);
1437 args->k_size = _end - _stext;
1439 args->k_entry2 = 0x60;
1441 top_addr = _ALIGN_UP(args->k_size, align);
1443 if (RELOC(prom_initrd_start) != 0) {
1444 args->rd_image = RELOC(prom_initrd_start);
1445 args->rd_size = RELOC(prom_initrd_end) - args->rd_image;
1446 args->rd_loc = top_addr;
1447 top_addr = _ALIGN_UP(args->rd_loc + args->rd_size, align);
1450 /* Pickup an address for the HAL. We want to go really high
1451 * up to avoid problem with future kexecs. On the other hand
1452 * we don't want to be all over the TCEs on P5IOC2 machines
1453 * which are going to be up there too. We assume the machine
1454 * has plenty of memory, and we ask for the HAL for now to
1455 * be just below the 1G point, or above the initrd
1457 opal_addr = _ALIGN_DOWN(0x40000000 - RELOC(prom_opal_size), align);
1458 if (opal_addr < top_addr)
1459 opal_addr = top_addr;
1460 args->hal_addr = opal_addr;
1462 /* Copy the command line to the kernel image */
1463 strlcpy(RELOC(boot_command_line), RELOC(prom_cmd_line),
1466 prom_debug(" k_image = 0x%lx\n", args->k_image);
1467 prom_debug(" k_size = 0x%lx\n", args->k_size);
1468 prom_debug(" k_entry = 0x%lx\n", args->k_entry);
1469 prom_debug(" k_entry2 = 0x%lx\n", args->k_entry2);
1470 prom_debug(" hal_addr = 0x%lx\n", args->hal_addr);
1471 prom_debug(" rd_image = 0x%lx\n", args->rd_image);
1472 prom_debug(" rd_size = 0x%lx\n", args->rd_size);
1473 prom_debug(" rd_loc = 0x%lx\n", args->rd_loc);
1474 prom_printf("Performing OPAL takeover,this can take a few minutes..\n");
1479 opal_do_takeover(args);
1483 * Allocate room for and instantiate OPAL
1485 static void __init prom_instantiate_opal(void)
1490 u64 size = 0, align = 0x10000;
1493 prom_debug("prom_instantiate_opal: start...\n");
1495 opal_node = call_prom("finddevice", 1, 1, ADDR("/ibm,opal"));
1496 prom_debug("opal_node: %x\n", opal_node);
1497 if (!PHANDLE_VALID(opal_node))
1500 prom_getprop(opal_node, "opal-runtime-size", &size, sizeof(size));
1503 prom_getprop(opal_node, "opal-runtime-alignment", &align,
1506 base = alloc_down(size, align, 0);
1508 prom_printf("OPAL allocation failed !\n");
1512 opal_inst = call_prom("open", 1, 1, ADDR("/ibm,opal"));
1513 if (!IHANDLE_VALID(opal_inst)) {
1514 prom_printf("opening opal package failed (%x)\n", opal_inst);
1518 prom_printf("instantiating opal at 0x%x...", base);
1520 if (call_prom_ret("call-method", 4, 3, rets,
1521 ADDR("load-opal-runtime"),
1523 base >> 32, base & 0xffffffff) != 0
1524 || (rets[0] == 0 && rets[1] == 0)) {
1525 prom_printf(" failed\n");
1528 entry = (((u64)rets[0]) << 32) | rets[1];
1530 prom_printf(" done\n");
1532 reserve_mem(base, size);
1534 prom_debug("opal base = 0x%x\n", base);
1535 prom_debug("opal align = 0x%x\n", align);
1536 prom_debug("opal entry = 0x%x\n", entry);
1537 prom_debug("opal size = 0x%x\n", (long)size);
1539 prom_setprop(opal_node, "/ibm,opal", "opal-base-address",
1540 &base, sizeof(base));
1541 prom_setprop(opal_node, "/ibm,opal", "opal-entry-address",
1542 &entry, sizeof(entry));
1544 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
1545 RELOC(prom_opal_base) = base;
1546 RELOC(prom_opal_entry) = entry;
1548 prom_debug("prom_instantiate_opal: end...\n");
1551 #endif /* CONFIG_PPC_POWERNV */
1554 * Allocate room for and instantiate RTAS
1556 static void __init prom_instantiate_rtas(void)
1560 u32 base, entry = 0;
1563 prom_debug("prom_instantiate_rtas: start...\n");
1565 rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1566 prom_debug("rtas_node: %x\n", rtas_node);
1567 if (!PHANDLE_VALID(rtas_node))
1570 prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1574 base = alloc_down(size, PAGE_SIZE, 0);
1576 prom_panic("Could not allocate memory for RTAS\n");
1578 rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1579 if (!IHANDLE_VALID(rtas_inst)) {
1580 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
1584 prom_printf("instantiating rtas at 0x%x...", base);
1586 if (call_prom_ret("call-method", 3, 2, &entry,
1587 ADDR("instantiate-rtas"),
1588 rtas_inst, base) != 0
1590 prom_printf(" failed\n");
1593 prom_printf(" done\n");
1595 reserve_mem(base, size);
1597 prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1598 &base, sizeof(base));
1599 prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1600 &entry, sizeof(entry));
1602 #ifdef CONFIG_PPC_POWERNV
1603 /* PowerVN takeover hack */
1604 RELOC(prom_rtas_data) = base;
1605 RELOC(prom_rtas_entry) = entry;
1606 prom_getprop(rtas_node, "start-cpu", &RELOC(prom_rtas_start_cpu), 4);
1608 prom_debug("rtas base = 0x%x\n", base);
1609 prom_debug("rtas entry = 0x%x\n", entry);
1610 prom_debug("rtas size = 0x%x\n", (long)size);
1612 prom_debug("prom_instantiate_rtas: end...\n");
1617 * Allocate room for and initialize TCE tables
1619 static void __init prom_initialize_tce_table(void)
1623 char compatible[64], type[64], model[64];
1624 char *path = RELOC(prom_scratch);
1626 u32 minalign, minsize;
1627 u64 tce_entry, *tce_entryp;
1628 u64 local_alloc_top, local_alloc_bottom;
1631 if (RELOC(prom_iommu_off))
1634 prom_debug("starting prom_initialize_tce_table\n");
1636 /* Cache current top of allocs so we reserve a single block */
1637 local_alloc_top = RELOC(alloc_top_high);
1638 local_alloc_bottom = local_alloc_top;
1640 /* Search all nodes looking for PHBs. */
1641 for (node = 0; prom_next_node(&node); ) {
1645 prom_getprop(node, "compatible",
1646 compatible, sizeof(compatible));
1647 prom_getprop(node, "device_type", type, sizeof(type));
1648 prom_getprop(node, "model", model, sizeof(model));
1650 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1653 /* Keep the old logic intact to avoid regression. */
1654 if (compatible[0] != 0) {
1655 if ((strstr(compatible, RELOC("python")) == NULL) &&
1656 (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1657 (strstr(compatible, RELOC("Winnipeg")) == NULL))
1659 } else if (model[0] != 0) {
1660 if ((strstr(model, RELOC("ython")) == NULL) &&
1661 (strstr(model, RELOC("peedwagon")) == NULL) &&
1662 (strstr(model, RELOC("innipeg")) == NULL))
1666 if (prom_getprop(node, "tce-table-minalign", &minalign,
1667 sizeof(minalign)) == PROM_ERROR)
1669 if (prom_getprop(node, "tce-table-minsize", &minsize,
1670 sizeof(minsize)) == PROM_ERROR)
1671 minsize = 4UL << 20;
1674 * Even though we read what OF wants, we just set the table
1675 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
1676 * By doing this, we avoid the pitfalls of trying to DMA to
1677 * MMIO space and the DMA alias hole.
1679 * On POWER4, firmware sets the TCE region by assuming
1680 * each TCE table is 8MB. Using this memory for anything
1681 * else will impact performance, so we always allocate 8MB.
1684 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1685 minsize = 8UL << 20;
1687 minsize = 4UL << 20;
1689 /* Align to the greater of the align or size */
1690 align = max(minalign, minsize);
1691 base = alloc_down(minsize, align, 1);
1693 prom_panic("ERROR, cannot find space for TCE table.\n");
1694 if (base < local_alloc_bottom)
1695 local_alloc_bottom = base;
1697 /* It seems OF doesn't null-terminate the path :-( */
1698 memset(path, 0, PROM_SCRATCH_SIZE);
1699 /* Call OF to setup the TCE hardware */
1700 if (call_prom("package-to-path", 3, 1, node,
1701 path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1702 prom_printf("package-to-path failed\n");
1705 /* Save away the TCE table attributes for later use. */
1706 prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1707 prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1709 prom_debug("TCE table: %s\n", path);
1710 prom_debug("\tnode = 0x%x\n", node);
1711 prom_debug("\tbase = 0x%x\n", base);
1712 prom_debug("\tsize = 0x%x\n", minsize);
1714 /* Initialize the table to have a one-to-one mapping
1715 * over the allocated size.
1717 tce_entryp = (u64 *)base;
1718 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1719 tce_entry = (i << PAGE_SHIFT);
1721 *tce_entryp = tce_entry;
1724 prom_printf("opening PHB %s", path);
1725 phb_node = call_prom("open", 1, 1, path);
1727 prom_printf("... failed\n");
1729 prom_printf("... done\n");
1731 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1732 phb_node, -1, minsize,
1733 (u32) base, (u32) (base >> 32));
1734 call_prom("close", 1, 0, phb_node);
1737 reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1739 /* These are only really needed if there is a memory limit in
1740 * effect, but we don't know so export them always. */
1741 RELOC(prom_tce_alloc_start) = local_alloc_bottom;
1742 RELOC(prom_tce_alloc_end) = local_alloc_top;
1744 /* Flag the first invalid entry */
1745 prom_debug("ending prom_initialize_tce_table\n");
1750 * With CHRP SMP we need to use the OF to start the other processors.
1751 * We can't wait until smp_boot_cpus (the OF is trashed by then)
1752 * so we have to put the processors into a holding pattern controlled
1753 * by the kernel (not OF) before we destroy the OF.
1755 * This uses a chunk of low memory, puts some holding pattern
1756 * code there and sends the other processors off to there until
1757 * smp_boot_cpus tells them to do something. The holding pattern
1758 * checks that address until its cpu # is there, when it is that
1759 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
1760 * of setting those values.
1762 * We also use physical address 0x4 here to tell when a cpu
1763 * is in its holding pattern code.
1768 * We want to reference the copy of __secondary_hold_* in the
1769 * 0 - 0x100 address range
1771 #define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
1773 static void __init prom_hold_cpus(void)
1779 struct prom_t *_prom = &RELOC(prom);
1780 unsigned long *spinloop
1781 = (void *) LOW_ADDR(__secondary_hold_spinloop);
1782 unsigned long *acknowledge
1783 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
1784 unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
1786 prom_debug("prom_hold_cpus: start...\n");
1787 prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop);
1788 prom_debug(" 1) *spinloop = 0x%x\n", *spinloop);
1789 prom_debug(" 1) acknowledge = 0x%x\n",
1790 (unsigned long)acknowledge);
1791 prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge);
1792 prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold);
1794 /* Set the common spinloop variable, so all of the secondary cpus
1795 * will block when they are awakened from their OF spinloop.
1796 * This must occur for both SMP and non SMP kernels, since OF will
1797 * be trashed when we move the kernel.
1802 for (node = 0; prom_next_node(&node); ) {
1804 prom_getprop(node, "device_type", type, sizeof(type));
1805 if (strcmp(type, RELOC("cpu")) != 0)
1808 /* Skip non-configured cpus. */
1809 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1810 if (strcmp(type, RELOC("okay")) != 0)
1814 prom_getprop(node, "reg", ®, sizeof(reg));
1816 prom_debug("cpu hw idx = %lu\n", reg);
1818 /* Init the acknowledge var which will be reset by
1819 * the secondary cpu when it awakens from its OF
1822 *acknowledge = (unsigned long)-1;
1824 if (reg != _prom->cpu) {
1825 /* Primary Thread of non-boot cpu or any thread */
1826 prom_printf("starting cpu hw idx %lu... ", reg);
1827 call_prom("start-cpu", 3, 0, node,
1828 secondary_hold, reg);
1830 for (i = 0; (i < 100000000) &&
1831 (*acknowledge == ((unsigned long)-1)); i++ )
1834 if (*acknowledge == reg)
1835 prom_printf("done\n");
1837 prom_printf("failed: %x\n", *acknowledge);
1841 prom_printf("boot cpu hw idx %lu\n", reg);
1842 #endif /* CONFIG_SMP */
1845 prom_debug("prom_hold_cpus: end...\n");
1849 static void __init prom_init_client_services(unsigned long pp)
1851 struct prom_t *_prom = &RELOC(prom);
1853 /* Get a handle to the prom entry point before anything else */
1854 RELOC(prom_entry) = pp;
1856 /* get a handle for the stdout device */
1857 _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1858 if (!PHANDLE_VALID(_prom->chosen))
1859 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1861 /* get device tree root */
1862 _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1863 if (!PHANDLE_VALID(_prom->root))
1864 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1871 * For really old powermacs, we need to map things we claim.
1872 * For that, we need the ihandle of the mmu.
1873 * Also, on the longtrail, we need to work around other bugs.
1875 static void __init prom_find_mmu(void)
1877 struct prom_t *_prom = &RELOC(prom);
1881 oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1882 if (!PHANDLE_VALID(oprom))
1884 if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1886 version[sizeof(version) - 1] = 0;
1887 /* XXX might need to add other versions here */
1888 if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1889 of_workarounds = OF_WA_CLAIM;
1890 else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1891 of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1892 call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1895 _prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
1896 prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1897 sizeof(_prom->mmumap));
1898 if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1899 of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
1902 #define prom_find_mmu()
1905 static void __init prom_init_stdout(void)
1907 struct prom_t *_prom = &RELOC(prom);
1908 char *path = RELOC(of_stdout_device);
1912 if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1913 prom_panic("cannot find stdout");
1915 _prom->stdout = val;
1917 /* Get the full OF pathname of the stdout device */
1918 memset(path, 0, 256);
1919 call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1920 val = call_prom("instance-to-package", 1, 1, _prom->stdout);
1921 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1923 prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
1924 prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1925 path, strlen(path) + 1);
1927 /* If it's a display, note it */
1928 memset(type, 0, sizeof(type));
1929 prom_getprop(val, "device_type", type, sizeof(type));
1930 if (strcmp(type, RELOC("display")) == 0)
1931 prom_setprop(val, path, "linux,boot-display", NULL, 0);
1934 static int __init prom_find_machine_type(void)
1936 struct prom_t *_prom = &RELOC(prom);
1944 /* Look for a PowerMac or a Cell */
1945 len = prom_getprop(_prom->root, "compatible",
1946 compat, sizeof(compat)-1);
1950 char *p = &compat[i];
1954 if (strstr(p, RELOC("Power Macintosh")) ||
1955 strstr(p, RELOC("MacRISC")))
1956 return PLATFORM_POWERMAC;
1958 /* We must make sure we don't detect the IBM Cell
1959 * blades as pSeries due to some firmware issues,
1962 if (strstr(p, RELOC("IBM,CBEA")) ||
1963 strstr(p, RELOC("IBM,CPBW-1.0")))
1964 return PLATFORM_GENERIC;
1965 #endif /* CONFIG_PPC64 */
1970 /* Try to detect OPAL */
1971 if (PHANDLE_VALID(call_prom("finddevice", 1, 1, ADDR("/ibm,opal"))))
1972 return PLATFORM_OPAL;
1974 /* Try to figure out if it's an IBM pSeries or any other
1975 * PAPR compliant platform. We assume it is if :
1976 * - /device_type is "chrp" (please, do NOT use that for future
1980 len = prom_getprop(_prom->root, "device_type",
1981 compat, sizeof(compat)-1);
1983 return PLATFORM_GENERIC;
1984 if (strcmp(compat, RELOC("chrp")))
1985 return PLATFORM_GENERIC;
1987 /* Default to pSeries. We need to know if we are running LPAR */
1988 rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1989 if (!PHANDLE_VALID(rtas))
1990 return PLATFORM_GENERIC;
1991 x = prom_getproplen(rtas, "ibm,hypertas-functions");
1992 if (x != PROM_ERROR) {
1993 prom_debug("Hypertas detected, assuming LPAR !\n");
1994 return PLATFORM_PSERIES_LPAR;
1996 return PLATFORM_PSERIES;
1998 return PLATFORM_GENERIC;
2002 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
2004 return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
2008 * If we have a display that we don't know how to drive,
2009 * we will want to try to execute OF's open method for it
2010 * later. However, OF will probably fall over if we do that
2011 * we've taken over the MMU.
2012 * So we check whether we will need to open the display,
2013 * and if so, open it now.
2015 static void __init prom_check_displays(void)
2017 char type[16], *path;
2022 static unsigned char default_colors[] = {
2040 const unsigned char *clut;
2042 prom_debug("Looking for displays\n");
2043 for (node = 0; prom_next_node(&node); ) {
2044 memset(type, 0, sizeof(type));
2045 prom_getprop(node, "device_type", type, sizeof(type));
2046 if (strcmp(type, RELOC("display")) != 0)
2049 /* It seems OF doesn't null-terminate the path :-( */
2050 path = RELOC(prom_scratch);
2051 memset(path, 0, PROM_SCRATCH_SIZE);
2054 * leave some room at the end of the path for appending extra
2057 if (call_prom("package-to-path", 3, 1, node, path,
2058 PROM_SCRATCH_SIZE-10) == PROM_ERROR)
2060 prom_printf("found display : %s, opening... ", path);
2062 ih = call_prom("open", 1, 1, path);
2064 prom_printf("failed\n");
2069 prom_printf("done\n");
2070 prom_setprop(node, path, "linux,opened", NULL, 0);
2072 /* Setup a usable color table when the appropriate
2073 * method is available. Should update this to set-colors */
2074 clut = RELOC(default_colors);
2075 for (i = 0; i < 16; i++, clut += 3)
2076 if (prom_set_color(ih, i, clut[0], clut[1],
2080 #ifdef CONFIG_LOGO_LINUX_CLUT224
2081 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
2082 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
2083 if (prom_set_color(ih, i + 32, clut[0], clut[1],
2086 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
2091 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
2092 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
2093 unsigned long needed, unsigned long align)
2097 *mem_start = _ALIGN(*mem_start, align);
2098 while ((*mem_start + needed) > *mem_end) {
2099 unsigned long room, chunk;
2101 prom_debug("Chunk exhausted, claiming more at %x...\n",
2102 RELOC(alloc_bottom));
2103 room = RELOC(alloc_top) - RELOC(alloc_bottom);
2104 if (room > DEVTREE_CHUNK_SIZE)
2105 room = DEVTREE_CHUNK_SIZE;
2106 if (room < PAGE_SIZE)
2107 prom_panic("No memory for flatten_device_tree "
2109 chunk = alloc_up(room, 0);
2111 prom_panic("No memory for flatten_device_tree "
2112 "(claim failed)\n");
2113 *mem_end = chunk + room;
2116 ret = (void *)*mem_start;
2117 *mem_start += needed;
2122 #define dt_push_token(token, mem_start, mem_end) \
2123 do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
2125 static unsigned long __init dt_find_string(char *str)
2129 s = os = (char *)RELOC(dt_string_start);
2131 while (s < (char *)RELOC(dt_string_end)) {
2132 if (strcmp(s, str) == 0)
2140 * The Open Firmware 1275 specification states properties must be 31 bytes or
2141 * less, however not all firmwares obey this. Make it 64 bytes to be safe.
2143 #define MAX_PROPERTY_NAME 64
2145 static void __init scan_dt_build_strings(phandle node,
2146 unsigned long *mem_start,
2147 unsigned long *mem_end)
2149 char *prev_name, *namep, *sstart;
2153 sstart = (char *)RELOC(dt_string_start);
2155 /* get and store all property names */
2156 prev_name = RELOC("");
2158 /* 64 is max len of name including nul. */
2159 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
2160 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
2161 /* No more nodes: unwind alloc */
2162 *mem_start = (unsigned long)namep;
2167 if (strcmp(namep, RELOC("name")) == 0) {
2168 *mem_start = (unsigned long)namep;
2169 prev_name = RELOC("name");
2172 /* get/create string entry */
2173 soff = dt_find_string(namep);
2175 *mem_start = (unsigned long)namep;
2176 namep = sstart + soff;
2178 /* Trim off some if we can */
2179 *mem_start = (unsigned long)namep + strlen(namep) + 1;
2180 RELOC(dt_string_end) = *mem_start;
2185 /* do all our children */
2186 child = call_prom("child", 1, 1, node);
2187 while (child != 0) {
2188 scan_dt_build_strings(child, mem_start, mem_end);
2189 child = call_prom("peer", 1, 1, child);
2193 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
2194 unsigned long *mem_end)
2197 char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
2199 unsigned char *valp;
2200 static char pname[MAX_PROPERTY_NAME];
2201 int l, room, has_phandle = 0;
2203 dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
2205 /* get the node's full name */
2206 namep = (char *)*mem_start;
2207 room = *mem_end - *mem_start;
2210 l = call_prom("package-to-path", 3, 1, node, namep, room);
2212 /* Didn't fit? Get more room. */
2214 if (l >= *mem_end - *mem_start)
2215 namep = make_room(mem_start, mem_end, l+1, 1);
2216 call_prom("package-to-path", 3, 1, node, namep, l);
2220 /* Fixup an Apple bug where they have bogus \0 chars in the
2221 * middle of the path in some properties, and extract
2222 * the unit name (everything after the last '/').
2224 for (lp = p = namep, ep = namep + l; p < ep; p++) {
2231 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
2234 /* get it again for debugging */
2235 path = RELOC(prom_scratch);
2236 memset(path, 0, PROM_SCRATCH_SIZE);
2237 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
2239 /* get and store all properties */
2240 prev_name = RELOC("");
2241 sstart = (char *)RELOC(dt_string_start);
2243 if (call_prom("nextprop", 3, 1, node, prev_name,
2248 if (strcmp(RELOC(pname), RELOC("name")) == 0) {
2249 prev_name = RELOC("name");
2253 /* find string offset */
2254 soff = dt_find_string(RELOC(pname));
2256 prom_printf("WARNING: Can't find string index for"
2257 " <%s>, node %s\n", RELOC(pname), path);
2260 prev_name = sstart + soff;
2263 l = call_prom("getproplen", 2, 1, node, RELOC(pname));
2266 if (l == PROM_ERROR)
2269 /* push property head */
2270 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2271 dt_push_token(l, mem_start, mem_end);
2272 dt_push_token(soff, mem_start, mem_end);
2274 /* push property content */
2275 valp = make_room(mem_start, mem_end, l, 4);
2276 call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
2277 *mem_start = _ALIGN(*mem_start, 4);
2279 if (!strcmp(RELOC(pname), RELOC("phandle")))
2283 /* Add a "linux,phandle" property if no "phandle" property already
2284 * existed (can happen with OPAL)
2287 soff = dt_find_string(RELOC("linux,phandle"));
2289 prom_printf("WARNING: Can't find string index for"
2290 " <linux-phandle> node %s\n", path);
2292 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2293 dt_push_token(4, mem_start, mem_end);
2294 dt_push_token(soff, mem_start, mem_end);
2295 valp = make_room(mem_start, mem_end, 4, 4);
2296 *(u32 *)valp = node;
2300 /* do all our children */
2301 child = call_prom("child", 1, 1, node);
2302 while (child != 0) {
2303 scan_dt_build_struct(child, mem_start, mem_end);
2304 child = call_prom("peer", 1, 1, child);
2307 dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
2310 static void __init flatten_device_tree(void)
2313 unsigned long mem_start, mem_end, room;
2314 struct boot_param_header *hdr;
2315 struct prom_t *_prom = &RELOC(prom);
2320 * Check how much room we have between alloc top & bottom (+/- a
2321 * few pages), crop to 1MB, as this is our "chunk" size
2323 room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
2324 if (room > DEVTREE_CHUNK_SIZE)
2325 room = DEVTREE_CHUNK_SIZE;
2326 prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
2328 /* Now try to claim that */
2329 mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
2331 prom_panic("Can't allocate initial device-tree chunk\n");
2332 mem_end = mem_start + room;
2334 /* Get root of tree */
2335 root = call_prom("peer", 1, 1, (phandle)0);
2336 if (root == (phandle)0)
2337 prom_panic ("couldn't get device tree root\n");
2339 /* Build header and make room for mem rsv map */
2340 mem_start = _ALIGN(mem_start, 4);
2341 hdr = make_room(&mem_start, &mem_end,
2342 sizeof(struct boot_param_header), 4);
2343 RELOC(dt_header_start) = (unsigned long)hdr;
2344 rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
2346 /* Start of strings */
2347 mem_start = PAGE_ALIGN(mem_start);
2348 RELOC(dt_string_start) = mem_start;
2349 mem_start += 4; /* hole */
2351 /* Add "linux,phandle" in there, we'll need it */
2352 namep = make_room(&mem_start, &mem_end, 16, 1);
2353 strcpy(namep, RELOC("linux,phandle"));
2354 mem_start = (unsigned long)namep + strlen(namep) + 1;
2356 /* Build string array */
2357 prom_printf("Building dt strings...\n");
2358 scan_dt_build_strings(root, &mem_start, &mem_end);
2359 RELOC(dt_string_end) = mem_start;
2361 /* Build structure */
2362 mem_start = PAGE_ALIGN(mem_start);
2363 RELOC(dt_struct_start) = mem_start;
2364 prom_printf("Building dt structure...\n");
2365 scan_dt_build_struct(root, &mem_start, &mem_end);
2366 dt_push_token(OF_DT_END, &mem_start, &mem_end);
2367 RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
2370 hdr->boot_cpuid_phys = _prom->cpu;
2371 hdr->magic = OF_DT_HEADER;
2372 hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
2373 hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
2374 hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
2375 hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
2376 hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
2377 hdr->version = OF_DT_VERSION;
2378 /* Version 16 is not backward compatible */
2379 hdr->last_comp_version = 0x10;
2381 /* Copy the reserve map in */
2382 memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
2387 prom_printf("reserved memory map:\n");
2388 for (i = 0; i < RELOC(mem_reserve_cnt); i++)
2389 prom_printf(" %x - %x\n",
2390 RELOC(mem_reserve_map)[i].base,
2391 RELOC(mem_reserve_map)[i].size);
2394 /* Bump mem_reserve_cnt to cause further reservations to fail
2395 * since it's too late.
2397 RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
2399 prom_printf("Device tree strings 0x%x -> 0x%x\n",
2400 RELOC(dt_string_start), RELOC(dt_string_end));
2401 prom_printf("Device tree struct 0x%x -> 0x%x\n",
2402 RELOC(dt_struct_start), RELOC(dt_struct_end));
2406 #ifdef CONFIG_PPC_MAPLE
2407 /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2408 * The values are bad, and it doesn't even have the right number of cells. */
2409 static void __init fixup_device_tree_maple(void)
2412 u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
2416 name = "/ht@0/isa@4";
2417 isa = call_prom("finddevice", 1, 1, ADDR(name));
2418 if (!PHANDLE_VALID(isa)) {
2419 name = "/ht@0/isa@6";
2420 isa = call_prom("finddevice", 1, 1, ADDR(name));
2421 rloc = 0x01003000; /* IO space; PCI device = 6 */
2423 if (!PHANDLE_VALID(isa))
2426 if (prom_getproplen(isa, "ranges") != 12)
2428 if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2432 if (isa_ranges[0] != 0x1 ||
2433 isa_ranges[1] != 0xf4000000 ||
2434 isa_ranges[2] != 0x00010000)
2437 prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
2439 isa_ranges[0] = 0x1;
2440 isa_ranges[1] = 0x0;
2441 isa_ranges[2] = rloc;
2442 isa_ranges[3] = 0x0;
2443 isa_ranges[4] = 0x0;
2444 isa_ranges[5] = 0x00010000;
2445 prom_setprop(isa, name, "ranges",
2446 isa_ranges, sizeof(isa_ranges));
2449 #define CPC925_MC_START 0xf8000000
2450 #define CPC925_MC_LENGTH 0x1000000
2451 /* The values for memory-controller don't have right number of cells */
2452 static void __init fixup_device_tree_maple_memory_controller(void)
2456 char *name = "/hostbridge@f8000000";
2457 struct prom_t *_prom = &RELOC(prom);
2460 mc = call_prom("finddevice", 1, 1, ADDR(name));
2461 if (!PHANDLE_VALID(mc))
2464 if (prom_getproplen(mc, "reg") != 8)
2467 prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
2468 prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
2469 if ((ac != 2) || (sc != 2))
2472 if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
2475 if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
2478 prom_printf("Fixing up bogus hostbridge on Maple...\n");
2481 mc_reg[1] = CPC925_MC_START;
2483 mc_reg[3] = CPC925_MC_LENGTH;
2484 prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
2487 #define fixup_device_tree_maple()
2488 #define fixup_device_tree_maple_memory_controller()
2491 #ifdef CONFIG_PPC_CHRP
2493 * Pegasos and BriQ lacks the "ranges" property in the isa node
2494 * Pegasos needs decimal IRQ 14/15, not hexadecimal
2495 * Pegasos has the IDE configured in legacy mode, but advertised as native
2497 static void __init fixup_device_tree_chrp(void)
2501 u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2505 name = "/pci@80000000/isa@c";
2506 ph = call_prom("finddevice", 1, 1, ADDR(name));
2507 if (!PHANDLE_VALID(ph)) {
2508 name = "/pci@ff500000/isa@6";
2509 ph = call_prom("finddevice", 1, 1, ADDR(name));
2510 rloc = 0x01003000; /* IO space; PCI device = 6 */
2512 if (PHANDLE_VALID(ph)) {
2513 rc = prom_getproplen(ph, "ranges");
2514 if (rc == 0 || rc == PROM_ERROR) {
2515 prom_printf("Fixing up missing ISA range on Pegasos...\n");
2522 prop[5] = 0x00010000;
2523 prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2527 name = "/pci@80000000/ide@C,1";
2528 ph = call_prom("finddevice", 1, 1, ADDR(name));
2529 if (PHANDLE_VALID(ph)) {
2530 prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2533 prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2534 prom_printf("Fixing up IDE class-code on Pegasos...\n");
2535 rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2536 if (rc == sizeof(u32)) {
2538 prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2543 #define fixup_device_tree_chrp()
2546 #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
2547 static void __init fixup_device_tree_pmac(void)
2549 phandle u3, i2c, mpic;
2554 /* Some G5s have a missing interrupt definition, fix it up here */
2555 u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2556 if (!PHANDLE_VALID(u3))
2558 i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2559 if (!PHANDLE_VALID(i2c))
2561 mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2562 if (!PHANDLE_VALID(mpic))
2565 /* check if proper rev of u3 */
2566 if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2569 if (u3_rev < 0x35 || u3_rev > 0x39)
2571 /* does it need fixup ? */
2572 if (prom_getproplen(i2c, "interrupts") > 0)
2575 prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2577 /* interrupt on this revision of u3 is number 0 and level */
2580 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2581 &interrupts, sizeof(interrupts));
2583 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2584 &parent, sizeof(parent));
2587 #define fixup_device_tree_pmac()
2590 #ifdef CONFIG_PPC_EFIKA
2592 * The MPC5200 FEC driver requires an phy-handle property to tell it how
2593 * to talk to the phy. If the phy-handle property is missing, then this
2594 * function is called to add the appropriate nodes and link it to the
2597 static void __init fixup_device_tree_efika_add_phy(void)
2603 /* Check if /builtin/ethernet exists - bail if it doesn't */
2604 node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
2605 if (!PHANDLE_VALID(node))
2608 /* Check if the phy-handle property exists - bail if it does */
2609 rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2614 * At this point the ethernet device doesn't have a phy described.
2615 * Now we need to add the missing phy node and linkage
2618 /* Check for an MDIO bus node - if missing then create one */
2619 node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2620 if (!PHANDLE_VALID(node)) {
2621 prom_printf("Adding Ethernet MDIO node\n");
2622 call_prom("interpret", 1, 1,
2623 " s\" /builtin\" find-device"
2625 " 1 encode-int s\" #address-cells\" property"
2626 " 0 encode-int s\" #size-cells\" property"
2627 " s\" mdio\" device-name"
2628 " s\" fsl,mpc5200b-mdio\" encode-string"
2629 " s\" compatible\" property"
2630 " 0xf0003000 0x400 reg"
2632 " 0x5 encode-int encode+"
2633 " 0x3 encode-int encode+"
2634 " s\" interrupts\" property"
2638 /* Check for a PHY device node - if missing then create one and
2639 * give it's phandle to the ethernet node */
2640 node = call_prom("finddevice", 1, 1,
2641 ADDR("/builtin/mdio/ethernet-phy"));
2642 if (!PHANDLE_VALID(node)) {
2643 prom_printf("Adding Ethernet PHY node\n");
2644 call_prom("interpret", 1, 1,
2645 " s\" /builtin/mdio\" find-device"
2647 " s\" ethernet-phy\" device-name"
2648 " 0x10 encode-int s\" reg\" property"
2652 " s\" /builtin/ethernet\" find-device"
2654 " s\" phy-handle\" property"
2659 static void __init fixup_device_tree_efika(void)
2661 int sound_irq[3] = { 2, 2, 0 };
2662 int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2663 3,4,0, 3,5,0, 3,6,0, 3,7,0,
2664 3,8,0, 3,9,0, 3,10,0, 3,11,0,
2665 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2670 /* Check if we're really running on a EFIKA */
2671 node = call_prom("finddevice", 1, 1, ADDR("/"));
2672 if (!PHANDLE_VALID(node))
2675 rv = prom_getprop(node, "model", prop, sizeof(prop));
2676 if (rv == PROM_ERROR)
2678 if (strcmp(prop, "EFIKA5K2"))
2681 prom_printf("Applying EFIKA device tree fixups\n");
2683 /* Claiming to be 'chrp' is death */
2684 node = call_prom("finddevice", 1, 1, ADDR("/"));
2685 rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2686 if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2687 prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2689 /* CODEGEN,description is exposed in /proc/cpuinfo so
2691 rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2692 if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2693 prom_setprop(node, "/", "CODEGEN,description",
2694 "Efika 5200B PowerPC System",
2695 sizeof("Efika 5200B PowerPC System"));
2697 /* Fixup bestcomm interrupts property */
2698 node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2699 if (PHANDLE_VALID(node)) {
2700 len = prom_getproplen(node, "interrupts");
2702 prom_printf("Fixing bestcomm interrupts property\n");
2703 prom_setprop(node, "/builtin/bestcom", "interrupts",
2704 bcomm_irq, sizeof(bcomm_irq));
2708 /* Fixup sound interrupts property */
2709 node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2710 if (PHANDLE_VALID(node)) {
2711 rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2712 if (rv == PROM_ERROR) {
2713 prom_printf("Adding sound interrupts property\n");
2714 prom_setprop(node, "/builtin/sound", "interrupts",
2715 sound_irq, sizeof(sound_irq));
2719 /* Make sure ethernet phy-handle property exists */
2720 fixup_device_tree_efika_add_phy();
2723 #define fixup_device_tree_efika()
2726 static void __init fixup_device_tree(void)
2728 fixup_device_tree_maple();
2729 fixup_device_tree_maple_memory_controller();
2730 fixup_device_tree_chrp();
2731 fixup_device_tree_pmac();
2732 fixup_device_tree_efika();
2735 static void __init prom_find_boot_cpu(void)
2737 struct prom_t *_prom = &RELOC(prom);
2743 if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
2746 cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2748 prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
2749 _prom->cpu = getprop_rval;
2751 prom_debug("Booting CPU hw index = %lu\n", _prom->cpu);
2754 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2756 #ifdef CONFIG_BLK_DEV_INITRD
2757 struct prom_t *_prom = &RELOC(prom);
2759 if (r3 && r4 && r4 != 0xdeadbeef) {
2762 RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
2763 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2765 val = RELOC(prom_initrd_start);
2766 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2768 val = RELOC(prom_initrd_end);
2769 prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2772 reserve_mem(RELOC(prom_initrd_start),
2773 RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
2775 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
2776 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
2778 #endif /* CONFIG_BLK_DEV_INITRD */
2783 * We enter here early on, when the Open Firmware prom is still
2784 * handling exceptions and the MMU hash table for us.
2787 unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2789 unsigned long r6, unsigned long r7,
2790 unsigned long kbase)
2792 struct prom_t *_prom;
2796 unsigned long offset = reloc_offset();
2800 _prom = &RELOC(prom);
2803 * First zero the BSS
2805 memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
2808 * Init interface to Open Firmware, get some node references,
2811 prom_init_client_services(pp);
2814 * See if this OF is old enough that we need to do explicit maps
2815 * and other workarounds
2820 * Init prom stdout device
2824 prom_printf("Preparing to boot %s", RELOC(linux_banner));
2827 * Get default machine type. At this point, we do not differentiate
2828 * between pSeries SMP and pSeries LPAR
2830 RELOC(of_platform) = prom_find_machine_type();
2831 prom_printf("Detected machine type: %x\n", RELOC(of_platform));
2833 #ifndef CONFIG_NONSTATIC_KERNEL
2834 /* Bail if this is a kdump kernel. */
2835 if (PHYSICAL_START > 0)
2836 prom_panic("Error: You can't boot a kdump kernel from OF!\n");
2840 * Check for an initrd
2842 prom_check_initrd(r3, r4);
2844 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
2846 * On pSeries, inform the firmware about our capabilities
2848 if (RELOC(of_platform) == PLATFORM_PSERIES ||
2849 RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
2850 prom_send_capabilities();
2854 * Copy the CPU hold code
2856 if (RELOC(of_platform) != PLATFORM_POWERMAC)
2857 copy_and_flush(0, kbase, 0x100, 0);
2860 * Do early parsing of command line
2862 early_cmdline_parse();
2865 * Initialize memory management within prom_init
2870 * Determine which cpu is actually running right _now_
2872 prom_find_boot_cpu();
2875 * Initialize display devices
2877 prom_check_displays();
2881 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2882 * that uses the allocator, we need to make sure we get the top of memory
2883 * available for us here...
2885 if (RELOC(of_platform) == PLATFORM_PSERIES)
2886 prom_initialize_tce_table();
2890 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
2891 * have a usable RTAS implementation.
2893 if (RELOC(of_platform) != PLATFORM_POWERMAC &&
2894 RELOC(of_platform) != PLATFORM_OPAL)
2895 prom_instantiate_rtas();
2897 #ifdef CONFIG_PPC_POWERNV
2898 /* Detect HAL and try instanciating it & doing takeover */
2899 if (RELOC(of_platform) == PLATFORM_PSERIES_LPAR) {
2901 if (RELOC(of_platform) == PLATFORM_OPAL) {
2902 prom_opal_hold_cpus();
2903 prom_opal_takeover();
2905 } else if (RELOC(of_platform) == PLATFORM_OPAL)
2906 prom_instantiate_opal();
2910 * On non-powermacs, put all CPUs in spin-loops.
2912 * PowerMacs use a different mechanism to spin CPUs
2914 if (RELOC(of_platform) != PLATFORM_POWERMAC &&
2915 RELOC(of_platform) != PLATFORM_OPAL)
2919 * Fill in some infos for use by the kernel later on
2921 if (RELOC(prom_memory_limit))
2922 prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
2923 &RELOC(prom_memory_limit),
2924 sizeof(prom_memory_limit));
2926 if (RELOC(prom_iommu_off))
2927 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2930 if (RELOC(prom_iommu_force_on))
2931 prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2934 if (RELOC(prom_tce_alloc_start)) {
2935 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
2936 &RELOC(prom_tce_alloc_start),
2937 sizeof(prom_tce_alloc_start));
2938 prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
2939 &RELOC(prom_tce_alloc_end),
2940 sizeof(prom_tce_alloc_end));
2945 * Fixup any known bugs in the device-tree
2947 fixup_device_tree();
2950 * Now finally create the flattened device-tree
2952 prom_printf("copying OF device tree...\n");
2953 flatten_device_tree();
2956 * in case stdin is USB and still active on IBM machines...
2957 * Unfortunately quiesce crashes on some powermacs if we have
2958 * closed stdin already (in particular the powerbook 101). It
2959 * appears that the OPAL version of OFW doesn't like it either.
2961 if (RELOC(of_platform) != PLATFORM_POWERMAC &&
2962 RELOC(of_platform) != PLATFORM_OPAL)
2966 * Call OF "quiesce" method to shut down pending DMA's from
2969 prom_printf("Calling quiesce...\n");
2970 call_prom("quiesce", 0, 0);
2973 * And finally, call the kernel passing it the flattened device
2974 * tree and NULL as r5, thus triggering the new entry point which
2975 * is common to us and kexec
2977 hdr = RELOC(dt_header_start);
2979 /* Don't print anything after quiesce under OPAL, it crashes OFW */
2980 if (RELOC(of_platform) != PLATFORM_OPAL) {
2981 prom_printf("returning from prom_init\n");
2982 prom_debug("->dt_header_start=0x%x\n", hdr);
2986 reloc_got2(-offset);
2989 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
2990 /* OPAL early debug gets the OPAL base & entry in r8 and r9 */
2991 __start(hdr, kbase, 0, 0, 0,
2992 RELOC(prom_opal_base), RELOC(prom_opal_entry));
2994 __start(hdr, kbase, 0, 0, 0, 0, 0);