1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Procedures for interfacing to Open Firmware.
5 * Paul Mackerras August 1996.
6 * Copyright (C) 1996-2005 Paul Mackerras.
8 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
9 * {engebret|bergner}@us.ibm.com
14 /* we cannot use FORTIFY as it brings in new symbols */
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/init.h>
21 #include <linux/threads.h>
22 #include <linux/spinlock.h>
23 #include <linux/types.h>
24 #include <linux/pci.h>
25 #include <linux/proc_fs.h>
26 #include <linux/delay.h>
27 #include <linux/initrd.h>
28 #include <linux/bitops.h>
32 #include <asm/processor.h>
37 #include <asm/pgtable.h>
38 #include <asm/iommu.h>
39 #include <asm/btext.h>
40 #include <asm/sections.h>
41 #include <asm/machdep.h>
42 #include <asm/asm-prototypes.h>
44 #include <linux/linux_logo.h>
46 /* All of prom_init bss lives here */
47 #define __prombss __section(.bss.prominit)
50 * Eventually bump that one up
52 #define DEVTREE_CHUNK_SIZE 0x100000
55 * This is the size of the local memory reserve map that gets copied
56 * into the boot params passed to the kernel. That size is totally
57 * flexible as the kernel just reads the list until it encounters an
58 * entry with size 0, so it can be changed without breaking binary
61 #define MEM_RESERVE_MAP_SIZE 8
64 * prom_init() is called very early on, before the kernel text
65 * and data have been mapped to KERNELBASE. At this point the code
66 * is running at whatever address it has been loaded at.
67 * On ppc32 we compile with -mrelocatable, which means that references
68 * to extern and static variables get relocated automatically.
69 * ppc64 objects are always relocatable, we just need to relocate the
72 * Because OF may have mapped I/O devices into the area starting at
73 * KERNELBASE, particularly on CHRP machines, we can't safely call
74 * OF once the kernel has been mapped to KERNELBASE. Therefore all
75 * OF calls must be done within prom_init().
77 * ADDR is used in calls to call_prom. The 4th and following
78 * arguments to call_prom should be 32-bit values.
79 * On ppc64, 64 bit values are truncated to 32 bits (and
80 * fortunately don't get interpreted as two arguments).
82 #define ADDR(x) (u32)(unsigned long)(x)
85 #define OF_WORKAROUNDS 0
87 #define OF_WORKAROUNDS of_workarounds
88 static int of_workarounds __prombss;
91 #define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */
92 #define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */
94 #define PROM_BUG() do { \
95 prom_printf("kernel BUG at %s line 0x%x!\n", \
96 __FILE__, __LINE__); \
97 __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \
101 #define prom_debug(x...) prom_printf(x)
103 #define prom_debug(x...) do { } while (0)
107 typedef u32 prom_arg_t;
125 struct mem_map_entry {
130 typedef __be32 cell_t;
132 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
133 unsigned long r6, unsigned long r7, unsigned long r8,
137 extern int enter_prom(struct prom_args *args, unsigned long entry);
139 static inline int enter_prom(struct prom_args *args, unsigned long entry)
141 return ((int (*)(struct prom_args *))entry)(args);
145 extern void copy_and_flush(unsigned long dest, unsigned long src,
146 unsigned long size, unsigned long offset);
149 static struct prom_t __prombss prom;
151 static unsigned long __prombss prom_entry;
153 static char __prombss of_stdout_device[256];
154 static char __prombss prom_scratch[256];
156 static unsigned long __prombss dt_header_start;
157 static unsigned long __prombss dt_struct_start, dt_struct_end;
158 static unsigned long __prombss dt_string_start, dt_string_end;
160 static unsigned long __prombss prom_initrd_start, prom_initrd_end;
163 static int __prombss prom_iommu_force_on;
164 static int __prombss prom_iommu_off;
165 static unsigned long __prombss prom_tce_alloc_start;
166 static unsigned long __prombss prom_tce_alloc_end;
169 #ifdef CONFIG_PPC_PSERIES
170 static bool __prombss prom_radix_disable;
173 struct platform_support {
180 /* Platforms codes are now obsolete in the kernel. Now only used within this
181 * file and ultimately gone too. Feel free to change them if you need, they
182 * are not shared with anything outside of this file anymore
184 #define PLATFORM_PSERIES 0x0100
185 #define PLATFORM_PSERIES_LPAR 0x0101
186 #define PLATFORM_LPAR 0x0001
187 #define PLATFORM_POWERMAC 0x0400
188 #define PLATFORM_GENERIC 0x0500
190 static int __prombss of_platform;
192 static char __prombss prom_cmd_line[COMMAND_LINE_SIZE];
194 static unsigned long __prombss prom_memory_limit;
196 static unsigned long __prombss alloc_top;
197 static unsigned long __prombss alloc_top_high;
198 static unsigned long __prombss alloc_bottom;
199 static unsigned long __prombss rmo_top;
200 static unsigned long __prombss ram_top;
202 static struct mem_map_entry __prombss mem_reserve_map[MEM_RESERVE_MAP_SIZE];
203 static int __prombss mem_reserve_cnt;
205 static cell_t __prombss regbuf[1024];
207 static bool __prombss rtas_has_query_cpu_stopped;
211 * Error results ... some OF calls will return "-1" on error, some
212 * will return 0, some will return either. To simplify, here are
213 * macros to use with any ihandle or phandle return value to check if
217 #define PROM_ERROR (-1u)
218 #define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR)
219 #define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR)
221 /* Copied from lib/string.c and lib/kstrtox.c */
223 static int __init prom_strcmp(const char *cs, const char *ct)
225 unsigned char c1, c2;
231 return c1 < c2 ? -1 : 1;
238 static char __init *prom_strcpy(char *dest, const char *src)
242 while ((*dest++ = *src++) != '\0')
247 static int __init prom_strncmp(const char *cs, const char *ct, size_t count)
249 unsigned char c1, c2;
255 return c1 < c2 ? -1 : 1;
263 static size_t __init prom_strlen(const char *s)
267 for (sc = s; *sc != '\0'; ++sc)
272 static int __init prom_memcmp(const void *cs, const void *ct, size_t count)
274 const unsigned char *su1, *su2;
277 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
278 if ((res = *su1 - *su2) != 0)
283 static char __init *prom_strstr(const char *s1, const char *s2)
287 l2 = prom_strlen(s2);
290 l1 = prom_strlen(s1);
293 if (!prom_memcmp(s1, s2, l2))
300 static size_t __init prom_strlcpy(char *dest, const char *src, size_t size)
302 size_t ret = prom_strlen(src);
305 size_t len = (ret >= size) ? size - 1 : ret;
306 memcpy(dest, src, len);
312 #ifdef CONFIG_PPC_PSERIES
313 static int __init prom_strtobool(const char *s, bool *res)
351 /* This is the one and *ONLY* place where we actually call open
355 static int __init call_prom(const char *service, int nargs, int nret, ...)
358 struct prom_args args;
361 args.service = cpu_to_be32(ADDR(service));
362 args.nargs = cpu_to_be32(nargs);
363 args.nret = cpu_to_be32(nret);
365 va_start(list, nret);
366 for (i = 0; i < nargs; i++)
367 args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
370 for (i = 0; i < nret; i++)
371 args.args[nargs+i] = 0;
373 if (enter_prom(&args, prom_entry) < 0)
376 return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
379 static int __init call_prom_ret(const char *service, int nargs, int nret,
380 prom_arg_t *rets, ...)
383 struct prom_args args;
386 args.service = cpu_to_be32(ADDR(service));
387 args.nargs = cpu_to_be32(nargs);
388 args.nret = cpu_to_be32(nret);
390 va_start(list, rets);
391 for (i = 0; i < nargs; i++)
392 args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
395 for (i = 0; i < nret; i++)
396 args.args[nargs+i] = 0;
398 if (enter_prom(&args, prom_entry) < 0)
402 for (i = 1; i < nret; ++i)
403 rets[i-1] = be32_to_cpu(args.args[nargs+i]);
405 return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
409 static void __init prom_print(const char *msg)
413 if (prom.stdout == 0)
416 for (p = msg; *p != 0; p = q) {
417 for (q = p; *q != 0 && *q != '\n'; ++q)
420 call_prom("write", 3, 1, prom.stdout, p, q - p);
424 call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
430 * Both prom_print_hex & prom_print_dec takes an unsigned long as input so that
431 * we do not need __udivdi3 or __umoddi3 on 32bits.
433 static void __init prom_print_hex(unsigned long val)
435 int i, nibbles = sizeof(val)*2;
436 char buf[sizeof(val)*2+1];
438 for (i = nibbles-1; i >= 0; i--) {
439 buf[i] = (val & 0xf) + '0';
441 buf[i] += ('a'-'0'-10);
445 call_prom("write", 3, 1, prom.stdout, buf, nibbles);
448 /* max number of decimal digits in an unsigned long */
450 static void __init prom_print_dec(unsigned long val)
453 char buf[UL_DIGITS+1];
455 for (i = UL_DIGITS-1; i >= 0; i--) {
456 buf[i] = (val % 10) + '0';
461 /* shift stuff down */
462 size = UL_DIGITS - i;
463 call_prom("write", 3, 1, prom.stdout, buf+i, size);
467 static void __init prom_printf(const char *format, ...)
469 const char *p, *q, *s;
475 va_start(args, format);
476 for (p = format; *p != 0; p = q) {
477 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
480 call_prom("write", 3, 1, prom.stdout, p, q - p);
485 call_prom("write", 3, 1, prom.stdout,
499 s = va_arg(args, const char *);
506 v = va_arg(args, unsigned int);
509 v = va_arg(args, unsigned long);
513 v = va_arg(args, unsigned long long);
522 v = va_arg(args, unsigned int);
525 v = va_arg(args, unsigned long);
529 v = va_arg(args, unsigned long long);
538 vs = va_arg(args, int);
541 vs = va_arg(args, long);
545 vs = va_arg(args, long long);
560 static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
564 if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
566 * Old OF requires we claim physical and virtual separately
567 * and then map explicitly (assuming virtual mode)
572 ret = call_prom_ret("call-method", 5, 2, &result,
573 ADDR("claim"), prom.memory,
575 if (ret != 0 || result == -1)
577 ret = call_prom_ret("call-method", 5, 2, &result,
578 ADDR("claim"), prom.mmumap,
581 call_prom("call-method", 4, 1, ADDR("release"),
582 prom.memory, size, virt);
585 /* the 0x12 is M (coherence) + PP == read/write */
586 call_prom("call-method", 6, 1,
587 ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
590 return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
594 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
597 /* Do not call exit because it clears the screen on pmac
598 * it also causes some sort of double-fault on early pmacs */
599 if (of_platform == PLATFORM_POWERMAC)
602 /* ToDo: should put up an SRC here on pSeries */
603 call_prom("exit", 0, 0);
605 for (;;) /* should never get here */
610 static int __init prom_next_node(phandle *nodep)
614 if ((node = *nodep) != 0
615 && (*nodep = call_prom("child", 1, 1, node)) != 0)
617 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
620 if ((node = call_prom("parent", 1, 1, node)) == 0)
622 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
627 static inline int __init prom_getprop(phandle node, const char *pname,
628 void *value, size_t valuelen)
630 return call_prom("getprop", 4, 1, node, ADDR(pname),
631 (u32)(unsigned long) value, (u32) valuelen);
634 static inline int __init prom_getproplen(phandle node, const char *pname)
636 return call_prom("getproplen", 2, 1, node, ADDR(pname));
639 static void add_string(char **str, const char *q)
649 static char *tohex(unsigned int x)
651 static const char digits[] __initconst = "0123456789abcdef";
652 static char result[9] __prombss;
659 result[i] = digits[x & 0xf];
661 } while (x != 0 && i > 0);
665 static int __init prom_setprop(phandle node, const char *nodename,
666 const char *pname, void *value, size_t valuelen)
670 if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
671 return call_prom("setprop", 4, 1, node, ADDR(pname),
672 (u32)(unsigned long) value, (u32) valuelen);
674 /* gah... setprop doesn't work on longtrail, have to use interpret */
676 add_string(&p, "dev");
677 add_string(&p, nodename);
678 add_string(&p, tohex((u32)(unsigned long) value));
679 add_string(&p, tohex(valuelen));
680 add_string(&p, tohex(ADDR(pname)));
681 add_string(&p, tohex(prom_strlen(pname)));
682 add_string(&p, "property");
684 return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
687 /* We can't use the standard versions because of relocation headaches. */
688 #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
689 || ('a' <= (c) && (c) <= 'f') \
690 || ('A' <= (c) && (c) <= 'F'))
692 #define isdigit(c) ('0' <= (c) && (c) <= '9')
693 #define islower(c) ('a' <= (c) && (c) <= 'z')
694 #define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c))
696 static unsigned long prom_strtoul(const char *cp, const char **endp)
698 unsigned long result = 0, base = 10, value;
703 if (toupper(*cp) == 'X') {
709 while (isxdigit(*cp) &&
710 (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
711 result = result * base + value;
721 static unsigned long prom_memparse(const char *ptr, const char **retptr)
723 unsigned long ret = prom_strtoul(ptr, retptr);
727 * We can't use a switch here because GCC *may* generate a
728 * jump table which won't work, because we're not running at
729 * the address we're linked at.
731 if ('G' == **retptr || 'g' == **retptr)
734 if ('M' == **retptr || 'm' == **retptr)
737 if ('K' == **retptr || 'k' == **retptr)
749 * Early parsing of the command line passed to the kernel, used for
750 * "mem=x" and the options that affect the iommu
752 static void __init early_cmdline_parse(void)
759 prom_cmd_line[0] = 0;
761 if ((long)prom.chosen > 0)
762 l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
763 if (IS_ENABLED(CONFIG_CMDLINE_BOOL) && (l <= 0 || p[0] == '\0')) /* dbl check */
764 prom_strlcpy(prom_cmd_line, CONFIG_CMDLINE, sizeof(prom_cmd_line));
765 prom_printf("command line: %s\n", prom_cmd_line);
768 opt = prom_strstr(prom_cmd_line, "iommu=");
770 prom_printf("iommu opt is: %s\n", opt);
772 while (*opt && *opt == ' ')
774 if (!prom_strncmp(opt, "off", 3))
776 else if (!prom_strncmp(opt, "force", 5))
777 prom_iommu_force_on = 1;
780 opt = prom_strstr(prom_cmd_line, "mem=");
783 prom_memory_limit = prom_memparse(opt, (const char **)&opt);
785 /* Align to 16 MB == size of ppc64 large page */
786 prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
790 #ifdef CONFIG_PPC_PSERIES
791 prom_radix_disable = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
792 opt = prom_strstr(prom_cmd_line, "disable_radix");
795 if (*opt && *opt == '=') {
798 if (prom_strtobool(++opt, &val))
799 prom_radix_disable = false;
801 prom_radix_disable = val;
803 prom_radix_disable = true;
805 if (prom_radix_disable)
806 prom_debug("Radix disabled from cmdline\n");
807 #endif /* CONFIG_PPC_PSERIES */
810 #ifdef CONFIG_PPC_PSERIES
812 * The architecture vector has an array of PVR mask/value pairs,
813 * followed by # option vectors - 1, followed by the option vectors.
815 * See prom.h for the definition of the bits specified in the
816 * architecture vector.
819 /* Firmware expects the value to be n - 1, where n is the # of vectors */
820 #define NUM_VECTORS(n) ((n) - 1)
823 * Firmware expects 1 + n - 2, where n is the length of the option vector in
824 * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
826 #define VECTOR_LENGTH(n) (1 + (n) - 2)
828 struct option_vector1 {
834 struct option_vector2 {
848 struct option_vector3 {
853 struct option_vector4 {
858 struct option_vector5 {
870 u8 platform_facilities;
881 struct option_vector6 {
887 struct ibm_arch_vec {
888 struct { u32 mask, val; } pvrs[12];
893 struct option_vector1 vec1;
896 struct option_vector2 vec2;
899 struct option_vector3 vec3;
902 struct option_vector4 vec4;
905 struct option_vector5 vec5;
908 struct option_vector6 vec6;
911 static const struct ibm_arch_vec ibm_architecture_vec_template __initconst = {
914 .mask = cpu_to_be32(0xfffe0000), /* POWER5/POWER5+ */
915 .val = cpu_to_be32(0x003a0000),
918 .mask = cpu_to_be32(0xffff0000), /* POWER6 */
919 .val = cpu_to_be32(0x003e0000),
922 .mask = cpu_to_be32(0xffff0000), /* POWER7 */
923 .val = cpu_to_be32(0x003f0000),
926 .mask = cpu_to_be32(0xffff0000), /* POWER8E */
927 .val = cpu_to_be32(0x004b0000),
930 .mask = cpu_to_be32(0xffff0000), /* POWER8NVL */
931 .val = cpu_to_be32(0x004c0000),
934 .mask = cpu_to_be32(0xffff0000), /* POWER8 */
935 .val = cpu_to_be32(0x004d0000),
938 .mask = cpu_to_be32(0xffff0000), /* POWER9 */
939 .val = cpu_to_be32(0x004e0000),
942 .mask = cpu_to_be32(0xffffffff), /* all 3.00-compliant */
943 .val = cpu_to_be32(0x0f000005),
946 .mask = cpu_to_be32(0xffffffff), /* all 2.07-compliant */
947 .val = cpu_to_be32(0x0f000004),
950 .mask = cpu_to_be32(0xffffffff), /* all 2.06-compliant */
951 .val = cpu_to_be32(0x0f000003),
954 .mask = cpu_to_be32(0xffffffff), /* all 2.05-compliant */
955 .val = cpu_to_be32(0x0f000002),
958 .mask = cpu_to_be32(0xfffffffe), /* all 2.04-compliant and earlier */
959 .val = cpu_to_be32(0x0f000001),
963 .num_vectors = NUM_VECTORS(6),
965 .vec1_len = VECTOR_LENGTH(sizeof(struct option_vector1)),
968 .arch_versions = OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
969 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
970 .arch_versions3 = OV1_PPC_3_00,
973 .vec2_len = VECTOR_LENGTH(sizeof(struct option_vector2)),
974 /* option vector 2: Open Firmware options supported */
976 .byte1 = OV2_REAL_MODE,
978 .real_base = cpu_to_be32(0xffffffff),
979 .real_size = cpu_to_be32(0xffffffff),
980 .virt_base = cpu_to_be32(0xffffffff),
981 .virt_size = cpu_to_be32(0xffffffff),
982 .load_base = cpu_to_be32(0xffffffff),
983 .min_rma = cpu_to_be32(512), /* 512MB min RMA */
984 .min_load = cpu_to_be32(0xffffffff), /* full client load */
985 .min_rma_percent = 0, /* min RMA percentage of total RAM */
986 .max_pft_size = 48, /* max log_2(hash table size) */
989 .vec3_len = VECTOR_LENGTH(sizeof(struct option_vector3)),
990 /* option vector 3: processor options supported */
992 .byte1 = 0, /* don't ignore, don't halt */
993 .byte2 = OV3_FP | OV3_VMX | OV3_DFP,
996 .vec4_len = VECTOR_LENGTH(sizeof(struct option_vector4)),
997 /* option vector 4: IBM PAPR implementation */
999 .byte1 = 0, /* don't halt */
1000 .min_vp_cap = OV4_MIN_ENT_CAP, /* minimum VP entitled capacity */
1003 .vec5_len = VECTOR_LENGTH(sizeof(struct option_vector5)),
1004 /* option vector 5: PAPR/OF options */
1006 .byte1 = 0, /* don't ignore, don't halt */
1007 .byte2 = OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
1008 OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
1009 #ifdef CONFIG_PCI_MSI
1010 /* PCIe/MSI support. Without MSI full PCIe is not supported */
1017 #ifdef CONFIG_PPC_SMLPAR
1018 OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
1022 .associativity = OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
1023 .bin_opts = OV5_FEAT(OV5_RESIZE_HPT) | OV5_FEAT(OV5_HP_EVT),
1024 .micro_checkpoint = 0,
1026 .max_cpus = cpu_to_be32(NR_CPUS), /* number of cores supported */
1029 .platform_facilities = OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) | OV5_FEAT(OV5_PFO_HW_842),
1033 .byte22 = OV5_FEAT(OV5_DRMEM_V2),
1040 /* option vector 6: IBM PAPR hints */
1041 .vec6_len = VECTOR_LENGTH(sizeof(struct option_vector6)),
1044 .secondary_pteg = 0,
1045 .os_name = OV6_LINUX,
1049 static struct ibm_arch_vec __prombss ibm_architecture_vec ____cacheline_aligned;
1051 /* Old method - ELF header with PT_NOTE sections only works on BE */
1052 #ifdef __BIG_ENDIAN__
1053 static const struct fake_elf {
1060 char name[8]; /* "PowerPC" */
1074 char name[24]; /* "IBM,RPA-Client-Config" */
1078 u32 min_rmo_percent;
1086 } fake_elf __initconst = {
1088 .e_ident = { 0x7f, 'E', 'L', 'F',
1089 ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
1090 .e_type = ET_EXEC, /* yeah right */
1091 .e_machine = EM_PPC,
1092 .e_version = EV_CURRENT,
1093 .e_phoff = offsetof(struct fake_elf, phdr),
1094 .e_phentsize = sizeof(Elf32_Phdr),
1100 .p_offset = offsetof(struct fake_elf, chrpnote),
1101 .p_filesz = sizeof(struct chrpnote)
1104 .p_offset = offsetof(struct fake_elf, rpanote),
1105 .p_filesz = sizeof(struct rpanote)
1109 .namesz = sizeof("PowerPC"),
1110 .descsz = sizeof(struct chrpdesc),
1114 .real_mode = ~0U, /* ~0 means "don't care" */
1123 .namesz = sizeof("IBM,RPA-Client-Config"),
1124 .descsz = sizeof(struct rpadesc),
1126 .name = "IBM,RPA-Client-Config",
1129 .min_rmo_size = 64, /* in megabytes */
1130 .min_rmo_percent = 0,
1131 .max_pft_size = 48, /* 2^48 bytes max PFT size */
1138 #endif /* __BIG_ENDIAN__ */
1140 static int __init prom_count_smt_threads(void)
1146 /* Pick up th first CPU node we can find */
1147 for (node = 0; prom_next_node(&node); ) {
1149 prom_getprop(node, "device_type", type, sizeof(type));
1151 if (prom_strcmp(type, "cpu"))
1154 * There is an entry for each smt thread, each entry being
1155 * 4 bytes long. All cpus should have the same number of
1156 * smt threads, so return after finding the first.
1158 plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
1159 if (plen == PROM_ERROR)
1162 prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
1165 if (plen < 1 || plen > 64) {
1166 prom_printf("Threads per core %lu out of bounds, assuming 1\n",
1167 (unsigned long)plen);
1172 prom_debug("No threads found, assuming 1 per core\n");
1178 static void __init prom_parse_mmu_model(u8 val,
1179 struct platform_support *support)
1182 case OV5_FEAT(OV5_MMU_DYNAMIC):
1183 case OV5_FEAT(OV5_MMU_EITHER): /* Either Available */
1184 prom_debug("MMU - either supported\n");
1185 support->radix_mmu = !prom_radix_disable;
1186 support->hash_mmu = true;
1188 case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */
1189 prom_debug("MMU - radix only\n");
1190 if (prom_radix_disable) {
1192 * If we __have__ to do radix, we're better off ignoring
1193 * the command line rather than not booting.
1195 prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
1197 support->radix_mmu = true;
1199 case OV5_FEAT(OV5_MMU_HASH):
1200 prom_debug("MMU - hash only\n");
1201 support->hash_mmu = true;
1204 prom_debug("Unknown mmu support option: 0x%x\n", val);
1209 static void __init prom_parse_xive_model(u8 val,
1210 struct platform_support *support)
1213 case OV5_FEAT(OV5_XIVE_EITHER): /* Either Available */
1214 prom_debug("XIVE - either mode supported\n");
1215 support->xive = true;
1217 case OV5_FEAT(OV5_XIVE_EXPLOIT): /* Only Exploitation mode */
1218 prom_debug("XIVE - exploitation mode supported\n");
1219 support->xive = true;
1221 case OV5_FEAT(OV5_XIVE_LEGACY): /* Only Legacy mode */
1222 prom_debug("XIVE - legacy mode supported\n");
1225 prom_debug("Unknown xive support option: 0x%x\n", val);
1230 static void __init prom_parse_platform_support(u8 index, u8 val,
1231 struct platform_support *support)
1234 case OV5_INDX(OV5_MMU_SUPPORT): /* MMU Model */
1235 prom_parse_mmu_model(val & OV5_FEAT(OV5_MMU_SUPPORT), support);
1237 case OV5_INDX(OV5_RADIX_GTSE): /* Radix Extensions */
1238 if (val & OV5_FEAT(OV5_RADIX_GTSE)) {
1239 prom_debug("Radix - GTSE supported\n");
1240 support->radix_gtse = true;
1243 case OV5_INDX(OV5_XIVE_SUPPORT): /* Interrupt mode */
1244 prom_parse_xive_model(val & OV5_FEAT(OV5_XIVE_SUPPORT),
1250 static void __init prom_check_platform_support(void)
1252 struct platform_support supported = {
1255 .radix_gtse = false,
1258 int prop_len = prom_getproplen(prom.chosen,
1259 "ibm,arch-vec-5-platform-support");
1262 * First copy the architecture vec template
1264 * use memcpy() instead of *vec = *vec_template so that GCC replaces it
1265 * by __memcpy() when KASAN is active
1267 memcpy(&ibm_architecture_vec, &ibm_architecture_vec_template,
1268 sizeof(ibm_architecture_vec));
1273 prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1275 if (prop_len > sizeof(vec))
1276 prom_printf("WARNING: ibm,arch-vec-5-platform-support longer than expected (len: %d)\n",
1278 prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support",
1280 for (i = 0; i < sizeof(vec); i += 2) {
1281 prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2
1284 prom_parse_platform_support(vec[i], vec[i + 1],
1289 if (supported.radix_mmu && supported.radix_gtse &&
1290 IS_ENABLED(CONFIG_PPC_RADIX_MMU)) {
1291 /* Radix preferred - but we require GTSE for now */
1292 prom_debug("Asking for radix with GTSE\n");
1293 ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1294 ibm_architecture_vec.vec5.radix_ext = OV5_FEAT(OV5_RADIX_GTSE);
1295 } else if (supported.hash_mmu) {
1296 /* Default to hash mmu (if we can) */
1297 prom_debug("Asking for hash\n");
1298 ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1300 /* We're probably on a legacy hypervisor */
1301 prom_debug("Assuming legacy hash support\n");
1304 if (supported.xive) {
1305 prom_debug("Asking for XIVE\n");
1306 ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1310 static void __init prom_send_capabilities(void)
1316 /* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1317 prom_check_platform_support();
1319 root = call_prom("open", 1, 1, ADDR("/"));
1321 /* We need to tell the FW about the number of cores we support.
1323 * To do that, we count the number of threads on the first core
1324 * (we assume this is the same for all cores) and use it to
1328 cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1329 prom_printf("Max number of cores passed to firmware: %u (NR_CPUS = %d)\n",
1332 ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1334 /* try calling the ibm,client-architecture-support method */
1335 prom_printf("Calling ibm,client-architecture-support...");
1336 if (call_prom_ret("call-method", 3, 2, &ret,
1337 ADDR("ibm,client-architecture-support"),
1339 ADDR(&ibm_architecture_vec)) == 0) {
1340 /* the call exists... */
1342 prom_printf("\nWARNING: ibm,client-architecture"
1343 "-support call FAILED!\n");
1344 call_prom("close", 1, 0, root);
1345 prom_printf(" done\n");
1348 call_prom("close", 1, 0, root);
1349 prom_printf(" not implemented\n");
1352 #ifdef __BIG_ENDIAN__
1356 /* no ibm,client-architecture-support call, try the old way */
1357 elfloader = call_prom("open", 1, 1,
1358 ADDR("/packages/elf-loader"));
1359 if (elfloader == 0) {
1360 prom_printf("couldn't open /packages/elf-loader\n");
1363 call_prom("call-method", 3, 1, ADDR("process-elf-header"),
1364 elfloader, ADDR(&fake_elf));
1365 call_prom("close", 1, 0, elfloader);
1367 #endif /* __BIG_ENDIAN__ */
1369 #endif /* CONFIG_PPC_PSERIES */
1372 * Memory allocation strategy... our layout is normally:
1374 * at 14Mb or more we have vmlinux, then a gap and initrd. In some
1375 * rare cases, initrd might end up being before the kernel though.
1376 * We assume this won't override the final kernel at 0, we have no
1377 * provision to handle that in this version, but it should hopefully
1380 * alloc_top is set to the top of RMO, eventually shrink down if the
1383 * alloc_bottom is set to the top of kernel/initrd
1385 * from there, allocations are done this way : rtas is allocated
1386 * topmost, and the device-tree is allocated from the bottom. We try
1387 * to grow the device-tree allocation as we progress. If we can't,
1388 * then we fail, we don't currently have a facility to restart
1389 * elsewhere, but that shouldn't be necessary.
1391 * Note that calls to reserve_mem have to be done explicitly, memory
1392 * allocated with either alloc_up or alloc_down isn't automatically
1398 * Allocates memory in the RMO upward from the kernel/initrd
1400 * When align is 0, this is a special case, it means to allocate in place
1401 * at the current location of alloc_bottom or fail (that is basically
1402 * extending the previous allocation). Used for the device-tree flattening
1404 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
1406 unsigned long base = alloc_bottom;
1407 unsigned long addr = 0;
1410 base = _ALIGN_UP(base, align);
1411 prom_debug("%s(%lx, %lx)\n", __func__, size, align);
1413 prom_panic("alloc_up() called with mem not initialized\n");
1416 base = _ALIGN_UP(alloc_bottom, align);
1418 base = alloc_bottom;
1420 for(; (base + size) <= alloc_top;
1421 base = _ALIGN_UP(base + 0x100000, align)) {
1422 prom_debug(" trying: 0x%lx\n\r", base);
1423 addr = (unsigned long)prom_claim(base, size, 0);
1424 if (addr != PROM_ERROR && addr != 0)
1432 alloc_bottom = addr + size;
1434 prom_debug(" -> %lx\n", addr);
1435 prom_debug(" alloc_bottom : %lx\n", alloc_bottom);
1436 prom_debug(" alloc_top : %lx\n", alloc_top);
1437 prom_debug(" alloc_top_hi : %lx\n", alloc_top_high);
1438 prom_debug(" rmo_top : %lx\n", rmo_top);
1439 prom_debug(" ram_top : %lx\n", ram_top);
1445 * Allocates memory downward, either from top of RMO, or if highmem
1446 * is set, from the top of RAM. Note that this one doesn't handle
1447 * failures. It does claim memory if highmem is not set.
1449 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
1452 unsigned long base, addr = 0;
1454 prom_debug("%s(%lx, %lx, %s)\n", __func__, size, align,
1455 highmem ? "(high)" : "(low)");
1457 prom_panic("alloc_down() called with mem not initialized\n");
1460 /* Carve out storage for the TCE table. */
1461 addr = _ALIGN_DOWN(alloc_top_high - size, align);
1462 if (addr <= alloc_bottom)
1464 /* Will we bump into the RMO ? If yes, check out that we
1465 * didn't overlap existing allocations there, if we did,
1466 * we are dead, we must be the first in town !
1468 if (addr < rmo_top) {
1469 /* Good, we are first */
1470 if (alloc_top == rmo_top)
1471 alloc_top = rmo_top = addr;
1475 alloc_top_high = addr;
1479 base = _ALIGN_DOWN(alloc_top - size, align);
1480 for (; base > alloc_bottom;
1481 base = _ALIGN_DOWN(base - 0x100000, align)) {
1482 prom_debug(" trying: 0x%lx\n\r", base);
1483 addr = (unsigned long)prom_claim(base, size, 0);
1484 if (addr != PROM_ERROR && addr != 0)
1493 prom_debug(" -> %lx\n", addr);
1494 prom_debug(" alloc_bottom : %lx\n", alloc_bottom);
1495 prom_debug(" alloc_top : %lx\n", alloc_top);
1496 prom_debug(" alloc_top_hi : %lx\n", alloc_top_high);
1497 prom_debug(" rmo_top : %lx\n", rmo_top);
1498 prom_debug(" ram_top : %lx\n", ram_top);
1504 * Parse a "reg" cell
1506 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
1509 unsigned long r = 0;
1511 /* Ignore more than 2 cells */
1512 while (s > sizeof(unsigned long) / 4) {
1516 r = be32_to_cpu(*p++);
1520 r |= be32_to_cpu(*(p++));
1528 * Very dumb function for adding to the memory reserve list, but
1529 * we don't need anything smarter at this point
1531 * XXX Eventually check for collisions. They should NEVER happen.
1532 * If problems seem to show up, it would be a good start to track
1535 static void __init reserve_mem(u64 base, u64 size)
1537 u64 top = base + size;
1538 unsigned long cnt = mem_reserve_cnt;
1543 /* We need to always keep one empty entry so that we
1544 * have our terminator with "size" set to 0 since we are
1545 * dumb and just copy this entire array to the boot params
1547 base = _ALIGN_DOWN(base, PAGE_SIZE);
1548 top = _ALIGN_UP(top, PAGE_SIZE);
1551 if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
1552 prom_panic("Memory reserve map exhausted !\n");
1553 mem_reserve_map[cnt].base = cpu_to_be64(base);
1554 mem_reserve_map[cnt].size = cpu_to_be64(size);
1555 mem_reserve_cnt = cnt + 1;
1559 * Initialize memory allocation mechanism, parse "memory" nodes and
1560 * obtain that way the top of memory and RMO to setup out local allocator
1562 static void __init prom_init_mem(void)
1575 * We iterate the memory nodes to find
1576 * 1) top of RMO (first node)
1579 val = cpu_to_be32(2);
1580 prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1581 rac = be32_to_cpu(val);
1582 val = cpu_to_be32(1);
1583 prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1584 rsc = be32_to_cpu(val);
1585 prom_debug("root_addr_cells: %x\n", rac);
1586 prom_debug("root_size_cells: %x\n", rsc);
1588 prom_debug("scanning memory:\n");
1590 path = prom_scratch;
1593 for (node = 0; prom_next_node(&node); ) {
1595 prom_getprop(node, "device_type", type, sizeof(type));
1599 * CHRP Longtrail machines have no device_type
1600 * on the memory node, so check the name instead...
1602 prom_getprop(node, "name", type, sizeof(type));
1604 if (prom_strcmp(type, "memory"))
1607 plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
1608 if (plen > sizeof(regbuf)) {
1609 prom_printf("memory node too large for buffer !\n");
1610 plen = sizeof(regbuf);
1613 endp = p + (plen / sizeof(cell_t));
1616 memset(path, 0, sizeof(prom_scratch));
1617 call_prom("package-to-path", 3, 1, node, path, sizeof(prom_scratch) - 1);
1618 prom_debug(" node %s :\n", path);
1619 #endif /* DEBUG_PROM */
1621 while ((endp - p) >= (rac + rsc)) {
1622 unsigned long base, size;
1624 base = prom_next_cell(rac, &p);
1625 size = prom_next_cell(rsc, &p);
1629 prom_debug(" %lx %lx\n", base, size);
1630 if (base == 0 && (of_platform & PLATFORM_LPAR))
1632 if ((base + size) > ram_top)
1633 ram_top = base + size;
1637 alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
1640 * If prom_memory_limit is set we reduce the upper limits *except* for
1641 * alloc_top_high. This must be the real top of RAM so we can put
1645 alloc_top_high = ram_top;
1647 if (prom_memory_limit) {
1648 if (prom_memory_limit <= alloc_bottom) {
1649 prom_printf("Ignoring mem=%lx <= alloc_bottom.\n",
1651 prom_memory_limit = 0;
1652 } else if (prom_memory_limit >= ram_top) {
1653 prom_printf("Ignoring mem=%lx >= ram_top.\n",
1655 prom_memory_limit = 0;
1657 ram_top = prom_memory_limit;
1658 rmo_top = min(rmo_top, prom_memory_limit);
1663 * Setup our top alloc point, that is top of RMO or top of
1664 * segment 0 when running non-LPAR.
1665 * Some RS64 machines have buggy firmware where claims up at
1666 * 1GB fail. Cap at 768MB as a workaround.
1667 * Since 768MB is plenty of room, and we need to cap to something
1668 * reasonable on 32-bit, cap at 768MB on all machines.
1672 rmo_top = min(0x30000000ul, rmo_top);
1673 alloc_top = rmo_top;
1674 alloc_top_high = ram_top;
1677 * Check if we have an initrd after the kernel but still inside
1678 * the RMO. If we do move our bottom point to after it.
1680 if (prom_initrd_start &&
1681 prom_initrd_start < rmo_top &&
1682 prom_initrd_end > alloc_bottom)
1683 alloc_bottom = PAGE_ALIGN(prom_initrd_end);
1685 prom_printf("memory layout at init:\n");
1686 prom_printf(" memory_limit : %lx (16 MB aligned)\n",
1688 prom_printf(" alloc_bottom : %lx\n", alloc_bottom);
1689 prom_printf(" alloc_top : %lx\n", alloc_top);
1690 prom_printf(" alloc_top_hi : %lx\n", alloc_top_high);
1691 prom_printf(" rmo_top : %lx\n", rmo_top);
1692 prom_printf(" ram_top : %lx\n", ram_top);
1695 static void __init prom_close_stdin(void)
1700 if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1701 stdin = be32_to_cpu(val);
1702 call_prom("close", 1, 0, stdin);
1707 * Allocate room for and instantiate RTAS
1709 static void __init prom_instantiate_rtas(void)
1713 u32 base, entry = 0;
1717 prom_debug("prom_instantiate_rtas: start...\n");
1719 rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1720 prom_debug("rtas_node: %x\n", rtas_node);
1721 if (!PHANDLE_VALID(rtas_node))
1725 prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1726 size = be32_to_cpu(val);
1730 base = alloc_down(size, PAGE_SIZE, 0);
1732 prom_panic("Could not allocate memory for RTAS\n");
1734 rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1735 if (!IHANDLE_VALID(rtas_inst)) {
1736 prom_printf("opening rtas package failed (%x)\n", rtas_inst);
1740 prom_printf("instantiating rtas at 0x%x...", base);
1742 if (call_prom_ret("call-method", 3, 2, &entry,
1743 ADDR("instantiate-rtas"),
1744 rtas_inst, base) != 0
1746 prom_printf(" failed\n");
1749 prom_printf(" done\n");
1751 reserve_mem(base, size);
1753 val = cpu_to_be32(base);
1754 prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1756 val = cpu_to_be32(entry);
1757 prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1760 /* Check if it supports "query-cpu-stopped-state" */
1761 if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1762 &val, sizeof(val)) != PROM_ERROR)
1763 rtas_has_query_cpu_stopped = true;
1765 prom_debug("rtas base = 0x%x\n", base);
1766 prom_debug("rtas entry = 0x%x\n", entry);
1767 prom_debug("rtas size = 0x%x\n", size);
1769 prom_debug("prom_instantiate_rtas: end...\n");
1774 * Allocate room for and instantiate Stored Measurement Log (SML)
1776 static void __init prom_instantiate_sml(void)
1778 phandle ibmvtpm_node;
1779 ihandle ibmvtpm_inst;
1780 u32 entry = 0, size = 0, succ = 0;
1784 prom_debug("prom_instantiate_sml: start...\n");
1786 ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
1787 prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
1788 if (!PHANDLE_VALID(ibmvtpm_node))
1791 ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
1792 if (!IHANDLE_VALID(ibmvtpm_inst)) {
1793 prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
1797 if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1798 &val, sizeof(val)) != PROM_ERROR) {
1799 if (call_prom_ret("call-method", 2, 2, &succ,
1800 ADDR("reformat-sml-to-efi-alignment"),
1801 ibmvtpm_inst) != 0 || succ == 0) {
1802 prom_printf("Reformat SML to EFI alignment failed\n");
1806 if (call_prom_ret("call-method", 2, 2, &size,
1807 ADDR("sml-get-allocated-size"),
1808 ibmvtpm_inst) != 0 || size == 0) {
1809 prom_printf("SML get allocated size failed\n");
1813 if (call_prom_ret("call-method", 2, 2, &size,
1814 ADDR("sml-get-handover-size"),
1815 ibmvtpm_inst) != 0 || size == 0) {
1816 prom_printf("SML get handover size failed\n");
1821 base = alloc_down(size, PAGE_SIZE, 0);
1823 prom_panic("Could not allocate memory for sml\n");
1825 prom_printf("instantiating sml at 0x%llx...", base);
1827 memset((void *)base, 0, size);
1829 if (call_prom_ret("call-method", 4, 2, &entry,
1830 ADDR("sml-handover"),
1831 ibmvtpm_inst, size, base) != 0 || entry == 0) {
1832 prom_printf("SML handover failed\n");
1835 prom_printf(" done\n");
1837 reserve_mem(base, size);
1839 prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
1840 &base, sizeof(base));
1841 prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
1842 &size, sizeof(size));
1844 prom_debug("sml base = 0x%llx\n", base);
1845 prom_debug("sml size = 0x%x\n", size);
1847 prom_debug("prom_instantiate_sml: end...\n");
1851 * Allocate room for and initialize TCE tables
1853 #ifdef __BIG_ENDIAN__
1854 static void __init prom_initialize_tce_table(void)
1858 char compatible[64], type[64], model[64];
1859 char *path = prom_scratch;
1861 u32 minalign, minsize;
1862 u64 tce_entry, *tce_entryp;
1863 u64 local_alloc_top, local_alloc_bottom;
1869 prom_debug("starting prom_initialize_tce_table\n");
1871 /* Cache current top of allocs so we reserve a single block */
1872 local_alloc_top = alloc_top_high;
1873 local_alloc_bottom = local_alloc_top;
1875 /* Search all nodes looking for PHBs. */
1876 for (node = 0; prom_next_node(&node); ) {
1880 prom_getprop(node, "compatible",
1881 compatible, sizeof(compatible));
1882 prom_getprop(node, "device_type", type, sizeof(type));
1883 prom_getprop(node, "model", model, sizeof(model));
1885 if ((type[0] == 0) || (prom_strstr(type, "pci") == NULL))
1888 /* Keep the old logic intact to avoid regression. */
1889 if (compatible[0] != 0) {
1890 if ((prom_strstr(compatible, "python") == NULL) &&
1891 (prom_strstr(compatible, "Speedwagon") == NULL) &&
1892 (prom_strstr(compatible, "Winnipeg") == NULL))
1894 } else if (model[0] != 0) {
1895 if ((prom_strstr(model, "ython") == NULL) &&
1896 (prom_strstr(model, "peedwagon") == NULL) &&
1897 (prom_strstr(model, "innipeg") == NULL))
1901 if (prom_getprop(node, "tce-table-minalign", &minalign,
1902 sizeof(minalign)) == PROM_ERROR)
1904 if (prom_getprop(node, "tce-table-minsize", &minsize,
1905 sizeof(minsize)) == PROM_ERROR)
1906 minsize = 4UL << 20;
1909 * Even though we read what OF wants, we just set the table
1910 * size to 4 MB. This is enough to map 2GB of PCI DMA space.
1911 * By doing this, we avoid the pitfalls of trying to DMA to
1912 * MMIO space and the DMA alias hole.
1914 minsize = 4UL << 20;
1916 /* Align to the greater of the align or size */
1917 align = max(minalign, minsize);
1918 base = alloc_down(minsize, align, 1);
1920 prom_panic("ERROR, cannot find space for TCE table.\n");
1921 if (base < local_alloc_bottom)
1922 local_alloc_bottom = base;
1924 /* It seems OF doesn't null-terminate the path :-( */
1925 memset(path, 0, sizeof(prom_scratch));
1926 /* Call OF to setup the TCE hardware */
1927 if (call_prom("package-to-path", 3, 1, node,
1928 path, sizeof(prom_scratch) - 1) == PROM_ERROR) {
1929 prom_printf("package-to-path failed\n");
1932 /* Save away the TCE table attributes for later use. */
1933 prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1934 prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1936 prom_debug("TCE table: %s\n", path);
1937 prom_debug("\tnode = 0x%x\n", node);
1938 prom_debug("\tbase = 0x%llx\n", base);
1939 prom_debug("\tsize = 0x%x\n", minsize);
1941 /* Initialize the table to have a one-to-one mapping
1942 * over the allocated size.
1944 tce_entryp = (u64 *)base;
1945 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1946 tce_entry = (i << PAGE_SHIFT);
1948 *tce_entryp = tce_entry;
1951 prom_printf("opening PHB %s", path);
1952 phb_node = call_prom("open", 1, 1, path);
1954 prom_printf("... failed\n");
1956 prom_printf("... done\n");
1958 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1959 phb_node, -1, minsize,
1960 (u32) base, (u32) (base >> 32));
1961 call_prom("close", 1, 0, phb_node);
1964 reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1966 /* These are only really needed if there is a memory limit in
1967 * effect, but we don't know so export them always. */
1968 prom_tce_alloc_start = local_alloc_bottom;
1969 prom_tce_alloc_end = local_alloc_top;
1971 /* Flag the first invalid entry */
1972 prom_debug("ending prom_initialize_tce_table\n");
1974 #endif /* __BIG_ENDIAN__ */
1975 #endif /* CONFIG_PPC64 */
1978 * With CHRP SMP we need to use the OF to start the other processors.
1979 * We can't wait until smp_boot_cpus (the OF is trashed by then)
1980 * so we have to put the processors into a holding pattern controlled
1981 * by the kernel (not OF) before we destroy the OF.
1983 * This uses a chunk of low memory, puts some holding pattern
1984 * code there and sends the other processors off to there until
1985 * smp_boot_cpus tells them to do something. The holding pattern
1986 * checks that address until its cpu # is there, when it is that
1987 * cpu jumps to __secondary_start(). smp_boot_cpus() takes care
1988 * of setting those values.
1990 * We also use physical address 0x4 here to tell when a cpu
1991 * is in its holding pattern code.
1996 * We want to reference the copy of __secondary_hold_* in the
1997 * 0 - 0x100 address range
1999 #define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff)
2001 static void __init prom_hold_cpus(void)
2006 unsigned long *spinloop
2007 = (void *) LOW_ADDR(__secondary_hold_spinloop);
2008 unsigned long *acknowledge
2009 = (void *) LOW_ADDR(__secondary_hold_acknowledge);
2010 unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
2013 * On pseries, if RTAS supports "query-cpu-stopped-state",
2014 * we skip this stage, the CPUs will be started by the
2015 * kernel using RTAS.
2017 if ((of_platform == PLATFORM_PSERIES ||
2018 of_platform == PLATFORM_PSERIES_LPAR) &&
2019 rtas_has_query_cpu_stopped) {
2020 prom_printf("prom_hold_cpus: skipped\n");
2024 prom_debug("prom_hold_cpus: start...\n");
2025 prom_debug(" 1) spinloop = 0x%lx\n", (unsigned long)spinloop);
2026 prom_debug(" 1) *spinloop = 0x%lx\n", *spinloop);
2027 prom_debug(" 1) acknowledge = 0x%lx\n",
2028 (unsigned long)acknowledge);
2029 prom_debug(" 1) *acknowledge = 0x%lx\n", *acknowledge);
2030 prom_debug(" 1) secondary_hold = 0x%lx\n", secondary_hold);
2032 /* Set the common spinloop variable, so all of the secondary cpus
2033 * will block when they are awakened from their OF spinloop.
2034 * This must occur for both SMP and non SMP kernels, since OF will
2035 * be trashed when we move the kernel.
2040 for (node = 0; prom_next_node(&node); ) {
2041 unsigned int cpu_no;
2045 prom_getprop(node, "device_type", type, sizeof(type));
2046 if (prom_strcmp(type, "cpu") != 0)
2049 /* Skip non-configured cpus. */
2050 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
2051 if (prom_strcmp(type, "okay") != 0)
2054 reg = cpu_to_be32(-1); /* make sparse happy */
2055 prom_getprop(node, "reg", ®, sizeof(reg));
2056 cpu_no = be32_to_cpu(reg);
2058 prom_debug("cpu hw idx = %u\n", cpu_no);
2060 /* Init the acknowledge var which will be reset by
2061 * the secondary cpu when it awakens from its OF
2064 *acknowledge = (unsigned long)-1;
2066 if (cpu_no != prom.cpu) {
2067 /* Primary Thread of non-boot cpu or any thread */
2068 prom_printf("starting cpu hw idx %u... ", cpu_no);
2069 call_prom("start-cpu", 3, 0, node,
2070 secondary_hold, cpu_no);
2072 for (i = 0; (i < 100000000) &&
2073 (*acknowledge == ((unsigned long)-1)); i++ )
2076 if (*acknowledge == cpu_no)
2077 prom_printf("done\n");
2079 prom_printf("failed: %lx\n", *acknowledge);
2083 prom_printf("boot cpu hw idx %u\n", cpu_no);
2084 #endif /* CONFIG_SMP */
2087 prom_debug("prom_hold_cpus: end...\n");
2091 static void __init prom_init_client_services(unsigned long pp)
2093 /* Get a handle to the prom entry point before anything else */
2096 /* get a handle for the stdout device */
2097 prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
2098 if (!PHANDLE_VALID(prom.chosen))
2099 prom_panic("cannot find chosen"); /* msg won't be printed :( */
2101 /* get device tree root */
2102 prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
2103 if (!PHANDLE_VALID(prom.root))
2104 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
2111 * For really old powermacs, we need to map things we claim.
2112 * For that, we need the ihandle of the mmu.
2113 * Also, on the longtrail, we need to work around other bugs.
2115 static void __init prom_find_mmu(void)
2120 oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
2121 if (!PHANDLE_VALID(oprom))
2123 if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
2125 version[sizeof(version) - 1] = 0;
2126 /* XXX might need to add other versions here */
2127 if (prom_strcmp(version, "Open Firmware, 1.0.5") == 0)
2128 of_workarounds = OF_WA_CLAIM;
2129 else if (prom_strncmp(version, "FirmWorks,3.", 12) == 0) {
2130 of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
2131 call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2134 prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
2135 prom_getprop(prom.chosen, "mmu", &prom.mmumap,
2136 sizeof(prom.mmumap));
2137 prom.mmumap = be32_to_cpu(prom.mmumap);
2138 if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2139 of_workarounds &= ~OF_WA_CLAIM; /* hmmm */
2142 #define prom_find_mmu()
2145 static void __init prom_init_stdout(void)
2147 char *path = of_stdout_device;
2149 phandle stdout_node;
2152 if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
2153 prom_panic("cannot find stdout");
2155 prom.stdout = be32_to_cpu(val);
2157 /* Get the full OF pathname of the stdout device */
2158 memset(path, 0, 256);
2159 call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
2160 prom_printf("OF stdout device is: %s\n", of_stdout_device);
2161 prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2162 path, prom_strlen(path) + 1);
2164 /* instance-to-package fails on PA-Semi */
2165 stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
2166 if (stdout_node != PROM_ERROR) {
2167 val = cpu_to_be32(stdout_node);
2169 /* If it's a display, note it */
2170 memset(type, 0, sizeof(type));
2171 prom_getprop(stdout_node, "device_type", type, sizeof(type));
2172 if (prom_strcmp(type, "display") == 0)
2173 prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
2177 static int __init prom_find_machine_type(void)
2186 /* Look for a PowerMac or a Cell */
2187 len = prom_getprop(prom.root, "compatible",
2188 compat, sizeof(compat)-1);
2192 char *p = &compat[i];
2193 int sl = prom_strlen(p);
2196 if (prom_strstr(p, "Power Macintosh") ||
2197 prom_strstr(p, "MacRISC"))
2198 return PLATFORM_POWERMAC;
2200 /* We must make sure we don't detect the IBM Cell
2201 * blades as pSeries due to some firmware issues,
2204 if (prom_strstr(p, "IBM,CBEA") ||
2205 prom_strstr(p, "IBM,CPBW-1.0"))
2206 return PLATFORM_GENERIC;
2207 #endif /* CONFIG_PPC64 */
2212 /* Try to figure out if it's an IBM pSeries or any other
2213 * PAPR compliant platform. We assume it is if :
2214 * - /device_type is "chrp" (please, do NOT use that for future
2218 len = prom_getprop(prom.root, "device_type",
2219 compat, sizeof(compat)-1);
2221 return PLATFORM_GENERIC;
2222 if (prom_strcmp(compat, "chrp"))
2223 return PLATFORM_GENERIC;
2225 /* Default to pSeries. We need to know if we are running LPAR */
2226 rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2227 if (!PHANDLE_VALID(rtas))
2228 return PLATFORM_GENERIC;
2229 x = prom_getproplen(rtas, "ibm,hypertas-functions");
2230 if (x != PROM_ERROR) {
2231 prom_debug("Hypertas detected, assuming LPAR !\n");
2232 return PLATFORM_PSERIES_LPAR;
2234 return PLATFORM_PSERIES;
2236 return PLATFORM_GENERIC;
2240 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
2242 return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
2246 * If we have a display that we don't know how to drive,
2247 * we will want to try to execute OF's open method for it
2248 * later. However, OF will probably fall over if we do that
2249 * we've taken over the MMU.
2250 * So we check whether we will need to open the display,
2251 * and if so, open it now.
2253 static void __init prom_check_displays(void)
2255 char type[16], *path;
2260 static const unsigned char default_colors[] __initconst = {
2278 const unsigned char *clut;
2280 prom_debug("Looking for displays\n");
2281 for (node = 0; prom_next_node(&node); ) {
2282 memset(type, 0, sizeof(type));
2283 prom_getprop(node, "device_type", type, sizeof(type));
2284 if (prom_strcmp(type, "display") != 0)
2287 /* It seems OF doesn't null-terminate the path :-( */
2288 path = prom_scratch;
2289 memset(path, 0, sizeof(prom_scratch));
2292 * leave some room at the end of the path for appending extra
2295 if (call_prom("package-to-path", 3, 1, node, path,
2296 sizeof(prom_scratch) - 10) == PROM_ERROR)
2298 prom_printf("found display : %s, opening... ", path);
2300 ih = call_prom("open", 1, 1, path);
2302 prom_printf("failed\n");
2307 prom_printf("done\n");
2308 prom_setprop(node, path, "linux,opened", NULL, 0);
2310 /* Setup a usable color table when the appropriate
2311 * method is available. Should update this to set-colors */
2312 clut = default_colors;
2313 for (i = 0; i < 16; i++, clut += 3)
2314 if (prom_set_color(ih, i, clut[0], clut[1],
2318 #ifdef CONFIG_LOGO_LINUX_CLUT224
2319 clut = PTRRELOC(logo_linux_clut224.clut);
2320 for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
2321 if (prom_set_color(ih, i + 32, clut[0], clut[1],
2324 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
2326 #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
2327 if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
2329 u32 width, height, pitch, addr;
2331 prom_printf("Setting btext !\n");
2332 prom_getprop(node, "width", &width, 4);
2333 prom_getprop(node, "height", &height, 4);
2334 prom_getprop(node, "linebytes", &pitch, 4);
2335 prom_getprop(node, "address", &addr, 4);
2336 prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
2337 width, height, pitch, addr);
2338 btext_setup_display(width, height, 8, pitch, addr);
2339 btext_prepare_BAT();
2341 #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
2346 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
2347 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
2348 unsigned long needed, unsigned long align)
2352 *mem_start = _ALIGN(*mem_start, align);
2353 while ((*mem_start + needed) > *mem_end) {
2354 unsigned long room, chunk;
2356 prom_debug("Chunk exhausted, claiming more at %lx...\n",
2358 room = alloc_top - alloc_bottom;
2359 if (room > DEVTREE_CHUNK_SIZE)
2360 room = DEVTREE_CHUNK_SIZE;
2361 if (room < PAGE_SIZE)
2362 prom_panic("No memory for flatten_device_tree "
2364 chunk = alloc_up(room, 0);
2366 prom_panic("No memory for flatten_device_tree "
2367 "(claim failed)\n");
2368 *mem_end = chunk + room;
2371 ret = (void *)*mem_start;
2372 *mem_start += needed;
2377 #define dt_push_token(token, mem_start, mem_end) do { \
2378 void *room = make_room(mem_start, mem_end, 4, 4); \
2379 *(__be32 *)room = cpu_to_be32(token); \
2382 static unsigned long __init dt_find_string(char *str)
2386 s = os = (char *)dt_string_start;
2388 while (s < (char *)dt_string_end) {
2389 if (prom_strcmp(s, str) == 0)
2391 s += prom_strlen(s) + 1;
2397 * The Open Firmware 1275 specification states properties must be 31 bytes or
2398 * less, however not all firmwares obey this. Make it 64 bytes to be safe.
2400 #define MAX_PROPERTY_NAME 64
2402 static void __init scan_dt_build_strings(phandle node,
2403 unsigned long *mem_start,
2404 unsigned long *mem_end)
2406 char *prev_name, *namep, *sstart;
2410 sstart = (char *)dt_string_start;
2412 /* get and store all property names */
2415 /* 64 is max len of name including nul. */
2416 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
2417 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
2418 /* No more nodes: unwind alloc */
2419 *mem_start = (unsigned long)namep;
2424 if (prom_strcmp(namep, "name") == 0) {
2425 *mem_start = (unsigned long)namep;
2429 /* get/create string entry */
2430 soff = dt_find_string(namep);
2432 *mem_start = (unsigned long)namep;
2433 namep = sstart + soff;
2435 /* Trim off some if we can */
2436 *mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
2437 dt_string_end = *mem_start;
2442 /* do all our children */
2443 child = call_prom("child", 1, 1, node);
2444 while (child != 0) {
2445 scan_dt_build_strings(child, mem_start, mem_end);
2446 child = call_prom("peer", 1, 1, child);
2450 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
2451 unsigned long *mem_end)
2454 char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
2456 unsigned char *valp;
2457 static char pname[MAX_PROPERTY_NAME] __prombss;
2458 int l, room, has_phandle = 0;
2460 dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
2462 /* get the node's full name */
2463 namep = (char *)*mem_start;
2464 room = *mem_end - *mem_start;
2467 l = call_prom("package-to-path", 3, 1, node, namep, room);
2469 /* Didn't fit? Get more room. */
2471 if (l >= *mem_end - *mem_start)
2472 namep = make_room(mem_start, mem_end, l+1, 1);
2473 call_prom("package-to-path", 3, 1, node, namep, l);
2477 /* Fixup an Apple bug where they have bogus \0 chars in the
2478 * middle of the path in some properties, and extract
2479 * the unit name (everything after the last '/').
2481 for (lp = p = namep, ep = namep + l; p < ep; p++) {
2488 *mem_start = _ALIGN((unsigned long)lp + 1, 4);
2491 /* get it again for debugging */
2492 path = prom_scratch;
2493 memset(path, 0, sizeof(prom_scratch));
2494 call_prom("package-to-path", 3, 1, node, path, sizeof(prom_scratch) - 1);
2496 /* get and store all properties */
2498 sstart = (char *)dt_string_start;
2500 if (call_prom("nextprop", 3, 1, node, prev_name,
2505 if (prom_strcmp(pname, "name") == 0) {
2510 /* find string offset */
2511 soff = dt_find_string(pname);
2513 prom_printf("WARNING: Can't find string index for"
2514 " <%s>, node %s\n", pname, path);
2517 prev_name = sstart + soff;
2520 l = call_prom("getproplen", 2, 1, node, pname);
2523 if (l == PROM_ERROR)
2526 /* push property head */
2527 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2528 dt_push_token(l, mem_start, mem_end);
2529 dt_push_token(soff, mem_start, mem_end);
2531 /* push property content */
2532 valp = make_room(mem_start, mem_end, l, 4);
2533 call_prom("getprop", 4, 1, node, pname, valp, l);
2534 *mem_start = _ALIGN(*mem_start, 4);
2536 if (!prom_strcmp(pname, "phandle"))
2540 /* Add a "phandle" property if none already exist */
2542 soff = dt_find_string("phandle");
2544 prom_printf("WARNING: Can't find string index for <phandle> node %s\n", path);
2546 dt_push_token(OF_DT_PROP, mem_start, mem_end);
2547 dt_push_token(4, mem_start, mem_end);
2548 dt_push_token(soff, mem_start, mem_end);
2549 valp = make_room(mem_start, mem_end, 4, 4);
2550 *(__be32 *)valp = cpu_to_be32(node);
2554 /* do all our children */
2555 child = call_prom("child", 1, 1, node);
2556 while (child != 0) {
2557 scan_dt_build_struct(child, mem_start, mem_end);
2558 child = call_prom("peer", 1, 1, child);
2561 dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
2564 static void __init flatten_device_tree(void)
2567 unsigned long mem_start, mem_end, room;
2568 struct boot_param_header *hdr;
2573 * Check how much room we have between alloc top & bottom (+/- a
2574 * few pages), crop to 1MB, as this is our "chunk" size
2576 room = alloc_top - alloc_bottom - 0x4000;
2577 if (room > DEVTREE_CHUNK_SIZE)
2578 room = DEVTREE_CHUNK_SIZE;
2579 prom_debug("starting device tree allocs at %lx\n", alloc_bottom);
2581 /* Now try to claim that */
2582 mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
2584 prom_panic("Can't allocate initial device-tree chunk\n");
2585 mem_end = mem_start + room;
2587 /* Get root of tree */
2588 root = call_prom("peer", 1, 1, (phandle)0);
2589 if (root == (phandle)0)
2590 prom_panic ("couldn't get device tree root\n");
2592 /* Build header and make room for mem rsv map */
2593 mem_start = _ALIGN(mem_start, 4);
2594 hdr = make_room(&mem_start, &mem_end,
2595 sizeof(struct boot_param_header), 4);
2596 dt_header_start = (unsigned long)hdr;
2597 rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
2599 /* Start of strings */
2600 mem_start = PAGE_ALIGN(mem_start);
2601 dt_string_start = mem_start;
2602 mem_start += 4; /* hole */
2604 /* Add "phandle" in there, we'll need it */
2605 namep = make_room(&mem_start, &mem_end, 16, 1);
2606 prom_strcpy(namep, "phandle");
2607 mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
2609 /* Build string array */
2610 prom_printf("Building dt strings...\n");
2611 scan_dt_build_strings(root, &mem_start, &mem_end);
2612 dt_string_end = mem_start;
2614 /* Build structure */
2615 mem_start = PAGE_ALIGN(mem_start);
2616 dt_struct_start = mem_start;
2617 prom_printf("Building dt structure...\n");
2618 scan_dt_build_struct(root, &mem_start, &mem_end);
2619 dt_push_token(OF_DT_END, &mem_start, &mem_end);
2620 dt_struct_end = PAGE_ALIGN(mem_start);
2623 hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2624 hdr->magic = cpu_to_be32(OF_DT_HEADER);
2625 hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2626 hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2627 hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2628 hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2629 hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2630 hdr->version = cpu_to_be32(OF_DT_VERSION);
2631 /* Version 16 is not backward compatible */
2632 hdr->last_comp_version = cpu_to_be32(0x10);
2634 /* Copy the reserve map in */
2635 memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
2640 prom_printf("reserved memory map:\n");
2641 for (i = 0; i < mem_reserve_cnt; i++)
2642 prom_printf(" %llx - %llx\n",
2643 be64_to_cpu(mem_reserve_map[i].base),
2644 be64_to_cpu(mem_reserve_map[i].size));
2647 /* Bump mem_reserve_cnt to cause further reservations to fail
2648 * since it's too late.
2650 mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
2652 prom_printf("Device tree strings 0x%lx -> 0x%lx\n",
2653 dt_string_start, dt_string_end);
2654 prom_printf("Device tree struct 0x%lx -> 0x%lx\n",
2655 dt_struct_start, dt_struct_end);
2658 #ifdef CONFIG_PPC_MAPLE
2659 /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2660 * The values are bad, and it doesn't even have the right number of cells. */
2661 static void __init fixup_device_tree_maple(void)
2664 u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
2668 name = "/ht@0/isa@4";
2669 isa = call_prom("finddevice", 1, 1, ADDR(name));
2670 if (!PHANDLE_VALID(isa)) {
2671 name = "/ht@0/isa@6";
2672 isa = call_prom("finddevice", 1, 1, ADDR(name));
2673 rloc = 0x01003000; /* IO space; PCI device = 6 */
2675 if (!PHANDLE_VALID(isa))
2678 if (prom_getproplen(isa, "ranges") != 12)
2680 if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2684 if (isa_ranges[0] != 0x1 ||
2685 isa_ranges[1] != 0xf4000000 ||
2686 isa_ranges[2] != 0x00010000)
2689 prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
2691 isa_ranges[0] = 0x1;
2692 isa_ranges[1] = 0x0;
2693 isa_ranges[2] = rloc;
2694 isa_ranges[3] = 0x0;
2695 isa_ranges[4] = 0x0;
2696 isa_ranges[5] = 0x00010000;
2697 prom_setprop(isa, name, "ranges",
2698 isa_ranges, sizeof(isa_ranges));
2701 #define CPC925_MC_START 0xf8000000
2702 #define CPC925_MC_LENGTH 0x1000000
2703 /* The values for memory-controller don't have right number of cells */
2704 static void __init fixup_device_tree_maple_memory_controller(void)
2708 char *name = "/hostbridge@f8000000";
2711 mc = call_prom("finddevice", 1, 1, ADDR(name));
2712 if (!PHANDLE_VALID(mc))
2715 if (prom_getproplen(mc, "reg") != 8)
2718 prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
2719 prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
2720 if ((ac != 2) || (sc != 2))
2723 if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
2726 if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
2729 prom_printf("Fixing up bogus hostbridge on Maple...\n");
2732 mc_reg[1] = CPC925_MC_START;
2734 mc_reg[3] = CPC925_MC_LENGTH;
2735 prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
2738 #define fixup_device_tree_maple()
2739 #define fixup_device_tree_maple_memory_controller()
2742 #ifdef CONFIG_PPC_CHRP
2744 * Pegasos and BriQ lacks the "ranges" property in the isa node
2745 * Pegasos needs decimal IRQ 14/15, not hexadecimal
2746 * Pegasos has the IDE configured in legacy mode, but advertised as native
2748 static void __init fixup_device_tree_chrp(void)
2752 u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2756 name = "/pci@80000000/isa@c";
2757 ph = call_prom("finddevice", 1, 1, ADDR(name));
2758 if (!PHANDLE_VALID(ph)) {
2759 name = "/pci@ff500000/isa@6";
2760 ph = call_prom("finddevice", 1, 1, ADDR(name));
2761 rloc = 0x01003000; /* IO space; PCI device = 6 */
2763 if (PHANDLE_VALID(ph)) {
2764 rc = prom_getproplen(ph, "ranges");
2765 if (rc == 0 || rc == PROM_ERROR) {
2766 prom_printf("Fixing up missing ISA range on Pegasos...\n");
2773 prop[5] = 0x00010000;
2774 prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2778 name = "/pci@80000000/ide@C,1";
2779 ph = call_prom("finddevice", 1, 1, ADDR(name));
2780 if (PHANDLE_VALID(ph)) {
2781 prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2784 prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2785 prom_printf("Fixing up IDE class-code on Pegasos...\n");
2786 rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2787 if (rc == sizeof(u32)) {
2789 prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2794 #define fixup_device_tree_chrp()
2797 #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
2798 static void __init fixup_device_tree_pmac(void)
2800 phandle u3, i2c, mpic;
2805 /* Some G5s have a missing interrupt definition, fix it up here */
2806 u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2807 if (!PHANDLE_VALID(u3))
2809 i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2810 if (!PHANDLE_VALID(i2c))
2812 mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2813 if (!PHANDLE_VALID(mpic))
2816 /* check if proper rev of u3 */
2817 if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2820 if (u3_rev < 0x35 || u3_rev > 0x39)
2822 /* does it need fixup ? */
2823 if (prom_getproplen(i2c, "interrupts") > 0)
2826 prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2828 /* interrupt on this revision of u3 is number 0 and level */
2831 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2832 &interrupts, sizeof(interrupts));
2834 prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2835 &parent, sizeof(parent));
2838 #define fixup_device_tree_pmac()
2841 #ifdef CONFIG_PPC_EFIKA
2843 * The MPC5200 FEC driver requires an phy-handle property to tell it how
2844 * to talk to the phy. If the phy-handle property is missing, then this
2845 * function is called to add the appropriate nodes and link it to the
2848 static void __init fixup_device_tree_efika_add_phy(void)
2854 /* Check if /builtin/ethernet exists - bail if it doesn't */
2855 node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
2856 if (!PHANDLE_VALID(node))
2859 /* Check if the phy-handle property exists - bail if it does */
2860 rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2865 * At this point the ethernet device doesn't have a phy described.
2866 * Now we need to add the missing phy node and linkage
2869 /* Check for an MDIO bus node - if missing then create one */
2870 node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2871 if (!PHANDLE_VALID(node)) {
2872 prom_printf("Adding Ethernet MDIO node\n");
2873 call_prom("interpret", 1, 1,
2874 " s\" /builtin\" find-device"
2876 " 1 encode-int s\" #address-cells\" property"
2877 " 0 encode-int s\" #size-cells\" property"
2878 " s\" mdio\" device-name"
2879 " s\" fsl,mpc5200b-mdio\" encode-string"
2880 " s\" compatible\" property"
2881 " 0xf0003000 0x400 reg"
2883 " 0x5 encode-int encode+"
2884 " 0x3 encode-int encode+"
2885 " s\" interrupts\" property"
2889 /* Check for a PHY device node - if missing then create one and
2890 * give it's phandle to the ethernet node */
2891 node = call_prom("finddevice", 1, 1,
2892 ADDR("/builtin/mdio/ethernet-phy"));
2893 if (!PHANDLE_VALID(node)) {
2894 prom_printf("Adding Ethernet PHY node\n");
2895 call_prom("interpret", 1, 1,
2896 " s\" /builtin/mdio\" find-device"
2898 " s\" ethernet-phy\" device-name"
2899 " 0x10 encode-int s\" reg\" property"
2903 " s\" /builtin/ethernet\" find-device"
2905 " s\" phy-handle\" property"
2910 static void __init fixup_device_tree_efika(void)
2912 int sound_irq[3] = { 2, 2, 0 };
2913 int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2914 3,4,0, 3,5,0, 3,6,0, 3,7,0,
2915 3,8,0, 3,9,0, 3,10,0, 3,11,0,
2916 3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2921 /* Check if we're really running on a EFIKA */
2922 node = call_prom("finddevice", 1, 1, ADDR("/"));
2923 if (!PHANDLE_VALID(node))
2926 rv = prom_getprop(node, "model", prop, sizeof(prop));
2927 if (rv == PROM_ERROR)
2929 if (prom_strcmp(prop, "EFIKA5K2"))
2932 prom_printf("Applying EFIKA device tree fixups\n");
2934 /* Claiming to be 'chrp' is death */
2935 node = call_prom("finddevice", 1, 1, ADDR("/"));
2936 rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2937 if (rv != PROM_ERROR && (prom_strcmp(prop, "chrp") == 0))
2938 prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2940 /* CODEGEN,description is exposed in /proc/cpuinfo so
2942 rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2943 if (rv != PROM_ERROR && (prom_strstr(prop, "CHRP")))
2944 prom_setprop(node, "/", "CODEGEN,description",
2945 "Efika 5200B PowerPC System",
2946 sizeof("Efika 5200B PowerPC System"));
2948 /* Fixup bestcomm interrupts property */
2949 node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2950 if (PHANDLE_VALID(node)) {
2951 len = prom_getproplen(node, "interrupts");
2953 prom_printf("Fixing bestcomm interrupts property\n");
2954 prom_setprop(node, "/builtin/bestcom", "interrupts",
2955 bcomm_irq, sizeof(bcomm_irq));
2959 /* Fixup sound interrupts property */
2960 node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2961 if (PHANDLE_VALID(node)) {
2962 rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2963 if (rv == PROM_ERROR) {
2964 prom_printf("Adding sound interrupts property\n");
2965 prom_setprop(node, "/builtin/sound", "interrupts",
2966 sound_irq, sizeof(sound_irq));
2970 /* Make sure ethernet phy-handle property exists */
2971 fixup_device_tree_efika_add_phy();
2974 #define fixup_device_tree_efika()
2977 #ifdef CONFIG_PPC_PASEMI_NEMO
2979 * CFE supplied on Nemo is broken in several ways, biggest
2980 * problem is that it reassigns ISA interrupts to unused mpic ints.
2981 * Add an interrupt-controller property for the io-bridge to use
2982 * and correct the ints so we can attach them to an irq_domain
2984 static void __init fixup_device_tree_pasemi(void)
2986 u32 interrupts[2], parent, rval, val = 0;
2987 char *name, *pci_name;
2990 /* Find the root pci node */
2991 name = "/pxp@0,e0000000";
2992 iob = call_prom("finddevice", 1, 1, ADDR(name));
2993 if (!PHANDLE_VALID(iob))
2996 /* check if interrupt-controller node set yet */
2997 if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
3000 prom_printf("adding interrupt-controller property for SB600...\n");
3002 prom_setprop(iob, name, "interrupt-controller", &val, 0);
3004 pci_name = "/pxp@0,e0000000/pci@11";
3005 node = call_prom("finddevice", 1, 1, ADDR(pci_name));
3008 for( ; prom_next_node(&node); ) {
3009 /* scan each node for one with an interrupt */
3010 if (!PHANDLE_VALID(node))
3013 rval = prom_getproplen(node, "interrupts");
3014 if (rval == 0 || rval == PROM_ERROR)
3017 prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
3018 if ((interrupts[0] < 212) || (interrupts[0] > 222))
3021 /* found a node, update both interrupts and interrupt-parent */
3022 if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
3023 interrupts[0] -= 203;
3024 if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
3025 interrupts[0] -= 213;
3026 if (interrupts[0] == 221)
3028 if (interrupts[0] == 222)
3031 prom_setprop(node, pci_name, "interrupts", interrupts,
3032 sizeof(interrupts));
3033 prom_setprop(node, pci_name, "interrupt-parent", &parent,
3038 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
3039 * so that generic isa-bridge code can add the SB600 and its on-board
3042 name = "/pxp@0,e0000000/io-bridge@0";
3043 iob = call_prom("finddevice", 1, 1, ADDR(name));
3044 if (!PHANDLE_VALID(iob))
3047 /* device_type is already set, just change it. */
3049 prom_printf("Changing device_type of SB600 node...\n");
3051 prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
3053 #else /* !CONFIG_PPC_PASEMI_NEMO */
3054 static inline void fixup_device_tree_pasemi(void) { }
3057 static void __init fixup_device_tree(void)
3059 fixup_device_tree_maple();
3060 fixup_device_tree_maple_memory_controller();
3061 fixup_device_tree_chrp();
3062 fixup_device_tree_pmac();
3063 fixup_device_tree_efika();
3064 fixup_device_tree_pasemi();
3067 static void __init prom_find_boot_cpu(void)
3074 if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
3076 prom_cpu = be32_to_cpu(rval);
3078 cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
3080 if (!PHANDLE_VALID(cpu_pkg))
3083 prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
3084 prom.cpu = be32_to_cpu(rval);
3086 prom_debug("Booting CPU hw index = %d\n", prom.cpu);
3089 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
3091 #ifdef CONFIG_BLK_DEV_INITRD
3092 if (r3 && r4 && r4 != 0xdeadbeef) {
3095 prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
3096 prom_initrd_end = prom_initrd_start + r4;
3098 val = cpu_to_be64(prom_initrd_start);
3099 prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
3101 val = cpu_to_be64(prom_initrd_end);
3102 prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
3105 reserve_mem(prom_initrd_start,
3106 prom_initrd_end - prom_initrd_start);
3108 prom_debug("initrd_start=0x%lx\n", prom_initrd_start);
3109 prom_debug("initrd_end=0x%lx\n", prom_initrd_end);
3111 #endif /* CONFIG_BLK_DEV_INITRD */
3115 #ifdef CONFIG_RELOCATABLE
3116 static void reloc_toc(void)
3120 static void unreloc_toc(void)
3124 static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
3127 unsigned long *toc_entry;
3129 /* Get the start of the TOC by using r2 directly. */
3130 asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
3132 for (i = 0; i < nr_entries; i++) {
3133 *toc_entry = *toc_entry + offset;
3138 static void reloc_toc(void)
3140 unsigned long offset = reloc_offset();
3141 unsigned long nr_entries =
3142 (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
3144 __reloc_toc(offset, nr_entries);
3149 static void unreloc_toc(void)
3151 unsigned long offset = reloc_offset();
3152 unsigned long nr_entries =
3153 (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
3157 __reloc_toc(-offset, nr_entries);
3163 * We enter here early on, when the Open Firmware prom is still
3164 * handling exceptions and the MMU hash table for us.
3167 unsigned long __init prom_init(unsigned long r3, unsigned long r4,
3169 unsigned long r6, unsigned long r7,
3170 unsigned long kbase)
3175 unsigned long offset = reloc_offset();
3182 * First zero the BSS
3184 memset(&__bss_start, 0, __bss_stop - __bss_start);
3187 * Init interface to Open Firmware, get some node references,
3190 prom_init_client_services(pp);
3193 * See if this OF is old enough that we need to do explicit maps
3194 * and other workarounds
3199 * Init prom stdout device
3203 prom_printf("Preparing to boot %s", linux_banner);
3206 * Get default machine type. At this point, we do not differentiate
3207 * between pSeries SMP and pSeries LPAR
3209 of_platform = prom_find_machine_type();
3210 prom_printf("Detected machine type: %x\n", of_platform);
3212 #ifndef CONFIG_NONSTATIC_KERNEL
3213 /* Bail if this is a kdump kernel. */
3214 if (PHYSICAL_START > 0)
3215 prom_panic("Error: You can't boot a kdump kernel from OF!\n");
3219 * Check for an initrd
3221 prom_check_initrd(r3, r4);
3224 * Do early parsing of command line
3226 early_cmdline_parse();
3228 #ifdef CONFIG_PPC_PSERIES
3230 * On pSeries, inform the firmware about our capabilities
3232 if (of_platform == PLATFORM_PSERIES ||
3233 of_platform == PLATFORM_PSERIES_LPAR)
3234 prom_send_capabilities();
3238 * Copy the CPU hold code
3240 if (of_platform != PLATFORM_POWERMAC)
3241 copy_and_flush(0, kbase, 0x100, 0);
3244 * Initialize memory management within prom_init
3249 * Determine which cpu is actually running right _now_
3251 prom_find_boot_cpu();
3254 * Initialize display devices
3256 prom_check_displays();
3258 #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
3260 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
3261 * that uses the allocator, we need to make sure we get the top of memory
3262 * available for us here...
3264 if (of_platform == PLATFORM_PSERIES)
3265 prom_initialize_tce_table();
3269 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
3270 * have a usable RTAS implementation.
3272 if (of_platform != PLATFORM_POWERMAC)
3273 prom_instantiate_rtas();
3276 /* instantiate sml */
3277 prom_instantiate_sml();
3281 * On non-powermacs, put all CPUs in spin-loops.
3283 * PowerMacs use a different mechanism to spin CPUs
3285 * (This must be done after instanciating RTAS)
3287 if (of_platform != PLATFORM_POWERMAC)
3291 * Fill in some infos for use by the kernel later on
3293 if (prom_memory_limit) {
3294 __be64 val = cpu_to_be64(prom_memory_limit);
3295 prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3300 prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3303 if (prom_iommu_force_on)
3304 prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3307 if (prom_tce_alloc_start) {
3308 prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
3309 &prom_tce_alloc_start,
3310 sizeof(prom_tce_alloc_start));
3311 prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
3312 &prom_tce_alloc_end,
3313 sizeof(prom_tce_alloc_end));
3318 * Fixup any known bugs in the device-tree
3320 fixup_device_tree();
3323 * Now finally create the flattened device-tree
3325 prom_printf("copying OF device tree...\n");
3326 flatten_device_tree();
3329 * in case stdin is USB and still active on IBM machines...
3330 * Unfortunately quiesce crashes on some powermacs if we have
3331 * closed stdin already (in particular the powerbook 101).
3333 if (of_platform != PLATFORM_POWERMAC)
3337 * Call OF "quiesce" method to shut down pending DMA's from
3340 prom_printf("Quiescing Open Firmware ...\n");
3341 call_prom("quiesce", 0, 0);
3344 * And finally, call the kernel passing it the flattened device
3345 * tree and NULL as r5, thus triggering the new entry point which
3346 * is common to us and kexec
3348 hdr = dt_header_start;
3350 /* Don't print anything after quiesce under OPAL, it crashes OFW */
3351 prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
3352 prom_debug("->dt_header_start=0x%lx\n", hdr);
3355 reloc_got2(-offset);
3360 __start(hdr, kbase, 0, 0, 0, 0, 0);