xref: /openbmc/linux/arch/powerpc/kernel/prom_init.c (revision dbd0c5d5296f291a5c3affee4fbdde254632ffca)
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.
695ac47f7aSAnton Blanchard  * ppc64 objects are always relocatable, we just need to relocate the
705ac47f7aSAnton Blanchard  * TOC.
719b6b563cSPaul Mackerras  *
729b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
739b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
749b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
759b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
769b6b563cSPaul Mackerras  *
779b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
789b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
799b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
809b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
819b6b563cSPaul Mackerras  */
825ac47f7aSAnton Blanchard #define ADDR(x)		(u32)(unsigned long)(x)
835ac47f7aSAnton Blanchard 
849b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
85a23414beSPaul Mackerras #define OF_WORKAROUNDS	0
869b6b563cSPaul Mackerras #else
87a23414beSPaul Mackerras #define OF_WORKAROUNDS	of_workarounds
88a23414beSPaul Mackerras int of_workarounds;
899b6b563cSPaul Mackerras #endif
909b6b563cSPaul Mackerras 
91a23414beSPaul Mackerras #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
92a23414beSPaul Mackerras #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
93a23414beSPaul Mackerras 
949b6b563cSPaul Mackerras #define PROM_BUG() do {						\
959b6b563cSPaul Mackerras         prom_printf("kernel BUG at %s line 0x%x!\n",		\
965827d416SAnton Blanchard 		    __FILE__, __LINE__);			\
979b6b563cSPaul Mackerras         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
989b6b563cSPaul Mackerras } while (0)
999b6b563cSPaul Mackerras 
1009b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1019b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1029b6b563cSPaul Mackerras #else
1039b6b563cSPaul Mackerras #define prom_debug(x...)
1049b6b563cSPaul Mackerras #endif
1059b6b563cSPaul Mackerras 
1069b6b563cSPaul Mackerras 
1079b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1089b6b563cSPaul Mackerras 
1099b6b563cSPaul Mackerras struct prom_args {
110493adffcSBenjamin Herrenschmidt         __be32 service;
111493adffcSBenjamin Herrenschmidt         __be32 nargs;
112493adffcSBenjamin Herrenschmidt         __be32 nret;
113493adffcSBenjamin Herrenschmidt         __be32 args[10];
1149b6b563cSPaul Mackerras };
1159b6b563cSPaul Mackerras 
1169b6b563cSPaul Mackerras struct prom_t {
1179b6b563cSPaul Mackerras 	ihandle root;
118a23414beSPaul Mackerras 	phandle chosen;
1199b6b563cSPaul Mackerras 	int cpu;
1209b6b563cSPaul Mackerras 	ihandle stdout;
121a575b807SPaul Mackerras 	ihandle mmumap;
122a23414beSPaul Mackerras 	ihandle memory;
1239b6b563cSPaul Mackerras };
1249b6b563cSPaul Mackerras 
1259b6b563cSPaul Mackerras struct mem_map_entry {
126493adffcSBenjamin Herrenschmidt 	__be64	base;
127493adffcSBenjamin Herrenschmidt 	__be64	size;
1289b6b563cSPaul Mackerras };
1299b6b563cSPaul Mackerras 
130493adffcSBenjamin Herrenschmidt typedef __be32 cell_t;
1319b6b563cSPaul Mackerras 
1326e35d5daSBenjamin Herrenschmidt extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
1336e35d5daSBenjamin Herrenschmidt 		    unsigned long r6, unsigned long r7, unsigned long r8,
1346e35d5daSBenjamin Herrenschmidt 		    unsigned long r9);
1359b6b563cSPaul Mackerras 
1369b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
137c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1389b6b563cSPaul Mackerras #else
139c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1409b6b563cSPaul Mackerras {
141c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1429b6b563cSPaul Mackerras }
1439b6b563cSPaul Mackerras #endif
1449b6b563cSPaul Mackerras 
1459b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1469b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1479b6b563cSPaul Mackerras 
1489b6b563cSPaul Mackerras /* prom structure */
1499b6b563cSPaul Mackerras static struct prom_t __initdata prom;
1509b6b563cSPaul Mackerras 
1519b6b563cSPaul Mackerras static unsigned long prom_entry __initdata;
1529b6b563cSPaul Mackerras 
1539b6b563cSPaul Mackerras #define PROM_SCRATCH_SIZE 256
1549b6b563cSPaul Mackerras 
1559b6b563cSPaul Mackerras static char __initdata of_stdout_device[256];
1569b6b563cSPaul Mackerras static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
1579b6b563cSPaul Mackerras 
1589b6b563cSPaul Mackerras static unsigned long __initdata dt_header_start;
1599b6b563cSPaul Mackerras static unsigned long __initdata dt_struct_start, dt_struct_end;
1609b6b563cSPaul Mackerras static unsigned long __initdata dt_string_start, dt_string_end;
1619b6b563cSPaul Mackerras 
1629b6b563cSPaul Mackerras static unsigned long __initdata prom_initrd_start, prom_initrd_end;
1639b6b563cSPaul Mackerras 
1649b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
165165785e5SJeremy Kerr static int __initdata prom_iommu_force_on;
166165785e5SJeremy Kerr static int __initdata prom_iommu_off;
1679b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_start;
1689b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_end;
1699b6b563cSPaul Mackerras #endif
1709b6b563cSPaul Mackerras 
171e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
172e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
173e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
174e8222502SBenjamin Herrenschmidt  */
175e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
176e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
177e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
178e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
179e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
18027f44888SBenjamin Herrenschmidt #define PLATFORM_OPAL		0x0600
181e8222502SBenjamin Herrenschmidt 
1829b6b563cSPaul Mackerras static int __initdata of_platform;
1839b6b563cSPaul Mackerras 
1849b6b563cSPaul Mackerras static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
1859b6b563cSPaul Mackerras 
186cf68787bSBenjamin Krill static unsigned long __initdata prom_memory_limit;
187cf68787bSBenjamin Krill 
1889b6b563cSPaul Mackerras static unsigned long __initdata alloc_top;
1899b6b563cSPaul Mackerras static unsigned long __initdata alloc_top_high;
1909b6b563cSPaul Mackerras static unsigned long __initdata alloc_bottom;
1919b6b563cSPaul Mackerras static unsigned long __initdata rmo_top;
1929b6b563cSPaul Mackerras static unsigned long __initdata ram_top;
1939b6b563cSPaul Mackerras 
1949b6b563cSPaul Mackerras static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
1959b6b563cSPaul Mackerras static int __initdata mem_reserve_cnt;
1969b6b563cSPaul Mackerras 
1979b6b563cSPaul Mackerras static cell_t __initdata regbuf[1024];
1989b6b563cSPaul Mackerras 
199dbe78b40SBenjamin Herrenschmidt static bool rtas_has_query_cpu_stopped;
200dbe78b40SBenjamin Herrenschmidt 
2019b6b563cSPaul Mackerras 
2029b6b563cSPaul Mackerras /*
2039b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2049b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2059b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2069b6b563cSPaul Mackerras  * it is valid
2079b6b563cSPaul Mackerras  */
2089b6b563cSPaul Mackerras 
2099b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2109b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2119b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2129b6b563cSPaul Mackerras 
2139b6b563cSPaul Mackerras 
2149b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
2159b6b563cSPaul Mackerras  * firmware.
2169b6b563cSPaul Mackerras  */
2179b6b563cSPaul Mackerras 
2189b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
2199b6b563cSPaul Mackerras {
2209b6b563cSPaul Mackerras 	int i;
2219b6b563cSPaul Mackerras 	struct prom_args args;
2229b6b563cSPaul Mackerras 	va_list list;
2239b6b563cSPaul Mackerras 
224493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
225493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
226493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
2279b6b563cSPaul Mackerras 
2289b6b563cSPaul Mackerras 	va_start(list, nret);
2299b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
230493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
2319b6b563cSPaul Mackerras 	va_end(list);
2329b6b563cSPaul Mackerras 
2339b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
2349b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
2359b6b563cSPaul Mackerras 
2365827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
237c4988820SPaul Mackerras 		return PROM_ERROR;
2389b6b563cSPaul Mackerras 
239493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
2409b6b563cSPaul Mackerras }
2419b6b563cSPaul Mackerras 
2429b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
2439b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
2449b6b563cSPaul Mackerras {
2459b6b563cSPaul Mackerras 	int i;
2469b6b563cSPaul Mackerras 	struct prom_args args;
2479b6b563cSPaul Mackerras 	va_list list;
2489b6b563cSPaul Mackerras 
249493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
250493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
251493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
2529b6b563cSPaul Mackerras 
2539b6b563cSPaul Mackerras 	va_start(list, rets);
2549b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
255493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
2569b6b563cSPaul Mackerras 	va_end(list);
2579b6b563cSPaul Mackerras 
2589b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
259ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
2609b6b563cSPaul Mackerras 
2615827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
262c4988820SPaul Mackerras 		return PROM_ERROR;
2639b6b563cSPaul Mackerras 
2649b6b563cSPaul Mackerras 	if (rets != NULL)
2659b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
266493adffcSBenjamin Herrenschmidt 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
2679b6b563cSPaul Mackerras 
268493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
2699b6b563cSPaul Mackerras }
2709b6b563cSPaul Mackerras 
2719b6b563cSPaul Mackerras 
2729b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
2739b6b563cSPaul Mackerras {
2749b6b563cSPaul Mackerras 	const char *p, *q;
2759b6b563cSPaul Mackerras 
2765827d416SAnton Blanchard 	if (prom.stdout == 0)
2779b6b563cSPaul Mackerras 		return;
2789b6b563cSPaul Mackerras 
2799b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
2809b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
2819b6b563cSPaul Mackerras 			;
2829b6b563cSPaul Mackerras 		if (q > p)
2835827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
2849b6b563cSPaul Mackerras 		if (*q == 0)
2859b6b563cSPaul Mackerras 			break;
2869b6b563cSPaul Mackerras 		++q;
2875827d416SAnton Blanchard 		call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
2889b6b563cSPaul Mackerras 	}
2899b6b563cSPaul Mackerras }
2909b6b563cSPaul Mackerras 
2919b6b563cSPaul Mackerras 
2929b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
2939b6b563cSPaul Mackerras {
2949b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
2959b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
2969b6b563cSPaul Mackerras 
2979b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
2989b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
2999b6b563cSPaul Mackerras 		if (buf[i] > '9')
3009b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
3019b6b563cSPaul Mackerras 		val >>= 4;
3029b6b563cSPaul Mackerras 	}
3039b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
3045827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf, nibbles);
3059b6b563cSPaul Mackerras }
3069b6b563cSPaul Mackerras 
3072c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */
3082c48a7d6SMichael Neuling #define UL_DIGITS 21
3092c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val)
3102c48a7d6SMichael Neuling {
3112c48a7d6SMichael Neuling 	int i, size;
3122c48a7d6SMichael Neuling 	char buf[UL_DIGITS+1];
3132c48a7d6SMichael Neuling 
3142c48a7d6SMichael Neuling 	for (i = UL_DIGITS-1; i >= 0;  i--) {
3152c48a7d6SMichael Neuling 		buf[i] = (val % 10) + '0';
3162c48a7d6SMichael Neuling 		val = val/10;
3172c48a7d6SMichael Neuling 		if (val == 0)
3182c48a7d6SMichael Neuling 			break;
3192c48a7d6SMichael Neuling 	}
3202c48a7d6SMichael Neuling 	/* shift stuff down */
3212c48a7d6SMichael Neuling 	size = UL_DIGITS - i;
3225827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf+i, size);
3232c48a7d6SMichael Neuling }
3249b6b563cSPaul Mackerras 
3259b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
3269b6b563cSPaul Mackerras {
3279b6b563cSPaul Mackerras 	const char *p, *q, *s;
3289b6b563cSPaul Mackerras 	va_list args;
3299b6b563cSPaul Mackerras 	unsigned long v;
330af277149SBenjamin Herrenschmidt 	long vs;
3319b6b563cSPaul Mackerras 
3329b6b563cSPaul Mackerras 	va_start(args, format);
3339b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
3349b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
3359b6b563cSPaul Mackerras 			;
3369b6b563cSPaul Mackerras 		if (q > p)
3375827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
3389b6b563cSPaul Mackerras 		if (*q == 0)
3399b6b563cSPaul Mackerras 			break;
3409b6b563cSPaul Mackerras 		if (*q == '\n') {
3419b6b563cSPaul Mackerras 			++q;
3425827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout,
3439b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
3449b6b563cSPaul Mackerras 			continue;
3459b6b563cSPaul Mackerras 		}
3469b6b563cSPaul Mackerras 		++q;
3479b6b563cSPaul Mackerras 		if (*q == 0)
3489b6b563cSPaul Mackerras 			break;
3499b6b563cSPaul Mackerras 		switch (*q) {
3509b6b563cSPaul Mackerras 		case 's':
3519b6b563cSPaul Mackerras 			++q;
3529b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
3539b6b563cSPaul Mackerras 			prom_print(s);
3549b6b563cSPaul Mackerras 			break;
3559b6b563cSPaul Mackerras 		case 'x':
3569b6b563cSPaul Mackerras 			++q;
3579b6b563cSPaul Mackerras 			v = va_arg(args, unsigned long);
3589b6b563cSPaul Mackerras 			prom_print_hex(v);
3599b6b563cSPaul Mackerras 			break;
360af277149SBenjamin Herrenschmidt 		case 'd':
361af277149SBenjamin Herrenschmidt 			++q;
362af277149SBenjamin Herrenschmidt 			vs = va_arg(args, int);
363af277149SBenjamin Herrenschmidt 			if (vs < 0) {
3645827d416SAnton Blanchard 				prom_print("-");
365af277149SBenjamin Herrenschmidt 				vs = -vs;
366af277149SBenjamin Herrenschmidt 			}
367af277149SBenjamin Herrenschmidt 			prom_print_dec(vs);
368af277149SBenjamin Herrenschmidt 			break;
3692c48a7d6SMichael Neuling 		case 'l':
3702c48a7d6SMichael Neuling 			++q;
371af277149SBenjamin Herrenschmidt 			if (*q == 0)
372af277149SBenjamin Herrenschmidt 				break;
373af277149SBenjamin Herrenschmidt 			else if (*q == 'x') {
374af277149SBenjamin Herrenschmidt 				++q;
375af277149SBenjamin Herrenschmidt 				v = va_arg(args, unsigned long);
376af277149SBenjamin Herrenschmidt 				prom_print_hex(v);
377af277149SBenjamin Herrenschmidt 			} else if (*q == 'u') { /* '%lu' */
3782c48a7d6SMichael Neuling 				++q;
3792c48a7d6SMichael Neuling 				v = va_arg(args, unsigned long);
3802c48a7d6SMichael Neuling 				prom_print_dec(v);
381af277149SBenjamin Herrenschmidt 			} else if (*q == 'd') { /* %ld */
382af277149SBenjamin Herrenschmidt 				++q;
383af277149SBenjamin Herrenschmidt 				vs = va_arg(args, long);
384af277149SBenjamin Herrenschmidt 				if (vs < 0) {
3855827d416SAnton Blanchard 					prom_print("-");
386af277149SBenjamin Herrenschmidt 					vs = -vs;
387af277149SBenjamin Herrenschmidt 				}
388af277149SBenjamin Herrenschmidt 				prom_print_dec(vs);
3892c48a7d6SMichael Neuling 			}
3902c48a7d6SMichael Neuling 			break;
3919b6b563cSPaul Mackerras 		}
3929b6b563cSPaul Mackerras 	}
3939b6b563cSPaul Mackerras }
3949b6b563cSPaul Mackerras 
3959b6b563cSPaul Mackerras 
396a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
397a575b807SPaul Mackerras 				unsigned long align)
398a575b807SPaul Mackerras {
399a575b807SPaul Mackerras 
400a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
401a23414beSPaul Mackerras 		/*
402a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
403a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
404a23414beSPaul Mackerras 		 */
405a23414beSPaul Mackerras 		int ret;
406a23414beSPaul Mackerras 		prom_arg_t result;
407a23414beSPaul Mackerras 
408a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
4095827d416SAnton Blanchard 				    ADDR("claim"), prom.memory,
410a23414beSPaul Mackerras 				    align, size, virt);
411a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
412a23414beSPaul Mackerras 			return -1;
413a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
4145827d416SAnton Blanchard 				    ADDR("claim"), prom.mmumap,
415a23414beSPaul Mackerras 				    align, size, virt);
416a23414beSPaul Mackerras 		if (ret != 0) {
417a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
4185827d416SAnton Blanchard 				  prom.memory, size, virt);
419a23414beSPaul Mackerras 			return -1;
420a23414beSPaul Mackerras 		}
421a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
422a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
4235827d416SAnton Blanchard 			  ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
424a23414beSPaul Mackerras 		return virt;
425a23414beSPaul Mackerras 	}
426a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
427a23414beSPaul Mackerras 			 (prom_arg_t)align);
428a575b807SPaul Mackerras }
429a575b807SPaul Mackerras 
4309b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
4319b6b563cSPaul Mackerras {
4329b6b563cSPaul Mackerras 	prom_print(reason);
433add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
434add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
4355827d416SAnton Blanchard 	if (of_platform == PLATFORM_POWERMAC)
436add60ef3SOlaf Hering 		asm("trap\n");
437add60ef3SOlaf Hering 
4381d9a4731SStephen Rothwell 	/* ToDo: should put up an SRC here on pSeries */
4399b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
4409b6b563cSPaul Mackerras 
4419b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
4429b6b563cSPaul Mackerras 		;
4439b6b563cSPaul Mackerras }
4449b6b563cSPaul Mackerras 
4459b6b563cSPaul Mackerras 
4469b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
4479b6b563cSPaul Mackerras {
4489b6b563cSPaul Mackerras 	phandle node;
4499b6b563cSPaul Mackerras 
4509b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
4519b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
4529b6b563cSPaul Mackerras 		return 1;
4539b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4549b6b563cSPaul Mackerras 		return 1;
4559b6b563cSPaul Mackerras 	for (;;) {
4569b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
4579b6b563cSPaul Mackerras 			return 0;
4589b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4599b6b563cSPaul Mackerras 			return 1;
4609b6b563cSPaul Mackerras 	}
4619b6b563cSPaul Mackerras }
4629b6b563cSPaul Mackerras 
46321fe3301SBenjamin Herrenschmidt static int inline prom_getprop(phandle node, const char *pname,
4649b6b563cSPaul Mackerras 			       void *value, size_t valuelen)
4659b6b563cSPaul Mackerras {
4669b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
4679b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
4689b6b563cSPaul Mackerras }
4699b6b563cSPaul Mackerras 
47021fe3301SBenjamin Herrenschmidt static int inline prom_getproplen(phandle node, const char *pname)
4719b6b563cSPaul Mackerras {
4729b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
4739b6b563cSPaul Mackerras }
4749b6b563cSPaul Mackerras 
475a23414beSPaul Mackerras static void add_string(char **str, const char *q)
4769b6b563cSPaul Mackerras {
477a23414beSPaul Mackerras 	char *p = *str;
478a23414beSPaul Mackerras 
479a23414beSPaul Mackerras 	while (*q)
480a23414beSPaul Mackerras 		*p++ = *q++;
481a23414beSPaul Mackerras 	*p++ = ' ';
482a23414beSPaul Mackerras 	*str = p;
483a23414beSPaul Mackerras }
484a23414beSPaul Mackerras 
485a23414beSPaul Mackerras static char *tohex(unsigned int x)
486a23414beSPaul Mackerras {
487a23414beSPaul Mackerras 	static char digits[] = "0123456789abcdef";
488a23414beSPaul Mackerras 	static char result[9];
489a23414beSPaul Mackerras 	int i;
490a23414beSPaul Mackerras 
491a23414beSPaul Mackerras 	result[8] = 0;
492a23414beSPaul Mackerras 	i = 8;
493a23414beSPaul Mackerras 	do {
494a23414beSPaul Mackerras 		--i;
495a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
496a23414beSPaul Mackerras 		x >>= 4;
497a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
498a23414beSPaul Mackerras 	return &result[i];
499a23414beSPaul Mackerras }
500a23414beSPaul Mackerras 
501a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
502a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
503a23414beSPaul Mackerras {
504a23414beSPaul Mackerras 	char cmd[256], *p;
505a23414beSPaul Mackerras 
506a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
5079b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
5089b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
509a23414beSPaul Mackerras 
510a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
511a23414beSPaul Mackerras 	p = cmd;
512a23414beSPaul Mackerras 	add_string(&p, "dev");
513a23414beSPaul Mackerras 	add_string(&p, nodename);
514a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
515a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
516a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
5175827d416SAnton Blanchard 	add_string(&p, tohex(strlen(pname)));
518a23414beSPaul Mackerras 	add_string(&p, "property");
519a23414beSPaul Mackerras 	*p = 0;
520a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
5219b6b563cSPaul Mackerras }
5229b6b563cSPaul Mackerras 
5235827d416SAnton Blanchard /* We can't use the standard versions because of relocation headaches. */
524cf68787bSBenjamin Krill #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
525cf68787bSBenjamin Krill 			 || ('a' <= (c) && (c) <= 'f') \
526cf68787bSBenjamin Krill 			 || ('A' <= (c) && (c) <= 'F'))
527cf68787bSBenjamin Krill 
528cf68787bSBenjamin Krill #define isdigit(c)	('0' <= (c) && (c) <= '9')
529cf68787bSBenjamin Krill #define islower(c)	('a' <= (c) && (c) <= 'z')
530cf68787bSBenjamin Krill #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
531cf68787bSBenjamin Krill 
532493adffcSBenjamin Herrenschmidt static unsigned long prom_strtoul(const char *cp, const char **endp)
533cf68787bSBenjamin Krill {
534cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
535cf68787bSBenjamin Krill 
536cf68787bSBenjamin Krill 	if (*cp == '0') {
537cf68787bSBenjamin Krill 		base = 8;
538cf68787bSBenjamin Krill 		cp++;
539cf68787bSBenjamin Krill 		if (toupper(*cp) == 'X') {
540cf68787bSBenjamin Krill 			cp++;
541cf68787bSBenjamin Krill 			base = 16;
542cf68787bSBenjamin Krill 		}
543cf68787bSBenjamin Krill 	}
544cf68787bSBenjamin Krill 
545cf68787bSBenjamin Krill 	while (isxdigit(*cp) &&
546cf68787bSBenjamin Krill 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
547cf68787bSBenjamin Krill 		result = result * base + value;
548cf68787bSBenjamin Krill 		cp++;
549cf68787bSBenjamin Krill 	}
550cf68787bSBenjamin Krill 
551cf68787bSBenjamin Krill 	if (endp)
552cf68787bSBenjamin Krill 		*endp = cp;
553cf68787bSBenjamin Krill 
554cf68787bSBenjamin Krill 	return result;
555cf68787bSBenjamin Krill }
556cf68787bSBenjamin Krill 
557493adffcSBenjamin Herrenschmidt static unsigned long prom_memparse(const char *ptr, const char **retptr)
558cf68787bSBenjamin Krill {
559cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
560cf68787bSBenjamin Krill 	int shift = 0;
561cf68787bSBenjamin Krill 
562cf68787bSBenjamin Krill 	/*
563cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
564cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
565cf68787bSBenjamin Krill 	 * the address we're linked at.
566cf68787bSBenjamin Krill 	 */
567cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
568cf68787bSBenjamin Krill 		shift = 30;
569cf68787bSBenjamin Krill 
570cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
571cf68787bSBenjamin Krill 		shift = 20;
572cf68787bSBenjamin Krill 
573cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
574cf68787bSBenjamin Krill 		shift = 10;
575cf68787bSBenjamin Krill 
576cf68787bSBenjamin Krill 	if (shift) {
577cf68787bSBenjamin Krill 		ret <<= shift;
578cf68787bSBenjamin Krill 		(*retptr)++;
579cf68787bSBenjamin Krill 	}
580cf68787bSBenjamin Krill 
581cf68787bSBenjamin Krill 	return ret;
582cf68787bSBenjamin Krill }
583cf68787bSBenjamin Krill 
5849b6b563cSPaul Mackerras /*
5859b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
5869b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
5879b6b563cSPaul Mackerras  */
5889b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
5899b6b563cSPaul Mackerras {
590cc5d0189SBenjamin Herrenschmidt 	const char *opt;
591cf68787bSBenjamin Krill 
592cc5d0189SBenjamin Herrenschmidt 	char *p;
5939b6b563cSPaul Mackerras 	int l = 0;
5949b6b563cSPaul Mackerras 
5955827d416SAnton Blanchard 	prom_cmd_line[0] = 0;
5965827d416SAnton Blanchard 	p = prom_cmd_line;
5975827d416SAnton Blanchard 	if ((long)prom.chosen > 0)
5985827d416SAnton Blanchard 		l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
5999b6b563cSPaul Mackerras #ifdef CONFIG_CMDLINE
6000e4aa9c2SAmos Waterland 	if (l <= 0 || p[0] == '\0') /* dbl check */
6015827d416SAnton Blanchard 		strlcpy(prom_cmd_line,
6025827d416SAnton Blanchard 			CONFIG_CMDLINE, sizeof(prom_cmd_line));
6039b6b563cSPaul Mackerras #endif /* CONFIG_CMDLINE */
6045827d416SAnton Blanchard 	prom_printf("command line: %s\n", prom_cmd_line);
6059b6b563cSPaul Mackerras 
6069b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
6075827d416SAnton Blanchard 	opt = strstr(prom_cmd_line, "iommu=");
6089b6b563cSPaul Mackerras 	if (opt) {
6099b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
6109b6b563cSPaul Mackerras 		opt += 6;
6119b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
6129b6b563cSPaul Mackerras 			opt++;
6135827d416SAnton Blanchard 		if (!strncmp(opt, "off", 3))
6145827d416SAnton Blanchard 			prom_iommu_off = 1;
6155827d416SAnton Blanchard 		else if (!strncmp(opt, "force", 5))
6165827d416SAnton Blanchard 			prom_iommu_force_on = 1;
6179b6b563cSPaul Mackerras 	}
6189b6b563cSPaul Mackerras #endif
6195827d416SAnton Blanchard 	opt = strstr(prom_cmd_line, "mem=");
620cf68787bSBenjamin Krill 	if (opt) {
621cf68787bSBenjamin Krill 		opt += 4;
6225827d416SAnton Blanchard 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
623cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
624cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
6255827d416SAnton Blanchard 		prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
626cf68787bSBenjamin Krill #endif
627cf68787bSBenjamin Krill 	}
6289b6b563cSPaul Mackerras }
6299b6b563cSPaul Mackerras 
63027f44888SBenjamin Herrenschmidt #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
6319b6b563cSPaul Mackerras /*
632530b5e14SNathan Fontenot  * The architecture vector has an array of PVR mask/value pairs,
633530b5e14SNathan Fontenot  * followed by # option vectors - 1, followed by the option vectors.
634530b5e14SNathan Fontenot  *
635530b5e14SNathan Fontenot  * See prom.h for the definition of the bits specified in the
636530b5e14SNathan Fontenot  * architecture vector.
637f709bfacSPaul Mackerras  *
638f709bfacSPaul Mackerras  * Because the description vector contains a mix of byte and word
639f709bfacSPaul Mackerras  * values, we declare it as an unsigned char array, and use this
640f709bfacSPaul Mackerras  * macro to put word values in.
641f709bfacSPaul Mackerras  */
642f709bfacSPaul Mackerras #define W(x)	((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
643f709bfacSPaul Mackerras 		((x) >> 8) & 0xff, (x) & 0xff
644f709bfacSPaul Mackerras 
645530b5e14SNathan Fontenot unsigned char ibm_architecture_vec[] = {
646f709bfacSPaul Mackerras 	W(0xfffe0000), W(0x003a0000),	/* POWER5/POWER5+ */
64703054d51SAnton Blanchard 	W(0xffff0000), W(0x003e0000),	/* POWER6 */
648e952e6c4SMichael Neuling 	W(0xffff0000), W(0x003f0000),	/* POWER7 */
64933959f88SMichael Neuling 	W(0xffff0000), W(0x004b0000),	/* POWER8E */
65033959f88SMichael Neuling 	W(0xffff0000), W(0x004d0000),	/* POWER8 */
651df77c799SMichael Neuling 	W(0xffffffff), W(0x0f000004),	/* all 2.07-compliant */
6520cb99013SJoel Schopp 	W(0xffffffff), W(0x0f000003),	/* all 2.06-compliant */
6530efbc18aSPaul Mackerras 	W(0xffffffff), W(0x0f000002),	/* all 2.05-compliant */
654f709bfacSPaul Mackerras 	W(0xfffffffe), W(0x0f000001),	/* all 2.04-compliant and earlier */
65528bb9ee1Sjschopp@austin.ibm.com 	6 - 1,				/* 6 option vectors */
656f709bfacSPaul Mackerras 
657f709bfacSPaul Mackerras 	/* option vector 1: processor architectures supported */
65811e9ed43SWill Schmidt 	3 - 2,				/* length */
659f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
660f709bfacSPaul Mackerras 	OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
661df77c799SMichael Neuling 	OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
662f709bfacSPaul Mackerras 
663f709bfacSPaul Mackerras 	/* option vector 2: Open Firmware options supported */
66411e9ed43SWill Schmidt 	34 - 2,				/* length */
665f709bfacSPaul Mackerras 	OV2_REAL_MODE,
666f709bfacSPaul Mackerras 	0, 0,
667f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_base */
668f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_size */
669f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_base */
670f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_size */
671f709bfacSPaul Mackerras 	W(0xffffffff),			/* load_base */
67233392640SAnton Blanchard 	W(256),				/* 256MB min RMA */
673f709bfacSPaul Mackerras 	W(0xffffffff),			/* full client load */
674f709bfacSPaul Mackerras 	0,				/* min RMA percentage of total RAM */
675f709bfacSPaul Mackerras 	48,				/* max log_2(hash table size) */
676f709bfacSPaul Mackerras 
677f709bfacSPaul Mackerras 	/* option vector 3: processor options supported */
67811e9ed43SWill Schmidt 	3 - 2,				/* length */
679f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
680974a76f5SPaul Mackerras 	OV3_FP | OV3_VMX | OV3_DFP,
681f709bfacSPaul Mackerras 
682f709bfacSPaul Mackerras 	/* option vector 4: IBM PAPR implementation */
683404e32e4SRobert Jennings 	3 - 2,				/* length */
684f709bfacSPaul Mackerras 	0,				/* don't halt */
685404e32e4SRobert Jennings 	OV4_MIN_ENT_CAP,		/* minimum VP entitled capacity */
686f709bfacSPaul Mackerras 
687f709bfacSPaul Mackerras 	/* option vector 5: PAPR/OF options */
688df77c799SMichael Neuling 	19 - 2,				/* length */
689f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
690f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
691f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
692f0ff7eb4SNathan Fontenot #ifdef CONFIG_PCI_MSI
693f0ff7eb4SNathan Fontenot 	/* PCIe/MSI support.  Without MSI full PCIe is not supported */
694f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_MSI),
695f0ff7eb4SNathan Fontenot #else
6968391e42aSNathan Fontenot 	0,
697f0ff7eb4SNathan Fontenot #endif
698f0ff7eb4SNathan Fontenot 	0,
699f0ff7eb4SNathan Fontenot #ifdef CONFIG_PPC_SMLPAR
700f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
701f0ff7eb4SNathan Fontenot #else
702f0ff7eb4SNathan Fontenot 	0,
703f0ff7eb4SNathan Fontenot #endif
7041b1218d3SNathan Fontenot 	OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
70528bb9ee1Sjschopp@austin.ibm.com 	0,
70628bb9ee1Sjschopp@austin.ibm.com 	0,
70728bb9ee1Sjschopp@austin.ibm.com 	0,
708efec959fSBenjamin Herrenschmidt 	/* WARNING: The offset of the "number of cores" field below
709efec959fSBenjamin Herrenschmidt 	 * must match by the macro below. Update the definition if
710efec959fSBenjamin Herrenschmidt 	 * the structure layout changes.
711efec959fSBenjamin Herrenschmidt 	 */
71233959f88SMichael Neuling #define IBM_ARCH_VEC_NRCORES_OFFSET	125
71328bb9ee1Sjschopp@austin.ibm.com 	W(NR_CPUS),			/* number of cores supported */
714828d2b59SKent Yoder 	0,
715828d2b59SKent Yoder 	0,
716828d2b59SKent Yoder 	0,
717828d2b59SKent Yoder 	0,
718f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) |
719f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_PFO_HW_842),
720f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_SUB_PROCESSORS),
72128bb9ee1Sjschopp@austin.ibm.com 	/* option vector 6: IBM PAPR hints */
72228bb9ee1Sjschopp@austin.ibm.com 	4 - 2,				/* length */
72328bb9ee1Sjschopp@austin.ibm.com 	0,
72428bb9ee1Sjschopp@austin.ibm.com 	0,
72528bb9ee1Sjschopp@austin.ibm.com 	OV6_LINUX,
72628bb9ee1Sjschopp@austin.ibm.com 
727f709bfacSPaul Mackerras };
728f709bfacSPaul Mackerras 
729493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
730493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
7319b6b563cSPaul Mackerras static struct fake_elf {
7329b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
7339b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
7349b6b563cSPaul Mackerras 	struct chrpnote {
7359b6b563cSPaul Mackerras 		u32	namesz;
7369b6b563cSPaul Mackerras 		u32	descsz;
7379b6b563cSPaul Mackerras 		u32	type;
7389b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
7399b6b563cSPaul Mackerras 		struct chrpdesc {
7409b6b563cSPaul Mackerras 			u32	real_mode;
7419b6b563cSPaul Mackerras 			u32	real_base;
7429b6b563cSPaul Mackerras 			u32	real_size;
7439b6b563cSPaul Mackerras 			u32	virt_base;
7449b6b563cSPaul Mackerras 			u32	virt_size;
7459b6b563cSPaul Mackerras 			u32	load_base;
7469b6b563cSPaul Mackerras 		} chrpdesc;
7479b6b563cSPaul Mackerras 	} chrpnote;
7489b6b563cSPaul Mackerras 	struct rpanote {
7499b6b563cSPaul Mackerras 		u32	namesz;
7509b6b563cSPaul Mackerras 		u32	descsz;
7519b6b563cSPaul Mackerras 		u32	type;
7529b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
7539b6b563cSPaul Mackerras 		struct rpadesc {
7549b6b563cSPaul Mackerras 			u32	lpar_affinity;
7559b6b563cSPaul Mackerras 			u32	min_rmo_size;
7569b6b563cSPaul Mackerras 			u32	min_rmo_percent;
7579b6b563cSPaul Mackerras 			u32	max_pft_size;
7589b6b563cSPaul Mackerras 			u32	splpar;
7599b6b563cSPaul Mackerras 			u32	min_load;
7609b6b563cSPaul Mackerras 			u32	new_mem_def;
7619b6b563cSPaul Mackerras 			u32	ignore_me;
7629b6b563cSPaul Mackerras 		} rpadesc;
7639b6b563cSPaul Mackerras 	} rpanote;
7645663a123SPaul Mackerras } fake_elf = {
7659b6b563cSPaul Mackerras 	.elfhdr = {
7669b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
7679b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
7689b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
7699b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
7709b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
7719b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
7729b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
7739b6b563cSPaul Mackerras 		.e_phnum = 2
7749b6b563cSPaul Mackerras 	},
7759b6b563cSPaul Mackerras 	.phdr = {
7769b6b563cSPaul Mackerras 		[0] = {
7779b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
7789b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
7799b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
7809b6b563cSPaul Mackerras 		}, [1] = {
7819b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
7829b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
7839b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
7849b6b563cSPaul Mackerras 		}
7859b6b563cSPaul Mackerras 	},
7869b6b563cSPaul Mackerras 	.chrpnote = {
7879b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
7889b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
7899b6b563cSPaul Mackerras 		.type = 0x1275,
7909b6b563cSPaul Mackerras 		.name = "PowerPC",
7919b6b563cSPaul Mackerras 		.chrpdesc = {
7929b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
7939b6b563cSPaul Mackerras 			.real_base = ~0U,
7949b6b563cSPaul Mackerras 			.real_size = ~0U,
7959b6b563cSPaul Mackerras 			.virt_base = ~0U,
7969b6b563cSPaul Mackerras 			.virt_size = ~0U,
7979b6b563cSPaul Mackerras 			.load_base = ~0U
7989b6b563cSPaul Mackerras 		},
7999b6b563cSPaul Mackerras 	},
8009b6b563cSPaul Mackerras 	.rpanote = {
8019b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
8029b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
8039b6b563cSPaul Mackerras 		.type = 0x12759999,
8049b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
8059b6b563cSPaul Mackerras 		.rpadesc = {
8065663a123SPaul Mackerras 			.lpar_affinity = 0,
8075663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
8089b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
8095663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
8109b6b563cSPaul Mackerras 			.splpar = 1,
8119b6b563cSPaul Mackerras 			.min_load = ~0U,
8125663a123SPaul Mackerras 			.new_mem_def = 0
8139b6b563cSPaul Mackerras 		}
8149b6b563cSPaul Mackerras 	}
8159b6b563cSPaul Mackerras };
816493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
8179b6b563cSPaul Mackerras 
818efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
819efec959fSBenjamin Herrenschmidt {
820efec959fSBenjamin Herrenschmidt 	phandle node;
821efec959fSBenjamin Herrenschmidt 	char type[64];
822efec959fSBenjamin Herrenschmidt 	unsigned int plen;
823efec959fSBenjamin Herrenschmidt 
824efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
825efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
826efec959fSBenjamin Herrenschmidt 		type[0] = 0;
827efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
828efec959fSBenjamin Herrenschmidt 
8295827d416SAnton Blanchard 		if (strcmp(type, "cpu"))
830efec959fSBenjamin Herrenschmidt 			continue;
831efec959fSBenjamin Herrenschmidt 		/*
832efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
833efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
834efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
835efec959fSBenjamin Herrenschmidt 		 */
836efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
837efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
838efec959fSBenjamin Herrenschmidt 			break;
839efec959fSBenjamin Herrenschmidt 		plen >>= 2;
8402c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
841efec959fSBenjamin Herrenschmidt 
842efec959fSBenjamin Herrenschmidt 		/* Sanity check */
843efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
8442c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
845efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
846efec959fSBenjamin Herrenschmidt 			return 1;
847efec959fSBenjamin Herrenschmidt 		}
848efec959fSBenjamin Herrenschmidt 		return plen;
849efec959fSBenjamin Herrenschmidt 	}
850efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
851efec959fSBenjamin Herrenschmidt 
852efec959fSBenjamin Herrenschmidt 	return 1;
853efec959fSBenjamin Herrenschmidt 
854efec959fSBenjamin Herrenschmidt }
855efec959fSBenjamin Herrenschmidt 
856efec959fSBenjamin Herrenschmidt 
8579b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
8589b6b563cSPaul Mackerras {
859493adffcSBenjamin Herrenschmidt 	ihandle root;
860f709bfacSPaul Mackerras 	prom_arg_t ret;
861*dbd0c5d5SLaurent Dufour 	u32 cores;
862*dbd0c5d5SLaurent Dufour 	unsigned char *ptcores;
8639b6b563cSPaul Mackerras 
864f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
865f709bfacSPaul Mackerras 	if (root != 0) {
866efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
867efec959fSBenjamin Herrenschmidt 		 *
868efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
869efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
870efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
871efec959fSBenjamin Herrenschmidt 		 */
872*dbd0c5d5SLaurent Dufour 
873*dbd0c5d5SLaurent Dufour 		/* The core value may start at an odd address. If such a word
874*dbd0c5d5SLaurent Dufour 		 * access is made at a cache line boundary, this leads to an
875*dbd0c5d5SLaurent Dufour 		 * exception which may not be handled at this time.
876*dbd0c5d5SLaurent Dufour 		 * Forcing a per byte access to avoid exception.
877*dbd0c5d5SLaurent Dufour 		 */
878*dbd0c5d5SLaurent Dufour 		ptcores = &ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET];
879*dbd0c5d5SLaurent Dufour 		cores = 0;
880*dbd0c5d5SLaurent Dufour 		cores |= ptcores[0] << 24;
881*dbd0c5d5SLaurent Dufour 		cores |= ptcores[1] << 16;
882*dbd0c5d5SLaurent Dufour 		cores |= ptcores[2] << 8;
883*dbd0c5d5SLaurent Dufour 		cores |= ptcores[3];
884*dbd0c5d5SLaurent Dufour 		if (cores != NR_CPUS) {
885efec959fSBenjamin Herrenschmidt 			prom_printf("WARNING ! "
8862c48a7d6SMichael Neuling 				    "ibm_architecture_vec structure inconsistent: %lu!\n",
887*dbd0c5d5SLaurent Dufour 				    cores);
888efec959fSBenjamin Herrenschmidt 		} else {
889*dbd0c5d5SLaurent Dufour 			cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
8902c48a7d6SMichael Neuling 			prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
891*dbd0c5d5SLaurent Dufour 				    cores, NR_CPUS);
892*dbd0c5d5SLaurent Dufour 			ptcores[0] = (cores >> 24) & 0xff;
893*dbd0c5d5SLaurent Dufour 			ptcores[1] = (cores >> 16) & 0xff;
894*dbd0c5d5SLaurent Dufour 			ptcores[2] = (cores >> 8) & 0xff;
895*dbd0c5d5SLaurent Dufour 			ptcores[3] = cores & 0xff;
896efec959fSBenjamin Herrenschmidt 		}
897efec959fSBenjamin Herrenschmidt 
898f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
899049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
900f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
901f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
90233b74977SBenjamin Herrenschmidt 				  root,
903f709bfacSPaul Mackerras 				  ADDR(ibm_architecture_vec)) == 0) {
904f709bfacSPaul Mackerras 			/* the call exists... */
905f709bfacSPaul Mackerras 			if (ret)
9064da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
907f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
908f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
9094da727aeSAnton Blanchard 			prom_printf(" done\n");
910f709bfacSPaul Mackerras 			return;
911f709bfacSPaul Mackerras 		}
912f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
913049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
914f709bfacSPaul Mackerras 	}
915f709bfacSPaul Mackerras 
916493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
917493adffcSBenjamin Herrenschmidt 	{
918493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
919493adffcSBenjamin Herrenschmidt 
920f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
921493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
922493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
9239b6b563cSPaul Mackerras 		if (elfloader == 0) {
9249b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
9259b6b563cSPaul Mackerras 			return;
9269b6b563cSPaul Mackerras 		}
9279b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
9289b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
9299b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
9309b6b563cSPaul Mackerras 	}
931493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
932493adffcSBenjamin Herrenschmidt }
933493adffcSBenjamin Herrenschmidt #endif /* #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV) */
9349b6b563cSPaul Mackerras 
9359b6b563cSPaul Mackerras /*
9369b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
9379b6b563cSPaul Mackerras  *
9389b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
9399b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
9409b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
9419b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
9429b6b563cSPaul Mackerras  *  never happen.
9439b6b563cSPaul Mackerras  *
9449b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
9459b6b563cSPaul Mackerras  *  TCEs overlap
9469b6b563cSPaul Mackerras  *
9479b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
9489b6b563cSPaul Mackerras  *
9499b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
9509b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
9519b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
9529b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
9539b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
9549b6b563cSPaul Mackerras  *
9559b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
9569b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
9579b6b563cSPaul Mackerras  *  reserved.
9589b6b563cSPaul Mackerras  */
9599b6b563cSPaul Mackerras 
9609b6b563cSPaul Mackerras 
9619b6b563cSPaul Mackerras /*
9629b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
9639b6b563cSPaul Mackerras  *
9649b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
9659b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
9669b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
9679b6b563cSPaul Mackerras  */
9689b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
9699b6b563cSPaul Mackerras {
9705827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
9719b6b563cSPaul Mackerras 	unsigned long addr = 0;
9729b6b563cSPaul Mackerras 
973c4988820SPaul Mackerras 	if (align)
974c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
9759b6b563cSPaul Mackerras 	prom_debug("alloc_up(%x, %x)\n", size, align);
9765827d416SAnton Blanchard 	if (ram_top == 0)
9779b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
9789b6b563cSPaul Mackerras 
9799b6b563cSPaul Mackerras 	if (align)
9805827d416SAnton Blanchard 		base = _ALIGN_UP(alloc_bottom, align);
9819b6b563cSPaul Mackerras 	else
9825827d416SAnton Blanchard 		base = alloc_bottom;
9839b6b563cSPaul Mackerras 
9845827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
9859b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
9869b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
9879b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
988c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
9899b6b563cSPaul Mackerras 			break;
9909b6b563cSPaul Mackerras 		addr = 0;
9919b6b563cSPaul Mackerras 		if (align == 0)
9929b6b563cSPaul Mackerras 			break;
9939b6b563cSPaul Mackerras 	}
9949b6b563cSPaul Mackerras 	if (addr == 0)
9959b6b563cSPaul Mackerras 		return 0;
9965827d416SAnton Blanchard 	alloc_bottom = addr + size;
9979b6b563cSPaul Mackerras 
9989b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
9995827d416SAnton Blanchard 	prom_debug("  alloc_bottom : %x\n", alloc_bottom);
10005827d416SAnton Blanchard 	prom_debug("  alloc_top    : %x\n", alloc_top);
10015827d416SAnton Blanchard 	prom_debug("  alloc_top_hi : %x\n", alloc_top_high);
10025827d416SAnton Blanchard 	prom_debug("  rmo_top      : %x\n", rmo_top);
10035827d416SAnton Blanchard 	prom_debug("  ram_top      : %x\n", ram_top);
10049b6b563cSPaul Mackerras 
10059b6b563cSPaul Mackerras 	return addr;
10069b6b563cSPaul Mackerras }
10079b6b563cSPaul Mackerras 
10089b6b563cSPaul Mackerras /*
10099b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
10109b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
10119b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
10129b6b563cSPaul Mackerras  */
10139b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
10149b6b563cSPaul Mackerras 				       int highmem)
10159b6b563cSPaul Mackerras {
10169b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
10179b6b563cSPaul Mackerras 
10189b6b563cSPaul Mackerras 	prom_debug("alloc_down(%x, %x, %s)\n", size, align,
10195827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
10205827d416SAnton Blanchard 	if (ram_top == 0)
10219b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
10229b6b563cSPaul Mackerras 
10239b6b563cSPaul Mackerras 	if (highmem) {
10249b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
10255827d416SAnton Blanchard 		addr = _ALIGN_DOWN(alloc_top_high - size, align);
10265827d416SAnton Blanchard 		if (addr <= alloc_bottom)
10279b6b563cSPaul Mackerras 			return 0;
10289b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
10299b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
10309b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
10319b6b563cSPaul Mackerras 		 */
10325827d416SAnton Blanchard 		if (addr < rmo_top) {
10339b6b563cSPaul Mackerras 			/* Good, we are first */
10345827d416SAnton Blanchard 			if (alloc_top == rmo_top)
10355827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
10369b6b563cSPaul Mackerras 			else
10379b6b563cSPaul Mackerras 				return 0;
10389b6b563cSPaul Mackerras 		}
10395827d416SAnton Blanchard 		alloc_top_high = addr;
10409b6b563cSPaul Mackerras 		goto bail;
10419b6b563cSPaul Mackerras 	}
10429b6b563cSPaul Mackerras 
10435827d416SAnton Blanchard 	base = _ALIGN_DOWN(alloc_top - size, align);
10445827d416SAnton Blanchard 	for (; base > alloc_bottom;
10459b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
10469b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
10479b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1048c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
10499b6b563cSPaul Mackerras 			break;
10509b6b563cSPaul Mackerras 		addr = 0;
10519b6b563cSPaul Mackerras 	}
10529b6b563cSPaul Mackerras 	if (addr == 0)
10539b6b563cSPaul Mackerras 		return 0;
10545827d416SAnton Blanchard 	alloc_top = addr;
10559b6b563cSPaul Mackerras 
10569b6b563cSPaul Mackerras  bail:
10579b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
10585827d416SAnton Blanchard 	prom_debug("  alloc_bottom : %x\n", alloc_bottom);
10595827d416SAnton Blanchard 	prom_debug("  alloc_top    : %x\n", alloc_top);
10605827d416SAnton Blanchard 	prom_debug("  alloc_top_hi : %x\n", alloc_top_high);
10615827d416SAnton Blanchard 	prom_debug("  rmo_top      : %x\n", rmo_top);
10625827d416SAnton Blanchard 	prom_debug("  ram_top      : %x\n", ram_top);
10639b6b563cSPaul Mackerras 
10649b6b563cSPaul Mackerras 	return addr;
10659b6b563cSPaul Mackerras }
10669b6b563cSPaul Mackerras 
10679b6b563cSPaul Mackerras /*
10689b6b563cSPaul Mackerras  * Parse a "reg" cell
10699b6b563cSPaul Mackerras  */
10709b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
10719b6b563cSPaul Mackerras {
10729b6b563cSPaul Mackerras 	cell_t *p = *cellp;
10739b6b563cSPaul Mackerras 	unsigned long r = 0;
10749b6b563cSPaul Mackerras 
10759b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
10769b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
10779b6b563cSPaul Mackerras 		p++;
10789b6b563cSPaul Mackerras 		s--;
10799b6b563cSPaul Mackerras 	}
1080493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
10819b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
108235499c01SPaul Mackerras 	if (s > 1) {
10839b6b563cSPaul Mackerras 		r <<= 32;
1084493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
10859b6b563cSPaul Mackerras 	}
10869b6b563cSPaul Mackerras #endif
10879b6b563cSPaul Mackerras 	*cellp = p;
10889b6b563cSPaul Mackerras 	return r;
10899b6b563cSPaul Mackerras }
10909b6b563cSPaul Mackerras 
10919b6b563cSPaul Mackerras /*
10929b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
10939b6b563cSPaul Mackerras  * we don't need anything smarter at this point
10949b6b563cSPaul Mackerras  *
10959b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
10969b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
10979b6b563cSPaul Mackerras  * them down.
10989b6b563cSPaul Mackerras  */
10990108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
11009b6b563cSPaul Mackerras {
1101cbbcf340SKumar Gala 	u64 top = base + size;
11025827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
11039b6b563cSPaul Mackerras 
11049b6b563cSPaul Mackerras 	if (size == 0)
11059b6b563cSPaul Mackerras 		return;
11069b6b563cSPaul Mackerras 
11079b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
11089b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
11099b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
11109b6b563cSPaul Mackerras 	 */
11119b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
11129b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
11139b6b563cSPaul Mackerras 	size = top - base;
11149b6b563cSPaul Mackerras 
11159b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
11169b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1117493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1118493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
11195827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
11209b6b563cSPaul Mackerras }
11219b6b563cSPaul Mackerras 
11229b6b563cSPaul Mackerras /*
1123b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
11249b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
11259b6b563cSPaul Mackerras  */
11269b6b563cSPaul Mackerras static void __init prom_init_mem(void)
11279b6b563cSPaul Mackerras {
11289b6b563cSPaul Mackerras 	phandle node;
11299b6b563cSPaul Mackerras 	char *path, type[64];
11309b6b563cSPaul Mackerras 	unsigned int plen;
11319b6b563cSPaul Mackerras 	cell_t *p, *endp;
1132493adffcSBenjamin Herrenschmidt 	__be32 val;
11339b6b563cSPaul Mackerras 	u32 rac, rsc;
11349b6b563cSPaul Mackerras 
11359b6b563cSPaul Mackerras 	/*
11369b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
11379b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
11389b6b563cSPaul Mackerras 	 * 2) top of memory
11399b6b563cSPaul Mackerras 	 */
1140493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1141493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1142493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1143493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1144493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1145493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1146493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1147493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
11489b6b563cSPaul Mackerras 
11499b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
11505827d416SAnton Blanchard 	path = prom_scratch;
11519b6b563cSPaul Mackerras 
11529b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
11539b6b563cSPaul Mackerras 		type[0] = 0;
11549b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
11559b6b563cSPaul Mackerras 
1156c4988820SPaul Mackerras 		if (type[0] == 0) {
1157c4988820SPaul Mackerras 			/*
1158c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1159c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1160c4988820SPaul Mackerras 			 */
1161c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1162c4988820SPaul Mackerras 		}
11635827d416SAnton Blanchard 		if (strcmp(type, "memory"))
11649b6b563cSPaul Mackerras 			continue;
11659b6b563cSPaul Mackerras 
11665827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
11679b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
11689b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
11699b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
11709b6b563cSPaul Mackerras 		}
11715827d416SAnton Blanchard 		p = regbuf;
11729b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
11739b6b563cSPaul Mackerras 
11749b6b563cSPaul Mackerras #ifdef DEBUG_PROM
11759b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
11769b6b563cSPaul Mackerras 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
11779b6b563cSPaul Mackerras 		prom_debug("  node %s :\n", path);
11789b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
11799b6b563cSPaul Mackerras 
11809b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
11819b6b563cSPaul Mackerras 			unsigned long base, size;
11829b6b563cSPaul Mackerras 
11839b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
11849b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
11859b6b563cSPaul Mackerras 
11869b6b563cSPaul Mackerras 			if (size == 0)
11879b6b563cSPaul Mackerras 				continue;
11889b6b563cSPaul Mackerras 			prom_debug("    %x %x\n", base, size);
11895827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
11905827d416SAnton Blanchard 				rmo_top = size;
11915827d416SAnton Blanchard 			if ((base + size) > ram_top)
11925827d416SAnton Blanchard 				ram_top = base + size;
11939b6b563cSPaul Mackerras 		}
11949b6b563cSPaul Mackerras 	}
11959b6b563cSPaul Mackerras 
11965827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
11979b6b563cSPaul Mackerras 
11989b6b563cSPaul Mackerras 	/*
1199cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1200cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1201cf68787bSBenjamin Krill 	 * TCE's up there.
1202cf68787bSBenjamin Krill 	 */
1203cf68787bSBenjamin Krill 
12045827d416SAnton Blanchard 	alloc_top_high = ram_top;
1205cf68787bSBenjamin Krill 
12065827d416SAnton Blanchard 	if (prom_memory_limit) {
12075827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1208cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
12095827d416SAnton Blanchard 				prom_memory_limit);
12105827d416SAnton Blanchard 			prom_memory_limit = 0;
12115827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1212cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x >= ram_top.\n",
12135827d416SAnton Blanchard 				prom_memory_limit);
12145827d416SAnton Blanchard 			prom_memory_limit = 0;
1215cf68787bSBenjamin Krill 		} else {
12165827d416SAnton Blanchard 			ram_top = prom_memory_limit;
12175827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1218cf68787bSBenjamin Krill 		}
1219cf68787bSBenjamin Krill 	}
1220cf68787bSBenjamin Krill 
1221cf68787bSBenjamin Krill 	/*
12229b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
12239b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
12249b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
12259b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
12269b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
12279b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
12289b6b563cSPaul Mackerras 	 */
12295827d416SAnton Blanchard 	if (!rmo_top)
12305827d416SAnton Blanchard 		rmo_top = ram_top;
12315827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
12325827d416SAnton Blanchard 	alloc_top = rmo_top;
12335827d416SAnton Blanchard 	alloc_top_high = ram_top;
12349b6b563cSPaul Mackerras 
123564968f60SPaul Mackerras 	/*
123664968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
123764968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
123864968f60SPaul Mackerras 	 */
12395827d416SAnton Blanchard 	if (prom_initrd_start &&
12405827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
12415827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
12425827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
124364968f60SPaul Mackerras 
12449b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
12455827d416SAnton Blanchard 	prom_printf("  memory_limit : %x (16 MB aligned)\n", prom_memory_limit);
12465827d416SAnton Blanchard 	prom_printf("  alloc_bottom : %x\n", alloc_bottom);
12475827d416SAnton Blanchard 	prom_printf("  alloc_top    : %x\n", alloc_top);
12485827d416SAnton Blanchard 	prom_printf("  alloc_top_hi : %x\n", alloc_top_high);
12495827d416SAnton Blanchard 	prom_printf("  rmo_top      : %x\n", rmo_top);
12505827d416SAnton Blanchard 	prom_printf("  ram_top      : %x\n", ram_top);
12519b6b563cSPaul Mackerras }
12529b6b563cSPaul Mackerras 
125327f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
125427f44888SBenjamin Herrenschmidt {
1255493adffcSBenjamin Herrenschmidt 	__be32 val;
1256493adffcSBenjamin Herrenschmidt 	ihandle stdin;
125727f44888SBenjamin Herrenschmidt 
1258493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1259493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1260493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1261493adffcSBenjamin Herrenschmidt 	}
126227f44888SBenjamin Herrenschmidt }
126327f44888SBenjamin Herrenschmidt 
126427f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
126527f44888SBenjamin Herrenschmidt 
12666e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
12676e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_base;
12686e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_entry;
12696e35d5daSBenjamin Herrenschmidt #endif
12706e35d5daSBenjamin Herrenschmidt 
1271493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
127227f44888SBenjamin Herrenschmidt /* XXX Don't change this structure without updating opal-takeover.S */
127327f44888SBenjamin Herrenschmidt static struct opal_secondary_data {
127427f44888SBenjamin Herrenschmidt 	s64				ack;	/*  0 */
127527f44888SBenjamin Herrenschmidt 	u64				go;	/*  8 */
127627f44888SBenjamin Herrenschmidt 	struct opal_takeover_args	args;	/* 16 */
127727f44888SBenjamin Herrenschmidt } opal_secondary_data;
127827f44888SBenjamin Herrenschmidt 
1279493adffcSBenjamin Herrenschmidt static u64 __initdata prom_opal_align;
1280493adffcSBenjamin Herrenschmidt static u64 __initdata prom_opal_size;
1281493adffcSBenjamin Herrenschmidt static int __initdata prom_rtas_start_cpu;
1282493adffcSBenjamin Herrenschmidt static u64 __initdata prom_rtas_data;
1283493adffcSBenjamin Herrenschmidt static u64 __initdata prom_rtas_entry;
1284493adffcSBenjamin Herrenschmidt 
128527f44888SBenjamin Herrenschmidt extern char opal_secondary_entry;
128627f44888SBenjamin Herrenschmidt 
12872cb387aeSLi Zhong static void __init prom_query_opal(void)
128827f44888SBenjamin Herrenschmidt {
128927f44888SBenjamin Herrenschmidt 	long rc;
129027f44888SBenjamin Herrenschmidt 
12917680057cSBenjamin Herrenschmidt 	/* We must not query for OPAL presence on a machine that
12927680057cSBenjamin Herrenschmidt 	 * supports TNK takeover (970 blades), as this uses the same
12937680057cSBenjamin Herrenschmidt 	 * h-call with different arguments and will crash
12947680057cSBenjamin Herrenschmidt 	 */
12957680057cSBenjamin Herrenschmidt 	if (PHANDLE_VALID(call_prom("finddevice", 1, 1,
12967680057cSBenjamin Herrenschmidt 				    ADDR("/tnk-memory-map")))) {
12977680057cSBenjamin Herrenschmidt 		prom_printf("TNK takeover detected, skipping OPAL check\n");
12987680057cSBenjamin Herrenschmidt 		return;
12997680057cSBenjamin Herrenschmidt 	}
13007680057cSBenjamin Herrenschmidt 
130127f44888SBenjamin Herrenschmidt 	prom_printf("Querying for OPAL presence... ");
1302493adffcSBenjamin Herrenschmidt 
13035827d416SAnton Blanchard 	rc = opal_query_takeover(&prom_opal_size,
13045827d416SAnton Blanchard 				 &prom_opal_align);
130527f44888SBenjamin Herrenschmidt 	prom_debug("(rc = %ld) ", rc);
130627f44888SBenjamin Herrenschmidt 	if (rc != 0) {
130727f44888SBenjamin Herrenschmidt 		prom_printf("not there.\n");
130827f44888SBenjamin Herrenschmidt 		return;
130927f44888SBenjamin Herrenschmidt 	}
13105827d416SAnton Blanchard 	of_platform = PLATFORM_OPAL;
131127f44888SBenjamin Herrenschmidt 	prom_printf(" there !\n");
13125827d416SAnton Blanchard 	prom_debug("  opal_size  = 0x%lx\n", prom_opal_size);
13135827d416SAnton Blanchard 	prom_debug("  opal_align = 0x%lx\n", prom_opal_align);
13145827d416SAnton Blanchard 	if (prom_opal_align < 0x10000)
13155827d416SAnton Blanchard 		prom_opal_align = 0x10000;
131627f44888SBenjamin Herrenschmidt }
131727f44888SBenjamin Herrenschmidt 
1318620e5050SVladimir Murzin static int __init prom_rtas_call(int token, int nargs, int nret,
1319620e5050SVladimir Murzin 				 int *outputs, ...)
132027f44888SBenjamin Herrenschmidt {
132127f44888SBenjamin Herrenschmidt 	struct rtas_args rtas_args;
132227f44888SBenjamin Herrenschmidt 	va_list list;
132327f44888SBenjamin Herrenschmidt 	int i;
132427f44888SBenjamin Herrenschmidt 
132527f44888SBenjamin Herrenschmidt 	rtas_args.token = token;
132627f44888SBenjamin Herrenschmidt 	rtas_args.nargs = nargs;
132727f44888SBenjamin Herrenschmidt 	rtas_args.nret  = nret;
132827f44888SBenjamin Herrenschmidt 	rtas_args.rets  = (rtas_arg_t *)&(rtas_args.args[nargs]);
132927f44888SBenjamin Herrenschmidt 	va_start(list, outputs);
133027f44888SBenjamin Herrenschmidt 	for (i = 0; i < nargs; ++i)
133127f44888SBenjamin Herrenschmidt 		rtas_args.args[i] = va_arg(list, rtas_arg_t);
133227f44888SBenjamin Herrenschmidt 	va_end(list);
133327f44888SBenjamin Herrenschmidt 
133427f44888SBenjamin Herrenschmidt 	for (i = 0; i < nret; ++i)
133527f44888SBenjamin Herrenschmidt 		rtas_args.rets[i] = 0;
133627f44888SBenjamin Herrenschmidt 
13375827d416SAnton Blanchard 	opal_enter_rtas(&rtas_args, prom_rtas_data,
13385827d416SAnton Blanchard 			prom_rtas_entry);
133927f44888SBenjamin Herrenschmidt 
134027f44888SBenjamin Herrenschmidt 	if (nret > 1 && outputs != NULL)
134127f44888SBenjamin Herrenschmidt 		for (i = 0; i < nret-1; ++i)
134227f44888SBenjamin Herrenschmidt 			outputs[i] = rtas_args.rets[i+1];
134327f44888SBenjamin Herrenschmidt 	return (nret > 0)? rtas_args.rets[0]: 0;
134427f44888SBenjamin Herrenschmidt }
134527f44888SBenjamin Herrenschmidt 
134627f44888SBenjamin Herrenschmidt static void __init prom_opal_hold_cpus(void)
134727f44888SBenjamin Herrenschmidt {
134827f44888SBenjamin Herrenschmidt 	int i, cnt, cpu, rc;
134927f44888SBenjamin Herrenschmidt 	long j;
135027f44888SBenjamin Herrenschmidt 	phandle node;
135127f44888SBenjamin Herrenschmidt 	char type[64];
135227f44888SBenjamin Herrenschmidt 	u32 servers[8];
13535827d416SAnton Blanchard 	void *entry = (unsigned long *)&opal_secondary_entry;
13545827d416SAnton Blanchard 	struct opal_secondary_data *data = &opal_secondary_data;
135527f44888SBenjamin Herrenschmidt 
135627f44888SBenjamin Herrenschmidt 	prom_debug("prom_opal_hold_cpus: start...\n");
135727f44888SBenjamin Herrenschmidt 	prom_debug("    - entry       = 0x%x\n", entry);
135827f44888SBenjamin Herrenschmidt 	prom_debug("    - data        = 0x%x\n", data);
135927f44888SBenjamin Herrenschmidt 
136027f44888SBenjamin Herrenschmidt 	data->ack = -1;
136127f44888SBenjamin Herrenschmidt 	data->go = 0;
136227f44888SBenjamin Herrenschmidt 
136327f44888SBenjamin Herrenschmidt 	/* look for cpus */
136427f44888SBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
136527f44888SBenjamin Herrenschmidt 		type[0] = 0;
136627f44888SBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
13675827d416SAnton Blanchard 		if (strcmp(type, "cpu") != 0)
136827f44888SBenjamin Herrenschmidt 			continue;
136927f44888SBenjamin Herrenschmidt 
137027f44888SBenjamin Herrenschmidt 		/* Skip non-configured cpus. */
137127f44888SBenjamin Herrenschmidt 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
13725827d416SAnton Blanchard 			if (strcmp(type, "okay") != 0)
137327f44888SBenjamin Herrenschmidt 				continue;
137427f44888SBenjamin Herrenschmidt 
137527f44888SBenjamin Herrenschmidt 		cnt = prom_getprop(node, "ibm,ppc-interrupt-server#s", servers,
137627f44888SBenjamin Herrenschmidt 			     sizeof(servers));
137727f44888SBenjamin Herrenschmidt 		if (cnt == PROM_ERROR)
137827f44888SBenjamin Herrenschmidt 			break;
137927f44888SBenjamin Herrenschmidt 		cnt >>= 2;
138027f44888SBenjamin Herrenschmidt 		for (i = 0; i < cnt; i++) {
138127f44888SBenjamin Herrenschmidt 			cpu = servers[i];
138227f44888SBenjamin Herrenschmidt 			prom_debug("CPU %d ... ", cpu);
13835827d416SAnton Blanchard 			if (cpu == prom.cpu) {
138427f44888SBenjamin Herrenschmidt 				prom_debug("booted !\n");
138527f44888SBenjamin Herrenschmidt 				continue;
138627f44888SBenjamin Herrenschmidt 			}
138727f44888SBenjamin Herrenschmidt 			prom_debug("starting ... ");
138827f44888SBenjamin Herrenschmidt 
138927f44888SBenjamin Herrenschmidt 			/* Init the acknowledge var which will be reset by
139027f44888SBenjamin Herrenschmidt 			 * the secondary cpu when it awakens from its OF
139127f44888SBenjamin Herrenschmidt 			 * spinloop.
139227f44888SBenjamin Herrenschmidt 			 */
139327f44888SBenjamin Herrenschmidt 			data->ack = -1;
13945827d416SAnton Blanchard 			rc = prom_rtas_call(prom_rtas_start_cpu, 3, 1,
139527f44888SBenjamin Herrenschmidt 					    NULL, cpu, entry, data);
139627f44888SBenjamin Herrenschmidt 			prom_debug("rtas rc=%d ...", rc);
139727f44888SBenjamin Herrenschmidt 
139827f44888SBenjamin Herrenschmidt 			for (j = 0; j < 100000000 && data->ack == -1; j++) {
139927f44888SBenjamin Herrenschmidt 				HMT_low();
140027f44888SBenjamin Herrenschmidt 				mb();
140127f44888SBenjamin Herrenschmidt 			}
140227f44888SBenjamin Herrenschmidt 			HMT_medium();
140327f44888SBenjamin Herrenschmidt 			if (data->ack != -1)
140427f44888SBenjamin Herrenschmidt 				prom_debug("done, PIR=0x%x\n", data->ack);
140527f44888SBenjamin Herrenschmidt 			else
140627f44888SBenjamin Herrenschmidt 				prom_debug("timeout !\n");
140727f44888SBenjamin Herrenschmidt 		}
140827f44888SBenjamin Herrenschmidt 	}
140927f44888SBenjamin Herrenschmidt 	prom_debug("prom_opal_hold_cpus: end...\n");
141027f44888SBenjamin Herrenschmidt }
141127f44888SBenjamin Herrenschmidt 
14122cb387aeSLi Zhong static void __init prom_opal_takeover(void)
141327f44888SBenjamin Herrenschmidt {
14145827d416SAnton Blanchard 	struct opal_secondary_data *data = &opal_secondary_data;
141527f44888SBenjamin Herrenschmidt 	struct opal_takeover_args *args = &data->args;
14165827d416SAnton Blanchard 	u64 align = prom_opal_align;
141727f44888SBenjamin Herrenschmidt 	u64 top_addr, opal_addr;
141827f44888SBenjamin Herrenschmidt 
14195827d416SAnton Blanchard 	args->k_image	= (u64)_stext;
142027f44888SBenjamin Herrenschmidt 	args->k_size	= _end - _stext;
142127f44888SBenjamin Herrenschmidt 	args->k_entry	= 0;
142227f44888SBenjamin Herrenschmidt 	args->k_entry2	= 0x60;
142327f44888SBenjamin Herrenschmidt 
142427f44888SBenjamin Herrenschmidt 	top_addr = _ALIGN_UP(args->k_size, align);
142527f44888SBenjamin Herrenschmidt 
14265827d416SAnton Blanchard 	if (prom_initrd_start != 0) {
14275827d416SAnton Blanchard 		args->rd_image = prom_initrd_start;
14285827d416SAnton Blanchard 		args->rd_size = prom_initrd_end - args->rd_image;
142927f44888SBenjamin Herrenschmidt 		args->rd_loc = top_addr;
143027f44888SBenjamin Herrenschmidt 		top_addr = _ALIGN_UP(args->rd_loc + args->rd_size, align);
143127f44888SBenjamin Herrenschmidt 	}
143227f44888SBenjamin Herrenschmidt 
143327f44888SBenjamin Herrenschmidt 	/* Pickup an address for the HAL. We want to go really high
143427f44888SBenjamin Herrenschmidt 	 * up to avoid problem with future kexecs. On the other hand
143527f44888SBenjamin Herrenschmidt 	 * we don't want to be all over the TCEs on P5IOC2 machines
143627f44888SBenjamin Herrenschmidt 	 * which are going to be up there too. We assume the machine
143727f44888SBenjamin Herrenschmidt 	 * has plenty of memory, and we ask for the HAL for now to
143827f44888SBenjamin Herrenschmidt 	 * be just below the 1G point, or above the initrd
143927f44888SBenjamin Herrenschmidt 	 */
14405827d416SAnton Blanchard 	opal_addr = _ALIGN_DOWN(0x40000000 - prom_opal_size, align);
144127f44888SBenjamin Herrenschmidt 	if (opal_addr < top_addr)
144227f44888SBenjamin Herrenschmidt 		opal_addr = top_addr;
144327f44888SBenjamin Herrenschmidt 	args->hal_addr = opal_addr;
144427f44888SBenjamin Herrenschmidt 
1445817c21adSBenjamin Herrenschmidt 	/* Copy the command line to the kernel image */
14465827d416SAnton Blanchard 	strlcpy(boot_command_line, prom_cmd_line,
1447817c21adSBenjamin Herrenschmidt 		COMMAND_LINE_SIZE);
1448817c21adSBenjamin Herrenschmidt 
144927f44888SBenjamin Herrenschmidt 	prom_debug("  k_image    = 0x%lx\n", args->k_image);
145027f44888SBenjamin Herrenschmidt 	prom_debug("  k_size     = 0x%lx\n", args->k_size);
145127f44888SBenjamin Herrenschmidt 	prom_debug("  k_entry    = 0x%lx\n", args->k_entry);
145227f44888SBenjamin Herrenschmidt 	prom_debug("  k_entry2   = 0x%lx\n", args->k_entry2);
145327f44888SBenjamin Herrenschmidt 	prom_debug("  hal_addr   = 0x%lx\n", args->hal_addr);
145427f44888SBenjamin Herrenschmidt 	prom_debug("  rd_image   = 0x%lx\n", args->rd_image);
145527f44888SBenjamin Herrenschmidt 	prom_debug("  rd_size    = 0x%lx\n", args->rd_size);
145627f44888SBenjamin Herrenschmidt 	prom_debug("  rd_loc     = 0x%lx\n", args->rd_loc);
145727f44888SBenjamin Herrenschmidt 	prom_printf("Performing OPAL takeover,this can take a few minutes..\n");
145827f44888SBenjamin Herrenschmidt 	prom_close_stdin();
145927f44888SBenjamin Herrenschmidt 	mb();
146027f44888SBenjamin Herrenschmidt 	data->go = 1;
146127f44888SBenjamin Herrenschmidt 	for (;;)
146227f44888SBenjamin Herrenschmidt 		opal_do_takeover(args);
146327f44888SBenjamin Herrenschmidt }
1464493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
14656e35d5daSBenjamin Herrenschmidt 
14666e35d5daSBenjamin Herrenschmidt /*
14676e35d5daSBenjamin Herrenschmidt  * Allocate room for and instantiate OPAL
14686e35d5daSBenjamin Herrenschmidt  */
14696e35d5daSBenjamin Herrenschmidt static void __init prom_instantiate_opal(void)
14706e35d5daSBenjamin Herrenschmidt {
14716e35d5daSBenjamin Herrenschmidt 	phandle opal_node;
14726e35d5daSBenjamin Herrenschmidt 	ihandle opal_inst;
14736e35d5daSBenjamin Herrenschmidt 	u64 base, entry;
14746e35d5daSBenjamin Herrenschmidt 	u64 size = 0, align = 0x10000;
1475493adffcSBenjamin Herrenschmidt 	__be64 val64;
14766e35d5daSBenjamin Herrenschmidt 	u32 rets[2];
14776e35d5daSBenjamin Herrenschmidt 
14786e35d5daSBenjamin Herrenschmidt 	prom_debug("prom_instantiate_opal: start...\n");
14796e35d5daSBenjamin Herrenschmidt 
14806e35d5daSBenjamin Herrenschmidt 	opal_node = call_prom("finddevice", 1, 1, ADDR("/ibm,opal"));
14816e35d5daSBenjamin Herrenschmidt 	prom_debug("opal_node: %x\n", opal_node);
14826e35d5daSBenjamin Herrenschmidt 	if (!PHANDLE_VALID(opal_node))
14836e35d5daSBenjamin Herrenschmidt 		return;
14846e35d5daSBenjamin Herrenschmidt 
1485493adffcSBenjamin Herrenschmidt 	val64 = 0;
1486493adffcSBenjamin Herrenschmidt 	prom_getprop(opal_node, "opal-runtime-size", &val64, sizeof(val64));
1487493adffcSBenjamin Herrenschmidt 	size = be64_to_cpu(val64);
14886e35d5daSBenjamin Herrenschmidt 	if (size == 0)
14896e35d5daSBenjamin Herrenschmidt 		return;
1490493adffcSBenjamin Herrenschmidt 	val64 = 0;
1491493adffcSBenjamin Herrenschmidt 	prom_getprop(opal_node, "opal-runtime-alignment", &val64,sizeof(val64));
1492493adffcSBenjamin Herrenschmidt 	align = be64_to_cpu(val64);
14936e35d5daSBenjamin Herrenschmidt 
14946e35d5daSBenjamin Herrenschmidt 	base = alloc_down(size, align, 0);
14956e35d5daSBenjamin Herrenschmidt 	if (base == 0) {
14966e35d5daSBenjamin Herrenschmidt 		prom_printf("OPAL allocation failed !\n");
14976e35d5daSBenjamin Herrenschmidt 		return;
14986e35d5daSBenjamin Herrenschmidt 	}
14996e35d5daSBenjamin Herrenschmidt 
15006e35d5daSBenjamin Herrenschmidt 	opal_inst = call_prom("open", 1, 1, ADDR("/ibm,opal"));
15016e35d5daSBenjamin Herrenschmidt 	if (!IHANDLE_VALID(opal_inst)) {
15026e35d5daSBenjamin Herrenschmidt 		prom_printf("opening opal package failed (%x)\n", opal_inst);
15036e35d5daSBenjamin Herrenschmidt 		return;
15046e35d5daSBenjamin Herrenschmidt 	}
15056e35d5daSBenjamin Herrenschmidt 
15066e35d5daSBenjamin Herrenschmidt 	prom_printf("instantiating opal at 0x%x...", base);
15076e35d5daSBenjamin Herrenschmidt 
15086e35d5daSBenjamin Herrenschmidt 	if (call_prom_ret("call-method", 4, 3, rets,
15096e35d5daSBenjamin Herrenschmidt 			  ADDR("load-opal-runtime"),
15106e35d5daSBenjamin Herrenschmidt 			  opal_inst,
15116e35d5daSBenjamin Herrenschmidt 			  base >> 32, base & 0xffffffff) != 0
15126e35d5daSBenjamin Herrenschmidt 	    || (rets[0] == 0 && rets[1] == 0)) {
15136e35d5daSBenjamin Herrenschmidt 		prom_printf(" failed\n");
15146e35d5daSBenjamin Herrenschmidt 		return;
15156e35d5daSBenjamin Herrenschmidt 	}
15166e35d5daSBenjamin Herrenschmidt 	entry = (((u64)rets[0]) << 32) | rets[1];
15176e35d5daSBenjamin Herrenschmidt 
15186e35d5daSBenjamin Herrenschmidt 	prom_printf(" done\n");
15196e35d5daSBenjamin Herrenschmidt 
15206e35d5daSBenjamin Herrenschmidt 	reserve_mem(base, size);
15216e35d5daSBenjamin Herrenschmidt 
15226e35d5daSBenjamin Herrenschmidt 	prom_debug("opal base     = 0x%x\n", base);
15236e35d5daSBenjamin Herrenschmidt 	prom_debug("opal align    = 0x%x\n", align);
15246e35d5daSBenjamin Herrenschmidt 	prom_debug("opal entry    = 0x%x\n", entry);
15256e35d5daSBenjamin Herrenschmidt 	prom_debug("opal size     = 0x%x\n", (long)size);
15266e35d5daSBenjamin Herrenschmidt 
15276e35d5daSBenjamin Herrenschmidt 	prom_setprop(opal_node, "/ibm,opal", "opal-base-address",
15286e35d5daSBenjamin Herrenschmidt 		     &base, sizeof(base));
15296e35d5daSBenjamin Herrenschmidt 	prom_setprop(opal_node, "/ibm,opal", "opal-entry-address",
15306e35d5daSBenjamin Herrenschmidt 		     &entry, sizeof(entry));
15316e35d5daSBenjamin Herrenschmidt 
15326e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
15335827d416SAnton Blanchard 	prom_opal_base = base;
15345827d416SAnton Blanchard 	prom_opal_entry = entry;
15356e35d5daSBenjamin Herrenschmidt #endif
15366e35d5daSBenjamin Herrenschmidt 	prom_debug("prom_instantiate_opal: end...\n");
15376e35d5daSBenjamin Herrenschmidt }
15386e35d5daSBenjamin Herrenschmidt 
153927f44888SBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV */
15409b6b563cSPaul Mackerras 
15419b6b563cSPaul Mackerras /*
15429b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
15439b6b563cSPaul Mackerras  */
15449b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
15459b6b563cSPaul Mackerras {
15469b6b563cSPaul Mackerras 	phandle rtas_node;
15479b6b563cSPaul Mackerras 	ihandle rtas_inst;
15489b6b563cSPaul Mackerras 	u32 base, entry = 0;
1549493adffcSBenjamin Herrenschmidt 	__be32 val;
15509b6b563cSPaul Mackerras 	u32 size = 0;
15519b6b563cSPaul Mackerras 
15529b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
15539b6b563cSPaul Mackerras 
15549b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
15559b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
15569b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
15579b6b563cSPaul Mackerras 		return;
15589b6b563cSPaul Mackerras 
1559493adffcSBenjamin Herrenschmidt 	val = 0;
1560493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1561493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
15629b6b563cSPaul Mackerras 	if (size == 0)
15639b6b563cSPaul Mackerras 		return;
15649b6b563cSPaul Mackerras 
15659b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
15666d1e2c6cSAnton Blanchard 	if (base == 0)
15676d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
15689b6b563cSPaul Mackerras 
15699b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
15709b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1571a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
15729b6b563cSPaul Mackerras 		return;
15739b6b563cSPaul Mackerras 	}
15749b6b563cSPaul Mackerras 
15759b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
15769b6b563cSPaul Mackerras 
15779b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
15789b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1579a23414beSPaul Mackerras 			  rtas_inst, base) != 0
15809b6b563cSPaul Mackerras 	    || entry == 0) {
15819b6b563cSPaul Mackerras 		prom_printf(" failed\n");
15829b6b563cSPaul Mackerras 		return;
15839b6b563cSPaul Mackerras 	}
15849b6b563cSPaul Mackerras 	prom_printf(" done\n");
15859b6b563cSPaul Mackerras 
15869b6b563cSPaul Mackerras 	reserve_mem(base, size);
15879b6b563cSPaul Mackerras 
1588493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1589a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1590493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1591493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1592a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1593493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
15949b6b563cSPaul Mackerras 
1595dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1596dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1597dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1598dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1599dbe78b40SBenjamin Herrenschmidt 
1600493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC_POWERNV) && defined(__BIG_ENDIAN__)
160127f44888SBenjamin Herrenschmidt 	/* PowerVN takeover hack */
16025827d416SAnton Blanchard 	prom_rtas_data = base;
16035827d416SAnton Blanchard 	prom_rtas_entry = entry;
16045827d416SAnton Blanchard 	prom_getprop(rtas_node, "start-cpu", &prom_rtas_start_cpu, 4);
160527f44888SBenjamin Herrenschmidt #endif
16069b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
16079b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
16089b6b563cSPaul Mackerras 	prom_debug("rtas size     = 0x%x\n", (long)size);
16099b6b563cSPaul Mackerras 
16109b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
16119b6b563cSPaul Mackerras }
16129b6b563cSPaul Mackerras 
16139b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
16149b6b563cSPaul Mackerras /*
16154a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
16164a727429SAshley Lai  */
16174a727429SAshley Lai static void __init prom_instantiate_sml(void)
16184a727429SAshley Lai {
16194a727429SAshley Lai 	phandle ibmvtpm_node;
16204a727429SAshley Lai 	ihandle ibmvtpm_inst;
16214a727429SAshley Lai 	u32 entry = 0, size = 0;
16224a727429SAshley Lai 	u64 base;
16234a727429SAshley Lai 
16244a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
16254a727429SAshley Lai 
16264a727429SAshley Lai 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/ibm,vtpm"));
16274a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
16284a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
16294a727429SAshley Lai 		return;
16304a727429SAshley Lai 
16314a727429SAshley Lai 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/ibm,vtpm"));
16324a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
16334a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
16344a727429SAshley Lai 		return;
16354a727429SAshley Lai 	}
16364a727429SAshley Lai 
16374a727429SAshley Lai 	if (call_prom_ret("call-method", 2, 2, &size,
16384a727429SAshley Lai 			  ADDR("sml-get-handover-size"),
16394a727429SAshley Lai 			  ibmvtpm_inst) != 0 || size == 0) {
16404a727429SAshley Lai 		prom_printf("SML get handover size failed\n");
16414a727429SAshley Lai 		return;
16424a727429SAshley Lai 	}
16434a727429SAshley Lai 
16444a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
16454a727429SAshley Lai 	if (base == 0)
16464a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
16474a727429SAshley Lai 
16484a727429SAshley Lai 	prom_printf("instantiating sml at 0x%x...", base);
16494a727429SAshley Lai 
16504a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
16514a727429SAshley Lai 			  ADDR("sml-handover"),
16524a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
16534a727429SAshley Lai 		prom_printf("SML handover failed\n");
16544a727429SAshley Lai 		return;
16554a727429SAshley Lai 	}
16564a727429SAshley Lai 	prom_printf(" done\n");
16574a727429SAshley Lai 
16584a727429SAshley Lai 	reserve_mem(base, size);
16594a727429SAshley Lai 
16604a727429SAshley Lai 	prom_setprop(ibmvtpm_node, "/ibm,vtpm", "linux,sml-base",
16614a727429SAshley Lai 		     &base, sizeof(base));
16624a727429SAshley Lai 	prom_setprop(ibmvtpm_node, "/ibm,vtpm", "linux,sml-size",
16634a727429SAshley Lai 		     &size, sizeof(size));
16644a727429SAshley Lai 
16654a727429SAshley Lai 	prom_debug("sml base     = 0x%x\n", base);
16664a727429SAshley Lai 	prom_debug("sml size     = 0x%x\n", (long)size);
16674a727429SAshley Lai 
16684a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
16694a727429SAshley Lai }
16704a727429SAshley Lai 
16714a727429SAshley Lai /*
16729b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
16739b6b563cSPaul Mackerras  */
1674493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
16759b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
16769b6b563cSPaul Mackerras {
16779b6b563cSPaul Mackerras 	phandle node;
16789b6b563cSPaul Mackerras 	ihandle phb_node;
16799b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
16805827d416SAnton Blanchard 	char *path = prom_scratch;
16819b6b563cSPaul Mackerras 	u64 base, align;
16829b6b563cSPaul Mackerras 	u32 minalign, minsize;
16839b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
16849b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
16859b6b563cSPaul Mackerras 	u64 i;
16869b6b563cSPaul Mackerras 
16875827d416SAnton Blanchard 	if (prom_iommu_off)
16889b6b563cSPaul Mackerras 		return;
16899b6b563cSPaul Mackerras 
16909b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
16919b6b563cSPaul Mackerras 
16929b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
16935827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
16949b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
16959b6b563cSPaul Mackerras 
16969b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
16979b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
16989b6b563cSPaul Mackerras 		compatible[0] = 0;
16999b6b563cSPaul Mackerras 		type[0] = 0;
17009b6b563cSPaul Mackerras 		model[0] = 0;
17019b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
17029b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
17039b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
17049b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
17059b6b563cSPaul Mackerras 
17065827d416SAnton Blanchard 		if ((type[0] == 0) || (strstr(type, "pci") == NULL))
17079b6b563cSPaul Mackerras 			continue;
17089b6b563cSPaul Mackerras 
1709e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
17109b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
17115827d416SAnton Blanchard 			if ((strstr(compatible, "python") == NULL) &&
17125827d416SAnton Blanchard 			    (strstr(compatible, "Speedwagon") == NULL) &&
17135827d416SAnton Blanchard 			    (strstr(compatible, "Winnipeg") == NULL))
17149b6b563cSPaul Mackerras 				continue;
17159b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
17165827d416SAnton Blanchard 			if ((strstr(model, "ython") == NULL) &&
17175827d416SAnton Blanchard 			    (strstr(model, "peedwagon") == NULL) &&
17185827d416SAnton Blanchard 			    (strstr(model, "innipeg") == NULL))
17199b6b563cSPaul Mackerras 				continue;
17209b6b563cSPaul Mackerras 		}
17219b6b563cSPaul Mackerras 
17229b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
17239b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
17249b6b563cSPaul Mackerras 			minalign = 0;
17259b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
17269b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
17279b6b563cSPaul Mackerras 			minsize = 4UL << 20;
17289b6b563cSPaul Mackerras 
17299b6b563cSPaul Mackerras 		/*
17309b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
17319b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
17329b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
17339b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
17349b6b563cSPaul Mackerras 		 *
17359b6b563cSPaul Mackerras 		 * On POWER4, firmware sets the TCE region by assuming
17369b6b563cSPaul Mackerras 		 * each TCE table is 8MB. Using this memory for anything
17379b6b563cSPaul Mackerras 		 * else will impact performance, so we always allocate 8MB.
17389b6b563cSPaul Mackerras 		 * Anton
17399b6b563cSPaul Mackerras 		 */
1740d3dbeef6SMichael Ellerman 		if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p))
17419b6b563cSPaul Mackerras 			minsize = 8UL << 20;
17429b6b563cSPaul Mackerras 		else
17439b6b563cSPaul Mackerras 			minsize = 4UL << 20;
17449b6b563cSPaul Mackerras 
17459b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
17469b6b563cSPaul Mackerras 		align = max(minalign, minsize);
17479b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
17489b6b563cSPaul Mackerras 		if (base == 0)
17499b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
17509b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
17519b6b563cSPaul Mackerras 			local_alloc_bottom = base;
17529b6b563cSPaul Mackerras 
17539b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
1754aca71ef8SLi Zefan 		memset(path, 0, PROM_SCRATCH_SIZE);
17559b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
17569b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
17579b6b563cSPaul Mackerras 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
17589b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
17599b6b563cSPaul Mackerras 		}
17609b6b563cSPaul Mackerras 
1761a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
1762a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1763a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1764a23414beSPaul Mackerras 
17659b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
17669b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
17679b6b563cSPaul Mackerras 		prom_debug("\tbase = 0x%x\n", base);
17689b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
17699b6b563cSPaul Mackerras 
17709b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
17719b6b563cSPaul Mackerras 		 * over the allocated size.
17729b6b563cSPaul Mackerras 		 */
17732b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
17749b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
17759b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
17769b6b563cSPaul Mackerras 			tce_entry |= 0x3;
17779b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
17789b6b563cSPaul Mackerras 		}
17799b6b563cSPaul Mackerras 
17809b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
17819b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
17829b6b563cSPaul Mackerras 		if (phb_node == 0)
17839b6b563cSPaul Mackerras 			prom_printf("... failed\n");
17849b6b563cSPaul Mackerras 		else
17859b6b563cSPaul Mackerras 			prom_printf("... done\n");
17869b6b563cSPaul Mackerras 
17879b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
17889b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
17899b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
17909b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
17919b6b563cSPaul Mackerras 	}
17929b6b563cSPaul Mackerras 
17939b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
17949b6b563cSPaul Mackerras 
17952babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
17962babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
17975827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
17985827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
17999b6b563cSPaul Mackerras 
18009b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
18019b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
18029b6b563cSPaul Mackerras }
1803493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1804493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
18059b6b563cSPaul Mackerras 
18069b6b563cSPaul Mackerras /*
18079b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
18089b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
18099b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
18109b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
18119b6b563cSPaul Mackerras  *
18129b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
18139b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
18149b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
18159b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
18169b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
18179b6b563cSPaul Mackerras  * of setting those values.
18189b6b563cSPaul Mackerras  *
18199b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
18209b6b563cSPaul Mackerras  * is in its holding pattern code.
18219b6b563cSPaul Mackerras  *
18229b6b563cSPaul Mackerras  * -- Cort
18239b6b563cSPaul Mackerras  */
1824bbd0abdaSPaul Mackerras /*
1825bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
1826bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
1827bbd0abdaSPaul Mackerras  */
1828bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1829bbd0abdaSPaul Mackerras 
18309b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
18319b6b563cSPaul Mackerras {
18329b6b563cSPaul Mackerras 	unsigned long i;
18339b6b563cSPaul Mackerras 	phandle node;
18349b6b563cSPaul Mackerras 	char type[64];
18359b6b563cSPaul Mackerras 	unsigned long *spinloop
1836bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
18379b6b563cSPaul Mackerras 	unsigned long *acknowledge
1838bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1839bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
18409b6b563cSPaul Mackerras 
1841dbe78b40SBenjamin Herrenschmidt 	/*
1842dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
1843dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
1844dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
1845dbe78b40SBenjamin Herrenschmidt 	 */
1846dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
1847dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
1848dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
1849dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
1850dbe78b40SBenjamin Herrenschmidt 		return;
1851dbe78b40SBenjamin Herrenschmidt 	}
1852dbe78b40SBenjamin Herrenschmidt 
18539b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
18549b6b563cSPaul Mackerras 	prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
18559b6b563cSPaul Mackerras 	prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
18569b6b563cSPaul Mackerras 	prom_debug("    1) acknowledge    = 0x%x\n",
18579b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
18589b6b563cSPaul Mackerras 	prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
18599b6b563cSPaul Mackerras 	prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
18609b6b563cSPaul Mackerras 
18619b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
18629b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
18639b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
18649b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
18659b6b563cSPaul Mackerras 	 */
18669b6b563cSPaul Mackerras 	*spinloop = 0;
18679b6b563cSPaul Mackerras 
18689b6b563cSPaul Mackerras 	/* look for cpus */
18699b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
1870493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
1871493adffcSBenjamin Herrenschmidt 		__be32 reg;
1872493adffcSBenjamin Herrenschmidt 
18739b6b563cSPaul Mackerras 		type[0] = 0;
18749b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
18755827d416SAnton Blanchard 		if (strcmp(type, "cpu") != 0)
18769b6b563cSPaul Mackerras 			continue;
18779b6b563cSPaul Mackerras 
18789b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
18799b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
18805827d416SAnton Blanchard 			if (strcmp(type, "okay") != 0)
18819b6b563cSPaul Mackerras 				continue;
18829b6b563cSPaul Mackerras 
1883493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
18849b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
1885493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
18869b6b563cSPaul Mackerras 
1887493adffcSBenjamin Herrenschmidt 		prom_debug("cpu hw idx   = %lu\n", cpu_no);
18889b6b563cSPaul Mackerras 
18899b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
18909b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
18919b6b563cSPaul Mackerras 		 * spinloop.
18929b6b563cSPaul Mackerras 		 */
18939b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
18949b6b563cSPaul Mackerras 
1895493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
189627f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
1897493adffcSBenjamin Herrenschmidt 			prom_printf("starting cpu hw idx %lu... ", cpu_no);
18989b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
1899493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
19009b6b563cSPaul Mackerras 
19019b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
19029b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
19039b6b563cSPaul Mackerras 				mb();
19049b6b563cSPaul Mackerras 
1905493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
19069b6b563cSPaul Mackerras 				prom_printf("done\n");
1907bbd0abdaSPaul Mackerras 			else
19089b6b563cSPaul Mackerras 				prom_printf("failed: %x\n", *acknowledge);
19099b6b563cSPaul Mackerras 		}
19109b6b563cSPaul Mackerras #ifdef CONFIG_SMP
19119b6b563cSPaul Mackerras 		else
1912493adffcSBenjamin Herrenschmidt 			prom_printf("boot cpu hw idx %lu\n", cpu_no);
19139b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
19149b6b563cSPaul Mackerras 	}
19159b6b563cSPaul Mackerras 
19169b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
19179b6b563cSPaul Mackerras }
19189b6b563cSPaul Mackerras 
19199b6b563cSPaul Mackerras 
19209b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
19219b6b563cSPaul Mackerras {
19229b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
19235827d416SAnton Blanchard 	prom_entry = pp;
19249b6b563cSPaul Mackerras 
19259b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
19265827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
19275827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
19289b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
19299b6b563cSPaul Mackerras 
19309b6b563cSPaul Mackerras 	/* get device tree root */
19315827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
19325827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
19339b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1934a575b807SPaul Mackerras 
19355827d416SAnton Blanchard 	prom.mmumap = 0;
19369b6b563cSPaul Mackerras }
19379b6b563cSPaul Mackerras 
1938a575b807SPaul Mackerras #ifdef CONFIG_PPC32
1939a575b807SPaul Mackerras /*
1940a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
1941a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
1942a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
1943a575b807SPaul Mackerras  */
1944a575b807SPaul Mackerras static void __init prom_find_mmu(void)
1945a575b807SPaul Mackerras {
1946a575b807SPaul Mackerras 	phandle oprom;
1947a575b807SPaul Mackerras 	char version[64];
1948a575b807SPaul Mackerras 
1949a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1950a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
1951a575b807SPaul Mackerras 		return;
1952a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1953a575b807SPaul Mackerras 		return;
1954a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
1955a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
1956a23414beSPaul Mackerras 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1957a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
1958a23414beSPaul Mackerras 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1959a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1960a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1961a23414beSPaul Mackerras 	} else
1962a575b807SPaul Mackerras 		return;
19635827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
19645827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
19655827d416SAnton Blanchard 		     sizeof(prom.mmumap));
1966493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
19675827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
1968a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
1969a575b807SPaul Mackerras }
1970a575b807SPaul Mackerras #else
1971a575b807SPaul Mackerras #define prom_find_mmu()
1972a575b807SPaul Mackerras #endif
1973a575b807SPaul Mackerras 
19749b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
19759b6b563cSPaul Mackerras {
19765827d416SAnton Blanchard 	char *path = of_stdout_device;
19779b6b563cSPaul Mackerras 	char type[16];
1978493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
1979493adffcSBenjamin Herrenschmidt 	__be32 val;
19809b6b563cSPaul Mackerras 
19815827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
19829b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
19839b6b563cSPaul Mackerras 
1984493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
19859b6b563cSPaul Mackerras 
19869b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
19879b6b563cSPaul Mackerras 	memset(path, 0, 256);
19885827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
1989493adffcSBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
1990493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(stdout_node);
19915827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-package",
1992a23414beSPaul Mackerras 		     &val, sizeof(val));
19935827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
19945827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
1995a23414beSPaul Mackerras 		     path, strlen(path) + 1);
19969b6b563cSPaul Mackerras 
19979b6b563cSPaul Mackerras 	/* If it's a display, note it */
19989b6b563cSPaul Mackerras 	memset(type, 0, sizeof(type));
1999493adffcSBenjamin Herrenschmidt 	prom_getprop(stdout_node, "device_type", type, sizeof(type));
20005827d416SAnton Blanchard 	if (strcmp(type, "display") == 0)
2001493adffcSBenjamin Herrenschmidt 		prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
20029b6b563cSPaul Mackerras }
20039b6b563cSPaul Mackerras 
20049b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
20059b6b563cSPaul Mackerras {
20069b6b563cSPaul Mackerras 	char compat[256];
20079b6b563cSPaul Mackerras 	int len, i = 0;
200821fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
20099b6b563cSPaul Mackerras 	phandle rtas;
2010e8222502SBenjamin Herrenschmidt 	int x;
201121fe3301SBenjamin Herrenschmidt #endif
2012e8222502SBenjamin Herrenschmidt 
20136e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
20145827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
20159b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
20169b6b563cSPaul Mackerras 	if (len > 0) {
20179b6b563cSPaul Mackerras 		compat[len] = 0;
20189b6b563cSPaul Mackerras 		while (i < len) {
20199b6b563cSPaul Mackerras 			char *p = &compat[i];
20209b6b563cSPaul Mackerras 			int sl = strlen(p);
20219b6b563cSPaul Mackerras 			if (sl == 0)
20229b6b563cSPaul Mackerras 				break;
20235827d416SAnton Blanchard 			if (strstr(p, "Power Macintosh") ||
20245827d416SAnton Blanchard 			    strstr(p, "MacRISC"))
20259b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
2026133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
2027133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
2028133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
2029133dda1eSArnd Bergmann 			 * so we do it here.
2030133dda1eSArnd Bergmann 			 */
20315827d416SAnton Blanchard 			if (strstr(p, "IBM,CBEA") ||
20325827d416SAnton Blanchard 			    strstr(p, "IBM,CPBW-1.0"))
2033133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
2034133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
20359b6b563cSPaul Mackerras 			i += sl + 1;
20369b6b563cSPaul Mackerras 		}
20379b6b563cSPaul Mackerras 	}
20389b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
20396e35d5daSBenjamin Herrenschmidt 	/* Try to detect OPAL */
20406e35d5daSBenjamin Herrenschmidt 	if (PHANDLE_VALID(call_prom("finddevice", 1, 1, ADDR("/ibm,opal"))))
20416e35d5daSBenjamin Herrenschmidt 		return PLATFORM_OPAL;
20426e35d5daSBenjamin Herrenschmidt 
20436e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
2044e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
2045e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
2046e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
2047e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
2048e8222502SBenjamin Herrenschmidt 	 */
20495827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
2050e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
2051e8222502SBenjamin Herrenschmidt 	if (len <= 0)
2052e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
20535827d416SAnton Blanchard 	if (strcmp(compat, "chrp"))
2054e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2055e8222502SBenjamin Herrenschmidt 
20569b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
20579b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2058e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
2059e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2060e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
20619b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
20624da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
20639b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
20649b6b563cSPaul Mackerras 	}
20659b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
20669b6b563cSPaul Mackerras #else
2067e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
20689b6b563cSPaul Mackerras #endif
20699b6b563cSPaul Mackerras }
20709b6b563cSPaul Mackerras 
20719b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
20729b6b563cSPaul Mackerras {
20739b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
20749b6b563cSPaul Mackerras }
20759b6b563cSPaul Mackerras 
20769b6b563cSPaul Mackerras /*
20779b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
20789b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
20799b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
20809b6b563cSPaul Mackerras  * we've taken over the MMU.
20819b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
20829b6b563cSPaul Mackerras  * and if so, open it now.
20839b6b563cSPaul Mackerras  */
20849b6b563cSPaul Mackerras static void __init prom_check_displays(void)
20859b6b563cSPaul Mackerras {
20869b6b563cSPaul Mackerras 	char type[16], *path;
20879b6b563cSPaul Mackerras 	phandle node;
20889b6b563cSPaul Mackerras 	ihandle ih;
20899b6b563cSPaul Mackerras 	int i;
20909b6b563cSPaul Mackerras 
20919b6b563cSPaul Mackerras 	static unsigned char default_colors[] = {
20929b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
20939b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
20949b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
20959b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
20969b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
20979b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
20989b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
20999b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
21009b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
21019b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
21029b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
21039b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
21049b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
21059b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
21069b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
21079b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
21089b6b563cSPaul Mackerras 	};
21099b6b563cSPaul Mackerras 	const unsigned char *clut;
21109b6b563cSPaul Mackerras 
21114da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
21129b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
21139b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
21149b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
21155827d416SAnton Blanchard 		if (strcmp(type, "display") != 0)
21169b6b563cSPaul Mackerras 			continue;
21179b6b563cSPaul Mackerras 
21189b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
21195827d416SAnton Blanchard 		path = prom_scratch;
21209b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
21219b6b563cSPaul Mackerras 
21229b6b563cSPaul Mackerras 		/*
21239b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
21249b6b563cSPaul Mackerras 		 * arguments
21259b6b563cSPaul Mackerras 		 */
21269b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
21279b6b563cSPaul Mackerras 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
21289b6b563cSPaul Mackerras 			continue;
21299b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
21309b6b563cSPaul Mackerras 
21319b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
21329b6b563cSPaul Mackerras 		if (ih == 0) {
21339b6b563cSPaul Mackerras 			prom_printf("failed\n");
21349b6b563cSPaul Mackerras 			continue;
21359b6b563cSPaul Mackerras 		}
21369b6b563cSPaul Mackerras 
21379b6b563cSPaul Mackerras 		/* Success */
21389b6b563cSPaul Mackerras 		prom_printf("done\n");
2139a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
21409b6b563cSPaul Mackerras 
21419b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
21429b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
21435827d416SAnton Blanchard 		clut = default_colors;
21443f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
21459b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
21469b6b563cSPaul Mackerras 					   clut[2]) != 0)
21479b6b563cSPaul Mackerras 				break;
21489b6b563cSPaul Mackerras 
21499b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
21505827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
21515827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
21529b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
21539b6b563cSPaul Mackerras 					   clut[2]) != 0)
21549b6b563cSPaul Mackerras 				break;
21559b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
21567191b615SBenjamin Herrenschmidt 
21577191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
21587191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
21597191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
21607191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
21617191b615SBenjamin Herrenschmidt 
21627191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
21637191b615SBenjamin Herrenschmidt 			prom_getprop(node, "width", &width, 4);
21647191b615SBenjamin Herrenschmidt 			prom_getprop(node, "height", &height, 4);
21657191b615SBenjamin Herrenschmidt 			prom_getprop(node, "linebytes", &pitch, 4);
21667191b615SBenjamin Herrenschmidt 			prom_getprop(node, "address", &addr, 4);
21677191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
21687191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
21697191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
21707191b615SBenjamin Herrenschmidt 		}
21717191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
21729b6b563cSPaul Mackerras 	}
21739b6b563cSPaul Mackerras }
21749b6b563cSPaul Mackerras 
21759b6b563cSPaul Mackerras 
21769b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
21779b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
21789b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
21799b6b563cSPaul Mackerras {
21809b6b563cSPaul Mackerras 	void *ret;
21819b6b563cSPaul Mackerras 
21829b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
21839b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
21849b6b563cSPaul Mackerras 		unsigned long room, chunk;
21859b6b563cSPaul Mackerras 
21869b6b563cSPaul Mackerras 		prom_debug("Chunk exhausted, claiming more at %x...\n",
21875827d416SAnton Blanchard 			   alloc_bottom);
21885827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
21899b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
21909b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
21919b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2192fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2193fbafd728SAnton Blanchard 				   "(no room)\n");
21949b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
21959b6b563cSPaul Mackerras 		if (chunk == 0)
2196fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2197fbafd728SAnton Blanchard 				   "(claim failed)\n");
2198966728ddSAnton Blanchard 		*mem_end = chunk + room;
21999b6b563cSPaul Mackerras 	}
22009b6b563cSPaul Mackerras 
22019b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
22029b6b563cSPaul Mackerras 	*mem_start += needed;
22039b6b563cSPaul Mackerras 
22049b6b563cSPaul Mackerras 	return ret;
22059b6b563cSPaul Mackerras }
22069b6b563cSPaul Mackerras 
2207493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2208493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2209493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2210493adffcSBenjamin Herrenschmidt 	} while(0)
22119b6b563cSPaul Mackerras 
22129b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
22139b6b563cSPaul Mackerras {
22149b6b563cSPaul Mackerras 	char *s, *os;
22159b6b563cSPaul Mackerras 
22165827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
22179b6b563cSPaul Mackerras 	s += 4;
22185827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
22199b6b563cSPaul Mackerras 		if (strcmp(s, str) == 0)
22209b6b563cSPaul Mackerras 			return s - os;
22219b6b563cSPaul Mackerras 		s += strlen(s) + 1;
22229b6b563cSPaul Mackerras 	}
22239b6b563cSPaul Mackerras 	return 0;
22249b6b563cSPaul Mackerras }
22259b6b563cSPaul Mackerras 
22269b6b563cSPaul Mackerras /*
22279b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
22289b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
22299b6b563cSPaul Mackerras  */
22309b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
22319b6b563cSPaul Mackerras 
22329b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
22339b6b563cSPaul Mackerras 					 unsigned long *mem_start,
22349b6b563cSPaul Mackerras 					 unsigned long *mem_end)
22359b6b563cSPaul Mackerras {
22369b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
22379b6b563cSPaul Mackerras 	unsigned long soff;
22389b6b563cSPaul Mackerras 	phandle child;
22399b6b563cSPaul Mackerras 
22405827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
22419b6b563cSPaul Mackerras 
22429b6b563cSPaul Mackerras 	/* get and store all property names */
22435827d416SAnton Blanchard 	prev_name = "";
22449b6b563cSPaul Mackerras 	for (;;) {
22459b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
22469b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
22479b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
22489b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
22499b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
22509b6b563cSPaul Mackerras 			break;
22519b6b563cSPaul Mackerras 		}
22529b6b563cSPaul Mackerras 
22539b6b563cSPaul Mackerras  		/* skip "name" */
22545827d416SAnton Blanchard  		if (strcmp(namep, "name") == 0) {
22559b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
22565827d416SAnton Blanchard  			prev_name = "name";
22579b6b563cSPaul Mackerras  			continue;
22589b6b563cSPaul Mackerras  		}
22599b6b563cSPaul Mackerras 		/* get/create string entry */
22609b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
22619b6b563cSPaul Mackerras 		if (soff != 0) {
22629b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
22639b6b563cSPaul Mackerras 			namep = sstart + soff;
22649b6b563cSPaul Mackerras 		} else {
22659b6b563cSPaul Mackerras 			/* Trim off some if we can */
22669b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
22675827d416SAnton Blanchard 			dt_string_end = *mem_start;
22689b6b563cSPaul Mackerras 		}
22699b6b563cSPaul Mackerras 		prev_name = namep;
22709b6b563cSPaul Mackerras 	}
22719b6b563cSPaul Mackerras 
22729b6b563cSPaul Mackerras 	/* do all our children */
22739b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
22749b6b563cSPaul Mackerras 	while (child != 0) {
22759b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
22769b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
22779b6b563cSPaul Mackerras 	}
22789b6b563cSPaul Mackerras }
22799b6b563cSPaul Mackerras 
22809b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
22819b6b563cSPaul Mackerras 					unsigned long *mem_end)
22829b6b563cSPaul Mackerras {
22839b6b563cSPaul Mackerras 	phandle child;
22849b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
22859b6b563cSPaul Mackerras 	unsigned long soff;
22869b6b563cSPaul Mackerras 	unsigned char *valp;
22879b6b563cSPaul Mackerras 	static char pname[MAX_PROPERTY_NAME];
22886e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
22899b6b563cSPaul Mackerras 
22909b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
22919b6b563cSPaul Mackerras 
22929b6b563cSPaul Mackerras 	/* get the node's full name */
22939b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2294c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2295c4988820SPaul Mackerras 	if (room > 255)
2296c4988820SPaul Mackerras 		room = 255;
2297c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
22989b6b563cSPaul Mackerras 	if (l >= 0) {
22999b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2300c4988820SPaul Mackerras 		if (l >= room) {
2301c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
23029b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
23039b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
23049b6b563cSPaul Mackerras 		}
23059b6b563cSPaul Mackerras 		namep[l] = '\0';
23069b6b563cSPaul Mackerras 
23079b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2308a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2309a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
23109b6b563cSPaul Mackerras 		 */
2311a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
23129b6b563cSPaul Mackerras 			if (*p == '/')
2313a575b807SPaul Mackerras 				lp = namep;
2314a575b807SPaul Mackerras 			else if (*p != 0)
2315a575b807SPaul Mackerras 				*lp++ = *p;
2316a575b807SPaul Mackerras 		}
2317a575b807SPaul Mackerras 		*lp = 0;
2318a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
23199b6b563cSPaul Mackerras 	}
23209b6b563cSPaul Mackerras 
23219b6b563cSPaul Mackerras 	/* get it again for debugging */
23225827d416SAnton Blanchard 	path = prom_scratch;
23239b6b563cSPaul Mackerras 	memset(path, 0, PROM_SCRATCH_SIZE);
23249b6b563cSPaul Mackerras 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
23259b6b563cSPaul Mackerras 
23269b6b563cSPaul Mackerras 	/* get and store all properties */
23275827d416SAnton Blanchard 	prev_name = "";
23285827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
23299b6b563cSPaul Mackerras 	for (;;) {
23309b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
23315827d416SAnton Blanchard 			      pname) != 1)
23329b6b563cSPaul Mackerras 			break;
23339b6b563cSPaul Mackerras 
23349b6b563cSPaul Mackerras  		/* skip "name" */
23355827d416SAnton Blanchard  		if (strcmp(pname, "name") == 0) {
23365827d416SAnton Blanchard  			prev_name = "name";
23379b6b563cSPaul Mackerras  			continue;
23389b6b563cSPaul Mackerras  		}
23399b6b563cSPaul Mackerras 
23409b6b563cSPaul Mackerras 		/* find string offset */
23415827d416SAnton Blanchard 		soff = dt_find_string(pname);
23429b6b563cSPaul Mackerras 		if (soff == 0) {
23439b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
23445827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
23459b6b563cSPaul Mackerras 			break;
23469b6b563cSPaul Mackerras 		}
23479b6b563cSPaul Mackerras 		prev_name = sstart + soff;
23489b6b563cSPaul Mackerras 
23499b6b563cSPaul Mackerras 		/* get length */
23505827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
23519b6b563cSPaul Mackerras 
23529b6b563cSPaul Mackerras 		/* sanity checks */
23539b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
23549b6b563cSPaul Mackerras 			continue;
23559b6b563cSPaul Mackerras 
23569b6b563cSPaul Mackerras 		/* push property head */
23579b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
23589b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
23599b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
23609b6b563cSPaul Mackerras 
23619b6b563cSPaul Mackerras 		/* push property content */
23629b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
23635827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
23649b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
23656e35d5daSBenjamin Herrenschmidt 
23665827d416SAnton Blanchard 		if (!strcmp(pname, "phandle"))
23676e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
23689b6b563cSPaul Mackerras 	}
23699b6b563cSPaul Mackerras 
23706e35d5daSBenjamin Herrenschmidt 	/* Add a "linux,phandle" property if no "phandle" property already
23716e35d5daSBenjamin Herrenschmidt 	 * existed (can happen with OPAL)
23726e35d5daSBenjamin Herrenschmidt 	 */
23736e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
23745827d416SAnton Blanchard 		soff = dt_find_string("linux,phandle");
23759b6b563cSPaul Mackerras 		if (soff == 0)
23769b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
23779b6b563cSPaul Mackerras 				    " <linux-phandle> node %s\n", path);
23789b6b563cSPaul Mackerras 		else {
23799b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
23809b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
23819b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
23829b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
2383493adffcSBenjamin Herrenschmidt 			*(__be32 *)valp = cpu_to_be32(node);
23849b6b563cSPaul Mackerras 		}
23856e35d5daSBenjamin Herrenschmidt 	}
23869b6b563cSPaul Mackerras 
23879b6b563cSPaul Mackerras 	/* do all our children */
23889b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
23899b6b563cSPaul Mackerras 	while (child != 0) {
23909b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
23919b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
23929b6b563cSPaul Mackerras 	}
23939b6b563cSPaul Mackerras 
23949b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
23959b6b563cSPaul Mackerras }
23969b6b563cSPaul Mackerras 
23979b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
23989b6b563cSPaul Mackerras {
23999b6b563cSPaul Mackerras 	phandle root;
24009b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
24019b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
24029b6b563cSPaul Mackerras 	char *namep;
24039b6b563cSPaul Mackerras 	u64 *rsvmap;
24049b6b563cSPaul Mackerras 
24059b6b563cSPaul Mackerras 	/*
24069b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2407fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
24089b6b563cSPaul Mackerras 	 */
24095827d416SAnton Blanchard 	room = alloc_top - alloc_bottom - 0x4000;
24109b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
24119b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
24125827d416SAnton Blanchard 	prom_debug("starting device tree allocs at %x\n", alloc_bottom);
24139b6b563cSPaul Mackerras 
24149b6b563cSPaul Mackerras 	/* Now try to claim that */
24159b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
24169b6b563cSPaul Mackerras 	if (mem_start == 0)
24179b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2418966728ddSAnton Blanchard 	mem_end = mem_start + room;
24199b6b563cSPaul Mackerras 
24209b6b563cSPaul Mackerras 	/* Get root of tree */
24219b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
24229b6b563cSPaul Mackerras 	if (root == (phandle)0)
24239b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
24249b6b563cSPaul Mackerras 
24259b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
24269b6b563cSPaul Mackerras 	mem_start = _ALIGN(mem_start, 4);
24279b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
24289b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
24295827d416SAnton Blanchard 	dt_header_start = (unsigned long)hdr;
24309b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
24319b6b563cSPaul Mackerras 
24329b6b563cSPaul Mackerras 	/* Start of strings */
24339b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
24345827d416SAnton Blanchard 	dt_string_start = mem_start;
24359b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
24369b6b563cSPaul Mackerras 
24379b6b563cSPaul Mackerras 	/* Add "linux,phandle" in there, we'll need it */
24389b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
24395827d416SAnton Blanchard 	strcpy(namep, "linux,phandle");
24409b6b563cSPaul Mackerras 	mem_start = (unsigned long)namep + strlen(namep) + 1;
24419b6b563cSPaul Mackerras 
24429b6b563cSPaul Mackerras 	/* Build string array */
24439b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
24449b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
24455827d416SAnton Blanchard 	dt_string_end = mem_start;
24469b6b563cSPaul Mackerras 
24479b6b563cSPaul Mackerras 	/* Build structure */
24489b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
24495827d416SAnton Blanchard 	dt_struct_start = mem_start;
24509b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
24519b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
24529b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
24535827d416SAnton Blanchard 	dt_struct_end = PAGE_ALIGN(mem_start);
24549b6b563cSPaul Mackerras 
24559b6b563cSPaul Mackerras 	/* Finish header */
2456493adffcSBenjamin Herrenschmidt 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2457493adffcSBenjamin Herrenschmidt 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2458493adffcSBenjamin Herrenschmidt 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2459493adffcSBenjamin Herrenschmidt 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2460493adffcSBenjamin Herrenschmidt 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2461493adffcSBenjamin Herrenschmidt 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2462493adffcSBenjamin Herrenschmidt 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2463493adffcSBenjamin Herrenschmidt 	hdr->version = cpu_to_be32(OF_DT_VERSION);
24649b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
2465493adffcSBenjamin Herrenschmidt 	hdr->last_comp_version = cpu_to_be32(0x10);
24669b6b563cSPaul Mackerras 
24674d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
24685827d416SAnton Blanchard 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
24699b6b563cSPaul Mackerras 
24709b6b563cSPaul Mackerras #ifdef DEBUG_PROM
24719b6b563cSPaul Mackerras 	{
24729b6b563cSPaul Mackerras 		int i;
24739b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
24745827d416SAnton Blanchard 		for (i = 0; i < mem_reserve_cnt; i++)
24759b6b563cSPaul Mackerras 			prom_printf("  %x - %x\n",
2476493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].base),
2477493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].size));
24789b6b563cSPaul Mackerras 	}
24799b6b563cSPaul Mackerras #endif
24804d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
24814d1f3f25SJimi Xenidis 	 * since it's too late.
24824d1f3f25SJimi Xenidis 	 */
24835827d416SAnton Blanchard 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
24849b6b563cSPaul Mackerras 
24859b6b563cSPaul Mackerras 	prom_printf("Device tree strings 0x%x -> 0x%x\n",
24865827d416SAnton Blanchard 		    dt_string_start, dt_string_end);
24879b6b563cSPaul Mackerras 	prom_printf("Device tree struct  0x%x -> 0x%x\n",
24885827d416SAnton Blanchard 		    dt_struct_start, dt_struct_end);
24899b6b563cSPaul Mackerras }
24909b6b563cSPaul Mackerras 
249154f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
249254f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
249354f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
249454f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
24959b6b563cSPaul Mackerras {
249654f4ee18SHollis Blanchard 	phandle isa;
2497980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
249854f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2499980a6513SBenjamin Herrenschmidt 	char *name;
250054f4ee18SHollis Blanchard 
2501980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2502980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2503980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2504980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2505980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2506980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2507980a6513SBenjamin Herrenschmidt 	}
250854f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
250954f4ee18SHollis Blanchard 		return;
251054f4ee18SHollis Blanchard 
2511980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2512980a6513SBenjamin Herrenschmidt 		return;
251354f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
251454f4ee18SHollis Blanchard 		== PROM_ERROR)
251554f4ee18SHollis Blanchard 		return;
251654f4ee18SHollis Blanchard 
251754f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
251854f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
251954f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
252054f4ee18SHollis Blanchard 		return;
252154f4ee18SHollis Blanchard 
2522980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
252354f4ee18SHollis Blanchard 
252454f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
252554f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2526980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
252754f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
252854f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
252954f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2530980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
253154f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
253254f4ee18SHollis Blanchard }
25338f101a05SHarry Ciao 
25348f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
25358f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
25368f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
25378f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
25388f101a05SHarry Ciao {
25398f101a05SHarry Ciao 	phandle mc;
25408f101a05SHarry Ciao 	u32 mc_reg[4];
25418f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
25428f101a05SHarry Ciao 	u32 ac, sc;
25438f101a05SHarry Ciao 
25448f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
25458f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
25468f101a05SHarry Ciao 		return;
25478f101a05SHarry Ciao 
25488f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
25498f101a05SHarry Ciao 		return;
25508f101a05SHarry Ciao 
25515827d416SAnton Blanchard 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
25525827d416SAnton Blanchard 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
25538f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
25548f101a05SHarry Ciao 		return;
25558f101a05SHarry Ciao 
25568f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
25578f101a05SHarry Ciao 		return;
25588f101a05SHarry Ciao 
25598f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
25608f101a05SHarry Ciao 		return;
25618f101a05SHarry Ciao 
25628f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
25638f101a05SHarry Ciao 
25648f101a05SHarry Ciao 	mc_reg[0] = 0x0;
25658f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
25668f101a05SHarry Ciao 	mc_reg[2] = 0x0;
25678f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
25688f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
25698f101a05SHarry Ciao }
257054f4ee18SHollis Blanchard #else
257154f4ee18SHollis Blanchard #define fixup_device_tree_maple()
25728f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
257354f4ee18SHollis Blanchard #endif
257454f4ee18SHollis Blanchard 
2575e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2576e4805922SOlaf Hering /*
2577e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2578e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2579556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2580e4805922SOlaf Hering  */
2581e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2582e8c0acf9SBenjamin Herrenschmidt {
2583e4805922SOlaf Hering 	phandle ph;
2584e4805922SOlaf Hering 	u32 prop[6];
258526c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2586e8c0acf9SBenjamin Herrenschmidt 	char *name;
2587e8c0acf9SBenjamin Herrenschmidt 	int rc;
2588e8c0acf9SBenjamin Herrenschmidt 
2589e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2590e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2591e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
259226c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2593e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
259426c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
259526c5032eSBenjamin Herrenschmidt 	}
2596e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2597e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2598e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2599e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2600e8c0acf9SBenjamin Herrenschmidt 
2601e4805922SOlaf Hering 			prop[0] = 0x1;
2602e4805922SOlaf Hering 			prop[1] = 0x0;
2603e4805922SOlaf Hering 			prop[2] = rloc;
2604e4805922SOlaf Hering 			prop[3] = 0x0;
2605e4805922SOlaf Hering 			prop[4] = 0x0;
2606e4805922SOlaf Hering 			prop[5] = 0x00010000;
2607e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2608e4805922SOlaf Hering 		}
2609e4805922SOlaf Hering 	}
2610e4805922SOlaf Hering 
2611e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2612e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2613e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2614e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2615e4805922SOlaf Hering 		prop[0] = 14;
2616e4805922SOlaf Hering 		prop[1] = 0x0;
2617556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2618556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2619556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2620556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2621556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2622556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2623556ecf9bSOlaf Hering 		}
2624e4805922SOlaf Hering 	}
2625e8c0acf9SBenjamin Herrenschmidt }
2626e8c0acf9SBenjamin Herrenschmidt #else
2627e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2628e8c0acf9SBenjamin Herrenschmidt #endif
2629e8c0acf9SBenjamin Herrenschmidt 
26309b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
263154f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
263254f4ee18SHollis Blanchard {
26339b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
26349b6b563cSPaul Mackerras 	u32 u3_rev;
26359b6b563cSPaul Mackerras 	u32 interrupts[2];
26369b6b563cSPaul Mackerras 	u32 parent;
26379b6b563cSPaul Mackerras 
26389b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
26399b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
26409b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
26419b6b563cSPaul Mackerras 		return;
26429b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
26439b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
26449b6b563cSPaul Mackerras 		return;
26459b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
26469b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
26479b6b563cSPaul Mackerras 		return;
26489b6b563cSPaul Mackerras 
26499b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
26509b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
26519b6b563cSPaul Mackerras 	    == PROM_ERROR)
26529b6b563cSPaul Mackerras 		return;
26537d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
26549b6b563cSPaul Mackerras 		return;
26559b6b563cSPaul Mackerras 	/* does it need fixup ? */
26569b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
26579b6b563cSPaul Mackerras 		return;
26589b6b563cSPaul Mackerras 
26599b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
26609b6b563cSPaul Mackerras 
26619b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
26629b6b563cSPaul Mackerras 	interrupts[0] = 0;
26639b6b563cSPaul Mackerras 	interrupts[1] = 1;
2664a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2665a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
26669b6b563cSPaul Mackerras 	parent = (u32)mpic;
2667a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2668a23414beSPaul Mackerras 		     &parent, sizeof(parent));
26699b6b563cSPaul Mackerras }
267054f4ee18SHollis Blanchard #else
267154f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
267254f4ee18SHollis Blanchard #endif
26739b6b563cSPaul Mackerras 
267488fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
267594d2dde7SGrant Likely /*
267694d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
267794d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
267894d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
267994d2dde7SGrant Likely  * ethernet node.
268094d2dde7SGrant Likely  */
268194d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
268288fd2a9dSSylvain Munaut {
268388fd2a9dSSylvain Munaut 	u32 node;
268488fd2a9dSSylvain Munaut 	char prop[64];
268594d2dde7SGrant Likely 	int rv;
268688fd2a9dSSylvain Munaut 
268794d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
268894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
268988fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
269088fd2a9dSSylvain Munaut 		return;
269188fd2a9dSSylvain Munaut 
269294d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
269394d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
269494d2dde7SGrant Likely 	if (!rv)
269588fd2a9dSSylvain Munaut 		return;
269688fd2a9dSSylvain Munaut 
269794d2dde7SGrant Likely 	/*
269894d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
269994d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
270094d2dde7SGrant Likely 	 */
270188fd2a9dSSylvain Munaut 
270294d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
27036f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
27046f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
27056f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
27066f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
27076f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
27086f4347c9SOlaf Hering 			" new-device"
27096f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
27106f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
271194d2dde7SGrant Likely 				" s\" mdio\" device-name"
271294d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
27136f4347c9SOlaf Hering 				" s\" compatible\" property"
27146f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
27156f4347c9SOlaf Hering 				" 0x2 encode-int"
27166f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
27176f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
27186f4347c9SOlaf Hering 				" s\" interrupts\" property"
27196f4347c9SOlaf Hering 			" finish-device");
27206f4347c9SOlaf Hering 	};
27216f4347c9SOlaf Hering 
272294d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
272394d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
272494d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
272594d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
27266f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
27276f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
27286f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
27296f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
27306f4347c9SOlaf Hering 			" new-device"
27316f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
27326f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
27336f4347c9SOlaf Hering 				" my-self"
27346f4347c9SOlaf Hering 				" ihandle>phandle"
27356f4347c9SOlaf Hering 			" finish-device"
27366f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
27376f4347c9SOlaf Hering 				" encode-int"
27386f4347c9SOlaf Hering 				" s\" phy-handle\" property"
27396f4347c9SOlaf Hering 			" device-end");
27406f4347c9SOlaf Hering 	}
274194d2dde7SGrant Likely }
27426f4347c9SOlaf Hering 
274394d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
274494d2dde7SGrant Likely {
274594d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
274694d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
274794d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
274894d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
274994d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
275094d2dde7SGrant Likely 	u32 node;
275194d2dde7SGrant Likely 	char prop[64];
275294d2dde7SGrant Likely 	int rv, len;
275394d2dde7SGrant Likely 
275494d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
275594d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
275694d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
275794d2dde7SGrant Likely 		return;
275894d2dde7SGrant Likely 
275994d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
276094d2dde7SGrant Likely 	if (rv == PROM_ERROR)
276194d2dde7SGrant Likely 		return;
276294d2dde7SGrant Likely 	if (strcmp(prop, "EFIKA5K2"))
276394d2dde7SGrant Likely 		return;
276494d2dde7SGrant Likely 
276594d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
276694d2dde7SGrant Likely 
276794d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
276894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
276994d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
277094d2dde7SGrant Likely 	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
277194d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
277294d2dde7SGrant Likely 
27737f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
27747f4392cdSDavid Woodhouse 	   fix that too */
27757f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
27767f4392cdSDavid Woodhouse 	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
27777f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
27787f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
27797f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
27807f4392cdSDavid Woodhouse 
278194d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
278294d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
278394d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
278494d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
278594d2dde7SGrant Likely 		if (len == 12) {
278694d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
278794d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
278894d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
278994d2dde7SGrant Likely 		}
279094d2dde7SGrant Likely 	}
279194d2dde7SGrant Likely 
279294d2dde7SGrant Likely 	/* Fixup sound interrupts property */
279394d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
279494d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
279594d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
279694d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
279794d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
279894d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
279994d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
280094d2dde7SGrant Likely 		}
280194d2dde7SGrant Likely 	}
280294d2dde7SGrant Likely 
280394d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
280494d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
280588fd2a9dSSylvain Munaut }
280688fd2a9dSSylvain Munaut #else
280788fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
280888fd2a9dSSylvain Munaut #endif
280988fd2a9dSSylvain Munaut 
281054f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
281154f4ee18SHollis Blanchard {
281254f4ee18SHollis Blanchard 	fixup_device_tree_maple();
28138f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
2814e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
281554f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
281688fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
281754f4ee18SHollis Blanchard }
28189b6b563cSPaul Mackerras 
28199b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
28209b6b563cSPaul Mackerras {
2821493adffcSBenjamin Herrenschmidt 	__be32 rval;
28229b6b563cSPaul Mackerras 	ihandle prom_cpu;
28239b6b563cSPaul Mackerras 	phandle cpu_pkg;
28249b6b563cSPaul Mackerras 
2825493adffcSBenjamin Herrenschmidt 	rval = 0;
2826493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
2827a575b807SPaul Mackerras 		return;
2828493adffcSBenjamin Herrenschmidt 	prom_cpu = be32_to_cpu(rval);
28299b6b563cSPaul Mackerras 
28309b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
28319b6b563cSPaul Mackerras 
2832493adffcSBenjamin Herrenschmidt 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
2833493adffcSBenjamin Herrenschmidt 	prom.cpu = be32_to_cpu(rval);
28349b6b563cSPaul Mackerras 
28355827d416SAnton Blanchard 	prom_debug("Booting CPU hw index = %lu\n", prom.cpu);
28369b6b563cSPaul Mackerras }
28379b6b563cSPaul Mackerras 
28389b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
28399b6b563cSPaul Mackerras {
28409b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
28419b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
2842493adffcSBenjamin Herrenschmidt 		__be64 val;
28439b6b563cSPaul Mackerras 
28445827d416SAnton Blanchard 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
28455827d416SAnton Blanchard 		prom_initrd_end = prom_initrd_start + r4;
28469b6b563cSPaul Mackerras 
2847493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_start);
28485827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
2849a23414beSPaul Mackerras 			     &val, sizeof(val));
2850493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_end);
28515827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
2852a23414beSPaul Mackerras 			     &val, sizeof(val));
28539b6b563cSPaul Mackerras 
28545827d416SAnton Blanchard 		reserve_mem(prom_initrd_start,
28555827d416SAnton Blanchard 			    prom_initrd_end - prom_initrd_start);
28569b6b563cSPaul Mackerras 
28575827d416SAnton Blanchard 		prom_debug("initrd_start=0x%x\n", prom_initrd_start);
28585827d416SAnton Blanchard 		prom_debug("initrd_end=0x%x\n", prom_initrd_end);
28599b6b563cSPaul Mackerras 	}
28609b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
28619b6b563cSPaul Mackerras }
28629b6b563cSPaul Mackerras 
28635ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64
28645ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE
28655ac47f7aSAnton Blanchard static void reloc_toc(void)
28665ac47f7aSAnton Blanchard {
28675ac47f7aSAnton Blanchard }
28685ac47f7aSAnton Blanchard 
28695ac47f7aSAnton Blanchard static void unreloc_toc(void)
28705ac47f7aSAnton Blanchard {
28715ac47f7aSAnton Blanchard }
28725ac47f7aSAnton Blanchard #else
28731674400aSAnton Blanchard static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
28745ac47f7aSAnton Blanchard {
28755ac47f7aSAnton Blanchard 	unsigned long i;
28761674400aSAnton Blanchard 	unsigned long *toc_entry;
28771674400aSAnton Blanchard 
28781674400aSAnton Blanchard 	/* Get the start of the TOC by using r2 directly. */
28791674400aSAnton Blanchard 	asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
28805ac47f7aSAnton Blanchard 
28815ac47f7aSAnton Blanchard 	for (i = 0; i < nr_entries; i++) {
28825ac47f7aSAnton Blanchard 		*toc_entry = *toc_entry + offset;
28835ac47f7aSAnton Blanchard 		toc_entry++;
28845ac47f7aSAnton Blanchard 	}
28855ac47f7aSAnton Blanchard }
28865ac47f7aSAnton Blanchard 
28875ac47f7aSAnton Blanchard static void reloc_toc(void)
28885ac47f7aSAnton Blanchard {
28895ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
28905ac47f7aSAnton Blanchard 	unsigned long nr_entries =
28915ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
28925ac47f7aSAnton Blanchard 
28931674400aSAnton Blanchard 	__reloc_toc(offset, nr_entries);
28945ac47f7aSAnton Blanchard 
28955ac47f7aSAnton Blanchard 	mb();
28965ac47f7aSAnton Blanchard }
28975ac47f7aSAnton Blanchard 
28985ac47f7aSAnton Blanchard static void unreloc_toc(void)
28995ac47f7aSAnton Blanchard {
29005ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
29015ac47f7aSAnton Blanchard 	unsigned long nr_entries =
29025ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
29035ac47f7aSAnton Blanchard 
29045ac47f7aSAnton Blanchard 	mb();
29055ac47f7aSAnton Blanchard 
29061674400aSAnton Blanchard 	__reloc_toc(-offset, nr_entries);
29075ac47f7aSAnton Blanchard }
29085ac47f7aSAnton Blanchard #endif
29095ac47f7aSAnton Blanchard #endif
291027f44888SBenjamin Herrenschmidt 
29119b6b563cSPaul Mackerras /*
29129b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
29139b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
29149b6b563cSPaul Mackerras  */
29159b6b563cSPaul Mackerras 
29169b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
29179b6b563cSPaul Mackerras 			       unsigned long pp,
2918549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
2919549e8152SPaul Mackerras 			       unsigned long kbase)
29209b6b563cSPaul Mackerras {
29219b6b563cSPaul Mackerras 	unsigned long hdr;
29229b6b563cSPaul Mackerras 
29239b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
2924549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
29259b6b563cSPaul Mackerras 	reloc_got2(offset);
29265ac47f7aSAnton Blanchard #else
29275ac47f7aSAnton Blanchard 	reloc_toc();
29289b6b563cSPaul Mackerras #endif
29299b6b563cSPaul Mackerras 
29309b6b563cSPaul Mackerras 	/*
29319b6b563cSPaul Mackerras 	 * First zero the BSS
29329b6b563cSPaul Mackerras 	 */
29335827d416SAnton Blanchard 	memset(&__bss_start, 0, __bss_stop - __bss_start);
29349b6b563cSPaul Mackerras 
29359b6b563cSPaul Mackerras 	/*
29369b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
29379b6b563cSPaul Mackerras 	 * like /chosen
29389b6b563cSPaul Mackerras 	 */
29399b6b563cSPaul Mackerras 	prom_init_client_services(pp);
29409b6b563cSPaul Mackerras 
29419b6b563cSPaul Mackerras 	/*
2942a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
2943a23414beSPaul Mackerras 	 * and other workarounds
2944a23414beSPaul Mackerras 	 */
2945a23414beSPaul Mackerras 	prom_find_mmu();
2946a23414beSPaul Mackerras 
2947a23414beSPaul Mackerras 	/*
29489b6b563cSPaul Mackerras 	 * Init prom stdout device
29499b6b563cSPaul Mackerras 	 */
29509b6b563cSPaul Mackerras 	prom_init_stdout();
29519b6b563cSPaul Mackerras 
29525827d416SAnton Blanchard 	prom_printf("Preparing to boot %s", linux_banner);
2953e7943fbbSMichael Ellerman 
29549b6b563cSPaul Mackerras 	/*
29559b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
29569b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
29579b6b563cSPaul Mackerras 	 */
29585827d416SAnton Blanchard 	of_platform = prom_find_machine_type();
29595827d416SAnton Blanchard 	prom_printf("Detected machine type: %x\n", of_platform);
29609b6b563cSPaul Mackerras 
29610f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
2962add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
2963add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
2964add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
2965549e8152SPaul Mackerras #endif
2966add60ef3SOlaf Hering 
2967add60ef3SOlaf Hering 	/*
2968add60ef3SOlaf Hering 	 * Check for an initrd
2969add60ef3SOlaf Hering 	 */
2970add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
2971add60ef3SOlaf Hering 
297227f44888SBenjamin Herrenschmidt #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
29739b6b563cSPaul Mackerras 	/*
29749b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
29759b6b563cSPaul Mackerras 	 */
29765827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES ||
29775827d416SAnton Blanchard 	    of_platform == PLATFORM_PSERIES_LPAR)
29789b6b563cSPaul Mackerras 		prom_send_capabilities();
29799b6b563cSPaul Mackerras #endif
29809b6b563cSPaul Mackerras 
29819b6b563cSPaul Mackerras 	/*
2982f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
29839b6b563cSPaul Mackerras 	 */
29845827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC)
2985549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
29869b6b563cSPaul Mackerras 
29879b6b563cSPaul Mackerras 	/*
29889b6b563cSPaul Mackerras 	 * Do early parsing of command line
29899b6b563cSPaul Mackerras 	 */
29909b6b563cSPaul Mackerras 	early_cmdline_parse();
29919b6b563cSPaul Mackerras 
29929b6b563cSPaul Mackerras 	/*
29939b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
29949b6b563cSPaul Mackerras 	 */
29959b6b563cSPaul Mackerras 	prom_init_mem();
29969b6b563cSPaul Mackerras 
29979b6b563cSPaul Mackerras 	/*
29989b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
29999b6b563cSPaul Mackerras 	 */
30009b6b563cSPaul Mackerras 	prom_find_boot_cpu();
30019b6b563cSPaul Mackerras 
30029b6b563cSPaul Mackerras 	/*
30039b6b563cSPaul Mackerras 	 * Initialize display devices
30049b6b563cSPaul Mackerras 	 */
30059b6b563cSPaul Mackerras 	prom_check_displays();
30069b6b563cSPaul Mackerras 
3007493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
30089b6b563cSPaul Mackerras 	/*
30099b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
30109b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
30119b6b563cSPaul Mackerras 	 * available for us here...
30129b6b563cSPaul Mackerras 	 */
30135827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES)
30149b6b563cSPaul Mackerras 		prom_initialize_tce_table();
30159b6b563cSPaul Mackerras #endif
30169b6b563cSPaul Mackerras 
30179b6b563cSPaul Mackerras 	/*
301827f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
301927f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
30209b6b563cSPaul Mackerras 	 */
30215827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
30225827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
30239b6b563cSPaul Mackerras 		prom_instantiate_rtas();
302427f44888SBenjamin Herrenschmidt 
302527f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
3026493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
302727f44888SBenjamin Herrenschmidt 	/* Detect HAL and try instanciating it & doing takeover */
30285827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES_LPAR) {
302927f44888SBenjamin Herrenschmidt 		prom_query_opal();
30305827d416SAnton Blanchard 		if (of_platform == PLATFORM_OPAL) {
303127f44888SBenjamin Herrenschmidt 			prom_opal_hold_cpus();
303227f44888SBenjamin Herrenschmidt 			prom_opal_takeover();
30339b6b563cSPaul Mackerras 		}
3034493adffcSBenjamin Herrenschmidt 	} else
3035493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
3036493adffcSBenjamin Herrenschmidt 	if (of_platform == PLATFORM_OPAL)
30376e35d5daSBenjamin Herrenschmidt 		prom_instantiate_opal();
3038493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV */
303927f44888SBenjamin Herrenschmidt 
30404a727429SAshley Lai #ifdef CONFIG_PPC64
30414a727429SAshley Lai 	/* instantiate sml */
30424a727429SAshley Lai 	prom_instantiate_sml();
30434a727429SAshley Lai #endif
30444a727429SAshley Lai 
304527f44888SBenjamin Herrenschmidt 	/*
304627f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
304727f44888SBenjamin Herrenschmidt 	 *
304827f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
3049dbe78b40SBenjamin Herrenschmidt 	 *
3050dbe78b40SBenjamin Herrenschmidt 	 * (This must be done after instanciating RTAS)
305127f44888SBenjamin Herrenschmidt 	 */
30525827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
30535827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
305427f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
30559b6b563cSPaul Mackerras 
30569b6b563cSPaul Mackerras 	/*
30579b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
30589b6b563cSPaul Mackerras 	 */
3059493adffcSBenjamin Herrenschmidt 	if (prom_memory_limit) {
3060493adffcSBenjamin Herrenschmidt 		__be64 val = cpu_to_be64(prom_memory_limit);
30615827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3062493adffcSBenjamin Herrenschmidt 			     &val, sizeof(val));
3063493adffcSBenjamin Herrenschmidt 	}
30649b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
30655827d416SAnton Blanchard 	if (prom_iommu_off)
30665827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3067a23414beSPaul Mackerras 			     NULL, 0);
30689b6b563cSPaul Mackerras 
30695827d416SAnton Blanchard 	if (prom_iommu_force_on)
30705827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3071a23414beSPaul Mackerras 			     NULL, 0);
30729b6b563cSPaul Mackerras 
30735827d416SAnton Blanchard 	if (prom_tce_alloc_start) {
30745827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
30755827d416SAnton Blanchard 			     &prom_tce_alloc_start,
30769b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
30775827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
30785827d416SAnton Blanchard 			     &prom_tce_alloc_end,
30799b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
30809b6b563cSPaul Mackerras 	}
30819b6b563cSPaul Mackerras #endif
30829b6b563cSPaul Mackerras 
30839b6b563cSPaul Mackerras 	/*
30849b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
30859b6b563cSPaul Mackerras 	 */
30869b6b563cSPaul Mackerras 	fixup_device_tree();
30879b6b563cSPaul Mackerras 
30889b6b563cSPaul Mackerras 	/*
30899b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
30909b6b563cSPaul Mackerras 	 */
30919b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
30929b6b563cSPaul Mackerras 	flatten_device_tree();
30939b6b563cSPaul Mackerras 
30943825ac0eSPaul Mackerras 	/*
30953825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
30963825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
309740dfef66SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101). It
309840dfef66SBenjamin Herrenschmidt 	 * appears that the OPAL version of OFW doesn't like it either.
30993825ac0eSPaul Mackerras 	 */
31005827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
31015827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
31029b6b563cSPaul Mackerras 		prom_close_stdin();
31039b6b563cSPaul Mackerras 
31049b6b563cSPaul Mackerras 	/*
31059b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
31069b6b563cSPaul Mackerras 	 * devices etc...
31079b6b563cSPaul Mackerras 	 */
31089b6b563cSPaul Mackerras 	prom_printf("Calling quiesce...\n");
31099b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
31109b6b563cSPaul Mackerras 
31119b6b563cSPaul Mackerras 	/*
31129b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
31139b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
31149b6b563cSPaul Mackerras 	 * is common to us and kexec
31159b6b563cSPaul Mackerras 	 */
31165827d416SAnton Blanchard 	hdr = dt_header_start;
311740dfef66SBenjamin Herrenschmidt 
311840dfef66SBenjamin Herrenschmidt 	/* Don't print anything after quiesce under OPAL, it crashes OFW */
31195827d416SAnton Blanchard 	if (of_platform != PLATFORM_OPAL) {
31209b6b563cSPaul Mackerras 		prom_printf("returning from prom_init\n");
31219b6b563cSPaul Mackerras 		prom_debug("->dt_header_start=0x%x\n", hdr);
312240dfef66SBenjamin Herrenschmidt 	}
31239b6b563cSPaul Mackerras 
31249b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
31259b6b563cSPaul Mackerras 	reloc_got2(-offset);
31265ac47f7aSAnton Blanchard #else
31275ac47f7aSAnton Blanchard 	unreloc_toc();
31289b6b563cSPaul Mackerras #endif
31299b6b563cSPaul Mackerras 
31306e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
31316e35d5daSBenjamin Herrenschmidt 	/* OPAL early debug gets the OPAL base & entry in r8 and r9 */
31326e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0,
31335827d416SAnton Blanchard 		prom_opal_base, prom_opal_entry);
31346e35d5daSBenjamin Herrenschmidt #else
31356e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
31366e35d5daSBenjamin Herrenschmidt #endif
31379b6b563cSPaul Mackerras 
31389b6b563cSPaul Mackerras 	return 0;
31399b6b563cSPaul Mackerras }
3140