xref: /linux/arch/powerpc/kernel/prom_init.c (revision c886087caee759790db47f345f8382d653015de3)
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 */
51e63334e5SBenjamin Herrenschmidt #define __prombss __initdata
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 
175*c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
176*c886087cSBenjamin Herrenschmidt static bool prom_radix_disable __prombss;
177*c886087cSBenjamin 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 
213dbe78b40SBenjamin Herrenschmidt static bool 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 {
528a23414beSPaul Mackerras 	static char digits[] = "0123456789abcdef";
529a23414beSPaul Mackerras 	static char result[9];
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 
670*c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
671*c886087cSBenjamin 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");
687*c886087cSBenjamin 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 
79176ffb578SMichael Ellerman struct ibm_arch_vec __cacheline_aligned ibm_architecture_vec = {
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 
929493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
930493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
93130c69ca0SBenjamin Herrenschmidt static const struct fake_elf {
9329b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
9339b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
9349b6b563cSPaul Mackerras 	struct chrpnote {
9359b6b563cSPaul Mackerras 		u32	namesz;
9369b6b563cSPaul Mackerras 		u32	descsz;
9379b6b563cSPaul Mackerras 		u32	type;
9389b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
9399b6b563cSPaul Mackerras 		struct chrpdesc {
9409b6b563cSPaul Mackerras 			u32	real_mode;
9419b6b563cSPaul Mackerras 			u32	real_base;
9429b6b563cSPaul Mackerras 			u32	real_size;
9439b6b563cSPaul Mackerras 			u32	virt_base;
9449b6b563cSPaul Mackerras 			u32	virt_size;
9459b6b563cSPaul Mackerras 			u32	load_base;
9469b6b563cSPaul Mackerras 		} chrpdesc;
9479b6b563cSPaul Mackerras 	} chrpnote;
9489b6b563cSPaul Mackerras 	struct rpanote {
9499b6b563cSPaul Mackerras 		u32	namesz;
9509b6b563cSPaul Mackerras 		u32	descsz;
9519b6b563cSPaul Mackerras 		u32	type;
9529b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
9539b6b563cSPaul Mackerras 		struct rpadesc {
9549b6b563cSPaul Mackerras 			u32	lpar_affinity;
9559b6b563cSPaul Mackerras 			u32	min_rmo_size;
9569b6b563cSPaul Mackerras 			u32	min_rmo_percent;
9579b6b563cSPaul Mackerras 			u32	max_pft_size;
9589b6b563cSPaul Mackerras 			u32	splpar;
9599b6b563cSPaul Mackerras 			u32	min_load;
9609b6b563cSPaul Mackerras 			u32	new_mem_def;
9619b6b563cSPaul Mackerras 			u32	ignore_me;
9629b6b563cSPaul Mackerras 		} rpadesc;
9639b6b563cSPaul Mackerras 	} rpanote;
9645663a123SPaul Mackerras } fake_elf = {
9659b6b563cSPaul Mackerras 	.elfhdr = {
9669b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
9679b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
9689b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
9699b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
9709b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
9719b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
9729b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
9739b6b563cSPaul Mackerras 		.e_phnum = 2
9749b6b563cSPaul Mackerras 	},
9759b6b563cSPaul Mackerras 	.phdr = {
9769b6b563cSPaul Mackerras 		[0] = {
9779b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
9789b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
9799b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
9809b6b563cSPaul Mackerras 		}, [1] = {
9819b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
9829b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
9839b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
9849b6b563cSPaul Mackerras 		}
9859b6b563cSPaul Mackerras 	},
9869b6b563cSPaul Mackerras 	.chrpnote = {
9879b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
9889b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
9899b6b563cSPaul Mackerras 		.type = 0x1275,
9909b6b563cSPaul Mackerras 		.name = "PowerPC",
9919b6b563cSPaul Mackerras 		.chrpdesc = {
9929b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
9939b6b563cSPaul Mackerras 			.real_base = ~0U,
9949b6b563cSPaul Mackerras 			.real_size = ~0U,
9959b6b563cSPaul Mackerras 			.virt_base = ~0U,
9969b6b563cSPaul Mackerras 			.virt_size = ~0U,
9979b6b563cSPaul Mackerras 			.load_base = ~0U
9989b6b563cSPaul Mackerras 		},
9999b6b563cSPaul Mackerras 	},
10009b6b563cSPaul Mackerras 	.rpanote = {
10019b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
10029b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
10039b6b563cSPaul Mackerras 		.type = 0x12759999,
10049b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
10059b6b563cSPaul Mackerras 		.rpadesc = {
10065663a123SPaul Mackerras 			.lpar_affinity = 0,
10075663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
10089b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
10095663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
10109b6b563cSPaul Mackerras 			.splpar = 1,
10119b6b563cSPaul Mackerras 			.min_load = ~0U,
10125663a123SPaul Mackerras 			.new_mem_def = 0
10139b6b563cSPaul Mackerras 		}
10149b6b563cSPaul Mackerras 	}
10159b6b563cSPaul Mackerras };
1016493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
10179b6b563cSPaul Mackerras 
1018efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
1019efec959fSBenjamin Herrenschmidt {
1020efec959fSBenjamin Herrenschmidt 	phandle node;
1021efec959fSBenjamin Herrenschmidt 	char type[64];
1022efec959fSBenjamin Herrenschmidt 	unsigned int plen;
1023efec959fSBenjamin Herrenschmidt 
1024efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
1025efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
1026efec959fSBenjamin Herrenschmidt 		type[0] = 0;
1027efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
1028efec959fSBenjamin Herrenschmidt 
10295827d416SAnton Blanchard 		if (strcmp(type, "cpu"))
1030efec959fSBenjamin Herrenschmidt 			continue;
1031efec959fSBenjamin Herrenschmidt 		/*
1032efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
1033efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
1034efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
1035efec959fSBenjamin Herrenschmidt 		 */
1036efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
1037efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
1038efec959fSBenjamin Herrenschmidt 			break;
1039efec959fSBenjamin Herrenschmidt 		plen >>= 2;
10402c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
1041efec959fSBenjamin Herrenschmidt 
1042efec959fSBenjamin Herrenschmidt 		/* Sanity check */
1043efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
10442c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
1045efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
1046efec959fSBenjamin Herrenschmidt 			return 1;
1047efec959fSBenjamin Herrenschmidt 		}
1048efec959fSBenjamin Herrenschmidt 		return plen;
1049efec959fSBenjamin Herrenschmidt 	}
1050efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
1051efec959fSBenjamin Herrenschmidt 
1052efec959fSBenjamin Herrenschmidt 	return 1;
1053efec959fSBenjamin Herrenschmidt 
1054efec959fSBenjamin Herrenschmidt }
1055efec959fSBenjamin Herrenschmidt 
1056014d02cbSSuraj Jitindar Singh static void __init prom_parse_mmu_model(u8 val,
1057014d02cbSSuraj Jitindar Singh 					struct platform_support *support)
1058014d02cbSSuraj Jitindar Singh {
1059014d02cbSSuraj Jitindar Singh 	switch (val) {
1060014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_DYNAMIC):
1061014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_EITHER): /* Either Available */
1062014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - either supported\n");
1063014d02cbSSuraj Jitindar Singh 		support->radix_mmu = !prom_radix_disable;
1064014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1065014d02cbSSuraj Jitindar Singh 		break;
1066014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */
1067014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - radix only\n");
1068014d02cbSSuraj Jitindar Singh 		if (prom_radix_disable) {
1069014d02cbSSuraj Jitindar Singh 			/*
1070014d02cbSSuraj Jitindar Singh 			 * If we __have__ to do radix, we're better off ignoring
1071014d02cbSSuraj Jitindar Singh 			 * the command line rather than not booting.
1072014d02cbSSuraj Jitindar Singh 			 */
1073014d02cbSSuraj Jitindar Singh 			prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
1074014d02cbSSuraj Jitindar Singh 		}
1075014d02cbSSuraj Jitindar Singh 		support->radix_mmu = true;
1076014d02cbSSuraj Jitindar Singh 		break;
1077014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_HASH):
1078014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - hash only\n");
1079014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1080014d02cbSSuraj Jitindar Singh 		break;
1081014d02cbSSuraj Jitindar Singh 	default:
1082014d02cbSSuraj Jitindar Singh 		prom_debug("Unknown mmu support option: 0x%x\n", val);
1083014d02cbSSuraj Jitindar Singh 		break;
1084014d02cbSSuraj Jitindar Singh 	}
1085014d02cbSSuraj Jitindar Singh }
1086014d02cbSSuraj Jitindar Singh 
1087ac5e5a54SCédric Le Goater static void __init prom_parse_xive_model(u8 val,
1088ac5e5a54SCédric Le Goater 					 struct platform_support *support)
1089ac5e5a54SCédric Le Goater {
1090ac5e5a54SCédric Le Goater 	switch (val) {
1091ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EITHER): /* Either Available */
1092ac5e5a54SCédric Le Goater 		prom_debug("XIVE - either mode supported\n");
1093ac5e5a54SCédric Le Goater 		support->xive = true;
1094ac5e5a54SCédric Le Goater 		break;
1095ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EXPLOIT): /* Only Exploitation mode */
1096ac5e5a54SCédric Le Goater 		prom_debug("XIVE - exploitation mode supported\n");
1097ac5e5a54SCédric Le Goater 		support->xive = true;
1098ac5e5a54SCédric Le Goater 		break;
1099ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_LEGACY): /* Only Legacy mode */
1100ac5e5a54SCédric Le Goater 		prom_debug("XIVE - legacy mode supported\n");
1101ac5e5a54SCédric Le Goater 		break;
1102ac5e5a54SCédric Le Goater 	default:
1103ac5e5a54SCédric Le Goater 		prom_debug("Unknown xive support option: 0x%x\n", val);
1104ac5e5a54SCédric Le Goater 		break;
1105ac5e5a54SCédric Le Goater 	}
1106ac5e5a54SCédric Le Goater }
1107ac5e5a54SCédric Le Goater 
1108014d02cbSSuraj Jitindar Singh static void __init prom_parse_platform_support(u8 index, u8 val,
1109014d02cbSSuraj Jitindar Singh 					       struct platform_support *support)
1110014d02cbSSuraj Jitindar Singh {
1111014d02cbSSuraj Jitindar Singh 	switch (index) {
1112014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_MMU_SUPPORT): /* MMU Model */
1113014d02cbSSuraj Jitindar Singh 		prom_parse_mmu_model(val & OV5_FEAT(OV5_MMU_SUPPORT), support);
1114014d02cbSSuraj Jitindar Singh 		break;
1115014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_RADIX_GTSE): /* Radix Extensions */
1116014d02cbSSuraj Jitindar Singh 		if (val & OV5_FEAT(OV5_RADIX_GTSE)) {
1117014d02cbSSuraj Jitindar Singh 			prom_debug("Radix - GTSE supported\n");
1118014d02cbSSuraj Jitindar Singh 			support->radix_gtse = true;
1119014d02cbSSuraj Jitindar Singh 		}
1120014d02cbSSuraj Jitindar Singh 		break;
1121ac5e5a54SCédric Le Goater 	case OV5_INDX(OV5_XIVE_SUPPORT): /* Interrupt mode */
1122ac5e5a54SCédric Le Goater 		prom_parse_xive_model(val & OV5_FEAT(OV5_XIVE_SUPPORT),
1123ac5e5a54SCédric Le Goater 				      support);
1124ac5e5a54SCédric Le Goater 		break;
1125014d02cbSSuraj Jitindar Singh 	}
1126014d02cbSSuraj Jitindar Singh }
1127014d02cbSSuraj Jitindar Singh 
1128014d02cbSSuraj Jitindar Singh static void __init prom_check_platform_support(void)
1129014d02cbSSuraj Jitindar Singh {
1130014d02cbSSuraj Jitindar Singh 	struct platform_support supported = {
1131014d02cbSSuraj Jitindar Singh 		.hash_mmu = false,
1132014d02cbSSuraj Jitindar Singh 		.radix_mmu = false,
1133ac5e5a54SCédric Le Goater 		.radix_gtse = false,
1134ac5e5a54SCédric Le Goater 		.xive = false
1135014d02cbSSuraj Jitindar Singh 	};
1136014d02cbSSuraj Jitindar Singh 	int prop_len = prom_getproplen(prom.chosen,
1137014d02cbSSuraj Jitindar Singh 				       "ibm,arch-vec-5-platform-support");
1138014d02cbSSuraj Jitindar Singh 	if (prop_len > 1) {
1139014d02cbSSuraj Jitindar Singh 		int i;
1140ab912399SSuraj Jitindar Singh 		u8 vec[8];
1141014d02cbSSuraj Jitindar Singh 		prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1142014d02cbSSuraj Jitindar Singh 			   prop_len);
1143ab912399SSuraj Jitindar Singh 		if (prop_len > sizeof(vec))
1144ab912399SSuraj Jitindar Singh 			prom_printf("WARNING: ibm,arch-vec-5-platform-support longer than expected (len: %d)\n",
1145ab912399SSuraj Jitindar Singh 				    prop_len);
1146014d02cbSSuraj Jitindar Singh 		prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support",
1147014d02cbSSuraj Jitindar Singh 			     &vec, sizeof(vec));
1148ab912399SSuraj Jitindar Singh 		for (i = 0; i < sizeof(vec); i += 2) {
1149014d02cbSSuraj Jitindar Singh 			prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2
1150014d02cbSSuraj Jitindar Singh 								  , vec[i]
1151014d02cbSSuraj Jitindar Singh 								  , vec[i + 1]);
1152014d02cbSSuraj Jitindar Singh 			prom_parse_platform_support(vec[i], vec[i + 1],
1153014d02cbSSuraj Jitindar Singh 						    &supported);
1154014d02cbSSuraj Jitindar Singh 		}
1155014d02cbSSuraj Jitindar Singh 	}
1156014d02cbSSuraj Jitindar Singh 
115779b46868SAlexey Kardashevskiy 	if (supported.radix_mmu && supported.radix_gtse &&
115879b46868SAlexey Kardashevskiy 	    IS_ENABLED(CONFIG_PPC_RADIX_MMU)) {
1159014d02cbSSuraj Jitindar Singh 		/* Radix preferred - but we require GTSE for now */
1160014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for radix with GTSE\n");
1161014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1162014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.radix_ext = OV5_FEAT(OV5_RADIX_GTSE);
1163014d02cbSSuraj Jitindar Singh 	} else if (supported.hash_mmu) {
1164014d02cbSSuraj Jitindar Singh 		/* Default to hash mmu (if we can) */
1165014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for hash\n");
1166014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1167014d02cbSSuraj Jitindar Singh 	} else {
1168014d02cbSSuraj Jitindar Singh 		/* We're probably on a legacy hypervisor */
1169014d02cbSSuraj Jitindar Singh 		prom_debug("Assuming legacy hash support\n");
1170014d02cbSSuraj Jitindar Singh 	}
1171ac5e5a54SCédric Le Goater 
1172ac5e5a54SCédric Le Goater 	if (supported.xive) {
1173ac5e5a54SCédric Le Goater 		prom_debug("Asking for XIVE\n");
1174ac5e5a54SCédric Le Goater 		ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1175ac5e5a54SCédric Le Goater 	}
1176014d02cbSSuraj Jitindar Singh }
1177efec959fSBenjamin Herrenschmidt 
11789b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
11799b6b563cSPaul Mackerras {
1180493adffcSBenjamin Herrenschmidt 	ihandle root;
1181f709bfacSPaul Mackerras 	prom_arg_t ret;
1182dbd0c5d5SLaurent Dufour 	u32 cores;
11839b6b563cSPaul Mackerras 
1184014d02cbSSuraj Jitindar Singh 	/* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1185014d02cbSSuraj Jitindar Singh 	prom_check_platform_support();
1186014d02cbSSuraj Jitindar Singh 
1187f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
1188f709bfacSPaul Mackerras 	if (root != 0) {
1189efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
1190efec959fSBenjamin Herrenschmidt 		 *
1191efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
1192efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
1193efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
1194efec959fSBenjamin Herrenschmidt 		 */
1195dbd0c5d5SLaurent Dufour 
1196dbd0c5d5SLaurent Dufour 		cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1197eae5f709SMathieu Malaterre 		prom_printf("Max number of cores passed to firmware: %u (NR_CPUS = %d)\n",
1198dbd0c5d5SLaurent Dufour 			    cores, NR_CPUS);
119976ffb578SMichael Ellerman 
120076ffb578SMichael Ellerman 		ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1201efec959fSBenjamin Herrenschmidt 
1202f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
1203049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
1204f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
1205f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
120633b74977SBenjamin Herrenschmidt 				  root,
120776ffb578SMichael Ellerman 				  ADDR(&ibm_architecture_vec)) == 0) {
1208f709bfacSPaul Mackerras 			/* the call exists... */
1209f709bfacSPaul Mackerras 			if (ret)
12104da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
1211f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
1212f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
12134da727aeSAnton Blanchard 			prom_printf(" done\n");
1214f709bfacSPaul Mackerras 			return;
1215f709bfacSPaul Mackerras 		}
1216f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
1217049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
1218f709bfacSPaul Mackerras 	}
1219f709bfacSPaul Mackerras 
1220493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
1221493adffcSBenjamin Herrenschmidt 	{
1222493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
1223493adffcSBenjamin Herrenschmidt 
1224f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
1225493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
1226493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
12279b6b563cSPaul Mackerras 		if (elfloader == 0) {
12289b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
12299b6b563cSPaul Mackerras 			return;
12309b6b563cSPaul Mackerras 		}
12319b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
12329b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
12339b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
12349b6b563cSPaul Mackerras 	}
1235493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1236493adffcSBenjamin Herrenschmidt }
123711fdb309SBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
12389b6b563cSPaul Mackerras 
12399b6b563cSPaul Mackerras /*
12409b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
12419b6b563cSPaul Mackerras  *
12429b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
12439b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
12449b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
12459b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
12469b6b563cSPaul Mackerras  *  never happen.
12479b6b563cSPaul Mackerras  *
12489b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
12499b6b563cSPaul Mackerras  *  TCEs overlap
12509b6b563cSPaul Mackerras  *
12519b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
12529b6b563cSPaul Mackerras  *
12539b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
12549b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
12559b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
12569b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
12579b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
12589b6b563cSPaul Mackerras  *
12599b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
12609b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
12619b6b563cSPaul Mackerras  *  reserved.
12629b6b563cSPaul Mackerras  */
12639b6b563cSPaul Mackerras 
12649b6b563cSPaul Mackerras 
12659b6b563cSPaul Mackerras /*
12669b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
12679b6b563cSPaul Mackerras  *
12689b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
12699b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
12709b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
12719b6b563cSPaul Mackerras  */
12729b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
12739b6b563cSPaul Mackerras {
12745827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
12759b6b563cSPaul Mackerras 	unsigned long addr = 0;
12769b6b563cSPaul Mackerras 
1277c4988820SPaul Mackerras 	if (align)
1278c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
1279eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx)\n", __func__, size, align);
12805827d416SAnton Blanchard 	if (ram_top == 0)
12819b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
12829b6b563cSPaul Mackerras 
12839b6b563cSPaul Mackerras 	if (align)
12845827d416SAnton Blanchard 		base = _ALIGN_UP(alloc_bottom, align);
12859b6b563cSPaul Mackerras 	else
12865827d416SAnton Blanchard 		base = alloc_bottom;
12879b6b563cSPaul Mackerras 
12885827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
12899b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
1290eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
12919b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1292c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
12939b6b563cSPaul Mackerras 			break;
12949b6b563cSPaul Mackerras 		addr = 0;
12959b6b563cSPaul Mackerras 		if (align == 0)
12969b6b563cSPaul Mackerras 			break;
12979b6b563cSPaul Mackerras 	}
12989b6b563cSPaul Mackerras 	if (addr == 0)
12999b6b563cSPaul Mackerras 		return 0;
13005827d416SAnton Blanchard 	alloc_bottom = addr + size;
13019b6b563cSPaul Mackerras 
1302eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1303eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1304eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1305eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1306eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1307eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
13089b6b563cSPaul Mackerras 
13099b6b563cSPaul Mackerras 	return addr;
13109b6b563cSPaul Mackerras }
13119b6b563cSPaul Mackerras 
13129b6b563cSPaul Mackerras /*
13139b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
13149b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
13159b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
13169b6b563cSPaul Mackerras  */
13179b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
13189b6b563cSPaul Mackerras 				       int highmem)
13199b6b563cSPaul Mackerras {
13209b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
13219b6b563cSPaul Mackerras 
1322eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx, %s)\n", __func__, size, align,
13235827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
13245827d416SAnton Blanchard 	if (ram_top == 0)
13259b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
13269b6b563cSPaul Mackerras 
13279b6b563cSPaul Mackerras 	if (highmem) {
13289b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
13295827d416SAnton Blanchard 		addr = _ALIGN_DOWN(alloc_top_high - size, align);
13305827d416SAnton Blanchard 		if (addr <= alloc_bottom)
13319b6b563cSPaul Mackerras 			return 0;
13329b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
13339b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
13349b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
13359b6b563cSPaul Mackerras 		 */
13365827d416SAnton Blanchard 		if (addr < rmo_top) {
13379b6b563cSPaul Mackerras 			/* Good, we are first */
13385827d416SAnton Blanchard 			if (alloc_top == rmo_top)
13395827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
13409b6b563cSPaul Mackerras 			else
13419b6b563cSPaul Mackerras 				return 0;
13429b6b563cSPaul Mackerras 		}
13435827d416SAnton Blanchard 		alloc_top_high = addr;
13449b6b563cSPaul Mackerras 		goto bail;
13459b6b563cSPaul Mackerras 	}
13469b6b563cSPaul Mackerras 
13475827d416SAnton Blanchard 	base = _ALIGN_DOWN(alloc_top - size, align);
13485827d416SAnton Blanchard 	for (; base > alloc_bottom;
13499b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
1350eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
13519b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1352c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
13539b6b563cSPaul Mackerras 			break;
13549b6b563cSPaul Mackerras 		addr = 0;
13559b6b563cSPaul Mackerras 	}
13569b6b563cSPaul Mackerras 	if (addr == 0)
13579b6b563cSPaul Mackerras 		return 0;
13585827d416SAnton Blanchard 	alloc_top = addr;
13599b6b563cSPaul Mackerras 
13609b6b563cSPaul Mackerras  bail:
1361eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1362eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1363eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1364eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1365eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1366eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
13679b6b563cSPaul Mackerras 
13689b6b563cSPaul Mackerras 	return addr;
13699b6b563cSPaul Mackerras }
13709b6b563cSPaul Mackerras 
13719b6b563cSPaul Mackerras /*
13729b6b563cSPaul Mackerras  * Parse a "reg" cell
13739b6b563cSPaul Mackerras  */
13749b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
13759b6b563cSPaul Mackerras {
13769b6b563cSPaul Mackerras 	cell_t *p = *cellp;
13779b6b563cSPaul Mackerras 	unsigned long r = 0;
13789b6b563cSPaul Mackerras 
13799b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
13809b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
13819b6b563cSPaul Mackerras 		p++;
13829b6b563cSPaul Mackerras 		s--;
13839b6b563cSPaul Mackerras 	}
1384493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
13859b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
138635499c01SPaul Mackerras 	if (s > 1) {
13879b6b563cSPaul Mackerras 		r <<= 32;
1388493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
13899b6b563cSPaul Mackerras 	}
13909b6b563cSPaul Mackerras #endif
13919b6b563cSPaul Mackerras 	*cellp = p;
13929b6b563cSPaul Mackerras 	return r;
13939b6b563cSPaul Mackerras }
13949b6b563cSPaul Mackerras 
13959b6b563cSPaul Mackerras /*
13969b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
13979b6b563cSPaul Mackerras  * we don't need anything smarter at this point
13989b6b563cSPaul Mackerras  *
13999b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
14009b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
14019b6b563cSPaul Mackerras  * them down.
14029b6b563cSPaul Mackerras  */
14030108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
14049b6b563cSPaul Mackerras {
1405cbbcf340SKumar Gala 	u64 top = base + size;
14065827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
14079b6b563cSPaul Mackerras 
14089b6b563cSPaul Mackerras 	if (size == 0)
14099b6b563cSPaul Mackerras 		return;
14109b6b563cSPaul Mackerras 
14119b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
14129b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
14139b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
14149b6b563cSPaul Mackerras 	 */
14159b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
14169b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
14179b6b563cSPaul Mackerras 	size = top - base;
14189b6b563cSPaul Mackerras 
14199b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
14209b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1421493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1422493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
14235827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
14249b6b563cSPaul Mackerras }
14259b6b563cSPaul Mackerras 
14269b6b563cSPaul Mackerras /*
1427b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
14289b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
14299b6b563cSPaul Mackerras  */
14309b6b563cSPaul Mackerras static void __init prom_init_mem(void)
14319b6b563cSPaul Mackerras {
14329b6b563cSPaul Mackerras 	phandle node;
1433eab00a20SMathieu Malaterre #ifdef DEBUG_PROM
1434eab00a20SMathieu Malaterre 	char *path;
1435eab00a20SMathieu Malaterre #endif
1436eab00a20SMathieu Malaterre 	char type[64];
14379b6b563cSPaul Mackerras 	unsigned int plen;
14389b6b563cSPaul Mackerras 	cell_t *p, *endp;
1439493adffcSBenjamin Herrenschmidt 	__be32 val;
14409b6b563cSPaul Mackerras 	u32 rac, rsc;
14419b6b563cSPaul Mackerras 
14429b6b563cSPaul Mackerras 	/*
14439b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
14449b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
14459b6b563cSPaul Mackerras 	 * 2) top of memory
14469b6b563cSPaul Mackerras 	 */
1447493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1448493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1449493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1450493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1451493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1452493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1453493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1454493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
14559b6b563cSPaul Mackerras 
14569b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
1457eab00a20SMathieu Malaterre #ifdef DEBUG_PROM
14585827d416SAnton Blanchard 	path = prom_scratch;
1459eab00a20SMathieu Malaterre #endif
14609b6b563cSPaul Mackerras 
14619b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
14629b6b563cSPaul Mackerras 		type[0] = 0;
14639b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
14649b6b563cSPaul Mackerras 
1465c4988820SPaul Mackerras 		if (type[0] == 0) {
1466c4988820SPaul Mackerras 			/*
1467c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1468c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1469c4988820SPaul Mackerras 			 */
1470c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1471c4988820SPaul Mackerras 		}
14725827d416SAnton Blanchard 		if (strcmp(type, "memory"))
14739b6b563cSPaul Mackerras 			continue;
14749b6b563cSPaul Mackerras 
14755827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
14769b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
14779b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
14789b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
14799b6b563cSPaul Mackerras 		}
14805827d416SAnton Blanchard 		p = regbuf;
14819b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
14829b6b563cSPaul Mackerras 
14839b6b563cSPaul Mackerras #ifdef DEBUG_PROM
14849b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
14859b6b563cSPaul Mackerras 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
14869b6b563cSPaul Mackerras 		prom_debug("  node %s :\n", path);
14879b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
14889b6b563cSPaul Mackerras 
14899b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
14909b6b563cSPaul Mackerras 			unsigned long base, size;
14919b6b563cSPaul Mackerras 
14929b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
14939b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
14949b6b563cSPaul Mackerras 
14959b6b563cSPaul Mackerras 			if (size == 0)
14969b6b563cSPaul Mackerras 				continue;
1497eae5f709SMathieu Malaterre 			prom_debug("    %lx %lx\n", base, size);
14985827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
14995827d416SAnton Blanchard 				rmo_top = size;
15005827d416SAnton Blanchard 			if ((base + size) > ram_top)
15015827d416SAnton Blanchard 				ram_top = base + size;
15029b6b563cSPaul Mackerras 		}
15039b6b563cSPaul Mackerras 	}
15049b6b563cSPaul Mackerras 
15055827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
15069b6b563cSPaul Mackerras 
15079b6b563cSPaul Mackerras 	/*
1508cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1509cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1510cf68787bSBenjamin Krill 	 * TCE's up there.
1511cf68787bSBenjamin Krill 	 */
1512cf68787bSBenjamin Krill 
15135827d416SAnton Blanchard 	alloc_top_high = ram_top;
1514cf68787bSBenjamin Krill 
15155827d416SAnton Blanchard 	if (prom_memory_limit) {
15165827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1517eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx <= alloc_bottom.\n",
15185827d416SAnton Blanchard 				    prom_memory_limit);
15195827d416SAnton Blanchard 			prom_memory_limit = 0;
15205827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1521eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx >= ram_top.\n",
15225827d416SAnton Blanchard 				    prom_memory_limit);
15235827d416SAnton Blanchard 			prom_memory_limit = 0;
1524cf68787bSBenjamin Krill 		} else {
15255827d416SAnton Blanchard 			ram_top = prom_memory_limit;
15265827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1527cf68787bSBenjamin Krill 		}
1528cf68787bSBenjamin Krill 	}
1529cf68787bSBenjamin Krill 
1530cf68787bSBenjamin Krill 	/*
15319b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
15329b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
15339b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
15349b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
15359b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
15369b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
15379b6b563cSPaul Mackerras 	 */
15385827d416SAnton Blanchard 	if (!rmo_top)
15395827d416SAnton Blanchard 		rmo_top = ram_top;
15405827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
15415827d416SAnton Blanchard 	alloc_top = rmo_top;
15425827d416SAnton Blanchard 	alloc_top_high = ram_top;
15439b6b563cSPaul Mackerras 
154464968f60SPaul Mackerras 	/*
154564968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
154664968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
154764968f60SPaul Mackerras 	 */
15485827d416SAnton Blanchard 	if (prom_initrd_start &&
15495827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
15505827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
15515827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
155264968f60SPaul Mackerras 
15539b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
1554eae5f709SMathieu Malaterre 	prom_printf("  memory_limit : %lx (16 MB aligned)\n",
1555eae5f709SMathieu Malaterre 		    prom_memory_limit);
1556eae5f709SMathieu Malaterre 	prom_printf("  alloc_bottom : %lx\n", alloc_bottom);
1557eae5f709SMathieu Malaterre 	prom_printf("  alloc_top    : %lx\n", alloc_top);
1558eae5f709SMathieu Malaterre 	prom_printf("  alloc_top_hi : %lx\n", alloc_top_high);
1559eae5f709SMathieu Malaterre 	prom_printf("  rmo_top      : %lx\n", rmo_top);
1560eae5f709SMathieu Malaterre 	prom_printf("  ram_top      : %lx\n", ram_top);
15619b6b563cSPaul Mackerras }
15629b6b563cSPaul Mackerras 
156327f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
156427f44888SBenjamin Herrenschmidt {
1565493adffcSBenjamin Herrenschmidt 	__be32 val;
1566493adffcSBenjamin Herrenschmidt 	ihandle stdin;
156727f44888SBenjamin Herrenschmidt 
1568493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1569493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1570493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1571493adffcSBenjamin Herrenschmidt 	}
157227f44888SBenjamin Herrenschmidt }
157327f44888SBenjamin Herrenschmidt 
15749b6b563cSPaul Mackerras /*
15759b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
15769b6b563cSPaul Mackerras  */
15779b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
15789b6b563cSPaul Mackerras {
15799b6b563cSPaul Mackerras 	phandle rtas_node;
15809b6b563cSPaul Mackerras 	ihandle rtas_inst;
15819b6b563cSPaul Mackerras 	u32 base, entry = 0;
1582493adffcSBenjamin Herrenschmidt 	__be32 val;
15839b6b563cSPaul Mackerras 	u32 size = 0;
15849b6b563cSPaul Mackerras 
15859b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
15869b6b563cSPaul Mackerras 
15879b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
15889b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
15899b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
15909b6b563cSPaul Mackerras 		return;
15919b6b563cSPaul Mackerras 
1592493adffcSBenjamin Herrenschmidt 	val = 0;
1593493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1594493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
15959b6b563cSPaul Mackerras 	if (size == 0)
15969b6b563cSPaul Mackerras 		return;
15979b6b563cSPaul Mackerras 
15989b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
15996d1e2c6cSAnton Blanchard 	if (base == 0)
16006d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
16019b6b563cSPaul Mackerras 
16029b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
16039b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1604a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
16059b6b563cSPaul Mackerras 		return;
16069b6b563cSPaul Mackerras 	}
16079b6b563cSPaul Mackerras 
16089b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
16099b6b563cSPaul Mackerras 
16109b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
16119b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1612a23414beSPaul Mackerras 			  rtas_inst, base) != 0
16139b6b563cSPaul Mackerras 	    || entry == 0) {
16149b6b563cSPaul Mackerras 		prom_printf(" failed\n");
16159b6b563cSPaul Mackerras 		return;
16169b6b563cSPaul Mackerras 	}
16179b6b563cSPaul Mackerras 	prom_printf(" done\n");
16189b6b563cSPaul Mackerras 
16199b6b563cSPaul Mackerras 	reserve_mem(base, size);
16209b6b563cSPaul Mackerras 
1621493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1622a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1623493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1624493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1625a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1626493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
16279b6b563cSPaul Mackerras 
1628dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1629dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1630dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1631dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1632dbe78b40SBenjamin Herrenschmidt 
16339b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
16349b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
1635eae5f709SMathieu Malaterre 	prom_debug("rtas size     = 0x%x\n", size);
16369b6b563cSPaul Mackerras 
16379b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
16389b6b563cSPaul Mackerras }
16399b6b563cSPaul Mackerras 
16409b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
16419b6b563cSPaul Mackerras /*
16424a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
16434a727429SAshley Lai  */
16444a727429SAshley Lai static void __init prom_instantiate_sml(void)
16454a727429SAshley Lai {
16464a727429SAshley Lai 	phandle ibmvtpm_node;
16474a727429SAshley Lai 	ihandle ibmvtpm_inst;
1648b4ed0469SHon Ching \(Vicky\) Lo 	u32 entry = 0, size = 0, succ = 0;
16494a727429SAshley Lai 	u64 base;
1650b4ed0469SHon Ching \(Vicky\) Lo 	__be32 val;
16514a727429SAshley Lai 
16524a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
16534a727429SAshley Lai 
16542f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
16554a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
16564a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
16574a727429SAshley Lai 		return;
16584a727429SAshley Lai 
16592f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
16604a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
16614a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
16624a727429SAshley Lai 		return;
16634a727429SAshley Lai 	}
16644a727429SAshley Lai 
1665b4ed0469SHon Ching \(Vicky\) Lo 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1666b4ed0469SHon Ching \(Vicky\) Lo 			 &val, sizeof(val)) != PROM_ERROR) {
1667b4ed0469SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &succ,
1668b4ed0469SHon Ching \(Vicky\) Lo 				  ADDR("reformat-sml-to-efi-alignment"),
1669b4ed0469SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || succ == 0) {
1670b4ed0469SHon Ching \(Vicky\) Lo 			prom_printf("Reformat SML to EFI alignment failed\n");
1671b4ed0469SHon Ching \(Vicky\) Lo 			return;
1672b4ed0469SHon Ching \(Vicky\) Lo 		}
1673b4ed0469SHon Ching \(Vicky\) Lo 
16744a727429SAshley Lai 		if (call_prom_ret("call-method", 2, 2, &size,
16759e5d4af4SHon Ching \(Vicky\) Lo 				  ADDR("sml-get-allocated-size"),
16769e5d4af4SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || size == 0) {
16779e5d4af4SHon Ching \(Vicky\) Lo 			prom_printf("SML get allocated size failed\n");
16789e5d4af4SHon Ching \(Vicky\) Lo 			return;
16799e5d4af4SHon Ching \(Vicky\) Lo 		}
16809e5d4af4SHon Ching \(Vicky\) Lo 	} else {
16819e5d4af4SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &size,
16824a727429SAshley Lai 				  ADDR("sml-get-handover-size"),
16834a727429SAshley Lai 				  ibmvtpm_inst) != 0 || size == 0) {
16844a727429SAshley Lai 			prom_printf("SML get handover size failed\n");
16854a727429SAshley Lai 			return;
16864a727429SAshley Lai 		}
16879e5d4af4SHon Ching \(Vicky\) Lo 	}
16884a727429SAshley Lai 
16894a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
16904a727429SAshley Lai 	if (base == 0)
16914a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
16924a727429SAshley Lai 
1693eae5f709SMathieu Malaterre 	prom_printf("instantiating sml at 0x%llx...", base);
16944a727429SAshley Lai 
16959e5d4af4SHon Ching \(Vicky\) Lo 	memset((void *)base, 0, size);
16969e5d4af4SHon Ching \(Vicky\) Lo 
16974a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
16984a727429SAshley Lai 			  ADDR("sml-handover"),
16994a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
17004a727429SAshley Lai 		prom_printf("SML handover failed\n");
17014a727429SAshley Lai 		return;
17024a727429SAshley Lai 	}
17034a727429SAshley Lai 	prom_printf(" done\n");
17044a727429SAshley Lai 
17054a727429SAshley Lai 	reserve_mem(base, size);
17064a727429SAshley Lai 
17072f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
17084a727429SAshley Lai 		     &base, sizeof(base));
17092f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
17104a727429SAshley Lai 		     &size, sizeof(size));
17114a727429SAshley Lai 
1712eae5f709SMathieu Malaterre 	prom_debug("sml base     = 0x%llx\n", base);
1713eae5f709SMathieu Malaterre 	prom_debug("sml size     = 0x%x\n", size);
17144a727429SAshley Lai 
17154a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
17164a727429SAshley Lai }
17174a727429SAshley Lai 
17184a727429SAshley Lai /*
17199b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
17209b6b563cSPaul Mackerras  */
1721493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
17229b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
17239b6b563cSPaul Mackerras {
17249b6b563cSPaul Mackerras 	phandle node;
17259b6b563cSPaul Mackerras 	ihandle phb_node;
17269b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
17275827d416SAnton Blanchard 	char *path = prom_scratch;
17289b6b563cSPaul Mackerras 	u64 base, align;
17299b6b563cSPaul Mackerras 	u32 minalign, minsize;
17309b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
17319b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
17329b6b563cSPaul Mackerras 	u64 i;
17339b6b563cSPaul Mackerras 
17345827d416SAnton Blanchard 	if (prom_iommu_off)
17359b6b563cSPaul Mackerras 		return;
17369b6b563cSPaul Mackerras 
17379b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
17389b6b563cSPaul Mackerras 
17399b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
17405827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
17419b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
17429b6b563cSPaul Mackerras 
17439b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
17449b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
17459b6b563cSPaul Mackerras 		compatible[0] = 0;
17469b6b563cSPaul Mackerras 		type[0] = 0;
17479b6b563cSPaul Mackerras 		model[0] = 0;
17489b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
17499b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
17509b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
17519b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
17529b6b563cSPaul Mackerras 
17535827d416SAnton Blanchard 		if ((type[0] == 0) || (strstr(type, "pci") == NULL))
17549b6b563cSPaul Mackerras 			continue;
17559b6b563cSPaul Mackerras 
1756e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
17579b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
17585827d416SAnton Blanchard 			if ((strstr(compatible, "python") == NULL) &&
17595827d416SAnton Blanchard 			    (strstr(compatible, "Speedwagon") == NULL) &&
17605827d416SAnton Blanchard 			    (strstr(compatible, "Winnipeg") == NULL))
17619b6b563cSPaul Mackerras 				continue;
17629b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
17635827d416SAnton Blanchard 			if ((strstr(model, "ython") == NULL) &&
17645827d416SAnton Blanchard 			    (strstr(model, "peedwagon") == NULL) &&
17655827d416SAnton Blanchard 			    (strstr(model, "innipeg") == NULL))
17669b6b563cSPaul Mackerras 				continue;
17679b6b563cSPaul Mackerras 		}
17689b6b563cSPaul Mackerras 
17699b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
17709b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
17719b6b563cSPaul Mackerras 			minalign = 0;
17729b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
17739b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
17749b6b563cSPaul Mackerras 			minsize = 4UL << 20;
17759b6b563cSPaul Mackerras 
17769b6b563cSPaul Mackerras 		/*
17779b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
17789b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
17799b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
17809b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
17819b6b563cSPaul Mackerras 		 */
17829b6b563cSPaul Mackerras 		minsize = 4UL << 20;
17839b6b563cSPaul Mackerras 
17849b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
17859b6b563cSPaul Mackerras 		align = max(minalign, minsize);
17869b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
17879b6b563cSPaul Mackerras 		if (base == 0)
17889b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
17899b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
17909b6b563cSPaul Mackerras 			local_alloc_bottom = base;
17919b6b563cSPaul Mackerras 
17929b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
1793aca71ef8SLi Zefan 		memset(path, 0, PROM_SCRATCH_SIZE);
17949b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
17959b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
17969b6b563cSPaul Mackerras 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
17979b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
17989b6b563cSPaul Mackerras 		}
17999b6b563cSPaul Mackerras 
1800a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
1801a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1802a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1803a23414beSPaul Mackerras 
18049b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
18059b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
1806eae5f709SMathieu Malaterre 		prom_debug("\tbase = 0x%llx\n", base);
18079b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
18089b6b563cSPaul Mackerras 
18099b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
18109b6b563cSPaul Mackerras 		 * over the allocated size.
18119b6b563cSPaul Mackerras 		 */
18122b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
18139b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
18149b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
18159b6b563cSPaul Mackerras 			tce_entry |= 0x3;
18169b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
18179b6b563cSPaul Mackerras 		}
18189b6b563cSPaul Mackerras 
18199b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
18209b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
18219b6b563cSPaul Mackerras 		if (phb_node == 0)
18229b6b563cSPaul Mackerras 			prom_printf("... failed\n");
18239b6b563cSPaul Mackerras 		else
18249b6b563cSPaul Mackerras 			prom_printf("... done\n");
18259b6b563cSPaul Mackerras 
18269b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
18279b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
18289b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
18299b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
18309b6b563cSPaul Mackerras 	}
18319b6b563cSPaul Mackerras 
18329b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
18339b6b563cSPaul Mackerras 
18342babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
18352babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
18365827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
18375827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
18389b6b563cSPaul Mackerras 
18399b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
18409b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
18419b6b563cSPaul Mackerras }
1842493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1843493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
18449b6b563cSPaul Mackerras 
18459b6b563cSPaul Mackerras /*
18469b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
18479b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
18489b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
18499b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
18509b6b563cSPaul Mackerras  *
18519b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
18529b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
18539b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
18549b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
18559b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
18569b6b563cSPaul Mackerras  * of setting those values.
18579b6b563cSPaul Mackerras  *
18589b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
18599b6b563cSPaul Mackerras  * is in its holding pattern code.
18609b6b563cSPaul Mackerras  *
18619b6b563cSPaul Mackerras  * -- Cort
18629b6b563cSPaul Mackerras  */
1863bbd0abdaSPaul Mackerras /*
1864bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
1865bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
1866bbd0abdaSPaul Mackerras  */
1867bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1868bbd0abdaSPaul Mackerras 
18699b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
18709b6b563cSPaul Mackerras {
18719b6b563cSPaul Mackerras 	unsigned long i;
18729b6b563cSPaul Mackerras 	phandle node;
18739b6b563cSPaul Mackerras 	char type[64];
18749b6b563cSPaul Mackerras 	unsigned long *spinloop
1875bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
18769b6b563cSPaul Mackerras 	unsigned long *acknowledge
1877bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1878bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
18799b6b563cSPaul Mackerras 
1880dbe78b40SBenjamin Herrenschmidt 	/*
1881dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
1882dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
1883dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
1884dbe78b40SBenjamin Herrenschmidt 	 */
1885dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
1886dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
1887dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
1888dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
1889dbe78b40SBenjamin Herrenschmidt 		return;
1890dbe78b40SBenjamin Herrenschmidt 	}
1891dbe78b40SBenjamin Herrenschmidt 
18929b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
1893eae5f709SMathieu Malaterre 	prom_debug("    1) spinloop       = 0x%lx\n", (unsigned long)spinloop);
1894eae5f709SMathieu Malaterre 	prom_debug("    1) *spinloop      = 0x%lx\n", *spinloop);
1895eae5f709SMathieu Malaterre 	prom_debug("    1) acknowledge    = 0x%lx\n",
18969b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
1897eae5f709SMathieu Malaterre 	prom_debug("    1) *acknowledge   = 0x%lx\n", *acknowledge);
1898eae5f709SMathieu Malaterre 	prom_debug("    1) secondary_hold = 0x%lx\n", secondary_hold);
18999b6b563cSPaul Mackerras 
19009b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
19019b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
19029b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
19039b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
19049b6b563cSPaul Mackerras 	 */
19059b6b563cSPaul Mackerras 	*spinloop = 0;
19069b6b563cSPaul Mackerras 
19079b6b563cSPaul Mackerras 	/* look for cpus */
19089b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
1909493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
1910493adffcSBenjamin Herrenschmidt 		__be32 reg;
1911493adffcSBenjamin Herrenschmidt 
19129b6b563cSPaul Mackerras 		type[0] = 0;
19139b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
19145827d416SAnton Blanchard 		if (strcmp(type, "cpu") != 0)
19159b6b563cSPaul Mackerras 			continue;
19169b6b563cSPaul Mackerras 
19179b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
19189b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
19195827d416SAnton Blanchard 			if (strcmp(type, "okay") != 0)
19209b6b563cSPaul Mackerras 				continue;
19219b6b563cSPaul Mackerras 
1922493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
19239b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
1924493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
19259b6b563cSPaul Mackerras 
1926eae5f709SMathieu Malaterre 		prom_debug("cpu hw idx   = %u\n", cpu_no);
19279b6b563cSPaul Mackerras 
19289b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
19299b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
19309b6b563cSPaul Mackerras 		 * spinloop.
19319b6b563cSPaul Mackerras 		 */
19329b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
19339b6b563cSPaul Mackerras 
1934493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
193527f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
1936eae5f709SMathieu Malaterre 			prom_printf("starting cpu hw idx %u... ", cpu_no);
19379b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
1938493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
19399b6b563cSPaul Mackerras 
19409b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
19419b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
19429b6b563cSPaul Mackerras 				mb();
19439b6b563cSPaul Mackerras 
1944493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
19459b6b563cSPaul Mackerras 				prom_printf("done\n");
1946bbd0abdaSPaul Mackerras 			else
1947eae5f709SMathieu Malaterre 				prom_printf("failed: %lx\n", *acknowledge);
19489b6b563cSPaul Mackerras 		}
19499b6b563cSPaul Mackerras #ifdef CONFIG_SMP
19509b6b563cSPaul Mackerras 		else
1951eae5f709SMathieu Malaterre 			prom_printf("boot cpu hw idx %u\n", cpu_no);
19529b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
19539b6b563cSPaul Mackerras 	}
19549b6b563cSPaul Mackerras 
19559b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
19569b6b563cSPaul Mackerras }
19579b6b563cSPaul Mackerras 
19589b6b563cSPaul Mackerras 
19599b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
19609b6b563cSPaul Mackerras {
19619b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
19625827d416SAnton Blanchard 	prom_entry = pp;
19639b6b563cSPaul Mackerras 
19649b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
19655827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
19665827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
19679b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
19689b6b563cSPaul Mackerras 
19699b6b563cSPaul Mackerras 	/* get device tree root */
19705827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
19715827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
19729b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1973a575b807SPaul Mackerras 
19745827d416SAnton Blanchard 	prom.mmumap = 0;
19759b6b563cSPaul Mackerras }
19769b6b563cSPaul Mackerras 
1977a575b807SPaul Mackerras #ifdef CONFIG_PPC32
1978a575b807SPaul Mackerras /*
1979a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
1980a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
1981a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
1982a575b807SPaul Mackerras  */
1983a575b807SPaul Mackerras static void __init prom_find_mmu(void)
1984a575b807SPaul Mackerras {
1985a575b807SPaul Mackerras 	phandle oprom;
1986a575b807SPaul Mackerras 	char version[64];
1987a575b807SPaul Mackerras 
1988a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1989a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
1990a575b807SPaul Mackerras 		return;
1991a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1992a575b807SPaul Mackerras 		return;
1993a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
1994a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
1995a23414beSPaul Mackerras 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1996a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
1997a23414beSPaul Mackerras 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1998a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1999a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2000a23414beSPaul Mackerras 	} else
2001a575b807SPaul Mackerras 		return;
20025827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
20035827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
20045827d416SAnton Blanchard 		     sizeof(prom.mmumap));
2005493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
20065827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2007a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
2008a575b807SPaul Mackerras }
2009a575b807SPaul Mackerras #else
2010a575b807SPaul Mackerras #define prom_find_mmu()
2011a575b807SPaul Mackerras #endif
2012a575b807SPaul Mackerras 
20139b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
20149b6b563cSPaul Mackerras {
20155827d416SAnton Blanchard 	char *path = of_stdout_device;
20169b6b563cSPaul Mackerras 	char type[16];
2017493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
2018493adffcSBenjamin Herrenschmidt 	__be32 val;
20199b6b563cSPaul Mackerras 
20205827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
20219b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
20229b6b563cSPaul Mackerras 
2023493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
20249b6b563cSPaul Mackerras 
20259b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
20269b6b563cSPaul Mackerras 	memset(path, 0, 256);
20275827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
20285827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
20295827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2030a23414beSPaul Mackerras 		     path, strlen(path) + 1);
20319b6b563cSPaul Mackerras 
203210348f59SBenjamin Herrenschmidt 	/* instance-to-package fails on PA-Semi */
203310348f59SBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
203410348f59SBenjamin Herrenschmidt 	if (stdout_node != PROM_ERROR) {
203510348f59SBenjamin Herrenschmidt 		val = cpu_to_be32(stdout_node);
203610348f59SBenjamin Herrenschmidt 
20379b6b563cSPaul Mackerras 		/* If it's a display, note it */
20389b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
2039493adffcSBenjamin Herrenschmidt 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
20405827d416SAnton Blanchard 		if (strcmp(type, "display") == 0)
2041493adffcSBenjamin Herrenschmidt 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
20429b6b563cSPaul Mackerras 	}
204310348f59SBenjamin Herrenschmidt }
20449b6b563cSPaul Mackerras 
20459b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
20469b6b563cSPaul Mackerras {
20479b6b563cSPaul Mackerras 	char compat[256];
20489b6b563cSPaul Mackerras 	int len, i = 0;
204921fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
20509b6b563cSPaul Mackerras 	phandle rtas;
2051e8222502SBenjamin Herrenschmidt 	int x;
205221fe3301SBenjamin Herrenschmidt #endif
2053e8222502SBenjamin Herrenschmidt 
20546e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
20555827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
20569b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
20579b6b563cSPaul Mackerras 	if (len > 0) {
20589b6b563cSPaul Mackerras 		compat[len] = 0;
20599b6b563cSPaul Mackerras 		while (i < len) {
20609b6b563cSPaul Mackerras 			char *p = &compat[i];
20619b6b563cSPaul Mackerras 			int sl = strlen(p);
20629b6b563cSPaul Mackerras 			if (sl == 0)
20639b6b563cSPaul Mackerras 				break;
20645827d416SAnton Blanchard 			if (strstr(p, "Power Macintosh") ||
20655827d416SAnton Blanchard 			    strstr(p, "MacRISC"))
20669b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
2067133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
2068133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
2069133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
2070133dda1eSArnd Bergmann 			 * so we do it here.
2071133dda1eSArnd Bergmann 			 */
20725827d416SAnton Blanchard 			if (strstr(p, "IBM,CBEA") ||
20735827d416SAnton Blanchard 			    strstr(p, "IBM,CPBW-1.0"))
2074133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
2075133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
20769b6b563cSPaul Mackerras 			i += sl + 1;
20779b6b563cSPaul Mackerras 		}
20789b6b563cSPaul Mackerras 	}
20799b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
20806e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
2081e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
2082e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
2083e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
2084e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
2085e8222502SBenjamin Herrenschmidt 	 */
20865827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
2087e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
2088e8222502SBenjamin Herrenschmidt 	if (len <= 0)
2089e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
20905827d416SAnton Blanchard 	if (strcmp(compat, "chrp"))
2091e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2092e8222502SBenjamin Herrenschmidt 
20939b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
20949b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2095e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
2096e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2097e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
20989b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
20994da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
21009b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
21019b6b563cSPaul Mackerras 	}
21029b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
21039b6b563cSPaul Mackerras #else
2104e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
21059b6b563cSPaul Mackerras #endif
21069b6b563cSPaul Mackerras }
21079b6b563cSPaul Mackerras 
21089b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
21099b6b563cSPaul Mackerras {
21109b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
21119b6b563cSPaul Mackerras }
21129b6b563cSPaul Mackerras 
21139b6b563cSPaul Mackerras /*
21149b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
21159b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
21169b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
21179b6b563cSPaul Mackerras  * we've taken over the MMU.
21189b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
21199b6b563cSPaul Mackerras  * and if so, open it now.
21209b6b563cSPaul Mackerras  */
21219b6b563cSPaul Mackerras static void __init prom_check_displays(void)
21229b6b563cSPaul Mackerras {
21239b6b563cSPaul Mackerras 	char type[16], *path;
21249b6b563cSPaul Mackerras 	phandle node;
21259b6b563cSPaul Mackerras 	ihandle ih;
21269b6b563cSPaul Mackerras 	int i;
21279b6b563cSPaul Mackerras 
21287f995d3bSBenjamin Herrenschmidt 	static const unsigned char default_colors[] = {
21299b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
21309b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
21319b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
21329b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
21339b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
21349b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
21359b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
21369b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
21379b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
21389b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
21399b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
21409b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
21419b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
21429b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
21439b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
21449b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
21459b6b563cSPaul Mackerras 	};
21469b6b563cSPaul Mackerras 	const unsigned char *clut;
21479b6b563cSPaul Mackerras 
21484da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
21499b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
21509b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
21519b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
21525827d416SAnton Blanchard 		if (strcmp(type, "display") != 0)
21539b6b563cSPaul Mackerras 			continue;
21549b6b563cSPaul Mackerras 
21559b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
21565827d416SAnton Blanchard 		path = prom_scratch;
21579b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
21589b6b563cSPaul Mackerras 
21599b6b563cSPaul Mackerras 		/*
21609b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
21619b6b563cSPaul Mackerras 		 * arguments
21629b6b563cSPaul Mackerras 		 */
21639b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
21649b6b563cSPaul Mackerras 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
21659b6b563cSPaul Mackerras 			continue;
21669b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
21679b6b563cSPaul Mackerras 
21689b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
21699b6b563cSPaul Mackerras 		if (ih == 0) {
21709b6b563cSPaul Mackerras 			prom_printf("failed\n");
21719b6b563cSPaul Mackerras 			continue;
21729b6b563cSPaul Mackerras 		}
21739b6b563cSPaul Mackerras 
21749b6b563cSPaul Mackerras 		/* Success */
21759b6b563cSPaul Mackerras 		prom_printf("done\n");
2176a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
21779b6b563cSPaul Mackerras 
21789b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
21799b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
21805827d416SAnton Blanchard 		clut = default_colors;
21813f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
21829b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
21839b6b563cSPaul Mackerras 					   clut[2]) != 0)
21849b6b563cSPaul Mackerras 				break;
21859b6b563cSPaul Mackerras 
21869b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
21875827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
21885827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
21899b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
21909b6b563cSPaul Mackerras 					   clut[2]) != 0)
21919b6b563cSPaul Mackerras 				break;
21929b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
21937191b615SBenjamin Herrenschmidt 
21947191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
21957191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
21967191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
21977191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
21987191b615SBenjamin Herrenschmidt 
21997191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
22007191b615SBenjamin Herrenschmidt 			prom_getprop(node, "width", &width, 4);
22017191b615SBenjamin Herrenschmidt 			prom_getprop(node, "height", &height, 4);
22027191b615SBenjamin Herrenschmidt 			prom_getprop(node, "linebytes", &pitch, 4);
22037191b615SBenjamin Herrenschmidt 			prom_getprop(node, "address", &addr, 4);
22047191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
22057191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
22067191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
22077191b615SBenjamin Herrenschmidt 		}
22087191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
22099b6b563cSPaul Mackerras 	}
22109b6b563cSPaul Mackerras }
22119b6b563cSPaul Mackerras 
22129b6b563cSPaul Mackerras 
22139b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
22149b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
22159b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
22169b6b563cSPaul Mackerras {
22179b6b563cSPaul Mackerras 	void *ret;
22189b6b563cSPaul Mackerras 
22199b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
22209b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
22219b6b563cSPaul Mackerras 		unsigned long room, chunk;
22229b6b563cSPaul Mackerras 
2223eae5f709SMathieu Malaterre 		prom_debug("Chunk exhausted, claiming more at %lx...\n",
22245827d416SAnton Blanchard 			   alloc_bottom);
22255827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
22269b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
22279b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
22289b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2229fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2230fbafd728SAnton Blanchard 				   "(no room)\n");
22319b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
22329b6b563cSPaul Mackerras 		if (chunk == 0)
2233fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2234fbafd728SAnton Blanchard 				   "(claim failed)\n");
2235966728ddSAnton Blanchard 		*mem_end = chunk + room;
22369b6b563cSPaul Mackerras 	}
22379b6b563cSPaul Mackerras 
22389b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
22399b6b563cSPaul Mackerras 	*mem_start += needed;
22409b6b563cSPaul Mackerras 
22419b6b563cSPaul Mackerras 	return ret;
22429b6b563cSPaul Mackerras }
22439b6b563cSPaul Mackerras 
2244493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2245493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2246493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2247493adffcSBenjamin Herrenschmidt 	} while(0)
22489b6b563cSPaul Mackerras 
22499b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
22509b6b563cSPaul Mackerras {
22519b6b563cSPaul Mackerras 	char *s, *os;
22529b6b563cSPaul Mackerras 
22535827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
22549b6b563cSPaul Mackerras 	s += 4;
22555827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
22569b6b563cSPaul Mackerras 		if (strcmp(s, str) == 0)
22579b6b563cSPaul Mackerras 			return s - os;
22589b6b563cSPaul Mackerras 		s += strlen(s) + 1;
22599b6b563cSPaul Mackerras 	}
22609b6b563cSPaul Mackerras 	return 0;
22619b6b563cSPaul Mackerras }
22629b6b563cSPaul Mackerras 
22639b6b563cSPaul Mackerras /*
22649b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
22659b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
22669b6b563cSPaul Mackerras  */
22679b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
22689b6b563cSPaul Mackerras 
22699b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
22709b6b563cSPaul Mackerras 					 unsigned long *mem_start,
22719b6b563cSPaul Mackerras 					 unsigned long *mem_end)
22729b6b563cSPaul Mackerras {
22739b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
22749b6b563cSPaul Mackerras 	unsigned long soff;
22759b6b563cSPaul Mackerras 	phandle child;
22769b6b563cSPaul Mackerras 
22775827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
22789b6b563cSPaul Mackerras 
22799b6b563cSPaul Mackerras 	/* get and store all property names */
22805827d416SAnton Blanchard 	prev_name = "";
22819b6b563cSPaul Mackerras 	for (;;) {
22829b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
22839b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
22849b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
22859b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
22869b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
22879b6b563cSPaul Mackerras 			break;
22889b6b563cSPaul Mackerras 		}
22899b6b563cSPaul Mackerras 
22909b6b563cSPaul Mackerras  		/* skip "name" */
22915827d416SAnton Blanchard  		if (strcmp(namep, "name") == 0) {
22929b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
22935827d416SAnton Blanchard  			prev_name = "name";
22949b6b563cSPaul Mackerras  			continue;
22959b6b563cSPaul Mackerras  		}
22969b6b563cSPaul Mackerras 		/* get/create string entry */
22979b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
22989b6b563cSPaul Mackerras 		if (soff != 0) {
22999b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
23009b6b563cSPaul Mackerras 			namep = sstart + soff;
23019b6b563cSPaul Mackerras 		} else {
23029b6b563cSPaul Mackerras 			/* Trim off some if we can */
23039b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
23045827d416SAnton Blanchard 			dt_string_end = *mem_start;
23059b6b563cSPaul Mackerras 		}
23069b6b563cSPaul Mackerras 		prev_name = namep;
23079b6b563cSPaul Mackerras 	}
23089b6b563cSPaul Mackerras 
23099b6b563cSPaul Mackerras 	/* do all our children */
23109b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
23119b6b563cSPaul Mackerras 	while (child != 0) {
23129b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
23139b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
23149b6b563cSPaul Mackerras 	}
23159b6b563cSPaul Mackerras }
23169b6b563cSPaul Mackerras 
23179b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
23189b6b563cSPaul Mackerras 					unsigned long *mem_end)
23199b6b563cSPaul Mackerras {
23209b6b563cSPaul Mackerras 	phandle child;
23219b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
23229b6b563cSPaul Mackerras 	unsigned long soff;
23239b6b563cSPaul Mackerras 	unsigned char *valp;
23249b6b563cSPaul Mackerras 	static char pname[MAX_PROPERTY_NAME];
23256e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
23269b6b563cSPaul Mackerras 
23279b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
23289b6b563cSPaul Mackerras 
23299b6b563cSPaul Mackerras 	/* get the node's full name */
23309b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2331c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2332c4988820SPaul Mackerras 	if (room > 255)
2333c4988820SPaul Mackerras 		room = 255;
2334c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
23359b6b563cSPaul Mackerras 	if (l >= 0) {
23369b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2337c4988820SPaul Mackerras 		if (l >= room) {
2338c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
23399b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
23409b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
23419b6b563cSPaul Mackerras 		}
23429b6b563cSPaul Mackerras 		namep[l] = '\0';
23439b6b563cSPaul Mackerras 
23449b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2345a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2346a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
23479b6b563cSPaul Mackerras 		 */
2348a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
23499b6b563cSPaul Mackerras 			if (*p == '/')
2350a575b807SPaul Mackerras 				lp = namep;
2351a575b807SPaul Mackerras 			else if (*p != 0)
2352a575b807SPaul Mackerras 				*lp++ = *p;
2353a575b807SPaul Mackerras 		}
2354a575b807SPaul Mackerras 		*lp = 0;
2355a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
23569b6b563cSPaul Mackerras 	}
23579b6b563cSPaul Mackerras 
23589b6b563cSPaul Mackerras 	/* get it again for debugging */
23595827d416SAnton Blanchard 	path = prom_scratch;
23609b6b563cSPaul Mackerras 	memset(path, 0, PROM_SCRATCH_SIZE);
23619b6b563cSPaul Mackerras 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
23629b6b563cSPaul Mackerras 
23639b6b563cSPaul Mackerras 	/* get and store all properties */
23645827d416SAnton Blanchard 	prev_name = "";
23655827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
23669b6b563cSPaul Mackerras 	for (;;) {
23679b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
23685827d416SAnton Blanchard 			      pname) != 1)
23699b6b563cSPaul Mackerras 			break;
23709b6b563cSPaul Mackerras 
23719b6b563cSPaul Mackerras  		/* skip "name" */
23725827d416SAnton Blanchard  		if (strcmp(pname, "name") == 0) {
23735827d416SAnton Blanchard  			prev_name = "name";
23749b6b563cSPaul Mackerras  			continue;
23759b6b563cSPaul Mackerras  		}
23769b6b563cSPaul Mackerras 
23779b6b563cSPaul Mackerras 		/* find string offset */
23785827d416SAnton Blanchard 		soff = dt_find_string(pname);
23799b6b563cSPaul Mackerras 		if (soff == 0) {
23809b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
23815827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
23829b6b563cSPaul Mackerras 			break;
23839b6b563cSPaul Mackerras 		}
23849b6b563cSPaul Mackerras 		prev_name = sstart + soff;
23859b6b563cSPaul Mackerras 
23869b6b563cSPaul Mackerras 		/* get length */
23875827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
23889b6b563cSPaul Mackerras 
23899b6b563cSPaul Mackerras 		/* sanity checks */
23909b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
23919b6b563cSPaul Mackerras 			continue;
23929b6b563cSPaul Mackerras 
23939b6b563cSPaul Mackerras 		/* push property head */
23949b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
23959b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
23969b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
23979b6b563cSPaul Mackerras 
23989b6b563cSPaul Mackerras 		/* push property content */
23999b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
24005827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
24019b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
24026e35d5daSBenjamin Herrenschmidt 
24035827d416SAnton Blanchard 		if (!strcmp(pname, "phandle"))
24046e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
24059b6b563cSPaul Mackerras 	}
24069b6b563cSPaul Mackerras 
24076e35d5daSBenjamin Herrenschmidt 	/* Add a "linux,phandle" property if no "phandle" property already
240811fdb309SBenjamin Herrenschmidt 	 * existed.
24096e35d5daSBenjamin Herrenschmidt 	 */
24106e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
24115827d416SAnton Blanchard 		soff = dt_find_string("linux,phandle");
24129b6b563cSPaul Mackerras 		if (soff == 0)
24139b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
24149b6b563cSPaul Mackerras 				    " <linux-phandle> node %s\n", path);
24159b6b563cSPaul Mackerras 		else {
24169b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
24179b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
24189b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
24199b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
2420493adffcSBenjamin Herrenschmidt 			*(__be32 *)valp = cpu_to_be32(node);
24219b6b563cSPaul Mackerras 		}
24226e35d5daSBenjamin Herrenschmidt 	}
24239b6b563cSPaul Mackerras 
24249b6b563cSPaul Mackerras 	/* do all our children */
24259b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
24269b6b563cSPaul Mackerras 	while (child != 0) {
24279b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
24289b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
24299b6b563cSPaul Mackerras 	}
24309b6b563cSPaul Mackerras 
24319b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
24329b6b563cSPaul Mackerras }
24339b6b563cSPaul Mackerras 
24349b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
24359b6b563cSPaul Mackerras {
24369b6b563cSPaul Mackerras 	phandle root;
24379b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
24389b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
24399b6b563cSPaul Mackerras 	char *namep;
24409b6b563cSPaul Mackerras 	u64 *rsvmap;
24419b6b563cSPaul Mackerras 
24429b6b563cSPaul Mackerras 	/*
24439b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2444fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
24459b6b563cSPaul Mackerras 	 */
24465827d416SAnton Blanchard 	room = alloc_top - alloc_bottom - 0x4000;
24479b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
24489b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
2449eae5f709SMathieu Malaterre 	prom_debug("starting device tree allocs at %lx\n", alloc_bottom);
24509b6b563cSPaul Mackerras 
24519b6b563cSPaul Mackerras 	/* Now try to claim that */
24529b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
24539b6b563cSPaul Mackerras 	if (mem_start == 0)
24549b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2455966728ddSAnton Blanchard 	mem_end = mem_start + room;
24569b6b563cSPaul Mackerras 
24579b6b563cSPaul Mackerras 	/* Get root of tree */
24589b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
24599b6b563cSPaul Mackerras 	if (root == (phandle)0)
24609b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
24619b6b563cSPaul Mackerras 
24629b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
24639b6b563cSPaul Mackerras 	mem_start = _ALIGN(mem_start, 4);
24649b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
24659b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
24665827d416SAnton Blanchard 	dt_header_start = (unsigned long)hdr;
24679b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
24689b6b563cSPaul Mackerras 
24699b6b563cSPaul Mackerras 	/* Start of strings */
24709b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
24715827d416SAnton Blanchard 	dt_string_start = mem_start;
24729b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
24739b6b563cSPaul Mackerras 
24749b6b563cSPaul Mackerras 	/* Add "linux,phandle" in there, we'll need it */
24759b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
24765827d416SAnton Blanchard 	strcpy(namep, "linux,phandle");
24779b6b563cSPaul Mackerras 	mem_start = (unsigned long)namep + strlen(namep) + 1;
24789b6b563cSPaul Mackerras 
24799b6b563cSPaul Mackerras 	/* Build string array */
24809b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
24819b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
24825827d416SAnton Blanchard 	dt_string_end = mem_start;
24839b6b563cSPaul Mackerras 
24849b6b563cSPaul Mackerras 	/* Build structure */
24859b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
24865827d416SAnton Blanchard 	dt_struct_start = mem_start;
24879b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
24889b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
24899b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
24905827d416SAnton Blanchard 	dt_struct_end = PAGE_ALIGN(mem_start);
24919b6b563cSPaul Mackerras 
24929b6b563cSPaul Mackerras 	/* Finish header */
2493493adffcSBenjamin Herrenschmidt 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2494493adffcSBenjamin Herrenschmidt 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2495493adffcSBenjamin Herrenschmidt 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2496493adffcSBenjamin Herrenschmidt 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2497493adffcSBenjamin Herrenschmidt 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2498493adffcSBenjamin Herrenschmidt 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2499493adffcSBenjamin Herrenschmidt 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2500493adffcSBenjamin Herrenschmidt 	hdr->version = cpu_to_be32(OF_DT_VERSION);
25019b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
2502493adffcSBenjamin Herrenschmidt 	hdr->last_comp_version = cpu_to_be32(0x10);
25039b6b563cSPaul Mackerras 
25044d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
25055827d416SAnton Blanchard 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
25069b6b563cSPaul Mackerras 
25079b6b563cSPaul Mackerras #ifdef DEBUG_PROM
25089b6b563cSPaul Mackerras 	{
25099b6b563cSPaul Mackerras 		int i;
25109b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
25115827d416SAnton Blanchard 		for (i = 0; i < mem_reserve_cnt; i++)
2512eae5f709SMathieu Malaterre 			prom_printf("  %llx - %llx\n",
2513493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].base),
2514493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].size));
25159b6b563cSPaul Mackerras 	}
25169b6b563cSPaul Mackerras #endif
25174d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
25184d1f3f25SJimi Xenidis 	 * since it's too late.
25194d1f3f25SJimi Xenidis 	 */
25205827d416SAnton Blanchard 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
25219b6b563cSPaul Mackerras 
2522eae5f709SMathieu Malaterre 	prom_printf("Device tree strings 0x%lx -> 0x%lx\n",
25235827d416SAnton Blanchard 		    dt_string_start, dt_string_end);
2524eae5f709SMathieu Malaterre 	prom_printf("Device tree struct  0x%lx -> 0x%lx\n",
25255827d416SAnton Blanchard 		    dt_struct_start, dt_struct_end);
25269b6b563cSPaul Mackerras }
25279b6b563cSPaul Mackerras 
252854f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
252954f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
253054f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
253154f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
25329b6b563cSPaul Mackerras {
253354f4ee18SHollis Blanchard 	phandle isa;
2534980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
253554f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2536980a6513SBenjamin Herrenschmidt 	char *name;
253754f4ee18SHollis Blanchard 
2538980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2539980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2540980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2541980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2542980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2543980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2544980a6513SBenjamin Herrenschmidt 	}
254554f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
254654f4ee18SHollis Blanchard 		return;
254754f4ee18SHollis Blanchard 
2548980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2549980a6513SBenjamin Herrenschmidt 		return;
255054f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
255154f4ee18SHollis Blanchard 		== PROM_ERROR)
255254f4ee18SHollis Blanchard 		return;
255354f4ee18SHollis Blanchard 
255454f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
255554f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
255654f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
255754f4ee18SHollis Blanchard 		return;
255854f4ee18SHollis Blanchard 
2559980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
256054f4ee18SHollis Blanchard 
256154f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
256254f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2563980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
256454f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
256554f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
256654f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2567980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
256854f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
256954f4ee18SHollis Blanchard }
25708f101a05SHarry Ciao 
25718f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
25728f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
25738f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
25748f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
25758f101a05SHarry Ciao {
25768f101a05SHarry Ciao 	phandle mc;
25778f101a05SHarry Ciao 	u32 mc_reg[4];
25788f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
25798f101a05SHarry Ciao 	u32 ac, sc;
25808f101a05SHarry Ciao 
25818f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
25828f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
25838f101a05SHarry Ciao 		return;
25848f101a05SHarry Ciao 
25858f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
25868f101a05SHarry Ciao 		return;
25878f101a05SHarry Ciao 
25885827d416SAnton Blanchard 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
25895827d416SAnton Blanchard 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
25908f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
25918f101a05SHarry Ciao 		return;
25928f101a05SHarry Ciao 
25938f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
25948f101a05SHarry Ciao 		return;
25958f101a05SHarry Ciao 
25968f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
25978f101a05SHarry Ciao 		return;
25988f101a05SHarry Ciao 
25998f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
26008f101a05SHarry Ciao 
26018f101a05SHarry Ciao 	mc_reg[0] = 0x0;
26028f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
26038f101a05SHarry Ciao 	mc_reg[2] = 0x0;
26048f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
26058f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
26068f101a05SHarry Ciao }
260754f4ee18SHollis Blanchard #else
260854f4ee18SHollis Blanchard #define fixup_device_tree_maple()
26098f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
261054f4ee18SHollis Blanchard #endif
261154f4ee18SHollis Blanchard 
2612e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2613e4805922SOlaf Hering /*
2614e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2615e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2616556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2617e4805922SOlaf Hering  */
2618e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2619e8c0acf9SBenjamin Herrenschmidt {
2620e4805922SOlaf Hering 	phandle ph;
2621e4805922SOlaf Hering 	u32 prop[6];
262226c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2623e8c0acf9SBenjamin Herrenschmidt 	char *name;
2624e8c0acf9SBenjamin Herrenschmidt 	int rc;
2625e8c0acf9SBenjamin Herrenschmidt 
2626e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2627e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2628e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
262926c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2630e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
263126c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
263226c5032eSBenjamin Herrenschmidt 	}
2633e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2634e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2635e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2636e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2637e8c0acf9SBenjamin Herrenschmidt 
2638e4805922SOlaf Hering 			prop[0] = 0x1;
2639e4805922SOlaf Hering 			prop[1] = 0x0;
2640e4805922SOlaf Hering 			prop[2] = rloc;
2641e4805922SOlaf Hering 			prop[3] = 0x0;
2642e4805922SOlaf Hering 			prop[4] = 0x0;
2643e4805922SOlaf Hering 			prop[5] = 0x00010000;
2644e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2645e4805922SOlaf Hering 		}
2646e4805922SOlaf Hering 	}
2647e4805922SOlaf Hering 
2648e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2649e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2650e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2651e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2652e4805922SOlaf Hering 		prop[0] = 14;
2653e4805922SOlaf Hering 		prop[1] = 0x0;
2654556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2655556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2656556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2657556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2658556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2659556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2660556ecf9bSOlaf Hering 		}
2661e4805922SOlaf Hering 	}
2662e8c0acf9SBenjamin Herrenschmidt }
2663e8c0acf9SBenjamin Herrenschmidt #else
2664e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2665e8c0acf9SBenjamin Herrenschmidt #endif
2666e8c0acf9SBenjamin Herrenschmidt 
26679b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
266854f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
266954f4ee18SHollis Blanchard {
26709b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
26719b6b563cSPaul Mackerras 	u32 u3_rev;
26729b6b563cSPaul Mackerras 	u32 interrupts[2];
26739b6b563cSPaul Mackerras 	u32 parent;
26749b6b563cSPaul Mackerras 
26759b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
26769b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
26779b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
26789b6b563cSPaul Mackerras 		return;
26799b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
26809b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
26819b6b563cSPaul Mackerras 		return;
26829b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
26839b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
26849b6b563cSPaul Mackerras 		return;
26859b6b563cSPaul Mackerras 
26869b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
26879b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
26889b6b563cSPaul Mackerras 	    == PROM_ERROR)
26899b6b563cSPaul Mackerras 		return;
26907d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
26919b6b563cSPaul Mackerras 		return;
26929b6b563cSPaul Mackerras 	/* does it need fixup ? */
26939b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
26949b6b563cSPaul Mackerras 		return;
26959b6b563cSPaul Mackerras 
26969b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
26979b6b563cSPaul Mackerras 
26989b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
26999b6b563cSPaul Mackerras 	interrupts[0] = 0;
27009b6b563cSPaul Mackerras 	interrupts[1] = 1;
2701a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2702a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
27039b6b563cSPaul Mackerras 	parent = (u32)mpic;
2704a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2705a23414beSPaul Mackerras 		     &parent, sizeof(parent));
27069b6b563cSPaul Mackerras }
270754f4ee18SHollis Blanchard #else
270854f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
270954f4ee18SHollis Blanchard #endif
27109b6b563cSPaul Mackerras 
271188fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
271294d2dde7SGrant Likely /*
271394d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
271494d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
271594d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
271694d2dde7SGrant Likely  * ethernet node.
271794d2dde7SGrant Likely  */
271894d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
271988fd2a9dSSylvain Munaut {
272088fd2a9dSSylvain Munaut 	u32 node;
272188fd2a9dSSylvain Munaut 	char prop[64];
272294d2dde7SGrant Likely 	int rv;
272388fd2a9dSSylvain Munaut 
272494d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
272594d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
272688fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
272788fd2a9dSSylvain Munaut 		return;
272888fd2a9dSSylvain Munaut 
272994d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
273094d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
273194d2dde7SGrant Likely 	if (!rv)
273288fd2a9dSSylvain Munaut 		return;
273388fd2a9dSSylvain Munaut 
273494d2dde7SGrant Likely 	/*
273594d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
273694d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
273794d2dde7SGrant Likely 	 */
273888fd2a9dSSylvain Munaut 
273994d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
27406f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
27416f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
27426f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
27436f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
27446f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
27456f4347c9SOlaf Hering 			" new-device"
27466f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
27476f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
274894d2dde7SGrant Likely 				" s\" mdio\" device-name"
274994d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
27506f4347c9SOlaf Hering 				" s\" compatible\" property"
27516f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
27526f4347c9SOlaf Hering 				" 0x2 encode-int"
27536f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
27546f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
27556f4347c9SOlaf Hering 				" s\" interrupts\" property"
27566f4347c9SOlaf Hering 			" finish-device");
27576f4347c9SOlaf Hering 	};
27586f4347c9SOlaf Hering 
275994d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
276094d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
276194d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
276294d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
27636f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
27646f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
27656f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
27666f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
27676f4347c9SOlaf Hering 			" new-device"
27686f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
27696f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
27706f4347c9SOlaf Hering 				" my-self"
27716f4347c9SOlaf Hering 				" ihandle>phandle"
27726f4347c9SOlaf Hering 			" finish-device"
27736f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
27746f4347c9SOlaf Hering 				" encode-int"
27756f4347c9SOlaf Hering 				" s\" phy-handle\" property"
27766f4347c9SOlaf Hering 			" device-end");
27776f4347c9SOlaf Hering 	}
277894d2dde7SGrant Likely }
27796f4347c9SOlaf Hering 
278094d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
278194d2dde7SGrant Likely {
278294d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
278394d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
278494d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
278594d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
278694d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
278794d2dde7SGrant Likely 	u32 node;
278894d2dde7SGrant Likely 	char prop[64];
278994d2dde7SGrant Likely 	int rv, len;
279094d2dde7SGrant Likely 
279194d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
279294d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
279394d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
279494d2dde7SGrant Likely 		return;
279594d2dde7SGrant Likely 
279694d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
279794d2dde7SGrant Likely 	if (rv == PROM_ERROR)
279894d2dde7SGrant Likely 		return;
279994d2dde7SGrant Likely 	if (strcmp(prop, "EFIKA5K2"))
280094d2dde7SGrant Likely 		return;
280194d2dde7SGrant Likely 
280294d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
280394d2dde7SGrant Likely 
280494d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
280594d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
280694d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
280794d2dde7SGrant Likely 	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
280894d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
280994d2dde7SGrant Likely 
28107f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
28117f4392cdSDavid Woodhouse 	   fix that too */
28127f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
28137f4392cdSDavid Woodhouse 	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
28147f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
28157f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
28167f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
28177f4392cdSDavid Woodhouse 
281894d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
281994d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
282094d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
282194d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
282294d2dde7SGrant Likely 		if (len == 12) {
282394d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
282494d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
282594d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
282694d2dde7SGrant Likely 		}
282794d2dde7SGrant Likely 	}
282894d2dde7SGrant Likely 
282994d2dde7SGrant Likely 	/* Fixup sound interrupts property */
283094d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
283194d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
283294d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
283394d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
283494d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
283594d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
283694d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
283794d2dde7SGrant Likely 		}
283894d2dde7SGrant Likely 	}
283994d2dde7SGrant Likely 
284094d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
284194d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
284288fd2a9dSSylvain Munaut }
284388fd2a9dSSylvain Munaut #else
284488fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
284588fd2a9dSSylvain Munaut #endif
284688fd2a9dSSylvain Munaut 
284750246787SDarren Stevens #ifdef CONFIG_PPC_PASEMI_NEMO
284850246787SDarren Stevens /*
284950246787SDarren Stevens  * CFE supplied on Nemo is broken in several ways, biggest
285050246787SDarren Stevens  * problem is that it reassigns ISA interrupts to unused mpic ints.
285150246787SDarren Stevens  * Add an interrupt-controller property for the io-bridge to use
285250246787SDarren Stevens  * and correct the ints so we can attach them to an irq_domain
285350246787SDarren Stevens  */
285450246787SDarren Stevens static void __init fixup_device_tree_pasemi(void)
285550246787SDarren Stevens {
285650246787SDarren Stevens 	u32 interrupts[2], parent, rval, val = 0;
285750246787SDarren Stevens 	char *name, *pci_name;
285850246787SDarren Stevens 	phandle iob, node;
285950246787SDarren Stevens 
286050246787SDarren Stevens 	/* Find the root pci node */
286150246787SDarren Stevens 	name = "/pxp@0,e0000000";
286250246787SDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
286350246787SDarren Stevens 	if (!PHANDLE_VALID(iob))
286450246787SDarren Stevens 		return;
286550246787SDarren Stevens 
286650246787SDarren Stevens 	/* check if interrupt-controller node set yet */
286750246787SDarren Stevens 	if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
286850246787SDarren Stevens 		return;
286950246787SDarren Stevens 
287050246787SDarren Stevens 	prom_printf("adding interrupt-controller property for SB600...\n");
287150246787SDarren Stevens 
287250246787SDarren Stevens 	prom_setprop(iob, name, "interrupt-controller", &val, 0);
287350246787SDarren Stevens 
287450246787SDarren Stevens 	pci_name = "/pxp@0,e0000000/pci@11";
287550246787SDarren Stevens 	node = call_prom("finddevice", 1, 1, ADDR(pci_name));
287650246787SDarren Stevens 	parent = ADDR(iob);
287750246787SDarren Stevens 
287850246787SDarren Stevens 	for( ; prom_next_node(&node); ) {
287950246787SDarren Stevens 		/* scan each node for one with an interrupt */
288050246787SDarren Stevens 		if (!PHANDLE_VALID(node))
288150246787SDarren Stevens 			continue;
288250246787SDarren Stevens 
288350246787SDarren Stevens 		rval = prom_getproplen(node, "interrupts");
288450246787SDarren Stevens 		if (rval == 0 || rval == PROM_ERROR)
288550246787SDarren Stevens 			continue;
288650246787SDarren Stevens 
288750246787SDarren Stevens 		prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
288850246787SDarren Stevens 		if ((interrupts[0] < 212) || (interrupts[0] > 222))
288950246787SDarren Stevens 			continue;
289050246787SDarren Stevens 
289150246787SDarren Stevens 		/* found a node, update both interrupts and interrupt-parent */
289250246787SDarren Stevens 		if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
289350246787SDarren Stevens 			interrupts[0] -= 203;
289450246787SDarren Stevens 		if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
289550246787SDarren Stevens 			interrupts[0] -= 213;
289650246787SDarren Stevens 		if (interrupts[0] == 221)
289750246787SDarren Stevens 			interrupts[0] = 14;
289850246787SDarren Stevens 		if (interrupts[0] == 222)
289950246787SDarren Stevens 			interrupts[0] = 8;
290050246787SDarren Stevens 
290150246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupts", interrupts,
290250246787SDarren Stevens 					sizeof(interrupts));
290350246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupt-parent", &parent,
290450246787SDarren Stevens 					sizeof(parent));
290550246787SDarren Stevens 	}
2906687e16bcSDarren Stevens 
2907687e16bcSDarren Stevens 	/*
2908687e16bcSDarren Stevens 	 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
2909687e16bcSDarren Stevens 	 * so that generic isa-bridge code can add the SB600 and its on-board
2910687e16bcSDarren Stevens 	 * peripherals.
2911687e16bcSDarren Stevens 	 */
2912687e16bcSDarren Stevens 	name = "/pxp@0,e0000000/io-bridge@0";
2913687e16bcSDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
2914687e16bcSDarren Stevens 	if (!PHANDLE_VALID(iob))
2915687e16bcSDarren Stevens 		return;
2916687e16bcSDarren Stevens 
2917687e16bcSDarren Stevens 	/* device_type is already set, just change it. */
2918687e16bcSDarren Stevens 
2919687e16bcSDarren Stevens 	prom_printf("Changing device_type of SB600 node...\n");
2920687e16bcSDarren Stevens 
2921687e16bcSDarren Stevens 	prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
292250246787SDarren Stevens }
292350246787SDarren Stevens #else	/* !CONFIG_PPC_PASEMI_NEMO */
292450246787SDarren Stevens static inline void fixup_device_tree_pasemi(void) { }
292550246787SDarren Stevens #endif
292650246787SDarren Stevens 
292754f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
292854f4ee18SHollis Blanchard {
292954f4ee18SHollis Blanchard 	fixup_device_tree_maple();
29308f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
2931e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
293254f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
293388fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
293450246787SDarren Stevens 	fixup_device_tree_pasemi();
293554f4ee18SHollis Blanchard }
29369b6b563cSPaul Mackerras 
29379b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
29389b6b563cSPaul Mackerras {
2939493adffcSBenjamin Herrenschmidt 	__be32 rval;
29409b6b563cSPaul Mackerras 	ihandle prom_cpu;
29419b6b563cSPaul Mackerras 	phandle cpu_pkg;
29429b6b563cSPaul Mackerras 
2943493adffcSBenjamin Herrenschmidt 	rval = 0;
2944493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
2945a575b807SPaul Mackerras 		return;
2946493adffcSBenjamin Herrenschmidt 	prom_cpu = be32_to_cpu(rval);
29479b6b563cSPaul Mackerras 
29489b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
29499b6b563cSPaul Mackerras 
2950af2b7fa1SDarren Stevens 	if (!PHANDLE_VALID(cpu_pkg))
2951af2b7fa1SDarren Stevens 		return;
2952af2b7fa1SDarren Stevens 
2953493adffcSBenjamin Herrenschmidt 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
2954493adffcSBenjamin Herrenschmidt 	prom.cpu = be32_to_cpu(rval);
29559b6b563cSPaul Mackerras 
2956eae5f709SMathieu Malaterre 	prom_debug("Booting CPU hw index = %d\n", prom.cpu);
29579b6b563cSPaul Mackerras }
29589b6b563cSPaul Mackerras 
29599b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
29609b6b563cSPaul Mackerras {
29619b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
29629b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
2963493adffcSBenjamin Herrenschmidt 		__be64 val;
29649b6b563cSPaul Mackerras 
29655827d416SAnton Blanchard 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
29665827d416SAnton Blanchard 		prom_initrd_end = prom_initrd_start + r4;
29679b6b563cSPaul Mackerras 
2968493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_start);
29695827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
2970a23414beSPaul Mackerras 			     &val, sizeof(val));
2971493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_end);
29725827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
2973a23414beSPaul Mackerras 			     &val, sizeof(val));
29749b6b563cSPaul Mackerras 
29755827d416SAnton Blanchard 		reserve_mem(prom_initrd_start,
29765827d416SAnton Blanchard 			    prom_initrd_end - prom_initrd_start);
29779b6b563cSPaul Mackerras 
2978eae5f709SMathieu Malaterre 		prom_debug("initrd_start=0x%lx\n", prom_initrd_start);
2979eae5f709SMathieu Malaterre 		prom_debug("initrd_end=0x%lx\n", prom_initrd_end);
29809b6b563cSPaul Mackerras 	}
29819b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
29829b6b563cSPaul Mackerras }
29839b6b563cSPaul Mackerras 
29845ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64
29855ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE
29865ac47f7aSAnton Blanchard static void reloc_toc(void)
29875ac47f7aSAnton Blanchard {
29885ac47f7aSAnton Blanchard }
29895ac47f7aSAnton Blanchard 
29905ac47f7aSAnton Blanchard static void unreloc_toc(void)
29915ac47f7aSAnton Blanchard {
29925ac47f7aSAnton Blanchard }
29935ac47f7aSAnton Blanchard #else
29941674400aSAnton Blanchard static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
29955ac47f7aSAnton Blanchard {
29965ac47f7aSAnton Blanchard 	unsigned long i;
29971674400aSAnton Blanchard 	unsigned long *toc_entry;
29981674400aSAnton Blanchard 
29991674400aSAnton Blanchard 	/* Get the start of the TOC by using r2 directly. */
30001674400aSAnton Blanchard 	asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
30015ac47f7aSAnton Blanchard 
30025ac47f7aSAnton Blanchard 	for (i = 0; i < nr_entries; i++) {
30035ac47f7aSAnton Blanchard 		*toc_entry = *toc_entry + offset;
30045ac47f7aSAnton Blanchard 		toc_entry++;
30055ac47f7aSAnton Blanchard 	}
30065ac47f7aSAnton Blanchard }
30075ac47f7aSAnton Blanchard 
30085ac47f7aSAnton Blanchard static void reloc_toc(void)
30095ac47f7aSAnton Blanchard {
30105ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
30115ac47f7aSAnton Blanchard 	unsigned long nr_entries =
30125ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
30135ac47f7aSAnton Blanchard 
30141674400aSAnton Blanchard 	__reloc_toc(offset, nr_entries);
30155ac47f7aSAnton Blanchard 
30165ac47f7aSAnton Blanchard 	mb();
30175ac47f7aSAnton Blanchard }
30185ac47f7aSAnton Blanchard 
30195ac47f7aSAnton Blanchard static void unreloc_toc(void)
30205ac47f7aSAnton Blanchard {
30215ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
30225ac47f7aSAnton Blanchard 	unsigned long nr_entries =
30235ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
30245ac47f7aSAnton Blanchard 
30255ac47f7aSAnton Blanchard 	mb();
30265ac47f7aSAnton Blanchard 
30271674400aSAnton Blanchard 	__reloc_toc(-offset, nr_entries);
30285ac47f7aSAnton Blanchard }
30295ac47f7aSAnton Blanchard #endif
30305ac47f7aSAnton Blanchard #endif
303127f44888SBenjamin Herrenschmidt 
30329b6b563cSPaul Mackerras /*
30339b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
30349b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
30359b6b563cSPaul Mackerras  */
30369b6b563cSPaul Mackerras 
30379b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
30389b6b563cSPaul Mackerras 			       unsigned long pp,
3039549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
3040549e8152SPaul Mackerras 			       unsigned long kbase)
30419b6b563cSPaul Mackerras {
30429b6b563cSPaul Mackerras 	unsigned long hdr;
30439b6b563cSPaul Mackerras 
30449b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
3045549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
30469b6b563cSPaul Mackerras 	reloc_got2(offset);
30475ac47f7aSAnton Blanchard #else
30485ac47f7aSAnton Blanchard 	reloc_toc();
30499b6b563cSPaul Mackerras #endif
30509b6b563cSPaul Mackerras 
30519b6b563cSPaul Mackerras 	/*
30529b6b563cSPaul Mackerras 	 * First zero the BSS
30539b6b563cSPaul Mackerras 	 */
30545827d416SAnton Blanchard 	memset(&__bss_start, 0, __bss_stop - __bss_start);
30559b6b563cSPaul Mackerras 
30569b6b563cSPaul Mackerras 	/*
30579b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
30589b6b563cSPaul Mackerras 	 * like /chosen
30599b6b563cSPaul Mackerras 	 */
30609b6b563cSPaul Mackerras 	prom_init_client_services(pp);
30619b6b563cSPaul Mackerras 
30629b6b563cSPaul Mackerras 	/*
3063a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
3064a23414beSPaul Mackerras 	 * and other workarounds
3065a23414beSPaul Mackerras 	 */
3066a23414beSPaul Mackerras 	prom_find_mmu();
3067a23414beSPaul Mackerras 
3068a23414beSPaul Mackerras 	/*
30699b6b563cSPaul Mackerras 	 * Init prom stdout device
30709b6b563cSPaul Mackerras 	 */
30719b6b563cSPaul Mackerras 	prom_init_stdout();
30729b6b563cSPaul Mackerras 
30735827d416SAnton Blanchard 	prom_printf("Preparing to boot %s", linux_banner);
3074e7943fbbSMichael Ellerman 
30759b6b563cSPaul Mackerras 	/*
30769b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
30779b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
30789b6b563cSPaul Mackerras 	 */
30795827d416SAnton Blanchard 	of_platform = prom_find_machine_type();
30805827d416SAnton Blanchard 	prom_printf("Detected machine type: %x\n", of_platform);
30819b6b563cSPaul Mackerras 
30820f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
3083add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
3084add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
3085add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
3086549e8152SPaul Mackerras #endif
3087add60ef3SOlaf Hering 
3088add60ef3SOlaf Hering 	/*
3089add60ef3SOlaf Hering 	 * Check for an initrd
3090add60ef3SOlaf Hering 	 */
3091add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
3092add60ef3SOlaf Hering 
309312cc9fd6SSuraj Jitindar Singh 	/*
309412cc9fd6SSuraj Jitindar Singh 	 * Do early parsing of command line
309512cc9fd6SSuraj Jitindar Singh 	 */
309612cc9fd6SSuraj Jitindar Singh 	early_cmdline_parse();
309712cc9fd6SSuraj Jitindar Singh 
309811fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
30999b6b563cSPaul Mackerras 	/*
31009b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
31019b6b563cSPaul Mackerras 	 */
31025827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES ||
31035827d416SAnton Blanchard 	    of_platform == PLATFORM_PSERIES_LPAR)
31049b6b563cSPaul Mackerras 		prom_send_capabilities();
31059b6b563cSPaul Mackerras #endif
31069b6b563cSPaul Mackerras 
31079b6b563cSPaul Mackerras 	/*
3108f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
31099b6b563cSPaul Mackerras 	 */
31105827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC)
3111549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
31129b6b563cSPaul Mackerras 
31139b6b563cSPaul Mackerras 	/*
31149b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
31159b6b563cSPaul Mackerras 	 */
31169b6b563cSPaul Mackerras 	prom_init_mem();
31179b6b563cSPaul Mackerras 
31189b6b563cSPaul Mackerras 	/*
31199b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
31209b6b563cSPaul Mackerras 	 */
31219b6b563cSPaul Mackerras 	prom_find_boot_cpu();
31229b6b563cSPaul Mackerras 
31239b6b563cSPaul Mackerras 	/*
31249b6b563cSPaul Mackerras 	 * Initialize display devices
31259b6b563cSPaul Mackerras 	 */
31269b6b563cSPaul Mackerras 	prom_check_displays();
31279b6b563cSPaul Mackerras 
3128493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
31299b6b563cSPaul Mackerras 	/*
31309b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
31319b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
31329b6b563cSPaul Mackerras 	 * available for us here...
31339b6b563cSPaul Mackerras 	 */
31345827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES)
31359b6b563cSPaul Mackerras 		prom_initialize_tce_table();
31369b6b563cSPaul Mackerras #endif
31379b6b563cSPaul Mackerras 
31389b6b563cSPaul Mackerras 	/*
313927f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
314027f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
31419b6b563cSPaul Mackerras 	 */
314211fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
31439b6b563cSPaul Mackerras 		prom_instantiate_rtas();
314427f44888SBenjamin Herrenschmidt 
31454a727429SAshley Lai #ifdef CONFIG_PPC64
31464a727429SAshley Lai 	/* instantiate sml */
31474a727429SAshley Lai 	prom_instantiate_sml();
31484a727429SAshley Lai #endif
31494a727429SAshley Lai 
315027f44888SBenjamin Herrenschmidt 	/*
315127f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
315227f44888SBenjamin Herrenschmidt 	 *
315327f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
3154dbe78b40SBenjamin Herrenschmidt 	 *
3155dbe78b40SBenjamin Herrenschmidt 	 * (This must be done after instanciating RTAS)
315627f44888SBenjamin Herrenschmidt 	 */
315711fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
315827f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
31599b6b563cSPaul Mackerras 
31609b6b563cSPaul Mackerras 	/*
31619b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
31629b6b563cSPaul Mackerras 	 */
3163493adffcSBenjamin Herrenschmidt 	if (prom_memory_limit) {
3164493adffcSBenjamin Herrenschmidt 		__be64 val = cpu_to_be64(prom_memory_limit);
31655827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3166493adffcSBenjamin Herrenschmidt 			     &val, sizeof(val));
3167493adffcSBenjamin Herrenschmidt 	}
31689b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
31695827d416SAnton Blanchard 	if (prom_iommu_off)
31705827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3171a23414beSPaul Mackerras 			     NULL, 0);
31729b6b563cSPaul Mackerras 
31735827d416SAnton Blanchard 	if (prom_iommu_force_on)
31745827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3175a23414beSPaul Mackerras 			     NULL, 0);
31769b6b563cSPaul Mackerras 
31775827d416SAnton Blanchard 	if (prom_tce_alloc_start) {
31785827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
31795827d416SAnton Blanchard 			     &prom_tce_alloc_start,
31809b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
31815827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
31825827d416SAnton Blanchard 			     &prom_tce_alloc_end,
31839b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
31849b6b563cSPaul Mackerras 	}
31859b6b563cSPaul Mackerras #endif
31869b6b563cSPaul Mackerras 
31879b6b563cSPaul Mackerras 	/*
31889b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
31899b6b563cSPaul Mackerras 	 */
31909b6b563cSPaul Mackerras 	fixup_device_tree();
31919b6b563cSPaul Mackerras 
31929b6b563cSPaul Mackerras 	/*
31939b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
31949b6b563cSPaul Mackerras 	 */
31959b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
31969b6b563cSPaul Mackerras 	flatten_device_tree();
31979b6b563cSPaul Mackerras 
31983825ac0eSPaul Mackerras 	/*
31993825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
32003825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
320111fdb309SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101).
32023825ac0eSPaul Mackerras 	 */
320311fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
32049b6b563cSPaul Mackerras 		prom_close_stdin();
32059b6b563cSPaul Mackerras 
32069b6b563cSPaul Mackerras 	/*
32079b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
32089b6b563cSPaul Mackerras 	 * devices etc...
32099b6b563cSPaul Mackerras 	 */
32107e862d7eSMichael Ellerman 	prom_printf("Quiescing Open Firmware ...\n");
32119b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
32129b6b563cSPaul Mackerras 
32139b6b563cSPaul Mackerras 	/*
32149b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
32159b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
32169b6b563cSPaul Mackerras 	 * is common to us and kexec
32179b6b563cSPaul Mackerras 	 */
32185827d416SAnton Blanchard 	hdr = dt_header_start;
321940dfef66SBenjamin Herrenschmidt 
322040dfef66SBenjamin Herrenschmidt 	/* Don't print anything after quiesce under OPAL, it crashes OFW */
32217d70c63cSBenjamin Herrenschmidt 	prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
3222eae5f709SMathieu Malaterre 	prom_debug("->dt_header_start=0x%lx\n", hdr);
32239b6b563cSPaul Mackerras 
32249b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
32259b6b563cSPaul Mackerras 	reloc_got2(-offset);
32265ac47f7aSAnton Blanchard #else
32275ac47f7aSAnton Blanchard 	unreloc_toc();
32289b6b563cSPaul Mackerras #endif
32299b6b563cSPaul Mackerras 
32306e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
32319b6b563cSPaul Mackerras 
32329b6b563cSPaul Mackerras 	return 0;
32339b6b563cSPaul Mackerras }
3234