xref: /linux/arch/powerpc/kernel/prom_init.c (revision cbe46bd4f5104552b612505b73d366f66efc2341)
19b6b563cSPaul Mackerras /*
29b6b563cSPaul Mackerras  * Procedures for interfacing to Open Firmware.
39b6b563cSPaul Mackerras  *
49b6b563cSPaul Mackerras  * Paul Mackerras	August 1996.
59b6b563cSPaul Mackerras  * Copyright (C) 1996-2005 Paul Mackerras.
69b6b563cSPaul Mackerras  *
79b6b563cSPaul Mackerras  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
89b6b563cSPaul Mackerras  *    {engebret|bergner}@us.ibm.com
99b6b563cSPaul Mackerras  *
109b6b563cSPaul Mackerras  *      This program is free software; you can redistribute it and/or
119b6b563cSPaul Mackerras  *      modify it under the terms of the GNU General Public License
129b6b563cSPaul Mackerras  *      as published by the Free Software Foundation; either version
139b6b563cSPaul Mackerras  *      2 of the License, or (at your option) any later version.
149b6b563cSPaul Mackerras  */
159b6b563cSPaul Mackerras 
169b6b563cSPaul Mackerras #undef DEBUG_PROM
179b6b563cSPaul Mackerras 
18054f367aSDaniel Axtens /* we cannot use FORTIFY as it brings in new symbols */
19054f367aSDaniel Axtens #define __NO_FORTIFY
20054f367aSDaniel Axtens 
219b6b563cSPaul Mackerras #include <stdarg.h>
229b6b563cSPaul Mackerras #include <linux/kernel.h>
239b6b563cSPaul Mackerras #include <linux/string.h>
249b6b563cSPaul Mackerras #include <linux/init.h>
259b6b563cSPaul Mackerras #include <linux/threads.h>
269b6b563cSPaul Mackerras #include <linux/spinlock.h>
279b6b563cSPaul Mackerras #include <linux/types.h>
289b6b563cSPaul Mackerras #include <linux/pci.h>
299b6b563cSPaul Mackerras #include <linux/proc_fs.h>
309b6b563cSPaul Mackerras #include <linux/delay.h>
319b6b563cSPaul Mackerras #include <linux/initrd.h>
329b6b563cSPaul Mackerras #include <linux/bitops.h>
339b6b563cSPaul Mackerras #include <asm/prom.h>
349b6b563cSPaul Mackerras #include <asm/rtas.h>
359b6b563cSPaul Mackerras #include <asm/page.h>
369b6b563cSPaul Mackerras #include <asm/processor.h>
379b6b563cSPaul Mackerras #include <asm/irq.h>
389b6b563cSPaul Mackerras #include <asm/io.h>
399b6b563cSPaul Mackerras #include <asm/smp.h>
409b6b563cSPaul Mackerras #include <asm/mmu.h>
419b6b563cSPaul Mackerras #include <asm/pgtable.h>
429b6b563cSPaul Mackerras #include <asm/iommu.h>
439b6b563cSPaul Mackerras #include <asm/btext.h>
449b6b563cSPaul Mackerras #include <asm/sections.h>
459b6b563cSPaul Mackerras #include <asm/machdep.h>
460545d543SDaniel Axtens #include <asm/asm-prototypes.h>
479b6b563cSPaul Mackerras 
489b6b563cSPaul Mackerras #include <linux/linux_logo.h>
499b6b563cSPaul Mackerras 
50e63334e5SBenjamin Herrenschmidt /* All of prom_init bss lives here */
515f69e388SBenjamin Herrenschmidt #define __prombss __section(.bss.prominit)
52e63334e5SBenjamin Herrenschmidt 
539b6b563cSPaul Mackerras /*
549b6b563cSPaul Mackerras  * Eventually bump that one up
559b6b563cSPaul Mackerras  */
569b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE	0x100000
579b6b563cSPaul Mackerras 
589b6b563cSPaul Mackerras /*
599b6b563cSPaul Mackerras  * This is the size of the local memory reserve map that gets copied
609b6b563cSPaul Mackerras  * into the boot params passed to the kernel. That size is totally
619b6b563cSPaul Mackerras  * flexible as the kernel just reads the list until it encounters an
629b6b563cSPaul Mackerras  * entry with size 0, so it can be changed without breaking binary
639b6b563cSPaul Mackerras  * compatibility
649b6b563cSPaul Mackerras  */
659b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE	8
669b6b563cSPaul Mackerras 
679b6b563cSPaul Mackerras /*
689b6b563cSPaul Mackerras  * prom_init() is called very early on, before the kernel text
699b6b563cSPaul Mackerras  * and data have been mapped to KERNELBASE.  At this point the code
709b6b563cSPaul Mackerras  * is running at whatever address it has been loaded at.
719b6b563cSPaul Mackerras  * On ppc32 we compile with -mrelocatable, which means that references
729b6b563cSPaul Mackerras  * to extern and static variables get relocated automatically.
735ac47f7aSAnton Blanchard  * ppc64 objects are always relocatable, we just need to relocate the
745ac47f7aSAnton Blanchard  * TOC.
759b6b563cSPaul Mackerras  *
769b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
779b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
789b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
799b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
809b6b563cSPaul Mackerras  *
819b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
829b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
839b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
849b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
859b6b563cSPaul Mackerras  */
865ac47f7aSAnton Blanchard #define ADDR(x)		(u32)(unsigned long)(x)
875ac47f7aSAnton Blanchard 
889b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
89a23414beSPaul Mackerras #define OF_WORKAROUNDS	0
909b6b563cSPaul Mackerras #else
91a23414beSPaul Mackerras #define OF_WORKAROUNDS	of_workarounds
92e63334e5SBenjamin Herrenschmidt static int of_workarounds __prombss;
939b6b563cSPaul Mackerras #endif
949b6b563cSPaul Mackerras 
95a23414beSPaul Mackerras #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
96a23414beSPaul Mackerras #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
97a23414beSPaul Mackerras 
989b6b563cSPaul Mackerras #define PROM_BUG() do {						\
999b6b563cSPaul Mackerras         prom_printf("kernel BUG at %s line 0x%x!\n",		\
1005827d416SAnton Blanchard 		    __FILE__, __LINE__);			\
1019b6b563cSPaul Mackerras         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
1029b6b563cSPaul Mackerras } while (0)
1039b6b563cSPaul Mackerras 
1049b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1059b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1069b6b563cSPaul Mackerras #else
10785aa4b98SMathieu Malaterre #define prom_debug(x...)	do { } while (0)
1089b6b563cSPaul Mackerras #endif
1099b6b563cSPaul Mackerras 
1109b6b563cSPaul Mackerras 
1119b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1129b6b563cSPaul Mackerras 
1139b6b563cSPaul Mackerras struct prom_args {
114493adffcSBenjamin Herrenschmidt         __be32 service;
115493adffcSBenjamin Herrenschmidt         __be32 nargs;
116493adffcSBenjamin Herrenschmidt         __be32 nret;
117493adffcSBenjamin Herrenschmidt         __be32 args[10];
1189b6b563cSPaul Mackerras };
1199b6b563cSPaul Mackerras 
1209b6b563cSPaul Mackerras struct prom_t {
1219b6b563cSPaul Mackerras 	ihandle root;
122a23414beSPaul Mackerras 	phandle chosen;
1239b6b563cSPaul Mackerras 	int cpu;
1249b6b563cSPaul Mackerras 	ihandle stdout;
125a575b807SPaul Mackerras 	ihandle mmumap;
126a23414beSPaul Mackerras 	ihandle memory;
1279b6b563cSPaul Mackerras };
1289b6b563cSPaul Mackerras 
1299b6b563cSPaul Mackerras struct mem_map_entry {
130493adffcSBenjamin Herrenschmidt 	__be64	base;
131493adffcSBenjamin Herrenschmidt 	__be64	size;
1329b6b563cSPaul Mackerras };
1339b6b563cSPaul Mackerras 
134493adffcSBenjamin Herrenschmidt typedef __be32 cell_t;
1359b6b563cSPaul Mackerras 
1366e35d5daSBenjamin Herrenschmidt extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
1376e35d5daSBenjamin Herrenschmidt 		    unsigned long r6, unsigned long r7, unsigned long r8,
1386e35d5daSBenjamin Herrenschmidt 		    unsigned long r9);
1399b6b563cSPaul Mackerras 
1409b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
141c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1429b6b563cSPaul Mackerras #else
143c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1449b6b563cSPaul Mackerras {
145c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1469b6b563cSPaul Mackerras }
1479b6b563cSPaul Mackerras #endif
1489b6b563cSPaul Mackerras 
1499b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1509b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1519b6b563cSPaul Mackerras 
1529b6b563cSPaul Mackerras /* prom structure */
153e63334e5SBenjamin Herrenschmidt static struct prom_t __prombss prom;
1549b6b563cSPaul Mackerras 
155e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_entry;
1569b6b563cSPaul Mackerras 
1579b6b563cSPaul Mackerras #define PROM_SCRATCH_SIZE 256
1589b6b563cSPaul Mackerras 
159e63334e5SBenjamin Herrenschmidt static char __prombss of_stdout_device[256];
160e63334e5SBenjamin Herrenschmidt static char __prombss prom_scratch[PROM_SCRATCH_SIZE];
1619b6b563cSPaul Mackerras 
162e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_header_start;
163e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_struct_start, dt_struct_end;
164e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_string_start, dt_string_end;
1659b6b563cSPaul Mackerras 
166e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_initrd_start, prom_initrd_end;
1679b6b563cSPaul Mackerras 
1689b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
169e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_force_on;
170e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_off;
171e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_start;
172e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_end;
1739b6b563cSPaul Mackerras #endif
1749b6b563cSPaul Mackerras 
175c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
1768ca2d515SBenjamin Herrenschmidt static bool __prombss prom_radix_disable;
177c886087cSBenjamin Herrenschmidt #endif
178014d02cbSSuraj Jitindar Singh 
179014d02cbSSuraj Jitindar Singh struct platform_support {
180014d02cbSSuraj Jitindar Singh 	bool hash_mmu;
181014d02cbSSuraj Jitindar Singh 	bool radix_mmu;
182014d02cbSSuraj Jitindar Singh 	bool radix_gtse;
183ac5e5a54SCédric Le Goater 	bool xive;
184014d02cbSSuraj Jitindar Singh };
185014d02cbSSuraj Jitindar Singh 
186e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
187e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
188e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
189e8222502SBenjamin Herrenschmidt  */
190e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
191e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
192e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
193e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
194e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
195e8222502SBenjamin Herrenschmidt 
196e63334e5SBenjamin Herrenschmidt static int __prombss of_platform;
1979b6b563cSPaul Mackerras 
198e63334e5SBenjamin Herrenschmidt static char __prombss prom_cmd_line[COMMAND_LINE_SIZE];
1999b6b563cSPaul Mackerras 
200e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_memory_limit;
201cf68787bSBenjamin Krill 
202e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top;
203e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top_high;
204e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_bottom;
205e63334e5SBenjamin Herrenschmidt static unsigned long __prombss rmo_top;
206e63334e5SBenjamin Herrenschmidt static unsigned long __prombss ram_top;
2079b6b563cSPaul Mackerras 
208e63334e5SBenjamin Herrenschmidt static struct mem_map_entry __prombss mem_reserve_map[MEM_RESERVE_MAP_SIZE];
209e63334e5SBenjamin Herrenschmidt static int __prombss mem_reserve_cnt;
2109b6b563cSPaul Mackerras 
211e63334e5SBenjamin Herrenschmidt static cell_t __prombss regbuf[1024];
2129b6b563cSPaul Mackerras 
2138ca2d515SBenjamin Herrenschmidt static bool  __prombss rtas_has_query_cpu_stopped;
214dbe78b40SBenjamin Herrenschmidt 
2159b6b563cSPaul Mackerras 
2169b6b563cSPaul Mackerras /*
2179b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2189b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2199b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2209b6b563cSPaul Mackerras  * it is valid
2219b6b563cSPaul Mackerras  */
2229b6b563cSPaul Mackerras 
2239b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2249b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2259b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2269b6b563cSPaul Mackerras 
2279b6b563cSPaul Mackerras 
2289b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
2299b6b563cSPaul Mackerras  * firmware.
2309b6b563cSPaul Mackerras  */
2319b6b563cSPaul Mackerras 
2329b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
2339b6b563cSPaul Mackerras {
2349b6b563cSPaul Mackerras 	int i;
2359b6b563cSPaul Mackerras 	struct prom_args args;
2369b6b563cSPaul Mackerras 	va_list list;
2379b6b563cSPaul Mackerras 
238493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
239493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
240493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
2419b6b563cSPaul Mackerras 
2429b6b563cSPaul Mackerras 	va_start(list, nret);
2439b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
244493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
2459b6b563cSPaul Mackerras 	va_end(list);
2469b6b563cSPaul Mackerras 
2479b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
2489b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
2499b6b563cSPaul Mackerras 
2505827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
251c4988820SPaul Mackerras 		return PROM_ERROR;
2529b6b563cSPaul Mackerras 
253493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
2549b6b563cSPaul Mackerras }
2559b6b563cSPaul Mackerras 
2569b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
2579b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
2589b6b563cSPaul Mackerras {
2599b6b563cSPaul Mackerras 	int i;
2609b6b563cSPaul Mackerras 	struct prom_args args;
2619b6b563cSPaul Mackerras 	va_list list;
2629b6b563cSPaul Mackerras 
263493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
264493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
265493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
2669b6b563cSPaul Mackerras 
2679b6b563cSPaul Mackerras 	va_start(list, rets);
2689b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
269493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
2709b6b563cSPaul Mackerras 	va_end(list);
2719b6b563cSPaul Mackerras 
2729b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
273ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
2749b6b563cSPaul Mackerras 
2755827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
276c4988820SPaul Mackerras 		return PROM_ERROR;
2779b6b563cSPaul Mackerras 
2789b6b563cSPaul Mackerras 	if (rets != NULL)
2799b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
280493adffcSBenjamin Herrenschmidt 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
2819b6b563cSPaul Mackerras 
282493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
2839b6b563cSPaul Mackerras }
2849b6b563cSPaul Mackerras 
2859b6b563cSPaul Mackerras 
2869b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
2879b6b563cSPaul Mackerras {
2889b6b563cSPaul Mackerras 	const char *p, *q;
2899b6b563cSPaul Mackerras 
2905827d416SAnton Blanchard 	if (prom.stdout == 0)
2919b6b563cSPaul Mackerras 		return;
2929b6b563cSPaul Mackerras 
2939b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
2949b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
2959b6b563cSPaul Mackerras 			;
2969b6b563cSPaul Mackerras 		if (q > p)
2975827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
2989b6b563cSPaul Mackerras 		if (*q == 0)
2999b6b563cSPaul Mackerras 			break;
3009b6b563cSPaul Mackerras 		++q;
3015827d416SAnton Blanchard 		call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
3029b6b563cSPaul Mackerras 	}
3039b6b563cSPaul Mackerras }
3049b6b563cSPaul Mackerras 
3059b6b563cSPaul Mackerras 
3068af1da40SMathieu Malaterre /*
3078af1da40SMathieu Malaterre  * Both prom_print_hex & prom_print_dec takes an unsigned long as input so that
3088af1da40SMathieu Malaterre  * we do not need __udivdi3 or __umoddi3 on 32bits.
3098af1da40SMathieu Malaterre  */
3109b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
3119b6b563cSPaul Mackerras {
3129b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
3139b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
3149b6b563cSPaul Mackerras 
3159b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
3169b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
3179b6b563cSPaul Mackerras 		if (buf[i] > '9')
3189b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
3199b6b563cSPaul Mackerras 		val >>= 4;
3209b6b563cSPaul Mackerras 	}
3219b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
3225827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf, nibbles);
3239b6b563cSPaul Mackerras }
3249b6b563cSPaul Mackerras 
3252c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */
3262c48a7d6SMichael Neuling #define UL_DIGITS 21
3272c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val)
3282c48a7d6SMichael Neuling {
3292c48a7d6SMichael Neuling 	int i, size;
3302c48a7d6SMichael Neuling 	char buf[UL_DIGITS+1];
3312c48a7d6SMichael Neuling 
3322c48a7d6SMichael Neuling 	for (i = UL_DIGITS-1; i >= 0;  i--) {
3332c48a7d6SMichael Neuling 		buf[i] = (val % 10) + '0';
3342c48a7d6SMichael Neuling 		val = val/10;
3352c48a7d6SMichael Neuling 		if (val == 0)
3362c48a7d6SMichael Neuling 			break;
3372c48a7d6SMichael Neuling 	}
3382c48a7d6SMichael Neuling 	/* shift stuff down */
3392c48a7d6SMichael Neuling 	size = UL_DIGITS - i;
3405827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf+i, size);
3412c48a7d6SMichael Neuling }
3429b6b563cSPaul Mackerras 
343eae5f709SMathieu Malaterre __printf(1, 2)
3449b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
3459b6b563cSPaul Mackerras {
3469b6b563cSPaul Mackerras 	const char *p, *q, *s;
3479b6b563cSPaul Mackerras 	va_list args;
3489b6b563cSPaul Mackerras 	unsigned long v;
349af277149SBenjamin Herrenschmidt 	long vs;
3508af1da40SMathieu Malaterre 	int n = 0;
3519b6b563cSPaul Mackerras 
3529b6b563cSPaul Mackerras 	va_start(args, format);
3539b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
3549b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
3559b6b563cSPaul Mackerras 			;
3569b6b563cSPaul Mackerras 		if (q > p)
3575827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
3589b6b563cSPaul Mackerras 		if (*q == 0)
3599b6b563cSPaul Mackerras 			break;
3609b6b563cSPaul Mackerras 		if (*q == '\n') {
3619b6b563cSPaul Mackerras 			++q;
3625827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout,
3639b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
3649b6b563cSPaul Mackerras 			continue;
3659b6b563cSPaul Mackerras 		}
3669b6b563cSPaul Mackerras 		++q;
3679b6b563cSPaul Mackerras 		if (*q == 0)
3689b6b563cSPaul Mackerras 			break;
3698af1da40SMathieu Malaterre 		while (*q == 'l') {
3708af1da40SMathieu Malaterre 			++q;
3718af1da40SMathieu Malaterre 			++n;
3728af1da40SMathieu Malaterre 		}
3739b6b563cSPaul Mackerras 		switch (*q) {
3749b6b563cSPaul Mackerras 		case 's':
3759b6b563cSPaul Mackerras 			++q;
3769b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
3779b6b563cSPaul Mackerras 			prom_print(s);
3789b6b563cSPaul Mackerras 			break;
3799b6b563cSPaul Mackerras 		case 'x':
3809b6b563cSPaul Mackerras 			++q;
3818af1da40SMathieu Malaterre 			switch (n) {
3828af1da40SMathieu Malaterre 			case 0:
3838af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
3848af1da40SMathieu Malaterre 				break;
3858af1da40SMathieu Malaterre 			case 1:
3869b6b563cSPaul Mackerras 				v = va_arg(args, unsigned long);
3878af1da40SMathieu Malaterre 				break;
3888af1da40SMathieu Malaterre 			case 2:
3898af1da40SMathieu Malaterre 			default:
3908af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
3918af1da40SMathieu Malaterre 				break;
3928af1da40SMathieu Malaterre 			}
3939b6b563cSPaul Mackerras 			prom_print_hex(v);
3949b6b563cSPaul Mackerras 			break;
3958af1da40SMathieu Malaterre 		case 'u':
3968af1da40SMathieu Malaterre 			++q;
3978af1da40SMathieu Malaterre 			switch (n) {
3988af1da40SMathieu Malaterre 			case 0:
3998af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
4008af1da40SMathieu Malaterre 				break;
4018af1da40SMathieu Malaterre 			case 1:
4028af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long);
4038af1da40SMathieu Malaterre 				break;
4048af1da40SMathieu Malaterre 			case 2:
4058af1da40SMathieu Malaterre 			default:
4068af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
4078af1da40SMathieu Malaterre 				break;
4088af1da40SMathieu Malaterre 			}
4098af1da40SMathieu Malaterre 			prom_print_dec(v);
4108af1da40SMathieu Malaterre 			break;
411af277149SBenjamin Herrenschmidt 		case 'd':
412af277149SBenjamin Herrenschmidt 			++q;
4138af1da40SMathieu Malaterre 			switch (n) {
4148af1da40SMathieu Malaterre 			case 0:
415af277149SBenjamin Herrenschmidt 				vs = va_arg(args, int);
416af277149SBenjamin Herrenschmidt 				break;
4178af1da40SMathieu Malaterre 			case 1:
418af277149SBenjamin Herrenschmidt 				vs = va_arg(args, long);
4198af1da40SMathieu Malaterre 				break;
4208af1da40SMathieu Malaterre 			case 2:
4218af1da40SMathieu Malaterre 			default:
4228af1da40SMathieu Malaterre 				vs = va_arg(args, long long);
4238af1da40SMathieu Malaterre 				break;
4248af1da40SMathieu Malaterre 			}
425af277149SBenjamin Herrenschmidt 			if (vs < 0) {
4265827d416SAnton Blanchard 				prom_print("-");
427af277149SBenjamin Herrenschmidt 				vs = -vs;
428af277149SBenjamin Herrenschmidt 			}
429af277149SBenjamin Herrenschmidt 			prom_print_dec(vs);
4302c48a7d6SMichael Neuling 			break;
4319b6b563cSPaul Mackerras 		}
4329b6b563cSPaul Mackerras 	}
4331b855e16SDaniel Axtens 	va_end(args);
4349b6b563cSPaul Mackerras }
4359b6b563cSPaul Mackerras 
4369b6b563cSPaul Mackerras 
437a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
438a575b807SPaul Mackerras 				unsigned long align)
439a575b807SPaul Mackerras {
440a575b807SPaul Mackerras 
441a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
442a23414beSPaul Mackerras 		/*
443a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
444a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
445a23414beSPaul Mackerras 		 */
446a23414beSPaul Mackerras 		int ret;
447a23414beSPaul Mackerras 		prom_arg_t result;
448a23414beSPaul Mackerras 
449a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
4505827d416SAnton Blanchard 				    ADDR("claim"), prom.memory,
451a23414beSPaul Mackerras 				    align, size, virt);
452a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
453a23414beSPaul Mackerras 			return -1;
454a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
4555827d416SAnton Blanchard 				    ADDR("claim"), prom.mmumap,
456a23414beSPaul Mackerras 				    align, size, virt);
457a23414beSPaul Mackerras 		if (ret != 0) {
458a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
4595827d416SAnton Blanchard 				  prom.memory, size, virt);
460a23414beSPaul Mackerras 			return -1;
461a23414beSPaul Mackerras 		}
462a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
463a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
4645827d416SAnton Blanchard 			  ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
465a23414beSPaul Mackerras 		return virt;
466a23414beSPaul Mackerras 	}
467a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
468a23414beSPaul Mackerras 			 (prom_arg_t)align);
469a575b807SPaul Mackerras }
470a575b807SPaul Mackerras 
4719b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
4729b6b563cSPaul Mackerras {
4739b6b563cSPaul Mackerras 	prom_print(reason);
474add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
475add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
4765827d416SAnton Blanchard 	if (of_platform == PLATFORM_POWERMAC)
477add60ef3SOlaf Hering 		asm("trap\n");
478add60ef3SOlaf Hering 
4791d9a4731SStephen Rothwell 	/* ToDo: should put up an SRC here on pSeries */
4809b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
4819b6b563cSPaul Mackerras 
4829b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
4839b6b563cSPaul Mackerras 		;
4849b6b563cSPaul Mackerras }
4859b6b563cSPaul Mackerras 
4869b6b563cSPaul Mackerras 
4879b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
4889b6b563cSPaul Mackerras {
4899b6b563cSPaul Mackerras 	phandle node;
4909b6b563cSPaul Mackerras 
4919b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
4929b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
4939b6b563cSPaul Mackerras 		return 1;
4949b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4959b6b563cSPaul Mackerras 		return 1;
4969b6b563cSPaul Mackerras 	for (;;) {
4979b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
4989b6b563cSPaul Mackerras 			return 0;
4999b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
5009b6b563cSPaul Mackerras 			return 1;
5019b6b563cSPaul Mackerras 	}
5029b6b563cSPaul Mackerras }
5039b6b563cSPaul Mackerras 
50460d862e5STobias Klauser static inline int prom_getprop(phandle node, const char *pname,
5059b6b563cSPaul Mackerras 			       void *value, size_t valuelen)
5069b6b563cSPaul Mackerras {
5079b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
5089b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
5099b6b563cSPaul Mackerras }
5109b6b563cSPaul Mackerras 
51160d862e5STobias Klauser static inline int prom_getproplen(phandle node, const char *pname)
5129b6b563cSPaul Mackerras {
5139b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
5149b6b563cSPaul Mackerras }
5159b6b563cSPaul Mackerras 
516a23414beSPaul Mackerras static void add_string(char **str, const char *q)
5179b6b563cSPaul Mackerras {
518a23414beSPaul Mackerras 	char *p = *str;
519a23414beSPaul Mackerras 
520a23414beSPaul Mackerras 	while (*q)
521a23414beSPaul Mackerras 		*p++ = *q++;
522a23414beSPaul Mackerras 	*p++ = ' ';
523a23414beSPaul Mackerras 	*str = p;
524a23414beSPaul Mackerras }
525a23414beSPaul Mackerras 
526a23414beSPaul Mackerras static char *tohex(unsigned int x)
527a23414beSPaul Mackerras {
5288ca2d515SBenjamin Herrenschmidt 	static const char digits[] __initconst = "0123456789abcdef";
5298ca2d515SBenjamin Herrenschmidt 	static char result[9] __prombss;
530a23414beSPaul Mackerras 	int i;
531a23414beSPaul Mackerras 
532a23414beSPaul Mackerras 	result[8] = 0;
533a23414beSPaul Mackerras 	i = 8;
534a23414beSPaul Mackerras 	do {
535a23414beSPaul Mackerras 		--i;
536a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
537a23414beSPaul Mackerras 		x >>= 4;
538a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
539a23414beSPaul Mackerras 	return &result[i];
540a23414beSPaul Mackerras }
541a23414beSPaul Mackerras 
542a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
543a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
544a23414beSPaul Mackerras {
545a23414beSPaul Mackerras 	char cmd[256], *p;
546a23414beSPaul Mackerras 
547a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
5489b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
5499b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
550a23414beSPaul Mackerras 
551a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
552a23414beSPaul Mackerras 	p = cmd;
553a23414beSPaul Mackerras 	add_string(&p, "dev");
554a23414beSPaul Mackerras 	add_string(&p, nodename);
555a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
556a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
557a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
5585827d416SAnton Blanchard 	add_string(&p, tohex(strlen(pname)));
559a23414beSPaul Mackerras 	add_string(&p, "property");
560a23414beSPaul Mackerras 	*p = 0;
561a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
5629b6b563cSPaul Mackerras }
5639b6b563cSPaul Mackerras 
5645827d416SAnton Blanchard /* We can't use the standard versions because of relocation headaches. */
565cf68787bSBenjamin Krill #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
566cf68787bSBenjamin Krill 			 || ('a' <= (c) && (c) <= 'f') \
567cf68787bSBenjamin Krill 			 || ('A' <= (c) && (c) <= 'F'))
568cf68787bSBenjamin Krill 
569cf68787bSBenjamin Krill #define isdigit(c)	('0' <= (c) && (c) <= '9')
570cf68787bSBenjamin Krill #define islower(c)	('a' <= (c) && (c) <= 'z')
571cf68787bSBenjamin Krill #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
572cf68787bSBenjamin Krill 
573493adffcSBenjamin Herrenschmidt static unsigned long prom_strtoul(const char *cp, const char **endp)
574cf68787bSBenjamin Krill {
575cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
576cf68787bSBenjamin Krill 
577cf68787bSBenjamin Krill 	if (*cp == '0') {
578cf68787bSBenjamin Krill 		base = 8;
579cf68787bSBenjamin Krill 		cp++;
580cf68787bSBenjamin Krill 		if (toupper(*cp) == 'X') {
581cf68787bSBenjamin Krill 			cp++;
582cf68787bSBenjamin Krill 			base = 16;
583cf68787bSBenjamin Krill 		}
584cf68787bSBenjamin Krill 	}
585cf68787bSBenjamin Krill 
586cf68787bSBenjamin Krill 	while (isxdigit(*cp) &&
587cf68787bSBenjamin Krill 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
588cf68787bSBenjamin Krill 		result = result * base + value;
589cf68787bSBenjamin Krill 		cp++;
590cf68787bSBenjamin Krill 	}
591cf68787bSBenjamin Krill 
592cf68787bSBenjamin Krill 	if (endp)
593cf68787bSBenjamin Krill 		*endp = cp;
594cf68787bSBenjamin Krill 
595cf68787bSBenjamin Krill 	return result;
596cf68787bSBenjamin Krill }
597cf68787bSBenjamin Krill 
598493adffcSBenjamin Herrenschmidt static unsigned long prom_memparse(const char *ptr, const char **retptr)
599cf68787bSBenjamin Krill {
600cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
601cf68787bSBenjamin Krill 	int shift = 0;
602cf68787bSBenjamin Krill 
603cf68787bSBenjamin Krill 	/*
604cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
605cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
606cf68787bSBenjamin Krill 	 * the address we're linked at.
607cf68787bSBenjamin Krill 	 */
608cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
609cf68787bSBenjamin Krill 		shift = 30;
610cf68787bSBenjamin Krill 
611cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
612cf68787bSBenjamin Krill 		shift = 20;
613cf68787bSBenjamin Krill 
614cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
615cf68787bSBenjamin Krill 		shift = 10;
616cf68787bSBenjamin Krill 
617cf68787bSBenjamin Krill 	if (shift) {
618cf68787bSBenjamin Krill 		ret <<= shift;
619cf68787bSBenjamin Krill 		(*retptr)++;
620cf68787bSBenjamin Krill 	}
621cf68787bSBenjamin Krill 
622cf68787bSBenjamin Krill 	return ret;
623cf68787bSBenjamin Krill }
624cf68787bSBenjamin Krill 
6259b6b563cSPaul Mackerras /*
6269b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
6279b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
6289b6b563cSPaul Mackerras  */
6299b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
6309b6b563cSPaul Mackerras {
631cc5d0189SBenjamin Herrenschmidt 	const char *opt;
632cf68787bSBenjamin Krill 
633cc5d0189SBenjamin Herrenschmidt 	char *p;
634*cbe46bd4SChristophe Leroy 	int l = 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);
640*cbe46bd4SChristophe Leroy 	if (IS_ENABLED(CONFIG_CMDLINE_BOOL) && (l <= 0 || p[0] == '\0')) /* dbl check */
641*cbe46bd4SChristophe Leroy 		strlcpy(prom_cmd_line, CONFIG_CMDLINE, sizeof(prom_cmd_line));
6425827d416SAnton Blanchard 	prom_printf("command line: %s\n", prom_cmd_line);
6439b6b563cSPaul Mackerras 
6449b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
6455827d416SAnton Blanchard 	opt = strstr(prom_cmd_line, "iommu=");
6469b6b563cSPaul Mackerras 	if (opt) {
6479b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
6489b6b563cSPaul Mackerras 		opt += 6;
6499b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
6509b6b563cSPaul Mackerras 			opt++;
6515827d416SAnton Blanchard 		if (!strncmp(opt, "off", 3))
6525827d416SAnton Blanchard 			prom_iommu_off = 1;
6535827d416SAnton Blanchard 		else if (!strncmp(opt, "force", 5))
6545827d416SAnton Blanchard 			prom_iommu_force_on = 1;
6559b6b563cSPaul Mackerras 	}
6569b6b563cSPaul Mackerras #endif
6575827d416SAnton Blanchard 	opt = strstr(prom_cmd_line, "mem=");
658cf68787bSBenjamin Krill 	if (opt) {
659cf68787bSBenjamin Krill 		opt += 4;
6605827d416SAnton Blanchard 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
661cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
662cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
6635827d416SAnton Blanchard 		prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
664cf68787bSBenjamin Krill #endif
665cf68787bSBenjamin Krill 	}
666014d02cbSSuraj Jitindar Singh 
667c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
668c886087cSBenjamin Herrenschmidt 	prom_radix_disable = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
669014d02cbSSuraj Jitindar Singh 	opt = strstr(prom_cmd_line, "disable_radix");
670014d02cbSSuraj Jitindar Singh 	if (opt) {
6717a22d632SAneesh Kumar K.V 		opt += 13;
6727a22d632SAneesh Kumar K.V 		if (*opt && *opt == '=') {
6737a22d632SAneesh Kumar K.V 			bool val;
6747a22d632SAneesh Kumar K.V 
6757a22d632SAneesh Kumar K.V 			if (kstrtobool(++opt, &val))
6767a22d632SAneesh Kumar K.V 				prom_radix_disable = false;
6777a22d632SAneesh Kumar K.V 			else
6787a22d632SAneesh Kumar K.V 				prom_radix_disable = val;
6797a22d632SAneesh Kumar K.V 		} else
680014d02cbSSuraj Jitindar Singh 			prom_radix_disable = true;
681014d02cbSSuraj Jitindar Singh 	}
6827a22d632SAneesh Kumar K.V 	if (prom_radix_disable)
6837a22d632SAneesh Kumar K.V 		prom_debug("Radix disabled from cmdline\n");
684c886087cSBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
6859b6b563cSPaul Mackerras }
6869b6b563cSPaul Mackerras 
68711fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
6889b6b563cSPaul Mackerras /*
689530b5e14SNathan Fontenot  * The architecture vector has an array of PVR mask/value pairs,
690530b5e14SNathan Fontenot  * followed by # option vectors - 1, followed by the option vectors.
691530b5e14SNathan Fontenot  *
692530b5e14SNathan Fontenot  * See prom.h for the definition of the bits specified in the
693530b5e14SNathan Fontenot  * architecture vector.
694f709bfacSPaul Mackerras  */
695f709bfacSPaul Mackerras 
696e8a4fd0aSMichael Ellerman /* Firmware expects the value to be n - 1, where n is the # of vectors */
697e8a4fd0aSMichael Ellerman #define NUM_VECTORS(n)		((n) - 1)
698e8a4fd0aSMichael Ellerman 
699e8a4fd0aSMichael Ellerman /*
700e8a4fd0aSMichael Ellerman  * Firmware expects 1 + n - 2, where n is the length of the option vector in
701e8a4fd0aSMichael Ellerman  * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
702e8a4fd0aSMichael Ellerman  */
703e8a4fd0aSMichael Ellerman #define VECTOR_LENGTH(n)	(1 + (n) - 2)
704e8a4fd0aSMichael Ellerman 
705d03d1d65SMichael Ellerman struct option_vector1 {
706d03d1d65SMichael Ellerman 	u8 byte1;
707d03d1d65SMichael Ellerman 	u8 arch_versions;
708cc3d2940SPaul Mackerras 	u8 arch_versions3;
709d03d1d65SMichael Ellerman } __packed;
710d03d1d65SMichael Ellerman 
711d03d1d65SMichael Ellerman struct option_vector2 {
712d03d1d65SMichael Ellerman 	u8 byte1;
713d03d1d65SMichael Ellerman 	__be16 reserved;
714d03d1d65SMichael Ellerman 	__be32 real_base;
715d03d1d65SMichael Ellerman 	__be32 real_size;
716d03d1d65SMichael Ellerman 	__be32 virt_base;
717d03d1d65SMichael Ellerman 	__be32 virt_size;
718d03d1d65SMichael Ellerman 	__be32 load_base;
719d03d1d65SMichael Ellerman 	__be32 min_rma;
720d03d1d65SMichael Ellerman 	__be32 min_load;
721d03d1d65SMichael Ellerman 	u8 min_rma_percent;
722d03d1d65SMichael Ellerman 	u8 max_pft_size;
723d03d1d65SMichael Ellerman } __packed;
724d03d1d65SMichael Ellerman 
725d03d1d65SMichael Ellerman struct option_vector3 {
726d03d1d65SMichael Ellerman 	u8 byte1;
727d03d1d65SMichael Ellerman 	u8 byte2;
728d03d1d65SMichael Ellerman } __packed;
729d03d1d65SMichael Ellerman 
730d03d1d65SMichael Ellerman struct option_vector4 {
731d03d1d65SMichael Ellerman 	u8 byte1;
732d03d1d65SMichael Ellerman 	u8 min_vp_cap;
733d03d1d65SMichael Ellerman } __packed;
734d03d1d65SMichael Ellerman 
735d03d1d65SMichael Ellerman struct option_vector5 {
736d03d1d65SMichael Ellerman 	u8 byte1;
737d03d1d65SMichael Ellerman 	u8 byte2;
738d03d1d65SMichael Ellerman 	u8 byte3;
739d03d1d65SMichael Ellerman 	u8 cmo;
740d03d1d65SMichael Ellerman 	u8 associativity;
741d03d1d65SMichael Ellerman 	u8 bin_opts;
742d03d1d65SMichael Ellerman 	u8 micro_checkpoint;
743d03d1d65SMichael Ellerman 	u8 reserved0;
744d03d1d65SMichael Ellerman 	__be32 max_cpus;
745d03d1d65SMichael Ellerman 	__be16 papr_level;
746d03d1d65SMichael Ellerman 	__be16 reserved1;
747d03d1d65SMichael Ellerman 	u8 platform_facilities;
748d03d1d65SMichael Ellerman 	u8 reserved2;
749d03d1d65SMichael Ellerman 	__be16 reserved3;
750d03d1d65SMichael Ellerman 	u8 subprocessors;
751cc3d2940SPaul Mackerras 	u8 byte22;
752cc3d2940SPaul Mackerras 	u8 intarch;
753cc3d2940SPaul Mackerras 	u8 mmu;
754014d02cbSSuraj Jitindar Singh 	u8 hash_ext;
755014d02cbSSuraj Jitindar Singh 	u8 radix_ext;
756d03d1d65SMichael Ellerman } __packed;
757d03d1d65SMichael Ellerman 
758d03d1d65SMichael Ellerman struct option_vector6 {
759d03d1d65SMichael Ellerman 	u8 reserved;
760d03d1d65SMichael Ellerman 	u8 secondary_pteg;
761d03d1d65SMichael Ellerman 	u8 os_name;
762d03d1d65SMichael Ellerman } __packed;
763d03d1d65SMichael Ellerman 
76476ffb578SMichael Ellerman struct ibm_arch_vec {
765cc3d2940SPaul Mackerras 	struct { u32 mask, val; } pvrs[12];
76676ffb578SMichael Ellerman 
76776ffb578SMichael Ellerman 	u8 num_vectors;
76876ffb578SMichael Ellerman 
76976ffb578SMichael Ellerman 	u8 vec1_len;
77076ffb578SMichael Ellerman 	struct option_vector1 vec1;
77176ffb578SMichael Ellerman 
77276ffb578SMichael Ellerman 	u8 vec2_len;
77376ffb578SMichael Ellerman 	struct option_vector2 vec2;
77476ffb578SMichael Ellerman 
77576ffb578SMichael Ellerman 	u8 vec3_len;
77676ffb578SMichael Ellerman 	struct option_vector3 vec3;
77776ffb578SMichael Ellerman 
77876ffb578SMichael Ellerman 	u8 vec4_len;
77976ffb578SMichael Ellerman 	struct option_vector4 vec4;
78076ffb578SMichael Ellerman 
78176ffb578SMichael Ellerman 	u8 vec5_len;
78276ffb578SMichael Ellerman 	struct option_vector5 vec5;
78376ffb578SMichael Ellerman 
78476ffb578SMichael Ellerman 	u8 vec6_len;
78576ffb578SMichael Ellerman 	struct option_vector6 vec6;
78676ffb578SMichael Ellerman } __packed;
78776ffb578SMichael Ellerman 
788d00e34b9SBenjamin Herrenschmidt static const struct ibm_arch_vec ibm_architecture_vec_template __initconst = {
78976ffb578SMichael Ellerman 	.pvrs = {
79076ffb578SMichael Ellerman 		{
79176ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffe0000), /* POWER5/POWER5+ */
79276ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003a0000),
79376ffb578SMichael Ellerman 		},
79476ffb578SMichael Ellerman 		{
79576ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER6 */
79676ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003e0000),
79776ffb578SMichael Ellerman 		},
79876ffb578SMichael Ellerman 		{
79976ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER7 */
80076ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003f0000),
80176ffb578SMichael Ellerman 		},
80276ffb578SMichael Ellerman 		{
80376ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8E */
80476ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004b0000),
80576ffb578SMichael Ellerman 		},
80676ffb578SMichael Ellerman 		{
80776ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8NVL */
80876ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004c0000),
80976ffb578SMichael Ellerman 		},
81076ffb578SMichael Ellerman 		{
81176ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8 */
81276ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004d0000),
81376ffb578SMichael Ellerman 		},
81476ffb578SMichael Ellerman 		{
815cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffff0000), /* POWER9 */
816cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x004e0000),
817cc3d2940SPaul Mackerras 		},
818cc3d2940SPaul Mackerras 		{
819cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffffffff), /* all 3.00-compliant */
820cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x0f000005),
821cc3d2940SPaul Mackerras 		},
822cc3d2940SPaul Mackerras 		{
82376ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.07-compliant */
82476ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000004),
82576ffb578SMichael Ellerman 		},
82676ffb578SMichael Ellerman 		{
82776ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.06-compliant */
82876ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000003),
82976ffb578SMichael Ellerman 		},
83076ffb578SMichael Ellerman 		{
83176ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.05-compliant */
83276ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000002),
83376ffb578SMichael Ellerman 		},
83476ffb578SMichael Ellerman 		{
83576ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffffffe), /* all 2.04-compliant and earlier */
83676ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000001),
83776ffb578SMichael Ellerman 		},
83876ffb578SMichael Ellerman 	},
83976ffb578SMichael Ellerman 
84076ffb578SMichael Ellerman 	.num_vectors = NUM_VECTORS(6),
84176ffb578SMichael Ellerman 
84276ffb578SMichael Ellerman 	.vec1_len = VECTOR_LENGTH(sizeof(struct option_vector1)),
84376ffb578SMichael Ellerman 	.vec1 = {
84476ffb578SMichael Ellerman 		.byte1 = 0,
84576ffb578SMichael Ellerman 		.arch_versions = OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
84676ffb578SMichael Ellerman 				 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
847cc3d2940SPaul Mackerras 		.arch_versions3 = OV1_PPC_3_00,
84876ffb578SMichael Ellerman 	},
84976ffb578SMichael Ellerman 
85076ffb578SMichael Ellerman 	.vec2_len = VECTOR_LENGTH(sizeof(struct option_vector2)),
85176ffb578SMichael Ellerman 	/* option vector 2: Open Firmware options supported */
85276ffb578SMichael Ellerman 	.vec2 = {
85376ffb578SMichael Ellerman 		.byte1 = OV2_REAL_MODE,
85476ffb578SMichael Ellerman 		.reserved = 0,
85576ffb578SMichael Ellerman 		.real_base = cpu_to_be32(0xffffffff),
85676ffb578SMichael Ellerman 		.real_size = cpu_to_be32(0xffffffff),
85776ffb578SMichael Ellerman 		.virt_base = cpu_to_be32(0xffffffff),
85876ffb578SMichael Ellerman 		.virt_size = cpu_to_be32(0xffffffff),
85976ffb578SMichael Ellerman 		.load_base = cpu_to_be32(0xffffffff),
860687da8fcSSukadev Bhattiprolu 		.min_rma = cpu_to_be32(512),		/* 512MB min RMA */
86176ffb578SMichael Ellerman 		.min_load = cpu_to_be32(0xffffffff),	/* full client load */
86276ffb578SMichael Ellerman 		.min_rma_percent = 0,	/* min RMA percentage of total RAM */
86376ffb578SMichael Ellerman 		.max_pft_size = 48,	/* max log_2(hash table size) */
86476ffb578SMichael Ellerman 	},
86576ffb578SMichael Ellerman 
86676ffb578SMichael Ellerman 	.vec3_len = VECTOR_LENGTH(sizeof(struct option_vector3)),
86776ffb578SMichael Ellerman 	/* option vector 3: processor options supported */
86876ffb578SMichael Ellerman 	.vec3 = {
86976ffb578SMichael Ellerman 		.byte1 = 0,			/* don't ignore, don't halt */
87076ffb578SMichael Ellerman 		.byte2 = OV3_FP | OV3_VMX | OV3_DFP,
87176ffb578SMichael Ellerman 	},
87276ffb578SMichael Ellerman 
87376ffb578SMichael Ellerman 	.vec4_len = VECTOR_LENGTH(sizeof(struct option_vector4)),
87476ffb578SMichael Ellerman 	/* option vector 4: IBM PAPR implementation */
87576ffb578SMichael Ellerman 	.vec4 = {
87676ffb578SMichael Ellerman 		.byte1 = 0,			/* don't halt */
87776ffb578SMichael Ellerman 		.min_vp_cap = OV4_MIN_ENT_CAP,	/* minimum VP entitled capacity */
87876ffb578SMichael Ellerman 	},
87976ffb578SMichael Ellerman 
88076ffb578SMichael Ellerman 	.vec5_len = VECTOR_LENGTH(sizeof(struct option_vector5)),
88176ffb578SMichael Ellerman 	/* option vector 5: PAPR/OF options */
88276ffb578SMichael Ellerman 	.vec5 = {
88376ffb578SMichael Ellerman 		.byte1 = 0,				/* don't ignore, don't halt */
88476ffb578SMichael Ellerman 		.byte2 = OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
88576ffb578SMichael Ellerman 		OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
88676ffb578SMichael Ellerman #ifdef CONFIG_PCI_MSI
88776ffb578SMichael Ellerman 		/* PCIe/MSI support.  Without MSI full PCIe is not supported */
88876ffb578SMichael Ellerman 		OV5_FEAT(OV5_MSI),
88976ffb578SMichael Ellerman #else
89076ffb578SMichael Ellerman 		0,
89176ffb578SMichael Ellerman #endif
89276ffb578SMichael Ellerman 		.byte3 = 0,
89376ffb578SMichael Ellerman 		.cmo =
89476ffb578SMichael Ellerman #ifdef CONFIG_PPC_SMLPAR
89576ffb578SMichael Ellerman 		OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
89676ffb578SMichael Ellerman #else
89776ffb578SMichael Ellerman 		0,
89876ffb578SMichael Ellerman #endif
89976ffb578SMichael Ellerman 		.associativity = OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
9003dbbaf20SMichael Roth 		.bin_opts = OV5_FEAT(OV5_RESIZE_HPT) | OV5_FEAT(OV5_HP_EVT),
90176ffb578SMichael Ellerman 		.micro_checkpoint = 0,
90276ffb578SMichael Ellerman 		.reserved0 = 0,
90376ffb578SMichael Ellerman 		.max_cpus = cpu_to_be32(NR_CPUS),	/* number of cores supported */
90476ffb578SMichael Ellerman 		.papr_level = 0,
90576ffb578SMichael Ellerman 		.reserved1 = 0,
90676ffb578SMichael Ellerman 		.platform_facilities = OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) | OV5_FEAT(OV5_PFO_HW_842),
90776ffb578SMichael Ellerman 		.reserved2 = 0,
90876ffb578SMichael Ellerman 		.reserved3 = 0,
90976ffb578SMichael Ellerman 		.subprocessors = 1,
9100c38ed6fSNathan Fontenot 		.byte22 = OV5_FEAT(OV5_DRMEM_V2),
911cc3d2940SPaul Mackerras 		.intarch = 0,
912014d02cbSSuraj Jitindar Singh 		.mmu = 0,
913014d02cbSSuraj Jitindar Singh 		.hash_ext = 0,
914014d02cbSSuraj Jitindar Singh 		.radix_ext = 0,
91576ffb578SMichael Ellerman 	},
91676ffb578SMichael Ellerman 
91776ffb578SMichael Ellerman 	/* option vector 6: IBM PAPR hints */
91876ffb578SMichael Ellerman 	.vec6_len = VECTOR_LENGTH(sizeof(struct option_vector6)),
91976ffb578SMichael Ellerman 	.vec6 = {
92076ffb578SMichael Ellerman 		.reserved = 0,
92176ffb578SMichael Ellerman 		.secondary_pteg = 0,
92276ffb578SMichael Ellerman 		.os_name = OV6_LINUX,
92376ffb578SMichael Ellerman 	},
92476ffb578SMichael Ellerman };
92576ffb578SMichael Ellerman 
926a614f52eSBenjamin Herrenschmidt static struct ibm_arch_vec __prombss ibm_architecture_vec  ____cacheline_aligned;
927a614f52eSBenjamin Herrenschmidt 
928493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
929493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
93030c69ca0SBenjamin Herrenschmidt static const struct fake_elf {
9319b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
9329b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
9339b6b563cSPaul Mackerras 	struct chrpnote {
9349b6b563cSPaul Mackerras 		u32	namesz;
9359b6b563cSPaul Mackerras 		u32	descsz;
9369b6b563cSPaul Mackerras 		u32	type;
9379b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
9389b6b563cSPaul Mackerras 		struct chrpdesc {
9399b6b563cSPaul Mackerras 			u32	real_mode;
9409b6b563cSPaul Mackerras 			u32	real_base;
9419b6b563cSPaul Mackerras 			u32	real_size;
9429b6b563cSPaul Mackerras 			u32	virt_base;
9439b6b563cSPaul Mackerras 			u32	virt_size;
9449b6b563cSPaul Mackerras 			u32	load_base;
9459b6b563cSPaul Mackerras 		} chrpdesc;
9469b6b563cSPaul Mackerras 	} chrpnote;
9479b6b563cSPaul Mackerras 	struct rpanote {
9489b6b563cSPaul Mackerras 		u32	namesz;
9499b6b563cSPaul Mackerras 		u32	descsz;
9509b6b563cSPaul Mackerras 		u32	type;
9519b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
9529b6b563cSPaul Mackerras 		struct rpadesc {
9539b6b563cSPaul Mackerras 			u32	lpar_affinity;
9549b6b563cSPaul Mackerras 			u32	min_rmo_size;
9559b6b563cSPaul Mackerras 			u32	min_rmo_percent;
9569b6b563cSPaul Mackerras 			u32	max_pft_size;
9579b6b563cSPaul Mackerras 			u32	splpar;
9589b6b563cSPaul Mackerras 			u32	min_load;
9599b6b563cSPaul Mackerras 			u32	new_mem_def;
9609b6b563cSPaul Mackerras 			u32	ignore_me;
9619b6b563cSPaul Mackerras 		} rpadesc;
9629b6b563cSPaul Mackerras 	} rpanote;
963d00e34b9SBenjamin Herrenschmidt } fake_elf __initconst = {
9649b6b563cSPaul Mackerras 	.elfhdr = {
9659b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
9669b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
9679b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
9689b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
9699b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
9709b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
9719b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
9729b6b563cSPaul Mackerras 		.e_phnum = 2
9739b6b563cSPaul Mackerras 	},
9749b6b563cSPaul Mackerras 	.phdr = {
9759b6b563cSPaul Mackerras 		[0] = {
9769b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
9779b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
9789b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
9799b6b563cSPaul Mackerras 		}, [1] = {
9809b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
9819b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
9829b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
9839b6b563cSPaul Mackerras 		}
9849b6b563cSPaul Mackerras 	},
9859b6b563cSPaul Mackerras 	.chrpnote = {
9869b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
9879b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
9889b6b563cSPaul Mackerras 		.type = 0x1275,
9899b6b563cSPaul Mackerras 		.name = "PowerPC",
9909b6b563cSPaul Mackerras 		.chrpdesc = {
9919b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
9929b6b563cSPaul Mackerras 			.real_base = ~0U,
9939b6b563cSPaul Mackerras 			.real_size = ~0U,
9949b6b563cSPaul Mackerras 			.virt_base = ~0U,
9959b6b563cSPaul Mackerras 			.virt_size = ~0U,
9969b6b563cSPaul Mackerras 			.load_base = ~0U
9979b6b563cSPaul Mackerras 		},
9989b6b563cSPaul Mackerras 	},
9999b6b563cSPaul Mackerras 	.rpanote = {
10009b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
10019b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
10029b6b563cSPaul Mackerras 		.type = 0x12759999,
10039b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
10049b6b563cSPaul Mackerras 		.rpadesc = {
10055663a123SPaul Mackerras 			.lpar_affinity = 0,
10065663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
10079b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
10085663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
10099b6b563cSPaul Mackerras 			.splpar = 1,
10109b6b563cSPaul Mackerras 			.min_load = ~0U,
10115663a123SPaul Mackerras 			.new_mem_def = 0
10129b6b563cSPaul Mackerras 		}
10139b6b563cSPaul Mackerras 	}
10149b6b563cSPaul Mackerras };
1015493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
10169b6b563cSPaul Mackerras 
1017efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
1018efec959fSBenjamin Herrenschmidt {
1019efec959fSBenjamin Herrenschmidt 	phandle node;
1020efec959fSBenjamin Herrenschmidt 	char type[64];
1021efec959fSBenjamin Herrenschmidt 	unsigned int plen;
1022efec959fSBenjamin Herrenschmidt 
1023efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
1024efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
1025efec959fSBenjamin Herrenschmidt 		type[0] = 0;
1026efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
1027efec959fSBenjamin Herrenschmidt 
10285827d416SAnton Blanchard 		if (strcmp(type, "cpu"))
1029efec959fSBenjamin Herrenschmidt 			continue;
1030efec959fSBenjamin Herrenschmidt 		/*
1031efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
1032efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
1033efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
1034efec959fSBenjamin Herrenschmidt 		 */
1035efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
1036efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
1037efec959fSBenjamin Herrenschmidt 			break;
1038efec959fSBenjamin Herrenschmidt 		plen >>= 2;
10392c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
1040efec959fSBenjamin Herrenschmidt 
1041efec959fSBenjamin Herrenschmidt 		/* Sanity check */
1042efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
10432c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
1044efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
1045efec959fSBenjamin Herrenschmidt 			return 1;
1046efec959fSBenjamin Herrenschmidt 		}
1047efec959fSBenjamin Herrenschmidt 		return plen;
1048efec959fSBenjamin Herrenschmidt 	}
1049efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
1050efec959fSBenjamin Herrenschmidt 
1051efec959fSBenjamin Herrenschmidt 	return 1;
1052efec959fSBenjamin Herrenschmidt 
1053efec959fSBenjamin Herrenschmidt }
1054efec959fSBenjamin Herrenschmidt 
1055014d02cbSSuraj Jitindar Singh static void __init prom_parse_mmu_model(u8 val,
1056014d02cbSSuraj Jitindar Singh 					struct platform_support *support)
1057014d02cbSSuraj Jitindar Singh {
1058014d02cbSSuraj Jitindar Singh 	switch (val) {
1059014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_DYNAMIC):
1060014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_EITHER): /* Either Available */
1061014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - either supported\n");
1062014d02cbSSuraj Jitindar Singh 		support->radix_mmu = !prom_radix_disable;
1063014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1064014d02cbSSuraj Jitindar Singh 		break;
1065014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */
1066014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - radix only\n");
1067014d02cbSSuraj Jitindar Singh 		if (prom_radix_disable) {
1068014d02cbSSuraj Jitindar Singh 			/*
1069014d02cbSSuraj Jitindar Singh 			 * If we __have__ to do radix, we're better off ignoring
1070014d02cbSSuraj Jitindar Singh 			 * the command line rather than not booting.
1071014d02cbSSuraj Jitindar Singh 			 */
1072014d02cbSSuraj Jitindar Singh 			prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
1073014d02cbSSuraj Jitindar Singh 		}
1074014d02cbSSuraj Jitindar Singh 		support->radix_mmu = true;
1075014d02cbSSuraj Jitindar Singh 		break;
1076014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_HASH):
1077014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - hash only\n");
1078014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1079014d02cbSSuraj Jitindar Singh 		break;
1080014d02cbSSuraj Jitindar Singh 	default:
1081014d02cbSSuraj Jitindar Singh 		prom_debug("Unknown mmu support option: 0x%x\n", val);
1082014d02cbSSuraj Jitindar Singh 		break;
1083014d02cbSSuraj Jitindar Singh 	}
1084014d02cbSSuraj Jitindar Singh }
1085014d02cbSSuraj Jitindar Singh 
1086ac5e5a54SCédric Le Goater static void __init prom_parse_xive_model(u8 val,
1087ac5e5a54SCédric Le Goater 					 struct platform_support *support)
1088ac5e5a54SCédric Le Goater {
1089ac5e5a54SCédric Le Goater 	switch (val) {
1090ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EITHER): /* Either Available */
1091ac5e5a54SCédric Le Goater 		prom_debug("XIVE - either mode supported\n");
1092ac5e5a54SCédric Le Goater 		support->xive = true;
1093ac5e5a54SCédric Le Goater 		break;
1094ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EXPLOIT): /* Only Exploitation mode */
1095ac5e5a54SCédric Le Goater 		prom_debug("XIVE - exploitation mode supported\n");
1096ac5e5a54SCédric Le Goater 		support->xive = true;
1097ac5e5a54SCédric Le Goater 		break;
1098ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_LEGACY): /* Only Legacy mode */
1099ac5e5a54SCédric Le Goater 		prom_debug("XIVE - legacy mode supported\n");
1100ac5e5a54SCédric Le Goater 		break;
1101ac5e5a54SCédric Le Goater 	default:
1102ac5e5a54SCédric Le Goater 		prom_debug("Unknown xive support option: 0x%x\n", val);
1103ac5e5a54SCédric Le Goater 		break;
1104ac5e5a54SCédric Le Goater 	}
1105ac5e5a54SCédric Le Goater }
1106ac5e5a54SCédric Le Goater 
1107014d02cbSSuraj Jitindar Singh static void __init prom_parse_platform_support(u8 index, u8 val,
1108014d02cbSSuraj Jitindar Singh 					       struct platform_support *support)
1109014d02cbSSuraj Jitindar Singh {
1110014d02cbSSuraj Jitindar Singh 	switch (index) {
1111014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_MMU_SUPPORT): /* MMU Model */
1112014d02cbSSuraj Jitindar Singh 		prom_parse_mmu_model(val & OV5_FEAT(OV5_MMU_SUPPORT), support);
1113014d02cbSSuraj Jitindar Singh 		break;
1114014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_RADIX_GTSE): /* Radix Extensions */
1115014d02cbSSuraj Jitindar Singh 		if (val & OV5_FEAT(OV5_RADIX_GTSE)) {
1116014d02cbSSuraj Jitindar Singh 			prom_debug("Radix - GTSE supported\n");
1117014d02cbSSuraj Jitindar Singh 			support->radix_gtse = true;
1118014d02cbSSuraj Jitindar Singh 		}
1119014d02cbSSuraj Jitindar Singh 		break;
1120ac5e5a54SCédric Le Goater 	case OV5_INDX(OV5_XIVE_SUPPORT): /* Interrupt mode */
1121ac5e5a54SCédric Le Goater 		prom_parse_xive_model(val & OV5_FEAT(OV5_XIVE_SUPPORT),
1122ac5e5a54SCédric Le Goater 				      support);
1123ac5e5a54SCédric Le Goater 		break;
1124014d02cbSSuraj Jitindar Singh 	}
1125014d02cbSSuraj Jitindar Singh }
1126014d02cbSSuraj Jitindar Singh 
1127014d02cbSSuraj Jitindar Singh static void __init prom_check_platform_support(void)
1128014d02cbSSuraj Jitindar Singh {
1129014d02cbSSuraj Jitindar Singh 	struct platform_support supported = {
1130014d02cbSSuraj Jitindar Singh 		.hash_mmu = false,
1131014d02cbSSuraj Jitindar Singh 		.radix_mmu = false,
1132ac5e5a54SCédric Le Goater 		.radix_gtse = false,
1133ac5e5a54SCédric Le Goater 		.xive = false
1134014d02cbSSuraj Jitindar Singh 	};
1135014d02cbSSuraj Jitindar Singh 	int prop_len = prom_getproplen(prom.chosen,
1136014d02cbSSuraj Jitindar Singh 				       "ibm,arch-vec-5-platform-support");
1137a614f52eSBenjamin Herrenschmidt 
1138a614f52eSBenjamin Herrenschmidt 	/* First copy the architecture vec template */
1139a614f52eSBenjamin Herrenschmidt 	ibm_architecture_vec = ibm_architecture_vec_template;
1140a614f52eSBenjamin Herrenschmidt 
1141014d02cbSSuraj Jitindar Singh 	if (prop_len > 1) {
1142014d02cbSSuraj Jitindar Singh 		int i;
1143ab912399SSuraj Jitindar Singh 		u8 vec[8];
1144014d02cbSSuraj Jitindar Singh 		prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1145014d02cbSSuraj Jitindar Singh 			   prop_len);
1146ab912399SSuraj Jitindar Singh 		if (prop_len > sizeof(vec))
1147ab912399SSuraj Jitindar Singh 			prom_printf("WARNING: ibm,arch-vec-5-platform-support longer than expected (len: %d)\n",
1148ab912399SSuraj Jitindar Singh 				    prop_len);
1149014d02cbSSuraj Jitindar Singh 		prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support",
1150014d02cbSSuraj Jitindar Singh 			     &vec, sizeof(vec));
1151ab912399SSuraj Jitindar Singh 		for (i = 0; i < sizeof(vec); i += 2) {
1152014d02cbSSuraj Jitindar Singh 			prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2
1153014d02cbSSuraj Jitindar Singh 								  , vec[i]
1154014d02cbSSuraj Jitindar Singh 								  , vec[i + 1]);
1155014d02cbSSuraj Jitindar Singh 			prom_parse_platform_support(vec[i], vec[i + 1],
1156014d02cbSSuraj Jitindar Singh 						    &supported);
1157014d02cbSSuraj Jitindar Singh 		}
1158014d02cbSSuraj Jitindar Singh 	}
1159014d02cbSSuraj Jitindar Singh 
116079b46868SAlexey Kardashevskiy 	if (supported.radix_mmu && supported.radix_gtse &&
116179b46868SAlexey Kardashevskiy 	    IS_ENABLED(CONFIG_PPC_RADIX_MMU)) {
1162014d02cbSSuraj Jitindar Singh 		/* Radix preferred - but we require GTSE for now */
1163014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for radix with GTSE\n");
1164014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1165014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.radix_ext = OV5_FEAT(OV5_RADIX_GTSE);
1166014d02cbSSuraj Jitindar Singh 	} else if (supported.hash_mmu) {
1167014d02cbSSuraj Jitindar Singh 		/* Default to hash mmu (if we can) */
1168014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for hash\n");
1169014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1170014d02cbSSuraj Jitindar Singh 	} else {
1171014d02cbSSuraj Jitindar Singh 		/* We're probably on a legacy hypervisor */
1172014d02cbSSuraj Jitindar Singh 		prom_debug("Assuming legacy hash support\n");
1173014d02cbSSuraj Jitindar Singh 	}
1174ac5e5a54SCédric Le Goater 
1175ac5e5a54SCédric Le Goater 	if (supported.xive) {
1176ac5e5a54SCédric Le Goater 		prom_debug("Asking for XIVE\n");
1177ac5e5a54SCédric Le Goater 		ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1178ac5e5a54SCédric Le Goater 	}
1179014d02cbSSuraj Jitindar Singh }
1180efec959fSBenjamin Herrenschmidt 
11819b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
11829b6b563cSPaul Mackerras {
1183493adffcSBenjamin Herrenschmidt 	ihandle root;
1184f709bfacSPaul Mackerras 	prom_arg_t ret;
1185dbd0c5d5SLaurent Dufour 	u32 cores;
11869b6b563cSPaul Mackerras 
1187014d02cbSSuraj Jitindar Singh 	/* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1188014d02cbSSuraj Jitindar Singh 	prom_check_platform_support();
1189014d02cbSSuraj Jitindar Singh 
1190f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
1191f709bfacSPaul Mackerras 	if (root != 0) {
1192efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
1193efec959fSBenjamin Herrenschmidt 		 *
1194efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
1195efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
1196efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
1197efec959fSBenjamin Herrenschmidt 		 */
1198dbd0c5d5SLaurent Dufour 
1199dbd0c5d5SLaurent Dufour 		cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1200eae5f709SMathieu Malaterre 		prom_printf("Max number of cores passed to firmware: %u (NR_CPUS = %d)\n",
1201dbd0c5d5SLaurent Dufour 			    cores, NR_CPUS);
120276ffb578SMichael Ellerman 
120376ffb578SMichael Ellerman 		ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1204efec959fSBenjamin Herrenschmidt 
1205f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
1206049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
1207f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
1208f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
120933b74977SBenjamin Herrenschmidt 				  root,
121076ffb578SMichael Ellerman 				  ADDR(&ibm_architecture_vec)) == 0) {
1211f709bfacSPaul Mackerras 			/* the call exists... */
1212f709bfacSPaul Mackerras 			if (ret)
12134da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
1214f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
1215f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
12164da727aeSAnton Blanchard 			prom_printf(" done\n");
1217f709bfacSPaul Mackerras 			return;
1218f709bfacSPaul Mackerras 		}
1219f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
1220049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
1221f709bfacSPaul Mackerras 	}
1222f709bfacSPaul Mackerras 
1223493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
1224493adffcSBenjamin Herrenschmidt 	{
1225493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
1226493adffcSBenjamin Herrenschmidt 
1227f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
1228493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
1229493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
12309b6b563cSPaul Mackerras 		if (elfloader == 0) {
12319b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
12329b6b563cSPaul Mackerras 			return;
12339b6b563cSPaul Mackerras 		}
12349b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
12359b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
12369b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
12379b6b563cSPaul Mackerras 	}
1238493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1239493adffcSBenjamin Herrenschmidt }
124011fdb309SBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
12419b6b563cSPaul Mackerras 
12429b6b563cSPaul Mackerras /*
12439b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
12449b6b563cSPaul Mackerras  *
12459b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
12469b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
12479b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
12489b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
12499b6b563cSPaul Mackerras  *  never happen.
12509b6b563cSPaul Mackerras  *
12519b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
12529b6b563cSPaul Mackerras  *  TCEs overlap
12539b6b563cSPaul Mackerras  *
12549b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
12559b6b563cSPaul Mackerras  *
12569b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
12579b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
12589b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
12599b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
12609b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
12619b6b563cSPaul Mackerras  *
12629b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
12639b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
12649b6b563cSPaul Mackerras  *  reserved.
12659b6b563cSPaul Mackerras  */
12669b6b563cSPaul Mackerras 
12679b6b563cSPaul Mackerras 
12689b6b563cSPaul Mackerras /*
12699b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
12709b6b563cSPaul Mackerras  *
12719b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
12729b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
12739b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
12749b6b563cSPaul Mackerras  */
12759b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
12769b6b563cSPaul Mackerras {
12775827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
12789b6b563cSPaul Mackerras 	unsigned long addr = 0;
12799b6b563cSPaul Mackerras 
1280c4988820SPaul Mackerras 	if (align)
1281c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
1282eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx)\n", __func__, size, align);
12835827d416SAnton Blanchard 	if (ram_top == 0)
12849b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
12859b6b563cSPaul Mackerras 
12869b6b563cSPaul Mackerras 	if (align)
12875827d416SAnton Blanchard 		base = _ALIGN_UP(alloc_bottom, align);
12889b6b563cSPaul Mackerras 	else
12895827d416SAnton Blanchard 		base = alloc_bottom;
12909b6b563cSPaul Mackerras 
12915827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
12929b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
1293eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
12949b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1295c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
12969b6b563cSPaul Mackerras 			break;
12979b6b563cSPaul Mackerras 		addr = 0;
12989b6b563cSPaul Mackerras 		if (align == 0)
12999b6b563cSPaul Mackerras 			break;
13009b6b563cSPaul Mackerras 	}
13019b6b563cSPaul Mackerras 	if (addr == 0)
13029b6b563cSPaul Mackerras 		return 0;
13035827d416SAnton Blanchard 	alloc_bottom = addr + size;
13049b6b563cSPaul Mackerras 
1305eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1306eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1307eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1308eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1309eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1310eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
13119b6b563cSPaul Mackerras 
13129b6b563cSPaul Mackerras 	return addr;
13139b6b563cSPaul Mackerras }
13149b6b563cSPaul Mackerras 
13159b6b563cSPaul Mackerras /*
13169b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
13179b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
13189b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
13199b6b563cSPaul Mackerras  */
13209b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
13219b6b563cSPaul Mackerras 				       int highmem)
13229b6b563cSPaul Mackerras {
13239b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
13249b6b563cSPaul Mackerras 
1325eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx, %s)\n", __func__, size, align,
13265827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
13275827d416SAnton Blanchard 	if (ram_top == 0)
13289b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
13299b6b563cSPaul Mackerras 
13309b6b563cSPaul Mackerras 	if (highmem) {
13319b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
13325827d416SAnton Blanchard 		addr = _ALIGN_DOWN(alloc_top_high - size, align);
13335827d416SAnton Blanchard 		if (addr <= alloc_bottom)
13349b6b563cSPaul Mackerras 			return 0;
13359b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
13369b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
13379b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
13389b6b563cSPaul Mackerras 		 */
13395827d416SAnton Blanchard 		if (addr < rmo_top) {
13409b6b563cSPaul Mackerras 			/* Good, we are first */
13415827d416SAnton Blanchard 			if (alloc_top == rmo_top)
13425827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
13439b6b563cSPaul Mackerras 			else
13449b6b563cSPaul Mackerras 				return 0;
13459b6b563cSPaul Mackerras 		}
13465827d416SAnton Blanchard 		alloc_top_high = addr;
13479b6b563cSPaul Mackerras 		goto bail;
13489b6b563cSPaul Mackerras 	}
13499b6b563cSPaul Mackerras 
13505827d416SAnton Blanchard 	base = _ALIGN_DOWN(alloc_top - size, align);
13515827d416SAnton Blanchard 	for (; base > alloc_bottom;
13529b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
1353eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
13549b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1355c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
13569b6b563cSPaul Mackerras 			break;
13579b6b563cSPaul Mackerras 		addr = 0;
13589b6b563cSPaul Mackerras 	}
13599b6b563cSPaul Mackerras 	if (addr == 0)
13609b6b563cSPaul Mackerras 		return 0;
13615827d416SAnton Blanchard 	alloc_top = addr;
13629b6b563cSPaul Mackerras 
13639b6b563cSPaul Mackerras  bail:
1364eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1365eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1366eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1367eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1368eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1369eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
13709b6b563cSPaul Mackerras 
13719b6b563cSPaul Mackerras 	return addr;
13729b6b563cSPaul Mackerras }
13739b6b563cSPaul Mackerras 
13749b6b563cSPaul Mackerras /*
13759b6b563cSPaul Mackerras  * Parse a "reg" cell
13769b6b563cSPaul Mackerras  */
13779b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
13789b6b563cSPaul Mackerras {
13799b6b563cSPaul Mackerras 	cell_t *p = *cellp;
13809b6b563cSPaul Mackerras 	unsigned long r = 0;
13819b6b563cSPaul Mackerras 
13829b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
13839b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
13849b6b563cSPaul Mackerras 		p++;
13859b6b563cSPaul Mackerras 		s--;
13869b6b563cSPaul Mackerras 	}
1387493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
13889b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
138935499c01SPaul Mackerras 	if (s > 1) {
13909b6b563cSPaul Mackerras 		r <<= 32;
1391493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
13929b6b563cSPaul Mackerras 	}
13939b6b563cSPaul Mackerras #endif
13949b6b563cSPaul Mackerras 	*cellp = p;
13959b6b563cSPaul Mackerras 	return r;
13969b6b563cSPaul Mackerras }
13979b6b563cSPaul Mackerras 
13989b6b563cSPaul Mackerras /*
13999b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
14009b6b563cSPaul Mackerras  * we don't need anything smarter at this point
14019b6b563cSPaul Mackerras  *
14029b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
14039b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
14049b6b563cSPaul Mackerras  * them down.
14059b6b563cSPaul Mackerras  */
14060108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
14079b6b563cSPaul Mackerras {
1408cbbcf340SKumar Gala 	u64 top = base + size;
14095827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
14109b6b563cSPaul Mackerras 
14119b6b563cSPaul Mackerras 	if (size == 0)
14129b6b563cSPaul Mackerras 		return;
14139b6b563cSPaul Mackerras 
14149b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
14159b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
14169b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
14179b6b563cSPaul Mackerras 	 */
14189b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
14199b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
14209b6b563cSPaul Mackerras 	size = top - base;
14219b6b563cSPaul Mackerras 
14229b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
14239b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1424493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1425493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
14265827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
14279b6b563cSPaul Mackerras }
14289b6b563cSPaul Mackerras 
14299b6b563cSPaul Mackerras /*
1430b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
14319b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
14329b6b563cSPaul Mackerras  */
14339b6b563cSPaul Mackerras static void __init prom_init_mem(void)
14349b6b563cSPaul Mackerras {
14359b6b563cSPaul Mackerras 	phandle node;
1436eab00a20SMathieu Malaterre #ifdef DEBUG_PROM
1437eab00a20SMathieu Malaterre 	char *path;
1438eab00a20SMathieu Malaterre #endif
1439eab00a20SMathieu Malaterre 	char type[64];
14409b6b563cSPaul Mackerras 	unsigned int plen;
14419b6b563cSPaul Mackerras 	cell_t *p, *endp;
1442493adffcSBenjamin Herrenschmidt 	__be32 val;
14439b6b563cSPaul Mackerras 	u32 rac, rsc;
14449b6b563cSPaul Mackerras 
14459b6b563cSPaul Mackerras 	/*
14469b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
14479b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
14489b6b563cSPaul Mackerras 	 * 2) top of memory
14499b6b563cSPaul Mackerras 	 */
1450493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1451493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1452493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1453493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1454493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1455493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1456493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1457493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
14589b6b563cSPaul Mackerras 
14599b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
1460eab00a20SMathieu Malaterre #ifdef DEBUG_PROM
14615827d416SAnton Blanchard 	path = prom_scratch;
1462eab00a20SMathieu Malaterre #endif
14639b6b563cSPaul Mackerras 
14649b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
14659b6b563cSPaul Mackerras 		type[0] = 0;
14669b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
14679b6b563cSPaul Mackerras 
1468c4988820SPaul Mackerras 		if (type[0] == 0) {
1469c4988820SPaul Mackerras 			/*
1470c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1471c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1472c4988820SPaul Mackerras 			 */
1473c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1474c4988820SPaul Mackerras 		}
14755827d416SAnton Blanchard 		if (strcmp(type, "memory"))
14769b6b563cSPaul Mackerras 			continue;
14779b6b563cSPaul Mackerras 
14785827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
14799b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
14809b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
14819b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
14829b6b563cSPaul Mackerras 		}
14835827d416SAnton Blanchard 		p = regbuf;
14849b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
14859b6b563cSPaul Mackerras 
14869b6b563cSPaul Mackerras #ifdef DEBUG_PROM
14879b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
14889b6b563cSPaul Mackerras 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
14899b6b563cSPaul Mackerras 		prom_debug("  node %s :\n", path);
14909b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
14919b6b563cSPaul Mackerras 
14929b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
14939b6b563cSPaul Mackerras 			unsigned long base, size;
14949b6b563cSPaul Mackerras 
14959b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
14969b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
14979b6b563cSPaul Mackerras 
14989b6b563cSPaul Mackerras 			if (size == 0)
14999b6b563cSPaul Mackerras 				continue;
1500eae5f709SMathieu Malaterre 			prom_debug("    %lx %lx\n", base, size);
15015827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
15025827d416SAnton Blanchard 				rmo_top = size;
15035827d416SAnton Blanchard 			if ((base + size) > ram_top)
15045827d416SAnton Blanchard 				ram_top = base + size;
15059b6b563cSPaul Mackerras 		}
15069b6b563cSPaul Mackerras 	}
15079b6b563cSPaul Mackerras 
15085827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
15099b6b563cSPaul Mackerras 
15109b6b563cSPaul Mackerras 	/*
1511cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1512cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1513cf68787bSBenjamin Krill 	 * TCE's up there.
1514cf68787bSBenjamin Krill 	 */
1515cf68787bSBenjamin Krill 
15165827d416SAnton Blanchard 	alloc_top_high = ram_top;
1517cf68787bSBenjamin Krill 
15185827d416SAnton Blanchard 	if (prom_memory_limit) {
15195827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1520eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx <= alloc_bottom.\n",
15215827d416SAnton Blanchard 				    prom_memory_limit);
15225827d416SAnton Blanchard 			prom_memory_limit = 0;
15235827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1524eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx >= ram_top.\n",
15255827d416SAnton Blanchard 				    prom_memory_limit);
15265827d416SAnton Blanchard 			prom_memory_limit = 0;
1527cf68787bSBenjamin Krill 		} else {
15285827d416SAnton Blanchard 			ram_top = prom_memory_limit;
15295827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1530cf68787bSBenjamin Krill 		}
1531cf68787bSBenjamin Krill 	}
1532cf68787bSBenjamin Krill 
1533cf68787bSBenjamin Krill 	/*
15349b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
15359b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
15369b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
15379b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
15389b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
15399b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
15409b6b563cSPaul Mackerras 	 */
15415827d416SAnton Blanchard 	if (!rmo_top)
15425827d416SAnton Blanchard 		rmo_top = ram_top;
15435827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
15445827d416SAnton Blanchard 	alloc_top = rmo_top;
15455827d416SAnton Blanchard 	alloc_top_high = ram_top;
15469b6b563cSPaul Mackerras 
154764968f60SPaul Mackerras 	/*
154864968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
154964968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
155064968f60SPaul Mackerras 	 */
15515827d416SAnton Blanchard 	if (prom_initrd_start &&
15525827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
15535827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
15545827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
155564968f60SPaul Mackerras 
15569b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
1557eae5f709SMathieu Malaterre 	prom_printf("  memory_limit : %lx (16 MB aligned)\n",
1558eae5f709SMathieu Malaterre 		    prom_memory_limit);
1559eae5f709SMathieu Malaterre 	prom_printf("  alloc_bottom : %lx\n", alloc_bottom);
1560eae5f709SMathieu Malaterre 	prom_printf("  alloc_top    : %lx\n", alloc_top);
1561eae5f709SMathieu Malaterre 	prom_printf("  alloc_top_hi : %lx\n", alloc_top_high);
1562eae5f709SMathieu Malaterre 	prom_printf("  rmo_top      : %lx\n", rmo_top);
1563eae5f709SMathieu Malaterre 	prom_printf("  ram_top      : %lx\n", ram_top);
15649b6b563cSPaul Mackerras }
15659b6b563cSPaul Mackerras 
156627f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
156727f44888SBenjamin Herrenschmidt {
1568493adffcSBenjamin Herrenschmidt 	__be32 val;
1569493adffcSBenjamin Herrenschmidt 	ihandle stdin;
157027f44888SBenjamin Herrenschmidt 
1571493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1572493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1573493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1574493adffcSBenjamin Herrenschmidt 	}
157527f44888SBenjamin Herrenschmidt }
157627f44888SBenjamin Herrenschmidt 
15779b6b563cSPaul Mackerras /*
15789b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
15799b6b563cSPaul Mackerras  */
15809b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
15819b6b563cSPaul Mackerras {
15829b6b563cSPaul Mackerras 	phandle rtas_node;
15839b6b563cSPaul Mackerras 	ihandle rtas_inst;
15849b6b563cSPaul Mackerras 	u32 base, entry = 0;
1585493adffcSBenjamin Herrenschmidt 	__be32 val;
15869b6b563cSPaul Mackerras 	u32 size = 0;
15879b6b563cSPaul Mackerras 
15889b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
15899b6b563cSPaul Mackerras 
15909b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
15919b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
15929b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
15939b6b563cSPaul Mackerras 		return;
15949b6b563cSPaul Mackerras 
1595493adffcSBenjamin Herrenschmidt 	val = 0;
1596493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1597493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
15989b6b563cSPaul Mackerras 	if (size == 0)
15999b6b563cSPaul Mackerras 		return;
16009b6b563cSPaul Mackerras 
16019b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
16026d1e2c6cSAnton Blanchard 	if (base == 0)
16036d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
16049b6b563cSPaul Mackerras 
16059b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
16069b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1607a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
16089b6b563cSPaul Mackerras 		return;
16099b6b563cSPaul Mackerras 	}
16109b6b563cSPaul Mackerras 
16119b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
16129b6b563cSPaul Mackerras 
16139b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
16149b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1615a23414beSPaul Mackerras 			  rtas_inst, base) != 0
16169b6b563cSPaul Mackerras 	    || entry == 0) {
16179b6b563cSPaul Mackerras 		prom_printf(" failed\n");
16189b6b563cSPaul Mackerras 		return;
16199b6b563cSPaul Mackerras 	}
16209b6b563cSPaul Mackerras 	prom_printf(" done\n");
16219b6b563cSPaul Mackerras 
16229b6b563cSPaul Mackerras 	reserve_mem(base, size);
16239b6b563cSPaul Mackerras 
1624493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1625a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1626493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1627493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1628a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1629493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
16309b6b563cSPaul Mackerras 
1631dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1632dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1633dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1634dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1635dbe78b40SBenjamin Herrenschmidt 
16369b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
16379b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
1638eae5f709SMathieu Malaterre 	prom_debug("rtas size     = 0x%x\n", size);
16399b6b563cSPaul Mackerras 
16409b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
16419b6b563cSPaul Mackerras }
16429b6b563cSPaul Mackerras 
16439b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
16449b6b563cSPaul Mackerras /*
16454a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
16464a727429SAshley Lai  */
16474a727429SAshley Lai static void __init prom_instantiate_sml(void)
16484a727429SAshley Lai {
16494a727429SAshley Lai 	phandle ibmvtpm_node;
16504a727429SAshley Lai 	ihandle ibmvtpm_inst;
1651b4ed0469SHon Ching \(Vicky\) Lo 	u32 entry = 0, size = 0, succ = 0;
16524a727429SAshley Lai 	u64 base;
1653b4ed0469SHon Ching \(Vicky\) Lo 	__be32 val;
16544a727429SAshley Lai 
16554a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
16564a727429SAshley Lai 
16572f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
16584a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
16594a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
16604a727429SAshley Lai 		return;
16614a727429SAshley Lai 
16622f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
16634a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
16644a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
16654a727429SAshley Lai 		return;
16664a727429SAshley Lai 	}
16674a727429SAshley Lai 
1668b4ed0469SHon Ching \(Vicky\) Lo 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1669b4ed0469SHon Ching \(Vicky\) Lo 			 &val, sizeof(val)) != PROM_ERROR) {
1670b4ed0469SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &succ,
1671b4ed0469SHon Ching \(Vicky\) Lo 				  ADDR("reformat-sml-to-efi-alignment"),
1672b4ed0469SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || succ == 0) {
1673b4ed0469SHon Ching \(Vicky\) Lo 			prom_printf("Reformat SML to EFI alignment failed\n");
1674b4ed0469SHon Ching \(Vicky\) Lo 			return;
1675b4ed0469SHon Ching \(Vicky\) Lo 		}
1676b4ed0469SHon Ching \(Vicky\) Lo 
16774a727429SAshley Lai 		if (call_prom_ret("call-method", 2, 2, &size,
16789e5d4af4SHon Ching \(Vicky\) Lo 				  ADDR("sml-get-allocated-size"),
16799e5d4af4SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || size == 0) {
16809e5d4af4SHon Ching \(Vicky\) Lo 			prom_printf("SML get allocated size failed\n");
16819e5d4af4SHon Ching \(Vicky\) Lo 			return;
16829e5d4af4SHon Ching \(Vicky\) Lo 		}
16839e5d4af4SHon Ching \(Vicky\) Lo 	} else {
16849e5d4af4SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &size,
16854a727429SAshley Lai 				  ADDR("sml-get-handover-size"),
16864a727429SAshley Lai 				  ibmvtpm_inst) != 0 || size == 0) {
16874a727429SAshley Lai 			prom_printf("SML get handover size failed\n");
16884a727429SAshley Lai 			return;
16894a727429SAshley Lai 		}
16909e5d4af4SHon Ching \(Vicky\) Lo 	}
16914a727429SAshley Lai 
16924a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
16934a727429SAshley Lai 	if (base == 0)
16944a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
16954a727429SAshley Lai 
1696eae5f709SMathieu Malaterre 	prom_printf("instantiating sml at 0x%llx...", base);
16974a727429SAshley Lai 
16989e5d4af4SHon Ching \(Vicky\) Lo 	memset((void *)base, 0, size);
16999e5d4af4SHon Ching \(Vicky\) Lo 
17004a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
17014a727429SAshley Lai 			  ADDR("sml-handover"),
17024a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
17034a727429SAshley Lai 		prom_printf("SML handover failed\n");
17044a727429SAshley Lai 		return;
17054a727429SAshley Lai 	}
17064a727429SAshley Lai 	prom_printf(" done\n");
17074a727429SAshley Lai 
17084a727429SAshley Lai 	reserve_mem(base, size);
17094a727429SAshley Lai 
17102f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
17114a727429SAshley Lai 		     &base, sizeof(base));
17122f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
17134a727429SAshley Lai 		     &size, sizeof(size));
17144a727429SAshley Lai 
1715eae5f709SMathieu Malaterre 	prom_debug("sml base     = 0x%llx\n", base);
1716eae5f709SMathieu Malaterre 	prom_debug("sml size     = 0x%x\n", size);
17174a727429SAshley Lai 
17184a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
17194a727429SAshley Lai }
17204a727429SAshley Lai 
17214a727429SAshley Lai /*
17229b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
17239b6b563cSPaul Mackerras  */
1724493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
17259b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
17269b6b563cSPaul Mackerras {
17279b6b563cSPaul Mackerras 	phandle node;
17289b6b563cSPaul Mackerras 	ihandle phb_node;
17299b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
17305827d416SAnton Blanchard 	char *path = prom_scratch;
17319b6b563cSPaul Mackerras 	u64 base, align;
17329b6b563cSPaul Mackerras 	u32 minalign, minsize;
17339b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
17349b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
17359b6b563cSPaul Mackerras 	u64 i;
17369b6b563cSPaul Mackerras 
17375827d416SAnton Blanchard 	if (prom_iommu_off)
17389b6b563cSPaul Mackerras 		return;
17399b6b563cSPaul Mackerras 
17409b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
17419b6b563cSPaul Mackerras 
17429b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
17435827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
17449b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
17459b6b563cSPaul Mackerras 
17469b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
17479b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
17489b6b563cSPaul Mackerras 		compatible[0] = 0;
17499b6b563cSPaul Mackerras 		type[0] = 0;
17509b6b563cSPaul Mackerras 		model[0] = 0;
17519b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
17529b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
17539b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
17549b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
17559b6b563cSPaul Mackerras 
17565827d416SAnton Blanchard 		if ((type[0] == 0) || (strstr(type, "pci") == NULL))
17579b6b563cSPaul Mackerras 			continue;
17589b6b563cSPaul Mackerras 
1759e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
17609b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
17615827d416SAnton Blanchard 			if ((strstr(compatible, "python") == NULL) &&
17625827d416SAnton Blanchard 			    (strstr(compatible, "Speedwagon") == NULL) &&
17635827d416SAnton Blanchard 			    (strstr(compatible, "Winnipeg") == NULL))
17649b6b563cSPaul Mackerras 				continue;
17659b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
17665827d416SAnton Blanchard 			if ((strstr(model, "ython") == NULL) &&
17675827d416SAnton Blanchard 			    (strstr(model, "peedwagon") == NULL) &&
17685827d416SAnton Blanchard 			    (strstr(model, "innipeg") == NULL))
17699b6b563cSPaul Mackerras 				continue;
17709b6b563cSPaul Mackerras 		}
17719b6b563cSPaul Mackerras 
17729b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
17739b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
17749b6b563cSPaul Mackerras 			minalign = 0;
17759b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
17769b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
17779b6b563cSPaul Mackerras 			minsize = 4UL << 20;
17789b6b563cSPaul Mackerras 
17799b6b563cSPaul Mackerras 		/*
17809b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
17819b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
17829b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
17839b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
17849b6b563cSPaul Mackerras 		 */
17859b6b563cSPaul Mackerras 		minsize = 4UL << 20;
17869b6b563cSPaul Mackerras 
17879b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
17889b6b563cSPaul Mackerras 		align = max(minalign, minsize);
17899b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
17909b6b563cSPaul Mackerras 		if (base == 0)
17919b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
17929b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
17939b6b563cSPaul Mackerras 			local_alloc_bottom = base;
17949b6b563cSPaul Mackerras 
17959b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
1796aca71ef8SLi Zefan 		memset(path, 0, PROM_SCRATCH_SIZE);
17979b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
17989b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
17999b6b563cSPaul Mackerras 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
18009b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
18019b6b563cSPaul Mackerras 		}
18029b6b563cSPaul Mackerras 
1803a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
1804a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1805a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1806a23414beSPaul Mackerras 
18079b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
18089b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
1809eae5f709SMathieu Malaterre 		prom_debug("\tbase = 0x%llx\n", base);
18109b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
18119b6b563cSPaul Mackerras 
18129b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
18139b6b563cSPaul Mackerras 		 * over the allocated size.
18149b6b563cSPaul Mackerras 		 */
18152b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
18169b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
18179b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
18189b6b563cSPaul Mackerras 			tce_entry |= 0x3;
18199b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
18209b6b563cSPaul Mackerras 		}
18219b6b563cSPaul Mackerras 
18229b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
18239b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
18249b6b563cSPaul Mackerras 		if (phb_node == 0)
18259b6b563cSPaul Mackerras 			prom_printf("... failed\n");
18269b6b563cSPaul Mackerras 		else
18279b6b563cSPaul Mackerras 			prom_printf("... done\n");
18289b6b563cSPaul Mackerras 
18299b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
18309b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
18319b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
18329b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
18339b6b563cSPaul Mackerras 	}
18349b6b563cSPaul Mackerras 
18359b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
18369b6b563cSPaul Mackerras 
18372babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
18382babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
18395827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
18405827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
18419b6b563cSPaul Mackerras 
18429b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
18439b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
18449b6b563cSPaul Mackerras }
1845493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1846493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
18479b6b563cSPaul Mackerras 
18489b6b563cSPaul Mackerras /*
18499b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
18509b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
18519b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
18529b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
18539b6b563cSPaul Mackerras  *
18549b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
18559b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
18569b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
18579b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
18589b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
18599b6b563cSPaul Mackerras  * of setting those values.
18609b6b563cSPaul Mackerras  *
18619b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
18629b6b563cSPaul Mackerras  * is in its holding pattern code.
18639b6b563cSPaul Mackerras  *
18649b6b563cSPaul Mackerras  * -- Cort
18659b6b563cSPaul Mackerras  */
1866bbd0abdaSPaul Mackerras /*
1867bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
1868bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
1869bbd0abdaSPaul Mackerras  */
1870bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1871bbd0abdaSPaul Mackerras 
18729b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
18739b6b563cSPaul Mackerras {
18749b6b563cSPaul Mackerras 	unsigned long i;
18759b6b563cSPaul Mackerras 	phandle node;
18769b6b563cSPaul Mackerras 	char type[64];
18779b6b563cSPaul Mackerras 	unsigned long *spinloop
1878bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
18799b6b563cSPaul Mackerras 	unsigned long *acknowledge
1880bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1881bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
18829b6b563cSPaul Mackerras 
1883dbe78b40SBenjamin Herrenschmidt 	/*
1884dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
1885dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
1886dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
1887dbe78b40SBenjamin Herrenschmidt 	 */
1888dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
1889dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
1890dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
1891dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
1892dbe78b40SBenjamin Herrenschmidt 		return;
1893dbe78b40SBenjamin Herrenschmidt 	}
1894dbe78b40SBenjamin Herrenschmidt 
18959b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
1896eae5f709SMathieu Malaterre 	prom_debug("    1) spinloop       = 0x%lx\n", (unsigned long)spinloop);
1897eae5f709SMathieu Malaterre 	prom_debug("    1) *spinloop      = 0x%lx\n", *spinloop);
1898eae5f709SMathieu Malaterre 	prom_debug("    1) acknowledge    = 0x%lx\n",
18999b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
1900eae5f709SMathieu Malaterre 	prom_debug("    1) *acknowledge   = 0x%lx\n", *acknowledge);
1901eae5f709SMathieu Malaterre 	prom_debug("    1) secondary_hold = 0x%lx\n", secondary_hold);
19029b6b563cSPaul Mackerras 
19039b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
19049b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
19059b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
19069b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
19079b6b563cSPaul Mackerras 	 */
19089b6b563cSPaul Mackerras 	*spinloop = 0;
19099b6b563cSPaul Mackerras 
19109b6b563cSPaul Mackerras 	/* look for cpus */
19119b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
1912493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
1913493adffcSBenjamin Herrenschmidt 		__be32 reg;
1914493adffcSBenjamin Herrenschmidt 
19159b6b563cSPaul Mackerras 		type[0] = 0;
19169b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
19175827d416SAnton Blanchard 		if (strcmp(type, "cpu") != 0)
19189b6b563cSPaul Mackerras 			continue;
19199b6b563cSPaul Mackerras 
19209b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
19219b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
19225827d416SAnton Blanchard 			if (strcmp(type, "okay") != 0)
19239b6b563cSPaul Mackerras 				continue;
19249b6b563cSPaul Mackerras 
1925493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
19269b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
1927493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
19289b6b563cSPaul Mackerras 
1929eae5f709SMathieu Malaterre 		prom_debug("cpu hw idx   = %u\n", cpu_no);
19309b6b563cSPaul Mackerras 
19319b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
19329b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
19339b6b563cSPaul Mackerras 		 * spinloop.
19349b6b563cSPaul Mackerras 		 */
19359b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
19369b6b563cSPaul Mackerras 
1937493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
193827f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
1939eae5f709SMathieu Malaterre 			prom_printf("starting cpu hw idx %u... ", cpu_no);
19409b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
1941493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
19429b6b563cSPaul Mackerras 
19439b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
19449b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
19459b6b563cSPaul Mackerras 				mb();
19469b6b563cSPaul Mackerras 
1947493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
19489b6b563cSPaul Mackerras 				prom_printf("done\n");
1949bbd0abdaSPaul Mackerras 			else
1950eae5f709SMathieu Malaterre 				prom_printf("failed: %lx\n", *acknowledge);
19519b6b563cSPaul Mackerras 		}
19529b6b563cSPaul Mackerras #ifdef CONFIG_SMP
19539b6b563cSPaul Mackerras 		else
1954eae5f709SMathieu Malaterre 			prom_printf("boot cpu hw idx %u\n", cpu_no);
19559b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
19569b6b563cSPaul Mackerras 	}
19579b6b563cSPaul Mackerras 
19589b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
19599b6b563cSPaul Mackerras }
19609b6b563cSPaul Mackerras 
19619b6b563cSPaul Mackerras 
19629b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
19639b6b563cSPaul Mackerras {
19649b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
19655827d416SAnton Blanchard 	prom_entry = pp;
19669b6b563cSPaul Mackerras 
19679b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
19685827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
19695827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
19709b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
19719b6b563cSPaul Mackerras 
19729b6b563cSPaul Mackerras 	/* get device tree root */
19735827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
19745827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
19759b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1976a575b807SPaul Mackerras 
19775827d416SAnton Blanchard 	prom.mmumap = 0;
19789b6b563cSPaul Mackerras }
19799b6b563cSPaul Mackerras 
1980a575b807SPaul Mackerras #ifdef CONFIG_PPC32
1981a575b807SPaul Mackerras /*
1982a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
1983a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
1984a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
1985a575b807SPaul Mackerras  */
1986a575b807SPaul Mackerras static void __init prom_find_mmu(void)
1987a575b807SPaul Mackerras {
1988a575b807SPaul Mackerras 	phandle oprom;
1989a575b807SPaul Mackerras 	char version[64];
1990a575b807SPaul Mackerras 
1991a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1992a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
1993a575b807SPaul Mackerras 		return;
1994a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1995a575b807SPaul Mackerras 		return;
1996a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
1997a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
1998a23414beSPaul Mackerras 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1999a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
2000a23414beSPaul Mackerras 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
2001a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
2002a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2003a23414beSPaul Mackerras 	} else
2004a575b807SPaul Mackerras 		return;
20055827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
20065827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
20075827d416SAnton Blanchard 		     sizeof(prom.mmumap));
2008493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
20095827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2010a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
2011a575b807SPaul Mackerras }
2012a575b807SPaul Mackerras #else
2013a575b807SPaul Mackerras #define prom_find_mmu()
2014a575b807SPaul Mackerras #endif
2015a575b807SPaul Mackerras 
20169b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
20179b6b563cSPaul Mackerras {
20185827d416SAnton Blanchard 	char *path = of_stdout_device;
20199b6b563cSPaul Mackerras 	char type[16];
2020493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
2021493adffcSBenjamin Herrenschmidt 	__be32 val;
20229b6b563cSPaul Mackerras 
20235827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
20249b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
20259b6b563cSPaul Mackerras 
2026493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
20279b6b563cSPaul Mackerras 
20289b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
20299b6b563cSPaul Mackerras 	memset(path, 0, 256);
20305827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
20315827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
20325827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2033a23414beSPaul Mackerras 		     path, strlen(path) + 1);
20349b6b563cSPaul Mackerras 
203510348f59SBenjamin Herrenschmidt 	/* instance-to-package fails on PA-Semi */
203610348f59SBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
203710348f59SBenjamin Herrenschmidt 	if (stdout_node != PROM_ERROR) {
203810348f59SBenjamin Herrenschmidt 		val = cpu_to_be32(stdout_node);
203910348f59SBenjamin Herrenschmidt 
20409b6b563cSPaul Mackerras 		/* If it's a display, note it */
20419b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
2042493adffcSBenjamin Herrenschmidt 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
20435827d416SAnton Blanchard 		if (strcmp(type, "display") == 0)
2044493adffcSBenjamin Herrenschmidt 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
20459b6b563cSPaul Mackerras 	}
204610348f59SBenjamin Herrenschmidt }
20479b6b563cSPaul Mackerras 
20489b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
20499b6b563cSPaul Mackerras {
20509b6b563cSPaul Mackerras 	char compat[256];
20519b6b563cSPaul Mackerras 	int len, i = 0;
205221fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
20539b6b563cSPaul Mackerras 	phandle rtas;
2054e8222502SBenjamin Herrenschmidt 	int x;
205521fe3301SBenjamin Herrenschmidt #endif
2056e8222502SBenjamin Herrenschmidt 
20576e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
20585827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
20599b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
20609b6b563cSPaul Mackerras 	if (len > 0) {
20619b6b563cSPaul Mackerras 		compat[len] = 0;
20629b6b563cSPaul Mackerras 		while (i < len) {
20639b6b563cSPaul Mackerras 			char *p = &compat[i];
20649b6b563cSPaul Mackerras 			int sl = strlen(p);
20659b6b563cSPaul Mackerras 			if (sl == 0)
20669b6b563cSPaul Mackerras 				break;
20675827d416SAnton Blanchard 			if (strstr(p, "Power Macintosh") ||
20685827d416SAnton Blanchard 			    strstr(p, "MacRISC"))
20699b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
2070133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
2071133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
2072133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
2073133dda1eSArnd Bergmann 			 * so we do it here.
2074133dda1eSArnd Bergmann 			 */
20755827d416SAnton Blanchard 			if (strstr(p, "IBM,CBEA") ||
20765827d416SAnton Blanchard 			    strstr(p, "IBM,CPBW-1.0"))
2077133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
2078133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
20799b6b563cSPaul Mackerras 			i += sl + 1;
20809b6b563cSPaul Mackerras 		}
20819b6b563cSPaul Mackerras 	}
20829b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
20836e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
2084e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
2085e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
2086e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
2087e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
2088e8222502SBenjamin Herrenschmidt 	 */
20895827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
2090e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
2091e8222502SBenjamin Herrenschmidt 	if (len <= 0)
2092e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
20935827d416SAnton Blanchard 	if (strcmp(compat, "chrp"))
2094e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2095e8222502SBenjamin Herrenschmidt 
20969b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
20979b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2098e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
2099e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2100e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
21019b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
21024da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
21039b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
21049b6b563cSPaul Mackerras 	}
21059b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
21069b6b563cSPaul Mackerras #else
2107e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
21089b6b563cSPaul Mackerras #endif
21099b6b563cSPaul Mackerras }
21109b6b563cSPaul Mackerras 
21119b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
21129b6b563cSPaul Mackerras {
21139b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
21149b6b563cSPaul Mackerras }
21159b6b563cSPaul Mackerras 
21169b6b563cSPaul Mackerras /*
21179b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
21189b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
21199b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
21209b6b563cSPaul Mackerras  * we've taken over the MMU.
21219b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
21229b6b563cSPaul Mackerras  * and if so, open it now.
21239b6b563cSPaul Mackerras  */
21249b6b563cSPaul Mackerras static void __init prom_check_displays(void)
21259b6b563cSPaul Mackerras {
21269b6b563cSPaul Mackerras 	char type[16], *path;
21279b6b563cSPaul Mackerras 	phandle node;
21289b6b563cSPaul Mackerras 	ihandle ih;
21299b6b563cSPaul Mackerras 	int i;
21309b6b563cSPaul Mackerras 
2131d00e34b9SBenjamin Herrenschmidt 	static const unsigned char default_colors[] __initconst = {
21329b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
21339b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
21349b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
21359b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
21369b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
21379b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
21389b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
21399b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
21409b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
21419b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
21429b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
21439b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
21449b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
21459b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
21469b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
21479b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
21489b6b563cSPaul Mackerras 	};
21499b6b563cSPaul Mackerras 	const unsigned char *clut;
21509b6b563cSPaul Mackerras 
21514da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
21529b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
21539b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
21549b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
21555827d416SAnton Blanchard 		if (strcmp(type, "display") != 0)
21569b6b563cSPaul Mackerras 			continue;
21579b6b563cSPaul Mackerras 
21589b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
21595827d416SAnton Blanchard 		path = prom_scratch;
21609b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
21619b6b563cSPaul Mackerras 
21629b6b563cSPaul Mackerras 		/*
21639b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
21649b6b563cSPaul Mackerras 		 * arguments
21659b6b563cSPaul Mackerras 		 */
21669b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
21679b6b563cSPaul Mackerras 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
21689b6b563cSPaul Mackerras 			continue;
21699b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
21709b6b563cSPaul Mackerras 
21719b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
21729b6b563cSPaul Mackerras 		if (ih == 0) {
21739b6b563cSPaul Mackerras 			prom_printf("failed\n");
21749b6b563cSPaul Mackerras 			continue;
21759b6b563cSPaul Mackerras 		}
21769b6b563cSPaul Mackerras 
21779b6b563cSPaul Mackerras 		/* Success */
21789b6b563cSPaul Mackerras 		prom_printf("done\n");
2179a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
21809b6b563cSPaul Mackerras 
21819b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
21829b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
21835827d416SAnton Blanchard 		clut = default_colors;
21843f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
21859b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
21869b6b563cSPaul Mackerras 					   clut[2]) != 0)
21879b6b563cSPaul Mackerras 				break;
21889b6b563cSPaul Mackerras 
21899b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
21905827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
21915827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
21929b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
21939b6b563cSPaul Mackerras 					   clut[2]) != 0)
21949b6b563cSPaul Mackerras 				break;
21959b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
21967191b615SBenjamin Herrenschmidt 
21977191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
21987191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
21997191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
22007191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
22017191b615SBenjamin Herrenschmidt 
22027191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
22037191b615SBenjamin Herrenschmidt 			prom_getprop(node, "width", &width, 4);
22047191b615SBenjamin Herrenschmidt 			prom_getprop(node, "height", &height, 4);
22057191b615SBenjamin Herrenschmidt 			prom_getprop(node, "linebytes", &pitch, 4);
22067191b615SBenjamin Herrenschmidt 			prom_getprop(node, "address", &addr, 4);
22077191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
22087191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
22097191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
22107191b615SBenjamin Herrenschmidt 		}
22117191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
22129b6b563cSPaul Mackerras 	}
22139b6b563cSPaul Mackerras }
22149b6b563cSPaul Mackerras 
22159b6b563cSPaul Mackerras 
22169b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
22179b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
22189b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
22199b6b563cSPaul Mackerras {
22209b6b563cSPaul Mackerras 	void *ret;
22219b6b563cSPaul Mackerras 
22229b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
22239b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
22249b6b563cSPaul Mackerras 		unsigned long room, chunk;
22259b6b563cSPaul Mackerras 
2226eae5f709SMathieu Malaterre 		prom_debug("Chunk exhausted, claiming more at %lx...\n",
22275827d416SAnton Blanchard 			   alloc_bottom);
22285827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
22299b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
22309b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
22319b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2232fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2233fbafd728SAnton Blanchard 				   "(no room)\n");
22349b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
22359b6b563cSPaul Mackerras 		if (chunk == 0)
2236fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2237fbafd728SAnton Blanchard 				   "(claim failed)\n");
2238966728ddSAnton Blanchard 		*mem_end = chunk + room;
22399b6b563cSPaul Mackerras 	}
22409b6b563cSPaul Mackerras 
22419b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
22429b6b563cSPaul Mackerras 	*mem_start += needed;
22439b6b563cSPaul Mackerras 
22449b6b563cSPaul Mackerras 	return ret;
22459b6b563cSPaul Mackerras }
22469b6b563cSPaul Mackerras 
2247493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2248493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2249493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2250493adffcSBenjamin Herrenschmidt 	} while(0)
22519b6b563cSPaul Mackerras 
22529b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
22539b6b563cSPaul Mackerras {
22549b6b563cSPaul Mackerras 	char *s, *os;
22559b6b563cSPaul Mackerras 
22565827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
22579b6b563cSPaul Mackerras 	s += 4;
22585827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
22599b6b563cSPaul Mackerras 		if (strcmp(s, str) == 0)
22609b6b563cSPaul Mackerras 			return s - os;
22619b6b563cSPaul Mackerras 		s += strlen(s) + 1;
22629b6b563cSPaul Mackerras 	}
22639b6b563cSPaul Mackerras 	return 0;
22649b6b563cSPaul Mackerras }
22659b6b563cSPaul Mackerras 
22669b6b563cSPaul Mackerras /*
22679b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
22689b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
22699b6b563cSPaul Mackerras  */
22709b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
22719b6b563cSPaul Mackerras 
22729b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
22739b6b563cSPaul Mackerras 					 unsigned long *mem_start,
22749b6b563cSPaul Mackerras 					 unsigned long *mem_end)
22759b6b563cSPaul Mackerras {
22769b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
22779b6b563cSPaul Mackerras 	unsigned long soff;
22789b6b563cSPaul Mackerras 	phandle child;
22799b6b563cSPaul Mackerras 
22805827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
22819b6b563cSPaul Mackerras 
22829b6b563cSPaul Mackerras 	/* get and store all property names */
22835827d416SAnton Blanchard 	prev_name = "";
22849b6b563cSPaul Mackerras 	for (;;) {
22859b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
22869b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
22879b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
22889b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
22899b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
22909b6b563cSPaul Mackerras 			break;
22919b6b563cSPaul Mackerras 		}
22929b6b563cSPaul Mackerras 
22939b6b563cSPaul Mackerras  		/* skip "name" */
22945827d416SAnton Blanchard  		if (strcmp(namep, "name") == 0) {
22959b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
22965827d416SAnton Blanchard  			prev_name = "name";
22979b6b563cSPaul Mackerras  			continue;
22989b6b563cSPaul Mackerras  		}
22999b6b563cSPaul Mackerras 		/* get/create string entry */
23009b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
23019b6b563cSPaul Mackerras 		if (soff != 0) {
23029b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
23039b6b563cSPaul Mackerras 			namep = sstart + soff;
23049b6b563cSPaul Mackerras 		} else {
23059b6b563cSPaul Mackerras 			/* Trim off some if we can */
23069b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
23075827d416SAnton Blanchard 			dt_string_end = *mem_start;
23089b6b563cSPaul Mackerras 		}
23099b6b563cSPaul Mackerras 		prev_name = namep;
23109b6b563cSPaul Mackerras 	}
23119b6b563cSPaul Mackerras 
23129b6b563cSPaul Mackerras 	/* do all our children */
23139b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
23149b6b563cSPaul Mackerras 	while (child != 0) {
23159b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
23169b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
23179b6b563cSPaul Mackerras 	}
23189b6b563cSPaul Mackerras }
23199b6b563cSPaul Mackerras 
23209b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
23219b6b563cSPaul Mackerras 					unsigned long *mem_end)
23229b6b563cSPaul Mackerras {
23239b6b563cSPaul Mackerras 	phandle child;
23249b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
23259b6b563cSPaul Mackerras 	unsigned long soff;
23269b6b563cSPaul Mackerras 	unsigned char *valp;
23278ca2d515SBenjamin Herrenschmidt 	static char pname[MAX_PROPERTY_NAME] __prombss;
23286e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
23299b6b563cSPaul Mackerras 
23309b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
23319b6b563cSPaul Mackerras 
23329b6b563cSPaul Mackerras 	/* get the node's full name */
23339b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2334c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2335c4988820SPaul Mackerras 	if (room > 255)
2336c4988820SPaul Mackerras 		room = 255;
2337c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
23389b6b563cSPaul Mackerras 	if (l >= 0) {
23399b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2340c4988820SPaul Mackerras 		if (l >= room) {
2341c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
23429b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
23439b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
23449b6b563cSPaul Mackerras 		}
23459b6b563cSPaul Mackerras 		namep[l] = '\0';
23469b6b563cSPaul Mackerras 
23479b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2348a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2349a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
23509b6b563cSPaul Mackerras 		 */
2351a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
23529b6b563cSPaul Mackerras 			if (*p == '/')
2353a575b807SPaul Mackerras 				lp = namep;
2354a575b807SPaul Mackerras 			else if (*p != 0)
2355a575b807SPaul Mackerras 				*lp++ = *p;
2356a575b807SPaul Mackerras 		}
2357a575b807SPaul Mackerras 		*lp = 0;
2358a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
23599b6b563cSPaul Mackerras 	}
23609b6b563cSPaul Mackerras 
23619b6b563cSPaul Mackerras 	/* get it again for debugging */
23625827d416SAnton Blanchard 	path = prom_scratch;
23639b6b563cSPaul Mackerras 	memset(path, 0, PROM_SCRATCH_SIZE);
23649b6b563cSPaul Mackerras 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
23659b6b563cSPaul Mackerras 
23669b6b563cSPaul Mackerras 	/* get and store all properties */
23675827d416SAnton Blanchard 	prev_name = "";
23685827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
23699b6b563cSPaul Mackerras 	for (;;) {
23709b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
23715827d416SAnton Blanchard 			      pname) != 1)
23729b6b563cSPaul Mackerras 			break;
23739b6b563cSPaul Mackerras 
23749b6b563cSPaul Mackerras  		/* skip "name" */
23755827d416SAnton Blanchard  		if (strcmp(pname, "name") == 0) {
23765827d416SAnton Blanchard  			prev_name = "name";
23779b6b563cSPaul Mackerras  			continue;
23789b6b563cSPaul Mackerras  		}
23799b6b563cSPaul Mackerras 
23809b6b563cSPaul Mackerras 		/* find string offset */
23815827d416SAnton Blanchard 		soff = dt_find_string(pname);
23829b6b563cSPaul Mackerras 		if (soff == 0) {
23839b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
23845827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
23859b6b563cSPaul Mackerras 			break;
23869b6b563cSPaul Mackerras 		}
23879b6b563cSPaul Mackerras 		prev_name = sstart + soff;
23889b6b563cSPaul Mackerras 
23899b6b563cSPaul Mackerras 		/* get length */
23905827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
23919b6b563cSPaul Mackerras 
23929b6b563cSPaul Mackerras 		/* sanity checks */
23939b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
23949b6b563cSPaul Mackerras 			continue;
23959b6b563cSPaul Mackerras 
23969b6b563cSPaul Mackerras 		/* push property head */
23979b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
23989b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
23999b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
24009b6b563cSPaul Mackerras 
24019b6b563cSPaul Mackerras 		/* push property content */
24029b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
24035827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
24049b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
24056e35d5daSBenjamin Herrenschmidt 
24065827d416SAnton Blanchard 		if (!strcmp(pname, "phandle"))
24076e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
24089b6b563cSPaul Mackerras 	}
24099b6b563cSPaul Mackerras 
2410f1f208e5SBenjamin Herrenschmidt 	/* Add a "phandle" property if none already exist */
24116e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
2412f1f208e5SBenjamin Herrenschmidt 		soff = dt_find_string("phandle");
24139b6b563cSPaul Mackerras 		if (soff == 0)
2414f1f208e5SBenjamin Herrenschmidt 			prom_printf("WARNING: Can't find string index for <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 
2474f1f208e5SBenjamin Herrenschmidt 	/* Add "phandle" in there, we'll need it */
24759b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
2476f1f208e5SBenjamin Herrenschmidt 	strcpy(namep, "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