xref: /linux/arch/powerpc/kernel/prom_init.c (revision f1f208e54d08ccf00121c700a9bb1fe3e55b3a51)
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 
18054f367aSDaniel Axtens /* we cannot use FORTIFY as it brings in new symbols */
19054f367aSDaniel Axtens #define __NO_FORTIFY
20054f367aSDaniel Axtens 
219b6b563cSPaul Mackerras #include <stdarg.h>
229b6b563cSPaul Mackerras #include <linux/kernel.h>
239b6b563cSPaul Mackerras #include <linux/string.h>
249b6b563cSPaul Mackerras #include <linux/init.h>
259b6b563cSPaul Mackerras #include <linux/threads.h>
269b6b563cSPaul Mackerras #include <linux/spinlock.h>
279b6b563cSPaul Mackerras #include <linux/types.h>
289b6b563cSPaul Mackerras #include <linux/pci.h>
299b6b563cSPaul Mackerras #include <linux/proc_fs.h>
309b6b563cSPaul Mackerras #include <linux/delay.h>
319b6b563cSPaul Mackerras #include <linux/initrd.h>
329b6b563cSPaul Mackerras #include <linux/bitops.h>
339b6b563cSPaul Mackerras #include <asm/prom.h>
349b6b563cSPaul Mackerras #include <asm/rtas.h>
359b6b563cSPaul Mackerras #include <asm/page.h>
369b6b563cSPaul Mackerras #include <asm/processor.h>
379b6b563cSPaul Mackerras #include <asm/irq.h>
389b6b563cSPaul Mackerras #include <asm/io.h>
399b6b563cSPaul Mackerras #include <asm/smp.h>
409b6b563cSPaul Mackerras #include <asm/mmu.h>
419b6b563cSPaul Mackerras #include <asm/pgtable.h>
429b6b563cSPaul Mackerras #include <asm/iommu.h>
439b6b563cSPaul Mackerras #include <asm/btext.h>
449b6b563cSPaul Mackerras #include <asm/sections.h>
459b6b563cSPaul Mackerras #include <asm/machdep.h>
460545d543SDaniel Axtens #include <asm/asm-prototypes.h>
479b6b563cSPaul Mackerras 
489b6b563cSPaul Mackerras #include <linux/linux_logo.h>
499b6b563cSPaul Mackerras 
50e63334e5SBenjamin Herrenschmidt /* All of prom_init bss lives here */
515f69e388SBenjamin Herrenschmidt #define __prombss __section(.bss.prominit)
52e63334e5SBenjamin Herrenschmidt 
539b6b563cSPaul Mackerras /*
549b6b563cSPaul Mackerras  * Eventually bump that one up
559b6b563cSPaul Mackerras  */
569b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE	0x100000
579b6b563cSPaul Mackerras 
589b6b563cSPaul Mackerras /*
599b6b563cSPaul Mackerras  * This is the size of the local memory reserve map that gets copied
609b6b563cSPaul Mackerras  * into the boot params passed to the kernel. That size is totally
619b6b563cSPaul Mackerras  * flexible as the kernel just reads the list until it encounters an
629b6b563cSPaul Mackerras  * entry with size 0, so it can be changed without breaking binary
639b6b563cSPaul Mackerras  * compatibility
649b6b563cSPaul Mackerras  */
659b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE	8
669b6b563cSPaul Mackerras 
679b6b563cSPaul Mackerras /*
689b6b563cSPaul Mackerras  * prom_init() is called very early on, before the kernel text
699b6b563cSPaul Mackerras  * and data have been mapped to KERNELBASE.  At this point the code
709b6b563cSPaul Mackerras  * is running at whatever address it has been loaded at.
719b6b563cSPaul Mackerras  * On ppc32 we compile with -mrelocatable, which means that references
729b6b563cSPaul Mackerras  * to extern and static variables get relocated automatically.
735ac47f7aSAnton Blanchard  * ppc64 objects are always relocatable, we just need to relocate the
745ac47f7aSAnton Blanchard  * TOC.
759b6b563cSPaul Mackerras  *
769b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
779b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
789b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
799b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
809b6b563cSPaul Mackerras  *
819b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
829b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
839b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
849b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
859b6b563cSPaul Mackerras  */
865ac47f7aSAnton Blanchard #define ADDR(x)		(u32)(unsigned long)(x)
875ac47f7aSAnton Blanchard 
889b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
89a23414beSPaul Mackerras #define OF_WORKAROUNDS	0
909b6b563cSPaul Mackerras #else
91a23414beSPaul Mackerras #define OF_WORKAROUNDS	of_workarounds
92e63334e5SBenjamin Herrenschmidt static int of_workarounds __prombss;
939b6b563cSPaul Mackerras #endif
949b6b563cSPaul Mackerras 
95a23414beSPaul Mackerras #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
96a23414beSPaul Mackerras #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
97a23414beSPaul Mackerras 
989b6b563cSPaul Mackerras #define PROM_BUG() do {						\
999b6b563cSPaul Mackerras         prom_printf("kernel BUG at %s line 0x%x!\n",		\
1005827d416SAnton Blanchard 		    __FILE__, __LINE__);			\
1019b6b563cSPaul Mackerras         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
1029b6b563cSPaul Mackerras } while (0)
1039b6b563cSPaul Mackerras 
1049b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1059b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1069b6b563cSPaul Mackerras #else
10785aa4b98SMathieu Malaterre #define prom_debug(x...)	do { } while (0)
1089b6b563cSPaul Mackerras #endif
1099b6b563cSPaul Mackerras 
1109b6b563cSPaul Mackerras 
1119b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1129b6b563cSPaul Mackerras 
1139b6b563cSPaul Mackerras struct prom_args {
114493adffcSBenjamin Herrenschmidt         __be32 service;
115493adffcSBenjamin Herrenschmidt         __be32 nargs;
116493adffcSBenjamin Herrenschmidt         __be32 nret;
117493adffcSBenjamin Herrenschmidt         __be32 args[10];
1189b6b563cSPaul Mackerras };
1199b6b563cSPaul Mackerras 
1209b6b563cSPaul Mackerras struct prom_t {
1219b6b563cSPaul Mackerras 	ihandle root;
122a23414beSPaul Mackerras 	phandle chosen;
1239b6b563cSPaul Mackerras 	int cpu;
1249b6b563cSPaul Mackerras 	ihandle stdout;
125a575b807SPaul Mackerras 	ihandle mmumap;
126a23414beSPaul Mackerras 	ihandle memory;
1279b6b563cSPaul Mackerras };
1289b6b563cSPaul Mackerras 
1299b6b563cSPaul Mackerras struct mem_map_entry {
130493adffcSBenjamin Herrenschmidt 	__be64	base;
131493adffcSBenjamin Herrenschmidt 	__be64	size;
1329b6b563cSPaul Mackerras };
1339b6b563cSPaul Mackerras 
134493adffcSBenjamin Herrenschmidt typedef __be32 cell_t;
1359b6b563cSPaul Mackerras 
1366e35d5daSBenjamin Herrenschmidt extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
1376e35d5daSBenjamin Herrenschmidt 		    unsigned long r6, unsigned long r7, unsigned long r8,
1386e35d5daSBenjamin Herrenschmidt 		    unsigned long r9);
1399b6b563cSPaul Mackerras 
1409b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
141c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1429b6b563cSPaul Mackerras #else
143c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1449b6b563cSPaul Mackerras {
145c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1469b6b563cSPaul Mackerras }
1479b6b563cSPaul Mackerras #endif
1489b6b563cSPaul Mackerras 
1499b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1509b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1519b6b563cSPaul Mackerras 
1529b6b563cSPaul Mackerras /* prom structure */
153e63334e5SBenjamin Herrenschmidt static struct prom_t __prombss prom;
1549b6b563cSPaul Mackerras 
155e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_entry;
1569b6b563cSPaul Mackerras 
1579b6b563cSPaul Mackerras #define PROM_SCRATCH_SIZE 256
1589b6b563cSPaul Mackerras 
159e63334e5SBenjamin Herrenschmidt static char __prombss of_stdout_device[256];
160e63334e5SBenjamin Herrenschmidt static char __prombss prom_scratch[PROM_SCRATCH_SIZE];
1619b6b563cSPaul Mackerras 
162e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_header_start;
163e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_struct_start, dt_struct_end;
164e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_string_start, dt_string_end;
1659b6b563cSPaul Mackerras 
166e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_initrd_start, prom_initrd_end;
1679b6b563cSPaul Mackerras 
1689b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
169e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_force_on;
170e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_off;
171e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_start;
172e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_end;
1739b6b563cSPaul Mackerras #endif
1749b6b563cSPaul Mackerras 
175c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
1768ca2d515SBenjamin Herrenschmidt static bool __prombss prom_radix_disable;
177c886087cSBenjamin Herrenschmidt #endif
178014d02cbSSuraj Jitindar Singh 
179014d02cbSSuraj Jitindar Singh struct platform_support {
180014d02cbSSuraj Jitindar Singh 	bool hash_mmu;
181014d02cbSSuraj Jitindar Singh 	bool radix_mmu;
182014d02cbSSuraj Jitindar Singh 	bool radix_gtse;
183ac5e5a54SCédric Le Goater 	bool xive;
184014d02cbSSuraj Jitindar Singh };
185014d02cbSSuraj Jitindar Singh 
186e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
187e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
188e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
189e8222502SBenjamin Herrenschmidt  */
190e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
191e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
192e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
193e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
194e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
195e8222502SBenjamin Herrenschmidt 
196e63334e5SBenjamin Herrenschmidt static int __prombss of_platform;
1979b6b563cSPaul Mackerras 
198e63334e5SBenjamin Herrenschmidt static char __prombss prom_cmd_line[COMMAND_LINE_SIZE];
1999b6b563cSPaul Mackerras 
200e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_memory_limit;
201cf68787bSBenjamin Krill 
202e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top;
203e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top_high;
204e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_bottom;
205e63334e5SBenjamin Herrenschmidt static unsigned long __prombss rmo_top;
206e63334e5SBenjamin Herrenschmidt static unsigned long __prombss ram_top;
2079b6b563cSPaul Mackerras 
208e63334e5SBenjamin Herrenschmidt static struct mem_map_entry __prombss mem_reserve_map[MEM_RESERVE_MAP_SIZE];
209e63334e5SBenjamin Herrenschmidt static int __prombss mem_reserve_cnt;
2109b6b563cSPaul Mackerras 
211e63334e5SBenjamin Herrenschmidt static cell_t __prombss regbuf[1024];
2129b6b563cSPaul Mackerras 
2138ca2d515SBenjamin Herrenschmidt static bool  __prombss rtas_has_query_cpu_stopped;
214dbe78b40SBenjamin Herrenschmidt 
2159b6b563cSPaul Mackerras 
2169b6b563cSPaul Mackerras /*
2179b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2189b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2199b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2209b6b563cSPaul Mackerras  * it is valid
2219b6b563cSPaul Mackerras  */
2229b6b563cSPaul Mackerras 
2239b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2249b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2259b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2269b6b563cSPaul Mackerras 
2279b6b563cSPaul Mackerras 
2289b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
2299b6b563cSPaul Mackerras  * firmware.
2309b6b563cSPaul Mackerras  */
2319b6b563cSPaul Mackerras 
2329b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
2339b6b563cSPaul Mackerras {
2349b6b563cSPaul Mackerras 	int i;
2359b6b563cSPaul Mackerras 	struct prom_args args;
2369b6b563cSPaul Mackerras 	va_list list;
2379b6b563cSPaul Mackerras 
238493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
239493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
240493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
2419b6b563cSPaul Mackerras 
2429b6b563cSPaul Mackerras 	va_start(list, nret);
2439b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
244493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
2459b6b563cSPaul Mackerras 	va_end(list);
2469b6b563cSPaul Mackerras 
2479b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
2489b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
2499b6b563cSPaul Mackerras 
2505827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
251c4988820SPaul Mackerras 		return PROM_ERROR;
2529b6b563cSPaul Mackerras 
253493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
2549b6b563cSPaul Mackerras }
2559b6b563cSPaul Mackerras 
2569b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
2579b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
2589b6b563cSPaul Mackerras {
2599b6b563cSPaul Mackerras 	int i;
2609b6b563cSPaul Mackerras 	struct prom_args args;
2619b6b563cSPaul Mackerras 	va_list list;
2629b6b563cSPaul Mackerras 
263493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
264493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
265493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
2669b6b563cSPaul Mackerras 
2679b6b563cSPaul Mackerras 	va_start(list, rets);
2689b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
269493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
2709b6b563cSPaul Mackerras 	va_end(list);
2719b6b563cSPaul Mackerras 
2729b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
273ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
2749b6b563cSPaul Mackerras 
2755827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
276c4988820SPaul Mackerras 		return PROM_ERROR;
2779b6b563cSPaul Mackerras 
2789b6b563cSPaul Mackerras 	if (rets != NULL)
2799b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
280493adffcSBenjamin Herrenschmidt 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
2819b6b563cSPaul Mackerras 
282493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
2839b6b563cSPaul Mackerras }
2849b6b563cSPaul Mackerras 
2859b6b563cSPaul Mackerras 
2869b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
2879b6b563cSPaul Mackerras {
2889b6b563cSPaul Mackerras 	const char *p, *q;
2899b6b563cSPaul Mackerras 
2905827d416SAnton Blanchard 	if (prom.stdout == 0)
2919b6b563cSPaul Mackerras 		return;
2929b6b563cSPaul Mackerras 
2939b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
2949b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
2959b6b563cSPaul Mackerras 			;
2969b6b563cSPaul Mackerras 		if (q > p)
2975827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
2989b6b563cSPaul Mackerras 		if (*q == 0)
2999b6b563cSPaul Mackerras 			break;
3009b6b563cSPaul Mackerras 		++q;
3015827d416SAnton Blanchard 		call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
3029b6b563cSPaul Mackerras 	}
3039b6b563cSPaul Mackerras }
3049b6b563cSPaul Mackerras 
3059b6b563cSPaul Mackerras 
3068af1da40SMathieu Malaterre /*
3078af1da40SMathieu Malaterre  * Both prom_print_hex & prom_print_dec takes an unsigned long as input so that
3088af1da40SMathieu Malaterre  * we do not need __udivdi3 or __umoddi3 on 32bits.
3098af1da40SMathieu Malaterre  */
3109b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
3119b6b563cSPaul Mackerras {
3129b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
3139b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
3149b6b563cSPaul Mackerras 
3159b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
3169b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
3179b6b563cSPaul Mackerras 		if (buf[i] > '9')
3189b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
3199b6b563cSPaul Mackerras 		val >>= 4;
3209b6b563cSPaul Mackerras 	}
3219b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
3225827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf, nibbles);
3239b6b563cSPaul Mackerras }
3249b6b563cSPaul Mackerras 
3252c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */
3262c48a7d6SMichael Neuling #define UL_DIGITS 21
3272c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val)
3282c48a7d6SMichael Neuling {
3292c48a7d6SMichael Neuling 	int i, size;
3302c48a7d6SMichael Neuling 	char buf[UL_DIGITS+1];
3312c48a7d6SMichael Neuling 
3322c48a7d6SMichael Neuling 	for (i = UL_DIGITS-1; i >= 0;  i--) {
3332c48a7d6SMichael Neuling 		buf[i] = (val % 10) + '0';
3342c48a7d6SMichael Neuling 		val = val/10;
3352c48a7d6SMichael Neuling 		if (val == 0)
3362c48a7d6SMichael Neuling 			break;
3372c48a7d6SMichael Neuling 	}
3382c48a7d6SMichael Neuling 	/* shift stuff down */
3392c48a7d6SMichael Neuling 	size = UL_DIGITS - i;
3405827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf+i, size);
3412c48a7d6SMichael Neuling }
3429b6b563cSPaul Mackerras 
343eae5f709SMathieu Malaterre __printf(1, 2)
3449b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
3459b6b563cSPaul Mackerras {
3469b6b563cSPaul Mackerras 	const char *p, *q, *s;
3479b6b563cSPaul Mackerras 	va_list args;
3489b6b563cSPaul Mackerras 	unsigned long v;
349af277149SBenjamin Herrenschmidt 	long vs;
3508af1da40SMathieu Malaterre 	int n = 0;
3519b6b563cSPaul Mackerras 
3529b6b563cSPaul Mackerras 	va_start(args, format);
3539b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
3549b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
3559b6b563cSPaul Mackerras 			;
3569b6b563cSPaul Mackerras 		if (q > p)
3575827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
3589b6b563cSPaul Mackerras 		if (*q == 0)
3599b6b563cSPaul Mackerras 			break;
3609b6b563cSPaul Mackerras 		if (*q == '\n') {
3619b6b563cSPaul Mackerras 			++q;
3625827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout,
3639b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
3649b6b563cSPaul Mackerras 			continue;
3659b6b563cSPaul Mackerras 		}
3669b6b563cSPaul Mackerras 		++q;
3679b6b563cSPaul Mackerras 		if (*q == 0)
3689b6b563cSPaul Mackerras 			break;
3698af1da40SMathieu Malaterre 		while (*q == 'l') {
3708af1da40SMathieu Malaterre 			++q;
3718af1da40SMathieu Malaterre 			++n;
3728af1da40SMathieu Malaterre 		}
3739b6b563cSPaul Mackerras 		switch (*q) {
3749b6b563cSPaul Mackerras 		case 's':
3759b6b563cSPaul Mackerras 			++q;
3769b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
3779b6b563cSPaul Mackerras 			prom_print(s);
3789b6b563cSPaul Mackerras 			break;
3799b6b563cSPaul Mackerras 		case 'x':
3809b6b563cSPaul Mackerras 			++q;
3818af1da40SMathieu Malaterre 			switch (n) {
3828af1da40SMathieu Malaterre 			case 0:
3838af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
3848af1da40SMathieu Malaterre 				break;
3858af1da40SMathieu Malaterre 			case 1:
3869b6b563cSPaul Mackerras 				v = va_arg(args, unsigned long);
3878af1da40SMathieu Malaterre 				break;
3888af1da40SMathieu Malaterre 			case 2:
3898af1da40SMathieu Malaterre 			default:
3908af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
3918af1da40SMathieu Malaterre 				break;
3928af1da40SMathieu Malaterre 			}
3939b6b563cSPaul Mackerras 			prom_print_hex(v);
3949b6b563cSPaul Mackerras 			break;
3958af1da40SMathieu Malaterre 		case 'u':
3968af1da40SMathieu Malaterre 			++q;
3978af1da40SMathieu Malaterre 			switch (n) {
3988af1da40SMathieu Malaterre 			case 0:
3998af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
4008af1da40SMathieu Malaterre 				break;
4018af1da40SMathieu Malaterre 			case 1:
4028af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long);
4038af1da40SMathieu Malaterre 				break;
4048af1da40SMathieu Malaterre 			case 2:
4058af1da40SMathieu Malaterre 			default:
4068af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
4078af1da40SMathieu Malaterre 				break;
4088af1da40SMathieu Malaterre 			}
4098af1da40SMathieu Malaterre 			prom_print_dec(v);
4108af1da40SMathieu Malaterre 			break;
411af277149SBenjamin Herrenschmidt 		case 'd':
412af277149SBenjamin Herrenschmidt 			++q;
4138af1da40SMathieu Malaterre 			switch (n) {
4148af1da40SMathieu Malaterre 			case 0:
415af277149SBenjamin Herrenschmidt 				vs = va_arg(args, int);
416af277149SBenjamin Herrenschmidt 				break;
4178af1da40SMathieu Malaterre 			case 1:
418af277149SBenjamin Herrenschmidt 				vs = va_arg(args, long);
4198af1da40SMathieu Malaterre 				break;
4208af1da40SMathieu Malaterre 			case 2:
4218af1da40SMathieu Malaterre 			default:
4228af1da40SMathieu Malaterre 				vs = va_arg(args, long long);
4238af1da40SMathieu Malaterre 				break;
4248af1da40SMathieu Malaterre 			}
425af277149SBenjamin Herrenschmidt 			if (vs < 0) {
4265827d416SAnton Blanchard 				prom_print("-");
427af277149SBenjamin Herrenschmidt 				vs = -vs;
428af277149SBenjamin Herrenschmidt 			}
429af277149SBenjamin Herrenschmidt 			prom_print_dec(vs);
4302c48a7d6SMichael Neuling 			break;
4319b6b563cSPaul Mackerras 		}
4329b6b563cSPaul Mackerras 	}
4331b855e16SDaniel Axtens 	va_end(args);
4349b6b563cSPaul Mackerras }
4359b6b563cSPaul Mackerras 
4369b6b563cSPaul Mackerras 
437a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
438a575b807SPaul Mackerras 				unsigned long align)
439a575b807SPaul Mackerras {
440a575b807SPaul Mackerras 
441a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
442a23414beSPaul Mackerras 		/*
443a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
444a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
445a23414beSPaul Mackerras 		 */
446a23414beSPaul Mackerras 		int ret;
447a23414beSPaul Mackerras 		prom_arg_t result;
448a23414beSPaul Mackerras 
449a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
4505827d416SAnton Blanchard 				    ADDR("claim"), prom.memory,
451a23414beSPaul Mackerras 				    align, size, virt);
452a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
453a23414beSPaul Mackerras 			return -1;
454a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
4555827d416SAnton Blanchard 				    ADDR("claim"), prom.mmumap,
456a23414beSPaul Mackerras 				    align, size, virt);
457a23414beSPaul Mackerras 		if (ret != 0) {
458a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
4595827d416SAnton Blanchard 				  prom.memory, size, virt);
460a23414beSPaul Mackerras 			return -1;
461a23414beSPaul Mackerras 		}
462a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
463a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
4645827d416SAnton Blanchard 			  ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
465a23414beSPaul Mackerras 		return virt;
466a23414beSPaul Mackerras 	}
467a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
468a23414beSPaul Mackerras 			 (prom_arg_t)align);
469a575b807SPaul Mackerras }
470a575b807SPaul Mackerras 
4719b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
4729b6b563cSPaul Mackerras {
4739b6b563cSPaul Mackerras 	prom_print(reason);
474add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
475add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
4765827d416SAnton Blanchard 	if (of_platform == PLATFORM_POWERMAC)
477add60ef3SOlaf Hering 		asm("trap\n");
478add60ef3SOlaf Hering 
4791d9a4731SStephen Rothwell 	/* ToDo: should put up an SRC here on pSeries */
4809b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
4819b6b563cSPaul Mackerras 
4829b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
4839b6b563cSPaul Mackerras 		;
4849b6b563cSPaul Mackerras }
4859b6b563cSPaul Mackerras 
4869b6b563cSPaul Mackerras 
4879b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
4889b6b563cSPaul Mackerras {
4899b6b563cSPaul Mackerras 	phandle node;
4909b6b563cSPaul Mackerras 
4919b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
4929b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
4939b6b563cSPaul Mackerras 		return 1;
4949b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4959b6b563cSPaul Mackerras 		return 1;
4969b6b563cSPaul Mackerras 	for (;;) {
4979b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
4989b6b563cSPaul Mackerras 			return 0;
4999b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
5009b6b563cSPaul Mackerras 			return 1;
5019b6b563cSPaul Mackerras 	}
5029b6b563cSPaul Mackerras }
5039b6b563cSPaul Mackerras 
50460d862e5STobias Klauser static inline int prom_getprop(phandle node, const char *pname,
5059b6b563cSPaul Mackerras 			       void *value, size_t valuelen)
5069b6b563cSPaul Mackerras {
5079b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
5089b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
5099b6b563cSPaul Mackerras }
5109b6b563cSPaul Mackerras 
51160d862e5STobias Klauser static inline int prom_getproplen(phandle node, const char *pname)
5129b6b563cSPaul Mackerras {
5139b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
5149b6b563cSPaul Mackerras }
5159b6b563cSPaul Mackerras 
516a23414beSPaul Mackerras static void add_string(char **str, const char *q)
5179b6b563cSPaul Mackerras {
518a23414beSPaul Mackerras 	char *p = *str;
519a23414beSPaul Mackerras 
520a23414beSPaul Mackerras 	while (*q)
521a23414beSPaul Mackerras 		*p++ = *q++;
522a23414beSPaul Mackerras 	*p++ = ' ';
523a23414beSPaul Mackerras 	*str = p;
524a23414beSPaul Mackerras }
525a23414beSPaul Mackerras 
526a23414beSPaul Mackerras static char *tohex(unsigned int x)
527a23414beSPaul Mackerras {
5288ca2d515SBenjamin Herrenschmidt 	static const char digits[] __initconst = "0123456789abcdef";
5298ca2d515SBenjamin Herrenschmidt 	static char result[9] __prombss;
530a23414beSPaul Mackerras 	int i;
531a23414beSPaul Mackerras 
532a23414beSPaul Mackerras 	result[8] = 0;
533a23414beSPaul Mackerras 	i = 8;
534a23414beSPaul Mackerras 	do {
535a23414beSPaul Mackerras 		--i;
536a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
537a23414beSPaul Mackerras 		x >>= 4;
538a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
539a23414beSPaul Mackerras 	return &result[i];
540a23414beSPaul Mackerras }
541a23414beSPaul Mackerras 
542a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
543a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
544a23414beSPaul Mackerras {
545a23414beSPaul Mackerras 	char cmd[256], *p;
546a23414beSPaul Mackerras 
547a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
5489b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
5499b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
550a23414beSPaul Mackerras 
551a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
552a23414beSPaul Mackerras 	p = cmd;
553a23414beSPaul Mackerras 	add_string(&p, "dev");
554a23414beSPaul Mackerras 	add_string(&p, nodename);
555a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
556a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
557a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
5585827d416SAnton Blanchard 	add_string(&p, tohex(strlen(pname)));
559a23414beSPaul Mackerras 	add_string(&p, "property");
560a23414beSPaul Mackerras 	*p = 0;
561a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
5629b6b563cSPaul Mackerras }
5639b6b563cSPaul Mackerras 
5645827d416SAnton Blanchard /* We can't use the standard versions because of relocation headaches. */
565cf68787bSBenjamin Krill #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
566cf68787bSBenjamin Krill 			 || ('a' <= (c) && (c) <= 'f') \
567cf68787bSBenjamin Krill 			 || ('A' <= (c) && (c) <= 'F'))
568cf68787bSBenjamin Krill 
569cf68787bSBenjamin Krill #define isdigit(c)	('0' <= (c) && (c) <= '9')
570cf68787bSBenjamin Krill #define islower(c)	('a' <= (c) && (c) <= 'z')
571cf68787bSBenjamin Krill #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
572cf68787bSBenjamin Krill 
573493adffcSBenjamin Herrenschmidt static unsigned long prom_strtoul(const char *cp, const char **endp)
574cf68787bSBenjamin Krill {
575cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
576cf68787bSBenjamin Krill 
577cf68787bSBenjamin Krill 	if (*cp == '0') {
578cf68787bSBenjamin Krill 		base = 8;
579cf68787bSBenjamin Krill 		cp++;
580cf68787bSBenjamin Krill 		if (toupper(*cp) == 'X') {
581cf68787bSBenjamin Krill 			cp++;
582cf68787bSBenjamin Krill 			base = 16;
583cf68787bSBenjamin Krill 		}
584cf68787bSBenjamin Krill 	}
585cf68787bSBenjamin Krill 
586cf68787bSBenjamin Krill 	while (isxdigit(*cp) &&
587cf68787bSBenjamin Krill 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
588cf68787bSBenjamin Krill 		result = result * base + value;
589cf68787bSBenjamin Krill 		cp++;
590cf68787bSBenjamin Krill 	}
591cf68787bSBenjamin Krill 
592cf68787bSBenjamin Krill 	if (endp)
593cf68787bSBenjamin Krill 		*endp = cp;
594cf68787bSBenjamin Krill 
595cf68787bSBenjamin Krill 	return result;
596cf68787bSBenjamin Krill }
597cf68787bSBenjamin Krill 
598493adffcSBenjamin Herrenschmidt static unsigned long prom_memparse(const char *ptr, const char **retptr)
599cf68787bSBenjamin Krill {
600cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
601cf68787bSBenjamin Krill 	int shift = 0;
602cf68787bSBenjamin Krill 
603cf68787bSBenjamin Krill 	/*
604cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
605cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
606cf68787bSBenjamin Krill 	 * the address we're linked at.
607cf68787bSBenjamin Krill 	 */
608cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
609cf68787bSBenjamin Krill 		shift = 30;
610cf68787bSBenjamin Krill 
611cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
612cf68787bSBenjamin Krill 		shift = 20;
613cf68787bSBenjamin Krill 
614cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
615cf68787bSBenjamin Krill 		shift = 10;
616cf68787bSBenjamin Krill 
617cf68787bSBenjamin Krill 	if (shift) {
618cf68787bSBenjamin Krill 		ret <<= shift;
619cf68787bSBenjamin Krill 		(*retptr)++;
620cf68787bSBenjamin Krill 	}
621cf68787bSBenjamin Krill 
622cf68787bSBenjamin Krill 	return ret;
623cf68787bSBenjamin Krill }
624cf68787bSBenjamin Krill 
6259b6b563cSPaul Mackerras /*
6269b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
6279b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
6289b6b563cSPaul Mackerras  */
6299b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
6309b6b563cSPaul Mackerras {
631cc5d0189SBenjamin Herrenschmidt 	const char *opt;
632cf68787bSBenjamin Krill 
633cc5d0189SBenjamin Herrenschmidt 	char *p;
634eab00a20SMathieu Malaterre 	int l __maybe_unused = 0;
6359b6b563cSPaul Mackerras 
6365827d416SAnton Blanchard 	prom_cmd_line[0] = 0;
6375827d416SAnton Blanchard 	p = prom_cmd_line;
6385827d416SAnton Blanchard 	if ((long)prom.chosen > 0)
6395827d416SAnton Blanchard 		l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
6409b6b563cSPaul Mackerras #ifdef CONFIG_CMDLINE
6410e4aa9c2SAmos Waterland 	if (l <= 0 || p[0] == '\0') /* dbl check */
6425827d416SAnton Blanchard 		strlcpy(prom_cmd_line,
6435827d416SAnton Blanchard 			CONFIG_CMDLINE, sizeof(prom_cmd_line));
6449b6b563cSPaul Mackerras #endif /* CONFIG_CMDLINE */
6455827d416SAnton Blanchard 	prom_printf("command line: %s\n", prom_cmd_line);
6469b6b563cSPaul Mackerras 
6479b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
6485827d416SAnton Blanchard 	opt = strstr(prom_cmd_line, "iommu=");
6499b6b563cSPaul Mackerras 	if (opt) {
6509b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
6519b6b563cSPaul Mackerras 		opt += 6;
6529b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
6539b6b563cSPaul Mackerras 			opt++;
6545827d416SAnton Blanchard 		if (!strncmp(opt, "off", 3))
6555827d416SAnton Blanchard 			prom_iommu_off = 1;
6565827d416SAnton Blanchard 		else if (!strncmp(opt, "force", 5))
6575827d416SAnton Blanchard 			prom_iommu_force_on = 1;
6589b6b563cSPaul Mackerras 	}
6599b6b563cSPaul Mackerras #endif
6605827d416SAnton Blanchard 	opt = strstr(prom_cmd_line, "mem=");
661cf68787bSBenjamin Krill 	if (opt) {
662cf68787bSBenjamin Krill 		opt += 4;
6635827d416SAnton Blanchard 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
664cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
665cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
6665827d416SAnton Blanchard 		prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
667cf68787bSBenjamin Krill #endif
668cf68787bSBenjamin Krill 	}
669014d02cbSSuraj Jitindar Singh 
670c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
671c886087cSBenjamin Herrenschmidt 	prom_radix_disable = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
672014d02cbSSuraj Jitindar Singh 	opt = strstr(prom_cmd_line, "disable_radix");
673014d02cbSSuraj Jitindar Singh 	if (opt) {
6747a22d632SAneesh Kumar K.V 		opt += 13;
6757a22d632SAneesh Kumar K.V 		if (*opt && *opt == '=') {
6767a22d632SAneesh Kumar K.V 			bool val;
6777a22d632SAneesh Kumar K.V 
6787a22d632SAneesh Kumar K.V 			if (kstrtobool(++opt, &val))
6797a22d632SAneesh Kumar K.V 				prom_radix_disable = false;
6807a22d632SAneesh Kumar K.V 			else
6817a22d632SAneesh Kumar K.V 				prom_radix_disable = val;
6827a22d632SAneesh Kumar K.V 		} else
683014d02cbSSuraj Jitindar Singh 			prom_radix_disable = true;
684014d02cbSSuraj Jitindar Singh 	}
6857a22d632SAneesh Kumar K.V 	if (prom_radix_disable)
6867a22d632SAneesh Kumar K.V 		prom_debug("Radix disabled from cmdline\n");
687c886087cSBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
6889b6b563cSPaul Mackerras }
6899b6b563cSPaul Mackerras 
69011fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
6919b6b563cSPaul Mackerras /*
692530b5e14SNathan Fontenot  * The architecture vector has an array of PVR mask/value pairs,
693530b5e14SNathan Fontenot  * followed by # option vectors - 1, followed by the option vectors.
694530b5e14SNathan Fontenot  *
695530b5e14SNathan Fontenot  * See prom.h for the definition of the bits specified in the
696530b5e14SNathan Fontenot  * architecture vector.
697f709bfacSPaul Mackerras  */
698f709bfacSPaul Mackerras 
699e8a4fd0aSMichael Ellerman /* Firmware expects the value to be n - 1, where n is the # of vectors */
700e8a4fd0aSMichael Ellerman #define NUM_VECTORS(n)		((n) - 1)
701e8a4fd0aSMichael Ellerman 
702e8a4fd0aSMichael Ellerman /*
703e8a4fd0aSMichael Ellerman  * Firmware expects 1 + n - 2, where n is the length of the option vector in
704e8a4fd0aSMichael Ellerman  * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
705e8a4fd0aSMichael Ellerman  */
706e8a4fd0aSMichael Ellerman #define VECTOR_LENGTH(n)	(1 + (n) - 2)
707e8a4fd0aSMichael Ellerman 
708d03d1d65SMichael Ellerman struct option_vector1 {
709d03d1d65SMichael Ellerman 	u8 byte1;
710d03d1d65SMichael Ellerman 	u8 arch_versions;
711cc3d2940SPaul Mackerras 	u8 arch_versions3;
712d03d1d65SMichael Ellerman } __packed;
713d03d1d65SMichael Ellerman 
714d03d1d65SMichael Ellerman struct option_vector2 {
715d03d1d65SMichael Ellerman 	u8 byte1;
716d03d1d65SMichael Ellerman 	__be16 reserved;
717d03d1d65SMichael Ellerman 	__be32 real_base;
718d03d1d65SMichael Ellerman 	__be32 real_size;
719d03d1d65SMichael Ellerman 	__be32 virt_base;
720d03d1d65SMichael Ellerman 	__be32 virt_size;
721d03d1d65SMichael Ellerman 	__be32 load_base;
722d03d1d65SMichael Ellerman 	__be32 min_rma;
723d03d1d65SMichael Ellerman 	__be32 min_load;
724d03d1d65SMichael Ellerman 	u8 min_rma_percent;
725d03d1d65SMichael Ellerman 	u8 max_pft_size;
726d03d1d65SMichael Ellerman } __packed;
727d03d1d65SMichael Ellerman 
728d03d1d65SMichael Ellerman struct option_vector3 {
729d03d1d65SMichael Ellerman 	u8 byte1;
730d03d1d65SMichael Ellerman 	u8 byte2;
731d03d1d65SMichael Ellerman } __packed;
732d03d1d65SMichael Ellerman 
733d03d1d65SMichael Ellerman struct option_vector4 {
734d03d1d65SMichael Ellerman 	u8 byte1;
735d03d1d65SMichael Ellerman 	u8 min_vp_cap;
736d03d1d65SMichael Ellerman } __packed;
737d03d1d65SMichael Ellerman 
738d03d1d65SMichael Ellerman struct option_vector5 {
739d03d1d65SMichael Ellerman 	u8 byte1;
740d03d1d65SMichael Ellerman 	u8 byte2;
741d03d1d65SMichael Ellerman 	u8 byte3;
742d03d1d65SMichael Ellerman 	u8 cmo;
743d03d1d65SMichael Ellerman 	u8 associativity;
744d03d1d65SMichael Ellerman 	u8 bin_opts;
745d03d1d65SMichael Ellerman 	u8 micro_checkpoint;
746d03d1d65SMichael Ellerman 	u8 reserved0;
747d03d1d65SMichael Ellerman 	__be32 max_cpus;
748d03d1d65SMichael Ellerman 	__be16 papr_level;
749d03d1d65SMichael Ellerman 	__be16 reserved1;
750d03d1d65SMichael Ellerman 	u8 platform_facilities;
751d03d1d65SMichael Ellerman 	u8 reserved2;
752d03d1d65SMichael Ellerman 	__be16 reserved3;
753d03d1d65SMichael Ellerman 	u8 subprocessors;
754cc3d2940SPaul Mackerras 	u8 byte22;
755cc3d2940SPaul Mackerras 	u8 intarch;
756cc3d2940SPaul Mackerras 	u8 mmu;
757014d02cbSSuraj Jitindar Singh 	u8 hash_ext;
758014d02cbSSuraj Jitindar Singh 	u8 radix_ext;
759d03d1d65SMichael Ellerman } __packed;
760d03d1d65SMichael Ellerman 
761d03d1d65SMichael Ellerman struct option_vector6 {
762d03d1d65SMichael Ellerman 	u8 reserved;
763d03d1d65SMichael Ellerman 	u8 secondary_pteg;
764d03d1d65SMichael Ellerman 	u8 os_name;
765d03d1d65SMichael Ellerman } __packed;
766d03d1d65SMichael Ellerman 
76776ffb578SMichael Ellerman struct ibm_arch_vec {
768cc3d2940SPaul Mackerras 	struct { u32 mask, val; } pvrs[12];
76976ffb578SMichael Ellerman 
77076ffb578SMichael Ellerman 	u8 num_vectors;
77176ffb578SMichael Ellerman 
77276ffb578SMichael Ellerman 	u8 vec1_len;
77376ffb578SMichael Ellerman 	struct option_vector1 vec1;
77476ffb578SMichael Ellerman 
77576ffb578SMichael Ellerman 	u8 vec2_len;
77676ffb578SMichael Ellerman 	struct option_vector2 vec2;
77776ffb578SMichael Ellerman 
77876ffb578SMichael Ellerman 	u8 vec3_len;
77976ffb578SMichael Ellerman 	struct option_vector3 vec3;
78076ffb578SMichael Ellerman 
78176ffb578SMichael Ellerman 	u8 vec4_len;
78276ffb578SMichael Ellerman 	struct option_vector4 vec4;
78376ffb578SMichael Ellerman 
78476ffb578SMichael Ellerman 	u8 vec5_len;
78576ffb578SMichael Ellerman 	struct option_vector5 vec5;
78676ffb578SMichael Ellerman 
78776ffb578SMichael Ellerman 	u8 vec6_len;
78876ffb578SMichael Ellerman 	struct option_vector6 vec6;
78976ffb578SMichael Ellerman } __packed;
79076ffb578SMichael Ellerman 
791d00e34b9SBenjamin Herrenschmidt static const struct ibm_arch_vec ibm_architecture_vec_template __initconst = {
79276ffb578SMichael Ellerman 	.pvrs = {
79376ffb578SMichael Ellerman 		{
79476ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffe0000), /* POWER5/POWER5+ */
79576ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003a0000),
79676ffb578SMichael Ellerman 		},
79776ffb578SMichael Ellerman 		{
79876ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER6 */
79976ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003e0000),
80076ffb578SMichael Ellerman 		},
80176ffb578SMichael Ellerman 		{
80276ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER7 */
80376ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003f0000),
80476ffb578SMichael Ellerman 		},
80576ffb578SMichael Ellerman 		{
80676ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8E */
80776ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004b0000),
80876ffb578SMichael Ellerman 		},
80976ffb578SMichael Ellerman 		{
81076ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8NVL */
81176ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004c0000),
81276ffb578SMichael Ellerman 		},
81376ffb578SMichael Ellerman 		{
81476ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8 */
81576ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004d0000),
81676ffb578SMichael Ellerman 		},
81776ffb578SMichael Ellerman 		{
818cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffff0000), /* POWER9 */
819cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x004e0000),
820cc3d2940SPaul Mackerras 		},
821cc3d2940SPaul Mackerras 		{
822cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffffffff), /* all 3.00-compliant */
823cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x0f000005),
824cc3d2940SPaul Mackerras 		},
825cc3d2940SPaul Mackerras 		{
82676ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.07-compliant */
82776ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000004),
82876ffb578SMichael Ellerman 		},
82976ffb578SMichael Ellerman 		{
83076ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.06-compliant */
83176ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000003),
83276ffb578SMichael Ellerman 		},
83376ffb578SMichael Ellerman 		{
83476ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.05-compliant */
83576ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000002),
83676ffb578SMichael Ellerman 		},
83776ffb578SMichael Ellerman 		{
83876ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffffffe), /* all 2.04-compliant and earlier */
83976ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000001),
84076ffb578SMichael Ellerman 		},
84176ffb578SMichael Ellerman 	},
84276ffb578SMichael Ellerman 
84376ffb578SMichael Ellerman 	.num_vectors = NUM_VECTORS(6),
84476ffb578SMichael Ellerman 
84576ffb578SMichael Ellerman 	.vec1_len = VECTOR_LENGTH(sizeof(struct option_vector1)),
84676ffb578SMichael Ellerman 	.vec1 = {
84776ffb578SMichael Ellerman 		.byte1 = 0,
84876ffb578SMichael Ellerman 		.arch_versions = OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
84976ffb578SMichael Ellerman 				 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
850cc3d2940SPaul Mackerras 		.arch_versions3 = OV1_PPC_3_00,
85176ffb578SMichael Ellerman 	},
85276ffb578SMichael Ellerman 
85376ffb578SMichael Ellerman 	.vec2_len = VECTOR_LENGTH(sizeof(struct option_vector2)),
85476ffb578SMichael Ellerman 	/* option vector 2: Open Firmware options supported */
85576ffb578SMichael Ellerman 	.vec2 = {
85676ffb578SMichael Ellerman 		.byte1 = OV2_REAL_MODE,
85776ffb578SMichael Ellerman 		.reserved = 0,
85876ffb578SMichael Ellerman 		.real_base = cpu_to_be32(0xffffffff),
85976ffb578SMichael Ellerman 		.real_size = cpu_to_be32(0xffffffff),
86076ffb578SMichael Ellerman 		.virt_base = cpu_to_be32(0xffffffff),
86176ffb578SMichael Ellerman 		.virt_size = cpu_to_be32(0xffffffff),
86276ffb578SMichael Ellerman 		.load_base = cpu_to_be32(0xffffffff),
863687da8fcSSukadev Bhattiprolu 		.min_rma = cpu_to_be32(512),		/* 512MB min RMA */
86476ffb578SMichael Ellerman 		.min_load = cpu_to_be32(0xffffffff),	/* full client load */
86576ffb578SMichael Ellerman 		.min_rma_percent = 0,	/* min RMA percentage of total RAM */
86676ffb578SMichael Ellerman 		.max_pft_size = 48,	/* max log_2(hash table size) */
86776ffb578SMichael Ellerman 	},
86876ffb578SMichael Ellerman 
86976ffb578SMichael Ellerman 	.vec3_len = VECTOR_LENGTH(sizeof(struct option_vector3)),
87076ffb578SMichael Ellerman 	/* option vector 3: processor options supported */
87176ffb578SMichael Ellerman 	.vec3 = {
87276ffb578SMichael Ellerman 		.byte1 = 0,			/* don't ignore, don't halt */
87376ffb578SMichael Ellerman 		.byte2 = OV3_FP | OV3_VMX | OV3_DFP,
87476ffb578SMichael Ellerman 	},
87576ffb578SMichael Ellerman 
87676ffb578SMichael Ellerman 	.vec4_len = VECTOR_LENGTH(sizeof(struct option_vector4)),
87776ffb578SMichael Ellerman 	/* option vector 4: IBM PAPR implementation */
87876ffb578SMichael Ellerman 	.vec4 = {
87976ffb578SMichael Ellerman 		.byte1 = 0,			/* don't halt */
88076ffb578SMichael Ellerman 		.min_vp_cap = OV4_MIN_ENT_CAP,	/* minimum VP entitled capacity */
88176ffb578SMichael Ellerman 	},
88276ffb578SMichael Ellerman 
88376ffb578SMichael Ellerman 	.vec5_len = VECTOR_LENGTH(sizeof(struct option_vector5)),
88476ffb578SMichael Ellerman 	/* option vector 5: PAPR/OF options */
88576ffb578SMichael Ellerman 	.vec5 = {
88676ffb578SMichael Ellerman 		.byte1 = 0,				/* don't ignore, don't halt */
88776ffb578SMichael Ellerman 		.byte2 = OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
88876ffb578SMichael Ellerman 		OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
88976ffb578SMichael Ellerman #ifdef CONFIG_PCI_MSI
89076ffb578SMichael Ellerman 		/* PCIe/MSI support.  Without MSI full PCIe is not supported */
89176ffb578SMichael Ellerman 		OV5_FEAT(OV5_MSI),
89276ffb578SMichael Ellerman #else
89376ffb578SMichael Ellerman 		0,
89476ffb578SMichael Ellerman #endif
89576ffb578SMichael Ellerman 		.byte3 = 0,
89676ffb578SMichael Ellerman 		.cmo =
89776ffb578SMichael Ellerman #ifdef CONFIG_PPC_SMLPAR
89876ffb578SMichael Ellerman 		OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
89976ffb578SMichael Ellerman #else
90076ffb578SMichael Ellerman 		0,
90176ffb578SMichael Ellerman #endif
90276ffb578SMichael Ellerman 		.associativity = OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
9033dbbaf20SMichael Roth 		.bin_opts = OV5_FEAT(OV5_RESIZE_HPT) | OV5_FEAT(OV5_HP_EVT),
90476ffb578SMichael Ellerman 		.micro_checkpoint = 0,
90576ffb578SMichael Ellerman 		.reserved0 = 0,
90676ffb578SMichael Ellerman 		.max_cpus = cpu_to_be32(NR_CPUS),	/* number of cores supported */
90776ffb578SMichael Ellerman 		.papr_level = 0,
90876ffb578SMichael Ellerman 		.reserved1 = 0,
90976ffb578SMichael Ellerman 		.platform_facilities = OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) | OV5_FEAT(OV5_PFO_HW_842),
91076ffb578SMichael Ellerman 		.reserved2 = 0,
91176ffb578SMichael Ellerman 		.reserved3 = 0,
91276ffb578SMichael Ellerman 		.subprocessors = 1,
9130c38ed6fSNathan Fontenot 		.byte22 = OV5_FEAT(OV5_DRMEM_V2),
914cc3d2940SPaul Mackerras 		.intarch = 0,
915014d02cbSSuraj Jitindar Singh 		.mmu = 0,
916014d02cbSSuraj Jitindar Singh 		.hash_ext = 0,
917014d02cbSSuraj Jitindar Singh 		.radix_ext = 0,
91876ffb578SMichael Ellerman 	},
91976ffb578SMichael Ellerman 
92076ffb578SMichael Ellerman 	/* option vector 6: IBM PAPR hints */
92176ffb578SMichael Ellerman 	.vec6_len = VECTOR_LENGTH(sizeof(struct option_vector6)),
92276ffb578SMichael Ellerman 	.vec6 = {
92376ffb578SMichael Ellerman 		.reserved = 0,
92476ffb578SMichael Ellerman 		.secondary_pteg = 0,
92576ffb578SMichael Ellerman 		.os_name = OV6_LINUX,
92676ffb578SMichael Ellerman 	},
92776ffb578SMichael Ellerman };
92876ffb578SMichael Ellerman 
929a614f52eSBenjamin Herrenschmidt static struct ibm_arch_vec __prombss ibm_architecture_vec  ____cacheline_aligned;
930a614f52eSBenjamin Herrenschmidt 
931493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
932493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
93330c69ca0SBenjamin Herrenschmidt static const struct fake_elf {
9349b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
9359b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
9369b6b563cSPaul Mackerras 	struct chrpnote {
9379b6b563cSPaul Mackerras 		u32	namesz;
9389b6b563cSPaul Mackerras 		u32	descsz;
9399b6b563cSPaul Mackerras 		u32	type;
9409b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
9419b6b563cSPaul Mackerras 		struct chrpdesc {
9429b6b563cSPaul Mackerras 			u32	real_mode;
9439b6b563cSPaul Mackerras 			u32	real_base;
9449b6b563cSPaul Mackerras 			u32	real_size;
9459b6b563cSPaul Mackerras 			u32	virt_base;
9469b6b563cSPaul Mackerras 			u32	virt_size;
9479b6b563cSPaul Mackerras 			u32	load_base;
9489b6b563cSPaul Mackerras 		} chrpdesc;
9499b6b563cSPaul Mackerras 	} chrpnote;
9509b6b563cSPaul Mackerras 	struct rpanote {
9519b6b563cSPaul Mackerras 		u32	namesz;
9529b6b563cSPaul Mackerras 		u32	descsz;
9539b6b563cSPaul Mackerras 		u32	type;
9549b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
9559b6b563cSPaul Mackerras 		struct rpadesc {
9569b6b563cSPaul Mackerras 			u32	lpar_affinity;
9579b6b563cSPaul Mackerras 			u32	min_rmo_size;
9589b6b563cSPaul Mackerras 			u32	min_rmo_percent;
9599b6b563cSPaul Mackerras 			u32	max_pft_size;
9609b6b563cSPaul Mackerras 			u32	splpar;
9619b6b563cSPaul Mackerras 			u32	min_load;
9629b6b563cSPaul Mackerras 			u32	new_mem_def;
9639b6b563cSPaul Mackerras 			u32	ignore_me;
9649b6b563cSPaul Mackerras 		} rpadesc;
9659b6b563cSPaul Mackerras 	} rpanote;
966d00e34b9SBenjamin Herrenschmidt } fake_elf __initconst = {
9679b6b563cSPaul Mackerras 	.elfhdr = {
9689b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
9699b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
9709b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
9719b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
9729b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
9739b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
9749b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
9759b6b563cSPaul Mackerras 		.e_phnum = 2
9769b6b563cSPaul Mackerras 	},
9779b6b563cSPaul Mackerras 	.phdr = {
9789b6b563cSPaul Mackerras 		[0] = {
9799b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
9809b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
9819b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
9829b6b563cSPaul Mackerras 		}, [1] = {
9839b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
9849b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
9859b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
9869b6b563cSPaul Mackerras 		}
9879b6b563cSPaul Mackerras 	},
9889b6b563cSPaul Mackerras 	.chrpnote = {
9899b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
9909b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
9919b6b563cSPaul Mackerras 		.type = 0x1275,
9929b6b563cSPaul Mackerras 		.name = "PowerPC",
9939b6b563cSPaul Mackerras 		.chrpdesc = {
9949b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
9959b6b563cSPaul Mackerras 			.real_base = ~0U,
9969b6b563cSPaul Mackerras 			.real_size = ~0U,
9979b6b563cSPaul Mackerras 			.virt_base = ~0U,
9989b6b563cSPaul Mackerras 			.virt_size = ~0U,
9999b6b563cSPaul Mackerras 			.load_base = ~0U
10009b6b563cSPaul Mackerras 		},
10019b6b563cSPaul Mackerras 	},
10029b6b563cSPaul Mackerras 	.rpanote = {
10039b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
10049b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
10059b6b563cSPaul Mackerras 		.type = 0x12759999,
10069b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
10079b6b563cSPaul Mackerras 		.rpadesc = {
10085663a123SPaul Mackerras 			.lpar_affinity = 0,
10095663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
10109b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
10115663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
10129b6b563cSPaul Mackerras 			.splpar = 1,
10139b6b563cSPaul Mackerras 			.min_load = ~0U,
10145663a123SPaul Mackerras 			.new_mem_def = 0
10159b6b563cSPaul Mackerras 		}
10169b6b563cSPaul Mackerras 	}
10179b6b563cSPaul Mackerras };
1018493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
10199b6b563cSPaul Mackerras 
1020efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
1021efec959fSBenjamin Herrenschmidt {
1022efec959fSBenjamin Herrenschmidt 	phandle node;
1023efec959fSBenjamin Herrenschmidt 	char type[64];
1024efec959fSBenjamin Herrenschmidt 	unsigned int plen;
1025efec959fSBenjamin Herrenschmidt 
1026efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
1027efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
1028efec959fSBenjamin Herrenschmidt 		type[0] = 0;
1029efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
1030efec959fSBenjamin Herrenschmidt 
10315827d416SAnton Blanchard 		if (strcmp(type, "cpu"))
1032efec959fSBenjamin Herrenschmidt 			continue;
1033efec959fSBenjamin Herrenschmidt 		/*
1034efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
1035efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
1036efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
1037efec959fSBenjamin Herrenschmidt 		 */
1038efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
1039efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
1040efec959fSBenjamin Herrenschmidt 			break;
1041efec959fSBenjamin Herrenschmidt 		plen >>= 2;
10422c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
1043efec959fSBenjamin Herrenschmidt 
1044efec959fSBenjamin Herrenschmidt 		/* Sanity check */
1045efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
10462c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
1047efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
1048efec959fSBenjamin Herrenschmidt 			return 1;
1049efec959fSBenjamin Herrenschmidt 		}
1050efec959fSBenjamin Herrenschmidt 		return plen;
1051efec959fSBenjamin Herrenschmidt 	}
1052efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
1053efec959fSBenjamin Herrenschmidt 
1054efec959fSBenjamin Herrenschmidt 	return 1;
1055efec959fSBenjamin Herrenschmidt 
1056efec959fSBenjamin Herrenschmidt }
1057efec959fSBenjamin Herrenschmidt 
1058014d02cbSSuraj Jitindar Singh static void __init prom_parse_mmu_model(u8 val,
1059014d02cbSSuraj Jitindar Singh 					struct platform_support *support)
1060014d02cbSSuraj Jitindar Singh {
1061014d02cbSSuraj Jitindar Singh 	switch (val) {
1062014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_DYNAMIC):
1063014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_EITHER): /* Either Available */
1064014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - either supported\n");
1065014d02cbSSuraj Jitindar Singh 		support->radix_mmu = !prom_radix_disable;
1066014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1067014d02cbSSuraj Jitindar Singh 		break;
1068014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */
1069014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - radix only\n");
1070014d02cbSSuraj Jitindar Singh 		if (prom_radix_disable) {
1071014d02cbSSuraj Jitindar Singh 			/*
1072014d02cbSSuraj Jitindar Singh 			 * If we __have__ to do radix, we're better off ignoring
1073014d02cbSSuraj Jitindar Singh 			 * the command line rather than not booting.
1074014d02cbSSuraj Jitindar Singh 			 */
1075014d02cbSSuraj Jitindar Singh 			prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
1076014d02cbSSuraj Jitindar Singh 		}
1077014d02cbSSuraj Jitindar Singh 		support->radix_mmu = true;
1078014d02cbSSuraj Jitindar Singh 		break;
1079014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_HASH):
1080014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - hash only\n");
1081014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1082014d02cbSSuraj Jitindar Singh 		break;
1083014d02cbSSuraj Jitindar Singh 	default:
1084014d02cbSSuraj Jitindar Singh 		prom_debug("Unknown mmu support option: 0x%x\n", val);
1085014d02cbSSuraj Jitindar Singh 		break;
1086014d02cbSSuraj Jitindar Singh 	}
1087014d02cbSSuraj Jitindar Singh }
1088014d02cbSSuraj Jitindar Singh 
1089ac5e5a54SCédric Le Goater static void __init prom_parse_xive_model(u8 val,
1090ac5e5a54SCédric Le Goater 					 struct platform_support *support)
1091ac5e5a54SCédric Le Goater {
1092ac5e5a54SCédric Le Goater 	switch (val) {
1093ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EITHER): /* Either Available */
1094ac5e5a54SCédric Le Goater 		prom_debug("XIVE - either mode supported\n");
1095ac5e5a54SCédric Le Goater 		support->xive = true;
1096ac5e5a54SCédric Le Goater 		break;
1097ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EXPLOIT): /* Only Exploitation mode */
1098ac5e5a54SCédric Le Goater 		prom_debug("XIVE - exploitation mode supported\n");
1099ac5e5a54SCédric Le Goater 		support->xive = true;
1100ac5e5a54SCédric Le Goater 		break;
1101ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_LEGACY): /* Only Legacy mode */
1102ac5e5a54SCédric Le Goater 		prom_debug("XIVE - legacy mode supported\n");
1103ac5e5a54SCédric Le Goater 		break;
1104ac5e5a54SCédric Le Goater 	default:
1105ac5e5a54SCédric Le Goater 		prom_debug("Unknown xive support option: 0x%x\n", val);
1106ac5e5a54SCédric Le Goater 		break;
1107ac5e5a54SCédric Le Goater 	}
1108ac5e5a54SCédric Le Goater }
1109ac5e5a54SCédric Le Goater 
1110014d02cbSSuraj Jitindar Singh static void __init prom_parse_platform_support(u8 index, u8 val,
1111014d02cbSSuraj Jitindar Singh 					       struct platform_support *support)
1112014d02cbSSuraj Jitindar Singh {
1113014d02cbSSuraj Jitindar Singh 	switch (index) {
1114014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_MMU_SUPPORT): /* MMU Model */
1115014d02cbSSuraj Jitindar Singh 		prom_parse_mmu_model(val & OV5_FEAT(OV5_MMU_SUPPORT), support);
1116014d02cbSSuraj Jitindar Singh 		break;
1117014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_RADIX_GTSE): /* Radix Extensions */
1118014d02cbSSuraj Jitindar Singh 		if (val & OV5_FEAT(OV5_RADIX_GTSE)) {
1119014d02cbSSuraj Jitindar Singh 			prom_debug("Radix - GTSE supported\n");
1120014d02cbSSuraj Jitindar Singh 			support->radix_gtse = true;
1121014d02cbSSuraj Jitindar Singh 		}
1122014d02cbSSuraj Jitindar Singh 		break;
1123ac5e5a54SCédric Le Goater 	case OV5_INDX(OV5_XIVE_SUPPORT): /* Interrupt mode */
1124ac5e5a54SCédric Le Goater 		prom_parse_xive_model(val & OV5_FEAT(OV5_XIVE_SUPPORT),
1125ac5e5a54SCédric Le Goater 				      support);
1126ac5e5a54SCédric Le Goater 		break;
1127014d02cbSSuraj Jitindar Singh 	}
1128014d02cbSSuraj Jitindar Singh }
1129014d02cbSSuraj Jitindar Singh 
1130014d02cbSSuraj Jitindar Singh static void __init prom_check_platform_support(void)
1131014d02cbSSuraj Jitindar Singh {
1132014d02cbSSuraj Jitindar Singh 	struct platform_support supported = {
1133014d02cbSSuraj Jitindar Singh 		.hash_mmu = false,
1134014d02cbSSuraj Jitindar Singh 		.radix_mmu = false,
1135ac5e5a54SCédric Le Goater 		.radix_gtse = false,
1136ac5e5a54SCédric Le Goater 		.xive = false
1137014d02cbSSuraj Jitindar Singh 	};
1138014d02cbSSuraj Jitindar Singh 	int prop_len = prom_getproplen(prom.chosen,
1139014d02cbSSuraj Jitindar Singh 				       "ibm,arch-vec-5-platform-support");
1140a614f52eSBenjamin Herrenschmidt 
1141a614f52eSBenjamin Herrenschmidt 	/* First copy the architecture vec template */
1142a614f52eSBenjamin Herrenschmidt 	ibm_architecture_vec = ibm_architecture_vec_template;
1143a614f52eSBenjamin Herrenschmidt 
1144014d02cbSSuraj Jitindar Singh 	if (prop_len > 1) {
1145014d02cbSSuraj Jitindar Singh 		int i;
1146ab912399SSuraj Jitindar Singh 		u8 vec[8];
1147014d02cbSSuraj Jitindar Singh 		prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1148014d02cbSSuraj Jitindar Singh 			   prop_len);
1149ab912399SSuraj Jitindar Singh 		if (prop_len > sizeof(vec))
1150ab912399SSuraj Jitindar Singh 			prom_printf("WARNING: ibm,arch-vec-5-platform-support longer than expected (len: %d)\n",
1151ab912399SSuraj Jitindar Singh 				    prop_len);
1152014d02cbSSuraj Jitindar Singh 		prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support",
1153014d02cbSSuraj Jitindar Singh 			     &vec, sizeof(vec));
1154ab912399SSuraj Jitindar Singh 		for (i = 0; i < sizeof(vec); i += 2) {
1155014d02cbSSuraj Jitindar Singh 			prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2
1156014d02cbSSuraj Jitindar Singh 								  , vec[i]
1157014d02cbSSuraj Jitindar Singh 								  , vec[i + 1]);
1158014d02cbSSuraj Jitindar Singh 			prom_parse_platform_support(vec[i], vec[i + 1],
1159014d02cbSSuraj Jitindar Singh 						    &supported);
1160014d02cbSSuraj Jitindar Singh 		}
1161014d02cbSSuraj Jitindar Singh 	}
1162014d02cbSSuraj Jitindar Singh 
116379b46868SAlexey Kardashevskiy 	if (supported.radix_mmu && supported.radix_gtse &&
116479b46868SAlexey Kardashevskiy 	    IS_ENABLED(CONFIG_PPC_RADIX_MMU)) {
1165014d02cbSSuraj Jitindar Singh 		/* Radix preferred - but we require GTSE for now */
1166014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for radix with GTSE\n");
1167014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1168014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.radix_ext = OV5_FEAT(OV5_RADIX_GTSE);
1169014d02cbSSuraj Jitindar Singh 	} else if (supported.hash_mmu) {
1170014d02cbSSuraj Jitindar Singh 		/* Default to hash mmu (if we can) */
1171014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for hash\n");
1172014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1173014d02cbSSuraj Jitindar Singh 	} else {
1174014d02cbSSuraj Jitindar Singh 		/* We're probably on a legacy hypervisor */
1175014d02cbSSuraj Jitindar Singh 		prom_debug("Assuming legacy hash support\n");
1176014d02cbSSuraj Jitindar Singh 	}
1177ac5e5a54SCédric Le Goater 
1178ac5e5a54SCédric Le Goater 	if (supported.xive) {
1179ac5e5a54SCédric Le Goater 		prom_debug("Asking for XIVE\n");
1180ac5e5a54SCédric Le Goater 		ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1181ac5e5a54SCédric Le Goater 	}
1182014d02cbSSuraj Jitindar Singh }
1183efec959fSBenjamin Herrenschmidt 
11849b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
11859b6b563cSPaul Mackerras {
1186493adffcSBenjamin Herrenschmidt 	ihandle root;
1187f709bfacSPaul Mackerras 	prom_arg_t ret;
1188dbd0c5d5SLaurent Dufour 	u32 cores;
11899b6b563cSPaul Mackerras 
1190014d02cbSSuraj Jitindar Singh 	/* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1191014d02cbSSuraj Jitindar Singh 	prom_check_platform_support();
1192014d02cbSSuraj Jitindar Singh 
1193f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
1194f709bfacSPaul Mackerras 	if (root != 0) {
1195efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
1196efec959fSBenjamin Herrenschmidt 		 *
1197efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
1198efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
1199efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
1200efec959fSBenjamin Herrenschmidt 		 */
1201dbd0c5d5SLaurent Dufour 
1202dbd0c5d5SLaurent Dufour 		cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1203eae5f709SMathieu Malaterre 		prom_printf("Max number of cores passed to firmware: %u (NR_CPUS = %d)\n",
1204dbd0c5d5SLaurent Dufour 			    cores, NR_CPUS);
120576ffb578SMichael Ellerman 
120676ffb578SMichael Ellerman 		ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1207efec959fSBenjamin Herrenschmidt 
1208f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
1209049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
1210f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
1211f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
121233b74977SBenjamin Herrenschmidt 				  root,
121376ffb578SMichael Ellerman 				  ADDR(&ibm_architecture_vec)) == 0) {
1214f709bfacSPaul Mackerras 			/* the call exists... */
1215f709bfacSPaul Mackerras 			if (ret)
12164da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
1217f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
1218f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
12194da727aeSAnton Blanchard 			prom_printf(" done\n");
1220f709bfacSPaul Mackerras 			return;
1221f709bfacSPaul Mackerras 		}
1222f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
1223049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
1224f709bfacSPaul Mackerras 	}
1225f709bfacSPaul Mackerras 
1226493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
1227493adffcSBenjamin Herrenschmidt 	{
1228493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
1229493adffcSBenjamin Herrenschmidt 
1230f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
1231493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
1232493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
12339b6b563cSPaul Mackerras 		if (elfloader == 0) {
12349b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
12359b6b563cSPaul Mackerras 			return;
12369b6b563cSPaul Mackerras 		}
12379b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
12389b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
12399b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
12409b6b563cSPaul Mackerras 	}
1241493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1242493adffcSBenjamin Herrenschmidt }
124311fdb309SBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
12449b6b563cSPaul Mackerras 
12459b6b563cSPaul Mackerras /*
12469b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
12479b6b563cSPaul Mackerras  *
12489b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
12499b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
12509b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
12519b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
12529b6b563cSPaul Mackerras  *  never happen.
12539b6b563cSPaul Mackerras  *
12549b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
12559b6b563cSPaul Mackerras  *  TCEs overlap
12569b6b563cSPaul Mackerras  *
12579b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
12589b6b563cSPaul Mackerras  *
12599b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
12609b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
12619b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
12629b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
12639b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
12649b6b563cSPaul Mackerras  *
12659b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
12669b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
12679b6b563cSPaul Mackerras  *  reserved.
12689b6b563cSPaul Mackerras  */
12699b6b563cSPaul Mackerras 
12709b6b563cSPaul Mackerras 
12719b6b563cSPaul Mackerras /*
12729b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
12739b6b563cSPaul Mackerras  *
12749b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
12759b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
12769b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
12779b6b563cSPaul Mackerras  */
12789b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
12799b6b563cSPaul Mackerras {
12805827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
12819b6b563cSPaul Mackerras 	unsigned long addr = 0;
12829b6b563cSPaul Mackerras 
1283c4988820SPaul Mackerras 	if (align)
1284c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
1285eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx)\n", __func__, size, align);
12865827d416SAnton Blanchard 	if (ram_top == 0)
12879b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
12889b6b563cSPaul Mackerras 
12899b6b563cSPaul Mackerras 	if (align)
12905827d416SAnton Blanchard 		base = _ALIGN_UP(alloc_bottom, align);
12919b6b563cSPaul Mackerras 	else
12925827d416SAnton Blanchard 		base = alloc_bottom;
12939b6b563cSPaul Mackerras 
12945827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
12959b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
1296eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
12979b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1298c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
12999b6b563cSPaul Mackerras 			break;
13009b6b563cSPaul Mackerras 		addr = 0;
13019b6b563cSPaul Mackerras 		if (align == 0)
13029b6b563cSPaul Mackerras 			break;
13039b6b563cSPaul Mackerras 	}
13049b6b563cSPaul Mackerras 	if (addr == 0)
13059b6b563cSPaul Mackerras 		return 0;
13065827d416SAnton Blanchard 	alloc_bottom = addr + size;
13079b6b563cSPaul Mackerras 
1308eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1309eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1310eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1311eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1312eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1313eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
13149b6b563cSPaul Mackerras 
13159b6b563cSPaul Mackerras 	return addr;
13169b6b563cSPaul Mackerras }
13179b6b563cSPaul Mackerras 
13189b6b563cSPaul Mackerras /*
13199b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
13209b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
13219b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
13229b6b563cSPaul Mackerras  */
13239b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
13249b6b563cSPaul Mackerras 				       int highmem)
13259b6b563cSPaul Mackerras {
13269b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
13279b6b563cSPaul Mackerras 
1328eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx, %s)\n", __func__, size, align,
13295827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
13305827d416SAnton Blanchard 	if (ram_top == 0)
13319b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
13329b6b563cSPaul Mackerras 
13339b6b563cSPaul Mackerras 	if (highmem) {
13349b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
13355827d416SAnton Blanchard 		addr = _ALIGN_DOWN(alloc_top_high - size, align);
13365827d416SAnton Blanchard 		if (addr <= alloc_bottom)
13379b6b563cSPaul Mackerras 			return 0;
13389b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
13399b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
13409b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
13419b6b563cSPaul Mackerras 		 */
13425827d416SAnton Blanchard 		if (addr < rmo_top) {
13439b6b563cSPaul Mackerras 			/* Good, we are first */
13445827d416SAnton Blanchard 			if (alloc_top == rmo_top)
13455827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
13469b6b563cSPaul Mackerras 			else
13479b6b563cSPaul Mackerras 				return 0;
13489b6b563cSPaul Mackerras 		}
13495827d416SAnton Blanchard 		alloc_top_high = addr;
13509b6b563cSPaul Mackerras 		goto bail;
13519b6b563cSPaul Mackerras 	}
13529b6b563cSPaul Mackerras 
13535827d416SAnton Blanchard 	base = _ALIGN_DOWN(alloc_top - size, align);
13545827d416SAnton Blanchard 	for (; base > alloc_bottom;
13559b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
1356eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
13579b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1358c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
13599b6b563cSPaul Mackerras 			break;
13609b6b563cSPaul Mackerras 		addr = 0;
13619b6b563cSPaul Mackerras 	}
13629b6b563cSPaul Mackerras 	if (addr == 0)
13639b6b563cSPaul Mackerras 		return 0;
13645827d416SAnton Blanchard 	alloc_top = addr;
13659b6b563cSPaul Mackerras 
13669b6b563cSPaul Mackerras  bail:
1367eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1368eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1369eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1370eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1371eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1372eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
13739b6b563cSPaul Mackerras 
13749b6b563cSPaul Mackerras 	return addr;
13759b6b563cSPaul Mackerras }
13769b6b563cSPaul Mackerras 
13779b6b563cSPaul Mackerras /*
13789b6b563cSPaul Mackerras  * Parse a "reg" cell
13799b6b563cSPaul Mackerras  */
13809b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
13819b6b563cSPaul Mackerras {
13829b6b563cSPaul Mackerras 	cell_t *p = *cellp;
13839b6b563cSPaul Mackerras 	unsigned long r = 0;
13849b6b563cSPaul Mackerras 
13859b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
13869b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
13879b6b563cSPaul Mackerras 		p++;
13889b6b563cSPaul Mackerras 		s--;
13899b6b563cSPaul Mackerras 	}
1390493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
13919b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
139235499c01SPaul Mackerras 	if (s > 1) {
13939b6b563cSPaul Mackerras 		r <<= 32;
1394493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
13959b6b563cSPaul Mackerras 	}
13969b6b563cSPaul Mackerras #endif
13979b6b563cSPaul Mackerras 	*cellp = p;
13989b6b563cSPaul Mackerras 	return r;
13999b6b563cSPaul Mackerras }
14009b6b563cSPaul Mackerras 
14019b6b563cSPaul Mackerras /*
14029b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
14039b6b563cSPaul Mackerras  * we don't need anything smarter at this point
14049b6b563cSPaul Mackerras  *
14059b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
14069b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
14079b6b563cSPaul Mackerras  * them down.
14089b6b563cSPaul Mackerras  */
14090108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
14109b6b563cSPaul Mackerras {
1411cbbcf340SKumar Gala 	u64 top = base + size;
14125827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
14139b6b563cSPaul Mackerras 
14149b6b563cSPaul Mackerras 	if (size == 0)
14159b6b563cSPaul Mackerras 		return;
14169b6b563cSPaul Mackerras 
14179b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
14189b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
14199b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
14209b6b563cSPaul Mackerras 	 */
14219b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
14229b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
14239b6b563cSPaul Mackerras 	size = top - base;
14249b6b563cSPaul Mackerras 
14259b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
14269b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1427493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1428493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
14295827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
14309b6b563cSPaul Mackerras }
14319b6b563cSPaul Mackerras 
14329b6b563cSPaul Mackerras /*
1433b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
14349b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
14359b6b563cSPaul Mackerras  */
14369b6b563cSPaul Mackerras static void __init prom_init_mem(void)
14379b6b563cSPaul Mackerras {
14389b6b563cSPaul Mackerras 	phandle node;
1439eab00a20SMathieu Malaterre #ifdef DEBUG_PROM
1440eab00a20SMathieu Malaterre 	char *path;
1441eab00a20SMathieu Malaterre #endif
1442eab00a20SMathieu Malaterre 	char type[64];
14439b6b563cSPaul Mackerras 	unsigned int plen;
14449b6b563cSPaul Mackerras 	cell_t *p, *endp;
1445493adffcSBenjamin Herrenschmidt 	__be32 val;
14469b6b563cSPaul Mackerras 	u32 rac, rsc;
14479b6b563cSPaul Mackerras 
14489b6b563cSPaul Mackerras 	/*
14499b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
14509b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
14519b6b563cSPaul Mackerras 	 * 2) top of memory
14529b6b563cSPaul Mackerras 	 */
1453493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1454493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1455493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1456493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1457493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1458493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1459493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1460493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
14619b6b563cSPaul Mackerras 
14629b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
1463eab00a20SMathieu Malaterre #ifdef DEBUG_PROM
14645827d416SAnton Blanchard 	path = prom_scratch;
1465eab00a20SMathieu Malaterre #endif
14669b6b563cSPaul Mackerras 
14679b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
14689b6b563cSPaul Mackerras 		type[0] = 0;
14699b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
14709b6b563cSPaul Mackerras 
1471c4988820SPaul Mackerras 		if (type[0] == 0) {
1472c4988820SPaul Mackerras 			/*
1473c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1474c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1475c4988820SPaul Mackerras 			 */
1476c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1477c4988820SPaul Mackerras 		}
14785827d416SAnton Blanchard 		if (strcmp(type, "memory"))
14799b6b563cSPaul Mackerras 			continue;
14809b6b563cSPaul Mackerras 
14815827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
14829b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
14839b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
14849b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
14859b6b563cSPaul Mackerras 		}
14865827d416SAnton Blanchard 		p = regbuf;
14879b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
14889b6b563cSPaul Mackerras 
14899b6b563cSPaul Mackerras #ifdef DEBUG_PROM
14909b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
14919b6b563cSPaul Mackerras 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
14929b6b563cSPaul Mackerras 		prom_debug("  node %s :\n", path);
14939b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
14949b6b563cSPaul Mackerras 
14959b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
14969b6b563cSPaul Mackerras 			unsigned long base, size;
14979b6b563cSPaul Mackerras 
14989b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
14999b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
15009b6b563cSPaul Mackerras 
15019b6b563cSPaul Mackerras 			if (size == 0)
15029b6b563cSPaul Mackerras 				continue;
1503eae5f709SMathieu Malaterre 			prom_debug("    %lx %lx\n", base, size);
15045827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
15055827d416SAnton Blanchard 				rmo_top = size;
15065827d416SAnton Blanchard 			if ((base + size) > ram_top)
15075827d416SAnton Blanchard 				ram_top = base + size;
15089b6b563cSPaul Mackerras 		}
15099b6b563cSPaul Mackerras 	}
15109b6b563cSPaul Mackerras 
15115827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
15129b6b563cSPaul Mackerras 
15139b6b563cSPaul Mackerras 	/*
1514cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1515cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1516cf68787bSBenjamin Krill 	 * TCE's up there.
1517cf68787bSBenjamin Krill 	 */
1518cf68787bSBenjamin Krill 
15195827d416SAnton Blanchard 	alloc_top_high = ram_top;
1520cf68787bSBenjamin Krill 
15215827d416SAnton Blanchard 	if (prom_memory_limit) {
15225827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1523eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx <= alloc_bottom.\n",
15245827d416SAnton Blanchard 				    prom_memory_limit);
15255827d416SAnton Blanchard 			prom_memory_limit = 0;
15265827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1527eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx >= ram_top.\n",
15285827d416SAnton Blanchard 				    prom_memory_limit);
15295827d416SAnton Blanchard 			prom_memory_limit = 0;
1530cf68787bSBenjamin Krill 		} else {
15315827d416SAnton Blanchard 			ram_top = prom_memory_limit;
15325827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1533cf68787bSBenjamin Krill 		}
1534cf68787bSBenjamin Krill 	}
1535cf68787bSBenjamin Krill 
1536cf68787bSBenjamin Krill 	/*
15379b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
15389b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
15399b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
15409b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
15419b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
15429b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
15439b6b563cSPaul Mackerras 	 */
15445827d416SAnton Blanchard 	if (!rmo_top)
15455827d416SAnton Blanchard 		rmo_top = ram_top;
15465827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
15475827d416SAnton Blanchard 	alloc_top = rmo_top;
15485827d416SAnton Blanchard 	alloc_top_high = ram_top;
15499b6b563cSPaul Mackerras 
155064968f60SPaul Mackerras 	/*
155164968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
155264968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
155364968f60SPaul Mackerras 	 */
15545827d416SAnton Blanchard 	if (prom_initrd_start &&
15555827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
15565827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
15575827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
155864968f60SPaul Mackerras 
15599b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
1560eae5f709SMathieu Malaterre 	prom_printf("  memory_limit : %lx (16 MB aligned)\n",
1561eae5f709SMathieu Malaterre 		    prom_memory_limit);
1562eae5f709SMathieu Malaterre 	prom_printf("  alloc_bottom : %lx\n", alloc_bottom);
1563eae5f709SMathieu Malaterre 	prom_printf("  alloc_top    : %lx\n", alloc_top);
1564eae5f709SMathieu Malaterre 	prom_printf("  alloc_top_hi : %lx\n", alloc_top_high);
1565eae5f709SMathieu Malaterre 	prom_printf("  rmo_top      : %lx\n", rmo_top);
1566eae5f709SMathieu Malaterre 	prom_printf("  ram_top      : %lx\n", ram_top);
15679b6b563cSPaul Mackerras }
15689b6b563cSPaul Mackerras 
156927f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
157027f44888SBenjamin Herrenschmidt {
1571493adffcSBenjamin Herrenschmidt 	__be32 val;
1572493adffcSBenjamin Herrenschmidt 	ihandle stdin;
157327f44888SBenjamin Herrenschmidt 
1574493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1575493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1576493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1577493adffcSBenjamin Herrenschmidt 	}
157827f44888SBenjamin Herrenschmidt }
157927f44888SBenjamin Herrenschmidt 
15809b6b563cSPaul Mackerras /*
15819b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
15829b6b563cSPaul Mackerras  */
15839b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
15849b6b563cSPaul Mackerras {
15859b6b563cSPaul Mackerras 	phandle rtas_node;
15869b6b563cSPaul Mackerras 	ihandle rtas_inst;
15879b6b563cSPaul Mackerras 	u32 base, entry = 0;
1588493adffcSBenjamin Herrenschmidt 	__be32 val;
15899b6b563cSPaul Mackerras 	u32 size = 0;
15909b6b563cSPaul Mackerras 
15919b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
15929b6b563cSPaul Mackerras 
15939b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
15949b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
15959b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
15969b6b563cSPaul Mackerras 		return;
15979b6b563cSPaul Mackerras 
1598493adffcSBenjamin Herrenschmidt 	val = 0;
1599493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1600493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
16019b6b563cSPaul Mackerras 	if (size == 0)
16029b6b563cSPaul Mackerras 		return;
16039b6b563cSPaul Mackerras 
16049b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
16056d1e2c6cSAnton Blanchard 	if (base == 0)
16066d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
16079b6b563cSPaul Mackerras 
16089b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
16099b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1610a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
16119b6b563cSPaul Mackerras 		return;
16129b6b563cSPaul Mackerras 	}
16139b6b563cSPaul Mackerras 
16149b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
16159b6b563cSPaul Mackerras 
16169b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
16179b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1618a23414beSPaul Mackerras 			  rtas_inst, base) != 0
16199b6b563cSPaul Mackerras 	    || entry == 0) {
16209b6b563cSPaul Mackerras 		prom_printf(" failed\n");
16219b6b563cSPaul Mackerras 		return;
16229b6b563cSPaul Mackerras 	}
16239b6b563cSPaul Mackerras 	prom_printf(" done\n");
16249b6b563cSPaul Mackerras 
16259b6b563cSPaul Mackerras 	reserve_mem(base, size);
16269b6b563cSPaul Mackerras 
1627493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1628a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1629493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1630493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1631a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1632493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
16339b6b563cSPaul Mackerras 
1634dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1635dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1636dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1637dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1638dbe78b40SBenjamin Herrenschmidt 
16399b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
16409b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
1641eae5f709SMathieu Malaterre 	prom_debug("rtas size     = 0x%x\n", size);
16429b6b563cSPaul Mackerras 
16439b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
16449b6b563cSPaul Mackerras }
16459b6b563cSPaul Mackerras 
16469b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
16479b6b563cSPaul Mackerras /*
16484a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
16494a727429SAshley Lai  */
16504a727429SAshley Lai static void __init prom_instantiate_sml(void)
16514a727429SAshley Lai {
16524a727429SAshley Lai 	phandle ibmvtpm_node;
16534a727429SAshley Lai 	ihandle ibmvtpm_inst;
1654b4ed0469SHon Ching \(Vicky\) Lo 	u32 entry = 0, size = 0, succ = 0;
16554a727429SAshley Lai 	u64 base;
1656b4ed0469SHon Ching \(Vicky\) Lo 	__be32 val;
16574a727429SAshley Lai 
16584a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
16594a727429SAshley Lai 
16602f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
16614a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
16624a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
16634a727429SAshley Lai 		return;
16644a727429SAshley Lai 
16652f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
16664a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
16674a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
16684a727429SAshley Lai 		return;
16694a727429SAshley Lai 	}
16704a727429SAshley Lai 
1671b4ed0469SHon Ching \(Vicky\) Lo 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1672b4ed0469SHon Ching \(Vicky\) Lo 			 &val, sizeof(val)) != PROM_ERROR) {
1673b4ed0469SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &succ,
1674b4ed0469SHon Ching \(Vicky\) Lo 				  ADDR("reformat-sml-to-efi-alignment"),
1675b4ed0469SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || succ == 0) {
1676b4ed0469SHon Ching \(Vicky\) Lo 			prom_printf("Reformat SML to EFI alignment failed\n");
1677b4ed0469SHon Ching \(Vicky\) Lo 			return;
1678b4ed0469SHon Ching \(Vicky\) Lo 		}
1679b4ed0469SHon Ching \(Vicky\) Lo 
16804a727429SAshley Lai 		if (call_prom_ret("call-method", 2, 2, &size,
16819e5d4af4SHon Ching \(Vicky\) Lo 				  ADDR("sml-get-allocated-size"),
16829e5d4af4SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || size == 0) {
16839e5d4af4SHon Ching \(Vicky\) Lo 			prom_printf("SML get allocated size failed\n");
16849e5d4af4SHon Ching \(Vicky\) Lo 			return;
16859e5d4af4SHon Ching \(Vicky\) Lo 		}
16869e5d4af4SHon Ching \(Vicky\) Lo 	} else {
16879e5d4af4SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &size,
16884a727429SAshley Lai 				  ADDR("sml-get-handover-size"),
16894a727429SAshley Lai 				  ibmvtpm_inst) != 0 || size == 0) {
16904a727429SAshley Lai 			prom_printf("SML get handover size failed\n");
16914a727429SAshley Lai 			return;
16924a727429SAshley Lai 		}
16939e5d4af4SHon Ching \(Vicky\) Lo 	}
16944a727429SAshley Lai 
16954a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
16964a727429SAshley Lai 	if (base == 0)
16974a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
16984a727429SAshley Lai 
1699eae5f709SMathieu Malaterre 	prom_printf("instantiating sml at 0x%llx...", base);
17004a727429SAshley Lai 
17019e5d4af4SHon Ching \(Vicky\) Lo 	memset((void *)base, 0, size);
17029e5d4af4SHon Ching \(Vicky\) Lo 
17034a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
17044a727429SAshley Lai 			  ADDR("sml-handover"),
17054a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
17064a727429SAshley Lai 		prom_printf("SML handover failed\n");
17074a727429SAshley Lai 		return;
17084a727429SAshley Lai 	}
17094a727429SAshley Lai 	prom_printf(" done\n");
17104a727429SAshley Lai 
17114a727429SAshley Lai 	reserve_mem(base, size);
17124a727429SAshley Lai 
17132f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
17144a727429SAshley Lai 		     &base, sizeof(base));
17152f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
17164a727429SAshley Lai 		     &size, sizeof(size));
17174a727429SAshley Lai 
1718eae5f709SMathieu Malaterre 	prom_debug("sml base     = 0x%llx\n", base);
1719eae5f709SMathieu Malaterre 	prom_debug("sml size     = 0x%x\n", size);
17204a727429SAshley Lai 
17214a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
17224a727429SAshley Lai }
17234a727429SAshley Lai 
17244a727429SAshley Lai /*
17259b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
17269b6b563cSPaul Mackerras  */
1727493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
17289b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
17299b6b563cSPaul Mackerras {
17309b6b563cSPaul Mackerras 	phandle node;
17319b6b563cSPaul Mackerras 	ihandle phb_node;
17329b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
17335827d416SAnton Blanchard 	char *path = prom_scratch;
17349b6b563cSPaul Mackerras 	u64 base, align;
17359b6b563cSPaul Mackerras 	u32 minalign, minsize;
17369b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
17379b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
17389b6b563cSPaul Mackerras 	u64 i;
17399b6b563cSPaul Mackerras 
17405827d416SAnton Blanchard 	if (prom_iommu_off)
17419b6b563cSPaul Mackerras 		return;
17429b6b563cSPaul Mackerras 
17439b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
17449b6b563cSPaul Mackerras 
17459b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
17465827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
17479b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
17489b6b563cSPaul Mackerras 
17499b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
17509b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
17519b6b563cSPaul Mackerras 		compatible[0] = 0;
17529b6b563cSPaul Mackerras 		type[0] = 0;
17539b6b563cSPaul Mackerras 		model[0] = 0;
17549b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
17559b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
17569b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
17579b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
17589b6b563cSPaul Mackerras 
17595827d416SAnton Blanchard 		if ((type[0] == 0) || (strstr(type, "pci") == NULL))
17609b6b563cSPaul Mackerras 			continue;
17619b6b563cSPaul Mackerras 
1762e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
17639b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
17645827d416SAnton Blanchard 			if ((strstr(compatible, "python") == NULL) &&
17655827d416SAnton Blanchard 			    (strstr(compatible, "Speedwagon") == NULL) &&
17665827d416SAnton Blanchard 			    (strstr(compatible, "Winnipeg") == NULL))
17679b6b563cSPaul Mackerras 				continue;
17689b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
17695827d416SAnton Blanchard 			if ((strstr(model, "ython") == NULL) &&
17705827d416SAnton Blanchard 			    (strstr(model, "peedwagon") == NULL) &&
17715827d416SAnton Blanchard 			    (strstr(model, "innipeg") == NULL))
17729b6b563cSPaul Mackerras 				continue;
17739b6b563cSPaul Mackerras 		}
17749b6b563cSPaul Mackerras 
17759b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
17769b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
17779b6b563cSPaul Mackerras 			minalign = 0;
17789b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
17799b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
17809b6b563cSPaul Mackerras 			minsize = 4UL << 20;
17819b6b563cSPaul Mackerras 
17829b6b563cSPaul Mackerras 		/*
17839b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
17849b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
17859b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
17869b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
17879b6b563cSPaul Mackerras 		 */
17889b6b563cSPaul Mackerras 		minsize = 4UL << 20;
17899b6b563cSPaul Mackerras 
17909b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
17919b6b563cSPaul Mackerras 		align = max(minalign, minsize);
17929b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
17939b6b563cSPaul Mackerras 		if (base == 0)
17949b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
17959b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
17969b6b563cSPaul Mackerras 			local_alloc_bottom = base;
17979b6b563cSPaul Mackerras 
17989b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
1799aca71ef8SLi Zefan 		memset(path, 0, PROM_SCRATCH_SIZE);
18009b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
18019b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
18029b6b563cSPaul Mackerras 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
18039b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
18049b6b563cSPaul Mackerras 		}
18059b6b563cSPaul Mackerras 
1806a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
1807a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1808a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1809a23414beSPaul Mackerras 
18109b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
18119b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
1812eae5f709SMathieu Malaterre 		prom_debug("\tbase = 0x%llx\n", base);
18139b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
18149b6b563cSPaul Mackerras 
18159b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
18169b6b563cSPaul Mackerras 		 * over the allocated size.
18179b6b563cSPaul Mackerras 		 */
18182b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
18199b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
18209b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
18219b6b563cSPaul Mackerras 			tce_entry |= 0x3;
18229b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
18239b6b563cSPaul Mackerras 		}
18249b6b563cSPaul Mackerras 
18259b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
18269b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
18279b6b563cSPaul Mackerras 		if (phb_node == 0)
18289b6b563cSPaul Mackerras 			prom_printf("... failed\n");
18299b6b563cSPaul Mackerras 		else
18309b6b563cSPaul Mackerras 			prom_printf("... done\n");
18319b6b563cSPaul Mackerras 
18329b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
18339b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
18349b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
18359b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
18369b6b563cSPaul Mackerras 	}
18379b6b563cSPaul Mackerras 
18389b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
18399b6b563cSPaul Mackerras 
18402babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
18412babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
18425827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
18435827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
18449b6b563cSPaul Mackerras 
18459b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
18469b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
18479b6b563cSPaul Mackerras }
1848493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1849493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
18509b6b563cSPaul Mackerras 
18519b6b563cSPaul Mackerras /*
18529b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
18539b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
18549b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
18559b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
18569b6b563cSPaul Mackerras  *
18579b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
18589b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
18599b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
18609b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
18619b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
18629b6b563cSPaul Mackerras  * of setting those values.
18639b6b563cSPaul Mackerras  *
18649b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
18659b6b563cSPaul Mackerras  * is in its holding pattern code.
18669b6b563cSPaul Mackerras  *
18679b6b563cSPaul Mackerras  * -- Cort
18689b6b563cSPaul Mackerras  */
1869bbd0abdaSPaul Mackerras /*
1870bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
1871bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
1872bbd0abdaSPaul Mackerras  */
1873bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1874bbd0abdaSPaul Mackerras 
18759b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
18769b6b563cSPaul Mackerras {
18779b6b563cSPaul Mackerras 	unsigned long i;
18789b6b563cSPaul Mackerras 	phandle node;
18799b6b563cSPaul Mackerras 	char type[64];
18809b6b563cSPaul Mackerras 	unsigned long *spinloop
1881bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
18829b6b563cSPaul Mackerras 	unsigned long *acknowledge
1883bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1884bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
18859b6b563cSPaul Mackerras 
1886dbe78b40SBenjamin Herrenschmidt 	/*
1887dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
1888dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
1889dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
1890dbe78b40SBenjamin Herrenschmidt 	 */
1891dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
1892dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
1893dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
1894dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
1895dbe78b40SBenjamin Herrenschmidt 		return;
1896dbe78b40SBenjamin Herrenschmidt 	}
1897dbe78b40SBenjamin Herrenschmidt 
18989b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
1899eae5f709SMathieu Malaterre 	prom_debug("    1) spinloop       = 0x%lx\n", (unsigned long)spinloop);
1900eae5f709SMathieu Malaterre 	prom_debug("    1) *spinloop      = 0x%lx\n", *spinloop);
1901eae5f709SMathieu Malaterre 	prom_debug("    1) acknowledge    = 0x%lx\n",
19029b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
1903eae5f709SMathieu Malaterre 	prom_debug("    1) *acknowledge   = 0x%lx\n", *acknowledge);
1904eae5f709SMathieu Malaterre 	prom_debug("    1) secondary_hold = 0x%lx\n", secondary_hold);
19059b6b563cSPaul Mackerras 
19069b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
19079b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
19089b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
19099b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
19109b6b563cSPaul Mackerras 	 */
19119b6b563cSPaul Mackerras 	*spinloop = 0;
19129b6b563cSPaul Mackerras 
19139b6b563cSPaul Mackerras 	/* look for cpus */
19149b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
1915493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
1916493adffcSBenjamin Herrenschmidt 		__be32 reg;
1917493adffcSBenjamin Herrenschmidt 
19189b6b563cSPaul Mackerras 		type[0] = 0;
19199b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
19205827d416SAnton Blanchard 		if (strcmp(type, "cpu") != 0)
19219b6b563cSPaul Mackerras 			continue;
19229b6b563cSPaul Mackerras 
19239b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
19249b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
19255827d416SAnton Blanchard 			if (strcmp(type, "okay") != 0)
19269b6b563cSPaul Mackerras 				continue;
19279b6b563cSPaul Mackerras 
1928493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
19299b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
1930493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
19319b6b563cSPaul Mackerras 
1932eae5f709SMathieu Malaterre 		prom_debug("cpu hw idx   = %u\n", cpu_no);
19339b6b563cSPaul Mackerras 
19349b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
19359b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
19369b6b563cSPaul Mackerras 		 * spinloop.
19379b6b563cSPaul Mackerras 		 */
19389b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
19399b6b563cSPaul Mackerras 
1940493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
194127f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
1942eae5f709SMathieu Malaterre 			prom_printf("starting cpu hw idx %u... ", cpu_no);
19439b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
1944493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
19459b6b563cSPaul Mackerras 
19469b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
19479b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
19489b6b563cSPaul Mackerras 				mb();
19499b6b563cSPaul Mackerras 
1950493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
19519b6b563cSPaul Mackerras 				prom_printf("done\n");
1952bbd0abdaSPaul Mackerras 			else
1953eae5f709SMathieu Malaterre 				prom_printf("failed: %lx\n", *acknowledge);
19549b6b563cSPaul Mackerras 		}
19559b6b563cSPaul Mackerras #ifdef CONFIG_SMP
19569b6b563cSPaul Mackerras 		else
1957eae5f709SMathieu Malaterre 			prom_printf("boot cpu hw idx %u\n", cpu_no);
19589b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
19599b6b563cSPaul Mackerras 	}
19609b6b563cSPaul Mackerras 
19619b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
19629b6b563cSPaul Mackerras }
19639b6b563cSPaul Mackerras 
19649b6b563cSPaul Mackerras 
19659b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
19669b6b563cSPaul Mackerras {
19679b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
19685827d416SAnton Blanchard 	prom_entry = pp;
19699b6b563cSPaul Mackerras 
19709b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
19715827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
19725827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
19739b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
19749b6b563cSPaul Mackerras 
19759b6b563cSPaul Mackerras 	/* get device tree root */
19765827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
19775827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
19789b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1979a575b807SPaul Mackerras 
19805827d416SAnton Blanchard 	prom.mmumap = 0;
19819b6b563cSPaul Mackerras }
19829b6b563cSPaul Mackerras 
1983a575b807SPaul Mackerras #ifdef CONFIG_PPC32
1984a575b807SPaul Mackerras /*
1985a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
1986a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
1987a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
1988a575b807SPaul Mackerras  */
1989a575b807SPaul Mackerras static void __init prom_find_mmu(void)
1990a575b807SPaul Mackerras {
1991a575b807SPaul Mackerras 	phandle oprom;
1992a575b807SPaul Mackerras 	char version[64];
1993a575b807SPaul Mackerras 
1994a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1995a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
1996a575b807SPaul Mackerras 		return;
1997a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1998a575b807SPaul Mackerras 		return;
1999a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
2000a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
2001a23414beSPaul Mackerras 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
2002a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
2003a23414beSPaul Mackerras 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
2004a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
2005a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2006a23414beSPaul Mackerras 	} else
2007a575b807SPaul Mackerras 		return;
20085827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
20095827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
20105827d416SAnton Blanchard 		     sizeof(prom.mmumap));
2011493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
20125827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2013a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
2014a575b807SPaul Mackerras }
2015a575b807SPaul Mackerras #else
2016a575b807SPaul Mackerras #define prom_find_mmu()
2017a575b807SPaul Mackerras #endif
2018a575b807SPaul Mackerras 
20199b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
20209b6b563cSPaul Mackerras {
20215827d416SAnton Blanchard 	char *path = of_stdout_device;
20229b6b563cSPaul Mackerras 	char type[16];
2023493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
2024493adffcSBenjamin Herrenschmidt 	__be32 val;
20259b6b563cSPaul Mackerras 
20265827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
20279b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
20289b6b563cSPaul Mackerras 
2029493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
20309b6b563cSPaul Mackerras 
20319b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
20329b6b563cSPaul Mackerras 	memset(path, 0, 256);
20335827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
20345827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
20355827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2036a23414beSPaul Mackerras 		     path, strlen(path) + 1);
20379b6b563cSPaul Mackerras 
203810348f59SBenjamin Herrenschmidt 	/* instance-to-package fails on PA-Semi */
203910348f59SBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
204010348f59SBenjamin Herrenschmidt 	if (stdout_node != PROM_ERROR) {
204110348f59SBenjamin Herrenschmidt 		val = cpu_to_be32(stdout_node);
204210348f59SBenjamin Herrenschmidt 
20439b6b563cSPaul Mackerras 		/* If it's a display, note it */
20449b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
2045493adffcSBenjamin Herrenschmidt 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
20465827d416SAnton Blanchard 		if (strcmp(type, "display") == 0)
2047493adffcSBenjamin Herrenschmidt 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
20489b6b563cSPaul Mackerras 	}
204910348f59SBenjamin Herrenschmidt }
20509b6b563cSPaul Mackerras 
20519b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
20529b6b563cSPaul Mackerras {
20539b6b563cSPaul Mackerras 	char compat[256];
20549b6b563cSPaul Mackerras 	int len, i = 0;
205521fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
20569b6b563cSPaul Mackerras 	phandle rtas;
2057e8222502SBenjamin Herrenschmidt 	int x;
205821fe3301SBenjamin Herrenschmidt #endif
2059e8222502SBenjamin Herrenschmidt 
20606e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
20615827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
20629b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
20639b6b563cSPaul Mackerras 	if (len > 0) {
20649b6b563cSPaul Mackerras 		compat[len] = 0;
20659b6b563cSPaul Mackerras 		while (i < len) {
20669b6b563cSPaul Mackerras 			char *p = &compat[i];
20679b6b563cSPaul Mackerras 			int sl = strlen(p);
20689b6b563cSPaul Mackerras 			if (sl == 0)
20699b6b563cSPaul Mackerras 				break;
20705827d416SAnton Blanchard 			if (strstr(p, "Power Macintosh") ||
20715827d416SAnton Blanchard 			    strstr(p, "MacRISC"))
20729b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
2073133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
2074133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
2075133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
2076133dda1eSArnd Bergmann 			 * so we do it here.
2077133dda1eSArnd Bergmann 			 */
20785827d416SAnton Blanchard 			if (strstr(p, "IBM,CBEA") ||
20795827d416SAnton Blanchard 			    strstr(p, "IBM,CPBW-1.0"))
2080133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
2081133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
20829b6b563cSPaul Mackerras 			i += sl + 1;
20839b6b563cSPaul Mackerras 		}
20849b6b563cSPaul Mackerras 	}
20859b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
20866e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
2087e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
2088e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
2089e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
2090e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
2091e8222502SBenjamin Herrenschmidt 	 */
20925827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
2093e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
2094e8222502SBenjamin Herrenschmidt 	if (len <= 0)
2095e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
20965827d416SAnton Blanchard 	if (strcmp(compat, "chrp"))
2097e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2098e8222502SBenjamin Herrenschmidt 
20999b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
21009b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2101e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
2102e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2103e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
21049b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
21054da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
21069b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
21079b6b563cSPaul Mackerras 	}
21089b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
21099b6b563cSPaul Mackerras #else
2110e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
21119b6b563cSPaul Mackerras #endif
21129b6b563cSPaul Mackerras }
21139b6b563cSPaul Mackerras 
21149b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
21159b6b563cSPaul Mackerras {
21169b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
21179b6b563cSPaul Mackerras }
21189b6b563cSPaul Mackerras 
21199b6b563cSPaul Mackerras /*
21209b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
21219b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
21229b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
21239b6b563cSPaul Mackerras  * we've taken over the MMU.
21249b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
21259b6b563cSPaul Mackerras  * and if so, open it now.
21269b6b563cSPaul Mackerras  */
21279b6b563cSPaul Mackerras static void __init prom_check_displays(void)
21289b6b563cSPaul Mackerras {
21299b6b563cSPaul Mackerras 	char type[16], *path;
21309b6b563cSPaul Mackerras 	phandle node;
21319b6b563cSPaul Mackerras 	ihandle ih;
21329b6b563cSPaul Mackerras 	int i;
21339b6b563cSPaul Mackerras 
2134d00e34b9SBenjamin Herrenschmidt 	static const unsigned char default_colors[] __initconst = {
21359b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
21369b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
21379b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
21389b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
21399b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
21409b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
21419b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
21429b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
21439b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
21449b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
21459b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
21469b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
21479b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
21489b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
21499b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
21509b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
21519b6b563cSPaul Mackerras 	};
21529b6b563cSPaul Mackerras 	const unsigned char *clut;
21539b6b563cSPaul Mackerras 
21544da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
21559b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
21569b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
21579b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
21585827d416SAnton Blanchard 		if (strcmp(type, "display") != 0)
21599b6b563cSPaul Mackerras 			continue;
21609b6b563cSPaul Mackerras 
21619b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
21625827d416SAnton Blanchard 		path = prom_scratch;
21639b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
21649b6b563cSPaul Mackerras 
21659b6b563cSPaul Mackerras 		/*
21669b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
21679b6b563cSPaul Mackerras 		 * arguments
21689b6b563cSPaul Mackerras 		 */
21699b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
21709b6b563cSPaul Mackerras 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
21719b6b563cSPaul Mackerras 			continue;
21729b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
21739b6b563cSPaul Mackerras 
21749b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
21759b6b563cSPaul Mackerras 		if (ih == 0) {
21769b6b563cSPaul Mackerras 			prom_printf("failed\n");
21779b6b563cSPaul Mackerras 			continue;
21789b6b563cSPaul Mackerras 		}
21799b6b563cSPaul Mackerras 
21809b6b563cSPaul Mackerras 		/* Success */
21819b6b563cSPaul Mackerras 		prom_printf("done\n");
2182a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
21839b6b563cSPaul Mackerras 
21849b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
21859b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
21865827d416SAnton Blanchard 		clut = default_colors;
21873f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
21889b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
21899b6b563cSPaul Mackerras 					   clut[2]) != 0)
21909b6b563cSPaul Mackerras 				break;
21919b6b563cSPaul Mackerras 
21929b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
21935827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
21945827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
21959b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
21969b6b563cSPaul Mackerras 					   clut[2]) != 0)
21979b6b563cSPaul Mackerras 				break;
21989b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
21997191b615SBenjamin Herrenschmidt 
22007191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
22017191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
22027191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
22037191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
22047191b615SBenjamin Herrenschmidt 
22057191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
22067191b615SBenjamin Herrenschmidt 			prom_getprop(node, "width", &width, 4);
22077191b615SBenjamin Herrenschmidt 			prom_getprop(node, "height", &height, 4);
22087191b615SBenjamin Herrenschmidt 			prom_getprop(node, "linebytes", &pitch, 4);
22097191b615SBenjamin Herrenschmidt 			prom_getprop(node, "address", &addr, 4);
22107191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
22117191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
22127191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
22137191b615SBenjamin Herrenschmidt 		}
22147191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
22159b6b563cSPaul Mackerras 	}
22169b6b563cSPaul Mackerras }
22179b6b563cSPaul Mackerras 
22189b6b563cSPaul Mackerras 
22199b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
22209b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
22219b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
22229b6b563cSPaul Mackerras {
22239b6b563cSPaul Mackerras 	void *ret;
22249b6b563cSPaul Mackerras 
22259b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
22269b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
22279b6b563cSPaul Mackerras 		unsigned long room, chunk;
22289b6b563cSPaul Mackerras 
2229eae5f709SMathieu Malaterre 		prom_debug("Chunk exhausted, claiming more at %lx...\n",
22305827d416SAnton Blanchard 			   alloc_bottom);
22315827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
22329b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
22339b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
22349b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2235fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2236fbafd728SAnton Blanchard 				   "(no room)\n");
22379b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
22389b6b563cSPaul Mackerras 		if (chunk == 0)
2239fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2240fbafd728SAnton Blanchard 				   "(claim failed)\n");
2241966728ddSAnton Blanchard 		*mem_end = chunk + room;
22429b6b563cSPaul Mackerras 	}
22439b6b563cSPaul Mackerras 
22449b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
22459b6b563cSPaul Mackerras 	*mem_start += needed;
22469b6b563cSPaul Mackerras 
22479b6b563cSPaul Mackerras 	return ret;
22489b6b563cSPaul Mackerras }
22499b6b563cSPaul Mackerras 
2250493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2251493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2252493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2253493adffcSBenjamin Herrenschmidt 	} while(0)
22549b6b563cSPaul Mackerras 
22559b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
22569b6b563cSPaul Mackerras {
22579b6b563cSPaul Mackerras 	char *s, *os;
22589b6b563cSPaul Mackerras 
22595827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
22609b6b563cSPaul Mackerras 	s += 4;
22615827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
22629b6b563cSPaul Mackerras 		if (strcmp(s, str) == 0)
22639b6b563cSPaul Mackerras 			return s - os;
22649b6b563cSPaul Mackerras 		s += strlen(s) + 1;
22659b6b563cSPaul Mackerras 	}
22669b6b563cSPaul Mackerras 	return 0;
22679b6b563cSPaul Mackerras }
22689b6b563cSPaul Mackerras 
22699b6b563cSPaul Mackerras /*
22709b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
22719b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
22729b6b563cSPaul Mackerras  */
22739b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
22749b6b563cSPaul Mackerras 
22759b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
22769b6b563cSPaul Mackerras 					 unsigned long *mem_start,
22779b6b563cSPaul Mackerras 					 unsigned long *mem_end)
22789b6b563cSPaul Mackerras {
22799b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
22809b6b563cSPaul Mackerras 	unsigned long soff;
22819b6b563cSPaul Mackerras 	phandle child;
22829b6b563cSPaul Mackerras 
22835827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
22849b6b563cSPaul Mackerras 
22859b6b563cSPaul Mackerras 	/* get and store all property names */
22865827d416SAnton Blanchard 	prev_name = "";
22879b6b563cSPaul Mackerras 	for (;;) {
22889b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
22899b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
22909b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
22919b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
22929b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
22939b6b563cSPaul Mackerras 			break;
22949b6b563cSPaul Mackerras 		}
22959b6b563cSPaul Mackerras 
22969b6b563cSPaul Mackerras  		/* skip "name" */
22975827d416SAnton Blanchard  		if (strcmp(namep, "name") == 0) {
22989b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
22995827d416SAnton Blanchard  			prev_name = "name";
23009b6b563cSPaul Mackerras  			continue;
23019b6b563cSPaul Mackerras  		}
23029b6b563cSPaul Mackerras 		/* get/create string entry */
23039b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
23049b6b563cSPaul Mackerras 		if (soff != 0) {
23059b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
23069b6b563cSPaul Mackerras 			namep = sstart + soff;
23079b6b563cSPaul Mackerras 		} else {
23089b6b563cSPaul Mackerras 			/* Trim off some if we can */
23099b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
23105827d416SAnton Blanchard 			dt_string_end = *mem_start;
23119b6b563cSPaul Mackerras 		}
23129b6b563cSPaul Mackerras 		prev_name = namep;
23139b6b563cSPaul Mackerras 	}
23149b6b563cSPaul Mackerras 
23159b6b563cSPaul Mackerras 	/* do all our children */
23169b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
23179b6b563cSPaul Mackerras 	while (child != 0) {
23189b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
23199b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
23209b6b563cSPaul Mackerras 	}
23219b6b563cSPaul Mackerras }
23229b6b563cSPaul Mackerras 
23239b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
23249b6b563cSPaul Mackerras 					unsigned long *mem_end)
23259b6b563cSPaul Mackerras {
23269b6b563cSPaul Mackerras 	phandle child;
23279b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
23289b6b563cSPaul Mackerras 	unsigned long soff;
23299b6b563cSPaul Mackerras 	unsigned char *valp;
23308ca2d515SBenjamin Herrenschmidt 	static char pname[MAX_PROPERTY_NAME] __prombss;
23316e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
23329b6b563cSPaul Mackerras 
23339b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
23349b6b563cSPaul Mackerras 
23359b6b563cSPaul Mackerras 	/* get the node's full name */
23369b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2337c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2338c4988820SPaul Mackerras 	if (room > 255)
2339c4988820SPaul Mackerras 		room = 255;
2340c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
23419b6b563cSPaul Mackerras 	if (l >= 0) {
23429b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2343c4988820SPaul Mackerras 		if (l >= room) {
2344c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
23459b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
23469b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
23479b6b563cSPaul Mackerras 		}
23489b6b563cSPaul Mackerras 		namep[l] = '\0';
23499b6b563cSPaul Mackerras 
23509b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2351a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2352a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
23539b6b563cSPaul Mackerras 		 */
2354a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
23559b6b563cSPaul Mackerras 			if (*p == '/')
2356a575b807SPaul Mackerras 				lp = namep;
2357a575b807SPaul Mackerras 			else if (*p != 0)
2358a575b807SPaul Mackerras 				*lp++ = *p;
2359a575b807SPaul Mackerras 		}
2360a575b807SPaul Mackerras 		*lp = 0;
2361a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
23629b6b563cSPaul Mackerras 	}
23639b6b563cSPaul Mackerras 
23649b6b563cSPaul Mackerras 	/* get it again for debugging */
23655827d416SAnton Blanchard 	path = prom_scratch;
23669b6b563cSPaul Mackerras 	memset(path, 0, PROM_SCRATCH_SIZE);
23679b6b563cSPaul Mackerras 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
23689b6b563cSPaul Mackerras 
23699b6b563cSPaul Mackerras 	/* get and store all properties */
23705827d416SAnton Blanchard 	prev_name = "";
23715827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
23729b6b563cSPaul Mackerras 	for (;;) {
23739b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
23745827d416SAnton Blanchard 			      pname) != 1)
23759b6b563cSPaul Mackerras 			break;
23769b6b563cSPaul Mackerras 
23779b6b563cSPaul Mackerras  		/* skip "name" */
23785827d416SAnton Blanchard  		if (strcmp(pname, "name") == 0) {
23795827d416SAnton Blanchard  			prev_name = "name";
23809b6b563cSPaul Mackerras  			continue;
23819b6b563cSPaul Mackerras  		}
23829b6b563cSPaul Mackerras 
23839b6b563cSPaul Mackerras 		/* find string offset */
23845827d416SAnton Blanchard 		soff = dt_find_string(pname);
23859b6b563cSPaul Mackerras 		if (soff == 0) {
23869b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
23875827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
23889b6b563cSPaul Mackerras 			break;
23899b6b563cSPaul Mackerras 		}
23909b6b563cSPaul Mackerras 		prev_name = sstart + soff;
23919b6b563cSPaul Mackerras 
23929b6b563cSPaul Mackerras 		/* get length */
23935827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
23949b6b563cSPaul Mackerras 
23959b6b563cSPaul Mackerras 		/* sanity checks */
23969b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
23979b6b563cSPaul Mackerras 			continue;
23989b6b563cSPaul Mackerras 
23999b6b563cSPaul Mackerras 		/* push property head */
24009b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
24019b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
24029b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
24039b6b563cSPaul Mackerras 
24049b6b563cSPaul Mackerras 		/* push property content */
24059b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
24065827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
24079b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
24086e35d5daSBenjamin Herrenschmidt 
24095827d416SAnton Blanchard 		if (!strcmp(pname, "phandle"))
24106e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
24119b6b563cSPaul Mackerras 	}
24129b6b563cSPaul Mackerras 
2413*f1f208e5SBenjamin Herrenschmidt 	/* Add a "phandle" property if none already exist */
24146e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
2415*f1f208e5SBenjamin Herrenschmidt 		soff = dt_find_string("phandle");
24169b6b563cSPaul Mackerras 		if (soff == 0)
2417*f1f208e5SBenjamin Herrenschmidt 			prom_printf("WARNING: Can't find string index for <phandle> node %s\n", path);
24189b6b563cSPaul Mackerras 		else {
24199b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
24209b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
24219b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
24229b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
2423493adffcSBenjamin Herrenschmidt 			*(__be32 *)valp = cpu_to_be32(node);
24249b6b563cSPaul Mackerras 		}
24256e35d5daSBenjamin Herrenschmidt 	}
24269b6b563cSPaul Mackerras 
24279b6b563cSPaul Mackerras 	/* do all our children */
24289b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
24299b6b563cSPaul Mackerras 	while (child != 0) {
24309b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
24319b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
24329b6b563cSPaul Mackerras 	}
24339b6b563cSPaul Mackerras 
24349b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
24359b6b563cSPaul Mackerras }
24369b6b563cSPaul Mackerras 
24379b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
24389b6b563cSPaul Mackerras {
24399b6b563cSPaul Mackerras 	phandle root;
24409b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
24419b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
24429b6b563cSPaul Mackerras 	char *namep;
24439b6b563cSPaul Mackerras 	u64 *rsvmap;
24449b6b563cSPaul Mackerras 
24459b6b563cSPaul Mackerras 	/*
24469b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2447fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
24489b6b563cSPaul Mackerras 	 */
24495827d416SAnton Blanchard 	room = alloc_top - alloc_bottom - 0x4000;
24509b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
24519b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
2452eae5f709SMathieu Malaterre 	prom_debug("starting device tree allocs at %lx\n", alloc_bottom);
24539b6b563cSPaul Mackerras 
24549b6b563cSPaul Mackerras 	/* Now try to claim that */
24559b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
24569b6b563cSPaul Mackerras 	if (mem_start == 0)
24579b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2458966728ddSAnton Blanchard 	mem_end = mem_start + room;
24599b6b563cSPaul Mackerras 
24609b6b563cSPaul Mackerras 	/* Get root of tree */
24619b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
24629b6b563cSPaul Mackerras 	if (root == (phandle)0)
24639b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
24649b6b563cSPaul Mackerras 
24659b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
24669b6b563cSPaul Mackerras 	mem_start = _ALIGN(mem_start, 4);
24679b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
24689b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
24695827d416SAnton Blanchard 	dt_header_start = (unsigned long)hdr;
24709b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
24719b6b563cSPaul Mackerras 
24729b6b563cSPaul Mackerras 	/* Start of strings */
24739b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
24745827d416SAnton Blanchard 	dt_string_start = mem_start;
24759b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
24769b6b563cSPaul Mackerras 
2477*f1f208e5SBenjamin Herrenschmidt 	/* Add "phandle" in there, we'll need it */
24789b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
2479*f1f208e5SBenjamin Herrenschmidt 	strcpy(namep, "phandle");
24809b6b563cSPaul Mackerras 	mem_start = (unsigned long)namep + strlen(namep) + 1;
24819b6b563cSPaul Mackerras 
24829b6b563cSPaul Mackerras 	/* Build string array */
24839b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
24849b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
24855827d416SAnton Blanchard 	dt_string_end = mem_start;
24869b6b563cSPaul Mackerras 
24879b6b563cSPaul Mackerras 	/* Build structure */
24889b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
24895827d416SAnton Blanchard 	dt_struct_start = mem_start;
24909b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
24919b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
24929b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
24935827d416SAnton Blanchard 	dt_struct_end = PAGE_ALIGN(mem_start);
24949b6b563cSPaul Mackerras 
24959b6b563cSPaul Mackerras 	/* Finish header */
2496493adffcSBenjamin Herrenschmidt 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2497493adffcSBenjamin Herrenschmidt 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2498493adffcSBenjamin Herrenschmidt 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2499493adffcSBenjamin Herrenschmidt 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2500493adffcSBenjamin Herrenschmidt 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2501493adffcSBenjamin Herrenschmidt 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2502493adffcSBenjamin Herrenschmidt 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2503493adffcSBenjamin Herrenschmidt 	hdr->version = cpu_to_be32(OF_DT_VERSION);
25049b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
2505493adffcSBenjamin Herrenschmidt 	hdr->last_comp_version = cpu_to_be32(0x10);
25069b6b563cSPaul Mackerras 
25074d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
25085827d416SAnton Blanchard 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
25099b6b563cSPaul Mackerras 
25109b6b563cSPaul Mackerras #ifdef DEBUG_PROM
25119b6b563cSPaul Mackerras 	{
25129b6b563cSPaul Mackerras 		int i;
25139b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
25145827d416SAnton Blanchard 		for (i = 0; i < mem_reserve_cnt; i++)
2515eae5f709SMathieu Malaterre 			prom_printf("  %llx - %llx\n",
2516493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].base),
2517493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].size));
25189b6b563cSPaul Mackerras 	}
25199b6b563cSPaul Mackerras #endif
25204d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
25214d1f3f25SJimi Xenidis 	 * since it's too late.
25224d1f3f25SJimi Xenidis 	 */
25235827d416SAnton Blanchard 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
25249b6b563cSPaul Mackerras 
2525eae5f709SMathieu Malaterre 	prom_printf("Device tree strings 0x%lx -> 0x%lx\n",
25265827d416SAnton Blanchard 		    dt_string_start, dt_string_end);
2527eae5f709SMathieu Malaterre 	prom_printf("Device tree struct  0x%lx -> 0x%lx\n",
25285827d416SAnton Blanchard 		    dt_struct_start, dt_struct_end);
25299b6b563cSPaul Mackerras }
25309b6b563cSPaul Mackerras 
253154f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
253254f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
253354f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
253454f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
25359b6b563cSPaul Mackerras {
253654f4ee18SHollis Blanchard 	phandle isa;
2537980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
253854f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2539980a6513SBenjamin Herrenschmidt 	char *name;
254054f4ee18SHollis Blanchard 
2541980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2542980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2543980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2544980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2545980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2546980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2547980a6513SBenjamin Herrenschmidt 	}
254854f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
254954f4ee18SHollis Blanchard 		return;
255054f4ee18SHollis Blanchard 
2551980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2552980a6513SBenjamin Herrenschmidt 		return;
255354f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
255454f4ee18SHollis Blanchard 		== PROM_ERROR)
255554f4ee18SHollis Blanchard 		return;
255654f4ee18SHollis Blanchard 
255754f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
255854f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
255954f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
256054f4ee18SHollis Blanchard 		return;
256154f4ee18SHollis Blanchard 
2562980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
256354f4ee18SHollis Blanchard 
256454f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
256554f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2566980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
256754f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
256854f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
256954f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2570980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
257154f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
257254f4ee18SHollis Blanchard }
25738f101a05SHarry Ciao 
25748f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
25758f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
25768f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
25778f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
25788f101a05SHarry Ciao {
25798f101a05SHarry Ciao 	phandle mc;
25808f101a05SHarry Ciao 	u32 mc_reg[4];
25818f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
25828f101a05SHarry Ciao 	u32 ac, sc;
25838f101a05SHarry Ciao 
25848f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
25858f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
25868f101a05SHarry Ciao 		return;
25878f101a05SHarry Ciao 
25888f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
25898f101a05SHarry Ciao 		return;
25908f101a05SHarry Ciao 
25915827d416SAnton Blanchard 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
25925827d416SAnton Blanchard 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
25938f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
25948f101a05SHarry Ciao 		return;
25958f101a05SHarry Ciao 
25968f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
25978f101a05SHarry Ciao 		return;
25988f101a05SHarry Ciao 
25998f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
26008f101a05SHarry Ciao 		return;
26018f101a05SHarry Ciao 
26028f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
26038f101a05SHarry Ciao 
26048f101a05SHarry Ciao 	mc_reg[0] = 0x0;
26058f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
26068f101a05SHarry Ciao 	mc_reg[2] = 0x0;
26078f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
26088f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
26098f101a05SHarry Ciao }
261054f4ee18SHollis Blanchard #else
261154f4ee18SHollis Blanchard #define fixup_device_tree_maple()
26128f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
261354f4ee18SHollis Blanchard #endif
261454f4ee18SHollis Blanchard 
2615e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2616e4805922SOlaf Hering /*
2617e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2618e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2619556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2620e4805922SOlaf Hering  */
2621e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2622e8c0acf9SBenjamin Herrenschmidt {
2623e4805922SOlaf Hering 	phandle ph;
2624e4805922SOlaf Hering 	u32 prop[6];
262526c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2626e8c0acf9SBenjamin Herrenschmidt 	char *name;
2627e8c0acf9SBenjamin Herrenschmidt 	int rc;
2628e8c0acf9SBenjamin Herrenschmidt 
2629e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2630e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2631e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
263226c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2633e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
263426c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
263526c5032eSBenjamin Herrenschmidt 	}
2636e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2637e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2638e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2639e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2640e8c0acf9SBenjamin Herrenschmidt 
2641e4805922SOlaf Hering 			prop[0] = 0x1;
2642e4805922SOlaf Hering 			prop[1] = 0x0;
2643e4805922SOlaf Hering 			prop[2] = rloc;
2644e4805922SOlaf Hering 			prop[3] = 0x0;
2645e4805922SOlaf Hering 			prop[4] = 0x0;
2646e4805922SOlaf Hering 			prop[5] = 0x00010000;
2647e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2648e4805922SOlaf Hering 		}
2649e4805922SOlaf Hering 	}
2650e4805922SOlaf Hering 
2651e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2652e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2653e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2654e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2655e4805922SOlaf Hering 		prop[0] = 14;
2656e4805922SOlaf Hering 		prop[1] = 0x0;
2657556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2658556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2659556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2660556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2661556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2662556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2663556ecf9bSOlaf Hering 		}
2664e4805922SOlaf Hering 	}
2665e8c0acf9SBenjamin Herrenschmidt }
2666e8c0acf9SBenjamin Herrenschmidt #else
2667e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2668e8c0acf9SBenjamin Herrenschmidt #endif
2669e8c0acf9SBenjamin Herrenschmidt 
26709b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
267154f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
267254f4ee18SHollis Blanchard {
26739b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
26749b6b563cSPaul Mackerras 	u32 u3_rev;
26759b6b563cSPaul Mackerras 	u32 interrupts[2];
26769b6b563cSPaul Mackerras 	u32 parent;
26779b6b563cSPaul Mackerras 
26789b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
26799b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
26809b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
26819b6b563cSPaul Mackerras 		return;
26829b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
26839b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
26849b6b563cSPaul Mackerras 		return;
26859b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
26869b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
26879b6b563cSPaul Mackerras 		return;
26889b6b563cSPaul Mackerras 
26899b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
26909b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
26919b6b563cSPaul Mackerras 	    == PROM_ERROR)
26929b6b563cSPaul Mackerras 		return;
26937d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
26949b6b563cSPaul Mackerras 		return;
26959b6b563cSPaul Mackerras 	/* does it need fixup ? */
26969b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
26979b6b563cSPaul Mackerras 		return;
26989b6b563cSPaul Mackerras 
26999b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
27009b6b563cSPaul Mackerras 
27019b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
27029b6b563cSPaul Mackerras 	interrupts[0] = 0;
27039b6b563cSPaul Mackerras 	interrupts[1] = 1;
2704a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2705a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
27069b6b563cSPaul Mackerras 	parent = (u32)mpic;
2707a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2708a23414beSPaul Mackerras 		     &parent, sizeof(parent));
27099b6b563cSPaul Mackerras }
271054f4ee18SHollis Blanchard #else
271154f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
271254f4ee18SHollis Blanchard #endif
27139b6b563cSPaul Mackerras 
271488fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
271594d2dde7SGrant Likely /*
271694d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
271794d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
271894d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
271994d2dde7SGrant Likely  * ethernet node.
272094d2dde7SGrant Likely  */
272194d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
272288fd2a9dSSylvain Munaut {
272388fd2a9dSSylvain Munaut 	u32 node;
272488fd2a9dSSylvain Munaut 	char prop[64];
272594d2dde7SGrant Likely 	int rv;
272688fd2a9dSSylvain Munaut 
272794d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
272894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
272988fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
273088fd2a9dSSylvain Munaut 		return;
273188fd2a9dSSylvain Munaut 
273294d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
273394d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
273494d2dde7SGrant Likely 	if (!rv)
273588fd2a9dSSylvain Munaut 		return;
273688fd2a9dSSylvain Munaut 
273794d2dde7SGrant Likely 	/*
273894d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
273994d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
274094d2dde7SGrant Likely 	 */
274188fd2a9dSSylvain Munaut 
274294d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
27436f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
27446f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
27456f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
27466f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
27476f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
27486f4347c9SOlaf Hering 			" new-device"
27496f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
27506f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
275194d2dde7SGrant Likely 				" s\" mdio\" device-name"
275294d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
27536f4347c9SOlaf Hering 				" s\" compatible\" property"
27546f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
27556f4347c9SOlaf Hering 				" 0x2 encode-int"
27566f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
27576f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
27586f4347c9SOlaf Hering 				" s\" interrupts\" property"
27596f4347c9SOlaf Hering 			" finish-device");
27606f4347c9SOlaf Hering 	};
27616f4347c9SOlaf Hering 
276294d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
276394d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
276494d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
276594d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
27666f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
27676f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
27686f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
27696f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
27706f4347c9SOlaf Hering 			" new-device"
27716f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
27726f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
27736f4347c9SOlaf Hering 				" my-self"
27746f4347c9SOlaf Hering 				" ihandle>phandle"
27756f4347c9SOlaf Hering 			" finish-device"
27766f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
27776f4347c9SOlaf Hering 				" encode-int"
27786f4347c9SOlaf Hering 				" s\" phy-handle\" property"
27796f4347c9SOlaf Hering 			" device-end");
27806f4347c9SOlaf Hering 	}
278194d2dde7SGrant Likely }
27826f4347c9SOlaf Hering 
278394d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
278494d2dde7SGrant Likely {
278594d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
278694d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
278794d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
278894d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
278994d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
279094d2dde7SGrant Likely 	u32 node;
279194d2dde7SGrant Likely 	char prop[64];
279294d2dde7SGrant Likely 	int rv, len;
279394d2dde7SGrant Likely 
279494d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
279594d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
279694d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
279794d2dde7SGrant Likely 		return;
279894d2dde7SGrant Likely 
279994d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
280094d2dde7SGrant Likely 	if (rv == PROM_ERROR)
280194d2dde7SGrant Likely 		return;
280294d2dde7SGrant Likely 	if (strcmp(prop, "EFIKA5K2"))
280394d2dde7SGrant Likely 		return;
280494d2dde7SGrant Likely 
280594d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
280694d2dde7SGrant Likely 
280794d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
280894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
280994d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
281094d2dde7SGrant Likely 	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
281194d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
281294d2dde7SGrant Likely 
28137f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
28147f4392cdSDavid Woodhouse 	   fix that too */
28157f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
28167f4392cdSDavid Woodhouse 	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
28177f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
28187f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
28197f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
28207f4392cdSDavid Woodhouse 
282194d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
282294d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
282394d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
282494d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
282594d2dde7SGrant Likely 		if (len == 12) {
282694d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
282794d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
282894d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
282994d2dde7SGrant Likely 		}
283094d2dde7SGrant Likely 	}
283194d2dde7SGrant Likely 
283294d2dde7SGrant Likely 	/* Fixup sound interrupts property */
283394d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
283494d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
283594d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
283694d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
283794d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
283894d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
283994d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
284094d2dde7SGrant Likely 		}
284194d2dde7SGrant Likely 	}
284294d2dde7SGrant Likely 
284394d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
284494d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
284588fd2a9dSSylvain Munaut }
284688fd2a9dSSylvain Munaut #else
284788fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
284888fd2a9dSSylvain Munaut #endif
284988fd2a9dSSylvain Munaut 
285050246787SDarren Stevens #ifdef CONFIG_PPC_PASEMI_NEMO
285150246787SDarren Stevens /*
285250246787SDarren Stevens  * CFE supplied on Nemo is broken in several ways, biggest
285350246787SDarren Stevens  * problem is that it reassigns ISA interrupts to unused mpic ints.
285450246787SDarren Stevens  * Add an interrupt-controller property for the io-bridge to use
285550246787SDarren Stevens  * and correct the ints so we can attach them to an irq_domain
285650246787SDarren Stevens  */
285750246787SDarren Stevens static void __init fixup_device_tree_pasemi(void)
285850246787SDarren Stevens {
285950246787SDarren Stevens 	u32 interrupts[2], parent, rval, val = 0;
286050246787SDarren Stevens 	char *name, *pci_name;
286150246787SDarren Stevens 	phandle iob, node;
286250246787SDarren Stevens 
286350246787SDarren Stevens 	/* Find the root pci node */
286450246787SDarren Stevens 	name = "/pxp@0,e0000000";
286550246787SDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
286650246787SDarren Stevens 	if (!PHANDLE_VALID(iob))
286750246787SDarren Stevens 		return;
286850246787SDarren Stevens 
286950246787SDarren Stevens 	/* check if interrupt-controller node set yet */
287050246787SDarren Stevens 	if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
287150246787SDarren Stevens 		return;
287250246787SDarren Stevens 
287350246787SDarren Stevens 	prom_printf("adding interrupt-controller property for SB600...\n");
287450246787SDarren Stevens 
287550246787SDarren Stevens 	prom_setprop(iob, name, "interrupt-controller", &val, 0);
287650246787SDarren Stevens 
287750246787SDarren Stevens 	pci_name = "/pxp@0,e0000000/pci@11";
287850246787SDarren Stevens 	node = call_prom("finddevice", 1, 1, ADDR(pci_name));
287950246787SDarren Stevens 	parent = ADDR(iob);
288050246787SDarren Stevens 
288150246787SDarren Stevens 	for( ; prom_next_node(&node); ) {
288250246787SDarren Stevens 		/* scan each node for one with an interrupt */
288350246787SDarren Stevens 		if (!PHANDLE_VALID(node))
288450246787SDarren Stevens 			continue;
288550246787SDarren Stevens 
288650246787SDarren Stevens 		rval = prom_getproplen(node, "interrupts");
288750246787SDarren Stevens 		if (rval == 0 || rval == PROM_ERROR)
288850246787SDarren Stevens 			continue;
288950246787SDarren Stevens 
289050246787SDarren Stevens 		prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
289150246787SDarren Stevens 		if ((interrupts[0] < 212) || (interrupts[0] > 222))
289250246787SDarren Stevens 			continue;
289350246787SDarren Stevens 
289450246787SDarren Stevens 		/* found a node, update both interrupts and interrupt-parent */
289550246787SDarren Stevens 		if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
289650246787SDarren Stevens 			interrupts[0] -= 203;
289750246787SDarren Stevens 		if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
289850246787SDarren Stevens 			interrupts[0] -= 213;
289950246787SDarren Stevens 		if (interrupts[0] == 221)
290050246787SDarren Stevens 			interrupts[0] = 14;
290150246787SDarren Stevens 		if (interrupts[0] == 222)
290250246787SDarren Stevens 			interrupts[0] = 8;
290350246787SDarren Stevens 
290450246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupts", interrupts,
290550246787SDarren Stevens 					sizeof(interrupts));
290650246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupt-parent", &parent,
290750246787SDarren Stevens 					sizeof(parent));
290850246787SDarren Stevens 	}
2909687e16bcSDarren Stevens 
2910687e16bcSDarren Stevens 	/*
2911687e16bcSDarren Stevens 	 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
2912687e16bcSDarren Stevens 	 * so that generic isa-bridge code can add the SB600 and its on-board
2913687e16bcSDarren Stevens 	 * peripherals.
2914687e16bcSDarren Stevens 	 */
2915687e16bcSDarren Stevens 	name = "/pxp@0,e0000000/io-bridge@0";
2916687e16bcSDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
2917687e16bcSDarren Stevens 	if (!PHANDLE_VALID(iob))
2918687e16bcSDarren Stevens 		return;
2919687e16bcSDarren Stevens 
2920687e16bcSDarren Stevens 	/* device_type is already set, just change it. */
2921687e16bcSDarren Stevens 
2922687e16bcSDarren Stevens 	prom_printf("Changing device_type of SB600 node...\n");
2923687e16bcSDarren Stevens 
2924687e16bcSDarren Stevens 	prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
292550246787SDarren Stevens }
292650246787SDarren Stevens #else	/* !CONFIG_PPC_PASEMI_NEMO */
292750246787SDarren Stevens static inline void fixup_device_tree_pasemi(void) { }
292850246787SDarren Stevens #endif
292950246787SDarren Stevens 
293054f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
293154f4ee18SHollis Blanchard {
293254f4ee18SHollis Blanchard 	fixup_device_tree_maple();
29338f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
2934e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
293554f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
293688fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
293750246787SDarren Stevens 	fixup_device_tree_pasemi();
293854f4ee18SHollis Blanchard }
29399b6b563cSPaul Mackerras 
29409b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
29419b6b563cSPaul Mackerras {
2942493adffcSBenjamin Herrenschmidt 	__be32 rval;
29439b6b563cSPaul Mackerras 	ihandle prom_cpu;
29449b6b563cSPaul Mackerras 	phandle cpu_pkg;
29459b6b563cSPaul Mackerras 
2946493adffcSBenjamin Herrenschmidt 	rval = 0;
2947493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
2948a575b807SPaul Mackerras 		return;
2949493adffcSBenjamin Herrenschmidt 	prom_cpu = be32_to_cpu(rval);
29509b6b563cSPaul Mackerras 
29519b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
29529b6b563cSPaul Mackerras 
2953af2b7fa1SDarren Stevens 	if (!PHANDLE_VALID(cpu_pkg))
2954af2b7fa1SDarren Stevens 		return;
2955af2b7fa1SDarren Stevens 
2956493adffcSBenjamin Herrenschmidt 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
2957493adffcSBenjamin Herrenschmidt 	prom.cpu = be32_to_cpu(rval);
29589b6b563cSPaul Mackerras 
2959eae5f709SMathieu Malaterre 	prom_debug("Booting CPU hw index = %d\n", prom.cpu);
29609b6b563cSPaul Mackerras }
29619b6b563cSPaul Mackerras 
29629b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
29639b6b563cSPaul Mackerras {
29649b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
29659b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
2966493adffcSBenjamin Herrenschmidt 		__be64 val;
29679b6b563cSPaul Mackerras 
29685827d416SAnton Blanchard 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
29695827d416SAnton Blanchard 		prom_initrd_end = prom_initrd_start + r4;
29709b6b563cSPaul Mackerras 
2971493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_start);
29725827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
2973a23414beSPaul Mackerras 			     &val, sizeof(val));
2974493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_end);
29755827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
2976a23414beSPaul Mackerras 			     &val, sizeof(val));
29779b6b563cSPaul Mackerras 
29785827d416SAnton Blanchard 		reserve_mem(prom_initrd_start,
29795827d416SAnton Blanchard 			    prom_initrd_end - prom_initrd_start);
29809b6b563cSPaul Mackerras 
2981eae5f709SMathieu Malaterre 		prom_debug("initrd_start=0x%lx\n", prom_initrd_start);
2982eae5f709SMathieu Malaterre 		prom_debug("initrd_end=0x%lx\n", prom_initrd_end);
29839b6b563cSPaul Mackerras 	}
29849b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
29859b6b563cSPaul Mackerras }
29869b6b563cSPaul Mackerras 
29875ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64
29885ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE
29895ac47f7aSAnton Blanchard static void reloc_toc(void)
29905ac47f7aSAnton Blanchard {
29915ac47f7aSAnton Blanchard }
29925ac47f7aSAnton Blanchard 
29935ac47f7aSAnton Blanchard static void unreloc_toc(void)
29945ac47f7aSAnton Blanchard {
29955ac47f7aSAnton Blanchard }
29965ac47f7aSAnton Blanchard #else
29971674400aSAnton Blanchard static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
29985ac47f7aSAnton Blanchard {
29995ac47f7aSAnton Blanchard 	unsigned long i;
30001674400aSAnton Blanchard 	unsigned long *toc_entry;
30011674400aSAnton Blanchard 
30021674400aSAnton Blanchard 	/* Get the start of the TOC by using r2 directly. */
30031674400aSAnton Blanchard 	asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
30045ac47f7aSAnton Blanchard 
30055ac47f7aSAnton Blanchard 	for (i = 0; i < nr_entries; i++) {
30065ac47f7aSAnton Blanchard 		*toc_entry = *toc_entry + offset;
30075ac47f7aSAnton Blanchard 		toc_entry++;
30085ac47f7aSAnton Blanchard 	}
30095ac47f7aSAnton Blanchard }
30105ac47f7aSAnton Blanchard 
30115ac47f7aSAnton Blanchard static void reloc_toc(void)
30125ac47f7aSAnton Blanchard {
30135ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
30145ac47f7aSAnton Blanchard 	unsigned long nr_entries =
30155ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
30165ac47f7aSAnton Blanchard 
30171674400aSAnton Blanchard 	__reloc_toc(offset, nr_entries);
30185ac47f7aSAnton Blanchard 
30195ac47f7aSAnton Blanchard 	mb();
30205ac47f7aSAnton Blanchard }
30215ac47f7aSAnton Blanchard 
30225ac47f7aSAnton Blanchard static void unreloc_toc(void)
30235ac47f7aSAnton Blanchard {
30245ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
30255ac47f7aSAnton Blanchard 	unsigned long nr_entries =
30265ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
30275ac47f7aSAnton Blanchard 
30285ac47f7aSAnton Blanchard 	mb();
30295ac47f7aSAnton Blanchard 
30301674400aSAnton Blanchard 	__reloc_toc(-offset, nr_entries);
30315ac47f7aSAnton Blanchard }
30325ac47f7aSAnton Blanchard #endif
30335ac47f7aSAnton Blanchard #endif
303427f44888SBenjamin Herrenschmidt 
30359b6b563cSPaul Mackerras /*
30369b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
30379b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
30389b6b563cSPaul Mackerras  */
30399b6b563cSPaul Mackerras 
30409b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
30419b6b563cSPaul Mackerras 			       unsigned long pp,
3042549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
3043549e8152SPaul Mackerras 			       unsigned long kbase)
30449b6b563cSPaul Mackerras {
30459b6b563cSPaul Mackerras 	unsigned long hdr;
30469b6b563cSPaul Mackerras 
30479b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
3048549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
30499b6b563cSPaul Mackerras 	reloc_got2(offset);
30505ac47f7aSAnton Blanchard #else
30515ac47f7aSAnton Blanchard 	reloc_toc();
30529b6b563cSPaul Mackerras #endif
30539b6b563cSPaul Mackerras 
30549b6b563cSPaul Mackerras 	/*
30559b6b563cSPaul Mackerras 	 * First zero the BSS
30569b6b563cSPaul Mackerras 	 */
30575827d416SAnton Blanchard 	memset(&__bss_start, 0, __bss_stop - __bss_start);
30589b6b563cSPaul Mackerras 
30599b6b563cSPaul Mackerras 	/*
30609b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
30619b6b563cSPaul Mackerras 	 * like /chosen
30629b6b563cSPaul Mackerras 	 */
30639b6b563cSPaul Mackerras 	prom_init_client_services(pp);
30649b6b563cSPaul Mackerras 
30659b6b563cSPaul Mackerras 	/*
3066a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
3067a23414beSPaul Mackerras 	 * and other workarounds
3068a23414beSPaul Mackerras 	 */
3069a23414beSPaul Mackerras 	prom_find_mmu();
3070a23414beSPaul Mackerras 
3071a23414beSPaul Mackerras 	/*
30729b6b563cSPaul Mackerras 	 * Init prom stdout device
30739b6b563cSPaul Mackerras 	 */
30749b6b563cSPaul Mackerras 	prom_init_stdout();
30759b6b563cSPaul Mackerras 
30765827d416SAnton Blanchard 	prom_printf("Preparing to boot %s", linux_banner);
3077e7943fbbSMichael Ellerman 
30789b6b563cSPaul Mackerras 	/*
30799b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
30809b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
30819b6b563cSPaul Mackerras 	 */
30825827d416SAnton Blanchard 	of_platform = prom_find_machine_type();
30835827d416SAnton Blanchard 	prom_printf("Detected machine type: %x\n", of_platform);
30849b6b563cSPaul Mackerras 
30850f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
3086add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
3087add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
3088add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
3089549e8152SPaul Mackerras #endif
3090add60ef3SOlaf Hering 
3091add60ef3SOlaf Hering 	/*
3092add60ef3SOlaf Hering 	 * Check for an initrd
3093add60ef3SOlaf Hering 	 */
3094add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
3095add60ef3SOlaf Hering 
309612cc9fd6SSuraj Jitindar Singh 	/*
309712cc9fd6SSuraj Jitindar Singh 	 * Do early parsing of command line
309812cc9fd6SSuraj Jitindar Singh 	 */
309912cc9fd6SSuraj Jitindar Singh 	early_cmdline_parse();
310012cc9fd6SSuraj Jitindar Singh 
310111fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
31029b6b563cSPaul Mackerras 	/*
31039b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
31049b6b563cSPaul Mackerras 	 */
31055827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES ||
31065827d416SAnton Blanchard 	    of_platform == PLATFORM_PSERIES_LPAR)
31079b6b563cSPaul Mackerras 		prom_send_capabilities();
31089b6b563cSPaul Mackerras #endif
31099b6b563cSPaul Mackerras 
31109b6b563cSPaul Mackerras 	/*
3111f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
31129b6b563cSPaul Mackerras 	 */
31135827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC)
3114549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
31159b6b563cSPaul Mackerras 
31169b6b563cSPaul Mackerras 	/*
31179b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
31189b6b563cSPaul Mackerras 	 */
31199b6b563cSPaul Mackerras 	prom_init_mem();
31209b6b563cSPaul Mackerras 
31219b6b563cSPaul Mackerras 	/*
31229b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
31239b6b563cSPaul Mackerras 	 */
31249b6b563cSPaul Mackerras 	prom_find_boot_cpu();
31259b6b563cSPaul Mackerras 
31269b6b563cSPaul Mackerras 	/*
31279b6b563cSPaul Mackerras 	 * Initialize display devices
31289b6b563cSPaul Mackerras 	 */
31299b6b563cSPaul Mackerras 	prom_check_displays();
31309b6b563cSPaul Mackerras 
3131493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
31329b6b563cSPaul Mackerras 	/*
31339b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
31349b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
31359b6b563cSPaul Mackerras 	 * available for us here...
31369b6b563cSPaul Mackerras 	 */
31375827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES)
31389b6b563cSPaul Mackerras 		prom_initialize_tce_table();
31399b6b563cSPaul Mackerras #endif
31409b6b563cSPaul Mackerras 
31419b6b563cSPaul Mackerras 	/*
314227f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
314327f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
31449b6b563cSPaul Mackerras 	 */
314511fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
31469b6b563cSPaul Mackerras 		prom_instantiate_rtas();
314727f44888SBenjamin Herrenschmidt 
31484a727429SAshley Lai #ifdef CONFIG_PPC64
31494a727429SAshley Lai 	/* instantiate sml */
31504a727429SAshley Lai 	prom_instantiate_sml();
31514a727429SAshley Lai #endif
31524a727429SAshley Lai 
315327f44888SBenjamin Herrenschmidt 	/*
315427f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
315527f44888SBenjamin Herrenschmidt 	 *
315627f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
3157dbe78b40SBenjamin Herrenschmidt 	 *
3158dbe78b40SBenjamin Herrenschmidt 	 * (This must be done after instanciating RTAS)
315927f44888SBenjamin Herrenschmidt 	 */
316011fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
316127f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
31629b6b563cSPaul Mackerras 
31639b6b563cSPaul Mackerras 	/*
31649b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
31659b6b563cSPaul Mackerras 	 */
3166493adffcSBenjamin Herrenschmidt 	if (prom_memory_limit) {
3167493adffcSBenjamin Herrenschmidt 		__be64 val = cpu_to_be64(prom_memory_limit);
31685827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3169493adffcSBenjamin Herrenschmidt 			     &val, sizeof(val));
3170493adffcSBenjamin Herrenschmidt 	}
31719b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
31725827d416SAnton Blanchard 	if (prom_iommu_off)
31735827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3174a23414beSPaul Mackerras 			     NULL, 0);
31759b6b563cSPaul Mackerras 
31765827d416SAnton Blanchard 	if (prom_iommu_force_on)
31775827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3178a23414beSPaul Mackerras 			     NULL, 0);
31799b6b563cSPaul Mackerras 
31805827d416SAnton Blanchard 	if (prom_tce_alloc_start) {
31815827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
31825827d416SAnton Blanchard 			     &prom_tce_alloc_start,
31839b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
31845827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
31855827d416SAnton Blanchard 			     &prom_tce_alloc_end,
31869b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
31879b6b563cSPaul Mackerras 	}
31889b6b563cSPaul Mackerras #endif
31899b6b563cSPaul Mackerras 
31909b6b563cSPaul Mackerras 	/*
31919b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
31929b6b563cSPaul Mackerras 	 */
31939b6b563cSPaul Mackerras 	fixup_device_tree();
31949b6b563cSPaul Mackerras 
31959b6b563cSPaul Mackerras 	/*
31969b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
31979b6b563cSPaul Mackerras 	 */
31989b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
31999b6b563cSPaul Mackerras 	flatten_device_tree();
32009b6b563cSPaul Mackerras 
32013825ac0eSPaul Mackerras 	/*
32023825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
32033825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
320411fdb309SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101).
32053825ac0eSPaul Mackerras 	 */
320611fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
32079b6b563cSPaul Mackerras 		prom_close_stdin();
32089b6b563cSPaul Mackerras 
32099b6b563cSPaul Mackerras 	/*
32109b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
32119b6b563cSPaul Mackerras 	 * devices etc...
32129b6b563cSPaul Mackerras 	 */
32137e862d7eSMichael Ellerman 	prom_printf("Quiescing Open Firmware ...\n");
32149b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
32159b6b563cSPaul Mackerras 
32169b6b563cSPaul Mackerras 	/*
32179b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
32189b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
32199b6b563cSPaul Mackerras 	 * is common to us and kexec
32209b6b563cSPaul Mackerras 	 */
32215827d416SAnton Blanchard 	hdr = dt_header_start;
322240dfef66SBenjamin Herrenschmidt 
322340dfef66SBenjamin Herrenschmidt 	/* Don't print anything after quiesce under OPAL, it crashes OFW */
32247d70c63cSBenjamin Herrenschmidt 	prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
3225eae5f709SMathieu Malaterre 	prom_debug("->dt_header_start=0x%lx\n", hdr);
32269b6b563cSPaul Mackerras 
32279b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
32289b6b563cSPaul Mackerras 	reloc_got2(-offset);
32295ac47f7aSAnton Blanchard #else
32305ac47f7aSAnton Blanchard 	unreloc_toc();
32319b6b563cSPaul Mackerras #endif
32329b6b563cSPaul Mackerras 
32336e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
32349b6b563cSPaul Mackerras 
32359b6b563cSPaul Mackerras 	return 0;
32369b6b563cSPaul Mackerras }
3237