19b6b563cSPaul Mackerras /* 29b6b563cSPaul Mackerras * Procedures for interfacing to Open Firmware. 39b6b563cSPaul Mackerras * 49b6b563cSPaul Mackerras * Paul Mackerras August 1996. 59b6b563cSPaul Mackerras * Copyright (C) 1996-2005 Paul Mackerras. 69b6b563cSPaul Mackerras * 79b6b563cSPaul Mackerras * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. 89b6b563cSPaul Mackerras * {engebret|bergner}@us.ibm.com 99b6b563cSPaul Mackerras * 109b6b563cSPaul Mackerras * This program is free software; you can redistribute it and/or 119b6b563cSPaul Mackerras * modify it under the terms of the GNU General Public License 129b6b563cSPaul Mackerras * as published by the Free Software Foundation; either version 139b6b563cSPaul Mackerras * 2 of the License, or (at your option) any later version. 149b6b563cSPaul Mackerras */ 159b6b563cSPaul Mackerras 169b6b563cSPaul Mackerras #undef DEBUG_PROM 179b6b563cSPaul Mackerras 189b6b563cSPaul Mackerras #include <stdarg.h> 199b6b563cSPaul Mackerras #include <linux/kernel.h> 209b6b563cSPaul Mackerras #include <linux/string.h> 219b6b563cSPaul Mackerras #include <linux/init.h> 229b6b563cSPaul Mackerras #include <linux/threads.h> 239b6b563cSPaul Mackerras #include <linux/spinlock.h> 249b6b563cSPaul Mackerras #include <linux/types.h> 259b6b563cSPaul Mackerras #include <linux/pci.h> 269b6b563cSPaul Mackerras #include <linux/proc_fs.h> 279b6b563cSPaul Mackerras #include <linux/stringify.h> 289b6b563cSPaul Mackerras #include <linux/delay.h> 299b6b563cSPaul Mackerras #include <linux/initrd.h> 309b6b563cSPaul Mackerras #include <linux/bitops.h> 319b6b563cSPaul Mackerras #include <asm/prom.h> 329b6b563cSPaul Mackerras #include <asm/rtas.h> 339b6b563cSPaul Mackerras #include <asm/page.h> 349b6b563cSPaul Mackerras #include <asm/processor.h> 359b6b563cSPaul Mackerras #include <asm/irq.h> 369b6b563cSPaul Mackerras #include <asm/io.h> 379b6b563cSPaul Mackerras #include <asm/smp.h> 389b6b563cSPaul Mackerras #include <asm/mmu.h> 399b6b563cSPaul Mackerras #include <asm/pgtable.h> 409b6b563cSPaul Mackerras #include <asm/iommu.h> 419b6b563cSPaul Mackerras #include <asm/btext.h> 429b6b563cSPaul Mackerras #include <asm/sections.h> 439b6b563cSPaul Mackerras #include <asm/machdep.h> 4427f44888SBenjamin Herrenschmidt #include <asm/opal.h> 450545d543SDaniel Axtens #include <asm/asm-prototypes.h> 469b6b563cSPaul Mackerras 479b6b563cSPaul Mackerras #include <linux/linux_logo.h> 489b6b563cSPaul Mackerras 499b6b563cSPaul Mackerras /* 509b6b563cSPaul Mackerras * Eventually bump that one up 519b6b563cSPaul Mackerras */ 529b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE 0x100000 539b6b563cSPaul Mackerras 549b6b563cSPaul Mackerras /* 559b6b563cSPaul Mackerras * This is the size of the local memory reserve map that gets copied 569b6b563cSPaul Mackerras * into the boot params passed to the kernel. That size is totally 579b6b563cSPaul Mackerras * flexible as the kernel just reads the list until it encounters an 589b6b563cSPaul Mackerras * entry with size 0, so it can be changed without breaking binary 599b6b563cSPaul Mackerras * compatibility 609b6b563cSPaul Mackerras */ 619b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE 8 629b6b563cSPaul Mackerras 639b6b563cSPaul Mackerras /* 649b6b563cSPaul Mackerras * prom_init() is called very early on, before the kernel text 659b6b563cSPaul Mackerras * and data have been mapped to KERNELBASE. At this point the code 669b6b563cSPaul Mackerras * is running at whatever address it has been loaded at. 679b6b563cSPaul Mackerras * On ppc32 we compile with -mrelocatable, which means that references 689b6b563cSPaul Mackerras * to extern and static variables get relocated automatically. 695ac47f7aSAnton Blanchard * ppc64 objects are always relocatable, we just need to relocate the 705ac47f7aSAnton Blanchard * TOC. 719b6b563cSPaul Mackerras * 729b6b563cSPaul Mackerras * Because OF may have mapped I/O devices into the area starting at 739b6b563cSPaul Mackerras * KERNELBASE, particularly on CHRP machines, we can't safely call 749b6b563cSPaul Mackerras * OF once the kernel has been mapped to KERNELBASE. Therefore all 759b6b563cSPaul Mackerras * OF calls must be done within prom_init(). 769b6b563cSPaul Mackerras * 779b6b563cSPaul Mackerras * ADDR is used in calls to call_prom. The 4th and following 789b6b563cSPaul Mackerras * arguments to call_prom should be 32-bit values. 799b6b563cSPaul Mackerras * On ppc64, 64 bit values are truncated to 32 bits (and 809b6b563cSPaul Mackerras * fortunately don't get interpreted as two arguments). 819b6b563cSPaul Mackerras */ 825ac47f7aSAnton Blanchard #define ADDR(x) (u32)(unsigned long)(x) 835ac47f7aSAnton Blanchard 849b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 85a23414beSPaul Mackerras #define OF_WORKAROUNDS 0 869b6b563cSPaul Mackerras #else 87a23414beSPaul Mackerras #define OF_WORKAROUNDS of_workarounds 88a23414beSPaul Mackerras int of_workarounds; 899b6b563cSPaul Mackerras #endif 909b6b563cSPaul Mackerras 91a23414beSPaul Mackerras #define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */ 92a23414beSPaul Mackerras #define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */ 93a23414beSPaul Mackerras 949b6b563cSPaul Mackerras #define PROM_BUG() do { \ 959b6b563cSPaul Mackerras prom_printf("kernel BUG at %s line 0x%x!\n", \ 965827d416SAnton Blanchard __FILE__, __LINE__); \ 979b6b563cSPaul Mackerras __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \ 989b6b563cSPaul Mackerras } while (0) 999b6b563cSPaul Mackerras 1009b6b563cSPaul Mackerras #ifdef DEBUG_PROM 1019b6b563cSPaul Mackerras #define prom_debug(x...) prom_printf(x) 1029b6b563cSPaul Mackerras #else 1039b6b563cSPaul Mackerras #define prom_debug(x...) 1049b6b563cSPaul Mackerras #endif 1059b6b563cSPaul Mackerras 1069b6b563cSPaul Mackerras 1079b6b563cSPaul Mackerras typedef u32 prom_arg_t; 1089b6b563cSPaul Mackerras 1099b6b563cSPaul Mackerras struct prom_args { 110493adffcSBenjamin Herrenschmidt __be32 service; 111493adffcSBenjamin Herrenschmidt __be32 nargs; 112493adffcSBenjamin Herrenschmidt __be32 nret; 113493adffcSBenjamin Herrenschmidt __be32 args[10]; 1149b6b563cSPaul Mackerras }; 1159b6b563cSPaul Mackerras 1169b6b563cSPaul Mackerras struct prom_t { 1179b6b563cSPaul Mackerras ihandle root; 118a23414beSPaul Mackerras phandle chosen; 1199b6b563cSPaul Mackerras int cpu; 1209b6b563cSPaul Mackerras ihandle stdout; 121a575b807SPaul Mackerras ihandle mmumap; 122a23414beSPaul Mackerras ihandle memory; 1239b6b563cSPaul Mackerras }; 1249b6b563cSPaul Mackerras 1259b6b563cSPaul Mackerras struct mem_map_entry { 126493adffcSBenjamin Herrenschmidt __be64 base; 127493adffcSBenjamin Herrenschmidt __be64 size; 1289b6b563cSPaul Mackerras }; 1299b6b563cSPaul Mackerras 130493adffcSBenjamin Herrenschmidt typedef __be32 cell_t; 1319b6b563cSPaul Mackerras 1326e35d5daSBenjamin Herrenschmidt extern void __start(unsigned long r3, unsigned long r4, unsigned long r5, 1336e35d5daSBenjamin Herrenschmidt unsigned long r6, unsigned long r7, unsigned long r8, 1346e35d5daSBenjamin Herrenschmidt unsigned long r9); 1359b6b563cSPaul Mackerras 1369b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 137c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry); 1389b6b563cSPaul Mackerras #else 139c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry) 1409b6b563cSPaul Mackerras { 141c4988820SPaul Mackerras return ((int (*)(struct prom_args *))entry)(args); 1429b6b563cSPaul Mackerras } 1439b6b563cSPaul Mackerras #endif 1449b6b563cSPaul Mackerras 1459b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src, 1469b6b563cSPaul Mackerras unsigned long size, unsigned long offset); 1479b6b563cSPaul Mackerras 1489b6b563cSPaul Mackerras /* prom structure */ 1499b6b563cSPaul Mackerras static struct prom_t __initdata prom; 1509b6b563cSPaul Mackerras 1519b6b563cSPaul Mackerras static unsigned long prom_entry __initdata; 1529b6b563cSPaul Mackerras 1539b6b563cSPaul Mackerras #define PROM_SCRATCH_SIZE 256 1549b6b563cSPaul Mackerras 1559b6b563cSPaul Mackerras static char __initdata of_stdout_device[256]; 1569b6b563cSPaul Mackerras static char __initdata prom_scratch[PROM_SCRATCH_SIZE]; 1579b6b563cSPaul Mackerras 1589b6b563cSPaul Mackerras static unsigned long __initdata dt_header_start; 1599b6b563cSPaul Mackerras static unsigned long __initdata dt_struct_start, dt_struct_end; 1609b6b563cSPaul Mackerras static unsigned long __initdata dt_string_start, dt_string_end; 1619b6b563cSPaul Mackerras 1629b6b563cSPaul Mackerras static unsigned long __initdata prom_initrd_start, prom_initrd_end; 1639b6b563cSPaul Mackerras 1649b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 165165785e5SJeremy Kerr static int __initdata prom_iommu_force_on; 166165785e5SJeremy Kerr static int __initdata prom_iommu_off; 1679b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_start; 1689b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_end; 1699b6b563cSPaul Mackerras #endif 1709b6b563cSPaul Mackerras 171e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this 172e8222502SBenjamin Herrenschmidt * file and ultimately gone too. Feel free to change them if you need, they 173e8222502SBenjamin Herrenschmidt * are not shared with anything outside of this file anymore 174e8222502SBenjamin Herrenschmidt */ 175e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES 0x0100 176e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR 0x0101 177e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR 0x0001 178e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC 0x0400 179e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC 0x0500 18027f44888SBenjamin Herrenschmidt #define PLATFORM_OPAL 0x0600 181e8222502SBenjamin Herrenschmidt 1829b6b563cSPaul Mackerras static int __initdata of_platform; 1839b6b563cSPaul Mackerras 1849b6b563cSPaul Mackerras static char __initdata prom_cmd_line[COMMAND_LINE_SIZE]; 1859b6b563cSPaul Mackerras 186cf68787bSBenjamin Krill static unsigned long __initdata prom_memory_limit; 187cf68787bSBenjamin Krill 1889b6b563cSPaul Mackerras static unsigned long __initdata alloc_top; 1899b6b563cSPaul Mackerras static unsigned long __initdata alloc_top_high; 1909b6b563cSPaul Mackerras static unsigned long __initdata alloc_bottom; 1919b6b563cSPaul Mackerras static unsigned long __initdata rmo_top; 1929b6b563cSPaul Mackerras static unsigned long __initdata ram_top; 1939b6b563cSPaul Mackerras 1949b6b563cSPaul Mackerras static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE]; 1959b6b563cSPaul Mackerras static int __initdata mem_reserve_cnt; 1969b6b563cSPaul Mackerras 1979b6b563cSPaul Mackerras static cell_t __initdata regbuf[1024]; 1989b6b563cSPaul Mackerras 199dbe78b40SBenjamin Herrenschmidt static bool rtas_has_query_cpu_stopped; 200dbe78b40SBenjamin Herrenschmidt 2019b6b563cSPaul Mackerras 2029b6b563cSPaul Mackerras /* 2039b6b563cSPaul Mackerras * Error results ... some OF calls will return "-1" on error, some 2049b6b563cSPaul Mackerras * will return 0, some will return either. To simplify, here are 2059b6b563cSPaul Mackerras * macros to use with any ihandle or phandle return value to check if 2069b6b563cSPaul Mackerras * it is valid 2079b6b563cSPaul Mackerras */ 2089b6b563cSPaul Mackerras 2099b6b563cSPaul Mackerras #define PROM_ERROR (-1u) 2109b6b563cSPaul Mackerras #define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR) 2119b6b563cSPaul Mackerras #define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR) 2129b6b563cSPaul Mackerras 2139b6b563cSPaul Mackerras 2149b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open 2159b6b563cSPaul Mackerras * firmware. 2169b6b563cSPaul Mackerras */ 2179b6b563cSPaul Mackerras 2189b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...) 2199b6b563cSPaul Mackerras { 2209b6b563cSPaul Mackerras int i; 2219b6b563cSPaul Mackerras struct prom_args args; 2229b6b563cSPaul Mackerras va_list list; 2239b6b563cSPaul Mackerras 224493adffcSBenjamin Herrenschmidt args.service = cpu_to_be32(ADDR(service)); 225493adffcSBenjamin Herrenschmidt args.nargs = cpu_to_be32(nargs); 226493adffcSBenjamin Herrenschmidt args.nret = cpu_to_be32(nret); 2279b6b563cSPaul Mackerras 2289b6b563cSPaul Mackerras va_start(list, nret); 2299b6b563cSPaul Mackerras for (i = 0; i < nargs; i++) 230493adffcSBenjamin Herrenschmidt args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t)); 2319b6b563cSPaul Mackerras va_end(list); 2329b6b563cSPaul Mackerras 2339b6b563cSPaul Mackerras for (i = 0; i < nret; i++) 2349b6b563cSPaul Mackerras args.args[nargs+i] = 0; 2359b6b563cSPaul Mackerras 2365827d416SAnton Blanchard if (enter_prom(&args, prom_entry) < 0) 237c4988820SPaul Mackerras return PROM_ERROR; 2389b6b563cSPaul Mackerras 239493adffcSBenjamin Herrenschmidt return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0; 2409b6b563cSPaul Mackerras } 2419b6b563cSPaul Mackerras 2429b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret, 2439b6b563cSPaul Mackerras prom_arg_t *rets, ...) 2449b6b563cSPaul Mackerras { 2459b6b563cSPaul Mackerras int i; 2469b6b563cSPaul Mackerras struct prom_args args; 2479b6b563cSPaul Mackerras va_list list; 2489b6b563cSPaul Mackerras 249493adffcSBenjamin Herrenschmidt args.service = cpu_to_be32(ADDR(service)); 250493adffcSBenjamin Herrenschmidt args.nargs = cpu_to_be32(nargs); 251493adffcSBenjamin Herrenschmidt args.nret = cpu_to_be32(nret); 2529b6b563cSPaul Mackerras 2539b6b563cSPaul Mackerras va_start(list, rets); 2549b6b563cSPaul Mackerras for (i = 0; i < nargs; i++) 255493adffcSBenjamin Herrenschmidt args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t)); 2569b6b563cSPaul Mackerras va_end(list); 2579b6b563cSPaul Mackerras 2589b6b563cSPaul Mackerras for (i = 0; i < nret; i++) 259ed1189b7SOlaf Hering args.args[nargs+i] = 0; 2609b6b563cSPaul Mackerras 2615827d416SAnton Blanchard if (enter_prom(&args, prom_entry) < 0) 262c4988820SPaul Mackerras return PROM_ERROR; 2639b6b563cSPaul Mackerras 2649b6b563cSPaul Mackerras if (rets != NULL) 2659b6b563cSPaul Mackerras for (i = 1; i < nret; ++i) 266493adffcSBenjamin Herrenschmidt rets[i-1] = be32_to_cpu(args.args[nargs+i]); 2679b6b563cSPaul Mackerras 268493adffcSBenjamin Herrenschmidt return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0; 2699b6b563cSPaul Mackerras } 2709b6b563cSPaul Mackerras 2719b6b563cSPaul Mackerras 2729b6b563cSPaul Mackerras static void __init prom_print(const char *msg) 2739b6b563cSPaul Mackerras { 2749b6b563cSPaul Mackerras const char *p, *q; 2759b6b563cSPaul Mackerras 2765827d416SAnton Blanchard if (prom.stdout == 0) 2779b6b563cSPaul Mackerras return; 2789b6b563cSPaul Mackerras 2799b6b563cSPaul Mackerras for (p = msg; *p != 0; p = q) { 2809b6b563cSPaul Mackerras for (q = p; *q != 0 && *q != '\n'; ++q) 2819b6b563cSPaul Mackerras ; 2829b6b563cSPaul Mackerras if (q > p) 2835827d416SAnton Blanchard call_prom("write", 3, 1, prom.stdout, p, q - p); 2849b6b563cSPaul Mackerras if (*q == 0) 2859b6b563cSPaul Mackerras break; 2869b6b563cSPaul Mackerras ++q; 2875827d416SAnton Blanchard call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2); 2889b6b563cSPaul Mackerras } 2899b6b563cSPaul Mackerras } 2909b6b563cSPaul Mackerras 2919b6b563cSPaul Mackerras 2929b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val) 2939b6b563cSPaul Mackerras { 2949b6b563cSPaul Mackerras int i, nibbles = sizeof(val)*2; 2959b6b563cSPaul Mackerras char buf[sizeof(val)*2+1]; 2969b6b563cSPaul Mackerras 2979b6b563cSPaul Mackerras for (i = nibbles-1; i >= 0; i--) { 2989b6b563cSPaul Mackerras buf[i] = (val & 0xf) + '0'; 2999b6b563cSPaul Mackerras if (buf[i] > '9') 3009b6b563cSPaul Mackerras buf[i] += ('a'-'0'-10); 3019b6b563cSPaul Mackerras val >>= 4; 3029b6b563cSPaul Mackerras } 3039b6b563cSPaul Mackerras buf[nibbles] = '\0'; 3045827d416SAnton Blanchard call_prom("write", 3, 1, prom.stdout, buf, nibbles); 3059b6b563cSPaul Mackerras } 3069b6b563cSPaul Mackerras 3072c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */ 3082c48a7d6SMichael Neuling #define UL_DIGITS 21 3092c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val) 3102c48a7d6SMichael Neuling { 3112c48a7d6SMichael Neuling int i, size; 3122c48a7d6SMichael Neuling char buf[UL_DIGITS+1]; 3132c48a7d6SMichael Neuling 3142c48a7d6SMichael Neuling for (i = UL_DIGITS-1; i >= 0; i--) { 3152c48a7d6SMichael Neuling buf[i] = (val % 10) + '0'; 3162c48a7d6SMichael Neuling val = val/10; 3172c48a7d6SMichael Neuling if (val == 0) 3182c48a7d6SMichael Neuling break; 3192c48a7d6SMichael Neuling } 3202c48a7d6SMichael Neuling /* shift stuff down */ 3212c48a7d6SMichael Neuling size = UL_DIGITS - i; 3225827d416SAnton Blanchard call_prom("write", 3, 1, prom.stdout, buf+i, size); 3232c48a7d6SMichael Neuling } 3249b6b563cSPaul Mackerras 3259b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...) 3269b6b563cSPaul Mackerras { 3279b6b563cSPaul Mackerras const char *p, *q, *s; 3289b6b563cSPaul Mackerras va_list args; 3299b6b563cSPaul Mackerras unsigned long v; 330af277149SBenjamin Herrenschmidt long vs; 3319b6b563cSPaul Mackerras 3329b6b563cSPaul Mackerras va_start(args, format); 3339b6b563cSPaul Mackerras for (p = format; *p != 0; p = q) { 3349b6b563cSPaul Mackerras for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q) 3359b6b563cSPaul Mackerras ; 3369b6b563cSPaul Mackerras if (q > p) 3375827d416SAnton Blanchard call_prom("write", 3, 1, prom.stdout, p, q - p); 3389b6b563cSPaul Mackerras if (*q == 0) 3399b6b563cSPaul Mackerras break; 3409b6b563cSPaul Mackerras if (*q == '\n') { 3419b6b563cSPaul Mackerras ++q; 3425827d416SAnton Blanchard call_prom("write", 3, 1, prom.stdout, 3439b6b563cSPaul Mackerras ADDR("\r\n"), 2); 3449b6b563cSPaul Mackerras continue; 3459b6b563cSPaul Mackerras } 3469b6b563cSPaul Mackerras ++q; 3479b6b563cSPaul Mackerras if (*q == 0) 3489b6b563cSPaul Mackerras break; 3499b6b563cSPaul Mackerras switch (*q) { 3509b6b563cSPaul Mackerras case 's': 3519b6b563cSPaul Mackerras ++q; 3529b6b563cSPaul Mackerras s = va_arg(args, const char *); 3539b6b563cSPaul Mackerras prom_print(s); 3549b6b563cSPaul Mackerras break; 3559b6b563cSPaul Mackerras case 'x': 3569b6b563cSPaul Mackerras ++q; 3579b6b563cSPaul Mackerras v = va_arg(args, unsigned long); 3589b6b563cSPaul Mackerras prom_print_hex(v); 3599b6b563cSPaul Mackerras break; 360af277149SBenjamin Herrenschmidt case 'd': 361af277149SBenjamin Herrenschmidt ++q; 362af277149SBenjamin Herrenschmidt vs = va_arg(args, int); 363af277149SBenjamin Herrenschmidt if (vs < 0) { 3645827d416SAnton Blanchard prom_print("-"); 365af277149SBenjamin Herrenschmidt vs = -vs; 366af277149SBenjamin Herrenschmidt } 367af277149SBenjamin Herrenschmidt prom_print_dec(vs); 368af277149SBenjamin Herrenschmidt break; 3692c48a7d6SMichael Neuling case 'l': 3702c48a7d6SMichael Neuling ++q; 371af277149SBenjamin Herrenschmidt if (*q == 0) 372af277149SBenjamin Herrenschmidt break; 373af277149SBenjamin Herrenschmidt else if (*q == 'x') { 374af277149SBenjamin Herrenschmidt ++q; 375af277149SBenjamin Herrenschmidt v = va_arg(args, unsigned long); 376af277149SBenjamin Herrenschmidt prom_print_hex(v); 377af277149SBenjamin Herrenschmidt } else if (*q == 'u') { /* '%lu' */ 3782c48a7d6SMichael Neuling ++q; 3792c48a7d6SMichael Neuling v = va_arg(args, unsigned long); 3802c48a7d6SMichael Neuling prom_print_dec(v); 381af277149SBenjamin Herrenschmidt } else if (*q == 'd') { /* %ld */ 382af277149SBenjamin Herrenschmidt ++q; 383af277149SBenjamin Herrenschmidt vs = va_arg(args, long); 384af277149SBenjamin Herrenschmidt if (vs < 0) { 3855827d416SAnton Blanchard prom_print("-"); 386af277149SBenjamin Herrenschmidt vs = -vs; 387af277149SBenjamin Herrenschmidt } 388af277149SBenjamin Herrenschmidt prom_print_dec(vs); 3892c48a7d6SMichael Neuling } 3902c48a7d6SMichael Neuling break; 3919b6b563cSPaul Mackerras } 3929b6b563cSPaul Mackerras } 3931b855e16SDaniel Axtens va_end(args); 3949b6b563cSPaul Mackerras } 3959b6b563cSPaul Mackerras 3969b6b563cSPaul Mackerras 397a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size, 398a575b807SPaul Mackerras unsigned long align) 399a575b807SPaul Mackerras { 400a575b807SPaul Mackerras 401a23414beSPaul Mackerras if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) { 402a23414beSPaul Mackerras /* 403a23414beSPaul Mackerras * Old OF requires we claim physical and virtual separately 404a23414beSPaul Mackerras * and then map explicitly (assuming virtual mode) 405a23414beSPaul Mackerras */ 406a23414beSPaul Mackerras int ret; 407a23414beSPaul Mackerras prom_arg_t result; 408a23414beSPaul Mackerras 409a23414beSPaul Mackerras ret = call_prom_ret("call-method", 5, 2, &result, 4105827d416SAnton Blanchard ADDR("claim"), prom.memory, 411a23414beSPaul Mackerras align, size, virt); 412a23414beSPaul Mackerras if (ret != 0 || result == -1) 413a23414beSPaul Mackerras return -1; 414a23414beSPaul Mackerras ret = call_prom_ret("call-method", 5, 2, &result, 4155827d416SAnton Blanchard ADDR("claim"), prom.mmumap, 416a23414beSPaul Mackerras align, size, virt); 417a23414beSPaul Mackerras if (ret != 0) { 418a23414beSPaul Mackerras call_prom("call-method", 4, 1, ADDR("release"), 4195827d416SAnton Blanchard prom.memory, size, virt); 420a23414beSPaul Mackerras return -1; 421a23414beSPaul Mackerras } 422a23414beSPaul Mackerras /* the 0x12 is M (coherence) + PP == read/write */ 423a575b807SPaul Mackerras call_prom("call-method", 6, 1, 4245827d416SAnton Blanchard ADDR("map"), prom.mmumap, 0x12, size, virt, virt); 425a23414beSPaul Mackerras return virt; 426a23414beSPaul Mackerras } 427a23414beSPaul Mackerras return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size, 428a23414beSPaul Mackerras (prom_arg_t)align); 429a575b807SPaul Mackerras } 430a575b807SPaul Mackerras 4319b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason) 4329b6b563cSPaul Mackerras { 4339b6b563cSPaul Mackerras prom_print(reason); 434add60ef3SOlaf Hering /* Do not call exit because it clears the screen on pmac 435add60ef3SOlaf Hering * it also causes some sort of double-fault on early pmacs */ 4365827d416SAnton Blanchard if (of_platform == PLATFORM_POWERMAC) 437add60ef3SOlaf Hering asm("trap\n"); 438add60ef3SOlaf Hering 4391d9a4731SStephen Rothwell /* ToDo: should put up an SRC here on pSeries */ 4409b6b563cSPaul Mackerras call_prom("exit", 0, 0); 4419b6b563cSPaul Mackerras 4429b6b563cSPaul Mackerras for (;;) /* should never get here */ 4439b6b563cSPaul Mackerras ; 4449b6b563cSPaul Mackerras } 4459b6b563cSPaul Mackerras 4469b6b563cSPaul Mackerras 4479b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep) 4489b6b563cSPaul Mackerras { 4499b6b563cSPaul Mackerras phandle node; 4509b6b563cSPaul Mackerras 4519b6b563cSPaul Mackerras if ((node = *nodep) != 0 4529b6b563cSPaul Mackerras && (*nodep = call_prom("child", 1, 1, node)) != 0) 4539b6b563cSPaul Mackerras return 1; 4549b6b563cSPaul Mackerras if ((*nodep = call_prom("peer", 1, 1, node)) != 0) 4559b6b563cSPaul Mackerras return 1; 4569b6b563cSPaul Mackerras for (;;) { 4579b6b563cSPaul Mackerras if ((node = call_prom("parent", 1, 1, node)) == 0) 4589b6b563cSPaul Mackerras return 0; 4599b6b563cSPaul Mackerras if ((*nodep = call_prom("peer", 1, 1, node)) != 0) 4609b6b563cSPaul Mackerras return 1; 4619b6b563cSPaul Mackerras } 4629b6b563cSPaul Mackerras } 4639b6b563cSPaul Mackerras 46421fe3301SBenjamin Herrenschmidt static int inline prom_getprop(phandle node, const char *pname, 4659b6b563cSPaul Mackerras void *value, size_t valuelen) 4669b6b563cSPaul Mackerras { 4679b6b563cSPaul Mackerras return call_prom("getprop", 4, 1, node, ADDR(pname), 4689b6b563cSPaul Mackerras (u32)(unsigned long) value, (u32) valuelen); 4699b6b563cSPaul Mackerras } 4709b6b563cSPaul Mackerras 47121fe3301SBenjamin Herrenschmidt static int inline prom_getproplen(phandle node, const char *pname) 4729b6b563cSPaul Mackerras { 4739b6b563cSPaul Mackerras return call_prom("getproplen", 2, 1, node, ADDR(pname)); 4749b6b563cSPaul Mackerras } 4759b6b563cSPaul Mackerras 476a23414beSPaul Mackerras static void add_string(char **str, const char *q) 4779b6b563cSPaul Mackerras { 478a23414beSPaul Mackerras char *p = *str; 479a23414beSPaul Mackerras 480a23414beSPaul Mackerras while (*q) 481a23414beSPaul Mackerras *p++ = *q++; 482a23414beSPaul Mackerras *p++ = ' '; 483a23414beSPaul Mackerras *str = p; 484a23414beSPaul Mackerras } 485a23414beSPaul Mackerras 486a23414beSPaul Mackerras static char *tohex(unsigned int x) 487a23414beSPaul Mackerras { 488a23414beSPaul Mackerras static char digits[] = "0123456789abcdef"; 489a23414beSPaul Mackerras static char result[9]; 490a23414beSPaul Mackerras int i; 491a23414beSPaul Mackerras 492a23414beSPaul Mackerras result[8] = 0; 493a23414beSPaul Mackerras i = 8; 494a23414beSPaul Mackerras do { 495a23414beSPaul Mackerras --i; 496a23414beSPaul Mackerras result[i] = digits[x & 0xf]; 497a23414beSPaul Mackerras x >>= 4; 498a23414beSPaul Mackerras } while (x != 0 && i > 0); 499a23414beSPaul Mackerras return &result[i]; 500a23414beSPaul Mackerras } 501a23414beSPaul Mackerras 502a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename, 503a23414beSPaul Mackerras const char *pname, void *value, size_t valuelen) 504a23414beSPaul Mackerras { 505a23414beSPaul Mackerras char cmd[256], *p; 506a23414beSPaul Mackerras 507a23414beSPaul Mackerras if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL)) 5089b6b563cSPaul Mackerras return call_prom("setprop", 4, 1, node, ADDR(pname), 5099b6b563cSPaul Mackerras (u32)(unsigned long) value, (u32) valuelen); 510a23414beSPaul Mackerras 511a23414beSPaul Mackerras /* gah... setprop doesn't work on longtrail, have to use interpret */ 512a23414beSPaul Mackerras p = cmd; 513a23414beSPaul Mackerras add_string(&p, "dev"); 514a23414beSPaul Mackerras add_string(&p, nodename); 515a23414beSPaul Mackerras add_string(&p, tohex((u32)(unsigned long) value)); 516a23414beSPaul Mackerras add_string(&p, tohex(valuelen)); 517a23414beSPaul Mackerras add_string(&p, tohex(ADDR(pname))); 5185827d416SAnton Blanchard add_string(&p, tohex(strlen(pname))); 519a23414beSPaul Mackerras add_string(&p, "property"); 520a23414beSPaul Mackerras *p = 0; 521a23414beSPaul Mackerras return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd); 5229b6b563cSPaul Mackerras } 5239b6b563cSPaul Mackerras 5245827d416SAnton Blanchard /* We can't use the standard versions because of relocation headaches. */ 525cf68787bSBenjamin Krill #define isxdigit(c) (('0' <= (c) && (c) <= '9') \ 526cf68787bSBenjamin Krill || ('a' <= (c) && (c) <= 'f') \ 527cf68787bSBenjamin Krill || ('A' <= (c) && (c) <= 'F')) 528cf68787bSBenjamin Krill 529cf68787bSBenjamin Krill #define isdigit(c) ('0' <= (c) && (c) <= '9') 530cf68787bSBenjamin Krill #define islower(c) ('a' <= (c) && (c) <= 'z') 531cf68787bSBenjamin Krill #define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c)) 532cf68787bSBenjamin Krill 533493adffcSBenjamin Herrenschmidt static unsigned long prom_strtoul(const char *cp, const char **endp) 534cf68787bSBenjamin Krill { 535cf68787bSBenjamin Krill unsigned long result = 0, base = 10, value; 536cf68787bSBenjamin Krill 537cf68787bSBenjamin Krill if (*cp == '0') { 538cf68787bSBenjamin Krill base = 8; 539cf68787bSBenjamin Krill cp++; 540cf68787bSBenjamin Krill if (toupper(*cp) == 'X') { 541cf68787bSBenjamin Krill cp++; 542cf68787bSBenjamin Krill base = 16; 543cf68787bSBenjamin Krill } 544cf68787bSBenjamin Krill } 545cf68787bSBenjamin Krill 546cf68787bSBenjamin Krill while (isxdigit(*cp) && 547cf68787bSBenjamin Krill (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) { 548cf68787bSBenjamin Krill result = result * base + value; 549cf68787bSBenjamin Krill cp++; 550cf68787bSBenjamin Krill } 551cf68787bSBenjamin Krill 552cf68787bSBenjamin Krill if (endp) 553cf68787bSBenjamin Krill *endp = cp; 554cf68787bSBenjamin Krill 555cf68787bSBenjamin Krill return result; 556cf68787bSBenjamin Krill } 557cf68787bSBenjamin Krill 558493adffcSBenjamin Herrenschmidt static unsigned long prom_memparse(const char *ptr, const char **retptr) 559cf68787bSBenjamin Krill { 560cf68787bSBenjamin Krill unsigned long ret = prom_strtoul(ptr, retptr); 561cf68787bSBenjamin Krill int shift = 0; 562cf68787bSBenjamin Krill 563cf68787bSBenjamin Krill /* 564cf68787bSBenjamin Krill * We can't use a switch here because GCC *may* generate a 565cf68787bSBenjamin Krill * jump table which won't work, because we're not running at 566cf68787bSBenjamin Krill * the address we're linked at. 567cf68787bSBenjamin Krill */ 568cf68787bSBenjamin Krill if ('G' == **retptr || 'g' == **retptr) 569cf68787bSBenjamin Krill shift = 30; 570cf68787bSBenjamin Krill 571cf68787bSBenjamin Krill if ('M' == **retptr || 'm' == **retptr) 572cf68787bSBenjamin Krill shift = 20; 573cf68787bSBenjamin Krill 574cf68787bSBenjamin Krill if ('K' == **retptr || 'k' == **retptr) 575cf68787bSBenjamin Krill shift = 10; 576cf68787bSBenjamin Krill 577cf68787bSBenjamin Krill if (shift) { 578cf68787bSBenjamin Krill ret <<= shift; 579cf68787bSBenjamin Krill (*retptr)++; 580cf68787bSBenjamin Krill } 581cf68787bSBenjamin Krill 582cf68787bSBenjamin Krill return ret; 583cf68787bSBenjamin Krill } 584cf68787bSBenjamin Krill 5859b6b563cSPaul Mackerras /* 5869b6b563cSPaul Mackerras * Early parsing of the command line passed to the kernel, used for 5879b6b563cSPaul Mackerras * "mem=x" and the options that affect the iommu 5889b6b563cSPaul Mackerras */ 5899b6b563cSPaul Mackerras static void __init early_cmdline_parse(void) 5909b6b563cSPaul Mackerras { 591cc5d0189SBenjamin Herrenschmidt const char *opt; 592cf68787bSBenjamin Krill 593cc5d0189SBenjamin Herrenschmidt char *p; 5949b6b563cSPaul Mackerras int l = 0; 5959b6b563cSPaul Mackerras 5965827d416SAnton Blanchard prom_cmd_line[0] = 0; 5975827d416SAnton Blanchard p = prom_cmd_line; 5985827d416SAnton Blanchard if ((long)prom.chosen > 0) 5995827d416SAnton Blanchard l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1); 6009b6b563cSPaul Mackerras #ifdef CONFIG_CMDLINE 6010e4aa9c2SAmos Waterland if (l <= 0 || p[0] == '\0') /* dbl check */ 6025827d416SAnton Blanchard strlcpy(prom_cmd_line, 6035827d416SAnton Blanchard CONFIG_CMDLINE, sizeof(prom_cmd_line)); 6049b6b563cSPaul Mackerras #endif /* CONFIG_CMDLINE */ 6055827d416SAnton Blanchard prom_printf("command line: %s\n", prom_cmd_line); 6069b6b563cSPaul Mackerras 6079b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 6085827d416SAnton Blanchard opt = strstr(prom_cmd_line, "iommu="); 6099b6b563cSPaul Mackerras if (opt) { 6109b6b563cSPaul Mackerras prom_printf("iommu opt is: %s\n", opt); 6119b6b563cSPaul Mackerras opt += 6; 6129b6b563cSPaul Mackerras while (*opt && *opt == ' ') 6139b6b563cSPaul Mackerras opt++; 6145827d416SAnton Blanchard if (!strncmp(opt, "off", 3)) 6155827d416SAnton Blanchard prom_iommu_off = 1; 6165827d416SAnton Blanchard else if (!strncmp(opt, "force", 5)) 6175827d416SAnton Blanchard prom_iommu_force_on = 1; 6189b6b563cSPaul Mackerras } 6199b6b563cSPaul Mackerras #endif 6205827d416SAnton Blanchard opt = strstr(prom_cmd_line, "mem="); 621cf68787bSBenjamin Krill if (opt) { 622cf68787bSBenjamin Krill opt += 4; 6235827d416SAnton Blanchard prom_memory_limit = prom_memparse(opt, (const char **)&opt); 624cf68787bSBenjamin Krill #ifdef CONFIG_PPC64 625cf68787bSBenjamin Krill /* Align to 16 MB == size of ppc64 large page */ 6265827d416SAnton Blanchard prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000); 627cf68787bSBenjamin Krill #endif 628cf68787bSBenjamin Krill } 6299b6b563cSPaul Mackerras } 6309b6b563cSPaul Mackerras 63127f44888SBenjamin Herrenschmidt #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV) 6329b6b563cSPaul Mackerras /* 633530b5e14SNathan Fontenot * The architecture vector has an array of PVR mask/value pairs, 634530b5e14SNathan Fontenot * followed by # option vectors - 1, followed by the option vectors. 635530b5e14SNathan Fontenot * 636530b5e14SNathan Fontenot * See prom.h for the definition of the bits specified in the 637530b5e14SNathan Fontenot * architecture vector. 638f709bfacSPaul Mackerras * 639f709bfacSPaul Mackerras * Because the description vector contains a mix of byte and word 640f709bfacSPaul Mackerras * values, we declare it as an unsigned char array, and use this 641f709bfacSPaul Mackerras * macro to put word values in. 642f709bfacSPaul Mackerras */ 643f709bfacSPaul Mackerras #define W(x) ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \ 644f709bfacSPaul Mackerras ((x) >> 8) & 0xff, (x) & 0xff 645f709bfacSPaul Mackerras 646e8a4fd0aSMichael Ellerman /* Firmware expects the value to be n - 1, where n is the # of vectors */ 647e8a4fd0aSMichael Ellerman #define NUM_VECTORS(n) ((n) - 1) 648e8a4fd0aSMichael Ellerman 649e8a4fd0aSMichael Ellerman /* 650e8a4fd0aSMichael Ellerman * Firmware expects 1 + n - 2, where n is the length of the option vector in 651e8a4fd0aSMichael Ellerman * bytes. The 1 accounts for the length byte itself, the - 2 .. ? 652e8a4fd0aSMichael Ellerman */ 653e8a4fd0aSMichael Ellerman #define VECTOR_LENGTH(n) (1 + (n) - 2) 654e8a4fd0aSMichael Ellerman 655530b5e14SNathan Fontenot unsigned char ibm_architecture_vec[] = { 656f709bfacSPaul Mackerras W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */ 65703054d51SAnton Blanchard W(0xffff0000), W(0x003e0000), /* POWER6 */ 658e952e6c4SMichael Neuling W(0xffff0000), W(0x003f0000), /* POWER7 */ 65933959f88SMichael Neuling W(0xffff0000), W(0x004b0000), /* POWER8E */ 6607cc85103SThomas Huth W(0xffff0000), W(0x004c0000), /* POWER8NVL */ 66133959f88SMichael Neuling W(0xffff0000), W(0x004d0000), /* POWER8 */ 662df77c799SMichael Neuling W(0xffffffff), W(0x0f000004), /* all 2.07-compliant */ 6630cb99013SJoel Schopp W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */ 6640efbc18aSPaul Mackerras W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */ 665f709bfacSPaul Mackerras W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */ 666e8a4fd0aSMichael Ellerman NUM_VECTORS(6), /* 6 option vectors */ 667f709bfacSPaul Mackerras 668f709bfacSPaul Mackerras /* option vector 1: processor architectures supported */ 669e8a4fd0aSMichael Ellerman VECTOR_LENGTH(2), /* length */ 670f709bfacSPaul Mackerras 0, /* don't ignore, don't halt */ 671f709bfacSPaul Mackerras OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 | 672df77c799SMichael Neuling OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07, 673f709bfacSPaul Mackerras 674f709bfacSPaul Mackerras /* option vector 2: Open Firmware options supported */ 675e8a4fd0aSMichael Ellerman VECTOR_LENGTH(33), /* length */ 676f709bfacSPaul Mackerras OV2_REAL_MODE, 677f709bfacSPaul Mackerras 0, 0, 678f709bfacSPaul Mackerras W(0xffffffff), /* real_base */ 679f709bfacSPaul Mackerras W(0xffffffff), /* real_size */ 680f709bfacSPaul Mackerras W(0xffffffff), /* virt_base */ 681f709bfacSPaul Mackerras W(0xffffffff), /* virt_size */ 682f709bfacSPaul Mackerras W(0xffffffff), /* load_base */ 68333392640SAnton Blanchard W(256), /* 256MB min RMA */ 684f709bfacSPaul Mackerras W(0xffffffff), /* full client load */ 685f709bfacSPaul Mackerras 0, /* min RMA percentage of total RAM */ 686f709bfacSPaul Mackerras 48, /* max log_2(hash table size) */ 687f709bfacSPaul Mackerras 688f709bfacSPaul Mackerras /* option vector 3: processor options supported */ 689e8a4fd0aSMichael Ellerman VECTOR_LENGTH(2), /* length */ 690f709bfacSPaul Mackerras 0, /* don't ignore, don't halt */ 691974a76f5SPaul Mackerras OV3_FP | OV3_VMX | OV3_DFP, 692f709bfacSPaul Mackerras 693f709bfacSPaul Mackerras /* option vector 4: IBM PAPR implementation */ 694e8a4fd0aSMichael Ellerman VECTOR_LENGTH(2), /* length */ 695f709bfacSPaul Mackerras 0, /* don't halt */ 696404e32e4SRobert Jennings OV4_MIN_ENT_CAP, /* minimum VP entitled capacity */ 697f709bfacSPaul Mackerras 698f709bfacSPaul Mackerras /* option vector 5: PAPR/OF options */ 69966443efaSMichael Ellerman VECTOR_LENGTH(21), /* length */ 700f709bfacSPaul Mackerras 0, /* don't ignore, don't halt */ 701f0ff7eb4SNathan Fontenot OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) | 702f0ff7eb4SNathan Fontenot OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) | 703f0ff7eb4SNathan Fontenot #ifdef CONFIG_PCI_MSI 704f0ff7eb4SNathan Fontenot /* PCIe/MSI support. Without MSI full PCIe is not supported */ 705f0ff7eb4SNathan Fontenot OV5_FEAT(OV5_MSI), 706f0ff7eb4SNathan Fontenot #else 7078391e42aSNathan Fontenot 0, 708f0ff7eb4SNathan Fontenot #endif 709f0ff7eb4SNathan Fontenot 0, 710f0ff7eb4SNathan Fontenot #ifdef CONFIG_PPC_SMLPAR 711f0ff7eb4SNathan Fontenot OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO), 712f0ff7eb4SNathan Fontenot #else 713f0ff7eb4SNathan Fontenot 0, 714f0ff7eb4SNathan Fontenot #endif 7151b1218d3SNathan Fontenot OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN), 71628bb9ee1Sjschopp@austin.ibm.com 0, 71728bb9ee1Sjschopp@austin.ibm.com 0, 71828bb9ee1Sjschopp@austin.ibm.com 0, 719efec959fSBenjamin Herrenschmidt /* WARNING: The offset of the "number of cores" field below 720efec959fSBenjamin Herrenschmidt * must match by the macro below. Update the definition if 721efec959fSBenjamin Herrenschmidt * the structure layout changes. 722efec959fSBenjamin Herrenschmidt */ 7232c2a63e3SMichael Ellerman #define IBM_ARCH_VEC_NRCORES_OFFSET 133 72428bb9ee1Sjschopp@austin.ibm.com W(NR_CPUS), /* number of cores supported */ 725828d2b59SKent Yoder 0, 726828d2b59SKent Yoder 0, 727828d2b59SKent Yoder 0, 728828d2b59SKent Yoder 0, 729f0ff7eb4SNathan Fontenot OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) | 73066443efaSMichael Ellerman OV5_FEAT(OV5_PFO_HW_842), /* Byte 17 */ 73166443efaSMichael Ellerman 0, /* Byte 18 */ 73266443efaSMichael Ellerman 0, /* Byte 19 */ 73366443efaSMichael Ellerman 0, /* Byte 20 */ 73466443efaSMichael Ellerman OV5_FEAT(OV5_SUB_PROCESSORS), /* Byte 21 */ 735e8a4fd0aSMichael Ellerman 73628bb9ee1Sjschopp@austin.ibm.com /* option vector 6: IBM PAPR hints */ 737e8a4fd0aSMichael Ellerman VECTOR_LENGTH(3), /* length */ 73828bb9ee1Sjschopp@austin.ibm.com 0, 73928bb9ee1Sjschopp@austin.ibm.com 0, 74028bb9ee1Sjschopp@austin.ibm.com OV6_LINUX, 741f709bfacSPaul Mackerras }; 742f709bfacSPaul Mackerras 743493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */ 744493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__ 7459b6b563cSPaul Mackerras static struct fake_elf { 7469b6b563cSPaul Mackerras Elf32_Ehdr elfhdr; 7479b6b563cSPaul Mackerras Elf32_Phdr phdr[2]; 7489b6b563cSPaul Mackerras struct chrpnote { 7499b6b563cSPaul Mackerras u32 namesz; 7509b6b563cSPaul Mackerras u32 descsz; 7519b6b563cSPaul Mackerras u32 type; 7529b6b563cSPaul Mackerras char name[8]; /* "PowerPC" */ 7539b6b563cSPaul Mackerras struct chrpdesc { 7549b6b563cSPaul Mackerras u32 real_mode; 7559b6b563cSPaul Mackerras u32 real_base; 7569b6b563cSPaul Mackerras u32 real_size; 7579b6b563cSPaul Mackerras u32 virt_base; 7589b6b563cSPaul Mackerras u32 virt_size; 7599b6b563cSPaul Mackerras u32 load_base; 7609b6b563cSPaul Mackerras } chrpdesc; 7619b6b563cSPaul Mackerras } chrpnote; 7629b6b563cSPaul Mackerras struct rpanote { 7639b6b563cSPaul Mackerras u32 namesz; 7649b6b563cSPaul Mackerras u32 descsz; 7659b6b563cSPaul Mackerras u32 type; 7669b6b563cSPaul Mackerras char name[24]; /* "IBM,RPA-Client-Config" */ 7679b6b563cSPaul Mackerras struct rpadesc { 7689b6b563cSPaul Mackerras u32 lpar_affinity; 7699b6b563cSPaul Mackerras u32 min_rmo_size; 7709b6b563cSPaul Mackerras u32 min_rmo_percent; 7719b6b563cSPaul Mackerras u32 max_pft_size; 7729b6b563cSPaul Mackerras u32 splpar; 7739b6b563cSPaul Mackerras u32 min_load; 7749b6b563cSPaul Mackerras u32 new_mem_def; 7759b6b563cSPaul Mackerras u32 ignore_me; 7769b6b563cSPaul Mackerras } rpadesc; 7779b6b563cSPaul Mackerras } rpanote; 7785663a123SPaul Mackerras } fake_elf = { 7799b6b563cSPaul Mackerras .elfhdr = { 7809b6b563cSPaul Mackerras .e_ident = { 0x7f, 'E', 'L', 'F', 7819b6b563cSPaul Mackerras ELFCLASS32, ELFDATA2MSB, EV_CURRENT }, 7829b6b563cSPaul Mackerras .e_type = ET_EXEC, /* yeah right */ 7839b6b563cSPaul Mackerras .e_machine = EM_PPC, 7849b6b563cSPaul Mackerras .e_version = EV_CURRENT, 7859b6b563cSPaul Mackerras .e_phoff = offsetof(struct fake_elf, phdr), 7869b6b563cSPaul Mackerras .e_phentsize = sizeof(Elf32_Phdr), 7879b6b563cSPaul Mackerras .e_phnum = 2 7889b6b563cSPaul Mackerras }, 7899b6b563cSPaul Mackerras .phdr = { 7909b6b563cSPaul Mackerras [0] = { 7919b6b563cSPaul Mackerras .p_type = PT_NOTE, 7929b6b563cSPaul Mackerras .p_offset = offsetof(struct fake_elf, chrpnote), 7939b6b563cSPaul Mackerras .p_filesz = sizeof(struct chrpnote) 7949b6b563cSPaul Mackerras }, [1] = { 7959b6b563cSPaul Mackerras .p_type = PT_NOTE, 7969b6b563cSPaul Mackerras .p_offset = offsetof(struct fake_elf, rpanote), 7979b6b563cSPaul Mackerras .p_filesz = sizeof(struct rpanote) 7989b6b563cSPaul Mackerras } 7999b6b563cSPaul Mackerras }, 8009b6b563cSPaul Mackerras .chrpnote = { 8019b6b563cSPaul Mackerras .namesz = sizeof("PowerPC"), 8029b6b563cSPaul Mackerras .descsz = sizeof(struct chrpdesc), 8039b6b563cSPaul Mackerras .type = 0x1275, 8049b6b563cSPaul Mackerras .name = "PowerPC", 8059b6b563cSPaul Mackerras .chrpdesc = { 8069b6b563cSPaul Mackerras .real_mode = ~0U, /* ~0 means "don't care" */ 8079b6b563cSPaul Mackerras .real_base = ~0U, 8089b6b563cSPaul Mackerras .real_size = ~0U, 8099b6b563cSPaul Mackerras .virt_base = ~0U, 8109b6b563cSPaul Mackerras .virt_size = ~0U, 8119b6b563cSPaul Mackerras .load_base = ~0U 8129b6b563cSPaul Mackerras }, 8139b6b563cSPaul Mackerras }, 8149b6b563cSPaul Mackerras .rpanote = { 8159b6b563cSPaul Mackerras .namesz = sizeof("IBM,RPA-Client-Config"), 8169b6b563cSPaul Mackerras .descsz = sizeof(struct rpadesc), 8179b6b563cSPaul Mackerras .type = 0x12759999, 8189b6b563cSPaul Mackerras .name = "IBM,RPA-Client-Config", 8199b6b563cSPaul Mackerras .rpadesc = { 8205663a123SPaul Mackerras .lpar_affinity = 0, 8215663a123SPaul Mackerras .min_rmo_size = 64, /* in megabytes */ 8229b6b563cSPaul Mackerras .min_rmo_percent = 0, 8235663a123SPaul Mackerras .max_pft_size = 48, /* 2^48 bytes max PFT size */ 8249b6b563cSPaul Mackerras .splpar = 1, 8259b6b563cSPaul Mackerras .min_load = ~0U, 8265663a123SPaul Mackerras .new_mem_def = 0 8279b6b563cSPaul Mackerras } 8289b6b563cSPaul Mackerras } 8299b6b563cSPaul Mackerras }; 830493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */ 8319b6b563cSPaul Mackerras 832efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void) 833efec959fSBenjamin Herrenschmidt { 834efec959fSBenjamin Herrenschmidt phandle node; 835efec959fSBenjamin Herrenschmidt char type[64]; 836efec959fSBenjamin Herrenschmidt unsigned int plen; 837efec959fSBenjamin Herrenschmidt 838efec959fSBenjamin Herrenschmidt /* Pick up th first CPU node we can find */ 839efec959fSBenjamin Herrenschmidt for (node = 0; prom_next_node(&node); ) { 840efec959fSBenjamin Herrenschmidt type[0] = 0; 841efec959fSBenjamin Herrenschmidt prom_getprop(node, "device_type", type, sizeof(type)); 842efec959fSBenjamin Herrenschmidt 8435827d416SAnton Blanchard if (strcmp(type, "cpu")) 844efec959fSBenjamin Herrenschmidt continue; 845efec959fSBenjamin Herrenschmidt /* 846efec959fSBenjamin Herrenschmidt * There is an entry for each smt thread, each entry being 847efec959fSBenjamin Herrenschmidt * 4 bytes long. All cpus should have the same number of 848efec959fSBenjamin Herrenschmidt * smt threads, so return after finding the first. 849efec959fSBenjamin Herrenschmidt */ 850efec959fSBenjamin Herrenschmidt plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s"); 851efec959fSBenjamin Herrenschmidt if (plen == PROM_ERROR) 852efec959fSBenjamin Herrenschmidt break; 853efec959fSBenjamin Herrenschmidt plen >>= 2; 8542c48a7d6SMichael Neuling prom_debug("Found %lu smt threads per core\n", (unsigned long)plen); 855efec959fSBenjamin Herrenschmidt 856efec959fSBenjamin Herrenschmidt /* Sanity check */ 857efec959fSBenjamin Herrenschmidt if (plen < 1 || plen > 64) { 8582c48a7d6SMichael Neuling prom_printf("Threads per core %lu out of bounds, assuming 1\n", 859efec959fSBenjamin Herrenschmidt (unsigned long)plen); 860efec959fSBenjamin Herrenschmidt return 1; 861efec959fSBenjamin Herrenschmidt } 862efec959fSBenjamin Herrenschmidt return plen; 863efec959fSBenjamin Herrenschmidt } 864efec959fSBenjamin Herrenschmidt prom_debug("No threads found, assuming 1 per core\n"); 865efec959fSBenjamin Herrenschmidt 866efec959fSBenjamin Herrenschmidt return 1; 867efec959fSBenjamin Herrenschmidt 868efec959fSBenjamin Herrenschmidt } 869efec959fSBenjamin Herrenschmidt 870efec959fSBenjamin Herrenschmidt 8719b6b563cSPaul Mackerras static void __init prom_send_capabilities(void) 8729b6b563cSPaul Mackerras { 873493adffcSBenjamin Herrenschmidt ihandle root; 874f709bfacSPaul Mackerras prom_arg_t ret; 875dbd0c5d5SLaurent Dufour u32 cores; 876dbd0c5d5SLaurent Dufour unsigned char *ptcores; 8779b6b563cSPaul Mackerras 878f709bfacSPaul Mackerras root = call_prom("open", 1, 1, ADDR("/")); 879f709bfacSPaul Mackerras if (root != 0) { 880efec959fSBenjamin Herrenschmidt /* We need to tell the FW about the number of cores we support. 881efec959fSBenjamin Herrenschmidt * 882efec959fSBenjamin Herrenschmidt * To do that, we count the number of threads on the first core 883efec959fSBenjamin Herrenschmidt * (we assume this is the same for all cores) and use it to 884efec959fSBenjamin Herrenschmidt * divide NR_CPUS. 885efec959fSBenjamin Herrenschmidt */ 886dbd0c5d5SLaurent Dufour 887dbd0c5d5SLaurent Dufour /* The core value may start at an odd address. If such a word 888dbd0c5d5SLaurent Dufour * access is made at a cache line boundary, this leads to an 889dbd0c5d5SLaurent Dufour * exception which may not be handled at this time. 890dbd0c5d5SLaurent Dufour * Forcing a per byte access to avoid exception. 891dbd0c5d5SLaurent Dufour */ 892dbd0c5d5SLaurent Dufour ptcores = &ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET]; 893dbd0c5d5SLaurent Dufour cores = 0; 894dbd0c5d5SLaurent Dufour cores |= ptcores[0] << 24; 895dbd0c5d5SLaurent Dufour cores |= ptcores[1] << 16; 896dbd0c5d5SLaurent Dufour cores |= ptcores[2] << 8; 897dbd0c5d5SLaurent Dufour cores |= ptcores[3]; 898dbd0c5d5SLaurent Dufour if (cores != NR_CPUS) { 899efec959fSBenjamin Herrenschmidt prom_printf("WARNING ! " 9002c48a7d6SMichael Neuling "ibm_architecture_vec structure inconsistent: %lu!\n", 901dbd0c5d5SLaurent Dufour cores); 902efec959fSBenjamin Herrenschmidt } else { 903dbd0c5d5SLaurent Dufour cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads()); 9042c48a7d6SMichael Neuling prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n", 905dbd0c5d5SLaurent Dufour cores, NR_CPUS); 906dbd0c5d5SLaurent Dufour ptcores[0] = (cores >> 24) & 0xff; 907dbd0c5d5SLaurent Dufour ptcores[1] = (cores >> 16) & 0xff; 908dbd0c5d5SLaurent Dufour ptcores[2] = (cores >> 8) & 0xff; 909dbd0c5d5SLaurent Dufour ptcores[3] = cores & 0xff; 910efec959fSBenjamin Herrenschmidt } 911efec959fSBenjamin Herrenschmidt 912f709bfacSPaul Mackerras /* try calling the ibm,client-architecture-support method */ 913049d0497SAnton Blanchard prom_printf("Calling ibm,client-architecture-support..."); 914f709bfacSPaul Mackerras if (call_prom_ret("call-method", 3, 2, &ret, 915f709bfacSPaul Mackerras ADDR("ibm,client-architecture-support"), 91633b74977SBenjamin Herrenschmidt root, 917f709bfacSPaul Mackerras ADDR(ibm_architecture_vec)) == 0) { 918f709bfacSPaul Mackerras /* the call exists... */ 919f709bfacSPaul Mackerras if (ret) 9204da727aeSAnton Blanchard prom_printf("\nWARNING: ibm,client-architecture" 921f709bfacSPaul Mackerras "-support call FAILED!\n"); 922f709bfacSPaul Mackerras call_prom("close", 1, 0, root); 9234da727aeSAnton Blanchard prom_printf(" done\n"); 924f709bfacSPaul Mackerras return; 925f709bfacSPaul Mackerras } 926f709bfacSPaul Mackerras call_prom("close", 1, 0, root); 927049d0497SAnton Blanchard prom_printf(" not implemented\n"); 928f709bfacSPaul Mackerras } 929f709bfacSPaul Mackerras 930493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__ 931493adffcSBenjamin Herrenschmidt { 932493adffcSBenjamin Herrenschmidt ihandle elfloader; 933493adffcSBenjamin Herrenschmidt 934f709bfacSPaul Mackerras /* no ibm,client-architecture-support call, try the old way */ 935493adffcSBenjamin Herrenschmidt elfloader = call_prom("open", 1, 1, 936493adffcSBenjamin Herrenschmidt ADDR("/packages/elf-loader")); 9379b6b563cSPaul Mackerras if (elfloader == 0) { 9389b6b563cSPaul Mackerras prom_printf("couldn't open /packages/elf-loader\n"); 9399b6b563cSPaul Mackerras return; 9409b6b563cSPaul Mackerras } 9419b6b563cSPaul Mackerras call_prom("call-method", 3, 1, ADDR("process-elf-header"), 9429b6b563cSPaul Mackerras elfloader, ADDR(&fake_elf)); 9439b6b563cSPaul Mackerras call_prom("close", 1, 0, elfloader); 9449b6b563cSPaul Mackerras } 945493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */ 946493adffcSBenjamin Herrenschmidt } 947493adffcSBenjamin Herrenschmidt #endif /* #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV) */ 9489b6b563cSPaul Mackerras 9499b6b563cSPaul Mackerras /* 9509b6b563cSPaul Mackerras * Memory allocation strategy... our layout is normally: 9519b6b563cSPaul Mackerras * 9529b6b563cSPaul Mackerras * at 14Mb or more we have vmlinux, then a gap and initrd. In some 9539b6b563cSPaul Mackerras * rare cases, initrd might end up being before the kernel though. 9549b6b563cSPaul Mackerras * We assume this won't override the final kernel at 0, we have no 9559b6b563cSPaul Mackerras * provision to handle that in this version, but it should hopefully 9569b6b563cSPaul Mackerras * never happen. 9579b6b563cSPaul Mackerras * 9589b6b563cSPaul Mackerras * alloc_top is set to the top of RMO, eventually shrink down if the 9599b6b563cSPaul Mackerras * TCEs overlap 9609b6b563cSPaul Mackerras * 9619b6b563cSPaul Mackerras * alloc_bottom is set to the top of kernel/initrd 9629b6b563cSPaul Mackerras * 9639b6b563cSPaul Mackerras * from there, allocations are done this way : rtas is allocated 9649b6b563cSPaul Mackerras * topmost, and the device-tree is allocated from the bottom. We try 9659b6b563cSPaul Mackerras * to grow the device-tree allocation as we progress. If we can't, 9669b6b563cSPaul Mackerras * then we fail, we don't currently have a facility to restart 9679b6b563cSPaul Mackerras * elsewhere, but that shouldn't be necessary. 9689b6b563cSPaul Mackerras * 9699b6b563cSPaul Mackerras * Note that calls to reserve_mem have to be done explicitly, memory 9709b6b563cSPaul Mackerras * allocated with either alloc_up or alloc_down isn't automatically 9719b6b563cSPaul Mackerras * reserved. 9729b6b563cSPaul Mackerras */ 9739b6b563cSPaul Mackerras 9749b6b563cSPaul Mackerras 9759b6b563cSPaul Mackerras /* 9769b6b563cSPaul Mackerras * Allocates memory in the RMO upward from the kernel/initrd 9779b6b563cSPaul Mackerras * 9789b6b563cSPaul Mackerras * When align is 0, this is a special case, it means to allocate in place 9799b6b563cSPaul Mackerras * at the current location of alloc_bottom or fail (that is basically 9809b6b563cSPaul Mackerras * extending the previous allocation). Used for the device-tree flattening 9819b6b563cSPaul Mackerras */ 9829b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align) 9839b6b563cSPaul Mackerras { 9845827d416SAnton Blanchard unsigned long base = alloc_bottom; 9859b6b563cSPaul Mackerras unsigned long addr = 0; 9869b6b563cSPaul Mackerras 987c4988820SPaul Mackerras if (align) 988c4988820SPaul Mackerras base = _ALIGN_UP(base, align); 9899b6b563cSPaul Mackerras prom_debug("alloc_up(%x, %x)\n", size, align); 9905827d416SAnton Blanchard if (ram_top == 0) 9919b6b563cSPaul Mackerras prom_panic("alloc_up() called with mem not initialized\n"); 9929b6b563cSPaul Mackerras 9939b6b563cSPaul Mackerras if (align) 9945827d416SAnton Blanchard base = _ALIGN_UP(alloc_bottom, align); 9959b6b563cSPaul Mackerras else 9965827d416SAnton Blanchard base = alloc_bottom; 9979b6b563cSPaul Mackerras 9985827d416SAnton Blanchard for(; (base + size) <= alloc_top; 9999b6b563cSPaul Mackerras base = _ALIGN_UP(base + 0x100000, align)) { 10009b6b563cSPaul Mackerras prom_debug(" trying: 0x%x\n\r", base); 10019b6b563cSPaul Mackerras addr = (unsigned long)prom_claim(base, size, 0); 1002c4988820SPaul Mackerras if (addr != PROM_ERROR && addr != 0) 10039b6b563cSPaul Mackerras break; 10049b6b563cSPaul Mackerras addr = 0; 10059b6b563cSPaul Mackerras if (align == 0) 10069b6b563cSPaul Mackerras break; 10079b6b563cSPaul Mackerras } 10089b6b563cSPaul Mackerras if (addr == 0) 10099b6b563cSPaul Mackerras return 0; 10105827d416SAnton Blanchard alloc_bottom = addr + size; 10119b6b563cSPaul Mackerras 10129b6b563cSPaul Mackerras prom_debug(" -> %x\n", addr); 10135827d416SAnton Blanchard prom_debug(" alloc_bottom : %x\n", alloc_bottom); 10145827d416SAnton Blanchard prom_debug(" alloc_top : %x\n", alloc_top); 10155827d416SAnton Blanchard prom_debug(" alloc_top_hi : %x\n", alloc_top_high); 10165827d416SAnton Blanchard prom_debug(" rmo_top : %x\n", rmo_top); 10175827d416SAnton Blanchard prom_debug(" ram_top : %x\n", ram_top); 10189b6b563cSPaul Mackerras 10199b6b563cSPaul Mackerras return addr; 10209b6b563cSPaul Mackerras } 10219b6b563cSPaul Mackerras 10229b6b563cSPaul Mackerras /* 10239b6b563cSPaul Mackerras * Allocates memory downward, either from top of RMO, or if highmem 10249b6b563cSPaul Mackerras * is set, from the top of RAM. Note that this one doesn't handle 10259b6b563cSPaul Mackerras * failures. It does claim memory if highmem is not set. 10269b6b563cSPaul Mackerras */ 10279b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align, 10289b6b563cSPaul Mackerras int highmem) 10299b6b563cSPaul Mackerras { 10309b6b563cSPaul Mackerras unsigned long base, addr = 0; 10319b6b563cSPaul Mackerras 10329b6b563cSPaul Mackerras prom_debug("alloc_down(%x, %x, %s)\n", size, align, 10335827d416SAnton Blanchard highmem ? "(high)" : "(low)"); 10345827d416SAnton Blanchard if (ram_top == 0) 10359b6b563cSPaul Mackerras prom_panic("alloc_down() called with mem not initialized\n"); 10369b6b563cSPaul Mackerras 10379b6b563cSPaul Mackerras if (highmem) { 10389b6b563cSPaul Mackerras /* Carve out storage for the TCE table. */ 10395827d416SAnton Blanchard addr = _ALIGN_DOWN(alloc_top_high - size, align); 10405827d416SAnton Blanchard if (addr <= alloc_bottom) 10419b6b563cSPaul Mackerras return 0; 10429b6b563cSPaul Mackerras /* Will we bump into the RMO ? If yes, check out that we 10439b6b563cSPaul Mackerras * didn't overlap existing allocations there, if we did, 10449b6b563cSPaul Mackerras * we are dead, we must be the first in town ! 10459b6b563cSPaul Mackerras */ 10465827d416SAnton Blanchard if (addr < rmo_top) { 10479b6b563cSPaul Mackerras /* Good, we are first */ 10485827d416SAnton Blanchard if (alloc_top == rmo_top) 10495827d416SAnton Blanchard alloc_top = rmo_top = addr; 10509b6b563cSPaul Mackerras else 10519b6b563cSPaul Mackerras return 0; 10529b6b563cSPaul Mackerras } 10535827d416SAnton Blanchard alloc_top_high = addr; 10549b6b563cSPaul Mackerras goto bail; 10559b6b563cSPaul Mackerras } 10569b6b563cSPaul Mackerras 10575827d416SAnton Blanchard base = _ALIGN_DOWN(alloc_top - size, align); 10585827d416SAnton Blanchard for (; base > alloc_bottom; 10599b6b563cSPaul Mackerras base = _ALIGN_DOWN(base - 0x100000, align)) { 10609b6b563cSPaul Mackerras prom_debug(" trying: 0x%x\n\r", base); 10619b6b563cSPaul Mackerras addr = (unsigned long)prom_claim(base, size, 0); 1062c4988820SPaul Mackerras if (addr != PROM_ERROR && addr != 0) 10639b6b563cSPaul Mackerras break; 10649b6b563cSPaul Mackerras addr = 0; 10659b6b563cSPaul Mackerras } 10669b6b563cSPaul Mackerras if (addr == 0) 10679b6b563cSPaul Mackerras return 0; 10685827d416SAnton Blanchard alloc_top = addr; 10699b6b563cSPaul Mackerras 10709b6b563cSPaul Mackerras bail: 10719b6b563cSPaul Mackerras prom_debug(" -> %x\n", addr); 10725827d416SAnton Blanchard prom_debug(" alloc_bottom : %x\n", alloc_bottom); 10735827d416SAnton Blanchard prom_debug(" alloc_top : %x\n", alloc_top); 10745827d416SAnton Blanchard prom_debug(" alloc_top_hi : %x\n", alloc_top_high); 10755827d416SAnton Blanchard prom_debug(" rmo_top : %x\n", rmo_top); 10765827d416SAnton Blanchard prom_debug(" ram_top : %x\n", ram_top); 10779b6b563cSPaul Mackerras 10789b6b563cSPaul Mackerras return addr; 10799b6b563cSPaul Mackerras } 10809b6b563cSPaul Mackerras 10819b6b563cSPaul Mackerras /* 10829b6b563cSPaul Mackerras * Parse a "reg" cell 10839b6b563cSPaul Mackerras */ 10849b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp) 10859b6b563cSPaul Mackerras { 10869b6b563cSPaul Mackerras cell_t *p = *cellp; 10879b6b563cSPaul Mackerras unsigned long r = 0; 10889b6b563cSPaul Mackerras 10899b6b563cSPaul Mackerras /* Ignore more than 2 cells */ 10909b6b563cSPaul Mackerras while (s > sizeof(unsigned long) / 4) { 10919b6b563cSPaul Mackerras p++; 10929b6b563cSPaul Mackerras s--; 10939b6b563cSPaul Mackerras } 1094493adffcSBenjamin Herrenschmidt r = be32_to_cpu(*p++); 10959b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 109635499c01SPaul Mackerras if (s > 1) { 10979b6b563cSPaul Mackerras r <<= 32; 1098493adffcSBenjamin Herrenschmidt r |= be32_to_cpu(*(p++)); 10999b6b563cSPaul Mackerras } 11009b6b563cSPaul Mackerras #endif 11019b6b563cSPaul Mackerras *cellp = p; 11029b6b563cSPaul Mackerras return r; 11039b6b563cSPaul Mackerras } 11049b6b563cSPaul Mackerras 11059b6b563cSPaul Mackerras /* 11069b6b563cSPaul Mackerras * Very dumb function for adding to the memory reserve list, but 11079b6b563cSPaul Mackerras * we don't need anything smarter at this point 11089b6b563cSPaul Mackerras * 11099b6b563cSPaul Mackerras * XXX Eventually check for collisions. They should NEVER happen. 11109b6b563cSPaul Mackerras * If problems seem to show up, it would be a good start to track 11119b6b563cSPaul Mackerras * them down. 11129b6b563cSPaul Mackerras */ 11130108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size) 11149b6b563cSPaul Mackerras { 1115cbbcf340SKumar Gala u64 top = base + size; 11165827d416SAnton Blanchard unsigned long cnt = mem_reserve_cnt; 11179b6b563cSPaul Mackerras 11189b6b563cSPaul Mackerras if (size == 0) 11199b6b563cSPaul Mackerras return; 11209b6b563cSPaul Mackerras 11219b6b563cSPaul Mackerras /* We need to always keep one empty entry so that we 11229b6b563cSPaul Mackerras * have our terminator with "size" set to 0 since we are 11239b6b563cSPaul Mackerras * dumb and just copy this entire array to the boot params 11249b6b563cSPaul Mackerras */ 11259b6b563cSPaul Mackerras base = _ALIGN_DOWN(base, PAGE_SIZE); 11269b6b563cSPaul Mackerras top = _ALIGN_UP(top, PAGE_SIZE); 11279b6b563cSPaul Mackerras size = top - base; 11289b6b563cSPaul Mackerras 11299b6b563cSPaul Mackerras if (cnt >= (MEM_RESERVE_MAP_SIZE - 1)) 11309b6b563cSPaul Mackerras prom_panic("Memory reserve map exhausted !\n"); 1131493adffcSBenjamin Herrenschmidt mem_reserve_map[cnt].base = cpu_to_be64(base); 1132493adffcSBenjamin Herrenschmidt mem_reserve_map[cnt].size = cpu_to_be64(size); 11335827d416SAnton Blanchard mem_reserve_cnt = cnt + 1; 11349b6b563cSPaul Mackerras } 11359b6b563cSPaul Mackerras 11369b6b563cSPaul Mackerras /* 1137b3c2ffd5SAdrian Bunk * Initialize memory allocation mechanism, parse "memory" nodes and 11389b6b563cSPaul Mackerras * obtain that way the top of memory and RMO to setup out local allocator 11399b6b563cSPaul Mackerras */ 11409b6b563cSPaul Mackerras static void __init prom_init_mem(void) 11419b6b563cSPaul Mackerras { 11429b6b563cSPaul Mackerras phandle node; 11439b6b563cSPaul Mackerras char *path, type[64]; 11449b6b563cSPaul Mackerras unsigned int plen; 11459b6b563cSPaul Mackerras cell_t *p, *endp; 1146493adffcSBenjamin Herrenschmidt __be32 val; 11479b6b563cSPaul Mackerras u32 rac, rsc; 11489b6b563cSPaul Mackerras 11499b6b563cSPaul Mackerras /* 11509b6b563cSPaul Mackerras * We iterate the memory nodes to find 11519b6b563cSPaul Mackerras * 1) top of RMO (first node) 11529b6b563cSPaul Mackerras * 2) top of memory 11539b6b563cSPaul Mackerras */ 1154493adffcSBenjamin Herrenschmidt val = cpu_to_be32(2); 1155493adffcSBenjamin Herrenschmidt prom_getprop(prom.root, "#address-cells", &val, sizeof(val)); 1156493adffcSBenjamin Herrenschmidt rac = be32_to_cpu(val); 1157493adffcSBenjamin Herrenschmidt val = cpu_to_be32(1); 1158493adffcSBenjamin Herrenschmidt prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc)); 1159493adffcSBenjamin Herrenschmidt rsc = be32_to_cpu(val); 1160493adffcSBenjamin Herrenschmidt prom_debug("root_addr_cells: %x\n", rac); 1161493adffcSBenjamin Herrenschmidt prom_debug("root_size_cells: %x\n", rsc); 11629b6b563cSPaul Mackerras 11639b6b563cSPaul Mackerras prom_debug("scanning memory:\n"); 11645827d416SAnton Blanchard path = prom_scratch; 11659b6b563cSPaul Mackerras 11669b6b563cSPaul Mackerras for (node = 0; prom_next_node(&node); ) { 11679b6b563cSPaul Mackerras type[0] = 0; 11689b6b563cSPaul Mackerras prom_getprop(node, "device_type", type, sizeof(type)); 11699b6b563cSPaul Mackerras 1170c4988820SPaul Mackerras if (type[0] == 0) { 1171c4988820SPaul Mackerras /* 1172c4988820SPaul Mackerras * CHRP Longtrail machines have no device_type 1173c4988820SPaul Mackerras * on the memory node, so check the name instead... 1174c4988820SPaul Mackerras */ 1175c4988820SPaul Mackerras prom_getprop(node, "name", type, sizeof(type)); 1176c4988820SPaul Mackerras } 11775827d416SAnton Blanchard if (strcmp(type, "memory")) 11789b6b563cSPaul Mackerras continue; 11799b6b563cSPaul Mackerras 11805827d416SAnton Blanchard plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf)); 11819b6b563cSPaul Mackerras if (plen > sizeof(regbuf)) { 11829b6b563cSPaul Mackerras prom_printf("memory node too large for buffer !\n"); 11839b6b563cSPaul Mackerras plen = sizeof(regbuf); 11849b6b563cSPaul Mackerras } 11855827d416SAnton Blanchard p = regbuf; 11869b6b563cSPaul Mackerras endp = p + (plen / sizeof(cell_t)); 11879b6b563cSPaul Mackerras 11889b6b563cSPaul Mackerras #ifdef DEBUG_PROM 11899b6b563cSPaul Mackerras memset(path, 0, PROM_SCRATCH_SIZE); 11909b6b563cSPaul Mackerras call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1); 11919b6b563cSPaul Mackerras prom_debug(" node %s :\n", path); 11929b6b563cSPaul Mackerras #endif /* DEBUG_PROM */ 11939b6b563cSPaul Mackerras 11949b6b563cSPaul Mackerras while ((endp - p) >= (rac + rsc)) { 11959b6b563cSPaul Mackerras unsigned long base, size; 11969b6b563cSPaul Mackerras 11979b6b563cSPaul Mackerras base = prom_next_cell(rac, &p); 11989b6b563cSPaul Mackerras size = prom_next_cell(rsc, &p); 11999b6b563cSPaul Mackerras 12009b6b563cSPaul Mackerras if (size == 0) 12019b6b563cSPaul Mackerras continue; 12029b6b563cSPaul Mackerras prom_debug(" %x %x\n", base, size); 12035827d416SAnton Blanchard if (base == 0 && (of_platform & PLATFORM_LPAR)) 12045827d416SAnton Blanchard rmo_top = size; 12055827d416SAnton Blanchard if ((base + size) > ram_top) 12065827d416SAnton Blanchard ram_top = base + size; 12079b6b563cSPaul Mackerras } 12089b6b563cSPaul Mackerras } 12099b6b563cSPaul Mackerras 12105827d416SAnton Blanchard alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000); 12119b6b563cSPaul Mackerras 12129b6b563cSPaul Mackerras /* 1213cf68787bSBenjamin Krill * If prom_memory_limit is set we reduce the upper limits *except* for 1214cf68787bSBenjamin Krill * alloc_top_high. This must be the real top of RAM so we can put 1215cf68787bSBenjamin Krill * TCE's up there. 1216cf68787bSBenjamin Krill */ 1217cf68787bSBenjamin Krill 12185827d416SAnton Blanchard alloc_top_high = ram_top; 1219cf68787bSBenjamin Krill 12205827d416SAnton Blanchard if (prom_memory_limit) { 12215827d416SAnton Blanchard if (prom_memory_limit <= alloc_bottom) { 1222cf68787bSBenjamin Krill prom_printf("Ignoring mem=%x <= alloc_bottom.\n", 12235827d416SAnton Blanchard prom_memory_limit); 12245827d416SAnton Blanchard prom_memory_limit = 0; 12255827d416SAnton Blanchard } else if (prom_memory_limit >= ram_top) { 1226cf68787bSBenjamin Krill prom_printf("Ignoring mem=%x >= ram_top.\n", 12275827d416SAnton Blanchard prom_memory_limit); 12285827d416SAnton Blanchard prom_memory_limit = 0; 1229cf68787bSBenjamin Krill } else { 12305827d416SAnton Blanchard ram_top = prom_memory_limit; 12315827d416SAnton Blanchard rmo_top = min(rmo_top, prom_memory_limit); 1232cf68787bSBenjamin Krill } 1233cf68787bSBenjamin Krill } 1234cf68787bSBenjamin Krill 1235cf68787bSBenjamin Krill /* 12369b6b563cSPaul Mackerras * Setup our top alloc point, that is top of RMO or top of 12379b6b563cSPaul Mackerras * segment 0 when running non-LPAR. 12389b6b563cSPaul Mackerras * Some RS64 machines have buggy firmware where claims up at 12399b6b563cSPaul Mackerras * 1GB fail. Cap at 768MB as a workaround. 12409b6b563cSPaul Mackerras * Since 768MB is plenty of room, and we need to cap to something 12419b6b563cSPaul Mackerras * reasonable on 32-bit, cap at 768MB on all machines. 12429b6b563cSPaul Mackerras */ 12435827d416SAnton Blanchard if (!rmo_top) 12445827d416SAnton Blanchard rmo_top = ram_top; 12455827d416SAnton Blanchard rmo_top = min(0x30000000ul, rmo_top); 12465827d416SAnton Blanchard alloc_top = rmo_top; 12475827d416SAnton Blanchard alloc_top_high = ram_top; 12489b6b563cSPaul Mackerras 124964968f60SPaul Mackerras /* 125064968f60SPaul Mackerras * Check if we have an initrd after the kernel but still inside 125164968f60SPaul Mackerras * the RMO. If we do move our bottom point to after it. 125264968f60SPaul Mackerras */ 12535827d416SAnton Blanchard if (prom_initrd_start && 12545827d416SAnton Blanchard prom_initrd_start < rmo_top && 12555827d416SAnton Blanchard prom_initrd_end > alloc_bottom) 12565827d416SAnton Blanchard alloc_bottom = PAGE_ALIGN(prom_initrd_end); 125764968f60SPaul Mackerras 12589b6b563cSPaul Mackerras prom_printf("memory layout at init:\n"); 12595827d416SAnton Blanchard prom_printf(" memory_limit : %x (16 MB aligned)\n", prom_memory_limit); 12605827d416SAnton Blanchard prom_printf(" alloc_bottom : %x\n", alloc_bottom); 12615827d416SAnton Blanchard prom_printf(" alloc_top : %x\n", alloc_top); 12625827d416SAnton Blanchard prom_printf(" alloc_top_hi : %x\n", alloc_top_high); 12635827d416SAnton Blanchard prom_printf(" rmo_top : %x\n", rmo_top); 12645827d416SAnton Blanchard prom_printf(" ram_top : %x\n", ram_top); 12659b6b563cSPaul Mackerras } 12669b6b563cSPaul Mackerras 126727f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void) 126827f44888SBenjamin Herrenschmidt { 1269493adffcSBenjamin Herrenschmidt __be32 val; 1270493adffcSBenjamin Herrenschmidt ihandle stdin; 127127f44888SBenjamin Herrenschmidt 1272493adffcSBenjamin Herrenschmidt if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) { 1273493adffcSBenjamin Herrenschmidt stdin = be32_to_cpu(val); 1274493adffcSBenjamin Herrenschmidt call_prom("close", 1, 0, stdin); 1275493adffcSBenjamin Herrenschmidt } 127627f44888SBenjamin Herrenschmidt } 127727f44888SBenjamin Herrenschmidt 127827f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV 127927f44888SBenjamin Herrenschmidt 12806e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL 12816e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_base; 12826e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_entry; 12836e35d5daSBenjamin Herrenschmidt #endif 12846e35d5daSBenjamin Herrenschmidt 12856e35d5daSBenjamin Herrenschmidt /* 12866e35d5daSBenjamin Herrenschmidt * Allocate room for and instantiate OPAL 12876e35d5daSBenjamin Herrenschmidt */ 12886e35d5daSBenjamin Herrenschmidt static void __init prom_instantiate_opal(void) 12896e35d5daSBenjamin Herrenschmidt { 12906e35d5daSBenjamin Herrenschmidt phandle opal_node; 12916e35d5daSBenjamin Herrenschmidt ihandle opal_inst; 12926e35d5daSBenjamin Herrenschmidt u64 base, entry; 12936e35d5daSBenjamin Herrenschmidt u64 size = 0, align = 0x10000; 1294493adffcSBenjamin Herrenschmidt __be64 val64; 12956e35d5daSBenjamin Herrenschmidt u32 rets[2]; 12966e35d5daSBenjamin Herrenschmidt 12976e35d5daSBenjamin Herrenschmidt prom_debug("prom_instantiate_opal: start...\n"); 12986e35d5daSBenjamin Herrenschmidt 12996e35d5daSBenjamin Herrenschmidt opal_node = call_prom("finddevice", 1, 1, ADDR("/ibm,opal")); 13006e35d5daSBenjamin Herrenschmidt prom_debug("opal_node: %x\n", opal_node); 13016e35d5daSBenjamin Herrenschmidt if (!PHANDLE_VALID(opal_node)) 13026e35d5daSBenjamin Herrenschmidt return; 13036e35d5daSBenjamin Herrenschmidt 1304493adffcSBenjamin Herrenschmidt val64 = 0; 1305493adffcSBenjamin Herrenschmidt prom_getprop(opal_node, "opal-runtime-size", &val64, sizeof(val64)); 1306493adffcSBenjamin Herrenschmidt size = be64_to_cpu(val64); 13076e35d5daSBenjamin Herrenschmidt if (size == 0) 13086e35d5daSBenjamin Herrenschmidt return; 1309493adffcSBenjamin Herrenschmidt val64 = 0; 1310493adffcSBenjamin Herrenschmidt prom_getprop(opal_node, "opal-runtime-alignment", &val64,sizeof(val64)); 1311493adffcSBenjamin Herrenschmidt align = be64_to_cpu(val64); 13126e35d5daSBenjamin Herrenschmidt 13136e35d5daSBenjamin Herrenschmidt base = alloc_down(size, align, 0); 13146e35d5daSBenjamin Herrenschmidt if (base == 0) { 13156e35d5daSBenjamin Herrenschmidt prom_printf("OPAL allocation failed !\n"); 13166e35d5daSBenjamin Herrenschmidt return; 13176e35d5daSBenjamin Herrenschmidt } 13186e35d5daSBenjamin Herrenschmidt 13196e35d5daSBenjamin Herrenschmidt opal_inst = call_prom("open", 1, 1, ADDR("/ibm,opal")); 13206e35d5daSBenjamin Herrenschmidt if (!IHANDLE_VALID(opal_inst)) { 13216e35d5daSBenjamin Herrenschmidt prom_printf("opening opal package failed (%x)\n", opal_inst); 13226e35d5daSBenjamin Herrenschmidt return; 13236e35d5daSBenjamin Herrenschmidt } 13246e35d5daSBenjamin Herrenschmidt 13256e35d5daSBenjamin Herrenschmidt prom_printf("instantiating opal at 0x%x...", base); 13266e35d5daSBenjamin Herrenschmidt 13276e35d5daSBenjamin Herrenschmidt if (call_prom_ret("call-method", 4, 3, rets, 13286e35d5daSBenjamin Herrenschmidt ADDR("load-opal-runtime"), 13296e35d5daSBenjamin Herrenschmidt opal_inst, 13306e35d5daSBenjamin Herrenschmidt base >> 32, base & 0xffffffff) != 0 13316e35d5daSBenjamin Herrenschmidt || (rets[0] == 0 && rets[1] == 0)) { 13326e35d5daSBenjamin Herrenschmidt prom_printf(" failed\n"); 13336e35d5daSBenjamin Herrenschmidt return; 13346e35d5daSBenjamin Herrenschmidt } 13356e35d5daSBenjamin Herrenschmidt entry = (((u64)rets[0]) << 32) | rets[1]; 13366e35d5daSBenjamin Herrenschmidt 13376e35d5daSBenjamin Herrenschmidt prom_printf(" done\n"); 13386e35d5daSBenjamin Herrenschmidt 13396e35d5daSBenjamin Herrenschmidt reserve_mem(base, size); 13406e35d5daSBenjamin Herrenschmidt 13416e35d5daSBenjamin Herrenschmidt prom_debug("opal base = 0x%x\n", base); 13426e35d5daSBenjamin Herrenschmidt prom_debug("opal align = 0x%x\n", align); 13436e35d5daSBenjamin Herrenschmidt prom_debug("opal entry = 0x%x\n", entry); 13446e35d5daSBenjamin Herrenschmidt prom_debug("opal size = 0x%x\n", (long)size); 13456e35d5daSBenjamin Herrenschmidt 13466e35d5daSBenjamin Herrenschmidt prom_setprop(opal_node, "/ibm,opal", "opal-base-address", 13476e35d5daSBenjamin Herrenschmidt &base, sizeof(base)); 13486e35d5daSBenjamin Herrenschmidt prom_setprop(opal_node, "/ibm,opal", "opal-entry-address", 13496e35d5daSBenjamin Herrenschmidt &entry, sizeof(entry)); 13506e35d5daSBenjamin Herrenschmidt 13516e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL 13525827d416SAnton Blanchard prom_opal_base = base; 13535827d416SAnton Blanchard prom_opal_entry = entry; 13546e35d5daSBenjamin Herrenschmidt #endif 13556e35d5daSBenjamin Herrenschmidt prom_debug("prom_instantiate_opal: end...\n"); 13566e35d5daSBenjamin Herrenschmidt } 13576e35d5daSBenjamin Herrenschmidt 135827f44888SBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV */ 13599b6b563cSPaul Mackerras 13609b6b563cSPaul Mackerras /* 13619b6b563cSPaul Mackerras * Allocate room for and instantiate RTAS 13629b6b563cSPaul Mackerras */ 13639b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void) 13649b6b563cSPaul Mackerras { 13659b6b563cSPaul Mackerras phandle rtas_node; 13669b6b563cSPaul Mackerras ihandle rtas_inst; 13679b6b563cSPaul Mackerras u32 base, entry = 0; 1368493adffcSBenjamin Herrenschmidt __be32 val; 13699b6b563cSPaul Mackerras u32 size = 0; 13709b6b563cSPaul Mackerras 13719b6b563cSPaul Mackerras prom_debug("prom_instantiate_rtas: start...\n"); 13729b6b563cSPaul Mackerras 13739b6b563cSPaul Mackerras rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas")); 13749b6b563cSPaul Mackerras prom_debug("rtas_node: %x\n", rtas_node); 13759b6b563cSPaul Mackerras if (!PHANDLE_VALID(rtas_node)) 13769b6b563cSPaul Mackerras return; 13779b6b563cSPaul Mackerras 1378493adffcSBenjamin Herrenschmidt val = 0; 1379493adffcSBenjamin Herrenschmidt prom_getprop(rtas_node, "rtas-size", &val, sizeof(size)); 1380493adffcSBenjamin Herrenschmidt size = be32_to_cpu(val); 13819b6b563cSPaul Mackerras if (size == 0) 13829b6b563cSPaul Mackerras return; 13839b6b563cSPaul Mackerras 13849b6b563cSPaul Mackerras base = alloc_down(size, PAGE_SIZE, 0); 13856d1e2c6cSAnton Blanchard if (base == 0) 13866d1e2c6cSAnton Blanchard prom_panic("Could not allocate memory for RTAS\n"); 13879b6b563cSPaul Mackerras 13889b6b563cSPaul Mackerras rtas_inst = call_prom("open", 1, 1, ADDR("/rtas")); 13899b6b563cSPaul Mackerras if (!IHANDLE_VALID(rtas_inst)) { 1390a23414beSPaul Mackerras prom_printf("opening rtas package failed (%x)\n", rtas_inst); 13919b6b563cSPaul Mackerras return; 13929b6b563cSPaul Mackerras } 13939b6b563cSPaul Mackerras 13949b6b563cSPaul Mackerras prom_printf("instantiating rtas at 0x%x...", base); 13959b6b563cSPaul Mackerras 13969b6b563cSPaul Mackerras if (call_prom_ret("call-method", 3, 2, &entry, 13979b6b563cSPaul Mackerras ADDR("instantiate-rtas"), 1398a23414beSPaul Mackerras rtas_inst, base) != 0 13999b6b563cSPaul Mackerras || entry == 0) { 14009b6b563cSPaul Mackerras prom_printf(" failed\n"); 14019b6b563cSPaul Mackerras return; 14029b6b563cSPaul Mackerras } 14039b6b563cSPaul Mackerras prom_printf(" done\n"); 14049b6b563cSPaul Mackerras 14059b6b563cSPaul Mackerras reserve_mem(base, size); 14069b6b563cSPaul Mackerras 1407493adffcSBenjamin Herrenschmidt val = cpu_to_be32(base); 1408a23414beSPaul Mackerras prom_setprop(rtas_node, "/rtas", "linux,rtas-base", 1409493adffcSBenjamin Herrenschmidt &val, sizeof(val)); 1410493adffcSBenjamin Herrenschmidt val = cpu_to_be32(entry); 1411a23414beSPaul Mackerras prom_setprop(rtas_node, "/rtas", "linux,rtas-entry", 1412493adffcSBenjamin Herrenschmidt &val, sizeof(val)); 14139b6b563cSPaul Mackerras 1414dbe78b40SBenjamin Herrenschmidt /* Check if it supports "query-cpu-stopped-state" */ 1415dbe78b40SBenjamin Herrenschmidt if (prom_getprop(rtas_node, "query-cpu-stopped-state", 1416dbe78b40SBenjamin Herrenschmidt &val, sizeof(val)) != PROM_ERROR) 1417dbe78b40SBenjamin Herrenschmidt rtas_has_query_cpu_stopped = true; 1418dbe78b40SBenjamin Herrenschmidt 14199b6b563cSPaul Mackerras prom_debug("rtas base = 0x%x\n", base); 14209b6b563cSPaul Mackerras prom_debug("rtas entry = 0x%x\n", entry); 14219b6b563cSPaul Mackerras prom_debug("rtas size = 0x%x\n", (long)size); 14229b6b563cSPaul Mackerras 14239b6b563cSPaul Mackerras prom_debug("prom_instantiate_rtas: end...\n"); 14249b6b563cSPaul Mackerras } 14259b6b563cSPaul Mackerras 14269b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 14279b6b563cSPaul Mackerras /* 14284a727429SAshley Lai * Allocate room for and instantiate Stored Measurement Log (SML) 14294a727429SAshley Lai */ 14304a727429SAshley Lai static void __init prom_instantiate_sml(void) 14314a727429SAshley Lai { 14324a727429SAshley Lai phandle ibmvtpm_node; 14334a727429SAshley Lai ihandle ibmvtpm_inst; 1434b4ed0469SHon Ching \(Vicky\) Lo u32 entry = 0, size = 0, succ = 0; 14354a727429SAshley Lai u64 base; 1436b4ed0469SHon Ching \(Vicky\) Lo __be32 val; 14374a727429SAshley Lai 14384a727429SAshley Lai prom_debug("prom_instantiate_sml: start...\n"); 14394a727429SAshley Lai 14402f82e982SHon Ching \(Vicky\) Lo ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm")); 14414a727429SAshley Lai prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node); 14424a727429SAshley Lai if (!PHANDLE_VALID(ibmvtpm_node)) 14434a727429SAshley Lai return; 14444a727429SAshley Lai 14452f82e982SHon Ching \(Vicky\) Lo ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm")); 14464a727429SAshley Lai if (!IHANDLE_VALID(ibmvtpm_inst)) { 14474a727429SAshley Lai prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst); 14484a727429SAshley Lai return; 14494a727429SAshley Lai } 14504a727429SAshley Lai 1451b4ed0469SHon Ching \(Vicky\) Lo if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported", 1452b4ed0469SHon Ching \(Vicky\) Lo &val, sizeof(val)) != PROM_ERROR) { 1453b4ed0469SHon Ching \(Vicky\) Lo if (call_prom_ret("call-method", 2, 2, &succ, 1454b4ed0469SHon Ching \(Vicky\) Lo ADDR("reformat-sml-to-efi-alignment"), 1455b4ed0469SHon Ching \(Vicky\) Lo ibmvtpm_inst) != 0 || succ == 0) { 1456b4ed0469SHon Ching \(Vicky\) Lo prom_printf("Reformat SML to EFI alignment failed\n"); 1457b4ed0469SHon Ching \(Vicky\) Lo return; 1458b4ed0469SHon Ching \(Vicky\) Lo } 1459b4ed0469SHon Ching \(Vicky\) Lo 14604a727429SAshley Lai if (call_prom_ret("call-method", 2, 2, &size, 14619e5d4af4SHon Ching \(Vicky\) Lo ADDR("sml-get-allocated-size"), 14629e5d4af4SHon Ching \(Vicky\) Lo ibmvtpm_inst) != 0 || size == 0) { 14639e5d4af4SHon Ching \(Vicky\) Lo prom_printf("SML get allocated size failed\n"); 14649e5d4af4SHon Ching \(Vicky\) Lo return; 14659e5d4af4SHon Ching \(Vicky\) Lo } 14669e5d4af4SHon Ching \(Vicky\) Lo } else { 14679e5d4af4SHon Ching \(Vicky\) Lo if (call_prom_ret("call-method", 2, 2, &size, 14684a727429SAshley Lai ADDR("sml-get-handover-size"), 14694a727429SAshley Lai ibmvtpm_inst) != 0 || size == 0) { 14704a727429SAshley Lai prom_printf("SML get handover size failed\n"); 14714a727429SAshley Lai return; 14724a727429SAshley Lai } 14739e5d4af4SHon Ching \(Vicky\) Lo } 14744a727429SAshley Lai 14754a727429SAshley Lai base = alloc_down(size, PAGE_SIZE, 0); 14764a727429SAshley Lai if (base == 0) 14774a727429SAshley Lai prom_panic("Could not allocate memory for sml\n"); 14784a727429SAshley Lai 14794a727429SAshley Lai prom_printf("instantiating sml at 0x%x...", base); 14804a727429SAshley Lai 14819e5d4af4SHon Ching \(Vicky\) Lo memset((void *)base, 0, size); 14829e5d4af4SHon Ching \(Vicky\) Lo 14834a727429SAshley Lai if (call_prom_ret("call-method", 4, 2, &entry, 14844a727429SAshley Lai ADDR("sml-handover"), 14854a727429SAshley Lai ibmvtpm_inst, size, base) != 0 || entry == 0) { 14864a727429SAshley Lai prom_printf("SML handover failed\n"); 14874a727429SAshley Lai return; 14884a727429SAshley Lai } 14894a727429SAshley Lai prom_printf(" done\n"); 14904a727429SAshley Lai 14914a727429SAshley Lai reserve_mem(base, size); 14924a727429SAshley Lai 14932f82e982SHon Ching \(Vicky\) Lo prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base", 14944a727429SAshley Lai &base, sizeof(base)); 14952f82e982SHon Ching \(Vicky\) Lo prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size", 14964a727429SAshley Lai &size, sizeof(size)); 14974a727429SAshley Lai 14984a727429SAshley Lai prom_debug("sml base = 0x%x\n", base); 14994a727429SAshley Lai prom_debug("sml size = 0x%x\n", (long)size); 15004a727429SAshley Lai 15014a727429SAshley Lai prom_debug("prom_instantiate_sml: end...\n"); 15024a727429SAshley Lai } 15034a727429SAshley Lai 15044a727429SAshley Lai /* 15059b6b563cSPaul Mackerras * Allocate room for and initialize TCE tables 15069b6b563cSPaul Mackerras */ 1507493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__ 15089b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void) 15099b6b563cSPaul Mackerras { 15109b6b563cSPaul Mackerras phandle node; 15119b6b563cSPaul Mackerras ihandle phb_node; 15129b6b563cSPaul Mackerras char compatible[64], type[64], model[64]; 15135827d416SAnton Blanchard char *path = prom_scratch; 15149b6b563cSPaul Mackerras u64 base, align; 15159b6b563cSPaul Mackerras u32 minalign, minsize; 15169b6b563cSPaul Mackerras u64 tce_entry, *tce_entryp; 15179b6b563cSPaul Mackerras u64 local_alloc_top, local_alloc_bottom; 15189b6b563cSPaul Mackerras u64 i; 15199b6b563cSPaul Mackerras 15205827d416SAnton Blanchard if (prom_iommu_off) 15219b6b563cSPaul Mackerras return; 15229b6b563cSPaul Mackerras 15239b6b563cSPaul Mackerras prom_debug("starting prom_initialize_tce_table\n"); 15249b6b563cSPaul Mackerras 15259b6b563cSPaul Mackerras /* Cache current top of allocs so we reserve a single block */ 15265827d416SAnton Blanchard local_alloc_top = alloc_top_high; 15279b6b563cSPaul Mackerras local_alloc_bottom = local_alloc_top; 15289b6b563cSPaul Mackerras 15299b6b563cSPaul Mackerras /* Search all nodes looking for PHBs. */ 15309b6b563cSPaul Mackerras for (node = 0; prom_next_node(&node); ) { 15319b6b563cSPaul Mackerras compatible[0] = 0; 15329b6b563cSPaul Mackerras type[0] = 0; 15339b6b563cSPaul Mackerras model[0] = 0; 15349b6b563cSPaul Mackerras prom_getprop(node, "compatible", 15359b6b563cSPaul Mackerras compatible, sizeof(compatible)); 15369b6b563cSPaul Mackerras prom_getprop(node, "device_type", type, sizeof(type)); 15379b6b563cSPaul Mackerras prom_getprop(node, "model", model, sizeof(model)); 15389b6b563cSPaul Mackerras 15395827d416SAnton Blanchard if ((type[0] == 0) || (strstr(type, "pci") == NULL)) 15409b6b563cSPaul Mackerras continue; 15419b6b563cSPaul Mackerras 1542e788ff13SLinas Vepstas /* Keep the old logic intact to avoid regression. */ 15439b6b563cSPaul Mackerras if (compatible[0] != 0) { 15445827d416SAnton Blanchard if ((strstr(compatible, "python") == NULL) && 15455827d416SAnton Blanchard (strstr(compatible, "Speedwagon") == NULL) && 15465827d416SAnton Blanchard (strstr(compatible, "Winnipeg") == NULL)) 15479b6b563cSPaul Mackerras continue; 15489b6b563cSPaul Mackerras } else if (model[0] != 0) { 15495827d416SAnton Blanchard if ((strstr(model, "ython") == NULL) && 15505827d416SAnton Blanchard (strstr(model, "peedwagon") == NULL) && 15515827d416SAnton Blanchard (strstr(model, "innipeg") == NULL)) 15529b6b563cSPaul Mackerras continue; 15539b6b563cSPaul Mackerras } 15549b6b563cSPaul Mackerras 15559b6b563cSPaul Mackerras if (prom_getprop(node, "tce-table-minalign", &minalign, 15569b6b563cSPaul Mackerras sizeof(minalign)) == PROM_ERROR) 15579b6b563cSPaul Mackerras minalign = 0; 15589b6b563cSPaul Mackerras if (prom_getprop(node, "tce-table-minsize", &minsize, 15599b6b563cSPaul Mackerras sizeof(minsize)) == PROM_ERROR) 15609b6b563cSPaul Mackerras minsize = 4UL << 20; 15619b6b563cSPaul Mackerras 15629b6b563cSPaul Mackerras /* 15639b6b563cSPaul Mackerras * Even though we read what OF wants, we just set the table 15649b6b563cSPaul Mackerras * size to 4 MB. This is enough to map 2GB of PCI DMA space. 15659b6b563cSPaul Mackerras * By doing this, we avoid the pitfalls of trying to DMA to 15669b6b563cSPaul Mackerras * MMIO space and the DMA alias hole. 15679b6b563cSPaul Mackerras * 15689b6b563cSPaul Mackerras * On POWER4, firmware sets the TCE region by assuming 15699b6b563cSPaul Mackerras * each TCE table is 8MB. Using this memory for anything 15709b6b563cSPaul Mackerras * else will impact performance, so we always allocate 8MB. 15719b6b563cSPaul Mackerras * Anton 15729b6b563cSPaul Mackerras */ 1573d3dbeef6SMichael Ellerman if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p)) 15749b6b563cSPaul Mackerras minsize = 8UL << 20; 15759b6b563cSPaul Mackerras else 15769b6b563cSPaul Mackerras minsize = 4UL << 20; 15779b6b563cSPaul Mackerras 15789b6b563cSPaul Mackerras /* Align to the greater of the align or size */ 15799b6b563cSPaul Mackerras align = max(minalign, minsize); 15809b6b563cSPaul Mackerras base = alloc_down(minsize, align, 1); 15819b6b563cSPaul Mackerras if (base == 0) 15829b6b563cSPaul Mackerras prom_panic("ERROR, cannot find space for TCE table.\n"); 15839b6b563cSPaul Mackerras if (base < local_alloc_bottom) 15849b6b563cSPaul Mackerras local_alloc_bottom = base; 15859b6b563cSPaul Mackerras 15869b6b563cSPaul Mackerras /* It seems OF doesn't null-terminate the path :-( */ 1587aca71ef8SLi Zefan memset(path, 0, PROM_SCRATCH_SIZE); 15889b6b563cSPaul Mackerras /* Call OF to setup the TCE hardware */ 15899b6b563cSPaul Mackerras if (call_prom("package-to-path", 3, 1, node, 15909b6b563cSPaul Mackerras path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) { 15919b6b563cSPaul Mackerras prom_printf("package-to-path failed\n"); 15929b6b563cSPaul Mackerras } 15939b6b563cSPaul Mackerras 1594a23414beSPaul Mackerras /* Save away the TCE table attributes for later use. */ 1595a23414beSPaul Mackerras prom_setprop(node, path, "linux,tce-base", &base, sizeof(base)); 1596a23414beSPaul Mackerras prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize)); 1597a23414beSPaul Mackerras 15989b6b563cSPaul Mackerras prom_debug("TCE table: %s\n", path); 15999b6b563cSPaul Mackerras prom_debug("\tnode = 0x%x\n", node); 16009b6b563cSPaul Mackerras prom_debug("\tbase = 0x%x\n", base); 16019b6b563cSPaul Mackerras prom_debug("\tsize = 0x%x\n", minsize); 16029b6b563cSPaul Mackerras 16039b6b563cSPaul Mackerras /* Initialize the table to have a one-to-one mapping 16049b6b563cSPaul Mackerras * over the allocated size. 16059b6b563cSPaul Mackerras */ 16062b931fb6SIngo Molnar tce_entryp = (u64 *)base; 16079b6b563cSPaul Mackerras for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) { 16089b6b563cSPaul Mackerras tce_entry = (i << PAGE_SHIFT); 16099b6b563cSPaul Mackerras tce_entry |= 0x3; 16109b6b563cSPaul Mackerras *tce_entryp = tce_entry; 16119b6b563cSPaul Mackerras } 16129b6b563cSPaul Mackerras 16139b6b563cSPaul Mackerras prom_printf("opening PHB %s", path); 16149b6b563cSPaul Mackerras phb_node = call_prom("open", 1, 1, path); 16159b6b563cSPaul Mackerras if (phb_node == 0) 16169b6b563cSPaul Mackerras prom_printf("... failed\n"); 16179b6b563cSPaul Mackerras else 16189b6b563cSPaul Mackerras prom_printf("... done\n"); 16199b6b563cSPaul Mackerras 16209b6b563cSPaul Mackerras call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"), 16219b6b563cSPaul Mackerras phb_node, -1, minsize, 16229b6b563cSPaul Mackerras (u32) base, (u32) (base >> 32)); 16239b6b563cSPaul Mackerras call_prom("close", 1, 0, phb_node); 16249b6b563cSPaul Mackerras } 16259b6b563cSPaul Mackerras 16269b6b563cSPaul Mackerras reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom); 16279b6b563cSPaul Mackerras 16282babf5c2SMichael Ellerman /* These are only really needed if there is a memory limit in 16292babf5c2SMichael Ellerman * effect, but we don't know so export them always. */ 16305827d416SAnton Blanchard prom_tce_alloc_start = local_alloc_bottom; 16315827d416SAnton Blanchard prom_tce_alloc_end = local_alloc_top; 16329b6b563cSPaul Mackerras 16339b6b563cSPaul Mackerras /* Flag the first invalid entry */ 16349b6b563cSPaul Mackerras prom_debug("ending prom_initialize_tce_table\n"); 16359b6b563cSPaul Mackerras } 1636493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */ 1637493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */ 16389b6b563cSPaul Mackerras 16399b6b563cSPaul Mackerras /* 16409b6b563cSPaul Mackerras * With CHRP SMP we need to use the OF to start the other processors. 16419b6b563cSPaul Mackerras * We can't wait until smp_boot_cpus (the OF is trashed by then) 16429b6b563cSPaul Mackerras * so we have to put the processors into a holding pattern controlled 16439b6b563cSPaul Mackerras * by the kernel (not OF) before we destroy the OF. 16449b6b563cSPaul Mackerras * 16459b6b563cSPaul Mackerras * This uses a chunk of low memory, puts some holding pattern 16469b6b563cSPaul Mackerras * code there and sends the other processors off to there until 16479b6b563cSPaul Mackerras * smp_boot_cpus tells them to do something. The holding pattern 16489b6b563cSPaul Mackerras * checks that address until its cpu # is there, when it is that 16499b6b563cSPaul Mackerras * cpu jumps to __secondary_start(). smp_boot_cpus() takes care 16509b6b563cSPaul Mackerras * of setting those values. 16519b6b563cSPaul Mackerras * 16529b6b563cSPaul Mackerras * We also use physical address 0x4 here to tell when a cpu 16539b6b563cSPaul Mackerras * is in its holding pattern code. 16549b6b563cSPaul Mackerras * 16559b6b563cSPaul Mackerras * -- Cort 16569b6b563cSPaul Mackerras */ 1657bbd0abdaSPaul Mackerras /* 1658bbd0abdaSPaul Mackerras * We want to reference the copy of __secondary_hold_* in the 1659bbd0abdaSPaul Mackerras * 0 - 0x100 address range 1660bbd0abdaSPaul Mackerras */ 1661bbd0abdaSPaul Mackerras #define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff) 1662bbd0abdaSPaul Mackerras 16639b6b563cSPaul Mackerras static void __init prom_hold_cpus(void) 16649b6b563cSPaul Mackerras { 16659b6b563cSPaul Mackerras unsigned long i; 16669b6b563cSPaul Mackerras phandle node; 16679b6b563cSPaul Mackerras char type[64]; 16689b6b563cSPaul Mackerras unsigned long *spinloop 1669bbd0abdaSPaul Mackerras = (void *) LOW_ADDR(__secondary_hold_spinloop); 16709b6b563cSPaul Mackerras unsigned long *acknowledge 1671bbd0abdaSPaul Mackerras = (void *) LOW_ADDR(__secondary_hold_acknowledge); 1672bbd0abdaSPaul Mackerras unsigned long secondary_hold = LOW_ADDR(__secondary_hold); 16739b6b563cSPaul Mackerras 1674dbe78b40SBenjamin Herrenschmidt /* 1675dbe78b40SBenjamin Herrenschmidt * On pseries, if RTAS supports "query-cpu-stopped-state", 1676dbe78b40SBenjamin Herrenschmidt * we skip this stage, the CPUs will be started by the 1677dbe78b40SBenjamin Herrenschmidt * kernel using RTAS. 1678dbe78b40SBenjamin Herrenschmidt */ 1679dbe78b40SBenjamin Herrenschmidt if ((of_platform == PLATFORM_PSERIES || 1680dbe78b40SBenjamin Herrenschmidt of_platform == PLATFORM_PSERIES_LPAR) && 1681dbe78b40SBenjamin Herrenschmidt rtas_has_query_cpu_stopped) { 1682dbe78b40SBenjamin Herrenschmidt prom_printf("prom_hold_cpus: skipped\n"); 1683dbe78b40SBenjamin Herrenschmidt return; 1684dbe78b40SBenjamin Herrenschmidt } 1685dbe78b40SBenjamin Herrenschmidt 16869b6b563cSPaul Mackerras prom_debug("prom_hold_cpus: start...\n"); 16879b6b563cSPaul Mackerras prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop); 16889b6b563cSPaul Mackerras prom_debug(" 1) *spinloop = 0x%x\n", *spinloop); 16899b6b563cSPaul Mackerras prom_debug(" 1) acknowledge = 0x%x\n", 16909b6b563cSPaul Mackerras (unsigned long)acknowledge); 16919b6b563cSPaul Mackerras prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge); 16929b6b563cSPaul Mackerras prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold); 16939b6b563cSPaul Mackerras 16949b6b563cSPaul Mackerras /* Set the common spinloop variable, so all of the secondary cpus 16959b6b563cSPaul Mackerras * will block when they are awakened from their OF spinloop. 16969b6b563cSPaul Mackerras * This must occur for both SMP and non SMP kernels, since OF will 16979b6b563cSPaul Mackerras * be trashed when we move the kernel. 16989b6b563cSPaul Mackerras */ 16999b6b563cSPaul Mackerras *spinloop = 0; 17009b6b563cSPaul Mackerras 17019b6b563cSPaul Mackerras /* look for cpus */ 17029b6b563cSPaul Mackerras for (node = 0; prom_next_node(&node); ) { 1703493adffcSBenjamin Herrenschmidt unsigned int cpu_no; 1704493adffcSBenjamin Herrenschmidt __be32 reg; 1705493adffcSBenjamin Herrenschmidt 17069b6b563cSPaul Mackerras type[0] = 0; 17079b6b563cSPaul Mackerras prom_getprop(node, "device_type", type, sizeof(type)); 17085827d416SAnton Blanchard if (strcmp(type, "cpu") != 0) 17099b6b563cSPaul Mackerras continue; 17109b6b563cSPaul Mackerras 17119b6b563cSPaul Mackerras /* Skip non-configured cpus. */ 17129b6b563cSPaul Mackerras if (prom_getprop(node, "status", type, sizeof(type)) > 0) 17135827d416SAnton Blanchard if (strcmp(type, "okay") != 0) 17149b6b563cSPaul Mackerras continue; 17159b6b563cSPaul Mackerras 1716493adffcSBenjamin Herrenschmidt reg = cpu_to_be32(-1); /* make sparse happy */ 17179b6b563cSPaul Mackerras prom_getprop(node, "reg", ®, sizeof(reg)); 1718493adffcSBenjamin Herrenschmidt cpu_no = be32_to_cpu(reg); 17199b6b563cSPaul Mackerras 1720493adffcSBenjamin Herrenschmidt prom_debug("cpu hw idx = %lu\n", cpu_no); 17219b6b563cSPaul Mackerras 17229b6b563cSPaul Mackerras /* Init the acknowledge var which will be reset by 17239b6b563cSPaul Mackerras * the secondary cpu when it awakens from its OF 17249b6b563cSPaul Mackerras * spinloop. 17259b6b563cSPaul Mackerras */ 17269b6b563cSPaul Mackerras *acknowledge = (unsigned long)-1; 17279b6b563cSPaul Mackerras 1728493adffcSBenjamin Herrenschmidt if (cpu_no != prom.cpu) { 172927f44888SBenjamin Herrenschmidt /* Primary Thread of non-boot cpu or any thread */ 1730493adffcSBenjamin Herrenschmidt prom_printf("starting cpu hw idx %lu... ", cpu_no); 17319b6b563cSPaul Mackerras call_prom("start-cpu", 3, 0, node, 1732493adffcSBenjamin Herrenschmidt secondary_hold, cpu_no); 17339b6b563cSPaul Mackerras 17349b6b563cSPaul Mackerras for (i = 0; (i < 100000000) && 17359b6b563cSPaul Mackerras (*acknowledge == ((unsigned long)-1)); i++ ) 17369b6b563cSPaul Mackerras mb(); 17379b6b563cSPaul Mackerras 1738493adffcSBenjamin Herrenschmidt if (*acknowledge == cpu_no) 17399b6b563cSPaul Mackerras prom_printf("done\n"); 1740bbd0abdaSPaul Mackerras else 17419b6b563cSPaul Mackerras prom_printf("failed: %x\n", *acknowledge); 17429b6b563cSPaul Mackerras } 17439b6b563cSPaul Mackerras #ifdef CONFIG_SMP 17449b6b563cSPaul Mackerras else 1745493adffcSBenjamin Herrenschmidt prom_printf("boot cpu hw idx %lu\n", cpu_no); 17469b6b563cSPaul Mackerras #endif /* CONFIG_SMP */ 17479b6b563cSPaul Mackerras } 17489b6b563cSPaul Mackerras 17499b6b563cSPaul Mackerras prom_debug("prom_hold_cpus: end...\n"); 17509b6b563cSPaul Mackerras } 17519b6b563cSPaul Mackerras 17529b6b563cSPaul Mackerras 17539b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp) 17549b6b563cSPaul Mackerras { 17559b6b563cSPaul Mackerras /* Get a handle to the prom entry point before anything else */ 17565827d416SAnton Blanchard prom_entry = pp; 17579b6b563cSPaul Mackerras 17589b6b563cSPaul Mackerras /* get a handle for the stdout device */ 17595827d416SAnton Blanchard prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen")); 17605827d416SAnton Blanchard if (!PHANDLE_VALID(prom.chosen)) 17619b6b563cSPaul Mackerras prom_panic("cannot find chosen"); /* msg won't be printed :( */ 17629b6b563cSPaul Mackerras 17639b6b563cSPaul Mackerras /* get device tree root */ 17645827d416SAnton Blanchard prom.root = call_prom("finddevice", 1, 1, ADDR("/")); 17655827d416SAnton Blanchard if (!PHANDLE_VALID(prom.root)) 17669b6b563cSPaul Mackerras prom_panic("cannot find device tree root"); /* msg won't be printed :( */ 1767a575b807SPaul Mackerras 17685827d416SAnton Blanchard prom.mmumap = 0; 17699b6b563cSPaul Mackerras } 17709b6b563cSPaul Mackerras 1771a575b807SPaul Mackerras #ifdef CONFIG_PPC32 1772a575b807SPaul Mackerras /* 1773a575b807SPaul Mackerras * For really old powermacs, we need to map things we claim. 1774a575b807SPaul Mackerras * For that, we need the ihandle of the mmu. 1775a23414beSPaul Mackerras * Also, on the longtrail, we need to work around other bugs. 1776a575b807SPaul Mackerras */ 1777a575b807SPaul Mackerras static void __init prom_find_mmu(void) 1778a575b807SPaul Mackerras { 1779a575b807SPaul Mackerras phandle oprom; 1780a575b807SPaul Mackerras char version[64]; 1781a575b807SPaul Mackerras 1782a575b807SPaul Mackerras oprom = call_prom("finddevice", 1, 1, ADDR("/openprom")); 1783a575b807SPaul Mackerras if (!PHANDLE_VALID(oprom)) 1784a575b807SPaul Mackerras return; 1785a575b807SPaul Mackerras if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0) 1786a575b807SPaul Mackerras return; 1787a575b807SPaul Mackerras version[sizeof(version) - 1] = 0; 1788a575b807SPaul Mackerras /* XXX might need to add other versions here */ 1789a23414beSPaul Mackerras if (strcmp(version, "Open Firmware, 1.0.5") == 0) 1790a23414beSPaul Mackerras of_workarounds = OF_WA_CLAIM; 1791a23414beSPaul Mackerras else if (strncmp(version, "FirmWorks,3.", 12) == 0) { 1792a23414beSPaul Mackerras of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL; 1793a23414beSPaul Mackerras call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim"); 1794a23414beSPaul Mackerras } else 1795a575b807SPaul Mackerras return; 17965827d416SAnton Blanchard prom.memory = call_prom("open", 1, 1, ADDR("/memory")); 17975827d416SAnton Blanchard prom_getprop(prom.chosen, "mmu", &prom.mmumap, 17985827d416SAnton Blanchard sizeof(prom.mmumap)); 1799493adffcSBenjamin Herrenschmidt prom.mmumap = be32_to_cpu(prom.mmumap); 18005827d416SAnton Blanchard if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap)) 1801a23414beSPaul Mackerras of_workarounds &= ~OF_WA_CLAIM; /* hmmm */ 1802a575b807SPaul Mackerras } 1803a575b807SPaul Mackerras #else 1804a575b807SPaul Mackerras #define prom_find_mmu() 1805a575b807SPaul Mackerras #endif 1806a575b807SPaul Mackerras 18079b6b563cSPaul Mackerras static void __init prom_init_stdout(void) 18089b6b563cSPaul Mackerras { 18095827d416SAnton Blanchard char *path = of_stdout_device; 18109b6b563cSPaul Mackerras char type[16]; 1811493adffcSBenjamin Herrenschmidt phandle stdout_node; 1812493adffcSBenjamin Herrenschmidt __be32 val; 18139b6b563cSPaul Mackerras 18145827d416SAnton Blanchard if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0) 18159b6b563cSPaul Mackerras prom_panic("cannot find stdout"); 18169b6b563cSPaul Mackerras 1817493adffcSBenjamin Herrenschmidt prom.stdout = be32_to_cpu(val); 18189b6b563cSPaul Mackerras 18199b6b563cSPaul Mackerras /* Get the full OF pathname of the stdout device */ 18209b6b563cSPaul Mackerras memset(path, 0, 256); 18215827d416SAnton Blanchard call_prom("instance-to-path", 3, 1, prom.stdout, path, 255); 18225827d416SAnton Blanchard prom_printf("OF stdout device is: %s\n", of_stdout_device); 18235827d416SAnton Blanchard prom_setprop(prom.chosen, "/chosen", "linux,stdout-path", 1824a23414beSPaul Mackerras path, strlen(path) + 1); 18259b6b563cSPaul Mackerras 182610348f59SBenjamin Herrenschmidt /* instance-to-package fails on PA-Semi */ 182710348f59SBenjamin Herrenschmidt stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout); 182810348f59SBenjamin Herrenschmidt if (stdout_node != PROM_ERROR) { 182910348f59SBenjamin Herrenschmidt val = cpu_to_be32(stdout_node); 183010348f59SBenjamin Herrenschmidt prom_setprop(prom.chosen, "/chosen", "linux,stdout-package", 183110348f59SBenjamin Herrenschmidt &val, sizeof(val)); 183210348f59SBenjamin Herrenschmidt 18339b6b563cSPaul Mackerras /* If it's a display, note it */ 18349b6b563cSPaul Mackerras memset(type, 0, sizeof(type)); 1835493adffcSBenjamin Herrenschmidt prom_getprop(stdout_node, "device_type", type, sizeof(type)); 18365827d416SAnton Blanchard if (strcmp(type, "display") == 0) 1837493adffcSBenjamin Herrenschmidt prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0); 18389b6b563cSPaul Mackerras } 183910348f59SBenjamin Herrenschmidt } 18409b6b563cSPaul Mackerras 18419b6b563cSPaul Mackerras static int __init prom_find_machine_type(void) 18429b6b563cSPaul Mackerras { 18439b6b563cSPaul Mackerras char compat[256]; 18449b6b563cSPaul Mackerras int len, i = 0; 184521fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64 18469b6b563cSPaul Mackerras phandle rtas; 1847e8222502SBenjamin Herrenschmidt int x; 184821fe3301SBenjamin Herrenschmidt #endif 1849e8222502SBenjamin Herrenschmidt 18506e35d5daSBenjamin Herrenschmidt /* Look for a PowerMac or a Cell */ 18515827d416SAnton Blanchard len = prom_getprop(prom.root, "compatible", 18529b6b563cSPaul Mackerras compat, sizeof(compat)-1); 18539b6b563cSPaul Mackerras if (len > 0) { 18549b6b563cSPaul Mackerras compat[len] = 0; 18559b6b563cSPaul Mackerras while (i < len) { 18569b6b563cSPaul Mackerras char *p = &compat[i]; 18579b6b563cSPaul Mackerras int sl = strlen(p); 18589b6b563cSPaul Mackerras if (sl == 0) 18599b6b563cSPaul Mackerras break; 18605827d416SAnton Blanchard if (strstr(p, "Power Macintosh") || 18615827d416SAnton Blanchard strstr(p, "MacRISC")) 18629b6b563cSPaul Mackerras return PLATFORM_POWERMAC; 1863133dda1eSArnd Bergmann #ifdef CONFIG_PPC64 1864133dda1eSArnd Bergmann /* We must make sure we don't detect the IBM Cell 1865133dda1eSArnd Bergmann * blades as pSeries due to some firmware issues, 1866133dda1eSArnd Bergmann * so we do it here. 1867133dda1eSArnd Bergmann */ 18685827d416SAnton Blanchard if (strstr(p, "IBM,CBEA") || 18695827d416SAnton Blanchard strstr(p, "IBM,CPBW-1.0")) 1870133dda1eSArnd Bergmann return PLATFORM_GENERIC; 1871133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */ 18729b6b563cSPaul Mackerras i += sl + 1; 18739b6b563cSPaul Mackerras } 18749b6b563cSPaul Mackerras } 18759b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 18766e35d5daSBenjamin Herrenschmidt /* Try to detect OPAL */ 18776e35d5daSBenjamin Herrenschmidt if (PHANDLE_VALID(call_prom("finddevice", 1, 1, ADDR("/ibm,opal")))) 18786e35d5daSBenjamin Herrenschmidt return PLATFORM_OPAL; 18796e35d5daSBenjamin Herrenschmidt 18806e35d5daSBenjamin Herrenschmidt /* Try to figure out if it's an IBM pSeries or any other 1881e8222502SBenjamin Herrenschmidt * PAPR compliant platform. We assume it is if : 1882e8222502SBenjamin Herrenschmidt * - /device_type is "chrp" (please, do NOT use that for future 1883e8222502SBenjamin Herrenschmidt * non-IBM designs ! 1884e8222502SBenjamin Herrenschmidt * - it has /rtas 1885e8222502SBenjamin Herrenschmidt */ 18865827d416SAnton Blanchard len = prom_getprop(prom.root, "device_type", 1887e8222502SBenjamin Herrenschmidt compat, sizeof(compat)-1); 1888e8222502SBenjamin Herrenschmidt if (len <= 0) 1889e8222502SBenjamin Herrenschmidt return PLATFORM_GENERIC; 18905827d416SAnton Blanchard if (strcmp(compat, "chrp")) 1891e8222502SBenjamin Herrenschmidt return PLATFORM_GENERIC; 1892e8222502SBenjamin Herrenschmidt 18939b6b563cSPaul Mackerras /* Default to pSeries. We need to know if we are running LPAR */ 18949b6b563cSPaul Mackerras rtas = call_prom("finddevice", 1, 1, ADDR("/rtas")); 1895e8222502SBenjamin Herrenschmidt if (!PHANDLE_VALID(rtas)) 1896e8222502SBenjamin Herrenschmidt return PLATFORM_GENERIC; 1897e8222502SBenjamin Herrenschmidt x = prom_getproplen(rtas, "ibm,hypertas-functions"); 18989b6b563cSPaul Mackerras if (x != PROM_ERROR) { 18994da727aeSAnton Blanchard prom_debug("Hypertas detected, assuming LPAR !\n"); 19009b6b563cSPaul Mackerras return PLATFORM_PSERIES_LPAR; 19019b6b563cSPaul Mackerras } 19029b6b563cSPaul Mackerras return PLATFORM_PSERIES; 19039b6b563cSPaul Mackerras #else 1904e8222502SBenjamin Herrenschmidt return PLATFORM_GENERIC; 19059b6b563cSPaul Mackerras #endif 19069b6b563cSPaul Mackerras } 19079b6b563cSPaul Mackerras 19089b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b) 19099b6b563cSPaul Mackerras { 19109b6b563cSPaul Mackerras return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r); 19119b6b563cSPaul Mackerras } 19129b6b563cSPaul Mackerras 19139b6b563cSPaul Mackerras /* 19149b6b563cSPaul Mackerras * If we have a display that we don't know how to drive, 19159b6b563cSPaul Mackerras * we will want to try to execute OF's open method for it 19169b6b563cSPaul Mackerras * later. However, OF will probably fall over if we do that 19179b6b563cSPaul Mackerras * we've taken over the MMU. 19189b6b563cSPaul Mackerras * So we check whether we will need to open the display, 19199b6b563cSPaul Mackerras * and if so, open it now. 19209b6b563cSPaul Mackerras */ 19219b6b563cSPaul Mackerras static void __init prom_check_displays(void) 19229b6b563cSPaul Mackerras { 19239b6b563cSPaul Mackerras char type[16], *path; 19249b6b563cSPaul Mackerras phandle node; 19259b6b563cSPaul Mackerras ihandle ih; 19269b6b563cSPaul Mackerras int i; 19279b6b563cSPaul Mackerras 19289b6b563cSPaul Mackerras static unsigned char default_colors[] = { 19299b6b563cSPaul Mackerras 0x00, 0x00, 0x00, 19309b6b563cSPaul Mackerras 0x00, 0x00, 0xaa, 19319b6b563cSPaul Mackerras 0x00, 0xaa, 0x00, 19329b6b563cSPaul Mackerras 0x00, 0xaa, 0xaa, 19339b6b563cSPaul Mackerras 0xaa, 0x00, 0x00, 19349b6b563cSPaul Mackerras 0xaa, 0x00, 0xaa, 19359b6b563cSPaul Mackerras 0xaa, 0xaa, 0x00, 19369b6b563cSPaul Mackerras 0xaa, 0xaa, 0xaa, 19379b6b563cSPaul Mackerras 0x55, 0x55, 0x55, 19389b6b563cSPaul Mackerras 0x55, 0x55, 0xff, 19399b6b563cSPaul Mackerras 0x55, 0xff, 0x55, 19409b6b563cSPaul Mackerras 0x55, 0xff, 0xff, 19419b6b563cSPaul Mackerras 0xff, 0x55, 0x55, 19429b6b563cSPaul Mackerras 0xff, 0x55, 0xff, 19439b6b563cSPaul Mackerras 0xff, 0xff, 0x55, 19449b6b563cSPaul Mackerras 0xff, 0xff, 0xff 19459b6b563cSPaul Mackerras }; 19469b6b563cSPaul Mackerras const unsigned char *clut; 19479b6b563cSPaul Mackerras 19484da727aeSAnton Blanchard prom_debug("Looking for displays\n"); 19499b6b563cSPaul Mackerras for (node = 0; prom_next_node(&node); ) { 19509b6b563cSPaul Mackerras memset(type, 0, sizeof(type)); 19519b6b563cSPaul Mackerras prom_getprop(node, "device_type", type, sizeof(type)); 19525827d416SAnton Blanchard if (strcmp(type, "display") != 0) 19539b6b563cSPaul Mackerras continue; 19549b6b563cSPaul Mackerras 19559b6b563cSPaul Mackerras /* It seems OF doesn't null-terminate the path :-( */ 19565827d416SAnton Blanchard path = prom_scratch; 19579b6b563cSPaul Mackerras memset(path, 0, PROM_SCRATCH_SIZE); 19589b6b563cSPaul Mackerras 19599b6b563cSPaul Mackerras /* 19609b6b563cSPaul Mackerras * leave some room at the end of the path for appending extra 19619b6b563cSPaul Mackerras * arguments 19629b6b563cSPaul Mackerras */ 19639b6b563cSPaul Mackerras if (call_prom("package-to-path", 3, 1, node, path, 19649b6b563cSPaul Mackerras PROM_SCRATCH_SIZE-10) == PROM_ERROR) 19659b6b563cSPaul Mackerras continue; 19669b6b563cSPaul Mackerras prom_printf("found display : %s, opening... ", path); 19679b6b563cSPaul Mackerras 19689b6b563cSPaul Mackerras ih = call_prom("open", 1, 1, path); 19699b6b563cSPaul Mackerras if (ih == 0) { 19709b6b563cSPaul Mackerras prom_printf("failed\n"); 19719b6b563cSPaul Mackerras continue; 19729b6b563cSPaul Mackerras } 19739b6b563cSPaul Mackerras 19749b6b563cSPaul Mackerras /* Success */ 19759b6b563cSPaul Mackerras prom_printf("done\n"); 1976a23414beSPaul Mackerras prom_setprop(node, path, "linux,opened", NULL, 0); 19779b6b563cSPaul Mackerras 19789b6b563cSPaul Mackerras /* Setup a usable color table when the appropriate 19799b6b563cSPaul Mackerras * method is available. Should update this to set-colors */ 19805827d416SAnton Blanchard clut = default_colors; 19813f53638cSBenjamin Herrenschmidt for (i = 0; i < 16; i++, clut += 3) 19829b6b563cSPaul Mackerras if (prom_set_color(ih, i, clut[0], clut[1], 19839b6b563cSPaul Mackerras clut[2]) != 0) 19849b6b563cSPaul Mackerras break; 19859b6b563cSPaul Mackerras 19869b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224 19875827d416SAnton Blanchard clut = PTRRELOC(logo_linux_clut224.clut); 19885827d416SAnton Blanchard for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3) 19899b6b563cSPaul Mackerras if (prom_set_color(ih, i + 32, clut[0], clut[1], 19909b6b563cSPaul Mackerras clut[2]) != 0) 19919b6b563cSPaul Mackerras break; 19929b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */ 19937191b615SBenjamin Herrenschmidt 19947191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX 19957191b615SBenjamin Herrenschmidt if (prom_getprop(node, "linux,boot-display", NULL, 0) != 19967191b615SBenjamin Herrenschmidt PROM_ERROR) { 19977191b615SBenjamin Herrenschmidt u32 width, height, pitch, addr; 19987191b615SBenjamin Herrenschmidt 19997191b615SBenjamin Herrenschmidt prom_printf("Setting btext !\n"); 20007191b615SBenjamin Herrenschmidt prom_getprop(node, "width", &width, 4); 20017191b615SBenjamin Herrenschmidt prom_getprop(node, "height", &height, 4); 20027191b615SBenjamin Herrenschmidt prom_getprop(node, "linebytes", &pitch, 4); 20037191b615SBenjamin Herrenschmidt prom_getprop(node, "address", &addr, 4); 20047191b615SBenjamin Herrenschmidt prom_printf("W=%d H=%d LB=%d addr=0x%x\n", 20057191b615SBenjamin Herrenschmidt width, height, pitch, addr); 20067191b615SBenjamin Herrenschmidt btext_setup_display(width, height, 8, pitch, addr); 20077191b615SBenjamin Herrenschmidt } 20087191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */ 20099b6b563cSPaul Mackerras } 20109b6b563cSPaul Mackerras } 20119b6b563cSPaul Mackerras 20129b6b563cSPaul Mackerras 20139b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */ 20149b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end, 20159b6b563cSPaul Mackerras unsigned long needed, unsigned long align) 20169b6b563cSPaul Mackerras { 20179b6b563cSPaul Mackerras void *ret; 20189b6b563cSPaul Mackerras 20199b6b563cSPaul Mackerras *mem_start = _ALIGN(*mem_start, align); 20209b6b563cSPaul Mackerras while ((*mem_start + needed) > *mem_end) { 20219b6b563cSPaul Mackerras unsigned long room, chunk; 20229b6b563cSPaul Mackerras 20239b6b563cSPaul Mackerras prom_debug("Chunk exhausted, claiming more at %x...\n", 20245827d416SAnton Blanchard alloc_bottom); 20255827d416SAnton Blanchard room = alloc_top - alloc_bottom; 20269b6b563cSPaul Mackerras if (room > DEVTREE_CHUNK_SIZE) 20279b6b563cSPaul Mackerras room = DEVTREE_CHUNK_SIZE; 20289b6b563cSPaul Mackerras if (room < PAGE_SIZE) 2029fbafd728SAnton Blanchard prom_panic("No memory for flatten_device_tree " 2030fbafd728SAnton Blanchard "(no room)\n"); 20319b6b563cSPaul Mackerras chunk = alloc_up(room, 0); 20329b6b563cSPaul Mackerras if (chunk == 0) 2033fbafd728SAnton Blanchard prom_panic("No memory for flatten_device_tree " 2034fbafd728SAnton Blanchard "(claim failed)\n"); 2035966728ddSAnton Blanchard *mem_end = chunk + room; 20369b6b563cSPaul Mackerras } 20379b6b563cSPaul Mackerras 20389b6b563cSPaul Mackerras ret = (void *)*mem_start; 20399b6b563cSPaul Mackerras *mem_start += needed; 20409b6b563cSPaul Mackerras 20419b6b563cSPaul Mackerras return ret; 20429b6b563cSPaul Mackerras } 20439b6b563cSPaul Mackerras 2044493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { \ 2045493adffcSBenjamin Herrenschmidt void *room = make_room(mem_start, mem_end, 4, 4); \ 2046493adffcSBenjamin Herrenschmidt *(__be32 *)room = cpu_to_be32(token); \ 2047493adffcSBenjamin Herrenschmidt } while(0) 20489b6b563cSPaul Mackerras 20499b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str) 20509b6b563cSPaul Mackerras { 20519b6b563cSPaul Mackerras char *s, *os; 20529b6b563cSPaul Mackerras 20535827d416SAnton Blanchard s = os = (char *)dt_string_start; 20549b6b563cSPaul Mackerras s += 4; 20555827d416SAnton Blanchard while (s < (char *)dt_string_end) { 20569b6b563cSPaul Mackerras if (strcmp(s, str) == 0) 20579b6b563cSPaul Mackerras return s - os; 20589b6b563cSPaul Mackerras s += strlen(s) + 1; 20599b6b563cSPaul Mackerras } 20609b6b563cSPaul Mackerras return 0; 20619b6b563cSPaul Mackerras } 20629b6b563cSPaul Mackerras 20639b6b563cSPaul Mackerras /* 20649b6b563cSPaul Mackerras * The Open Firmware 1275 specification states properties must be 31 bytes or 20659b6b563cSPaul Mackerras * less, however not all firmwares obey this. Make it 64 bytes to be safe. 20669b6b563cSPaul Mackerras */ 20679b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64 20689b6b563cSPaul Mackerras 20699b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node, 20709b6b563cSPaul Mackerras unsigned long *mem_start, 20719b6b563cSPaul Mackerras unsigned long *mem_end) 20729b6b563cSPaul Mackerras { 20739b6b563cSPaul Mackerras char *prev_name, *namep, *sstart; 20749b6b563cSPaul Mackerras unsigned long soff; 20759b6b563cSPaul Mackerras phandle child; 20769b6b563cSPaul Mackerras 20775827d416SAnton Blanchard sstart = (char *)dt_string_start; 20789b6b563cSPaul Mackerras 20799b6b563cSPaul Mackerras /* get and store all property names */ 20805827d416SAnton Blanchard prev_name = ""; 20819b6b563cSPaul Mackerras for (;;) { 20829b6b563cSPaul Mackerras /* 64 is max len of name including nul. */ 20839b6b563cSPaul Mackerras namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1); 20849b6b563cSPaul Mackerras if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) { 20859b6b563cSPaul Mackerras /* No more nodes: unwind alloc */ 20869b6b563cSPaul Mackerras *mem_start = (unsigned long)namep; 20879b6b563cSPaul Mackerras break; 20889b6b563cSPaul Mackerras } 20899b6b563cSPaul Mackerras 20909b6b563cSPaul Mackerras /* skip "name" */ 20915827d416SAnton Blanchard if (strcmp(namep, "name") == 0) { 20929b6b563cSPaul Mackerras *mem_start = (unsigned long)namep; 20935827d416SAnton Blanchard prev_name = "name"; 20949b6b563cSPaul Mackerras continue; 20959b6b563cSPaul Mackerras } 20969b6b563cSPaul Mackerras /* get/create string entry */ 20979b6b563cSPaul Mackerras soff = dt_find_string(namep); 20989b6b563cSPaul Mackerras if (soff != 0) { 20999b6b563cSPaul Mackerras *mem_start = (unsigned long)namep; 21009b6b563cSPaul Mackerras namep = sstart + soff; 21019b6b563cSPaul Mackerras } else { 21029b6b563cSPaul Mackerras /* Trim off some if we can */ 21039b6b563cSPaul Mackerras *mem_start = (unsigned long)namep + strlen(namep) + 1; 21045827d416SAnton Blanchard dt_string_end = *mem_start; 21059b6b563cSPaul Mackerras } 21069b6b563cSPaul Mackerras prev_name = namep; 21079b6b563cSPaul Mackerras } 21089b6b563cSPaul Mackerras 21099b6b563cSPaul Mackerras /* do all our children */ 21109b6b563cSPaul Mackerras child = call_prom("child", 1, 1, node); 21119b6b563cSPaul Mackerras while (child != 0) { 21129b6b563cSPaul Mackerras scan_dt_build_strings(child, mem_start, mem_end); 21139b6b563cSPaul Mackerras child = call_prom("peer", 1, 1, child); 21149b6b563cSPaul Mackerras } 21159b6b563cSPaul Mackerras } 21169b6b563cSPaul Mackerras 21179b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start, 21189b6b563cSPaul Mackerras unsigned long *mem_end) 21199b6b563cSPaul Mackerras { 21209b6b563cSPaul Mackerras phandle child; 21219b6b563cSPaul Mackerras char *namep, *prev_name, *sstart, *p, *ep, *lp, *path; 21229b6b563cSPaul Mackerras unsigned long soff; 21239b6b563cSPaul Mackerras unsigned char *valp; 21249b6b563cSPaul Mackerras static char pname[MAX_PROPERTY_NAME]; 21256e35d5daSBenjamin Herrenschmidt int l, room, has_phandle = 0; 21269b6b563cSPaul Mackerras 21279b6b563cSPaul Mackerras dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end); 21289b6b563cSPaul Mackerras 21299b6b563cSPaul Mackerras /* get the node's full name */ 21309b6b563cSPaul Mackerras namep = (char *)*mem_start; 2131c4988820SPaul Mackerras room = *mem_end - *mem_start; 2132c4988820SPaul Mackerras if (room > 255) 2133c4988820SPaul Mackerras room = 255; 2134c4988820SPaul Mackerras l = call_prom("package-to-path", 3, 1, node, namep, room); 21359b6b563cSPaul Mackerras if (l >= 0) { 21369b6b563cSPaul Mackerras /* Didn't fit? Get more room. */ 2137c4988820SPaul Mackerras if (l >= room) { 2138c4988820SPaul Mackerras if (l >= *mem_end - *mem_start) 21399b6b563cSPaul Mackerras namep = make_room(mem_start, mem_end, l+1, 1); 21409b6b563cSPaul Mackerras call_prom("package-to-path", 3, 1, node, namep, l); 21419b6b563cSPaul Mackerras } 21429b6b563cSPaul Mackerras namep[l] = '\0'; 21439b6b563cSPaul Mackerras 21449b6b563cSPaul Mackerras /* Fixup an Apple bug where they have bogus \0 chars in the 2145a575b807SPaul Mackerras * middle of the path in some properties, and extract 2146a575b807SPaul Mackerras * the unit name (everything after the last '/'). 21479b6b563cSPaul Mackerras */ 2148a575b807SPaul Mackerras for (lp = p = namep, ep = namep + l; p < ep; p++) { 21499b6b563cSPaul Mackerras if (*p == '/') 2150a575b807SPaul Mackerras lp = namep; 2151a575b807SPaul Mackerras else if (*p != 0) 2152a575b807SPaul Mackerras *lp++ = *p; 2153a575b807SPaul Mackerras } 2154a575b807SPaul Mackerras *lp = 0; 2155a575b807SPaul Mackerras *mem_start = _ALIGN((unsigned long)lp + 1, 4); 21569b6b563cSPaul Mackerras } 21579b6b563cSPaul Mackerras 21589b6b563cSPaul Mackerras /* get it again for debugging */ 21595827d416SAnton Blanchard path = prom_scratch; 21609b6b563cSPaul Mackerras memset(path, 0, PROM_SCRATCH_SIZE); 21619b6b563cSPaul Mackerras call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1); 21629b6b563cSPaul Mackerras 21639b6b563cSPaul Mackerras /* get and store all properties */ 21645827d416SAnton Blanchard prev_name = ""; 21655827d416SAnton Blanchard sstart = (char *)dt_string_start; 21669b6b563cSPaul Mackerras for (;;) { 21679b6b563cSPaul Mackerras if (call_prom("nextprop", 3, 1, node, prev_name, 21685827d416SAnton Blanchard pname) != 1) 21699b6b563cSPaul Mackerras break; 21709b6b563cSPaul Mackerras 21719b6b563cSPaul Mackerras /* skip "name" */ 21725827d416SAnton Blanchard if (strcmp(pname, "name") == 0) { 21735827d416SAnton Blanchard prev_name = "name"; 21749b6b563cSPaul Mackerras continue; 21759b6b563cSPaul Mackerras } 21769b6b563cSPaul Mackerras 21779b6b563cSPaul Mackerras /* find string offset */ 21785827d416SAnton Blanchard soff = dt_find_string(pname); 21799b6b563cSPaul Mackerras if (soff == 0) { 21809b6b563cSPaul Mackerras prom_printf("WARNING: Can't find string index for" 21815827d416SAnton Blanchard " <%s>, node %s\n", pname, path); 21829b6b563cSPaul Mackerras break; 21839b6b563cSPaul Mackerras } 21849b6b563cSPaul Mackerras prev_name = sstart + soff; 21859b6b563cSPaul Mackerras 21869b6b563cSPaul Mackerras /* get length */ 21875827d416SAnton Blanchard l = call_prom("getproplen", 2, 1, node, pname); 21889b6b563cSPaul Mackerras 21899b6b563cSPaul Mackerras /* sanity checks */ 21909b6b563cSPaul Mackerras if (l == PROM_ERROR) 21919b6b563cSPaul Mackerras continue; 21929b6b563cSPaul Mackerras 21939b6b563cSPaul Mackerras /* push property head */ 21949b6b563cSPaul Mackerras dt_push_token(OF_DT_PROP, mem_start, mem_end); 21959b6b563cSPaul Mackerras dt_push_token(l, mem_start, mem_end); 21969b6b563cSPaul Mackerras dt_push_token(soff, mem_start, mem_end); 21979b6b563cSPaul Mackerras 21989b6b563cSPaul Mackerras /* push property content */ 21999b6b563cSPaul Mackerras valp = make_room(mem_start, mem_end, l, 4); 22005827d416SAnton Blanchard call_prom("getprop", 4, 1, node, pname, valp, l); 22019b6b563cSPaul Mackerras *mem_start = _ALIGN(*mem_start, 4); 22026e35d5daSBenjamin Herrenschmidt 22035827d416SAnton Blanchard if (!strcmp(pname, "phandle")) 22046e35d5daSBenjamin Herrenschmidt has_phandle = 1; 22059b6b563cSPaul Mackerras } 22069b6b563cSPaul Mackerras 22076e35d5daSBenjamin Herrenschmidt /* Add a "linux,phandle" property if no "phandle" property already 22086e35d5daSBenjamin Herrenschmidt * existed (can happen with OPAL) 22096e35d5daSBenjamin Herrenschmidt */ 22106e35d5daSBenjamin Herrenschmidt if (!has_phandle) { 22115827d416SAnton Blanchard soff = dt_find_string("linux,phandle"); 22129b6b563cSPaul Mackerras if (soff == 0) 22139b6b563cSPaul Mackerras prom_printf("WARNING: Can't find string index for" 22149b6b563cSPaul Mackerras " <linux-phandle> node %s\n", path); 22159b6b563cSPaul Mackerras else { 22169b6b563cSPaul Mackerras dt_push_token(OF_DT_PROP, mem_start, mem_end); 22179b6b563cSPaul Mackerras dt_push_token(4, mem_start, mem_end); 22189b6b563cSPaul Mackerras dt_push_token(soff, mem_start, mem_end); 22199b6b563cSPaul Mackerras valp = make_room(mem_start, mem_end, 4, 4); 2220493adffcSBenjamin Herrenschmidt *(__be32 *)valp = cpu_to_be32(node); 22219b6b563cSPaul Mackerras } 22226e35d5daSBenjamin Herrenschmidt } 22239b6b563cSPaul Mackerras 22249b6b563cSPaul Mackerras /* do all our children */ 22259b6b563cSPaul Mackerras child = call_prom("child", 1, 1, node); 22269b6b563cSPaul Mackerras while (child != 0) { 22279b6b563cSPaul Mackerras scan_dt_build_struct(child, mem_start, mem_end); 22289b6b563cSPaul Mackerras child = call_prom("peer", 1, 1, child); 22299b6b563cSPaul Mackerras } 22309b6b563cSPaul Mackerras 22319b6b563cSPaul Mackerras dt_push_token(OF_DT_END_NODE, mem_start, mem_end); 22329b6b563cSPaul Mackerras } 22339b6b563cSPaul Mackerras 22349b6b563cSPaul Mackerras static void __init flatten_device_tree(void) 22359b6b563cSPaul Mackerras { 22369b6b563cSPaul Mackerras phandle root; 22379b6b563cSPaul Mackerras unsigned long mem_start, mem_end, room; 22389b6b563cSPaul Mackerras struct boot_param_header *hdr; 22399b6b563cSPaul Mackerras char *namep; 22409b6b563cSPaul Mackerras u64 *rsvmap; 22419b6b563cSPaul Mackerras 22429b6b563cSPaul Mackerras /* 22439b6b563cSPaul Mackerras * Check how much room we have between alloc top & bottom (+/- a 2244fbafd728SAnton Blanchard * few pages), crop to 1MB, as this is our "chunk" size 22459b6b563cSPaul Mackerras */ 22465827d416SAnton Blanchard room = alloc_top - alloc_bottom - 0x4000; 22479b6b563cSPaul Mackerras if (room > DEVTREE_CHUNK_SIZE) 22489b6b563cSPaul Mackerras room = DEVTREE_CHUNK_SIZE; 22495827d416SAnton Blanchard prom_debug("starting device tree allocs at %x\n", alloc_bottom); 22509b6b563cSPaul Mackerras 22519b6b563cSPaul Mackerras /* Now try to claim that */ 22529b6b563cSPaul Mackerras mem_start = (unsigned long)alloc_up(room, PAGE_SIZE); 22539b6b563cSPaul Mackerras if (mem_start == 0) 22549b6b563cSPaul Mackerras prom_panic("Can't allocate initial device-tree chunk\n"); 2255966728ddSAnton Blanchard mem_end = mem_start + room; 22569b6b563cSPaul Mackerras 22579b6b563cSPaul Mackerras /* Get root of tree */ 22589b6b563cSPaul Mackerras root = call_prom("peer", 1, 1, (phandle)0); 22599b6b563cSPaul Mackerras if (root == (phandle)0) 22609b6b563cSPaul Mackerras prom_panic ("couldn't get device tree root\n"); 22619b6b563cSPaul Mackerras 22629b6b563cSPaul Mackerras /* Build header and make room for mem rsv map */ 22639b6b563cSPaul Mackerras mem_start = _ALIGN(mem_start, 4); 22649b6b563cSPaul Mackerras hdr = make_room(&mem_start, &mem_end, 22659b6b563cSPaul Mackerras sizeof(struct boot_param_header), 4); 22665827d416SAnton Blanchard dt_header_start = (unsigned long)hdr; 22679b6b563cSPaul Mackerras rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8); 22689b6b563cSPaul Mackerras 22699b6b563cSPaul Mackerras /* Start of strings */ 22709b6b563cSPaul Mackerras mem_start = PAGE_ALIGN(mem_start); 22715827d416SAnton Blanchard dt_string_start = mem_start; 22729b6b563cSPaul Mackerras mem_start += 4; /* hole */ 22739b6b563cSPaul Mackerras 22749b6b563cSPaul Mackerras /* Add "linux,phandle" in there, we'll need it */ 22759b6b563cSPaul Mackerras namep = make_room(&mem_start, &mem_end, 16, 1); 22765827d416SAnton Blanchard strcpy(namep, "linux,phandle"); 22779b6b563cSPaul Mackerras mem_start = (unsigned long)namep + strlen(namep) + 1; 22789b6b563cSPaul Mackerras 22799b6b563cSPaul Mackerras /* Build string array */ 22809b6b563cSPaul Mackerras prom_printf("Building dt strings...\n"); 22819b6b563cSPaul Mackerras scan_dt_build_strings(root, &mem_start, &mem_end); 22825827d416SAnton Blanchard dt_string_end = mem_start; 22839b6b563cSPaul Mackerras 22849b6b563cSPaul Mackerras /* Build structure */ 22859b6b563cSPaul Mackerras mem_start = PAGE_ALIGN(mem_start); 22865827d416SAnton Blanchard dt_struct_start = mem_start; 22879b6b563cSPaul Mackerras prom_printf("Building dt structure...\n"); 22889b6b563cSPaul Mackerras scan_dt_build_struct(root, &mem_start, &mem_end); 22899b6b563cSPaul Mackerras dt_push_token(OF_DT_END, &mem_start, &mem_end); 22905827d416SAnton Blanchard dt_struct_end = PAGE_ALIGN(mem_start); 22919b6b563cSPaul Mackerras 22929b6b563cSPaul Mackerras /* Finish header */ 2293493adffcSBenjamin Herrenschmidt hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu); 2294493adffcSBenjamin Herrenschmidt hdr->magic = cpu_to_be32(OF_DT_HEADER); 2295493adffcSBenjamin Herrenschmidt hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start); 2296493adffcSBenjamin Herrenschmidt hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start); 2297493adffcSBenjamin Herrenschmidt hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start); 2298493adffcSBenjamin Herrenschmidt hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start); 2299493adffcSBenjamin Herrenschmidt hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start); 2300493adffcSBenjamin Herrenschmidt hdr->version = cpu_to_be32(OF_DT_VERSION); 23019b6b563cSPaul Mackerras /* Version 16 is not backward compatible */ 2302493adffcSBenjamin Herrenschmidt hdr->last_comp_version = cpu_to_be32(0x10); 23039b6b563cSPaul Mackerras 23044d1f3f25SJimi Xenidis /* Copy the reserve map in */ 23055827d416SAnton Blanchard memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map)); 23069b6b563cSPaul Mackerras 23079b6b563cSPaul Mackerras #ifdef DEBUG_PROM 23089b6b563cSPaul Mackerras { 23099b6b563cSPaul Mackerras int i; 23109b6b563cSPaul Mackerras prom_printf("reserved memory map:\n"); 23115827d416SAnton Blanchard for (i = 0; i < mem_reserve_cnt; i++) 23129b6b563cSPaul Mackerras prom_printf(" %x - %x\n", 2313493adffcSBenjamin Herrenschmidt be64_to_cpu(mem_reserve_map[i].base), 2314493adffcSBenjamin Herrenschmidt be64_to_cpu(mem_reserve_map[i].size)); 23159b6b563cSPaul Mackerras } 23169b6b563cSPaul Mackerras #endif 23174d1f3f25SJimi Xenidis /* Bump mem_reserve_cnt to cause further reservations to fail 23184d1f3f25SJimi Xenidis * since it's too late. 23194d1f3f25SJimi Xenidis */ 23205827d416SAnton Blanchard mem_reserve_cnt = MEM_RESERVE_MAP_SIZE; 23219b6b563cSPaul Mackerras 23229b6b563cSPaul Mackerras prom_printf("Device tree strings 0x%x -> 0x%x\n", 23235827d416SAnton Blanchard dt_string_start, dt_string_end); 23249b6b563cSPaul Mackerras prom_printf("Device tree struct 0x%x -> 0x%x\n", 23255827d416SAnton Blanchard dt_struct_start, dt_struct_end); 23269b6b563cSPaul Mackerras } 23279b6b563cSPaul Mackerras 232854f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE 232954f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property. 233054f4ee18SHollis Blanchard * The values are bad, and it doesn't even have the right number of cells. */ 233154f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void) 23329b6b563cSPaul Mackerras { 233354f4ee18SHollis Blanchard phandle isa; 2334980a6513SBenjamin Herrenschmidt u32 rloc = 0x01002000; /* IO space; PCI device = 4 */ 233554f4ee18SHollis Blanchard u32 isa_ranges[6]; 2336980a6513SBenjamin Herrenschmidt char *name; 233754f4ee18SHollis Blanchard 2338980a6513SBenjamin Herrenschmidt name = "/ht@0/isa@4"; 2339980a6513SBenjamin Herrenschmidt isa = call_prom("finddevice", 1, 1, ADDR(name)); 2340980a6513SBenjamin Herrenschmidt if (!PHANDLE_VALID(isa)) { 2341980a6513SBenjamin Herrenschmidt name = "/ht@0/isa@6"; 2342980a6513SBenjamin Herrenschmidt isa = call_prom("finddevice", 1, 1, ADDR(name)); 2343980a6513SBenjamin Herrenschmidt rloc = 0x01003000; /* IO space; PCI device = 6 */ 2344980a6513SBenjamin Herrenschmidt } 234554f4ee18SHollis Blanchard if (!PHANDLE_VALID(isa)) 234654f4ee18SHollis Blanchard return; 234754f4ee18SHollis Blanchard 2348980a6513SBenjamin Herrenschmidt if (prom_getproplen(isa, "ranges") != 12) 2349980a6513SBenjamin Herrenschmidt return; 235054f4ee18SHollis Blanchard if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges)) 235154f4ee18SHollis Blanchard == PROM_ERROR) 235254f4ee18SHollis Blanchard return; 235354f4ee18SHollis Blanchard 235454f4ee18SHollis Blanchard if (isa_ranges[0] != 0x1 || 235554f4ee18SHollis Blanchard isa_ranges[1] != 0xf4000000 || 235654f4ee18SHollis Blanchard isa_ranges[2] != 0x00010000) 235754f4ee18SHollis Blanchard return; 235854f4ee18SHollis Blanchard 2359980a6513SBenjamin Herrenschmidt prom_printf("Fixing up bogus ISA range on Maple/Apache...\n"); 236054f4ee18SHollis Blanchard 236154f4ee18SHollis Blanchard isa_ranges[0] = 0x1; 236254f4ee18SHollis Blanchard isa_ranges[1] = 0x0; 2363980a6513SBenjamin Herrenschmidt isa_ranges[2] = rloc; 236454f4ee18SHollis Blanchard isa_ranges[3] = 0x0; 236554f4ee18SHollis Blanchard isa_ranges[4] = 0x0; 236654f4ee18SHollis Blanchard isa_ranges[5] = 0x00010000; 2367980a6513SBenjamin Herrenschmidt prom_setprop(isa, name, "ranges", 236854f4ee18SHollis Blanchard isa_ranges, sizeof(isa_ranges)); 236954f4ee18SHollis Blanchard } 23708f101a05SHarry Ciao 23718f101a05SHarry Ciao #define CPC925_MC_START 0xf8000000 23728f101a05SHarry Ciao #define CPC925_MC_LENGTH 0x1000000 23738f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */ 23748f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void) 23758f101a05SHarry Ciao { 23768f101a05SHarry Ciao phandle mc; 23778f101a05SHarry Ciao u32 mc_reg[4]; 23788f101a05SHarry Ciao char *name = "/hostbridge@f8000000"; 23798f101a05SHarry Ciao u32 ac, sc; 23808f101a05SHarry Ciao 23818f101a05SHarry Ciao mc = call_prom("finddevice", 1, 1, ADDR(name)); 23828f101a05SHarry Ciao if (!PHANDLE_VALID(mc)) 23838f101a05SHarry Ciao return; 23848f101a05SHarry Ciao 23858f101a05SHarry Ciao if (prom_getproplen(mc, "reg") != 8) 23868f101a05SHarry Ciao return; 23878f101a05SHarry Ciao 23885827d416SAnton Blanchard prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac)); 23895827d416SAnton Blanchard prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc)); 23908f101a05SHarry Ciao if ((ac != 2) || (sc != 2)) 23918f101a05SHarry Ciao return; 23928f101a05SHarry Ciao 23938f101a05SHarry Ciao if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR) 23948f101a05SHarry Ciao return; 23958f101a05SHarry Ciao 23968f101a05SHarry Ciao if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH) 23978f101a05SHarry Ciao return; 23988f101a05SHarry Ciao 23998f101a05SHarry Ciao prom_printf("Fixing up bogus hostbridge on Maple...\n"); 24008f101a05SHarry Ciao 24018f101a05SHarry Ciao mc_reg[0] = 0x0; 24028f101a05SHarry Ciao mc_reg[1] = CPC925_MC_START; 24038f101a05SHarry Ciao mc_reg[2] = 0x0; 24048f101a05SHarry Ciao mc_reg[3] = CPC925_MC_LENGTH; 24058f101a05SHarry Ciao prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg)); 24068f101a05SHarry Ciao } 240754f4ee18SHollis Blanchard #else 240854f4ee18SHollis Blanchard #define fixup_device_tree_maple() 24098f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller() 241054f4ee18SHollis Blanchard #endif 241154f4ee18SHollis Blanchard 2412e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP 2413e4805922SOlaf Hering /* 2414e4805922SOlaf Hering * Pegasos and BriQ lacks the "ranges" property in the isa node 2415e4805922SOlaf Hering * Pegasos needs decimal IRQ 14/15, not hexadecimal 2416556ecf9bSOlaf Hering * Pegasos has the IDE configured in legacy mode, but advertised as native 2417e4805922SOlaf Hering */ 2418e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void) 2419e8c0acf9SBenjamin Herrenschmidt { 2420e4805922SOlaf Hering phandle ph; 2421e4805922SOlaf Hering u32 prop[6]; 242226c5032eSBenjamin Herrenschmidt u32 rloc = 0x01006000; /* IO space; PCI device = 12 */ 2423e8c0acf9SBenjamin Herrenschmidt char *name; 2424e8c0acf9SBenjamin Herrenschmidt int rc; 2425e8c0acf9SBenjamin Herrenschmidt 2426e8c0acf9SBenjamin Herrenschmidt name = "/pci@80000000/isa@c"; 2427e4805922SOlaf Hering ph = call_prom("finddevice", 1, 1, ADDR(name)); 2428e4805922SOlaf Hering if (!PHANDLE_VALID(ph)) { 242926c5032eSBenjamin Herrenschmidt name = "/pci@ff500000/isa@6"; 2430e4805922SOlaf Hering ph = call_prom("finddevice", 1, 1, ADDR(name)); 243126c5032eSBenjamin Herrenschmidt rloc = 0x01003000; /* IO space; PCI device = 6 */ 243226c5032eSBenjamin Herrenschmidt } 2433e4805922SOlaf Hering if (PHANDLE_VALID(ph)) { 2434e4805922SOlaf Hering rc = prom_getproplen(ph, "ranges"); 2435e4805922SOlaf Hering if (rc == 0 || rc == PROM_ERROR) { 2436e8c0acf9SBenjamin Herrenschmidt prom_printf("Fixing up missing ISA range on Pegasos...\n"); 2437e8c0acf9SBenjamin Herrenschmidt 2438e4805922SOlaf Hering prop[0] = 0x1; 2439e4805922SOlaf Hering prop[1] = 0x0; 2440e4805922SOlaf Hering prop[2] = rloc; 2441e4805922SOlaf Hering prop[3] = 0x0; 2442e4805922SOlaf Hering prop[4] = 0x0; 2443e4805922SOlaf Hering prop[5] = 0x00010000; 2444e4805922SOlaf Hering prom_setprop(ph, name, "ranges", prop, sizeof(prop)); 2445e4805922SOlaf Hering } 2446e4805922SOlaf Hering } 2447e4805922SOlaf Hering 2448e4805922SOlaf Hering name = "/pci@80000000/ide@C,1"; 2449e4805922SOlaf Hering ph = call_prom("finddevice", 1, 1, ADDR(name)); 2450e4805922SOlaf Hering if (PHANDLE_VALID(ph)) { 2451e4805922SOlaf Hering prom_printf("Fixing up IDE interrupt on Pegasos...\n"); 2452e4805922SOlaf Hering prop[0] = 14; 2453e4805922SOlaf Hering prop[1] = 0x0; 2454556ecf9bSOlaf Hering prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32)); 2455556ecf9bSOlaf Hering prom_printf("Fixing up IDE class-code on Pegasos...\n"); 2456556ecf9bSOlaf Hering rc = prom_getprop(ph, "class-code", prop, sizeof(u32)); 2457556ecf9bSOlaf Hering if (rc == sizeof(u32)) { 2458556ecf9bSOlaf Hering prop[0] &= ~0x5; 2459556ecf9bSOlaf Hering prom_setprop(ph, name, "class-code", prop, sizeof(u32)); 2460556ecf9bSOlaf Hering } 2461e4805922SOlaf Hering } 2462e8c0acf9SBenjamin Herrenschmidt } 2463e8c0acf9SBenjamin Herrenschmidt #else 2464e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp() 2465e8c0acf9SBenjamin Herrenschmidt #endif 2466e8c0acf9SBenjamin Herrenschmidt 24679b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC) 246854f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void) 246954f4ee18SHollis Blanchard { 24709b6b563cSPaul Mackerras phandle u3, i2c, mpic; 24719b6b563cSPaul Mackerras u32 u3_rev; 24729b6b563cSPaul Mackerras u32 interrupts[2]; 24739b6b563cSPaul Mackerras u32 parent; 24749b6b563cSPaul Mackerras 24759b6b563cSPaul Mackerras /* Some G5s have a missing interrupt definition, fix it up here */ 24769b6b563cSPaul Mackerras u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000")); 24779b6b563cSPaul Mackerras if (!PHANDLE_VALID(u3)) 24789b6b563cSPaul Mackerras return; 24799b6b563cSPaul Mackerras i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000")); 24809b6b563cSPaul Mackerras if (!PHANDLE_VALID(i2c)) 24819b6b563cSPaul Mackerras return; 24829b6b563cSPaul Mackerras mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000")); 24839b6b563cSPaul Mackerras if (!PHANDLE_VALID(mpic)) 24849b6b563cSPaul Mackerras return; 24859b6b563cSPaul Mackerras 24869b6b563cSPaul Mackerras /* check if proper rev of u3 */ 24879b6b563cSPaul Mackerras if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev)) 24889b6b563cSPaul Mackerras == PROM_ERROR) 24899b6b563cSPaul Mackerras return; 24907d49697eSBenjamin Herrenschmidt if (u3_rev < 0x35 || u3_rev > 0x39) 24919b6b563cSPaul Mackerras return; 24929b6b563cSPaul Mackerras /* does it need fixup ? */ 24939b6b563cSPaul Mackerras if (prom_getproplen(i2c, "interrupts") > 0) 24949b6b563cSPaul Mackerras return; 24959b6b563cSPaul Mackerras 24969b6b563cSPaul Mackerras prom_printf("fixing up bogus interrupts for u3 i2c...\n"); 24979b6b563cSPaul Mackerras 24989b6b563cSPaul Mackerras /* interrupt on this revision of u3 is number 0 and level */ 24999b6b563cSPaul Mackerras interrupts[0] = 0; 25009b6b563cSPaul Mackerras interrupts[1] = 1; 2501a23414beSPaul Mackerras prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts", 2502a23414beSPaul Mackerras &interrupts, sizeof(interrupts)); 25039b6b563cSPaul Mackerras parent = (u32)mpic; 2504a23414beSPaul Mackerras prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent", 2505a23414beSPaul Mackerras &parent, sizeof(parent)); 25069b6b563cSPaul Mackerras } 250754f4ee18SHollis Blanchard #else 250854f4ee18SHollis Blanchard #define fixup_device_tree_pmac() 250954f4ee18SHollis Blanchard #endif 25109b6b563cSPaul Mackerras 251188fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA 251294d2dde7SGrant Likely /* 251394d2dde7SGrant Likely * The MPC5200 FEC driver requires an phy-handle property to tell it how 251494d2dde7SGrant Likely * to talk to the phy. If the phy-handle property is missing, then this 251594d2dde7SGrant Likely * function is called to add the appropriate nodes and link it to the 251694d2dde7SGrant Likely * ethernet node. 251794d2dde7SGrant Likely */ 251894d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void) 251988fd2a9dSSylvain Munaut { 252088fd2a9dSSylvain Munaut u32 node; 252188fd2a9dSSylvain Munaut char prop[64]; 252294d2dde7SGrant Likely int rv; 252388fd2a9dSSylvain Munaut 252494d2dde7SGrant Likely /* Check if /builtin/ethernet exists - bail if it doesn't */ 252594d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet")); 252688fd2a9dSSylvain Munaut if (!PHANDLE_VALID(node)) 252788fd2a9dSSylvain Munaut return; 252888fd2a9dSSylvain Munaut 252994d2dde7SGrant Likely /* Check if the phy-handle property exists - bail if it does */ 253094d2dde7SGrant Likely rv = prom_getprop(node, "phy-handle", prop, sizeof(prop)); 253194d2dde7SGrant Likely if (!rv) 253288fd2a9dSSylvain Munaut return; 253388fd2a9dSSylvain Munaut 253494d2dde7SGrant Likely /* 253594d2dde7SGrant Likely * At this point the ethernet device doesn't have a phy described. 253694d2dde7SGrant Likely * Now we need to add the missing phy node and linkage 253794d2dde7SGrant Likely */ 253888fd2a9dSSylvain Munaut 253994d2dde7SGrant Likely /* Check for an MDIO bus node - if missing then create one */ 25406f4347c9SOlaf Hering node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio")); 25416f4347c9SOlaf Hering if (!PHANDLE_VALID(node)) { 25426f4347c9SOlaf Hering prom_printf("Adding Ethernet MDIO node\n"); 25436f4347c9SOlaf Hering call_prom("interpret", 1, 1, 25446f4347c9SOlaf Hering " s\" /builtin\" find-device" 25456f4347c9SOlaf Hering " new-device" 25466f4347c9SOlaf Hering " 1 encode-int s\" #address-cells\" property" 25476f4347c9SOlaf Hering " 0 encode-int s\" #size-cells\" property" 254894d2dde7SGrant Likely " s\" mdio\" device-name" 254994d2dde7SGrant Likely " s\" fsl,mpc5200b-mdio\" encode-string" 25506f4347c9SOlaf Hering " s\" compatible\" property" 25516f4347c9SOlaf Hering " 0xf0003000 0x400 reg" 25526f4347c9SOlaf Hering " 0x2 encode-int" 25536f4347c9SOlaf Hering " 0x5 encode-int encode+" 25546f4347c9SOlaf Hering " 0x3 encode-int encode+" 25556f4347c9SOlaf Hering " s\" interrupts\" property" 25566f4347c9SOlaf Hering " finish-device"); 25576f4347c9SOlaf Hering }; 25586f4347c9SOlaf Hering 255994d2dde7SGrant Likely /* Check for a PHY device node - if missing then create one and 256094d2dde7SGrant Likely * give it's phandle to the ethernet node */ 256194d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, 256294d2dde7SGrant Likely ADDR("/builtin/mdio/ethernet-phy")); 25636f4347c9SOlaf Hering if (!PHANDLE_VALID(node)) { 25646f4347c9SOlaf Hering prom_printf("Adding Ethernet PHY node\n"); 25656f4347c9SOlaf Hering call_prom("interpret", 1, 1, 25666f4347c9SOlaf Hering " s\" /builtin/mdio\" find-device" 25676f4347c9SOlaf Hering " new-device" 25686f4347c9SOlaf Hering " s\" ethernet-phy\" device-name" 25696f4347c9SOlaf Hering " 0x10 encode-int s\" reg\" property" 25706f4347c9SOlaf Hering " my-self" 25716f4347c9SOlaf Hering " ihandle>phandle" 25726f4347c9SOlaf Hering " finish-device" 25736f4347c9SOlaf Hering " s\" /builtin/ethernet\" find-device" 25746f4347c9SOlaf Hering " encode-int" 25756f4347c9SOlaf Hering " s\" phy-handle\" property" 25766f4347c9SOlaf Hering " device-end"); 25776f4347c9SOlaf Hering } 257894d2dde7SGrant Likely } 25796f4347c9SOlaf Hering 258094d2dde7SGrant Likely static void __init fixup_device_tree_efika(void) 258194d2dde7SGrant Likely { 258294d2dde7SGrant Likely int sound_irq[3] = { 2, 2, 0 }; 258394d2dde7SGrant Likely int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0, 258494d2dde7SGrant Likely 3,4,0, 3,5,0, 3,6,0, 3,7,0, 258594d2dde7SGrant Likely 3,8,0, 3,9,0, 3,10,0, 3,11,0, 258694d2dde7SGrant Likely 3,12,0, 3,13,0, 3,14,0, 3,15,0 }; 258794d2dde7SGrant Likely u32 node; 258894d2dde7SGrant Likely char prop[64]; 258994d2dde7SGrant Likely int rv, len; 259094d2dde7SGrant Likely 259194d2dde7SGrant Likely /* Check if we're really running on a EFIKA */ 259294d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, ADDR("/")); 259394d2dde7SGrant Likely if (!PHANDLE_VALID(node)) 259494d2dde7SGrant Likely return; 259594d2dde7SGrant Likely 259694d2dde7SGrant Likely rv = prom_getprop(node, "model", prop, sizeof(prop)); 259794d2dde7SGrant Likely if (rv == PROM_ERROR) 259894d2dde7SGrant Likely return; 259994d2dde7SGrant Likely if (strcmp(prop, "EFIKA5K2")) 260094d2dde7SGrant Likely return; 260194d2dde7SGrant Likely 260294d2dde7SGrant Likely prom_printf("Applying EFIKA device tree fixups\n"); 260394d2dde7SGrant Likely 260494d2dde7SGrant Likely /* Claiming to be 'chrp' is death */ 260594d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, ADDR("/")); 260694d2dde7SGrant Likely rv = prom_getprop(node, "device_type", prop, sizeof(prop)); 260794d2dde7SGrant Likely if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0)) 260894d2dde7SGrant Likely prom_setprop(node, "/", "device_type", "efika", sizeof("efika")); 260994d2dde7SGrant Likely 26107f4392cdSDavid Woodhouse /* CODEGEN,description is exposed in /proc/cpuinfo so 26117f4392cdSDavid Woodhouse fix that too */ 26127f4392cdSDavid Woodhouse rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop)); 26137f4392cdSDavid Woodhouse if (rv != PROM_ERROR && (strstr(prop, "CHRP"))) 26147f4392cdSDavid Woodhouse prom_setprop(node, "/", "CODEGEN,description", 26157f4392cdSDavid Woodhouse "Efika 5200B PowerPC System", 26167f4392cdSDavid Woodhouse sizeof("Efika 5200B PowerPC System")); 26177f4392cdSDavid Woodhouse 261894d2dde7SGrant Likely /* Fixup bestcomm interrupts property */ 261994d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm")); 262094d2dde7SGrant Likely if (PHANDLE_VALID(node)) { 262194d2dde7SGrant Likely len = prom_getproplen(node, "interrupts"); 262294d2dde7SGrant Likely if (len == 12) { 262394d2dde7SGrant Likely prom_printf("Fixing bestcomm interrupts property\n"); 262494d2dde7SGrant Likely prom_setprop(node, "/builtin/bestcom", "interrupts", 262594d2dde7SGrant Likely bcomm_irq, sizeof(bcomm_irq)); 262694d2dde7SGrant Likely } 262794d2dde7SGrant Likely } 262894d2dde7SGrant Likely 262994d2dde7SGrant Likely /* Fixup sound interrupts property */ 263094d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound")); 263194d2dde7SGrant Likely if (PHANDLE_VALID(node)) { 263294d2dde7SGrant Likely rv = prom_getprop(node, "interrupts", prop, sizeof(prop)); 263394d2dde7SGrant Likely if (rv == PROM_ERROR) { 263494d2dde7SGrant Likely prom_printf("Adding sound interrupts property\n"); 263594d2dde7SGrant Likely prom_setprop(node, "/builtin/sound", "interrupts", 263694d2dde7SGrant Likely sound_irq, sizeof(sound_irq)); 263794d2dde7SGrant Likely } 263894d2dde7SGrant Likely } 263994d2dde7SGrant Likely 264094d2dde7SGrant Likely /* Make sure ethernet phy-handle property exists */ 264194d2dde7SGrant Likely fixup_device_tree_efika_add_phy(); 264288fd2a9dSSylvain Munaut } 264388fd2a9dSSylvain Munaut #else 264488fd2a9dSSylvain Munaut #define fixup_device_tree_efika() 264588fd2a9dSSylvain Munaut #endif 264688fd2a9dSSylvain Munaut 264750246787SDarren Stevens #ifdef CONFIG_PPC_PASEMI_NEMO 264850246787SDarren Stevens /* 264950246787SDarren Stevens * CFE supplied on Nemo is broken in several ways, biggest 265050246787SDarren Stevens * problem is that it reassigns ISA interrupts to unused mpic ints. 265150246787SDarren Stevens * Add an interrupt-controller property for the io-bridge to use 265250246787SDarren Stevens * and correct the ints so we can attach them to an irq_domain 265350246787SDarren Stevens */ 265450246787SDarren Stevens static void __init fixup_device_tree_pasemi(void) 265550246787SDarren Stevens { 265650246787SDarren Stevens u32 interrupts[2], parent, rval, val = 0; 265750246787SDarren Stevens char *name, *pci_name; 265850246787SDarren Stevens phandle iob, node; 265950246787SDarren Stevens 266050246787SDarren Stevens /* Find the root pci node */ 266150246787SDarren Stevens name = "/pxp@0,e0000000"; 266250246787SDarren Stevens iob = call_prom("finddevice", 1, 1, ADDR(name)); 266350246787SDarren Stevens if (!PHANDLE_VALID(iob)) 266450246787SDarren Stevens return; 266550246787SDarren Stevens 266650246787SDarren Stevens /* check if interrupt-controller node set yet */ 266750246787SDarren Stevens if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR) 266850246787SDarren Stevens return; 266950246787SDarren Stevens 267050246787SDarren Stevens prom_printf("adding interrupt-controller property for SB600...\n"); 267150246787SDarren Stevens 267250246787SDarren Stevens prom_setprop(iob, name, "interrupt-controller", &val, 0); 267350246787SDarren Stevens 267450246787SDarren Stevens pci_name = "/pxp@0,e0000000/pci@11"; 267550246787SDarren Stevens node = call_prom("finddevice", 1, 1, ADDR(pci_name)); 267650246787SDarren Stevens parent = ADDR(iob); 267750246787SDarren Stevens 267850246787SDarren Stevens for( ; prom_next_node(&node); ) { 267950246787SDarren Stevens /* scan each node for one with an interrupt */ 268050246787SDarren Stevens if (!PHANDLE_VALID(node)) 268150246787SDarren Stevens continue; 268250246787SDarren Stevens 268350246787SDarren Stevens rval = prom_getproplen(node, "interrupts"); 268450246787SDarren Stevens if (rval == 0 || rval == PROM_ERROR) 268550246787SDarren Stevens continue; 268650246787SDarren Stevens 268750246787SDarren Stevens prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts)); 268850246787SDarren Stevens if ((interrupts[0] < 212) || (interrupts[0] > 222)) 268950246787SDarren Stevens continue; 269050246787SDarren Stevens 269150246787SDarren Stevens /* found a node, update both interrupts and interrupt-parent */ 269250246787SDarren Stevens if ((interrupts[0] >= 212) && (interrupts[0] <= 215)) 269350246787SDarren Stevens interrupts[0] -= 203; 269450246787SDarren Stevens if ((interrupts[0] >= 216) && (interrupts[0] <= 220)) 269550246787SDarren Stevens interrupts[0] -= 213; 269650246787SDarren Stevens if (interrupts[0] == 221) 269750246787SDarren Stevens interrupts[0] = 14; 269850246787SDarren Stevens if (interrupts[0] == 222) 269950246787SDarren Stevens interrupts[0] = 8; 270050246787SDarren Stevens 270150246787SDarren Stevens prom_setprop(node, pci_name, "interrupts", interrupts, 270250246787SDarren Stevens sizeof(interrupts)); 270350246787SDarren Stevens prom_setprop(node, pci_name, "interrupt-parent", &parent, 270450246787SDarren Stevens sizeof(parent)); 270550246787SDarren Stevens } 2706*687e16bcSDarren Stevens 2707*687e16bcSDarren Stevens /* 2708*687e16bcSDarren Stevens * The io-bridge has device_type set to 'io-bridge' change it to 'isa' 2709*687e16bcSDarren Stevens * so that generic isa-bridge code can add the SB600 and its on-board 2710*687e16bcSDarren Stevens * peripherals. 2711*687e16bcSDarren Stevens */ 2712*687e16bcSDarren Stevens name = "/pxp@0,e0000000/io-bridge@0"; 2713*687e16bcSDarren Stevens iob = call_prom("finddevice", 1, 1, ADDR(name)); 2714*687e16bcSDarren Stevens if (!PHANDLE_VALID(iob)) 2715*687e16bcSDarren Stevens return; 2716*687e16bcSDarren Stevens 2717*687e16bcSDarren Stevens /* device_type is already set, just change it. */ 2718*687e16bcSDarren Stevens 2719*687e16bcSDarren Stevens prom_printf("Changing device_type of SB600 node...\n"); 2720*687e16bcSDarren Stevens 2721*687e16bcSDarren Stevens prom_setprop(iob, name, "device_type", "isa", sizeof("isa")); 272250246787SDarren Stevens } 272350246787SDarren Stevens #else /* !CONFIG_PPC_PASEMI_NEMO */ 272450246787SDarren Stevens static inline void fixup_device_tree_pasemi(void) { } 272550246787SDarren Stevens #endif 272650246787SDarren Stevens 272754f4ee18SHollis Blanchard static void __init fixup_device_tree(void) 272854f4ee18SHollis Blanchard { 272954f4ee18SHollis Blanchard fixup_device_tree_maple(); 27308f101a05SHarry Ciao fixup_device_tree_maple_memory_controller(); 2731e8c0acf9SBenjamin Herrenschmidt fixup_device_tree_chrp(); 273254f4ee18SHollis Blanchard fixup_device_tree_pmac(); 273388fd2a9dSSylvain Munaut fixup_device_tree_efika(); 273450246787SDarren Stevens fixup_device_tree_pasemi(); 273554f4ee18SHollis Blanchard } 27369b6b563cSPaul Mackerras 27379b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void) 27389b6b563cSPaul Mackerras { 2739493adffcSBenjamin Herrenschmidt __be32 rval; 27409b6b563cSPaul Mackerras ihandle prom_cpu; 27419b6b563cSPaul Mackerras phandle cpu_pkg; 27429b6b563cSPaul Mackerras 2743493adffcSBenjamin Herrenschmidt rval = 0; 2744493adffcSBenjamin Herrenschmidt if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0) 2745a575b807SPaul Mackerras return; 2746493adffcSBenjamin Herrenschmidt prom_cpu = be32_to_cpu(rval); 27479b6b563cSPaul Mackerras 27489b6b563cSPaul Mackerras cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu); 27499b6b563cSPaul Mackerras 2750493adffcSBenjamin Herrenschmidt prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval)); 2751493adffcSBenjamin Herrenschmidt prom.cpu = be32_to_cpu(rval); 27529b6b563cSPaul Mackerras 27535827d416SAnton Blanchard prom_debug("Booting CPU hw index = %lu\n", prom.cpu); 27549b6b563cSPaul Mackerras } 27559b6b563cSPaul Mackerras 27569b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4) 27579b6b563cSPaul Mackerras { 27589b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD 27599b6b563cSPaul Mackerras if (r3 && r4 && r4 != 0xdeadbeef) { 2760493adffcSBenjamin Herrenschmidt __be64 val; 27619b6b563cSPaul Mackerras 27625827d416SAnton Blanchard prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3; 27635827d416SAnton Blanchard prom_initrd_end = prom_initrd_start + r4; 27649b6b563cSPaul Mackerras 2765493adffcSBenjamin Herrenschmidt val = cpu_to_be64(prom_initrd_start); 27665827d416SAnton Blanchard prom_setprop(prom.chosen, "/chosen", "linux,initrd-start", 2767a23414beSPaul Mackerras &val, sizeof(val)); 2768493adffcSBenjamin Herrenschmidt val = cpu_to_be64(prom_initrd_end); 27695827d416SAnton Blanchard prom_setprop(prom.chosen, "/chosen", "linux,initrd-end", 2770a23414beSPaul Mackerras &val, sizeof(val)); 27719b6b563cSPaul Mackerras 27725827d416SAnton Blanchard reserve_mem(prom_initrd_start, 27735827d416SAnton Blanchard prom_initrd_end - prom_initrd_start); 27749b6b563cSPaul Mackerras 27755827d416SAnton Blanchard prom_debug("initrd_start=0x%x\n", prom_initrd_start); 27765827d416SAnton Blanchard prom_debug("initrd_end=0x%x\n", prom_initrd_end); 27779b6b563cSPaul Mackerras } 27789b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */ 27799b6b563cSPaul Mackerras } 27809b6b563cSPaul Mackerras 27815ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64 27825ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE 27835ac47f7aSAnton Blanchard static void reloc_toc(void) 27845ac47f7aSAnton Blanchard { 27855ac47f7aSAnton Blanchard } 27865ac47f7aSAnton Blanchard 27875ac47f7aSAnton Blanchard static void unreloc_toc(void) 27885ac47f7aSAnton Blanchard { 27895ac47f7aSAnton Blanchard } 27905ac47f7aSAnton Blanchard #else 27911674400aSAnton Blanchard static void __reloc_toc(unsigned long offset, unsigned long nr_entries) 27925ac47f7aSAnton Blanchard { 27935ac47f7aSAnton Blanchard unsigned long i; 27941674400aSAnton Blanchard unsigned long *toc_entry; 27951674400aSAnton Blanchard 27961674400aSAnton Blanchard /* Get the start of the TOC by using r2 directly. */ 27971674400aSAnton Blanchard asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry)); 27985ac47f7aSAnton Blanchard 27995ac47f7aSAnton Blanchard for (i = 0; i < nr_entries; i++) { 28005ac47f7aSAnton Blanchard *toc_entry = *toc_entry + offset; 28015ac47f7aSAnton Blanchard toc_entry++; 28025ac47f7aSAnton Blanchard } 28035ac47f7aSAnton Blanchard } 28045ac47f7aSAnton Blanchard 28055ac47f7aSAnton Blanchard static void reloc_toc(void) 28065ac47f7aSAnton Blanchard { 28075ac47f7aSAnton Blanchard unsigned long offset = reloc_offset(); 28085ac47f7aSAnton Blanchard unsigned long nr_entries = 28095ac47f7aSAnton Blanchard (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long); 28105ac47f7aSAnton Blanchard 28111674400aSAnton Blanchard __reloc_toc(offset, nr_entries); 28125ac47f7aSAnton Blanchard 28135ac47f7aSAnton Blanchard mb(); 28145ac47f7aSAnton Blanchard } 28155ac47f7aSAnton Blanchard 28165ac47f7aSAnton Blanchard static void unreloc_toc(void) 28175ac47f7aSAnton Blanchard { 28185ac47f7aSAnton Blanchard unsigned long offset = reloc_offset(); 28195ac47f7aSAnton Blanchard unsigned long nr_entries = 28205ac47f7aSAnton Blanchard (__prom_init_toc_end - __prom_init_toc_start) / sizeof(long); 28215ac47f7aSAnton Blanchard 28225ac47f7aSAnton Blanchard mb(); 28235ac47f7aSAnton Blanchard 28241674400aSAnton Blanchard __reloc_toc(-offset, nr_entries); 28255ac47f7aSAnton Blanchard } 28265ac47f7aSAnton Blanchard #endif 28275ac47f7aSAnton Blanchard #endif 282827f44888SBenjamin Herrenschmidt 28299b6b563cSPaul Mackerras /* 28309b6b563cSPaul Mackerras * We enter here early on, when the Open Firmware prom is still 28319b6b563cSPaul Mackerras * handling exceptions and the MMU hash table for us. 28329b6b563cSPaul Mackerras */ 28339b6b563cSPaul Mackerras 28349b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4, 28359b6b563cSPaul Mackerras unsigned long pp, 2836549e8152SPaul Mackerras unsigned long r6, unsigned long r7, 2837549e8152SPaul Mackerras unsigned long kbase) 28389b6b563cSPaul Mackerras { 28399b6b563cSPaul Mackerras unsigned long hdr; 28409b6b563cSPaul Mackerras 28419b6b563cSPaul Mackerras #ifdef CONFIG_PPC32 2842549e8152SPaul Mackerras unsigned long offset = reloc_offset(); 28439b6b563cSPaul Mackerras reloc_got2(offset); 28445ac47f7aSAnton Blanchard #else 28455ac47f7aSAnton Blanchard reloc_toc(); 28469b6b563cSPaul Mackerras #endif 28479b6b563cSPaul Mackerras 28489b6b563cSPaul Mackerras /* 28499b6b563cSPaul Mackerras * First zero the BSS 28509b6b563cSPaul Mackerras */ 28515827d416SAnton Blanchard memset(&__bss_start, 0, __bss_stop - __bss_start); 28529b6b563cSPaul Mackerras 28539b6b563cSPaul Mackerras /* 28549b6b563cSPaul Mackerras * Init interface to Open Firmware, get some node references, 28559b6b563cSPaul Mackerras * like /chosen 28569b6b563cSPaul Mackerras */ 28579b6b563cSPaul Mackerras prom_init_client_services(pp); 28589b6b563cSPaul Mackerras 28599b6b563cSPaul Mackerras /* 2860a23414beSPaul Mackerras * See if this OF is old enough that we need to do explicit maps 2861a23414beSPaul Mackerras * and other workarounds 2862a23414beSPaul Mackerras */ 2863a23414beSPaul Mackerras prom_find_mmu(); 2864a23414beSPaul Mackerras 2865a23414beSPaul Mackerras /* 28669b6b563cSPaul Mackerras * Init prom stdout device 28679b6b563cSPaul Mackerras */ 28689b6b563cSPaul Mackerras prom_init_stdout(); 28699b6b563cSPaul Mackerras 28705827d416SAnton Blanchard prom_printf("Preparing to boot %s", linux_banner); 2871e7943fbbSMichael Ellerman 28729b6b563cSPaul Mackerras /* 28739b6b563cSPaul Mackerras * Get default machine type. At this point, we do not differentiate 28749b6b563cSPaul Mackerras * between pSeries SMP and pSeries LPAR 28759b6b563cSPaul Mackerras */ 28765827d416SAnton Blanchard of_platform = prom_find_machine_type(); 28775827d416SAnton Blanchard prom_printf("Detected machine type: %x\n", of_platform); 28789b6b563cSPaul Mackerras 28790f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL 2880add60ef3SOlaf Hering /* Bail if this is a kdump kernel. */ 2881add60ef3SOlaf Hering if (PHYSICAL_START > 0) 2882add60ef3SOlaf Hering prom_panic("Error: You can't boot a kdump kernel from OF!\n"); 2883549e8152SPaul Mackerras #endif 2884add60ef3SOlaf Hering 2885add60ef3SOlaf Hering /* 2886add60ef3SOlaf Hering * Check for an initrd 2887add60ef3SOlaf Hering */ 2888add60ef3SOlaf Hering prom_check_initrd(r3, r4); 2889add60ef3SOlaf Hering 289027f44888SBenjamin Herrenschmidt #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV) 28919b6b563cSPaul Mackerras /* 28929b6b563cSPaul Mackerras * On pSeries, inform the firmware about our capabilities 28939b6b563cSPaul Mackerras */ 28945827d416SAnton Blanchard if (of_platform == PLATFORM_PSERIES || 28955827d416SAnton Blanchard of_platform == PLATFORM_PSERIES_LPAR) 28969b6b563cSPaul Mackerras prom_send_capabilities(); 28979b6b563cSPaul Mackerras #endif 28989b6b563cSPaul Mackerras 28999b6b563cSPaul Mackerras /* 2900f3f66f59SArnd Bergmann * Copy the CPU hold code 29019b6b563cSPaul Mackerras */ 29025827d416SAnton Blanchard if (of_platform != PLATFORM_POWERMAC) 2903549e8152SPaul Mackerras copy_and_flush(0, kbase, 0x100, 0); 29049b6b563cSPaul Mackerras 29059b6b563cSPaul Mackerras /* 29069b6b563cSPaul Mackerras * Do early parsing of command line 29079b6b563cSPaul Mackerras */ 29089b6b563cSPaul Mackerras early_cmdline_parse(); 29099b6b563cSPaul Mackerras 29109b6b563cSPaul Mackerras /* 29119b6b563cSPaul Mackerras * Initialize memory management within prom_init 29129b6b563cSPaul Mackerras */ 29139b6b563cSPaul Mackerras prom_init_mem(); 29149b6b563cSPaul Mackerras 29159b6b563cSPaul Mackerras /* 29169b6b563cSPaul Mackerras * Determine which cpu is actually running right _now_ 29179b6b563cSPaul Mackerras */ 29189b6b563cSPaul Mackerras prom_find_boot_cpu(); 29199b6b563cSPaul Mackerras 29209b6b563cSPaul Mackerras /* 29219b6b563cSPaul Mackerras * Initialize display devices 29229b6b563cSPaul Mackerras */ 29239b6b563cSPaul Mackerras prom_check_displays(); 29249b6b563cSPaul Mackerras 2925493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__) 29269b6b563cSPaul Mackerras /* 29279b6b563cSPaul Mackerras * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else 29289b6b563cSPaul Mackerras * that uses the allocator, we need to make sure we get the top of memory 29299b6b563cSPaul Mackerras * available for us here... 29309b6b563cSPaul Mackerras */ 29315827d416SAnton Blanchard if (of_platform == PLATFORM_PSERIES) 29329b6b563cSPaul Mackerras prom_initialize_tce_table(); 29339b6b563cSPaul Mackerras #endif 29349b6b563cSPaul Mackerras 29359b6b563cSPaul Mackerras /* 293627f44888SBenjamin Herrenschmidt * On non-powermacs, try to instantiate RTAS. PowerMacs don't 293727f44888SBenjamin Herrenschmidt * have a usable RTAS implementation. 29389b6b563cSPaul Mackerras */ 29395827d416SAnton Blanchard if (of_platform != PLATFORM_POWERMAC && 29405827d416SAnton Blanchard of_platform != PLATFORM_OPAL) 29419b6b563cSPaul Mackerras prom_instantiate_rtas(); 294227f44888SBenjamin Herrenschmidt 294327f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV 2944493adffcSBenjamin Herrenschmidt if (of_platform == PLATFORM_OPAL) 29456e35d5daSBenjamin Herrenschmidt prom_instantiate_opal(); 2946493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV */ 294727f44888SBenjamin Herrenschmidt 29484a727429SAshley Lai #ifdef CONFIG_PPC64 29494a727429SAshley Lai /* instantiate sml */ 29504a727429SAshley Lai prom_instantiate_sml(); 29514a727429SAshley Lai #endif 29524a727429SAshley Lai 295327f44888SBenjamin Herrenschmidt /* 295427f44888SBenjamin Herrenschmidt * On non-powermacs, put all CPUs in spin-loops. 295527f44888SBenjamin Herrenschmidt * 295627f44888SBenjamin Herrenschmidt * PowerMacs use a different mechanism to spin CPUs 2957dbe78b40SBenjamin Herrenschmidt * 2958dbe78b40SBenjamin Herrenschmidt * (This must be done after instanciating RTAS) 295927f44888SBenjamin Herrenschmidt */ 29605827d416SAnton Blanchard if (of_platform != PLATFORM_POWERMAC && 29615827d416SAnton Blanchard of_platform != PLATFORM_OPAL) 296227f44888SBenjamin Herrenschmidt prom_hold_cpus(); 29639b6b563cSPaul Mackerras 29649b6b563cSPaul Mackerras /* 29659b6b563cSPaul Mackerras * Fill in some infos for use by the kernel later on 29669b6b563cSPaul Mackerras */ 2967493adffcSBenjamin Herrenschmidt if (prom_memory_limit) { 2968493adffcSBenjamin Herrenschmidt __be64 val = cpu_to_be64(prom_memory_limit); 29695827d416SAnton Blanchard prom_setprop(prom.chosen, "/chosen", "linux,memory-limit", 2970493adffcSBenjamin Herrenschmidt &val, sizeof(val)); 2971493adffcSBenjamin Herrenschmidt } 29729b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 29735827d416SAnton Blanchard if (prom_iommu_off) 29745827d416SAnton Blanchard prom_setprop(prom.chosen, "/chosen", "linux,iommu-off", 2975a23414beSPaul Mackerras NULL, 0); 29769b6b563cSPaul Mackerras 29775827d416SAnton Blanchard if (prom_iommu_force_on) 29785827d416SAnton Blanchard prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on", 2979a23414beSPaul Mackerras NULL, 0); 29809b6b563cSPaul Mackerras 29815827d416SAnton Blanchard if (prom_tce_alloc_start) { 29825827d416SAnton Blanchard prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start", 29835827d416SAnton Blanchard &prom_tce_alloc_start, 29849b6b563cSPaul Mackerras sizeof(prom_tce_alloc_start)); 29855827d416SAnton Blanchard prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end", 29865827d416SAnton Blanchard &prom_tce_alloc_end, 29879b6b563cSPaul Mackerras sizeof(prom_tce_alloc_end)); 29889b6b563cSPaul Mackerras } 29899b6b563cSPaul Mackerras #endif 29909b6b563cSPaul Mackerras 29919b6b563cSPaul Mackerras /* 29929b6b563cSPaul Mackerras * Fixup any known bugs in the device-tree 29939b6b563cSPaul Mackerras */ 29949b6b563cSPaul Mackerras fixup_device_tree(); 29959b6b563cSPaul Mackerras 29969b6b563cSPaul Mackerras /* 29979b6b563cSPaul Mackerras * Now finally create the flattened device-tree 29989b6b563cSPaul Mackerras */ 29999b6b563cSPaul Mackerras prom_printf("copying OF device tree...\n"); 30009b6b563cSPaul Mackerras flatten_device_tree(); 30019b6b563cSPaul Mackerras 30023825ac0eSPaul Mackerras /* 30033825ac0eSPaul Mackerras * in case stdin is USB and still active on IBM machines... 30043825ac0eSPaul Mackerras * Unfortunately quiesce crashes on some powermacs if we have 300540dfef66SBenjamin Herrenschmidt * closed stdin already (in particular the powerbook 101). It 300640dfef66SBenjamin Herrenschmidt * appears that the OPAL version of OFW doesn't like it either. 30073825ac0eSPaul Mackerras */ 30085827d416SAnton Blanchard if (of_platform != PLATFORM_POWERMAC && 30095827d416SAnton Blanchard of_platform != PLATFORM_OPAL) 30109b6b563cSPaul Mackerras prom_close_stdin(); 30119b6b563cSPaul Mackerras 30129b6b563cSPaul Mackerras /* 30139b6b563cSPaul Mackerras * Call OF "quiesce" method to shut down pending DMA's from 30149b6b563cSPaul Mackerras * devices etc... 30159b6b563cSPaul Mackerras */ 30167e862d7eSMichael Ellerman prom_printf("Quiescing Open Firmware ...\n"); 30179b6b563cSPaul Mackerras call_prom("quiesce", 0, 0); 30189b6b563cSPaul Mackerras 30199b6b563cSPaul Mackerras /* 30209b6b563cSPaul Mackerras * And finally, call the kernel passing it the flattened device 30219b6b563cSPaul Mackerras * tree and NULL as r5, thus triggering the new entry point which 30229b6b563cSPaul Mackerras * is common to us and kexec 30239b6b563cSPaul Mackerras */ 30245827d416SAnton Blanchard hdr = dt_header_start; 302540dfef66SBenjamin Herrenschmidt 302640dfef66SBenjamin Herrenschmidt /* Don't print anything after quiesce under OPAL, it crashes OFW */ 30275827d416SAnton Blanchard if (of_platform != PLATFORM_OPAL) { 30287d70c63cSBenjamin Herrenschmidt prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase); 30299b6b563cSPaul Mackerras prom_debug("->dt_header_start=0x%x\n", hdr); 303040dfef66SBenjamin Herrenschmidt } 30319b6b563cSPaul Mackerras 30329b6b563cSPaul Mackerras #ifdef CONFIG_PPC32 30339b6b563cSPaul Mackerras reloc_got2(-offset); 30345ac47f7aSAnton Blanchard #else 30355ac47f7aSAnton Blanchard unreloc_toc(); 30369b6b563cSPaul Mackerras #endif 30379b6b563cSPaul Mackerras 30386e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL 30396e35d5daSBenjamin Herrenschmidt /* OPAL early debug gets the OPAL base & entry in r8 and r9 */ 30406e35d5daSBenjamin Herrenschmidt __start(hdr, kbase, 0, 0, 0, 30415827d416SAnton Blanchard prom_opal_base, prom_opal_entry); 30426e35d5daSBenjamin Herrenschmidt #else 30436e35d5daSBenjamin Herrenschmidt __start(hdr, kbase, 0, 0, 0, 0, 0); 30446e35d5daSBenjamin Herrenschmidt #endif 30459b6b563cSPaul Mackerras 30469b6b563cSPaul Mackerras return 0; 30479b6b563cSPaul Mackerras } 3048