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/system.h> 399b6b563cSPaul Mackerras #include <asm/mmu.h> 409b6b563cSPaul Mackerras #include <asm/pgtable.h> 419b6b563cSPaul Mackerras #include <asm/pci.h> 429b6b563cSPaul Mackerras #include <asm/iommu.h> 439b6b563cSPaul Mackerras #include <asm/btext.h> 449b6b563cSPaul Mackerras #include <asm/sections.h> 459b6b563cSPaul Mackerras #include <asm/machdep.h> 469b6b563cSPaul Mackerras 479b6b563cSPaul Mackerras #include <linux/linux_logo.h> 489b6b563cSPaul Mackerras 499b6b563cSPaul Mackerras /* 509b6b563cSPaul Mackerras * Properties whose value is longer than this get excluded from our 519b6b563cSPaul Mackerras * copy of the device tree. This value does need to be big enough to 529b6b563cSPaul Mackerras * ensure that we don't lose things like the interrupt-map property 539b6b563cSPaul Mackerras * on a PCI-PCI bridge. 549b6b563cSPaul Mackerras */ 559b6b563cSPaul Mackerras #define MAX_PROPERTY_LENGTH (1UL * 1024 * 1024) 569b6b563cSPaul Mackerras 579b6b563cSPaul Mackerras /* 589b6b563cSPaul Mackerras * Eventually bump that one up 599b6b563cSPaul Mackerras */ 609b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE 0x100000 619b6b563cSPaul Mackerras 629b6b563cSPaul Mackerras /* 639b6b563cSPaul Mackerras * This is the size of the local memory reserve map that gets copied 649b6b563cSPaul Mackerras * into the boot params passed to the kernel. That size is totally 659b6b563cSPaul Mackerras * flexible as the kernel just reads the list until it encounters an 669b6b563cSPaul Mackerras * entry with size 0, so it can be changed without breaking binary 679b6b563cSPaul Mackerras * compatibility 689b6b563cSPaul Mackerras */ 699b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE 8 709b6b563cSPaul Mackerras 719b6b563cSPaul Mackerras /* 729b6b563cSPaul Mackerras * prom_init() is called very early on, before the kernel text 739b6b563cSPaul Mackerras * and data have been mapped to KERNELBASE. At this point the code 749b6b563cSPaul Mackerras * is running at whatever address it has been loaded at. 759b6b563cSPaul Mackerras * On ppc32 we compile with -mrelocatable, which means that references 769b6b563cSPaul Mackerras * to extern and static variables get relocated automatically. 779b6b563cSPaul Mackerras * On ppc64 we have to relocate the references explicitly with 789b6b563cSPaul Mackerras * RELOC. (Note that strings count as static variables.) 799b6b563cSPaul Mackerras * 809b6b563cSPaul Mackerras * Because OF may have mapped I/O devices into the area starting at 819b6b563cSPaul Mackerras * KERNELBASE, particularly on CHRP machines, we can't safely call 829b6b563cSPaul Mackerras * OF once the kernel has been mapped to KERNELBASE. Therefore all 839b6b563cSPaul Mackerras * OF calls must be done within prom_init(). 849b6b563cSPaul Mackerras * 859b6b563cSPaul Mackerras * ADDR is used in calls to call_prom. The 4th and following 869b6b563cSPaul Mackerras * arguments to call_prom should be 32-bit values. 879b6b563cSPaul Mackerras * On ppc64, 64 bit values are truncated to 32 bits (and 889b6b563cSPaul Mackerras * fortunately don't get interpreted as two arguments). 899b6b563cSPaul Mackerras */ 909b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 919b6b563cSPaul Mackerras #define RELOC(x) (*PTRRELOC(&(x))) 929b6b563cSPaul Mackerras #define ADDR(x) (u32) add_reloc_offset((unsigned long)(x)) 93a23414beSPaul Mackerras #define OF_WORKAROUNDS 0 949b6b563cSPaul Mackerras #else 959b6b563cSPaul Mackerras #define RELOC(x) (x) 969b6b563cSPaul Mackerras #define ADDR(x) (u32) (x) 97a23414beSPaul Mackerras #define OF_WORKAROUNDS of_workarounds 98a23414beSPaul Mackerras int of_workarounds; 999b6b563cSPaul Mackerras #endif 1009b6b563cSPaul Mackerras 101a23414beSPaul Mackerras #define OF_WA_CLAIM 1 /* do phys/virt claim separately, then map */ 102a23414beSPaul Mackerras #define OF_WA_LONGTRAIL 2 /* work around longtrail bugs */ 103a23414beSPaul Mackerras 1049b6b563cSPaul Mackerras #define PROM_BUG() do { \ 1059b6b563cSPaul Mackerras prom_printf("kernel BUG at %s line 0x%x!\n", \ 1069b6b563cSPaul Mackerras RELOC(__FILE__), __LINE__); \ 1079b6b563cSPaul Mackerras __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR); \ 1089b6b563cSPaul Mackerras } while (0) 1099b6b563cSPaul Mackerras 1109b6b563cSPaul Mackerras #ifdef DEBUG_PROM 1119b6b563cSPaul Mackerras #define prom_debug(x...) prom_printf(x) 1129b6b563cSPaul Mackerras #else 1139b6b563cSPaul Mackerras #define prom_debug(x...) 1149b6b563cSPaul Mackerras #endif 1159b6b563cSPaul Mackerras 1169b6b563cSPaul Mackerras 1179b6b563cSPaul Mackerras typedef u32 prom_arg_t; 1189b6b563cSPaul Mackerras 1199b6b563cSPaul Mackerras struct prom_args { 1209b6b563cSPaul Mackerras u32 service; 1219b6b563cSPaul Mackerras u32 nargs; 1229b6b563cSPaul Mackerras u32 nret; 1239b6b563cSPaul Mackerras prom_arg_t args[10]; 1249b6b563cSPaul Mackerras }; 1259b6b563cSPaul Mackerras 1269b6b563cSPaul Mackerras struct prom_t { 1279b6b563cSPaul Mackerras ihandle root; 128a23414beSPaul Mackerras phandle chosen; 1299b6b563cSPaul Mackerras int cpu; 1309b6b563cSPaul Mackerras ihandle stdout; 131a575b807SPaul Mackerras ihandle mmumap; 132a23414beSPaul Mackerras ihandle memory; 1339b6b563cSPaul Mackerras }; 1349b6b563cSPaul Mackerras 1359b6b563cSPaul Mackerras struct mem_map_entry { 136cbbcf340SKumar Gala u64 base; 137cbbcf340SKumar Gala u64 size; 1389b6b563cSPaul Mackerras }; 1399b6b563cSPaul Mackerras 1409b6b563cSPaul Mackerras typedef u32 cell_t; 1419b6b563cSPaul Mackerras 1429b6b563cSPaul Mackerras extern void __start(unsigned long r3, unsigned long r4, unsigned long r5); 1439b6b563cSPaul Mackerras 1449b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 145c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry); 1469b6b563cSPaul Mackerras #else 147c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry) 1489b6b563cSPaul Mackerras { 149c4988820SPaul Mackerras return ((int (*)(struct prom_args *))entry)(args); 1509b6b563cSPaul Mackerras } 1519b6b563cSPaul Mackerras #endif 1529b6b563cSPaul Mackerras 1539b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src, 1549b6b563cSPaul Mackerras unsigned long size, unsigned long offset); 1559b6b563cSPaul Mackerras 1569b6b563cSPaul Mackerras /* prom structure */ 1579b6b563cSPaul Mackerras static struct prom_t __initdata prom; 1589b6b563cSPaul Mackerras 1599b6b563cSPaul Mackerras static unsigned long prom_entry __initdata; 1609b6b563cSPaul Mackerras 1619b6b563cSPaul Mackerras #define PROM_SCRATCH_SIZE 256 1629b6b563cSPaul Mackerras 1639b6b563cSPaul Mackerras static char __initdata of_stdout_device[256]; 1649b6b563cSPaul Mackerras static char __initdata prom_scratch[PROM_SCRATCH_SIZE]; 1659b6b563cSPaul Mackerras 1669b6b563cSPaul Mackerras static unsigned long __initdata dt_header_start; 1679b6b563cSPaul Mackerras static unsigned long __initdata dt_struct_start, dt_struct_end; 1689b6b563cSPaul Mackerras static unsigned long __initdata dt_string_start, dt_string_end; 1699b6b563cSPaul Mackerras 1709b6b563cSPaul Mackerras static unsigned long __initdata prom_initrd_start, prom_initrd_end; 1719b6b563cSPaul Mackerras 1729b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 173165785e5SJeremy Kerr static int __initdata prom_iommu_force_on; 174165785e5SJeremy Kerr static int __initdata prom_iommu_off; 1759b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_start; 1769b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_end; 1779b6b563cSPaul Mackerras #endif 1789b6b563cSPaul Mackerras 179e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this 180e8222502SBenjamin Herrenschmidt * file and ultimately gone too. Feel free to change them if you need, they 181e8222502SBenjamin Herrenschmidt * are not shared with anything outside of this file anymore 182e8222502SBenjamin Herrenschmidt */ 183e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES 0x0100 184e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR 0x0101 185e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR 0x0001 186e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC 0x0400 187e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC 0x0500 188e8222502SBenjamin Herrenschmidt 1899b6b563cSPaul Mackerras static int __initdata of_platform; 1909b6b563cSPaul Mackerras 1919b6b563cSPaul Mackerras static char __initdata prom_cmd_line[COMMAND_LINE_SIZE]; 1929b6b563cSPaul Mackerras 193cf68787bSBenjamin Krill static unsigned long __initdata prom_memory_limit; 194cf68787bSBenjamin Krill 1959b6b563cSPaul Mackerras static unsigned long __initdata alloc_top; 1969b6b563cSPaul Mackerras static unsigned long __initdata alloc_top_high; 1979b6b563cSPaul Mackerras static unsigned long __initdata alloc_bottom; 1989b6b563cSPaul Mackerras static unsigned long __initdata rmo_top; 1999b6b563cSPaul Mackerras static unsigned long __initdata ram_top; 2009b6b563cSPaul Mackerras 2019b6b563cSPaul Mackerras static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE]; 2029b6b563cSPaul Mackerras static int __initdata mem_reserve_cnt; 2039b6b563cSPaul Mackerras 2049b6b563cSPaul Mackerras static cell_t __initdata regbuf[1024]; 2059b6b563cSPaul Mackerras 2069b6b563cSPaul Mackerras 2079b6b563cSPaul Mackerras /* 2089b6b563cSPaul Mackerras * Error results ... some OF calls will return "-1" on error, some 2099b6b563cSPaul Mackerras * will return 0, some will return either. To simplify, here are 2109b6b563cSPaul Mackerras * macros to use with any ihandle or phandle return value to check if 2119b6b563cSPaul Mackerras * it is valid 2129b6b563cSPaul Mackerras */ 2139b6b563cSPaul Mackerras 2149b6b563cSPaul Mackerras #define PROM_ERROR (-1u) 2159b6b563cSPaul Mackerras #define PHANDLE_VALID(p) ((p) != 0 && (p) != PROM_ERROR) 2169b6b563cSPaul Mackerras #define IHANDLE_VALID(i) ((i) != 0 && (i) != PROM_ERROR) 2179b6b563cSPaul Mackerras 2189b6b563cSPaul Mackerras 2199b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open 2209b6b563cSPaul Mackerras * firmware. 2219b6b563cSPaul Mackerras */ 2229b6b563cSPaul Mackerras 2239b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...) 2249b6b563cSPaul Mackerras { 2259b6b563cSPaul Mackerras int i; 2269b6b563cSPaul Mackerras struct prom_args args; 2279b6b563cSPaul Mackerras va_list list; 2289b6b563cSPaul Mackerras 2299b6b563cSPaul Mackerras args.service = ADDR(service); 2309b6b563cSPaul Mackerras args.nargs = nargs; 2319b6b563cSPaul Mackerras args.nret = nret; 2329b6b563cSPaul Mackerras 2339b6b563cSPaul Mackerras va_start(list, nret); 2349b6b563cSPaul Mackerras for (i = 0; i < nargs; i++) 2359b6b563cSPaul Mackerras args.args[i] = va_arg(list, prom_arg_t); 2369b6b563cSPaul Mackerras va_end(list); 2379b6b563cSPaul Mackerras 2389b6b563cSPaul Mackerras for (i = 0; i < nret; i++) 2399b6b563cSPaul Mackerras args.args[nargs+i] = 0; 2409b6b563cSPaul Mackerras 241c4988820SPaul Mackerras if (enter_prom(&args, RELOC(prom_entry)) < 0) 242c4988820SPaul Mackerras return PROM_ERROR; 2439b6b563cSPaul Mackerras 2449b6b563cSPaul Mackerras return (nret > 0) ? args.args[nargs] : 0; 2459b6b563cSPaul Mackerras } 2469b6b563cSPaul Mackerras 2479b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret, 2489b6b563cSPaul Mackerras prom_arg_t *rets, ...) 2499b6b563cSPaul Mackerras { 2509b6b563cSPaul Mackerras int i; 2519b6b563cSPaul Mackerras struct prom_args args; 2529b6b563cSPaul Mackerras va_list list; 2539b6b563cSPaul Mackerras 2549b6b563cSPaul Mackerras args.service = ADDR(service); 2559b6b563cSPaul Mackerras args.nargs = nargs; 2569b6b563cSPaul Mackerras args.nret = nret; 2579b6b563cSPaul Mackerras 2589b6b563cSPaul Mackerras va_start(list, rets); 2599b6b563cSPaul Mackerras for (i = 0; i < nargs; i++) 2609b6b563cSPaul Mackerras args.args[i] = va_arg(list, prom_arg_t); 2619b6b563cSPaul Mackerras va_end(list); 2629b6b563cSPaul Mackerras 2639b6b563cSPaul Mackerras for (i = 0; i < nret; i++) 264ed1189b7SOlaf Hering args.args[nargs+i] = 0; 2659b6b563cSPaul Mackerras 266c4988820SPaul Mackerras if (enter_prom(&args, RELOC(prom_entry)) < 0) 267c4988820SPaul Mackerras return PROM_ERROR; 2689b6b563cSPaul Mackerras 2699b6b563cSPaul Mackerras if (rets != NULL) 2709b6b563cSPaul Mackerras for (i = 1; i < nret; ++i) 271c5200c90SPaul Mackerras rets[i-1] = args.args[nargs+i]; 2729b6b563cSPaul Mackerras 2739b6b563cSPaul Mackerras return (nret > 0) ? args.args[nargs] : 0; 2749b6b563cSPaul Mackerras } 2759b6b563cSPaul Mackerras 2769b6b563cSPaul Mackerras 2779b6b563cSPaul Mackerras static void __init prom_print(const char *msg) 2789b6b563cSPaul Mackerras { 2799b6b563cSPaul Mackerras const char *p, *q; 2809b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 2819b6b563cSPaul Mackerras 2829b6b563cSPaul Mackerras if (_prom->stdout == 0) 2839b6b563cSPaul Mackerras return; 2849b6b563cSPaul Mackerras 2859b6b563cSPaul Mackerras for (p = msg; *p != 0; p = q) { 2869b6b563cSPaul Mackerras for (q = p; *q != 0 && *q != '\n'; ++q) 2879b6b563cSPaul Mackerras ; 2889b6b563cSPaul Mackerras if (q > p) 2899b6b563cSPaul Mackerras call_prom("write", 3, 1, _prom->stdout, p, q - p); 2909b6b563cSPaul Mackerras if (*q == 0) 2919b6b563cSPaul Mackerras break; 2929b6b563cSPaul Mackerras ++q; 2939b6b563cSPaul Mackerras call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2); 2949b6b563cSPaul Mackerras } 2959b6b563cSPaul Mackerras } 2969b6b563cSPaul Mackerras 2979b6b563cSPaul Mackerras 2989b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val) 2999b6b563cSPaul Mackerras { 3009b6b563cSPaul Mackerras int i, nibbles = sizeof(val)*2; 3019b6b563cSPaul Mackerras char buf[sizeof(val)*2+1]; 3029b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 3039b6b563cSPaul Mackerras 3049b6b563cSPaul Mackerras for (i = nibbles-1; i >= 0; i--) { 3059b6b563cSPaul Mackerras buf[i] = (val & 0xf) + '0'; 3069b6b563cSPaul Mackerras if (buf[i] > '9') 3079b6b563cSPaul Mackerras buf[i] += ('a'-'0'-10); 3089b6b563cSPaul Mackerras val >>= 4; 3099b6b563cSPaul Mackerras } 3109b6b563cSPaul Mackerras buf[nibbles] = '\0'; 3119b6b563cSPaul Mackerras call_prom("write", 3, 1, _prom->stdout, buf, nibbles); 3129b6b563cSPaul Mackerras } 3139b6b563cSPaul Mackerras 3142c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */ 3152c48a7d6SMichael Neuling #define UL_DIGITS 21 3162c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val) 3172c48a7d6SMichael Neuling { 3182c48a7d6SMichael Neuling int i, size; 3192c48a7d6SMichael Neuling char buf[UL_DIGITS+1]; 3202c48a7d6SMichael Neuling struct prom_t *_prom = &RELOC(prom); 3212c48a7d6SMichael Neuling 3222c48a7d6SMichael Neuling for (i = UL_DIGITS-1; i >= 0; i--) { 3232c48a7d6SMichael Neuling buf[i] = (val % 10) + '0'; 3242c48a7d6SMichael Neuling val = val/10; 3252c48a7d6SMichael Neuling if (val == 0) 3262c48a7d6SMichael Neuling break; 3272c48a7d6SMichael Neuling } 3282c48a7d6SMichael Neuling /* shift stuff down */ 3292c48a7d6SMichael Neuling size = UL_DIGITS - i; 3302c48a7d6SMichael Neuling call_prom("write", 3, 1, _prom->stdout, buf+i, size); 3312c48a7d6SMichael Neuling } 3329b6b563cSPaul Mackerras 3339b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...) 3349b6b563cSPaul Mackerras { 3359b6b563cSPaul Mackerras const char *p, *q, *s; 3369b6b563cSPaul Mackerras va_list args; 3379b6b563cSPaul Mackerras unsigned long v; 338af277149SBenjamin Herrenschmidt long vs; 3399b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 3409b6b563cSPaul Mackerras 3419b6b563cSPaul Mackerras va_start(args, format); 3429b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 3439b6b563cSPaul Mackerras format = PTRRELOC(format); 3449b6b563cSPaul Mackerras #endif 3459b6b563cSPaul Mackerras for (p = format; *p != 0; p = q) { 3469b6b563cSPaul Mackerras for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q) 3479b6b563cSPaul Mackerras ; 3489b6b563cSPaul Mackerras if (q > p) 3499b6b563cSPaul Mackerras call_prom("write", 3, 1, _prom->stdout, p, q - p); 3509b6b563cSPaul Mackerras if (*q == 0) 3519b6b563cSPaul Mackerras break; 3529b6b563cSPaul Mackerras if (*q == '\n') { 3539b6b563cSPaul Mackerras ++q; 3549b6b563cSPaul Mackerras call_prom("write", 3, 1, _prom->stdout, 3559b6b563cSPaul Mackerras ADDR("\r\n"), 2); 3569b6b563cSPaul Mackerras continue; 3579b6b563cSPaul Mackerras } 3589b6b563cSPaul Mackerras ++q; 3599b6b563cSPaul Mackerras if (*q == 0) 3609b6b563cSPaul Mackerras break; 3619b6b563cSPaul Mackerras switch (*q) { 3629b6b563cSPaul Mackerras case 's': 3639b6b563cSPaul Mackerras ++q; 3649b6b563cSPaul Mackerras s = va_arg(args, const char *); 3659b6b563cSPaul Mackerras prom_print(s); 3669b6b563cSPaul Mackerras break; 3679b6b563cSPaul Mackerras case 'x': 3689b6b563cSPaul Mackerras ++q; 3699b6b563cSPaul Mackerras v = va_arg(args, unsigned long); 3709b6b563cSPaul Mackerras prom_print_hex(v); 3719b6b563cSPaul Mackerras break; 372af277149SBenjamin Herrenschmidt case 'd': 373af277149SBenjamin Herrenschmidt ++q; 374af277149SBenjamin Herrenschmidt vs = va_arg(args, int); 375af277149SBenjamin Herrenschmidt if (vs < 0) { 376af277149SBenjamin Herrenschmidt prom_print(RELOC("-")); 377af277149SBenjamin Herrenschmidt vs = -vs; 378af277149SBenjamin Herrenschmidt } 379af277149SBenjamin Herrenschmidt prom_print_dec(vs); 380af277149SBenjamin Herrenschmidt break; 3812c48a7d6SMichael Neuling case 'l': 3822c48a7d6SMichael Neuling ++q; 383af277149SBenjamin Herrenschmidt if (*q == 0) 384af277149SBenjamin Herrenschmidt break; 385af277149SBenjamin Herrenschmidt else if (*q == 'x') { 386af277149SBenjamin Herrenschmidt ++q; 387af277149SBenjamin Herrenschmidt v = va_arg(args, unsigned long); 388af277149SBenjamin Herrenschmidt prom_print_hex(v); 389af277149SBenjamin Herrenschmidt } else if (*q == 'u') { /* '%lu' */ 3902c48a7d6SMichael Neuling ++q; 3912c48a7d6SMichael Neuling v = va_arg(args, unsigned long); 3922c48a7d6SMichael Neuling prom_print_dec(v); 393af277149SBenjamin Herrenschmidt } else if (*q == 'd') { /* %ld */ 394af277149SBenjamin Herrenschmidt ++q; 395af277149SBenjamin Herrenschmidt vs = va_arg(args, long); 396af277149SBenjamin Herrenschmidt if (vs < 0) { 397af277149SBenjamin Herrenschmidt prom_print(RELOC("-")); 398af277149SBenjamin Herrenschmidt vs = -vs; 399af277149SBenjamin Herrenschmidt } 400af277149SBenjamin Herrenschmidt prom_print_dec(vs); 4012c48a7d6SMichael Neuling } 4022c48a7d6SMichael Neuling break; 4039b6b563cSPaul Mackerras } 4049b6b563cSPaul Mackerras } 4059b6b563cSPaul Mackerras } 4069b6b563cSPaul Mackerras 4079b6b563cSPaul Mackerras 408a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size, 409a575b807SPaul Mackerras unsigned long align) 410a575b807SPaul Mackerras { 411a575b807SPaul Mackerras struct prom_t *_prom = &RELOC(prom); 412a575b807SPaul Mackerras 413a23414beSPaul Mackerras if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) { 414a23414beSPaul Mackerras /* 415a23414beSPaul Mackerras * Old OF requires we claim physical and virtual separately 416a23414beSPaul Mackerras * and then map explicitly (assuming virtual mode) 417a23414beSPaul Mackerras */ 418a23414beSPaul Mackerras int ret; 419a23414beSPaul Mackerras prom_arg_t result; 420a23414beSPaul Mackerras 421a23414beSPaul Mackerras ret = call_prom_ret("call-method", 5, 2, &result, 422a23414beSPaul Mackerras ADDR("claim"), _prom->memory, 423a23414beSPaul Mackerras align, size, virt); 424a23414beSPaul Mackerras if (ret != 0 || result == -1) 425a23414beSPaul Mackerras return -1; 426a23414beSPaul Mackerras ret = call_prom_ret("call-method", 5, 2, &result, 427a23414beSPaul Mackerras ADDR("claim"), _prom->mmumap, 428a23414beSPaul Mackerras align, size, virt); 429a23414beSPaul Mackerras if (ret != 0) { 430a23414beSPaul Mackerras call_prom("call-method", 4, 1, ADDR("release"), 431a23414beSPaul Mackerras _prom->memory, size, virt); 432a23414beSPaul Mackerras return -1; 433a23414beSPaul Mackerras } 434a23414beSPaul Mackerras /* the 0x12 is M (coherence) + PP == read/write */ 435a575b807SPaul Mackerras call_prom("call-method", 6, 1, 436a23414beSPaul Mackerras ADDR("map"), _prom->mmumap, 0x12, size, virt, virt); 437a23414beSPaul Mackerras return virt; 438a23414beSPaul Mackerras } 439a23414beSPaul Mackerras return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size, 440a23414beSPaul Mackerras (prom_arg_t)align); 441a575b807SPaul Mackerras } 442a575b807SPaul Mackerras 4439b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason) 4449b6b563cSPaul Mackerras { 4459b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 4469b6b563cSPaul Mackerras reason = PTRRELOC(reason); 4479b6b563cSPaul Mackerras #endif 4489b6b563cSPaul Mackerras prom_print(reason); 449add60ef3SOlaf Hering /* Do not call exit because it clears the screen on pmac 450add60ef3SOlaf Hering * it also causes some sort of double-fault on early pmacs */ 451add60ef3SOlaf Hering if (RELOC(of_platform) == PLATFORM_POWERMAC) 452add60ef3SOlaf Hering asm("trap\n"); 453add60ef3SOlaf Hering 4549b6b563cSPaul Mackerras /* ToDo: should put up an SRC here on p/iSeries */ 4559b6b563cSPaul Mackerras call_prom("exit", 0, 0); 4569b6b563cSPaul Mackerras 4579b6b563cSPaul Mackerras for (;;) /* should never get here */ 4589b6b563cSPaul Mackerras ; 4599b6b563cSPaul Mackerras } 4609b6b563cSPaul Mackerras 4619b6b563cSPaul Mackerras 4629b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep) 4639b6b563cSPaul Mackerras { 4649b6b563cSPaul Mackerras phandle node; 4659b6b563cSPaul Mackerras 4669b6b563cSPaul Mackerras if ((node = *nodep) != 0 4679b6b563cSPaul Mackerras && (*nodep = call_prom("child", 1, 1, node)) != 0) 4689b6b563cSPaul Mackerras return 1; 4699b6b563cSPaul Mackerras if ((*nodep = call_prom("peer", 1, 1, node)) != 0) 4709b6b563cSPaul Mackerras return 1; 4719b6b563cSPaul Mackerras for (;;) { 4729b6b563cSPaul Mackerras if ((node = call_prom("parent", 1, 1, node)) == 0) 4739b6b563cSPaul Mackerras return 0; 4749b6b563cSPaul Mackerras if ((*nodep = call_prom("peer", 1, 1, node)) != 0) 4759b6b563cSPaul Mackerras return 1; 4769b6b563cSPaul Mackerras } 4779b6b563cSPaul Mackerras } 4789b6b563cSPaul Mackerras 47921fe3301SBenjamin Herrenschmidt static int inline prom_getprop(phandle node, const char *pname, 4809b6b563cSPaul Mackerras void *value, size_t valuelen) 4819b6b563cSPaul Mackerras { 4829b6b563cSPaul Mackerras return call_prom("getprop", 4, 1, node, ADDR(pname), 4839b6b563cSPaul Mackerras (u32)(unsigned long) value, (u32) valuelen); 4849b6b563cSPaul Mackerras } 4859b6b563cSPaul Mackerras 48621fe3301SBenjamin Herrenschmidt static int inline prom_getproplen(phandle node, const char *pname) 4879b6b563cSPaul Mackerras { 4889b6b563cSPaul Mackerras return call_prom("getproplen", 2, 1, node, ADDR(pname)); 4899b6b563cSPaul Mackerras } 4909b6b563cSPaul Mackerras 491a23414beSPaul Mackerras static void add_string(char **str, const char *q) 4929b6b563cSPaul Mackerras { 493a23414beSPaul Mackerras char *p = *str; 494a23414beSPaul Mackerras 495a23414beSPaul Mackerras while (*q) 496a23414beSPaul Mackerras *p++ = *q++; 497a23414beSPaul Mackerras *p++ = ' '; 498a23414beSPaul Mackerras *str = p; 499a23414beSPaul Mackerras } 500a23414beSPaul Mackerras 501a23414beSPaul Mackerras static char *tohex(unsigned int x) 502a23414beSPaul Mackerras { 503a23414beSPaul Mackerras static char digits[] = "0123456789abcdef"; 504a23414beSPaul Mackerras static char result[9]; 505a23414beSPaul Mackerras int i; 506a23414beSPaul Mackerras 507a23414beSPaul Mackerras result[8] = 0; 508a23414beSPaul Mackerras i = 8; 509a23414beSPaul Mackerras do { 510a23414beSPaul Mackerras --i; 511a23414beSPaul Mackerras result[i] = digits[x & 0xf]; 512a23414beSPaul Mackerras x >>= 4; 513a23414beSPaul Mackerras } while (x != 0 && i > 0); 514a23414beSPaul Mackerras return &result[i]; 515a23414beSPaul Mackerras } 516a23414beSPaul Mackerras 517a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename, 518a23414beSPaul Mackerras const char *pname, void *value, size_t valuelen) 519a23414beSPaul Mackerras { 520a23414beSPaul Mackerras char cmd[256], *p; 521a23414beSPaul Mackerras 522a23414beSPaul Mackerras if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL)) 5239b6b563cSPaul Mackerras return call_prom("setprop", 4, 1, node, ADDR(pname), 5249b6b563cSPaul Mackerras (u32)(unsigned long) value, (u32) valuelen); 525a23414beSPaul Mackerras 526a23414beSPaul Mackerras /* gah... setprop doesn't work on longtrail, have to use interpret */ 527a23414beSPaul Mackerras p = cmd; 528a23414beSPaul Mackerras add_string(&p, "dev"); 529a23414beSPaul Mackerras add_string(&p, nodename); 530a23414beSPaul Mackerras add_string(&p, tohex((u32)(unsigned long) value)); 531a23414beSPaul Mackerras add_string(&p, tohex(valuelen)); 532a23414beSPaul Mackerras add_string(&p, tohex(ADDR(pname))); 533a23414beSPaul Mackerras add_string(&p, tohex(strlen(RELOC(pname)))); 534a23414beSPaul Mackerras add_string(&p, "property"); 535a23414beSPaul Mackerras *p = 0; 536a23414beSPaul Mackerras return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd); 5379b6b563cSPaul Mackerras } 5389b6b563cSPaul Mackerras 539cf68787bSBenjamin Krill /* We can't use the standard versions because of RELOC headaches. */ 540cf68787bSBenjamin Krill #define isxdigit(c) (('0' <= (c) && (c) <= '9') \ 541cf68787bSBenjamin Krill || ('a' <= (c) && (c) <= 'f') \ 542cf68787bSBenjamin Krill || ('A' <= (c) && (c) <= 'F')) 543cf68787bSBenjamin Krill 544cf68787bSBenjamin Krill #define isdigit(c) ('0' <= (c) && (c) <= '9') 545cf68787bSBenjamin Krill #define islower(c) ('a' <= (c) && (c) <= 'z') 546cf68787bSBenjamin Krill #define toupper(c) (islower(c) ? ((c) - 'a' + 'A') : (c)) 547cf68787bSBenjamin Krill 548cf68787bSBenjamin Krill unsigned long prom_strtoul(const char *cp, const char **endp) 549cf68787bSBenjamin Krill { 550cf68787bSBenjamin Krill unsigned long result = 0, base = 10, value; 551cf68787bSBenjamin Krill 552cf68787bSBenjamin Krill if (*cp == '0') { 553cf68787bSBenjamin Krill base = 8; 554cf68787bSBenjamin Krill cp++; 555cf68787bSBenjamin Krill if (toupper(*cp) == 'X') { 556cf68787bSBenjamin Krill cp++; 557cf68787bSBenjamin Krill base = 16; 558cf68787bSBenjamin Krill } 559cf68787bSBenjamin Krill } 560cf68787bSBenjamin Krill 561cf68787bSBenjamin Krill while (isxdigit(*cp) && 562cf68787bSBenjamin Krill (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) { 563cf68787bSBenjamin Krill result = result * base + value; 564cf68787bSBenjamin Krill cp++; 565cf68787bSBenjamin Krill } 566cf68787bSBenjamin Krill 567cf68787bSBenjamin Krill if (endp) 568cf68787bSBenjamin Krill *endp = cp; 569cf68787bSBenjamin Krill 570cf68787bSBenjamin Krill return result; 571cf68787bSBenjamin Krill } 572cf68787bSBenjamin Krill 573cf68787bSBenjamin Krill unsigned long prom_memparse(const char *ptr, const char **retptr) 574cf68787bSBenjamin Krill { 575cf68787bSBenjamin Krill unsigned long ret = prom_strtoul(ptr, retptr); 576cf68787bSBenjamin Krill int shift = 0; 577cf68787bSBenjamin Krill 578cf68787bSBenjamin Krill /* 579cf68787bSBenjamin Krill * We can't use a switch here because GCC *may* generate a 580cf68787bSBenjamin Krill * jump table which won't work, because we're not running at 581cf68787bSBenjamin Krill * the address we're linked at. 582cf68787bSBenjamin Krill */ 583cf68787bSBenjamin Krill if ('G' == **retptr || 'g' == **retptr) 584cf68787bSBenjamin Krill shift = 30; 585cf68787bSBenjamin Krill 586cf68787bSBenjamin Krill if ('M' == **retptr || 'm' == **retptr) 587cf68787bSBenjamin Krill shift = 20; 588cf68787bSBenjamin Krill 589cf68787bSBenjamin Krill if ('K' == **retptr || 'k' == **retptr) 590cf68787bSBenjamin Krill shift = 10; 591cf68787bSBenjamin Krill 592cf68787bSBenjamin Krill if (shift) { 593cf68787bSBenjamin Krill ret <<= shift; 594cf68787bSBenjamin Krill (*retptr)++; 595cf68787bSBenjamin Krill } 596cf68787bSBenjamin Krill 597cf68787bSBenjamin Krill return ret; 598cf68787bSBenjamin Krill } 599cf68787bSBenjamin Krill 6009b6b563cSPaul Mackerras /* 6019b6b563cSPaul Mackerras * Early parsing of the command line passed to the kernel, used for 6029b6b563cSPaul Mackerras * "mem=x" and the options that affect the iommu 6039b6b563cSPaul Mackerras */ 6049b6b563cSPaul Mackerras static void __init early_cmdline_parse(void) 6059b6b563cSPaul Mackerras { 6069b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 607cc5d0189SBenjamin Herrenschmidt const char *opt; 608cf68787bSBenjamin Krill 609cc5d0189SBenjamin Herrenschmidt char *p; 6109b6b563cSPaul Mackerras int l = 0; 6119b6b563cSPaul Mackerras 6129b6b563cSPaul Mackerras RELOC(prom_cmd_line[0]) = 0; 6139b6b563cSPaul Mackerras p = RELOC(prom_cmd_line); 6149b6b563cSPaul Mackerras if ((long)_prom->chosen > 0) 6159b6b563cSPaul Mackerras l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1); 6169b6b563cSPaul Mackerras #ifdef CONFIG_CMDLINE 6170e4aa9c2SAmos Waterland if (l <= 0 || p[0] == '\0') /* dbl check */ 6189b6b563cSPaul Mackerras strlcpy(RELOC(prom_cmd_line), 6199b6b563cSPaul Mackerras RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line)); 6209b6b563cSPaul Mackerras #endif /* CONFIG_CMDLINE */ 6219b6b563cSPaul Mackerras prom_printf("command line: %s\n", RELOC(prom_cmd_line)); 6229b6b563cSPaul Mackerras 6239b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 6249b6b563cSPaul Mackerras opt = strstr(RELOC(prom_cmd_line), RELOC("iommu=")); 6259b6b563cSPaul Mackerras if (opt) { 6269b6b563cSPaul Mackerras prom_printf("iommu opt is: %s\n", opt); 6279b6b563cSPaul Mackerras opt += 6; 6289b6b563cSPaul Mackerras while (*opt && *opt == ' ') 6299b6b563cSPaul Mackerras opt++; 6309b6b563cSPaul Mackerras if (!strncmp(opt, RELOC("off"), 3)) 631165785e5SJeremy Kerr RELOC(prom_iommu_off) = 1; 6329b6b563cSPaul Mackerras else if (!strncmp(opt, RELOC("force"), 5)) 633165785e5SJeremy Kerr RELOC(prom_iommu_force_on) = 1; 6349b6b563cSPaul Mackerras } 6359b6b563cSPaul Mackerras #endif 636cf68787bSBenjamin Krill opt = strstr(RELOC(prom_cmd_line), RELOC("mem=")); 637cf68787bSBenjamin Krill if (opt) { 638cf68787bSBenjamin Krill opt += 4; 639cf68787bSBenjamin Krill RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt); 640cf68787bSBenjamin Krill #ifdef CONFIG_PPC64 641cf68787bSBenjamin Krill /* Align to 16 MB == size of ppc64 large page */ 642cf68787bSBenjamin Krill RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000); 643cf68787bSBenjamin Krill #endif 644cf68787bSBenjamin Krill } 6459b6b563cSPaul Mackerras } 6469b6b563cSPaul Mackerras 6479b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 6489b6b563cSPaul Mackerras /* 649f709bfacSPaul Mackerras * There are two methods for telling firmware what our capabilities are. 650f709bfacSPaul Mackerras * Newer machines have an "ibm,client-architecture-support" method on the 651f709bfacSPaul Mackerras * root node. For older machines, we have to call the "process-elf-header" 652f709bfacSPaul Mackerras * method in the /packages/elf-loader node, passing it a fake 32-bit 653f709bfacSPaul Mackerras * ELF header containing a couple of PT_NOTE sections that contain 654f709bfacSPaul Mackerras * structures that contain various information. 6559b6b563cSPaul Mackerras */ 656f709bfacSPaul Mackerras 657f709bfacSPaul Mackerras /* 658f709bfacSPaul Mackerras * New method - extensible architecture description vector. 659f709bfacSPaul Mackerras * 660f709bfacSPaul Mackerras * Because the description vector contains a mix of byte and word 661f709bfacSPaul Mackerras * values, we declare it as an unsigned char array, and use this 662f709bfacSPaul Mackerras * macro to put word values in. 663f709bfacSPaul Mackerras */ 664f709bfacSPaul Mackerras #define W(x) ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \ 665f709bfacSPaul Mackerras ((x) >> 8) & 0xff, (x) & 0xff 666f709bfacSPaul Mackerras 667f709bfacSPaul Mackerras /* Option vector bits - generic bits in byte 1 */ 668f709bfacSPaul Mackerras #define OV_IGNORE 0x80 /* ignore this vector */ 669f709bfacSPaul Mackerras #define OV_CESSATION_POLICY 0x40 /* halt if unsupported option present*/ 670f709bfacSPaul Mackerras 671f709bfacSPaul Mackerras /* Option vector 1: processor architectures supported */ 672f709bfacSPaul Mackerras #define OV1_PPC_2_00 0x80 /* set if we support PowerPC 2.00 */ 673f709bfacSPaul Mackerras #define OV1_PPC_2_01 0x40 /* set if we support PowerPC 2.01 */ 674f709bfacSPaul Mackerras #define OV1_PPC_2_02 0x20 /* set if we support PowerPC 2.02 */ 675f709bfacSPaul Mackerras #define OV1_PPC_2_03 0x10 /* set if we support PowerPC 2.03 */ 676f709bfacSPaul Mackerras #define OV1_PPC_2_04 0x08 /* set if we support PowerPC 2.04 */ 677f709bfacSPaul Mackerras #define OV1_PPC_2_05 0x04 /* set if we support PowerPC 2.05 */ 6780cb99013SJoel Schopp #define OV1_PPC_2_06 0x02 /* set if we support PowerPC 2.06 */ 679f709bfacSPaul Mackerras 680f709bfacSPaul Mackerras /* Option vector 2: Open Firmware options supported */ 681f709bfacSPaul Mackerras #define OV2_REAL_MODE 0x20 /* set if we want OF in real mode */ 682f709bfacSPaul Mackerras 683f709bfacSPaul Mackerras /* Option vector 3: processor options supported */ 684f709bfacSPaul Mackerras #define OV3_FP 0x80 /* floating point */ 685f709bfacSPaul Mackerras #define OV3_VMX 0x40 /* VMX/Altivec */ 686974a76f5SPaul Mackerras #define OV3_DFP 0x20 /* decimal FP */ 687f709bfacSPaul Mackerras 688f709bfacSPaul Mackerras /* Option vector 5: PAPR/OF options supported */ 689f709bfacSPaul Mackerras #define OV5_LPAR 0x80 /* logical partitioning supported */ 690f709bfacSPaul Mackerras #define OV5_SPLPAR 0x40 /* shared-processor LPAR supported */ 691f709bfacSPaul Mackerras /* ibm,dynamic-reconfiguration-memory property supported */ 692f709bfacSPaul Mackerras #define OV5_DRCONF_MEMORY 0x20 693f709bfacSPaul Mackerras #define OV5_LARGE_PAGES 0x10 /* large pages supported */ 694d8c391a5SJake Moilanen #define OV5_DONATE_DEDICATE_CPU 0x02 /* donate dedicated CPU support */ 695014dad90SMichael Ellerman /* PCIe/MSI support. Without MSI full PCIe is not supported */ 696014dad90SMichael Ellerman #ifdef CONFIG_PCI_MSI 697014dad90SMichael Ellerman #define OV5_MSI 0x01 /* PCIe/MSI support */ 698014dad90SMichael Ellerman #else 699014dad90SMichael Ellerman #define OV5_MSI 0x00 700014dad90SMichael Ellerman #endif /* CONFIG_PCI_MSI */ 7018391e42aSNathan Fontenot #ifdef CONFIG_PPC_SMLPAR 7028391e42aSNathan Fontenot #define OV5_CMO 0x80 /* Cooperative Memory Overcommitment */ 7039ee820faSBrian King #define OV5_XCMO 0x40 /* Page Coalescing */ 7048391e42aSNathan Fontenot #else 7058391e42aSNathan Fontenot #define OV5_CMO 0x00 7069ee820faSBrian King #define OV5_XCMO 0x00 7078391e42aSNathan Fontenot #endif 7084b83c330SAnton Blanchard #define OV5_TYPE1_AFFINITY 0x80 /* Type 1 NUMA affinity */ 709f709bfacSPaul Mackerras 71028bb9ee1Sjschopp@austin.ibm.com /* Option Vector 6: IBM PAPR hints */ 71128bb9ee1Sjschopp@austin.ibm.com #define OV6_LINUX 0x02 /* Linux is our OS */ 71228bb9ee1Sjschopp@austin.ibm.com 713f709bfacSPaul Mackerras /* 714f709bfacSPaul Mackerras * The architecture vector has an array of PVR mask/value pairs, 715f709bfacSPaul Mackerras * followed by # option vectors - 1, followed by the option vectors. 716f709bfacSPaul Mackerras */ 717f709bfacSPaul Mackerras static unsigned char ibm_architecture_vec[] = { 718f709bfacSPaul Mackerras W(0xfffe0000), W(0x003a0000), /* POWER5/POWER5+ */ 71903054d51SAnton Blanchard W(0xffff0000), W(0x003e0000), /* POWER6 */ 720e952e6c4SMichael Neuling W(0xffff0000), W(0x003f0000), /* POWER7 */ 7210cb99013SJoel Schopp W(0xffffffff), W(0x0f000003), /* all 2.06-compliant */ 7220efbc18aSPaul Mackerras W(0xffffffff), W(0x0f000002), /* all 2.05-compliant */ 723f709bfacSPaul Mackerras W(0xfffffffe), W(0x0f000001), /* all 2.04-compliant and earlier */ 72428bb9ee1Sjschopp@austin.ibm.com 6 - 1, /* 6 option vectors */ 725f709bfacSPaul Mackerras 726f709bfacSPaul Mackerras /* option vector 1: processor architectures supported */ 72711e9ed43SWill Schmidt 3 - 2, /* length */ 728f709bfacSPaul Mackerras 0, /* don't ignore, don't halt */ 729f709bfacSPaul Mackerras OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 | 7300cb99013SJoel Schopp OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06, 731f709bfacSPaul Mackerras 732f709bfacSPaul Mackerras /* option vector 2: Open Firmware options supported */ 73311e9ed43SWill Schmidt 34 - 2, /* length */ 734f709bfacSPaul Mackerras OV2_REAL_MODE, 735f709bfacSPaul Mackerras 0, 0, 736f709bfacSPaul Mackerras W(0xffffffff), /* real_base */ 737f709bfacSPaul Mackerras W(0xffffffff), /* real_size */ 738f709bfacSPaul Mackerras W(0xffffffff), /* virt_base */ 739f709bfacSPaul Mackerras W(0xffffffff), /* virt_size */ 740f709bfacSPaul Mackerras W(0xffffffff), /* load_base */ 741856cc2f0SAnton Blanchard W(64), /* 64MB min RMA */ 742f709bfacSPaul Mackerras W(0xffffffff), /* full client load */ 743f709bfacSPaul Mackerras 0, /* min RMA percentage of total RAM */ 744f709bfacSPaul Mackerras 48, /* max log_2(hash table size) */ 745f709bfacSPaul Mackerras 746f709bfacSPaul Mackerras /* option vector 3: processor options supported */ 74711e9ed43SWill Schmidt 3 - 2, /* length */ 748f709bfacSPaul Mackerras 0, /* don't ignore, don't halt */ 749974a76f5SPaul Mackerras OV3_FP | OV3_VMX | OV3_DFP, 750f709bfacSPaul Mackerras 751f709bfacSPaul Mackerras /* option vector 4: IBM PAPR implementation */ 75211e9ed43SWill Schmidt 2 - 2, /* length */ 753f709bfacSPaul Mackerras 0, /* don't halt */ 754f709bfacSPaul Mackerras 755f709bfacSPaul Mackerras /* option vector 5: PAPR/OF options */ 75628bb9ee1Sjschopp@austin.ibm.com 13 - 2, /* length */ 757f709bfacSPaul Mackerras 0, /* don't ignore, don't halt */ 758d8c391a5SJake Moilanen OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY | 759d8c391a5SJake Moilanen OV5_DONATE_DEDICATE_CPU | OV5_MSI, 7608391e42aSNathan Fontenot 0, 7619ee820faSBrian King OV5_CMO | OV5_XCMO, 7624b83c330SAnton Blanchard OV5_TYPE1_AFFINITY, 76328bb9ee1Sjschopp@austin.ibm.com 0, 76428bb9ee1Sjschopp@austin.ibm.com 0, 76528bb9ee1Sjschopp@austin.ibm.com 0, 766efec959fSBenjamin Herrenschmidt /* WARNING: The offset of the "number of cores" field below 767efec959fSBenjamin Herrenschmidt * must match by the macro below. Update the definition if 768efec959fSBenjamin Herrenschmidt * the structure layout changes. 769efec959fSBenjamin Herrenschmidt */ 770efec959fSBenjamin Herrenschmidt #define IBM_ARCH_VEC_NRCORES_OFFSET 100 77128bb9ee1Sjschopp@austin.ibm.com W(NR_CPUS), /* number of cores supported */ 77228bb9ee1Sjschopp@austin.ibm.com 77328bb9ee1Sjschopp@austin.ibm.com /* option vector 6: IBM PAPR hints */ 77428bb9ee1Sjschopp@austin.ibm.com 4 - 2, /* length */ 77528bb9ee1Sjschopp@austin.ibm.com 0, 77628bb9ee1Sjschopp@austin.ibm.com 0, 77728bb9ee1Sjschopp@austin.ibm.com OV6_LINUX, 77828bb9ee1Sjschopp@austin.ibm.com 779f709bfacSPaul Mackerras }; 780f709bfacSPaul Mackerras 781f709bfacSPaul Mackerras /* Old method - ELF header with PT_NOTE sections */ 7829b6b563cSPaul Mackerras static struct fake_elf { 7839b6b563cSPaul Mackerras Elf32_Ehdr elfhdr; 7849b6b563cSPaul Mackerras Elf32_Phdr phdr[2]; 7859b6b563cSPaul Mackerras struct chrpnote { 7869b6b563cSPaul Mackerras u32 namesz; 7879b6b563cSPaul Mackerras u32 descsz; 7889b6b563cSPaul Mackerras u32 type; 7899b6b563cSPaul Mackerras char name[8]; /* "PowerPC" */ 7909b6b563cSPaul Mackerras struct chrpdesc { 7919b6b563cSPaul Mackerras u32 real_mode; 7929b6b563cSPaul Mackerras u32 real_base; 7939b6b563cSPaul Mackerras u32 real_size; 7949b6b563cSPaul Mackerras u32 virt_base; 7959b6b563cSPaul Mackerras u32 virt_size; 7969b6b563cSPaul Mackerras u32 load_base; 7979b6b563cSPaul Mackerras } chrpdesc; 7989b6b563cSPaul Mackerras } chrpnote; 7999b6b563cSPaul Mackerras struct rpanote { 8009b6b563cSPaul Mackerras u32 namesz; 8019b6b563cSPaul Mackerras u32 descsz; 8029b6b563cSPaul Mackerras u32 type; 8039b6b563cSPaul Mackerras char name[24]; /* "IBM,RPA-Client-Config" */ 8049b6b563cSPaul Mackerras struct rpadesc { 8059b6b563cSPaul Mackerras u32 lpar_affinity; 8069b6b563cSPaul Mackerras u32 min_rmo_size; 8079b6b563cSPaul Mackerras u32 min_rmo_percent; 8089b6b563cSPaul Mackerras u32 max_pft_size; 8099b6b563cSPaul Mackerras u32 splpar; 8109b6b563cSPaul Mackerras u32 min_load; 8119b6b563cSPaul Mackerras u32 new_mem_def; 8129b6b563cSPaul Mackerras u32 ignore_me; 8139b6b563cSPaul Mackerras } rpadesc; 8149b6b563cSPaul Mackerras } rpanote; 8155663a123SPaul Mackerras } fake_elf = { 8169b6b563cSPaul Mackerras .elfhdr = { 8179b6b563cSPaul Mackerras .e_ident = { 0x7f, 'E', 'L', 'F', 8189b6b563cSPaul Mackerras ELFCLASS32, ELFDATA2MSB, EV_CURRENT }, 8199b6b563cSPaul Mackerras .e_type = ET_EXEC, /* yeah right */ 8209b6b563cSPaul Mackerras .e_machine = EM_PPC, 8219b6b563cSPaul Mackerras .e_version = EV_CURRENT, 8229b6b563cSPaul Mackerras .e_phoff = offsetof(struct fake_elf, phdr), 8239b6b563cSPaul Mackerras .e_phentsize = sizeof(Elf32_Phdr), 8249b6b563cSPaul Mackerras .e_phnum = 2 8259b6b563cSPaul Mackerras }, 8269b6b563cSPaul Mackerras .phdr = { 8279b6b563cSPaul Mackerras [0] = { 8289b6b563cSPaul Mackerras .p_type = PT_NOTE, 8299b6b563cSPaul Mackerras .p_offset = offsetof(struct fake_elf, chrpnote), 8309b6b563cSPaul Mackerras .p_filesz = sizeof(struct chrpnote) 8319b6b563cSPaul Mackerras }, [1] = { 8329b6b563cSPaul Mackerras .p_type = PT_NOTE, 8339b6b563cSPaul Mackerras .p_offset = offsetof(struct fake_elf, rpanote), 8349b6b563cSPaul Mackerras .p_filesz = sizeof(struct rpanote) 8359b6b563cSPaul Mackerras } 8369b6b563cSPaul Mackerras }, 8379b6b563cSPaul Mackerras .chrpnote = { 8389b6b563cSPaul Mackerras .namesz = sizeof("PowerPC"), 8399b6b563cSPaul Mackerras .descsz = sizeof(struct chrpdesc), 8409b6b563cSPaul Mackerras .type = 0x1275, 8419b6b563cSPaul Mackerras .name = "PowerPC", 8429b6b563cSPaul Mackerras .chrpdesc = { 8439b6b563cSPaul Mackerras .real_mode = ~0U, /* ~0 means "don't care" */ 8449b6b563cSPaul Mackerras .real_base = ~0U, 8459b6b563cSPaul Mackerras .real_size = ~0U, 8469b6b563cSPaul Mackerras .virt_base = ~0U, 8479b6b563cSPaul Mackerras .virt_size = ~0U, 8489b6b563cSPaul Mackerras .load_base = ~0U 8499b6b563cSPaul Mackerras }, 8509b6b563cSPaul Mackerras }, 8519b6b563cSPaul Mackerras .rpanote = { 8529b6b563cSPaul Mackerras .namesz = sizeof("IBM,RPA-Client-Config"), 8539b6b563cSPaul Mackerras .descsz = sizeof(struct rpadesc), 8549b6b563cSPaul Mackerras .type = 0x12759999, 8559b6b563cSPaul Mackerras .name = "IBM,RPA-Client-Config", 8569b6b563cSPaul Mackerras .rpadesc = { 8575663a123SPaul Mackerras .lpar_affinity = 0, 8585663a123SPaul Mackerras .min_rmo_size = 64, /* in megabytes */ 8599b6b563cSPaul Mackerras .min_rmo_percent = 0, 8605663a123SPaul Mackerras .max_pft_size = 48, /* 2^48 bytes max PFT size */ 8619b6b563cSPaul Mackerras .splpar = 1, 8629b6b563cSPaul Mackerras .min_load = ~0U, 8635663a123SPaul Mackerras .new_mem_def = 0 8649b6b563cSPaul Mackerras } 8659b6b563cSPaul Mackerras } 8669b6b563cSPaul Mackerras }; 8679b6b563cSPaul Mackerras 868efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void) 869efec959fSBenjamin Herrenschmidt { 870efec959fSBenjamin Herrenschmidt phandle node; 871efec959fSBenjamin Herrenschmidt char type[64]; 872efec959fSBenjamin Herrenschmidt unsigned int plen; 873efec959fSBenjamin Herrenschmidt 874efec959fSBenjamin Herrenschmidt /* Pick up th first CPU node we can find */ 875efec959fSBenjamin Herrenschmidt for (node = 0; prom_next_node(&node); ) { 876efec959fSBenjamin Herrenschmidt type[0] = 0; 877efec959fSBenjamin Herrenschmidt prom_getprop(node, "device_type", type, sizeof(type)); 878efec959fSBenjamin Herrenschmidt 879efec959fSBenjamin Herrenschmidt if (strcmp(type, RELOC("cpu"))) 880efec959fSBenjamin Herrenschmidt continue; 881efec959fSBenjamin Herrenschmidt /* 882efec959fSBenjamin Herrenschmidt * There is an entry for each smt thread, each entry being 883efec959fSBenjamin Herrenschmidt * 4 bytes long. All cpus should have the same number of 884efec959fSBenjamin Herrenschmidt * smt threads, so return after finding the first. 885efec959fSBenjamin Herrenschmidt */ 886efec959fSBenjamin Herrenschmidt plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s"); 887efec959fSBenjamin Herrenschmidt if (plen == PROM_ERROR) 888efec959fSBenjamin Herrenschmidt break; 889efec959fSBenjamin Herrenschmidt plen >>= 2; 8902c48a7d6SMichael Neuling prom_debug("Found %lu smt threads per core\n", (unsigned long)plen); 891efec959fSBenjamin Herrenschmidt 892efec959fSBenjamin Herrenschmidt /* Sanity check */ 893efec959fSBenjamin Herrenschmidt if (plen < 1 || plen > 64) { 8942c48a7d6SMichael Neuling prom_printf("Threads per core %lu out of bounds, assuming 1\n", 895efec959fSBenjamin Herrenschmidt (unsigned long)plen); 896efec959fSBenjamin Herrenschmidt return 1; 897efec959fSBenjamin Herrenschmidt } 898efec959fSBenjamin Herrenschmidt return plen; 899efec959fSBenjamin Herrenschmidt } 900efec959fSBenjamin Herrenschmidt prom_debug("No threads found, assuming 1 per core\n"); 901efec959fSBenjamin Herrenschmidt 902efec959fSBenjamin Herrenschmidt return 1; 903efec959fSBenjamin Herrenschmidt 904efec959fSBenjamin Herrenschmidt } 905efec959fSBenjamin Herrenschmidt 906efec959fSBenjamin Herrenschmidt 9079b6b563cSPaul Mackerras static void __init prom_send_capabilities(void) 9089b6b563cSPaul Mackerras { 909f709bfacSPaul Mackerras ihandle elfloader, root; 910f709bfacSPaul Mackerras prom_arg_t ret; 911efec959fSBenjamin Herrenschmidt u32 *cores; 9129b6b563cSPaul Mackerras 913f709bfacSPaul Mackerras root = call_prom("open", 1, 1, ADDR("/")); 914f709bfacSPaul Mackerras if (root != 0) { 915efec959fSBenjamin Herrenschmidt /* We need to tell the FW about the number of cores we support. 916efec959fSBenjamin Herrenschmidt * 917efec959fSBenjamin Herrenschmidt * To do that, we count the number of threads on the first core 918efec959fSBenjamin Herrenschmidt * (we assume this is the same for all cores) and use it to 919efec959fSBenjamin Herrenschmidt * divide NR_CPUS. 920efec959fSBenjamin Herrenschmidt */ 921efec959fSBenjamin Herrenschmidt cores = (u32 *)PTRRELOC(&ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET]); 922efec959fSBenjamin Herrenschmidt if (*cores != NR_CPUS) { 923efec959fSBenjamin Herrenschmidt prom_printf("WARNING ! " 9242c48a7d6SMichael Neuling "ibm_architecture_vec structure inconsistent: %lu!\n", 925efec959fSBenjamin Herrenschmidt *cores); 926efec959fSBenjamin Herrenschmidt } else { 92733ad5e4bSAnton Blanchard *cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads()); 9282c48a7d6SMichael Neuling prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n", 9292c48a7d6SMichael Neuling *cores, NR_CPUS); 930efec959fSBenjamin Herrenschmidt } 931efec959fSBenjamin Herrenschmidt 932f709bfacSPaul Mackerras /* try calling the ibm,client-architecture-support method */ 933049d0497SAnton Blanchard prom_printf("Calling ibm,client-architecture-support..."); 934f709bfacSPaul Mackerras if (call_prom_ret("call-method", 3, 2, &ret, 935f709bfacSPaul Mackerras ADDR("ibm,client-architecture-support"), 93633b74977SBenjamin Herrenschmidt root, 937f709bfacSPaul Mackerras ADDR(ibm_architecture_vec)) == 0) { 938f709bfacSPaul Mackerras /* the call exists... */ 939f709bfacSPaul Mackerras if (ret) 9404da727aeSAnton Blanchard prom_printf("\nWARNING: ibm,client-architecture" 941f709bfacSPaul Mackerras "-support call FAILED!\n"); 942f709bfacSPaul Mackerras call_prom("close", 1, 0, root); 9434da727aeSAnton Blanchard prom_printf(" done\n"); 944f709bfacSPaul Mackerras return; 945f709bfacSPaul Mackerras } 946f709bfacSPaul Mackerras call_prom("close", 1, 0, root); 947049d0497SAnton Blanchard prom_printf(" not implemented\n"); 948f709bfacSPaul Mackerras } 949f709bfacSPaul Mackerras 950f709bfacSPaul Mackerras /* no ibm,client-architecture-support call, try the old way */ 9519b6b563cSPaul Mackerras elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader")); 9529b6b563cSPaul Mackerras if (elfloader == 0) { 9539b6b563cSPaul Mackerras prom_printf("couldn't open /packages/elf-loader\n"); 9549b6b563cSPaul Mackerras return; 9559b6b563cSPaul Mackerras } 9569b6b563cSPaul Mackerras call_prom("call-method", 3, 1, ADDR("process-elf-header"), 9579b6b563cSPaul Mackerras elfloader, ADDR(&fake_elf)); 9589b6b563cSPaul Mackerras call_prom("close", 1, 0, elfloader); 9599b6b563cSPaul Mackerras } 9609b6b563cSPaul Mackerras #endif 9619b6b563cSPaul Mackerras 9629b6b563cSPaul Mackerras /* 9639b6b563cSPaul Mackerras * Memory allocation strategy... our layout is normally: 9649b6b563cSPaul Mackerras * 9659b6b563cSPaul Mackerras * at 14Mb or more we have vmlinux, then a gap and initrd. In some 9669b6b563cSPaul Mackerras * rare cases, initrd might end up being before the kernel though. 9679b6b563cSPaul Mackerras * We assume this won't override the final kernel at 0, we have no 9689b6b563cSPaul Mackerras * provision to handle that in this version, but it should hopefully 9699b6b563cSPaul Mackerras * never happen. 9709b6b563cSPaul Mackerras * 9719b6b563cSPaul Mackerras * alloc_top is set to the top of RMO, eventually shrink down if the 9729b6b563cSPaul Mackerras * TCEs overlap 9739b6b563cSPaul Mackerras * 9749b6b563cSPaul Mackerras * alloc_bottom is set to the top of kernel/initrd 9759b6b563cSPaul Mackerras * 9769b6b563cSPaul Mackerras * from there, allocations are done this way : rtas is allocated 9779b6b563cSPaul Mackerras * topmost, and the device-tree is allocated from the bottom. We try 9789b6b563cSPaul Mackerras * to grow the device-tree allocation as we progress. If we can't, 9799b6b563cSPaul Mackerras * then we fail, we don't currently have a facility to restart 9809b6b563cSPaul Mackerras * elsewhere, but that shouldn't be necessary. 9819b6b563cSPaul Mackerras * 9829b6b563cSPaul Mackerras * Note that calls to reserve_mem have to be done explicitly, memory 9839b6b563cSPaul Mackerras * allocated with either alloc_up or alloc_down isn't automatically 9849b6b563cSPaul Mackerras * reserved. 9859b6b563cSPaul Mackerras */ 9869b6b563cSPaul Mackerras 9879b6b563cSPaul Mackerras 9889b6b563cSPaul Mackerras /* 9899b6b563cSPaul Mackerras * Allocates memory in the RMO upward from the kernel/initrd 9909b6b563cSPaul Mackerras * 9919b6b563cSPaul Mackerras * When align is 0, this is a special case, it means to allocate in place 9929b6b563cSPaul Mackerras * at the current location of alloc_bottom or fail (that is basically 9939b6b563cSPaul Mackerras * extending the previous allocation). Used for the device-tree flattening 9949b6b563cSPaul Mackerras */ 9959b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align) 9969b6b563cSPaul Mackerras { 997c4988820SPaul Mackerras unsigned long base = RELOC(alloc_bottom); 9989b6b563cSPaul Mackerras unsigned long addr = 0; 9999b6b563cSPaul Mackerras 1000c4988820SPaul Mackerras if (align) 1001c4988820SPaul Mackerras base = _ALIGN_UP(base, align); 10029b6b563cSPaul Mackerras prom_debug("alloc_up(%x, %x)\n", size, align); 10039b6b563cSPaul Mackerras if (RELOC(ram_top) == 0) 10049b6b563cSPaul Mackerras prom_panic("alloc_up() called with mem not initialized\n"); 10059b6b563cSPaul Mackerras 10069b6b563cSPaul Mackerras if (align) 10079b6b563cSPaul Mackerras base = _ALIGN_UP(RELOC(alloc_bottom), align); 10089b6b563cSPaul Mackerras else 10099b6b563cSPaul Mackerras base = RELOC(alloc_bottom); 10109b6b563cSPaul Mackerras 10119b6b563cSPaul Mackerras for(; (base + size) <= RELOC(alloc_top); 10129b6b563cSPaul Mackerras base = _ALIGN_UP(base + 0x100000, align)) { 10139b6b563cSPaul Mackerras prom_debug(" trying: 0x%x\n\r", base); 10149b6b563cSPaul Mackerras addr = (unsigned long)prom_claim(base, size, 0); 1015c4988820SPaul Mackerras if (addr != PROM_ERROR && addr != 0) 10169b6b563cSPaul Mackerras break; 10179b6b563cSPaul Mackerras addr = 0; 10189b6b563cSPaul Mackerras if (align == 0) 10199b6b563cSPaul Mackerras break; 10209b6b563cSPaul Mackerras } 10219b6b563cSPaul Mackerras if (addr == 0) 10229b6b563cSPaul Mackerras return 0; 1023*966728ddSAnton Blanchard RELOC(alloc_bottom) = addr + size; 10249b6b563cSPaul Mackerras 10259b6b563cSPaul Mackerras prom_debug(" -> %x\n", addr); 10269b6b563cSPaul Mackerras prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom)); 10279b6b563cSPaul Mackerras prom_debug(" alloc_top : %x\n", RELOC(alloc_top)); 10289b6b563cSPaul Mackerras prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high)); 10299b6b563cSPaul Mackerras prom_debug(" rmo_top : %x\n", RELOC(rmo_top)); 10309b6b563cSPaul Mackerras prom_debug(" ram_top : %x\n", RELOC(ram_top)); 10319b6b563cSPaul Mackerras 10329b6b563cSPaul Mackerras return addr; 10339b6b563cSPaul Mackerras } 10349b6b563cSPaul Mackerras 10359b6b563cSPaul Mackerras /* 10369b6b563cSPaul Mackerras * Allocates memory downward, either from top of RMO, or if highmem 10379b6b563cSPaul Mackerras * is set, from the top of RAM. Note that this one doesn't handle 10389b6b563cSPaul Mackerras * failures. It does claim memory if highmem is not set. 10399b6b563cSPaul Mackerras */ 10409b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align, 10419b6b563cSPaul Mackerras int highmem) 10429b6b563cSPaul Mackerras { 10439b6b563cSPaul Mackerras unsigned long base, addr = 0; 10449b6b563cSPaul Mackerras 10459b6b563cSPaul Mackerras prom_debug("alloc_down(%x, %x, %s)\n", size, align, 10469b6b563cSPaul Mackerras highmem ? RELOC("(high)") : RELOC("(low)")); 10479b6b563cSPaul Mackerras if (RELOC(ram_top) == 0) 10489b6b563cSPaul Mackerras prom_panic("alloc_down() called with mem not initialized\n"); 10499b6b563cSPaul Mackerras 10509b6b563cSPaul Mackerras if (highmem) { 10519b6b563cSPaul Mackerras /* Carve out storage for the TCE table. */ 10529b6b563cSPaul Mackerras addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align); 10539b6b563cSPaul Mackerras if (addr <= RELOC(alloc_bottom)) 10549b6b563cSPaul Mackerras return 0; 10559b6b563cSPaul Mackerras /* Will we bump into the RMO ? If yes, check out that we 10569b6b563cSPaul Mackerras * didn't overlap existing allocations there, if we did, 10579b6b563cSPaul Mackerras * we are dead, we must be the first in town ! 10589b6b563cSPaul Mackerras */ 10599b6b563cSPaul Mackerras if (addr < RELOC(rmo_top)) { 10609b6b563cSPaul Mackerras /* Good, we are first */ 10619b6b563cSPaul Mackerras if (RELOC(alloc_top) == RELOC(rmo_top)) 10629b6b563cSPaul Mackerras RELOC(alloc_top) = RELOC(rmo_top) = addr; 10639b6b563cSPaul Mackerras else 10649b6b563cSPaul Mackerras return 0; 10659b6b563cSPaul Mackerras } 10669b6b563cSPaul Mackerras RELOC(alloc_top_high) = addr; 10679b6b563cSPaul Mackerras goto bail; 10689b6b563cSPaul Mackerras } 10699b6b563cSPaul Mackerras 10709b6b563cSPaul Mackerras base = _ALIGN_DOWN(RELOC(alloc_top) - size, align); 10719b6b563cSPaul Mackerras for (; base > RELOC(alloc_bottom); 10729b6b563cSPaul Mackerras base = _ALIGN_DOWN(base - 0x100000, align)) { 10739b6b563cSPaul Mackerras prom_debug(" trying: 0x%x\n\r", base); 10749b6b563cSPaul Mackerras addr = (unsigned long)prom_claim(base, size, 0); 1075c4988820SPaul Mackerras if (addr != PROM_ERROR && addr != 0) 10769b6b563cSPaul Mackerras break; 10779b6b563cSPaul Mackerras addr = 0; 10789b6b563cSPaul Mackerras } 10799b6b563cSPaul Mackerras if (addr == 0) 10809b6b563cSPaul Mackerras return 0; 10819b6b563cSPaul Mackerras RELOC(alloc_top) = addr; 10829b6b563cSPaul Mackerras 10839b6b563cSPaul Mackerras bail: 10849b6b563cSPaul Mackerras prom_debug(" -> %x\n", addr); 10859b6b563cSPaul Mackerras prom_debug(" alloc_bottom : %x\n", RELOC(alloc_bottom)); 10869b6b563cSPaul Mackerras prom_debug(" alloc_top : %x\n", RELOC(alloc_top)); 10879b6b563cSPaul Mackerras prom_debug(" alloc_top_hi : %x\n", RELOC(alloc_top_high)); 10889b6b563cSPaul Mackerras prom_debug(" rmo_top : %x\n", RELOC(rmo_top)); 10899b6b563cSPaul Mackerras prom_debug(" ram_top : %x\n", RELOC(ram_top)); 10909b6b563cSPaul Mackerras 10919b6b563cSPaul Mackerras return addr; 10929b6b563cSPaul Mackerras } 10939b6b563cSPaul Mackerras 10949b6b563cSPaul Mackerras /* 10959b6b563cSPaul Mackerras * Parse a "reg" cell 10969b6b563cSPaul Mackerras */ 10979b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp) 10989b6b563cSPaul Mackerras { 10999b6b563cSPaul Mackerras cell_t *p = *cellp; 11009b6b563cSPaul Mackerras unsigned long r = 0; 11019b6b563cSPaul Mackerras 11029b6b563cSPaul Mackerras /* Ignore more than 2 cells */ 11039b6b563cSPaul Mackerras while (s > sizeof(unsigned long) / 4) { 11049b6b563cSPaul Mackerras p++; 11059b6b563cSPaul Mackerras s--; 11069b6b563cSPaul Mackerras } 11079b6b563cSPaul Mackerras r = *p++; 11089b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 110935499c01SPaul Mackerras if (s > 1) { 11109b6b563cSPaul Mackerras r <<= 32; 11119b6b563cSPaul Mackerras r |= *(p++); 11129b6b563cSPaul Mackerras } 11139b6b563cSPaul Mackerras #endif 11149b6b563cSPaul Mackerras *cellp = p; 11159b6b563cSPaul Mackerras return r; 11169b6b563cSPaul Mackerras } 11179b6b563cSPaul Mackerras 11189b6b563cSPaul Mackerras /* 11199b6b563cSPaul Mackerras * Very dumb function for adding to the memory reserve list, but 11209b6b563cSPaul Mackerras * we don't need anything smarter at this point 11219b6b563cSPaul Mackerras * 11229b6b563cSPaul Mackerras * XXX Eventually check for collisions. They should NEVER happen. 11239b6b563cSPaul Mackerras * If problems seem to show up, it would be a good start to track 11249b6b563cSPaul Mackerras * them down. 11259b6b563cSPaul Mackerras */ 11260108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size) 11279b6b563cSPaul Mackerras { 1128cbbcf340SKumar Gala u64 top = base + size; 11299b6b563cSPaul Mackerras unsigned long cnt = RELOC(mem_reserve_cnt); 11309b6b563cSPaul Mackerras 11319b6b563cSPaul Mackerras if (size == 0) 11329b6b563cSPaul Mackerras return; 11339b6b563cSPaul Mackerras 11349b6b563cSPaul Mackerras /* We need to always keep one empty entry so that we 11359b6b563cSPaul Mackerras * have our terminator with "size" set to 0 since we are 11369b6b563cSPaul Mackerras * dumb and just copy this entire array to the boot params 11379b6b563cSPaul Mackerras */ 11389b6b563cSPaul Mackerras base = _ALIGN_DOWN(base, PAGE_SIZE); 11399b6b563cSPaul Mackerras top = _ALIGN_UP(top, PAGE_SIZE); 11409b6b563cSPaul Mackerras size = top - base; 11419b6b563cSPaul Mackerras 11429b6b563cSPaul Mackerras if (cnt >= (MEM_RESERVE_MAP_SIZE - 1)) 11439b6b563cSPaul Mackerras prom_panic("Memory reserve map exhausted !\n"); 11449b6b563cSPaul Mackerras RELOC(mem_reserve_map)[cnt].base = base; 11459b6b563cSPaul Mackerras RELOC(mem_reserve_map)[cnt].size = size; 11469b6b563cSPaul Mackerras RELOC(mem_reserve_cnt) = cnt + 1; 11479b6b563cSPaul Mackerras } 11489b6b563cSPaul Mackerras 11499b6b563cSPaul Mackerras /* 1150b3c2ffd5SAdrian Bunk * Initialize memory allocation mechanism, parse "memory" nodes and 11519b6b563cSPaul Mackerras * obtain that way the top of memory and RMO to setup out local allocator 11529b6b563cSPaul Mackerras */ 11539b6b563cSPaul Mackerras static void __init prom_init_mem(void) 11549b6b563cSPaul Mackerras { 11559b6b563cSPaul Mackerras phandle node; 11569b6b563cSPaul Mackerras char *path, type[64]; 11579b6b563cSPaul Mackerras unsigned int plen; 11589b6b563cSPaul Mackerras cell_t *p, *endp; 11599b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 11609b6b563cSPaul Mackerras u32 rac, rsc; 11619b6b563cSPaul Mackerras 11629b6b563cSPaul Mackerras /* 11639b6b563cSPaul Mackerras * We iterate the memory nodes to find 11649b6b563cSPaul Mackerras * 1) top of RMO (first node) 11659b6b563cSPaul Mackerras * 2) top of memory 11669b6b563cSPaul Mackerras */ 11679b6b563cSPaul Mackerras rac = 2; 11689b6b563cSPaul Mackerras prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac)); 11699b6b563cSPaul Mackerras rsc = 1; 11709b6b563cSPaul Mackerras prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc)); 11719b6b563cSPaul Mackerras prom_debug("root_addr_cells: %x\n", (unsigned long) rac); 11729b6b563cSPaul Mackerras prom_debug("root_size_cells: %x\n", (unsigned long) rsc); 11739b6b563cSPaul Mackerras 11749b6b563cSPaul Mackerras prom_debug("scanning memory:\n"); 11759b6b563cSPaul Mackerras path = RELOC(prom_scratch); 11769b6b563cSPaul Mackerras 11779b6b563cSPaul Mackerras for (node = 0; prom_next_node(&node); ) { 11789b6b563cSPaul Mackerras type[0] = 0; 11799b6b563cSPaul Mackerras prom_getprop(node, "device_type", type, sizeof(type)); 11809b6b563cSPaul Mackerras 1181c4988820SPaul Mackerras if (type[0] == 0) { 1182c4988820SPaul Mackerras /* 1183c4988820SPaul Mackerras * CHRP Longtrail machines have no device_type 1184c4988820SPaul Mackerras * on the memory node, so check the name instead... 1185c4988820SPaul Mackerras */ 1186c4988820SPaul Mackerras prom_getprop(node, "name", type, sizeof(type)); 1187c4988820SPaul Mackerras } 11889b6b563cSPaul Mackerras if (strcmp(type, RELOC("memory"))) 11899b6b563cSPaul Mackerras continue; 11909b6b563cSPaul Mackerras 11919b6b563cSPaul Mackerras plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf)); 11929b6b563cSPaul Mackerras if (plen > sizeof(regbuf)) { 11939b6b563cSPaul Mackerras prom_printf("memory node too large for buffer !\n"); 11949b6b563cSPaul Mackerras plen = sizeof(regbuf); 11959b6b563cSPaul Mackerras } 11969b6b563cSPaul Mackerras p = RELOC(regbuf); 11979b6b563cSPaul Mackerras endp = p + (plen / sizeof(cell_t)); 11989b6b563cSPaul Mackerras 11999b6b563cSPaul Mackerras #ifdef DEBUG_PROM 12009b6b563cSPaul Mackerras memset(path, 0, PROM_SCRATCH_SIZE); 12019b6b563cSPaul Mackerras call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1); 12029b6b563cSPaul Mackerras prom_debug(" node %s :\n", path); 12039b6b563cSPaul Mackerras #endif /* DEBUG_PROM */ 12049b6b563cSPaul Mackerras 12059b6b563cSPaul Mackerras while ((endp - p) >= (rac + rsc)) { 12069b6b563cSPaul Mackerras unsigned long base, size; 12079b6b563cSPaul Mackerras 12089b6b563cSPaul Mackerras base = prom_next_cell(rac, &p); 12099b6b563cSPaul Mackerras size = prom_next_cell(rsc, &p); 12109b6b563cSPaul Mackerras 12119b6b563cSPaul Mackerras if (size == 0) 12129b6b563cSPaul Mackerras continue; 12139b6b563cSPaul Mackerras prom_debug(" %x %x\n", base, size); 1214ab1b55e2SBenjamin Herrenschmidt if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR)) 12159b6b563cSPaul Mackerras RELOC(rmo_top) = size; 12169b6b563cSPaul Mackerras if ((base + size) > RELOC(ram_top)) 12179b6b563cSPaul Mackerras RELOC(ram_top) = base + size; 12189b6b563cSPaul Mackerras } 12199b6b563cSPaul Mackerras } 12209b6b563cSPaul Mackerras 12219b6b563cSPaul Mackerras RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000); 12229b6b563cSPaul Mackerras 12239b6b563cSPaul Mackerras /* Check if we have an initrd after the kernel, if we do move our bottom 12249b6b563cSPaul Mackerras * point to after it 12259b6b563cSPaul Mackerras */ 12269b6b563cSPaul Mackerras if (RELOC(prom_initrd_start)) { 12279b6b563cSPaul Mackerras if (RELOC(prom_initrd_end) > RELOC(alloc_bottom)) 12289b6b563cSPaul Mackerras RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end)); 12299b6b563cSPaul Mackerras } 12309b6b563cSPaul Mackerras 12319b6b563cSPaul Mackerras /* 1232cf68787bSBenjamin Krill * If prom_memory_limit is set we reduce the upper limits *except* for 1233cf68787bSBenjamin Krill * alloc_top_high. This must be the real top of RAM so we can put 1234cf68787bSBenjamin Krill * TCE's up there. 1235cf68787bSBenjamin Krill */ 1236cf68787bSBenjamin Krill 1237cf68787bSBenjamin Krill RELOC(alloc_top_high) = RELOC(ram_top); 1238cf68787bSBenjamin Krill 1239cf68787bSBenjamin Krill if (RELOC(prom_memory_limit)) { 1240cf68787bSBenjamin Krill if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) { 1241cf68787bSBenjamin Krill prom_printf("Ignoring mem=%x <= alloc_bottom.\n", 1242cf68787bSBenjamin Krill RELOC(prom_memory_limit)); 1243cf68787bSBenjamin Krill RELOC(prom_memory_limit) = 0; 1244cf68787bSBenjamin Krill } else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) { 1245cf68787bSBenjamin Krill prom_printf("Ignoring mem=%x >= ram_top.\n", 1246cf68787bSBenjamin Krill RELOC(prom_memory_limit)); 1247cf68787bSBenjamin Krill RELOC(prom_memory_limit) = 0; 1248cf68787bSBenjamin Krill } else { 1249cf68787bSBenjamin Krill RELOC(ram_top) = RELOC(prom_memory_limit); 1250cf68787bSBenjamin Krill RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit)); 1251cf68787bSBenjamin Krill } 1252cf68787bSBenjamin Krill } 1253cf68787bSBenjamin Krill 1254cf68787bSBenjamin Krill /* 12559b6b563cSPaul Mackerras * Setup our top alloc point, that is top of RMO or top of 12569b6b563cSPaul Mackerras * segment 0 when running non-LPAR. 12579b6b563cSPaul Mackerras * Some RS64 machines have buggy firmware where claims up at 12589b6b563cSPaul Mackerras * 1GB fail. Cap at 768MB as a workaround. 12599b6b563cSPaul Mackerras * Since 768MB is plenty of room, and we need to cap to something 12609b6b563cSPaul Mackerras * reasonable on 32-bit, cap at 768MB on all machines. 12619b6b563cSPaul Mackerras */ 12629b6b563cSPaul Mackerras if (!RELOC(rmo_top)) 12639b6b563cSPaul Mackerras RELOC(rmo_top) = RELOC(ram_top); 12649b6b563cSPaul Mackerras RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top)); 12659b6b563cSPaul Mackerras RELOC(alloc_top) = RELOC(rmo_top); 12662babf5c2SMichael Ellerman RELOC(alloc_top_high) = RELOC(ram_top); 12679b6b563cSPaul Mackerras 12689b6b563cSPaul Mackerras prom_printf("memory layout at init:\n"); 1269cf68787bSBenjamin Krill prom_printf(" memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit)); 12709b6b563cSPaul Mackerras prom_printf(" alloc_bottom : %x\n", RELOC(alloc_bottom)); 12719b6b563cSPaul Mackerras prom_printf(" alloc_top : %x\n", RELOC(alloc_top)); 12729b6b563cSPaul Mackerras prom_printf(" alloc_top_hi : %x\n", RELOC(alloc_top_high)); 12739b6b563cSPaul Mackerras prom_printf(" rmo_top : %x\n", RELOC(rmo_top)); 12749b6b563cSPaul Mackerras prom_printf(" ram_top : %x\n", RELOC(ram_top)); 12759b6b563cSPaul Mackerras } 12769b6b563cSPaul Mackerras 12779b6b563cSPaul Mackerras 12789b6b563cSPaul Mackerras /* 12799b6b563cSPaul Mackerras * Allocate room for and instantiate RTAS 12809b6b563cSPaul Mackerras */ 12819b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void) 12829b6b563cSPaul Mackerras { 12839b6b563cSPaul Mackerras phandle rtas_node; 12849b6b563cSPaul Mackerras ihandle rtas_inst; 12859b6b563cSPaul Mackerras u32 base, entry = 0; 12869b6b563cSPaul Mackerras u32 size = 0; 12879b6b563cSPaul Mackerras 12889b6b563cSPaul Mackerras prom_debug("prom_instantiate_rtas: start...\n"); 12899b6b563cSPaul Mackerras 12909b6b563cSPaul Mackerras rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas")); 12919b6b563cSPaul Mackerras prom_debug("rtas_node: %x\n", rtas_node); 12929b6b563cSPaul Mackerras if (!PHANDLE_VALID(rtas_node)) 12939b6b563cSPaul Mackerras return; 12949b6b563cSPaul Mackerras 12959b6b563cSPaul Mackerras prom_getprop(rtas_node, "rtas-size", &size, sizeof(size)); 12969b6b563cSPaul Mackerras if (size == 0) 12979b6b563cSPaul Mackerras return; 12989b6b563cSPaul Mackerras 12999b6b563cSPaul Mackerras base = alloc_down(size, PAGE_SIZE, 0); 13009b6b563cSPaul Mackerras if (base == 0) { 13019b6b563cSPaul Mackerras prom_printf("RTAS allocation failed !\n"); 13029b6b563cSPaul Mackerras return; 13039b6b563cSPaul Mackerras } 13049b6b563cSPaul Mackerras 13059b6b563cSPaul Mackerras rtas_inst = call_prom("open", 1, 1, ADDR("/rtas")); 13069b6b563cSPaul Mackerras if (!IHANDLE_VALID(rtas_inst)) { 1307a23414beSPaul Mackerras prom_printf("opening rtas package failed (%x)\n", rtas_inst); 13089b6b563cSPaul Mackerras return; 13099b6b563cSPaul Mackerras } 13109b6b563cSPaul Mackerras 13119b6b563cSPaul Mackerras prom_printf("instantiating rtas at 0x%x...", base); 13129b6b563cSPaul Mackerras 13139b6b563cSPaul Mackerras if (call_prom_ret("call-method", 3, 2, &entry, 13149b6b563cSPaul Mackerras ADDR("instantiate-rtas"), 1315a23414beSPaul Mackerras rtas_inst, base) != 0 13169b6b563cSPaul Mackerras || entry == 0) { 13179b6b563cSPaul Mackerras prom_printf(" failed\n"); 13189b6b563cSPaul Mackerras return; 13199b6b563cSPaul Mackerras } 13209b6b563cSPaul Mackerras prom_printf(" done\n"); 13219b6b563cSPaul Mackerras 13229b6b563cSPaul Mackerras reserve_mem(base, size); 13239b6b563cSPaul Mackerras 1324a23414beSPaul Mackerras prom_setprop(rtas_node, "/rtas", "linux,rtas-base", 1325a23414beSPaul Mackerras &base, sizeof(base)); 1326a23414beSPaul Mackerras prom_setprop(rtas_node, "/rtas", "linux,rtas-entry", 1327a23414beSPaul Mackerras &entry, sizeof(entry)); 13289b6b563cSPaul Mackerras 13299b6b563cSPaul Mackerras prom_debug("rtas base = 0x%x\n", base); 13309b6b563cSPaul Mackerras prom_debug("rtas entry = 0x%x\n", entry); 13319b6b563cSPaul Mackerras prom_debug("rtas size = 0x%x\n", (long)size); 13329b6b563cSPaul Mackerras 13339b6b563cSPaul Mackerras prom_debug("prom_instantiate_rtas: end...\n"); 13349b6b563cSPaul Mackerras } 13359b6b563cSPaul Mackerras 13369b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 13379b6b563cSPaul Mackerras /* 13389b6b563cSPaul Mackerras * Allocate room for and initialize TCE tables 13399b6b563cSPaul Mackerras */ 13409b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void) 13419b6b563cSPaul Mackerras { 13429b6b563cSPaul Mackerras phandle node; 13439b6b563cSPaul Mackerras ihandle phb_node; 13449b6b563cSPaul Mackerras char compatible[64], type[64], model[64]; 13459b6b563cSPaul Mackerras char *path = RELOC(prom_scratch); 13469b6b563cSPaul Mackerras u64 base, align; 13479b6b563cSPaul Mackerras u32 minalign, minsize; 13489b6b563cSPaul Mackerras u64 tce_entry, *tce_entryp; 13499b6b563cSPaul Mackerras u64 local_alloc_top, local_alloc_bottom; 13509b6b563cSPaul Mackerras u64 i; 13519b6b563cSPaul Mackerras 1352165785e5SJeremy Kerr if (RELOC(prom_iommu_off)) 13539b6b563cSPaul Mackerras return; 13549b6b563cSPaul Mackerras 13559b6b563cSPaul Mackerras prom_debug("starting prom_initialize_tce_table\n"); 13569b6b563cSPaul Mackerras 13579b6b563cSPaul Mackerras /* Cache current top of allocs so we reserve a single block */ 13589b6b563cSPaul Mackerras local_alloc_top = RELOC(alloc_top_high); 13599b6b563cSPaul Mackerras local_alloc_bottom = local_alloc_top; 13609b6b563cSPaul Mackerras 13619b6b563cSPaul Mackerras /* Search all nodes looking for PHBs. */ 13629b6b563cSPaul Mackerras for (node = 0; prom_next_node(&node); ) { 13639b6b563cSPaul Mackerras compatible[0] = 0; 13649b6b563cSPaul Mackerras type[0] = 0; 13659b6b563cSPaul Mackerras model[0] = 0; 13669b6b563cSPaul Mackerras prom_getprop(node, "compatible", 13679b6b563cSPaul Mackerras compatible, sizeof(compatible)); 13689b6b563cSPaul Mackerras prom_getprop(node, "device_type", type, sizeof(type)); 13699b6b563cSPaul Mackerras prom_getprop(node, "model", model, sizeof(model)); 13709b6b563cSPaul Mackerras 13719b6b563cSPaul Mackerras if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL)) 13729b6b563cSPaul Mackerras continue; 13739b6b563cSPaul Mackerras 1374e788ff13SLinas Vepstas /* Keep the old logic intact to avoid regression. */ 13759b6b563cSPaul Mackerras if (compatible[0] != 0) { 13769b6b563cSPaul Mackerras if ((strstr(compatible, RELOC("python")) == NULL) && 13779b6b563cSPaul Mackerras (strstr(compatible, RELOC("Speedwagon")) == NULL) && 13789b6b563cSPaul Mackerras (strstr(compatible, RELOC("Winnipeg")) == NULL)) 13799b6b563cSPaul Mackerras continue; 13809b6b563cSPaul Mackerras } else if (model[0] != 0) { 13819b6b563cSPaul Mackerras if ((strstr(model, RELOC("ython")) == NULL) && 13829b6b563cSPaul Mackerras (strstr(model, RELOC("peedwagon")) == NULL) && 13839b6b563cSPaul Mackerras (strstr(model, RELOC("innipeg")) == NULL)) 13849b6b563cSPaul Mackerras continue; 13859b6b563cSPaul Mackerras } 13869b6b563cSPaul Mackerras 13879b6b563cSPaul Mackerras if (prom_getprop(node, "tce-table-minalign", &minalign, 13889b6b563cSPaul Mackerras sizeof(minalign)) == PROM_ERROR) 13899b6b563cSPaul Mackerras minalign = 0; 13909b6b563cSPaul Mackerras if (prom_getprop(node, "tce-table-minsize", &minsize, 13919b6b563cSPaul Mackerras sizeof(minsize)) == PROM_ERROR) 13929b6b563cSPaul Mackerras minsize = 4UL << 20; 13939b6b563cSPaul Mackerras 13949b6b563cSPaul Mackerras /* 13959b6b563cSPaul Mackerras * Even though we read what OF wants, we just set the table 13969b6b563cSPaul Mackerras * size to 4 MB. This is enough to map 2GB of PCI DMA space. 13979b6b563cSPaul Mackerras * By doing this, we avoid the pitfalls of trying to DMA to 13989b6b563cSPaul Mackerras * MMIO space and the DMA alias hole. 13999b6b563cSPaul Mackerras * 14009b6b563cSPaul Mackerras * On POWER4, firmware sets the TCE region by assuming 14019b6b563cSPaul Mackerras * each TCE table is 8MB. Using this memory for anything 14029b6b563cSPaul Mackerras * else will impact performance, so we always allocate 8MB. 14039b6b563cSPaul Mackerras * Anton 14049b6b563cSPaul Mackerras */ 14059b6b563cSPaul Mackerras if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p)) 14069b6b563cSPaul Mackerras minsize = 8UL << 20; 14079b6b563cSPaul Mackerras else 14089b6b563cSPaul Mackerras minsize = 4UL << 20; 14099b6b563cSPaul Mackerras 14109b6b563cSPaul Mackerras /* Align to the greater of the align or size */ 14119b6b563cSPaul Mackerras align = max(minalign, minsize); 14129b6b563cSPaul Mackerras base = alloc_down(minsize, align, 1); 14139b6b563cSPaul Mackerras if (base == 0) 14149b6b563cSPaul Mackerras prom_panic("ERROR, cannot find space for TCE table.\n"); 14159b6b563cSPaul Mackerras if (base < local_alloc_bottom) 14169b6b563cSPaul Mackerras local_alloc_bottom = base; 14179b6b563cSPaul Mackerras 14189b6b563cSPaul Mackerras /* It seems OF doesn't null-terminate the path :-( */ 1419aca71ef8SLi Zefan memset(path, 0, PROM_SCRATCH_SIZE); 14209b6b563cSPaul Mackerras /* Call OF to setup the TCE hardware */ 14219b6b563cSPaul Mackerras if (call_prom("package-to-path", 3, 1, node, 14229b6b563cSPaul Mackerras path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) { 14239b6b563cSPaul Mackerras prom_printf("package-to-path failed\n"); 14249b6b563cSPaul Mackerras } 14259b6b563cSPaul Mackerras 1426a23414beSPaul Mackerras /* Save away the TCE table attributes for later use. */ 1427a23414beSPaul Mackerras prom_setprop(node, path, "linux,tce-base", &base, sizeof(base)); 1428a23414beSPaul Mackerras prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize)); 1429a23414beSPaul Mackerras 14309b6b563cSPaul Mackerras prom_debug("TCE table: %s\n", path); 14319b6b563cSPaul Mackerras prom_debug("\tnode = 0x%x\n", node); 14329b6b563cSPaul Mackerras prom_debug("\tbase = 0x%x\n", base); 14339b6b563cSPaul Mackerras prom_debug("\tsize = 0x%x\n", minsize); 14349b6b563cSPaul Mackerras 14359b6b563cSPaul Mackerras /* Initialize the table to have a one-to-one mapping 14369b6b563cSPaul Mackerras * over the allocated size. 14379b6b563cSPaul Mackerras */ 14382b931fb6SIngo Molnar tce_entryp = (u64 *)base; 14399b6b563cSPaul Mackerras for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) { 14409b6b563cSPaul Mackerras tce_entry = (i << PAGE_SHIFT); 14419b6b563cSPaul Mackerras tce_entry |= 0x3; 14429b6b563cSPaul Mackerras *tce_entryp = tce_entry; 14439b6b563cSPaul Mackerras } 14449b6b563cSPaul Mackerras 14459b6b563cSPaul Mackerras prom_printf("opening PHB %s", path); 14469b6b563cSPaul Mackerras phb_node = call_prom("open", 1, 1, path); 14479b6b563cSPaul Mackerras if (phb_node == 0) 14489b6b563cSPaul Mackerras prom_printf("... failed\n"); 14499b6b563cSPaul Mackerras else 14509b6b563cSPaul Mackerras prom_printf("... done\n"); 14519b6b563cSPaul Mackerras 14529b6b563cSPaul Mackerras call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"), 14539b6b563cSPaul Mackerras phb_node, -1, minsize, 14549b6b563cSPaul Mackerras (u32) base, (u32) (base >> 32)); 14559b6b563cSPaul Mackerras call_prom("close", 1, 0, phb_node); 14569b6b563cSPaul Mackerras } 14579b6b563cSPaul Mackerras 14589b6b563cSPaul Mackerras reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom); 14599b6b563cSPaul Mackerras 14602babf5c2SMichael Ellerman /* These are only really needed if there is a memory limit in 14612babf5c2SMichael Ellerman * effect, but we don't know so export them always. */ 14622babf5c2SMichael Ellerman RELOC(prom_tce_alloc_start) = local_alloc_bottom; 14639b6b563cSPaul Mackerras RELOC(prom_tce_alloc_end) = local_alloc_top; 14649b6b563cSPaul Mackerras 14659b6b563cSPaul Mackerras /* Flag the first invalid entry */ 14669b6b563cSPaul Mackerras prom_debug("ending prom_initialize_tce_table\n"); 14679b6b563cSPaul Mackerras } 14689b6b563cSPaul Mackerras #endif 14699b6b563cSPaul Mackerras 14709b6b563cSPaul Mackerras /* 14719b6b563cSPaul Mackerras * With CHRP SMP we need to use the OF to start the other processors. 14729b6b563cSPaul Mackerras * We can't wait until smp_boot_cpus (the OF is trashed by then) 14739b6b563cSPaul Mackerras * so we have to put the processors into a holding pattern controlled 14749b6b563cSPaul Mackerras * by the kernel (not OF) before we destroy the OF. 14759b6b563cSPaul Mackerras * 14769b6b563cSPaul Mackerras * This uses a chunk of low memory, puts some holding pattern 14779b6b563cSPaul Mackerras * code there and sends the other processors off to there until 14789b6b563cSPaul Mackerras * smp_boot_cpus tells them to do something. The holding pattern 14799b6b563cSPaul Mackerras * checks that address until its cpu # is there, when it is that 14809b6b563cSPaul Mackerras * cpu jumps to __secondary_start(). smp_boot_cpus() takes care 14819b6b563cSPaul Mackerras * of setting those values. 14829b6b563cSPaul Mackerras * 14839b6b563cSPaul Mackerras * We also use physical address 0x4 here to tell when a cpu 14849b6b563cSPaul Mackerras * is in its holding pattern code. 14859b6b563cSPaul Mackerras * 14869b6b563cSPaul Mackerras * -- Cort 14879b6b563cSPaul Mackerras */ 1488bbd0abdaSPaul Mackerras /* 1489bbd0abdaSPaul Mackerras * We want to reference the copy of __secondary_hold_* in the 1490bbd0abdaSPaul Mackerras * 0 - 0x100 address range 1491bbd0abdaSPaul Mackerras */ 1492bbd0abdaSPaul Mackerras #define LOW_ADDR(x) (((unsigned long) &(x)) & 0xff) 1493bbd0abdaSPaul Mackerras 14949b6b563cSPaul Mackerras static void __init prom_hold_cpus(void) 14959b6b563cSPaul Mackerras { 14969b6b563cSPaul Mackerras unsigned long i; 14979b6b563cSPaul Mackerras unsigned int reg; 14989b6b563cSPaul Mackerras phandle node; 14999b6b563cSPaul Mackerras char type[64]; 1500bbd0abdaSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 15019b6b563cSPaul Mackerras unsigned long *spinloop 1502bbd0abdaSPaul Mackerras = (void *) LOW_ADDR(__secondary_hold_spinloop); 15039b6b563cSPaul Mackerras unsigned long *acknowledge 1504bbd0abdaSPaul Mackerras = (void *) LOW_ADDR(__secondary_hold_acknowledge); 1505bbd0abdaSPaul Mackerras unsigned long secondary_hold = LOW_ADDR(__secondary_hold); 15069b6b563cSPaul Mackerras 15079b6b563cSPaul Mackerras prom_debug("prom_hold_cpus: start...\n"); 15089b6b563cSPaul Mackerras prom_debug(" 1) spinloop = 0x%x\n", (unsigned long)spinloop); 15099b6b563cSPaul Mackerras prom_debug(" 1) *spinloop = 0x%x\n", *spinloop); 15109b6b563cSPaul Mackerras prom_debug(" 1) acknowledge = 0x%x\n", 15119b6b563cSPaul Mackerras (unsigned long)acknowledge); 15129b6b563cSPaul Mackerras prom_debug(" 1) *acknowledge = 0x%x\n", *acknowledge); 15139b6b563cSPaul Mackerras prom_debug(" 1) secondary_hold = 0x%x\n", secondary_hold); 15149b6b563cSPaul Mackerras 15159b6b563cSPaul Mackerras /* Set the common spinloop variable, so all of the secondary cpus 15169b6b563cSPaul Mackerras * will block when they are awakened from their OF spinloop. 15179b6b563cSPaul Mackerras * This must occur for both SMP and non SMP kernels, since OF will 15189b6b563cSPaul Mackerras * be trashed when we move the kernel. 15199b6b563cSPaul Mackerras */ 15209b6b563cSPaul Mackerras *spinloop = 0; 15219b6b563cSPaul Mackerras 15229b6b563cSPaul Mackerras /* look for cpus */ 15239b6b563cSPaul Mackerras for (node = 0; prom_next_node(&node); ) { 15249b6b563cSPaul Mackerras type[0] = 0; 15259b6b563cSPaul Mackerras prom_getprop(node, "device_type", type, sizeof(type)); 15269b6b563cSPaul Mackerras if (strcmp(type, RELOC("cpu")) != 0) 15279b6b563cSPaul Mackerras continue; 15289b6b563cSPaul Mackerras 15299b6b563cSPaul Mackerras /* Skip non-configured cpus. */ 15309b6b563cSPaul Mackerras if (prom_getprop(node, "status", type, sizeof(type)) > 0) 15319b6b563cSPaul Mackerras if (strcmp(type, RELOC("okay")) != 0) 15329b6b563cSPaul Mackerras continue; 15339b6b563cSPaul Mackerras 15349b6b563cSPaul Mackerras reg = -1; 15359b6b563cSPaul Mackerras prom_getprop(node, "reg", ®, sizeof(reg)); 15369b6b563cSPaul Mackerras 15372c48a7d6SMichael Neuling prom_debug("cpu hw idx = %lu\n", reg); 15389b6b563cSPaul Mackerras 15399b6b563cSPaul Mackerras /* Init the acknowledge var which will be reset by 15409b6b563cSPaul Mackerras * the secondary cpu when it awakens from its OF 15419b6b563cSPaul Mackerras * spinloop. 15429b6b563cSPaul Mackerras */ 15439b6b563cSPaul Mackerras *acknowledge = (unsigned long)-1; 15449b6b563cSPaul Mackerras 15457d2f6075SNathan Lynch if (reg != _prom->cpu) { 15469b6b563cSPaul Mackerras /* Primary Thread of non-boot cpu */ 15472c48a7d6SMichael Neuling prom_printf("starting cpu hw idx %lu... ", reg); 15489b6b563cSPaul Mackerras call_prom("start-cpu", 3, 0, node, 15499b6b563cSPaul Mackerras secondary_hold, reg); 15509b6b563cSPaul Mackerras 15519b6b563cSPaul Mackerras for (i = 0; (i < 100000000) && 15529b6b563cSPaul Mackerras (*acknowledge == ((unsigned long)-1)); i++ ) 15539b6b563cSPaul Mackerras mb(); 15549b6b563cSPaul Mackerras 1555bbd0abdaSPaul Mackerras if (*acknowledge == reg) 15569b6b563cSPaul Mackerras prom_printf("done\n"); 1557bbd0abdaSPaul Mackerras else 15589b6b563cSPaul Mackerras prom_printf("failed: %x\n", *acknowledge); 15599b6b563cSPaul Mackerras } 15609b6b563cSPaul Mackerras #ifdef CONFIG_SMP 15619b6b563cSPaul Mackerras else 15622c48a7d6SMichael Neuling prom_printf("boot cpu hw idx %lu\n", reg); 15639b6b563cSPaul Mackerras #endif /* CONFIG_SMP */ 15649b6b563cSPaul Mackerras } 15659b6b563cSPaul Mackerras 15669b6b563cSPaul Mackerras prom_debug("prom_hold_cpus: end...\n"); 15679b6b563cSPaul Mackerras } 15689b6b563cSPaul Mackerras 15699b6b563cSPaul Mackerras 15709b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp) 15719b6b563cSPaul Mackerras { 15729b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 15739b6b563cSPaul Mackerras 15749b6b563cSPaul Mackerras /* Get a handle to the prom entry point before anything else */ 15759b6b563cSPaul Mackerras RELOC(prom_entry) = pp; 15769b6b563cSPaul Mackerras 15779b6b563cSPaul Mackerras /* get a handle for the stdout device */ 15789b6b563cSPaul Mackerras _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen")); 15799b6b563cSPaul Mackerras if (!PHANDLE_VALID(_prom->chosen)) 15809b6b563cSPaul Mackerras prom_panic("cannot find chosen"); /* msg won't be printed :( */ 15819b6b563cSPaul Mackerras 15829b6b563cSPaul Mackerras /* get device tree root */ 15839b6b563cSPaul Mackerras _prom->root = call_prom("finddevice", 1, 1, ADDR("/")); 15849b6b563cSPaul Mackerras if (!PHANDLE_VALID(_prom->root)) 15859b6b563cSPaul Mackerras prom_panic("cannot find device tree root"); /* msg won't be printed :( */ 1586a575b807SPaul Mackerras 1587a575b807SPaul Mackerras _prom->mmumap = 0; 15889b6b563cSPaul Mackerras } 15899b6b563cSPaul Mackerras 1590a575b807SPaul Mackerras #ifdef CONFIG_PPC32 1591a575b807SPaul Mackerras /* 1592a575b807SPaul Mackerras * For really old powermacs, we need to map things we claim. 1593a575b807SPaul Mackerras * For that, we need the ihandle of the mmu. 1594a23414beSPaul Mackerras * Also, on the longtrail, we need to work around other bugs. 1595a575b807SPaul Mackerras */ 1596a575b807SPaul Mackerras static void __init prom_find_mmu(void) 1597a575b807SPaul Mackerras { 1598a575b807SPaul Mackerras struct prom_t *_prom = &RELOC(prom); 1599a575b807SPaul Mackerras phandle oprom; 1600a575b807SPaul Mackerras char version[64]; 1601a575b807SPaul Mackerras 1602a575b807SPaul Mackerras oprom = call_prom("finddevice", 1, 1, ADDR("/openprom")); 1603a575b807SPaul Mackerras if (!PHANDLE_VALID(oprom)) 1604a575b807SPaul Mackerras return; 1605a575b807SPaul Mackerras if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0) 1606a575b807SPaul Mackerras return; 1607a575b807SPaul Mackerras version[sizeof(version) - 1] = 0; 1608a575b807SPaul Mackerras /* XXX might need to add other versions here */ 1609a23414beSPaul Mackerras if (strcmp(version, "Open Firmware, 1.0.5") == 0) 1610a23414beSPaul Mackerras of_workarounds = OF_WA_CLAIM; 1611a23414beSPaul Mackerras else if (strncmp(version, "FirmWorks,3.", 12) == 0) { 1612a23414beSPaul Mackerras of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL; 1613a23414beSPaul Mackerras call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim"); 1614a23414beSPaul Mackerras } else 1615a575b807SPaul Mackerras return; 1616a23414beSPaul Mackerras _prom->memory = call_prom("open", 1, 1, ADDR("/memory")); 1617a575b807SPaul Mackerras prom_getprop(_prom->chosen, "mmu", &_prom->mmumap, 1618a575b807SPaul Mackerras sizeof(_prom->mmumap)); 1619a23414beSPaul Mackerras if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap)) 1620a23414beSPaul Mackerras of_workarounds &= ~OF_WA_CLAIM; /* hmmm */ 1621a575b807SPaul Mackerras } 1622a575b807SPaul Mackerras #else 1623a575b807SPaul Mackerras #define prom_find_mmu() 1624a575b807SPaul Mackerras #endif 1625a575b807SPaul Mackerras 16269b6b563cSPaul Mackerras static void __init prom_init_stdout(void) 16279b6b563cSPaul Mackerras { 16289b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 16299b6b563cSPaul Mackerras char *path = RELOC(of_stdout_device); 16309b6b563cSPaul Mackerras char type[16]; 16319b6b563cSPaul Mackerras u32 val; 16329b6b563cSPaul Mackerras 16339b6b563cSPaul Mackerras if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0) 16349b6b563cSPaul Mackerras prom_panic("cannot find stdout"); 16359b6b563cSPaul Mackerras 16369b6b563cSPaul Mackerras _prom->stdout = val; 16379b6b563cSPaul Mackerras 16389b6b563cSPaul Mackerras /* Get the full OF pathname of the stdout device */ 16399b6b563cSPaul Mackerras memset(path, 0, 256); 16409b6b563cSPaul Mackerras call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255); 16419b6b563cSPaul Mackerras val = call_prom("instance-to-package", 1, 1, _prom->stdout); 1642a23414beSPaul Mackerras prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package", 1643a23414beSPaul Mackerras &val, sizeof(val)); 16449b6b563cSPaul Mackerras prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device)); 1645a23414beSPaul Mackerras prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path", 1646a23414beSPaul Mackerras path, strlen(path) + 1); 16479b6b563cSPaul Mackerras 16489b6b563cSPaul Mackerras /* If it's a display, note it */ 16499b6b563cSPaul Mackerras memset(type, 0, sizeof(type)); 16509b6b563cSPaul Mackerras prom_getprop(val, "device_type", type, sizeof(type)); 16519b6b563cSPaul Mackerras if (strcmp(type, RELOC("display")) == 0) 1652a23414beSPaul Mackerras prom_setprop(val, path, "linux,boot-display", NULL, 0); 16539b6b563cSPaul Mackerras } 16549b6b563cSPaul Mackerras 16559b6b563cSPaul Mackerras static void __init prom_close_stdin(void) 16569b6b563cSPaul Mackerras { 16579b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 16589b6b563cSPaul Mackerras ihandle val; 16599b6b563cSPaul Mackerras 16609b6b563cSPaul Mackerras if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0) 16619b6b563cSPaul Mackerras call_prom("close", 1, 0, val); 16629b6b563cSPaul Mackerras } 16639b6b563cSPaul Mackerras 16649b6b563cSPaul Mackerras static int __init prom_find_machine_type(void) 16659b6b563cSPaul Mackerras { 16669b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 16679b6b563cSPaul Mackerras char compat[256]; 16689b6b563cSPaul Mackerras int len, i = 0; 166921fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64 16709b6b563cSPaul Mackerras phandle rtas; 1671e8222502SBenjamin Herrenschmidt int x; 167221fe3301SBenjamin Herrenschmidt #endif 1673e8222502SBenjamin Herrenschmidt 1674e8222502SBenjamin Herrenschmidt /* Look for a PowerMac */ 16759b6b563cSPaul Mackerras len = prom_getprop(_prom->root, "compatible", 16769b6b563cSPaul Mackerras compat, sizeof(compat)-1); 16779b6b563cSPaul Mackerras if (len > 0) { 16789b6b563cSPaul Mackerras compat[len] = 0; 16799b6b563cSPaul Mackerras while (i < len) { 16809b6b563cSPaul Mackerras char *p = &compat[i]; 16819b6b563cSPaul Mackerras int sl = strlen(p); 16829b6b563cSPaul Mackerras if (sl == 0) 16839b6b563cSPaul Mackerras break; 16849b6b563cSPaul Mackerras if (strstr(p, RELOC("Power Macintosh")) || 1685a575b807SPaul Mackerras strstr(p, RELOC("MacRISC"))) 16869b6b563cSPaul Mackerras return PLATFORM_POWERMAC; 1687133dda1eSArnd Bergmann #ifdef CONFIG_PPC64 1688133dda1eSArnd Bergmann /* We must make sure we don't detect the IBM Cell 1689133dda1eSArnd Bergmann * blades as pSeries due to some firmware issues, 1690133dda1eSArnd Bergmann * so we do it here. 1691133dda1eSArnd Bergmann */ 1692133dda1eSArnd Bergmann if (strstr(p, RELOC("IBM,CBEA")) || 1693133dda1eSArnd Bergmann strstr(p, RELOC("IBM,CPBW-1.0"))) 1694133dda1eSArnd Bergmann return PLATFORM_GENERIC; 1695133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */ 16969b6b563cSPaul Mackerras i += sl + 1; 16979b6b563cSPaul Mackerras } 16989b6b563cSPaul Mackerras } 16999b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 1700e8222502SBenjamin Herrenschmidt /* If not a mac, try to figure out if it's an IBM pSeries or any other 1701e8222502SBenjamin Herrenschmidt * PAPR compliant platform. We assume it is if : 1702e8222502SBenjamin Herrenschmidt * - /device_type is "chrp" (please, do NOT use that for future 1703e8222502SBenjamin Herrenschmidt * non-IBM designs ! 1704e8222502SBenjamin Herrenschmidt * - it has /rtas 1705e8222502SBenjamin Herrenschmidt */ 17066f806ceeSMichael Ellerman len = prom_getprop(_prom->root, "device_type", 1707e8222502SBenjamin Herrenschmidt compat, sizeof(compat)-1); 1708e8222502SBenjamin Herrenschmidt if (len <= 0) 1709e8222502SBenjamin Herrenschmidt return PLATFORM_GENERIC; 1710cb6b2eb9SBenjamin Herrenschmidt if (strcmp(compat, RELOC("chrp"))) 1711e8222502SBenjamin Herrenschmidt return PLATFORM_GENERIC; 1712e8222502SBenjamin Herrenschmidt 17139b6b563cSPaul Mackerras /* Default to pSeries. We need to know if we are running LPAR */ 17149b6b563cSPaul Mackerras rtas = call_prom("finddevice", 1, 1, ADDR("/rtas")); 1715e8222502SBenjamin Herrenschmidt if (!PHANDLE_VALID(rtas)) 1716e8222502SBenjamin Herrenschmidt return PLATFORM_GENERIC; 1717e8222502SBenjamin Herrenschmidt x = prom_getproplen(rtas, "ibm,hypertas-functions"); 17189b6b563cSPaul Mackerras if (x != PROM_ERROR) { 17194da727aeSAnton Blanchard prom_debug("Hypertas detected, assuming LPAR !\n"); 17209b6b563cSPaul Mackerras return PLATFORM_PSERIES_LPAR; 17219b6b563cSPaul Mackerras } 17229b6b563cSPaul Mackerras return PLATFORM_PSERIES; 17239b6b563cSPaul Mackerras #else 1724e8222502SBenjamin Herrenschmidt return PLATFORM_GENERIC; 17259b6b563cSPaul Mackerras #endif 17269b6b563cSPaul Mackerras } 17279b6b563cSPaul Mackerras 17289b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b) 17299b6b563cSPaul Mackerras { 17309b6b563cSPaul Mackerras return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r); 17319b6b563cSPaul Mackerras } 17329b6b563cSPaul Mackerras 17339b6b563cSPaul Mackerras /* 17349b6b563cSPaul Mackerras * If we have a display that we don't know how to drive, 17359b6b563cSPaul Mackerras * we will want to try to execute OF's open method for it 17369b6b563cSPaul Mackerras * later. However, OF will probably fall over if we do that 17379b6b563cSPaul Mackerras * we've taken over the MMU. 17389b6b563cSPaul Mackerras * So we check whether we will need to open the display, 17399b6b563cSPaul Mackerras * and if so, open it now. 17409b6b563cSPaul Mackerras */ 17419b6b563cSPaul Mackerras static void __init prom_check_displays(void) 17429b6b563cSPaul Mackerras { 17439b6b563cSPaul Mackerras char type[16], *path; 17449b6b563cSPaul Mackerras phandle node; 17459b6b563cSPaul Mackerras ihandle ih; 17469b6b563cSPaul Mackerras int i; 17479b6b563cSPaul Mackerras 17489b6b563cSPaul Mackerras static unsigned char default_colors[] = { 17499b6b563cSPaul Mackerras 0x00, 0x00, 0x00, 17509b6b563cSPaul Mackerras 0x00, 0x00, 0xaa, 17519b6b563cSPaul Mackerras 0x00, 0xaa, 0x00, 17529b6b563cSPaul Mackerras 0x00, 0xaa, 0xaa, 17539b6b563cSPaul Mackerras 0xaa, 0x00, 0x00, 17549b6b563cSPaul Mackerras 0xaa, 0x00, 0xaa, 17559b6b563cSPaul Mackerras 0xaa, 0xaa, 0x00, 17569b6b563cSPaul Mackerras 0xaa, 0xaa, 0xaa, 17579b6b563cSPaul Mackerras 0x55, 0x55, 0x55, 17589b6b563cSPaul Mackerras 0x55, 0x55, 0xff, 17599b6b563cSPaul Mackerras 0x55, 0xff, 0x55, 17609b6b563cSPaul Mackerras 0x55, 0xff, 0xff, 17619b6b563cSPaul Mackerras 0xff, 0x55, 0x55, 17629b6b563cSPaul Mackerras 0xff, 0x55, 0xff, 17639b6b563cSPaul Mackerras 0xff, 0xff, 0x55, 17649b6b563cSPaul Mackerras 0xff, 0xff, 0xff 17659b6b563cSPaul Mackerras }; 17669b6b563cSPaul Mackerras const unsigned char *clut; 17679b6b563cSPaul Mackerras 17684da727aeSAnton Blanchard prom_debug("Looking for displays\n"); 17699b6b563cSPaul Mackerras for (node = 0; prom_next_node(&node); ) { 17709b6b563cSPaul Mackerras memset(type, 0, sizeof(type)); 17719b6b563cSPaul Mackerras prom_getprop(node, "device_type", type, sizeof(type)); 17729b6b563cSPaul Mackerras if (strcmp(type, RELOC("display")) != 0) 17739b6b563cSPaul Mackerras continue; 17749b6b563cSPaul Mackerras 17759b6b563cSPaul Mackerras /* It seems OF doesn't null-terminate the path :-( */ 17769b6b563cSPaul Mackerras path = RELOC(prom_scratch); 17779b6b563cSPaul Mackerras memset(path, 0, PROM_SCRATCH_SIZE); 17789b6b563cSPaul Mackerras 17799b6b563cSPaul Mackerras /* 17809b6b563cSPaul Mackerras * leave some room at the end of the path for appending extra 17819b6b563cSPaul Mackerras * arguments 17829b6b563cSPaul Mackerras */ 17839b6b563cSPaul Mackerras if (call_prom("package-to-path", 3, 1, node, path, 17849b6b563cSPaul Mackerras PROM_SCRATCH_SIZE-10) == PROM_ERROR) 17859b6b563cSPaul Mackerras continue; 17869b6b563cSPaul Mackerras prom_printf("found display : %s, opening... ", path); 17879b6b563cSPaul Mackerras 17889b6b563cSPaul Mackerras ih = call_prom("open", 1, 1, path); 17899b6b563cSPaul Mackerras if (ih == 0) { 17909b6b563cSPaul Mackerras prom_printf("failed\n"); 17919b6b563cSPaul Mackerras continue; 17929b6b563cSPaul Mackerras } 17939b6b563cSPaul Mackerras 17949b6b563cSPaul Mackerras /* Success */ 17959b6b563cSPaul Mackerras prom_printf("done\n"); 1796a23414beSPaul Mackerras prom_setprop(node, path, "linux,opened", NULL, 0); 17979b6b563cSPaul Mackerras 17989b6b563cSPaul Mackerras /* Setup a usable color table when the appropriate 17999b6b563cSPaul Mackerras * method is available. Should update this to set-colors */ 18009b6b563cSPaul Mackerras clut = RELOC(default_colors); 18019b6b563cSPaul Mackerras for (i = 0; i < 32; i++, clut += 3) 18029b6b563cSPaul Mackerras if (prom_set_color(ih, i, clut[0], clut[1], 18039b6b563cSPaul Mackerras clut[2]) != 0) 18049b6b563cSPaul Mackerras break; 18059b6b563cSPaul Mackerras 18069b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224 18079b6b563cSPaul Mackerras clut = PTRRELOC(RELOC(logo_linux_clut224.clut)); 18089b6b563cSPaul Mackerras for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3) 18099b6b563cSPaul Mackerras if (prom_set_color(ih, i + 32, clut[0], clut[1], 18109b6b563cSPaul Mackerras clut[2]) != 0) 18119b6b563cSPaul Mackerras break; 18129b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */ 18139b6b563cSPaul Mackerras } 18149b6b563cSPaul Mackerras } 18159b6b563cSPaul Mackerras 18169b6b563cSPaul Mackerras 18179b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */ 18189b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end, 18199b6b563cSPaul Mackerras unsigned long needed, unsigned long align) 18209b6b563cSPaul Mackerras { 18219b6b563cSPaul Mackerras void *ret; 18229b6b563cSPaul Mackerras 18239b6b563cSPaul Mackerras *mem_start = _ALIGN(*mem_start, align); 18249b6b563cSPaul Mackerras while ((*mem_start + needed) > *mem_end) { 18259b6b563cSPaul Mackerras unsigned long room, chunk; 18269b6b563cSPaul Mackerras 18279b6b563cSPaul Mackerras prom_debug("Chunk exhausted, claiming more at %x...\n", 18289b6b563cSPaul Mackerras RELOC(alloc_bottom)); 18299b6b563cSPaul Mackerras room = RELOC(alloc_top) - RELOC(alloc_bottom); 18309b6b563cSPaul Mackerras if (room > DEVTREE_CHUNK_SIZE) 18319b6b563cSPaul Mackerras room = DEVTREE_CHUNK_SIZE; 18329b6b563cSPaul Mackerras if (room < PAGE_SIZE) 18339b6b563cSPaul Mackerras prom_panic("No memory for flatten_device_tree (no room)"); 18349b6b563cSPaul Mackerras chunk = alloc_up(room, 0); 18359b6b563cSPaul Mackerras if (chunk == 0) 18369b6b563cSPaul Mackerras prom_panic("No memory for flatten_device_tree (claim failed)"); 1837*966728ddSAnton Blanchard *mem_end = chunk + room; 18389b6b563cSPaul Mackerras } 18399b6b563cSPaul Mackerras 18409b6b563cSPaul Mackerras ret = (void *)*mem_start; 18419b6b563cSPaul Mackerras *mem_start += needed; 18429b6b563cSPaul Mackerras 18439b6b563cSPaul Mackerras return ret; 18449b6b563cSPaul Mackerras } 18459b6b563cSPaul Mackerras 18469b6b563cSPaul Mackerras #define dt_push_token(token, mem_start, mem_end) \ 18479b6b563cSPaul Mackerras do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0) 18489b6b563cSPaul Mackerras 18499b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str) 18509b6b563cSPaul Mackerras { 18519b6b563cSPaul Mackerras char *s, *os; 18529b6b563cSPaul Mackerras 18539b6b563cSPaul Mackerras s = os = (char *)RELOC(dt_string_start); 18549b6b563cSPaul Mackerras s += 4; 18559b6b563cSPaul Mackerras while (s < (char *)RELOC(dt_string_end)) { 18569b6b563cSPaul Mackerras if (strcmp(s, str) == 0) 18579b6b563cSPaul Mackerras return s - os; 18589b6b563cSPaul Mackerras s += strlen(s) + 1; 18599b6b563cSPaul Mackerras } 18609b6b563cSPaul Mackerras return 0; 18619b6b563cSPaul Mackerras } 18629b6b563cSPaul Mackerras 18639b6b563cSPaul Mackerras /* 18649b6b563cSPaul Mackerras * The Open Firmware 1275 specification states properties must be 31 bytes or 18659b6b563cSPaul Mackerras * less, however not all firmwares obey this. Make it 64 bytes to be safe. 18669b6b563cSPaul Mackerras */ 18679b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64 18689b6b563cSPaul Mackerras 18699b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node, 18709b6b563cSPaul Mackerras unsigned long *mem_start, 18719b6b563cSPaul Mackerras unsigned long *mem_end) 18729b6b563cSPaul Mackerras { 18739b6b563cSPaul Mackerras char *prev_name, *namep, *sstart; 18749b6b563cSPaul Mackerras unsigned long soff; 18759b6b563cSPaul Mackerras phandle child; 18769b6b563cSPaul Mackerras 18779b6b563cSPaul Mackerras sstart = (char *)RELOC(dt_string_start); 18789b6b563cSPaul Mackerras 18799b6b563cSPaul Mackerras /* get and store all property names */ 18809b6b563cSPaul Mackerras prev_name = RELOC(""); 18819b6b563cSPaul Mackerras for (;;) { 18829b6b563cSPaul Mackerras /* 64 is max len of name including nul. */ 18839b6b563cSPaul Mackerras namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1); 18849b6b563cSPaul Mackerras if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) { 18859b6b563cSPaul Mackerras /* No more nodes: unwind alloc */ 18869b6b563cSPaul Mackerras *mem_start = (unsigned long)namep; 18879b6b563cSPaul Mackerras break; 18889b6b563cSPaul Mackerras } 18899b6b563cSPaul Mackerras 18909b6b563cSPaul Mackerras /* skip "name" */ 18919b6b563cSPaul Mackerras if (strcmp(namep, RELOC("name")) == 0) { 18929b6b563cSPaul Mackerras *mem_start = (unsigned long)namep; 18939b6b563cSPaul Mackerras prev_name = RELOC("name"); 18949b6b563cSPaul Mackerras continue; 18959b6b563cSPaul Mackerras } 18969b6b563cSPaul Mackerras /* get/create string entry */ 18979b6b563cSPaul Mackerras soff = dt_find_string(namep); 18989b6b563cSPaul Mackerras if (soff != 0) { 18999b6b563cSPaul Mackerras *mem_start = (unsigned long)namep; 19009b6b563cSPaul Mackerras namep = sstart + soff; 19019b6b563cSPaul Mackerras } else { 19029b6b563cSPaul Mackerras /* Trim off some if we can */ 19039b6b563cSPaul Mackerras *mem_start = (unsigned long)namep + strlen(namep) + 1; 19049b6b563cSPaul Mackerras RELOC(dt_string_end) = *mem_start; 19059b6b563cSPaul Mackerras } 19069b6b563cSPaul Mackerras prev_name = namep; 19079b6b563cSPaul Mackerras } 19089b6b563cSPaul Mackerras 19099b6b563cSPaul Mackerras /* do all our children */ 19109b6b563cSPaul Mackerras child = call_prom("child", 1, 1, node); 19119b6b563cSPaul Mackerras while (child != 0) { 19129b6b563cSPaul Mackerras scan_dt_build_strings(child, mem_start, mem_end); 19139b6b563cSPaul Mackerras child = call_prom("peer", 1, 1, child); 19149b6b563cSPaul Mackerras } 19159b6b563cSPaul Mackerras } 19169b6b563cSPaul Mackerras 19179b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start, 19189b6b563cSPaul Mackerras unsigned long *mem_end) 19199b6b563cSPaul Mackerras { 19209b6b563cSPaul Mackerras phandle child; 19219b6b563cSPaul Mackerras char *namep, *prev_name, *sstart, *p, *ep, *lp, *path; 19229b6b563cSPaul Mackerras unsigned long soff; 19239b6b563cSPaul Mackerras unsigned char *valp; 19249b6b563cSPaul Mackerras static char pname[MAX_PROPERTY_NAME]; 1925c4988820SPaul Mackerras int l, room; 19269b6b563cSPaul Mackerras 19279b6b563cSPaul Mackerras dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end); 19289b6b563cSPaul Mackerras 19299b6b563cSPaul Mackerras /* get the node's full name */ 19309b6b563cSPaul Mackerras namep = (char *)*mem_start; 1931c4988820SPaul Mackerras room = *mem_end - *mem_start; 1932c4988820SPaul Mackerras if (room > 255) 1933c4988820SPaul Mackerras room = 255; 1934c4988820SPaul Mackerras l = call_prom("package-to-path", 3, 1, node, namep, room); 19359b6b563cSPaul Mackerras if (l >= 0) { 19369b6b563cSPaul Mackerras /* Didn't fit? Get more room. */ 1937c4988820SPaul Mackerras if (l >= room) { 1938c4988820SPaul Mackerras if (l >= *mem_end - *mem_start) 19399b6b563cSPaul Mackerras namep = make_room(mem_start, mem_end, l+1, 1); 19409b6b563cSPaul Mackerras call_prom("package-to-path", 3, 1, node, namep, l); 19419b6b563cSPaul Mackerras } 19429b6b563cSPaul Mackerras namep[l] = '\0'; 19439b6b563cSPaul Mackerras 19449b6b563cSPaul Mackerras /* Fixup an Apple bug where they have bogus \0 chars in the 1945a575b807SPaul Mackerras * middle of the path in some properties, and extract 1946a575b807SPaul Mackerras * the unit name (everything after the last '/'). 19479b6b563cSPaul Mackerras */ 1948a575b807SPaul Mackerras for (lp = p = namep, ep = namep + l; p < ep; p++) { 19499b6b563cSPaul Mackerras if (*p == '/') 1950a575b807SPaul Mackerras lp = namep; 1951a575b807SPaul Mackerras else if (*p != 0) 1952a575b807SPaul Mackerras *lp++ = *p; 1953a575b807SPaul Mackerras } 1954a575b807SPaul Mackerras *lp = 0; 1955a575b807SPaul Mackerras *mem_start = _ALIGN((unsigned long)lp + 1, 4); 19569b6b563cSPaul Mackerras } 19579b6b563cSPaul Mackerras 19589b6b563cSPaul Mackerras /* get it again for debugging */ 19599b6b563cSPaul Mackerras path = RELOC(prom_scratch); 19609b6b563cSPaul Mackerras memset(path, 0, PROM_SCRATCH_SIZE); 19619b6b563cSPaul Mackerras call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1); 19629b6b563cSPaul Mackerras 19639b6b563cSPaul Mackerras /* get and store all properties */ 19649b6b563cSPaul Mackerras prev_name = RELOC(""); 19659b6b563cSPaul Mackerras sstart = (char *)RELOC(dt_string_start); 19669b6b563cSPaul Mackerras for (;;) { 19679b6b563cSPaul Mackerras if (call_prom("nextprop", 3, 1, node, prev_name, 19689b6b563cSPaul Mackerras RELOC(pname)) != 1) 19699b6b563cSPaul Mackerras break; 19709b6b563cSPaul Mackerras 19719b6b563cSPaul Mackerras /* skip "name" */ 19729b6b563cSPaul Mackerras if (strcmp(RELOC(pname), RELOC("name")) == 0) { 19739b6b563cSPaul Mackerras prev_name = RELOC("name"); 19749b6b563cSPaul Mackerras continue; 19759b6b563cSPaul Mackerras } 19769b6b563cSPaul Mackerras 19779b6b563cSPaul Mackerras /* find string offset */ 19789b6b563cSPaul Mackerras soff = dt_find_string(RELOC(pname)); 19799b6b563cSPaul Mackerras if (soff == 0) { 19809b6b563cSPaul Mackerras prom_printf("WARNING: Can't find string index for" 19819b6b563cSPaul Mackerras " <%s>, node %s\n", RELOC(pname), path); 19829b6b563cSPaul Mackerras break; 19839b6b563cSPaul Mackerras } 19849b6b563cSPaul Mackerras prev_name = sstart + soff; 19859b6b563cSPaul Mackerras 19869b6b563cSPaul Mackerras /* get length */ 19879b6b563cSPaul Mackerras l = call_prom("getproplen", 2, 1, node, RELOC(pname)); 19889b6b563cSPaul Mackerras 19899b6b563cSPaul Mackerras /* sanity checks */ 19909b6b563cSPaul Mackerras if (l == PROM_ERROR) 19919b6b563cSPaul Mackerras continue; 19929b6b563cSPaul Mackerras if (l > MAX_PROPERTY_LENGTH) { 19939b6b563cSPaul Mackerras prom_printf("WARNING: ignoring large property "); 19949b6b563cSPaul Mackerras /* It seems OF doesn't null-terminate the path :-( */ 19959b6b563cSPaul Mackerras prom_printf("[%s] ", path); 19969b6b563cSPaul Mackerras prom_printf("%s length 0x%x\n", RELOC(pname), l); 19979b6b563cSPaul Mackerras continue; 19989b6b563cSPaul Mackerras } 19999b6b563cSPaul Mackerras 20009b6b563cSPaul Mackerras /* push property head */ 20019b6b563cSPaul Mackerras dt_push_token(OF_DT_PROP, mem_start, mem_end); 20029b6b563cSPaul Mackerras dt_push_token(l, mem_start, mem_end); 20039b6b563cSPaul Mackerras dt_push_token(soff, mem_start, mem_end); 20049b6b563cSPaul Mackerras 20059b6b563cSPaul Mackerras /* push property content */ 20069b6b563cSPaul Mackerras valp = make_room(mem_start, mem_end, l, 4); 20079b6b563cSPaul Mackerras call_prom("getprop", 4, 1, node, RELOC(pname), valp, l); 20089b6b563cSPaul Mackerras *mem_start = _ALIGN(*mem_start, 4); 20099b6b563cSPaul Mackerras } 20109b6b563cSPaul Mackerras 20119b6b563cSPaul Mackerras /* Add a "linux,phandle" property. */ 20129b6b563cSPaul Mackerras soff = dt_find_string(RELOC("linux,phandle")); 20139b6b563cSPaul Mackerras if (soff == 0) 20149b6b563cSPaul Mackerras prom_printf("WARNING: Can't find string index for" 20159b6b563cSPaul Mackerras " <linux-phandle> node %s\n", path); 20169b6b563cSPaul Mackerras else { 20179b6b563cSPaul Mackerras dt_push_token(OF_DT_PROP, mem_start, mem_end); 20189b6b563cSPaul Mackerras dt_push_token(4, mem_start, mem_end); 20199b6b563cSPaul Mackerras dt_push_token(soff, mem_start, mem_end); 20209b6b563cSPaul Mackerras valp = make_room(mem_start, mem_end, 4, 4); 20219b6b563cSPaul Mackerras *(u32 *)valp = node; 20229b6b563cSPaul Mackerras } 20239b6b563cSPaul Mackerras 20249b6b563cSPaul Mackerras /* do all our children */ 20259b6b563cSPaul Mackerras child = call_prom("child", 1, 1, node); 20269b6b563cSPaul Mackerras while (child != 0) { 20279b6b563cSPaul Mackerras scan_dt_build_struct(child, mem_start, mem_end); 20289b6b563cSPaul Mackerras child = call_prom("peer", 1, 1, child); 20299b6b563cSPaul Mackerras } 20309b6b563cSPaul Mackerras 20319b6b563cSPaul Mackerras dt_push_token(OF_DT_END_NODE, mem_start, mem_end); 20329b6b563cSPaul Mackerras } 20339b6b563cSPaul Mackerras 20349b6b563cSPaul Mackerras static void __init flatten_device_tree(void) 20359b6b563cSPaul Mackerras { 20369b6b563cSPaul Mackerras phandle root; 20379b6b563cSPaul Mackerras unsigned long mem_start, mem_end, room; 20389b6b563cSPaul Mackerras struct boot_param_header *hdr; 20399b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 20409b6b563cSPaul Mackerras char *namep; 20419b6b563cSPaul Mackerras u64 *rsvmap; 20429b6b563cSPaul Mackerras 20439b6b563cSPaul Mackerras /* 20449b6b563cSPaul Mackerras * Check how much room we have between alloc top & bottom (+/- a 20459b6b563cSPaul Mackerras * few pages), crop to 4Mb, as this is our "chuck" size 20469b6b563cSPaul Mackerras */ 20479b6b563cSPaul Mackerras room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000; 20489b6b563cSPaul Mackerras if (room > DEVTREE_CHUNK_SIZE) 20499b6b563cSPaul Mackerras room = DEVTREE_CHUNK_SIZE; 20509b6b563cSPaul Mackerras prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom)); 20519b6b563cSPaul Mackerras 20529b6b563cSPaul Mackerras /* Now try to claim that */ 20539b6b563cSPaul Mackerras mem_start = (unsigned long)alloc_up(room, PAGE_SIZE); 20549b6b563cSPaul Mackerras if (mem_start == 0) 20559b6b563cSPaul Mackerras prom_panic("Can't allocate initial device-tree chunk\n"); 2056*966728ddSAnton Blanchard mem_end = mem_start + room; 20579b6b563cSPaul Mackerras 20589b6b563cSPaul Mackerras /* Get root of tree */ 20599b6b563cSPaul Mackerras root = call_prom("peer", 1, 1, (phandle)0); 20609b6b563cSPaul Mackerras if (root == (phandle)0) 20619b6b563cSPaul Mackerras prom_panic ("couldn't get device tree root\n"); 20629b6b563cSPaul Mackerras 20639b6b563cSPaul Mackerras /* Build header and make room for mem rsv map */ 20649b6b563cSPaul Mackerras mem_start = _ALIGN(mem_start, 4); 20659b6b563cSPaul Mackerras hdr = make_room(&mem_start, &mem_end, 20669b6b563cSPaul Mackerras sizeof(struct boot_param_header), 4); 20679b6b563cSPaul Mackerras RELOC(dt_header_start) = (unsigned long)hdr; 20689b6b563cSPaul Mackerras rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8); 20699b6b563cSPaul Mackerras 20709b6b563cSPaul Mackerras /* Start of strings */ 20719b6b563cSPaul Mackerras mem_start = PAGE_ALIGN(mem_start); 20729b6b563cSPaul Mackerras RELOC(dt_string_start) = mem_start; 20739b6b563cSPaul Mackerras mem_start += 4; /* hole */ 20749b6b563cSPaul Mackerras 20759b6b563cSPaul Mackerras /* Add "linux,phandle" in there, we'll need it */ 20769b6b563cSPaul Mackerras namep = make_room(&mem_start, &mem_end, 16, 1); 20779b6b563cSPaul Mackerras strcpy(namep, RELOC("linux,phandle")); 20789b6b563cSPaul Mackerras mem_start = (unsigned long)namep + strlen(namep) + 1; 20799b6b563cSPaul Mackerras 20809b6b563cSPaul Mackerras /* Build string array */ 20819b6b563cSPaul Mackerras prom_printf("Building dt strings...\n"); 20829b6b563cSPaul Mackerras scan_dt_build_strings(root, &mem_start, &mem_end); 20839b6b563cSPaul Mackerras RELOC(dt_string_end) = mem_start; 20849b6b563cSPaul Mackerras 20859b6b563cSPaul Mackerras /* Build structure */ 20869b6b563cSPaul Mackerras mem_start = PAGE_ALIGN(mem_start); 20879b6b563cSPaul Mackerras RELOC(dt_struct_start) = mem_start; 20889b6b563cSPaul Mackerras prom_printf("Building dt structure...\n"); 20899b6b563cSPaul Mackerras scan_dt_build_struct(root, &mem_start, &mem_end); 20909b6b563cSPaul Mackerras dt_push_token(OF_DT_END, &mem_start, &mem_end); 20919b6b563cSPaul Mackerras RELOC(dt_struct_end) = PAGE_ALIGN(mem_start); 20929b6b563cSPaul Mackerras 20939b6b563cSPaul Mackerras /* Finish header */ 20949b6b563cSPaul Mackerras hdr->boot_cpuid_phys = _prom->cpu; 20959b6b563cSPaul Mackerras hdr->magic = OF_DT_HEADER; 20969b6b563cSPaul Mackerras hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start); 20979b6b563cSPaul Mackerras hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start); 20989b6b563cSPaul Mackerras hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start); 20999b6b563cSPaul Mackerras hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start); 21009b6b563cSPaul Mackerras hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start); 21019b6b563cSPaul Mackerras hdr->version = OF_DT_VERSION; 21029b6b563cSPaul Mackerras /* Version 16 is not backward compatible */ 21039b6b563cSPaul Mackerras hdr->last_comp_version = 0x10; 21049b6b563cSPaul Mackerras 21054d1f3f25SJimi Xenidis /* Copy the reserve map in */ 21069b6b563cSPaul Mackerras memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map)); 21079b6b563cSPaul Mackerras 21089b6b563cSPaul Mackerras #ifdef DEBUG_PROM 21099b6b563cSPaul Mackerras { 21109b6b563cSPaul Mackerras int i; 21119b6b563cSPaul Mackerras prom_printf("reserved memory map:\n"); 21129b6b563cSPaul Mackerras for (i = 0; i < RELOC(mem_reserve_cnt); i++) 21139b6b563cSPaul Mackerras prom_printf(" %x - %x\n", 21149b6b563cSPaul Mackerras RELOC(mem_reserve_map)[i].base, 21159b6b563cSPaul Mackerras RELOC(mem_reserve_map)[i].size); 21169b6b563cSPaul Mackerras } 21179b6b563cSPaul Mackerras #endif 21184d1f3f25SJimi Xenidis /* Bump mem_reserve_cnt to cause further reservations to fail 21194d1f3f25SJimi Xenidis * since it's too late. 21204d1f3f25SJimi Xenidis */ 21219b6b563cSPaul Mackerras RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE; 21229b6b563cSPaul Mackerras 21239b6b563cSPaul Mackerras prom_printf("Device tree strings 0x%x -> 0x%x\n", 21249b6b563cSPaul Mackerras RELOC(dt_string_start), RELOC(dt_string_end)); 21259b6b563cSPaul Mackerras prom_printf("Device tree struct 0x%x -> 0x%x\n", 21269b6b563cSPaul Mackerras RELOC(dt_struct_start), RELOC(dt_struct_end)); 21279b6b563cSPaul Mackerras 21289b6b563cSPaul Mackerras } 21299b6b563cSPaul Mackerras 213054f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE 213154f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property. 213254f4ee18SHollis Blanchard * The values are bad, and it doesn't even have the right number of cells. */ 213354f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void) 21349b6b563cSPaul Mackerras { 213554f4ee18SHollis Blanchard phandle isa; 2136980a6513SBenjamin Herrenschmidt u32 rloc = 0x01002000; /* IO space; PCI device = 4 */ 213754f4ee18SHollis Blanchard u32 isa_ranges[6]; 2138980a6513SBenjamin Herrenschmidt char *name; 213954f4ee18SHollis Blanchard 2140980a6513SBenjamin Herrenschmidt name = "/ht@0/isa@4"; 2141980a6513SBenjamin Herrenschmidt isa = call_prom("finddevice", 1, 1, ADDR(name)); 2142980a6513SBenjamin Herrenschmidt if (!PHANDLE_VALID(isa)) { 2143980a6513SBenjamin Herrenschmidt name = "/ht@0/isa@6"; 2144980a6513SBenjamin Herrenschmidt isa = call_prom("finddevice", 1, 1, ADDR(name)); 2145980a6513SBenjamin Herrenschmidt rloc = 0x01003000; /* IO space; PCI device = 6 */ 2146980a6513SBenjamin Herrenschmidt } 214754f4ee18SHollis Blanchard if (!PHANDLE_VALID(isa)) 214854f4ee18SHollis Blanchard return; 214954f4ee18SHollis Blanchard 2150980a6513SBenjamin Herrenschmidt if (prom_getproplen(isa, "ranges") != 12) 2151980a6513SBenjamin Herrenschmidt return; 215254f4ee18SHollis Blanchard if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges)) 215354f4ee18SHollis Blanchard == PROM_ERROR) 215454f4ee18SHollis Blanchard return; 215554f4ee18SHollis Blanchard 215654f4ee18SHollis Blanchard if (isa_ranges[0] != 0x1 || 215754f4ee18SHollis Blanchard isa_ranges[1] != 0xf4000000 || 215854f4ee18SHollis Blanchard isa_ranges[2] != 0x00010000) 215954f4ee18SHollis Blanchard return; 216054f4ee18SHollis Blanchard 2161980a6513SBenjamin Herrenschmidt prom_printf("Fixing up bogus ISA range on Maple/Apache...\n"); 216254f4ee18SHollis Blanchard 216354f4ee18SHollis Blanchard isa_ranges[0] = 0x1; 216454f4ee18SHollis Blanchard isa_ranges[1] = 0x0; 2165980a6513SBenjamin Herrenschmidt isa_ranges[2] = rloc; 216654f4ee18SHollis Blanchard isa_ranges[3] = 0x0; 216754f4ee18SHollis Blanchard isa_ranges[4] = 0x0; 216854f4ee18SHollis Blanchard isa_ranges[5] = 0x00010000; 2169980a6513SBenjamin Herrenschmidt prom_setprop(isa, name, "ranges", 217054f4ee18SHollis Blanchard isa_ranges, sizeof(isa_ranges)); 217154f4ee18SHollis Blanchard } 21728f101a05SHarry Ciao 21738f101a05SHarry Ciao #define CPC925_MC_START 0xf8000000 21748f101a05SHarry Ciao #define CPC925_MC_LENGTH 0x1000000 21758f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */ 21768f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void) 21778f101a05SHarry Ciao { 21788f101a05SHarry Ciao phandle mc; 21798f101a05SHarry Ciao u32 mc_reg[4]; 21808f101a05SHarry Ciao char *name = "/hostbridge@f8000000"; 21818f101a05SHarry Ciao struct prom_t *_prom = &RELOC(prom); 21828f101a05SHarry Ciao u32 ac, sc; 21838f101a05SHarry Ciao 21848f101a05SHarry Ciao mc = call_prom("finddevice", 1, 1, ADDR(name)); 21858f101a05SHarry Ciao if (!PHANDLE_VALID(mc)) 21868f101a05SHarry Ciao return; 21878f101a05SHarry Ciao 21888f101a05SHarry Ciao if (prom_getproplen(mc, "reg") != 8) 21898f101a05SHarry Ciao return; 21908f101a05SHarry Ciao 21918f101a05SHarry Ciao prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac)); 21928f101a05SHarry Ciao prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc)); 21938f101a05SHarry Ciao if ((ac != 2) || (sc != 2)) 21948f101a05SHarry Ciao return; 21958f101a05SHarry Ciao 21968f101a05SHarry Ciao if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR) 21978f101a05SHarry Ciao return; 21988f101a05SHarry Ciao 21998f101a05SHarry Ciao if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH) 22008f101a05SHarry Ciao return; 22018f101a05SHarry Ciao 22028f101a05SHarry Ciao prom_printf("Fixing up bogus hostbridge on Maple...\n"); 22038f101a05SHarry Ciao 22048f101a05SHarry Ciao mc_reg[0] = 0x0; 22058f101a05SHarry Ciao mc_reg[1] = CPC925_MC_START; 22068f101a05SHarry Ciao mc_reg[2] = 0x0; 22078f101a05SHarry Ciao mc_reg[3] = CPC925_MC_LENGTH; 22088f101a05SHarry Ciao prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg)); 22098f101a05SHarry Ciao } 221054f4ee18SHollis Blanchard #else 221154f4ee18SHollis Blanchard #define fixup_device_tree_maple() 22128f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller() 221354f4ee18SHollis Blanchard #endif 221454f4ee18SHollis Blanchard 2215e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP 2216e4805922SOlaf Hering /* 2217e4805922SOlaf Hering * Pegasos and BriQ lacks the "ranges" property in the isa node 2218e4805922SOlaf Hering * Pegasos needs decimal IRQ 14/15, not hexadecimal 2219556ecf9bSOlaf Hering * Pegasos has the IDE configured in legacy mode, but advertised as native 2220e4805922SOlaf Hering */ 2221e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void) 2222e8c0acf9SBenjamin Herrenschmidt { 2223e4805922SOlaf Hering phandle ph; 2224e4805922SOlaf Hering u32 prop[6]; 222526c5032eSBenjamin Herrenschmidt u32 rloc = 0x01006000; /* IO space; PCI device = 12 */ 2226e8c0acf9SBenjamin Herrenschmidt char *name; 2227e8c0acf9SBenjamin Herrenschmidt int rc; 2228e8c0acf9SBenjamin Herrenschmidt 2229e8c0acf9SBenjamin Herrenschmidt name = "/pci@80000000/isa@c"; 2230e4805922SOlaf Hering ph = call_prom("finddevice", 1, 1, ADDR(name)); 2231e4805922SOlaf Hering if (!PHANDLE_VALID(ph)) { 223226c5032eSBenjamin Herrenschmidt name = "/pci@ff500000/isa@6"; 2233e4805922SOlaf Hering ph = call_prom("finddevice", 1, 1, ADDR(name)); 223426c5032eSBenjamin Herrenschmidt rloc = 0x01003000; /* IO space; PCI device = 6 */ 223526c5032eSBenjamin Herrenschmidt } 2236e4805922SOlaf Hering if (PHANDLE_VALID(ph)) { 2237e4805922SOlaf Hering rc = prom_getproplen(ph, "ranges"); 2238e4805922SOlaf Hering if (rc == 0 || rc == PROM_ERROR) { 2239e8c0acf9SBenjamin Herrenschmidt prom_printf("Fixing up missing ISA range on Pegasos...\n"); 2240e8c0acf9SBenjamin Herrenschmidt 2241e4805922SOlaf Hering prop[0] = 0x1; 2242e4805922SOlaf Hering prop[1] = 0x0; 2243e4805922SOlaf Hering prop[2] = rloc; 2244e4805922SOlaf Hering prop[3] = 0x0; 2245e4805922SOlaf Hering prop[4] = 0x0; 2246e4805922SOlaf Hering prop[5] = 0x00010000; 2247e4805922SOlaf Hering prom_setprop(ph, name, "ranges", prop, sizeof(prop)); 2248e4805922SOlaf Hering } 2249e4805922SOlaf Hering } 2250e4805922SOlaf Hering 2251e4805922SOlaf Hering name = "/pci@80000000/ide@C,1"; 2252e4805922SOlaf Hering ph = call_prom("finddevice", 1, 1, ADDR(name)); 2253e4805922SOlaf Hering if (PHANDLE_VALID(ph)) { 2254e4805922SOlaf Hering prom_printf("Fixing up IDE interrupt on Pegasos...\n"); 2255e4805922SOlaf Hering prop[0] = 14; 2256e4805922SOlaf Hering prop[1] = 0x0; 2257556ecf9bSOlaf Hering prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32)); 2258556ecf9bSOlaf Hering prom_printf("Fixing up IDE class-code on Pegasos...\n"); 2259556ecf9bSOlaf Hering rc = prom_getprop(ph, "class-code", prop, sizeof(u32)); 2260556ecf9bSOlaf Hering if (rc == sizeof(u32)) { 2261556ecf9bSOlaf Hering prop[0] &= ~0x5; 2262556ecf9bSOlaf Hering prom_setprop(ph, name, "class-code", prop, sizeof(u32)); 2263556ecf9bSOlaf Hering } 2264e4805922SOlaf Hering } 2265e8c0acf9SBenjamin Herrenschmidt } 2266e8c0acf9SBenjamin Herrenschmidt #else 2267e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp() 2268e8c0acf9SBenjamin Herrenschmidt #endif 2269e8c0acf9SBenjamin Herrenschmidt 22709b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC) 227154f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void) 227254f4ee18SHollis Blanchard { 22739b6b563cSPaul Mackerras phandle u3, i2c, mpic; 22749b6b563cSPaul Mackerras u32 u3_rev; 22759b6b563cSPaul Mackerras u32 interrupts[2]; 22769b6b563cSPaul Mackerras u32 parent; 22779b6b563cSPaul Mackerras 22789b6b563cSPaul Mackerras /* Some G5s have a missing interrupt definition, fix it up here */ 22799b6b563cSPaul Mackerras u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000")); 22809b6b563cSPaul Mackerras if (!PHANDLE_VALID(u3)) 22819b6b563cSPaul Mackerras return; 22829b6b563cSPaul Mackerras i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000")); 22839b6b563cSPaul Mackerras if (!PHANDLE_VALID(i2c)) 22849b6b563cSPaul Mackerras return; 22859b6b563cSPaul Mackerras mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000")); 22869b6b563cSPaul Mackerras if (!PHANDLE_VALID(mpic)) 22879b6b563cSPaul Mackerras return; 22889b6b563cSPaul Mackerras 22899b6b563cSPaul Mackerras /* check if proper rev of u3 */ 22909b6b563cSPaul Mackerras if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev)) 22919b6b563cSPaul Mackerras == PROM_ERROR) 22929b6b563cSPaul Mackerras return; 22937d49697eSBenjamin Herrenschmidt if (u3_rev < 0x35 || u3_rev > 0x39) 22949b6b563cSPaul Mackerras return; 22959b6b563cSPaul Mackerras /* does it need fixup ? */ 22969b6b563cSPaul Mackerras if (prom_getproplen(i2c, "interrupts") > 0) 22979b6b563cSPaul Mackerras return; 22989b6b563cSPaul Mackerras 22999b6b563cSPaul Mackerras prom_printf("fixing up bogus interrupts for u3 i2c...\n"); 23009b6b563cSPaul Mackerras 23019b6b563cSPaul Mackerras /* interrupt on this revision of u3 is number 0 and level */ 23029b6b563cSPaul Mackerras interrupts[0] = 0; 23039b6b563cSPaul Mackerras interrupts[1] = 1; 2304a23414beSPaul Mackerras prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts", 2305a23414beSPaul Mackerras &interrupts, sizeof(interrupts)); 23069b6b563cSPaul Mackerras parent = (u32)mpic; 2307a23414beSPaul Mackerras prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent", 2308a23414beSPaul Mackerras &parent, sizeof(parent)); 23099b6b563cSPaul Mackerras } 231054f4ee18SHollis Blanchard #else 231154f4ee18SHollis Blanchard #define fixup_device_tree_pmac() 231254f4ee18SHollis Blanchard #endif 23139b6b563cSPaul Mackerras 231488fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA 231594d2dde7SGrant Likely /* 231694d2dde7SGrant Likely * The MPC5200 FEC driver requires an phy-handle property to tell it how 231794d2dde7SGrant Likely * to talk to the phy. If the phy-handle property is missing, then this 231894d2dde7SGrant Likely * function is called to add the appropriate nodes and link it to the 231994d2dde7SGrant Likely * ethernet node. 232094d2dde7SGrant Likely */ 232194d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void) 232288fd2a9dSSylvain Munaut { 232388fd2a9dSSylvain Munaut u32 node; 232488fd2a9dSSylvain Munaut char prop[64]; 232594d2dde7SGrant Likely int rv; 232688fd2a9dSSylvain Munaut 232794d2dde7SGrant Likely /* Check if /builtin/ethernet exists - bail if it doesn't */ 232894d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet")); 232988fd2a9dSSylvain Munaut if (!PHANDLE_VALID(node)) 233088fd2a9dSSylvain Munaut return; 233188fd2a9dSSylvain Munaut 233294d2dde7SGrant Likely /* Check if the phy-handle property exists - bail if it does */ 233394d2dde7SGrant Likely rv = prom_getprop(node, "phy-handle", prop, sizeof(prop)); 233494d2dde7SGrant Likely if (!rv) 233588fd2a9dSSylvain Munaut return; 233688fd2a9dSSylvain Munaut 233794d2dde7SGrant Likely /* 233894d2dde7SGrant Likely * At this point the ethernet device doesn't have a phy described. 233994d2dde7SGrant Likely * Now we need to add the missing phy node and linkage 234094d2dde7SGrant Likely */ 234188fd2a9dSSylvain Munaut 234294d2dde7SGrant Likely /* Check for an MDIO bus node - if missing then create one */ 23436f4347c9SOlaf Hering node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio")); 23446f4347c9SOlaf Hering if (!PHANDLE_VALID(node)) { 23456f4347c9SOlaf Hering prom_printf("Adding Ethernet MDIO node\n"); 23466f4347c9SOlaf Hering call_prom("interpret", 1, 1, 23476f4347c9SOlaf Hering " s\" /builtin\" find-device" 23486f4347c9SOlaf Hering " new-device" 23496f4347c9SOlaf Hering " 1 encode-int s\" #address-cells\" property" 23506f4347c9SOlaf Hering " 0 encode-int s\" #size-cells\" property" 235194d2dde7SGrant Likely " s\" mdio\" device-name" 235294d2dde7SGrant Likely " s\" fsl,mpc5200b-mdio\" encode-string" 23536f4347c9SOlaf Hering " s\" compatible\" property" 23546f4347c9SOlaf Hering " 0xf0003000 0x400 reg" 23556f4347c9SOlaf Hering " 0x2 encode-int" 23566f4347c9SOlaf Hering " 0x5 encode-int encode+" 23576f4347c9SOlaf Hering " 0x3 encode-int encode+" 23586f4347c9SOlaf Hering " s\" interrupts\" property" 23596f4347c9SOlaf Hering " finish-device"); 23606f4347c9SOlaf Hering }; 23616f4347c9SOlaf Hering 236294d2dde7SGrant Likely /* Check for a PHY device node - if missing then create one and 236394d2dde7SGrant Likely * give it's phandle to the ethernet node */ 236494d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, 236594d2dde7SGrant Likely ADDR("/builtin/mdio/ethernet-phy")); 23666f4347c9SOlaf Hering if (!PHANDLE_VALID(node)) { 23676f4347c9SOlaf Hering prom_printf("Adding Ethernet PHY node\n"); 23686f4347c9SOlaf Hering call_prom("interpret", 1, 1, 23696f4347c9SOlaf Hering " s\" /builtin/mdio\" find-device" 23706f4347c9SOlaf Hering " new-device" 23716f4347c9SOlaf Hering " s\" ethernet-phy\" device-name" 23726f4347c9SOlaf Hering " 0x10 encode-int s\" reg\" property" 23736f4347c9SOlaf Hering " my-self" 23746f4347c9SOlaf Hering " ihandle>phandle" 23756f4347c9SOlaf Hering " finish-device" 23766f4347c9SOlaf Hering " s\" /builtin/ethernet\" find-device" 23776f4347c9SOlaf Hering " encode-int" 23786f4347c9SOlaf Hering " s\" phy-handle\" property" 23796f4347c9SOlaf Hering " device-end"); 23806f4347c9SOlaf Hering } 238194d2dde7SGrant Likely } 23826f4347c9SOlaf Hering 238394d2dde7SGrant Likely static void __init fixup_device_tree_efika(void) 238494d2dde7SGrant Likely { 238594d2dde7SGrant Likely int sound_irq[3] = { 2, 2, 0 }; 238694d2dde7SGrant Likely int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0, 238794d2dde7SGrant Likely 3,4,0, 3,5,0, 3,6,0, 3,7,0, 238894d2dde7SGrant Likely 3,8,0, 3,9,0, 3,10,0, 3,11,0, 238994d2dde7SGrant Likely 3,12,0, 3,13,0, 3,14,0, 3,15,0 }; 239094d2dde7SGrant Likely u32 node; 239194d2dde7SGrant Likely char prop[64]; 239294d2dde7SGrant Likely int rv, len; 239394d2dde7SGrant Likely 239494d2dde7SGrant Likely /* Check if we're really running on a EFIKA */ 239594d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, ADDR("/")); 239694d2dde7SGrant Likely if (!PHANDLE_VALID(node)) 239794d2dde7SGrant Likely return; 239894d2dde7SGrant Likely 239994d2dde7SGrant Likely rv = prom_getprop(node, "model", prop, sizeof(prop)); 240094d2dde7SGrant Likely if (rv == PROM_ERROR) 240194d2dde7SGrant Likely return; 240294d2dde7SGrant Likely if (strcmp(prop, "EFIKA5K2")) 240394d2dde7SGrant Likely return; 240494d2dde7SGrant Likely 240594d2dde7SGrant Likely prom_printf("Applying EFIKA device tree fixups\n"); 240694d2dde7SGrant Likely 240794d2dde7SGrant Likely /* Claiming to be 'chrp' is death */ 240894d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, ADDR("/")); 240994d2dde7SGrant Likely rv = prom_getprop(node, "device_type", prop, sizeof(prop)); 241094d2dde7SGrant Likely if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0)) 241194d2dde7SGrant Likely prom_setprop(node, "/", "device_type", "efika", sizeof("efika")); 241294d2dde7SGrant Likely 24137f4392cdSDavid Woodhouse /* CODEGEN,description is exposed in /proc/cpuinfo so 24147f4392cdSDavid Woodhouse fix that too */ 24157f4392cdSDavid Woodhouse rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop)); 24167f4392cdSDavid Woodhouse if (rv != PROM_ERROR && (strstr(prop, "CHRP"))) 24177f4392cdSDavid Woodhouse prom_setprop(node, "/", "CODEGEN,description", 24187f4392cdSDavid Woodhouse "Efika 5200B PowerPC System", 24197f4392cdSDavid Woodhouse sizeof("Efika 5200B PowerPC System")); 24207f4392cdSDavid Woodhouse 242194d2dde7SGrant Likely /* Fixup bestcomm interrupts property */ 242294d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm")); 242394d2dde7SGrant Likely if (PHANDLE_VALID(node)) { 242494d2dde7SGrant Likely len = prom_getproplen(node, "interrupts"); 242594d2dde7SGrant Likely if (len == 12) { 242694d2dde7SGrant Likely prom_printf("Fixing bestcomm interrupts property\n"); 242794d2dde7SGrant Likely prom_setprop(node, "/builtin/bestcom", "interrupts", 242894d2dde7SGrant Likely bcomm_irq, sizeof(bcomm_irq)); 242994d2dde7SGrant Likely } 243094d2dde7SGrant Likely } 243194d2dde7SGrant Likely 243294d2dde7SGrant Likely /* Fixup sound interrupts property */ 243394d2dde7SGrant Likely node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound")); 243494d2dde7SGrant Likely if (PHANDLE_VALID(node)) { 243594d2dde7SGrant Likely rv = prom_getprop(node, "interrupts", prop, sizeof(prop)); 243694d2dde7SGrant Likely if (rv == PROM_ERROR) { 243794d2dde7SGrant Likely prom_printf("Adding sound interrupts property\n"); 243894d2dde7SGrant Likely prom_setprop(node, "/builtin/sound", "interrupts", 243994d2dde7SGrant Likely sound_irq, sizeof(sound_irq)); 244094d2dde7SGrant Likely } 244194d2dde7SGrant Likely } 244294d2dde7SGrant Likely 244394d2dde7SGrant Likely /* Make sure ethernet phy-handle property exists */ 244494d2dde7SGrant Likely fixup_device_tree_efika_add_phy(); 244588fd2a9dSSylvain Munaut } 244688fd2a9dSSylvain Munaut #else 244788fd2a9dSSylvain Munaut #define fixup_device_tree_efika() 244888fd2a9dSSylvain Munaut #endif 244988fd2a9dSSylvain Munaut 245054f4ee18SHollis Blanchard static void __init fixup_device_tree(void) 245154f4ee18SHollis Blanchard { 245254f4ee18SHollis Blanchard fixup_device_tree_maple(); 24538f101a05SHarry Ciao fixup_device_tree_maple_memory_controller(); 2454e8c0acf9SBenjamin Herrenschmidt fixup_device_tree_chrp(); 245554f4ee18SHollis Blanchard fixup_device_tree_pmac(); 245688fd2a9dSSylvain Munaut fixup_device_tree_efika(); 245754f4ee18SHollis Blanchard } 24589b6b563cSPaul Mackerras 24599b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void) 24609b6b563cSPaul Mackerras { 24619b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 24629b6b563cSPaul Mackerras u32 getprop_rval; 24639b6b563cSPaul Mackerras ihandle prom_cpu; 24649b6b563cSPaul Mackerras phandle cpu_pkg; 24659b6b563cSPaul Mackerras 2466a575b807SPaul Mackerras _prom->cpu = 0; 24679b6b563cSPaul Mackerras if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0) 2468a575b807SPaul Mackerras return; 24699b6b563cSPaul Mackerras 24709b6b563cSPaul Mackerras cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu); 24719b6b563cSPaul Mackerras 24729b6b563cSPaul Mackerras prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval)); 24739b6b563cSPaul Mackerras _prom->cpu = getprop_rval; 24749b6b563cSPaul Mackerras 24752c48a7d6SMichael Neuling prom_debug("Booting CPU hw index = %lu\n", _prom->cpu); 24769b6b563cSPaul Mackerras } 24779b6b563cSPaul Mackerras 24789b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4) 24799b6b563cSPaul Mackerras { 24809b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD 24819b6b563cSPaul Mackerras struct prom_t *_prom = &RELOC(prom); 24829b6b563cSPaul Mackerras 24839b6b563cSPaul Mackerras if (r3 && r4 && r4 != 0xdeadbeef) { 24849b6b563cSPaul Mackerras unsigned long val; 24859b6b563cSPaul Mackerras 248651fae6deSMichael Ellerman RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3; 24879b6b563cSPaul Mackerras RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4; 24889b6b563cSPaul Mackerras 24899b6b563cSPaul Mackerras val = RELOC(prom_initrd_start); 2490a23414beSPaul Mackerras prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start", 2491a23414beSPaul Mackerras &val, sizeof(val)); 24929b6b563cSPaul Mackerras val = RELOC(prom_initrd_end); 2493a23414beSPaul Mackerras prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end", 2494a23414beSPaul Mackerras &val, sizeof(val)); 24959b6b563cSPaul Mackerras 24969b6b563cSPaul Mackerras reserve_mem(RELOC(prom_initrd_start), 24979b6b563cSPaul Mackerras RELOC(prom_initrd_end) - RELOC(prom_initrd_start)); 24989b6b563cSPaul Mackerras 24999b6b563cSPaul Mackerras prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start)); 25009b6b563cSPaul Mackerras prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end)); 25019b6b563cSPaul Mackerras } 25029b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */ 25039b6b563cSPaul Mackerras } 25049b6b563cSPaul Mackerras 25059b6b563cSPaul Mackerras /* 25069b6b563cSPaul Mackerras * We enter here early on, when the Open Firmware prom is still 25079b6b563cSPaul Mackerras * handling exceptions and the MMU hash table for us. 25089b6b563cSPaul Mackerras */ 25099b6b563cSPaul Mackerras 25109b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4, 25119b6b563cSPaul Mackerras unsigned long pp, 2512549e8152SPaul Mackerras unsigned long r6, unsigned long r7, 2513549e8152SPaul Mackerras unsigned long kbase) 25149b6b563cSPaul Mackerras { 25159b6b563cSPaul Mackerras struct prom_t *_prom; 25169b6b563cSPaul Mackerras unsigned long hdr; 25179b6b563cSPaul Mackerras 25189b6b563cSPaul Mackerras #ifdef CONFIG_PPC32 2519549e8152SPaul Mackerras unsigned long offset = reloc_offset(); 25209b6b563cSPaul Mackerras reloc_got2(offset); 25219b6b563cSPaul Mackerras #endif 25229b6b563cSPaul Mackerras 25239b6b563cSPaul Mackerras _prom = &RELOC(prom); 25249b6b563cSPaul Mackerras 25259b6b563cSPaul Mackerras /* 25269b6b563cSPaul Mackerras * First zero the BSS 25279b6b563cSPaul Mackerras */ 25289b6b563cSPaul Mackerras memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start); 25299b6b563cSPaul Mackerras 25309b6b563cSPaul Mackerras /* 25319b6b563cSPaul Mackerras * Init interface to Open Firmware, get some node references, 25329b6b563cSPaul Mackerras * like /chosen 25339b6b563cSPaul Mackerras */ 25349b6b563cSPaul Mackerras prom_init_client_services(pp); 25359b6b563cSPaul Mackerras 25369b6b563cSPaul Mackerras /* 2537a23414beSPaul Mackerras * See if this OF is old enough that we need to do explicit maps 2538a23414beSPaul Mackerras * and other workarounds 2539a23414beSPaul Mackerras */ 2540a23414beSPaul Mackerras prom_find_mmu(); 2541a23414beSPaul Mackerras 2542a23414beSPaul Mackerras /* 25439b6b563cSPaul Mackerras * Init prom stdout device 25449b6b563cSPaul Mackerras */ 25459b6b563cSPaul Mackerras prom_init_stdout(); 25469b6b563cSPaul Mackerras 2547151a9f4aSBenjamin Herrenschmidt prom_printf("Preparing to boot %s", RELOC(linux_banner)); 2548e7943fbbSMichael Ellerman 25499b6b563cSPaul Mackerras /* 25509b6b563cSPaul Mackerras * Get default machine type. At this point, we do not differentiate 25519b6b563cSPaul Mackerras * between pSeries SMP and pSeries LPAR 25529b6b563cSPaul Mackerras */ 25539b6b563cSPaul Mackerras RELOC(of_platform) = prom_find_machine_type(); 25549b6b563cSPaul Mackerras 2555549e8152SPaul Mackerras #ifndef CONFIG_RELOCATABLE 2556add60ef3SOlaf Hering /* Bail if this is a kdump kernel. */ 2557add60ef3SOlaf Hering if (PHYSICAL_START > 0) 2558add60ef3SOlaf Hering prom_panic("Error: You can't boot a kdump kernel from OF!\n"); 2559549e8152SPaul Mackerras #endif 2560add60ef3SOlaf Hering 2561add60ef3SOlaf Hering /* 2562add60ef3SOlaf Hering * Check for an initrd 2563add60ef3SOlaf Hering */ 2564add60ef3SOlaf Hering prom_check_initrd(r3, r4); 2565add60ef3SOlaf Hering 25669b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 25679b6b563cSPaul Mackerras /* 25689b6b563cSPaul Mackerras * On pSeries, inform the firmware about our capabilities 25699b6b563cSPaul Mackerras */ 2570799d6046SPaul Mackerras if (RELOC(of_platform) == PLATFORM_PSERIES || 2571799d6046SPaul Mackerras RELOC(of_platform) == PLATFORM_PSERIES_LPAR) 25729b6b563cSPaul Mackerras prom_send_capabilities(); 25739b6b563cSPaul Mackerras #endif 25749b6b563cSPaul Mackerras 25759b6b563cSPaul Mackerras /* 2576f3f66f59SArnd Bergmann * Copy the CPU hold code 25779b6b563cSPaul Mackerras */ 257855d36339SDavid Gibson if (RELOC(of_platform) != PLATFORM_POWERMAC) 2579549e8152SPaul Mackerras copy_and_flush(0, kbase, 0x100, 0); 25809b6b563cSPaul Mackerras 25819b6b563cSPaul Mackerras /* 25829b6b563cSPaul Mackerras * Do early parsing of command line 25839b6b563cSPaul Mackerras */ 25849b6b563cSPaul Mackerras early_cmdline_parse(); 25859b6b563cSPaul Mackerras 25869b6b563cSPaul Mackerras /* 25879b6b563cSPaul Mackerras * Initialize memory management within prom_init 25889b6b563cSPaul Mackerras */ 25899b6b563cSPaul Mackerras prom_init_mem(); 25909b6b563cSPaul Mackerras 25919b6b563cSPaul Mackerras /* 25929b6b563cSPaul Mackerras * Determine which cpu is actually running right _now_ 25939b6b563cSPaul Mackerras */ 25949b6b563cSPaul Mackerras prom_find_boot_cpu(); 25959b6b563cSPaul Mackerras 25969b6b563cSPaul Mackerras /* 25979b6b563cSPaul Mackerras * Initialize display devices 25989b6b563cSPaul Mackerras */ 25999b6b563cSPaul Mackerras prom_check_displays(); 26009b6b563cSPaul Mackerras 26019b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 26029b6b563cSPaul Mackerras /* 26039b6b563cSPaul Mackerras * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else 26049b6b563cSPaul Mackerras * that uses the allocator, we need to make sure we get the top of memory 26059b6b563cSPaul Mackerras * available for us here... 26069b6b563cSPaul Mackerras */ 26079b6b563cSPaul Mackerras if (RELOC(of_platform) == PLATFORM_PSERIES) 26089b6b563cSPaul Mackerras prom_initialize_tce_table(); 26099b6b563cSPaul Mackerras #endif 26109b6b563cSPaul Mackerras 26119b6b563cSPaul Mackerras /* 26129b6b563cSPaul Mackerras * On non-powermacs, try to instantiate RTAS and puts all CPUs 26139b6b563cSPaul Mackerras * in spin-loops. PowerMacs don't have a working RTAS and use 26149b6b563cSPaul Mackerras * a different way to spin CPUs 26159b6b563cSPaul Mackerras */ 26169b6b563cSPaul Mackerras if (RELOC(of_platform) != PLATFORM_POWERMAC) { 26179b6b563cSPaul Mackerras prom_instantiate_rtas(); 26189b6b563cSPaul Mackerras prom_hold_cpus(); 26199b6b563cSPaul Mackerras } 26209b6b563cSPaul Mackerras 26219b6b563cSPaul Mackerras /* 26229b6b563cSPaul Mackerras * Fill in some infos for use by the kernel later on 26239b6b563cSPaul Mackerras */ 2624cf68787bSBenjamin Krill if (RELOC(prom_memory_limit)) 2625cf68787bSBenjamin Krill prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit", 2626cf68787bSBenjamin Krill &RELOC(prom_memory_limit), 2627cf68787bSBenjamin Krill sizeof(prom_memory_limit)); 26289b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 2629165785e5SJeremy Kerr if (RELOC(prom_iommu_off)) 2630a23414beSPaul Mackerras prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off", 2631a23414beSPaul Mackerras NULL, 0); 26329b6b563cSPaul Mackerras 2633165785e5SJeremy Kerr if (RELOC(prom_iommu_force_on)) 2634a23414beSPaul Mackerras prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on", 2635a23414beSPaul Mackerras NULL, 0); 26369b6b563cSPaul Mackerras 26379b6b563cSPaul Mackerras if (RELOC(prom_tce_alloc_start)) { 2638a23414beSPaul Mackerras prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start", 26399b6b563cSPaul Mackerras &RELOC(prom_tce_alloc_start), 26409b6b563cSPaul Mackerras sizeof(prom_tce_alloc_start)); 2641a23414beSPaul Mackerras prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end", 26429b6b563cSPaul Mackerras &RELOC(prom_tce_alloc_end), 26439b6b563cSPaul Mackerras sizeof(prom_tce_alloc_end)); 26449b6b563cSPaul Mackerras } 26459b6b563cSPaul Mackerras #endif 26469b6b563cSPaul Mackerras 26479b6b563cSPaul Mackerras /* 26489b6b563cSPaul Mackerras * Fixup any known bugs in the device-tree 26499b6b563cSPaul Mackerras */ 26509b6b563cSPaul Mackerras fixup_device_tree(); 26519b6b563cSPaul Mackerras 26529b6b563cSPaul Mackerras /* 26539b6b563cSPaul Mackerras * Now finally create the flattened device-tree 26549b6b563cSPaul Mackerras */ 26559b6b563cSPaul Mackerras prom_printf("copying OF device tree...\n"); 26569b6b563cSPaul Mackerras flatten_device_tree(); 26579b6b563cSPaul Mackerras 26583825ac0eSPaul Mackerras /* 26593825ac0eSPaul Mackerras * in case stdin is USB and still active on IBM machines... 26603825ac0eSPaul Mackerras * Unfortunately quiesce crashes on some powermacs if we have 26613825ac0eSPaul Mackerras * closed stdin already (in particular the powerbook 101). 26623825ac0eSPaul Mackerras */ 26633825ac0eSPaul Mackerras if (RELOC(of_platform) != PLATFORM_POWERMAC) 26649b6b563cSPaul Mackerras prom_close_stdin(); 26659b6b563cSPaul Mackerras 26669b6b563cSPaul Mackerras /* 26679b6b563cSPaul Mackerras * Call OF "quiesce" method to shut down pending DMA's from 26689b6b563cSPaul Mackerras * devices etc... 26699b6b563cSPaul Mackerras */ 26709b6b563cSPaul Mackerras prom_printf("Calling quiesce...\n"); 26719b6b563cSPaul Mackerras call_prom("quiesce", 0, 0); 26729b6b563cSPaul Mackerras 26739b6b563cSPaul Mackerras /* 26749b6b563cSPaul Mackerras * And finally, call the kernel passing it the flattened device 26759b6b563cSPaul Mackerras * tree and NULL as r5, thus triggering the new entry point which 26769b6b563cSPaul Mackerras * is common to us and kexec 26779b6b563cSPaul Mackerras */ 26789b6b563cSPaul Mackerras hdr = RELOC(dt_header_start); 26799b6b563cSPaul Mackerras prom_printf("returning from prom_init\n"); 26809b6b563cSPaul Mackerras prom_debug("->dt_header_start=0x%x\n", hdr); 26819b6b563cSPaul Mackerras 26829b6b563cSPaul Mackerras #ifdef CONFIG_PPC32 26839b6b563cSPaul Mackerras reloc_got2(-offset); 26849b6b563cSPaul Mackerras #endif 26859b6b563cSPaul Mackerras 2686549e8152SPaul Mackerras __start(hdr, kbase, 0); 26879b6b563cSPaul Mackerras 26889b6b563cSPaul Mackerras return 0; 26899b6b563cSPaul Mackerras } 2690