xref: /linux/arch/powerpc/kernel/prom_init.c (revision 7d70c63c7132eb95e428e945242360233ab23d82)
19b6b563cSPaul Mackerras /*
29b6b563cSPaul Mackerras  * Procedures for interfacing to Open Firmware.
39b6b563cSPaul Mackerras  *
49b6b563cSPaul Mackerras  * Paul Mackerras	August 1996.
59b6b563cSPaul Mackerras  * Copyright (C) 1996-2005 Paul Mackerras.
69b6b563cSPaul Mackerras  *
79b6b563cSPaul Mackerras  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
89b6b563cSPaul Mackerras  *    {engebret|bergner}@us.ibm.com
99b6b563cSPaul Mackerras  *
109b6b563cSPaul Mackerras  *      This program is free software; you can redistribute it and/or
119b6b563cSPaul Mackerras  *      modify it under the terms of the GNU General Public License
129b6b563cSPaul Mackerras  *      as published by the Free Software Foundation; either version
139b6b563cSPaul Mackerras  *      2 of the License, or (at your option) any later version.
149b6b563cSPaul Mackerras  */
159b6b563cSPaul Mackerras 
169b6b563cSPaul Mackerras #undef DEBUG_PROM
179b6b563cSPaul Mackerras 
189b6b563cSPaul Mackerras #include <stdarg.h>
199b6b563cSPaul Mackerras #include <linux/kernel.h>
209b6b563cSPaul Mackerras #include <linux/string.h>
219b6b563cSPaul Mackerras #include <linux/init.h>
229b6b563cSPaul Mackerras #include <linux/threads.h>
239b6b563cSPaul Mackerras #include <linux/spinlock.h>
249b6b563cSPaul Mackerras #include <linux/types.h>
259b6b563cSPaul Mackerras #include <linux/pci.h>
269b6b563cSPaul Mackerras #include <linux/proc_fs.h>
279b6b563cSPaul Mackerras #include <linux/stringify.h>
289b6b563cSPaul Mackerras #include <linux/delay.h>
299b6b563cSPaul Mackerras #include <linux/initrd.h>
309b6b563cSPaul Mackerras #include <linux/bitops.h>
319b6b563cSPaul Mackerras #include <asm/prom.h>
329b6b563cSPaul Mackerras #include <asm/rtas.h>
339b6b563cSPaul Mackerras #include <asm/page.h>
349b6b563cSPaul Mackerras #include <asm/processor.h>
359b6b563cSPaul Mackerras #include <asm/irq.h>
369b6b563cSPaul Mackerras #include <asm/io.h>
379b6b563cSPaul Mackerras #include <asm/smp.h>
389b6b563cSPaul Mackerras #include <asm/mmu.h>
399b6b563cSPaul Mackerras #include <asm/pgtable.h>
409b6b563cSPaul Mackerras #include <asm/iommu.h>
419b6b563cSPaul Mackerras #include <asm/btext.h>
429b6b563cSPaul Mackerras #include <asm/sections.h>
439b6b563cSPaul Mackerras #include <asm/machdep.h>
4427f44888SBenjamin Herrenschmidt #include <asm/opal.h>
459b6b563cSPaul Mackerras 
469b6b563cSPaul Mackerras #include <linux/linux_logo.h>
479b6b563cSPaul Mackerras 
489b6b563cSPaul Mackerras /*
499b6b563cSPaul Mackerras  * Eventually bump that one up
509b6b563cSPaul Mackerras  */
519b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE	0x100000
529b6b563cSPaul Mackerras 
539b6b563cSPaul Mackerras /*
549b6b563cSPaul Mackerras  * This is the size of the local memory reserve map that gets copied
559b6b563cSPaul Mackerras  * into the boot params passed to the kernel. That size is totally
569b6b563cSPaul Mackerras  * flexible as the kernel just reads the list until it encounters an
579b6b563cSPaul Mackerras  * entry with size 0, so it can be changed without breaking binary
589b6b563cSPaul Mackerras  * compatibility
599b6b563cSPaul Mackerras  */
609b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE	8
619b6b563cSPaul Mackerras 
629b6b563cSPaul Mackerras /*
639b6b563cSPaul Mackerras  * prom_init() is called very early on, before the kernel text
649b6b563cSPaul Mackerras  * and data have been mapped to KERNELBASE.  At this point the code
659b6b563cSPaul Mackerras  * is running at whatever address it has been loaded at.
669b6b563cSPaul Mackerras  * On ppc32 we compile with -mrelocatable, which means that references
679b6b563cSPaul Mackerras  * to extern and static variables get relocated automatically.
685ac47f7aSAnton Blanchard  * ppc64 objects are always relocatable, we just need to relocate the
695ac47f7aSAnton Blanchard  * TOC.
709b6b563cSPaul Mackerras  *
719b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
729b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
739b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
749b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
759b6b563cSPaul Mackerras  *
769b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
779b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
789b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
799b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
809b6b563cSPaul Mackerras  */
815ac47f7aSAnton Blanchard #define ADDR(x)		(u32)(unsigned long)(x)
825ac47f7aSAnton Blanchard 
839b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
84a23414beSPaul Mackerras #define OF_WORKAROUNDS	0
859b6b563cSPaul Mackerras #else
86a23414beSPaul Mackerras #define OF_WORKAROUNDS	of_workarounds
87a23414beSPaul Mackerras int of_workarounds;
889b6b563cSPaul Mackerras #endif
899b6b563cSPaul Mackerras 
90a23414beSPaul Mackerras #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
91a23414beSPaul Mackerras #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
92a23414beSPaul Mackerras 
939b6b563cSPaul Mackerras #define PROM_BUG() do {						\
949b6b563cSPaul Mackerras         prom_printf("kernel BUG at %s line 0x%x!\n",		\
955827d416SAnton Blanchard 		    __FILE__, __LINE__);			\
969b6b563cSPaul Mackerras         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
979b6b563cSPaul Mackerras } while (0)
989b6b563cSPaul Mackerras 
999b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1009b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1019b6b563cSPaul Mackerras #else
1029b6b563cSPaul Mackerras #define prom_debug(x...)
1039b6b563cSPaul Mackerras #endif
1049b6b563cSPaul Mackerras 
1059b6b563cSPaul Mackerras 
1069b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1079b6b563cSPaul Mackerras 
1089b6b563cSPaul Mackerras struct prom_args {
109493adffcSBenjamin Herrenschmidt         __be32 service;
110493adffcSBenjamin Herrenschmidt         __be32 nargs;
111493adffcSBenjamin Herrenschmidt         __be32 nret;
112493adffcSBenjamin Herrenschmidt         __be32 args[10];
1139b6b563cSPaul Mackerras };
1149b6b563cSPaul Mackerras 
1159b6b563cSPaul Mackerras struct prom_t {
1169b6b563cSPaul Mackerras 	ihandle root;
117a23414beSPaul Mackerras 	phandle chosen;
1189b6b563cSPaul Mackerras 	int cpu;
1199b6b563cSPaul Mackerras 	ihandle stdout;
120a575b807SPaul Mackerras 	ihandle mmumap;
121a23414beSPaul Mackerras 	ihandle memory;
1229b6b563cSPaul Mackerras };
1239b6b563cSPaul Mackerras 
1249b6b563cSPaul Mackerras struct mem_map_entry {
125493adffcSBenjamin Herrenschmidt 	__be64	base;
126493adffcSBenjamin Herrenschmidt 	__be64	size;
1279b6b563cSPaul Mackerras };
1289b6b563cSPaul Mackerras 
129493adffcSBenjamin Herrenschmidt typedef __be32 cell_t;
1309b6b563cSPaul Mackerras 
1316e35d5daSBenjamin Herrenschmidt extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
1326e35d5daSBenjamin Herrenschmidt 		    unsigned long r6, unsigned long r7, unsigned long r8,
1336e35d5daSBenjamin Herrenschmidt 		    unsigned long r9);
1349b6b563cSPaul Mackerras 
1359b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
136c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1379b6b563cSPaul Mackerras #else
138c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1399b6b563cSPaul Mackerras {
140c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1419b6b563cSPaul Mackerras }
1429b6b563cSPaul Mackerras #endif
1439b6b563cSPaul Mackerras 
1449b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1459b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1469b6b563cSPaul Mackerras 
1479b6b563cSPaul Mackerras /* prom structure */
1489b6b563cSPaul Mackerras static struct prom_t __initdata prom;
1499b6b563cSPaul Mackerras 
1509b6b563cSPaul Mackerras static unsigned long prom_entry __initdata;
1519b6b563cSPaul Mackerras 
1529b6b563cSPaul Mackerras #define PROM_SCRATCH_SIZE 256
1539b6b563cSPaul Mackerras 
1549b6b563cSPaul Mackerras static char __initdata of_stdout_device[256];
1559b6b563cSPaul Mackerras static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
1569b6b563cSPaul Mackerras 
1579b6b563cSPaul Mackerras static unsigned long __initdata dt_header_start;
1589b6b563cSPaul Mackerras static unsigned long __initdata dt_struct_start, dt_struct_end;
1599b6b563cSPaul Mackerras static unsigned long __initdata dt_string_start, dt_string_end;
1609b6b563cSPaul Mackerras 
1619b6b563cSPaul Mackerras static unsigned long __initdata prom_initrd_start, prom_initrd_end;
1629b6b563cSPaul Mackerras 
1639b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
164165785e5SJeremy Kerr static int __initdata prom_iommu_force_on;
165165785e5SJeremy Kerr static int __initdata prom_iommu_off;
1669b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_start;
1679b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_end;
1689b6b563cSPaul Mackerras #endif
1699b6b563cSPaul Mackerras 
170e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
171e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
172e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
173e8222502SBenjamin Herrenschmidt  */
174e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
175e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
176e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
177e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
178e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
17927f44888SBenjamin Herrenschmidt #define PLATFORM_OPAL		0x0600
180e8222502SBenjamin Herrenschmidt 
1819b6b563cSPaul Mackerras static int __initdata of_platform;
1829b6b563cSPaul Mackerras 
1839b6b563cSPaul Mackerras static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
1849b6b563cSPaul Mackerras 
185cf68787bSBenjamin Krill static unsigned long __initdata prom_memory_limit;
186cf68787bSBenjamin Krill 
1879b6b563cSPaul Mackerras static unsigned long __initdata alloc_top;
1889b6b563cSPaul Mackerras static unsigned long __initdata alloc_top_high;
1899b6b563cSPaul Mackerras static unsigned long __initdata alloc_bottom;
1909b6b563cSPaul Mackerras static unsigned long __initdata rmo_top;
1919b6b563cSPaul Mackerras static unsigned long __initdata ram_top;
1929b6b563cSPaul Mackerras 
1939b6b563cSPaul Mackerras static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
1949b6b563cSPaul Mackerras static int __initdata mem_reserve_cnt;
1959b6b563cSPaul Mackerras 
1969b6b563cSPaul Mackerras static cell_t __initdata regbuf[1024];
1979b6b563cSPaul Mackerras 
198dbe78b40SBenjamin Herrenschmidt static bool rtas_has_query_cpu_stopped;
199dbe78b40SBenjamin Herrenschmidt 
2009b6b563cSPaul Mackerras 
2019b6b563cSPaul Mackerras /*
2029b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2039b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2049b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2059b6b563cSPaul Mackerras  * it is valid
2069b6b563cSPaul Mackerras  */
2079b6b563cSPaul Mackerras 
2089b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2099b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2109b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2119b6b563cSPaul Mackerras 
2129b6b563cSPaul Mackerras 
2139b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
2149b6b563cSPaul Mackerras  * firmware.
2159b6b563cSPaul Mackerras  */
2169b6b563cSPaul Mackerras 
2179b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
2189b6b563cSPaul Mackerras {
2199b6b563cSPaul Mackerras 	int i;
2209b6b563cSPaul Mackerras 	struct prom_args args;
2219b6b563cSPaul Mackerras 	va_list list;
2229b6b563cSPaul Mackerras 
223493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
224493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
225493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
2269b6b563cSPaul Mackerras 
2279b6b563cSPaul Mackerras 	va_start(list, nret);
2289b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
229493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
2309b6b563cSPaul Mackerras 	va_end(list);
2319b6b563cSPaul Mackerras 
2329b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
2339b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
2349b6b563cSPaul Mackerras 
2355827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
236c4988820SPaul Mackerras 		return PROM_ERROR;
2379b6b563cSPaul Mackerras 
238493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
2399b6b563cSPaul Mackerras }
2409b6b563cSPaul Mackerras 
2419b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
2429b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
2439b6b563cSPaul Mackerras {
2449b6b563cSPaul Mackerras 	int i;
2459b6b563cSPaul Mackerras 	struct prom_args args;
2469b6b563cSPaul Mackerras 	va_list list;
2479b6b563cSPaul Mackerras 
248493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
249493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
250493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
2519b6b563cSPaul Mackerras 
2529b6b563cSPaul Mackerras 	va_start(list, rets);
2539b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
254493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
2559b6b563cSPaul Mackerras 	va_end(list);
2569b6b563cSPaul Mackerras 
2579b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
258ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
2599b6b563cSPaul Mackerras 
2605827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
261c4988820SPaul Mackerras 		return PROM_ERROR;
2629b6b563cSPaul Mackerras 
2639b6b563cSPaul Mackerras 	if (rets != NULL)
2649b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
265493adffcSBenjamin Herrenschmidt 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
2669b6b563cSPaul Mackerras 
267493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
2689b6b563cSPaul Mackerras }
2699b6b563cSPaul Mackerras 
2709b6b563cSPaul Mackerras 
2719b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
2729b6b563cSPaul Mackerras {
2739b6b563cSPaul Mackerras 	const char *p, *q;
2749b6b563cSPaul Mackerras 
2755827d416SAnton Blanchard 	if (prom.stdout == 0)
2769b6b563cSPaul Mackerras 		return;
2779b6b563cSPaul Mackerras 
2789b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
2799b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
2809b6b563cSPaul Mackerras 			;
2819b6b563cSPaul Mackerras 		if (q > p)
2825827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
2839b6b563cSPaul Mackerras 		if (*q == 0)
2849b6b563cSPaul Mackerras 			break;
2859b6b563cSPaul Mackerras 		++q;
2865827d416SAnton Blanchard 		call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
2879b6b563cSPaul Mackerras 	}
2889b6b563cSPaul Mackerras }
2899b6b563cSPaul Mackerras 
2909b6b563cSPaul Mackerras 
2919b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
2929b6b563cSPaul Mackerras {
2939b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
2949b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
2959b6b563cSPaul Mackerras 
2969b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
2979b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
2989b6b563cSPaul Mackerras 		if (buf[i] > '9')
2999b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
3009b6b563cSPaul Mackerras 		val >>= 4;
3019b6b563cSPaul Mackerras 	}
3029b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
3035827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf, nibbles);
3049b6b563cSPaul Mackerras }
3059b6b563cSPaul Mackerras 
3062c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */
3072c48a7d6SMichael Neuling #define UL_DIGITS 21
3082c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val)
3092c48a7d6SMichael Neuling {
3102c48a7d6SMichael Neuling 	int i, size;
3112c48a7d6SMichael Neuling 	char buf[UL_DIGITS+1];
3122c48a7d6SMichael Neuling 
3132c48a7d6SMichael Neuling 	for (i = UL_DIGITS-1; i >= 0;  i--) {
3142c48a7d6SMichael Neuling 		buf[i] = (val % 10) + '0';
3152c48a7d6SMichael Neuling 		val = val/10;
3162c48a7d6SMichael Neuling 		if (val == 0)
3172c48a7d6SMichael Neuling 			break;
3182c48a7d6SMichael Neuling 	}
3192c48a7d6SMichael Neuling 	/* shift stuff down */
3202c48a7d6SMichael Neuling 	size = UL_DIGITS - i;
3215827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf+i, size);
3222c48a7d6SMichael Neuling }
3239b6b563cSPaul Mackerras 
3249b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
3259b6b563cSPaul Mackerras {
3269b6b563cSPaul Mackerras 	const char *p, *q, *s;
3279b6b563cSPaul Mackerras 	va_list args;
3289b6b563cSPaul Mackerras 	unsigned long v;
329af277149SBenjamin Herrenschmidt 	long vs;
3309b6b563cSPaul Mackerras 
3319b6b563cSPaul Mackerras 	va_start(args, format);
3329b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
3339b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
3349b6b563cSPaul Mackerras 			;
3359b6b563cSPaul Mackerras 		if (q > p)
3365827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
3379b6b563cSPaul Mackerras 		if (*q == 0)
3389b6b563cSPaul Mackerras 			break;
3399b6b563cSPaul Mackerras 		if (*q == '\n') {
3409b6b563cSPaul Mackerras 			++q;
3415827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout,
3429b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
3439b6b563cSPaul Mackerras 			continue;
3449b6b563cSPaul Mackerras 		}
3459b6b563cSPaul Mackerras 		++q;
3469b6b563cSPaul Mackerras 		if (*q == 0)
3479b6b563cSPaul Mackerras 			break;
3489b6b563cSPaul Mackerras 		switch (*q) {
3499b6b563cSPaul Mackerras 		case 's':
3509b6b563cSPaul Mackerras 			++q;
3519b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
3529b6b563cSPaul Mackerras 			prom_print(s);
3539b6b563cSPaul Mackerras 			break;
3549b6b563cSPaul Mackerras 		case 'x':
3559b6b563cSPaul Mackerras 			++q;
3569b6b563cSPaul Mackerras 			v = va_arg(args, unsigned long);
3579b6b563cSPaul Mackerras 			prom_print_hex(v);
3589b6b563cSPaul Mackerras 			break;
359af277149SBenjamin Herrenschmidt 		case 'd':
360af277149SBenjamin Herrenschmidt 			++q;
361af277149SBenjamin Herrenschmidt 			vs = va_arg(args, int);
362af277149SBenjamin Herrenschmidt 			if (vs < 0) {
3635827d416SAnton Blanchard 				prom_print("-");
364af277149SBenjamin Herrenschmidt 				vs = -vs;
365af277149SBenjamin Herrenschmidt 			}
366af277149SBenjamin Herrenschmidt 			prom_print_dec(vs);
367af277149SBenjamin Herrenschmidt 			break;
3682c48a7d6SMichael Neuling 		case 'l':
3692c48a7d6SMichael Neuling 			++q;
370af277149SBenjamin Herrenschmidt 			if (*q == 0)
371af277149SBenjamin Herrenschmidt 				break;
372af277149SBenjamin Herrenschmidt 			else if (*q == 'x') {
373af277149SBenjamin Herrenschmidt 				++q;
374af277149SBenjamin Herrenschmidt 				v = va_arg(args, unsigned long);
375af277149SBenjamin Herrenschmidt 				prom_print_hex(v);
376af277149SBenjamin Herrenschmidt 			} else if (*q == 'u') { /* '%lu' */
3772c48a7d6SMichael Neuling 				++q;
3782c48a7d6SMichael Neuling 				v = va_arg(args, unsigned long);
3792c48a7d6SMichael Neuling 				prom_print_dec(v);
380af277149SBenjamin Herrenschmidt 			} else if (*q == 'd') { /* %ld */
381af277149SBenjamin Herrenschmidt 				++q;
382af277149SBenjamin Herrenschmidt 				vs = va_arg(args, long);
383af277149SBenjamin Herrenschmidt 				if (vs < 0) {
3845827d416SAnton Blanchard 					prom_print("-");
385af277149SBenjamin Herrenschmidt 					vs = -vs;
386af277149SBenjamin Herrenschmidt 				}
387af277149SBenjamin Herrenschmidt 				prom_print_dec(vs);
3882c48a7d6SMichael Neuling 			}
3892c48a7d6SMichael Neuling 			break;
3909b6b563cSPaul Mackerras 		}
3919b6b563cSPaul Mackerras 	}
3921b855e16SDaniel Axtens 	va_end(args);
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 
645e8a4fd0aSMichael Ellerman /* Firmware expects the value to be n - 1, where n is the # of vectors */
646e8a4fd0aSMichael Ellerman #define NUM_VECTORS(n)		((n) - 1)
647e8a4fd0aSMichael Ellerman 
648e8a4fd0aSMichael Ellerman /*
649e8a4fd0aSMichael Ellerman  * Firmware expects 1 + n - 2, where n is the length of the option vector in
650e8a4fd0aSMichael Ellerman  * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
651e8a4fd0aSMichael Ellerman  */
652e8a4fd0aSMichael Ellerman #define VECTOR_LENGTH(n)	(1 + (n) - 2)
653e8a4fd0aSMichael Ellerman 
654530b5e14SNathan Fontenot unsigned char ibm_architecture_vec[] = {
655f709bfacSPaul Mackerras 	W(0xfffe0000), W(0x003a0000),	/* POWER5/POWER5+ */
65603054d51SAnton Blanchard 	W(0xffff0000), W(0x003e0000),	/* POWER6 */
657e952e6c4SMichael Neuling 	W(0xffff0000), W(0x003f0000),	/* POWER7 */
65833959f88SMichael Neuling 	W(0xffff0000), W(0x004b0000),	/* POWER8E */
6597cc85103SThomas Huth 	W(0xffff0000), W(0x004c0000),   /* POWER8NVL */
66033959f88SMichael Neuling 	W(0xffff0000), W(0x004d0000),	/* POWER8 */
661df77c799SMichael Neuling 	W(0xffffffff), W(0x0f000004),	/* all 2.07-compliant */
6620cb99013SJoel Schopp 	W(0xffffffff), W(0x0f000003),	/* all 2.06-compliant */
6630efbc18aSPaul Mackerras 	W(0xffffffff), W(0x0f000002),	/* all 2.05-compliant */
664f709bfacSPaul Mackerras 	W(0xfffffffe), W(0x0f000001),	/* all 2.04-compliant and earlier */
665e8a4fd0aSMichael Ellerman 	NUM_VECTORS(6),			/* 6 option vectors */
666f709bfacSPaul Mackerras 
667f709bfacSPaul Mackerras 	/* option vector 1: processor architectures supported */
668e8a4fd0aSMichael Ellerman 	VECTOR_LENGTH(2),		/* length */
669f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
670f709bfacSPaul Mackerras 	OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
671df77c799SMichael Neuling 	OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
672f709bfacSPaul Mackerras 
673f709bfacSPaul Mackerras 	/* option vector 2: Open Firmware options supported */
674e8a4fd0aSMichael Ellerman 	VECTOR_LENGTH(33),		/* length */
675f709bfacSPaul Mackerras 	OV2_REAL_MODE,
676f709bfacSPaul Mackerras 	0, 0,
677f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_base */
678f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_size */
679f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_base */
680f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_size */
681f709bfacSPaul Mackerras 	W(0xffffffff),			/* load_base */
68233392640SAnton Blanchard 	W(256),				/* 256MB min RMA */
683f709bfacSPaul Mackerras 	W(0xffffffff),			/* full client load */
684f709bfacSPaul Mackerras 	0,				/* min RMA percentage of total RAM */
685f709bfacSPaul Mackerras 	48,				/* max log_2(hash table size) */
686f709bfacSPaul Mackerras 
687f709bfacSPaul Mackerras 	/* option vector 3: processor options supported */
688e8a4fd0aSMichael Ellerman 	VECTOR_LENGTH(2),		/* length */
689f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
690974a76f5SPaul Mackerras 	OV3_FP | OV3_VMX | OV3_DFP,
691f709bfacSPaul Mackerras 
692f709bfacSPaul Mackerras 	/* option vector 4: IBM PAPR implementation */
693e8a4fd0aSMichael Ellerman 	VECTOR_LENGTH(2),		/* length */
694f709bfacSPaul Mackerras 	0,				/* don't halt */
695404e32e4SRobert Jennings 	OV4_MIN_ENT_CAP,		/* minimum VP entitled capacity */
696f709bfacSPaul Mackerras 
697f709bfacSPaul Mackerras 	/* option vector 5: PAPR/OF options */
698e8a4fd0aSMichael Ellerman 	VECTOR_LENGTH(18),		/* length */
699f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
700f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
701f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
702f0ff7eb4SNathan Fontenot #ifdef CONFIG_PCI_MSI
703f0ff7eb4SNathan Fontenot 	/* PCIe/MSI support.  Without MSI full PCIe is not supported */
704f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_MSI),
705f0ff7eb4SNathan Fontenot #else
7068391e42aSNathan Fontenot 	0,
707f0ff7eb4SNathan Fontenot #endif
708f0ff7eb4SNathan Fontenot 	0,
709f0ff7eb4SNathan Fontenot #ifdef CONFIG_PPC_SMLPAR
710f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
711f0ff7eb4SNathan Fontenot #else
712f0ff7eb4SNathan Fontenot 	0,
713f0ff7eb4SNathan Fontenot #endif
7141b1218d3SNathan Fontenot 	OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
71528bb9ee1Sjschopp@austin.ibm.com 	0,
71628bb9ee1Sjschopp@austin.ibm.com 	0,
71728bb9ee1Sjschopp@austin.ibm.com 	0,
718efec959fSBenjamin Herrenschmidt 	/* WARNING: The offset of the "number of cores" field below
719efec959fSBenjamin Herrenschmidt 	 * must match by the macro below. Update the definition if
720efec959fSBenjamin Herrenschmidt 	 * the structure layout changes.
721efec959fSBenjamin Herrenschmidt 	 */
7222c2a63e3SMichael Ellerman #define IBM_ARCH_VEC_NRCORES_OFFSET	133
72328bb9ee1Sjschopp@austin.ibm.com 	W(NR_CPUS),			/* number of cores supported */
724828d2b59SKent Yoder 	0,
725828d2b59SKent Yoder 	0,
726828d2b59SKent Yoder 	0,
727828d2b59SKent Yoder 	0,
728f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) |
729f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_PFO_HW_842),
730f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_SUB_PROCESSORS),
731e8a4fd0aSMichael Ellerman 
73228bb9ee1Sjschopp@austin.ibm.com 	/* option vector 6: IBM PAPR hints */
733e8a4fd0aSMichael Ellerman 	VECTOR_LENGTH(3),		/* length */
73428bb9ee1Sjschopp@austin.ibm.com 	0,
73528bb9ee1Sjschopp@austin.ibm.com 	0,
73628bb9ee1Sjschopp@austin.ibm.com 	OV6_LINUX,
737f709bfacSPaul Mackerras };
738f709bfacSPaul Mackerras 
739493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
740493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
7419b6b563cSPaul Mackerras static struct fake_elf {
7429b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
7439b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
7449b6b563cSPaul Mackerras 	struct chrpnote {
7459b6b563cSPaul Mackerras 		u32	namesz;
7469b6b563cSPaul Mackerras 		u32	descsz;
7479b6b563cSPaul Mackerras 		u32	type;
7489b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
7499b6b563cSPaul Mackerras 		struct chrpdesc {
7509b6b563cSPaul Mackerras 			u32	real_mode;
7519b6b563cSPaul Mackerras 			u32	real_base;
7529b6b563cSPaul Mackerras 			u32	real_size;
7539b6b563cSPaul Mackerras 			u32	virt_base;
7549b6b563cSPaul Mackerras 			u32	virt_size;
7559b6b563cSPaul Mackerras 			u32	load_base;
7569b6b563cSPaul Mackerras 		} chrpdesc;
7579b6b563cSPaul Mackerras 	} chrpnote;
7589b6b563cSPaul Mackerras 	struct rpanote {
7599b6b563cSPaul Mackerras 		u32	namesz;
7609b6b563cSPaul Mackerras 		u32	descsz;
7619b6b563cSPaul Mackerras 		u32	type;
7629b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
7639b6b563cSPaul Mackerras 		struct rpadesc {
7649b6b563cSPaul Mackerras 			u32	lpar_affinity;
7659b6b563cSPaul Mackerras 			u32	min_rmo_size;
7669b6b563cSPaul Mackerras 			u32	min_rmo_percent;
7679b6b563cSPaul Mackerras 			u32	max_pft_size;
7689b6b563cSPaul Mackerras 			u32	splpar;
7699b6b563cSPaul Mackerras 			u32	min_load;
7709b6b563cSPaul Mackerras 			u32	new_mem_def;
7719b6b563cSPaul Mackerras 			u32	ignore_me;
7729b6b563cSPaul Mackerras 		} rpadesc;
7739b6b563cSPaul Mackerras 	} rpanote;
7745663a123SPaul Mackerras } fake_elf = {
7759b6b563cSPaul Mackerras 	.elfhdr = {
7769b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
7779b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
7789b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
7799b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
7809b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
7819b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
7829b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
7839b6b563cSPaul Mackerras 		.e_phnum = 2
7849b6b563cSPaul Mackerras 	},
7859b6b563cSPaul Mackerras 	.phdr = {
7869b6b563cSPaul Mackerras 		[0] = {
7879b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
7889b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
7899b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
7909b6b563cSPaul Mackerras 		}, [1] = {
7919b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
7929b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
7939b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
7949b6b563cSPaul Mackerras 		}
7959b6b563cSPaul Mackerras 	},
7969b6b563cSPaul Mackerras 	.chrpnote = {
7979b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
7989b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
7999b6b563cSPaul Mackerras 		.type = 0x1275,
8009b6b563cSPaul Mackerras 		.name = "PowerPC",
8019b6b563cSPaul Mackerras 		.chrpdesc = {
8029b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
8039b6b563cSPaul Mackerras 			.real_base = ~0U,
8049b6b563cSPaul Mackerras 			.real_size = ~0U,
8059b6b563cSPaul Mackerras 			.virt_base = ~0U,
8069b6b563cSPaul Mackerras 			.virt_size = ~0U,
8079b6b563cSPaul Mackerras 			.load_base = ~0U
8089b6b563cSPaul Mackerras 		},
8099b6b563cSPaul Mackerras 	},
8109b6b563cSPaul Mackerras 	.rpanote = {
8119b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
8129b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
8139b6b563cSPaul Mackerras 		.type = 0x12759999,
8149b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
8159b6b563cSPaul Mackerras 		.rpadesc = {
8165663a123SPaul Mackerras 			.lpar_affinity = 0,
8175663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
8189b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
8195663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
8209b6b563cSPaul Mackerras 			.splpar = 1,
8219b6b563cSPaul Mackerras 			.min_load = ~0U,
8225663a123SPaul Mackerras 			.new_mem_def = 0
8239b6b563cSPaul Mackerras 		}
8249b6b563cSPaul Mackerras 	}
8259b6b563cSPaul Mackerras };
826493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
8279b6b563cSPaul Mackerras 
828efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
829efec959fSBenjamin Herrenschmidt {
830efec959fSBenjamin Herrenschmidt 	phandle node;
831efec959fSBenjamin Herrenschmidt 	char type[64];
832efec959fSBenjamin Herrenschmidt 	unsigned int plen;
833efec959fSBenjamin Herrenschmidt 
834efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
835efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
836efec959fSBenjamin Herrenschmidt 		type[0] = 0;
837efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
838efec959fSBenjamin Herrenschmidt 
8395827d416SAnton Blanchard 		if (strcmp(type, "cpu"))
840efec959fSBenjamin Herrenschmidt 			continue;
841efec959fSBenjamin Herrenschmidt 		/*
842efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
843efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
844efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
845efec959fSBenjamin Herrenschmidt 		 */
846efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
847efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
848efec959fSBenjamin Herrenschmidt 			break;
849efec959fSBenjamin Herrenschmidt 		plen >>= 2;
8502c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
851efec959fSBenjamin Herrenschmidt 
852efec959fSBenjamin Herrenschmidt 		/* Sanity check */
853efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
8542c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
855efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
856efec959fSBenjamin Herrenschmidt 			return 1;
857efec959fSBenjamin Herrenschmidt 		}
858efec959fSBenjamin Herrenschmidt 		return plen;
859efec959fSBenjamin Herrenschmidt 	}
860efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
861efec959fSBenjamin Herrenschmidt 
862efec959fSBenjamin Herrenschmidt 	return 1;
863efec959fSBenjamin Herrenschmidt 
864efec959fSBenjamin Herrenschmidt }
865efec959fSBenjamin Herrenschmidt 
866efec959fSBenjamin Herrenschmidt 
8679b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
8689b6b563cSPaul Mackerras {
869493adffcSBenjamin Herrenschmidt 	ihandle root;
870f709bfacSPaul Mackerras 	prom_arg_t ret;
871dbd0c5d5SLaurent Dufour 	u32 cores;
872dbd0c5d5SLaurent Dufour 	unsigned char *ptcores;
8739b6b563cSPaul Mackerras 
874f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
875f709bfacSPaul Mackerras 	if (root != 0) {
876efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
877efec959fSBenjamin Herrenschmidt 		 *
878efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
879efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
880efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
881efec959fSBenjamin Herrenschmidt 		 */
882dbd0c5d5SLaurent Dufour 
883dbd0c5d5SLaurent Dufour 		/* The core value may start at an odd address. If such a word
884dbd0c5d5SLaurent Dufour 		 * access is made at a cache line boundary, this leads to an
885dbd0c5d5SLaurent Dufour 		 * exception which may not be handled at this time.
886dbd0c5d5SLaurent Dufour 		 * Forcing a per byte access to avoid exception.
887dbd0c5d5SLaurent Dufour 		 */
888dbd0c5d5SLaurent Dufour 		ptcores = &ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET];
889dbd0c5d5SLaurent Dufour 		cores = 0;
890dbd0c5d5SLaurent Dufour 		cores |= ptcores[0] << 24;
891dbd0c5d5SLaurent Dufour 		cores |= ptcores[1] << 16;
892dbd0c5d5SLaurent Dufour 		cores |= ptcores[2] << 8;
893dbd0c5d5SLaurent Dufour 		cores |= ptcores[3];
894dbd0c5d5SLaurent Dufour 		if (cores != NR_CPUS) {
895efec959fSBenjamin Herrenschmidt 			prom_printf("WARNING ! "
8962c48a7d6SMichael Neuling 				    "ibm_architecture_vec structure inconsistent: %lu!\n",
897dbd0c5d5SLaurent Dufour 				    cores);
898efec959fSBenjamin Herrenschmidt 		} else {
899dbd0c5d5SLaurent Dufour 			cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
9002c48a7d6SMichael Neuling 			prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
901dbd0c5d5SLaurent Dufour 				    cores, NR_CPUS);
902dbd0c5d5SLaurent Dufour 			ptcores[0] = (cores >> 24) & 0xff;
903dbd0c5d5SLaurent Dufour 			ptcores[1] = (cores >> 16) & 0xff;
904dbd0c5d5SLaurent Dufour 			ptcores[2] = (cores >> 8) & 0xff;
905dbd0c5d5SLaurent Dufour 			ptcores[3] = cores & 0xff;
906efec959fSBenjamin Herrenschmidt 		}
907efec959fSBenjamin Herrenschmidt 
908f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
909049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
910f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
911f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
91233b74977SBenjamin Herrenschmidt 				  root,
913f709bfacSPaul Mackerras 				  ADDR(ibm_architecture_vec)) == 0) {
914f709bfacSPaul Mackerras 			/* the call exists... */
915f709bfacSPaul Mackerras 			if (ret)
9164da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
917f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
918f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
9194da727aeSAnton Blanchard 			prom_printf(" done\n");
920f709bfacSPaul Mackerras 			return;
921f709bfacSPaul Mackerras 		}
922f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
923049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
924f709bfacSPaul Mackerras 	}
925f709bfacSPaul Mackerras 
926493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
927493adffcSBenjamin Herrenschmidt 	{
928493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
929493adffcSBenjamin Herrenschmidt 
930f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
931493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
932493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
9339b6b563cSPaul Mackerras 		if (elfloader == 0) {
9349b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
9359b6b563cSPaul Mackerras 			return;
9369b6b563cSPaul Mackerras 		}
9379b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
9389b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
9399b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
9409b6b563cSPaul Mackerras 	}
941493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
942493adffcSBenjamin Herrenschmidt }
943493adffcSBenjamin Herrenschmidt #endif /* #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV) */
9449b6b563cSPaul Mackerras 
9459b6b563cSPaul Mackerras /*
9469b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
9479b6b563cSPaul Mackerras  *
9489b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
9499b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
9509b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
9519b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
9529b6b563cSPaul Mackerras  *  never happen.
9539b6b563cSPaul Mackerras  *
9549b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
9559b6b563cSPaul Mackerras  *  TCEs overlap
9569b6b563cSPaul Mackerras  *
9579b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
9589b6b563cSPaul Mackerras  *
9599b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
9609b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
9619b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
9629b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
9639b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
9649b6b563cSPaul Mackerras  *
9659b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
9669b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
9679b6b563cSPaul Mackerras  *  reserved.
9689b6b563cSPaul Mackerras  */
9699b6b563cSPaul Mackerras 
9709b6b563cSPaul Mackerras 
9719b6b563cSPaul Mackerras /*
9729b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
9739b6b563cSPaul Mackerras  *
9749b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
9759b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
9769b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
9779b6b563cSPaul Mackerras  */
9789b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
9799b6b563cSPaul Mackerras {
9805827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
9819b6b563cSPaul Mackerras 	unsigned long addr = 0;
9829b6b563cSPaul Mackerras 
983c4988820SPaul Mackerras 	if (align)
984c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
9859b6b563cSPaul Mackerras 	prom_debug("alloc_up(%x, %x)\n", size, align);
9865827d416SAnton Blanchard 	if (ram_top == 0)
9879b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
9889b6b563cSPaul Mackerras 
9899b6b563cSPaul Mackerras 	if (align)
9905827d416SAnton Blanchard 		base = _ALIGN_UP(alloc_bottom, align);
9919b6b563cSPaul Mackerras 	else
9925827d416SAnton Blanchard 		base = alloc_bottom;
9939b6b563cSPaul Mackerras 
9945827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
9959b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
9969b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
9979b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
998c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
9999b6b563cSPaul Mackerras 			break;
10009b6b563cSPaul Mackerras 		addr = 0;
10019b6b563cSPaul Mackerras 		if (align == 0)
10029b6b563cSPaul Mackerras 			break;
10039b6b563cSPaul Mackerras 	}
10049b6b563cSPaul Mackerras 	if (addr == 0)
10059b6b563cSPaul Mackerras 		return 0;
10065827d416SAnton Blanchard 	alloc_bottom = addr + size;
10079b6b563cSPaul Mackerras 
10089b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
10095827d416SAnton Blanchard 	prom_debug("  alloc_bottom : %x\n", alloc_bottom);
10105827d416SAnton Blanchard 	prom_debug("  alloc_top    : %x\n", alloc_top);
10115827d416SAnton Blanchard 	prom_debug("  alloc_top_hi : %x\n", alloc_top_high);
10125827d416SAnton Blanchard 	prom_debug("  rmo_top      : %x\n", rmo_top);
10135827d416SAnton Blanchard 	prom_debug("  ram_top      : %x\n", ram_top);
10149b6b563cSPaul Mackerras 
10159b6b563cSPaul Mackerras 	return addr;
10169b6b563cSPaul Mackerras }
10179b6b563cSPaul Mackerras 
10189b6b563cSPaul Mackerras /*
10199b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
10209b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
10219b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
10229b6b563cSPaul Mackerras  */
10239b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
10249b6b563cSPaul Mackerras 				       int highmem)
10259b6b563cSPaul Mackerras {
10269b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
10279b6b563cSPaul Mackerras 
10289b6b563cSPaul Mackerras 	prom_debug("alloc_down(%x, %x, %s)\n", size, align,
10295827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
10305827d416SAnton Blanchard 	if (ram_top == 0)
10319b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
10329b6b563cSPaul Mackerras 
10339b6b563cSPaul Mackerras 	if (highmem) {
10349b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
10355827d416SAnton Blanchard 		addr = _ALIGN_DOWN(alloc_top_high - size, align);
10365827d416SAnton Blanchard 		if (addr <= alloc_bottom)
10379b6b563cSPaul Mackerras 			return 0;
10389b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
10399b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
10409b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
10419b6b563cSPaul Mackerras 		 */
10425827d416SAnton Blanchard 		if (addr < rmo_top) {
10439b6b563cSPaul Mackerras 			/* Good, we are first */
10445827d416SAnton Blanchard 			if (alloc_top == rmo_top)
10455827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
10469b6b563cSPaul Mackerras 			else
10479b6b563cSPaul Mackerras 				return 0;
10489b6b563cSPaul Mackerras 		}
10495827d416SAnton Blanchard 		alloc_top_high = addr;
10509b6b563cSPaul Mackerras 		goto bail;
10519b6b563cSPaul Mackerras 	}
10529b6b563cSPaul Mackerras 
10535827d416SAnton Blanchard 	base = _ALIGN_DOWN(alloc_top - size, align);
10545827d416SAnton Blanchard 	for (; base > alloc_bottom;
10559b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
10569b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
10579b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1058c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
10599b6b563cSPaul Mackerras 			break;
10609b6b563cSPaul Mackerras 		addr = 0;
10619b6b563cSPaul Mackerras 	}
10629b6b563cSPaul Mackerras 	if (addr == 0)
10639b6b563cSPaul Mackerras 		return 0;
10645827d416SAnton Blanchard 	alloc_top = addr;
10659b6b563cSPaul Mackerras 
10669b6b563cSPaul Mackerras  bail:
10679b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
10685827d416SAnton Blanchard 	prom_debug("  alloc_bottom : %x\n", alloc_bottom);
10695827d416SAnton Blanchard 	prom_debug("  alloc_top    : %x\n", alloc_top);
10705827d416SAnton Blanchard 	prom_debug("  alloc_top_hi : %x\n", alloc_top_high);
10715827d416SAnton Blanchard 	prom_debug("  rmo_top      : %x\n", rmo_top);
10725827d416SAnton Blanchard 	prom_debug("  ram_top      : %x\n", ram_top);
10739b6b563cSPaul Mackerras 
10749b6b563cSPaul Mackerras 	return addr;
10759b6b563cSPaul Mackerras }
10769b6b563cSPaul Mackerras 
10779b6b563cSPaul Mackerras /*
10789b6b563cSPaul Mackerras  * Parse a "reg" cell
10799b6b563cSPaul Mackerras  */
10809b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
10819b6b563cSPaul Mackerras {
10829b6b563cSPaul Mackerras 	cell_t *p = *cellp;
10839b6b563cSPaul Mackerras 	unsigned long r = 0;
10849b6b563cSPaul Mackerras 
10859b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
10869b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
10879b6b563cSPaul Mackerras 		p++;
10889b6b563cSPaul Mackerras 		s--;
10899b6b563cSPaul Mackerras 	}
1090493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
10919b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
109235499c01SPaul Mackerras 	if (s > 1) {
10939b6b563cSPaul Mackerras 		r <<= 32;
1094493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
10959b6b563cSPaul Mackerras 	}
10969b6b563cSPaul Mackerras #endif
10979b6b563cSPaul Mackerras 	*cellp = p;
10989b6b563cSPaul Mackerras 	return r;
10999b6b563cSPaul Mackerras }
11009b6b563cSPaul Mackerras 
11019b6b563cSPaul Mackerras /*
11029b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
11039b6b563cSPaul Mackerras  * we don't need anything smarter at this point
11049b6b563cSPaul Mackerras  *
11059b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
11069b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
11079b6b563cSPaul Mackerras  * them down.
11089b6b563cSPaul Mackerras  */
11090108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
11109b6b563cSPaul Mackerras {
1111cbbcf340SKumar Gala 	u64 top = base + size;
11125827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
11139b6b563cSPaul Mackerras 
11149b6b563cSPaul Mackerras 	if (size == 0)
11159b6b563cSPaul Mackerras 		return;
11169b6b563cSPaul Mackerras 
11179b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
11189b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
11199b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
11209b6b563cSPaul Mackerras 	 */
11219b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
11229b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
11239b6b563cSPaul Mackerras 	size = top - base;
11249b6b563cSPaul Mackerras 
11259b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
11269b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1127493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1128493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
11295827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
11309b6b563cSPaul Mackerras }
11319b6b563cSPaul Mackerras 
11329b6b563cSPaul Mackerras /*
1133b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
11349b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
11359b6b563cSPaul Mackerras  */
11369b6b563cSPaul Mackerras static void __init prom_init_mem(void)
11379b6b563cSPaul Mackerras {
11389b6b563cSPaul Mackerras 	phandle node;
11399b6b563cSPaul Mackerras 	char *path, type[64];
11409b6b563cSPaul Mackerras 	unsigned int plen;
11419b6b563cSPaul Mackerras 	cell_t *p, *endp;
1142493adffcSBenjamin Herrenschmidt 	__be32 val;
11439b6b563cSPaul Mackerras 	u32 rac, rsc;
11449b6b563cSPaul Mackerras 
11459b6b563cSPaul Mackerras 	/*
11469b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
11479b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
11489b6b563cSPaul Mackerras 	 * 2) top of memory
11499b6b563cSPaul Mackerras 	 */
1150493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1151493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1152493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1153493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1154493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1155493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1156493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1157493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
11589b6b563cSPaul Mackerras 
11599b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
11605827d416SAnton Blanchard 	path = prom_scratch;
11619b6b563cSPaul Mackerras 
11629b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
11639b6b563cSPaul Mackerras 		type[0] = 0;
11649b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
11659b6b563cSPaul Mackerras 
1166c4988820SPaul Mackerras 		if (type[0] == 0) {
1167c4988820SPaul Mackerras 			/*
1168c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1169c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1170c4988820SPaul Mackerras 			 */
1171c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1172c4988820SPaul Mackerras 		}
11735827d416SAnton Blanchard 		if (strcmp(type, "memory"))
11749b6b563cSPaul Mackerras 			continue;
11759b6b563cSPaul Mackerras 
11765827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
11779b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
11789b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
11799b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
11809b6b563cSPaul Mackerras 		}
11815827d416SAnton Blanchard 		p = regbuf;
11829b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
11839b6b563cSPaul Mackerras 
11849b6b563cSPaul Mackerras #ifdef DEBUG_PROM
11859b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
11869b6b563cSPaul Mackerras 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
11879b6b563cSPaul Mackerras 		prom_debug("  node %s :\n", path);
11889b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
11899b6b563cSPaul Mackerras 
11909b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
11919b6b563cSPaul Mackerras 			unsigned long base, size;
11929b6b563cSPaul Mackerras 
11939b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
11949b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
11959b6b563cSPaul Mackerras 
11969b6b563cSPaul Mackerras 			if (size == 0)
11979b6b563cSPaul Mackerras 				continue;
11989b6b563cSPaul Mackerras 			prom_debug("    %x %x\n", base, size);
11995827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
12005827d416SAnton Blanchard 				rmo_top = size;
12015827d416SAnton Blanchard 			if ((base + size) > ram_top)
12025827d416SAnton Blanchard 				ram_top = base + size;
12039b6b563cSPaul Mackerras 		}
12049b6b563cSPaul Mackerras 	}
12059b6b563cSPaul Mackerras 
12065827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
12079b6b563cSPaul Mackerras 
12089b6b563cSPaul Mackerras 	/*
1209cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1210cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1211cf68787bSBenjamin Krill 	 * TCE's up there.
1212cf68787bSBenjamin Krill 	 */
1213cf68787bSBenjamin Krill 
12145827d416SAnton Blanchard 	alloc_top_high = ram_top;
1215cf68787bSBenjamin Krill 
12165827d416SAnton Blanchard 	if (prom_memory_limit) {
12175827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1218cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
12195827d416SAnton Blanchard 				prom_memory_limit);
12205827d416SAnton Blanchard 			prom_memory_limit = 0;
12215827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1222cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x >= ram_top.\n",
12235827d416SAnton Blanchard 				prom_memory_limit);
12245827d416SAnton Blanchard 			prom_memory_limit = 0;
1225cf68787bSBenjamin Krill 		} else {
12265827d416SAnton Blanchard 			ram_top = prom_memory_limit;
12275827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1228cf68787bSBenjamin Krill 		}
1229cf68787bSBenjamin Krill 	}
1230cf68787bSBenjamin Krill 
1231cf68787bSBenjamin Krill 	/*
12329b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
12339b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
12349b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
12359b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
12369b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
12379b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
12389b6b563cSPaul Mackerras 	 */
12395827d416SAnton Blanchard 	if (!rmo_top)
12405827d416SAnton Blanchard 		rmo_top = ram_top;
12415827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
12425827d416SAnton Blanchard 	alloc_top = rmo_top;
12435827d416SAnton Blanchard 	alloc_top_high = ram_top;
12449b6b563cSPaul Mackerras 
124564968f60SPaul Mackerras 	/*
124664968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
124764968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
124864968f60SPaul Mackerras 	 */
12495827d416SAnton Blanchard 	if (prom_initrd_start &&
12505827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
12515827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
12525827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
125364968f60SPaul Mackerras 
12549b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
12555827d416SAnton Blanchard 	prom_printf("  memory_limit : %x (16 MB aligned)\n", prom_memory_limit);
12565827d416SAnton Blanchard 	prom_printf("  alloc_bottom : %x\n", alloc_bottom);
12575827d416SAnton Blanchard 	prom_printf("  alloc_top    : %x\n", alloc_top);
12585827d416SAnton Blanchard 	prom_printf("  alloc_top_hi : %x\n", alloc_top_high);
12595827d416SAnton Blanchard 	prom_printf("  rmo_top      : %x\n", rmo_top);
12605827d416SAnton Blanchard 	prom_printf("  ram_top      : %x\n", ram_top);
12619b6b563cSPaul Mackerras }
12629b6b563cSPaul Mackerras 
126327f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
126427f44888SBenjamin Herrenschmidt {
1265493adffcSBenjamin Herrenschmidt 	__be32 val;
1266493adffcSBenjamin Herrenschmidt 	ihandle stdin;
126727f44888SBenjamin Herrenschmidt 
1268493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1269493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1270493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1271493adffcSBenjamin Herrenschmidt 	}
127227f44888SBenjamin Herrenschmidt }
127327f44888SBenjamin Herrenschmidt 
127427f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
127527f44888SBenjamin Herrenschmidt 
12766e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
12776e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_base;
12786e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_entry;
12796e35d5daSBenjamin Herrenschmidt #endif
12806e35d5daSBenjamin Herrenschmidt 
12816e35d5daSBenjamin Herrenschmidt /*
12826e35d5daSBenjamin Herrenschmidt  * Allocate room for and instantiate OPAL
12836e35d5daSBenjamin Herrenschmidt  */
12846e35d5daSBenjamin Herrenschmidt static void __init prom_instantiate_opal(void)
12856e35d5daSBenjamin Herrenschmidt {
12866e35d5daSBenjamin Herrenschmidt 	phandle opal_node;
12876e35d5daSBenjamin Herrenschmidt 	ihandle opal_inst;
12886e35d5daSBenjamin Herrenschmidt 	u64 base, entry;
12896e35d5daSBenjamin Herrenschmidt 	u64 size = 0, align = 0x10000;
1290493adffcSBenjamin Herrenschmidt 	__be64 val64;
12916e35d5daSBenjamin Herrenschmidt 	u32 rets[2];
12926e35d5daSBenjamin Herrenschmidt 
12936e35d5daSBenjamin Herrenschmidt 	prom_debug("prom_instantiate_opal: start...\n");
12946e35d5daSBenjamin Herrenschmidt 
12956e35d5daSBenjamin Herrenschmidt 	opal_node = call_prom("finddevice", 1, 1, ADDR("/ibm,opal"));
12966e35d5daSBenjamin Herrenschmidt 	prom_debug("opal_node: %x\n", opal_node);
12976e35d5daSBenjamin Herrenschmidt 	if (!PHANDLE_VALID(opal_node))
12986e35d5daSBenjamin Herrenschmidt 		return;
12996e35d5daSBenjamin Herrenschmidt 
1300493adffcSBenjamin Herrenschmidt 	val64 = 0;
1301493adffcSBenjamin Herrenschmidt 	prom_getprop(opal_node, "opal-runtime-size", &val64, sizeof(val64));
1302493adffcSBenjamin Herrenschmidt 	size = be64_to_cpu(val64);
13036e35d5daSBenjamin Herrenschmidt 	if (size == 0)
13046e35d5daSBenjamin Herrenschmidt 		return;
1305493adffcSBenjamin Herrenschmidt 	val64 = 0;
1306493adffcSBenjamin Herrenschmidt 	prom_getprop(opal_node, "opal-runtime-alignment", &val64,sizeof(val64));
1307493adffcSBenjamin Herrenschmidt 	align = be64_to_cpu(val64);
13086e35d5daSBenjamin Herrenschmidt 
13096e35d5daSBenjamin Herrenschmidt 	base = alloc_down(size, align, 0);
13106e35d5daSBenjamin Herrenschmidt 	if (base == 0) {
13116e35d5daSBenjamin Herrenschmidt 		prom_printf("OPAL allocation failed !\n");
13126e35d5daSBenjamin Herrenschmidt 		return;
13136e35d5daSBenjamin Herrenschmidt 	}
13146e35d5daSBenjamin Herrenschmidt 
13156e35d5daSBenjamin Herrenschmidt 	opal_inst = call_prom("open", 1, 1, ADDR("/ibm,opal"));
13166e35d5daSBenjamin Herrenschmidt 	if (!IHANDLE_VALID(opal_inst)) {
13176e35d5daSBenjamin Herrenschmidt 		prom_printf("opening opal package failed (%x)\n", opal_inst);
13186e35d5daSBenjamin Herrenschmidt 		return;
13196e35d5daSBenjamin Herrenschmidt 	}
13206e35d5daSBenjamin Herrenschmidt 
13216e35d5daSBenjamin Herrenschmidt 	prom_printf("instantiating opal at 0x%x...", base);
13226e35d5daSBenjamin Herrenschmidt 
13236e35d5daSBenjamin Herrenschmidt 	if (call_prom_ret("call-method", 4, 3, rets,
13246e35d5daSBenjamin Herrenschmidt 			  ADDR("load-opal-runtime"),
13256e35d5daSBenjamin Herrenschmidt 			  opal_inst,
13266e35d5daSBenjamin Herrenschmidt 			  base >> 32, base & 0xffffffff) != 0
13276e35d5daSBenjamin Herrenschmidt 	    || (rets[0] == 0 && rets[1] == 0)) {
13286e35d5daSBenjamin Herrenschmidt 		prom_printf(" failed\n");
13296e35d5daSBenjamin Herrenschmidt 		return;
13306e35d5daSBenjamin Herrenschmidt 	}
13316e35d5daSBenjamin Herrenschmidt 	entry = (((u64)rets[0]) << 32) | rets[1];
13326e35d5daSBenjamin Herrenschmidt 
13336e35d5daSBenjamin Herrenschmidt 	prom_printf(" done\n");
13346e35d5daSBenjamin Herrenschmidt 
13356e35d5daSBenjamin Herrenschmidt 	reserve_mem(base, size);
13366e35d5daSBenjamin Herrenschmidt 
13376e35d5daSBenjamin Herrenschmidt 	prom_debug("opal base     = 0x%x\n", base);
13386e35d5daSBenjamin Herrenschmidt 	prom_debug("opal align    = 0x%x\n", align);
13396e35d5daSBenjamin Herrenschmidt 	prom_debug("opal entry    = 0x%x\n", entry);
13406e35d5daSBenjamin Herrenschmidt 	prom_debug("opal size     = 0x%x\n", (long)size);
13416e35d5daSBenjamin Herrenschmidt 
13426e35d5daSBenjamin Herrenschmidt 	prom_setprop(opal_node, "/ibm,opal", "opal-base-address",
13436e35d5daSBenjamin Herrenschmidt 		     &base, sizeof(base));
13446e35d5daSBenjamin Herrenschmidt 	prom_setprop(opal_node, "/ibm,opal", "opal-entry-address",
13456e35d5daSBenjamin Herrenschmidt 		     &entry, sizeof(entry));
13466e35d5daSBenjamin Herrenschmidt 
13476e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
13485827d416SAnton Blanchard 	prom_opal_base = base;
13495827d416SAnton Blanchard 	prom_opal_entry = entry;
13506e35d5daSBenjamin Herrenschmidt #endif
13516e35d5daSBenjamin Herrenschmidt 	prom_debug("prom_instantiate_opal: end...\n");
13526e35d5daSBenjamin Herrenschmidt }
13536e35d5daSBenjamin Herrenschmidt 
135427f44888SBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV */
13559b6b563cSPaul Mackerras 
13569b6b563cSPaul Mackerras /*
13579b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
13589b6b563cSPaul Mackerras  */
13599b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
13609b6b563cSPaul Mackerras {
13619b6b563cSPaul Mackerras 	phandle rtas_node;
13629b6b563cSPaul Mackerras 	ihandle rtas_inst;
13639b6b563cSPaul Mackerras 	u32 base, entry = 0;
1364493adffcSBenjamin Herrenschmidt 	__be32 val;
13659b6b563cSPaul Mackerras 	u32 size = 0;
13669b6b563cSPaul Mackerras 
13679b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
13689b6b563cSPaul Mackerras 
13699b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
13709b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
13719b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
13729b6b563cSPaul Mackerras 		return;
13739b6b563cSPaul Mackerras 
1374493adffcSBenjamin Herrenschmidt 	val = 0;
1375493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1376493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
13779b6b563cSPaul Mackerras 	if (size == 0)
13789b6b563cSPaul Mackerras 		return;
13799b6b563cSPaul Mackerras 
13809b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
13816d1e2c6cSAnton Blanchard 	if (base == 0)
13826d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
13839b6b563cSPaul Mackerras 
13849b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
13859b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1386a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
13879b6b563cSPaul Mackerras 		return;
13889b6b563cSPaul Mackerras 	}
13899b6b563cSPaul Mackerras 
13909b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
13919b6b563cSPaul Mackerras 
13929b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
13939b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1394a23414beSPaul Mackerras 			  rtas_inst, base) != 0
13959b6b563cSPaul Mackerras 	    || entry == 0) {
13969b6b563cSPaul Mackerras 		prom_printf(" failed\n");
13979b6b563cSPaul Mackerras 		return;
13989b6b563cSPaul Mackerras 	}
13999b6b563cSPaul Mackerras 	prom_printf(" done\n");
14009b6b563cSPaul Mackerras 
14019b6b563cSPaul Mackerras 	reserve_mem(base, size);
14029b6b563cSPaul Mackerras 
1403493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1404a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1405493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1406493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1407a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1408493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
14099b6b563cSPaul Mackerras 
1410dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1411dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1412dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1413dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1414dbe78b40SBenjamin Herrenschmidt 
14159b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
14169b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
14179b6b563cSPaul Mackerras 	prom_debug("rtas size     = 0x%x\n", (long)size);
14189b6b563cSPaul Mackerras 
14199b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
14209b6b563cSPaul Mackerras }
14219b6b563cSPaul Mackerras 
14229b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
14239b6b563cSPaul Mackerras /*
14244a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
14254a727429SAshley Lai  */
14264a727429SAshley Lai static void __init prom_instantiate_sml(void)
14274a727429SAshley Lai {
14284a727429SAshley Lai 	phandle ibmvtpm_node;
14294a727429SAshley Lai 	ihandle ibmvtpm_inst;
1430b4ed0469SHon Ching \(Vicky\) Lo 	u32 entry = 0, size = 0, succ = 0;
14314a727429SAshley Lai 	u64 base;
1432b4ed0469SHon Ching \(Vicky\) Lo 	__be32 val;
14334a727429SAshley Lai 
14344a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
14354a727429SAshley Lai 
14362f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
14374a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
14384a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
14394a727429SAshley Lai 		return;
14404a727429SAshley Lai 
14412f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
14424a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
14434a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
14444a727429SAshley Lai 		return;
14454a727429SAshley Lai 	}
14464a727429SAshley Lai 
1447b4ed0469SHon Ching \(Vicky\) Lo 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1448b4ed0469SHon Ching \(Vicky\) Lo 			 &val, sizeof(val)) != PROM_ERROR) {
1449b4ed0469SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &succ,
1450b4ed0469SHon Ching \(Vicky\) Lo 				  ADDR("reformat-sml-to-efi-alignment"),
1451b4ed0469SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || succ == 0) {
1452b4ed0469SHon Ching \(Vicky\) Lo 			prom_printf("Reformat SML to EFI alignment failed\n");
1453b4ed0469SHon Ching \(Vicky\) Lo 			return;
1454b4ed0469SHon Ching \(Vicky\) Lo 		}
1455b4ed0469SHon Ching \(Vicky\) Lo 
14564a727429SAshley Lai 		if (call_prom_ret("call-method", 2, 2, &size,
14579e5d4af4SHon Ching \(Vicky\) Lo 				  ADDR("sml-get-allocated-size"),
14589e5d4af4SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || size == 0) {
14599e5d4af4SHon Ching \(Vicky\) Lo 			prom_printf("SML get allocated size failed\n");
14609e5d4af4SHon Ching \(Vicky\) Lo 			return;
14619e5d4af4SHon Ching \(Vicky\) Lo 		}
14629e5d4af4SHon Ching \(Vicky\) Lo 	} else {
14639e5d4af4SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &size,
14644a727429SAshley Lai 				  ADDR("sml-get-handover-size"),
14654a727429SAshley Lai 				  ibmvtpm_inst) != 0 || size == 0) {
14664a727429SAshley Lai 			prom_printf("SML get handover size failed\n");
14674a727429SAshley Lai 			return;
14684a727429SAshley Lai 		}
14699e5d4af4SHon Ching \(Vicky\) Lo 	}
14704a727429SAshley Lai 
14714a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
14724a727429SAshley Lai 	if (base == 0)
14734a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
14744a727429SAshley Lai 
14754a727429SAshley Lai 	prom_printf("instantiating sml at 0x%x...", base);
14764a727429SAshley Lai 
14779e5d4af4SHon Ching \(Vicky\) Lo 	memset((void *)base, 0, size);
14789e5d4af4SHon Ching \(Vicky\) Lo 
14794a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
14804a727429SAshley Lai 			  ADDR("sml-handover"),
14814a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
14824a727429SAshley Lai 		prom_printf("SML handover failed\n");
14834a727429SAshley Lai 		return;
14844a727429SAshley Lai 	}
14854a727429SAshley Lai 	prom_printf(" done\n");
14864a727429SAshley Lai 
14874a727429SAshley Lai 	reserve_mem(base, size);
14884a727429SAshley Lai 
14892f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
14904a727429SAshley Lai 		     &base, sizeof(base));
14912f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
14924a727429SAshley Lai 		     &size, sizeof(size));
14934a727429SAshley Lai 
14944a727429SAshley Lai 	prom_debug("sml base     = 0x%x\n", base);
14954a727429SAshley Lai 	prom_debug("sml size     = 0x%x\n", (long)size);
14964a727429SAshley Lai 
14974a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
14984a727429SAshley Lai }
14994a727429SAshley Lai 
15004a727429SAshley Lai /*
15019b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
15029b6b563cSPaul Mackerras  */
1503493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
15049b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
15059b6b563cSPaul Mackerras {
15069b6b563cSPaul Mackerras 	phandle node;
15079b6b563cSPaul Mackerras 	ihandle phb_node;
15089b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
15095827d416SAnton Blanchard 	char *path = prom_scratch;
15109b6b563cSPaul Mackerras 	u64 base, align;
15119b6b563cSPaul Mackerras 	u32 minalign, minsize;
15129b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
15139b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
15149b6b563cSPaul Mackerras 	u64 i;
15159b6b563cSPaul Mackerras 
15165827d416SAnton Blanchard 	if (prom_iommu_off)
15179b6b563cSPaul Mackerras 		return;
15189b6b563cSPaul Mackerras 
15199b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
15209b6b563cSPaul Mackerras 
15219b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
15225827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
15239b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
15249b6b563cSPaul Mackerras 
15259b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
15269b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
15279b6b563cSPaul Mackerras 		compatible[0] = 0;
15289b6b563cSPaul Mackerras 		type[0] = 0;
15299b6b563cSPaul Mackerras 		model[0] = 0;
15309b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
15319b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
15329b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
15339b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
15349b6b563cSPaul Mackerras 
15355827d416SAnton Blanchard 		if ((type[0] == 0) || (strstr(type, "pci") == NULL))
15369b6b563cSPaul Mackerras 			continue;
15379b6b563cSPaul Mackerras 
1538e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
15399b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
15405827d416SAnton Blanchard 			if ((strstr(compatible, "python") == NULL) &&
15415827d416SAnton Blanchard 			    (strstr(compatible, "Speedwagon") == NULL) &&
15425827d416SAnton Blanchard 			    (strstr(compatible, "Winnipeg") == NULL))
15439b6b563cSPaul Mackerras 				continue;
15449b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
15455827d416SAnton Blanchard 			if ((strstr(model, "ython") == NULL) &&
15465827d416SAnton Blanchard 			    (strstr(model, "peedwagon") == NULL) &&
15475827d416SAnton Blanchard 			    (strstr(model, "innipeg") == NULL))
15489b6b563cSPaul Mackerras 				continue;
15499b6b563cSPaul Mackerras 		}
15509b6b563cSPaul Mackerras 
15519b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
15529b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
15539b6b563cSPaul Mackerras 			minalign = 0;
15549b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
15559b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
15569b6b563cSPaul Mackerras 			minsize = 4UL << 20;
15579b6b563cSPaul Mackerras 
15589b6b563cSPaul Mackerras 		/*
15599b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
15609b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
15619b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
15629b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
15639b6b563cSPaul Mackerras 		 *
15649b6b563cSPaul Mackerras 		 * On POWER4, firmware sets the TCE region by assuming
15659b6b563cSPaul Mackerras 		 * each TCE table is 8MB. Using this memory for anything
15669b6b563cSPaul Mackerras 		 * else will impact performance, so we always allocate 8MB.
15679b6b563cSPaul Mackerras 		 * Anton
15689b6b563cSPaul Mackerras 		 */
1569d3dbeef6SMichael Ellerman 		if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p))
15709b6b563cSPaul Mackerras 			minsize = 8UL << 20;
15719b6b563cSPaul Mackerras 		else
15729b6b563cSPaul Mackerras 			minsize = 4UL << 20;
15739b6b563cSPaul Mackerras 
15749b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
15759b6b563cSPaul Mackerras 		align = max(minalign, minsize);
15769b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
15779b6b563cSPaul Mackerras 		if (base == 0)
15789b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
15799b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
15809b6b563cSPaul Mackerras 			local_alloc_bottom = base;
15819b6b563cSPaul Mackerras 
15829b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
1583aca71ef8SLi Zefan 		memset(path, 0, PROM_SCRATCH_SIZE);
15849b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
15859b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
15869b6b563cSPaul Mackerras 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
15879b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
15889b6b563cSPaul Mackerras 		}
15899b6b563cSPaul Mackerras 
1590a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
1591a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1592a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1593a23414beSPaul Mackerras 
15949b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
15959b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
15969b6b563cSPaul Mackerras 		prom_debug("\tbase = 0x%x\n", base);
15979b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
15989b6b563cSPaul Mackerras 
15999b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
16009b6b563cSPaul Mackerras 		 * over the allocated size.
16019b6b563cSPaul Mackerras 		 */
16022b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
16039b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
16049b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
16059b6b563cSPaul Mackerras 			tce_entry |= 0x3;
16069b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
16079b6b563cSPaul Mackerras 		}
16089b6b563cSPaul Mackerras 
16099b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
16109b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
16119b6b563cSPaul Mackerras 		if (phb_node == 0)
16129b6b563cSPaul Mackerras 			prom_printf("... failed\n");
16139b6b563cSPaul Mackerras 		else
16149b6b563cSPaul Mackerras 			prom_printf("... done\n");
16159b6b563cSPaul Mackerras 
16169b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
16179b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
16189b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
16199b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
16209b6b563cSPaul Mackerras 	}
16219b6b563cSPaul Mackerras 
16229b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
16239b6b563cSPaul Mackerras 
16242babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
16252babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
16265827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
16275827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
16289b6b563cSPaul Mackerras 
16299b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
16309b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
16319b6b563cSPaul Mackerras }
1632493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1633493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
16349b6b563cSPaul Mackerras 
16359b6b563cSPaul Mackerras /*
16369b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
16379b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
16389b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
16399b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
16409b6b563cSPaul Mackerras  *
16419b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
16429b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
16439b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
16449b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
16459b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
16469b6b563cSPaul Mackerras  * of setting those values.
16479b6b563cSPaul Mackerras  *
16489b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
16499b6b563cSPaul Mackerras  * is in its holding pattern code.
16509b6b563cSPaul Mackerras  *
16519b6b563cSPaul Mackerras  * -- Cort
16529b6b563cSPaul Mackerras  */
1653bbd0abdaSPaul Mackerras /*
1654bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
1655bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
1656bbd0abdaSPaul Mackerras  */
1657bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1658bbd0abdaSPaul Mackerras 
16599b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
16609b6b563cSPaul Mackerras {
16619b6b563cSPaul Mackerras 	unsigned long i;
16629b6b563cSPaul Mackerras 	phandle node;
16639b6b563cSPaul Mackerras 	char type[64];
16649b6b563cSPaul Mackerras 	unsigned long *spinloop
1665bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
16669b6b563cSPaul Mackerras 	unsigned long *acknowledge
1667bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1668bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
16699b6b563cSPaul Mackerras 
1670dbe78b40SBenjamin Herrenschmidt 	/*
1671dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
1672dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
1673dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
1674dbe78b40SBenjamin Herrenschmidt 	 */
1675dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
1676dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
1677dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
1678dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
1679dbe78b40SBenjamin Herrenschmidt 		return;
1680dbe78b40SBenjamin Herrenschmidt 	}
1681dbe78b40SBenjamin Herrenschmidt 
16829b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
16839b6b563cSPaul Mackerras 	prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
16849b6b563cSPaul Mackerras 	prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
16859b6b563cSPaul Mackerras 	prom_debug("    1) acknowledge    = 0x%x\n",
16869b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
16879b6b563cSPaul Mackerras 	prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
16889b6b563cSPaul Mackerras 	prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
16899b6b563cSPaul Mackerras 
16909b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
16919b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
16929b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
16939b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
16949b6b563cSPaul Mackerras 	 */
16959b6b563cSPaul Mackerras 	*spinloop = 0;
16969b6b563cSPaul Mackerras 
16979b6b563cSPaul Mackerras 	/* look for cpus */
16989b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
1699493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
1700493adffcSBenjamin Herrenschmidt 		__be32 reg;
1701493adffcSBenjamin Herrenschmidt 
17029b6b563cSPaul Mackerras 		type[0] = 0;
17039b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
17045827d416SAnton Blanchard 		if (strcmp(type, "cpu") != 0)
17059b6b563cSPaul Mackerras 			continue;
17069b6b563cSPaul Mackerras 
17079b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
17089b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
17095827d416SAnton Blanchard 			if (strcmp(type, "okay") != 0)
17109b6b563cSPaul Mackerras 				continue;
17119b6b563cSPaul Mackerras 
1712493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
17139b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
1714493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
17159b6b563cSPaul Mackerras 
1716493adffcSBenjamin Herrenschmidt 		prom_debug("cpu hw idx   = %lu\n", cpu_no);
17179b6b563cSPaul Mackerras 
17189b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
17199b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
17209b6b563cSPaul Mackerras 		 * spinloop.
17219b6b563cSPaul Mackerras 		 */
17229b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
17239b6b563cSPaul Mackerras 
1724493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
172527f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
1726493adffcSBenjamin Herrenschmidt 			prom_printf("starting cpu hw idx %lu... ", cpu_no);
17279b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
1728493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
17299b6b563cSPaul Mackerras 
17309b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
17319b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
17329b6b563cSPaul Mackerras 				mb();
17339b6b563cSPaul Mackerras 
1734493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
17359b6b563cSPaul Mackerras 				prom_printf("done\n");
1736bbd0abdaSPaul Mackerras 			else
17379b6b563cSPaul Mackerras 				prom_printf("failed: %x\n", *acknowledge);
17389b6b563cSPaul Mackerras 		}
17399b6b563cSPaul Mackerras #ifdef CONFIG_SMP
17409b6b563cSPaul Mackerras 		else
1741493adffcSBenjamin Herrenschmidt 			prom_printf("boot cpu hw idx %lu\n", cpu_no);
17429b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
17439b6b563cSPaul Mackerras 	}
17449b6b563cSPaul Mackerras 
17459b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
17469b6b563cSPaul Mackerras }
17479b6b563cSPaul Mackerras 
17489b6b563cSPaul Mackerras 
17499b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
17509b6b563cSPaul Mackerras {
17519b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
17525827d416SAnton Blanchard 	prom_entry = pp;
17539b6b563cSPaul Mackerras 
17549b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
17555827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
17565827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
17579b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
17589b6b563cSPaul Mackerras 
17599b6b563cSPaul Mackerras 	/* get device tree root */
17605827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
17615827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
17629b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1763a575b807SPaul Mackerras 
17645827d416SAnton Blanchard 	prom.mmumap = 0;
17659b6b563cSPaul Mackerras }
17669b6b563cSPaul Mackerras 
1767a575b807SPaul Mackerras #ifdef CONFIG_PPC32
1768a575b807SPaul Mackerras /*
1769a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
1770a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
1771a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
1772a575b807SPaul Mackerras  */
1773a575b807SPaul Mackerras static void __init prom_find_mmu(void)
1774a575b807SPaul Mackerras {
1775a575b807SPaul Mackerras 	phandle oprom;
1776a575b807SPaul Mackerras 	char version[64];
1777a575b807SPaul Mackerras 
1778a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1779a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
1780a575b807SPaul Mackerras 		return;
1781a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1782a575b807SPaul Mackerras 		return;
1783a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
1784a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
1785a23414beSPaul Mackerras 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1786a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
1787a23414beSPaul Mackerras 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1788a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1789a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1790a23414beSPaul Mackerras 	} else
1791a575b807SPaul Mackerras 		return;
17925827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
17935827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
17945827d416SAnton Blanchard 		     sizeof(prom.mmumap));
1795493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
17965827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
1797a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
1798a575b807SPaul Mackerras }
1799a575b807SPaul Mackerras #else
1800a575b807SPaul Mackerras #define prom_find_mmu()
1801a575b807SPaul Mackerras #endif
1802a575b807SPaul Mackerras 
18039b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
18049b6b563cSPaul Mackerras {
18055827d416SAnton Blanchard 	char *path = of_stdout_device;
18069b6b563cSPaul Mackerras 	char type[16];
1807493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
1808493adffcSBenjamin Herrenschmidt 	__be32 val;
18099b6b563cSPaul Mackerras 
18105827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
18119b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
18129b6b563cSPaul Mackerras 
1813493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
18149b6b563cSPaul Mackerras 
18159b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
18169b6b563cSPaul Mackerras 	memset(path, 0, 256);
18175827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
18185827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
18195827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
1820a23414beSPaul Mackerras 		     path, strlen(path) + 1);
18219b6b563cSPaul Mackerras 
182210348f59SBenjamin Herrenschmidt 	/* instance-to-package fails on PA-Semi */
182310348f59SBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
182410348f59SBenjamin Herrenschmidt 	if (stdout_node != PROM_ERROR) {
182510348f59SBenjamin Herrenschmidt 		val = cpu_to_be32(stdout_node);
182610348f59SBenjamin Herrenschmidt 		prom_setprop(prom.chosen, "/chosen", "linux,stdout-package",
182710348f59SBenjamin Herrenschmidt 			     &val, sizeof(val));
182810348f59SBenjamin Herrenschmidt 
18299b6b563cSPaul Mackerras 		/* If it's a display, note it */
18309b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
1831493adffcSBenjamin Herrenschmidt 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
18325827d416SAnton Blanchard 		if (strcmp(type, "display") == 0)
1833493adffcSBenjamin Herrenschmidt 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
18349b6b563cSPaul Mackerras 	}
183510348f59SBenjamin Herrenschmidt }
18369b6b563cSPaul Mackerras 
18379b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
18389b6b563cSPaul Mackerras {
18399b6b563cSPaul Mackerras 	char compat[256];
18409b6b563cSPaul Mackerras 	int len, i = 0;
184121fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
18429b6b563cSPaul Mackerras 	phandle rtas;
1843e8222502SBenjamin Herrenschmidt 	int x;
184421fe3301SBenjamin Herrenschmidt #endif
1845e8222502SBenjamin Herrenschmidt 
18466e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
18475827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
18489b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
18499b6b563cSPaul Mackerras 	if (len > 0) {
18509b6b563cSPaul Mackerras 		compat[len] = 0;
18519b6b563cSPaul Mackerras 		while (i < len) {
18529b6b563cSPaul Mackerras 			char *p = &compat[i];
18539b6b563cSPaul Mackerras 			int sl = strlen(p);
18549b6b563cSPaul Mackerras 			if (sl == 0)
18559b6b563cSPaul Mackerras 				break;
18565827d416SAnton Blanchard 			if (strstr(p, "Power Macintosh") ||
18575827d416SAnton Blanchard 			    strstr(p, "MacRISC"))
18589b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
1859133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
1860133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
1861133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
1862133dda1eSArnd Bergmann 			 * so we do it here.
1863133dda1eSArnd Bergmann 			 */
18645827d416SAnton Blanchard 			if (strstr(p, "IBM,CBEA") ||
18655827d416SAnton Blanchard 			    strstr(p, "IBM,CPBW-1.0"))
1866133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
1867133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
18689b6b563cSPaul Mackerras 			i += sl + 1;
18699b6b563cSPaul Mackerras 		}
18709b6b563cSPaul Mackerras 	}
18719b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
18726e35d5daSBenjamin Herrenschmidt 	/* Try to detect OPAL */
18736e35d5daSBenjamin Herrenschmidt 	if (PHANDLE_VALID(call_prom("finddevice", 1, 1, ADDR("/ibm,opal"))))
18746e35d5daSBenjamin Herrenschmidt 		return PLATFORM_OPAL;
18756e35d5daSBenjamin Herrenschmidt 
18766e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
1877e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
1878e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
1879e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
1880e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
1881e8222502SBenjamin Herrenschmidt 	 */
18825827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
1883e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
1884e8222502SBenjamin Herrenschmidt 	if (len <= 0)
1885e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
18865827d416SAnton Blanchard 	if (strcmp(compat, "chrp"))
1887e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
1888e8222502SBenjamin Herrenschmidt 
18899b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
18909b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1891e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
1892e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
1893e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
18949b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
18954da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
18969b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
18979b6b563cSPaul Mackerras 	}
18989b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
18999b6b563cSPaul Mackerras #else
1900e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
19019b6b563cSPaul Mackerras #endif
19029b6b563cSPaul Mackerras }
19039b6b563cSPaul Mackerras 
19049b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
19059b6b563cSPaul Mackerras {
19069b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
19079b6b563cSPaul Mackerras }
19089b6b563cSPaul Mackerras 
19099b6b563cSPaul Mackerras /*
19109b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
19119b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
19129b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
19139b6b563cSPaul Mackerras  * we've taken over the MMU.
19149b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
19159b6b563cSPaul Mackerras  * and if so, open it now.
19169b6b563cSPaul Mackerras  */
19179b6b563cSPaul Mackerras static void __init prom_check_displays(void)
19189b6b563cSPaul Mackerras {
19199b6b563cSPaul Mackerras 	char type[16], *path;
19209b6b563cSPaul Mackerras 	phandle node;
19219b6b563cSPaul Mackerras 	ihandle ih;
19229b6b563cSPaul Mackerras 	int i;
19239b6b563cSPaul Mackerras 
19249b6b563cSPaul Mackerras 	static unsigned char default_colors[] = {
19259b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
19269b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
19279b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
19289b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
19299b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
19309b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
19319b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
19329b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
19339b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
19349b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
19359b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
19369b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
19379b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
19389b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
19399b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
19409b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
19419b6b563cSPaul Mackerras 	};
19429b6b563cSPaul Mackerras 	const unsigned char *clut;
19439b6b563cSPaul Mackerras 
19444da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
19459b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
19469b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
19479b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
19485827d416SAnton Blanchard 		if (strcmp(type, "display") != 0)
19499b6b563cSPaul Mackerras 			continue;
19509b6b563cSPaul Mackerras 
19519b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
19525827d416SAnton Blanchard 		path = prom_scratch;
19539b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
19549b6b563cSPaul Mackerras 
19559b6b563cSPaul Mackerras 		/*
19569b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
19579b6b563cSPaul Mackerras 		 * arguments
19589b6b563cSPaul Mackerras 		 */
19599b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
19609b6b563cSPaul Mackerras 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
19619b6b563cSPaul Mackerras 			continue;
19629b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
19639b6b563cSPaul Mackerras 
19649b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
19659b6b563cSPaul Mackerras 		if (ih == 0) {
19669b6b563cSPaul Mackerras 			prom_printf("failed\n");
19679b6b563cSPaul Mackerras 			continue;
19689b6b563cSPaul Mackerras 		}
19699b6b563cSPaul Mackerras 
19709b6b563cSPaul Mackerras 		/* Success */
19719b6b563cSPaul Mackerras 		prom_printf("done\n");
1972a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
19739b6b563cSPaul Mackerras 
19749b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
19759b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
19765827d416SAnton Blanchard 		clut = default_colors;
19773f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
19789b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
19799b6b563cSPaul Mackerras 					   clut[2]) != 0)
19809b6b563cSPaul Mackerras 				break;
19819b6b563cSPaul Mackerras 
19829b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
19835827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
19845827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
19859b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
19869b6b563cSPaul Mackerras 					   clut[2]) != 0)
19879b6b563cSPaul Mackerras 				break;
19889b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
19897191b615SBenjamin Herrenschmidt 
19907191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
19917191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
19927191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
19937191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
19947191b615SBenjamin Herrenschmidt 
19957191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
19967191b615SBenjamin Herrenschmidt 			prom_getprop(node, "width", &width, 4);
19977191b615SBenjamin Herrenschmidt 			prom_getprop(node, "height", &height, 4);
19987191b615SBenjamin Herrenschmidt 			prom_getprop(node, "linebytes", &pitch, 4);
19997191b615SBenjamin Herrenschmidt 			prom_getprop(node, "address", &addr, 4);
20007191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
20017191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
20027191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
20037191b615SBenjamin Herrenschmidt 		}
20047191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
20059b6b563cSPaul Mackerras 	}
20069b6b563cSPaul Mackerras }
20079b6b563cSPaul Mackerras 
20089b6b563cSPaul Mackerras 
20099b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
20109b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
20119b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
20129b6b563cSPaul Mackerras {
20139b6b563cSPaul Mackerras 	void *ret;
20149b6b563cSPaul Mackerras 
20159b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
20169b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
20179b6b563cSPaul Mackerras 		unsigned long room, chunk;
20189b6b563cSPaul Mackerras 
20199b6b563cSPaul Mackerras 		prom_debug("Chunk exhausted, claiming more at %x...\n",
20205827d416SAnton Blanchard 			   alloc_bottom);
20215827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
20229b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
20239b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
20249b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2025fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2026fbafd728SAnton Blanchard 				   "(no room)\n");
20279b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
20289b6b563cSPaul Mackerras 		if (chunk == 0)
2029fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2030fbafd728SAnton Blanchard 				   "(claim failed)\n");
2031966728ddSAnton Blanchard 		*mem_end = chunk + room;
20329b6b563cSPaul Mackerras 	}
20339b6b563cSPaul Mackerras 
20349b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
20359b6b563cSPaul Mackerras 	*mem_start += needed;
20369b6b563cSPaul Mackerras 
20379b6b563cSPaul Mackerras 	return ret;
20389b6b563cSPaul Mackerras }
20399b6b563cSPaul Mackerras 
2040493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2041493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2042493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2043493adffcSBenjamin Herrenschmidt 	} while(0)
20449b6b563cSPaul Mackerras 
20459b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
20469b6b563cSPaul Mackerras {
20479b6b563cSPaul Mackerras 	char *s, *os;
20489b6b563cSPaul Mackerras 
20495827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
20509b6b563cSPaul Mackerras 	s += 4;
20515827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
20529b6b563cSPaul Mackerras 		if (strcmp(s, str) == 0)
20539b6b563cSPaul Mackerras 			return s - os;
20549b6b563cSPaul Mackerras 		s += strlen(s) + 1;
20559b6b563cSPaul Mackerras 	}
20569b6b563cSPaul Mackerras 	return 0;
20579b6b563cSPaul Mackerras }
20589b6b563cSPaul Mackerras 
20599b6b563cSPaul Mackerras /*
20609b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
20619b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
20629b6b563cSPaul Mackerras  */
20639b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
20649b6b563cSPaul Mackerras 
20659b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
20669b6b563cSPaul Mackerras 					 unsigned long *mem_start,
20679b6b563cSPaul Mackerras 					 unsigned long *mem_end)
20689b6b563cSPaul Mackerras {
20699b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
20709b6b563cSPaul Mackerras 	unsigned long soff;
20719b6b563cSPaul Mackerras 	phandle child;
20729b6b563cSPaul Mackerras 
20735827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
20749b6b563cSPaul Mackerras 
20759b6b563cSPaul Mackerras 	/* get and store all property names */
20765827d416SAnton Blanchard 	prev_name = "";
20779b6b563cSPaul Mackerras 	for (;;) {
20789b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
20799b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
20809b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
20819b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
20829b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
20839b6b563cSPaul Mackerras 			break;
20849b6b563cSPaul Mackerras 		}
20859b6b563cSPaul Mackerras 
20869b6b563cSPaul Mackerras  		/* skip "name" */
20875827d416SAnton Blanchard  		if (strcmp(namep, "name") == 0) {
20889b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
20895827d416SAnton Blanchard  			prev_name = "name";
20909b6b563cSPaul Mackerras  			continue;
20919b6b563cSPaul Mackerras  		}
20929b6b563cSPaul Mackerras 		/* get/create string entry */
20939b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
20949b6b563cSPaul Mackerras 		if (soff != 0) {
20959b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
20969b6b563cSPaul Mackerras 			namep = sstart + soff;
20979b6b563cSPaul Mackerras 		} else {
20989b6b563cSPaul Mackerras 			/* Trim off some if we can */
20999b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
21005827d416SAnton Blanchard 			dt_string_end = *mem_start;
21019b6b563cSPaul Mackerras 		}
21029b6b563cSPaul Mackerras 		prev_name = namep;
21039b6b563cSPaul Mackerras 	}
21049b6b563cSPaul Mackerras 
21059b6b563cSPaul Mackerras 	/* do all our children */
21069b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
21079b6b563cSPaul Mackerras 	while (child != 0) {
21089b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
21099b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
21109b6b563cSPaul Mackerras 	}
21119b6b563cSPaul Mackerras }
21129b6b563cSPaul Mackerras 
21139b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
21149b6b563cSPaul Mackerras 					unsigned long *mem_end)
21159b6b563cSPaul Mackerras {
21169b6b563cSPaul Mackerras 	phandle child;
21179b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
21189b6b563cSPaul Mackerras 	unsigned long soff;
21199b6b563cSPaul Mackerras 	unsigned char *valp;
21209b6b563cSPaul Mackerras 	static char pname[MAX_PROPERTY_NAME];
21216e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
21229b6b563cSPaul Mackerras 
21239b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
21249b6b563cSPaul Mackerras 
21259b6b563cSPaul Mackerras 	/* get the node's full name */
21269b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2127c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2128c4988820SPaul Mackerras 	if (room > 255)
2129c4988820SPaul Mackerras 		room = 255;
2130c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
21319b6b563cSPaul Mackerras 	if (l >= 0) {
21329b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2133c4988820SPaul Mackerras 		if (l >= room) {
2134c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
21359b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
21369b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
21379b6b563cSPaul Mackerras 		}
21389b6b563cSPaul Mackerras 		namep[l] = '\0';
21399b6b563cSPaul Mackerras 
21409b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2141a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2142a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
21439b6b563cSPaul Mackerras 		 */
2144a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
21459b6b563cSPaul Mackerras 			if (*p == '/')
2146a575b807SPaul Mackerras 				lp = namep;
2147a575b807SPaul Mackerras 			else if (*p != 0)
2148a575b807SPaul Mackerras 				*lp++ = *p;
2149a575b807SPaul Mackerras 		}
2150a575b807SPaul Mackerras 		*lp = 0;
2151a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
21529b6b563cSPaul Mackerras 	}
21539b6b563cSPaul Mackerras 
21549b6b563cSPaul Mackerras 	/* get it again for debugging */
21555827d416SAnton Blanchard 	path = prom_scratch;
21569b6b563cSPaul Mackerras 	memset(path, 0, PROM_SCRATCH_SIZE);
21579b6b563cSPaul Mackerras 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
21589b6b563cSPaul Mackerras 
21599b6b563cSPaul Mackerras 	/* get and store all properties */
21605827d416SAnton Blanchard 	prev_name = "";
21615827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
21629b6b563cSPaul Mackerras 	for (;;) {
21639b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
21645827d416SAnton Blanchard 			      pname) != 1)
21659b6b563cSPaul Mackerras 			break;
21669b6b563cSPaul Mackerras 
21679b6b563cSPaul Mackerras  		/* skip "name" */
21685827d416SAnton Blanchard  		if (strcmp(pname, "name") == 0) {
21695827d416SAnton Blanchard  			prev_name = "name";
21709b6b563cSPaul Mackerras  			continue;
21719b6b563cSPaul Mackerras  		}
21729b6b563cSPaul Mackerras 
21739b6b563cSPaul Mackerras 		/* find string offset */
21745827d416SAnton Blanchard 		soff = dt_find_string(pname);
21759b6b563cSPaul Mackerras 		if (soff == 0) {
21769b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
21775827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
21789b6b563cSPaul Mackerras 			break;
21799b6b563cSPaul Mackerras 		}
21809b6b563cSPaul Mackerras 		prev_name = sstart + soff;
21819b6b563cSPaul Mackerras 
21829b6b563cSPaul Mackerras 		/* get length */
21835827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
21849b6b563cSPaul Mackerras 
21859b6b563cSPaul Mackerras 		/* sanity checks */
21869b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
21879b6b563cSPaul Mackerras 			continue;
21889b6b563cSPaul Mackerras 
21899b6b563cSPaul Mackerras 		/* push property head */
21909b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
21919b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
21929b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
21939b6b563cSPaul Mackerras 
21949b6b563cSPaul Mackerras 		/* push property content */
21959b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
21965827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
21979b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
21986e35d5daSBenjamin Herrenschmidt 
21995827d416SAnton Blanchard 		if (!strcmp(pname, "phandle"))
22006e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
22019b6b563cSPaul Mackerras 	}
22029b6b563cSPaul Mackerras 
22036e35d5daSBenjamin Herrenschmidt 	/* Add a "linux,phandle" property if no "phandle" property already
22046e35d5daSBenjamin Herrenschmidt 	 * existed (can happen with OPAL)
22056e35d5daSBenjamin Herrenschmidt 	 */
22066e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
22075827d416SAnton Blanchard 		soff = dt_find_string("linux,phandle");
22089b6b563cSPaul Mackerras 		if (soff == 0)
22099b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
22109b6b563cSPaul Mackerras 				    " <linux-phandle> node %s\n", path);
22119b6b563cSPaul Mackerras 		else {
22129b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
22139b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
22149b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
22159b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
2216493adffcSBenjamin Herrenschmidt 			*(__be32 *)valp = cpu_to_be32(node);
22179b6b563cSPaul Mackerras 		}
22186e35d5daSBenjamin Herrenschmidt 	}
22199b6b563cSPaul Mackerras 
22209b6b563cSPaul Mackerras 	/* do all our children */
22219b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
22229b6b563cSPaul Mackerras 	while (child != 0) {
22239b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
22249b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
22259b6b563cSPaul Mackerras 	}
22269b6b563cSPaul Mackerras 
22279b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
22289b6b563cSPaul Mackerras }
22299b6b563cSPaul Mackerras 
22309b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
22319b6b563cSPaul Mackerras {
22329b6b563cSPaul Mackerras 	phandle root;
22339b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
22349b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
22359b6b563cSPaul Mackerras 	char *namep;
22369b6b563cSPaul Mackerras 	u64 *rsvmap;
22379b6b563cSPaul Mackerras 
22389b6b563cSPaul Mackerras 	/*
22399b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2240fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
22419b6b563cSPaul Mackerras 	 */
22425827d416SAnton Blanchard 	room = alloc_top - alloc_bottom - 0x4000;
22439b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
22449b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
22455827d416SAnton Blanchard 	prom_debug("starting device tree allocs at %x\n", alloc_bottom);
22469b6b563cSPaul Mackerras 
22479b6b563cSPaul Mackerras 	/* Now try to claim that */
22489b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
22499b6b563cSPaul Mackerras 	if (mem_start == 0)
22509b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2251966728ddSAnton Blanchard 	mem_end = mem_start + room;
22529b6b563cSPaul Mackerras 
22539b6b563cSPaul Mackerras 	/* Get root of tree */
22549b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
22559b6b563cSPaul Mackerras 	if (root == (phandle)0)
22569b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
22579b6b563cSPaul Mackerras 
22589b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
22599b6b563cSPaul Mackerras 	mem_start = _ALIGN(mem_start, 4);
22609b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
22619b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
22625827d416SAnton Blanchard 	dt_header_start = (unsigned long)hdr;
22639b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
22649b6b563cSPaul Mackerras 
22659b6b563cSPaul Mackerras 	/* Start of strings */
22669b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
22675827d416SAnton Blanchard 	dt_string_start = mem_start;
22689b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
22699b6b563cSPaul Mackerras 
22709b6b563cSPaul Mackerras 	/* Add "linux,phandle" in there, we'll need it */
22719b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
22725827d416SAnton Blanchard 	strcpy(namep, "linux,phandle");
22739b6b563cSPaul Mackerras 	mem_start = (unsigned long)namep + strlen(namep) + 1;
22749b6b563cSPaul Mackerras 
22759b6b563cSPaul Mackerras 	/* Build string array */
22769b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
22779b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
22785827d416SAnton Blanchard 	dt_string_end = mem_start;
22799b6b563cSPaul Mackerras 
22809b6b563cSPaul Mackerras 	/* Build structure */
22819b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
22825827d416SAnton Blanchard 	dt_struct_start = mem_start;
22839b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
22849b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
22859b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
22865827d416SAnton Blanchard 	dt_struct_end = PAGE_ALIGN(mem_start);
22879b6b563cSPaul Mackerras 
22889b6b563cSPaul Mackerras 	/* Finish header */
2289493adffcSBenjamin Herrenschmidt 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2290493adffcSBenjamin Herrenschmidt 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2291493adffcSBenjamin Herrenschmidt 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2292493adffcSBenjamin Herrenschmidt 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2293493adffcSBenjamin Herrenschmidt 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2294493adffcSBenjamin Herrenschmidt 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2295493adffcSBenjamin Herrenschmidt 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2296493adffcSBenjamin Herrenschmidt 	hdr->version = cpu_to_be32(OF_DT_VERSION);
22979b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
2298493adffcSBenjamin Herrenschmidt 	hdr->last_comp_version = cpu_to_be32(0x10);
22999b6b563cSPaul Mackerras 
23004d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
23015827d416SAnton Blanchard 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
23029b6b563cSPaul Mackerras 
23039b6b563cSPaul Mackerras #ifdef DEBUG_PROM
23049b6b563cSPaul Mackerras 	{
23059b6b563cSPaul Mackerras 		int i;
23069b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
23075827d416SAnton Blanchard 		for (i = 0; i < mem_reserve_cnt; i++)
23089b6b563cSPaul Mackerras 			prom_printf("  %x - %x\n",
2309493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].base),
2310493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].size));
23119b6b563cSPaul Mackerras 	}
23129b6b563cSPaul Mackerras #endif
23134d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
23144d1f3f25SJimi Xenidis 	 * since it's too late.
23154d1f3f25SJimi Xenidis 	 */
23165827d416SAnton Blanchard 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
23179b6b563cSPaul Mackerras 
23189b6b563cSPaul Mackerras 	prom_printf("Device tree strings 0x%x -> 0x%x\n",
23195827d416SAnton Blanchard 		    dt_string_start, dt_string_end);
23209b6b563cSPaul Mackerras 	prom_printf("Device tree struct  0x%x -> 0x%x\n",
23215827d416SAnton Blanchard 		    dt_struct_start, dt_struct_end);
23229b6b563cSPaul Mackerras }
23239b6b563cSPaul Mackerras 
232454f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
232554f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
232654f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
232754f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
23289b6b563cSPaul Mackerras {
232954f4ee18SHollis Blanchard 	phandle isa;
2330980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
233154f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2332980a6513SBenjamin Herrenschmidt 	char *name;
233354f4ee18SHollis Blanchard 
2334980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2335980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2336980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2337980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2338980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2339980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2340980a6513SBenjamin Herrenschmidt 	}
234154f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
234254f4ee18SHollis Blanchard 		return;
234354f4ee18SHollis Blanchard 
2344980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2345980a6513SBenjamin Herrenschmidt 		return;
234654f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
234754f4ee18SHollis Blanchard 		== PROM_ERROR)
234854f4ee18SHollis Blanchard 		return;
234954f4ee18SHollis Blanchard 
235054f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
235154f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
235254f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
235354f4ee18SHollis Blanchard 		return;
235454f4ee18SHollis Blanchard 
2355980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
235654f4ee18SHollis Blanchard 
235754f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
235854f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2359980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
236054f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
236154f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
236254f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2363980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
236454f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
236554f4ee18SHollis Blanchard }
23668f101a05SHarry Ciao 
23678f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
23688f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
23698f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
23708f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
23718f101a05SHarry Ciao {
23728f101a05SHarry Ciao 	phandle mc;
23738f101a05SHarry Ciao 	u32 mc_reg[4];
23748f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
23758f101a05SHarry Ciao 	u32 ac, sc;
23768f101a05SHarry Ciao 
23778f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
23788f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
23798f101a05SHarry Ciao 		return;
23808f101a05SHarry Ciao 
23818f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
23828f101a05SHarry Ciao 		return;
23838f101a05SHarry Ciao 
23845827d416SAnton Blanchard 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
23855827d416SAnton Blanchard 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
23868f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
23878f101a05SHarry Ciao 		return;
23888f101a05SHarry Ciao 
23898f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
23908f101a05SHarry Ciao 		return;
23918f101a05SHarry Ciao 
23928f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
23938f101a05SHarry Ciao 		return;
23948f101a05SHarry Ciao 
23958f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
23968f101a05SHarry Ciao 
23978f101a05SHarry Ciao 	mc_reg[0] = 0x0;
23988f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
23998f101a05SHarry Ciao 	mc_reg[2] = 0x0;
24008f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
24018f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
24028f101a05SHarry Ciao }
240354f4ee18SHollis Blanchard #else
240454f4ee18SHollis Blanchard #define fixup_device_tree_maple()
24058f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
240654f4ee18SHollis Blanchard #endif
240754f4ee18SHollis Blanchard 
2408e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2409e4805922SOlaf Hering /*
2410e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2411e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2412556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2413e4805922SOlaf Hering  */
2414e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2415e8c0acf9SBenjamin Herrenschmidt {
2416e4805922SOlaf Hering 	phandle ph;
2417e4805922SOlaf Hering 	u32 prop[6];
241826c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2419e8c0acf9SBenjamin Herrenschmidt 	char *name;
2420e8c0acf9SBenjamin Herrenschmidt 	int rc;
2421e8c0acf9SBenjamin Herrenschmidt 
2422e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2423e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2424e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
242526c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2426e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
242726c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
242826c5032eSBenjamin Herrenschmidt 	}
2429e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2430e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2431e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2432e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2433e8c0acf9SBenjamin Herrenschmidt 
2434e4805922SOlaf Hering 			prop[0] = 0x1;
2435e4805922SOlaf Hering 			prop[1] = 0x0;
2436e4805922SOlaf Hering 			prop[2] = rloc;
2437e4805922SOlaf Hering 			prop[3] = 0x0;
2438e4805922SOlaf Hering 			prop[4] = 0x0;
2439e4805922SOlaf Hering 			prop[5] = 0x00010000;
2440e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2441e4805922SOlaf Hering 		}
2442e4805922SOlaf Hering 	}
2443e4805922SOlaf Hering 
2444e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2445e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2446e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2447e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2448e4805922SOlaf Hering 		prop[0] = 14;
2449e4805922SOlaf Hering 		prop[1] = 0x0;
2450556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2451556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2452556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2453556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2454556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2455556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2456556ecf9bSOlaf Hering 		}
2457e4805922SOlaf Hering 	}
2458e8c0acf9SBenjamin Herrenschmidt }
2459e8c0acf9SBenjamin Herrenschmidt #else
2460e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2461e8c0acf9SBenjamin Herrenschmidt #endif
2462e8c0acf9SBenjamin Herrenschmidt 
24639b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
246454f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
246554f4ee18SHollis Blanchard {
24669b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
24679b6b563cSPaul Mackerras 	u32 u3_rev;
24689b6b563cSPaul Mackerras 	u32 interrupts[2];
24699b6b563cSPaul Mackerras 	u32 parent;
24709b6b563cSPaul Mackerras 
24719b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
24729b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
24739b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
24749b6b563cSPaul Mackerras 		return;
24759b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
24769b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
24779b6b563cSPaul Mackerras 		return;
24789b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
24799b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
24809b6b563cSPaul Mackerras 		return;
24819b6b563cSPaul Mackerras 
24829b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
24839b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
24849b6b563cSPaul Mackerras 	    == PROM_ERROR)
24859b6b563cSPaul Mackerras 		return;
24867d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
24879b6b563cSPaul Mackerras 		return;
24889b6b563cSPaul Mackerras 	/* does it need fixup ? */
24899b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
24909b6b563cSPaul Mackerras 		return;
24919b6b563cSPaul Mackerras 
24929b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
24939b6b563cSPaul Mackerras 
24949b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
24959b6b563cSPaul Mackerras 	interrupts[0] = 0;
24969b6b563cSPaul Mackerras 	interrupts[1] = 1;
2497a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2498a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
24999b6b563cSPaul Mackerras 	parent = (u32)mpic;
2500a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2501a23414beSPaul Mackerras 		     &parent, sizeof(parent));
25029b6b563cSPaul Mackerras }
250354f4ee18SHollis Blanchard #else
250454f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
250554f4ee18SHollis Blanchard #endif
25069b6b563cSPaul Mackerras 
250788fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
250894d2dde7SGrant Likely /*
250994d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
251094d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
251194d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
251294d2dde7SGrant Likely  * ethernet node.
251394d2dde7SGrant Likely  */
251494d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
251588fd2a9dSSylvain Munaut {
251688fd2a9dSSylvain Munaut 	u32 node;
251788fd2a9dSSylvain Munaut 	char prop[64];
251894d2dde7SGrant Likely 	int rv;
251988fd2a9dSSylvain Munaut 
252094d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
252194d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
252288fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
252388fd2a9dSSylvain Munaut 		return;
252488fd2a9dSSylvain Munaut 
252594d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
252694d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
252794d2dde7SGrant Likely 	if (!rv)
252888fd2a9dSSylvain Munaut 		return;
252988fd2a9dSSylvain Munaut 
253094d2dde7SGrant Likely 	/*
253194d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
253294d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
253394d2dde7SGrant Likely 	 */
253488fd2a9dSSylvain Munaut 
253594d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
25366f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
25376f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
25386f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
25396f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
25406f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
25416f4347c9SOlaf Hering 			" new-device"
25426f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
25436f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
254494d2dde7SGrant Likely 				" s\" mdio\" device-name"
254594d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
25466f4347c9SOlaf Hering 				" s\" compatible\" property"
25476f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
25486f4347c9SOlaf Hering 				" 0x2 encode-int"
25496f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
25506f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
25516f4347c9SOlaf Hering 				" s\" interrupts\" property"
25526f4347c9SOlaf Hering 			" finish-device");
25536f4347c9SOlaf Hering 	};
25546f4347c9SOlaf Hering 
255594d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
255694d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
255794d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
255894d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
25596f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
25606f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
25616f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
25626f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
25636f4347c9SOlaf Hering 			" new-device"
25646f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
25656f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
25666f4347c9SOlaf Hering 				" my-self"
25676f4347c9SOlaf Hering 				" ihandle>phandle"
25686f4347c9SOlaf Hering 			" finish-device"
25696f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
25706f4347c9SOlaf Hering 				" encode-int"
25716f4347c9SOlaf Hering 				" s\" phy-handle\" property"
25726f4347c9SOlaf Hering 			" device-end");
25736f4347c9SOlaf Hering 	}
257494d2dde7SGrant Likely }
25756f4347c9SOlaf Hering 
257694d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
257794d2dde7SGrant Likely {
257894d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
257994d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
258094d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
258194d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
258294d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
258394d2dde7SGrant Likely 	u32 node;
258494d2dde7SGrant Likely 	char prop[64];
258594d2dde7SGrant Likely 	int rv, len;
258694d2dde7SGrant Likely 
258794d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
258894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
258994d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
259094d2dde7SGrant Likely 		return;
259194d2dde7SGrant Likely 
259294d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
259394d2dde7SGrant Likely 	if (rv == PROM_ERROR)
259494d2dde7SGrant Likely 		return;
259594d2dde7SGrant Likely 	if (strcmp(prop, "EFIKA5K2"))
259694d2dde7SGrant Likely 		return;
259794d2dde7SGrant Likely 
259894d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
259994d2dde7SGrant Likely 
260094d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
260194d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
260294d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
260394d2dde7SGrant Likely 	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
260494d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
260594d2dde7SGrant Likely 
26067f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
26077f4392cdSDavid Woodhouse 	   fix that too */
26087f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
26097f4392cdSDavid Woodhouse 	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
26107f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
26117f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
26127f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
26137f4392cdSDavid Woodhouse 
261494d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
261594d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
261694d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
261794d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
261894d2dde7SGrant Likely 		if (len == 12) {
261994d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
262094d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
262194d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
262294d2dde7SGrant Likely 		}
262394d2dde7SGrant Likely 	}
262494d2dde7SGrant Likely 
262594d2dde7SGrant Likely 	/* Fixup sound interrupts property */
262694d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
262794d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
262894d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
262994d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
263094d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
263194d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
263294d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
263394d2dde7SGrant Likely 		}
263494d2dde7SGrant Likely 	}
263594d2dde7SGrant Likely 
263694d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
263794d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
263888fd2a9dSSylvain Munaut }
263988fd2a9dSSylvain Munaut #else
264088fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
264188fd2a9dSSylvain Munaut #endif
264288fd2a9dSSylvain Munaut 
264354f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
264454f4ee18SHollis Blanchard {
264554f4ee18SHollis Blanchard 	fixup_device_tree_maple();
26468f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
2647e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
264854f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
264988fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
265054f4ee18SHollis Blanchard }
26519b6b563cSPaul Mackerras 
26529b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
26539b6b563cSPaul Mackerras {
2654493adffcSBenjamin Herrenschmidt 	__be32 rval;
26559b6b563cSPaul Mackerras 	ihandle prom_cpu;
26569b6b563cSPaul Mackerras 	phandle cpu_pkg;
26579b6b563cSPaul Mackerras 
2658493adffcSBenjamin Herrenschmidt 	rval = 0;
2659493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
2660a575b807SPaul Mackerras 		return;
2661493adffcSBenjamin Herrenschmidt 	prom_cpu = be32_to_cpu(rval);
26629b6b563cSPaul Mackerras 
26639b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
26649b6b563cSPaul Mackerras 
2665493adffcSBenjamin Herrenschmidt 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
2666493adffcSBenjamin Herrenschmidt 	prom.cpu = be32_to_cpu(rval);
26679b6b563cSPaul Mackerras 
26685827d416SAnton Blanchard 	prom_debug("Booting CPU hw index = %lu\n", prom.cpu);
26699b6b563cSPaul Mackerras }
26709b6b563cSPaul Mackerras 
26719b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
26729b6b563cSPaul Mackerras {
26739b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
26749b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
2675493adffcSBenjamin Herrenschmidt 		__be64 val;
26769b6b563cSPaul Mackerras 
26775827d416SAnton Blanchard 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
26785827d416SAnton Blanchard 		prom_initrd_end = prom_initrd_start + r4;
26799b6b563cSPaul Mackerras 
2680493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_start);
26815827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
2682a23414beSPaul Mackerras 			     &val, sizeof(val));
2683493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_end);
26845827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
2685a23414beSPaul Mackerras 			     &val, sizeof(val));
26869b6b563cSPaul Mackerras 
26875827d416SAnton Blanchard 		reserve_mem(prom_initrd_start,
26885827d416SAnton Blanchard 			    prom_initrd_end - prom_initrd_start);
26899b6b563cSPaul Mackerras 
26905827d416SAnton Blanchard 		prom_debug("initrd_start=0x%x\n", prom_initrd_start);
26915827d416SAnton Blanchard 		prom_debug("initrd_end=0x%x\n", prom_initrd_end);
26929b6b563cSPaul Mackerras 	}
26939b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
26949b6b563cSPaul Mackerras }
26959b6b563cSPaul Mackerras 
26965ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64
26975ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE
26985ac47f7aSAnton Blanchard static void reloc_toc(void)
26995ac47f7aSAnton Blanchard {
27005ac47f7aSAnton Blanchard }
27015ac47f7aSAnton Blanchard 
27025ac47f7aSAnton Blanchard static void unreloc_toc(void)
27035ac47f7aSAnton Blanchard {
27045ac47f7aSAnton Blanchard }
27055ac47f7aSAnton Blanchard #else
27061674400aSAnton Blanchard static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
27075ac47f7aSAnton Blanchard {
27085ac47f7aSAnton Blanchard 	unsigned long i;
27091674400aSAnton Blanchard 	unsigned long *toc_entry;
27101674400aSAnton Blanchard 
27111674400aSAnton Blanchard 	/* Get the start of the TOC by using r2 directly. */
27121674400aSAnton Blanchard 	asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
27135ac47f7aSAnton Blanchard 
27145ac47f7aSAnton Blanchard 	for (i = 0; i < nr_entries; i++) {
27155ac47f7aSAnton Blanchard 		*toc_entry = *toc_entry + offset;
27165ac47f7aSAnton Blanchard 		toc_entry++;
27175ac47f7aSAnton Blanchard 	}
27185ac47f7aSAnton Blanchard }
27195ac47f7aSAnton Blanchard 
27205ac47f7aSAnton Blanchard static void reloc_toc(void)
27215ac47f7aSAnton Blanchard {
27225ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
27235ac47f7aSAnton Blanchard 	unsigned long nr_entries =
27245ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
27255ac47f7aSAnton Blanchard 
27261674400aSAnton Blanchard 	__reloc_toc(offset, nr_entries);
27275ac47f7aSAnton Blanchard 
27285ac47f7aSAnton Blanchard 	mb();
27295ac47f7aSAnton Blanchard }
27305ac47f7aSAnton Blanchard 
27315ac47f7aSAnton Blanchard static void unreloc_toc(void)
27325ac47f7aSAnton Blanchard {
27335ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
27345ac47f7aSAnton Blanchard 	unsigned long nr_entries =
27355ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
27365ac47f7aSAnton Blanchard 
27375ac47f7aSAnton Blanchard 	mb();
27385ac47f7aSAnton Blanchard 
27391674400aSAnton Blanchard 	__reloc_toc(-offset, nr_entries);
27405ac47f7aSAnton Blanchard }
27415ac47f7aSAnton Blanchard #endif
27425ac47f7aSAnton Blanchard #endif
274327f44888SBenjamin Herrenschmidt 
27449b6b563cSPaul Mackerras /*
27459b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
27469b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
27479b6b563cSPaul Mackerras  */
27489b6b563cSPaul Mackerras 
27499b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
27509b6b563cSPaul Mackerras 			       unsigned long pp,
2751549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
2752549e8152SPaul Mackerras 			       unsigned long kbase)
27539b6b563cSPaul Mackerras {
27549b6b563cSPaul Mackerras 	unsigned long hdr;
27559b6b563cSPaul Mackerras 
27569b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
2757549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
27589b6b563cSPaul Mackerras 	reloc_got2(offset);
27595ac47f7aSAnton Blanchard #else
27605ac47f7aSAnton Blanchard 	reloc_toc();
27619b6b563cSPaul Mackerras #endif
27629b6b563cSPaul Mackerras 
27639b6b563cSPaul Mackerras 	/*
27649b6b563cSPaul Mackerras 	 * First zero the BSS
27659b6b563cSPaul Mackerras 	 */
27665827d416SAnton Blanchard 	memset(&__bss_start, 0, __bss_stop - __bss_start);
27679b6b563cSPaul Mackerras 
27689b6b563cSPaul Mackerras 	/*
27699b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
27709b6b563cSPaul Mackerras 	 * like /chosen
27719b6b563cSPaul Mackerras 	 */
27729b6b563cSPaul Mackerras 	prom_init_client_services(pp);
27739b6b563cSPaul Mackerras 
27749b6b563cSPaul Mackerras 	/*
2775a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
2776a23414beSPaul Mackerras 	 * and other workarounds
2777a23414beSPaul Mackerras 	 */
2778a23414beSPaul Mackerras 	prom_find_mmu();
2779a23414beSPaul Mackerras 
2780a23414beSPaul Mackerras 	/*
27819b6b563cSPaul Mackerras 	 * Init prom stdout device
27829b6b563cSPaul Mackerras 	 */
27839b6b563cSPaul Mackerras 	prom_init_stdout();
27849b6b563cSPaul Mackerras 
27855827d416SAnton Blanchard 	prom_printf("Preparing to boot %s", linux_banner);
2786e7943fbbSMichael Ellerman 
27879b6b563cSPaul Mackerras 	/*
27889b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
27899b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
27909b6b563cSPaul Mackerras 	 */
27915827d416SAnton Blanchard 	of_platform = prom_find_machine_type();
27925827d416SAnton Blanchard 	prom_printf("Detected machine type: %x\n", of_platform);
27939b6b563cSPaul Mackerras 
27940f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
2795add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
2796add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
2797add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
2798549e8152SPaul Mackerras #endif
2799add60ef3SOlaf Hering 
2800add60ef3SOlaf Hering 	/*
2801add60ef3SOlaf Hering 	 * Check for an initrd
2802add60ef3SOlaf Hering 	 */
2803add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
2804add60ef3SOlaf Hering 
280527f44888SBenjamin Herrenschmidt #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
28069b6b563cSPaul Mackerras 	/*
28079b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
28089b6b563cSPaul Mackerras 	 */
28095827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES ||
28105827d416SAnton Blanchard 	    of_platform == PLATFORM_PSERIES_LPAR)
28119b6b563cSPaul Mackerras 		prom_send_capabilities();
28129b6b563cSPaul Mackerras #endif
28139b6b563cSPaul Mackerras 
28149b6b563cSPaul Mackerras 	/*
2815f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
28169b6b563cSPaul Mackerras 	 */
28175827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC)
2818549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
28199b6b563cSPaul Mackerras 
28209b6b563cSPaul Mackerras 	/*
28219b6b563cSPaul Mackerras 	 * Do early parsing of command line
28229b6b563cSPaul Mackerras 	 */
28239b6b563cSPaul Mackerras 	early_cmdline_parse();
28249b6b563cSPaul Mackerras 
28259b6b563cSPaul Mackerras 	/*
28269b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
28279b6b563cSPaul Mackerras 	 */
28289b6b563cSPaul Mackerras 	prom_init_mem();
28299b6b563cSPaul Mackerras 
28309b6b563cSPaul Mackerras 	/*
28319b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
28329b6b563cSPaul Mackerras 	 */
28339b6b563cSPaul Mackerras 	prom_find_boot_cpu();
28349b6b563cSPaul Mackerras 
28359b6b563cSPaul Mackerras 	/*
28369b6b563cSPaul Mackerras 	 * Initialize display devices
28379b6b563cSPaul Mackerras 	 */
28389b6b563cSPaul Mackerras 	prom_check_displays();
28399b6b563cSPaul Mackerras 
2840493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
28419b6b563cSPaul Mackerras 	/*
28429b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
28439b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
28449b6b563cSPaul Mackerras 	 * available for us here...
28459b6b563cSPaul Mackerras 	 */
28465827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES)
28479b6b563cSPaul Mackerras 		prom_initialize_tce_table();
28489b6b563cSPaul Mackerras #endif
28499b6b563cSPaul Mackerras 
28509b6b563cSPaul Mackerras 	/*
285127f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
285227f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
28539b6b563cSPaul Mackerras 	 */
28545827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
28555827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
28569b6b563cSPaul Mackerras 		prom_instantiate_rtas();
285727f44888SBenjamin Herrenschmidt 
285827f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
2859493adffcSBenjamin Herrenschmidt 	if (of_platform == PLATFORM_OPAL)
28606e35d5daSBenjamin Herrenschmidt 		prom_instantiate_opal();
2861493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV */
286227f44888SBenjamin Herrenschmidt 
28634a727429SAshley Lai #ifdef CONFIG_PPC64
28644a727429SAshley Lai 	/* instantiate sml */
28654a727429SAshley Lai 	prom_instantiate_sml();
28664a727429SAshley Lai #endif
28674a727429SAshley Lai 
286827f44888SBenjamin Herrenschmidt 	/*
286927f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
287027f44888SBenjamin Herrenschmidt 	 *
287127f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
2872dbe78b40SBenjamin Herrenschmidt 	 *
2873dbe78b40SBenjamin Herrenschmidt 	 * (This must be done after instanciating RTAS)
287427f44888SBenjamin Herrenschmidt 	 */
28755827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
28765827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
287727f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
28789b6b563cSPaul Mackerras 
28799b6b563cSPaul Mackerras 	/*
28809b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
28819b6b563cSPaul Mackerras 	 */
2882493adffcSBenjamin Herrenschmidt 	if (prom_memory_limit) {
2883493adffcSBenjamin Herrenschmidt 		__be64 val = cpu_to_be64(prom_memory_limit);
28845827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
2885493adffcSBenjamin Herrenschmidt 			     &val, sizeof(val));
2886493adffcSBenjamin Herrenschmidt 	}
28879b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
28885827d416SAnton Blanchard 	if (prom_iommu_off)
28895827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
2890a23414beSPaul Mackerras 			     NULL, 0);
28919b6b563cSPaul Mackerras 
28925827d416SAnton Blanchard 	if (prom_iommu_force_on)
28935827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
2894a23414beSPaul Mackerras 			     NULL, 0);
28959b6b563cSPaul Mackerras 
28965827d416SAnton Blanchard 	if (prom_tce_alloc_start) {
28975827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
28985827d416SAnton Blanchard 			     &prom_tce_alloc_start,
28999b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
29005827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
29015827d416SAnton Blanchard 			     &prom_tce_alloc_end,
29029b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
29039b6b563cSPaul Mackerras 	}
29049b6b563cSPaul Mackerras #endif
29059b6b563cSPaul Mackerras 
29069b6b563cSPaul Mackerras 	/*
29079b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
29089b6b563cSPaul Mackerras 	 */
29099b6b563cSPaul Mackerras 	fixup_device_tree();
29109b6b563cSPaul Mackerras 
29119b6b563cSPaul Mackerras 	/*
29129b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
29139b6b563cSPaul Mackerras 	 */
29149b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
29159b6b563cSPaul Mackerras 	flatten_device_tree();
29169b6b563cSPaul Mackerras 
29173825ac0eSPaul Mackerras 	/*
29183825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
29193825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
292040dfef66SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101). It
292140dfef66SBenjamin Herrenschmidt 	 * appears that the OPAL version of OFW doesn't like it either.
29223825ac0eSPaul Mackerras 	 */
29235827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
29245827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
29259b6b563cSPaul Mackerras 		prom_close_stdin();
29269b6b563cSPaul Mackerras 
29279b6b563cSPaul Mackerras 	/*
29289b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
29299b6b563cSPaul Mackerras 	 * devices etc...
29309b6b563cSPaul Mackerras 	 */
29317e862d7eSMichael Ellerman 	prom_printf("Quiescing Open Firmware ...\n");
29329b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
29339b6b563cSPaul Mackerras 
29349b6b563cSPaul Mackerras 	/*
29359b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
29369b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
29379b6b563cSPaul Mackerras 	 * is common to us and kexec
29389b6b563cSPaul Mackerras 	 */
29395827d416SAnton Blanchard 	hdr = dt_header_start;
294040dfef66SBenjamin Herrenschmidt 
294140dfef66SBenjamin Herrenschmidt 	/* Don't print anything after quiesce under OPAL, it crashes OFW */
29425827d416SAnton Blanchard 	if (of_platform != PLATFORM_OPAL) {
2943*7d70c63cSBenjamin Herrenschmidt 		prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
29449b6b563cSPaul Mackerras 		prom_debug("->dt_header_start=0x%x\n", hdr);
294540dfef66SBenjamin Herrenschmidt 	}
29469b6b563cSPaul Mackerras 
29479b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
29489b6b563cSPaul Mackerras 	reloc_got2(-offset);
29495ac47f7aSAnton Blanchard #else
29505ac47f7aSAnton Blanchard 	unreloc_toc();
29519b6b563cSPaul Mackerras #endif
29529b6b563cSPaul Mackerras 
29536e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
29546e35d5daSBenjamin Herrenschmidt 	/* OPAL early debug gets the OPAL base & entry in r8 and r9 */
29556e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0,
29565827d416SAnton Blanchard 		prom_opal_base, prom_opal_entry);
29576e35d5daSBenjamin Herrenschmidt #else
29586e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
29596e35d5daSBenjamin Herrenschmidt #endif
29609b6b563cSPaul Mackerras 
29619b6b563cSPaul Mackerras 	return 0;
29629b6b563cSPaul Mackerras }
2963