xref: /openbmc/linux/arch/powerpc/kernel/prom_init.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29b6b563cSPaul Mackerras /*
39b6b563cSPaul Mackerras  * Procedures for interfacing to Open Firmware.
49b6b563cSPaul Mackerras  *
59b6b563cSPaul Mackerras  * Paul Mackerras	August 1996.
69b6b563cSPaul Mackerras  * Copyright (C) 1996-2005 Paul Mackerras.
79b6b563cSPaul Mackerras  *
89b6b563cSPaul Mackerras  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
99b6b563cSPaul Mackerras  *    {engebret|bergner}@us.ibm.com
109b6b563cSPaul Mackerras  */
119b6b563cSPaul Mackerras 
129b6b563cSPaul Mackerras #undef DEBUG_PROM
139b6b563cSPaul Mackerras 
14054f367aSDaniel Axtens /* we cannot use FORTIFY as it brings in new symbols */
15054f367aSDaniel Axtens #define __NO_FORTIFY
16054f367aSDaniel Axtens 
17c0891ac1SAlexey Dobriyan #include <linux/stdarg.h>
189b6b563cSPaul Mackerras #include <linux/kernel.h>
199b6b563cSPaul Mackerras #include <linux/string.h>
209b6b563cSPaul Mackerras #include <linux/init.h>
219b6b563cSPaul Mackerras #include <linux/threads.h>
229b6b563cSPaul Mackerras #include <linux/spinlock.h>
239b6b563cSPaul Mackerras #include <linux/types.h>
249b6b563cSPaul Mackerras #include <linux/pci.h>
259b6b563cSPaul Mackerras #include <linux/proc_fs.h>
269b6b563cSPaul Mackerras #include <linux/delay.h>
279b6b563cSPaul Mackerras #include <linux/initrd.h>
289b6b563cSPaul Mackerras #include <linux/bitops.h>
2965fddcfcSMike Rapoport #include <linux/pgtable.h>
30ffaacd97SMichael Ellerman #include <linux/printk.h>
31e6f6390aSChristophe Leroy #include <linux/of.h>
32e6f6390aSChristophe Leroy #include <linux/of_fdt.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>
3759dc5bfcSNicholas Piggin #include <asm/interrupt.h>
389b6b563cSPaul Mackerras #include <asm/irq.h>
399b6b563cSPaul Mackerras #include <asm/io.h>
409b6b563cSPaul Mackerras #include <asm/smp.h>
419b6b563cSPaul Mackerras #include <asm/mmu.h>
429b6b563cSPaul Mackerras #include <asm/iommu.h>
439b6b563cSPaul Mackerras #include <asm/btext.h>
449b6b563cSPaul Mackerras #include <asm/sections.h>
45113fe88eSChristophe Leroy #include <asm/setup.h>
460545d543SDaniel Axtens #include <asm/asm-prototypes.h>
476a9c930bSRam Pai #include <asm/ultravisor-api.h>
489b6b563cSPaul Mackerras 
499b6b563cSPaul Mackerras #include <linux/linux_logo.h>
509b6b563cSPaul Mackerras 
51e63334e5SBenjamin Herrenschmidt /* All of prom_init bss lives here */
5233def849SJoe Perches #define __prombss __section(".bss.prominit")
53e63334e5SBenjamin Herrenschmidt 
549b6b563cSPaul Mackerras /*
559b6b563cSPaul Mackerras  * Eventually bump that one up
569b6b563cSPaul Mackerras  */
579b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE	0x100000
589b6b563cSPaul Mackerras 
599b6b563cSPaul Mackerras /*
609b6b563cSPaul Mackerras  * This is the size of the local memory reserve map that gets copied
619b6b563cSPaul Mackerras  * into the boot params passed to the kernel. That size is totally
629b6b563cSPaul Mackerras  * flexible as the kernel just reads the list until it encounters an
639b6b563cSPaul Mackerras  * entry with size 0, so it can be changed without breaking binary
649b6b563cSPaul Mackerras  * compatibility
659b6b563cSPaul Mackerras  */
669b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE	8
679b6b563cSPaul Mackerras 
689b6b563cSPaul Mackerras /*
699b6b563cSPaul Mackerras  * prom_init() is called very early on, before the kernel text
709b6b563cSPaul Mackerras  * and data have been mapped to KERNELBASE.  At this point the code
719b6b563cSPaul Mackerras  * is running at whatever address it has been loaded at.
729b6b563cSPaul Mackerras  * On ppc32 we compile with -mrelocatable, which means that references
739b6b563cSPaul Mackerras  * to extern and static variables get relocated automatically.
745ac47f7aSAnton Blanchard  * ppc64 objects are always relocatable, we just need to relocate the
755ac47f7aSAnton Blanchard  * TOC.
769b6b563cSPaul Mackerras  *
779b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
789b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
799b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
809b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
819b6b563cSPaul Mackerras  *
829b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
839b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
849b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
859b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
869b6b563cSPaul Mackerras  */
875ac47f7aSAnton Blanchard #define ADDR(x)		(u32)(unsigned long)(x)
885ac47f7aSAnton Blanchard 
899b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
90a23414beSPaul Mackerras #define OF_WORKAROUNDS	0
919b6b563cSPaul Mackerras #else
92a23414beSPaul Mackerras #define OF_WORKAROUNDS	of_workarounds
93e63334e5SBenjamin Herrenschmidt static int of_workarounds __prombss;
949b6b563cSPaul Mackerras #endif
959b6b563cSPaul Mackerras 
96a23414beSPaul Mackerras #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
97a23414beSPaul Mackerras #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
98a23414beSPaul Mackerras 
999b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1009b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1019b6b563cSPaul Mackerras #else
10285aa4b98SMathieu Malaterre #define prom_debug(x...)	do { } while (0)
1039b6b563cSPaul Mackerras #endif
1049b6b563cSPaul Mackerras 
1059b6b563cSPaul Mackerras 
1069b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1079b6b563cSPaul Mackerras 
1089b6b563cSPaul Mackerras struct prom_args {
109493adffcSBenjamin Herrenschmidt         __be32 service;
110493adffcSBenjamin Herrenschmidt         __be32 nargs;
111493adffcSBenjamin Herrenschmidt         __be32 nret;
112493adffcSBenjamin Herrenschmidt         __be32 args[10];
1139b6b563cSPaul Mackerras };
1149b6b563cSPaul Mackerras 
1159b6b563cSPaul Mackerras struct prom_t {
1169b6b563cSPaul Mackerras 	ihandle root;
117a23414beSPaul Mackerras 	phandle chosen;
1189b6b563cSPaul Mackerras 	int cpu;
1199b6b563cSPaul Mackerras 	ihandle stdout;
120a575b807SPaul Mackerras 	ihandle mmumap;
121a23414beSPaul Mackerras 	ihandle memory;
1229b6b563cSPaul Mackerras };
1239b6b563cSPaul Mackerras 
1249b6b563cSPaul Mackerras struct mem_map_entry {
125493adffcSBenjamin Herrenschmidt 	__be64	base;
126493adffcSBenjamin Herrenschmidt 	__be64	size;
1279b6b563cSPaul Mackerras };
1289b6b563cSPaul Mackerras 
129493adffcSBenjamin Herrenschmidt typedef __be32 cell_t;
1309b6b563cSPaul Mackerras 
1316e35d5daSBenjamin Herrenschmidt extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
1326e35d5daSBenjamin Herrenschmidt 		    unsigned long r6, unsigned long r7, unsigned long r8,
1336e35d5daSBenjamin Herrenschmidt 		    unsigned long r9);
1349b6b563cSPaul Mackerras 
1359b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
136c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1379b6b563cSPaul Mackerras #else
enter_prom(struct prom_args * args,unsigned long entry)138c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1399b6b563cSPaul Mackerras {
140c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1419b6b563cSPaul Mackerras }
1429b6b563cSPaul Mackerras #endif
1439b6b563cSPaul Mackerras 
1449b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1459b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1469b6b563cSPaul Mackerras 
1479b6b563cSPaul Mackerras /* prom structure */
148e63334e5SBenjamin Herrenschmidt static struct prom_t __prombss prom;
1499b6b563cSPaul Mackerras 
150e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_entry;
1519b6b563cSPaul Mackerras 
152e63334e5SBenjamin Herrenschmidt static char __prombss of_stdout_device[256];
153d7fbe2a0SChristophe Leroy static char __prombss prom_scratch[256];
1549b6b563cSPaul Mackerras 
155e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_header_start;
156e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_struct_start, dt_struct_end;
157e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_string_start, dt_string_end;
1589b6b563cSPaul Mackerras 
159e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_initrd_start, prom_initrd_end;
1609b6b563cSPaul Mackerras 
1619b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
162e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_force_on;
163e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_off;
164e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_start;
165e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_end;
1669b6b563cSPaul Mackerras #endif
1679b6b563cSPaul Mackerras 
168c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
1698ca2d515SBenjamin Herrenschmidt static bool __prombss prom_radix_disable;
170bf6b7661SAneesh Kumar K.V static bool __prombss prom_radix_gtse_disable;
171a3bf9fbdSGreg Kurz static bool __prombss prom_xive_disable;
172c886087cSBenjamin Herrenschmidt #endif
173014d02cbSSuraj Jitindar Singh 
1746a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
1756a9c930bSRam Pai static bool __prombss prom_svm_enable;
1766a9c930bSRam Pai #endif
1776a9c930bSRam Pai 
178014d02cbSSuraj Jitindar Singh struct platform_support {
179014d02cbSSuraj Jitindar Singh 	bool hash_mmu;
180014d02cbSSuraj Jitindar Singh 	bool radix_mmu;
181014d02cbSSuraj Jitindar Singh 	bool radix_gtse;
182ac5e5a54SCédric Le Goater 	bool xive;
183014d02cbSSuraj Jitindar Singh };
184014d02cbSSuraj Jitindar Singh 
185e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
186e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
187e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
188e8222502SBenjamin Herrenschmidt  */
189e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
190e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
191e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
192e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
193e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
194e8222502SBenjamin Herrenschmidt 
195e63334e5SBenjamin Herrenschmidt static int __prombss of_platform;
1969b6b563cSPaul Mackerras 
197e63334e5SBenjamin Herrenschmidt static char __prombss prom_cmd_line[COMMAND_LINE_SIZE];
1989b6b563cSPaul Mackerras 
199e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_memory_limit;
200cf68787bSBenjamin Krill 
201e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top;
202e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top_high;
203e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_bottom;
204e63334e5SBenjamin Herrenschmidt static unsigned long __prombss rmo_top;
205e63334e5SBenjamin Herrenschmidt static unsigned long __prombss ram_top;
2069b6b563cSPaul Mackerras 
207e63334e5SBenjamin Herrenschmidt static struct mem_map_entry __prombss mem_reserve_map[MEM_RESERVE_MAP_SIZE];
208e63334e5SBenjamin Herrenschmidt static int __prombss mem_reserve_cnt;
2099b6b563cSPaul Mackerras 
210e63334e5SBenjamin Herrenschmidt static cell_t __prombss regbuf[1024];
2119b6b563cSPaul Mackerras 
2128ca2d515SBenjamin Herrenschmidt static bool  __prombss rtas_has_query_cpu_stopped;
213dbe78b40SBenjamin Herrenschmidt 
2149b6b563cSPaul Mackerras 
2159b6b563cSPaul Mackerras /*
2169b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2179b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2189b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2199b6b563cSPaul Mackerras  * it is valid
2209b6b563cSPaul Mackerras  */
2219b6b563cSPaul Mackerras 
2229b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2239b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2249b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2259b6b563cSPaul Mackerras 
226450e7dd4SChristophe Leroy /* Copied from lib/string.c and lib/kstrtox.c */
227450e7dd4SChristophe Leroy 
prom_strcmp(const char * cs,const char * ct)228450e7dd4SChristophe Leroy static int __init prom_strcmp(const char *cs, const char *ct)
229450e7dd4SChristophe Leroy {
230450e7dd4SChristophe Leroy 	unsigned char c1, c2;
231450e7dd4SChristophe Leroy 
232450e7dd4SChristophe Leroy 	while (1) {
233450e7dd4SChristophe Leroy 		c1 = *cs++;
234450e7dd4SChristophe Leroy 		c2 = *ct++;
235450e7dd4SChristophe Leroy 		if (c1 != c2)
236450e7dd4SChristophe Leroy 			return c1 < c2 ? -1 : 1;
237450e7dd4SChristophe Leroy 		if (!c1)
238450e7dd4SChristophe Leroy 			break;
239450e7dd4SChristophe Leroy 	}
240450e7dd4SChristophe Leroy 	return 0;
241450e7dd4SChristophe Leroy }
242450e7dd4SChristophe Leroy 
prom_strscpy_pad(char * dest,const char * src,size_t n)243f47d5a4fSMichael Ellerman static ssize_t __init prom_strscpy_pad(char *dest, const char *src, size_t n)
244450e7dd4SChristophe Leroy {
245f47d5a4fSMichael Ellerman 	ssize_t rc;
246f47d5a4fSMichael Ellerman 	size_t i;
247450e7dd4SChristophe Leroy 
248f47d5a4fSMichael Ellerman 	if (n == 0 || n > INT_MAX)
249f47d5a4fSMichael Ellerman 		return -E2BIG;
250f47d5a4fSMichael Ellerman 
251f47d5a4fSMichael Ellerman 	// Copy up to n bytes
252f47d5a4fSMichael Ellerman 	for (i = 0; i < n && src[i] != '\0'; i++)
253f47d5a4fSMichael Ellerman 		dest[i] = src[i];
254f47d5a4fSMichael Ellerman 
255f47d5a4fSMichael Ellerman 	rc = i;
256f47d5a4fSMichael Ellerman 
257f47d5a4fSMichael Ellerman 	// If we copied all n then we have run out of space for the nul
258f47d5a4fSMichael Ellerman 	if (rc == n) {
259f47d5a4fSMichael Ellerman 		// Rewind by one character to ensure nul termination
260f47d5a4fSMichael Ellerman 		i--;
261f47d5a4fSMichael Ellerman 		rc = -E2BIG;
262f47d5a4fSMichael Ellerman 	}
263f47d5a4fSMichael Ellerman 
264f47d5a4fSMichael Ellerman 	for (; i < n; i++)
265f47d5a4fSMichael Ellerman 		dest[i] = '\0';
266f47d5a4fSMichael Ellerman 
267f47d5a4fSMichael Ellerman 	return rc;
268450e7dd4SChristophe Leroy }
269450e7dd4SChristophe Leroy 
prom_strncmp(const char * cs,const char * ct,size_t count)270450e7dd4SChristophe Leroy static int __init prom_strncmp(const char *cs, const char *ct, size_t count)
271450e7dd4SChristophe Leroy {
272450e7dd4SChristophe Leroy 	unsigned char c1, c2;
273450e7dd4SChristophe Leroy 
274450e7dd4SChristophe Leroy 	while (count) {
275450e7dd4SChristophe Leroy 		c1 = *cs++;
276450e7dd4SChristophe Leroy 		c2 = *ct++;
277450e7dd4SChristophe Leroy 		if (c1 != c2)
278450e7dd4SChristophe Leroy 			return c1 < c2 ? -1 : 1;
279450e7dd4SChristophe Leroy 		if (!c1)
280450e7dd4SChristophe Leroy 			break;
281450e7dd4SChristophe Leroy 		count--;
282450e7dd4SChristophe Leroy 	}
283450e7dd4SChristophe Leroy 	return 0;
284450e7dd4SChristophe Leroy }
285450e7dd4SChristophe Leroy 
prom_strlen(const char * s)286450e7dd4SChristophe Leroy static size_t __init prom_strlen(const char *s)
287450e7dd4SChristophe Leroy {
288450e7dd4SChristophe Leroy 	const char *sc;
289450e7dd4SChristophe Leroy 
290450e7dd4SChristophe Leroy 	for (sc = s; *sc != '\0'; ++sc)
291450e7dd4SChristophe Leroy 		/* nothing */;
292450e7dd4SChristophe Leroy 	return sc - s;
293450e7dd4SChristophe Leroy }
294450e7dd4SChristophe Leroy 
prom_memcmp(const void * cs,const void * ct,size_t count)295450e7dd4SChristophe Leroy static int __init prom_memcmp(const void *cs, const void *ct, size_t count)
296450e7dd4SChristophe Leroy {
297450e7dd4SChristophe Leroy 	const unsigned char *su1, *su2;
298450e7dd4SChristophe Leroy 	int res = 0;
299450e7dd4SChristophe Leroy 
300450e7dd4SChristophe Leroy 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
301450e7dd4SChristophe Leroy 		if ((res = *su1 - *su2) != 0)
302450e7dd4SChristophe Leroy 			break;
303450e7dd4SChristophe Leroy 	return res;
304450e7dd4SChristophe Leroy }
305450e7dd4SChristophe Leroy 
prom_strstr(const char * s1,const char * s2)306450e7dd4SChristophe Leroy static char __init *prom_strstr(const char *s1, const char *s2)
307450e7dd4SChristophe Leroy {
308450e7dd4SChristophe Leroy 	size_t l1, l2;
309450e7dd4SChristophe Leroy 
310450e7dd4SChristophe Leroy 	l2 = prom_strlen(s2);
311450e7dd4SChristophe Leroy 	if (!l2)
312450e7dd4SChristophe Leroy 		return (char *)s1;
313450e7dd4SChristophe Leroy 	l1 = prom_strlen(s1);
314450e7dd4SChristophe Leroy 	while (l1 >= l2) {
315450e7dd4SChristophe Leroy 		l1--;
316450e7dd4SChristophe Leroy 		if (!prom_memcmp(s1, s2, l2))
317450e7dd4SChristophe Leroy 			return (char *)s1;
318450e7dd4SChristophe Leroy 		s1++;
319450e7dd4SChristophe Leroy 	}
320450e7dd4SChristophe Leroy 	return NULL;
321450e7dd4SChristophe Leroy }
322450e7dd4SChristophe Leroy 
prom_strlcat(char * dest,const char * src,size_t count)323d79fbb3aSChris Packham static size_t __init prom_strlcat(char *dest, const char *src, size_t count)
324450e7dd4SChristophe Leroy {
325d79fbb3aSChris Packham 	size_t dsize = prom_strlen(dest);
326d79fbb3aSChris Packham 	size_t len = prom_strlen(src);
327d79fbb3aSChris Packham 	size_t res = dsize + len;
328450e7dd4SChristophe Leroy 
329d79fbb3aSChris Packham 	/* This would be a bug */
330d79fbb3aSChris Packham 	if (dsize >= count)
331d79fbb3aSChris Packham 		return count;
332d79fbb3aSChris Packham 
333d79fbb3aSChris Packham 	dest += dsize;
334d79fbb3aSChris Packham 	count -= dsize;
335d79fbb3aSChris Packham 	if (len >= count)
336d79fbb3aSChris Packham 		len = count-1;
337450e7dd4SChristophe Leroy 	memcpy(dest, src, len);
338d79fbb3aSChris Packham 	dest[len] = 0;
339d79fbb3aSChris Packham 	return res;
340d79fbb3aSChris Packham 
341450e7dd4SChristophe Leroy }
342450e7dd4SChristophe Leroy 
343450e7dd4SChristophe Leroy #ifdef CONFIG_PPC_PSERIES
prom_strtobool(const char * s,bool * res)344450e7dd4SChristophe Leroy static int __init prom_strtobool(const char *s, bool *res)
345450e7dd4SChristophe Leroy {
346450e7dd4SChristophe Leroy 	if (!s)
347450e7dd4SChristophe Leroy 		return -EINVAL;
348450e7dd4SChristophe Leroy 
349450e7dd4SChristophe Leroy 	switch (s[0]) {
350450e7dd4SChristophe Leroy 	case 'y':
351450e7dd4SChristophe Leroy 	case 'Y':
352450e7dd4SChristophe Leroy 	case '1':
353450e7dd4SChristophe Leroy 		*res = true;
354450e7dd4SChristophe Leroy 		return 0;
355450e7dd4SChristophe Leroy 	case 'n':
356450e7dd4SChristophe Leroy 	case 'N':
357450e7dd4SChristophe Leroy 	case '0':
358450e7dd4SChristophe Leroy 		*res = false;
359450e7dd4SChristophe Leroy 		return 0;
360450e7dd4SChristophe Leroy 	case 'o':
361450e7dd4SChristophe Leroy 	case 'O':
362450e7dd4SChristophe Leroy 		switch (s[1]) {
363450e7dd4SChristophe Leroy 		case 'n':
364450e7dd4SChristophe Leroy 		case 'N':
365450e7dd4SChristophe Leroy 			*res = true;
366450e7dd4SChristophe Leroy 			return 0;
367450e7dd4SChristophe Leroy 		case 'f':
368450e7dd4SChristophe Leroy 		case 'F':
369450e7dd4SChristophe Leroy 			*res = false;
370450e7dd4SChristophe Leroy 			return 0;
371450e7dd4SChristophe Leroy 		default:
372450e7dd4SChristophe Leroy 			break;
373450e7dd4SChristophe Leroy 		}
37449a41365SNick Desaulniers 		break;
375450e7dd4SChristophe Leroy 	default:
376450e7dd4SChristophe Leroy 		break;
377450e7dd4SChristophe Leroy 	}
378450e7dd4SChristophe Leroy 
379450e7dd4SChristophe Leroy 	return -EINVAL;
380450e7dd4SChristophe Leroy }
381450e7dd4SChristophe Leroy #endif
3829b6b563cSPaul Mackerras 
3839b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
3849b6b563cSPaul Mackerras  * firmware.
3859b6b563cSPaul Mackerras  */
3869b6b563cSPaul Mackerras 
call_prom(const char * service,int nargs,int nret,...)3879b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
3889b6b563cSPaul Mackerras {
3899b6b563cSPaul Mackerras 	int i;
3909b6b563cSPaul Mackerras 	struct prom_args args;
3919b6b563cSPaul Mackerras 	va_list list;
3929b6b563cSPaul Mackerras 
393493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
394493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
395493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
3969b6b563cSPaul Mackerras 
3979b6b563cSPaul Mackerras 	va_start(list, nret);
3989b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
399493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
4009b6b563cSPaul Mackerras 	va_end(list);
4019b6b563cSPaul Mackerras 
4029b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
4039b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
4049b6b563cSPaul Mackerras 
4055827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
406c4988820SPaul Mackerras 		return PROM_ERROR;
4079b6b563cSPaul Mackerras 
408493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
4099b6b563cSPaul Mackerras }
4109b6b563cSPaul Mackerras 
call_prom_ret(const char * service,int nargs,int nret,prom_arg_t * rets,...)4119b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
4129b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
4139b6b563cSPaul Mackerras {
4149b6b563cSPaul Mackerras 	int i;
4159b6b563cSPaul Mackerras 	struct prom_args args;
4169b6b563cSPaul Mackerras 	va_list list;
4179b6b563cSPaul Mackerras 
418493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
419493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
420493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
4219b6b563cSPaul Mackerras 
4229b6b563cSPaul Mackerras 	va_start(list, rets);
4239b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
424493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
4259b6b563cSPaul Mackerras 	va_end(list);
4269b6b563cSPaul Mackerras 
4279b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
428ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
4299b6b563cSPaul Mackerras 
4305827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
431c4988820SPaul Mackerras 		return PROM_ERROR;
4329b6b563cSPaul Mackerras 
4339b6b563cSPaul Mackerras 	if (rets != NULL)
4349b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
435493adffcSBenjamin Herrenschmidt 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
4369b6b563cSPaul Mackerras 
437493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
4389b6b563cSPaul Mackerras }
4399b6b563cSPaul Mackerras 
4409b6b563cSPaul Mackerras 
prom_print(const char * msg)4419b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
4429b6b563cSPaul Mackerras {
4439b6b563cSPaul Mackerras 	const char *p, *q;
4449b6b563cSPaul Mackerras 
4455827d416SAnton Blanchard 	if (prom.stdout == 0)
4469b6b563cSPaul Mackerras 		return;
4479b6b563cSPaul Mackerras 
4489b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
4499b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
4509b6b563cSPaul Mackerras 			;
4519b6b563cSPaul Mackerras 		if (q > p)
4525827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
4539b6b563cSPaul Mackerras 		if (*q == 0)
4549b6b563cSPaul Mackerras 			break;
4559b6b563cSPaul Mackerras 		++q;
4565827d416SAnton Blanchard 		call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
4579b6b563cSPaul Mackerras 	}
4589b6b563cSPaul Mackerras }
4599b6b563cSPaul Mackerras 
4609b6b563cSPaul Mackerras 
4618af1da40SMathieu Malaterre /*
4628af1da40SMathieu Malaterre  * Both prom_print_hex & prom_print_dec takes an unsigned long as input so that
4638af1da40SMathieu Malaterre  * we do not need __udivdi3 or __umoddi3 on 32bits.
4648af1da40SMathieu Malaterre  */
prom_print_hex(unsigned long val)4659b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
4669b6b563cSPaul Mackerras {
4679b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
4689b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
4699b6b563cSPaul Mackerras 
4709b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
4719b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
4729b6b563cSPaul Mackerras 		if (buf[i] > '9')
4739b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
4749b6b563cSPaul Mackerras 		val >>= 4;
4759b6b563cSPaul Mackerras 	}
4769b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
4775827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf, nibbles);
4789b6b563cSPaul Mackerras }
4799b6b563cSPaul Mackerras 
4802c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */
4812c48a7d6SMichael Neuling #define UL_DIGITS 21
prom_print_dec(unsigned long val)4822c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val)
4832c48a7d6SMichael Neuling {
4842c48a7d6SMichael Neuling 	int i, size;
4852c48a7d6SMichael Neuling 	char buf[UL_DIGITS+1];
4862c48a7d6SMichael Neuling 
4872c48a7d6SMichael Neuling 	for (i = UL_DIGITS-1; i >= 0;  i--) {
4882c48a7d6SMichael Neuling 		buf[i] = (val % 10) + '0';
4892c48a7d6SMichael Neuling 		val = val/10;
4902c48a7d6SMichael Neuling 		if (val == 0)
4912c48a7d6SMichael Neuling 			break;
4922c48a7d6SMichael Neuling 	}
4932c48a7d6SMichael Neuling 	/* shift stuff down */
4942c48a7d6SMichael Neuling 	size = UL_DIGITS - i;
4955827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf+i, size);
4962c48a7d6SMichael Neuling }
4979b6b563cSPaul Mackerras 
498eae5f709SMathieu Malaterre __printf(1, 2)
prom_printf(const char * format,...)4999b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
5009b6b563cSPaul Mackerras {
5019b6b563cSPaul Mackerras 	const char *p, *q, *s;
5029b6b563cSPaul Mackerras 	va_list args;
5039b6b563cSPaul Mackerras 	unsigned long v;
504af277149SBenjamin Herrenschmidt 	long vs;
5058af1da40SMathieu Malaterre 	int n = 0;
5069b6b563cSPaul Mackerras 
5079b6b563cSPaul Mackerras 	va_start(args, format);
5089b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
5099b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
5109b6b563cSPaul Mackerras 			;
5119b6b563cSPaul Mackerras 		if (q > p)
5125827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
5139b6b563cSPaul Mackerras 		if (*q == 0)
5149b6b563cSPaul Mackerras 			break;
5159b6b563cSPaul Mackerras 		if (*q == '\n') {
5169b6b563cSPaul Mackerras 			++q;
5175827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout,
5189b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
5199b6b563cSPaul Mackerras 			continue;
5209b6b563cSPaul Mackerras 		}
5219b6b563cSPaul Mackerras 		++q;
5229b6b563cSPaul Mackerras 		if (*q == 0)
5239b6b563cSPaul Mackerras 			break;
5248af1da40SMathieu Malaterre 		while (*q == 'l') {
5258af1da40SMathieu Malaterre 			++q;
5268af1da40SMathieu Malaterre 			++n;
5278af1da40SMathieu Malaterre 		}
5289b6b563cSPaul Mackerras 		switch (*q) {
5299b6b563cSPaul Mackerras 		case 's':
5309b6b563cSPaul Mackerras 			++q;
5319b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
5329b6b563cSPaul Mackerras 			prom_print(s);
5339b6b563cSPaul Mackerras 			break;
5349b6b563cSPaul Mackerras 		case 'x':
5359b6b563cSPaul Mackerras 			++q;
5368af1da40SMathieu Malaterre 			switch (n) {
5378af1da40SMathieu Malaterre 			case 0:
5388af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
5398af1da40SMathieu Malaterre 				break;
5408af1da40SMathieu Malaterre 			case 1:
5419b6b563cSPaul Mackerras 				v = va_arg(args, unsigned long);
5428af1da40SMathieu Malaterre 				break;
5438af1da40SMathieu Malaterre 			case 2:
5448af1da40SMathieu Malaterre 			default:
5458af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
5468af1da40SMathieu Malaterre 				break;
5478af1da40SMathieu Malaterre 			}
5489b6b563cSPaul Mackerras 			prom_print_hex(v);
5499b6b563cSPaul Mackerras 			break;
5508af1da40SMathieu Malaterre 		case 'u':
5518af1da40SMathieu Malaterre 			++q;
5528af1da40SMathieu Malaterre 			switch (n) {
5538af1da40SMathieu Malaterre 			case 0:
5548af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
5558af1da40SMathieu Malaterre 				break;
5568af1da40SMathieu Malaterre 			case 1:
5578af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long);
5588af1da40SMathieu Malaterre 				break;
5598af1da40SMathieu Malaterre 			case 2:
5608af1da40SMathieu Malaterre 			default:
5618af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
5628af1da40SMathieu Malaterre 				break;
5638af1da40SMathieu Malaterre 			}
5648af1da40SMathieu Malaterre 			prom_print_dec(v);
5658af1da40SMathieu Malaterre 			break;
566af277149SBenjamin Herrenschmidt 		case 'd':
567af277149SBenjamin Herrenschmidt 			++q;
5688af1da40SMathieu Malaterre 			switch (n) {
5698af1da40SMathieu Malaterre 			case 0:
570af277149SBenjamin Herrenschmidt 				vs = va_arg(args, int);
571af277149SBenjamin Herrenschmidt 				break;
5728af1da40SMathieu Malaterre 			case 1:
573af277149SBenjamin Herrenschmidt 				vs = va_arg(args, long);
5748af1da40SMathieu Malaterre 				break;
5758af1da40SMathieu Malaterre 			case 2:
5768af1da40SMathieu Malaterre 			default:
5778af1da40SMathieu Malaterre 				vs = va_arg(args, long long);
5788af1da40SMathieu Malaterre 				break;
5798af1da40SMathieu Malaterre 			}
580af277149SBenjamin Herrenschmidt 			if (vs < 0) {
5815827d416SAnton Blanchard 				prom_print("-");
582af277149SBenjamin Herrenschmidt 				vs = -vs;
583af277149SBenjamin Herrenschmidt 			}
584af277149SBenjamin Herrenschmidt 			prom_print_dec(vs);
5852c48a7d6SMichael Neuling 			break;
5869b6b563cSPaul Mackerras 		}
5879b6b563cSPaul Mackerras 	}
5881b855e16SDaniel Axtens 	va_end(args);
5899b6b563cSPaul Mackerras }
5909b6b563cSPaul Mackerras 
5919b6b563cSPaul Mackerras 
prom_claim(unsigned long virt,unsigned long size,unsigned long align)592a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
593a575b807SPaul Mackerras 				unsigned long align)
594a575b807SPaul Mackerras {
595a575b807SPaul Mackerras 
596a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
597a23414beSPaul Mackerras 		/*
598a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
599a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
600a23414beSPaul Mackerras 		 */
601a23414beSPaul Mackerras 		int ret;
602a23414beSPaul Mackerras 		prom_arg_t result;
603a23414beSPaul Mackerras 
604a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
6055827d416SAnton Blanchard 				    ADDR("claim"), prom.memory,
606a23414beSPaul Mackerras 				    align, size, virt);
607a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
608a23414beSPaul Mackerras 			return -1;
609a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
6105827d416SAnton Blanchard 				    ADDR("claim"), prom.mmumap,
611a23414beSPaul Mackerras 				    align, size, virt);
612a23414beSPaul Mackerras 		if (ret != 0) {
613a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
6145827d416SAnton Blanchard 				  prom.memory, size, virt);
615a23414beSPaul Mackerras 			return -1;
616a23414beSPaul Mackerras 		}
617a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
618a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
6195827d416SAnton Blanchard 			  ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
620a23414beSPaul Mackerras 		return virt;
621a23414beSPaul Mackerras 	}
622a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
623a23414beSPaul Mackerras 			 (prom_arg_t)align);
624a575b807SPaul Mackerras }
625a575b807SPaul Mackerras 
prom_panic(const char * reason)6269b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
6279b6b563cSPaul Mackerras {
6289b6b563cSPaul Mackerras 	prom_print(reason);
629add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
630add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
6315827d416SAnton Blanchard 	if (of_platform == PLATFORM_POWERMAC)
632add60ef3SOlaf Hering 		asm("trap\n");
633add60ef3SOlaf Hering 
6341d9a4731SStephen Rothwell 	/* ToDo: should put up an SRC here on pSeries */
6359b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
6369b6b563cSPaul Mackerras 
6379b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
6389b6b563cSPaul Mackerras 		;
6399b6b563cSPaul Mackerras }
6409b6b563cSPaul Mackerras 
6419b6b563cSPaul Mackerras 
prom_next_node(phandle * nodep)6429b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
6439b6b563cSPaul Mackerras {
6449b6b563cSPaul Mackerras 	phandle node;
6459b6b563cSPaul Mackerras 
6469b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
6479b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
6489b6b563cSPaul Mackerras 		return 1;
6499b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
6509b6b563cSPaul Mackerras 		return 1;
6519b6b563cSPaul Mackerras 	for (;;) {
6529b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
6539b6b563cSPaul Mackerras 			return 0;
6549b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
6559b6b563cSPaul Mackerras 			return 1;
6569b6b563cSPaul Mackerras 	}
6579b6b563cSPaul Mackerras }
6589b6b563cSPaul Mackerras 
prom_getprop(phandle node,const char * pname,void * value,size_t valuelen)659480795a0SMasahiro Yamada static inline int __init prom_getprop(phandle node, const char *pname,
6609b6b563cSPaul Mackerras 				      void *value, size_t valuelen)
6619b6b563cSPaul Mackerras {
6629b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
6639b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
6649b6b563cSPaul Mackerras }
6659b6b563cSPaul Mackerras 
prom_getproplen(phandle node,const char * pname)666480795a0SMasahiro Yamada static inline int __init prom_getproplen(phandle node, const char *pname)
6679b6b563cSPaul Mackerras {
6689b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
6699b6b563cSPaul Mackerras }
6709b6b563cSPaul Mackerras 
add_string(char ** str,const char * q)671d276960dSNick Child static void __init add_string(char **str, const char *q)
6729b6b563cSPaul Mackerras {
673a23414beSPaul Mackerras 	char *p = *str;
674a23414beSPaul Mackerras 
675a23414beSPaul Mackerras 	while (*q)
676a23414beSPaul Mackerras 		*p++ = *q++;
677a23414beSPaul Mackerras 	*p++ = ' ';
678a23414beSPaul Mackerras 	*str = p;
679a23414beSPaul Mackerras }
680a23414beSPaul Mackerras 
tohex(unsigned int x)681d276960dSNick Child static char *__init tohex(unsigned int x)
682a23414beSPaul Mackerras {
6838ca2d515SBenjamin Herrenschmidt 	static const char digits[] __initconst = "0123456789abcdef";
6848ca2d515SBenjamin Herrenschmidt 	static char result[9] __prombss;
685a23414beSPaul Mackerras 	int i;
686a23414beSPaul Mackerras 
687a23414beSPaul Mackerras 	result[8] = 0;
688a23414beSPaul Mackerras 	i = 8;
689a23414beSPaul Mackerras 	do {
690a23414beSPaul Mackerras 		--i;
691a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
692a23414beSPaul Mackerras 		x >>= 4;
693a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
694a23414beSPaul Mackerras 	return &result[i];
695a23414beSPaul Mackerras }
696a23414beSPaul Mackerras 
prom_setprop(phandle node,const char * nodename,const char * pname,void * value,size_t valuelen)697a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
698a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
699a23414beSPaul Mackerras {
700a23414beSPaul Mackerras 	char cmd[256], *p;
701a23414beSPaul Mackerras 
702a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
7039b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
7049b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
705a23414beSPaul Mackerras 
706a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
707a23414beSPaul Mackerras 	p = cmd;
708a23414beSPaul Mackerras 	add_string(&p, "dev");
709a23414beSPaul Mackerras 	add_string(&p, nodename);
710a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
711a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
712a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
713450e7dd4SChristophe Leroy 	add_string(&p, tohex(prom_strlen(pname)));
714a23414beSPaul Mackerras 	add_string(&p, "property");
715a23414beSPaul Mackerras 	*p = 0;
716a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
7179b6b563cSPaul Mackerras }
7189b6b563cSPaul Mackerras 
7195827d416SAnton Blanchard /* We can't use the standard versions because of relocation headaches. */
7204cfdd920SAndy Shevchenko #define prom_isxdigit(c) \
7214cfdd920SAndy Shevchenko 	(('0' <= (c) && (c) <= '9') || ('a' <= (c) && (c) <= 'f') || ('A' <= (c) && (c) <= 'F'))
722cf68787bSBenjamin Krill 
7234cfdd920SAndy Shevchenko #define prom_isdigit(c)	('0' <= (c) && (c) <= '9')
7244cfdd920SAndy Shevchenko #define prom_islower(c)	('a' <= (c) && (c) <= 'z')
7254cfdd920SAndy Shevchenko #define prom_toupper(c)	(prom_islower(c) ? ((c) - 'a' + 'A') : (c))
726cf68787bSBenjamin Krill 
prom_strtoul(const char * cp,const char ** endp)727d276960dSNick Child static unsigned long __init prom_strtoul(const char *cp, const char **endp)
728cf68787bSBenjamin Krill {
729cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
730cf68787bSBenjamin Krill 
731cf68787bSBenjamin Krill 	if (*cp == '0') {
732cf68787bSBenjamin Krill 		base = 8;
733cf68787bSBenjamin Krill 		cp++;
7344cfdd920SAndy Shevchenko 		if (prom_toupper(*cp) == 'X') {
735cf68787bSBenjamin Krill 			cp++;
736cf68787bSBenjamin Krill 			base = 16;
737cf68787bSBenjamin Krill 		}
738cf68787bSBenjamin Krill 	}
739cf68787bSBenjamin Krill 
7404cfdd920SAndy Shevchenko 	while (prom_isxdigit(*cp) &&
7414cfdd920SAndy Shevchenko 	       (value = prom_isdigit(*cp) ? *cp - '0' : prom_toupper(*cp) - 'A' + 10) < base) {
742cf68787bSBenjamin Krill 		result = result * base + value;
743cf68787bSBenjamin Krill 		cp++;
744cf68787bSBenjamin Krill 	}
745cf68787bSBenjamin Krill 
746cf68787bSBenjamin Krill 	if (endp)
747cf68787bSBenjamin Krill 		*endp = cp;
748cf68787bSBenjamin Krill 
749cf68787bSBenjamin Krill 	return result;
750cf68787bSBenjamin Krill }
751cf68787bSBenjamin Krill 
prom_memparse(const char * ptr,const char ** retptr)752d276960dSNick Child static unsigned long __init prom_memparse(const char *ptr, const char **retptr)
753cf68787bSBenjamin Krill {
754cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
755cf68787bSBenjamin Krill 	int shift = 0;
756cf68787bSBenjamin Krill 
757cf68787bSBenjamin Krill 	/*
758cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
759cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
760cf68787bSBenjamin Krill 	 * the address we're linked at.
761cf68787bSBenjamin Krill 	 */
762cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
763cf68787bSBenjamin Krill 		shift = 30;
764cf68787bSBenjamin Krill 
765cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
766cf68787bSBenjamin Krill 		shift = 20;
767cf68787bSBenjamin Krill 
768cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
769cf68787bSBenjamin Krill 		shift = 10;
770cf68787bSBenjamin Krill 
771cf68787bSBenjamin Krill 	if (shift) {
772cf68787bSBenjamin Krill 		ret <<= shift;
773cf68787bSBenjamin Krill 		(*retptr)++;
774cf68787bSBenjamin Krill 	}
775cf68787bSBenjamin Krill 
776cf68787bSBenjamin Krill 	return ret;
777cf68787bSBenjamin Krill }
778cf68787bSBenjamin Krill 
7799b6b563cSPaul Mackerras /*
7809b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
7819b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
7829b6b563cSPaul Mackerras  */
early_cmdline_parse(void)7839b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
7849b6b563cSPaul Mackerras {
785cc5d0189SBenjamin Herrenschmidt 	const char *opt;
786cf68787bSBenjamin Krill 
787cc5d0189SBenjamin Herrenschmidt 	char *p;
788cbe46bd4SChristophe Leroy 	int l = 0;
7899b6b563cSPaul Mackerras 
7905827d416SAnton Blanchard 	prom_cmd_line[0] = 0;
7915827d416SAnton Blanchard 	p = prom_cmd_line;
792d79fbb3aSChris Packham 
793d79fbb3aSChris Packham 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && (long)prom.chosen > 0)
7945827d416SAnton Blanchard 		l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
795d79fbb3aSChris Packham 
796d79fbb3aSChris Packham 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) || l <= 0 || p[0] == '\0')
797d79fbb3aSChris Packham 		prom_strlcat(prom_cmd_line, " " CONFIG_CMDLINE,
798d79fbb3aSChris Packham 			     sizeof(prom_cmd_line));
799d79fbb3aSChris Packham 
8005827d416SAnton Blanchard 	prom_printf("command line: %s\n", prom_cmd_line);
8019b6b563cSPaul Mackerras 
8029b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
803450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "iommu=");
8049b6b563cSPaul Mackerras 	if (opt) {
8059b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
8069b6b563cSPaul Mackerras 		opt += 6;
8079b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
8089b6b563cSPaul Mackerras 			opt++;
809450e7dd4SChristophe Leroy 		if (!prom_strncmp(opt, "off", 3))
8105827d416SAnton Blanchard 			prom_iommu_off = 1;
811450e7dd4SChristophe Leroy 		else if (!prom_strncmp(opt, "force", 5))
8125827d416SAnton Blanchard 			prom_iommu_force_on = 1;
8139b6b563cSPaul Mackerras 	}
8149b6b563cSPaul Mackerras #endif
815450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "mem=");
816cf68787bSBenjamin Krill 	if (opt) {
817cf68787bSBenjamin Krill 		opt += 4;
8185827d416SAnton Blanchard 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
819cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
820cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
8215827d416SAnton Blanchard 		prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
822cf68787bSBenjamin Krill #endif
823cf68787bSBenjamin Krill 	}
824014d02cbSSuraj Jitindar Singh 
825c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
826c886087cSBenjamin Herrenschmidt 	prom_radix_disable = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
827450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "disable_radix");
828014d02cbSSuraj Jitindar Singh 	if (opt) {
8297a22d632SAneesh Kumar K.V 		opt += 13;
8307a22d632SAneesh Kumar K.V 		if (*opt && *opt == '=') {
8317a22d632SAneesh Kumar K.V 			bool val;
8327a22d632SAneesh Kumar K.V 
833450e7dd4SChristophe Leroy 			if (prom_strtobool(++opt, &val))
8347a22d632SAneesh Kumar K.V 				prom_radix_disable = false;
8357a22d632SAneesh Kumar K.V 			else
8367a22d632SAneesh Kumar K.V 				prom_radix_disable = val;
8377a22d632SAneesh Kumar K.V 		} else
838014d02cbSSuraj Jitindar Singh 			prom_radix_disable = true;
839014d02cbSSuraj Jitindar Singh 	}
8407a22d632SAneesh Kumar K.V 	if (prom_radix_disable)
8417a22d632SAneesh Kumar K.V 		prom_debug("Radix disabled from cmdline\n");
842a3bf9fbdSGreg Kurz 
843bf6b7661SAneesh Kumar K.V 	opt = prom_strstr(prom_cmd_line, "radix_hcall_invalidate=on");
844bf6b7661SAneesh Kumar K.V 	if (opt) {
845bf6b7661SAneesh Kumar K.V 		prom_radix_gtse_disable = true;
846bf6b7661SAneesh Kumar K.V 		prom_debug("Radix GTSE disabled from cmdline\n");
847bf6b7661SAneesh Kumar K.V 	}
848bf6b7661SAneesh Kumar K.V 
849a3bf9fbdSGreg Kurz 	opt = prom_strstr(prom_cmd_line, "xive=off");
850a3bf9fbdSGreg Kurz 	if (opt) {
851a3bf9fbdSGreg Kurz 		prom_xive_disable = true;
852a3bf9fbdSGreg Kurz 		prom_debug("XIVE disabled from cmdline\n");
853a3bf9fbdSGreg Kurz 	}
854c886087cSBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
8556a9c930bSRam Pai 
8566a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
8576a9c930bSRam Pai 	opt = prom_strstr(prom_cmd_line, "svm=");
8586a9c930bSRam Pai 	if (opt) {
8596a9c930bSRam Pai 		bool val;
8606a9c930bSRam Pai 
8616a9c930bSRam Pai 		opt += sizeof("svm=") - 1;
8626a9c930bSRam Pai 		if (!prom_strtobool(opt, &val))
8636a9c930bSRam Pai 			prom_svm_enable = val;
8646a9c930bSRam Pai 	}
8656a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
8669b6b563cSPaul Mackerras }
8679b6b563cSPaul Mackerras 
86811fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
8699b6b563cSPaul Mackerras /*
870530b5e14SNathan Fontenot  * The architecture vector has an array of PVR mask/value pairs,
871530b5e14SNathan Fontenot  * followed by # option vectors - 1, followed by the option vectors.
872530b5e14SNathan Fontenot  *
873530b5e14SNathan Fontenot  * See prom.h for the definition of the bits specified in the
874530b5e14SNathan Fontenot  * architecture vector.
875f709bfacSPaul Mackerras  */
876f709bfacSPaul Mackerras 
877e8a4fd0aSMichael Ellerman /* Firmware expects the value to be n - 1, where n is the # of vectors */
878e8a4fd0aSMichael Ellerman #define NUM_VECTORS(n)		((n) - 1)
879e8a4fd0aSMichael Ellerman 
880e8a4fd0aSMichael Ellerman /*
881e8a4fd0aSMichael Ellerman  * Firmware expects 1 + n - 2, where n is the length of the option vector in
882e8a4fd0aSMichael Ellerman  * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
883e8a4fd0aSMichael Ellerman  */
884e8a4fd0aSMichael Ellerman #define VECTOR_LENGTH(n)	(1 + (n) - 2)
885e8a4fd0aSMichael Ellerman 
886d03d1d65SMichael Ellerman struct option_vector1 {
887d03d1d65SMichael Ellerman 	u8 byte1;
888d03d1d65SMichael Ellerman 	u8 arch_versions;
889cc3d2940SPaul Mackerras 	u8 arch_versions3;
890d03d1d65SMichael Ellerman } __packed;
891d03d1d65SMichael Ellerman 
892d03d1d65SMichael Ellerman struct option_vector2 {
893d03d1d65SMichael Ellerman 	u8 byte1;
894d03d1d65SMichael Ellerman 	__be16 reserved;
895d03d1d65SMichael Ellerman 	__be32 real_base;
896d03d1d65SMichael Ellerman 	__be32 real_size;
897d03d1d65SMichael Ellerman 	__be32 virt_base;
898d03d1d65SMichael Ellerman 	__be32 virt_size;
899d03d1d65SMichael Ellerman 	__be32 load_base;
900d03d1d65SMichael Ellerman 	__be32 min_rma;
901d03d1d65SMichael Ellerman 	__be32 min_load;
902d03d1d65SMichael Ellerman 	u8 min_rma_percent;
903d03d1d65SMichael Ellerman 	u8 max_pft_size;
904d03d1d65SMichael Ellerman } __packed;
905d03d1d65SMichael Ellerman 
906d03d1d65SMichael Ellerman struct option_vector3 {
907d03d1d65SMichael Ellerman 	u8 byte1;
908d03d1d65SMichael Ellerman 	u8 byte2;
909d03d1d65SMichael Ellerman } __packed;
910d03d1d65SMichael Ellerman 
911d03d1d65SMichael Ellerman struct option_vector4 {
912d03d1d65SMichael Ellerman 	u8 byte1;
913d03d1d65SMichael Ellerman 	u8 min_vp_cap;
914d03d1d65SMichael Ellerman } __packed;
915d03d1d65SMichael Ellerman 
916d03d1d65SMichael Ellerman struct option_vector5 {
917d03d1d65SMichael Ellerman 	u8 byte1;
918d03d1d65SMichael Ellerman 	u8 byte2;
919d03d1d65SMichael Ellerman 	u8 byte3;
920d03d1d65SMichael Ellerman 	u8 cmo;
921d03d1d65SMichael Ellerman 	u8 associativity;
922d03d1d65SMichael Ellerman 	u8 bin_opts;
923d03d1d65SMichael Ellerman 	u8 micro_checkpoint;
924d03d1d65SMichael Ellerman 	u8 reserved0;
925d03d1d65SMichael Ellerman 	__be32 max_cpus;
926d03d1d65SMichael Ellerman 	__be16 papr_level;
927d03d1d65SMichael Ellerman 	__be16 reserved1;
928d03d1d65SMichael Ellerman 	u8 platform_facilities;
929d03d1d65SMichael Ellerman 	u8 reserved2;
930d03d1d65SMichael Ellerman 	__be16 reserved3;
931d03d1d65SMichael Ellerman 	u8 subprocessors;
932cc3d2940SPaul Mackerras 	u8 byte22;
933cc3d2940SPaul Mackerras 	u8 intarch;
934cc3d2940SPaul Mackerras 	u8 mmu;
935014d02cbSSuraj Jitindar Singh 	u8 hash_ext;
936014d02cbSSuraj Jitindar Singh 	u8 radix_ext;
937d03d1d65SMichael Ellerman } __packed;
938d03d1d65SMichael Ellerman 
939d03d1d65SMichael Ellerman struct option_vector6 {
940d03d1d65SMichael Ellerman 	u8 reserved;
941d03d1d65SMichael Ellerman 	u8 secondary_pteg;
942d03d1d65SMichael Ellerman 	u8 os_name;
943d03d1d65SMichael Ellerman } __packed;
944d03d1d65SMichael Ellerman 
945ffaacd97SMichael Ellerman struct option_vector7 {
946ffaacd97SMichael Ellerman 	u8 os_id[256];
947ffaacd97SMichael Ellerman } __packed;
948ffaacd97SMichael Ellerman 
94976ffb578SMichael Ellerman struct ibm_arch_vec {
950a3ea40d5SAlistair Popple 	struct { u32 mask, val; } pvrs[14];
95176ffb578SMichael Ellerman 
95276ffb578SMichael Ellerman 	u8 num_vectors;
95376ffb578SMichael Ellerman 
95476ffb578SMichael Ellerman 	u8 vec1_len;
95576ffb578SMichael Ellerman 	struct option_vector1 vec1;
95676ffb578SMichael Ellerman 
95776ffb578SMichael Ellerman 	u8 vec2_len;
95876ffb578SMichael Ellerman 	struct option_vector2 vec2;
95976ffb578SMichael Ellerman 
96076ffb578SMichael Ellerman 	u8 vec3_len;
96176ffb578SMichael Ellerman 	struct option_vector3 vec3;
96276ffb578SMichael Ellerman 
96376ffb578SMichael Ellerman 	u8 vec4_len;
96476ffb578SMichael Ellerman 	struct option_vector4 vec4;
96576ffb578SMichael Ellerman 
96676ffb578SMichael Ellerman 	u8 vec5_len;
96776ffb578SMichael Ellerman 	struct option_vector5 vec5;
96876ffb578SMichael Ellerman 
96976ffb578SMichael Ellerman 	u8 vec6_len;
97076ffb578SMichael Ellerman 	struct option_vector6 vec6;
971ffaacd97SMichael Ellerman 
972ffaacd97SMichael Ellerman 	u8 vec7_len;
973ffaacd97SMichael Ellerman 	struct option_vector7 vec7;
97476ffb578SMichael Ellerman } __packed;
97576ffb578SMichael Ellerman 
976d00e34b9SBenjamin Herrenschmidt static const struct ibm_arch_vec ibm_architecture_vec_template __initconst = {
97776ffb578SMichael Ellerman 	.pvrs = {
97876ffb578SMichael Ellerman 		{
97976ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffe0000), /* POWER5/POWER5+ */
98076ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003a0000),
98176ffb578SMichael Ellerman 		},
98276ffb578SMichael Ellerman 		{
98376ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER6 */
98476ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003e0000),
98576ffb578SMichael Ellerman 		},
98676ffb578SMichael Ellerman 		{
98776ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER7 */
98876ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003f0000),
98976ffb578SMichael Ellerman 		},
99076ffb578SMichael Ellerman 		{
99176ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8E */
99276ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004b0000),
99376ffb578SMichael Ellerman 		},
99476ffb578SMichael Ellerman 		{
99576ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8NVL */
99676ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004c0000),
99776ffb578SMichael Ellerman 		},
99876ffb578SMichael Ellerman 		{
99976ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8 */
100076ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004d0000),
100176ffb578SMichael Ellerman 		},
100276ffb578SMichael Ellerman 		{
1003cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffff0000), /* POWER9 */
1004cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x004e0000),
1005cc3d2940SPaul Mackerras 		},
1006cc3d2940SPaul Mackerras 		{
1007a3ea40d5SAlistair Popple 			.mask = cpu_to_be32(0xffff0000), /* POWER10 */
1008a3ea40d5SAlistair Popple 			.val  = cpu_to_be32(0x00800000),
1009a3ea40d5SAlistair Popple 		},
1010a3ea40d5SAlistair Popple 		{
1011a3ea40d5SAlistair Popple 			.mask = cpu_to_be32(0xffffffff), /* all 3.1-compliant */
1012a3ea40d5SAlistair Popple 			.val  = cpu_to_be32(0x0f000006),
1013a3ea40d5SAlistair Popple 		},
1014a3ea40d5SAlistair Popple 		{
1015cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffffffff), /* all 3.00-compliant */
1016cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x0f000005),
1017cc3d2940SPaul Mackerras 		},
1018cc3d2940SPaul Mackerras 		{
101976ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.07-compliant */
102076ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000004),
102176ffb578SMichael Ellerman 		},
102276ffb578SMichael Ellerman 		{
102376ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.06-compliant */
102476ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000003),
102576ffb578SMichael Ellerman 		},
102676ffb578SMichael Ellerman 		{
102776ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.05-compliant */
102876ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000002),
102976ffb578SMichael Ellerman 		},
103076ffb578SMichael Ellerman 		{
103176ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffffffe), /* all 2.04-compliant and earlier */
103276ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000001),
103376ffb578SMichael Ellerman 		},
103476ffb578SMichael Ellerman 	},
103576ffb578SMichael Ellerman 
103676ffb578SMichael Ellerman 	.num_vectors = NUM_VECTORS(6),
103776ffb578SMichael Ellerman 
103876ffb578SMichael Ellerman 	.vec1_len = VECTOR_LENGTH(sizeof(struct option_vector1)),
103976ffb578SMichael Ellerman 	.vec1 = {
104076ffb578SMichael Ellerman 		.byte1 = 0,
104176ffb578SMichael Ellerman 		.arch_versions = OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
104276ffb578SMichael Ellerman 				 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
1043a3ea40d5SAlistair Popple 		.arch_versions3 = OV1_PPC_3_00 | OV1_PPC_3_1,
104476ffb578SMichael Ellerman 	},
104576ffb578SMichael Ellerman 
104676ffb578SMichael Ellerman 	.vec2_len = VECTOR_LENGTH(sizeof(struct option_vector2)),
104776ffb578SMichael Ellerman 	/* option vector 2: Open Firmware options supported */
104876ffb578SMichael Ellerman 	.vec2 = {
104976ffb578SMichael Ellerman 		.byte1 = OV2_REAL_MODE,
105076ffb578SMichael Ellerman 		.reserved = 0,
105176ffb578SMichael Ellerman 		.real_base = cpu_to_be32(0xffffffff),
105276ffb578SMichael Ellerman 		.real_size = cpu_to_be32(0xffffffff),
105376ffb578SMichael Ellerman 		.virt_base = cpu_to_be32(0xffffffff),
105476ffb578SMichael Ellerman 		.virt_size = cpu_to_be32(0xffffffff),
105576ffb578SMichael Ellerman 		.load_base = cpu_to_be32(0xffffffff),
1056687da8fcSSukadev Bhattiprolu 		.min_rma = cpu_to_be32(512),		/* 512MB min RMA */
105776ffb578SMichael Ellerman 		.min_load = cpu_to_be32(0xffffffff),	/* full client load */
105876ffb578SMichael Ellerman 		.min_rma_percent = 0,	/* min RMA percentage of total RAM */
105976ffb578SMichael Ellerman 		.max_pft_size = 48,	/* max log_2(hash table size) */
106076ffb578SMichael Ellerman 	},
106176ffb578SMichael Ellerman 
106276ffb578SMichael Ellerman 	.vec3_len = VECTOR_LENGTH(sizeof(struct option_vector3)),
106376ffb578SMichael Ellerman 	/* option vector 3: processor options supported */
106476ffb578SMichael Ellerman 	.vec3 = {
106576ffb578SMichael Ellerman 		.byte1 = 0,			/* don't ignore, don't halt */
106676ffb578SMichael Ellerman 		.byte2 = OV3_FP | OV3_VMX | OV3_DFP,
106776ffb578SMichael Ellerman 	},
106876ffb578SMichael Ellerman 
106976ffb578SMichael Ellerman 	.vec4_len = VECTOR_LENGTH(sizeof(struct option_vector4)),
107076ffb578SMichael Ellerman 	/* option vector 4: IBM PAPR implementation */
107176ffb578SMichael Ellerman 	.vec4 = {
107276ffb578SMichael Ellerman 		.byte1 = 0,			/* don't halt */
107376ffb578SMichael Ellerman 		.min_vp_cap = OV4_MIN_ENT_CAP,	/* minimum VP entitled capacity */
107476ffb578SMichael Ellerman 	},
107576ffb578SMichael Ellerman 
107676ffb578SMichael Ellerman 	.vec5_len = VECTOR_LENGTH(sizeof(struct option_vector5)),
107776ffb578SMichael Ellerman 	/* option vector 5: PAPR/OF options */
107876ffb578SMichael Ellerman 	.vec5 = {
107976ffb578SMichael Ellerman 		.byte1 = 0,				/* don't ignore, don't halt */
108076ffb578SMichael Ellerman 		.byte2 = OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
108176ffb578SMichael Ellerman 		OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
108276ffb578SMichael Ellerman #ifdef CONFIG_PCI_MSI
108376ffb578SMichael Ellerman 		/* PCIe/MSI support.  Without MSI full PCIe is not supported */
108476ffb578SMichael Ellerman 		OV5_FEAT(OV5_MSI),
108576ffb578SMichael Ellerman #else
108676ffb578SMichael Ellerman 		0,
108776ffb578SMichael Ellerman #endif
108876ffb578SMichael Ellerman 		.byte3 = 0,
108976ffb578SMichael Ellerman 		.cmo =
109076ffb578SMichael Ellerman #ifdef CONFIG_PPC_SMLPAR
109176ffb578SMichael Ellerman 		OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
109276ffb578SMichael Ellerman #else
109376ffb578SMichael Ellerman 		0,
109476ffb578SMichael Ellerman #endif
10951c6b5a7eSAneesh Kumar K.V 		.associativity = OV5_FEAT(OV5_FORM1_AFFINITY) | OV5_FEAT(OV5_PRRN) |
10961c6b5a7eSAneesh Kumar K.V 		OV5_FEAT(OV5_FORM2_AFFINITY),
10973dbbaf20SMichael Roth 		.bin_opts = OV5_FEAT(OV5_RESIZE_HPT) | OV5_FEAT(OV5_HP_EVT),
109876ffb578SMichael Ellerman 		.micro_checkpoint = 0,
109976ffb578SMichael Ellerman 		.reserved0 = 0,
110076ffb578SMichael Ellerman 		.max_cpus = cpu_to_be32(NR_CPUS),	/* number of cores supported */
110176ffb578SMichael Ellerman 		.papr_level = 0,
110276ffb578SMichael Ellerman 		.reserved1 = 0,
110376ffb578SMichael Ellerman 		.platform_facilities = OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) | OV5_FEAT(OV5_PFO_HW_842),
110476ffb578SMichael Ellerman 		.reserved2 = 0,
110576ffb578SMichael Ellerman 		.reserved3 = 0,
110676ffb578SMichael Ellerman 		.subprocessors = 1,
11070a87ccd3STyrel Datwyler 		.byte22 = OV5_FEAT(OV5_DRMEM_V2) | OV5_FEAT(OV5_DRC_INFO),
1108cc3d2940SPaul Mackerras 		.intarch = 0,
1109014d02cbSSuraj Jitindar Singh 		.mmu = 0,
1110014d02cbSSuraj Jitindar Singh 		.hash_ext = 0,
1111014d02cbSSuraj Jitindar Singh 		.radix_ext = 0,
111276ffb578SMichael Ellerman 	},
111376ffb578SMichael Ellerman 
111476ffb578SMichael Ellerman 	/* option vector 6: IBM PAPR hints */
111576ffb578SMichael Ellerman 	.vec6_len = VECTOR_LENGTH(sizeof(struct option_vector6)),
111676ffb578SMichael Ellerman 	.vec6 = {
111776ffb578SMichael Ellerman 		.reserved = 0,
111876ffb578SMichael Ellerman 		.secondary_pteg = 0,
111976ffb578SMichael Ellerman 		.os_name = OV6_LINUX,
112076ffb578SMichael Ellerman 	},
1121ffaacd97SMichael Ellerman 
1122ffaacd97SMichael Ellerman 	/* option vector 7: OS Identification */
1123ffaacd97SMichael Ellerman 	.vec7_len = VECTOR_LENGTH(sizeof(struct option_vector7)),
112476ffb578SMichael Ellerman };
112576ffb578SMichael Ellerman 
1126a614f52eSBenjamin Herrenschmidt static struct ibm_arch_vec __prombss ibm_architecture_vec  ____cacheline_aligned;
1127a614f52eSBenjamin Herrenschmidt 
1128493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
1129493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
113030c69ca0SBenjamin Herrenschmidt static const struct fake_elf {
11319b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
11329b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
11339b6b563cSPaul Mackerras 	struct chrpnote {
11349b6b563cSPaul Mackerras 		u32	namesz;
11359b6b563cSPaul Mackerras 		u32	descsz;
11369b6b563cSPaul Mackerras 		u32	type;
11379b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
11389b6b563cSPaul Mackerras 		struct chrpdesc {
11399b6b563cSPaul Mackerras 			u32	real_mode;
11409b6b563cSPaul Mackerras 			u32	real_base;
11419b6b563cSPaul Mackerras 			u32	real_size;
11429b6b563cSPaul Mackerras 			u32	virt_base;
11439b6b563cSPaul Mackerras 			u32	virt_size;
11449b6b563cSPaul Mackerras 			u32	load_base;
11459b6b563cSPaul Mackerras 		} chrpdesc;
11469b6b563cSPaul Mackerras 	} chrpnote;
11479b6b563cSPaul Mackerras 	struct rpanote {
11489b6b563cSPaul Mackerras 		u32	namesz;
11499b6b563cSPaul Mackerras 		u32	descsz;
11509b6b563cSPaul Mackerras 		u32	type;
11519b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
11529b6b563cSPaul Mackerras 		struct rpadesc {
11539b6b563cSPaul Mackerras 			u32	lpar_affinity;
11549b6b563cSPaul Mackerras 			u32	min_rmo_size;
11559b6b563cSPaul Mackerras 			u32	min_rmo_percent;
11569b6b563cSPaul Mackerras 			u32	max_pft_size;
11579b6b563cSPaul Mackerras 			u32	splpar;
11589b6b563cSPaul Mackerras 			u32	min_load;
11599b6b563cSPaul Mackerras 			u32	new_mem_def;
11609b6b563cSPaul Mackerras 			u32	ignore_me;
11619b6b563cSPaul Mackerras 		} rpadesc;
11629b6b563cSPaul Mackerras 	} rpanote;
1163d00e34b9SBenjamin Herrenschmidt } fake_elf __initconst = {
11649b6b563cSPaul Mackerras 	.elfhdr = {
11659b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
11669b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
11679b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
11689b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
11699b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
11709b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
11719b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
11729b6b563cSPaul Mackerras 		.e_phnum = 2
11739b6b563cSPaul Mackerras 	},
11749b6b563cSPaul Mackerras 	.phdr = {
11759b6b563cSPaul Mackerras 		[0] = {
11769b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
11779b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
11789b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
11799b6b563cSPaul Mackerras 		}, [1] = {
11809b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
11819b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
11829b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
11839b6b563cSPaul Mackerras 		}
11849b6b563cSPaul Mackerras 	},
11859b6b563cSPaul Mackerras 	.chrpnote = {
11869b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
11879b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
11889b6b563cSPaul Mackerras 		.type = 0x1275,
11899b6b563cSPaul Mackerras 		.name = "PowerPC",
11909b6b563cSPaul Mackerras 		.chrpdesc = {
11919b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
11929b6b563cSPaul Mackerras 			.real_base = ~0U,
11939b6b563cSPaul Mackerras 			.real_size = ~0U,
11949b6b563cSPaul Mackerras 			.virt_base = ~0U,
11959b6b563cSPaul Mackerras 			.virt_size = ~0U,
11969b6b563cSPaul Mackerras 			.load_base = ~0U
11979b6b563cSPaul Mackerras 		},
11989b6b563cSPaul Mackerras 	},
11999b6b563cSPaul Mackerras 	.rpanote = {
12009b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
12019b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
12029b6b563cSPaul Mackerras 		.type = 0x12759999,
12039b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
12049b6b563cSPaul Mackerras 		.rpadesc = {
12055663a123SPaul Mackerras 			.lpar_affinity = 0,
12065663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
12079b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
12085663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
12099b6b563cSPaul Mackerras 			.splpar = 1,
12109b6b563cSPaul Mackerras 			.min_load = ~0U,
12115663a123SPaul Mackerras 			.new_mem_def = 0
12129b6b563cSPaul Mackerras 		}
12139b6b563cSPaul Mackerras 	}
12149b6b563cSPaul Mackerras };
1215493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
12169b6b563cSPaul Mackerras 
prom_count_smt_threads(void)1217efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
1218efec959fSBenjamin Herrenschmidt {
1219efec959fSBenjamin Herrenschmidt 	phandle node;
1220efec959fSBenjamin Herrenschmidt 	char type[64];
1221efec959fSBenjamin Herrenschmidt 	unsigned int plen;
1222efec959fSBenjamin Herrenschmidt 
1223efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
1224efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
1225efec959fSBenjamin Herrenschmidt 		type[0] = 0;
1226efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
1227efec959fSBenjamin Herrenschmidt 
1228450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "cpu"))
1229efec959fSBenjamin Herrenschmidt 			continue;
1230efec959fSBenjamin Herrenschmidt 		/*
1231efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
1232efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
1233efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
1234efec959fSBenjamin Herrenschmidt 		 */
1235efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
1236efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
1237efec959fSBenjamin Herrenschmidt 			break;
1238efec959fSBenjamin Herrenschmidt 		plen >>= 2;
12392c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
1240efec959fSBenjamin Herrenschmidt 
1241efec959fSBenjamin Herrenschmidt 		/* Sanity check */
1242efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
12432c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
1244efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
1245efec959fSBenjamin Herrenschmidt 			return 1;
1246efec959fSBenjamin Herrenschmidt 		}
1247efec959fSBenjamin Herrenschmidt 		return plen;
1248efec959fSBenjamin Herrenschmidt 	}
1249efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
1250efec959fSBenjamin Herrenschmidt 
1251efec959fSBenjamin Herrenschmidt 	return 1;
1252efec959fSBenjamin Herrenschmidt 
1253efec959fSBenjamin Herrenschmidt }
1254efec959fSBenjamin Herrenschmidt 
prom_parse_mmu_model(u8 val,struct platform_support * support)1255014d02cbSSuraj Jitindar Singh static void __init prom_parse_mmu_model(u8 val,
1256014d02cbSSuraj Jitindar Singh 					struct platform_support *support)
1257014d02cbSSuraj Jitindar Singh {
1258014d02cbSSuraj Jitindar Singh 	switch (val) {
1259014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_DYNAMIC):
1260014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_EITHER): /* Either Available */
1261014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - either supported\n");
1262014d02cbSSuraj Jitindar Singh 		support->radix_mmu = !prom_radix_disable;
1263014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1264014d02cbSSuraj Jitindar Singh 		break;
1265014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */
1266014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - radix only\n");
1267014d02cbSSuraj Jitindar Singh 		if (prom_radix_disable) {
1268014d02cbSSuraj Jitindar Singh 			/*
1269014d02cbSSuraj Jitindar Singh 			 * If we __have__ to do radix, we're better off ignoring
1270014d02cbSSuraj Jitindar Singh 			 * the command line rather than not booting.
1271014d02cbSSuraj Jitindar Singh 			 */
1272014d02cbSSuraj Jitindar Singh 			prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
1273014d02cbSSuraj Jitindar Singh 		}
1274014d02cbSSuraj Jitindar Singh 		support->radix_mmu = true;
1275014d02cbSSuraj Jitindar Singh 		break;
1276014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_HASH):
1277014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - hash only\n");
1278014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1279014d02cbSSuraj Jitindar Singh 		break;
1280014d02cbSSuraj Jitindar Singh 	default:
1281014d02cbSSuraj Jitindar Singh 		prom_debug("Unknown mmu support option: 0x%x\n", val);
1282014d02cbSSuraj Jitindar Singh 		break;
1283014d02cbSSuraj Jitindar Singh 	}
1284014d02cbSSuraj Jitindar Singh }
1285014d02cbSSuraj Jitindar Singh 
prom_parse_xive_model(u8 val,struct platform_support * support)1286ac5e5a54SCédric Le Goater static void __init prom_parse_xive_model(u8 val,
1287ac5e5a54SCédric Le Goater 					 struct platform_support *support)
1288ac5e5a54SCédric Le Goater {
1289ac5e5a54SCédric Le Goater 	switch (val) {
1290ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EITHER): /* Either Available */
1291ac5e5a54SCédric Le Goater 		prom_debug("XIVE - either mode supported\n");
1292a3bf9fbdSGreg Kurz 		support->xive = !prom_xive_disable;
1293ac5e5a54SCédric Le Goater 		break;
1294ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EXPLOIT): /* Only Exploitation mode */
1295ac5e5a54SCédric Le Goater 		prom_debug("XIVE - exploitation mode supported\n");
1296a3bf9fbdSGreg Kurz 		if (prom_xive_disable) {
1297a3bf9fbdSGreg Kurz 			/*
1298a3bf9fbdSGreg Kurz 			 * If we __have__ to do XIVE, we're better off ignoring
1299a3bf9fbdSGreg Kurz 			 * the command line rather than not booting.
1300a3bf9fbdSGreg Kurz 			 */
1301a3bf9fbdSGreg Kurz 			prom_printf("WARNING: Ignoring cmdline option xive=off\n");
1302a3bf9fbdSGreg Kurz 		}
1303ac5e5a54SCédric Le Goater 		support->xive = true;
1304ac5e5a54SCédric Le Goater 		break;
1305ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_LEGACY): /* Only Legacy mode */
1306ac5e5a54SCédric Le Goater 		prom_debug("XIVE - legacy mode supported\n");
1307ac5e5a54SCédric Le Goater 		break;
1308ac5e5a54SCédric Le Goater 	default:
1309ac5e5a54SCédric Le Goater 		prom_debug("Unknown xive support option: 0x%x\n", val);
1310ac5e5a54SCédric Le Goater 		break;
1311ac5e5a54SCédric Le Goater 	}
1312ac5e5a54SCédric Le Goater }
1313ac5e5a54SCédric Le Goater 
prom_parse_platform_support(u8 index,u8 val,struct platform_support * support)1314014d02cbSSuraj Jitindar Singh static void __init prom_parse_platform_support(u8 index, u8 val,
1315014d02cbSSuraj Jitindar Singh 					       struct platform_support *support)
1316014d02cbSSuraj Jitindar Singh {
1317014d02cbSSuraj Jitindar Singh 	switch (index) {
1318014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_MMU_SUPPORT): /* MMU Model */
1319014d02cbSSuraj Jitindar Singh 		prom_parse_mmu_model(val & OV5_FEAT(OV5_MMU_SUPPORT), support);
1320014d02cbSSuraj Jitindar Singh 		break;
1321014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_RADIX_GTSE): /* Radix Extensions */
1322bf6b7661SAneesh Kumar K.V 		if (val & OV5_FEAT(OV5_RADIX_GTSE))
1323bf6b7661SAneesh Kumar K.V 			support->radix_gtse = !prom_radix_gtse_disable;
1324014d02cbSSuraj Jitindar Singh 		break;
1325ac5e5a54SCédric Le Goater 	case OV5_INDX(OV5_XIVE_SUPPORT): /* Interrupt mode */
1326ac5e5a54SCédric Le Goater 		prom_parse_xive_model(val & OV5_FEAT(OV5_XIVE_SUPPORT),
1327ac5e5a54SCédric Le Goater 				      support);
1328ac5e5a54SCédric Le Goater 		break;
1329014d02cbSSuraj Jitindar Singh 	}
1330014d02cbSSuraj Jitindar Singh }
1331014d02cbSSuraj Jitindar Singh 
prom_check_platform_support(void)1332014d02cbSSuraj Jitindar Singh static void __init prom_check_platform_support(void)
1333014d02cbSSuraj Jitindar Singh {
1334014d02cbSSuraj Jitindar Singh 	struct platform_support supported = {
1335014d02cbSSuraj Jitindar Singh 		.hash_mmu = false,
1336014d02cbSSuraj Jitindar Singh 		.radix_mmu = false,
1337ac5e5a54SCédric Le Goater 		.radix_gtse = false,
1338ac5e5a54SCédric Le Goater 		.xive = false
1339014d02cbSSuraj Jitindar Singh 	};
1340014d02cbSSuraj Jitindar Singh 	int prop_len = prom_getproplen(prom.chosen,
1341014d02cbSSuraj Jitindar Singh 				       "ibm,arch-vec-5-platform-support");
1342a614f52eSBenjamin Herrenschmidt 
1343adcf5918SChristophe Leroy 	/*
1344adcf5918SChristophe Leroy 	 * First copy the architecture vec template
1345adcf5918SChristophe Leroy 	 *
1346adcf5918SChristophe Leroy 	 * use memcpy() instead of *vec = *vec_template so that GCC replaces it
1347adcf5918SChristophe Leroy 	 * by __memcpy() when KASAN is active
1348adcf5918SChristophe Leroy 	 */
1349adcf5918SChristophe Leroy 	memcpy(&ibm_architecture_vec, &ibm_architecture_vec_template,
1350adcf5918SChristophe Leroy 	       sizeof(ibm_architecture_vec));
1351a614f52eSBenjamin Herrenschmidt 
1352ffaacd97SMichael Ellerman 	prom_strscpy_pad(ibm_architecture_vec.vec7.os_id, linux_banner, 256);
1353ffaacd97SMichael Ellerman 
1354014d02cbSSuraj Jitindar Singh 	if (prop_len > 1) {
1355014d02cbSSuraj Jitindar Singh 		int i;
1356ab912399SSuraj Jitindar Singh 		u8 vec[8];
1357014d02cbSSuraj Jitindar Singh 		prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1358014d02cbSSuraj Jitindar Singh 			   prop_len);
1359ab912399SSuraj Jitindar Singh 		if (prop_len > sizeof(vec))
1360ab912399SSuraj Jitindar Singh 			prom_printf("WARNING: ibm,arch-vec-5-platform-support longer than expected (len: %d)\n",
1361ab912399SSuraj Jitindar Singh 				    prop_len);
1362ed5b00a0SCédric Le Goater 		prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support", &vec, sizeof(vec));
1363ed5b00a0SCédric Le Goater 		for (i = 0; i < prop_len; i += 2) {
1364ed5b00a0SCédric Le Goater 			prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2, vec[i], vec[i + 1]);
1365ed5b00a0SCédric Le Goater 			prom_parse_platform_support(vec[i], vec[i + 1], &supported);
1366014d02cbSSuraj Jitindar Singh 		}
1367014d02cbSSuraj Jitindar Singh 	}
1368014d02cbSSuraj Jitindar Singh 
1369029ab30bSBharata B Rao 	if (supported.radix_mmu && IS_ENABLED(CONFIG_PPC_RADIX_MMU)) {
1370029ab30bSBharata B Rao 		/* Radix preferred - Check if GTSE is also supported */
1371029ab30bSBharata B Rao 		prom_debug("Asking for radix\n");
1372014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1373029ab30bSBharata B Rao 		if (supported.radix_gtse)
1374029ab30bSBharata B Rao 			ibm_architecture_vec.vec5.radix_ext =
1375029ab30bSBharata B Rao 					OV5_FEAT(OV5_RADIX_GTSE);
1376029ab30bSBharata B Rao 		else
1377029ab30bSBharata B Rao 			prom_debug("Radix GTSE isn't supported\n");
1378014d02cbSSuraj Jitindar Singh 	} else if (supported.hash_mmu) {
1379014d02cbSSuraj Jitindar Singh 		/* Default to hash mmu (if we can) */
1380014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for hash\n");
1381014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1382014d02cbSSuraj Jitindar Singh 	} else {
1383014d02cbSSuraj Jitindar Singh 		/* We're probably on a legacy hypervisor */
1384014d02cbSSuraj Jitindar Singh 		prom_debug("Assuming legacy hash support\n");
1385014d02cbSSuraj Jitindar Singh 	}
1386ac5e5a54SCédric Le Goater 
1387ac5e5a54SCédric Le Goater 	if (supported.xive) {
1388ac5e5a54SCédric Le Goater 		prom_debug("Asking for XIVE\n");
1389ac5e5a54SCédric Le Goater 		ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1390ac5e5a54SCédric Le Goater 	}
1391014d02cbSSuraj Jitindar Singh }
1392efec959fSBenjamin Herrenschmidt 
prom_send_capabilities(void)13939b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
13949b6b563cSPaul Mackerras {
1395493adffcSBenjamin Herrenschmidt 	ihandle root;
1396f709bfacSPaul Mackerras 	prom_arg_t ret;
1397dbd0c5d5SLaurent Dufour 	u32 cores;
13989b6b563cSPaul Mackerras 
1399014d02cbSSuraj Jitindar Singh 	/* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1400014d02cbSSuraj Jitindar Singh 	prom_check_platform_support();
1401014d02cbSSuraj Jitindar Singh 
1402f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
1403f709bfacSPaul Mackerras 	if (root != 0) {
1404efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
1405efec959fSBenjamin Herrenschmidt 		 *
1406efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
1407efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
1408efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
1409efec959fSBenjamin Herrenschmidt 		 */
1410dbd0c5d5SLaurent Dufour 
1411dbd0c5d5SLaurent Dufour 		cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1412eae5f709SMathieu Malaterre 		prom_printf("Max number of cores passed to firmware: %u (NR_CPUS = %d)\n",
1413dbd0c5d5SLaurent Dufour 			    cores, NR_CPUS);
141476ffb578SMichael Ellerman 
141576ffb578SMichael Ellerman 		ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1416efec959fSBenjamin Herrenschmidt 
1417f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
1418049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
1419f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
1420f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
142133b74977SBenjamin Herrenschmidt 				  root,
142276ffb578SMichael Ellerman 				  ADDR(&ibm_architecture_vec)) == 0) {
1423f709bfacSPaul Mackerras 			/* the call exists... */
1424f709bfacSPaul Mackerras 			if (ret)
14254da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
1426f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
1427f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
14284da727aeSAnton Blanchard 			prom_printf(" done\n");
1429f709bfacSPaul Mackerras 			return;
1430f709bfacSPaul Mackerras 		}
1431f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
1432049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
1433f709bfacSPaul Mackerras 	}
1434f709bfacSPaul Mackerras 
1435493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
1436493adffcSBenjamin Herrenschmidt 	{
1437493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
1438493adffcSBenjamin Herrenschmidt 
1439f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
1440493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
1441493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
14429b6b563cSPaul Mackerras 		if (elfloader == 0) {
14439b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
14449b6b563cSPaul Mackerras 			return;
14459b6b563cSPaul Mackerras 		}
14469b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
14479b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
14489b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
14499b6b563cSPaul Mackerras 	}
1450493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1451493adffcSBenjamin Herrenschmidt }
145211fdb309SBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
14539b6b563cSPaul Mackerras 
14549b6b563cSPaul Mackerras /*
14559b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
14569b6b563cSPaul Mackerras  *
14579b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
14589b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
14599b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
14609b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
14619b6b563cSPaul Mackerras  *  never happen.
14629b6b563cSPaul Mackerras  *
14639b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
14649b6b563cSPaul Mackerras  *  TCEs overlap
14659b6b563cSPaul Mackerras  *
14669b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
14679b6b563cSPaul Mackerras  *
14689b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
14699b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
14709b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
14719b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
14729b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
14739b6b563cSPaul Mackerras  *
14749b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
14759b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
14769b6b563cSPaul Mackerras  *  reserved.
14779b6b563cSPaul Mackerras  */
14789b6b563cSPaul Mackerras 
14799b6b563cSPaul Mackerras 
14809b6b563cSPaul Mackerras /*
14819b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
14829b6b563cSPaul Mackerras  *
14839b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
14849b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
14859b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
14869b6b563cSPaul Mackerras  */
alloc_up(unsigned long size,unsigned long align)14879b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
14889b6b563cSPaul Mackerras {
14895827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
14909b6b563cSPaul Mackerras 	unsigned long addr = 0;
14919b6b563cSPaul Mackerras 
1492c4988820SPaul Mackerras 	if (align)
1493b7115316SChristophe Leroy 		base = ALIGN(base, align);
1494eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx)\n", __func__, size, align);
14955827d416SAnton Blanchard 	if (ram_top == 0)
14969b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
14979b6b563cSPaul Mackerras 
14989b6b563cSPaul Mackerras 	if (align)
1499b7115316SChristophe Leroy 		base = ALIGN(alloc_bottom, align);
15009b6b563cSPaul Mackerras 	else
15015827d416SAnton Blanchard 		base = alloc_bottom;
15029b6b563cSPaul Mackerras 
15035827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
1504b7115316SChristophe Leroy 	    base = ALIGN(base + 0x100000, align)) {
1505eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
15069b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1507c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
15089b6b563cSPaul Mackerras 			break;
15099b6b563cSPaul Mackerras 		addr = 0;
15109b6b563cSPaul Mackerras 		if (align == 0)
15119b6b563cSPaul Mackerras 			break;
15129b6b563cSPaul Mackerras 	}
15139b6b563cSPaul Mackerras 	if (addr == 0)
15149b6b563cSPaul Mackerras 		return 0;
15155827d416SAnton Blanchard 	alloc_bottom = addr + size;
15169b6b563cSPaul Mackerras 
1517eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1518eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1519eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1520eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1521eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1522eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
15239b6b563cSPaul Mackerras 
15249b6b563cSPaul Mackerras 	return addr;
15259b6b563cSPaul Mackerras }
15269b6b563cSPaul Mackerras 
15279b6b563cSPaul Mackerras /*
15289b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
15299b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
15309b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
15319b6b563cSPaul Mackerras  */
alloc_down(unsigned long size,unsigned long align,int highmem)15329b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
15339b6b563cSPaul Mackerras 				       int highmem)
15349b6b563cSPaul Mackerras {
15359b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
15369b6b563cSPaul Mackerras 
1537eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx, %s)\n", __func__, size, align,
15385827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
15395827d416SAnton Blanchard 	if (ram_top == 0)
15409b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
15419b6b563cSPaul Mackerras 
15429b6b563cSPaul Mackerras 	if (highmem) {
15439b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
1544e96d904eSChristophe Leroy 		addr = ALIGN_DOWN(alloc_top_high - size, align);
15455827d416SAnton Blanchard 		if (addr <= alloc_bottom)
15469b6b563cSPaul Mackerras 			return 0;
15479b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
15489b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
15499b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
15509b6b563cSPaul Mackerras 		 */
15515827d416SAnton Blanchard 		if (addr < rmo_top) {
15529b6b563cSPaul Mackerras 			/* Good, we are first */
15535827d416SAnton Blanchard 			if (alloc_top == rmo_top)
15545827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
15559b6b563cSPaul Mackerras 			else
15569b6b563cSPaul Mackerras 				return 0;
15579b6b563cSPaul Mackerras 		}
15585827d416SAnton Blanchard 		alloc_top_high = addr;
15599b6b563cSPaul Mackerras 		goto bail;
15609b6b563cSPaul Mackerras 	}
15619b6b563cSPaul Mackerras 
1562e96d904eSChristophe Leroy 	base = ALIGN_DOWN(alloc_top - size, align);
15635827d416SAnton Blanchard 	for (; base > alloc_bottom;
1564e96d904eSChristophe Leroy 	     base = ALIGN_DOWN(base - 0x100000, align))  {
1565eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
15669b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1567c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
15689b6b563cSPaul Mackerras 			break;
15699b6b563cSPaul Mackerras 		addr = 0;
15709b6b563cSPaul Mackerras 	}
15719b6b563cSPaul Mackerras 	if (addr == 0)
15729b6b563cSPaul Mackerras 		return 0;
15735827d416SAnton Blanchard 	alloc_top = addr;
15749b6b563cSPaul Mackerras 
15759b6b563cSPaul Mackerras  bail:
1576eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1577eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1578eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1579eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1580eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1581eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
15829b6b563cSPaul Mackerras 
15839b6b563cSPaul Mackerras 	return addr;
15849b6b563cSPaul Mackerras }
15859b6b563cSPaul Mackerras 
15869b6b563cSPaul Mackerras /*
15879b6b563cSPaul Mackerras  * Parse a "reg" cell
15889b6b563cSPaul Mackerras  */
prom_next_cell(int s,cell_t ** cellp)15899b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
15909b6b563cSPaul Mackerras {
15919b6b563cSPaul Mackerras 	cell_t *p = *cellp;
15929b6b563cSPaul Mackerras 	unsigned long r = 0;
15939b6b563cSPaul Mackerras 
15949b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
15959b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
15969b6b563cSPaul Mackerras 		p++;
15979b6b563cSPaul Mackerras 		s--;
15989b6b563cSPaul Mackerras 	}
1599493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
16009b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
160135499c01SPaul Mackerras 	if (s > 1) {
16029b6b563cSPaul Mackerras 		r <<= 32;
1603493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
16049b6b563cSPaul Mackerras 	}
16059b6b563cSPaul Mackerras #endif
16069b6b563cSPaul Mackerras 	*cellp = p;
16079b6b563cSPaul Mackerras 	return r;
16089b6b563cSPaul Mackerras }
16099b6b563cSPaul Mackerras 
16109b6b563cSPaul Mackerras /*
16119b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
16129b6b563cSPaul Mackerras  * we don't need anything smarter at this point
16139b6b563cSPaul Mackerras  *
16149b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
16159b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
16169b6b563cSPaul Mackerras  * them down.
16179b6b563cSPaul Mackerras  */
reserve_mem(u64 base,u64 size)16180108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
16199b6b563cSPaul Mackerras {
1620cbbcf340SKumar Gala 	u64 top = base + size;
16215827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
16229b6b563cSPaul Mackerras 
16239b6b563cSPaul Mackerras 	if (size == 0)
16249b6b563cSPaul Mackerras 		return;
16259b6b563cSPaul Mackerras 
16269b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
16279b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
16289b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
16299b6b563cSPaul Mackerras 	 */
1630e96d904eSChristophe Leroy 	base = ALIGN_DOWN(base, PAGE_SIZE);
1631b7115316SChristophe Leroy 	top = ALIGN(top, PAGE_SIZE);
16329b6b563cSPaul Mackerras 	size = top - base;
16339b6b563cSPaul Mackerras 
16349b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
16359b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1636493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1637493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
16385827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
16399b6b563cSPaul Mackerras }
16409b6b563cSPaul Mackerras 
16419b6b563cSPaul Mackerras /*
1642b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
16439b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
16449b6b563cSPaul Mackerras  */
prom_init_mem(void)16459b6b563cSPaul Mackerras static void __init prom_init_mem(void)
16469b6b563cSPaul Mackerras {
16479b6b563cSPaul Mackerras 	phandle node;
1648eab00a20SMathieu Malaterre 	char type[64];
16499b6b563cSPaul Mackerras 	unsigned int plen;
16509b6b563cSPaul Mackerras 	cell_t *p, *endp;
1651493adffcSBenjamin Herrenschmidt 	__be32 val;
16529b6b563cSPaul Mackerras 	u32 rac, rsc;
16539b6b563cSPaul Mackerras 
16549b6b563cSPaul Mackerras 	/*
16559b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
16569b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
16579b6b563cSPaul Mackerras 	 * 2) top of memory
16589b6b563cSPaul Mackerras 	 */
1659493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1660493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1661493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1662493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1663493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1664493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1665493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1666493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
16679b6b563cSPaul Mackerras 
16689b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
16699b6b563cSPaul Mackerras 
16709b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
16719b6b563cSPaul Mackerras 		type[0] = 0;
16729b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
16739b6b563cSPaul Mackerras 
1674c4988820SPaul Mackerras 		if (type[0] == 0) {
1675c4988820SPaul Mackerras 			/*
1676c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1677c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1678c4988820SPaul Mackerras 			 */
1679c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1680c4988820SPaul Mackerras 		}
1681450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "memory"))
16829b6b563cSPaul Mackerras 			continue;
16839b6b563cSPaul Mackerras 
16845827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
16859b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
16869b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
16879b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
16889b6b563cSPaul Mackerras 		}
16895827d416SAnton Blanchard 		p = regbuf;
16909b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
16919b6b563cSPaul Mackerras 
16929b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1693c806a6fdSMathieu Malaterre 		memset(prom_scratch, 0, sizeof(prom_scratch));
1694c806a6fdSMathieu Malaterre 		call_prom("package-to-path", 3, 1, node, prom_scratch,
1695c806a6fdSMathieu Malaterre 			  sizeof(prom_scratch) - 1);
1696c806a6fdSMathieu Malaterre 		prom_debug("  node %s :\n", prom_scratch);
16979b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
16989b6b563cSPaul Mackerras 
16999b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
17009b6b563cSPaul Mackerras 			unsigned long base, size;
17019b6b563cSPaul Mackerras 
17029b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
17039b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
17049b6b563cSPaul Mackerras 
17059b6b563cSPaul Mackerras 			if (size == 0)
17069b6b563cSPaul Mackerras 				continue;
1707eae5f709SMathieu Malaterre 			prom_debug("    %lx %lx\n", base, size);
17085827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
17095827d416SAnton Blanchard 				rmo_top = size;
17105827d416SAnton Blanchard 			if ((base + size) > ram_top)
17115827d416SAnton Blanchard 				ram_top = base + size;
17129b6b563cSPaul Mackerras 		}
17139b6b563cSPaul Mackerras 	}
17149b6b563cSPaul Mackerras 
17155827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
17169b6b563cSPaul Mackerras 
17179b6b563cSPaul Mackerras 	/*
1718cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1719cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1720cf68787bSBenjamin Krill 	 * TCE's up there.
1721cf68787bSBenjamin Krill 	 */
1722cf68787bSBenjamin Krill 
17235827d416SAnton Blanchard 	alloc_top_high = ram_top;
1724cf68787bSBenjamin Krill 
17255827d416SAnton Blanchard 	if (prom_memory_limit) {
17265827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1727eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx <= alloc_bottom.\n",
17285827d416SAnton Blanchard 				    prom_memory_limit);
17295827d416SAnton Blanchard 			prom_memory_limit = 0;
17305827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1731eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx >= ram_top.\n",
17325827d416SAnton Blanchard 				    prom_memory_limit);
17335827d416SAnton Blanchard 			prom_memory_limit = 0;
1734cf68787bSBenjamin Krill 		} else {
17355827d416SAnton Blanchard 			ram_top = prom_memory_limit;
17365827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1737cf68787bSBenjamin Krill 		}
1738cf68787bSBenjamin Krill 	}
1739cf68787bSBenjamin Krill 
1740cf68787bSBenjamin Krill 	/*
17419b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
17429b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
17439b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
17449b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
17459b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
17469b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
17479b6b563cSPaul Mackerras 	 */
17485827d416SAnton Blanchard 	if (!rmo_top)
17495827d416SAnton Blanchard 		rmo_top = ram_top;
17505827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
17515827d416SAnton Blanchard 	alloc_top = rmo_top;
17525827d416SAnton Blanchard 	alloc_top_high = ram_top;
17539b6b563cSPaul Mackerras 
175464968f60SPaul Mackerras 	/*
175564968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
175664968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
175764968f60SPaul Mackerras 	 */
17585827d416SAnton Blanchard 	if (prom_initrd_start &&
17595827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
17605827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
17615827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
176264968f60SPaul Mackerras 
17639b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
1764eae5f709SMathieu Malaterre 	prom_printf("  memory_limit : %lx (16 MB aligned)\n",
1765eae5f709SMathieu Malaterre 		    prom_memory_limit);
1766eae5f709SMathieu Malaterre 	prom_printf("  alloc_bottom : %lx\n", alloc_bottom);
1767eae5f709SMathieu Malaterre 	prom_printf("  alloc_top    : %lx\n", alloc_top);
1768eae5f709SMathieu Malaterre 	prom_printf("  alloc_top_hi : %lx\n", alloc_top_high);
1769eae5f709SMathieu Malaterre 	prom_printf("  rmo_top      : %lx\n", rmo_top);
1770eae5f709SMathieu Malaterre 	prom_printf("  ram_top      : %lx\n", ram_top);
17719b6b563cSPaul Mackerras }
17729b6b563cSPaul Mackerras 
prom_close_stdin(void)177327f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
177427f44888SBenjamin Herrenschmidt {
1775493adffcSBenjamin Herrenschmidt 	__be32 val;
1776493adffcSBenjamin Herrenschmidt 	ihandle stdin;
177727f44888SBenjamin Herrenschmidt 
1778493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1779493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1780493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1781493adffcSBenjamin Herrenschmidt 	}
178227f44888SBenjamin Herrenschmidt }
178327f44888SBenjamin Herrenschmidt 
17846a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
prom_rtas_hcall(uint64_t args)1785d276960dSNick Child static int __init prom_rtas_hcall(uint64_t args)
17866a9c930bSRam Pai {
17876a9c930bSRam Pai 	register uint64_t arg1 asm("r3") = H_RTAS;
17886a9c930bSRam Pai 	register uint64_t arg2 asm("r4") = args;
17896a9c930bSRam Pai 
17906a9c930bSRam Pai 	asm volatile("sc 1\n" : "=r" (arg1) :
17916a9c930bSRam Pai 			"r" (arg1),
17926a9c930bSRam Pai 			"r" (arg2) :);
179359dc5bfcSNicholas Piggin 	srr_regs_clobbered();
179459dc5bfcSNicholas Piggin 
17956a9c930bSRam Pai 	return arg1;
17966a9c930bSRam Pai }
17976a9c930bSRam Pai 
17986a9c930bSRam Pai static struct rtas_args __prombss os_term_args;
17996a9c930bSRam Pai 
prom_rtas_os_term(char * str)18006a9c930bSRam Pai static void __init prom_rtas_os_term(char *str)
18016a9c930bSRam Pai {
18026a9c930bSRam Pai 	phandle rtas_node;
18036a9c930bSRam Pai 	__be32 val;
18046a9c930bSRam Pai 	u32 token;
18056a9c930bSRam Pai 
18066a9c930bSRam Pai 	prom_debug("%s: start...\n", __func__);
18076a9c930bSRam Pai 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
18086a9c930bSRam Pai 	prom_debug("rtas_node: %x\n", rtas_node);
18096a9c930bSRam Pai 	if (!PHANDLE_VALID(rtas_node))
18106a9c930bSRam Pai 		return;
18116a9c930bSRam Pai 
18126a9c930bSRam Pai 	val = 0;
18136a9c930bSRam Pai 	prom_getprop(rtas_node, "ibm,os-term", &val, sizeof(val));
18146a9c930bSRam Pai 	token = be32_to_cpu(val);
18156a9c930bSRam Pai 	prom_debug("ibm,os-term: %x\n", token);
18166a9c930bSRam Pai 	if (token == 0)
18176a9c930bSRam Pai 		prom_panic("Could not get token for ibm,os-term\n");
18186a9c930bSRam Pai 	os_term_args.token = cpu_to_be32(token);
181974bb84e5SAlexey Kardashevskiy 	os_term_args.nargs = cpu_to_be32(1);
182074bb84e5SAlexey Kardashevskiy 	os_term_args.nret = cpu_to_be32(1);
182174bb84e5SAlexey Kardashevskiy 	os_term_args.args[0] = cpu_to_be32(__pa(str));
18226a9c930bSRam Pai 	prom_rtas_hcall((uint64_t)&os_term_args);
18236a9c930bSRam Pai }
18246a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
18256a9c930bSRam Pai 
18269b6b563cSPaul Mackerras /*
18279b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
18289b6b563cSPaul Mackerras  */
prom_instantiate_rtas(void)18299b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
18309b6b563cSPaul Mackerras {
18319b6b563cSPaul Mackerras 	phandle rtas_node;
18329b6b563cSPaul Mackerras 	ihandle rtas_inst;
18339b6b563cSPaul Mackerras 	u32 base, entry = 0;
1834493adffcSBenjamin Herrenschmidt 	__be32 val;
18359b6b563cSPaul Mackerras 	u32 size = 0;
18369b6b563cSPaul Mackerras 
18379b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
18389b6b563cSPaul Mackerras 
18399b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
18409b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
18419b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
18429b6b563cSPaul Mackerras 		return;
18439b6b563cSPaul Mackerras 
1844493adffcSBenjamin Herrenschmidt 	val = 0;
1845493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1846493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
18479b6b563cSPaul Mackerras 	if (size == 0)
18489b6b563cSPaul Mackerras 		return;
18499b6b563cSPaul Mackerras 
18509b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
18516d1e2c6cSAnton Blanchard 	if (base == 0)
18526d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
18539b6b563cSPaul Mackerras 
18549b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
18559b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1856a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
18579b6b563cSPaul Mackerras 		return;
18589b6b563cSPaul Mackerras 	}
18599b6b563cSPaul Mackerras 
18609b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
18619b6b563cSPaul Mackerras 
18629b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
18639b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1864a23414beSPaul Mackerras 			  rtas_inst, base) != 0
18659b6b563cSPaul Mackerras 	    || entry == 0) {
18669b6b563cSPaul Mackerras 		prom_printf(" failed\n");
18679b6b563cSPaul Mackerras 		return;
18689b6b563cSPaul Mackerras 	}
18699b6b563cSPaul Mackerras 	prom_printf(" done\n");
18709b6b563cSPaul Mackerras 
18719b6b563cSPaul Mackerras 	reserve_mem(base, size);
18729b6b563cSPaul Mackerras 
1873493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1874a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1875493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1876493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1877a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1878493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
18799b6b563cSPaul Mackerras 
1880dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1881dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1882dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1883dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1884dbe78b40SBenjamin Herrenschmidt 
18859b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
18869b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
1887eae5f709SMathieu Malaterre 	prom_debug("rtas size     = 0x%x\n", size);
18889b6b563cSPaul Mackerras 
18899b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
18909b6b563cSPaul Mackerras }
18919b6b563cSPaul Mackerras 
18929b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
18939b6b563cSPaul Mackerras /*
18944a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
18954a727429SAshley Lai  */
prom_instantiate_sml(void)18964a727429SAshley Lai static void __init prom_instantiate_sml(void)
18974a727429SAshley Lai {
18984a727429SAshley Lai 	phandle ibmvtpm_node;
18994a727429SAshley Lai 	ihandle ibmvtpm_inst;
1900b4ed0469SHon Ching \(Vicky\) Lo 	u32 entry = 0, size = 0, succ = 0;
19014a727429SAshley Lai 	u64 base;
1902b4ed0469SHon Ching \(Vicky\) Lo 	__be32 val;
19034a727429SAshley Lai 
19044a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
19054a727429SAshley Lai 
19062f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
19074a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
19084a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
19094a727429SAshley Lai 		return;
19104a727429SAshley Lai 
19112f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
19124a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
19134a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
19144a727429SAshley Lai 		return;
19154a727429SAshley Lai 	}
19164a727429SAshley Lai 
1917b4ed0469SHon Ching \(Vicky\) Lo 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1918b4ed0469SHon Ching \(Vicky\) Lo 			 &val, sizeof(val)) != PROM_ERROR) {
1919b4ed0469SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &succ,
1920b4ed0469SHon Ching \(Vicky\) Lo 				  ADDR("reformat-sml-to-efi-alignment"),
1921b4ed0469SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || succ == 0) {
1922b4ed0469SHon Ching \(Vicky\) Lo 			prom_printf("Reformat SML to EFI alignment failed\n");
1923b4ed0469SHon Ching \(Vicky\) Lo 			return;
1924b4ed0469SHon Ching \(Vicky\) Lo 		}
1925b4ed0469SHon Ching \(Vicky\) Lo 
19264a727429SAshley Lai 		if (call_prom_ret("call-method", 2, 2, &size,
19279e5d4af4SHon Ching \(Vicky\) Lo 				  ADDR("sml-get-allocated-size"),
19289e5d4af4SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || size == 0) {
19299e5d4af4SHon Ching \(Vicky\) Lo 			prom_printf("SML get allocated size failed\n");
19309e5d4af4SHon Ching \(Vicky\) Lo 			return;
19319e5d4af4SHon Ching \(Vicky\) Lo 		}
19329e5d4af4SHon Ching \(Vicky\) Lo 	} else {
19339e5d4af4SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &size,
19344a727429SAshley Lai 				  ADDR("sml-get-handover-size"),
19354a727429SAshley Lai 				  ibmvtpm_inst) != 0 || size == 0) {
19364a727429SAshley Lai 			prom_printf("SML get handover size failed\n");
19374a727429SAshley Lai 			return;
19384a727429SAshley Lai 		}
19399e5d4af4SHon Ching \(Vicky\) Lo 	}
19404a727429SAshley Lai 
19414a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
19424a727429SAshley Lai 	if (base == 0)
19434a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
19444a727429SAshley Lai 
1945eae5f709SMathieu Malaterre 	prom_printf("instantiating sml at 0x%llx...", base);
19464a727429SAshley Lai 
19479e5d4af4SHon Ching \(Vicky\) Lo 	memset((void *)base, 0, size);
19489e5d4af4SHon Ching \(Vicky\) Lo 
19494a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
19504a727429SAshley Lai 			  ADDR("sml-handover"),
19514a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
19524a727429SAshley Lai 		prom_printf("SML handover failed\n");
19534a727429SAshley Lai 		return;
19544a727429SAshley Lai 	}
19554a727429SAshley Lai 	prom_printf(" done\n");
19564a727429SAshley Lai 
19574a727429SAshley Lai 	reserve_mem(base, size);
19584a727429SAshley Lai 
19592f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
19604a727429SAshley Lai 		     &base, sizeof(base));
19612f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
19624a727429SAshley Lai 		     &size, sizeof(size));
19634a727429SAshley Lai 
1964eae5f709SMathieu Malaterre 	prom_debug("sml base     = 0x%llx\n", base);
1965eae5f709SMathieu Malaterre 	prom_debug("sml size     = 0x%x\n", size);
19664a727429SAshley Lai 
19674a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
19684a727429SAshley Lai }
19694a727429SAshley Lai 
19704a727429SAshley Lai /*
19719b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
19729b6b563cSPaul Mackerras  */
1973493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
prom_initialize_tce_table(void)19749b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
19759b6b563cSPaul Mackerras {
19769b6b563cSPaul Mackerras 	phandle node;
19779b6b563cSPaul Mackerras 	ihandle phb_node;
19789b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
19795827d416SAnton Blanchard 	char *path = prom_scratch;
19809b6b563cSPaul Mackerras 	u64 base, align;
19819b6b563cSPaul Mackerras 	u32 minalign, minsize;
19829b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
19839b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
19849b6b563cSPaul Mackerras 	u64 i;
19859b6b563cSPaul Mackerras 
19865827d416SAnton Blanchard 	if (prom_iommu_off)
19879b6b563cSPaul Mackerras 		return;
19889b6b563cSPaul Mackerras 
19899b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
19909b6b563cSPaul Mackerras 
19919b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
19925827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
19939b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
19949b6b563cSPaul Mackerras 
19959b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
19969b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
19979b6b563cSPaul Mackerras 		compatible[0] = 0;
19989b6b563cSPaul Mackerras 		type[0] = 0;
19999b6b563cSPaul Mackerras 		model[0] = 0;
20009b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
20019b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
20029b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
20039b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
20049b6b563cSPaul Mackerras 
2005450e7dd4SChristophe Leroy 		if ((type[0] == 0) || (prom_strstr(type, "pci") == NULL))
20069b6b563cSPaul Mackerras 			continue;
20079b6b563cSPaul Mackerras 
2008e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
20099b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
2010450e7dd4SChristophe Leroy 			if ((prom_strstr(compatible, "python") == NULL) &&
2011450e7dd4SChristophe Leroy 			    (prom_strstr(compatible, "Speedwagon") == NULL) &&
2012450e7dd4SChristophe Leroy 			    (prom_strstr(compatible, "Winnipeg") == NULL))
20139b6b563cSPaul Mackerras 				continue;
20149b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
2015450e7dd4SChristophe Leroy 			if ((prom_strstr(model, "ython") == NULL) &&
2016450e7dd4SChristophe Leroy 			    (prom_strstr(model, "peedwagon") == NULL) &&
2017450e7dd4SChristophe Leroy 			    (prom_strstr(model, "innipeg") == NULL))
20189b6b563cSPaul Mackerras 				continue;
20199b6b563cSPaul Mackerras 		}
20209b6b563cSPaul Mackerras 
20219b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
20229b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
20239b6b563cSPaul Mackerras 			minalign = 0;
20249b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
20259b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
20269b6b563cSPaul Mackerras 			minsize = 4UL << 20;
20279b6b563cSPaul Mackerras 
20289b6b563cSPaul Mackerras 		/*
20299b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
20309b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
20319b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
20329b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
20339b6b563cSPaul Mackerras 		 */
20349b6b563cSPaul Mackerras 		minsize = 4UL << 20;
20359b6b563cSPaul Mackerras 
20369b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
20379b6b563cSPaul Mackerras 		align = max(minalign, minsize);
20389b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
20399b6b563cSPaul Mackerras 		if (base == 0)
20409b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
20419b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
20429b6b563cSPaul Mackerras 			local_alloc_bottom = base;
20439b6b563cSPaul Mackerras 
20449b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
2045d7fbe2a0SChristophe Leroy 		memset(path, 0, sizeof(prom_scratch));
20469b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
20479b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
2048d7fbe2a0SChristophe Leroy 			      path, sizeof(prom_scratch) - 1) == PROM_ERROR) {
20499b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
20509b6b563cSPaul Mackerras 		}
20519b6b563cSPaul Mackerras 
2052a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
2053a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
2054a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
2055a23414beSPaul Mackerras 
20569b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
20579b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
2058eae5f709SMathieu Malaterre 		prom_debug("\tbase = 0x%llx\n", base);
20599b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
20609b6b563cSPaul Mackerras 
20619b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
20629b6b563cSPaul Mackerras 		 * over the allocated size.
20639b6b563cSPaul Mackerras 		 */
20642b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
20659b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
20669b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
20679b6b563cSPaul Mackerras 			tce_entry |= 0x3;
20689b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
20699b6b563cSPaul Mackerras 		}
20709b6b563cSPaul Mackerras 
20719b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
20729b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
20739b6b563cSPaul Mackerras 		if (phb_node == 0)
20749b6b563cSPaul Mackerras 			prom_printf("... failed\n");
20759b6b563cSPaul Mackerras 		else
20769b6b563cSPaul Mackerras 			prom_printf("... done\n");
20779b6b563cSPaul Mackerras 
20789b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
20799b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
20809b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
20819b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
20829b6b563cSPaul Mackerras 	}
20839b6b563cSPaul Mackerras 
20849b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
20859b6b563cSPaul Mackerras 
20862babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
20872babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
20885827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
20895827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
20909b6b563cSPaul Mackerras 
20919b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
20929b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
20939b6b563cSPaul Mackerras }
2094493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
2095493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
20969b6b563cSPaul Mackerras 
20979b6b563cSPaul Mackerras /*
20989b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
20999b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
21009b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
21019b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
21029b6b563cSPaul Mackerras  *
21039b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
21049b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
21059b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
21069b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
21079b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
21089b6b563cSPaul Mackerras  * of setting those values.
21099b6b563cSPaul Mackerras  *
21109b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
21119b6b563cSPaul Mackerras  * is in its holding pattern code.
21129b6b563cSPaul Mackerras  *
21139b6b563cSPaul Mackerras  * -- Cort
21149b6b563cSPaul Mackerras  */
2115bbd0abdaSPaul Mackerras /*
2116bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
2117bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
2118bbd0abdaSPaul Mackerras  */
2119bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
2120bbd0abdaSPaul Mackerras 
prom_hold_cpus(void)21219b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
21229b6b563cSPaul Mackerras {
21239b6b563cSPaul Mackerras 	unsigned long i;
21249b6b563cSPaul Mackerras 	phandle node;
21259b6b563cSPaul Mackerras 	char type[64];
21269b6b563cSPaul Mackerras 	unsigned long *spinloop
2127bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
21289b6b563cSPaul Mackerras 	unsigned long *acknowledge
2129bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
2130bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
21319b6b563cSPaul Mackerras 
2132dbe78b40SBenjamin Herrenschmidt 	/*
2133dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
2134dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
2135dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
2136dbe78b40SBenjamin Herrenschmidt 	 */
2137dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
2138dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
2139dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
2140dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
2141dbe78b40SBenjamin Herrenschmidt 		return;
2142dbe78b40SBenjamin Herrenschmidt 	}
2143dbe78b40SBenjamin Herrenschmidt 
21449b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
2145eae5f709SMathieu Malaterre 	prom_debug("    1) spinloop       = 0x%lx\n", (unsigned long)spinloop);
2146eae5f709SMathieu Malaterre 	prom_debug("    1) *spinloop      = 0x%lx\n", *spinloop);
2147eae5f709SMathieu Malaterre 	prom_debug("    1) acknowledge    = 0x%lx\n",
21489b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
2149eae5f709SMathieu Malaterre 	prom_debug("    1) *acknowledge   = 0x%lx\n", *acknowledge);
2150eae5f709SMathieu Malaterre 	prom_debug("    1) secondary_hold = 0x%lx\n", secondary_hold);
21519b6b563cSPaul Mackerras 
21529b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
21539b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
21549b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
21559b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
21569b6b563cSPaul Mackerras 	 */
21579b6b563cSPaul Mackerras 	*spinloop = 0;
21589b6b563cSPaul Mackerras 
21599b6b563cSPaul Mackerras 	/* look for cpus */
21609b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
2161493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
2162493adffcSBenjamin Herrenschmidt 		__be32 reg;
2163493adffcSBenjamin Herrenschmidt 
21649b6b563cSPaul Mackerras 		type[0] = 0;
21659b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
2166450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "cpu") != 0)
21679b6b563cSPaul Mackerras 			continue;
21689b6b563cSPaul Mackerras 
21699b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
21709b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
2171450e7dd4SChristophe Leroy 			if (prom_strcmp(type, "okay") != 0)
21729b6b563cSPaul Mackerras 				continue;
21739b6b563cSPaul Mackerras 
2174493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
21759b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
2176493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
21779b6b563cSPaul Mackerras 
2178eae5f709SMathieu Malaterre 		prom_debug("cpu hw idx   = %u\n", cpu_no);
21799b6b563cSPaul Mackerras 
21809b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
21819b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
21829b6b563cSPaul Mackerras 		 * spinloop.
21839b6b563cSPaul Mackerras 		 */
21849b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
21859b6b563cSPaul Mackerras 
2186493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
218727f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
2188eae5f709SMathieu Malaterre 			prom_printf("starting cpu hw idx %u... ", cpu_no);
21899b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
2190493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
21919b6b563cSPaul Mackerras 
21929b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
21939b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
21949b6b563cSPaul Mackerras 				mb();
21959b6b563cSPaul Mackerras 
2196493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
21979b6b563cSPaul Mackerras 				prom_printf("done\n");
2198bbd0abdaSPaul Mackerras 			else
2199eae5f709SMathieu Malaterre 				prom_printf("failed: %lx\n", *acknowledge);
22009b6b563cSPaul Mackerras 		}
22019b6b563cSPaul Mackerras #ifdef CONFIG_SMP
22029b6b563cSPaul Mackerras 		else
2203eae5f709SMathieu Malaterre 			prom_printf("boot cpu hw idx %u\n", cpu_no);
22049b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
22059b6b563cSPaul Mackerras 	}
22069b6b563cSPaul Mackerras 
22079b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
22089b6b563cSPaul Mackerras }
22099b6b563cSPaul Mackerras 
22109b6b563cSPaul Mackerras 
prom_init_client_services(unsigned long pp)22119b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
22129b6b563cSPaul Mackerras {
22139b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
22145827d416SAnton Blanchard 	prom_entry = pp;
22159b6b563cSPaul Mackerras 
22169b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
22175827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
22185827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
22199b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
22209b6b563cSPaul Mackerras 
22219b6b563cSPaul Mackerras 	/* get device tree root */
22225827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
22235827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
22249b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
2225a575b807SPaul Mackerras 
22265827d416SAnton Blanchard 	prom.mmumap = 0;
22279b6b563cSPaul Mackerras }
22289b6b563cSPaul Mackerras 
2229a575b807SPaul Mackerras #ifdef CONFIG_PPC32
2230a575b807SPaul Mackerras /*
2231a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
2232a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
2233a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
2234a575b807SPaul Mackerras  */
prom_find_mmu(void)2235a575b807SPaul Mackerras static void __init prom_find_mmu(void)
2236a575b807SPaul Mackerras {
2237a575b807SPaul Mackerras 	phandle oprom;
2238a575b807SPaul Mackerras 	char version[64];
2239a575b807SPaul Mackerras 
2240a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
2241a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
2242a575b807SPaul Mackerras 		return;
2243a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
2244a575b807SPaul Mackerras 		return;
2245a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
2246a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
2247450e7dd4SChristophe Leroy 	if (prom_strcmp(version, "Open Firmware, 1.0.5") == 0)
2248a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
2249450e7dd4SChristophe Leroy 	else if (prom_strncmp(version, "FirmWorks,3.", 12) == 0) {
2250a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
2251a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2252a23414beSPaul Mackerras 	} else
2253a575b807SPaul Mackerras 		return;
22545827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
22555827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
22565827d416SAnton Blanchard 		     sizeof(prom.mmumap));
2257493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
22585827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2259a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
2260a575b807SPaul Mackerras }
2261a575b807SPaul Mackerras #else
2262a575b807SPaul Mackerras #define prom_find_mmu()
2263a575b807SPaul Mackerras #endif
2264a575b807SPaul Mackerras 
prom_init_stdout(void)22659b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
22669b6b563cSPaul Mackerras {
22675827d416SAnton Blanchard 	char *path = of_stdout_device;
22689b6b563cSPaul Mackerras 	char type[16];
2269493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
2270493adffcSBenjamin Herrenschmidt 	__be32 val;
22719b6b563cSPaul Mackerras 
22725827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
22739b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
22749b6b563cSPaul Mackerras 
2275493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
22769b6b563cSPaul Mackerras 
22779b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
22789b6b563cSPaul Mackerras 	memset(path, 0, 256);
22795827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
22805827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
22815827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2282450e7dd4SChristophe Leroy 		     path, prom_strlen(path) + 1);
22839b6b563cSPaul Mackerras 
228410348f59SBenjamin Herrenschmidt 	/* instance-to-package fails on PA-Semi */
228510348f59SBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
228610348f59SBenjamin Herrenschmidt 	if (stdout_node != PROM_ERROR) {
228710348f59SBenjamin Herrenschmidt 		val = cpu_to_be32(stdout_node);
228810348f59SBenjamin Herrenschmidt 
22899b6b563cSPaul Mackerras 		/* If it's a display, note it */
22909b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
2291493adffcSBenjamin Herrenschmidt 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
2292450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "display") == 0)
2293493adffcSBenjamin Herrenschmidt 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
22949b6b563cSPaul Mackerras 	}
229510348f59SBenjamin Herrenschmidt }
22969b6b563cSPaul Mackerras 
prom_find_machine_type(void)22979b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
22989b6b563cSPaul Mackerras {
2299ca5dabcfSChristophe Leroy 	static char compat[256] __prombss;
23009b6b563cSPaul Mackerras 	int len, i = 0;
230121fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
23029b6b563cSPaul Mackerras 	phandle rtas;
2303e8222502SBenjamin Herrenschmidt 	int x;
230421fe3301SBenjamin Herrenschmidt #endif
2305e8222502SBenjamin Herrenschmidt 
23066e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
23075827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
23089b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
23099b6b563cSPaul Mackerras 	if (len > 0) {
23109b6b563cSPaul Mackerras 		compat[len] = 0;
23119b6b563cSPaul Mackerras 		while (i < len) {
23129b6b563cSPaul Mackerras 			char *p = &compat[i];
2313450e7dd4SChristophe Leroy 			int sl = prom_strlen(p);
23149b6b563cSPaul Mackerras 			if (sl == 0)
23159b6b563cSPaul Mackerras 				break;
2316450e7dd4SChristophe Leroy 			if (prom_strstr(p, "Power Macintosh") ||
2317450e7dd4SChristophe Leroy 			    prom_strstr(p, "MacRISC"))
23189b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
2319133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
2320133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
2321133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
2322133dda1eSArnd Bergmann 			 * so we do it here.
2323133dda1eSArnd Bergmann 			 */
2324450e7dd4SChristophe Leroy 			if (prom_strstr(p, "IBM,CBEA") ||
2325450e7dd4SChristophe Leroy 			    prom_strstr(p, "IBM,CPBW-1.0"))
2326133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
2327133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
23289b6b563cSPaul Mackerras 			i += sl + 1;
23299b6b563cSPaul Mackerras 		}
23309b6b563cSPaul Mackerras 	}
23319b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
23326e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
2333e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
2334e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
2335e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
2336e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
2337e8222502SBenjamin Herrenschmidt 	 */
23385827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
2339e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
2340e8222502SBenjamin Herrenschmidt 	if (len <= 0)
2341e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2342450e7dd4SChristophe Leroy 	if (prom_strcmp(compat, "chrp"))
2343e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2344e8222502SBenjamin Herrenschmidt 
23459b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
23469b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2347e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
2348e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2349e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
23509b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
23514da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
23529b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
23539b6b563cSPaul Mackerras 	}
23549b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
23559b6b563cSPaul Mackerras #else
2356e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
23579b6b563cSPaul Mackerras #endif
23589b6b563cSPaul Mackerras }
23599b6b563cSPaul Mackerras 
prom_set_color(ihandle ih,int i,int r,int g,int b)23609b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
23619b6b563cSPaul Mackerras {
23629b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
23639b6b563cSPaul Mackerras }
23649b6b563cSPaul Mackerras 
23659b6b563cSPaul Mackerras /*
23669b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
23679b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
23689b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
23699b6b563cSPaul Mackerras  * we've taken over the MMU.
23709b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
23719b6b563cSPaul Mackerras  * and if so, open it now.
23729b6b563cSPaul Mackerras  */
prom_check_displays(void)23739b6b563cSPaul Mackerras static void __init prom_check_displays(void)
23749b6b563cSPaul Mackerras {
23759b6b563cSPaul Mackerras 	char type[16], *path;
23769b6b563cSPaul Mackerras 	phandle node;
23779b6b563cSPaul Mackerras 	ihandle ih;
23789b6b563cSPaul Mackerras 	int i;
23799b6b563cSPaul Mackerras 
2380d00e34b9SBenjamin Herrenschmidt 	static const unsigned char default_colors[] __initconst = {
23819b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
23829b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
23839b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
23849b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
23859b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
23869b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
23879b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
23889b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
23899b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
23909b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
23919b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
23929b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
23939b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
23949b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
23959b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
23969b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
23979b6b563cSPaul Mackerras 	};
23989b6b563cSPaul Mackerras 	const unsigned char *clut;
23999b6b563cSPaul Mackerras 
24004da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
24019b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
24029b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
24039b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
2404450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "display") != 0)
24059b6b563cSPaul Mackerras 			continue;
24069b6b563cSPaul Mackerras 
24079b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
24085827d416SAnton Blanchard 		path = prom_scratch;
2409d7fbe2a0SChristophe Leroy 		memset(path, 0, sizeof(prom_scratch));
24109b6b563cSPaul Mackerras 
24119b6b563cSPaul Mackerras 		/*
24129b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
24139b6b563cSPaul Mackerras 		 * arguments
24149b6b563cSPaul Mackerras 		 */
24159b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
2416d7fbe2a0SChristophe Leroy 			      sizeof(prom_scratch) - 10) == PROM_ERROR)
24179b6b563cSPaul Mackerras 			continue;
24189b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
24199b6b563cSPaul Mackerras 
24209b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
24219b6b563cSPaul Mackerras 		if (ih == 0) {
24229b6b563cSPaul Mackerras 			prom_printf("failed\n");
24239b6b563cSPaul Mackerras 			continue;
24249b6b563cSPaul Mackerras 		}
24259b6b563cSPaul Mackerras 
24269b6b563cSPaul Mackerras 		/* Success */
24279b6b563cSPaul Mackerras 		prom_printf("done\n");
2428a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
24299b6b563cSPaul Mackerras 
24309b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
24319b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
24325827d416SAnton Blanchard 		clut = default_colors;
24333f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
24349b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
24359b6b563cSPaul Mackerras 					   clut[2]) != 0)
24369b6b563cSPaul Mackerras 				break;
24379b6b563cSPaul Mackerras 
24389b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
24395827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
24405827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
24419b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
24429b6b563cSPaul Mackerras 					   clut[2]) != 0)
24439b6b563cSPaul Mackerras 				break;
24449b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
24457191b615SBenjamin Herrenschmidt 
24467191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
24477191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
24487191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
24497191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
24507191b615SBenjamin Herrenschmidt 
24517191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
24526c71cfccSMichael Ellerman 
24536c71cfccSMichael Ellerman 			if (prom_getprop(node, "width", &width, 4) == PROM_ERROR)
24546c71cfccSMichael Ellerman 				return;
24556c71cfccSMichael Ellerman 
24566c71cfccSMichael Ellerman 			if (prom_getprop(node, "height", &height, 4) == PROM_ERROR)
24576c71cfccSMichael Ellerman 				return;
24586c71cfccSMichael Ellerman 
24596c71cfccSMichael Ellerman 			if (prom_getprop(node, "linebytes", &pitch, 4) == PROM_ERROR)
24606c71cfccSMichael Ellerman 				return;
24616c71cfccSMichael Ellerman 
24626c71cfccSMichael Ellerman 			if (prom_getprop(node, "address", &addr, 4) == PROM_ERROR)
24636c71cfccSMichael Ellerman 				return;
24646c71cfccSMichael Ellerman 
24657191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
24667191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
24677191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
2468c21f5a9eSChristophe Leroy 			btext_prepare_BAT();
24697191b615SBenjamin Herrenschmidt 		}
24707191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
24719b6b563cSPaul Mackerras 	}
24729b6b563cSPaul Mackerras }
24739b6b563cSPaul Mackerras 
24749b6b563cSPaul Mackerras 
24759b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
make_room(unsigned long * mem_start,unsigned long * mem_end,unsigned long needed,unsigned long align)24769b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
24779b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
24789b6b563cSPaul Mackerras {
24799b6b563cSPaul Mackerras 	void *ret;
24809b6b563cSPaul Mackerras 
2481d3f3d3bfSChristophe Leroy 	*mem_start = ALIGN(*mem_start, align);
24829b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
24839b6b563cSPaul Mackerras 		unsigned long room, chunk;
24849b6b563cSPaul Mackerras 
2485eae5f709SMathieu Malaterre 		prom_debug("Chunk exhausted, claiming more at %lx...\n",
24865827d416SAnton Blanchard 			   alloc_bottom);
24875827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
24889b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
24899b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
24909b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2491fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2492fbafd728SAnton Blanchard 				   "(no room)\n");
24939b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
24949b6b563cSPaul Mackerras 		if (chunk == 0)
2495fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2496fbafd728SAnton Blanchard 				   "(claim failed)\n");
2497966728ddSAnton Blanchard 		*mem_end = chunk + room;
24989b6b563cSPaul Mackerras 	}
24999b6b563cSPaul Mackerras 
25009b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
25019b6b563cSPaul Mackerras 	*mem_start += needed;
25029b6b563cSPaul Mackerras 
25039b6b563cSPaul Mackerras 	return ret;
25049b6b563cSPaul Mackerras }
25059b6b563cSPaul Mackerras 
2506493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2507493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2508493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2509493adffcSBenjamin Herrenschmidt 	} while(0)
25109b6b563cSPaul Mackerras 
dt_find_string(char * str)25119b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
25129b6b563cSPaul Mackerras {
25139b6b563cSPaul Mackerras 	char *s, *os;
25149b6b563cSPaul Mackerras 
25155827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
25169b6b563cSPaul Mackerras 	s += 4;
25175827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
2518450e7dd4SChristophe Leroy 		if (prom_strcmp(s, str) == 0)
25199b6b563cSPaul Mackerras 			return s - os;
2520450e7dd4SChristophe Leroy 		s += prom_strlen(s) + 1;
25219b6b563cSPaul Mackerras 	}
25229b6b563cSPaul Mackerras 	return 0;
25239b6b563cSPaul Mackerras }
25249b6b563cSPaul Mackerras 
25259b6b563cSPaul Mackerras /*
25269b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
25279b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
25289b6b563cSPaul Mackerras  */
25299b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
25309b6b563cSPaul Mackerras 
scan_dt_build_strings(phandle node,unsigned long * mem_start,unsigned long * mem_end)25319b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
25329b6b563cSPaul Mackerras 					 unsigned long *mem_start,
25339b6b563cSPaul Mackerras 					 unsigned long *mem_end)
25349b6b563cSPaul Mackerras {
25359b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
25369b6b563cSPaul Mackerras 	unsigned long soff;
25379b6b563cSPaul Mackerras 	phandle child;
25389b6b563cSPaul Mackerras 
25395827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
25409b6b563cSPaul Mackerras 
25419b6b563cSPaul Mackerras 	/* get and store all property names */
25425827d416SAnton Blanchard 	prev_name = "";
25439b6b563cSPaul Mackerras 	for (;;) {
25449b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
25459b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
25469b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
25479b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
25489b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
25499b6b563cSPaul Mackerras 			break;
25509b6b563cSPaul Mackerras 		}
25519b6b563cSPaul Mackerras 
25529b6b563cSPaul Mackerras  		/* skip "name" */
2553450e7dd4SChristophe Leroy 		if (prom_strcmp(namep, "name") == 0) {
25549b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
25555827d416SAnton Blanchard  			prev_name = "name";
25569b6b563cSPaul Mackerras  			continue;
25579b6b563cSPaul Mackerras  		}
25589b6b563cSPaul Mackerras 		/* get/create string entry */
25599b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
25609b6b563cSPaul Mackerras 		if (soff != 0) {
25619b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
25629b6b563cSPaul Mackerras 			namep = sstart + soff;
25639b6b563cSPaul Mackerras 		} else {
25649b6b563cSPaul Mackerras 			/* Trim off some if we can */
2565450e7dd4SChristophe Leroy 			*mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
25665827d416SAnton Blanchard 			dt_string_end = *mem_start;
25679b6b563cSPaul Mackerras 		}
25689b6b563cSPaul Mackerras 		prev_name = namep;
25699b6b563cSPaul Mackerras 	}
25709b6b563cSPaul Mackerras 
25719b6b563cSPaul Mackerras 	/* do all our children */
25729b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
25739b6b563cSPaul Mackerras 	while (child != 0) {
25749b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
25759b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
25769b6b563cSPaul Mackerras 	}
25779b6b563cSPaul Mackerras }
25789b6b563cSPaul Mackerras 
scan_dt_build_struct(phandle node,unsigned long * mem_start,unsigned long * mem_end)25799b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
25809b6b563cSPaul Mackerras 					unsigned long *mem_end)
25819b6b563cSPaul Mackerras {
25829b6b563cSPaul Mackerras 	phandle child;
25839b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
25849b6b563cSPaul Mackerras 	unsigned long soff;
25859b6b563cSPaul Mackerras 	unsigned char *valp;
25868ca2d515SBenjamin Herrenschmidt 	static char pname[MAX_PROPERTY_NAME] __prombss;
25876e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
25889b6b563cSPaul Mackerras 
25899b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
25909b6b563cSPaul Mackerras 
25919b6b563cSPaul Mackerras 	/* get the node's full name */
25929b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2593c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2594c4988820SPaul Mackerras 	if (room > 255)
2595c4988820SPaul Mackerras 		room = 255;
2596c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
25979b6b563cSPaul Mackerras 	if (l >= 0) {
25989b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2599c4988820SPaul Mackerras 		if (l >= room) {
2600c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
26019b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
26029b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
26039b6b563cSPaul Mackerras 		}
26049b6b563cSPaul Mackerras 		namep[l] = '\0';
26059b6b563cSPaul Mackerras 
26069b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2607a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2608a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
26099b6b563cSPaul Mackerras 		 */
2610a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
26119b6b563cSPaul Mackerras 			if (*p == '/')
2612a575b807SPaul Mackerras 				lp = namep;
2613a575b807SPaul Mackerras 			else if (*p != 0)
2614a575b807SPaul Mackerras 				*lp++ = *p;
2615a575b807SPaul Mackerras 		}
2616a575b807SPaul Mackerras 		*lp = 0;
2617d3f3d3bfSChristophe Leroy 		*mem_start = ALIGN((unsigned long)lp + 1, 4);
26189b6b563cSPaul Mackerras 	}
26199b6b563cSPaul Mackerras 
26209b6b563cSPaul Mackerras 	/* get it again for debugging */
26215827d416SAnton Blanchard 	path = prom_scratch;
2622d7fbe2a0SChristophe Leroy 	memset(path, 0, sizeof(prom_scratch));
2623d7fbe2a0SChristophe Leroy 	call_prom("package-to-path", 3, 1, node, path, sizeof(prom_scratch) - 1);
26249b6b563cSPaul Mackerras 
26259b6b563cSPaul Mackerras 	/* get and store all properties */
26265827d416SAnton Blanchard 	prev_name = "";
26275827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
26289b6b563cSPaul Mackerras 	for (;;) {
26299b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
26305827d416SAnton Blanchard 			      pname) != 1)
26319b6b563cSPaul Mackerras 			break;
26329b6b563cSPaul Mackerras 
26339b6b563cSPaul Mackerras  		/* skip "name" */
2634450e7dd4SChristophe Leroy 		if (prom_strcmp(pname, "name") == 0) {
26355827d416SAnton Blanchard  			prev_name = "name";
26369b6b563cSPaul Mackerras  			continue;
26379b6b563cSPaul Mackerras  		}
26389b6b563cSPaul Mackerras 
26399b6b563cSPaul Mackerras 		/* find string offset */
26405827d416SAnton Blanchard 		soff = dt_find_string(pname);
26419b6b563cSPaul Mackerras 		if (soff == 0) {
26429b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
26435827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
26449b6b563cSPaul Mackerras 			break;
26459b6b563cSPaul Mackerras 		}
26469b6b563cSPaul Mackerras 		prev_name = sstart + soff;
26479b6b563cSPaul Mackerras 
26489b6b563cSPaul Mackerras 		/* get length */
26495827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
26509b6b563cSPaul Mackerras 
26519b6b563cSPaul Mackerras 		/* sanity checks */
26529b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
26539b6b563cSPaul Mackerras 			continue;
26549b6b563cSPaul Mackerras 
26559b6b563cSPaul Mackerras 		/* push property head */
26569b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
26579b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
26589b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
26599b6b563cSPaul Mackerras 
26609b6b563cSPaul Mackerras 		/* push property content */
26619b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
26625827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
2663d3f3d3bfSChristophe Leroy 		*mem_start = ALIGN(*mem_start, 4);
26646e35d5daSBenjamin Herrenschmidt 
2665450e7dd4SChristophe Leroy 		if (!prom_strcmp(pname, "phandle"))
26666e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
26679b6b563cSPaul Mackerras 	}
26689b6b563cSPaul Mackerras 
2669f1f208e5SBenjamin Herrenschmidt 	/* Add a "phandle" property if none already exist */
26706e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
2671f1f208e5SBenjamin Herrenschmidt 		soff = dt_find_string("phandle");
26729b6b563cSPaul Mackerras 		if (soff == 0)
2673f1f208e5SBenjamin Herrenschmidt 			prom_printf("WARNING: Can't find string index for <phandle> node %s\n", path);
26749b6b563cSPaul Mackerras 		else {
26759b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
26769b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
26779b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
26789b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
2679493adffcSBenjamin Herrenschmidt 			*(__be32 *)valp = cpu_to_be32(node);
26809b6b563cSPaul Mackerras 		}
26816e35d5daSBenjamin Herrenschmidt 	}
26829b6b563cSPaul Mackerras 
26839b6b563cSPaul Mackerras 	/* do all our children */
26849b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
26859b6b563cSPaul Mackerras 	while (child != 0) {
26869b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
26879b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
26889b6b563cSPaul Mackerras 	}
26899b6b563cSPaul Mackerras 
26909b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
26919b6b563cSPaul Mackerras }
26929b6b563cSPaul Mackerras 
flatten_device_tree(void)26939b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
26949b6b563cSPaul Mackerras {
26959b6b563cSPaul Mackerras 	phandle root;
26969b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
26979b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
26989b6b563cSPaul Mackerras 	char *namep;
26999b6b563cSPaul Mackerras 	u64 *rsvmap;
27009b6b563cSPaul Mackerras 
27019b6b563cSPaul Mackerras 	/*
27029b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2703fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
27049b6b563cSPaul Mackerras 	 */
27055827d416SAnton Blanchard 	room = alloc_top - alloc_bottom - 0x4000;
27069b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
27079b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
2708eae5f709SMathieu Malaterre 	prom_debug("starting device tree allocs at %lx\n", alloc_bottom);
27099b6b563cSPaul Mackerras 
27109b6b563cSPaul Mackerras 	/* Now try to claim that */
27119b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
27129b6b563cSPaul Mackerras 	if (mem_start == 0)
27139b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2714966728ddSAnton Blanchard 	mem_end = mem_start + room;
27159b6b563cSPaul Mackerras 
27169b6b563cSPaul Mackerras 	/* Get root of tree */
27179b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
27189b6b563cSPaul Mackerras 	if (root == (phandle)0)
27199b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
27209b6b563cSPaul Mackerras 
27219b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
2722d3f3d3bfSChristophe Leroy 	mem_start = ALIGN(mem_start, 4);
27239b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
27249b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
27255827d416SAnton Blanchard 	dt_header_start = (unsigned long)hdr;
27269b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
27279b6b563cSPaul Mackerras 
27289b6b563cSPaul Mackerras 	/* Start of strings */
27299b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
27305827d416SAnton Blanchard 	dt_string_start = mem_start;
27319b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
27329b6b563cSPaul Mackerras 
2733f1f208e5SBenjamin Herrenschmidt 	/* Add "phandle" in there, we'll need it */
27349b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
2735f47d5a4fSMichael Ellerman 	prom_strscpy_pad(namep, "phandle", sizeof("phandle"));
2736450e7dd4SChristophe Leroy 	mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
27379b6b563cSPaul Mackerras 
27389b6b563cSPaul Mackerras 	/* Build string array */
27399b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
27409b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
27415827d416SAnton Blanchard 	dt_string_end = mem_start;
27429b6b563cSPaul Mackerras 
27439b6b563cSPaul Mackerras 	/* Build structure */
27449b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
27455827d416SAnton Blanchard 	dt_struct_start = mem_start;
27469b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
27479b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
27489b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
27495827d416SAnton Blanchard 	dt_struct_end = PAGE_ALIGN(mem_start);
27509b6b563cSPaul Mackerras 
27519b6b563cSPaul Mackerras 	/* Finish header */
2752493adffcSBenjamin Herrenschmidt 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2753493adffcSBenjamin Herrenschmidt 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2754493adffcSBenjamin Herrenschmidt 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2755493adffcSBenjamin Herrenschmidt 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2756493adffcSBenjamin Herrenschmidt 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2757493adffcSBenjamin Herrenschmidt 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2758493adffcSBenjamin Herrenschmidt 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2759493adffcSBenjamin Herrenschmidt 	hdr->version = cpu_to_be32(OF_DT_VERSION);
27609b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
2761493adffcSBenjamin Herrenschmidt 	hdr->last_comp_version = cpu_to_be32(0x10);
27629b6b563cSPaul Mackerras 
27634d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
27645827d416SAnton Blanchard 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
27659b6b563cSPaul Mackerras 
27669b6b563cSPaul Mackerras #ifdef DEBUG_PROM
27679b6b563cSPaul Mackerras 	{
27689b6b563cSPaul Mackerras 		int i;
27699b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
27705827d416SAnton Blanchard 		for (i = 0; i < mem_reserve_cnt; i++)
2771eae5f709SMathieu Malaterre 			prom_printf("  %llx - %llx\n",
2772493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].base),
2773493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].size));
27749b6b563cSPaul Mackerras 	}
27759b6b563cSPaul Mackerras #endif
27764d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
27774d1f3f25SJimi Xenidis 	 * since it's too late.
27784d1f3f25SJimi Xenidis 	 */
27795827d416SAnton Blanchard 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
27809b6b563cSPaul Mackerras 
2781eae5f709SMathieu Malaterre 	prom_printf("Device tree strings 0x%lx -> 0x%lx\n",
27825827d416SAnton Blanchard 		    dt_string_start, dt_string_end);
2783eae5f709SMathieu Malaterre 	prom_printf("Device tree struct  0x%lx -> 0x%lx\n",
27845827d416SAnton Blanchard 		    dt_struct_start, dt_struct_end);
27859b6b563cSPaul Mackerras }
27869b6b563cSPaul Mackerras 
278754f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
278854f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
278954f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
fixup_device_tree_maple(void)279054f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
27919b6b563cSPaul Mackerras {
279254f4ee18SHollis Blanchard 	phandle isa;
2793980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
279454f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2795980a6513SBenjamin Herrenschmidt 	char *name;
279654f4ee18SHollis Blanchard 
2797980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2798980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2799980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2800980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2801980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2802980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2803980a6513SBenjamin Herrenschmidt 	}
280454f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
280554f4ee18SHollis Blanchard 		return;
280654f4ee18SHollis Blanchard 
2807980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2808980a6513SBenjamin Herrenschmidt 		return;
280954f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
281054f4ee18SHollis Blanchard 		== PROM_ERROR)
281154f4ee18SHollis Blanchard 		return;
281254f4ee18SHollis Blanchard 
281354f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
281454f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
281554f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
281654f4ee18SHollis Blanchard 		return;
281754f4ee18SHollis Blanchard 
2818980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
281954f4ee18SHollis Blanchard 
282054f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
282154f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2822980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
282354f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
282454f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
282554f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2826980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
282754f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
282854f4ee18SHollis Blanchard }
28298f101a05SHarry Ciao 
28308f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
28318f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
28328f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
fixup_device_tree_maple_memory_controller(void)28338f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
28348f101a05SHarry Ciao {
28358f101a05SHarry Ciao 	phandle mc;
28368f101a05SHarry Ciao 	u32 mc_reg[4];
28378f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
28388f101a05SHarry Ciao 	u32 ac, sc;
28398f101a05SHarry Ciao 
28408f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
28418f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
28428f101a05SHarry Ciao 		return;
28438f101a05SHarry Ciao 
28448f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
28458f101a05SHarry Ciao 		return;
28468f101a05SHarry Ciao 
28475827d416SAnton Blanchard 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
28485827d416SAnton Blanchard 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
28498f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
28508f101a05SHarry Ciao 		return;
28518f101a05SHarry Ciao 
28528f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
28538f101a05SHarry Ciao 		return;
28548f101a05SHarry Ciao 
28558f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
28568f101a05SHarry Ciao 		return;
28578f101a05SHarry Ciao 
28588f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
28598f101a05SHarry Ciao 
28608f101a05SHarry Ciao 	mc_reg[0] = 0x0;
28618f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
28628f101a05SHarry Ciao 	mc_reg[2] = 0x0;
28638f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
28648f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
28658f101a05SHarry Ciao }
286654f4ee18SHollis Blanchard #else
286754f4ee18SHollis Blanchard #define fixup_device_tree_maple()
28688f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
286954f4ee18SHollis Blanchard #endif
287054f4ee18SHollis Blanchard 
2871e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2872e4805922SOlaf Hering /*
2873e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2874e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2875556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2876e4805922SOlaf Hering  */
fixup_device_tree_chrp(void)2877e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2878e8c0acf9SBenjamin Herrenschmidt {
2879e4805922SOlaf Hering 	phandle ph;
2880e4805922SOlaf Hering 	u32 prop[6];
288126c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2882e8c0acf9SBenjamin Herrenschmidt 	char *name;
2883e8c0acf9SBenjamin Herrenschmidt 	int rc;
2884e8c0acf9SBenjamin Herrenschmidt 
2885e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2886e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2887e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
288826c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2889e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
289026c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
289126c5032eSBenjamin Herrenschmidt 	}
2892e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2893e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2894e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2895e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2896e8c0acf9SBenjamin Herrenschmidt 
2897e4805922SOlaf Hering 			prop[0] = 0x1;
2898e4805922SOlaf Hering 			prop[1] = 0x0;
2899e4805922SOlaf Hering 			prop[2] = rloc;
2900e4805922SOlaf Hering 			prop[3] = 0x0;
2901e4805922SOlaf Hering 			prop[4] = 0x0;
2902e4805922SOlaf Hering 			prop[5] = 0x00010000;
2903e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2904e4805922SOlaf Hering 		}
2905e4805922SOlaf Hering 	}
2906e4805922SOlaf Hering 
2907e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2908e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2909e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2910e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2911e4805922SOlaf Hering 		prop[0] = 14;
2912e4805922SOlaf Hering 		prop[1] = 0x0;
2913556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2914556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2915556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2916556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2917556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2918556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2919556ecf9bSOlaf Hering 		}
2920e4805922SOlaf Hering 	}
2921e8c0acf9SBenjamin Herrenschmidt }
2922e8c0acf9SBenjamin Herrenschmidt #else
2923e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2924e8c0acf9SBenjamin Herrenschmidt #endif
2925e8c0acf9SBenjamin Herrenschmidt 
29269b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
fixup_device_tree_pmac64(void)2927*691284c2SMichael Ellerman static void __init fixup_device_tree_pmac64(void)
292854f4ee18SHollis Blanchard {
29299b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
29309b6b563cSPaul Mackerras 	u32 u3_rev;
29319b6b563cSPaul Mackerras 	u32 interrupts[2];
29329b6b563cSPaul Mackerras 	u32 parent;
29339b6b563cSPaul Mackerras 
29349b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
29359b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
29369b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
29379b6b563cSPaul Mackerras 		return;
29389b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
29399b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
29409b6b563cSPaul Mackerras 		return;
29419b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
29429b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
29439b6b563cSPaul Mackerras 		return;
29449b6b563cSPaul Mackerras 
29459b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
29469b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
29479b6b563cSPaul Mackerras 	    == PROM_ERROR)
29489b6b563cSPaul Mackerras 		return;
29497d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
29509b6b563cSPaul Mackerras 		return;
29519b6b563cSPaul Mackerras 	/* does it need fixup ? */
29529b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
29539b6b563cSPaul Mackerras 		return;
29549b6b563cSPaul Mackerras 
29559b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
29569b6b563cSPaul Mackerras 
29579b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
29589b6b563cSPaul Mackerras 	interrupts[0] = 0;
29599b6b563cSPaul Mackerras 	interrupts[1] = 1;
2960a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2961a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
29629b6b563cSPaul Mackerras 	parent = (u32)mpic;
2963a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2964a23414beSPaul Mackerras 		     &parent, sizeof(parent));
29659b6b563cSPaul Mackerras }
296654f4ee18SHollis Blanchard #else
2967*691284c2SMichael Ellerman #define fixup_device_tree_pmac64()
2968*691284c2SMichael Ellerman #endif
2969*691284c2SMichael Ellerman 
2970*691284c2SMichael Ellerman #ifdef CONFIG_PPC_PMAC
fixup_device_tree_pmac(void)2971*691284c2SMichael Ellerman static void __init fixup_device_tree_pmac(void)
2972*691284c2SMichael Ellerman {
2973*691284c2SMichael Ellerman 	__be32 val = 1;
2974*691284c2SMichael Ellerman 	char type[8];
2975*691284c2SMichael Ellerman 	phandle node;
2976*691284c2SMichael Ellerman 
2977*691284c2SMichael Ellerman 	// Some pmacs are missing #size-cells on escc nodes
2978*691284c2SMichael Ellerman 	for (node = 0; prom_next_node(&node); ) {
2979*691284c2SMichael Ellerman 		type[0] = '\0';
2980*691284c2SMichael Ellerman 		prom_getprop(node, "device_type", type, sizeof(type));
2981*691284c2SMichael Ellerman 		if (prom_strcmp(type, "escc"))
2982*691284c2SMichael Ellerman 			continue;
2983*691284c2SMichael Ellerman 
2984*691284c2SMichael Ellerman 		if (prom_getproplen(node, "#size-cells") != PROM_ERROR)
2985*691284c2SMichael Ellerman 			continue;
2986*691284c2SMichael Ellerman 
2987*691284c2SMichael Ellerman 		prom_setprop(node, NULL, "#size-cells", &val, sizeof(val));
2988*691284c2SMichael Ellerman 	}
2989*691284c2SMichael Ellerman }
2990*691284c2SMichael Ellerman #else
fixup_device_tree_pmac(void)2991*691284c2SMichael Ellerman static inline void fixup_device_tree_pmac(void) { }
299254f4ee18SHollis Blanchard #endif
29939b6b563cSPaul Mackerras 
299488fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
299594d2dde7SGrant Likely /*
299694d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
299794d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
299894d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
299994d2dde7SGrant Likely  * ethernet node.
300094d2dde7SGrant Likely  */
fixup_device_tree_efika_add_phy(void)300194d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
300288fd2a9dSSylvain Munaut {
300388fd2a9dSSylvain Munaut 	u32 node;
300488fd2a9dSSylvain Munaut 	char prop[64];
300594d2dde7SGrant Likely 	int rv;
300688fd2a9dSSylvain Munaut 
300794d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
300894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
300988fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
301088fd2a9dSSylvain Munaut 		return;
301188fd2a9dSSylvain Munaut 
301294d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
301394d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
3014869fb7e5SPeiwei Hu 	if (rv <= 0)
301588fd2a9dSSylvain Munaut 		return;
301688fd2a9dSSylvain Munaut 
301794d2dde7SGrant Likely 	/*
301894d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
301994d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
302094d2dde7SGrant Likely 	 */
302188fd2a9dSSylvain Munaut 
302294d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
30236f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
30246f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
30256f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
30266f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
30276f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
30286f4347c9SOlaf Hering 			" new-device"
30296f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
30306f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
303194d2dde7SGrant Likely 				" s\" mdio\" device-name"
303294d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
30336f4347c9SOlaf Hering 				" s\" compatible\" property"
30346f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
30356f4347c9SOlaf Hering 				" 0x2 encode-int"
30366f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
30376f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
30386f4347c9SOlaf Hering 				" s\" interrupts\" property"
30396f4347c9SOlaf Hering 			" finish-device");
30404f46d57cSJiapeng Chong 	}
30416f4347c9SOlaf Hering 
304294d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
304394d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
304494d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
304594d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
30466f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
30476f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
30486f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
30496f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
30506f4347c9SOlaf Hering 			" new-device"
30516f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
30526f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
30536f4347c9SOlaf Hering 				" my-self"
30546f4347c9SOlaf Hering 				" ihandle>phandle"
30556f4347c9SOlaf Hering 			" finish-device"
30566f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
30576f4347c9SOlaf Hering 				" encode-int"
30586f4347c9SOlaf Hering 				" s\" phy-handle\" property"
30596f4347c9SOlaf Hering 			" device-end");
30606f4347c9SOlaf Hering 	}
306194d2dde7SGrant Likely }
30626f4347c9SOlaf Hering 
fixup_device_tree_efika(void)306394d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
306494d2dde7SGrant Likely {
306594d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
306694d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
306794d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
306894d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
306994d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
307094d2dde7SGrant Likely 	u32 node;
307194d2dde7SGrant Likely 	char prop[64];
307294d2dde7SGrant Likely 	int rv, len;
307394d2dde7SGrant Likely 
307494d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
307594d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
307694d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
307794d2dde7SGrant Likely 		return;
307894d2dde7SGrant Likely 
307994d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
308094d2dde7SGrant Likely 	if (rv == PROM_ERROR)
308194d2dde7SGrant Likely 		return;
3082450e7dd4SChristophe Leroy 	if (prom_strcmp(prop, "EFIKA5K2"))
308394d2dde7SGrant Likely 		return;
308494d2dde7SGrant Likely 
308594d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
308694d2dde7SGrant Likely 
308794d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
308894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
308994d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
3090450e7dd4SChristophe Leroy 	if (rv != PROM_ERROR && (prom_strcmp(prop, "chrp") == 0))
309194d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
309294d2dde7SGrant Likely 
30937f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
30947f4392cdSDavid Woodhouse 	   fix that too */
30957f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
3096450e7dd4SChristophe Leroy 	if (rv != PROM_ERROR && (prom_strstr(prop, "CHRP")))
30977f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
30987f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
30997f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
31007f4392cdSDavid Woodhouse 
310194d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
310294d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
310394d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
310494d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
310594d2dde7SGrant Likely 		if (len == 12) {
310694d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
310794d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
310894d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
310994d2dde7SGrant Likely 		}
311094d2dde7SGrant Likely 	}
311194d2dde7SGrant Likely 
311294d2dde7SGrant Likely 	/* Fixup sound interrupts property */
311394d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
311494d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
311594d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
311694d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
311794d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
311894d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
311994d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
312094d2dde7SGrant Likely 		}
312194d2dde7SGrant Likely 	}
312294d2dde7SGrant Likely 
312394d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
312494d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
312588fd2a9dSSylvain Munaut }
312688fd2a9dSSylvain Munaut #else
312788fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
312888fd2a9dSSylvain Munaut #endif
312988fd2a9dSSylvain Munaut 
313050246787SDarren Stevens #ifdef CONFIG_PPC_PASEMI_NEMO
313150246787SDarren Stevens /*
313250246787SDarren Stevens  * CFE supplied on Nemo is broken in several ways, biggest
313350246787SDarren Stevens  * problem is that it reassigns ISA interrupts to unused mpic ints.
313450246787SDarren Stevens  * Add an interrupt-controller property for the io-bridge to use
313550246787SDarren Stevens  * and correct the ints so we can attach them to an irq_domain
313650246787SDarren Stevens  */
fixup_device_tree_pasemi(void)313750246787SDarren Stevens static void __init fixup_device_tree_pasemi(void)
313850246787SDarren Stevens {
313950246787SDarren Stevens 	u32 interrupts[2], parent, rval, val = 0;
314050246787SDarren Stevens 	char *name, *pci_name;
314150246787SDarren Stevens 	phandle iob, node;
314250246787SDarren Stevens 
314350246787SDarren Stevens 	/* Find the root pci node */
314450246787SDarren Stevens 	name = "/pxp@0,e0000000";
314550246787SDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
314650246787SDarren Stevens 	if (!PHANDLE_VALID(iob))
314750246787SDarren Stevens 		return;
314850246787SDarren Stevens 
314950246787SDarren Stevens 	/* check if interrupt-controller node set yet */
315050246787SDarren Stevens 	if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
315150246787SDarren Stevens 		return;
315250246787SDarren Stevens 
315350246787SDarren Stevens 	prom_printf("adding interrupt-controller property for SB600...\n");
315450246787SDarren Stevens 
315550246787SDarren Stevens 	prom_setprop(iob, name, "interrupt-controller", &val, 0);
315650246787SDarren Stevens 
315750246787SDarren Stevens 	pci_name = "/pxp@0,e0000000/pci@11";
315850246787SDarren Stevens 	node = call_prom("finddevice", 1, 1, ADDR(pci_name));
315950246787SDarren Stevens 	parent = ADDR(iob);
316050246787SDarren Stevens 
316150246787SDarren Stevens 	for( ; prom_next_node(&node); ) {
316250246787SDarren Stevens 		/* scan each node for one with an interrupt */
316350246787SDarren Stevens 		if (!PHANDLE_VALID(node))
316450246787SDarren Stevens 			continue;
316550246787SDarren Stevens 
316650246787SDarren Stevens 		rval = prom_getproplen(node, "interrupts");
316750246787SDarren Stevens 		if (rval == 0 || rval == PROM_ERROR)
316850246787SDarren Stevens 			continue;
316950246787SDarren Stevens 
317050246787SDarren Stevens 		prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
317150246787SDarren Stevens 		if ((interrupts[0] < 212) || (interrupts[0] > 222))
317250246787SDarren Stevens 			continue;
317350246787SDarren Stevens 
317450246787SDarren Stevens 		/* found a node, update both interrupts and interrupt-parent */
317550246787SDarren Stevens 		if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
317650246787SDarren Stevens 			interrupts[0] -= 203;
317750246787SDarren Stevens 		if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
317850246787SDarren Stevens 			interrupts[0] -= 213;
317950246787SDarren Stevens 		if (interrupts[0] == 221)
318050246787SDarren Stevens 			interrupts[0] = 14;
318150246787SDarren Stevens 		if (interrupts[0] == 222)
318250246787SDarren Stevens 			interrupts[0] = 8;
318350246787SDarren Stevens 
318450246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupts", interrupts,
318550246787SDarren Stevens 					sizeof(interrupts));
318650246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupt-parent", &parent,
318750246787SDarren Stevens 					sizeof(parent));
318850246787SDarren Stevens 	}
3189687e16bcSDarren Stevens 
3190687e16bcSDarren Stevens 	/*
3191687e16bcSDarren Stevens 	 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
3192687e16bcSDarren Stevens 	 * so that generic isa-bridge code can add the SB600 and its on-board
3193687e16bcSDarren Stevens 	 * peripherals.
3194687e16bcSDarren Stevens 	 */
3195687e16bcSDarren Stevens 	name = "/pxp@0,e0000000/io-bridge@0";
3196687e16bcSDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
3197687e16bcSDarren Stevens 	if (!PHANDLE_VALID(iob))
3198687e16bcSDarren Stevens 		return;
3199687e16bcSDarren Stevens 
3200687e16bcSDarren Stevens 	/* device_type is already set, just change it. */
3201687e16bcSDarren Stevens 
3202687e16bcSDarren Stevens 	prom_printf("Changing device_type of SB600 node...\n");
3203687e16bcSDarren Stevens 
3204687e16bcSDarren Stevens 	prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
320550246787SDarren Stevens }
320650246787SDarren Stevens #else	/* !CONFIG_PPC_PASEMI_NEMO */
fixup_device_tree_pasemi(void)320750246787SDarren Stevens static inline void fixup_device_tree_pasemi(void) { }
320850246787SDarren Stevens #endif
320950246787SDarren Stevens 
fixup_device_tree(void)321054f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
321154f4ee18SHollis Blanchard {
321254f4ee18SHollis Blanchard 	fixup_device_tree_maple();
32138f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
3214e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
321554f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
3216*691284c2SMichael Ellerman 	fixup_device_tree_pmac64();
321788fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
321850246787SDarren Stevens 	fixup_device_tree_pasemi();
321954f4ee18SHollis Blanchard }
32209b6b563cSPaul Mackerras 
prom_find_boot_cpu(void)32219b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
32229b6b563cSPaul Mackerras {
3223493adffcSBenjamin Herrenschmidt 	__be32 rval;
32249b6b563cSPaul Mackerras 	ihandle prom_cpu;
32259b6b563cSPaul Mackerras 	phandle cpu_pkg;
32269b6b563cSPaul Mackerras 
3227493adffcSBenjamin Herrenschmidt 	rval = 0;
3228493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
3229a575b807SPaul Mackerras 		return;
3230493adffcSBenjamin Herrenschmidt 	prom_cpu = be32_to_cpu(rval);
32319b6b563cSPaul Mackerras 
32329b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
32339b6b563cSPaul Mackerras 
3234af2b7fa1SDarren Stevens 	if (!PHANDLE_VALID(cpu_pkg))
3235af2b7fa1SDarren Stevens 		return;
3236af2b7fa1SDarren Stevens 
3237493adffcSBenjamin Herrenschmidt 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
3238493adffcSBenjamin Herrenschmidt 	prom.cpu = be32_to_cpu(rval);
32399b6b563cSPaul Mackerras 
3240eae5f709SMathieu Malaterre 	prom_debug("Booting CPU hw index = %d\n", prom.cpu);
32419b6b563cSPaul Mackerras }
32429b6b563cSPaul Mackerras 
prom_check_initrd(unsigned long r3,unsigned long r4)32439b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
32449b6b563cSPaul Mackerras {
32459b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
32469b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
3247493adffcSBenjamin Herrenschmidt 		__be64 val;
32489b6b563cSPaul Mackerras 
32495827d416SAnton Blanchard 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
32505827d416SAnton Blanchard 		prom_initrd_end = prom_initrd_start + r4;
32519b6b563cSPaul Mackerras 
3252493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_start);
32535827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
3254a23414beSPaul Mackerras 			     &val, sizeof(val));
3255493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_end);
32565827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
3257a23414beSPaul Mackerras 			     &val, sizeof(val));
32589b6b563cSPaul Mackerras 
32595827d416SAnton Blanchard 		reserve_mem(prom_initrd_start,
32605827d416SAnton Blanchard 			    prom_initrd_end - prom_initrd_start);
32619b6b563cSPaul Mackerras 
3262eae5f709SMathieu Malaterre 		prom_debug("initrd_start=0x%lx\n", prom_initrd_start);
3263eae5f709SMathieu Malaterre 		prom_debug("initrd_end=0x%lx\n", prom_initrd_end);
32649b6b563cSPaul Mackerras 	}
32659b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
32669b6b563cSPaul Mackerras }
32679b6b563cSPaul Mackerras 
32686a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
32696a9c930bSRam Pai /*
32706a9c930bSRam Pai  * Perform the Enter Secure Mode ultracall.
32716a9c930bSRam Pai  */
enter_secure_mode(unsigned long kbase,unsigned long fdt)3272d276960dSNick Child static int __init enter_secure_mode(unsigned long kbase, unsigned long fdt)
32736a9c930bSRam Pai {
32746a9c930bSRam Pai 	register unsigned long r3 asm("r3") = UV_ESM;
32756a9c930bSRam Pai 	register unsigned long r4 asm("r4") = kbase;
32766a9c930bSRam Pai 	register unsigned long r5 asm("r5") = fdt;
32776a9c930bSRam Pai 
32786a9c930bSRam Pai 	asm volatile("sc 2" : "+r"(r3) : "r"(r4), "r"(r5));
32796a9c930bSRam Pai 
32806a9c930bSRam Pai 	return r3;
32816a9c930bSRam Pai }
32826a9c930bSRam Pai 
32836a9c930bSRam Pai /*
32846a9c930bSRam Pai  * Call the Ultravisor to transfer us to secure memory if we have an ESM blob.
32856a9c930bSRam Pai  */
setup_secure_guest(unsigned long kbase,unsigned long fdt)3286aff77951SVladis Dronov static void __init setup_secure_guest(unsigned long kbase, unsigned long fdt)
32876a9c930bSRam Pai {
32886a9c930bSRam Pai 	int ret;
32896a9c930bSRam Pai 
32906a9c930bSRam Pai 	if (!prom_svm_enable)
32916a9c930bSRam Pai 		return;
32926a9c930bSRam Pai 
32936a9c930bSRam Pai 	/* Switch to secure mode. */
32946a9c930bSRam Pai 	prom_printf("Switching to secure mode.\n");
32956a9c930bSRam Pai 
329605d9a952SThiago Jung Bauermann 	/*
329705d9a952SThiago Jung Bauermann 	 * The ultravisor will do an integrity check of the kernel image but we
329805d9a952SThiago Jung Bauermann 	 * relocated it so the check will fail. Restore the original image by
329905d9a952SThiago Jung Bauermann 	 * relocating it back to the kernel virtual base address.
330005d9a952SThiago Jung Bauermann 	 */
330105d9a952SThiago Jung Bauermann 	relocate(KERNELBASE);
330205d9a952SThiago Jung Bauermann 
33036a9c930bSRam Pai 	ret = enter_secure_mode(kbase, fdt);
330405d9a952SThiago Jung Bauermann 
330505d9a952SThiago Jung Bauermann 	/* Relocate the kernel again. */
330605d9a952SThiago Jung Bauermann 	relocate(kbase);
330705d9a952SThiago Jung Bauermann 
33086a9c930bSRam Pai 	if (ret != U_SUCCESS) {
33096a9c930bSRam Pai 		prom_printf("Returned %d from switching to secure mode.\n", ret);
33106a9c930bSRam Pai 		prom_rtas_os_term("Switch to secure mode failed.\n");
33116a9c930bSRam Pai 	}
33126a9c930bSRam Pai }
33136a9c930bSRam Pai #else
setup_secure_guest(unsigned long kbase,unsigned long fdt)3314aff77951SVladis Dronov static void __init setup_secure_guest(unsigned long kbase, unsigned long fdt)
33156a9c930bSRam Pai {
33166a9c930bSRam Pai }
33176a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
33186a9c930bSRam Pai 
33199b6b563cSPaul Mackerras /*
33209b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
33219b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
33229b6b563cSPaul Mackerras  */
33239b6b563cSPaul Mackerras 
prom_init(unsigned long r3,unsigned long r4,unsigned long pp,unsigned long r6,unsigned long r7,unsigned long kbase)33249b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
33259b6b563cSPaul Mackerras 			       unsigned long pp,
3326549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
3327549e8152SPaul Mackerras 			       unsigned long kbase)
33289b6b563cSPaul Mackerras {
33299b6b563cSPaul Mackerras 	unsigned long hdr;
33309b6b563cSPaul Mackerras 
33319b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
3332549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
33339b6b563cSPaul Mackerras 	reloc_got2(offset);
33349b6b563cSPaul Mackerras #endif
33359b6b563cSPaul Mackerras 
33369b6b563cSPaul Mackerras 	/*
33379b6b563cSPaul Mackerras 	 * First zero the BSS
33389b6b563cSPaul Mackerras 	 */
33395827d416SAnton Blanchard 	memset(&__bss_start, 0, __bss_stop - __bss_start);
33409b6b563cSPaul Mackerras 
33419b6b563cSPaul Mackerras 	/*
33429b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
33439b6b563cSPaul Mackerras 	 * like /chosen
33449b6b563cSPaul Mackerras 	 */
33459b6b563cSPaul Mackerras 	prom_init_client_services(pp);
33469b6b563cSPaul Mackerras 
33479b6b563cSPaul Mackerras 	/*
3348a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
3349a23414beSPaul Mackerras 	 * and other workarounds
3350a23414beSPaul Mackerras 	 */
3351a23414beSPaul Mackerras 	prom_find_mmu();
3352a23414beSPaul Mackerras 
3353a23414beSPaul Mackerras 	/*
33549b6b563cSPaul Mackerras 	 * Init prom stdout device
33559b6b563cSPaul Mackerras 	 */
33569b6b563cSPaul Mackerras 	prom_init_stdout();
33579b6b563cSPaul Mackerras 
33585827d416SAnton Blanchard 	prom_printf("Preparing to boot %s", linux_banner);
3359e7943fbbSMichael Ellerman 
33609b6b563cSPaul Mackerras 	/*
33619b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
33629b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
33639b6b563cSPaul Mackerras 	 */
33645827d416SAnton Blanchard 	of_platform = prom_find_machine_type();
33655827d416SAnton Blanchard 	prom_printf("Detected machine type: %x\n", of_platform);
33669b6b563cSPaul Mackerras 
33670f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
3368add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
3369add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
3370add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
3371549e8152SPaul Mackerras #endif
3372add60ef3SOlaf Hering 
3373add60ef3SOlaf Hering 	/*
3374add60ef3SOlaf Hering 	 * Check for an initrd
3375add60ef3SOlaf Hering 	 */
3376add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
3377add60ef3SOlaf Hering 
337812cc9fd6SSuraj Jitindar Singh 	/*
337912cc9fd6SSuraj Jitindar Singh 	 * Do early parsing of command line
338012cc9fd6SSuraj Jitindar Singh 	 */
338112cc9fd6SSuraj Jitindar Singh 	early_cmdline_parse();
338212cc9fd6SSuraj Jitindar Singh 
338311fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
33849b6b563cSPaul Mackerras 	/*
33859b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
33869b6b563cSPaul Mackerras 	 */
33875827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES ||
33885827d416SAnton Blanchard 	    of_platform == PLATFORM_PSERIES_LPAR)
33899b6b563cSPaul Mackerras 		prom_send_capabilities();
33909b6b563cSPaul Mackerras #endif
33919b6b563cSPaul Mackerras 
33929b6b563cSPaul Mackerras 	/*
3393f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
33949b6b563cSPaul Mackerras 	 */
33955827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC)
3396549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
33979b6b563cSPaul Mackerras 
33989b6b563cSPaul Mackerras 	/*
33999b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
34009b6b563cSPaul Mackerras 	 */
34019b6b563cSPaul Mackerras 	prom_init_mem();
34029b6b563cSPaul Mackerras 
34039b6b563cSPaul Mackerras 	/*
34049b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
34059b6b563cSPaul Mackerras 	 */
34069b6b563cSPaul Mackerras 	prom_find_boot_cpu();
34079b6b563cSPaul Mackerras 
34089b6b563cSPaul Mackerras 	/*
34099b6b563cSPaul Mackerras 	 * Initialize display devices
34109b6b563cSPaul Mackerras 	 */
34119b6b563cSPaul Mackerras 	prom_check_displays();
34129b6b563cSPaul Mackerras 
3413493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
34149b6b563cSPaul Mackerras 	/*
34159b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
34169b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
34179b6b563cSPaul Mackerras 	 * available for us here...
34189b6b563cSPaul Mackerras 	 */
34195827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES)
34209b6b563cSPaul Mackerras 		prom_initialize_tce_table();
34219b6b563cSPaul Mackerras #endif
34229b6b563cSPaul Mackerras 
34239b6b563cSPaul Mackerras 	/*
342427f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
342527f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
34269b6b563cSPaul Mackerras 	 */
342711fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
34289b6b563cSPaul Mackerras 		prom_instantiate_rtas();
342927f44888SBenjamin Herrenschmidt 
34304a727429SAshley Lai #ifdef CONFIG_PPC64
34314a727429SAshley Lai 	/* instantiate sml */
34324a727429SAshley Lai 	prom_instantiate_sml();
34334a727429SAshley Lai #endif
34344a727429SAshley Lai 
343527f44888SBenjamin Herrenschmidt 	/*
343627f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
343727f44888SBenjamin Herrenschmidt 	 *
343827f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
3439dbe78b40SBenjamin Herrenschmidt 	 *
34401fd02f66SJulia Lawall 	 * (This must be done after instantiating RTAS)
344127f44888SBenjamin Herrenschmidt 	 */
344211fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
344327f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
34449b6b563cSPaul Mackerras 
34459b6b563cSPaul Mackerras 	/*
34469b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
34479b6b563cSPaul Mackerras 	 */
3448493adffcSBenjamin Herrenschmidt 	if (prom_memory_limit) {
3449493adffcSBenjamin Herrenschmidt 		__be64 val = cpu_to_be64(prom_memory_limit);
34505827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3451493adffcSBenjamin Herrenschmidt 			     &val, sizeof(val));
3452493adffcSBenjamin Herrenschmidt 	}
34539b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
34545827d416SAnton Blanchard 	if (prom_iommu_off)
34555827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3456a23414beSPaul Mackerras 			     NULL, 0);
34579b6b563cSPaul Mackerras 
34585827d416SAnton Blanchard 	if (prom_iommu_force_on)
34595827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3460a23414beSPaul Mackerras 			     NULL, 0);
34619b6b563cSPaul Mackerras 
34625827d416SAnton Blanchard 	if (prom_tce_alloc_start) {
34635827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
34645827d416SAnton Blanchard 			     &prom_tce_alloc_start,
34659b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
34665827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
34675827d416SAnton Blanchard 			     &prom_tce_alloc_end,
34689b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
34699b6b563cSPaul Mackerras 	}
34709b6b563cSPaul Mackerras #endif
34719b6b563cSPaul Mackerras 
34729b6b563cSPaul Mackerras 	/*
34739b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
34749b6b563cSPaul Mackerras 	 */
34759b6b563cSPaul Mackerras 	fixup_device_tree();
34769b6b563cSPaul Mackerras 
34779b6b563cSPaul Mackerras 	/*
34789b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
34799b6b563cSPaul Mackerras 	 */
34809b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
34819b6b563cSPaul Mackerras 	flatten_device_tree();
34829b6b563cSPaul Mackerras 
34833825ac0eSPaul Mackerras 	/*
34843825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
34853825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
348611fdb309SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101).
34873825ac0eSPaul Mackerras 	 */
348811fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
34899b6b563cSPaul Mackerras 		prom_close_stdin();
34909b6b563cSPaul Mackerras 
34919b6b563cSPaul Mackerras 	/*
34929b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
34939b6b563cSPaul Mackerras 	 * devices etc...
34949b6b563cSPaul Mackerras 	 */
34957e862d7eSMichael Ellerman 	prom_printf("Quiescing Open Firmware ...\n");
34969b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
34979b6b563cSPaul Mackerras 
34989b6b563cSPaul Mackerras 	/*
34999b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
35009b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
35019b6b563cSPaul Mackerras 	 * is common to us and kexec
35029b6b563cSPaul Mackerras 	 */
35035827d416SAnton Blanchard 	hdr = dt_header_start;
350440dfef66SBenjamin Herrenschmidt 
35057d70c63cSBenjamin Herrenschmidt 	prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
3506eae5f709SMathieu Malaterre 	prom_debug("->dt_header_start=0x%lx\n", hdr);
35079b6b563cSPaul Mackerras 
35089b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
35099b6b563cSPaul Mackerras 	reloc_got2(-offset);
35109b6b563cSPaul Mackerras #endif
35119b6b563cSPaul Mackerras 
35126a9c930bSRam Pai 	/* Move to secure memory if we're supposed to be secure guests. */
35136a9c930bSRam Pai 	setup_secure_guest(kbase, hdr);
35146a9c930bSRam Pai 
35156e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
35169b6b563cSPaul Mackerras 
35179b6b563cSPaul Mackerras 	return 0;
35189b6b563cSPaul Mackerras }
3519