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.
18 /* we cannot use FORTIFY as it brings in new symbols */
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/init.h>
25 #include <linux/threads.h>
26 #include <linux/spinlock.h>
27 #include <linux/types.h>
28 #include <linux/pci.h>
29 #include <linux/proc_fs.h>
30 #include <linux/delay.h>
31 #include <linux/initrd.h>
32 #include <linux/bitops.h>
36 #include <asm/processor.h>
41 #include <asm/pgtable.h>
42 #include <asm/iommu.h>
43 #include <asm/btext.h>
44 #include <asm/sections.h>
45 #include <asm/machdep.h>
46 #include <asm/asm-prototypes.h>
48 #include <linux/linux_logo.h>
50 /* All of prom_init bss lives here */
51 #define __prombss __section(.bss.prominit)
54 * Eventually bump that one up
56 #define DEVTREE_CHUNK_SIZE 0x100000
59 * This is the size of the local memory reserve map that gets copied
60 * into the boot params passed to the kernel. That size is totally
61 * flexible as the kernel just reads the list until it encounters an
62 * entry with size 0, so it can be changed without breaking binary
65 #define MEM_RESERVE_MAP_SIZE 8
68 * prom_init() is called very early on, before the kernel text
69 * and data have been mapped to KERNELBASE. At this point the code
70 * is running at whatever address it has been loaded at.
71 * On ppc32 we compile with -mrelocatable, which means that references
72 * to extern and static variables get relocated automatically.
73 * ppc64 objects are always relocatable, we just need to relocate the
76 * Because OF may have mapped I/O devices into the area starting at
77 * KERNELBASE, particularly on CHRP machines, we can't safely call
78 * OF once the kernel has been mapped to KERNELBASE. Therefore all
79 * OF calls must be done within prom_init().
81 * ADDR is used in calls to call_prom. The 4th and following
82 * arguments to call_prom should be 32-bit values.
83 * On ppc64, 64 bit values are truncated to 32 bits (and
84 * fortunately don't get interpreted as two arguments).
86 #define ADDR(x) (u32)(unsigned long)(x)
89 #define OF_WORKAROUNDS 0
91 #define OF_WORKAROUNDS of_workarounds
92 static int of_workarounds __prombss;
95 #define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */
96 #define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */
98 #define PROM_BUG() do { \
99 prom_printf("kernel BUG at %s line 0x%x!\n", \
100 __FILE__, __LINE__); \
101 __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
105 #define prom_debug(x...) prom_printf(x)
107 #define prom_debug(x...) do { } while (0)
111 typedef u32 prom_arg_t;
129 struct mem_map_entry {
134 typedef __be32 cell_t;
136 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
137 unsigned long r6, unsigned long r7, unsigned long r8,
141 extern int enter_prom(struct prom_args *args, unsigned long entry);
143 static inline int enter_prom(struct prom_args *args, unsigned long entry)
145 return ((int (*)(struct prom_args *))entry)(args);
149 extern void copy_and_flush(unsigned long dest, unsigned long src,
150 unsigned long size, unsigned long offset);
153 static struct prom_t __prombss prom;
155 static unsigned long __prombss prom_entry;
157 #define PROM_SCRATCH_SIZE 256
159 static char __prombss of_stdout_device[256];
160 static char __prombss prom_scratch[PROM_SCRATCH_SIZE];
162 static unsigned long __prombss dt_header_start;
163 static unsigned long __prombss dt_struct_start, dt_struct_end;
164 static unsigned long __prombss dt_string_start, dt_string_end;
166 static unsigned long __prombss prom_initrd_start, prom_initrd_end;
169 static int __prombss prom_iommu_force_on;
170 static int __prombss prom_iommu_off;
171 static unsigned long __prombss prom_tce_alloc_start;
172 static unsigned long __prombss prom_tce_alloc_end;
175 #ifdef CONFIG_PPC_PSERIES
176 static bool __prombss prom_radix_disable;
179 struct platform_support {
186 /* Platforms codes are now obsolete in the kernel. Now only used within this
187 * file and ultimately gone too. Feel free to change them if you need, they
188 * are not shared with anything outside of this file anymore
190 #define PLATFORM_PSERIES 0x0100
191 #define PLATFORM_PSERIES_LPAR 0x0101
192 #define PLATFORM_LPAR 0x0001
193 #define PLATFORM_POWERMAC 0x0400
194 #define PLATFORM_GENERIC 0x0500
196 static int __prombss of_platform;
198 static char __prombss prom_cmd_line[COMMAND_LINE_SIZE];
200 static unsigned long __prombss prom_memory_limit;
202 static unsigned long __prombss alloc_top;
203 static unsigned long __prombss alloc_top_high;
204 static unsigned long __prombss alloc_bottom;
205 static unsigned long __prombss rmo_top;
206 static unsigned long __prombss ram_top;
208 static struct mem_map_entry __prombss mem_reserve_map[MEM_RESERVE_MAP_SIZE];
209 static int __prombss mem_reserve_cnt;
211 static cell_t __prombss regbuf[1024];
213 static bool __prombss rtas_has_query_cpu_stopped;
217 * Error results ... some OF calls will return "-1" on error, some
218 * will return 0, some will return either. To simplify, here are
219 * macros to use with any ihandle or phandle return value to check if
223 #define PROM_ERROR (-1u)
224 #define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
225 #define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
228 /* This is the one and *ONLY* place where we actually call open
232 static int __init call_prom(const char *service, int nargs, int nret, ...)
235 struct prom_args args;
238 args.service = cpu_to_be32(ADDR(service));
239 args.nargs = cpu_to_be32(nargs);
240 args.nret = cpu_to_be32(nret);
242 va_start(list, nret);
243 for (i = 0; i < nargs; i++)
244 args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
247 for (i = 0; i < nret; i++)
248 args.args[nargs+i] = 0;
250 if (enter_prom(&args, prom_entry) < 0)
253 return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
256 static int __init call_prom_ret(const char *service, int nargs, int nret,
257 prom_arg_t *rets, ...)
260 struct prom_args args;
263 args.service = cpu_to_be32(ADDR(service));
264 args.nargs = cpu_to_be32(nargs);
265 args.nret = cpu_to_be32(nret);
267 va_start(list, rets);
268 for (i = 0; i < nargs; i++)
269 args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
272 for (i = 0; i < nret; i++)
273 args.args[nargs+i] = 0;
275 if (enter_prom(&args, prom_entry) < 0)
279 for (i = 1; i < nret; ++i)
280 rets[i-1] = be32_to_cpu(args.args[nargs+i]);
282 return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
286 static void __init prom_print(const char *msg)
290 if (prom.stdout == 0)
293 for (p = msg; *p != 0; p = q) {
294 for (q = p; *q != 0 && *q != '\n'; ++q)
297 call_prom("write", 3, 1, prom.stdout, p, q - p);
301 call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
307 * Both prom_print_hex & prom_print_dec takes an unsigned long as input so that
308 * we do not need __udivdi3 or __umoddi3 on 32bits.
310 static void __init prom_print_hex(unsigned long val)
312 int i, nibbles = sizeof(val)*2;
313 char buf[sizeof(val)*2+1];
315 for (i = nibbles-1; i >= 0; i--) {
316 buf[i] = (val & 0xf) + '0';
318 buf[i] += ('a'-'0'-10);
322 call_prom("write", 3, 1, prom.stdout, buf, nibbles);
325 /* max number of decimal digits in an unsigned long */
327 static void __init prom_print_dec(unsigned long val)
330 char buf[UL_DIGITS+1];
332 for (i = UL_DIGITS-1; i >= 0; i--) {
333 buf[i] = (val % 10) + '0';
338 /* shift stuff down */
339 size = UL_DIGITS - i;
340 call_prom("write", 3, 1, prom.stdout, buf+i, size);
344 static void __init prom_printf(const char *format, ...)
346 const char *p, *q, *s;
352 va_start(args, format);
353 for (p = format; *p != 0; p = q) {
354 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
357 call_prom("write", 3, 1, prom.stdout, p, q - p);
362 call_prom("write", 3, 1, prom.stdout,
376 s = va_arg(args, const char *);
383 v = va_arg(args, unsigned int);
386 v = va_arg(args, unsigned long);
390 v = va_arg(args, unsigned long long);
399 v = va_arg(args, unsigned int);
402 v = va_arg(args, unsigned long);
406 v = va_arg(args, unsigned long long);
415 vs = va_arg(args, int);
418 vs = va_arg(args, long);
422 vs = va_arg(args, long long);
437 static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
441 if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
443 * Old OF requires we claim physical and virtual separately
444 * and then map explicitly (assuming virtual mode)
449 ret = call_prom_ret("call-method", 5, 2, &result,
450 ADDR("claim"), prom.memory,
452 if (ret != 0 || result == -1)
454 ret = call_prom_ret("call-method", 5, 2, &result,
455 ADDR("claim"), prom.mmumap,
458 call_prom("call-method", 4, 1, ADDR("release"),
459 prom.memory, size, virt);
462 /* the 0x12 is M (coherence) + PP == read/write */
463 call_prom("call-method", 6, 1,
464 ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
467 return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
471 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
474 /* Do not call exit because it clears the screen on pmac
475 * it also causes some sort of double-fault on early pmacs */
476 if (of_platform == PLATFORM_POWERMAC)
479 /* ToDo: should put up an SRC here on pSeries */
480 call_prom("exit", 0, 0);
482 for (;;) /* should never get here */
487 static int __init prom_next_node(phandle *nodep)
491 if ((node = *nodep) != 0
492 && (*nodep = call_prom("child", 1, 1, node)) != 0)
494 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
497 if ((node = call_prom("parent", 1, 1, node)) == 0)
499 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
504 static inline int prom_getprop(phandle node, const char *pname,
505 void *value, size_t valuelen)
507 return call_prom("getprop", 4, 1, node, ADDR(pname),
508 (u32)(unsigned long) value, (u32) valuelen);
511 static inline int prom_getproplen(phandle node, const char *pname)
513 return call_prom("getproplen", 2, 1, node, ADDR(pname));
516 static void add_string(char **str, const char *q)
526 static char *tohex(unsigned int x)
528 static const char digits[] __initconst = "0123456789abcdef";
529 static char result[9] __prombss;
536 result[i] = digits[x & 0xf];
538 } while (x != 0 && i > 0);
542 static int __init prom_setprop(phandle node, const char *nodename,
543 const char *pname, void *value, size_t valuelen)
547 if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
548 return call_prom("setprop", 4, 1, node, ADDR(pname),
549 (u32)(unsigned long) value, (u32) valuelen);
551 /* gah... setprop doesn't work on longtrail, have to use interpret */
553 add_string(&p, "dev");
554 add_string(&p, nodename);
555 add_string(&p, tohex((u32)(unsigned long) value));
556 add_string(&p, tohex(valuelen));
557 add_string(&p, tohex(ADDR(pname)));
558 add_string(&p, tohex(strlen(pname)));
559 add_string(&p, "property");
561 return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
564 /* We can't use the standard versions because of relocation headaches. */
565 #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
566 || ('a' <= (c) && (c) <= 'f') \
567 || ('A' <= (c) && (c) <= 'F'))
569 #define isdigit(c) ('0' <= (c) && (c) <= '9')
570 #define islower(c) ('a' <= (c) && (c) <= 'z')
571 #define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c))
573 static unsigned long prom_strtoul(const char *cp, const char **endp)
575 unsigned long result = 0, base = 10, value;
580 if (toupper(*cp) == 'X') {
586 while (isxdigit(*cp) &&
587 (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
588 result = result * base + value;
598 static unsigned long prom_memparse(const char *ptr, const char **retptr)
600 unsigned long ret = prom_strtoul(ptr, retptr);
604 * We can't use a switch here because GCC *may* generate a
605 * jump table which won't work, because we're not running at
606 * the address we're linked at.
608 if ('G' == **retptr || 'g' == **retptr)
611 if ('M' == **retptr || 'm' == **retptr)
614 if ('K' == **retptr || 'k' == **retptr)
626 * Early parsing of the command line passed to the kernel, used for
627 * "mem=x" and the options that affect the iommu
629 static void __init early_cmdline_parse(void)
634 int l __maybe_unused = 0;
636 prom_cmd_line[0] = 0;
638 if ((long)prom.chosen > 0)
639 l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
640 #ifdef CONFIG_CMDLINE
641 if (l <= 0 || p[0] == '\0') /* dbl check */
642 strlcpy(prom_cmd_line,
643 CONFIG_CMDLINE, sizeof(prom_cmd_line));
644 #endif /* CONFIG_CMDLINE */
645 prom_printf("command line: %s\n", prom_cmd_line);
648 opt = strstr(prom_cmd_line, "iommu=");
650 prom_printf("iommu opt is: %s\n", opt);
652 while (*opt && *opt == ' ')
654 if (!strncmp(opt, "off", 3))
656 else if (!strncmp(opt, "force", 5))
657 prom_iommu_force_on = 1;
660 opt = strstr(prom_cmd_line, "mem=");
663 prom_memory_limit = prom_memparse(opt, (const char **)&opt);
665 /* Align to 16 MB == size of ppc64 large page */
666 prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
670 #ifdef CONFIG_PPC_PSERIES
671 prom_radix_disable = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
672 opt = strstr(prom_cmd_line, "disable_radix");
675 if (*opt && *opt == '=') {
678 if (kstrtobool(++opt, &val))
679 prom_radix_disable = false;
681 prom_radix_disable = val;
683 prom_radix_disable = true;
685 if (prom_radix_disable)
686 prom_debug("Radix disabled from cmdline\n");
687 #endif /* CONFIG_PPC_PSERIES */
690 #ifdef CONFIG_PPC_PSERIES
692 * The architecture vector has an array of PVR mask/value pairs,
693 * followed by # option vectors - 1, followed by the option vectors.
695 * See prom.h for the definition of the bits specified in the
696 * architecture vector.
699 /* Firmware expects the value to be n - 1, where n is the # of vectors */
700 #define NUM_VECTORS(n) ((n) - 1)
703 * Firmware expects 1 + n - 2, where n is the length of the option vector in
704 * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
706 #define VECTOR_LENGTH(n) (1 + (n) - 2)
708 struct option_vector1 {
714 struct option_vector2 {
728 struct option_vector3 {
733 struct option_vector4 {
738 struct option_vector5 {
750 u8 platform_facilities;
761 struct option_vector6 {
767 struct ibm_arch_vec {
768 struct { u32 mask, val; } pvrs[12];
773 struct option_vector1 vec1;
776 struct option_vector2 vec2;
779 struct option_vector3 vec3;
782 struct option_vector4 vec4;
785 struct option_vector5 vec5;
788 struct option_vector6 vec6;
791 static const struct ibm_arch_vec ibm_architecture_vec_template __initconst = {
794 .mask = cpu_to_be32(0xfffe0000), /* POWER5/POWER5+ */
795 .val = cpu_to_be32(0x003a0000),
798 .mask = cpu_to_be32(0xffff0000), /* POWER6 */
799 .val = cpu_to_be32(0x003e0000),
802 .mask = cpu_to_be32(0xffff0000), /* POWER7 */
803 .val = cpu_to_be32(0x003f0000),
806 .mask = cpu_to_be32(0xffff0000), /* POWER8E */
807 .val = cpu_to_be32(0x004b0000),
810 .mask = cpu_to_be32(0xffff0000), /* POWER8NVL */
811 .val = cpu_to_be32(0x004c0000),
814 .mask = cpu_to_be32(0xffff0000), /* POWER8 */
815 .val = cpu_to_be32(0x004d0000),
818 .mask = cpu_to_be32(0xffff0000), /* POWER9 */
819 .val = cpu_to_be32(0x004e0000),
822 .mask = cpu_to_be32(0xffffffff), /* all 3.00-compliant */
823 .val = cpu_to_be32(0x0f000005),
826 .mask = cpu_to_be32(0xffffffff), /* all 2.07-compliant */
827 .val = cpu_to_be32(0x0f000004),
830 .mask = cpu_to_be32(0xffffffff), /* all 2.06-compliant */
831 .val = cpu_to_be32(0x0f000003),
834 .mask = cpu_to_be32(0xffffffff), /* all 2.05-compliant */
835 .val = cpu_to_be32(0x0f000002),
838 .mask = cpu_to_be32(0xfffffffe), /* all 2.04-compliant and earlier */
839 .val = cpu_to_be32(0x0f000001),
843 .num_vectors = NUM_VECTORS(6),
845 .vec1_len = VECTOR_LENGTH(sizeof(struct option_vector1)),
848 .arch_versions = OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
849 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
850 .arch_versions3 = OV1_PPC_3_00,
853 .vec2_len = VECTOR_LENGTH(sizeof(struct option_vector2)),
854 /* option vector 2: Open Firmware options supported */
856 .byte1 = OV2_REAL_MODE,
858 .real_base = cpu_to_be32(0xffffffff),
859 .real_size = cpu_to_be32(0xffffffff),
860 .virt_base = cpu_to_be32(0xffffffff),
861 .virt_size = cpu_to_be32(0xffffffff),
862 .load_base = cpu_to_be32(0xffffffff),
863 .min_rma = cpu_to_be32(512), /* 512MB min RMA */
864 .min_load = cpu_to_be32(0xffffffff), /* full client load */
865 .min_rma_percent = 0, /* min RMA percentage of total RAM */
866 .max_pft_size = 48, /* max log_2(hash table size) */
869 .vec3_len = VECTOR_LENGTH(sizeof(struct option_vector3)),
870 /* option vector 3: processor options supported */
872 .byte1 = 0, /* don't ignore, don't halt */
873 .byte2 = OV3_FP | OV3_VMX | OV3_DFP,
876 .vec4_len = VECTOR_LENGTH(sizeof(struct option_vector4)),
877 /* option vector 4: IBM PAPR implementation */
879 .byte1 = 0, /* don't halt */
880 .min_vp_cap = OV4_MIN_ENT_CAP, /* minimum VP entitled capacity */
883 .vec5_len = VECTOR_LENGTH(sizeof(struct option_vector5)),
884 /* option vector 5: PAPR/OF options */
886 .byte1 = 0, /* don't ignore, don't halt */
887 .byte2 = OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
888 OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
889 #ifdef CONFIG_PCI_MSI
890 /* PCIe/MSI support. Without MSI full PCIe is not supported */
897 #ifdef CONFIG_PPC_SMLPAR
898 OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
902 .associativity = OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
903 .bin_opts = OV5_FEAT(OV5_RESIZE_HPT) | OV5_FEAT(OV5_HP_EVT),
904 .micro_checkpoint = 0,
906 .max_cpus = cpu_to_be32(NR_CPUS), /* number of cores supported */
909 .platform_facilities = OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) | OV5_FEAT(OV5_PFO_HW_842),
913 .byte22 = OV5_FEAT(OV5_DRMEM_V2),
920 /* option vector 6: IBM PAPR hints */
921 .vec6_len = VECTOR_LENGTH(sizeof(struct option_vector6)),
925 .os_name = OV6_LINUX,
929 static struct ibm_arch_vec __prombss ibm_architecture_vec ____cacheline_aligned;
931 /* Old method - ELF header with PT_NOTE sections only works on BE */
932 #ifdef __BIG_ENDIAN__
933 static const struct fake_elf {
940 char name[8]; /* "PowerPC" */
954 char name[24]; /* "IBM,RPA-Client-Config" */
966 } fake_elf __initconst = {
968 .e_ident = { 0x7f, 'E', 'L', 'F',
969 ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
970 .e_type = ET_EXEC, /* yeah right */
972 .e_version = EV_CURRENT,
973 .e_phoff = offsetof(struct fake_elf, phdr),
974 .e_phentsize = sizeof(Elf32_Phdr),
980 .p_offset = offsetof(struct fake_elf, chrpnote),
981 .p_filesz = sizeof(struct chrpnote)
984 .p_offset = offsetof(struct fake_elf, rpanote),
985 .p_filesz = sizeof(struct rpanote)
989 .namesz = sizeof("PowerPC"),
990 .descsz = sizeof(struct chrpdesc),
994 .real_mode = ~0U, /* ~0 means "don't care" */
1003 .namesz = sizeof("IBM,RPA-Client-Config"),
1004 .descsz = sizeof(struct rpadesc),
1006 .name = "IBM,RPA-Client-Config",
1009 .min_rmo_size = 64, /* in megabytes */
1010 .min_rmo_percent = 0,
1011 .max_pft_size = 48, /* 2^48 bytes max PFT size */
1018 #endif /* __BIG_ENDIAN__ */
1020 static int __init prom_count_smt_threads(void)
1026 /* Pick up th first CPU node we can find */
1027 for (node = 0; prom_next_node(&node); ) {
1029 prom_getprop(node, "device_type", type, sizeof(type));
1031 if (strcmp(type, "cpu"))
1034 * There is an entry for each smt thread, each entry being
1035 * 4 bytes long. All cpus should have the same number of
1036 * smt threads, so return after finding the first.
1038 plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
1039 if (plen == PROM_ERROR)
1042 prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
1045 if (plen < 1 || plen > 64) {
1046 prom_printf("Threads per core %lu out of bounds, assuming 1\n",
1047 (unsigned long)plen);
1052 prom_debug("No threads found, assuming 1 per core\n");
1058 static void __init prom_parse_mmu_model(u8 val,
1059 struct platform_support *support)
1062 case OV5_FEAT(OV5_MMU_DYNAMIC):
1063 case OV5_FEAT(OV5_MMU_EITHER): /* Either Available */
1064 prom_debug("MMU - either supported\n");
1065 support->radix_mmu = !prom_radix_disable;
1066 support->hash_mmu = true;
1068 case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */
1069 prom_debug("MMU - radix only\n");
1070 if (prom_radix_disable) {
1072 * If we __have__ to do radix, we're better off ignoring
1073 * the command line rather than not booting.
1075 prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
1077 support->radix_mmu = true;
1079 case OV5_FEAT(OV5_MMU_HASH):
1080 prom_debug("MMU - hash only\n");
1081 support->hash_mmu = true;
1084 prom_debug("Unknown mmu support option: 0x%x\n", val);
1089 static void __init prom_parse_xive_model(u8 val,
1090 struct platform_support *support)
1093 case OV5_FEAT(OV5_XIVE_EITHER): /* Either Available */
1094 prom_debug("XIVE - either mode supported\n");
1095 support->xive = true;
1097 case OV5_FEAT(OV5_XIVE_EXPLOIT): /* Only Exploitation mode */
1098 prom_debug("XIVE - exploitation mode supported\n");
1099 support->xive = true;
1101 case OV5_FEAT(OV5_XIVE_LEGACY): /* Only Legacy mode */
1102 prom_debug("XIVE - legacy mode supported\n");
1105 prom_debug("Unknown xive support option: 0x%x\n", val);
1110 static void __init prom_parse_platform_support(u8 index, u8 val,
1111 struct platform_support *support)
1114 case OV5_INDX(OV5_MMU_SUPPORT): /* MMU Model */
1115 prom_parse_mmu_model(val & OV5_FEAT(OV5_MMU_SUPPORT), support);
1117 case OV5_INDX(OV5_RADIX_GTSE): /* Radix Extensions */
1118 if (val & OV5_FEAT(OV5_RADIX_GTSE)) {
1119 prom_debug("Radix - GTSE supported\n");
1120 support->radix_gtse = true;
1123 case OV5_INDX(OV5_XIVE_SUPPORT): /* Interrupt mode */
1124 prom_parse_xive_model(val & OV5_FEAT(OV5_XIVE_SUPPORT),
1130 static void __init prom_check_platform_support(void)
1132 struct platform_support supported = {
1135 .radix_gtse = false,
1138 int prop_len = prom_getproplen(prom.chosen,
1139 "ibm,arch-vec-5-platform-support");
1141 /* First copy the architecture vec template */
1142 ibm_architecture_vec = ibm_architecture_vec_template;
1147 prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1149 if (prop_len > sizeof(vec))
1150 prom_printf("WARNING: ibm,arch-vec-5-platform-support longer than expected (len: %d)\n",
1152 prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support",
1154 for (i = 0; i < sizeof(vec); i += 2) {
1155 prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2
1158 prom_parse_platform_support(vec[i], vec[i + 1],
1163 if (supported.radix_mmu && supported.radix_gtse &&
1164 IS_ENABLED(CONFIG_PPC_RADIX_MMU)) {
1165 /* Radix preferred - but we require GTSE for now */
1166 prom_debug("Asking for radix with GTSE\n");
1167 ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1168 ibm_architecture_vec.vec5.radix_ext = OV5_FEAT(OV5_RADIX_GTSE);
1169 } else if (supported.hash_mmu) {
1170 /* Default to hash mmu (if we can) */
1171 prom_debug("Asking for hash\n");
1172 ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1174 /* We're probably on a legacy hypervisor */
1175 prom_debug("Assuming legacy hash support\n");
1178 if (supported.xive) {
1179 prom_debug("Asking for XIVE\n");
1180 ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1184 static void __init prom_send_capabilities(void)
1190 /* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1191 prom_check_platform_support();
1193 root = call_prom("open", 1, 1, ADDR("/"));
1195 /* We need to tell the FW about the number of cores we support.
1197 * To do that, we count the number of threads on the first core
1198 * (we assume this is the same for all cores) and use it to
1202 cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1203 prom_printf("Max number of cores passed to firmware: %u (NR_CPUS = %d)\n",
1206 ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1208 /* try calling the ibm,client-architecture-support method */
1209 prom_printf("Calling ibm,client-architecture-support...");
1210 if (call_prom_ret("call-method", 3, 2, &ret,
1211 ADDR("ibm,client-architecture-support"),
1213 ADDR(&ibm_architecture_vec)) == 0) {
1214 /* the call exists... */
1216 prom_printf("\nWARNING: ibm,client-architecture"
1217 "-support call FAILED!\n");
1218 call_prom("close", 1, 0, root);
1219 prom_printf(" done\n");
1222 call_prom("close", 1, 0, root);
1223 prom_printf(" not implemented\n");
1226 #ifdef __BIG_ENDIAN__
1230 /* no ibm,client-architecture-support call, try the old way */
1231 elfloader = call_prom("open", 1, 1,
1232 ADDR("/packages/elf-loader"));
1233 if (elfloader == 0) {
1234 prom_printf("couldn't open /packages/elf-loader\n");
1237 call_prom("call-method", 3, 1, ADDR("process-elf-header"),
1238 elfloader, ADDR(&fake_elf));
1239 call_prom("close", 1, 0, elfloader);
1241 #endif /* __BIG_ENDIAN__ */
1243 #endif /* CONFIG_PPC_PSERIES */
1246 * Memory allocation strategy... our layout is normally:
1248 * at 14Mb or more we have vmlinux, then a gap and initrd. In some
1249 * rare cases, initrd might end up being before the kernel though.
1250 * We assume this won't override the final kernel at 0, we have no
1251 * provision to handle that in this version, but it should hopefully
1254 * alloc_top is set to the top of RMO, eventually shrink down if the
1257 * alloc_bottom is set to the top of kernel/initrd
1259 * from there, allocations are done this way : rtas is allocated
1260 * topmost, and the device-tree is allocated from the bottom. We try
1261 * to grow the device-tree allocation as we progress. If we can't,
1262 * then we fail, we don't currently have a facility to restart
1263 * elsewhere, but that shouldn't be necessary.
1265 * Note that calls to reserve_mem have to be done explicitly, memory
1266 * allocated with either alloc_up or alloc_down isn't automatically
1272 * Allocates memory in the RMO upward from the kernel/initrd
1274 * When align is 0, this is a special case, it means to allocate in place
1275 * at the current location of alloc_bottom or fail (that is basically
1276 * extending the previous allocation). Used for the device-tree flattening
1278 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
1280 unsigned long base = alloc_bottom;
1281 unsigned long addr = 0;
1284 base = _ALIGN_UP(base, align);
1285 prom_debug("%s(%lx, %lx)\n", __func__, size, align);
1287 prom_panic("alloc_up() called with mem not initialized\n");
1290 base = _ALIGN_UP(alloc_bottom, align);
1292 base = alloc_bottom;
1294 for(; (base + size) <= alloc_top;
1295 base = _ALIGN_UP(base + 0x100000, align)) {
1296 prom_debug(" trying: 0x%lx\n\r", base);
1297 addr = (unsigned long)prom_claim(base, size, 0);
1298 if (addr != PROM_ERROR && addr != 0)
1306 alloc_bottom = addr + size;
1308 prom_debug(" -> %lx\n", addr);
1309 prom_debug(" alloc_bottom : %lx\n", alloc_bottom);
1310 prom_debug(" alloc_top : %lx\n", alloc_top);
1311 prom_debug(" alloc_top_hi : %lx\n", alloc_top_high);
1312 prom_debug(" rmo_top : %lx\n", rmo_top);
1313 prom_debug(" ram_top : %lx\n", ram_top);
1319 * Allocates memory downward, either from top of RMO, or if highmem
1320 * is set, from the top of RAM. Note that this one doesn't handle
1321 * failures. It does claim memory if highmem is not set.
1323 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
1326 unsigned long base, addr = 0;
1328 prom_debug("%s(%lx, %lx, %s)\n", __func__, size, align,
1329 highmem ? "(high)" : "(low)");
1331 prom_panic("alloc_down() called with mem not initialized\n");
1334 /* Carve out storage for the TCE table. */
1335 addr = _ALIGN_DOWN(alloc_top_high - size, align);
1336 if (addr <= alloc_bottom)
1338 /* Will we bump into the RMO ? If yes, check out that we
1339 * didn't overlap existing allocations there, if we did,
1340 * we are dead, we must be the first in town !
1342 if (addr < rmo_top) {
1343 /* Good, we are first */
1344 if (alloc_top == rmo_top)
1345 alloc_top = rmo_top = addr;
1349 alloc_top_high = addr;
1353 base = _ALIGN_DOWN(alloc_top - size, align);
1354 for (; base > alloc_bottom;
1355 base = _ALIGN_DOWN(base - 0x100000, align)) {
1356 prom_debug(" trying: 0x%lx\n\r", base);
1357 addr = (unsigned long)prom_claim(base, size, 0);
1358 if (addr != PROM_ERROR && addr != 0)
1367 prom_debug(" -> %lx\n", addr);
1368 prom_debug(" alloc_bottom : %lx\n", alloc_bottom);
1369 prom_debug(" alloc_top : %lx\n", alloc_top);
1370 prom_debug(" alloc_top_hi : %lx\n", alloc_top_high);
1371 prom_debug(" rmo_top : %lx\n", rmo_top);
1372 prom_debug(" ram_top : %lx\n", ram_top);
1378 * Parse a "reg" cell
1380 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
1383 unsigned long r = 0;
1385 /* Ignore more than 2 cells */
1386 while (s > sizeof(unsigned long) / 4) {
1390 r = be32_to_cpu(*p++);
1394 r |= be32_to_cpu(*(p++));
1402 * Very dumb function for adding to the memory reserve list, but
1403 * we don't need anything smarter at this point
1405 * XXX Eventually check for collisions. They should NEVER happen.
1406 * If problems seem to show up, it would be a good start to track
1409 static void __init reserve_mem(u64 base, u64 size)
1411 u64 top = base + size;
1412 unsigned long cnt = mem_reserve_cnt;
1417 /* We need to always keep one empty entry so that we
1418 * have our terminator with "size" set to 0 since we are
1419 * dumb and just copy this entire array to the boot params
1421 base = _ALIGN_DOWN(base, PAGE_SIZE);
1422 top = _ALIGN_UP(top, PAGE_SIZE);
1425 if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
1426 prom_panic("Memory reserve map exhausted !\n");
1427 mem_reserve_map[cnt].base = cpu_to_be64(base);
1428 mem_reserve_map[cnt].size = cpu_to_be64(size);
1429 mem_reserve_cnt = cnt + 1;
1433 * Initialize memory allocation mechanism, parse "memory" nodes and
1434 * obtain that way the top of memory and RMO to setup out local allocator
1436 static void __init prom_init_mem(void)
1449 * We iterate the memory nodes to find
1450 * 1) top of RMO (first node)
1453 val = cpu_to_be32(2);
1454 prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1455 rac = be32_to_cpu(val);
1456 val = cpu_to_be32(1);
1457 prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1458 rsc = be32_to_cpu(val);
1459 prom_debug("root_addr_cells: %x\n", rac);
1460 prom_debug("root_size_cells: %x\n", rsc);
1462 prom_debug("scanning memory:\n");
1464 path = prom_scratch;
1467 for (node = 0; prom_next_node(&node); ) {
1469 prom_getprop(node, "device_type", type, sizeof(type));
1473 * CHRP Longtrail machines have no device_type
1474 * on the memory node, so check the name instead...
1476 prom_getprop(node, "name", type, sizeof(type));
1478 if (strcmp(type, "memory"))
1481 plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
1482 if (plen > sizeof(regbuf)) {
1483 prom_printf("memory node too large for buffer !\n");
1484 plen = sizeof(regbuf);
1487 endp = p + (plen / sizeof(cell_t));
1490 memset(path, 0, PROM_SCRATCH_SIZE);
1491 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1492 prom_debug(" node %s :\n", path);
1493 #endif /* DEBUG_PROM */
1495 while ((endp - p) >= (rac + rsc)) {
1496 unsigned long base, size;
1498 base = prom_next_cell(rac, &p);
1499 size = prom_next_cell(rsc, &p);
1503 prom_debug(" %lx %lx\n", base, size);
1504 if (base == 0 && (of_platform & PLATFORM_LPAR))
1506 if ((base + size) > ram_top)
1507 ram_top = base + size;
1511 alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
1514 * If prom_memory_limit is set we reduce the upper limits *except* for
1515 * alloc_top_high. This must be the real top of RAM so we can put
1519 alloc_top_high = ram_top;
1521 if (prom_memory_limit) {
1522 if (prom_memory_limit <= alloc_bottom) {
1523 prom_printf("Ignoring mem=%lx <= alloc_bottom.\n",
1525 prom_memory_limit = 0;
1526 } else if (prom_memory_limit >= ram_top) {
1527 prom_printf("Ignoring mem=%lx >= ram_top.\n",
1529 prom_memory_limit = 0;
1531 ram_top = prom_memory_limit;
1532 rmo_top = min(rmo_top, prom_memory_limit);
1537 * Setup our top alloc point, that is top of RMO or top of
1538 * segment 0 when running non-LPAR.
1539 * Some RS64 machines have buggy firmware where claims up at
1540 * 1GB fail. Cap at 768MB as a workaround.
1541 * Since 768MB is plenty of room, and we need to cap to something
1542 * reasonable on 32-bit, cap at 768MB on all machines.
1546 rmo_top = min(0x30000000ul, rmo_top);
1547 alloc_top = rmo_top;
1548 alloc_top_high = ram_top;
1551 * Check if we have an initrd after the kernel but still inside
1552 * the RMO. If we do move our bottom point to after it.
1554 if (prom_initrd_start &&
1555 prom_initrd_start < rmo_top &&
1556 prom_initrd_end > alloc_bottom)
1557 alloc_bottom = PAGE_ALIGN(prom_initrd_end);
1559 prom_printf("memory layout at init:\n");
1560 prom_printf(" memory_limit : %lx (16 MB aligned)\n",
1562 prom_printf(" alloc_bottom : %lx\n", alloc_bottom);
1563 prom_printf(" alloc_top : %lx\n", alloc_top);
1564 prom_printf(" alloc_top_hi : %lx\n", alloc_top_high);
1565 prom_printf(" rmo_top : %lx\n", rmo_top);
1566 prom_printf(" ram_top : %lx\n", ram_top);
1569 static void __init prom_close_stdin(void)
1574 if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1575 stdin = be32_to_cpu(val);
1576 call_prom("close", 1, 0, stdin);
1581 * Allocate room for and instantiate RTAS
1583 static void __init prom_instantiate_rtas(void)
1587 u32 base, entry = 0;
1591 prom_debug("prom_instantiate_rtas: start...\n");
1593 rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1594 prom_debug("rtas_node: %x\n", rtas_node);
1595 if (!PHANDLE_VALID(rtas_node))
1599 prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1600 size = be32_to_cpu(val);
1604 base = alloc_down(size, PAGE_SIZE, 0);
1606 prom_panic("Could not allocate memory for RTAS\n");
1608 rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1609 if (!IHANDLE_VALID(rtas_inst)) {
1610 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
1614 prom_printf("instantiating rtas at 0x%x...", base);
1616 if (call_prom_ret("call-method", 3, 2, &entry,
1617 ADDR("instantiate-rtas"),
1618 rtas_inst, base) != 0
1620 prom_printf(" failed\n");
1623 prom_printf(" done\n");
1625 reserve_mem(base, size);
1627 val = cpu_to_be32(base);
1628 prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1630 val = cpu_to_be32(entry);
1631 prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1634 /* Check if it supports "query-cpu-stopped-state" */
1635 if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1636 &val, sizeof(val)) != PROM_ERROR)
1637 rtas_has_query_cpu_stopped = true;
1639 prom_debug("rtas base = 0x%x\n", base);
1640 prom_debug("rtas entry = 0x%x\n", entry);
1641 prom_debug("rtas size = 0x%x\n", size);
1643 prom_debug("prom_instantiate_rtas: end...\n");
1648 * Allocate room for and instantiate Stored Measurement Log (SML)
1650 static void __init prom_instantiate_sml(void)
1652 phandle ibmvtpm_node;
1653 ihandle ibmvtpm_inst;
1654 u32 entry = 0, size = 0, succ = 0;
1658 prom_debug("prom_instantiate_sml: start...\n");
1660 ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
1661 prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
1662 if (!PHANDLE_VALID(ibmvtpm_node))
1665 ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
1666 if (!IHANDLE_VALID(ibmvtpm_inst)) {
1667 prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
1671 if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1672 &val, sizeof(val)) != PROM_ERROR) {
1673 if (call_prom_ret("call-method", 2, 2, &succ,
1674 ADDR("reformat-sml-to-efi-alignment"),
1675 ibmvtpm_inst) != 0 || succ == 0) {
1676 prom_printf("Reformat SML to EFI alignment failed\n");
1680 if (call_prom_ret("call-method", 2, 2, &size,
1681 ADDR("sml-get-allocated-size"),
1682 ibmvtpm_inst) != 0 || size == 0) {
1683 prom_printf("SML get allocated size failed\n");
1687 if (call_prom_ret("call-method", 2, 2, &size,
1688 ADDR("sml-get-handover-size"),
1689 ibmvtpm_inst) != 0 || size == 0) {
1690 prom_printf("SML get handover size failed\n");
1695 base = alloc_down(size, PAGE_SIZE, 0);
1697 prom_panic("Could not allocate memory for sml\n");
1699 prom_printf("instantiating sml at 0x%llx...", base);
1701 memset((void *)base, 0, size);
1703 if (call_prom_ret("call-method", 4, 2, &entry,
1704 ADDR("sml-handover"),
1705 ibmvtpm_inst, size, base) != 0 || entry == 0) {
1706 prom_printf("SML handover failed\n");
1709 prom_printf(" done\n");
1711 reserve_mem(base, size);
1713 prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
1714 &base, sizeof(base));
1715 prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
1716 &size, sizeof(size));
1718 prom_debug("sml base = 0x%llx\n", base);
1719 prom_debug("sml size = 0x%x\n", size);
1721 prom_debug("prom_instantiate_sml: end...\n");
1725 * Allocate room for and initialize TCE tables
1727 #ifdef __BIG_ENDIAN__
1728 static void __init prom_initialize_tce_table(void)
1732 char compatible[64], type[64], model[64];
1733 char *path = prom_scratch;
1735 u32 minalign, minsize;
1736 u64 tce_entry, *tce_entryp;
1737 u64 local_alloc_top, local_alloc_bottom;
1743 prom_debug("starting prom_initialize_tce_table\n");
1745 /* Cache current top of allocs so we reserve a single block */
1746 local_alloc_top = alloc_top_high;
1747 local_alloc_bottom = local_alloc_top;
1749 /* Search all nodes looking for PHBs. */
1750 for (node = 0; prom_next_node(&node); ) {
1754 prom_getprop(node, "compatible",
1755 compatible, sizeof(compatible));
1756 prom_getprop(node, "device_type", type, sizeof(type));
1757 prom_getprop(node, "model", model, sizeof(model));
1759 if ((type[0] == 0) || (strstr(type, "pci") == NULL))
1762 /* Keep the old logic intact to avoid regression. */
1763 if (compatible[0] != 0) {
1764 if ((strstr(compatible, "python") == NULL) &&
1765 (strstr(compatible, "Speedwagon") == NULL) &&
1766 (strstr(compatible, "Winnipeg") == NULL))
1768 } else if (model[0] != 0) {
1769 if ((strstr(model, "ython") == NULL) &&
1770 (strstr(model, "peedwagon") == NULL) &&
1771 (strstr(model, "innipeg") == NULL))
1775 if (prom_getprop(node, "tce-table-minalign", &minalign,
1776 sizeof(minalign)) == PROM_ERROR)
1778 if (prom_getprop(node, "tce-table-minsize", &minsize,
1779 sizeof(minsize)) == PROM_ERROR)
1780 minsize = 4UL << 20;
1783 * Even though we read what OF wants, we just set the table
1784 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
1785 * By doing this, we avoid the pitfalls of trying to DMA to
1786 * MMIO space and the DMA alias hole.
1788 minsize = 4UL << 20;
1790 /* Align to the greater of the align or size */
1791 align = max(minalign, minsize);
1792 base = alloc_down(minsize, align, 1);
1794 prom_panic("ERROR, cannot find space for TCE table.\n");
1795 if (base < local_alloc_bottom)
1796 local_alloc_bottom = base;
1798 /* It seems OF doesn't null-terminate the path :-( */
1799 memset(path, 0, PROM_SCRATCH_SIZE);
1800 /* Call OF to setup the TCE hardware */
1801 if (call_prom("package-to-path", 3, 1, node,
1802 path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1803 prom_printf("package-to-path failed\n");
1806 /* Save away the TCE table attributes for later use. */
1807 prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1808 prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1810 prom_debug("TCE table: %s\n", path);
1811 prom_debug("\tnode = 0x%x\n", node);
1812 prom_debug("\tbase = 0x%llx\n", base);
1813 prom_debug("\tsize = 0x%x\n", minsize);
1815 /* Initialize the table to have a one-to-one mapping
1816 * over the allocated size.
1818 tce_entryp = (u64 *)base;
1819 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1820 tce_entry = (i << PAGE_SHIFT);
1822 *tce_entryp = tce_entry;
1825 prom_printf("opening PHB %s", path);
1826 phb_node = call_prom("open", 1, 1, path);
1828 prom_printf("... failed\n");
1830 prom_printf("... done\n");
1832 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1833 phb_node, -1, minsize,
1834 (u32) base, (u32) (base >> 32));
1835 call_prom("close", 1, 0, phb_node);
1838 reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1840 /* These are only really needed if there is a memory limit in
1841 * effect, but we don't know so export them always. */
1842 prom_tce_alloc_start = local_alloc_bottom;
1843 prom_tce_alloc_end = local_alloc_top;
1845 /* Flag the first invalid entry */
1846 prom_debug("ending prom_initialize_tce_table\n");
1848 #endif /* __BIG_ENDIAN__ */
1849 #endif /* CONFIG_PPC64 */
1852 * With CHRP SMP we need to use the OF to start the other processors.
1853 * We can't wait until smp_boot_cpus (the OF is trashed by then)
1854 * so we have to put the processors into a holding pattern controlled
1855 * by the kernel (not OF) before we destroy the OF.
1857 * This uses a chunk of low memory, puts some holding pattern
1858 * code there and sends the other processors off to there until
1859 * smp_boot_cpus tells them to do something. The holding pattern
1860 * checks that address until its cpu # is there, when it is that
1861 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
1862 * of setting those values.
1864 * We also use physical address 0x4 here to tell when a cpu
1865 * is in its holding pattern code.
1870 * We want to reference the copy of __secondary_hold_* in the
1871 * 0 - 0x100 address range
1873 #define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
1875 static void __init prom_hold_cpus(void)
1880 unsigned long *spinloop
1881 = (void *) LOW_ADDR(__secondary_hold_spinloop);
1882 unsigned long *acknowledge
1883 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
1884 unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
1887 * On pseries, if RTAS supports "query-cpu-stopped-state",
1888 * we skip this stage, the CPUs will be started by the
1889 * kernel using RTAS.
1891 if ((of_platform == PLATFORM_PSERIES ||
1892 of_platform == PLATFORM_PSERIES_LPAR) &&
1893 rtas_has_query_cpu_stopped) {
1894 prom_printf("prom_hold_cpus: skipped\n");
1898 prom_debug("prom_hold_cpus: start...\n");
1899 prom_debug(" 1) spinloop = 0x%lx\n", (unsigned long)spinloop);
1900 prom_debug(" 1) *spinloop = 0x%lx\n", *spinloop);
1901 prom_debug(" 1) acknowledge = 0x%lx\n",
1902 (unsigned long)acknowledge);
1903 prom_debug(" 1) *acknowledge = 0x%lx\n", *acknowledge);
1904 prom_debug(" 1) secondary_hold = 0x%lx\n", secondary_hold);
1906 /* Set the common spinloop variable, so all of the secondary cpus
1907 * will block when they are awakened from their OF spinloop.
1908 * This must occur for both SMP and non SMP kernels, since OF will
1909 * be trashed when we move the kernel.
1914 for (node = 0; prom_next_node(&node); ) {
1915 unsigned int cpu_no;
1919 prom_getprop(node, "device_type", type, sizeof(type));
1920 if (strcmp(type, "cpu") != 0)
1923 /* Skip non-configured cpus. */
1924 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1925 if (strcmp(type, "okay") != 0)
1928 reg = cpu_to_be32(-1); /* make sparse happy */
1929 prom_getprop(node, "reg", ®, sizeof(reg));
1930 cpu_no = be32_to_cpu(reg);
1932 prom_debug("cpu hw idx = %u\n", cpu_no);
1934 /* Init the acknowledge var which will be reset by
1935 * the secondary cpu when it awakens from its OF
1938 *acknowledge = (unsigned long)-1;
1940 if (cpu_no != prom.cpu) {
1941 /* Primary Thread of non-boot cpu or any thread */
1942 prom_printf("starting cpu hw idx %u... ", cpu_no);
1943 call_prom("start-cpu", 3, 0, node,
1944 secondary_hold, cpu_no);
1946 for (i = 0; (i < 100000000) &&
1947 (*acknowledge == ((unsigned long)-1)); i++ )
1950 if (*acknowledge == cpu_no)
1951 prom_printf("done\n");
1953 prom_printf("failed: %lx\n", *acknowledge);
1957 prom_printf("boot cpu hw idx %u\n", cpu_no);
1958 #endif /* CONFIG_SMP */
1961 prom_debug("prom_hold_cpus: end...\n");
1965 static void __init prom_init_client_services(unsigned long pp)
1967 /* Get a handle to the prom entry point before anything else */
1970 /* get a handle for the stdout device */
1971 prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1972 if (!PHANDLE_VALID(prom.chosen))
1973 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1975 /* get device tree root */
1976 prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
1977 if (!PHANDLE_VALID(prom.root))
1978 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1985 * For really old powermacs, we need to map things we claim.
1986 * For that, we need the ihandle of the mmu.
1987 * Also, on the longtrail, we need to work around other bugs.
1989 static void __init prom_find_mmu(void)
1994 oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1995 if (!PHANDLE_VALID(oprom))
1997 if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1999 version[sizeof(version) - 1] = 0;
2000 /* XXX might need to add other versions here */
2001 if (strcmp(version, "Open Firmware, 1.0.5") == 0)
2002 of_workarounds = OF_WA_CLAIM;
2003 else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
2004 of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
2005 call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2008 prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
2009 prom_getprop(prom.chosen, "mmu", &prom.mmumap,
2010 sizeof(prom.mmumap));
2011 prom.mmumap = be32_to_cpu(prom.mmumap);
2012 if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2013 of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
2016 #define prom_find_mmu()
2019 static void __init prom_init_stdout(void)
2021 char *path = of_stdout_device;
2023 phandle stdout_node;
2026 if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
2027 prom_panic("cannot find stdout");
2029 prom.stdout = be32_to_cpu(val);
2031 /* Get the full OF pathname of the stdout device */
2032 memset(path, 0, 256);
2033 call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
2034 prom_printf("OF stdout device is: %s\n", of_stdout_device);
2035 prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2036 path, strlen(path) + 1);
2038 /* instance-to-package fails on PA-Semi */
2039 stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
2040 if (stdout_node != PROM_ERROR) {
2041 val = cpu_to_be32(stdout_node);
2043 /* If it's a display, note it */
2044 memset(type, 0, sizeof(type));
2045 prom_getprop(stdout_node, "device_type", type, sizeof(type));
2046 if (strcmp(type, "display") == 0)
2047 prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
2051 static int __init prom_find_machine_type(void)
2060 /* Look for a PowerMac or a Cell */
2061 len = prom_getprop(prom.root, "compatible",
2062 compat, sizeof(compat)-1);
2066 char *p = &compat[i];
2070 if (strstr(p, "Power Macintosh") ||
2071 strstr(p, "MacRISC"))
2072 return PLATFORM_POWERMAC;
2074 /* We must make sure we don't detect the IBM Cell
2075 * blades as pSeries due to some firmware issues,
2078 if (strstr(p, "IBM,CBEA") ||
2079 strstr(p, "IBM,CPBW-1.0"))
2080 return PLATFORM_GENERIC;
2081 #endif /* CONFIG_PPC64 */
2086 /* Try to figure out if it's an IBM pSeries or any other
2087 * PAPR compliant platform. We assume it is if :
2088 * - /device_type is "chrp" (please, do NOT use that for future
2092 len = prom_getprop(prom.root, "device_type",
2093 compat, sizeof(compat)-1);
2095 return PLATFORM_GENERIC;
2096 if (strcmp(compat, "chrp"))
2097 return PLATFORM_GENERIC;
2099 /* Default to pSeries. We need to know if we are running LPAR */
2100 rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2101 if (!PHANDLE_VALID(rtas))
2102 return PLATFORM_GENERIC;
2103 x = prom_getproplen(rtas, "ibm,hypertas-functions");
2104 if (x != PROM_ERROR) {
2105 prom_debug("Hypertas detected, assuming LPAR !\n");
2106 return PLATFORM_PSERIES_LPAR;
2108 return PLATFORM_PSERIES;
2110 return PLATFORM_GENERIC;
2114 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
2116 return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
2120 * If we have a display that we don't know how to drive,
2121 * we will want to try to execute OF's open method for it
2122 * later. However, OF will probably fall over if we do that
2123 * we've taken over the MMU.
2124 * So we check whether we will need to open the display,
2125 * and if so, open it now.
2127 static void __init prom_check_displays(void)
2129 char type[16], *path;
2134 static const unsigned char default_colors[] __initconst = {
2152 const unsigned char *clut;
2154 prom_debug("Looking for displays\n");
2155 for (node = 0; prom_next_node(&node); ) {
2156 memset(type, 0, sizeof(type));
2157 prom_getprop(node, "device_type", type, sizeof(type));
2158 if (strcmp(type, "display") != 0)
2161 /* It seems OF doesn't null-terminate the path :-( */
2162 path = prom_scratch;
2163 memset(path, 0, PROM_SCRATCH_SIZE);
2166 * leave some room at the end of the path for appending extra
2169 if (call_prom("package-to-path", 3, 1, node, path,
2170 PROM_SCRATCH_SIZE-10) == PROM_ERROR)
2172 prom_printf("found display : %s, opening... ", path);
2174 ih = call_prom("open", 1, 1, path);
2176 prom_printf("failed\n");
2181 prom_printf("done\n");
2182 prom_setprop(node, path, "linux,opened", NULL, 0);
2184 /* Setup a usable color table when the appropriate
2185 * method is available. Should update this to set-colors */
2186 clut = default_colors;
2187 for (i = 0; i < 16; i++, clut += 3)
2188 if (prom_set_color(ih, i, clut[0], clut[1],
2192 #ifdef CONFIG_LOGO_LINUX_CLUT224
2193 clut = PTRRELOC(logo_linux_clut224.clut);
2194 for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
2195 if (prom_set_color(ih, i + 32, clut[0], clut[1],
2198 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
2200 #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
2201 if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
2203 u32 width, height, pitch, addr;
2205 prom_printf("Setting btext !\n");
2206 prom_getprop(node, "width", &width, 4);
2207 prom_getprop(node, "height", &height, 4);
2208 prom_getprop(node, "linebytes", &pitch, 4);
2209 prom_getprop(node, "address", &addr, 4);
2210 prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
2211 width, height, pitch, addr);
2212 btext_setup_display(width, height, 8, pitch, addr);
2214 #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
2219 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
2220 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
2221 unsigned long needed, unsigned long align)
2225 *mem_start = _ALIGN(*mem_start, align);
2226 while ((*mem_start + needed) > *mem_end) {
2227 unsigned long room, chunk;
2229 prom_debug("Chunk exhausted, claiming more at %lx...\n",
2231 room = alloc_top - alloc_bottom;
2232 if (room > DEVTREE_CHUNK_SIZE)
2233 room = DEVTREE_CHUNK_SIZE;
2234 if (room < PAGE_SIZE)
2235 prom_panic("No memory for flatten_device_tree "
2237 chunk = alloc_up(room, 0);
2239 prom_panic("No memory for flatten_device_tree "
2240 "(claim failed)\n");
2241 *mem_end = chunk + room;
2244 ret = (void *)*mem_start;
2245 *mem_start += needed;
2250 #define dt_push_token(token, mem_start, mem_end) do { \
2251 void *room = make_room(mem_start, mem_end, 4, 4); \
2252 *(__be32 *)room = cpu_to_be32(token); \
2255 static unsigned long __init dt_find_string(char *str)
2259 s = os = (char *)dt_string_start;
2261 while (s < (char *)dt_string_end) {
2262 if (strcmp(s, str) == 0)
2270 * The Open Firmware 1275 specification states properties must be 31 bytes or
2271 * less, however not all firmwares obey this. Make it 64 bytes to be safe.
2273 #define MAX_PROPERTY_NAME 64
2275 static void __init scan_dt_build_strings(phandle node,
2276 unsigned long *mem_start,
2277 unsigned long *mem_end)
2279 char *prev_name, *namep, *sstart;
2283 sstart = (char *)dt_string_start;
2285 /* get and store all property names */
2288 /* 64 is max len of name including nul. */
2289 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
2290 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
2291 /* No more nodes: unwind alloc */
2292 *mem_start = (unsigned long)namep;
2297 if (strcmp(namep, "name") == 0) {
2298 *mem_start = (unsigned long)namep;
2302 /* get/create string entry */
2303 soff = dt_find_string(namep);
2305 *mem_start = (unsigned long)namep;
2306 namep = sstart + soff;
2308 /* Trim off some if we can */
2309 *mem_start = (unsigned long)namep + strlen(namep) + 1;
2310 dt_string_end = *mem_start;
2315 /* do all our children */
2316 child = call_prom("child", 1, 1, node);
2317 while (child != 0) {
2318 scan_dt_build_strings(child, mem_start, mem_end);
2319 child = call_prom("peer", 1, 1, child);
2323 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
2324 unsigned long *mem_end)
2327 char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
2329 unsigned char *valp;
2330 static char pname[MAX_PROPERTY_NAME] __prombss;
2331 int l, room, has_phandle = 0;
2333 dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
2335 /* get the node's full name */
2336 namep = (char *)*mem_start;
2337 room = *mem_end - *mem_start;
2340 l = call_prom("package-to-path", 3, 1, node, namep, room);
2342 /* Didn't fit? Get more room. */
2344 if (l >= *mem_end - *mem_start)
2345 namep = make_room(mem_start, mem_end, l+1, 1);
2346 call_prom("package-to-path", 3, 1, node, namep, l);
2350 /* Fixup an Apple bug where they have bogus \0 chars in the
2351 * middle of the path in some properties, and extract
2352 * the unit name (everything after the last '/').
2354 for (lp = p = namep, ep = namep + l; p < ep; p++) {
2361 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
2364 /* get it again for debugging */
2365 path = prom_scratch;
2366 memset(path, 0, PROM_SCRATCH_SIZE);
2367 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
2369 /* get and store all properties */
2371 sstart = (char *)dt_string_start;
2373 if (call_prom("nextprop", 3, 1, node, prev_name,
2378 if (strcmp(pname, "name") == 0) {
2383 /* find string offset */
2384 soff = dt_find_string(pname);
2386 prom_printf("WARNING: Can't find string index for"
2387 " <%s>, node %s\n", pname, path);
2390 prev_name = sstart + soff;
2393 l = call_prom("getproplen", 2, 1, node, pname);
2396 if (l == PROM_ERROR)
2399 /* push property head */
2400 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2401 dt_push_token(l, mem_start, mem_end);
2402 dt_push_token(soff, mem_start, mem_end);
2404 /* push property content */
2405 valp = make_room(mem_start, mem_end, l, 4);
2406 call_prom("getprop", 4, 1, node, pname, valp, l);
2407 *mem_start = _ALIGN(*mem_start, 4);
2409 if (!strcmp(pname, "phandle"))
2413 /* Add a "phandle" property if none already exist */
2415 soff = dt_find_string("phandle");
2417 prom_printf("WARNING: Can't find string index for <phandle> node %s\n", path);
2419 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2420 dt_push_token(4, mem_start, mem_end);
2421 dt_push_token(soff, mem_start, mem_end);
2422 valp = make_room(mem_start, mem_end, 4, 4);
2423 *(__be32 *)valp = cpu_to_be32(node);
2427 /* do all our children */
2428 child = call_prom("child", 1, 1, node);
2429 while (child != 0) {
2430 scan_dt_build_struct(child, mem_start, mem_end);
2431 child = call_prom("peer", 1, 1, child);
2434 dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
2437 static void __init flatten_device_tree(void)
2440 unsigned long mem_start, mem_end, room;
2441 struct boot_param_header *hdr;
2446 * Check how much room we have between alloc top & bottom (+/- a
2447 * few pages), crop to 1MB, as this is our "chunk" size
2449 room = alloc_top - alloc_bottom - 0x4000;
2450 if (room > DEVTREE_CHUNK_SIZE)
2451 room = DEVTREE_CHUNK_SIZE;
2452 prom_debug("starting device tree allocs at %lx\n", alloc_bottom);
2454 /* Now try to claim that */
2455 mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
2457 prom_panic("Can't allocate initial device-tree chunk\n");
2458 mem_end = mem_start + room;
2460 /* Get root of tree */
2461 root = call_prom("peer", 1, 1, (phandle)0);
2462 if (root == (phandle)0)
2463 prom_panic ("couldn't get device tree root\n");
2465 /* Build header and make room for mem rsv map */
2466 mem_start = _ALIGN(mem_start, 4);
2467 hdr = make_room(&mem_start, &mem_end,
2468 sizeof(struct boot_param_header), 4);
2469 dt_header_start = (unsigned long)hdr;
2470 rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
2472 /* Start of strings */
2473 mem_start = PAGE_ALIGN(mem_start);
2474 dt_string_start = mem_start;
2475 mem_start += 4; /* hole */
2477 /* Add "phandle" in there, we'll need it */
2478 namep = make_room(&mem_start, &mem_end, 16, 1);
2479 strcpy(namep, "phandle");
2480 mem_start = (unsigned long)namep + strlen(namep) + 1;
2482 /* Build string array */
2483 prom_printf("Building dt strings...\n");
2484 scan_dt_build_strings(root, &mem_start, &mem_end);
2485 dt_string_end = mem_start;
2487 /* Build structure */
2488 mem_start = PAGE_ALIGN(mem_start);
2489 dt_struct_start = mem_start;
2490 prom_printf("Building dt structure...\n");
2491 scan_dt_build_struct(root, &mem_start, &mem_end);
2492 dt_push_token(OF_DT_END, &mem_start, &mem_end);
2493 dt_struct_end = PAGE_ALIGN(mem_start);
2496 hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2497 hdr->magic = cpu_to_be32(OF_DT_HEADER);
2498 hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2499 hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2500 hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2501 hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2502 hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2503 hdr->version = cpu_to_be32(OF_DT_VERSION);
2504 /* Version 16 is not backward compatible */
2505 hdr->last_comp_version = cpu_to_be32(0x10);
2507 /* Copy the reserve map in */
2508 memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
2513 prom_printf("reserved memory map:\n");
2514 for (i = 0; i < mem_reserve_cnt; i++)
2515 prom_printf(" %llx - %llx\n",
2516 be64_to_cpu(mem_reserve_map[i].base),
2517 be64_to_cpu(mem_reserve_map[i].size));
2520 /* Bump mem_reserve_cnt to cause further reservations to fail
2521 * since it's too late.
2523 mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
2525 prom_printf("Device tree strings 0x%lx -> 0x%lx\n",
2526 dt_string_start, dt_string_end);
2527 prom_printf("Device tree struct 0x%lx -> 0x%lx\n",
2528 dt_struct_start, dt_struct_end);
2531 #ifdef CONFIG_PPC_MAPLE
2532 /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2533 * The values are bad, and it doesn't even have the right number of cells. */
2534 static void __init fixup_device_tree_maple(void)
2537 u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
2541 name = "/ht@0/isa@4";
2542 isa = call_prom("finddevice", 1, 1, ADDR(name));
2543 if (!PHANDLE_VALID(isa)) {
2544 name = "/ht@0/isa@6";
2545 isa = call_prom("finddevice", 1, 1, ADDR(name));
2546 rloc = 0x01003000; /* IO space; PCI device = 6 */
2548 if (!PHANDLE_VALID(isa))
2551 if (prom_getproplen(isa, "ranges") != 12)
2553 if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2557 if (isa_ranges[0] != 0x1 ||
2558 isa_ranges[1] != 0xf4000000 ||
2559 isa_ranges[2] != 0x00010000)
2562 prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
2564 isa_ranges[0] = 0x1;
2565 isa_ranges[1] = 0x0;
2566 isa_ranges[2] = rloc;
2567 isa_ranges[3] = 0x0;
2568 isa_ranges[4] = 0x0;
2569 isa_ranges[5] = 0x00010000;
2570 prom_setprop(isa, name, "ranges",
2571 isa_ranges, sizeof(isa_ranges));
2574 #define CPC925_MC_START 0xf8000000
2575 #define CPC925_MC_LENGTH 0x1000000
2576 /* The values for memory-controller don't have right number of cells */
2577 static void __init fixup_device_tree_maple_memory_controller(void)
2581 char *name = "/hostbridge@f8000000";
2584 mc = call_prom("finddevice", 1, 1, ADDR(name));
2585 if (!PHANDLE_VALID(mc))
2588 if (prom_getproplen(mc, "reg") != 8)
2591 prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
2592 prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
2593 if ((ac != 2) || (sc != 2))
2596 if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
2599 if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
2602 prom_printf("Fixing up bogus hostbridge on Maple...\n");
2605 mc_reg[1] = CPC925_MC_START;
2607 mc_reg[3] = CPC925_MC_LENGTH;
2608 prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
2611 #define fixup_device_tree_maple()
2612 #define fixup_device_tree_maple_memory_controller()
2615 #ifdef CONFIG_PPC_CHRP
2617 * Pegasos and BriQ lacks the "ranges" property in the isa node
2618 * Pegasos needs decimal IRQ 14/15, not hexadecimal
2619 * Pegasos has the IDE configured in legacy mode, but advertised as native
2621 static void __init fixup_device_tree_chrp(void)
2625 u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2629 name = "/pci@80000000/isa@c";
2630 ph = call_prom("finddevice", 1, 1, ADDR(name));
2631 if (!PHANDLE_VALID(ph)) {
2632 name = "/pci@ff500000/isa@6";
2633 ph = call_prom("finddevice", 1, 1, ADDR(name));
2634 rloc = 0x01003000; /* IO space; PCI device = 6 */
2636 if (PHANDLE_VALID(ph)) {
2637 rc = prom_getproplen(ph, "ranges");
2638 if (rc == 0 || rc == PROM_ERROR) {
2639 prom_printf("Fixing up missing ISA range on Pegasos...\n");
2646 prop[5] = 0x00010000;
2647 prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2651 name = "/pci@80000000/ide@C,1";
2652 ph = call_prom("finddevice", 1, 1, ADDR(name));
2653 if (PHANDLE_VALID(ph)) {
2654 prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2657 prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2658 prom_printf("Fixing up IDE class-code on Pegasos...\n");
2659 rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2660 if (rc == sizeof(u32)) {
2662 prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2667 #define fixup_device_tree_chrp()
2670 #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
2671 static void __init fixup_device_tree_pmac(void)
2673 phandle u3, i2c, mpic;
2678 /* Some G5s have a missing interrupt definition, fix it up here */
2679 u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2680 if (!PHANDLE_VALID(u3))
2682 i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2683 if (!PHANDLE_VALID(i2c))
2685 mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2686 if (!PHANDLE_VALID(mpic))
2689 /* check if proper rev of u3 */
2690 if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2693 if (u3_rev < 0x35 || u3_rev > 0x39)
2695 /* does it need fixup ? */
2696 if (prom_getproplen(i2c, "interrupts") > 0)
2699 prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2701 /* interrupt on this revision of u3 is number 0 and level */
2704 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2705 &interrupts, sizeof(interrupts));
2707 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2708 &parent, sizeof(parent));
2711 #define fixup_device_tree_pmac()
2714 #ifdef CONFIG_PPC_EFIKA
2716 * The MPC5200 FEC driver requires an phy-handle property to tell it how
2717 * to talk to the phy. If the phy-handle property is missing, then this
2718 * function is called to add the appropriate nodes and link it to the
2721 static void __init fixup_device_tree_efika_add_phy(void)
2727 /* Check if /builtin/ethernet exists - bail if it doesn't */
2728 node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
2729 if (!PHANDLE_VALID(node))
2732 /* Check if the phy-handle property exists - bail if it does */
2733 rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2738 * At this point the ethernet device doesn't have a phy described.
2739 * Now we need to add the missing phy node and linkage
2742 /* Check for an MDIO bus node - if missing then create one */
2743 node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2744 if (!PHANDLE_VALID(node)) {
2745 prom_printf("Adding Ethernet MDIO node\n");
2746 call_prom("interpret", 1, 1,
2747 " s\" /builtin\" find-device"
2749 " 1 encode-int s\" #address-cells\" property"
2750 " 0 encode-int s\" #size-cells\" property"
2751 " s\" mdio\" device-name"
2752 " s\" fsl,mpc5200b-mdio\" encode-string"
2753 " s\" compatible\" property"
2754 " 0xf0003000 0x400 reg"
2756 " 0x5 encode-int encode+"
2757 " 0x3 encode-int encode+"
2758 " s\" interrupts\" property"
2762 /* Check for a PHY device node - if missing then create one and
2763 * give it's phandle to the ethernet node */
2764 node = call_prom("finddevice", 1, 1,
2765 ADDR("/builtin/mdio/ethernet-phy"));
2766 if (!PHANDLE_VALID(node)) {
2767 prom_printf("Adding Ethernet PHY node\n");
2768 call_prom("interpret", 1, 1,
2769 " s\" /builtin/mdio\" find-device"
2771 " s\" ethernet-phy\" device-name"
2772 " 0x10 encode-int s\" reg\" property"
2776 " s\" /builtin/ethernet\" find-device"
2778 " s\" phy-handle\" property"
2783 static void __init fixup_device_tree_efika(void)
2785 int sound_irq[3] = { 2, 2, 0 };
2786 int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2787 3,4,0, 3,5,0, 3,6,0, 3,7,0,
2788 3,8,0, 3,9,0, 3,10,0, 3,11,0,
2789 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2794 /* Check if we're really running on a EFIKA */
2795 node = call_prom("finddevice", 1, 1, ADDR("/"));
2796 if (!PHANDLE_VALID(node))
2799 rv = prom_getprop(node, "model", prop, sizeof(prop));
2800 if (rv == PROM_ERROR)
2802 if (strcmp(prop, "EFIKA5K2"))
2805 prom_printf("Applying EFIKA device tree fixups\n");
2807 /* Claiming to be 'chrp' is death */
2808 node = call_prom("finddevice", 1, 1, ADDR("/"));
2809 rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2810 if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2811 prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2813 /* CODEGEN,description is exposed in /proc/cpuinfo so
2815 rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2816 if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2817 prom_setprop(node, "/", "CODEGEN,description",
2818 "Efika 5200B PowerPC System",
2819 sizeof("Efika 5200B PowerPC System"));
2821 /* Fixup bestcomm interrupts property */
2822 node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2823 if (PHANDLE_VALID(node)) {
2824 len = prom_getproplen(node, "interrupts");
2826 prom_printf("Fixing bestcomm interrupts property\n");
2827 prom_setprop(node, "/builtin/bestcom", "interrupts",
2828 bcomm_irq, sizeof(bcomm_irq));
2832 /* Fixup sound interrupts property */
2833 node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2834 if (PHANDLE_VALID(node)) {
2835 rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2836 if (rv == PROM_ERROR) {
2837 prom_printf("Adding sound interrupts property\n");
2838 prom_setprop(node, "/builtin/sound", "interrupts",
2839 sound_irq, sizeof(sound_irq));
2843 /* Make sure ethernet phy-handle property exists */
2844 fixup_device_tree_efika_add_phy();
2847 #define fixup_device_tree_efika()
2850 #ifdef CONFIG_PPC_PASEMI_NEMO
2852 * CFE supplied on Nemo is broken in several ways, biggest
2853 * problem is that it reassigns ISA interrupts to unused mpic ints.
2854 * Add an interrupt-controller property for the io-bridge to use
2855 * and correct the ints so we can attach them to an irq_domain
2857 static void __init fixup_device_tree_pasemi(void)
2859 u32 interrupts[2], parent, rval, val = 0;
2860 char *name, *pci_name;
2863 /* Find the root pci node */
2864 name = "/pxp@0,e0000000";
2865 iob = call_prom("finddevice", 1, 1, ADDR(name));
2866 if (!PHANDLE_VALID(iob))
2869 /* check if interrupt-controller node set yet */
2870 if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
2873 prom_printf("adding interrupt-controller property for SB600...\n");
2875 prom_setprop(iob, name, "interrupt-controller", &val, 0);
2877 pci_name = "/pxp@0,e0000000/pci@11";
2878 node = call_prom("finddevice", 1, 1, ADDR(pci_name));
2881 for( ; prom_next_node(&node); ) {
2882 /* scan each node for one with an interrupt */
2883 if (!PHANDLE_VALID(node))
2886 rval = prom_getproplen(node, "interrupts");
2887 if (rval == 0 || rval == PROM_ERROR)
2890 prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
2891 if ((interrupts[0] < 212) || (interrupts[0] > 222))
2894 /* found a node, update both interrupts and interrupt-parent */
2895 if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
2896 interrupts[0] -= 203;
2897 if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
2898 interrupts[0] -= 213;
2899 if (interrupts[0] == 221)
2901 if (interrupts[0] == 222)
2904 prom_setprop(node, pci_name, "interrupts", interrupts,
2905 sizeof(interrupts));
2906 prom_setprop(node, pci_name, "interrupt-parent", &parent,
2911 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
2912 * so that generic isa-bridge code can add the SB600 and its on-board
2915 name = "/pxp@0,e0000000/io-bridge@0";
2916 iob = call_prom("finddevice", 1, 1, ADDR(name));
2917 if (!PHANDLE_VALID(iob))
2920 /* device_type is already set, just change it. */
2922 prom_printf("Changing device_type of SB600 node...\n");
2924 prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
2926 #else /* !CONFIG_PPC_PASEMI_NEMO */
2927 static inline void fixup_device_tree_pasemi(void) { }
2930 static void __init fixup_device_tree(void)
2932 fixup_device_tree_maple();
2933 fixup_device_tree_maple_memory_controller();
2934 fixup_device_tree_chrp();
2935 fixup_device_tree_pmac();
2936 fixup_device_tree_efika();
2937 fixup_device_tree_pasemi();
2940 static void __init prom_find_boot_cpu(void)
2947 if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
2949 prom_cpu = be32_to_cpu(rval);
2951 cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2953 if (!PHANDLE_VALID(cpu_pkg))
2956 prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
2957 prom.cpu = be32_to_cpu(rval);
2959 prom_debug("Booting CPU hw index = %d\n", prom.cpu);
2962 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2964 #ifdef CONFIG_BLK_DEV_INITRD
2965 if (r3 && r4 && r4 != 0xdeadbeef) {
2968 prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
2969 prom_initrd_end = prom_initrd_start + r4;
2971 val = cpu_to_be64(prom_initrd_start);
2972 prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
2974 val = cpu_to_be64(prom_initrd_end);
2975 prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
2978 reserve_mem(prom_initrd_start,
2979 prom_initrd_end - prom_initrd_start);
2981 prom_debug("initrd_start=0x%lx\n", prom_initrd_start);
2982 prom_debug("initrd_end=0x%lx\n", prom_initrd_end);
2984 #endif /* CONFIG_BLK_DEV_INITRD */
2988 #ifdef CONFIG_RELOCATABLE
2989 static void reloc_toc(void)
2993 static void unreloc_toc(void)
2997 static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
3000 unsigned long *toc_entry;
3002 /* Get the start of the TOC by using r2 directly. */
3003 asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
3005 for (i = 0; i < nr_entries; i++) {
3006 *toc_entry = *toc_entry + offset;
3011 static void reloc_toc(void)
3013 unsigned long offset = reloc_offset();
3014 unsigned long nr_entries =
3015 (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
3017 __reloc_toc(offset, nr_entries);
3022 static void unreloc_toc(void)
3024 unsigned long offset = reloc_offset();
3025 unsigned long nr_entries =
3026 (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
3030 __reloc_toc(-offset, nr_entries);
3036 * We enter here early on, when the Open Firmware prom is still
3037 * handling exceptions and the MMU hash table for us.
3040 unsigned long __init prom_init(unsigned long r3, unsigned long r4,
3042 unsigned long r6, unsigned long r7,
3043 unsigned long kbase)
3048 unsigned long offset = reloc_offset();
3055 * First zero the BSS
3057 memset(&__bss_start, 0, __bss_stop - __bss_start);
3060 * Init interface to Open Firmware, get some node references,
3063 prom_init_client_services(pp);
3066 * See if this OF is old enough that we need to do explicit maps
3067 * and other workarounds
3072 * Init prom stdout device
3076 prom_printf("Preparing to boot %s", linux_banner);
3079 * Get default machine type. At this point, we do not differentiate
3080 * between pSeries SMP and pSeries LPAR
3082 of_platform = prom_find_machine_type();
3083 prom_printf("Detected machine type: %x\n", of_platform);
3085 #ifndef CONFIG_NONSTATIC_KERNEL
3086 /* Bail if this is a kdump kernel. */
3087 if (PHYSICAL_START > 0)
3088 prom_panic("Error: You can't boot a kdump kernel from OF!\n");
3092 * Check for an initrd
3094 prom_check_initrd(r3, r4);
3097 * Do early parsing of command line
3099 early_cmdline_parse();
3101 #ifdef CONFIG_PPC_PSERIES
3103 * On pSeries, inform the firmware about our capabilities
3105 if (of_platform == PLATFORM_PSERIES ||
3106 of_platform == PLATFORM_PSERIES_LPAR)
3107 prom_send_capabilities();
3111 * Copy the CPU hold code
3113 if (of_platform != PLATFORM_POWERMAC)
3114 copy_and_flush(0, kbase, 0x100, 0);
3117 * Initialize memory management within prom_init
3122 * Determine which cpu is actually running right _now_
3124 prom_find_boot_cpu();
3127 * Initialize display devices
3129 prom_check_displays();
3131 #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
3133 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
3134 * that uses the allocator, we need to make sure we get the top of memory
3135 * available for us here...
3137 if (of_platform == PLATFORM_PSERIES)
3138 prom_initialize_tce_table();
3142 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
3143 * have a usable RTAS implementation.
3145 if (of_platform != PLATFORM_POWERMAC)
3146 prom_instantiate_rtas();
3149 /* instantiate sml */
3150 prom_instantiate_sml();
3154 * On non-powermacs, put all CPUs in spin-loops.
3156 * PowerMacs use a different mechanism to spin CPUs
3158 * (This must be done after instanciating RTAS)
3160 if (of_platform != PLATFORM_POWERMAC)
3164 * Fill in some infos for use by the kernel later on
3166 if (prom_memory_limit) {
3167 __be64 val = cpu_to_be64(prom_memory_limit);
3168 prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3173 prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3176 if (prom_iommu_force_on)
3177 prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3180 if (prom_tce_alloc_start) {
3181 prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
3182 &prom_tce_alloc_start,
3183 sizeof(prom_tce_alloc_start));
3184 prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
3185 &prom_tce_alloc_end,
3186 sizeof(prom_tce_alloc_end));
3191 * Fixup any known bugs in the device-tree
3193 fixup_device_tree();
3196 * Now finally create the flattened device-tree
3198 prom_printf("copying OF device tree...\n");
3199 flatten_device_tree();
3202 * in case stdin is USB and still active on IBM machines...
3203 * Unfortunately quiesce crashes on some powermacs if we have
3204 * closed stdin already (in particular the powerbook 101).
3206 if (of_platform != PLATFORM_POWERMAC)
3210 * Call OF "quiesce" method to shut down pending DMA's from
3213 prom_printf("Quiescing Open Firmware ...\n");
3214 call_prom("quiesce", 0, 0);
3217 * And finally, call the kernel passing it the flattened device
3218 * tree and NULL as r5, thus triggering the new entry point which
3219 * is common to us and kexec
3221 hdr = dt_header_start;
3223 /* Don't print anything after quiesce under OPAL, it crashes OFW */
3224 prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
3225 prom_debug("->dt_header_start=0x%lx\n", hdr);
3228 reloc_got2(-offset);
3233 __start(hdr, kbase, 0, 0, 0, 0, 0);