xref: /openbmc/linux/arch/powerpc/kernel/prom_init.c (revision 5ac47f7a6efbd4fa9141c249e8af3f74e7944eb7)
19b6b563cSPaul Mackerras /*
29b6b563cSPaul Mackerras  * Procedures for interfacing to Open Firmware.
39b6b563cSPaul Mackerras  *
49b6b563cSPaul Mackerras  * Paul Mackerras	August 1996.
59b6b563cSPaul Mackerras  * Copyright (C) 1996-2005 Paul Mackerras.
69b6b563cSPaul Mackerras  *
79b6b563cSPaul Mackerras  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
89b6b563cSPaul Mackerras  *    {engebret|bergner}@us.ibm.com
99b6b563cSPaul Mackerras  *
109b6b563cSPaul Mackerras  *      This program is free software; you can redistribute it and/or
119b6b563cSPaul Mackerras  *      modify it under the terms of the GNU General Public License
129b6b563cSPaul Mackerras  *      as published by the Free Software Foundation; either version
139b6b563cSPaul Mackerras  *      2 of the License, or (at your option) any later version.
149b6b563cSPaul Mackerras  */
159b6b563cSPaul Mackerras 
169b6b563cSPaul Mackerras #undef DEBUG_PROM
179b6b563cSPaul Mackerras 
189b6b563cSPaul Mackerras #include <stdarg.h>
199b6b563cSPaul Mackerras #include <linux/kernel.h>
209b6b563cSPaul Mackerras #include <linux/string.h>
219b6b563cSPaul Mackerras #include <linux/init.h>
229b6b563cSPaul Mackerras #include <linux/threads.h>
239b6b563cSPaul Mackerras #include <linux/spinlock.h>
249b6b563cSPaul Mackerras #include <linux/types.h>
259b6b563cSPaul Mackerras #include <linux/pci.h>
269b6b563cSPaul Mackerras #include <linux/proc_fs.h>
279b6b563cSPaul Mackerras #include <linux/stringify.h>
289b6b563cSPaul Mackerras #include <linux/delay.h>
299b6b563cSPaul Mackerras #include <linux/initrd.h>
309b6b563cSPaul Mackerras #include <linux/bitops.h>
319b6b563cSPaul Mackerras #include <asm/prom.h>
329b6b563cSPaul Mackerras #include <asm/rtas.h>
339b6b563cSPaul Mackerras #include <asm/page.h>
349b6b563cSPaul Mackerras #include <asm/processor.h>
359b6b563cSPaul Mackerras #include <asm/irq.h>
369b6b563cSPaul Mackerras #include <asm/io.h>
379b6b563cSPaul Mackerras #include <asm/smp.h>
389b6b563cSPaul Mackerras #include <asm/mmu.h>
399b6b563cSPaul Mackerras #include <asm/pgtable.h>
409b6b563cSPaul Mackerras #include <asm/pci.h>
419b6b563cSPaul Mackerras #include <asm/iommu.h>
429b6b563cSPaul Mackerras #include <asm/btext.h>
439b6b563cSPaul Mackerras #include <asm/sections.h>
449b6b563cSPaul Mackerras #include <asm/machdep.h>
4527f44888SBenjamin Herrenschmidt #include <asm/opal.h>
469b6b563cSPaul Mackerras 
479b6b563cSPaul Mackerras #include <linux/linux_logo.h>
489b6b563cSPaul Mackerras 
499b6b563cSPaul Mackerras /*
509b6b563cSPaul Mackerras  * Eventually bump that one up
519b6b563cSPaul Mackerras  */
529b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE	0x100000
539b6b563cSPaul Mackerras 
549b6b563cSPaul Mackerras /*
559b6b563cSPaul Mackerras  * This is the size of the local memory reserve map that gets copied
569b6b563cSPaul Mackerras  * into the boot params passed to the kernel. That size is totally
579b6b563cSPaul Mackerras  * flexible as the kernel just reads the list until it encounters an
589b6b563cSPaul Mackerras  * entry with size 0, so it can be changed without breaking binary
599b6b563cSPaul Mackerras  * compatibility
609b6b563cSPaul Mackerras  */
619b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE	8
629b6b563cSPaul Mackerras 
639b6b563cSPaul Mackerras /*
649b6b563cSPaul Mackerras  * prom_init() is called very early on, before the kernel text
659b6b563cSPaul Mackerras  * and data have been mapped to KERNELBASE.  At this point the code
669b6b563cSPaul Mackerras  * is running at whatever address it has been loaded at.
679b6b563cSPaul Mackerras  * On ppc32 we compile with -mrelocatable, which means that references
689b6b563cSPaul Mackerras  * to extern and static variables get relocated automatically.
69*5ac47f7aSAnton Blanchard  * ppc64 objects are always relocatable, we just need to relocate the
70*5ac47f7aSAnton Blanchard  * TOC.
719b6b563cSPaul Mackerras  *
729b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
739b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
749b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
759b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
769b6b563cSPaul Mackerras  *
779b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
789b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
799b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
809b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
819b6b563cSPaul Mackerras  */
82*5ac47f7aSAnton Blanchard #define RELOC(x)	(x)
83*5ac47f7aSAnton Blanchard #define ADDR(x)		(u32)(unsigned long)(x)
84*5ac47f7aSAnton Blanchard 
859b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
86a23414beSPaul Mackerras #define OF_WORKAROUNDS	0
879b6b563cSPaul Mackerras #else
88a23414beSPaul Mackerras #define OF_WORKAROUNDS	of_workarounds
89a23414beSPaul Mackerras int of_workarounds;
909b6b563cSPaul Mackerras #endif
919b6b563cSPaul Mackerras 
92a23414beSPaul Mackerras #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
93a23414beSPaul Mackerras #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
94a23414beSPaul Mackerras 
959b6b563cSPaul Mackerras #define PROM_BUG() do {						\
969b6b563cSPaul Mackerras         prom_printf("kernel BUG at %s line 0x%x!\n",		\
979b6b563cSPaul Mackerras 		    RELOC(__FILE__), __LINE__);			\
989b6b563cSPaul Mackerras         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
999b6b563cSPaul Mackerras } while (0)
1009b6b563cSPaul Mackerras 
1019b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1029b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1039b6b563cSPaul Mackerras #else
1049b6b563cSPaul Mackerras #define prom_debug(x...)
1059b6b563cSPaul Mackerras #endif
1069b6b563cSPaul Mackerras 
1079b6b563cSPaul Mackerras 
1089b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1099b6b563cSPaul Mackerras 
1109b6b563cSPaul Mackerras struct prom_args {
1119b6b563cSPaul Mackerras         u32 service;
1129b6b563cSPaul Mackerras         u32 nargs;
1139b6b563cSPaul Mackerras         u32 nret;
1149b6b563cSPaul Mackerras         prom_arg_t args[10];
1159b6b563cSPaul Mackerras };
1169b6b563cSPaul Mackerras 
1179b6b563cSPaul Mackerras struct prom_t {
1189b6b563cSPaul Mackerras 	ihandle root;
119a23414beSPaul Mackerras 	phandle chosen;
1209b6b563cSPaul Mackerras 	int cpu;
1219b6b563cSPaul Mackerras 	ihandle stdout;
122a575b807SPaul Mackerras 	ihandle mmumap;
123a23414beSPaul Mackerras 	ihandle memory;
1249b6b563cSPaul Mackerras };
1259b6b563cSPaul Mackerras 
1269b6b563cSPaul Mackerras struct mem_map_entry {
127cbbcf340SKumar Gala 	u64	base;
128cbbcf340SKumar Gala 	u64	size;
1299b6b563cSPaul Mackerras };
1309b6b563cSPaul Mackerras 
1319b6b563cSPaul Mackerras typedef u32 cell_t;
1329b6b563cSPaul Mackerras 
1336e35d5daSBenjamin Herrenschmidt extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
1346e35d5daSBenjamin Herrenschmidt 		    unsigned long r6, unsigned long r7, unsigned long r8,
1356e35d5daSBenjamin Herrenschmidt 		    unsigned long r9);
1369b6b563cSPaul Mackerras 
1379b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
138c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1399b6b563cSPaul Mackerras #else
140c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1419b6b563cSPaul Mackerras {
142c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1439b6b563cSPaul Mackerras }
1449b6b563cSPaul Mackerras #endif
1459b6b563cSPaul Mackerras 
1469b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1479b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1489b6b563cSPaul Mackerras 
1499b6b563cSPaul Mackerras /* prom structure */
1509b6b563cSPaul Mackerras static struct prom_t __initdata prom;
1519b6b563cSPaul Mackerras 
1529b6b563cSPaul Mackerras static unsigned long prom_entry __initdata;
1539b6b563cSPaul Mackerras 
1549b6b563cSPaul Mackerras #define PROM_SCRATCH_SIZE 256
1559b6b563cSPaul Mackerras 
1569b6b563cSPaul Mackerras static char __initdata of_stdout_device[256];
1579b6b563cSPaul Mackerras static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
1589b6b563cSPaul Mackerras 
1599b6b563cSPaul Mackerras static unsigned long __initdata dt_header_start;
1609b6b563cSPaul Mackerras static unsigned long __initdata dt_struct_start, dt_struct_end;
1619b6b563cSPaul Mackerras static unsigned long __initdata dt_string_start, dt_string_end;
1629b6b563cSPaul Mackerras 
1639b6b563cSPaul Mackerras static unsigned long __initdata prom_initrd_start, prom_initrd_end;
1649b6b563cSPaul Mackerras 
1659b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
166165785e5SJeremy Kerr static int __initdata prom_iommu_force_on;
167165785e5SJeremy Kerr static int __initdata prom_iommu_off;
1689b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_start;
1699b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_end;
1709b6b563cSPaul Mackerras #endif
1719b6b563cSPaul Mackerras 
172e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
173e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
174e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
175e8222502SBenjamin Herrenschmidt  */
176e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
177e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
178e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
179e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
180e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
18127f44888SBenjamin Herrenschmidt #define PLATFORM_OPAL		0x0600
182e8222502SBenjamin Herrenschmidt 
1839b6b563cSPaul Mackerras static int __initdata of_platform;
1849b6b563cSPaul Mackerras 
1859b6b563cSPaul Mackerras static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
1869b6b563cSPaul Mackerras 
187cf68787bSBenjamin Krill static unsigned long __initdata prom_memory_limit;
188cf68787bSBenjamin Krill 
1899b6b563cSPaul Mackerras static unsigned long __initdata alloc_top;
1909b6b563cSPaul Mackerras static unsigned long __initdata alloc_top_high;
1919b6b563cSPaul Mackerras static unsigned long __initdata alloc_bottom;
1929b6b563cSPaul Mackerras static unsigned long __initdata rmo_top;
1939b6b563cSPaul Mackerras static unsigned long __initdata ram_top;
1949b6b563cSPaul Mackerras 
1959b6b563cSPaul Mackerras static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
1969b6b563cSPaul Mackerras static int __initdata mem_reserve_cnt;
1979b6b563cSPaul Mackerras 
1989b6b563cSPaul Mackerras static cell_t __initdata regbuf[1024];
1999b6b563cSPaul Mackerras 
2009b6b563cSPaul Mackerras 
2019b6b563cSPaul Mackerras /*
2029b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2039b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2049b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2059b6b563cSPaul Mackerras  * it is valid
2069b6b563cSPaul Mackerras  */
2079b6b563cSPaul Mackerras 
2089b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2099b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2109b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2119b6b563cSPaul Mackerras 
2129b6b563cSPaul Mackerras 
2139b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
2149b6b563cSPaul Mackerras  * firmware.
2159b6b563cSPaul Mackerras  */
2169b6b563cSPaul Mackerras 
2179b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
2189b6b563cSPaul Mackerras {
2199b6b563cSPaul Mackerras 	int i;
2209b6b563cSPaul Mackerras 	struct prom_args args;
2219b6b563cSPaul Mackerras 	va_list list;
2229b6b563cSPaul Mackerras 
2239b6b563cSPaul Mackerras 	args.service = ADDR(service);
2249b6b563cSPaul Mackerras 	args.nargs = nargs;
2259b6b563cSPaul Mackerras 	args.nret = nret;
2269b6b563cSPaul Mackerras 
2279b6b563cSPaul Mackerras 	va_start(list, nret);
2289b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
2299b6b563cSPaul Mackerras 		args.args[i] = va_arg(list, prom_arg_t);
2309b6b563cSPaul Mackerras 	va_end(list);
2319b6b563cSPaul Mackerras 
2329b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
2339b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
2349b6b563cSPaul Mackerras 
235c4988820SPaul Mackerras 	if (enter_prom(&args, RELOC(prom_entry)) < 0)
236c4988820SPaul Mackerras 		return PROM_ERROR;
2379b6b563cSPaul Mackerras 
2389b6b563cSPaul Mackerras 	return (nret > 0) ? args.args[nargs] : 0;
2399b6b563cSPaul Mackerras }
2409b6b563cSPaul Mackerras 
2419b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
2429b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
2439b6b563cSPaul Mackerras {
2449b6b563cSPaul Mackerras 	int i;
2459b6b563cSPaul Mackerras 	struct prom_args args;
2469b6b563cSPaul Mackerras 	va_list list;
2479b6b563cSPaul Mackerras 
2489b6b563cSPaul Mackerras 	args.service = ADDR(service);
2499b6b563cSPaul Mackerras 	args.nargs = nargs;
2509b6b563cSPaul Mackerras 	args.nret = nret;
2519b6b563cSPaul Mackerras 
2529b6b563cSPaul Mackerras 	va_start(list, rets);
2539b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
2549b6b563cSPaul Mackerras 		args.args[i] = va_arg(list, prom_arg_t);
2559b6b563cSPaul Mackerras 	va_end(list);
2569b6b563cSPaul Mackerras 
2579b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
258ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
2599b6b563cSPaul Mackerras 
260c4988820SPaul Mackerras 	if (enter_prom(&args, RELOC(prom_entry)) < 0)
261c4988820SPaul Mackerras 		return PROM_ERROR;
2629b6b563cSPaul Mackerras 
2639b6b563cSPaul Mackerras 	if (rets != NULL)
2649b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
265c5200c90SPaul Mackerras 			rets[i-1] = args.args[nargs+i];
2669b6b563cSPaul Mackerras 
2679b6b563cSPaul Mackerras 	return (nret > 0) ? args.args[nargs] : 0;
2689b6b563cSPaul Mackerras }
2699b6b563cSPaul Mackerras 
2709b6b563cSPaul Mackerras 
2719b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
2729b6b563cSPaul Mackerras {
2739b6b563cSPaul Mackerras 	const char *p, *q;
2749b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
2759b6b563cSPaul Mackerras 
2769b6b563cSPaul Mackerras 	if (_prom->stdout == 0)
2779b6b563cSPaul Mackerras 		return;
2789b6b563cSPaul Mackerras 
2799b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
2809b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
2819b6b563cSPaul Mackerras 			;
2829b6b563cSPaul Mackerras 		if (q > p)
2839b6b563cSPaul Mackerras 			call_prom("write", 3, 1, _prom->stdout, p, q - p);
2849b6b563cSPaul Mackerras 		if (*q == 0)
2859b6b563cSPaul Mackerras 			break;
2869b6b563cSPaul Mackerras 		++q;
2879b6b563cSPaul Mackerras 		call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
2889b6b563cSPaul Mackerras 	}
2899b6b563cSPaul Mackerras }
2909b6b563cSPaul Mackerras 
2919b6b563cSPaul Mackerras 
2929b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
2939b6b563cSPaul Mackerras {
2949b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
2959b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
2969b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
2979b6b563cSPaul Mackerras 
2989b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
2999b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
3009b6b563cSPaul Mackerras 		if (buf[i] > '9')
3019b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
3029b6b563cSPaul Mackerras 		val >>= 4;
3039b6b563cSPaul Mackerras 	}
3049b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
3059b6b563cSPaul Mackerras 	call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
3069b6b563cSPaul Mackerras }
3079b6b563cSPaul Mackerras 
3082c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */
3092c48a7d6SMichael Neuling #define UL_DIGITS 21
3102c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val)
3112c48a7d6SMichael Neuling {
3122c48a7d6SMichael Neuling 	int i, size;
3132c48a7d6SMichael Neuling 	char buf[UL_DIGITS+1];
3142c48a7d6SMichael Neuling 	struct prom_t *_prom = &RELOC(prom);
3152c48a7d6SMichael Neuling 
3162c48a7d6SMichael Neuling 	for (i = UL_DIGITS-1; i >= 0;  i--) {
3172c48a7d6SMichael Neuling 		buf[i] = (val % 10) + '0';
3182c48a7d6SMichael Neuling 		val = val/10;
3192c48a7d6SMichael Neuling 		if (val == 0)
3202c48a7d6SMichael Neuling 			break;
3212c48a7d6SMichael Neuling 	}
3222c48a7d6SMichael Neuling 	/* shift stuff down */
3232c48a7d6SMichael Neuling 	size = UL_DIGITS - i;
3242c48a7d6SMichael Neuling 	call_prom("write", 3, 1, _prom->stdout, buf+i, size);
3252c48a7d6SMichael Neuling }
3269b6b563cSPaul Mackerras 
3279b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
3289b6b563cSPaul Mackerras {
3299b6b563cSPaul Mackerras 	const char *p, *q, *s;
3309b6b563cSPaul Mackerras 	va_list args;
3319b6b563cSPaul Mackerras 	unsigned long v;
332af277149SBenjamin Herrenschmidt 	long vs;
3339b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
3349b6b563cSPaul Mackerras 
3359b6b563cSPaul Mackerras 	va_start(args, format);
3369b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
3379b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
3389b6b563cSPaul Mackerras 			;
3399b6b563cSPaul Mackerras 		if (q > p)
3409b6b563cSPaul Mackerras 			call_prom("write", 3, 1, _prom->stdout, p, q - p);
3419b6b563cSPaul Mackerras 		if (*q == 0)
3429b6b563cSPaul Mackerras 			break;
3439b6b563cSPaul Mackerras 		if (*q == '\n') {
3449b6b563cSPaul Mackerras 			++q;
3459b6b563cSPaul Mackerras 			call_prom("write", 3, 1, _prom->stdout,
3469b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
3479b6b563cSPaul Mackerras 			continue;
3489b6b563cSPaul Mackerras 		}
3499b6b563cSPaul Mackerras 		++q;
3509b6b563cSPaul Mackerras 		if (*q == 0)
3519b6b563cSPaul Mackerras 			break;
3529b6b563cSPaul Mackerras 		switch (*q) {
3539b6b563cSPaul Mackerras 		case 's':
3549b6b563cSPaul Mackerras 			++q;
3559b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
3569b6b563cSPaul Mackerras 			prom_print(s);
3579b6b563cSPaul Mackerras 			break;
3589b6b563cSPaul Mackerras 		case 'x':
3599b6b563cSPaul Mackerras 			++q;
3609b6b563cSPaul Mackerras 			v = va_arg(args, unsigned long);
3619b6b563cSPaul Mackerras 			prom_print_hex(v);
3629b6b563cSPaul Mackerras 			break;
363af277149SBenjamin Herrenschmidt 		case 'd':
364af277149SBenjamin Herrenschmidt 			++q;
365af277149SBenjamin Herrenschmidt 			vs = va_arg(args, int);
366af277149SBenjamin Herrenschmidt 			if (vs < 0) {
367af277149SBenjamin Herrenschmidt 				prom_print(RELOC("-"));
368af277149SBenjamin Herrenschmidt 				vs = -vs;
369af277149SBenjamin Herrenschmidt 			}
370af277149SBenjamin Herrenschmidt 			prom_print_dec(vs);
371af277149SBenjamin Herrenschmidt 			break;
3722c48a7d6SMichael Neuling 		case 'l':
3732c48a7d6SMichael Neuling 			++q;
374af277149SBenjamin Herrenschmidt 			if (*q == 0)
375af277149SBenjamin Herrenschmidt 				break;
376af277149SBenjamin Herrenschmidt 			else if (*q == 'x') {
377af277149SBenjamin Herrenschmidt 				++q;
378af277149SBenjamin Herrenschmidt 				v = va_arg(args, unsigned long);
379af277149SBenjamin Herrenschmidt 				prom_print_hex(v);
380af277149SBenjamin Herrenschmidt 			} else if (*q == 'u') { /* '%lu' */
3812c48a7d6SMichael Neuling 				++q;
3822c48a7d6SMichael Neuling 				v = va_arg(args, unsigned long);
3832c48a7d6SMichael Neuling 				prom_print_dec(v);
384af277149SBenjamin Herrenschmidt 			} else if (*q == 'd') { /* %ld */
385af277149SBenjamin Herrenschmidt 				++q;
386af277149SBenjamin Herrenschmidt 				vs = va_arg(args, long);
387af277149SBenjamin Herrenschmidt 				if (vs < 0) {
388af277149SBenjamin Herrenschmidt 					prom_print(RELOC("-"));
389af277149SBenjamin Herrenschmidt 					vs = -vs;
390af277149SBenjamin Herrenschmidt 				}
391af277149SBenjamin Herrenschmidt 				prom_print_dec(vs);
3922c48a7d6SMichael Neuling 			}
3932c48a7d6SMichael Neuling 			break;
3949b6b563cSPaul Mackerras 		}
3959b6b563cSPaul Mackerras 	}
3969b6b563cSPaul Mackerras }
3979b6b563cSPaul Mackerras 
3989b6b563cSPaul Mackerras 
399a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
400a575b807SPaul Mackerras 				unsigned long align)
401a575b807SPaul Mackerras {
402a575b807SPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
403a575b807SPaul Mackerras 
404a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
405a23414beSPaul Mackerras 		/*
406a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
407a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
408a23414beSPaul Mackerras 		 */
409a23414beSPaul Mackerras 		int ret;
410a23414beSPaul Mackerras 		prom_arg_t result;
411a23414beSPaul Mackerras 
412a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
413a23414beSPaul Mackerras 				    ADDR("claim"), _prom->memory,
414a23414beSPaul Mackerras 				    align, size, virt);
415a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
416a23414beSPaul Mackerras 			return -1;
417a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
418a23414beSPaul Mackerras 				    ADDR("claim"), _prom->mmumap,
419a23414beSPaul Mackerras 				    align, size, virt);
420a23414beSPaul Mackerras 		if (ret != 0) {
421a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
422a23414beSPaul Mackerras 				  _prom->memory, size, virt);
423a23414beSPaul Mackerras 			return -1;
424a23414beSPaul Mackerras 		}
425a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
426a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
427a23414beSPaul Mackerras 			  ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
428a23414beSPaul Mackerras 		return virt;
429a23414beSPaul Mackerras 	}
430a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
431a23414beSPaul Mackerras 			 (prom_arg_t)align);
432a575b807SPaul Mackerras }
433a575b807SPaul Mackerras 
4349b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
4359b6b563cSPaul Mackerras {
4369b6b563cSPaul Mackerras 	prom_print(reason);
437add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
438add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
439add60ef3SOlaf Hering 	if (RELOC(of_platform) == PLATFORM_POWERMAC)
440add60ef3SOlaf Hering 		asm("trap\n");
441add60ef3SOlaf Hering 
4421d9a4731SStephen Rothwell 	/* ToDo: should put up an SRC here on pSeries */
4439b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
4449b6b563cSPaul Mackerras 
4459b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
4469b6b563cSPaul Mackerras 		;
4479b6b563cSPaul Mackerras }
4489b6b563cSPaul Mackerras 
4499b6b563cSPaul Mackerras 
4509b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
4519b6b563cSPaul Mackerras {
4529b6b563cSPaul Mackerras 	phandle node;
4539b6b563cSPaul Mackerras 
4549b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
4559b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
4569b6b563cSPaul Mackerras 		return 1;
4579b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4589b6b563cSPaul Mackerras 		return 1;
4599b6b563cSPaul Mackerras 	for (;;) {
4609b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
4619b6b563cSPaul Mackerras 			return 0;
4629b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4639b6b563cSPaul Mackerras 			return 1;
4649b6b563cSPaul Mackerras 	}
4659b6b563cSPaul Mackerras }
4669b6b563cSPaul Mackerras 
46721fe3301SBenjamin Herrenschmidt static int inline prom_getprop(phandle node, const char *pname,
4689b6b563cSPaul Mackerras 			       void *value, size_t valuelen)
4699b6b563cSPaul Mackerras {
4709b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
4719b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
4729b6b563cSPaul Mackerras }
4739b6b563cSPaul Mackerras 
47421fe3301SBenjamin Herrenschmidt static int inline prom_getproplen(phandle node, const char *pname)
4759b6b563cSPaul Mackerras {
4769b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
4779b6b563cSPaul Mackerras }
4789b6b563cSPaul Mackerras 
479a23414beSPaul Mackerras static void add_string(char **str, const char *q)
4809b6b563cSPaul Mackerras {
481a23414beSPaul Mackerras 	char *p = *str;
482a23414beSPaul Mackerras 
483a23414beSPaul Mackerras 	while (*q)
484a23414beSPaul Mackerras 		*p++ = *q++;
485a23414beSPaul Mackerras 	*p++ = ' ';
486a23414beSPaul Mackerras 	*str = p;
487a23414beSPaul Mackerras }
488a23414beSPaul Mackerras 
489a23414beSPaul Mackerras static char *tohex(unsigned int x)
490a23414beSPaul Mackerras {
491a23414beSPaul Mackerras 	static char digits[] = "0123456789abcdef";
492a23414beSPaul Mackerras 	static char result[9];
493a23414beSPaul Mackerras 	int i;
494a23414beSPaul Mackerras 
495a23414beSPaul Mackerras 	result[8] = 0;
496a23414beSPaul Mackerras 	i = 8;
497a23414beSPaul Mackerras 	do {
498a23414beSPaul Mackerras 		--i;
499a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
500a23414beSPaul Mackerras 		x >>= 4;
501a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
502a23414beSPaul Mackerras 	return &result[i];
503a23414beSPaul Mackerras }
504a23414beSPaul Mackerras 
505a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
506a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
507a23414beSPaul Mackerras {
508a23414beSPaul Mackerras 	char cmd[256], *p;
509a23414beSPaul Mackerras 
510a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
5119b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
5129b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
513a23414beSPaul Mackerras 
514a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
515a23414beSPaul Mackerras 	p = cmd;
516a23414beSPaul Mackerras 	add_string(&p, "dev");
517a23414beSPaul Mackerras 	add_string(&p, nodename);
518a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
519a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
520a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
521a23414beSPaul Mackerras 	add_string(&p, tohex(strlen(RELOC(pname))));
522a23414beSPaul Mackerras 	add_string(&p, "property");
523a23414beSPaul Mackerras 	*p = 0;
524a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
5259b6b563cSPaul Mackerras }
5269b6b563cSPaul Mackerras 
527cf68787bSBenjamin Krill /* We can't use the standard versions because of RELOC headaches. */
528cf68787bSBenjamin Krill #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
529cf68787bSBenjamin Krill 			 || ('a' <= (c) && (c) <= 'f') \
530cf68787bSBenjamin Krill 			 || ('A' <= (c) && (c) <= 'F'))
531cf68787bSBenjamin Krill 
532cf68787bSBenjamin Krill #define isdigit(c)	('0' <= (c) && (c) <= '9')
533cf68787bSBenjamin Krill #define islower(c)	('a' <= (c) && (c) <= 'z')
534cf68787bSBenjamin Krill #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
535cf68787bSBenjamin Krill 
536cf68787bSBenjamin Krill unsigned long prom_strtoul(const char *cp, const char **endp)
537cf68787bSBenjamin Krill {
538cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
539cf68787bSBenjamin Krill 
540cf68787bSBenjamin Krill 	if (*cp == '0') {
541cf68787bSBenjamin Krill 		base = 8;
542cf68787bSBenjamin Krill 		cp++;
543cf68787bSBenjamin Krill 		if (toupper(*cp) == 'X') {
544cf68787bSBenjamin Krill 			cp++;
545cf68787bSBenjamin Krill 			base = 16;
546cf68787bSBenjamin Krill 		}
547cf68787bSBenjamin Krill 	}
548cf68787bSBenjamin Krill 
549cf68787bSBenjamin Krill 	while (isxdigit(*cp) &&
550cf68787bSBenjamin Krill 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
551cf68787bSBenjamin Krill 		result = result * base + value;
552cf68787bSBenjamin Krill 		cp++;
553cf68787bSBenjamin Krill 	}
554cf68787bSBenjamin Krill 
555cf68787bSBenjamin Krill 	if (endp)
556cf68787bSBenjamin Krill 		*endp = cp;
557cf68787bSBenjamin Krill 
558cf68787bSBenjamin Krill 	return result;
559cf68787bSBenjamin Krill }
560cf68787bSBenjamin Krill 
561cf68787bSBenjamin Krill unsigned long prom_memparse(const char *ptr, const char **retptr)
562cf68787bSBenjamin Krill {
563cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
564cf68787bSBenjamin Krill 	int shift = 0;
565cf68787bSBenjamin Krill 
566cf68787bSBenjamin Krill 	/*
567cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
568cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
569cf68787bSBenjamin Krill 	 * the address we're linked at.
570cf68787bSBenjamin Krill 	 */
571cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
572cf68787bSBenjamin Krill 		shift = 30;
573cf68787bSBenjamin Krill 
574cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
575cf68787bSBenjamin Krill 		shift = 20;
576cf68787bSBenjamin Krill 
577cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
578cf68787bSBenjamin Krill 		shift = 10;
579cf68787bSBenjamin Krill 
580cf68787bSBenjamin Krill 	if (shift) {
581cf68787bSBenjamin Krill 		ret <<= shift;
582cf68787bSBenjamin Krill 		(*retptr)++;
583cf68787bSBenjamin Krill 	}
584cf68787bSBenjamin Krill 
585cf68787bSBenjamin Krill 	return ret;
586cf68787bSBenjamin Krill }
587cf68787bSBenjamin Krill 
5889b6b563cSPaul Mackerras /*
5899b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
5909b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
5919b6b563cSPaul Mackerras  */
5929b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
5939b6b563cSPaul Mackerras {
5949b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
595cc5d0189SBenjamin Herrenschmidt 	const char *opt;
596cf68787bSBenjamin Krill 
597cc5d0189SBenjamin Herrenschmidt 	char *p;
5989b6b563cSPaul Mackerras 	int l = 0;
5999b6b563cSPaul Mackerras 
6009b6b563cSPaul Mackerras 	RELOC(prom_cmd_line[0]) = 0;
6019b6b563cSPaul Mackerras 	p = RELOC(prom_cmd_line);
6029b6b563cSPaul Mackerras 	if ((long)_prom->chosen > 0)
6039b6b563cSPaul Mackerras 		l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
6049b6b563cSPaul Mackerras #ifdef CONFIG_CMDLINE
6050e4aa9c2SAmos Waterland 	if (l <= 0 || p[0] == '\0') /* dbl check */
6069b6b563cSPaul Mackerras 		strlcpy(RELOC(prom_cmd_line),
6079b6b563cSPaul Mackerras 			RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
6089b6b563cSPaul Mackerras #endif /* CONFIG_CMDLINE */
6099b6b563cSPaul Mackerras 	prom_printf("command line: %s\n", RELOC(prom_cmd_line));
6109b6b563cSPaul Mackerras 
6119b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
6129b6b563cSPaul Mackerras 	opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
6139b6b563cSPaul Mackerras 	if (opt) {
6149b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
6159b6b563cSPaul Mackerras 		opt += 6;
6169b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
6179b6b563cSPaul Mackerras 			opt++;
6189b6b563cSPaul Mackerras 		if (!strncmp(opt, RELOC("off"), 3))
619165785e5SJeremy Kerr 			RELOC(prom_iommu_off) = 1;
6209b6b563cSPaul Mackerras 		else if (!strncmp(opt, RELOC("force"), 5))
621165785e5SJeremy Kerr 			RELOC(prom_iommu_force_on) = 1;
6229b6b563cSPaul Mackerras 	}
6239b6b563cSPaul Mackerras #endif
624cf68787bSBenjamin Krill 	opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
625cf68787bSBenjamin Krill 	if (opt) {
626cf68787bSBenjamin Krill 		opt += 4;
627cf68787bSBenjamin Krill 		RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
628cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
629cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
630cf68787bSBenjamin Krill 		RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
631cf68787bSBenjamin Krill #endif
632cf68787bSBenjamin Krill 	}
6339b6b563cSPaul Mackerras }
6349b6b563cSPaul Mackerras 
63527f44888SBenjamin Herrenschmidt #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
6369b6b563cSPaul Mackerras /*
637f709bfacSPaul Mackerras  * There are two methods for telling firmware what our capabilities are.
638f709bfacSPaul Mackerras  * Newer machines have an "ibm,client-architecture-support" method on the
639f709bfacSPaul Mackerras  * root node.  For older machines, we have to call the "process-elf-header"
640f709bfacSPaul Mackerras  * method in the /packages/elf-loader node, passing it a fake 32-bit
641f709bfacSPaul Mackerras  * ELF header containing a couple of PT_NOTE sections that contain
642f709bfacSPaul Mackerras  * structures that contain various information.
6439b6b563cSPaul Mackerras  */
644f709bfacSPaul Mackerras 
645f709bfacSPaul Mackerras /*
646f709bfacSPaul Mackerras  * New method - extensible architecture description vector.
647f709bfacSPaul Mackerras  *
648f709bfacSPaul Mackerras  * Because the description vector contains a mix of byte and word
649f709bfacSPaul Mackerras  * values, we declare it as an unsigned char array, and use this
650f709bfacSPaul Mackerras  * macro to put word values in.
651f709bfacSPaul Mackerras  */
652f709bfacSPaul Mackerras #define W(x)	((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
653f709bfacSPaul Mackerras 		((x) >> 8) & 0xff, (x) & 0xff
654f709bfacSPaul Mackerras 
655f709bfacSPaul Mackerras /* Option vector bits - generic bits in byte 1 */
656f709bfacSPaul Mackerras #define OV_IGNORE		0x80	/* ignore this vector */
657f709bfacSPaul Mackerras #define OV_CESSATION_POLICY	0x40	/* halt if unsupported option present*/
658f709bfacSPaul Mackerras 
659f709bfacSPaul Mackerras /* Option vector 1: processor architectures supported */
660f709bfacSPaul Mackerras #define OV1_PPC_2_00		0x80	/* set if we support PowerPC 2.00 */
661f709bfacSPaul Mackerras #define OV1_PPC_2_01		0x40	/* set if we support PowerPC 2.01 */
662f709bfacSPaul Mackerras #define OV1_PPC_2_02		0x20	/* set if we support PowerPC 2.02 */
663f709bfacSPaul Mackerras #define OV1_PPC_2_03		0x10	/* set if we support PowerPC 2.03 */
664f709bfacSPaul Mackerras #define OV1_PPC_2_04		0x08	/* set if we support PowerPC 2.04 */
665f709bfacSPaul Mackerras #define OV1_PPC_2_05		0x04	/* set if we support PowerPC 2.05 */
6660cb99013SJoel Schopp #define OV1_PPC_2_06		0x02	/* set if we support PowerPC 2.06 */
667df77c799SMichael Neuling #define OV1_PPC_2_07		0x01	/* set if we support PowerPC 2.07 */
668f709bfacSPaul Mackerras 
669f709bfacSPaul Mackerras /* Option vector 2: Open Firmware options supported */
670f709bfacSPaul Mackerras #define OV2_REAL_MODE		0x20	/* set if we want OF in real mode */
671f709bfacSPaul Mackerras 
672f709bfacSPaul Mackerras /* Option vector 3: processor options supported */
673f709bfacSPaul Mackerras #define OV3_FP			0x80	/* floating point */
674f709bfacSPaul Mackerras #define OV3_VMX			0x40	/* VMX/Altivec */
675974a76f5SPaul Mackerras #define OV3_DFP			0x20	/* decimal FP */
676f709bfacSPaul Mackerras 
677404e32e4SRobert Jennings /* Option vector 4: IBM PAPR implementation */
678404e32e4SRobert Jennings #define OV4_MIN_ENT_CAP		0x01	/* minimum VP entitled capacity */
679404e32e4SRobert Jennings 
680f709bfacSPaul Mackerras /* Option vector 5: PAPR/OF options supported */
681f709bfacSPaul Mackerras #define OV5_LPAR		0x80	/* logical partitioning supported */
682f709bfacSPaul Mackerras #define OV5_SPLPAR		0x40	/* shared-processor LPAR supported */
683f709bfacSPaul Mackerras /* ibm,dynamic-reconfiguration-memory property supported */
684f709bfacSPaul Mackerras #define OV5_DRCONF_MEMORY	0x20
685f709bfacSPaul Mackerras #define OV5_LARGE_PAGES		0x10	/* large pages supported */
686d8c391a5SJake Moilanen #define OV5_DONATE_DEDICATE_CPU 0x02	/* donate dedicated CPU support */
687014dad90SMichael Ellerman /* PCIe/MSI support.  Without MSI full PCIe is not supported */
688014dad90SMichael Ellerman #ifdef CONFIG_PCI_MSI
689014dad90SMichael Ellerman #define OV5_MSI			0x01	/* PCIe/MSI support */
690014dad90SMichael Ellerman #else
691014dad90SMichael Ellerman #define OV5_MSI			0x00
692014dad90SMichael Ellerman #endif /* CONFIG_PCI_MSI */
6938391e42aSNathan Fontenot #ifdef CONFIG_PPC_SMLPAR
6948391e42aSNathan Fontenot #define OV5_CMO			0x80	/* Cooperative Memory Overcommitment */
6959ee820faSBrian King #define OV5_XCMO			0x40	/* Page Coalescing */
6968391e42aSNathan Fontenot #else
6978391e42aSNathan Fontenot #define OV5_CMO			0x00
6989ee820faSBrian King #define OV5_XCMO			0x00
6998391e42aSNathan Fontenot #endif
7004b83c330SAnton Blanchard #define OV5_TYPE1_AFFINITY	0x80	/* Type 1 NUMA affinity */
701828d2b59SKent Yoder #define OV5_PFO_HW_RNG		0x80	/* PFO Random Number Generator */
702da29aa8fSSeth Jennings #define OV5_PFO_HW_842		0x40	/* PFO Compression Accelerator */
7037e3a4fa1SKent Yoder #define OV5_PFO_HW_ENCR		0x20	/* PFO Encryption Accelerator */
704df77c799SMichael Neuling #define OV5_SUB_PROCESSORS	0x01    /* 1,2,or 4 Sub-Processors supported */
705f709bfacSPaul Mackerras 
70628bb9ee1Sjschopp@austin.ibm.com /* Option Vector 6: IBM PAPR hints */
70728bb9ee1Sjschopp@austin.ibm.com #define OV6_LINUX		0x02	/* Linux is our OS */
70828bb9ee1Sjschopp@austin.ibm.com 
709f709bfacSPaul Mackerras /*
710f709bfacSPaul Mackerras  * The architecture vector has an array of PVR mask/value pairs,
711f709bfacSPaul Mackerras  * followed by # option vectors - 1, followed by the option vectors.
712f709bfacSPaul Mackerras  */
713f709bfacSPaul Mackerras static unsigned char ibm_architecture_vec[] = {
714f709bfacSPaul Mackerras 	W(0xfffe0000), W(0x003a0000),	/* POWER5/POWER5+ */
71503054d51SAnton Blanchard 	W(0xffff0000), W(0x003e0000),	/* POWER6 */
716e952e6c4SMichael Neuling 	W(0xffff0000), W(0x003f0000),	/* POWER7 */
717df77c799SMichael Neuling 	W(0xffff0000), W(0x004b0000),	/* POWER8 */
718df77c799SMichael Neuling 	W(0xffffffff), W(0x0f000004),	/* all 2.07-compliant */
7190cb99013SJoel Schopp 	W(0xffffffff), W(0x0f000003),	/* all 2.06-compliant */
7200efbc18aSPaul Mackerras 	W(0xffffffff), W(0x0f000002),	/* all 2.05-compliant */
721f709bfacSPaul Mackerras 	W(0xfffffffe), W(0x0f000001),	/* all 2.04-compliant and earlier */
72228bb9ee1Sjschopp@austin.ibm.com 	6 - 1,				/* 6 option vectors */
723f709bfacSPaul Mackerras 
724f709bfacSPaul Mackerras 	/* option vector 1: processor architectures supported */
72511e9ed43SWill Schmidt 	3 - 2,				/* length */
726f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
727f709bfacSPaul Mackerras 	OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
728df77c799SMichael Neuling 	OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
729f709bfacSPaul Mackerras 
730f709bfacSPaul Mackerras 	/* option vector 2: Open Firmware options supported */
73111e9ed43SWill Schmidt 	34 - 2,				/* length */
732f709bfacSPaul Mackerras 	OV2_REAL_MODE,
733f709bfacSPaul Mackerras 	0, 0,
734f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_base */
735f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_size */
736f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_base */
737f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_size */
738f709bfacSPaul Mackerras 	W(0xffffffff),			/* load_base */
73933392640SAnton Blanchard 	W(256),				/* 256MB min RMA */
740f709bfacSPaul Mackerras 	W(0xffffffff),			/* full client load */
741f709bfacSPaul Mackerras 	0,				/* min RMA percentage of total RAM */
742f709bfacSPaul Mackerras 	48,				/* max log_2(hash table size) */
743f709bfacSPaul Mackerras 
744f709bfacSPaul Mackerras 	/* option vector 3: processor options supported */
74511e9ed43SWill Schmidt 	3 - 2,				/* length */
746f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
747974a76f5SPaul Mackerras 	OV3_FP | OV3_VMX | OV3_DFP,
748f709bfacSPaul Mackerras 
749f709bfacSPaul Mackerras 	/* option vector 4: IBM PAPR implementation */
750404e32e4SRobert Jennings 	3 - 2,				/* length */
751f709bfacSPaul Mackerras 	0,				/* don't halt */
752404e32e4SRobert Jennings 	OV4_MIN_ENT_CAP,		/* minimum VP entitled capacity */
753f709bfacSPaul Mackerras 
754f709bfacSPaul Mackerras 	/* option vector 5: PAPR/OF options */
755df77c799SMichael Neuling 	19 - 2,				/* length */
756f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
757d8c391a5SJake Moilanen 	OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
758d8c391a5SJake Moilanen 	OV5_DONATE_DEDICATE_CPU | OV5_MSI,
7598391e42aSNathan Fontenot 	0,
7609ee820faSBrian King 	OV5_CMO | OV5_XCMO,
7614b83c330SAnton Blanchard 	OV5_TYPE1_AFFINITY,
76228bb9ee1Sjschopp@austin.ibm.com 	0,
76328bb9ee1Sjschopp@austin.ibm.com 	0,
76428bb9ee1Sjschopp@austin.ibm.com 	0,
765efec959fSBenjamin Herrenschmidt 	/* WARNING: The offset of the "number of cores" field below
766efec959fSBenjamin Herrenschmidt 	 * must match by the macro below. Update the definition if
767efec959fSBenjamin Herrenschmidt 	 * the structure layout changes.
768efec959fSBenjamin Herrenschmidt 	 */
769df77c799SMichael Neuling #define IBM_ARCH_VEC_NRCORES_OFFSET	117
77028bb9ee1Sjschopp@austin.ibm.com 	W(NR_CPUS),			/* number of cores supported */
771828d2b59SKent Yoder 	0,
772828d2b59SKent Yoder 	0,
773828d2b59SKent Yoder 	0,
774828d2b59SKent Yoder 	0,
775da29aa8fSSeth Jennings 	OV5_PFO_HW_RNG | OV5_PFO_HW_ENCR | OV5_PFO_HW_842,
776df77c799SMichael Neuling 	OV5_SUB_PROCESSORS,
77728bb9ee1Sjschopp@austin.ibm.com 	/* option vector 6: IBM PAPR hints */
77828bb9ee1Sjschopp@austin.ibm.com 	4 - 2,				/* length */
77928bb9ee1Sjschopp@austin.ibm.com 	0,
78028bb9ee1Sjschopp@austin.ibm.com 	0,
78128bb9ee1Sjschopp@austin.ibm.com 	OV6_LINUX,
78228bb9ee1Sjschopp@austin.ibm.com 
783f709bfacSPaul Mackerras };
784f709bfacSPaul Mackerras 
785f709bfacSPaul Mackerras /* Old method - ELF header with PT_NOTE sections */
7869b6b563cSPaul Mackerras static struct fake_elf {
7879b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
7889b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
7899b6b563cSPaul Mackerras 	struct chrpnote {
7909b6b563cSPaul Mackerras 		u32	namesz;
7919b6b563cSPaul Mackerras 		u32	descsz;
7929b6b563cSPaul Mackerras 		u32	type;
7939b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
7949b6b563cSPaul Mackerras 		struct chrpdesc {
7959b6b563cSPaul Mackerras 			u32	real_mode;
7969b6b563cSPaul Mackerras 			u32	real_base;
7979b6b563cSPaul Mackerras 			u32	real_size;
7989b6b563cSPaul Mackerras 			u32	virt_base;
7999b6b563cSPaul Mackerras 			u32	virt_size;
8009b6b563cSPaul Mackerras 			u32	load_base;
8019b6b563cSPaul Mackerras 		} chrpdesc;
8029b6b563cSPaul Mackerras 	} chrpnote;
8039b6b563cSPaul Mackerras 	struct rpanote {
8049b6b563cSPaul Mackerras 		u32	namesz;
8059b6b563cSPaul Mackerras 		u32	descsz;
8069b6b563cSPaul Mackerras 		u32	type;
8079b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
8089b6b563cSPaul Mackerras 		struct rpadesc {
8099b6b563cSPaul Mackerras 			u32	lpar_affinity;
8109b6b563cSPaul Mackerras 			u32	min_rmo_size;
8119b6b563cSPaul Mackerras 			u32	min_rmo_percent;
8129b6b563cSPaul Mackerras 			u32	max_pft_size;
8139b6b563cSPaul Mackerras 			u32	splpar;
8149b6b563cSPaul Mackerras 			u32	min_load;
8159b6b563cSPaul Mackerras 			u32	new_mem_def;
8169b6b563cSPaul Mackerras 			u32	ignore_me;
8179b6b563cSPaul Mackerras 		} rpadesc;
8189b6b563cSPaul Mackerras 	} rpanote;
8195663a123SPaul Mackerras } fake_elf = {
8209b6b563cSPaul Mackerras 	.elfhdr = {
8219b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
8229b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
8239b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
8249b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
8259b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
8269b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
8279b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
8289b6b563cSPaul Mackerras 		.e_phnum = 2
8299b6b563cSPaul Mackerras 	},
8309b6b563cSPaul Mackerras 	.phdr = {
8319b6b563cSPaul Mackerras 		[0] = {
8329b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
8339b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
8349b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
8359b6b563cSPaul Mackerras 		}, [1] = {
8369b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
8379b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
8389b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
8399b6b563cSPaul Mackerras 		}
8409b6b563cSPaul Mackerras 	},
8419b6b563cSPaul Mackerras 	.chrpnote = {
8429b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
8439b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
8449b6b563cSPaul Mackerras 		.type = 0x1275,
8459b6b563cSPaul Mackerras 		.name = "PowerPC",
8469b6b563cSPaul Mackerras 		.chrpdesc = {
8479b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
8489b6b563cSPaul Mackerras 			.real_base = ~0U,
8499b6b563cSPaul Mackerras 			.real_size = ~0U,
8509b6b563cSPaul Mackerras 			.virt_base = ~0U,
8519b6b563cSPaul Mackerras 			.virt_size = ~0U,
8529b6b563cSPaul Mackerras 			.load_base = ~0U
8539b6b563cSPaul Mackerras 		},
8549b6b563cSPaul Mackerras 	},
8559b6b563cSPaul Mackerras 	.rpanote = {
8569b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
8579b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
8589b6b563cSPaul Mackerras 		.type = 0x12759999,
8599b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
8609b6b563cSPaul Mackerras 		.rpadesc = {
8615663a123SPaul Mackerras 			.lpar_affinity = 0,
8625663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
8639b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
8645663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
8659b6b563cSPaul Mackerras 			.splpar = 1,
8669b6b563cSPaul Mackerras 			.min_load = ~0U,
8675663a123SPaul Mackerras 			.new_mem_def = 0
8689b6b563cSPaul Mackerras 		}
8699b6b563cSPaul Mackerras 	}
8709b6b563cSPaul Mackerras };
8719b6b563cSPaul Mackerras 
872efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
873efec959fSBenjamin Herrenschmidt {
874efec959fSBenjamin Herrenschmidt 	phandle node;
875efec959fSBenjamin Herrenschmidt 	char type[64];
876efec959fSBenjamin Herrenschmidt 	unsigned int plen;
877efec959fSBenjamin Herrenschmidt 
878efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
879efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
880efec959fSBenjamin Herrenschmidt 		type[0] = 0;
881efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
882efec959fSBenjamin Herrenschmidt 
883efec959fSBenjamin Herrenschmidt 		if (strcmp(type, RELOC("cpu")))
884efec959fSBenjamin Herrenschmidt 			continue;
885efec959fSBenjamin Herrenschmidt 		/*
886efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
887efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
888efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
889efec959fSBenjamin Herrenschmidt 		 */
890efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
891efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
892efec959fSBenjamin Herrenschmidt 			break;
893efec959fSBenjamin Herrenschmidt 		plen >>= 2;
8942c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
895efec959fSBenjamin Herrenschmidt 
896efec959fSBenjamin Herrenschmidt 		/* Sanity check */
897efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
8982c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
899efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
900efec959fSBenjamin Herrenschmidt 			return 1;
901efec959fSBenjamin Herrenschmidt 		}
902efec959fSBenjamin Herrenschmidt 		return plen;
903efec959fSBenjamin Herrenschmidt 	}
904efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
905efec959fSBenjamin Herrenschmidt 
906efec959fSBenjamin Herrenschmidt 	return 1;
907efec959fSBenjamin Herrenschmidt 
908efec959fSBenjamin Herrenschmidt }
909efec959fSBenjamin Herrenschmidt 
910efec959fSBenjamin Herrenschmidt 
9119b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
9129b6b563cSPaul Mackerras {
913f709bfacSPaul Mackerras 	ihandle elfloader, root;
914f709bfacSPaul Mackerras 	prom_arg_t ret;
915efec959fSBenjamin Herrenschmidt 	u32 *cores;
9169b6b563cSPaul Mackerras 
917f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
918f709bfacSPaul Mackerras 	if (root != 0) {
919efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
920efec959fSBenjamin Herrenschmidt 		 *
921efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
922efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
923efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
924efec959fSBenjamin Herrenschmidt 		 */
925*5ac47f7aSAnton Blanchard 		cores = (u32 *)&ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET];
926efec959fSBenjamin Herrenschmidt 		if (*cores != NR_CPUS) {
927efec959fSBenjamin Herrenschmidt 			prom_printf("WARNING ! "
9282c48a7d6SMichael Neuling 				    "ibm_architecture_vec structure inconsistent: %lu!\n",
929efec959fSBenjamin Herrenschmidt 				    *cores);
930efec959fSBenjamin Herrenschmidt 		} else {
93133ad5e4bSAnton Blanchard 			*cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
9322c48a7d6SMichael Neuling 			prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
9332c48a7d6SMichael Neuling 				    *cores, NR_CPUS);
934efec959fSBenjamin Herrenschmidt 		}
935efec959fSBenjamin Herrenschmidt 
936f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
937049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
938f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
939f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
94033b74977SBenjamin Herrenschmidt 				  root,
941f709bfacSPaul Mackerras 				  ADDR(ibm_architecture_vec)) == 0) {
942f709bfacSPaul Mackerras 			/* the call exists... */
943f709bfacSPaul Mackerras 			if (ret)
9444da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
945f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
946f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
9474da727aeSAnton Blanchard 			prom_printf(" done\n");
948f709bfacSPaul Mackerras 			return;
949f709bfacSPaul Mackerras 		}
950f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
951049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
952f709bfacSPaul Mackerras 	}
953f709bfacSPaul Mackerras 
954f709bfacSPaul Mackerras 	/* no ibm,client-architecture-support call, try the old way */
9559b6b563cSPaul Mackerras 	elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
9569b6b563cSPaul Mackerras 	if (elfloader == 0) {
9579b6b563cSPaul Mackerras 		prom_printf("couldn't open /packages/elf-loader\n");
9589b6b563cSPaul Mackerras 		return;
9599b6b563cSPaul Mackerras 	}
9609b6b563cSPaul Mackerras 	call_prom("call-method", 3, 1, ADDR("process-elf-header"),
9619b6b563cSPaul Mackerras 			elfloader, ADDR(&fake_elf));
9629b6b563cSPaul Mackerras 	call_prom("close", 1, 0, elfloader);
9639b6b563cSPaul Mackerras }
9649b6b563cSPaul Mackerras #endif
9659b6b563cSPaul Mackerras 
9669b6b563cSPaul Mackerras /*
9679b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
9689b6b563cSPaul Mackerras  *
9699b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
9709b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
9719b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
9729b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
9739b6b563cSPaul Mackerras  *  never happen.
9749b6b563cSPaul Mackerras  *
9759b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
9769b6b563cSPaul Mackerras  *  TCEs overlap
9779b6b563cSPaul Mackerras  *
9789b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
9799b6b563cSPaul Mackerras  *
9809b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
9819b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
9829b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
9839b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
9849b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
9859b6b563cSPaul Mackerras  *
9869b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
9879b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
9889b6b563cSPaul Mackerras  *  reserved.
9899b6b563cSPaul Mackerras  */
9909b6b563cSPaul Mackerras 
9919b6b563cSPaul Mackerras 
9929b6b563cSPaul Mackerras /*
9939b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
9949b6b563cSPaul Mackerras  *
9959b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
9969b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
9979b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
9989b6b563cSPaul Mackerras  */
9999b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
10009b6b563cSPaul Mackerras {
1001c4988820SPaul Mackerras 	unsigned long base = RELOC(alloc_bottom);
10029b6b563cSPaul Mackerras 	unsigned long addr = 0;
10039b6b563cSPaul Mackerras 
1004c4988820SPaul Mackerras 	if (align)
1005c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
10069b6b563cSPaul Mackerras 	prom_debug("alloc_up(%x, %x)\n", size, align);
10079b6b563cSPaul Mackerras 	if (RELOC(ram_top) == 0)
10089b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
10099b6b563cSPaul Mackerras 
10109b6b563cSPaul Mackerras 	if (align)
10119b6b563cSPaul Mackerras 		base = _ALIGN_UP(RELOC(alloc_bottom), align);
10129b6b563cSPaul Mackerras 	else
10139b6b563cSPaul Mackerras 		base = RELOC(alloc_bottom);
10149b6b563cSPaul Mackerras 
10159b6b563cSPaul Mackerras 	for(; (base + size) <= RELOC(alloc_top);
10169b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
10179b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
10189b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1019c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
10209b6b563cSPaul Mackerras 			break;
10219b6b563cSPaul Mackerras 		addr = 0;
10229b6b563cSPaul Mackerras 		if (align == 0)
10239b6b563cSPaul Mackerras 			break;
10249b6b563cSPaul Mackerras 	}
10259b6b563cSPaul Mackerras 	if (addr == 0)
10269b6b563cSPaul Mackerras 		return 0;
1027966728ddSAnton Blanchard 	RELOC(alloc_bottom) = addr + size;
10289b6b563cSPaul Mackerras 
10299b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
10309b6b563cSPaul Mackerras 	prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
10319b6b563cSPaul Mackerras 	prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
10329b6b563cSPaul Mackerras 	prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
10339b6b563cSPaul Mackerras 	prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
10349b6b563cSPaul Mackerras 	prom_debug("  ram_top      : %x\n", RELOC(ram_top));
10359b6b563cSPaul Mackerras 
10369b6b563cSPaul Mackerras 	return addr;
10379b6b563cSPaul Mackerras }
10389b6b563cSPaul Mackerras 
10399b6b563cSPaul Mackerras /*
10409b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
10419b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
10429b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
10439b6b563cSPaul Mackerras  */
10449b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
10459b6b563cSPaul Mackerras 				       int highmem)
10469b6b563cSPaul Mackerras {
10479b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
10489b6b563cSPaul Mackerras 
10499b6b563cSPaul Mackerras 	prom_debug("alloc_down(%x, %x, %s)\n", size, align,
10509b6b563cSPaul Mackerras 		   highmem ? RELOC("(high)") : RELOC("(low)"));
10519b6b563cSPaul Mackerras 	if (RELOC(ram_top) == 0)
10529b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
10539b6b563cSPaul Mackerras 
10549b6b563cSPaul Mackerras 	if (highmem) {
10559b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
10569b6b563cSPaul Mackerras 		addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
10579b6b563cSPaul Mackerras 		if (addr <= RELOC(alloc_bottom))
10589b6b563cSPaul Mackerras 			return 0;
10599b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
10609b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
10619b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
10629b6b563cSPaul Mackerras 		 */
10639b6b563cSPaul Mackerras 		if (addr < RELOC(rmo_top)) {
10649b6b563cSPaul Mackerras 			/* Good, we are first */
10659b6b563cSPaul Mackerras 			if (RELOC(alloc_top) == RELOC(rmo_top))
10669b6b563cSPaul Mackerras 				RELOC(alloc_top) = RELOC(rmo_top) = addr;
10679b6b563cSPaul Mackerras 			else
10689b6b563cSPaul Mackerras 				return 0;
10699b6b563cSPaul Mackerras 		}
10709b6b563cSPaul Mackerras 		RELOC(alloc_top_high) = addr;
10719b6b563cSPaul Mackerras 		goto bail;
10729b6b563cSPaul Mackerras 	}
10739b6b563cSPaul Mackerras 
10749b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
10759b6b563cSPaul Mackerras 	for (; base > RELOC(alloc_bottom);
10769b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
10779b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
10789b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1079c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
10809b6b563cSPaul Mackerras 			break;
10819b6b563cSPaul Mackerras 		addr = 0;
10829b6b563cSPaul Mackerras 	}
10839b6b563cSPaul Mackerras 	if (addr == 0)
10849b6b563cSPaul Mackerras 		return 0;
10859b6b563cSPaul Mackerras 	RELOC(alloc_top) = addr;
10869b6b563cSPaul Mackerras 
10879b6b563cSPaul Mackerras  bail:
10889b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
10899b6b563cSPaul Mackerras 	prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
10909b6b563cSPaul Mackerras 	prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
10919b6b563cSPaul Mackerras 	prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
10929b6b563cSPaul Mackerras 	prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
10939b6b563cSPaul Mackerras 	prom_debug("  ram_top      : %x\n", RELOC(ram_top));
10949b6b563cSPaul Mackerras 
10959b6b563cSPaul Mackerras 	return addr;
10969b6b563cSPaul Mackerras }
10979b6b563cSPaul Mackerras 
10989b6b563cSPaul Mackerras /*
10999b6b563cSPaul Mackerras  * Parse a "reg" cell
11009b6b563cSPaul Mackerras  */
11019b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
11029b6b563cSPaul Mackerras {
11039b6b563cSPaul Mackerras 	cell_t *p = *cellp;
11049b6b563cSPaul Mackerras 	unsigned long r = 0;
11059b6b563cSPaul Mackerras 
11069b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
11079b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
11089b6b563cSPaul Mackerras 		p++;
11099b6b563cSPaul Mackerras 		s--;
11109b6b563cSPaul Mackerras 	}
11119b6b563cSPaul Mackerras 	r = *p++;
11129b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
111335499c01SPaul Mackerras 	if (s > 1) {
11149b6b563cSPaul Mackerras 		r <<= 32;
11159b6b563cSPaul Mackerras 		r |= *(p++);
11169b6b563cSPaul Mackerras 	}
11179b6b563cSPaul Mackerras #endif
11189b6b563cSPaul Mackerras 	*cellp = p;
11199b6b563cSPaul Mackerras 	return r;
11209b6b563cSPaul Mackerras }
11219b6b563cSPaul Mackerras 
11229b6b563cSPaul Mackerras /*
11239b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
11249b6b563cSPaul Mackerras  * we don't need anything smarter at this point
11259b6b563cSPaul Mackerras  *
11269b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
11279b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
11289b6b563cSPaul Mackerras  * them down.
11299b6b563cSPaul Mackerras  */
11300108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
11319b6b563cSPaul Mackerras {
1132cbbcf340SKumar Gala 	u64 top = base + size;
11339b6b563cSPaul Mackerras 	unsigned long cnt = RELOC(mem_reserve_cnt);
11349b6b563cSPaul Mackerras 
11359b6b563cSPaul Mackerras 	if (size == 0)
11369b6b563cSPaul Mackerras 		return;
11379b6b563cSPaul Mackerras 
11389b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
11399b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
11409b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
11419b6b563cSPaul Mackerras 	 */
11429b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
11439b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
11449b6b563cSPaul Mackerras 	size = top - base;
11459b6b563cSPaul Mackerras 
11469b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
11479b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
11489b6b563cSPaul Mackerras 	RELOC(mem_reserve_map)[cnt].base = base;
11499b6b563cSPaul Mackerras 	RELOC(mem_reserve_map)[cnt].size = size;
11509b6b563cSPaul Mackerras 	RELOC(mem_reserve_cnt) = cnt + 1;
11519b6b563cSPaul Mackerras }
11529b6b563cSPaul Mackerras 
11539b6b563cSPaul Mackerras /*
1154b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
11559b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
11569b6b563cSPaul Mackerras  */
11579b6b563cSPaul Mackerras static void __init prom_init_mem(void)
11589b6b563cSPaul Mackerras {
11599b6b563cSPaul Mackerras 	phandle node;
11609b6b563cSPaul Mackerras 	char *path, type[64];
11619b6b563cSPaul Mackerras 	unsigned int plen;
11629b6b563cSPaul Mackerras 	cell_t *p, *endp;
11639b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
11649b6b563cSPaul Mackerras 	u32 rac, rsc;
11659b6b563cSPaul Mackerras 
11669b6b563cSPaul Mackerras 	/*
11679b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
11689b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
11699b6b563cSPaul Mackerras 	 * 2) top of memory
11709b6b563cSPaul Mackerras 	 */
11719b6b563cSPaul Mackerras 	rac = 2;
11729b6b563cSPaul Mackerras 	prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
11739b6b563cSPaul Mackerras 	rsc = 1;
11749b6b563cSPaul Mackerras 	prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
11759b6b563cSPaul Mackerras 	prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
11769b6b563cSPaul Mackerras 	prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
11779b6b563cSPaul Mackerras 
11789b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
11799b6b563cSPaul Mackerras 	path = RELOC(prom_scratch);
11809b6b563cSPaul Mackerras 
11819b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
11829b6b563cSPaul Mackerras 		type[0] = 0;
11839b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
11849b6b563cSPaul Mackerras 
1185c4988820SPaul Mackerras 		if (type[0] == 0) {
1186c4988820SPaul Mackerras 			/*
1187c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1188c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1189c4988820SPaul Mackerras 			 */
1190c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1191c4988820SPaul Mackerras 		}
11929b6b563cSPaul Mackerras 		if (strcmp(type, RELOC("memory")))
11939b6b563cSPaul Mackerras 			continue;
11949b6b563cSPaul Mackerras 
11959b6b563cSPaul Mackerras 		plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
11969b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
11979b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
11989b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
11999b6b563cSPaul Mackerras 		}
12009b6b563cSPaul Mackerras 		p = RELOC(regbuf);
12019b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
12029b6b563cSPaul Mackerras 
12039b6b563cSPaul Mackerras #ifdef DEBUG_PROM
12049b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
12059b6b563cSPaul Mackerras 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
12069b6b563cSPaul Mackerras 		prom_debug("  node %s :\n", path);
12079b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
12089b6b563cSPaul Mackerras 
12099b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
12109b6b563cSPaul Mackerras 			unsigned long base, size;
12119b6b563cSPaul Mackerras 
12129b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
12139b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
12149b6b563cSPaul Mackerras 
12159b6b563cSPaul Mackerras 			if (size == 0)
12169b6b563cSPaul Mackerras 				continue;
12179b6b563cSPaul Mackerras 			prom_debug("    %x %x\n", base, size);
1218ab1b55e2SBenjamin Herrenschmidt 			if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
12199b6b563cSPaul Mackerras 				RELOC(rmo_top) = size;
12209b6b563cSPaul Mackerras 			if ((base + size) > RELOC(ram_top))
12219b6b563cSPaul Mackerras 				RELOC(ram_top) = base + size;
12229b6b563cSPaul Mackerras 		}
12239b6b563cSPaul Mackerras 	}
12249b6b563cSPaul Mackerras 
12259b6b563cSPaul Mackerras 	RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
12269b6b563cSPaul Mackerras 
12279b6b563cSPaul Mackerras 	/*
1228cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1229cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1230cf68787bSBenjamin Krill 	 * TCE's up there.
1231cf68787bSBenjamin Krill 	 */
1232cf68787bSBenjamin Krill 
1233cf68787bSBenjamin Krill 	RELOC(alloc_top_high) = RELOC(ram_top);
1234cf68787bSBenjamin Krill 
1235cf68787bSBenjamin Krill 	if (RELOC(prom_memory_limit)) {
1236cf68787bSBenjamin Krill 		if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
1237cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
1238cf68787bSBenjamin Krill 				RELOC(prom_memory_limit));
1239cf68787bSBenjamin Krill 			RELOC(prom_memory_limit) = 0;
1240cf68787bSBenjamin Krill 		} else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
1241cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x >= ram_top.\n",
1242cf68787bSBenjamin Krill 				RELOC(prom_memory_limit));
1243cf68787bSBenjamin Krill 			RELOC(prom_memory_limit) = 0;
1244cf68787bSBenjamin Krill 		} else {
1245cf68787bSBenjamin Krill 			RELOC(ram_top) = RELOC(prom_memory_limit);
1246cf68787bSBenjamin Krill 			RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
1247cf68787bSBenjamin Krill 		}
1248cf68787bSBenjamin Krill 	}
1249cf68787bSBenjamin Krill 
1250cf68787bSBenjamin Krill 	/*
12519b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
12529b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
12539b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
12549b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
12559b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
12569b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
12579b6b563cSPaul Mackerras 	 */
12589b6b563cSPaul Mackerras 	if (!RELOC(rmo_top))
12599b6b563cSPaul Mackerras 		RELOC(rmo_top) = RELOC(ram_top);
12609b6b563cSPaul Mackerras 	RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
12619b6b563cSPaul Mackerras 	RELOC(alloc_top) = RELOC(rmo_top);
12622babf5c2SMichael Ellerman 	RELOC(alloc_top_high) = RELOC(ram_top);
12639b6b563cSPaul Mackerras 
126464968f60SPaul Mackerras 	/*
126564968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
126664968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
126764968f60SPaul Mackerras 	 */
126864968f60SPaul Mackerras 	if (RELOC(prom_initrd_start) &&
126964968f60SPaul Mackerras 	    RELOC(prom_initrd_start) < RELOC(rmo_top) &&
127064968f60SPaul Mackerras 	    RELOC(prom_initrd_end) > RELOC(alloc_bottom))
127164968f60SPaul Mackerras 		RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
127264968f60SPaul Mackerras 
12739b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
1274cf68787bSBenjamin Krill 	prom_printf("  memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
12759b6b563cSPaul Mackerras 	prom_printf("  alloc_bottom : %x\n", RELOC(alloc_bottom));
12769b6b563cSPaul Mackerras 	prom_printf("  alloc_top    : %x\n", RELOC(alloc_top));
12779b6b563cSPaul Mackerras 	prom_printf("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
12789b6b563cSPaul Mackerras 	prom_printf("  rmo_top      : %x\n", RELOC(rmo_top));
12799b6b563cSPaul Mackerras 	prom_printf("  ram_top      : %x\n", RELOC(ram_top));
12809b6b563cSPaul Mackerras }
12819b6b563cSPaul Mackerras 
128227f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
128327f44888SBenjamin Herrenschmidt {
128427f44888SBenjamin Herrenschmidt 	struct prom_t *_prom = &RELOC(prom);
128527f44888SBenjamin Herrenschmidt 	ihandle val;
128627f44888SBenjamin Herrenschmidt 
128727f44888SBenjamin Herrenschmidt 	if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
128827f44888SBenjamin Herrenschmidt 		call_prom("close", 1, 0, val);
128927f44888SBenjamin Herrenschmidt }
129027f44888SBenjamin Herrenschmidt 
129127f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
129227f44888SBenjamin Herrenschmidt 
129327f44888SBenjamin Herrenschmidt static u64 __initdata prom_opal_size;
129427f44888SBenjamin Herrenschmidt static u64 __initdata prom_opal_align;
129527f44888SBenjamin Herrenschmidt static int __initdata prom_rtas_start_cpu;
129627f44888SBenjamin Herrenschmidt static u64 __initdata prom_rtas_data;
129727f44888SBenjamin Herrenschmidt static u64 __initdata prom_rtas_entry;
129827f44888SBenjamin Herrenschmidt 
12996e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
13006e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_base;
13016e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_entry;
13026e35d5daSBenjamin Herrenschmidt #endif
13036e35d5daSBenjamin Herrenschmidt 
130427f44888SBenjamin Herrenschmidt /* XXX Don't change this structure without updating opal-takeover.S */
130527f44888SBenjamin Herrenschmidt static struct opal_secondary_data {
130627f44888SBenjamin Herrenschmidt 	s64				ack;	/*  0 */
130727f44888SBenjamin Herrenschmidt 	u64				go;	/*  8 */
130827f44888SBenjamin Herrenschmidt 	struct opal_takeover_args	args;	/* 16 */
130927f44888SBenjamin Herrenschmidt } opal_secondary_data;
131027f44888SBenjamin Herrenschmidt 
131127f44888SBenjamin Herrenschmidt extern char opal_secondary_entry;
131227f44888SBenjamin Herrenschmidt 
13132cb387aeSLi Zhong static void __init prom_query_opal(void)
131427f44888SBenjamin Herrenschmidt {
131527f44888SBenjamin Herrenschmidt 	long rc;
131627f44888SBenjamin Herrenschmidt 
13177680057cSBenjamin Herrenschmidt 	/* We must not query for OPAL presence on a machine that
13187680057cSBenjamin Herrenschmidt 	 * supports TNK takeover (970 blades), as this uses the same
13197680057cSBenjamin Herrenschmidt 	 * h-call with different arguments and will crash
13207680057cSBenjamin Herrenschmidt 	 */
13217680057cSBenjamin Herrenschmidt 	if (PHANDLE_VALID(call_prom("finddevice", 1, 1,
13227680057cSBenjamin Herrenschmidt 				    ADDR("/tnk-memory-map")))) {
13237680057cSBenjamin Herrenschmidt 		prom_printf("TNK takeover detected, skipping OPAL check\n");
13247680057cSBenjamin Herrenschmidt 		return;
13257680057cSBenjamin Herrenschmidt 	}
13267680057cSBenjamin Herrenschmidt 
132727f44888SBenjamin Herrenschmidt 	prom_printf("Querying for OPAL presence... ");
132827f44888SBenjamin Herrenschmidt 	rc = opal_query_takeover(&RELOC(prom_opal_size),
132927f44888SBenjamin Herrenschmidt 				 &RELOC(prom_opal_align));
133027f44888SBenjamin Herrenschmidt 	prom_debug("(rc = %ld) ", rc);
133127f44888SBenjamin Herrenschmidt 	if (rc != 0) {
133227f44888SBenjamin Herrenschmidt 		prom_printf("not there.\n");
133327f44888SBenjamin Herrenschmidt 		return;
133427f44888SBenjamin Herrenschmidt 	}
133527f44888SBenjamin Herrenschmidt 	RELOC(of_platform) = PLATFORM_OPAL;
133627f44888SBenjamin Herrenschmidt 	prom_printf(" there !\n");
133727f44888SBenjamin Herrenschmidt 	prom_debug("  opal_size  = 0x%lx\n", RELOC(prom_opal_size));
133827f44888SBenjamin Herrenschmidt 	prom_debug("  opal_align = 0x%lx\n", RELOC(prom_opal_align));
133927f44888SBenjamin Herrenschmidt 	if (RELOC(prom_opal_align) < 0x10000)
134027f44888SBenjamin Herrenschmidt 		RELOC(prom_opal_align) = 0x10000;
134127f44888SBenjamin Herrenschmidt }
134227f44888SBenjamin Herrenschmidt 
134327f44888SBenjamin Herrenschmidt static int prom_rtas_call(int token, int nargs, int nret, int *outputs, ...)
134427f44888SBenjamin Herrenschmidt {
134527f44888SBenjamin Herrenschmidt 	struct rtas_args rtas_args;
134627f44888SBenjamin Herrenschmidt 	va_list list;
134727f44888SBenjamin Herrenschmidt 	int i;
134827f44888SBenjamin Herrenschmidt 
134927f44888SBenjamin Herrenschmidt 	rtas_args.token = token;
135027f44888SBenjamin Herrenschmidt 	rtas_args.nargs = nargs;
135127f44888SBenjamin Herrenschmidt 	rtas_args.nret  = nret;
135227f44888SBenjamin Herrenschmidt 	rtas_args.rets  = (rtas_arg_t *)&(rtas_args.args[nargs]);
135327f44888SBenjamin Herrenschmidt 	va_start(list, outputs);
135427f44888SBenjamin Herrenschmidt 	for (i = 0; i < nargs; ++i)
135527f44888SBenjamin Herrenschmidt 		rtas_args.args[i] = va_arg(list, rtas_arg_t);
135627f44888SBenjamin Herrenschmidt 	va_end(list);
135727f44888SBenjamin Herrenschmidt 
135827f44888SBenjamin Herrenschmidt 	for (i = 0; i < nret; ++i)
135927f44888SBenjamin Herrenschmidt 		rtas_args.rets[i] = 0;
136027f44888SBenjamin Herrenschmidt 
136127f44888SBenjamin Herrenschmidt 	opal_enter_rtas(&rtas_args, RELOC(prom_rtas_data),
136227f44888SBenjamin Herrenschmidt 			RELOC(prom_rtas_entry));
136327f44888SBenjamin Herrenschmidt 
136427f44888SBenjamin Herrenschmidt 	if (nret > 1 && outputs != NULL)
136527f44888SBenjamin Herrenschmidt 		for (i = 0; i < nret-1; ++i)
136627f44888SBenjamin Herrenschmidt 			outputs[i] = rtas_args.rets[i+1];
136727f44888SBenjamin Herrenschmidt 	return (nret > 0)? rtas_args.rets[0]: 0;
136827f44888SBenjamin Herrenschmidt }
136927f44888SBenjamin Herrenschmidt 
137027f44888SBenjamin Herrenschmidt static void __init prom_opal_hold_cpus(void)
137127f44888SBenjamin Herrenschmidt {
137227f44888SBenjamin Herrenschmidt 	int i, cnt, cpu, rc;
137327f44888SBenjamin Herrenschmidt 	long j;
137427f44888SBenjamin Herrenschmidt 	phandle node;
137527f44888SBenjamin Herrenschmidt 	char type[64];
137627f44888SBenjamin Herrenschmidt 	u32 servers[8];
137727f44888SBenjamin Herrenschmidt 	struct prom_t *_prom = &RELOC(prom);
137827f44888SBenjamin Herrenschmidt 	void *entry = (unsigned long *)&RELOC(opal_secondary_entry);
137927f44888SBenjamin Herrenschmidt 	struct opal_secondary_data *data = &RELOC(opal_secondary_data);
138027f44888SBenjamin Herrenschmidt 
138127f44888SBenjamin Herrenschmidt 	prom_debug("prom_opal_hold_cpus: start...\n");
138227f44888SBenjamin Herrenschmidt 	prom_debug("    - entry       = 0x%x\n", entry);
138327f44888SBenjamin Herrenschmidt 	prom_debug("    - data        = 0x%x\n", data);
138427f44888SBenjamin Herrenschmidt 
138527f44888SBenjamin Herrenschmidt 	data->ack = -1;
138627f44888SBenjamin Herrenschmidt 	data->go = 0;
138727f44888SBenjamin Herrenschmidt 
138827f44888SBenjamin Herrenschmidt 	/* look for cpus */
138927f44888SBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
139027f44888SBenjamin Herrenschmidt 		type[0] = 0;
139127f44888SBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
139227f44888SBenjamin Herrenschmidt 		if (strcmp(type, RELOC("cpu")) != 0)
139327f44888SBenjamin Herrenschmidt 			continue;
139427f44888SBenjamin Herrenschmidt 
139527f44888SBenjamin Herrenschmidt 		/* Skip non-configured cpus. */
139627f44888SBenjamin Herrenschmidt 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
139727f44888SBenjamin Herrenschmidt 			if (strcmp(type, RELOC("okay")) != 0)
139827f44888SBenjamin Herrenschmidt 				continue;
139927f44888SBenjamin Herrenschmidt 
140027f44888SBenjamin Herrenschmidt 		cnt = prom_getprop(node, "ibm,ppc-interrupt-server#s", servers,
140127f44888SBenjamin Herrenschmidt 			     sizeof(servers));
140227f44888SBenjamin Herrenschmidt 		if (cnt == PROM_ERROR)
140327f44888SBenjamin Herrenschmidt 			break;
140427f44888SBenjamin Herrenschmidt 		cnt >>= 2;
140527f44888SBenjamin Herrenschmidt 		for (i = 0; i < cnt; i++) {
140627f44888SBenjamin Herrenschmidt 			cpu = servers[i];
140727f44888SBenjamin Herrenschmidt 			prom_debug("CPU %d ... ", cpu);
140827f44888SBenjamin Herrenschmidt 			if (cpu == _prom->cpu) {
140927f44888SBenjamin Herrenschmidt 				prom_debug("booted !\n");
141027f44888SBenjamin Herrenschmidt 				continue;
141127f44888SBenjamin Herrenschmidt 			}
141227f44888SBenjamin Herrenschmidt 			prom_debug("starting ... ");
141327f44888SBenjamin Herrenschmidt 
141427f44888SBenjamin Herrenschmidt 			/* Init the acknowledge var which will be reset by
141527f44888SBenjamin Herrenschmidt 			 * the secondary cpu when it awakens from its OF
141627f44888SBenjamin Herrenschmidt 			 * spinloop.
141727f44888SBenjamin Herrenschmidt 			 */
141827f44888SBenjamin Herrenschmidt 			data->ack = -1;
141927f44888SBenjamin Herrenschmidt 			rc = prom_rtas_call(RELOC(prom_rtas_start_cpu), 3, 1,
142027f44888SBenjamin Herrenschmidt 					    NULL, cpu, entry, data);
142127f44888SBenjamin Herrenschmidt 			prom_debug("rtas rc=%d ...", rc);
142227f44888SBenjamin Herrenschmidt 
142327f44888SBenjamin Herrenschmidt 			for (j = 0; j < 100000000 && data->ack == -1; j++) {
142427f44888SBenjamin Herrenschmidt 				HMT_low();
142527f44888SBenjamin Herrenschmidt 				mb();
142627f44888SBenjamin Herrenschmidt 			}
142727f44888SBenjamin Herrenschmidt 			HMT_medium();
142827f44888SBenjamin Herrenschmidt 			if (data->ack != -1)
142927f44888SBenjamin Herrenschmidt 				prom_debug("done, PIR=0x%x\n", data->ack);
143027f44888SBenjamin Herrenschmidt 			else
143127f44888SBenjamin Herrenschmidt 				prom_debug("timeout !\n");
143227f44888SBenjamin Herrenschmidt 		}
143327f44888SBenjamin Herrenschmidt 	}
143427f44888SBenjamin Herrenschmidt 	prom_debug("prom_opal_hold_cpus: end...\n");
143527f44888SBenjamin Herrenschmidt }
143627f44888SBenjamin Herrenschmidt 
14372cb387aeSLi Zhong static void __init prom_opal_takeover(void)
143827f44888SBenjamin Herrenschmidt {
143927f44888SBenjamin Herrenschmidt 	struct opal_secondary_data *data = &RELOC(opal_secondary_data);
144027f44888SBenjamin Herrenschmidt 	struct opal_takeover_args *args = &data->args;
144127f44888SBenjamin Herrenschmidt 	u64 align = RELOC(prom_opal_align);
144227f44888SBenjamin Herrenschmidt 	u64 top_addr, opal_addr;
144327f44888SBenjamin Herrenschmidt 
144427f44888SBenjamin Herrenschmidt 	args->k_image	= (u64)RELOC(_stext);
144527f44888SBenjamin Herrenschmidt 	args->k_size	= _end - _stext;
144627f44888SBenjamin Herrenschmidt 	args->k_entry	= 0;
144727f44888SBenjamin Herrenschmidt 	args->k_entry2	= 0x60;
144827f44888SBenjamin Herrenschmidt 
144927f44888SBenjamin Herrenschmidt 	top_addr = _ALIGN_UP(args->k_size, align);
145027f44888SBenjamin Herrenschmidt 
145127f44888SBenjamin Herrenschmidt 	if (RELOC(prom_initrd_start) != 0) {
145227f44888SBenjamin Herrenschmidt 		args->rd_image = RELOC(prom_initrd_start);
145327f44888SBenjamin Herrenschmidt 		args->rd_size = RELOC(prom_initrd_end) - args->rd_image;
145427f44888SBenjamin Herrenschmidt 		args->rd_loc = top_addr;
145527f44888SBenjamin Herrenschmidt 		top_addr = _ALIGN_UP(args->rd_loc + args->rd_size, align);
145627f44888SBenjamin Herrenschmidt 	}
145727f44888SBenjamin Herrenschmidt 
145827f44888SBenjamin Herrenschmidt 	/* Pickup an address for the HAL. We want to go really high
145927f44888SBenjamin Herrenschmidt 	 * up to avoid problem with future kexecs. On the other hand
146027f44888SBenjamin Herrenschmidt 	 * we don't want to be all over the TCEs on P5IOC2 machines
146127f44888SBenjamin Herrenschmidt 	 * which are going to be up there too. We assume the machine
146227f44888SBenjamin Herrenschmidt 	 * has plenty of memory, and we ask for the HAL for now to
146327f44888SBenjamin Herrenschmidt 	 * be just below the 1G point, or above the initrd
146427f44888SBenjamin Herrenschmidt 	 */
146527f44888SBenjamin Herrenschmidt 	opal_addr = _ALIGN_DOWN(0x40000000 - RELOC(prom_opal_size), align);
146627f44888SBenjamin Herrenschmidt 	if (opal_addr < top_addr)
146727f44888SBenjamin Herrenschmidt 		opal_addr = top_addr;
146827f44888SBenjamin Herrenschmidt 	args->hal_addr = opal_addr;
146927f44888SBenjamin Herrenschmidt 
1470817c21adSBenjamin Herrenschmidt 	/* Copy the command line to the kernel image */
1471817c21adSBenjamin Herrenschmidt 	strlcpy(RELOC(boot_command_line), RELOC(prom_cmd_line),
1472817c21adSBenjamin Herrenschmidt 		COMMAND_LINE_SIZE);
1473817c21adSBenjamin Herrenschmidt 
147427f44888SBenjamin Herrenschmidt 	prom_debug("  k_image    = 0x%lx\n", args->k_image);
147527f44888SBenjamin Herrenschmidt 	prom_debug("  k_size     = 0x%lx\n", args->k_size);
147627f44888SBenjamin Herrenschmidt 	prom_debug("  k_entry    = 0x%lx\n", args->k_entry);
147727f44888SBenjamin Herrenschmidt 	prom_debug("  k_entry2   = 0x%lx\n", args->k_entry2);
147827f44888SBenjamin Herrenschmidt 	prom_debug("  hal_addr   = 0x%lx\n", args->hal_addr);
147927f44888SBenjamin Herrenschmidt 	prom_debug("  rd_image   = 0x%lx\n", args->rd_image);
148027f44888SBenjamin Herrenschmidt 	prom_debug("  rd_size    = 0x%lx\n", args->rd_size);
148127f44888SBenjamin Herrenschmidt 	prom_debug("  rd_loc     = 0x%lx\n", args->rd_loc);
148227f44888SBenjamin Herrenschmidt 	prom_printf("Performing OPAL takeover,this can take a few minutes..\n");
148327f44888SBenjamin Herrenschmidt 	prom_close_stdin();
148427f44888SBenjamin Herrenschmidt 	mb();
148527f44888SBenjamin Herrenschmidt 	data->go = 1;
148627f44888SBenjamin Herrenschmidt 	for (;;)
148727f44888SBenjamin Herrenschmidt 		opal_do_takeover(args);
148827f44888SBenjamin Herrenschmidt }
14896e35d5daSBenjamin Herrenschmidt 
14906e35d5daSBenjamin Herrenschmidt /*
14916e35d5daSBenjamin Herrenschmidt  * Allocate room for and instantiate OPAL
14926e35d5daSBenjamin Herrenschmidt  */
14936e35d5daSBenjamin Herrenschmidt static void __init prom_instantiate_opal(void)
14946e35d5daSBenjamin Herrenschmidt {
14956e35d5daSBenjamin Herrenschmidt 	phandle opal_node;
14966e35d5daSBenjamin Herrenschmidt 	ihandle opal_inst;
14976e35d5daSBenjamin Herrenschmidt 	u64 base, entry;
14986e35d5daSBenjamin Herrenschmidt 	u64 size = 0, align = 0x10000;
14996e35d5daSBenjamin Herrenschmidt 	u32 rets[2];
15006e35d5daSBenjamin Herrenschmidt 
15016e35d5daSBenjamin Herrenschmidt 	prom_debug("prom_instantiate_opal: start...\n");
15026e35d5daSBenjamin Herrenschmidt 
15036e35d5daSBenjamin Herrenschmidt 	opal_node = call_prom("finddevice", 1, 1, ADDR("/ibm,opal"));
15046e35d5daSBenjamin Herrenschmidt 	prom_debug("opal_node: %x\n", opal_node);
15056e35d5daSBenjamin Herrenschmidt 	if (!PHANDLE_VALID(opal_node))
15066e35d5daSBenjamin Herrenschmidt 		return;
15076e35d5daSBenjamin Herrenschmidt 
15086e35d5daSBenjamin Herrenschmidt 	prom_getprop(opal_node, "opal-runtime-size", &size, sizeof(size));
15096e35d5daSBenjamin Herrenschmidt 	if (size == 0)
15106e35d5daSBenjamin Herrenschmidt 		return;
15116e35d5daSBenjamin Herrenschmidt 	prom_getprop(opal_node, "opal-runtime-alignment", &align,
15126e35d5daSBenjamin Herrenschmidt 		     sizeof(align));
15136e35d5daSBenjamin Herrenschmidt 
15146e35d5daSBenjamin Herrenschmidt 	base = alloc_down(size, align, 0);
15156e35d5daSBenjamin Herrenschmidt 	if (base == 0) {
15166e35d5daSBenjamin Herrenschmidt 		prom_printf("OPAL allocation failed !\n");
15176e35d5daSBenjamin Herrenschmidt 		return;
15186e35d5daSBenjamin Herrenschmidt 	}
15196e35d5daSBenjamin Herrenschmidt 
15206e35d5daSBenjamin Herrenschmidt 	opal_inst = call_prom("open", 1, 1, ADDR("/ibm,opal"));
15216e35d5daSBenjamin Herrenschmidt 	if (!IHANDLE_VALID(opal_inst)) {
15226e35d5daSBenjamin Herrenschmidt 		prom_printf("opening opal package failed (%x)\n", opal_inst);
15236e35d5daSBenjamin Herrenschmidt 		return;
15246e35d5daSBenjamin Herrenschmidt 	}
15256e35d5daSBenjamin Herrenschmidt 
15266e35d5daSBenjamin Herrenschmidt 	prom_printf("instantiating opal at 0x%x...", base);
15276e35d5daSBenjamin Herrenschmidt 
15286e35d5daSBenjamin Herrenschmidt 	if (call_prom_ret("call-method", 4, 3, rets,
15296e35d5daSBenjamin Herrenschmidt 			  ADDR("load-opal-runtime"),
15306e35d5daSBenjamin Herrenschmidt 			  opal_inst,
15316e35d5daSBenjamin Herrenschmidt 			  base >> 32, base & 0xffffffff) != 0
15326e35d5daSBenjamin Herrenschmidt 	    || (rets[0] == 0 && rets[1] == 0)) {
15336e35d5daSBenjamin Herrenschmidt 		prom_printf(" failed\n");
15346e35d5daSBenjamin Herrenschmidt 		return;
15356e35d5daSBenjamin Herrenschmidt 	}
15366e35d5daSBenjamin Herrenschmidt 	entry = (((u64)rets[0]) << 32) | rets[1];
15376e35d5daSBenjamin Herrenschmidt 
15386e35d5daSBenjamin Herrenschmidt 	prom_printf(" done\n");
15396e35d5daSBenjamin Herrenschmidt 
15406e35d5daSBenjamin Herrenschmidt 	reserve_mem(base, size);
15416e35d5daSBenjamin Herrenschmidt 
15426e35d5daSBenjamin Herrenschmidt 	prom_debug("opal base     = 0x%x\n", base);
15436e35d5daSBenjamin Herrenschmidt 	prom_debug("opal align    = 0x%x\n", align);
15446e35d5daSBenjamin Herrenschmidt 	prom_debug("opal entry    = 0x%x\n", entry);
15456e35d5daSBenjamin Herrenschmidt 	prom_debug("opal size     = 0x%x\n", (long)size);
15466e35d5daSBenjamin Herrenschmidt 
15476e35d5daSBenjamin Herrenschmidt 	prom_setprop(opal_node, "/ibm,opal", "opal-base-address",
15486e35d5daSBenjamin Herrenschmidt 		     &base, sizeof(base));
15496e35d5daSBenjamin Herrenschmidt 	prom_setprop(opal_node, "/ibm,opal", "opal-entry-address",
15506e35d5daSBenjamin Herrenschmidt 		     &entry, sizeof(entry));
15516e35d5daSBenjamin Herrenschmidt 
15526e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
15536e35d5daSBenjamin Herrenschmidt 	RELOC(prom_opal_base) = base;
15546e35d5daSBenjamin Herrenschmidt 	RELOC(prom_opal_entry) = entry;
15556e35d5daSBenjamin Herrenschmidt #endif
15566e35d5daSBenjamin Herrenschmidt 	prom_debug("prom_instantiate_opal: end...\n");
15576e35d5daSBenjamin Herrenschmidt }
15586e35d5daSBenjamin Herrenschmidt 
155927f44888SBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV */
15609b6b563cSPaul Mackerras 
15619b6b563cSPaul Mackerras /*
15629b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
15639b6b563cSPaul Mackerras  */
15649b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
15659b6b563cSPaul Mackerras {
15669b6b563cSPaul Mackerras 	phandle rtas_node;
15679b6b563cSPaul Mackerras 	ihandle rtas_inst;
15689b6b563cSPaul Mackerras 	u32 base, entry = 0;
15699b6b563cSPaul Mackerras 	u32 size = 0;
15709b6b563cSPaul Mackerras 
15719b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
15729b6b563cSPaul Mackerras 
15739b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
15749b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
15759b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
15769b6b563cSPaul Mackerras 		return;
15779b6b563cSPaul Mackerras 
15789b6b563cSPaul Mackerras 	prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
15799b6b563cSPaul Mackerras 	if (size == 0)
15809b6b563cSPaul Mackerras 		return;
15819b6b563cSPaul Mackerras 
15829b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
15836d1e2c6cSAnton Blanchard 	if (base == 0)
15846d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
15859b6b563cSPaul Mackerras 
15869b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
15879b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1588a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
15899b6b563cSPaul Mackerras 		return;
15909b6b563cSPaul Mackerras 	}
15919b6b563cSPaul Mackerras 
15929b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
15939b6b563cSPaul Mackerras 
15949b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
15959b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1596a23414beSPaul Mackerras 			  rtas_inst, base) != 0
15979b6b563cSPaul Mackerras 	    || entry == 0) {
15989b6b563cSPaul Mackerras 		prom_printf(" failed\n");
15999b6b563cSPaul Mackerras 		return;
16009b6b563cSPaul Mackerras 	}
16019b6b563cSPaul Mackerras 	prom_printf(" done\n");
16029b6b563cSPaul Mackerras 
16039b6b563cSPaul Mackerras 	reserve_mem(base, size);
16049b6b563cSPaul Mackerras 
1605a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1606a23414beSPaul Mackerras 		     &base, sizeof(base));
1607a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1608a23414beSPaul Mackerras 		     &entry, sizeof(entry));
16099b6b563cSPaul Mackerras 
161027f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
161127f44888SBenjamin Herrenschmidt 	/* PowerVN takeover hack */
161227f44888SBenjamin Herrenschmidt 	RELOC(prom_rtas_data) = base;
161327f44888SBenjamin Herrenschmidt 	RELOC(prom_rtas_entry) = entry;
161427f44888SBenjamin Herrenschmidt 	prom_getprop(rtas_node, "start-cpu", &RELOC(prom_rtas_start_cpu), 4);
161527f44888SBenjamin Herrenschmidt #endif
16169b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
16179b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
16189b6b563cSPaul Mackerras 	prom_debug("rtas size     = 0x%x\n", (long)size);
16199b6b563cSPaul Mackerras 
16209b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
16219b6b563cSPaul Mackerras }
16229b6b563cSPaul Mackerras 
16239b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
16249b6b563cSPaul Mackerras /*
16254a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
16264a727429SAshley Lai  */
16274a727429SAshley Lai static void __init prom_instantiate_sml(void)
16284a727429SAshley Lai {
16294a727429SAshley Lai 	phandle ibmvtpm_node;
16304a727429SAshley Lai 	ihandle ibmvtpm_inst;
16314a727429SAshley Lai 	u32 entry = 0, size = 0;
16324a727429SAshley Lai 	u64 base;
16334a727429SAshley Lai 
16344a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
16354a727429SAshley Lai 
16364a727429SAshley Lai 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/ibm,vtpm"));
16374a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
16384a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
16394a727429SAshley Lai 		return;
16404a727429SAshley Lai 
16414a727429SAshley Lai 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/ibm,vtpm"));
16424a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
16434a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
16444a727429SAshley Lai 		return;
16454a727429SAshley Lai 	}
16464a727429SAshley Lai 
16474a727429SAshley Lai 	if (call_prom_ret("call-method", 2, 2, &size,
16484a727429SAshley Lai 			  ADDR("sml-get-handover-size"),
16494a727429SAshley Lai 			  ibmvtpm_inst) != 0 || size == 0) {
16504a727429SAshley Lai 		prom_printf("SML get handover size failed\n");
16514a727429SAshley Lai 		return;
16524a727429SAshley Lai 	}
16534a727429SAshley Lai 
16544a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
16554a727429SAshley Lai 	if (base == 0)
16564a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
16574a727429SAshley Lai 
16584a727429SAshley Lai 	prom_printf("instantiating sml at 0x%x...", base);
16594a727429SAshley Lai 
16604a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
16614a727429SAshley Lai 			  ADDR("sml-handover"),
16624a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
16634a727429SAshley Lai 		prom_printf("SML handover failed\n");
16644a727429SAshley Lai 		return;
16654a727429SAshley Lai 	}
16664a727429SAshley Lai 	prom_printf(" done\n");
16674a727429SAshley Lai 
16684a727429SAshley Lai 	reserve_mem(base, size);
16694a727429SAshley Lai 
16704a727429SAshley Lai 	prom_setprop(ibmvtpm_node, "/ibm,vtpm", "linux,sml-base",
16714a727429SAshley Lai 		     &base, sizeof(base));
16724a727429SAshley Lai 	prom_setprop(ibmvtpm_node, "/ibm,vtpm", "linux,sml-size",
16734a727429SAshley Lai 		     &size, sizeof(size));
16744a727429SAshley Lai 
16754a727429SAshley Lai 	prom_debug("sml base     = 0x%x\n", base);
16764a727429SAshley Lai 	prom_debug("sml size     = 0x%x\n", (long)size);
16774a727429SAshley Lai 
16784a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
16794a727429SAshley Lai }
16804a727429SAshley Lai 
16814a727429SAshley Lai /*
16829b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
16839b6b563cSPaul Mackerras  */
16849b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
16859b6b563cSPaul Mackerras {
16869b6b563cSPaul Mackerras 	phandle node;
16879b6b563cSPaul Mackerras 	ihandle phb_node;
16889b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
16899b6b563cSPaul Mackerras 	char *path = RELOC(prom_scratch);
16909b6b563cSPaul Mackerras 	u64 base, align;
16919b6b563cSPaul Mackerras 	u32 minalign, minsize;
16929b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
16939b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
16949b6b563cSPaul Mackerras 	u64 i;
16959b6b563cSPaul Mackerras 
1696165785e5SJeremy Kerr 	if (RELOC(prom_iommu_off))
16979b6b563cSPaul Mackerras 		return;
16989b6b563cSPaul Mackerras 
16999b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
17009b6b563cSPaul Mackerras 
17019b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
17029b6b563cSPaul Mackerras 	local_alloc_top = RELOC(alloc_top_high);
17039b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
17049b6b563cSPaul Mackerras 
17059b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
17069b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
17079b6b563cSPaul Mackerras 		compatible[0] = 0;
17089b6b563cSPaul Mackerras 		type[0] = 0;
17099b6b563cSPaul Mackerras 		model[0] = 0;
17109b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
17119b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
17129b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
17139b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
17149b6b563cSPaul Mackerras 
17159b6b563cSPaul Mackerras 		if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
17169b6b563cSPaul Mackerras 			continue;
17179b6b563cSPaul Mackerras 
1718e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
17199b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
17209b6b563cSPaul Mackerras 			if ((strstr(compatible, RELOC("python")) == NULL) &&
17219b6b563cSPaul Mackerras 			    (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
17229b6b563cSPaul Mackerras 			    (strstr(compatible, RELOC("Winnipeg")) == NULL))
17239b6b563cSPaul Mackerras 				continue;
17249b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
17259b6b563cSPaul Mackerras 			if ((strstr(model, RELOC("ython")) == NULL) &&
17269b6b563cSPaul Mackerras 			    (strstr(model, RELOC("peedwagon")) == NULL) &&
17279b6b563cSPaul Mackerras 			    (strstr(model, RELOC("innipeg")) == NULL))
17289b6b563cSPaul Mackerras 				continue;
17299b6b563cSPaul Mackerras 		}
17309b6b563cSPaul Mackerras 
17319b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
17329b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
17339b6b563cSPaul Mackerras 			minalign = 0;
17349b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
17359b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
17369b6b563cSPaul Mackerras 			minsize = 4UL << 20;
17379b6b563cSPaul Mackerras 
17389b6b563cSPaul Mackerras 		/*
17399b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
17409b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
17419b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
17429b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
17439b6b563cSPaul Mackerras 		 *
17449b6b563cSPaul Mackerras 		 * On POWER4, firmware sets the TCE region by assuming
17459b6b563cSPaul Mackerras 		 * each TCE table is 8MB. Using this memory for anything
17469b6b563cSPaul Mackerras 		 * else will impact performance, so we always allocate 8MB.
17479b6b563cSPaul Mackerras 		 * Anton
17489b6b563cSPaul Mackerras 		 */
1749d3dbeef6SMichael Ellerman 		if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p))
17509b6b563cSPaul Mackerras 			minsize = 8UL << 20;
17519b6b563cSPaul Mackerras 		else
17529b6b563cSPaul Mackerras 			minsize = 4UL << 20;
17539b6b563cSPaul Mackerras 
17549b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
17559b6b563cSPaul Mackerras 		align = max(minalign, minsize);
17569b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
17579b6b563cSPaul Mackerras 		if (base == 0)
17589b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
17599b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
17609b6b563cSPaul Mackerras 			local_alloc_bottom = base;
17619b6b563cSPaul Mackerras 
17629b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
1763aca71ef8SLi Zefan 		memset(path, 0, PROM_SCRATCH_SIZE);
17649b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
17659b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
17669b6b563cSPaul Mackerras 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
17679b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
17689b6b563cSPaul Mackerras 		}
17699b6b563cSPaul Mackerras 
1770a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
1771a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1772a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1773a23414beSPaul Mackerras 
17749b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
17759b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
17769b6b563cSPaul Mackerras 		prom_debug("\tbase = 0x%x\n", base);
17779b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
17789b6b563cSPaul Mackerras 
17799b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
17809b6b563cSPaul Mackerras 		 * over the allocated size.
17819b6b563cSPaul Mackerras 		 */
17822b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
17839b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
17849b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
17859b6b563cSPaul Mackerras 			tce_entry |= 0x3;
17869b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
17879b6b563cSPaul Mackerras 		}
17889b6b563cSPaul Mackerras 
17899b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
17909b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
17919b6b563cSPaul Mackerras 		if (phb_node == 0)
17929b6b563cSPaul Mackerras 			prom_printf("... failed\n");
17939b6b563cSPaul Mackerras 		else
17949b6b563cSPaul Mackerras 			prom_printf("... done\n");
17959b6b563cSPaul Mackerras 
17969b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
17979b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
17989b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
17999b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
18009b6b563cSPaul Mackerras 	}
18019b6b563cSPaul Mackerras 
18029b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
18039b6b563cSPaul Mackerras 
18042babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
18052babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
18062babf5c2SMichael Ellerman 	RELOC(prom_tce_alloc_start) = local_alloc_bottom;
18079b6b563cSPaul Mackerras 	RELOC(prom_tce_alloc_end) = local_alloc_top;
18089b6b563cSPaul Mackerras 
18099b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
18109b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
18119b6b563cSPaul Mackerras }
18129b6b563cSPaul Mackerras #endif
18139b6b563cSPaul Mackerras 
18149b6b563cSPaul Mackerras /*
18159b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
18169b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
18179b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
18189b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
18199b6b563cSPaul Mackerras  *
18209b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
18219b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
18229b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
18239b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
18249b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
18259b6b563cSPaul Mackerras  * of setting those values.
18269b6b563cSPaul Mackerras  *
18279b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
18289b6b563cSPaul Mackerras  * is in its holding pattern code.
18299b6b563cSPaul Mackerras  *
18309b6b563cSPaul Mackerras  * -- Cort
18319b6b563cSPaul Mackerras  */
1832bbd0abdaSPaul Mackerras /*
1833bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
1834bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
1835bbd0abdaSPaul Mackerras  */
1836bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1837bbd0abdaSPaul Mackerras 
18389b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
18399b6b563cSPaul Mackerras {
18409b6b563cSPaul Mackerras 	unsigned long i;
18419b6b563cSPaul Mackerras 	unsigned int reg;
18429b6b563cSPaul Mackerras 	phandle node;
18439b6b563cSPaul Mackerras 	char type[64];
1844bbd0abdaSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
18459b6b563cSPaul Mackerras 	unsigned long *spinloop
1846bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
18479b6b563cSPaul Mackerras 	unsigned long *acknowledge
1848bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1849bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
18509b6b563cSPaul Mackerras 
18519b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
18529b6b563cSPaul Mackerras 	prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
18539b6b563cSPaul Mackerras 	prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
18549b6b563cSPaul Mackerras 	prom_debug("    1) acknowledge    = 0x%x\n",
18559b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
18569b6b563cSPaul Mackerras 	prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
18579b6b563cSPaul Mackerras 	prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
18589b6b563cSPaul Mackerras 
18599b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
18609b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
18619b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
18629b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
18639b6b563cSPaul Mackerras 	 */
18649b6b563cSPaul Mackerras 	*spinloop = 0;
18659b6b563cSPaul Mackerras 
18669b6b563cSPaul Mackerras 	/* look for cpus */
18679b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
18689b6b563cSPaul Mackerras 		type[0] = 0;
18699b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
18709b6b563cSPaul Mackerras 		if (strcmp(type, RELOC("cpu")) != 0)
18719b6b563cSPaul Mackerras 			continue;
18729b6b563cSPaul Mackerras 
18739b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
18749b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
18759b6b563cSPaul Mackerras 			if (strcmp(type, RELOC("okay")) != 0)
18769b6b563cSPaul Mackerras 				continue;
18779b6b563cSPaul Mackerras 
18789b6b563cSPaul Mackerras 		reg = -1;
18799b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
18809b6b563cSPaul Mackerras 
18812c48a7d6SMichael Neuling 		prom_debug("cpu hw idx   = %lu\n", reg);
18829b6b563cSPaul Mackerras 
18839b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
18849b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
18859b6b563cSPaul Mackerras 		 * spinloop.
18869b6b563cSPaul Mackerras 		 */
18879b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
18889b6b563cSPaul Mackerras 
18897d2f6075SNathan Lynch 		if (reg != _prom->cpu) {
189027f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
18912c48a7d6SMichael Neuling 			prom_printf("starting cpu hw idx %lu... ", reg);
18929b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
18939b6b563cSPaul Mackerras 				  secondary_hold, reg);
18949b6b563cSPaul Mackerras 
18959b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
18969b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
18979b6b563cSPaul Mackerras 				mb();
18989b6b563cSPaul Mackerras 
1899bbd0abdaSPaul Mackerras 			if (*acknowledge == reg)
19009b6b563cSPaul Mackerras 				prom_printf("done\n");
1901bbd0abdaSPaul Mackerras 			else
19029b6b563cSPaul Mackerras 				prom_printf("failed: %x\n", *acknowledge);
19039b6b563cSPaul Mackerras 		}
19049b6b563cSPaul Mackerras #ifdef CONFIG_SMP
19059b6b563cSPaul Mackerras 		else
19062c48a7d6SMichael Neuling 			prom_printf("boot cpu hw idx %lu\n", reg);
19079b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
19089b6b563cSPaul Mackerras 	}
19099b6b563cSPaul Mackerras 
19109b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
19119b6b563cSPaul Mackerras }
19129b6b563cSPaul Mackerras 
19139b6b563cSPaul Mackerras 
19149b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
19159b6b563cSPaul Mackerras {
19169b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
19179b6b563cSPaul Mackerras 
19189b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
19199b6b563cSPaul Mackerras 	RELOC(prom_entry) = pp;
19209b6b563cSPaul Mackerras 
19219b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
19229b6b563cSPaul Mackerras 	_prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
19239b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(_prom->chosen))
19249b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
19259b6b563cSPaul Mackerras 
19269b6b563cSPaul Mackerras 	/* get device tree root */
19279b6b563cSPaul Mackerras 	_prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
19289b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(_prom->root))
19299b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1930a575b807SPaul Mackerras 
1931a575b807SPaul Mackerras 	_prom->mmumap = 0;
19329b6b563cSPaul Mackerras }
19339b6b563cSPaul Mackerras 
1934a575b807SPaul Mackerras #ifdef CONFIG_PPC32
1935a575b807SPaul Mackerras /*
1936a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
1937a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
1938a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
1939a575b807SPaul Mackerras  */
1940a575b807SPaul Mackerras static void __init prom_find_mmu(void)
1941a575b807SPaul Mackerras {
1942a575b807SPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
1943a575b807SPaul Mackerras 	phandle oprom;
1944a575b807SPaul Mackerras 	char version[64];
1945a575b807SPaul Mackerras 
1946a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1947a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
1948a575b807SPaul Mackerras 		return;
1949a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1950a575b807SPaul Mackerras 		return;
1951a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
1952a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
1953a23414beSPaul Mackerras 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1954a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
1955a23414beSPaul Mackerras 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1956a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1957a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1958a23414beSPaul Mackerras 	} else
1959a575b807SPaul Mackerras 		return;
1960a23414beSPaul Mackerras 	_prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
1961a575b807SPaul Mackerras 	prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1962a575b807SPaul Mackerras 		     sizeof(_prom->mmumap));
1963a23414beSPaul Mackerras 	if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1964a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
1965a575b807SPaul Mackerras }
1966a575b807SPaul Mackerras #else
1967a575b807SPaul Mackerras #define prom_find_mmu()
1968a575b807SPaul Mackerras #endif
1969a575b807SPaul Mackerras 
19709b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
19719b6b563cSPaul Mackerras {
19729b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
19739b6b563cSPaul Mackerras 	char *path = RELOC(of_stdout_device);
19749b6b563cSPaul Mackerras 	char type[16];
19759b6b563cSPaul Mackerras 	u32 val;
19769b6b563cSPaul Mackerras 
19779b6b563cSPaul Mackerras 	if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
19789b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
19799b6b563cSPaul Mackerras 
19809b6b563cSPaul Mackerras 	_prom->stdout = val;
19819b6b563cSPaul Mackerras 
19829b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
19839b6b563cSPaul Mackerras 	memset(path, 0, 256);
19849b6b563cSPaul Mackerras 	call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
19859b6b563cSPaul Mackerras 	val = call_prom("instance-to-package", 1, 1, _prom->stdout);
1986a23414beSPaul Mackerras 	prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1987a23414beSPaul Mackerras 		     &val, sizeof(val));
19889b6b563cSPaul Mackerras 	prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
1989a23414beSPaul Mackerras 	prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1990a23414beSPaul Mackerras 		     path, strlen(path) + 1);
19919b6b563cSPaul Mackerras 
19929b6b563cSPaul Mackerras 	/* If it's a display, note it */
19939b6b563cSPaul Mackerras 	memset(type, 0, sizeof(type));
19949b6b563cSPaul Mackerras 	prom_getprop(val, "device_type", type, sizeof(type));
19959b6b563cSPaul Mackerras 	if (strcmp(type, RELOC("display")) == 0)
1996a23414beSPaul Mackerras 		prom_setprop(val, path, "linux,boot-display", NULL, 0);
19979b6b563cSPaul Mackerras }
19989b6b563cSPaul Mackerras 
19999b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
20009b6b563cSPaul Mackerras {
20019b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
20029b6b563cSPaul Mackerras 	char compat[256];
20039b6b563cSPaul Mackerras 	int len, i = 0;
200421fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
20059b6b563cSPaul Mackerras 	phandle rtas;
2006e8222502SBenjamin Herrenschmidt 	int x;
200721fe3301SBenjamin Herrenschmidt #endif
2008e8222502SBenjamin Herrenschmidt 
20096e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
20109b6b563cSPaul Mackerras 	len = prom_getprop(_prom->root, "compatible",
20119b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
20129b6b563cSPaul Mackerras 	if (len > 0) {
20139b6b563cSPaul Mackerras 		compat[len] = 0;
20149b6b563cSPaul Mackerras 		while (i < len) {
20159b6b563cSPaul Mackerras 			char *p = &compat[i];
20169b6b563cSPaul Mackerras 			int sl = strlen(p);
20179b6b563cSPaul Mackerras 			if (sl == 0)
20189b6b563cSPaul Mackerras 				break;
20199b6b563cSPaul Mackerras 			if (strstr(p, RELOC("Power Macintosh")) ||
2020a575b807SPaul Mackerras 			    strstr(p, RELOC("MacRISC")))
20219b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
2022133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
2023133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
2024133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
2025133dda1eSArnd Bergmann 			 * so we do it here.
2026133dda1eSArnd Bergmann 			 */
2027133dda1eSArnd Bergmann 			if (strstr(p, RELOC("IBM,CBEA")) ||
2028133dda1eSArnd Bergmann 			    strstr(p, RELOC("IBM,CPBW-1.0")))
2029133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
2030133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
20319b6b563cSPaul Mackerras 			i += sl + 1;
20329b6b563cSPaul Mackerras 		}
20339b6b563cSPaul Mackerras 	}
20349b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
20356e35d5daSBenjamin Herrenschmidt 	/* Try to detect OPAL */
20366e35d5daSBenjamin Herrenschmidt 	if (PHANDLE_VALID(call_prom("finddevice", 1, 1, ADDR("/ibm,opal"))))
20376e35d5daSBenjamin Herrenschmidt 		return PLATFORM_OPAL;
20386e35d5daSBenjamin Herrenschmidt 
20396e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
2040e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
2041e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
2042e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
2043e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
2044e8222502SBenjamin Herrenschmidt 	 */
20456f806ceeSMichael Ellerman 	len = prom_getprop(_prom->root, "device_type",
2046e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
2047e8222502SBenjamin Herrenschmidt 	if (len <= 0)
2048e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2049cb6b2eb9SBenjamin Herrenschmidt 	if (strcmp(compat, RELOC("chrp")))
2050e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2051e8222502SBenjamin Herrenschmidt 
20529b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
20539b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2054e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
2055e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2056e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
20579b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
20584da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
20599b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
20609b6b563cSPaul Mackerras 	}
20619b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
20629b6b563cSPaul Mackerras #else
2063e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
20649b6b563cSPaul Mackerras #endif
20659b6b563cSPaul Mackerras }
20669b6b563cSPaul Mackerras 
20679b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
20689b6b563cSPaul Mackerras {
20699b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
20709b6b563cSPaul Mackerras }
20719b6b563cSPaul Mackerras 
20729b6b563cSPaul Mackerras /*
20739b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
20749b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
20759b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
20769b6b563cSPaul Mackerras  * we've taken over the MMU.
20779b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
20789b6b563cSPaul Mackerras  * and if so, open it now.
20799b6b563cSPaul Mackerras  */
20809b6b563cSPaul Mackerras static void __init prom_check_displays(void)
20819b6b563cSPaul Mackerras {
20829b6b563cSPaul Mackerras 	char type[16], *path;
20839b6b563cSPaul Mackerras 	phandle node;
20849b6b563cSPaul Mackerras 	ihandle ih;
20859b6b563cSPaul Mackerras 	int i;
20869b6b563cSPaul Mackerras 
20879b6b563cSPaul Mackerras 	static unsigned char default_colors[] = {
20889b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
20899b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
20909b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
20919b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
20929b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
20939b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
20949b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
20959b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
20969b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
20979b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
20989b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
20999b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
21009b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
21019b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
21029b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
21039b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
21049b6b563cSPaul Mackerras 	};
21059b6b563cSPaul Mackerras 	const unsigned char *clut;
21069b6b563cSPaul Mackerras 
21074da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
21089b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
21099b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
21109b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
21119b6b563cSPaul Mackerras 		if (strcmp(type, RELOC("display")) != 0)
21129b6b563cSPaul Mackerras 			continue;
21139b6b563cSPaul Mackerras 
21149b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
21159b6b563cSPaul Mackerras 		path = RELOC(prom_scratch);
21169b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
21179b6b563cSPaul Mackerras 
21189b6b563cSPaul Mackerras 		/*
21199b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
21209b6b563cSPaul Mackerras 		 * arguments
21219b6b563cSPaul Mackerras 		 */
21229b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
21239b6b563cSPaul Mackerras 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
21249b6b563cSPaul Mackerras 			continue;
21259b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
21269b6b563cSPaul Mackerras 
21279b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
21289b6b563cSPaul Mackerras 		if (ih == 0) {
21299b6b563cSPaul Mackerras 			prom_printf("failed\n");
21309b6b563cSPaul Mackerras 			continue;
21319b6b563cSPaul Mackerras 		}
21329b6b563cSPaul Mackerras 
21339b6b563cSPaul Mackerras 		/* Success */
21349b6b563cSPaul Mackerras 		prom_printf("done\n");
2135a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
21369b6b563cSPaul Mackerras 
21379b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
21389b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
21399b6b563cSPaul Mackerras 		clut = RELOC(default_colors);
21403f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
21419b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
21429b6b563cSPaul Mackerras 					   clut[2]) != 0)
21439b6b563cSPaul Mackerras 				break;
21449b6b563cSPaul Mackerras 
21459b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
21469b6b563cSPaul Mackerras 		clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
21479b6b563cSPaul Mackerras 		for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
21489b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
21499b6b563cSPaul Mackerras 					   clut[2]) != 0)
21509b6b563cSPaul Mackerras 				break;
21519b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
21529b6b563cSPaul Mackerras 	}
21539b6b563cSPaul Mackerras }
21549b6b563cSPaul Mackerras 
21559b6b563cSPaul Mackerras 
21569b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
21579b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
21589b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
21599b6b563cSPaul Mackerras {
21609b6b563cSPaul Mackerras 	void *ret;
21619b6b563cSPaul Mackerras 
21629b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
21639b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
21649b6b563cSPaul Mackerras 		unsigned long room, chunk;
21659b6b563cSPaul Mackerras 
21669b6b563cSPaul Mackerras 		prom_debug("Chunk exhausted, claiming more at %x...\n",
21679b6b563cSPaul Mackerras 			   RELOC(alloc_bottom));
21689b6b563cSPaul Mackerras 		room = RELOC(alloc_top) - RELOC(alloc_bottom);
21699b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
21709b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
21719b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2172fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2173fbafd728SAnton Blanchard 				   "(no room)\n");
21749b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
21759b6b563cSPaul Mackerras 		if (chunk == 0)
2176fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2177fbafd728SAnton Blanchard 				   "(claim failed)\n");
2178966728ddSAnton Blanchard 		*mem_end = chunk + room;
21799b6b563cSPaul Mackerras 	}
21809b6b563cSPaul Mackerras 
21819b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
21829b6b563cSPaul Mackerras 	*mem_start += needed;
21839b6b563cSPaul Mackerras 
21849b6b563cSPaul Mackerras 	return ret;
21859b6b563cSPaul Mackerras }
21869b6b563cSPaul Mackerras 
21879b6b563cSPaul Mackerras #define dt_push_token(token, mem_start, mem_end) \
21889b6b563cSPaul Mackerras 	do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
21899b6b563cSPaul Mackerras 
21909b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
21919b6b563cSPaul Mackerras {
21929b6b563cSPaul Mackerras 	char *s, *os;
21939b6b563cSPaul Mackerras 
21949b6b563cSPaul Mackerras 	s = os = (char *)RELOC(dt_string_start);
21959b6b563cSPaul Mackerras 	s += 4;
21969b6b563cSPaul Mackerras 	while (s <  (char *)RELOC(dt_string_end)) {
21979b6b563cSPaul Mackerras 		if (strcmp(s, str) == 0)
21989b6b563cSPaul Mackerras 			return s - os;
21999b6b563cSPaul Mackerras 		s += strlen(s) + 1;
22009b6b563cSPaul Mackerras 	}
22019b6b563cSPaul Mackerras 	return 0;
22029b6b563cSPaul Mackerras }
22039b6b563cSPaul Mackerras 
22049b6b563cSPaul Mackerras /*
22059b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
22069b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
22079b6b563cSPaul Mackerras  */
22089b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
22099b6b563cSPaul Mackerras 
22109b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
22119b6b563cSPaul Mackerras 					 unsigned long *mem_start,
22129b6b563cSPaul Mackerras 					 unsigned long *mem_end)
22139b6b563cSPaul Mackerras {
22149b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
22159b6b563cSPaul Mackerras 	unsigned long soff;
22169b6b563cSPaul Mackerras 	phandle child;
22179b6b563cSPaul Mackerras 
22189b6b563cSPaul Mackerras 	sstart =  (char *)RELOC(dt_string_start);
22199b6b563cSPaul Mackerras 
22209b6b563cSPaul Mackerras 	/* get and store all property names */
22219b6b563cSPaul Mackerras 	prev_name = RELOC("");
22229b6b563cSPaul Mackerras 	for (;;) {
22239b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
22249b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
22259b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
22269b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
22279b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
22289b6b563cSPaul Mackerras 			break;
22299b6b563cSPaul Mackerras 		}
22309b6b563cSPaul Mackerras 
22319b6b563cSPaul Mackerras  		/* skip "name" */
22329b6b563cSPaul Mackerras  		if (strcmp(namep, RELOC("name")) == 0) {
22339b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
22349b6b563cSPaul Mackerras  			prev_name = RELOC("name");
22359b6b563cSPaul Mackerras  			continue;
22369b6b563cSPaul Mackerras  		}
22379b6b563cSPaul Mackerras 		/* get/create string entry */
22389b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
22399b6b563cSPaul Mackerras 		if (soff != 0) {
22409b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
22419b6b563cSPaul Mackerras 			namep = sstart + soff;
22429b6b563cSPaul Mackerras 		} else {
22439b6b563cSPaul Mackerras 			/* Trim off some if we can */
22449b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
22459b6b563cSPaul Mackerras 			RELOC(dt_string_end) = *mem_start;
22469b6b563cSPaul Mackerras 		}
22479b6b563cSPaul Mackerras 		prev_name = namep;
22489b6b563cSPaul Mackerras 	}
22499b6b563cSPaul Mackerras 
22509b6b563cSPaul Mackerras 	/* do all our children */
22519b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
22529b6b563cSPaul Mackerras 	while (child != 0) {
22539b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
22549b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
22559b6b563cSPaul Mackerras 	}
22569b6b563cSPaul Mackerras }
22579b6b563cSPaul Mackerras 
22589b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
22599b6b563cSPaul Mackerras 					unsigned long *mem_end)
22609b6b563cSPaul Mackerras {
22619b6b563cSPaul Mackerras 	phandle child;
22629b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
22639b6b563cSPaul Mackerras 	unsigned long soff;
22649b6b563cSPaul Mackerras 	unsigned char *valp;
22659b6b563cSPaul Mackerras 	static char pname[MAX_PROPERTY_NAME];
22666e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
22679b6b563cSPaul Mackerras 
22689b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
22699b6b563cSPaul Mackerras 
22709b6b563cSPaul Mackerras 	/* get the node's full name */
22719b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2272c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2273c4988820SPaul Mackerras 	if (room > 255)
2274c4988820SPaul Mackerras 		room = 255;
2275c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
22769b6b563cSPaul Mackerras 	if (l >= 0) {
22779b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2278c4988820SPaul Mackerras 		if (l >= room) {
2279c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
22809b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
22819b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
22829b6b563cSPaul Mackerras 		}
22839b6b563cSPaul Mackerras 		namep[l] = '\0';
22849b6b563cSPaul Mackerras 
22859b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2286a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2287a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
22889b6b563cSPaul Mackerras 		 */
2289a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
22909b6b563cSPaul Mackerras 			if (*p == '/')
2291a575b807SPaul Mackerras 				lp = namep;
2292a575b807SPaul Mackerras 			else if (*p != 0)
2293a575b807SPaul Mackerras 				*lp++ = *p;
2294a575b807SPaul Mackerras 		}
2295a575b807SPaul Mackerras 		*lp = 0;
2296a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
22979b6b563cSPaul Mackerras 	}
22989b6b563cSPaul Mackerras 
22999b6b563cSPaul Mackerras 	/* get it again for debugging */
23009b6b563cSPaul Mackerras 	path = RELOC(prom_scratch);
23019b6b563cSPaul Mackerras 	memset(path, 0, PROM_SCRATCH_SIZE);
23029b6b563cSPaul Mackerras 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
23039b6b563cSPaul Mackerras 
23049b6b563cSPaul Mackerras 	/* get and store all properties */
23059b6b563cSPaul Mackerras 	prev_name = RELOC("");
23069b6b563cSPaul Mackerras 	sstart = (char *)RELOC(dt_string_start);
23079b6b563cSPaul Mackerras 	for (;;) {
23089b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
23099b6b563cSPaul Mackerras 			      RELOC(pname)) != 1)
23109b6b563cSPaul Mackerras 			break;
23119b6b563cSPaul Mackerras 
23129b6b563cSPaul Mackerras  		/* skip "name" */
23139b6b563cSPaul Mackerras  		if (strcmp(RELOC(pname), RELOC("name")) == 0) {
23149b6b563cSPaul Mackerras  			prev_name = RELOC("name");
23159b6b563cSPaul Mackerras  			continue;
23169b6b563cSPaul Mackerras  		}
23179b6b563cSPaul Mackerras 
23189b6b563cSPaul Mackerras 		/* find string offset */
23199b6b563cSPaul Mackerras 		soff = dt_find_string(RELOC(pname));
23209b6b563cSPaul Mackerras 		if (soff == 0) {
23219b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
23229b6b563cSPaul Mackerras 				    " <%s>, node %s\n", RELOC(pname), path);
23239b6b563cSPaul Mackerras 			break;
23249b6b563cSPaul Mackerras 		}
23259b6b563cSPaul Mackerras 		prev_name = sstart + soff;
23269b6b563cSPaul Mackerras 
23279b6b563cSPaul Mackerras 		/* get length */
23289b6b563cSPaul Mackerras 		l = call_prom("getproplen", 2, 1, node, RELOC(pname));
23299b6b563cSPaul Mackerras 
23309b6b563cSPaul Mackerras 		/* sanity checks */
23319b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
23329b6b563cSPaul Mackerras 			continue;
23339b6b563cSPaul Mackerras 
23349b6b563cSPaul Mackerras 		/* push property head */
23359b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
23369b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
23379b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
23389b6b563cSPaul Mackerras 
23399b6b563cSPaul Mackerras 		/* push property content */
23409b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
23419b6b563cSPaul Mackerras 		call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
23429b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
23436e35d5daSBenjamin Herrenschmidt 
23446e35d5daSBenjamin Herrenschmidt 		if (!strcmp(RELOC(pname), RELOC("phandle")))
23456e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
23469b6b563cSPaul Mackerras 	}
23479b6b563cSPaul Mackerras 
23486e35d5daSBenjamin Herrenschmidt 	/* Add a "linux,phandle" property if no "phandle" property already
23496e35d5daSBenjamin Herrenschmidt 	 * existed (can happen with OPAL)
23506e35d5daSBenjamin Herrenschmidt 	 */
23516e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
23529b6b563cSPaul Mackerras 		soff = dt_find_string(RELOC("linux,phandle"));
23539b6b563cSPaul Mackerras 		if (soff == 0)
23549b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
23559b6b563cSPaul Mackerras 				    " <linux-phandle> node %s\n", path);
23569b6b563cSPaul Mackerras 		else {
23579b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
23589b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
23599b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
23609b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
23619b6b563cSPaul Mackerras 			*(u32 *)valp = node;
23629b6b563cSPaul Mackerras 		}
23636e35d5daSBenjamin Herrenschmidt 	}
23649b6b563cSPaul Mackerras 
23659b6b563cSPaul Mackerras 	/* do all our children */
23669b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
23679b6b563cSPaul Mackerras 	while (child != 0) {
23689b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
23699b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
23709b6b563cSPaul Mackerras 	}
23719b6b563cSPaul Mackerras 
23729b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
23739b6b563cSPaul Mackerras }
23749b6b563cSPaul Mackerras 
23759b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
23769b6b563cSPaul Mackerras {
23779b6b563cSPaul Mackerras 	phandle root;
23789b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
23799b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
23809b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
23819b6b563cSPaul Mackerras 	char *namep;
23829b6b563cSPaul Mackerras 	u64 *rsvmap;
23839b6b563cSPaul Mackerras 
23849b6b563cSPaul Mackerras 	/*
23859b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2386fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
23879b6b563cSPaul Mackerras 	 */
23889b6b563cSPaul Mackerras 	room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
23899b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
23909b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
23919b6b563cSPaul Mackerras 	prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
23929b6b563cSPaul Mackerras 
23939b6b563cSPaul Mackerras 	/* Now try to claim that */
23949b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
23959b6b563cSPaul Mackerras 	if (mem_start == 0)
23969b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2397966728ddSAnton Blanchard 	mem_end = mem_start + room;
23989b6b563cSPaul Mackerras 
23999b6b563cSPaul Mackerras 	/* Get root of tree */
24009b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
24019b6b563cSPaul Mackerras 	if (root == (phandle)0)
24029b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
24039b6b563cSPaul Mackerras 
24049b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
24059b6b563cSPaul Mackerras 	mem_start = _ALIGN(mem_start, 4);
24069b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
24079b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
24089b6b563cSPaul Mackerras 	RELOC(dt_header_start) = (unsigned long)hdr;
24099b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
24109b6b563cSPaul Mackerras 
24119b6b563cSPaul Mackerras 	/* Start of strings */
24129b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
24139b6b563cSPaul Mackerras 	RELOC(dt_string_start) = mem_start;
24149b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
24159b6b563cSPaul Mackerras 
24169b6b563cSPaul Mackerras 	/* Add "linux,phandle" in there, we'll need it */
24179b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
24189b6b563cSPaul Mackerras 	strcpy(namep, RELOC("linux,phandle"));
24199b6b563cSPaul Mackerras 	mem_start = (unsigned long)namep + strlen(namep) + 1;
24209b6b563cSPaul Mackerras 
24219b6b563cSPaul Mackerras 	/* Build string array */
24229b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
24239b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
24249b6b563cSPaul Mackerras 	RELOC(dt_string_end) = mem_start;
24259b6b563cSPaul Mackerras 
24269b6b563cSPaul Mackerras 	/* Build structure */
24279b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
24289b6b563cSPaul Mackerras 	RELOC(dt_struct_start) = mem_start;
24299b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
24309b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
24319b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
24329b6b563cSPaul Mackerras 	RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
24339b6b563cSPaul Mackerras 
24349b6b563cSPaul Mackerras 	/* Finish header */
24359b6b563cSPaul Mackerras 	hdr->boot_cpuid_phys = _prom->cpu;
24369b6b563cSPaul Mackerras 	hdr->magic = OF_DT_HEADER;
24379b6b563cSPaul Mackerras 	hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
24389b6b563cSPaul Mackerras 	hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
24399b6b563cSPaul Mackerras 	hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
24409b6b563cSPaul Mackerras 	hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
24419b6b563cSPaul Mackerras 	hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
24429b6b563cSPaul Mackerras 	hdr->version = OF_DT_VERSION;
24439b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
24449b6b563cSPaul Mackerras 	hdr->last_comp_version = 0x10;
24459b6b563cSPaul Mackerras 
24464d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
24479b6b563cSPaul Mackerras 	memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
24489b6b563cSPaul Mackerras 
24499b6b563cSPaul Mackerras #ifdef DEBUG_PROM
24509b6b563cSPaul Mackerras 	{
24519b6b563cSPaul Mackerras 		int i;
24529b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
24539b6b563cSPaul Mackerras 		for (i = 0; i < RELOC(mem_reserve_cnt); i++)
24549b6b563cSPaul Mackerras 			prom_printf("  %x - %x\n",
24559b6b563cSPaul Mackerras 				    RELOC(mem_reserve_map)[i].base,
24569b6b563cSPaul Mackerras 				    RELOC(mem_reserve_map)[i].size);
24579b6b563cSPaul Mackerras 	}
24589b6b563cSPaul Mackerras #endif
24594d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
24604d1f3f25SJimi Xenidis 	 * since it's too late.
24614d1f3f25SJimi Xenidis 	 */
24629b6b563cSPaul Mackerras 	RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
24639b6b563cSPaul Mackerras 
24649b6b563cSPaul Mackerras 	prom_printf("Device tree strings 0x%x -> 0x%x\n",
24659b6b563cSPaul Mackerras 		    RELOC(dt_string_start), RELOC(dt_string_end));
24669b6b563cSPaul Mackerras 	prom_printf("Device tree struct  0x%x -> 0x%x\n",
24679b6b563cSPaul Mackerras 		    RELOC(dt_struct_start), RELOC(dt_struct_end));
24689b6b563cSPaul Mackerras 
24699b6b563cSPaul Mackerras }
24709b6b563cSPaul Mackerras 
247154f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
247254f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
247354f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
247454f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
24759b6b563cSPaul Mackerras {
247654f4ee18SHollis Blanchard 	phandle isa;
2477980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
247854f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2479980a6513SBenjamin Herrenschmidt 	char *name;
248054f4ee18SHollis Blanchard 
2481980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2482980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2483980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2484980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2485980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2486980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2487980a6513SBenjamin Herrenschmidt 	}
248854f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
248954f4ee18SHollis Blanchard 		return;
249054f4ee18SHollis Blanchard 
2491980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2492980a6513SBenjamin Herrenschmidt 		return;
249354f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
249454f4ee18SHollis Blanchard 		== PROM_ERROR)
249554f4ee18SHollis Blanchard 		return;
249654f4ee18SHollis Blanchard 
249754f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
249854f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
249954f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
250054f4ee18SHollis Blanchard 		return;
250154f4ee18SHollis Blanchard 
2502980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
250354f4ee18SHollis Blanchard 
250454f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
250554f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2506980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
250754f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
250854f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
250954f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2510980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
251154f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
251254f4ee18SHollis Blanchard }
25138f101a05SHarry Ciao 
25148f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
25158f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
25168f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
25178f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
25188f101a05SHarry Ciao {
25198f101a05SHarry Ciao 	phandle mc;
25208f101a05SHarry Ciao 	u32 mc_reg[4];
25218f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
25228f101a05SHarry Ciao 	struct prom_t *_prom = &RELOC(prom);
25238f101a05SHarry Ciao 	u32 ac, sc;
25248f101a05SHarry Ciao 
25258f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
25268f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
25278f101a05SHarry Ciao 		return;
25288f101a05SHarry Ciao 
25298f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
25308f101a05SHarry Ciao 		return;
25318f101a05SHarry Ciao 
25328f101a05SHarry Ciao 	prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
25338f101a05SHarry Ciao 	prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
25348f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
25358f101a05SHarry Ciao 		return;
25368f101a05SHarry Ciao 
25378f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
25388f101a05SHarry Ciao 		return;
25398f101a05SHarry Ciao 
25408f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
25418f101a05SHarry Ciao 		return;
25428f101a05SHarry Ciao 
25438f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
25448f101a05SHarry Ciao 
25458f101a05SHarry Ciao 	mc_reg[0] = 0x0;
25468f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
25478f101a05SHarry Ciao 	mc_reg[2] = 0x0;
25488f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
25498f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
25508f101a05SHarry Ciao }
255154f4ee18SHollis Blanchard #else
255254f4ee18SHollis Blanchard #define fixup_device_tree_maple()
25538f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
255454f4ee18SHollis Blanchard #endif
255554f4ee18SHollis Blanchard 
2556e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2557e4805922SOlaf Hering /*
2558e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2559e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2560556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2561e4805922SOlaf Hering  */
2562e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2563e8c0acf9SBenjamin Herrenschmidt {
2564e4805922SOlaf Hering 	phandle ph;
2565e4805922SOlaf Hering 	u32 prop[6];
256626c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2567e8c0acf9SBenjamin Herrenschmidt 	char *name;
2568e8c0acf9SBenjamin Herrenschmidt 	int rc;
2569e8c0acf9SBenjamin Herrenschmidt 
2570e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2571e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2572e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
257326c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2574e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
257526c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
257626c5032eSBenjamin Herrenschmidt 	}
2577e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2578e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2579e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2580e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2581e8c0acf9SBenjamin Herrenschmidt 
2582e4805922SOlaf Hering 			prop[0] = 0x1;
2583e4805922SOlaf Hering 			prop[1] = 0x0;
2584e4805922SOlaf Hering 			prop[2] = rloc;
2585e4805922SOlaf Hering 			prop[3] = 0x0;
2586e4805922SOlaf Hering 			prop[4] = 0x0;
2587e4805922SOlaf Hering 			prop[5] = 0x00010000;
2588e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2589e4805922SOlaf Hering 		}
2590e4805922SOlaf Hering 	}
2591e4805922SOlaf Hering 
2592e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2593e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2594e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2595e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2596e4805922SOlaf Hering 		prop[0] = 14;
2597e4805922SOlaf Hering 		prop[1] = 0x0;
2598556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2599556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2600556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2601556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2602556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2603556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2604556ecf9bSOlaf Hering 		}
2605e4805922SOlaf Hering 	}
2606e8c0acf9SBenjamin Herrenschmidt }
2607e8c0acf9SBenjamin Herrenschmidt #else
2608e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2609e8c0acf9SBenjamin Herrenschmidt #endif
2610e8c0acf9SBenjamin Herrenschmidt 
26119b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
261254f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
261354f4ee18SHollis Blanchard {
26149b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
26159b6b563cSPaul Mackerras 	u32 u3_rev;
26169b6b563cSPaul Mackerras 	u32 interrupts[2];
26179b6b563cSPaul Mackerras 	u32 parent;
26189b6b563cSPaul Mackerras 
26199b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
26209b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
26219b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
26229b6b563cSPaul Mackerras 		return;
26239b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
26249b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
26259b6b563cSPaul Mackerras 		return;
26269b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
26279b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
26289b6b563cSPaul Mackerras 		return;
26299b6b563cSPaul Mackerras 
26309b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
26319b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
26329b6b563cSPaul Mackerras 	    == PROM_ERROR)
26339b6b563cSPaul Mackerras 		return;
26347d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
26359b6b563cSPaul Mackerras 		return;
26369b6b563cSPaul Mackerras 	/* does it need fixup ? */
26379b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
26389b6b563cSPaul Mackerras 		return;
26399b6b563cSPaul Mackerras 
26409b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
26419b6b563cSPaul Mackerras 
26429b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
26439b6b563cSPaul Mackerras 	interrupts[0] = 0;
26449b6b563cSPaul Mackerras 	interrupts[1] = 1;
2645a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2646a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
26479b6b563cSPaul Mackerras 	parent = (u32)mpic;
2648a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2649a23414beSPaul Mackerras 		     &parent, sizeof(parent));
26509b6b563cSPaul Mackerras }
265154f4ee18SHollis Blanchard #else
265254f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
265354f4ee18SHollis Blanchard #endif
26549b6b563cSPaul Mackerras 
265588fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
265694d2dde7SGrant Likely /*
265794d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
265894d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
265994d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
266094d2dde7SGrant Likely  * ethernet node.
266194d2dde7SGrant Likely  */
266294d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
266388fd2a9dSSylvain Munaut {
266488fd2a9dSSylvain Munaut 	u32 node;
266588fd2a9dSSylvain Munaut 	char prop[64];
266694d2dde7SGrant Likely 	int rv;
266788fd2a9dSSylvain Munaut 
266894d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
266994d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
267088fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
267188fd2a9dSSylvain Munaut 		return;
267288fd2a9dSSylvain Munaut 
267394d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
267494d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
267594d2dde7SGrant Likely 	if (!rv)
267688fd2a9dSSylvain Munaut 		return;
267788fd2a9dSSylvain Munaut 
267894d2dde7SGrant Likely 	/*
267994d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
268094d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
268194d2dde7SGrant Likely 	 */
268288fd2a9dSSylvain Munaut 
268394d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
26846f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
26856f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
26866f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
26876f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
26886f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
26896f4347c9SOlaf Hering 			" new-device"
26906f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
26916f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
269294d2dde7SGrant Likely 				" s\" mdio\" device-name"
269394d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
26946f4347c9SOlaf Hering 				" s\" compatible\" property"
26956f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
26966f4347c9SOlaf Hering 				" 0x2 encode-int"
26976f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
26986f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
26996f4347c9SOlaf Hering 				" s\" interrupts\" property"
27006f4347c9SOlaf Hering 			" finish-device");
27016f4347c9SOlaf Hering 	};
27026f4347c9SOlaf Hering 
270394d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
270494d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
270594d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
270694d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
27076f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
27086f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
27096f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
27106f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
27116f4347c9SOlaf Hering 			" new-device"
27126f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
27136f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
27146f4347c9SOlaf Hering 				" my-self"
27156f4347c9SOlaf Hering 				" ihandle>phandle"
27166f4347c9SOlaf Hering 			" finish-device"
27176f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
27186f4347c9SOlaf Hering 				" encode-int"
27196f4347c9SOlaf Hering 				" s\" phy-handle\" property"
27206f4347c9SOlaf Hering 			" device-end");
27216f4347c9SOlaf Hering 	}
272294d2dde7SGrant Likely }
27236f4347c9SOlaf Hering 
272494d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
272594d2dde7SGrant Likely {
272694d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
272794d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
272894d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
272994d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
273094d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
273194d2dde7SGrant Likely 	u32 node;
273294d2dde7SGrant Likely 	char prop[64];
273394d2dde7SGrant Likely 	int rv, len;
273494d2dde7SGrant Likely 
273594d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
273694d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
273794d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
273894d2dde7SGrant Likely 		return;
273994d2dde7SGrant Likely 
274094d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
274194d2dde7SGrant Likely 	if (rv == PROM_ERROR)
274294d2dde7SGrant Likely 		return;
274394d2dde7SGrant Likely 	if (strcmp(prop, "EFIKA5K2"))
274494d2dde7SGrant Likely 		return;
274594d2dde7SGrant Likely 
274694d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
274794d2dde7SGrant Likely 
274894d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
274994d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
275094d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
275194d2dde7SGrant Likely 	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
275294d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
275394d2dde7SGrant Likely 
27547f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
27557f4392cdSDavid Woodhouse 	   fix that too */
27567f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
27577f4392cdSDavid Woodhouse 	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
27587f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
27597f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
27607f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
27617f4392cdSDavid Woodhouse 
276294d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
276394d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
276494d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
276594d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
276694d2dde7SGrant Likely 		if (len == 12) {
276794d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
276894d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
276994d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
277094d2dde7SGrant Likely 		}
277194d2dde7SGrant Likely 	}
277294d2dde7SGrant Likely 
277394d2dde7SGrant Likely 	/* Fixup sound interrupts property */
277494d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
277594d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
277694d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
277794d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
277894d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
277994d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
278094d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
278194d2dde7SGrant Likely 		}
278294d2dde7SGrant Likely 	}
278394d2dde7SGrant Likely 
278494d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
278594d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
278688fd2a9dSSylvain Munaut }
278788fd2a9dSSylvain Munaut #else
278888fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
278988fd2a9dSSylvain Munaut #endif
279088fd2a9dSSylvain Munaut 
279154f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
279254f4ee18SHollis Blanchard {
279354f4ee18SHollis Blanchard 	fixup_device_tree_maple();
27948f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
2795e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
279654f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
279788fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
279854f4ee18SHollis Blanchard }
27999b6b563cSPaul Mackerras 
28009b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
28019b6b563cSPaul Mackerras {
28029b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
28039b6b563cSPaul Mackerras 	u32 getprop_rval;
28049b6b563cSPaul Mackerras 	ihandle prom_cpu;
28059b6b563cSPaul Mackerras 	phandle cpu_pkg;
28069b6b563cSPaul Mackerras 
2807a575b807SPaul Mackerras 	_prom->cpu = 0;
28089b6b563cSPaul Mackerras 	if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
2809a575b807SPaul Mackerras 		return;
28109b6b563cSPaul Mackerras 
28119b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
28129b6b563cSPaul Mackerras 
28139b6b563cSPaul Mackerras 	prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
28149b6b563cSPaul Mackerras 	_prom->cpu = getprop_rval;
28159b6b563cSPaul Mackerras 
28162c48a7d6SMichael Neuling 	prom_debug("Booting CPU hw index = %lu\n", _prom->cpu);
28179b6b563cSPaul Mackerras }
28189b6b563cSPaul Mackerras 
28199b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
28209b6b563cSPaul Mackerras {
28219b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
28229b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
28239b6b563cSPaul Mackerras 
28249b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
28259b6b563cSPaul Mackerras 		unsigned long val;
28269b6b563cSPaul Mackerras 
282751fae6deSMichael Ellerman 		RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
28289b6b563cSPaul Mackerras 		RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
28299b6b563cSPaul Mackerras 
28309b6b563cSPaul Mackerras 		val = RELOC(prom_initrd_start);
2831a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2832a23414beSPaul Mackerras 			     &val, sizeof(val));
28339b6b563cSPaul Mackerras 		val = RELOC(prom_initrd_end);
2834a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2835a23414beSPaul Mackerras 			     &val, sizeof(val));
28369b6b563cSPaul Mackerras 
28379b6b563cSPaul Mackerras 		reserve_mem(RELOC(prom_initrd_start),
28389b6b563cSPaul Mackerras 			    RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
28399b6b563cSPaul Mackerras 
28409b6b563cSPaul Mackerras 		prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
28419b6b563cSPaul Mackerras 		prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
28429b6b563cSPaul Mackerras 	}
28439b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
28449b6b563cSPaul Mackerras }
28459b6b563cSPaul Mackerras 
2846*5ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64
2847*5ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE
2848*5ac47f7aSAnton Blanchard static void reloc_toc(void)
2849*5ac47f7aSAnton Blanchard {
2850*5ac47f7aSAnton Blanchard }
2851*5ac47f7aSAnton Blanchard 
2852*5ac47f7aSAnton Blanchard static void unreloc_toc(void)
2853*5ac47f7aSAnton Blanchard {
2854*5ac47f7aSAnton Blanchard }
2855*5ac47f7aSAnton Blanchard #else
2856*5ac47f7aSAnton Blanchard static void __reloc_toc(void *tocstart, unsigned long offset,
2857*5ac47f7aSAnton Blanchard 			unsigned long nr_entries)
2858*5ac47f7aSAnton Blanchard {
2859*5ac47f7aSAnton Blanchard 	unsigned long i;
2860*5ac47f7aSAnton Blanchard 	unsigned long *toc_entry = (unsigned long *)tocstart;
2861*5ac47f7aSAnton Blanchard 
2862*5ac47f7aSAnton Blanchard 	for (i = 0; i < nr_entries; i++) {
2863*5ac47f7aSAnton Blanchard 		*toc_entry = *toc_entry + offset;
2864*5ac47f7aSAnton Blanchard 		toc_entry++;
2865*5ac47f7aSAnton Blanchard 	}
2866*5ac47f7aSAnton Blanchard }
2867*5ac47f7aSAnton Blanchard 
2868*5ac47f7aSAnton Blanchard static void reloc_toc(void)
2869*5ac47f7aSAnton Blanchard {
2870*5ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
2871*5ac47f7aSAnton Blanchard 	unsigned long nr_entries =
2872*5ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
2873*5ac47f7aSAnton Blanchard 
2874*5ac47f7aSAnton Blanchard 	/* Need to add offset to get at __prom_init_toc_start */
2875*5ac47f7aSAnton Blanchard 	__reloc_toc(__prom_init_toc_start + offset, offset, nr_entries);
2876*5ac47f7aSAnton Blanchard 
2877*5ac47f7aSAnton Blanchard 	mb();
2878*5ac47f7aSAnton Blanchard }
2879*5ac47f7aSAnton Blanchard 
2880*5ac47f7aSAnton Blanchard static void unreloc_toc(void)
2881*5ac47f7aSAnton Blanchard {
2882*5ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
2883*5ac47f7aSAnton Blanchard 	unsigned long nr_entries =
2884*5ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
2885*5ac47f7aSAnton Blanchard 
2886*5ac47f7aSAnton Blanchard 	mb();
2887*5ac47f7aSAnton Blanchard 
2888*5ac47f7aSAnton Blanchard 	/* __prom_init_toc_start has been relocated, no need to add offset */
2889*5ac47f7aSAnton Blanchard 	__reloc_toc(__prom_init_toc_start, -offset, nr_entries);
2890*5ac47f7aSAnton Blanchard }
2891*5ac47f7aSAnton Blanchard #endif
2892*5ac47f7aSAnton Blanchard #endif
289327f44888SBenjamin Herrenschmidt 
28949b6b563cSPaul Mackerras /*
28959b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
28969b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
28979b6b563cSPaul Mackerras  */
28989b6b563cSPaul Mackerras 
28999b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
29009b6b563cSPaul Mackerras 			       unsigned long pp,
2901549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
2902549e8152SPaul Mackerras 			       unsigned long kbase)
29039b6b563cSPaul Mackerras {
29049b6b563cSPaul Mackerras 	struct prom_t *_prom;
29059b6b563cSPaul Mackerras 	unsigned long hdr;
29069b6b563cSPaul Mackerras 
29079b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
2908549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
29099b6b563cSPaul Mackerras 	reloc_got2(offset);
2910*5ac47f7aSAnton Blanchard #else
2911*5ac47f7aSAnton Blanchard 	reloc_toc();
29129b6b563cSPaul Mackerras #endif
29139b6b563cSPaul Mackerras 
29149b6b563cSPaul Mackerras 	_prom = &RELOC(prom);
29159b6b563cSPaul Mackerras 
29169b6b563cSPaul Mackerras 	/*
29179b6b563cSPaul Mackerras 	 * First zero the BSS
29189b6b563cSPaul Mackerras 	 */
29199b6b563cSPaul Mackerras 	memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
29209b6b563cSPaul Mackerras 
29219b6b563cSPaul Mackerras 	/*
29229b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
29239b6b563cSPaul Mackerras 	 * like /chosen
29249b6b563cSPaul Mackerras 	 */
29259b6b563cSPaul Mackerras 	prom_init_client_services(pp);
29269b6b563cSPaul Mackerras 
29279b6b563cSPaul Mackerras 	/*
2928a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
2929a23414beSPaul Mackerras 	 * and other workarounds
2930a23414beSPaul Mackerras 	 */
2931a23414beSPaul Mackerras 	prom_find_mmu();
2932a23414beSPaul Mackerras 
2933a23414beSPaul Mackerras 	/*
29349b6b563cSPaul Mackerras 	 * Init prom stdout device
29359b6b563cSPaul Mackerras 	 */
29369b6b563cSPaul Mackerras 	prom_init_stdout();
29379b6b563cSPaul Mackerras 
2938151a9f4aSBenjamin Herrenschmidt 	prom_printf("Preparing to boot %s", RELOC(linux_banner));
2939e7943fbbSMichael Ellerman 
29409b6b563cSPaul Mackerras 	/*
29419b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
29429b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
29439b6b563cSPaul Mackerras 	 */
29449b6b563cSPaul Mackerras 	RELOC(of_platform) = prom_find_machine_type();
29456e35d5daSBenjamin Herrenschmidt 	prom_printf("Detected machine type: %x\n", RELOC(of_platform));
29469b6b563cSPaul Mackerras 
29470f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
2948add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
2949add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
2950add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
2951549e8152SPaul Mackerras #endif
2952add60ef3SOlaf Hering 
2953add60ef3SOlaf Hering 	/*
2954add60ef3SOlaf Hering 	 * Check for an initrd
2955add60ef3SOlaf Hering 	 */
2956add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
2957add60ef3SOlaf Hering 
295827f44888SBenjamin Herrenschmidt #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
29599b6b563cSPaul Mackerras 	/*
29609b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
29619b6b563cSPaul Mackerras 	 */
2962799d6046SPaul Mackerras 	if (RELOC(of_platform) == PLATFORM_PSERIES ||
2963799d6046SPaul Mackerras 	    RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
29649b6b563cSPaul Mackerras 		prom_send_capabilities();
29659b6b563cSPaul Mackerras #endif
29669b6b563cSPaul Mackerras 
29679b6b563cSPaul Mackerras 	/*
2968f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
29699b6b563cSPaul Mackerras 	 */
297055d36339SDavid Gibson 	if (RELOC(of_platform) != PLATFORM_POWERMAC)
2971549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
29729b6b563cSPaul Mackerras 
29739b6b563cSPaul Mackerras 	/*
29749b6b563cSPaul Mackerras 	 * Do early parsing of command line
29759b6b563cSPaul Mackerras 	 */
29769b6b563cSPaul Mackerras 	early_cmdline_parse();
29779b6b563cSPaul Mackerras 
29789b6b563cSPaul Mackerras 	/*
29799b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
29809b6b563cSPaul Mackerras 	 */
29819b6b563cSPaul Mackerras 	prom_init_mem();
29829b6b563cSPaul Mackerras 
29839b6b563cSPaul Mackerras 	/*
29849b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
29859b6b563cSPaul Mackerras 	 */
29869b6b563cSPaul Mackerras 	prom_find_boot_cpu();
29879b6b563cSPaul Mackerras 
29889b6b563cSPaul Mackerras 	/*
29899b6b563cSPaul Mackerras 	 * Initialize display devices
29909b6b563cSPaul Mackerras 	 */
29919b6b563cSPaul Mackerras 	prom_check_displays();
29929b6b563cSPaul Mackerras 
29939b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
29949b6b563cSPaul Mackerras 	/*
29959b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
29969b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
29979b6b563cSPaul Mackerras 	 * available for us here...
29989b6b563cSPaul Mackerras 	 */
29999b6b563cSPaul Mackerras 	if (RELOC(of_platform) == PLATFORM_PSERIES)
30009b6b563cSPaul Mackerras 		prom_initialize_tce_table();
30019b6b563cSPaul Mackerras #endif
30029b6b563cSPaul Mackerras 
30039b6b563cSPaul Mackerras 	/*
300427f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
300527f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
30069b6b563cSPaul Mackerras 	 */
30076e35d5daSBenjamin Herrenschmidt 	if (RELOC(of_platform) != PLATFORM_POWERMAC &&
30086e35d5daSBenjamin Herrenschmidt 	    RELOC(of_platform) != PLATFORM_OPAL)
30099b6b563cSPaul Mackerras 		prom_instantiate_rtas();
301027f44888SBenjamin Herrenschmidt 
301127f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
301227f44888SBenjamin Herrenschmidt 	/* Detect HAL and try instanciating it & doing takeover */
301327f44888SBenjamin Herrenschmidt 	if (RELOC(of_platform) == PLATFORM_PSERIES_LPAR) {
301427f44888SBenjamin Herrenschmidt 		prom_query_opal();
301527f44888SBenjamin Herrenschmidt 		if (RELOC(of_platform) == PLATFORM_OPAL) {
301627f44888SBenjamin Herrenschmidt 			prom_opal_hold_cpus();
301727f44888SBenjamin Herrenschmidt 			prom_opal_takeover();
30189b6b563cSPaul Mackerras 		}
30196e35d5daSBenjamin Herrenschmidt 	} else if (RELOC(of_platform) == PLATFORM_OPAL)
30206e35d5daSBenjamin Herrenschmidt 		prom_instantiate_opal();
302127f44888SBenjamin Herrenschmidt #endif
302227f44888SBenjamin Herrenschmidt 
30234a727429SAshley Lai #ifdef CONFIG_PPC64
30244a727429SAshley Lai 	/* instantiate sml */
30254a727429SAshley Lai 	prom_instantiate_sml();
30264a727429SAshley Lai #endif
30274a727429SAshley Lai 
302827f44888SBenjamin Herrenschmidt 	/*
302927f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
303027f44888SBenjamin Herrenschmidt 	 *
303127f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
303227f44888SBenjamin Herrenschmidt 	 */
30336e35d5daSBenjamin Herrenschmidt 	if (RELOC(of_platform) != PLATFORM_POWERMAC &&
30346e35d5daSBenjamin Herrenschmidt 	    RELOC(of_platform) != PLATFORM_OPAL)
303527f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
30369b6b563cSPaul Mackerras 
30379b6b563cSPaul Mackerras 	/*
30389b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
30399b6b563cSPaul Mackerras 	 */
3040cf68787bSBenjamin Krill 	if (RELOC(prom_memory_limit))
3041cf68787bSBenjamin Krill 		prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
3042cf68787bSBenjamin Krill 			     &RELOC(prom_memory_limit),
3043cf68787bSBenjamin Krill 			     sizeof(prom_memory_limit));
30449b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
3045165785e5SJeremy Kerr 	if (RELOC(prom_iommu_off))
3046a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
3047a23414beSPaul Mackerras 			     NULL, 0);
30489b6b563cSPaul Mackerras 
3049165785e5SJeremy Kerr 	if (RELOC(prom_iommu_force_on))
3050a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
3051a23414beSPaul Mackerras 			     NULL, 0);
30529b6b563cSPaul Mackerras 
30539b6b563cSPaul Mackerras 	if (RELOC(prom_tce_alloc_start)) {
3054a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
30559b6b563cSPaul Mackerras 			     &RELOC(prom_tce_alloc_start),
30569b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
3057a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
30589b6b563cSPaul Mackerras 			     &RELOC(prom_tce_alloc_end),
30599b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
30609b6b563cSPaul Mackerras 	}
30619b6b563cSPaul Mackerras #endif
30629b6b563cSPaul Mackerras 
30639b6b563cSPaul Mackerras 	/*
30649b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
30659b6b563cSPaul Mackerras 	 */
30669b6b563cSPaul Mackerras 	fixup_device_tree();
30679b6b563cSPaul Mackerras 
30689b6b563cSPaul Mackerras 	/*
30699b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
30709b6b563cSPaul Mackerras 	 */
30719b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
30729b6b563cSPaul Mackerras 	flatten_device_tree();
30739b6b563cSPaul Mackerras 
30743825ac0eSPaul Mackerras 	/*
30753825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
30763825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
307740dfef66SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101). It
307840dfef66SBenjamin Herrenschmidt 	 * appears that the OPAL version of OFW doesn't like it either.
30793825ac0eSPaul Mackerras 	 */
308040dfef66SBenjamin Herrenschmidt 	if (RELOC(of_platform) != PLATFORM_POWERMAC &&
308140dfef66SBenjamin Herrenschmidt 	    RELOC(of_platform) != PLATFORM_OPAL)
30829b6b563cSPaul Mackerras 		prom_close_stdin();
30839b6b563cSPaul Mackerras 
30849b6b563cSPaul Mackerras 	/*
30859b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
30869b6b563cSPaul Mackerras 	 * devices etc...
30879b6b563cSPaul Mackerras 	 */
30889b6b563cSPaul Mackerras 	prom_printf("Calling quiesce...\n");
30899b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
30909b6b563cSPaul Mackerras 
30919b6b563cSPaul Mackerras 	/*
30929b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
30939b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
30949b6b563cSPaul Mackerras 	 * is common to us and kexec
30959b6b563cSPaul Mackerras 	 */
30969b6b563cSPaul Mackerras 	hdr = RELOC(dt_header_start);
309740dfef66SBenjamin Herrenschmidt 
309840dfef66SBenjamin Herrenschmidt 	/* Don't print anything after quiesce under OPAL, it crashes OFW */
309940dfef66SBenjamin Herrenschmidt 	if (RELOC(of_platform) != PLATFORM_OPAL) {
31009b6b563cSPaul Mackerras 		prom_printf("returning from prom_init\n");
31019b6b563cSPaul Mackerras 		prom_debug("->dt_header_start=0x%x\n", hdr);
310240dfef66SBenjamin Herrenschmidt 	}
31039b6b563cSPaul Mackerras 
31049b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
31059b6b563cSPaul Mackerras 	reloc_got2(-offset);
3106*5ac47f7aSAnton Blanchard #else
3107*5ac47f7aSAnton Blanchard 	unreloc_toc();
31089b6b563cSPaul Mackerras #endif
31099b6b563cSPaul Mackerras 
31106e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
31116e35d5daSBenjamin Herrenschmidt 	/* OPAL early debug gets the OPAL base & entry in r8 and r9 */
31126e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0,
31136e35d5daSBenjamin Herrenschmidt 		RELOC(prom_opal_base), RELOC(prom_opal_entry));
31146e35d5daSBenjamin Herrenschmidt #else
31156e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
31166e35d5daSBenjamin Herrenschmidt #endif
31179b6b563cSPaul Mackerras 
31189b6b563cSPaul Mackerras 	return 0;
31199b6b563cSPaul Mackerras }
3120