xref: /linux/arch/powerpc/kernel/prom_init.c (revision 59dc5bfca0cb6a29db1a50847684eb5c19f8f400)
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 
179b6b563cSPaul Mackerras #include <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>
319b6b563cSPaul Mackerras #include <asm/prom.h>
329b6b563cSPaul Mackerras #include <asm/rtas.h>
339b6b563cSPaul Mackerras #include <asm/page.h>
349b6b563cSPaul Mackerras #include <asm/processor.h>
35*59dc5bfcSNicholas Piggin #include <asm/interrupt.h>
369b6b563cSPaul Mackerras #include <asm/irq.h>
379b6b563cSPaul Mackerras #include <asm/io.h>
389b6b563cSPaul Mackerras #include <asm/smp.h>
399b6b563cSPaul Mackerras #include <asm/mmu.h>
409b6b563cSPaul Mackerras #include <asm/iommu.h>
419b6b563cSPaul Mackerras #include <asm/btext.h>
429b6b563cSPaul Mackerras #include <asm/sections.h>
439b6b563cSPaul Mackerras #include <asm/machdep.h>
440545d543SDaniel Axtens #include <asm/asm-prototypes.h>
456a9c930bSRam Pai #include <asm/ultravisor-api.h>
469b6b563cSPaul Mackerras 
479b6b563cSPaul Mackerras #include <linux/linux_logo.h>
489b6b563cSPaul Mackerras 
49e63334e5SBenjamin Herrenschmidt /* All of prom_init bss lives here */
5033def849SJoe Perches #define __prombss __section(".bss.prominit")
51e63334e5SBenjamin Herrenschmidt 
529b6b563cSPaul Mackerras /*
539b6b563cSPaul Mackerras  * Eventually bump that one up
549b6b563cSPaul Mackerras  */
559b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE	0x100000
569b6b563cSPaul Mackerras 
579b6b563cSPaul Mackerras /*
589b6b563cSPaul Mackerras  * This is the size of the local memory reserve map that gets copied
599b6b563cSPaul Mackerras  * into the boot params passed to the kernel. That size is totally
609b6b563cSPaul Mackerras  * flexible as the kernel just reads the list until it encounters an
619b6b563cSPaul Mackerras  * entry with size 0, so it can be changed without breaking binary
629b6b563cSPaul Mackerras  * compatibility
639b6b563cSPaul Mackerras  */
649b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE	8
659b6b563cSPaul Mackerras 
669b6b563cSPaul Mackerras /*
679b6b563cSPaul Mackerras  * prom_init() is called very early on, before the kernel text
689b6b563cSPaul Mackerras  * and data have been mapped to KERNELBASE.  At this point the code
699b6b563cSPaul Mackerras  * is running at whatever address it has been loaded at.
709b6b563cSPaul Mackerras  * On ppc32 we compile with -mrelocatable, which means that references
719b6b563cSPaul Mackerras  * to extern and static variables get relocated automatically.
725ac47f7aSAnton Blanchard  * ppc64 objects are always relocatable, we just need to relocate the
735ac47f7aSAnton Blanchard  * TOC.
749b6b563cSPaul Mackerras  *
759b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
769b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
779b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
789b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
799b6b563cSPaul Mackerras  *
809b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
819b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
829b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
839b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
849b6b563cSPaul Mackerras  */
855ac47f7aSAnton Blanchard #define ADDR(x)		(u32)(unsigned long)(x)
865ac47f7aSAnton Blanchard 
879b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
88a23414beSPaul Mackerras #define OF_WORKAROUNDS	0
899b6b563cSPaul Mackerras #else
90a23414beSPaul Mackerras #define OF_WORKAROUNDS	of_workarounds
91e63334e5SBenjamin Herrenschmidt static int of_workarounds __prombss;
929b6b563cSPaul Mackerras #endif
939b6b563cSPaul Mackerras 
94a23414beSPaul Mackerras #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
95a23414beSPaul Mackerras #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
96a23414beSPaul Mackerras 
979b6b563cSPaul Mackerras #define PROM_BUG() do {						\
989b6b563cSPaul Mackerras         prom_printf("kernel BUG at %s line 0x%x!\n",		\
995827d416SAnton Blanchard 		    __FILE__, __LINE__);			\
10063ce271bSChristophe Leroy 	__builtin_trap();					\
1019b6b563cSPaul Mackerras } while (0)
1029b6b563cSPaul Mackerras 
1039b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1049b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1059b6b563cSPaul Mackerras #else
10685aa4b98SMathieu Malaterre #define prom_debug(x...)	do { } while (0)
1079b6b563cSPaul Mackerras #endif
1089b6b563cSPaul Mackerras 
1099b6b563cSPaul Mackerras 
1109b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1119b6b563cSPaul Mackerras 
1129b6b563cSPaul Mackerras struct prom_args {
113493adffcSBenjamin Herrenschmidt         __be32 service;
114493adffcSBenjamin Herrenschmidt         __be32 nargs;
115493adffcSBenjamin Herrenschmidt         __be32 nret;
116493adffcSBenjamin Herrenschmidt         __be32 args[10];
1179b6b563cSPaul Mackerras };
1189b6b563cSPaul Mackerras 
1199b6b563cSPaul Mackerras struct prom_t {
1209b6b563cSPaul Mackerras 	ihandle root;
121a23414beSPaul Mackerras 	phandle chosen;
1229b6b563cSPaul Mackerras 	int cpu;
1239b6b563cSPaul Mackerras 	ihandle stdout;
124a575b807SPaul Mackerras 	ihandle mmumap;
125a23414beSPaul Mackerras 	ihandle memory;
1269b6b563cSPaul Mackerras };
1279b6b563cSPaul Mackerras 
1289b6b563cSPaul Mackerras struct mem_map_entry {
129493adffcSBenjamin Herrenschmidt 	__be64	base;
130493adffcSBenjamin Herrenschmidt 	__be64	size;
1319b6b563cSPaul Mackerras };
1329b6b563cSPaul Mackerras 
133493adffcSBenjamin Herrenschmidt typedef __be32 cell_t;
1349b6b563cSPaul Mackerras 
1356e35d5daSBenjamin Herrenschmidt extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
1366e35d5daSBenjamin Herrenschmidt 		    unsigned long r6, unsigned long r7, unsigned long r8,
1376e35d5daSBenjamin Herrenschmidt 		    unsigned long r9);
1389b6b563cSPaul Mackerras 
1399b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
140c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1419b6b563cSPaul Mackerras #else
142c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1439b6b563cSPaul Mackerras {
144c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1459b6b563cSPaul Mackerras }
1469b6b563cSPaul Mackerras #endif
1479b6b563cSPaul Mackerras 
1489b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1499b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1509b6b563cSPaul Mackerras 
1519b6b563cSPaul Mackerras /* prom structure */
152e63334e5SBenjamin Herrenschmidt static struct prom_t __prombss prom;
1539b6b563cSPaul Mackerras 
154e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_entry;
1559b6b563cSPaul Mackerras 
156e63334e5SBenjamin Herrenschmidt static char __prombss of_stdout_device[256];
157d7fbe2a0SChristophe Leroy static char __prombss prom_scratch[256];
1589b6b563cSPaul Mackerras 
159e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_header_start;
160e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_struct_start, dt_struct_end;
161e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_string_start, dt_string_end;
1629b6b563cSPaul Mackerras 
163e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_initrd_start, prom_initrd_end;
1649b6b563cSPaul Mackerras 
1659b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
166e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_force_on;
167e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_off;
168e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_start;
169e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_end;
1709b6b563cSPaul Mackerras #endif
1719b6b563cSPaul Mackerras 
172c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
1738ca2d515SBenjamin Herrenschmidt static bool __prombss prom_radix_disable;
174bf6b7661SAneesh Kumar K.V static bool __prombss prom_radix_gtse_disable;
175a3bf9fbdSGreg Kurz static bool __prombss prom_xive_disable;
176c886087cSBenjamin Herrenschmidt #endif
177014d02cbSSuraj Jitindar Singh 
1786a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
1796a9c930bSRam Pai static bool __prombss prom_svm_enable;
1806a9c930bSRam Pai #endif
1816a9c930bSRam Pai 
182014d02cbSSuraj Jitindar Singh struct platform_support {
183014d02cbSSuraj Jitindar Singh 	bool hash_mmu;
184014d02cbSSuraj Jitindar Singh 	bool radix_mmu;
185014d02cbSSuraj Jitindar Singh 	bool radix_gtse;
186ac5e5a54SCédric Le Goater 	bool xive;
187014d02cbSSuraj Jitindar Singh };
188014d02cbSSuraj Jitindar Singh 
189e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
190e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
191e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
192e8222502SBenjamin Herrenschmidt  */
193e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
194e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
195e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
196e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
197e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
198e8222502SBenjamin Herrenschmidt 
199e63334e5SBenjamin Herrenschmidt static int __prombss of_platform;
2009b6b563cSPaul Mackerras 
201e63334e5SBenjamin Herrenschmidt static char __prombss prom_cmd_line[COMMAND_LINE_SIZE];
2029b6b563cSPaul Mackerras 
203e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_memory_limit;
204cf68787bSBenjamin Krill 
205e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top;
206e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top_high;
207e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_bottom;
208e63334e5SBenjamin Herrenschmidt static unsigned long __prombss rmo_top;
209e63334e5SBenjamin Herrenschmidt static unsigned long __prombss ram_top;
2109b6b563cSPaul Mackerras 
211e63334e5SBenjamin Herrenschmidt static struct mem_map_entry __prombss mem_reserve_map[MEM_RESERVE_MAP_SIZE];
212e63334e5SBenjamin Herrenschmidt static int __prombss mem_reserve_cnt;
2139b6b563cSPaul Mackerras 
214e63334e5SBenjamin Herrenschmidt static cell_t __prombss regbuf[1024];
2159b6b563cSPaul Mackerras 
2168ca2d515SBenjamin Herrenschmidt static bool  __prombss rtas_has_query_cpu_stopped;
217dbe78b40SBenjamin Herrenschmidt 
2189b6b563cSPaul Mackerras 
2199b6b563cSPaul Mackerras /*
2209b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2219b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2229b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2239b6b563cSPaul Mackerras  * it is valid
2249b6b563cSPaul Mackerras  */
2259b6b563cSPaul Mackerras 
2269b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2279b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2289b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2299b6b563cSPaul Mackerras 
230450e7dd4SChristophe Leroy /* Copied from lib/string.c and lib/kstrtox.c */
231450e7dd4SChristophe Leroy 
232450e7dd4SChristophe Leroy static int __init prom_strcmp(const char *cs, const char *ct)
233450e7dd4SChristophe Leroy {
234450e7dd4SChristophe Leroy 	unsigned char c1, c2;
235450e7dd4SChristophe Leroy 
236450e7dd4SChristophe Leroy 	while (1) {
237450e7dd4SChristophe Leroy 		c1 = *cs++;
238450e7dd4SChristophe Leroy 		c2 = *ct++;
239450e7dd4SChristophe Leroy 		if (c1 != c2)
240450e7dd4SChristophe Leroy 			return c1 < c2 ? -1 : 1;
241450e7dd4SChristophe Leroy 		if (!c1)
242450e7dd4SChristophe Leroy 			break;
243450e7dd4SChristophe Leroy 	}
244450e7dd4SChristophe Leroy 	return 0;
245450e7dd4SChristophe Leroy }
246450e7dd4SChristophe Leroy 
247f47d5a4fSMichael Ellerman static ssize_t __init prom_strscpy_pad(char *dest, const char *src, size_t n)
248450e7dd4SChristophe Leroy {
249f47d5a4fSMichael Ellerman 	ssize_t rc;
250f47d5a4fSMichael Ellerman 	size_t i;
251450e7dd4SChristophe Leroy 
252f47d5a4fSMichael Ellerman 	if (n == 0 || n > INT_MAX)
253f47d5a4fSMichael Ellerman 		return -E2BIG;
254f47d5a4fSMichael Ellerman 
255f47d5a4fSMichael Ellerman 	// Copy up to n bytes
256f47d5a4fSMichael Ellerman 	for (i = 0; i < n && src[i] != '\0'; i++)
257f47d5a4fSMichael Ellerman 		dest[i] = src[i];
258f47d5a4fSMichael Ellerman 
259f47d5a4fSMichael Ellerman 	rc = i;
260f47d5a4fSMichael Ellerman 
261f47d5a4fSMichael Ellerman 	// If we copied all n then we have run out of space for the nul
262f47d5a4fSMichael Ellerman 	if (rc == n) {
263f47d5a4fSMichael Ellerman 		// Rewind by one character to ensure nul termination
264f47d5a4fSMichael Ellerman 		i--;
265f47d5a4fSMichael Ellerman 		rc = -E2BIG;
266f47d5a4fSMichael Ellerman 	}
267f47d5a4fSMichael Ellerman 
268f47d5a4fSMichael Ellerman 	for (; i < n; i++)
269f47d5a4fSMichael Ellerman 		dest[i] = '\0';
270f47d5a4fSMichael Ellerman 
271f47d5a4fSMichael Ellerman 	return rc;
272450e7dd4SChristophe Leroy }
273450e7dd4SChristophe Leroy 
274450e7dd4SChristophe Leroy static int __init prom_strncmp(const char *cs, const char *ct, size_t count)
275450e7dd4SChristophe Leroy {
276450e7dd4SChristophe Leroy 	unsigned char c1, c2;
277450e7dd4SChristophe Leroy 
278450e7dd4SChristophe Leroy 	while (count) {
279450e7dd4SChristophe Leroy 		c1 = *cs++;
280450e7dd4SChristophe Leroy 		c2 = *ct++;
281450e7dd4SChristophe Leroy 		if (c1 != c2)
282450e7dd4SChristophe Leroy 			return c1 < c2 ? -1 : 1;
283450e7dd4SChristophe Leroy 		if (!c1)
284450e7dd4SChristophe Leroy 			break;
285450e7dd4SChristophe Leroy 		count--;
286450e7dd4SChristophe Leroy 	}
287450e7dd4SChristophe Leroy 	return 0;
288450e7dd4SChristophe Leroy }
289450e7dd4SChristophe Leroy 
290450e7dd4SChristophe Leroy static size_t __init prom_strlen(const char *s)
291450e7dd4SChristophe Leroy {
292450e7dd4SChristophe Leroy 	const char *sc;
293450e7dd4SChristophe Leroy 
294450e7dd4SChristophe Leroy 	for (sc = s; *sc != '\0'; ++sc)
295450e7dd4SChristophe Leroy 		/* nothing */;
296450e7dd4SChristophe Leroy 	return sc - s;
297450e7dd4SChristophe Leroy }
298450e7dd4SChristophe Leroy 
299450e7dd4SChristophe Leroy static int __init prom_memcmp(const void *cs, const void *ct, size_t count)
300450e7dd4SChristophe Leroy {
301450e7dd4SChristophe Leroy 	const unsigned char *su1, *su2;
302450e7dd4SChristophe Leroy 	int res = 0;
303450e7dd4SChristophe Leroy 
304450e7dd4SChristophe Leroy 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
305450e7dd4SChristophe Leroy 		if ((res = *su1 - *su2) != 0)
306450e7dd4SChristophe Leroy 			break;
307450e7dd4SChristophe Leroy 	return res;
308450e7dd4SChristophe Leroy }
309450e7dd4SChristophe Leroy 
310450e7dd4SChristophe Leroy static char __init *prom_strstr(const char *s1, const char *s2)
311450e7dd4SChristophe Leroy {
312450e7dd4SChristophe Leroy 	size_t l1, l2;
313450e7dd4SChristophe Leroy 
314450e7dd4SChristophe Leroy 	l2 = prom_strlen(s2);
315450e7dd4SChristophe Leroy 	if (!l2)
316450e7dd4SChristophe Leroy 		return (char *)s1;
317450e7dd4SChristophe Leroy 	l1 = prom_strlen(s1);
318450e7dd4SChristophe Leroy 	while (l1 >= l2) {
319450e7dd4SChristophe Leroy 		l1--;
320450e7dd4SChristophe Leroy 		if (!prom_memcmp(s1, s2, l2))
321450e7dd4SChristophe Leroy 			return (char *)s1;
322450e7dd4SChristophe Leroy 		s1++;
323450e7dd4SChristophe Leroy 	}
324450e7dd4SChristophe Leroy 	return NULL;
325450e7dd4SChristophe Leroy }
326450e7dd4SChristophe Leroy 
327d79fbb3aSChris Packham static size_t __init prom_strlcat(char *dest, const char *src, size_t count)
328450e7dd4SChristophe Leroy {
329d79fbb3aSChris Packham 	size_t dsize = prom_strlen(dest);
330d79fbb3aSChris Packham 	size_t len = prom_strlen(src);
331d79fbb3aSChris Packham 	size_t res = dsize + len;
332450e7dd4SChristophe Leroy 
333d79fbb3aSChris Packham 	/* This would be a bug */
334d79fbb3aSChris Packham 	if (dsize >= count)
335d79fbb3aSChris Packham 		return count;
336d79fbb3aSChris Packham 
337d79fbb3aSChris Packham 	dest += dsize;
338d79fbb3aSChris Packham 	count -= dsize;
339d79fbb3aSChris Packham 	if (len >= count)
340d79fbb3aSChris Packham 		len = count-1;
341450e7dd4SChristophe Leroy 	memcpy(dest, src, len);
342d79fbb3aSChris Packham 	dest[len] = 0;
343d79fbb3aSChris Packham 	return res;
344d79fbb3aSChris Packham 
345450e7dd4SChristophe Leroy }
346450e7dd4SChristophe Leroy 
347450e7dd4SChristophe Leroy #ifdef CONFIG_PPC_PSERIES
348450e7dd4SChristophe Leroy static int __init prom_strtobool(const char *s, bool *res)
349450e7dd4SChristophe Leroy {
350450e7dd4SChristophe Leroy 	if (!s)
351450e7dd4SChristophe Leroy 		return -EINVAL;
352450e7dd4SChristophe Leroy 
353450e7dd4SChristophe Leroy 	switch (s[0]) {
354450e7dd4SChristophe Leroy 	case 'y':
355450e7dd4SChristophe Leroy 	case 'Y':
356450e7dd4SChristophe Leroy 	case '1':
357450e7dd4SChristophe Leroy 		*res = true;
358450e7dd4SChristophe Leroy 		return 0;
359450e7dd4SChristophe Leroy 	case 'n':
360450e7dd4SChristophe Leroy 	case 'N':
361450e7dd4SChristophe Leroy 	case '0':
362450e7dd4SChristophe Leroy 		*res = false;
363450e7dd4SChristophe Leroy 		return 0;
364450e7dd4SChristophe Leroy 	case 'o':
365450e7dd4SChristophe Leroy 	case 'O':
366450e7dd4SChristophe Leroy 		switch (s[1]) {
367450e7dd4SChristophe Leroy 		case 'n':
368450e7dd4SChristophe Leroy 		case 'N':
369450e7dd4SChristophe Leroy 			*res = true;
370450e7dd4SChristophe Leroy 			return 0;
371450e7dd4SChristophe Leroy 		case 'f':
372450e7dd4SChristophe Leroy 		case 'F':
373450e7dd4SChristophe Leroy 			*res = false;
374450e7dd4SChristophe Leroy 			return 0;
375450e7dd4SChristophe Leroy 		default:
376450e7dd4SChristophe Leroy 			break;
377450e7dd4SChristophe Leroy 		}
37849a41365SNick Desaulniers 		break;
379450e7dd4SChristophe Leroy 	default:
380450e7dd4SChristophe Leroy 		break;
381450e7dd4SChristophe Leroy 	}
382450e7dd4SChristophe Leroy 
383450e7dd4SChristophe Leroy 	return -EINVAL;
384450e7dd4SChristophe Leroy }
385450e7dd4SChristophe Leroy #endif
3869b6b563cSPaul Mackerras 
3879b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
3889b6b563cSPaul Mackerras  * firmware.
3899b6b563cSPaul Mackerras  */
3909b6b563cSPaul Mackerras 
3919b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
3929b6b563cSPaul Mackerras {
3939b6b563cSPaul Mackerras 	int i;
3949b6b563cSPaul Mackerras 	struct prom_args args;
3959b6b563cSPaul Mackerras 	va_list list;
3969b6b563cSPaul Mackerras 
397493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
398493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
399493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
4009b6b563cSPaul Mackerras 
4019b6b563cSPaul Mackerras 	va_start(list, nret);
4029b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
403493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
4049b6b563cSPaul Mackerras 	va_end(list);
4059b6b563cSPaul Mackerras 
4069b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
4079b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
4089b6b563cSPaul Mackerras 
4095827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
410c4988820SPaul Mackerras 		return PROM_ERROR;
4119b6b563cSPaul Mackerras 
412493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
4139b6b563cSPaul Mackerras }
4149b6b563cSPaul Mackerras 
4159b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
4169b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
4179b6b563cSPaul Mackerras {
4189b6b563cSPaul Mackerras 	int i;
4199b6b563cSPaul Mackerras 	struct prom_args args;
4209b6b563cSPaul Mackerras 	va_list list;
4219b6b563cSPaul Mackerras 
422493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
423493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
424493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
4259b6b563cSPaul Mackerras 
4269b6b563cSPaul Mackerras 	va_start(list, rets);
4279b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
428493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
4299b6b563cSPaul Mackerras 	va_end(list);
4309b6b563cSPaul Mackerras 
4319b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
432ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
4339b6b563cSPaul Mackerras 
4345827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
435c4988820SPaul Mackerras 		return PROM_ERROR;
4369b6b563cSPaul Mackerras 
4379b6b563cSPaul Mackerras 	if (rets != NULL)
4389b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
439493adffcSBenjamin Herrenschmidt 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
4409b6b563cSPaul Mackerras 
441493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
4429b6b563cSPaul Mackerras }
4439b6b563cSPaul Mackerras 
4449b6b563cSPaul Mackerras 
4459b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
4469b6b563cSPaul Mackerras {
4479b6b563cSPaul Mackerras 	const char *p, *q;
4489b6b563cSPaul Mackerras 
4495827d416SAnton Blanchard 	if (prom.stdout == 0)
4509b6b563cSPaul Mackerras 		return;
4519b6b563cSPaul Mackerras 
4529b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
4539b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
4549b6b563cSPaul Mackerras 			;
4559b6b563cSPaul Mackerras 		if (q > p)
4565827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
4579b6b563cSPaul Mackerras 		if (*q == 0)
4589b6b563cSPaul Mackerras 			break;
4599b6b563cSPaul Mackerras 		++q;
4605827d416SAnton Blanchard 		call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
4619b6b563cSPaul Mackerras 	}
4629b6b563cSPaul Mackerras }
4639b6b563cSPaul Mackerras 
4649b6b563cSPaul Mackerras 
4658af1da40SMathieu Malaterre /*
4668af1da40SMathieu Malaterre  * Both prom_print_hex & prom_print_dec takes an unsigned long as input so that
4678af1da40SMathieu Malaterre  * we do not need __udivdi3 or __umoddi3 on 32bits.
4688af1da40SMathieu Malaterre  */
4699b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
4709b6b563cSPaul Mackerras {
4719b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
4729b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
4739b6b563cSPaul Mackerras 
4749b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
4759b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
4769b6b563cSPaul Mackerras 		if (buf[i] > '9')
4779b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
4789b6b563cSPaul Mackerras 		val >>= 4;
4799b6b563cSPaul Mackerras 	}
4809b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
4815827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf, nibbles);
4829b6b563cSPaul Mackerras }
4839b6b563cSPaul Mackerras 
4842c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */
4852c48a7d6SMichael Neuling #define UL_DIGITS 21
4862c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val)
4872c48a7d6SMichael Neuling {
4882c48a7d6SMichael Neuling 	int i, size;
4892c48a7d6SMichael Neuling 	char buf[UL_DIGITS+1];
4902c48a7d6SMichael Neuling 
4912c48a7d6SMichael Neuling 	for (i = UL_DIGITS-1; i >= 0;  i--) {
4922c48a7d6SMichael Neuling 		buf[i] = (val % 10) + '0';
4932c48a7d6SMichael Neuling 		val = val/10;
4942c48a7d6SMichael Neuling 		if (val == 0)
4952c48a7d6SMichael Neuling 			break;
4962c48a7d6SMichael Neuling 	}
4972c48a7d6SMichael Neuling 	/* shift stuff down */
4982c48a7d6SMichael Neuling 	size = UL_DIGITS - i;
4995827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf+i, size);
5002c48a7d6SMichael Neuling }
5019b6b563cSPaul Mackerras 
502eae5f709SMathieu Malaterre __printf(1, 2)
5039b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
5049b6b563cSPaul Mackerras {
5059b6b563cSPaul Mackerras 	const char *p, *q, *s;
5069b6b563cSPaul Mackerras 	va_list args;
5079b6b563cSPaul Mackerras 	unsigned long v;
508af277149SBenjamin Herrenschmidt 	long vs;
5098af1da40SMathieu Malaterre 	int n = 0;
5109b6b563cSPaul Mackerras 
5119b6b563cSPaul Mackerras 	va_start(args, format);
5129b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
5139b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
5149b6b563cSPaul Mackerras 			;
5159b6b563cSPaul Mackerras 		if (q > p)
5165827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
5179b6b563cSPaul Mackerras 		if (*q == 0)
5189b6b563cSPaul Mackerras 			break;
5199b6b563cSPaul Mackerras 		if (*q == '\n') {
5209b6b563cSPaul Mackerras 			++q;
5215827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout,
5229b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
5239b6b563cSPaul Mackerras 			continue;
5249b6b563cSPaul Mackerras 		}
5259b6b563cSPaul Mackerras 		++q;
5269b6b563cSPaul Mackerras 		if (*q == 0)
5279b6b563cSPaul Mackerras 			break;
5288af1da40SMathieu Malaterre 		while (*q == 'l') {
5298af1da40SMathieu Malaterre 			++q;
5308af1da40SMathieu Malaterre 			++n;
5318af1da40SMathieu Malaterre 		}
5329b6b563cSPaul Mackerras 		switch (*q) {
5339b6b563cSPaul Mackerras 		case 's':
5349b6b563cSPaul Mackerras 			++q;
5359b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
5369b6b563cSPaul Mackerras 			prom_print(s);
5379b6b563cSPaul Mackerras 			break;
5389b6b563cSPaul Mackerras 		case 'x':
5399b6b563cSPaul Mackerras 			++q;
5408af1da40SMathieu Malaterre 			switch (n) {
5418af1da40SMathieu Malaterre 			case 0:
5428af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
5438af1da40SMathieu Malaterre 				break;
5448af1da40SMathieu Malaterre 			case 1:
5459b6b563cSPaul Mackerras 				v = va_arg(args, unsigned long);
5468af1da40SMathieu Malaterre 				break;
5478af1da40SMathieu Malaterre 			case 2:
5488af1da40SMathieu Malaterre 			default:
5498af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
5508af1da40SMathieu Malaterre 				break;
5518af1da40SMathieu Malaterre 			}
5529b6b563cSPaul Mackerras 			prom_print_hex(v);
5539b6b563cSPaul Mackerras 			break;
5548af1da40SMathieu Malaterre 		case 'u':
5558af1da40SMathieu Malaterre 			++q;
5568af1da40SMathieu Malaterre 			switch (n) {
5578af1da40SMathieu Malaterre 			case 0:
5588af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
5598af1da40SMathieu Malaterre 				break;
5608af1da40SMathieu Malaterre 			case 1:
5618af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long);
5628af1da40SMathieu Malaterre 				break;
5638af1da40SMathieu Malaterre 			case 2:
5648af1da40SMathieu Malaterre 			default:
5658af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
5668af1da40SMathieu Malaterre 				break;
5678af1da40SMathieu Malaterre 			}
5688af1da40SMathieu Malaterre 			prom_print_dec(v);
5698af1da40SMathieu Malaterre 			break;
570af277149SBenjamin Herrenschmidt 		case 'd':
571af277149SBenjamin Herrenschmidt 			++q;
5728af1da40SMathieu Malaterre 			switch (n) {
5738af1da40SMathieu Malaterre 			case 0:
574af277149SBenjamin Herrenschmidt 				vs = va_arg(args, int);
575af277149SBenjamin Herrenschmidt 				break;
5768af1da40SMathieu Malaterre 			case 1:
577af277149SBenjamin Herrenschmidt 				vs = va_arg(args, long);
5788af1da40SMathieu Malaterre 				break;
5798af1da40SMathieu Malaterre 			case 2:
5808af1da40SMathieu Malaterre 			default:
5818af1da40SMathieu Malaterre 				vs = va_arg(args, long long);
5828af1da40SMathieu Malaterre 				break;
5838af1da40SMathieu Malaterre 			}
584af277149SBenjamin Herrenschmidt 			if (vs < 0) {
5855827d416SAnton Blanchard 				prom_print("-");
586af277149SBenjamin Herrenschmidt 				vs = -vs;
587af277149SBenjamin Herrenschmidt 			}
588af277149SBenjamin Herrenschmidt 			prom_print_dec(vs);
5892c48a7d6SMichael Neuling 			break;
5909b6b563cSPaul Mackerras 		}
5919b6b563cSPaul Mackerras 	}
5921b855e16SDaniel Axtens 	va_end(args);
5939b6b563cSPaul Mackerras }
5949b6b563cSPaul Mackerras 
5959b6b563cSPaul Mackerras 
596a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
597a575b807SPaul Mackerras 				unsigned long align)
598a575b807SPaul Mackerras {
599a575b807SPaul Mackerras 
600a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
601a23414beSPaul Mackerras 		/*
602a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
603a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
604a23414beSPaul Mackerras 		 */
605a23414beSPaul Mackerras 		int ret;
606a23414beSPaul Mackerras 		prom_arg_t result;
607a23414beSPaul Mackerras 
608a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
6095827d416SAnton Blanchard 				    ADDR("claim"), prom.memory,
610a23414beSPaul Mackerras 				    align, size, virt);
611a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
612a23414beSPaul Mackerras 			return -1;
613a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
6145827d416SAnton Blanchard 				    ADDR("claim"), prom.mmumap,
615a23414beSPaul Mackerras 				    align, size, virt);
616a23414beSPaul Mackerras 		if (ret != 0) {
617a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
6185827d416SAnton Blanchard 				  prom.memory, size, virt);
619a23414beSPaul Mackerras 			return -1;
620a23414beSPaul Mackerras 		}
621a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
622a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
6235827d416SAnton Blanchard 			  ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
624a23414beSPaul Mackerras 		return virt;
625a23414beSPaul Mackerras 	}
626a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
627a23414beSPaul Mackerras 			 (prom_arg_t)align);
628a575b807SPaul Mackerras }
629a575b807SPaul Mackerras 
6309b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
6319b6b563cSPaul Mackerras {
6329b6b563cSPaul Mackerras 	prom_print(reason);
633add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
634add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
6355827d416SAnton Blanchard 	if (of_platform == PLATFORM_POWERMAC)
636add60ef3SOlaf Hering 		asm("trap\n");
637add60ef3SOlaf Hering 
6381d9a4731SStephen Rothwell 	/* ToDo: should put up an SRC here on pSeries */
6399b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
6409b6b563cSPaul Mackerras 
6419b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
6429b6b563cSPaul Mackerras 		;
6439b6b563cSPaul Mackerras }
6449b6b563cSPaul Mackerras 
6459b6b563cSPaul Mackerras 
6469b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
6479b6b563cSPaul Mackerras {
6489b6b563cSPaul Mackerras 	phandle node;
6499b6b563cSPaul Mackerras 
6509b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
6519b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
6529b6b563cSPaul Mackerras 		return 1;
6539b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
6549b6b563cSPaul Mackerras 		return 1;
6559b6b563cSPaul Mackerras 	for (;;) {
6569b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
6579b6b563cSPaul Mackerras 			return 0;
6589b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
6599b6b563cSPaul Mackerras 			return 1;
6609b6b563cSPaul Mackerras 	}
6619b6b563cSPaul Mackerras }
6629b6b563cSPaul Mackerras 
663480795a0SMasahiro Yamada static inline int __init prom_getprop(phandle node, const char *pname,
6649b6b563cSPaul Mackerras 				      void *value, size_t valuelen)
6659b6b563cSPaul Mackerras {
6669b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
6679b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
6689b6b563cSPaul Mackerras }
6699b6b563cSPaul Mackerras 
670480795a0SMasahiro Yamada static inline int __init prom_getproplen(phandle node, const char *pname)
6719b6b563cSPaul Mackerras {
6729b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
6739b6b563cSPaul Mackerras }
6749b6b563cSPaul Mackerras 
675a23414beSPaul Mackerras static void add_string(char **str, const char *q)
6769b6b563cSPaul Mackerras {
677a23414beSPaul Mackerras 	char *p = *str;
678a23414beSPaul Mackerras 
679a23414beSPaul Mackerras 	while (*q)
680a23414beSPaul Mackerras 		*p++ = *q++;
681a23414beSPaul Mackerras 	*p++ = ' ';
682a23414beSPaul Mackerras 	*str = p;
683a23414beSPaul Mackerras }
684a23414beSPaul Mackerras 
685a23414beSPaul Mackerras static char *tohex(unsigned int x)
686a23414beSPaul Mackerras {
6878ca2d515SBenjamin Herrenschmidt 	static const char digits[] __initconst = "0123456789abcdef";
6888ca2d515SBenjamin Herrenschmidt 	static char result[9] __prombss;
689a23414beSPaul Mackerras 	int i;
690a23414beSPaul Mackerras 
691a23414beSPaul Mackerras 	result[8] = 0;
692a23414beSPaul Mackerras 	i = 8;
693a23414beSPaul Mackerras 	do {
694a23414beSPaul Mackerras 		--i;
695a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
696a23414beSPaul Mackerras 		x >>= 4;
697a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
698a23414beSPaul Mackerras 	return &result[i];
699a23414beSPaul Mackerras }
700a23414beSPaul Mackerras 
701a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
702a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
703a23414beSPaul Mackerras {
704a23414beSPaul Mackerras 	char cmd[256], *p;
705a23414beSPaul Mackerras 
706a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
7079b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
7089b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
709a23414beSPaul Mackerras 
710a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
711a23414beSPaul Mackerras 	p = cmd;
712a23414beSPaul Mackerras 	add_string(&p, "dev");
713a23414beSPaul Mackerras 	add_string(&p, nodename);
714a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
715a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
716a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
717450e7dd4SChristophe Leroy 	add_string(&p, tohex(prom_strlen(pname)));
718a23414beSPaul Mackerras 	add_string(&p, "property");
719a23414beSPaul Mackerras 	*p = 0;
720a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
7219b6b563cSPaul Mackerras }
7229b6b563cSPaul Mackerras 
7235827d416SAnton Blanchard /* We can't use the standard versions because of relocation headaches. */
7244cfdd920SAndy Shevchenko #define prom_isxdigit(c) \
7254cfdd920SAndy Shevchenko 	(('0' <= (c) && (c) <= '9') || ('a' <= (c) && (c) <= 'f') || ('A' <= (c) && (c) <= 'F'))
726cf68787bSBenjamin Krill 
7274cfdd920SAndy Shevchenko #define prom_isdigit(c)	('0' <= (c) && (c) <= '9')
7284cfdd920SAndy Shevchenko #define prom_islower(c)	('a' <= (c) && (c) <= 'z')
7294cfdd920SAndy Shevchenko #define prom_toupper(c)	(prom_islower(c) ? ((c) - 'a' + 'A') : (c))
730cf68787bSBenjamin Krill 
731493adffcSBenjamin Herrenschmidt static unsigned long prom_strtoul(const char *cp, const char **endp)
732cf68787bSBenjamin Krill {
733cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
734cf68787bSBenjamin Krill 
735cf68787bSBenjamin Krill 	if (*cp == '0') {
736cf68787bSBenjamin Krill 		base = 8;
737cf68787bSBenjamin Krill 		cp++;
7384cfdd920SAndy Shevchenko 		if (prom_toupper(*cp) == 'X') {
739cf68787bSBenjamin Krill 			cp++;
740cf68787bSBenjamin Krill 			base = 16;
741cf68787bSBenjamin Krill 		}
742cf68787bSBenjamin Krill 	}
743cf68787bSBenjamin Krill 
7444cfdd920SAndy Shevchenko 	while (prom_isxdigit(*cp) &&
7454cfdd920SAndy Shevchenko 	       (value = prom_isdigit(*cp) ? *cp - '0' : prom_toupper(*cp) - 'A' + 10) < base) {
746cf68787bSBenjamin Krill 		result = result * base + value;
747cf68787bSBenjamin Krill 		cp++;
748cf68787bSBenjamin Krill 	}
749cf68787bSBenjamin Krill 
750cf68787bSBenjamin Krill 	if (endp)
751cf68787bSBenjamin Krill 		*endp = cp;
752cf68787bSBenjamin Krill 
753cf68787bSBenjamin Krill 	return result;
754cf68787bSBenjamin Krill }
755cf68787bSBenjamin Krill 
756493adffcSBenjamin Herrenschmidt static unsigned long prom_memparse(const char *ptr, const char **retptr)
757cf68787bSBenjamin Krill {
758cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
759cf68787bSBenjamin Krill 	int shift = 0;
760cf68787bSBenjamin Krill 
761cf68787bSBenjamin Krill 	/*
762cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
763cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
764cf68787bSBenjamin Krill 	 * the address we're linked at.
765cf68787bSBenjamin Krill 	 */
766cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
767cf68787bSBenjamin Krill 		shift = 30;
768cf68787bSBenjamin Krill 
769cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
770cf68787bSBenjamin Krill 		shift = 20;
771cf68787bSBenjamin Krill 
772cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
773cf68787bSBenjamin Krill 		shift = 10;
774cf68787bSBenjamin Krill 
775cf68787bSBenjamin Krill 	if (shift) {
776cf68787bSBenjamin Krill 		ret <<= shift;
777cf68787bSBenjamin Krill 		(*retptr)++;
778cf68787bSBenjamin Krill 	}
779cf68787bSBenjamin Krill 
780cf68787bSBenjamin Krill 	return ret;
781cf68787bSBenjamin Krill }
782cf68787bSBenjamin Krill 
7839b6b563cSPaul Mackerras /*
7849b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
7859b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
7869b6b563cSPaul Mackerras  */
7879b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
7889b6b563cSPaul Mackerras {
789cc5d0189SBenjamin Herrenschmidt 	const char *opt;
790cf68787bSBenjamin Krill 
791cc5d0189SBenjamin Herrenschmidt 	char *p;
792cbe46bd4SChristophe Leroy 	int l = 0;
7939b6b563cSPaul Mackerras 
7945827d416SAnton Blanchard 	prom_cmd_line[0] = 0;
7955827d416SAnton Blanchard 	p = prom_cmd_line;
796d79fbb3aSChris Packham 
797d79fbb3aSChris Packham 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && (long)prom.chosen > 0)
7985827d416SAnton Blanchard 		l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
799d79fbb3aSChris Packham 
800d79fbb3aSChris Packham 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) || l <= 0 || p[0] == '\0')
801d79fbb3aSChris Packham 		prom_strlcat(prom_cmd_line, " " CONFIG_CMDLINE,
802d79fbb3aSChris Packham 			     sizeof(prom_cmd_line));
803d79fbb3aSChris Packham 
8045827d416SAnton Blanchard 	prom_printf("command line: %s\n", prom_cmd_line);
8059b6b563cSPaul Mackerras 
8069b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
807450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "iommu=");
8089b6b563cSPaul Mackerras 	if (opt) {
8099b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
8109b6b563cSPaul Mackerras 		opt += 6;
8119b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
8129b6b563cSPaul Mackerras 			opt++;
813450e7dd4SChristophe Leroy 		if (!prom_strncmp(opt, "off", 3))
8145827d416SAnton Blanchard 			prom_iommu_off = 1;
815450e7dd4SChristophe Leroy 		else if (!prom_strncmp(opt, "force", 5))
8165827d416SAnton Blanchard 			prom_iommu_force_on = 1;
8179b6b563cSPaul Mackerras 	}
8189b6b563cSPaul Mackerras #endif
819450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "mem=");
820cf68787bSBenjamin Krill 	if (opt) {
821cf68787bSBenjamin Krill 		opt += 4;
8225827d416SAnton Blanchard 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
823cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
824cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
8255827d416SAnton Blanchard 		prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
826cf68787bSBenjamin Krill #endif
827cf68787bSBenjamin Krill 	}
828014d02cbSSuraj Jitindar Singh 
829c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
830c886087cSBenjamin Herrenschmidt 	prom_radix_disable = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
831450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "disable_radix");
832014d02cbSSuraj Jitindar Singh 	if (opt) {
8337a22d632SAneesh Kumar K.V 		opt += 13;
8347a22d632SAneesh Kumar K.V 		if (*opt && *opt == '=') {
8357a22d632SAneesh Kumar K.V 			bool val;
8367a22d632SAneesh Kumar K.V 
837450e7dd4SChristophe Leroy 			if (prom_strtobool(++opt, &val))
8387a22d632SAneesh Kumar K.V 				prom_radix_disable = false;
8397a22d632SAneesh Kumar K.V 			else
8407a22d632SAneesh Kumar K.V 				prom_radix_disable = val;
8417a22d632SAneesh Kumar K.V 		} else
842014d02cbSSuraj Jitindar Singh 			prom_radix_disable = true;
843014d02cbSSuraj Jitindar Singh 	}
8447a22d632SAneesh Kumar K.V 	if (prom_radix_disable)
8457a22d632SAneesh Kumar K.V 		prom_debug("Radix disabled from cmdline\n");
846a3bf9fbdSGreg Kurz 
847bf6b7661SAneesh Kumar K.V 	opt = prom_strstr(prom_cmd_line, "radix_hcall_invalidate=on");
848bf6b7661SAneesh Kumar K.V 	if (opt) {
849bf6b7661SAneesh Kumar K.V 		prom_radix_gtse_disable = true;
850bf6b7661SAneesh Kumar K.V 		prom_debug("Radix GTSE disabled from cmdline\n");
851bf6b7661SAneesh Kumar K.V 	}
852bf6b7661SAneesh Kumar K.V 
853a3bf9fbdSGreg Kurz 	opt = prom_strstr(prom_cmd_line, "xive=off");
854a3bf9fbdSGreg Kurz 	if (opt) {
855a3bf9fbdSGreg Kurz 		prom_xive_disable = true;
856a3bf9fbdSGreg Kurz 		prom_debug("XIVE disabled from cmdline\n");
857a3bf9fbdSGreg Kurz 	}
858c886087cSBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
8596a9c930bSRam Pai 
8606a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
8616a9c930bSRam Pai 	opt = prom_strstr(prom_cmd_line, "svm=");
8626a9c930bSRam Pai 	if (opt) {
8636a9c930bSRam Pai 		bool val;
8646a9c930bSRam Pai 
8656a9c930bSRam Pai 		opt += sizeof("svm=") - 1;
8666a9c930bSRam Pai 		if (!prom_strtobool(opt, &val))
8676a9c930bSRam Pai 			prom_svm_enable = val;
8686a9c930bSRam Pai 	}
8696a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
8709b6b563cSPaul Mackerras }
8719b6b563cSPaul Mackerras 
87211fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
8739b6b563cSPaul Mackerras /*
874530b5e14SNathan Fontenot  * The architecture vector has an array of PVR mask/value pairs,
875530b5e14SNathan Fontenot  * followed by # option vectors - 1, followed by the option vectors.
876530b5e14SNathan Fontenot  *
877530b5e14SNathan Fontenot  * See prom.h for the definition of the bits specified in the
878530b5e14SNathan Fontenot  * architecture vector.
879f709bfacSPaul Mackerras  */
880f709bfacSPaul Mackerras 
881e8a4fd0aSMichael Ellerman /* Firmware expects the value to be n - 1, where n is the # of vectors */
882e8a4fd0aSMichael Ellerman #define NUM_VECTORS(n)		((n) - 1)
883e8a4fd0aSMichael Ellerman 
884e8a4fd0aSMichael Ellerman /*
885e8a4fd0aSMichael Ellerman  * Firmware expects 1 + n - 2, where n is the length of the option vector in
886e8a4fd0aSMichael Ellerman  * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
887e8a4fd0aSMichael Ellerman  */
888e8a4fd0aSMichael Ellerman #define VECTOR_LENGTH(n)	(1 + (n) - 2)
889e8a4fd0aSMichael Ellerman 
890d03d1d65SMichael Ellerman struct option_vector1 {
891d03d1d65SMichael Ellerman 	u8 byte1;
892d03d1d65SMichael Ellerman 	u8 arch_versions;
893cc3d2940SPaul Mackerras 	u8 arch_versions3;
894d03d1d65SMichael Ellerman } __packed;
895d03d1d65SMichael Ellerman 
896d03d1d65SMichael Ellerman struct option_vector2 {
897d03d1d65SMichael Ellerman 	u8 byte1;
898d03d1d65SMichael Ellerman 	__be16 reserved;
899d03d1d65SMichael Ellerman 	__be32 real_base;
900d03d1d65SMichael Ellerman 	__be32 real_size;
901d03d1d65SMichael Ellerman 	__be32 virt_base;
902d03d1d65SMichael Ellerman 	__be32 virt_size;
903d03d1d65SMichael Ellerman 	__be32 load_base;
904d03d1d65SMichael Ellerman 	__be32 min_rma;
905d03d1d65SMichael Ellerman 	__be32 min_load;
906d03d1d65SMichael Ellerman 	u8 min_rma_percent;
907d03d1d65SMichael Ellerman 	u8 max_pft_size;
908d03d1d65SMichael Ellerman } __packed;
909d03d1d65SMichael Ellerman 
910d03d1d65SMichael Ellerman struct option_vector3 {
911d03d1d65SMichael Ellerman 	u8 byte1;
912d03d1d65SMichael Ellerman 	u8 byte2;
913d03d1d65SMichael Ellerman } __packed;
914d03d1d65SMichael Ellerman 
915d03d1d65SMichael Ellerman struct option_vector4 {
916d03d1d65SMichael Ellerman 	u8 byte1;
917d03d1d65SMichael Ellerman 	u8 min_vp_cap;
918d03d1d65SMichael Ellerman } __packed;
919d03d1d65SMichael Ellerman 
920d03d1d65SMichael Ellerman struct option_vector5 {
921d03d1d65SMichael Ellerman 	u8 byte1;
922d03d1d65SMichael Ellerman 	u8 byte2;
923d03d1d65SMichael Ellerman 	u8 byte3;
924d03d1d65SMichael Ellerman 	u8 cmo;
925d03d1d65SMichael Ellerman 	u8 associativity;
926d03d1d65SMichael Ellerman 	u8 bin_opts;
927d03d1d65SMichael Ellerman 	u8 micro_checkpoint;
928d03d1d65SMichael Ellerman 	u8 reserved0;
929d03d1d65SMichael Ellerman 	__be32 max_cpus;
930d03d1d65SMichael Ellerman 	__be16 papr_level;
931d03d1d65SMichael Ellerman 	__be16 reserved1;
932d03d1d65SMichael Ellerman 	u8 platform_facilities;
933d03d1d65SMichael Ellerman 	u8 reserved2;
934d03d1d65SMichael Ellerman 	__be16 reserved3;
935d03d1d65SMichael Ellerman 	u8 subprocessors;
936cc3d2940SPaul Mackerras 	u8 byte22;
937cc3d2940SPaul Mackerras 	u8 intarch;
938cc3d2940SPaul Mackerras 	u8 mmu;
939014d02cbSSuraj Jitindar Singh 	u8 hash_ext;
940014d02cbSSuraj Jitindar Singh 	u8 radix_ext;
941d03d1d65SMichael Ellerman } __packed;
942d03d1d65SMichael Ellerman 
943d03d1d65SMichael Ellerman struct option_vector6 {
944d03d1d65SMichael Ellerman 	u8 reserved;
945d03d1d65SMichael Ellerman 	u8 secondary_pteg;
946d03d1d65SMichael Ellerman 	u8 os_name;
947d03d1d65SMichael Ellerman } __packed;
948d03d1d65SMichael Ellerman 
949ffaacd97SMichael Ellerman struct option_vector7 {
950ffaacd97SMichael Ellerman 	u8 os_id[256];
951ffaacd97SMichael Ellerman } __packed;
952ffaacd97SMichael Ellerman 
95376ffb578SMichael Ellerman struct ibm_arch_vec {
954a3ea40d5SAlistair Popple 	struct { u32 mask, val; } pvrs[14];
95576ffb578SMichael Ellerman 
95676ffb578SMichael Ellerman 	u8 num_vectors;
95776ffb578SMichael Ellerman 
95876ffb578SMichael Ellerman 	u8 vec1_len;
95976ffb578SMichael Ellerman 	struct option_vector1 vec1;
96076ffb578SMichael Ellerman 
96176ffb578SMichael Ellerman 	u8 vec2_len;
96276ffb578SMichael Ellerman 	struct option_vector2 vec2;
96376ffb578SMichael Ellerman 
96476ffb578SMichael Ellerman 	u8 vec3_len;
96576ffb578SMichael Ellerman 	struct option_vector3 vec3;
96676ffb578SMichael Ellerman 
96776ffb578SMichael Ellerman 	u8 vec4_len;
96876ffb578SMichael Ellerman 	struct option_vector4 vec4;
96976ffb578SMichael Ellerman 
97076ffb578SMichael Ellerman 	u8 vec5_len;
97176ffb578SMichael Ellerman 	struct option_vector5 vec5;
97276ffb578SMichael Ellerman 
97376ffb578SMichael Ellerman 	u8 vec6_len;
97476ffb578SMichael Ellerman 	struct option_vector6 vec6;
975ffaacd97SMichael Ellerman 
976ffaacd97SMichael Ellerman 	u8 vec7_len;
977ffaacd97SMichael Ellerman 	struct option_vector7 vec7;
97876ffb578SMichael Ellerman } __packed;
97976ffb578SMichael Ellerman 
980d00e34b9SBenjamin Herrenschmidt static const struct ibm_arch_vec ibm_architecture_vec_template __initconst = {
98176ffb578SMichael Ellerman 	.pvrs = {
98276ffb578SMichael Ellerman 		{
98376ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffe0000), /* POWER5/POWER5+ */
98476ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003a0000),
98576ffb578SMichael Ellerman 		},
98676ffb578SMichael Ellerman 		{
98776ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER6 */
98876ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003e0000),
98976ffb578SMichael Ellerman 		},
99076ffb578SMichael Ellerman 		{
99176ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER7 */
99276ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003f0000),
99376ffb578SMichael Ellerman 		},
99476ffb578SMichael Ellerman 		{
99576ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8E */
99676ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004b0000),
99776ffb578SMichael Ellerman 		},
99876ffb578SMichael Ellerman 		{
99976ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8NVL */
100076ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004c0000),
100176ffb578SMichael Ellerman 		},
100276ffb578SMichael Ellerman 		{
100376ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8 */
100476ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004d0000),
100576ffb578SMichael Ellerman 		},
100676ffb578SMichael Ellerman 		{
1007cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffff0000), /* POWER9 */
1008cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x004e0000),
1009cc3d2940SPaul Mackerras 		},
1010cc3d2940SPaul Mackerras 		{
1011a3ea40d5SAlistair Popple 			.mask = cpu_to_be32(0xffff0000), /* POWER10 */
1012a3ea40d5SAlistair Popple 			.val  = cpu_to_be32(0x00800000),
1013a3ea40d5SAlistair Popple 		},
1014a3ea40d5SAlistair Popple 		{
1015a3ea40d5SAlistair Popple 			.mask = cpu_to_be32(0xffffffff), /* all 3.1-compliant */
1016a3ea40d5SAlistair Popple 			.val  = cpu_to_be32(0x0f000006),
1017a3ea40d5SAlistair Popple 		},
1018a3ea40d5SAlistair Popple 		{
1019cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffffffff), /* all 3.00-compliant */
1020cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x0f000005),
1021cc3d2940SPaul Mackerras 		},
1022cc3d2940SPaul Mackerras 		{
102376ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.07-compliant */
102476ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000004),
102576ffb578SMichael Ellerman 		},
102676ffb578SMichael Ellerman 		{
102776ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.06-compliant */
102876ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000003),
102976ffb578SMichael Ellerman 		},
103076ffb578SMichael Ellerman 		{
103176ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.05-compliant */
103276ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000002),
103376ffb578SMichael Ellerman 		},
103476ffb578SMichael Ellerman 		{
103576ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffffffe), /* all 2.04-compliant and earlier */
103676ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000001),
103776ffb578SMichael Ellerman 		},
103876ffb578SMichael Ellerman 	},
103976ffb578SMichael Ellerman 
104076ffb578SMichael Ellerman 	.num_vectors = NUM_VECTORS(6),
104176ffb578SMichael Ellerman 
104276ffb578SMichael Ellerman 	.vec1_len = VECTOR_LENGTH(sizeof(struct option_vector1)),
104376ffb578SMichael Ellerman 	.vec1 = {
104476ffb578SMichael Ellerman 		.byte1 = 0,
104576ffb578SMichael Ellerman 		.arch_versions = OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
104676ffb578SMichael Ellerman 				 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
1047a3ea40d5SAlistair Popple 		.arch_versions3 = OV1_PPC_3_00 | OV1_PPC_3_1,
104876ffb578SMichael Ellerman 	},
104976ffb578SMichael Ellerman 
105076ffb578SMichael Ellerman 	.vec2_len = VECTOR_LENGTH(sizeof(struct option_vector2)),
105176ffb578SMichael Ellerman 	/* option vector 2: Open Firmware options supported */
105276ffb578SMichael Ellerman 	.vec2 = {
105376ffb578SMichael Ellerman 		.byte1 = OV2_REAL_MODE,
105476ffb578SMichael Ellerman 		.reserved = 0,
105576ffb578SMichael Ellerman 		.real_base = cpu_to_be32(0xffffffff),
105676ffb578SMichael Ellerman 		.real_size = cpu_to_be32(0xffffffff),
105776ffb578SMichael Ellerman 		.virt_base = cpu_to_be32(0xffffffff),
105876ffb578SMichael Ellerman 		.virt_size = cpu_to_be32(0xffffffff),
105976ffb578SMichael Ellerman 		.load_base = cpu_to_be32(0xffffffff),
1060687da8fcSSukadev Bhattiprolu 		.min_rma = cpu_to_be32(512),		/* 512MB min RMA */
106176ffb578SMichael Ellerman 		.min_load = cpu_to_be32(0xffffffff),	/* full client load */
106276ffb578SMichael Ellerman 		.min_rma_percent = 0,	/* min RMA percentage of total RAM */
106376ffb578SMichael Ellerman 		.max_pft_size = 48,	/* max log_2(hash table size) */
106476ffb578SMichael Ellerman 	},
106576ffb578SMichael Ellerman 
106676ffb578SMichael Ellerman 	.vec3_len = VECTOR_LENGTH(sizeof(struct option_vector3)),
106776ffb578SMichael Ellerman 	/* option vector 3: processor options supported */
106876ffb578SMichael Ellerman 	.vec3 = {
106976ffb578SMichael Ellerman 		.byte1 = 0,			/* don't ignore, don't halt */
107076ffb578SMichael Ellerman 		.byte2 = OV3_FP | OV3_VMX | OV3_DFP,
107176ffb578SMichael Ellerman 	},
107276ffb578SMichael Ellerman 
107376ffb578SMichael Ellerman 	.vec4_len = VECTOR_LENGTH(sizeof(struct option_vector4)),
107476ffb578SMichael Ellerman 	/* option vector 4: IBM PAPR implementation */
107576ffb578SMichael Ellerman 	.vec4 = {
107676ffb578SMichael Ellerman 		.byte1 = 0,			/* don't halt */
107776ffb578SMichael Ellerman 		.min_vp_cap = OV4_MIN_ENT_CAP,	/* minimum VP entitled capacity */
107876ffb578SMichael Ellerman 	},
107976ffb578SMichael Ellerman 
108076ffb578SMichael Ellerman 	.vec5_len = VECTOR_LENGTH(sizeof(struct option_vector5)),
108176ffb578SMichael Ellerman 	/* option vector 5: PAPR/OF options */
108276ffb578SMichael Ellerman 	.vec5 = {
108376ffb578SMichael Ellerman 		.byte1 = 0,				/* don't ignore, don't halt */
108476ffb578SMichael Ellerman 		.byte2 = OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
108576ffb578SMichael Ellerman 		OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
108676ffb578SMichael Ellerman #ifdef CONFIG_PCI_MSI
108776ffb578SMichael Ellerman 		/* PCIe/MSI support.  Without MSI full PCIe is not supported */
108876ffb578SMichael Ellerman 		OV5_FEAT(OV5_MSI),
108976ffb578SMichael Ellerman #else
109076ffb578SMichael Ellerman 		0,
109176ffb578SMichael Ellerman #endif
109276ffb578SMichael Ellerman 		.byte3 = 0,
109376ffb578SMichael Ellerman 		.cmo =
109476ffb578SMichael Ellerman #ifdef CONFIG_PPC_SMLPAR
109576ffb578SMichael Ellerman 		OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
109676ffb578SMichael Ellerman #else
109776ffb578SMichael Ellerman 		0,
109876ffb578SMichael Ellerman #endif
109976ffb578SMichael Ellerman 		.associativity = OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
11003dbbaf20SMichael Roth 		.bin_opts = OV5_FEAT(OV5_RESIZE_HPT) | OV5_FEAT(OV5_HP_EVT),
110176ffb578SMichael Ellerman 		.micro_checkpoint = 0,
110276ffb578SMichael Ellerman 		.reserved0 = 0,
110376ffb578SMichael Ellerman 		.max_cpus = cpu_to_be32(NR_CPUS),	/* number of cores supported */
110476ffb578SMichael Ellerman 		.papr_level = 0,
110576ffb578SMichael Ellerman 		.reserved1 = 0,
110676ffb578SMichael Ellerman 		.platform_facilities = OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) | OV5_FEAT(OV5_PFO_HW_842),
110776ffb578SMichael Ellerman 		.reserved2 = 0,
110876ffb578SMichael Ellerman 		.reserved3 = 0,
110976ffb578SMichael Ellerman 		.subprocessors = 1,
11100a87ccd3STyrel Datwyler 		.byte22 = OV5_FEAT(OV5_DRMEM_V2) | OV5_FEAT(OV5_DRC_INFO),
1111cc3d2940SPaul Mackerras 		.intarch = 0,
1112014d02cbSSuraj Jitindar Singh 		.mmu = 0,
1113014d02cbSSuraj Jitindar Singh 		.hash_ext = 0,
1114014d02cbSSuraj Jitindar Singh 		.radix_ext = 0,
111576ffb578SMichael Ellerman 	},
111676ffb578SMichael Ellerman 
111776ffb578SMichael Ellerman 	/* option vector 6: IBM PAPR hints */
111876ffb578SMichael Ellerman 	.vec6_len = VECTOR_LENGTH(sizeof(struct option_vector6)),
111976ffb578SMichael Ellerman 	.vec6 = {
112076ffb578SMichael Ellerman 		.reserved = 0,
112176ffb578SMichael Ellerman 		.secondary_pteg = 0,
112276ffb578SMichael Ellerman 		.os_name = OV6_LINUX,
112376ffb578SMichael Ellerman 	},
1124ffaacd97SMichael Ellerman 
1125ffaacd97SMichael Ellerman 	/* option vector 7: OS Identification */
1126ffaacd97SMichael Ellerman 	.vec7_len = VECTOR_LENGTH(sizeof(struct option_vector7)),
112776ffb578SMichael Ellerman };
112876ffb578SMichael Ellerman 
1129a614f52eSBenjamin Herrenschmidt static struct ibm_arch_vec __prombss ibm_architecture_vec  ____cacheline_aligned;
1130a614f52eSBenjamin Herrenschmidt 
1131493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
1132493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
113330c69ca0SBenjamin Herrenschmidt static const struct fake_elf {
11349b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
11359b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
11369b6b563cSPaul Mackerras 	struct chrpnote {
11379b6b563cSPaul Mackerras 		u32	namesz;
11389b6b563cSPaul Mackerras 		u32	descsz;
11399b6b563cSPaul Mackerras 		u32	type;
11409b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
11419b6b563cSPaul Mackerras 		struct chrpdesc {
11429b6b563cSPaul Mackerras 			u32	real_mode;
11439b6b563cSPaul Mackerras 			u32	real_base;
11449b6b563cSPaul Mackerras 			u32	real_size;
11459b6b563cSPaul Mackerras 			u32	virt_base;
11469b6b563cSPaul Mackerras 			u32	virt_size;
11479b6b563cSPaul Mackerras 			u32	load_base;
11489b6b563cSPaul Mackerras 		} chrpdesc;
11499b6b563cSPaul Mackerras 	} chrpnote;
11509b6b563cSPaul Mackerras 	struct rpanote {
11519b6b563cSPaul Mackerras 		u32	namesz;
11529b6b563cSPaul Mackerras 		u32	descsz;
11539b6b563cSPaul Mackerras 		u32	type;
11549b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
11559b6b563cSPaul Mackerras 		struct rpadesc {
11569b6b563cSPaul Mackerras 			u32	lpar_affinity;
11579b6b563cSPaul Mackerras 			u32	min_rmo_size;
11589b6b563cSPaul Mackerras 			u32	min_rmo_percent;
11599b6b563cSPaul Mackerras 			u32	max_pft_size;
11609b6b563cSPaul Mackerras 			u32	splpar;
11619b6b563cSPaul Mackerras 			u32	min_load;
11629b6b563cSPaul Mackerras 			u32	new_mem_def;
11639b6b563cSPaul Mackerras 			u32	ignore_me;
11649b6b563cSPaul Mackerras 		} rpadesc;
11659b6b563cSPaul Mackerras 	} rpanote;
1166d00e34b9SBenjamin Herrenschmidt } fake_elf __initconst = {
11679b6b563cSPaul Mackerras 	.elfhdr = {
11689b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
11699b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
11709b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
11719b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
11729b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
11739b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
11749b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
11759b6b563cSPaul Mackerras 		.e_phnum = 2
11769b6b563cSPaul Mackerras 	},
11779b6b563cSPaul Mackerras 	.phdr = {
11789b6b563cSPaul Mackerras 		[0] = {
11799b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
11809b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
11819b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
11829b6b563cSPaul Mackerras 		}, [1] = {
11839b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
11849b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
11859b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
11869b6b563cSPaul Mackerras 		}
11879b6b563cSPaul Mackerras 	},
11889b6b563cSPaul Mackerras 	.chrpnote = {
11899b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
11909b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
11919b6b563cSPaul Mackerras 		.type = 0x1275,
11929b6b563cSPaul Mackerras 		.name = "PowerPC",
11939b6b563cSPaul Mackerras 		.chrpdesc = {
11949b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
11959b6b563cSPaul Mackerras 			.real_base = ~0U,
11969b6b563cSPaul Mackerras 			.real_size = ~0U,
11979b6b563cSPaul Mackerras 			.virt_base = ~0U,
11989b6b563cSPaul Mackerras 			.virt_size = ~0U,
11999b6b563cSPaul Mackerras 			.load_base = ~0U
12009b6b563cSPaul Mackerras 		},
12019b6b563cSPaul Mackerras 	},
12029b6b563cSPaul Mackerras 	.rpanote = {
12039b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
12049b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
12059b6b563cSPaul Mackerras 		.type = 0x12759999,
12069b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
12079b6b563cSPaul Mackerras 		.rpadesc = {
12085663a123SPaul Mackerras 			.lpar_affinity = 0,
12095663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
12109b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
12115663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
12129b6b563cSPaul Mackerras 			.splpar = 1,
12139b6b563cSPaul Mackerras 			.min_load = ~0U,
12145663a123SPaul Mackerras 			.new_mem_def = 0
12159b6b563cSPaul Mackerras 		}
12169b6b563cSPaul Mackerras 	}
12179b6b563cSPaul Mackerras };
1218493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
12199b6b563cSPaul Mackerras 
1220efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
1221efec959fSBenjamin Herrenschmidt {
1222efec959fSBenjamin Herrenschmidt 	phandle node;
1223efec959fSBenjamin Herrenschmidt 	char type[64];
1224efec959fSBenjamin Herrenschmidt 	unsigned int plen;
1225efec959fSBenjamin Herrenschmidt 
1226efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
1227efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
1228efec959fSBenjamin Herrenschmidt 		type[0] = 0;
1229efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
1230efec959fSBenjamin Herrenschmidt 
1231450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "cpu"))
1232efec959fSBenjamin Herrenschmidt 			continue;
1233efec959fSBenjamin Herrenschmidt 		/*
1234efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
1235efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
1236efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
1237efec959fSBenjamin Herrenschmidt 		 */
1238efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
1239efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
1240efec959fSBenjamin Herrenschmidt 			break;
1241efec959fSBenjamin Herrenschmidt 		plen >>= 2;
12422c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
1243efec959fSBenjamin Herrenschmidt 
1244efec959fSBenjamin Herrenschmidt 		/* Sanity check */
1245efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
12462c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
1247efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
1248efec959fSBenjamin Herrenschmidt 			return 1;
1249efec959fSBenjamin Herrenschmidt 		}
1250efec959fSBenjamin Herrenschmidt 		return plen;
1251efec959fSBenjamin Herrenschmidt 	}
1252efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
1253efec959fSBenjamin Herrenschmidt 
1254efec959fSBenjamin Herrenschmidt 	return 1;
1255efec959fSBenjamin Herrenschmidt 
1256efec959fSBenjamin Herrenschmidt }
1257efec959fSBenjamin Herrenschmidt 
1258014d02cbSSuraj Jitindar Singh static void __init prom_parse_mmu_model(u8 val,
1259014d02cbSSuraj Jitindar Singh 					struct platform_support *support)
1260014d02cbSSuraj Jitindar Singh {
1261014d02cbSSuraj Jitindar Singh 	switch (val) {
1262014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_DYNAMIC):
1263014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_EITHER): /* Either Available */
1264014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - either supported\n");
1265014d02cbSSuraj Jitindar Singh 		support->radix_mmu = !prom_radix_disable;
1266014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1267014d02cbSSuraj Jitindar Singh 		break;
1268014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */
1269014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - radix only\n");
1270014d02cbSSuraj Jitindar Singh 		if (prom_radix_disable) {
1271014d02cbSSuraj Jitindar Singh 			/*
1272014d02cbSSuraj Jitindar Singh 			 * If we __have__ to do radix, we're better off ignoring
1273014d02cbSSuraj Jitindar Singh 			 * the command line rather than not booting.
1274014d02cbSSuraj Jitindar Singh 			 */
1275014d02cbSSuraj Jitindar Singh 			prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
1276014d02cbSSuraj Jitindar Singh 		}
1277014d02cbSSuraj Jitindar Singh 		support->radix_mmu = true;
1278014d02cbSSuraj Jitindar Singh 		break;
1279014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_HASH):
1280014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - hash only\n");
1281014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1282014d02cbSSuraj Jitindar Singh 		break;
1283014d02cbSSuraj Jitindar Singh 	default:
1284014d02cbSSuraj Jitindar Singh 		prom_debug("Unknown mmu support option: 0x%x\n", val);
1285014d02cbSSuraj Jitindar Singh 		break;
1286014d02cbSSuraj Jitindar Singh 	}
1287014d02cbSSuraj Jitindar Singh }
1288014d02cbSSuraj Jitindar Singh 
1289ac5e5a54SCédric Le Goater static void __init prom_parse_xive_model(u8 val,
1290ac5e5a54SCédric Le Goater 					 struct platform_support *support)
1291ac5e5a54SCédric Le Goater {
1292ac5e5a54SCédric Le Goater 	switch (val) {
1293ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EITHER): /* Either Available */
1294ac5e5a54SCédric Le Goater 		prom_debug("XIVE - either mode supported\n");
1295a3bf9fbdSGreg Kurz 		support->xive = !prom_xive_disable;
1296ac5e5a54SCédric Le Goater 		break;
1297ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EXPLOIT): /* Only Exploitation mode */
1298ac5e5a54SCédric Le Goater 		prom_debug("XIVE - exploitation mode supported\n");
1299a3bf9fbdSGreg Kurz 		if (prom_xive_disable) {
1300a3bf9fbdSGreg Kurz 			/*
1301a3bf9fbdSGreg Kurz 			 * If we __have__ to do XIVE, we're better off ignoring
1302a3bf9fbdSGreg Kurz 			 * the command line rather than not booting.
1303a3bf9fbdSGreg Kurz 			 */
1304a3bf9fbdSGreg Kurz 			prom_printf("WARNING: Ignoring cmdline option xive=off\n");
1305a3bf9fbdSGreg Kurz 		}
1306ac5e5a54SCédric Le Goater 		support->xive = true;
1307ac5e5a54SCédric Le Goater 		break;
1308ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_LEGACY): /* Only Legacy mode */
1309ac5e5a54SCédric Le Goater 		prom_debug("XIVE - legacy mode supported\n");
1310ac5e5a54SCédric Le Goater 		break;
1311ac5e5a54SCédric Le Goater 	default:
1312ac5e5a54SCédric Le Goater 		prom_debug("Unknown xive support option: 0x%x\n", val);
1313ac5e5a54SCédric Le Goater 		break;
1314ac5e5a54SCédric Le Goater 	}
1315ac5e5a54SCédric Le Goater }
1316ac5e5a54SCédric Le Goater 
1317014d02cbSSuraj Jitindar Singh static void __init prom_parse_platform_support(u8 index, u8 val,
1318014d02cbSSuraj Jitindar Singh 					       struct platform_support *support)
1319014d02cbSSuraj Jitindar Singh {
1320014d02cbSSuraj Jitindar Singh 	switch (index) {
1321014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_MMU_SUPPORT): /* MMU Model */
1322014d02cbSSuraj Jitindar Singh 		prom_parse_mmu_model(val & OV5_FEAT(OV5_MMU_SUPPORT), support);
1323014d02cbSSuraj Jitindar Singh 		break;
1324014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_RADIX_GTSE): /* Radix Extensions */
1325bf6b7661SAneesh Kumar K.V 		if (val & OV5_FEAT(OV5_RADIX_GTSE))
1326bf6b7661SAneesh Kumar K.V 			support->radix_gtse = !prom_radix_gtse_disable;
1327014d02cbSSuraj Jitindar Singh 		break;
1328ac5e5a54SCédric Le Goater 	case OV5_INDX(OV5_XIVE_SUPPORT): /* Interrupt mode */
1329ac5e5a54SCédric Le Goater 		prom_parse_xive_model(val & OV5_FEAT(OV5_XIVE_SUPPORT),
1330ac5e5a54SCédric Le Goater 				      support);
1331ac5e5a54SCédric Le Goater 		break;
1332014d02cbSSuraj Jitindar Singh 	}
1333014d02cbSSuraj Jitindar Singh }
1334014d02cbSSuraj Jitindar Singh 
1335014d02cbSSuraj Jitindar Singh static void __init prom_check_platform_support(void)
1336014d02cbSSuraj Jitindar Singh {
1337014d02cbSSuraj Jitindar Singh 	struct platform_support supported = {
1338014d02cbSSuraj Jitindar Singh 		.hash_mmu = false,
1339014d02cbSSuraj Jitindar Singh 		.radix_mmu = false,
1340ac5e5a54SCédric Le Goater 		.radix_gtse = false,
1341ac5e5a54SCédric Le Goater 		.xive = false
1342014d02cbSSuraj Jitindar Singh 	};
1343014d02cbSSuraj Jitindar Singh 	int prop_len = prom_getproplen(prom.chosen,
1344014d02cbSSuraj Jitindar Singh 				       "ibm,arch-vec-5-platform-support");
1345a614f52eSBenjamin Herrenschmidt 
1346adcf5918SChristophe Leroy 	/*
1347adcf5918SChristophe Leroy 	 * First copy the architecture vec template
1348adcf5918SChristophe Leroy 	 *
1349adcf5918SChristophe Leroy 	 * use memcpy() instead of *vec = *vec_template so that GCC replaces it
1350adcf5918SChristophe Leroy 	 * by __memcpy() when KASAN is active
1351adcf5918SChristophe Leroy 	 */
1352adcf5918SChristophe Leroy 	memcpy(&ibm_architecture_vec, &ibm_architecture_vec_template,
1353adcf5918SChristophe Leroy 	       sizeof(ibm_architecture_vec));
1354a614f52eSBenjamin Herrenschmidt 
1355ffaacd97SMichael Ellerman 	prom_strscpy_pad(ibm_architecture_vec.vec7.os_id, linux_banner, 256);
1356ffaacd97SMichael Ellerman 
1357014d02cbSSuraj Jitindar Singh 	if (prop_len > 1) {
1358014d02cbSSuraj Jitindar Singh 		int i;
1359ab912399SSuraj Jitindar Singh 		u8 vec[8];
1360014d02cbSSuraj Jitindar Singh 		prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1361014d02cbSSuraj Jitindar Singh 			   prop_len);
1362ab912399SSuraj Jitindar Singh 		if (prop_len > sizeof(vec))
1363ab912399SSuraj Jitindar Singh 			prom_printf("WARNING: ibm,arch-vec-5-platform-support longer than expected (len: %d)\n",
1364ab912399SSuraj Jitindar Singh 				    prop_len);
1365ed5b00a0SCédric Le Goater 		prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support", &vec, sizeof(vec));
1366ed5b00a0SCédric Le Goater 		for (i = 0; i < prop_len; i += 2) {
1367ed5b00a0SCédric Le Goater 			prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2, vec[i], vec[i + 1]);
1368ed5b00a0SCédric Le Goater 			prom_parse_platform_support(vec[i], vec[i + 1], &supported);
1369014d02cbSSuraj Jitindar Singh 		}
1370014d02cbSSuraj Jitindar Singh 	}
1371014d02cbSSuraj Jitindar Singh 
1372029ab30bSBharata B Rao 	if (supported.radix_mmu && IS_ENABLED(CONFIG_PPC_RADIX_MMU)) {
1373029ab30bSBharata B Rao 		/* Radix preferred - Check if GTSE is also supported */
1374029ab30bSBharata B Rao 		prom_debug("Asking for radix\n");
1375014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1376029ab30bSBharata B Rao 		if (supported.radix_gtse)
1377029ab30bSBharata B Rao 			ibm_architecture_vec.vec5.radix_ext =
1378029ab30bSBharata B Rao 					OV5_FEAT(OV5_RADIX_GTSE);
1379029ab30bSBharata B Rao 		else
1380029ab30bSBharata B Rao 			prom_debug("Radix GTSE isn't supported\n");
1381014d02cbSSuraj Jitindar Singh 	} else if (supported.hash_mmu) {
1382014d02cbSSuraj Jitindar Singh 		/* Default to hash mmu (if we can) */
1383014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for hash\n");
1384014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1385014d02cbSSuraj Jitindar Singh 	} else {
1386014d02cbSSuraj Jitindar Singh 		/* We're probably on a legacy hypervisor */
1387014d02cbSSuraj Jitindar Singh 		prom_debug("Assuming legacy hash support\n");
1388014d02cbSSuraj Jitindar Singh 	}
1389ac5e5a54SCédric Le Goater 
1390ac5e5a54SCédric Le Goater 	if (supported.xive) {
1391ac5e5a54SCédric Le Goater 		prom_debug("Asking for XIVE\n");
1392ac5e5a54SCédric Le Goater 		ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1393ac5e5a54SCédric Le Goater 	}
1394014d02cbSSuraj Jitindar Singh }
1395efec959fSBenjamin Herrenschmidt 
13969b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
13979b6b563cSPaul Mackerras {
1398493adffcSBenjamin Herrenschmidt 	ihandle root;
1399f709bfacSPaul Mackerras 	prom_arg_t ret;
1400dbd0c5d5SLaurent Dufour 	u32 cores;
14019b6b563cSPaul Mackerras 
1402014d02cbSSuraj Jitindar Singh 	/* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1403014d02cbSSuraj Jitindar Singh 	prom_check_platform_support();
1404014d02cbSSuraj Jitindar Singh 
1405f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
1406f709bfacSPaul Mackerras 	if (root != 0) {
1407efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
1408efec959fSBenjamin Herrenschmidt 		 *
1409efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
1410efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
1411efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
1412efec959fSBenjamin Herrenschmidt 		 */
1413dbd0c5d5SLaurent Dufour 
1414dbd0c5d5SLaurent Dufour 		cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1415eae5f709SMathieu Malaterre 		prom_printf("Max number of cores passed to firmware: %u (NR_CPUS = %d)\n",
1416dbd0c5d5SLaurent Dufour 			    cores, NR_CPUS);
141776ffb578SMichael Ellerman 
141876ffb578SMichael Ellerman 		ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1419efec959fSBenjamin Herrenschmidt 
1420f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
1421049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
1422f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
1423f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
142433b74977SBenjamin Herrenschmidt 				  root,
142576ffb578SMichael Ellerman 				  ADDR(&ibm_architecture_vec)) == 0) {
1426f709bfacSPaul Mackerras 			/* the call exists... */
1427f709bfacSPaul Mackerras 			if (ret)
14284da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
1429f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
1430f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
14314da727aeSAnton Blanchard 			prom_printf(" done\n");
1432f709bfacSPaul Mackerras 			return;
1433f709bfacSPaul Mackerras 		}
1434f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
1435049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
1436f709bfacSPaul Mackerras 	}
1437f709bfacSPaul Mackerras 
1438493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
1439493adffcSBenjamin Herrenschmidt 	{
1440493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
1441493adffcSBenjamin Herrenschmidt 
1442f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
1443493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
1444493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
14459b6b563cSPaul Mackerras 		if (elfloader == 0) {
14469b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
14479b6b563cSPaul Mackerras 			return;
14489b6b563cSPaul Mackerras 		}
14499b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
14509b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
14519b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
14529b6b563cSPaul Mackerras 	}
1453493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1454493adffcSBenjamin Herrenschmidt }
145511fdb309SBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
14569b6b563cSPaul Mackerras 
14579b6b563cSPaul Mackerras /*
14589b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
14599b6b563cSPaul Mackerras  *
14609b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
14619b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
14629b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
14639b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
14649b6b563cSPaul Mackerras  *  never happen.
14659b6b563cSPaul Mackerras  *
14669b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
14679b6b563cSPaul Mackerras  *  TCEs overlap
14689b6b563cSPaul Mackerras  *
14699b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
14709b6b563cSPaul Mackerras  *
14719b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
14729b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
14739b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
14749b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
14759b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
14769b6b563cSPaul Mackerras  *
14779b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
14789b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
14799b6b563cSPaul Mackerras  *  reserved.
14809b6b563cSPaul Mackerras  */
14819b6b563cSPaul Mackerras 
14829b6b563cSPaul Mackerras 
14839b6b563cSPaul Mackerras /*
14849b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
14859b6b563cSPaul Mackerras  *
14869b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
14879b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
14889b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
14899b6b563cSPaul Mackerras  */
14909b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
14919b6b563cSPaul Mackerras {
14925827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
14939b6b563cSPaul Mackerras 	unsigned long addr = 0;
14949b6b563cSPaul Mackerras 
1495c4988820SPaul Mackerras 	if (align)
1496b7115316SChristophe Leroy 		base = ALIGN(base, align);
1497eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx)\n", __func__, size, align);
14985827d416SAnton Blanchard 	if (ram_top == 0)
14999b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
15009b6b563cSPaul Mackerras 
15019b6b563cSPaul Mackerras 	if (align)
1502b7115316SChristophe Leroy 		base = ALIGN(alloc_bottom, align);
15039b6b563cSPaul Mackerras 	else
15045827d416SAnton Blanchard 		base = alloc_bottom;
15059b6b563cSPaul Mackerras 
15065827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
1507b7115316SChristophe Leroy 	    base = ALIGN(base + 0x100000, align)) {
1508eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
15099b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1510c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
15119b6b563cSPaul Mackerras 			break;
15129b6b563cSPaul Mackerras 		addr = 0;
15139b6b563cSPaul Mackerras 		if (align == 0)
15149b6b563cSPaul Mackerras 			break;
15159b6b563cSPaul Mackerras 	}
15169b6b563cSPaul Mackerras 	if (addr == 0)
15179b6b563cSPaul Mackerras 		return 0;
15185827d416SAnton Blanchard 	alloc_bottom = addr + size;
15199b6b563cSPaul Mackerras 
1520eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1521eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1522eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1523eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1524eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1525eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
15269b6b563cSPaul Mackerras 
15279b6b563cSPaul Mackerras 	return addr;
15289b6b563cSPaul Mackerras }
15299b6b563cSPaul Mackerras 
15309b6b563cSPaul Mackerras /*
15319b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
15329b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
15339b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
15349b6b563cSPaul Mackerras  */
15359b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
15369b6b563cSPaul Mackerras 				       int highmem)
15379b6b563cSPaul Mackerras {
15389b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
15399b6b563cSPaul Mackerras 
1540eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx, %s)\n", __func__, size, align,
15415827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
15425827d416SAnton Blanchard 	if (ram_top == 0)
15439b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
15449b6b563cSPaul Mackerras 
15459b6b563cSPaul Mackerras 	if (highmem) {
15469b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
1547e96d904eSChristophe Leroy 		addr = ALIGN_DOWN(alloc_top_high - size, align);
15485827d416SAnton Blanchard 		if (addr <= alloc_bottom)
15499b6b563cSPaul Mackerras 			return 0;
15509b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
15519b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
15529b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
15539b6b563cSPaul Mackerras 		 */
15545827d416SAnton Blanchard 		if (addr < rmo_top) {
15559b6b563cSPaul Mackerras 			/* Good, we are first */
15565827d416SAnton Blanchard 			if (alloc_top == rmo_top)
15575827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
15589b6b563cSPaul Mackerras 			else
15599b6b563cSPaul Mackerras 				return 0;
15609b6b563cSPaul Mackerras 		}
15615827d416SAnton Blanchard 		alloc_top_high = addr;
15629b6b563cSPaul Mackerras 		goto bail;
15639b6b563cSPaul Mackerras 	}
15649b6b563cSPaul Mackerras 
1565e96d904eSChristophe Leroy 	base = ALIGN_DOWN(alloc_top - size, align);
15665827d416SAnton Blanchard 	for (; base > alloc_bottom;
1567e96d904eSChristophe Leroy 	     base = ALIGN_DOWN(base - 0x100000, align))  {
1568eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
15699b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1570c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
15719b6b563cSPaul Mackerras 			break;
15729b6b563cSPaul Mackerras 		addr = 0;
15739b6b563cSPaul Mackerras 	}
15749b6b563cSPaul Mackerras 	if (addr == 0)
15759b6b563cSPaul Mackerras 		return 0;
15765827d416SAnton Blanchard 	alloc_top = addr;
15779b6b563cSPaul Mackerras 
15789b6b563cSPaul Mackerras  bail:
1579eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1580eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1581eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1582eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1583eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1584eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
15859b6b563cSPaul Mackerras 
15869b6b563cSPaul Mackerras 	return addr;
15879b6b563cSPaul Mackerras }
15889b6b563cSPaul Mackerras 
15899b6b563cSPaul Mackerras /*
15909b6b563cSPaul Mackerras  * Parse a "reg" cell
15919b6b563cSPaul Mackerras  */
15929b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
15939b6b563cSPaul Mackerras {
15949b6b563cSPaul Mackerras 	cell_t *p = *cellp;
15959b6b563cSPaul Mackerras 	unsigned long r = 0;
15969b6b563cSPaul Mackerras 
15979b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
15989b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
15999b6b563cSPaul Mackerras 		p++;
16009b6b563cSPaul Mackerras 		s--;
16019b6b563cSPaul Mackerras 	}
1602493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
16039b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
160435499c01SPaul Mackerras 	if (s > 1) {
16059b6b563cSPaul Mackerras 		r <<= 32;
1606493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
16079b6b563cSPaul Mackerras 	}
16089b6b563cSPaul Mackerras #endif
16099b6b563cSPaul Mackerras 	*cellp = p;
16109b6b563cSPaul Mackerras 	return r;
16119b6b563cSPaul Mackerras }
16129b6b563cSPaul Mackerras 
16139b6b563cSPaul Mackerras /*
16149b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
16159b6b563cSPaul Mackerras  * we don't need anything smarter at this point
16169b6b563cSPaul Mackerras  *
16179b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
16189b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
16199b6b563cSPaul Mackerras  * them down.
16209b6b563cSPaul Mackerras  */
16210108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
16229b6b563cSPaul Mackerras {
1623cbbcf340SKumar Gala 	u64 top = base + size;
16245827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
16259b6b563cSPaul Mackerras 
16269b6b563cSPaul Mackerras 	if (size == 0)
16279b6b563cSPaul Mackerras 		return;
16289b6b563cSPaul Mackerras 
16299b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
16309b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
16319b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
16329b6b563cSPaul Mackerras 	 */
1633e96d904eSChristophe Leroy 	base = ALIGN_DOWN(base, PAGE_SIZE);
1634b7115316SChristophe Leroy 	top = ALIGN(top, PAGE_SIZE);
16359b6b563cSPaul Mackerras 	size = top - base;
16369b6b563cSPaul Mackerras 
16379b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
16389b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1639493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1640493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
16415827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
16429b6b563cSPaul Mackerras }
16439b6b563cSPaul Mackerras 
16449b6b563cSPaul Mackerras /*
1645b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
16469b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
16479b6b563cSPaul Mackerras  */
16489b6b563cSPaul Mackerras static void __init prom_init_mem(void)
16499b6b563cSPaul Mackerras {
16509b6b563cSPaul Mackerras 	phandle node;
1651eab00a20SMathieu Malaterre 	char type[64];
16529b6b563cSPaul Mackerras 	unsigned int plen;
16539b6b563cSPaul Mackerras 	cell_t *p, *endp;
1654493adffcSBenjamin Herrenschmidt 	__be32 val;
16559b6b563cSPaul Mackerras 	u32 rac, rsc;
16569b6b563cSPaul Mackerras 
16579b6b563cSPaul Mackerras 	/*
16589b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
16599b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
16609b6b563cSPaul Mackerras 	 * 2) top of memory
16619b6b563cSPaul Mackerras 	 */
1662493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1663493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1664493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1665493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1666493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1667493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1668493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1669493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
16709b6b563cSPaul Mackerras 
16719b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
16729b6b563cSPaul Mackerras 
16739b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
16749b6b563cSPaul Mackerras 		type[0] = 0;
16759b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
16769b6b563cSPaul Mackerras 
1677c4988820SPaul Mackerras 		if (type[0] == 0) {
1678c4988820SPaul Mackerras 			/*
1679c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1680c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1681c4988820SPaul Mackerras 			 */
1682c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1683c4988820SPaul Mackerras 		}
1684450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "memory"))
16859b6b563cSPaul Mackerras 			continue;
16869b6b563cSPaul Mackerras 
16875827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
16889b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
16899b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
16909b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
16919b6b563cSPaul Mackerras 		}
16925827d416SAnton Blanchard 		p = regbuf;
16939b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
16949b6b563cSPaul Mackerras 
16959b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1696c806a6fdSMathieu Malaterre 		memset(prom_scratch, 0, sizeof(prom_scratch));
1697c806a6fdSMathieu Malaterre 		call_prom("package-to-path", 3, 1, node, prom_scratch,
1698c806a6fdSMathieu Malaterre 			  sizeof(prom_scratch) - 1);
1699c806a6fdSMathieu Malaterre 		prom_debug("  node %s :\n", prom_scratch);
17009b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
17019b6b563cSPaul Mackerras 
17029b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
17039b6b563cSPaul Mackerras 			unsigned long base, size;
17049b6b563cSPaul Mackerras 
17059b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
17069b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
17079b6b563cSPaul Mackerras 
17089b6b563cSPaul Mackerras 			if (size == 0)
17099b6b563cSPaul Mackerras 				continue;
1710eae5f709SMathieu Malaterre 			prom_debug("    %lx %lx\n", base, size);
17115827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
17125827d416SAnton Blanchard 				rmo_top = size;
17135827d416SAnton Blanchard 			if ((base + size) > ram_top)
17145827d416SAnton Blanchard 				ram_top = base + size;
17159b6b563cSPaul Mackerras 		}
17169b6b563cSPaul Mackerras 	}
17179b6b563cSPaul Mackerras 
17185827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
17199b6b563cSPaul Mackerras 
17209b6b563cSPaul Mackerras 	/*
1721cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1722cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1723cf68787bSBenjamin Krill 	 * TCE's up there.
1724cf68787bSBenjamin Krill 	 */
1725cf68787bSBenjamin Krill 
17265827d416SAnton Blanchard 	alloc_top_high = ram_top;
1727cf68787bSBenjamin Krill 
17285827d416SAnton Blanchard 	if (prom_memory_limit) {
17295827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1730eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx <= alloc_bottom.\n",
17315827d416SAnton Blanchard 				    prom_memory_limit);
17325827d416SAnton Blanchard 			prom_memory_limit = 0;
17335827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1734eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx >= ram_top.\n",
17355827d416SAnton Blanchard 				    prom_memory_limit);
17365827d416SAnton Blanchard 			prom_memory_limit = 0;
1737cf68787bSBenjamin Krill 		} else {
17385827d416SAnton Blanchard 			ram_top = prom_memory_limit;
17395827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1740cf68787bSBenjamin Krill 		}
1741cf68787bSBenjamin Krill 	}
1742cf68787bSBenjamin Krill 
1743cf68787bSBenjamin Krill 	/*
17449b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
17459b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
17469b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
17479b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
17489b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
17499b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
17509b6b563cSPaul Mackerras 	 */
17515827d416SAnton Blanchard 	if (!rmo_top)
17525827d416SAnton Blanchard 		rmo_top = ram_top;
17535827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
17545827d416SAnton Blanchard 	alloc_top = rmo_top;
17555827d416SAnton Blanchard 	alloc_top_high = ram_top;
17569b6b563cSPaul Mackerras 
175764968f60SPaul Mackerras 	/*
175864968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
175964968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
176064968f60SPaul Mackerras 	 */
17615827d416SAnton Blanchard 	if (prom_initrd_start &&
17625827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
17635827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
17645827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
176564968f60SPaul Mackerras 
17669b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
1767eae5f709SMathieu Malaterre 	prom_printf("  memory_limit : %lx (16 MB aligned)\n",
1768eae5f709SMathieu Malaterre 		    prom_memory_limit);
1769eae5f709SMathieu Malaterre 	prom_printf("  alloc_bottom : %lx\n", alloc_bottom);
1770eae5f709SMathieu Malaterre 	prom_printf("  alloc_top    : %lx\n", alloc_top);
1771eae5f709SMathieu Malaterre 	prom_printf("  alloc_top_hi : %lx\n", alloc_top_high);
1772eae5f709SMathieu Malaterre 	prom_printf("  rmo_top      : %lx\n", rmo_top);
1773eae5f709SMathieu Malaterre 	prom_printf("  ram_top      : %lx\n", ram_top);
17749b6b563cSPaul Mackerras }
17759b6b563cSPaul Mackerras 
177627f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
177727f44888SBenjamin Herrenschmidt {
1778493adffcSBenjamin Herrenschmidt 	__be32 val;
1779493adffcSBenjamin Herrenschmidt 	ihandle stdin;
178027f44888SBenjamin Herrenschmidt 
1781493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1782493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1783493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1784493adffcSBenjamin Herrenschmidt 	}
178527f44888SBenjamin Herrenschmidt }
178627f44888SBenjamin Herrenschmidt 
17876a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
17886a9c930bSRam Pai static int prom_rtas_hcall(uint64_t args)
17896a9c930bSRam Pai {
17906a9c930bSRam Pai 	register uint64_t arg1 asm("r3") = H_RTAS;
17916a9c930bSRam Pai 	register uint64_t arg2 asm("r4") = args;
17926a9c930bSRam Pai 
17936a9c930bSRam Pai 	asm volatile("sc 1\n" : "=r" (arg1) :
17946a9c930bSRam Pai 			"r" (arg1),
17956a9c930bSRam Pai 			"r" (arg2) :);
1796*59dc5bfcSNicholas Piggin 	srr_regs_clobbered();
1797*59dc5bfcSNicholas Piggin 
17986a9c930bSRam Pai 	return arg1;
17996a9c930bSRam Pai }
18006a9c930bSRam Pai 
18016a9c930bSRam Pai static struct rtas_args __prombss os_term_args;
18026a9c930bSRam Pai 
18036a9c930bSRam Pai static void __init prom_rtas_os_term(char *str)
18046a9c930bSRam Pai {
18056a9c930bSRam Pai 	phandle rtas_node;
18066a9c930bSRam Pai 	__be32 val;
18076a9c930bSRam Pai 	u32 token;
18086a9c930bSRam Pai 
18096a9c930bSRam Pai 	prom_debug("%s: start...\n", __func__);
18106a9c930bSRam Pai 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
18116a9c930bSRam Pai 	prom_debug("rtas_node: %x\n", rtas_node);
18126a9c930bSRam Pai 	if (!PHANDLE_VALID(rtas_node))
18136a9c930bSRam Pai 		return;
18146a9c930bSRam Pai 
18156a9c930bSRam Pai 	val = 0;
18166a9c930bSRam Pai 	prom_getprop(rtas_node, "ibm,os-term", &val, sizeof(val));
18176a9c930bSRam Pai 	token = be32_to_cpu(val);
18186a9c930bSRam Pai 	prom_debug("ibm,os-term: %x\n", token);
18196a9c930bSRam Pai 	if (token == 0)
18206a9c930bSRam Pai 		prom_panic("Could not get token for ibm,os-term\n");
18216a9c930bSRam Pai 	os_term_args.token = cpu_to_be32(token);
182274bb84e5SAlexey Kardashevskiy 	os_term_args.nargs = cpu_to_be32(1);
182374bb84e5SAlexey Kardashevskiy 	os_term_args.nret = cpu_to_be32(1);
182474bb84e5SAlexey Kardashevskiy 	os_term_args.args[0] = cpu_to_be32(__pa(str));
18256a9c930bSRam Pai 	prom_rtas_hcall((uint64_t)&os_term_args);
18266a9c930bSRam Pai }
18276a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
18286a9c930bSRam Pai 
18299b6b563cSPaul Mackerras /*
18309b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
18319b6b563cSPaul Mackerras  */
18329b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
18339b6b563cSPaul Mackerras {
18349b6b563cSPaul Mackerras 	phandle rtas_node;
18359b6b563cSPaul Mackerras 	ihandle rtas_inst;
18369b6b563cSPaul Mackerras 	u32 base, entry = 0;
1837493adffcSBenjamin Herrenschmidt 	__be32 val;
18389b6b563cSPaul Mackerras 	u32 size = 0;
18399b6b563cSPaul Mackerras 
18409b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
18419b6b563cSPaul Mackerras 
18429b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
18439b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
18449b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
18459b6b563cSPaul Mackerras 		return;
18469b6b563cSPaul Mackerras 
1847493adffcSBenjamin Herrenschmidt 	val = 0;
1848493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1849493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
18509b6b563cSPaul Mackerras 	if (size == 0)
18519b6b563cSPaul Mackerras 		return;
18529b6b563cSPaul Mackerras 
18539b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
18546d1e2c6cSAnton Blanchard 	if (base == 0)
18556d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
18569b6b563cSPaul Mackerras 
18579b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
18589b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1859a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
18609b6b563cSPaul Mackerras 		return;
18619b6b563cSPaul Mackerras 	}
18629b6b563cSPaul Mackerras 
18639b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
18649b6b563cSPaul Mackerras 
18659b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
18669b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1867a23414beSPaul Mackerras 			  rtas_inst, base) != 0
18689b6b563cSPaul Mackerras 	    || entry == 0) {
18699b6b563cSPaul Mackerras 		prom_printf(" failed\n");
18709b6b563cSPaul Mackerras 		return;
18719b6b563cSPaul Mackerras 	}
18729b6b563cSPaul Mackerras 	prom_printf(" done\n");
18739b6b563cSPaul Mackerras 
18749b6b563cSPaul Mackerras 	reserve_mem(base, size);
18759b6b563cSPaul Mackerras 
1876493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1877a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1878493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1879493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1880a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1881493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
18829b6b563cSPaul Mackerras 
1883dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1884dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1885dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1886dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1887dbe78b40SBenjamin Herrenschmidt 
18889b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
18899b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
1890eae5f709SMathieu Malaterre 	prom_debug("rtas size     = 0x%x\n", size);
18919b6b563cSPaul Mackerras 
18929b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
18939b6b563cSPaul Mackerras }
18949b6b563cSPaul Mackerras 
18959b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
18969b6b563cSPaul Mackerras /*
18974a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
18984a727429SAshley Lai  */
18994a727429SAshley Lai static void __init prom_instantiate_sml(void)
19004a727429SAshley Lai {
19014a727429SAshley Lai 	phandle ibmvtpm_node;
19024a727429SAshley Lai 	ihandle ibmvtpm_inst;
1903b4ed0469SHon Ching \(Vicky\) Lo 	u32 entry = 0, size = 0, succ = 0;
19044a727429SAshley Lai 	u64 base;
1905b4ed0469SHon Ching \(Vicky\) Lo 	__be32 val;
19064a727429SAshley Lai 
19074a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
19084a727429SAshley Lai 
19092f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
19104a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
19114a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
19124a727429SAshley Lai 		return;
19134a727429SAshley Lai 
19142f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
19154a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
19164a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
19174a727429SAshley Lai 		return;
19184a727429SAshley Lai 	}
19194a727429SAshley Lai 
1920b4ed0469SHon Ching \(Vicky\) Lo 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1921b4ed0469SHon Ching \(Vicky\) Lo 			 &val, sizeof(val)) != PROM_ERROR) {
1922b4ed0469SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &succ,
1923b4ed0469SHon Ching \(Vicky\) Lo 				  ADDR("reformat-sml-to-efi-alignment"),
1924b4ed0469SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || succ == 0) {
1925b4ed0469SHon Ching \(Vicky\) Lo 			prom_printf("Reformat SML to EFI alignment failed\n");
1926b4ed0469SHon Ching \(Vicky\) Lo 			return;
1927b4ed0469SHon Ching \(Vicky\) Lo 		}
1928b4ed0469SHon Ching \(Vicky\) Lo 
19294a727429SAshley Lai 		if (call_prom_ret("call-method", 2, 2, &size,
19309e5d4af4SHon Ching \(Vicky\) Lo 				  ADDR("sml-get-allocated-size"),
19319e5d4af4SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || size == 0) {
19329e5d4af4SHon Ching \(Vicky\) Lo 			prom_printf("SML get allocated size failed\n");
19339e5d4af4SHon Ching \(Vicky\) Lo 			return;
19349e5d4af4SHon Ching \(Vicky\) Lo 		}
19359e5d4af4SHon Ching \(Vicky\) Lo 	} else {
19369e5d4af4SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &size,
19374a727429SAshley Lai 				  ADDR("sml-get-handover-size"),
19384a727429SAshley Lai 				  ibmvtpm_inst) != 0 || size == 0) {
19394a727429SAshley Lai 			prom_printf("SML get handover size failed\n");
19404a727429SAshley Lai 			return;
19414a727429SAshley Lai 		}
19429e5d4af4SHon Ching \(Vicky\) Lo 	}
19434a727429SAshley Lai 
19444a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
19454a727429SAshley Lai 	if (base == 0)
19464a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
19474a727429SAshley Lai 
1948eae5f709SMathieu Malaterre 	prom_printf("instantiating sml at 0x%llx...", base);
19494a727429SAshley Lai 
19509e5d4af4SHon Ching \(Vicky\) Lo 	memset((void *)base, 0, size);
19519e5d4af4SHon Ching \(Vicky\) Lo 
19524a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
19534a727429SAshley Lai 			  ADDR("sml-handover"),
19544a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
19554a727429SAshley Lai 		prom_printf("SML handover failed\n");
19564a727429SAshley Lai 		return;
19574a727429SAshley Lai 	}
19584a727429SAshley Lai 	prom_printf(" done\n");
19594a727429SAshley Lai 
19604a727429SAshley Lai 	reserve_mem(base, size);
19614a727429SAshley Lai 
19622f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
19634a727429SAshley Lai 		     &base, sizeof(base));
19642f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
19654a727429SAshley Lai 		     &size, sizeof(size));
19664a727429SAshley Lai 
1967eae5f709SMathieu Malaterre 	prom_debug("sml base     = 0x%llx\n", base);
1968eae5f709SMathieu Malaterre 	prom_debug("sml size     = 0x%x\n", size);
19694a727429SAshley Lai 
19704a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
19714a727429SAshley Lai }
19724a727429SAshley Lai 
19734a727429SAshley Lai /*
19749b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
19759b6b563cSPaul Mackerras  */
1976493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
19779b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
19789b6b563cSPaul Mackerras {
19799b6b563cSPaul Mackerras 	phandle node;
19809b6b563cSPaul Mackerras 	ihandle phb_node;
19819b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
19825827d416SAnton Blanchard 	char *path = prom_scratch;
19839b6b563cSPaul Mackerras 	u64 base, align;
19849b6b563cSPaul Mackerras 	u32 minalign, minsize;
19859b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
19869b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
19879b6b563cSPaul Mackerras 	u64 i;
19889b6b563cSPaul Mackerras 
19895827d416SAnton Blanchard 	if (prom_iommu_off)
19909b6b563cSPaul Mackerras 		return;
19919b6b563cSPaul Mackerras 
19929b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
19939b6b563cSPaul Mackerras 
19949b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
19955827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
19969b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
19979b6b563cSPaul Mackerras 
19989b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
19999b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
20009b6b563cSPaul Mackerras 		compatible[0] = 0;
20019b6b563cSPaul Mackerras 		type[0] = 0;
20029b6b563cSPaul Mackerras 		model[0] = 0;
20039b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
20049b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
20059b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
20069b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
20079b6b563cSPaul Mackerras 
2008450e7dd4SChristophe Leroy 		if ((type[0] == 0) || (prom_strstr(type, "pci") == NULL))
20099b6b563cSPaul Mackerras 			continue;
20109b6b563cSPaul Mackerras 
2011e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
20129b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
2013450e7dd4SChristophe Leroy 			if ((prom_strstr(compatible, "python") == NULL) &&
2014450e7dd4SChristophe Leroy 			    (prom_strstr(compatible, "Speedwagon") == NULL) &&
2015450e7dd4SChristophe Leroy 			    (prom_strstr(compatible, "Winnipeg") == NULL))
20169b6b563cSPaul Mackerras 				continue;
20179b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
2018450e7dd4SChristophe Leroy 			if ((prom_strstr(model, "ython") == NULL) &&
2019450e7dd4SChristophe Leroy 			    (prom_strstr(model, "peedwagon") == NULL) &&
2020450e7dd4SChristophe Leroy 			    (prom_strstr(model, "innipeg") == NULL))
20219b6b563cSPaul Mackerras 				continue;
20229b6b563cSPaul Mackerras 		}
20239b6b563cSPaul Mackerras 
20249b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
20259b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
20269b6b563cSPaul Mackerras 			minalign = 0;
20279b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
20289b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
20299b6b563cSPaul Mackerras 			minsize = 4UL << 20;
20309b6b563cSPaul Mackerras 
20319b6b563cSPaul Mackerras 		/*
20329b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
20339b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
20349b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
20359b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
20369b6b563cSPaul Mackerras 		 */
20379b6b563cSPaul Mackerras 		minsize = 4UL << 20;
20389b6b563cSPaul Mackerras 
20399b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
20409b6b563cSPaul Mackerras 		align = max(minalign, minsize);
20419b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
20429b6b563cSPaul Mackerras 		if (base == 0)
20439b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
20449b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
20459b6b563cSPaul Mackerras 			local_alloc_bottom = base;
20469b6b563cSPaul Mackerras 
20479b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
2048d7fbe2a0SChristophe Leroy 		memset(path, 0, sizeof(prom_scratch));
20499b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
20509b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
2051d7fbe2a0SChristophe Leroy 			      path, sizeof(prom_scratch) - 1) == PROM_ERROR) {
20529b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
20539b6b563cSPaul Mackerras 		}
20549b6b563cSPaul Mackerras 
2055a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
2056a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
2057a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
2058a23414beSPaul Mackerras 
20599b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
20609b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
2061eae5f709SMathieu Malaterre 		prom_debug("\tbase = 0x%llx\n", base);
20629b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
20639b6b563cSPaul Mackerras 
20649b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
20659b6b563cSPaul Mackerras 		 * over the allocated size.
20669b6b563cSPaul Mackerras 		 */
20672b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
20689b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
20699b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
20709b6b563cSPaul Mackerras 			tce_entry |= 0x3;
20719b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
20729b6b563cSPaul Mackerras 		}
20739b6b563cSPaul Mackerras 
20749b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
20759b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
20769b6b563cSPaul Mackerras 		if (phb_node == 0)
20779b6b563cSPaul Mackerras 			prom_printf("... failed\n");
20789b6b563cSPaul Mackerras 		else
20799b6b563cSPaul Mackerras 			prom_printf("... done\n");
20809b6b563cSPaul Mackerras 
20819b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
20829b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
20839b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
20849b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
20859b6b563cSPaul Mackerras 	}
20869b6b563cSPaul Mackerras 
20879b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
20889b6b563cSPaul Mackerras 
20892babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
20902babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
20915827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
20925827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
20939b6b563cSPaul Mackerras 
20949b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
20959b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
20969b6b563cSPaul Mackerras }
2097493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
2098493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
20999b6b563cSPaul Mackerras 
21009b6b563cSPaul Mackerras /*
21019b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
21029b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
21039b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
21049b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
21059b6b563cSPaul Mackerras  *
21069b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
21079b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
21089b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
21099b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
21109b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
21119b6b563cSPaul Mackerras  * of setting those values.
21129b6b563cSPaul Mackerras  *
21139b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
21149b6b563cSPaul Mackerras  * is in its holding pattern code.
21159b6b563cSPaul Mackerras  *
21169b6b563cSPaul Mackerras  * -- Cort
21179b6b563cSPaul Mackerras  */
2118bbd0abdaSPaul Mackerras /*
2119bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
2120bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
2121bbd0abdaSPaul Mackerras  */
2122bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
2123bbd0abdaSPaul Mackerras 
21249b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
21259b6b563cSPaul Mackerras {
21269b6b563cSPaul Mackerras 	unsigned long i;
21279b6b563cSPaul Mackerras 	phandle node;
21289b6b563cSPaul Mackerras 	char type[64];
21299b6b563cSPaul Mackerras 	unsigned long *spinloop
2130bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
21319b6b563cSPaul Mackerras 	unsigned long *acknowledge
2132bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
2133bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
21349b6b563cSPaul Mackerras 
2135dbe78b40SBenjamin Herrenschmidt 	/*
2136dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
2137dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
2138dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
2139dbe78b40SBenjamin Herrenschmidt 	 */
2140dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
2141dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
2142dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
2143dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
2144dbe78b40SBenjamin Herrenschmidt 		return;
2145dbe78b40SBenjamin Herrenschmidt 	}
2146dbe78b40SBenjamin Herrenschmidt 
21479b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
2148eae5f709SMathieu Malaterre 	prom_debug("    1) spinloop       = 0x%lx\n", (unsigned long)spinloop);
2149eae5f709SMathieu Malaterre 	prom_debug("    1) *spinloop      = 0x%lx\n", *spinloop);
2150eae5f709SMathieu Malaterre 	prom_debug("    1) acknowledge    = 0x%lx\n",
21519b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
2152eae5f709SMathieu Malaterre 	prom_debug("    1) *acknowledge   = 0x%lx\n", *acknowledge);
2153eae5f709SMathieu Malaterre 	prom_debug("    1) secondary_hold = 0x%lx\n", secondary_hold);
21549b6b563cSPaul Mackerras 
21559b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
21569b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
21579b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
21589b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
21599b6b563cSPaul Mackerras 	 */
21609b6b563cSPaul Mackerras 	*spinloop = 0;
21619b6b563cSPaul Mackerras 
21629b6b563cSPaul Mackerras 	/* look for cpus */
21639b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
2164493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
2165493adffcSBenjamin Herrenschmidt 		__be32 reg;
2166493adffcSBenjamin Herrenschmidt 
21679b6b563cSPaul Mackerras 		type[0] = 0;
21689b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
2169450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "cpu") != 0)
21709b6b563cSPaul Mackerras 			continue;
21719b6b563cSPaul Mackerras 
21729b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
21739b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
2174450e7dd4SChristophe Leroy 			if (prom_strcmp(type, "okay") != 0)
21759b6b563cSPaul Mackerras 				continue;
21769b6b563cSPaul Mackerras 
2177493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
21789b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
2179493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
21809b6b563cSPaul Mackerras 
2181eae5f709SMathieu Malaterre 		prom_debug("cpu hw idx   = %u\n", cpu_no);
21829b6b563cSPaul Mackerras 
21839b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
21849b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
21859b6b563cSPaul Mackerras 		 * spinloop.
21869b6b563cSPaul Mackerras 		 */
21879b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
21889b6b563cSPaul Mackerras 
2189493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
219027f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
2191eae5f709SMathieu Malaterre 			prom_printf("starting cpu hw idx %u... ", cpu_no);
21929b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
2193493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
21949b6b563cSPaul Mackerras 
21959b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
21969b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
21979b6b563cSPaul Mackerras 				mb();
21989b6b563cSPaul Mackerras 
2199493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
22009b6b563cSPaul Mackerras 				prom_printf("done\n");
2201bbd0abdaSPaul Mackerras 			else
2202eae5f709SMathieu Malaterre 				prom_printf("failed: %lx\n", *acknowledge);
22039b6b563cSPaul Mackerras 		}
22049b6b563cSPaul Mackerras #ifdef CONFIG_SMP
22059b6b563cSPaul Mackerras 		else
2206eae5f709SMathieu Malaterre 			prom_printf("boot cpu hw idx %u\n", cpu_no);
22079b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
22089b6b563cSPaul Mackerras 	}
22099b6b563cSPaul Mackerras 
22109b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
22119b6b563cSPaul Mackerras }
22129b6b563cSPaul Mackerras 
22139b6b563cSPaul Mackerras 
22149b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
22159b6b563cSPaul Mackerras {
22169b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
22175827d416SAnton Blanchard 	prom_entry = pp;
22189b6b563cSPaul Mackerras 
22199b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
22205827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
22215827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
22229b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
22239b6b563cSPaul Mackerras 
22249b6b563cSPaul Mackerras 	/* get device tree root */
22255827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
22265827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
22279b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
2228a575b807SPaul Mackerras 
22295827d416SAnton Blanchard 	prom.mmumap = 0;
22309b6b563cSPaul Mackerras }
22319b6b563cSPaul Mackerras 
2232a575b807SPaul Mackerras #ifdef CONFIG_PPC32
2233a575b807SPaul Mackerras /*
2234a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
2235a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
2236a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
2237a575b807SPaul Mackerras  */
2238a575b807SPaul Mackerras static void __init prom_find_mmu(void)
2239a575b807SPaul Mackerras {
2240a575b807SPaul Mackerras 	phandle oprom;
2241a575b807SPaul Mackerras 	char version[64];
2242a575b807SPaul Mackerras 
2243a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
2244a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
2245a575b807SPaul Mackerras 		return;
2246a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
2247a575b807SPaul Mackerras 		return;
2248a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
2249a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
2250450e7dd4SChristophe Leroy 	if (prom_strcmp(version, "Open Firmware, 1.0.5") == 0)
2251a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
2252450e7dd4SChristophe Leroy 	else if (prom_strncmp(version, "FirmWorks,3.", 12) == 0) {
2253a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
2254a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2255a23414beSPaul Mackerras 	} else
2256a575b807SPaul Mackerras 		return;
22575827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
22585827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
22595827d416SAnton Blanchard 		     sizeof(prom.mmumap));
2260493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
22615827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2262a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
2263a575b807SPaul Mackerras }
2264a575b807SPaul Mackerras #else
2265a575b807SPaul Mackerras #define prom_find_mmu()
2266a575b807SPaul Mackerras #endif
2267a575b807SPaul Mackerras 
22689b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
22699b6b563cSPaul Mackerras {
22705827d416SAnton Blanchard 	char *path = of_stdout_device;
22719b6b563cSPaul Mackerras 	char type[16];
2272493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
2273493adffcSBenjamin Herrenschmidt 	__be32 val;
22749b6b563cSPaul Mackerras 
22755827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
22769b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
22779b6b563cSPaul Mackerras 
2278493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
22799b6b563cSPaul Mackerras 
22809b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
22819b6b563cSPaul Mackerras 	memset(path, 0, 256);
22825827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
22835827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
22845827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2285450e7dd4SChristophe Leroy 		     path, prom_strlen(path) + 1);
22869b6b563cSPaul Mackerras 
228710348f59SBenjamin Herrenschmidt 	/* instance-to-package fails on PA-Semi */
228810348f59SBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
228910348f59SBenjamin Herrenschmidt 	if (stdout_node != PROM_ERROR) {
229010348f59SBenjamin Herrenschmidt 		val = cpu_to_be32(stdout_node);
229110348f59SBenjamin Herrenschmidt 
22929b6b563cSPaul Mackerras 		/* If it's a display, note it */
22939b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
2294493adffcSBenjamin Herrenschmidt 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
2295450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "display") == 0)
2296493adffcSBenjamin Herrenschmidt 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
22979b6b563cSPaul Mackerras 	}
229810348f59SBenjamin Herrenschmidt }
22999b6b563cSPaul Mackerras 
23009b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
23019b6b563cSPaul Mackerras {
23029b6b563cSPaul Mackerras 	char compat[256];
23039b6b563cSPaul Mackerras 	int len, i = 0;
230421fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
23059b6b563cSPaul Mackerras 	phandle rtas;
2306e8222502SBenjamin Herrenschmidt 	int x;
230721fe3301SBenjamin Herrenschmidt #endif
2308e8222502SBenjamin Herrenschmidt 
23096e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
23105827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
23119b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
23129b6b563cSPaul Mackerras 	if (len > 0) {
23139b6b563cSPaul Mackerras 		compat[len] = 0;
23149b6b563cSPaul Mackerras 		while (i < len) {
23159b6b563cSPaul Mackerras 			char *p = &compat[i];
2316450e7dd4SChristophe Leroy 			int sl = prom_strlen(p);
23179b6b563cSPaul Mackerras 			if (sl == 0)
23189b6b563cSPaul Mackerras 				break;
2319450e7dd4SChristophe Leroy 			if (prom_strstr(p, "Power Macintosh") ||
2320450e7dd4SChristophe Leroy 			    prom_strstr(p, "MacRISC"))
23219b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
2322133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
2323133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
2324133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
2325133dda1eSArnd Bergmann 			 * so we do it here.
2326133dda1eSArnd Bergmann 			 */
2327450e7dd4SChristophe Leroy 			if (prom_strstr(p, "IBM,CBEA") ||
2328450e7dd4SChristophe Leroy 			    prom_strstr(p, "IBM,CPBW-1.0"))
2329133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
2330133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
23319b6b563cSPaul Mackerras 			i += sl + 1;
23329b6b563cSPaul Mackerras 		}
23339b6b563cSPaul Mackerras 	}
23349b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
23356e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
2336e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
2337e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
2338e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
2339e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
2340e8222502SBenjamin Herrenschmidt 	 */
23415827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
2342e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
2343e8222502SBenjamin Herrenschmidt 	if (len <= 0)
2344e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2345450e7dd4SChristophe Leroy 	if (prom_strcmp(compat, "chrp"))
2346e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2347e8222502SBenjamin Herrenschmidt 
23489b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
23499b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2350e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
2351e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2352e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
23539b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
23544da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
23559b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
23569b6b563cSPaul Mackerras 	}
23579b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
23589b6b563cSPaul Mackerras #else
2359e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
23609b6b563cSPaul Mackerras #endif
23619b6b563cSPaul Mackerras }
23629b6b563cSPaul Mackerras 
23639b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
23649b6b563cSPaul Mackerras {
23659b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
23669b6b563cSPaul Mackerras }
23679b6b563cSPaul Mackerras 
23689b6b563cSPaul Mackerras /*
23699b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
23709b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
23719b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
23729b6b563cSPaul Mackerras  * we've taken over the MMU.
23739b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
23749b6b563cSPaul Mackerras  * and if so, open it now.
23759b6b563cSPaul Mackerras  */
23769b6b563cSPaul Mackerras static void __init prom_check_displays(void)
23779b6b563cSPaul Mackerras {
23789b6b563cSPaul Mackerras 	char type[16], *path;
23799b6b563cSPaul Mackerras 	phandle node;
23809b6b563cSPaul Mackerras 	ihandle ih;
23819b6b563cSPaul Mackerras 	int i;
23829b6b563cSPaul Mackerras 
2383d00e34b9SBenjamin Herrenschmidt 	static const unsigned char default_colors[] __initconst = {
23849b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
23859b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
23869b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
23879b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
23889b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
23899b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
23909b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
23919b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
23929b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
23939b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
23949b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
23959b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
23969b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
23979b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
23989b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
23999b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
24009b6b563cSPaul Mackerras 	};
24019b6b563cSPaul Mackerras 	const unsigned char *clut;
24029b6b563cSPaul Mackerras 
24034da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
24049b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
24059b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
24069b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
2407450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "display") != 0)
24089b6b563cSPaul Mackerras 			continue;
24099b6b563cSPaul Mackerras 
24109b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
24115827d416SAnton Blanchard 		path = prom_scratch;
2412d7fbe2a0SChristophe Leroy 		memset(path, 0, sizeof(prom_scratch));
24139b6b563cSPaul Mackerras 
24149b6b563cSPaul Mackerras 		/*
24159b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
24169b6b563cSPaul Mackerras 		 * arguments
24179b6b563cSPaul Mackerras 		 */
24189b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
2419d7fbe2a0SChristophe Leroy 			      sizeof(prom_scratch) - 10) == PROM_ERROR)
24209b6b563cSPaul Mackerras 			continue;
24219b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
24229b6b563cSPaul Mackerras 
24239b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
24249b6b563cSPaul Mackerras 		if (ih == 0) {
24259b6b563cSPaul Mackerras 			prom_printf("failed\n");
24269b6b563cSPaul Mackerras 			continue;
24279b6b563cSPaul Mackerras 		}
24289b6b563cSPaul Mackerras 
24299b6b563cSPaul Mackerras 		/* Success */
24309b6b563cSPaul Mackerras 		prom_printf("done\n");
2431a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
24329b6b563cSPaul Mackerras 
24339b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
24349b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
24355827d416SAnton Blanchard 		clut = default_colors;
24363f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
24379b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
24389b6b563cSPaul Mackerras 					   clut[2]) != 0)
24399b6b563cSPaul Mackerras 				break;
24409b6b563cSPaul Mackerras 
24419b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
24425827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
24435827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
24449b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
24459b6b563cSPaul Mackerras 					   clut[2]) != 0)
24469b6b563cSPaul Mackerras 				break;
24479b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
24487191b615SBenjamin Herrenschmidt 
24497191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
24507191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
24517191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
24527191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
24537191b615SBenjamin Herrenschmidt 
24547191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
24556c71cfccSMichael Ellerman 
24566c71cfccSMichael Ellerman 			if (prom_getprop(node, "width", &width, 4) == PROM_ERROR)
24576c71cfccSMichael Ellerman 				return;
24586c71cfccSMichael Ellerman 
24596c71cfccSMichael Ellerman 			if (prom_getprop(node, "height", &height, 4) == PROM_ERROR)
24606c71cfccSMichael Ellerman 				return;
24616c71cfccSMichael Ellerman 
24626c71cfccSMichael Ellerman 			if (prom_getprop(node, "linebytes", &pitch, 4) == PROM_ERROR)
24636c71cfccSMichael Ellerman 				return;
24646c71cfccSMichael Ellerman 
24656c71cfccSMichael Ellerman 			if (prom_getprop(node, "address", &addr, 4) == PROM_ERROR)
24666c71cfccSMichael Ellerman 				return;
24676c71cfccSMichael Ellerman 
24687191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
24697191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
24707191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
2471c21f5a9eSChristophe Leroy 			btext_prepare_BAT();
24727191b615SBenjamin Herrenschmidt 		}
24737191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
24749b6b563cSPaul Mackerras 	}
24759b6b563cSPaul Mackerras }
24769b6b563cSPaul Mackerras 
24779b6b563cSPaul Mackerras 
24789b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
24799b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
24809b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
24819b6b563cSPaul Mackerras {
24829b6b563cSPaul Mackerras 	void *ret;
24839b6b563cSPaul Mackerras 
2484d3f3d3bfSChristophe Leroy 	*mem_start = ALIGN(*mem_start, align);
24859b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
24869b6b563cSPaul Mackerras 		unsigned long room, chunk;
24879b6b563cSPaul Mackerras 
2488eae5f709SMathieu Malaterre 		prom_debug("Chunk exhausted, claiming more at %lx...\n",
24895827d416SAnton Blanchard 			   alloc_bottom);
24905827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
24919b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
24929b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
24939b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2494fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2495fbafd728SAnton Blanchard 				   "(no room)\n");
24969b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
24979b6b563cSPaul Mackerras 		if (chunk == 0)
2498fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2499fbafd728SAnton Blanchard 				   "(claim failed)\n");
2500966728ddSAnton Blanchard 		*mem_end = chunk + room;
25019b6b563cSPaul Mackerras 	}
25029b6b563cSPaul Mackerras 
25039b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
25049b6b563cSPaul Mackerras 	*mem_start += needed;
25059b6b563cSPaul Mackerras 
25069b6b563cSPaul Mackerras 	return ret;
25079b6b563cSPaul Mackerras }
25089b6b563cSPaul Mackerras 
2509493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2510493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2511493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2512493adffcSBenjamin Herrenschmidt 	} while(0)
25139b6b563cSPaul Mackerras 
25149b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
25159b6b563cSPaul Mackerras {
25169b6b563cSPaul Mackerras 	char *s, *os;
25179b6b563cSPaul Mackerras 
25185827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
25199b6b563cSPaul Mackerras 	s += 4;
25205827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
2521450e7dd4SChristophe Leroy 		if (prom_strcmp(s, str) == 0)
25229b6b563cSPaul Mackerras 			return s - os;
2523450e7dd4SChristophe Leroy 		s += prom_strlen(s) + 1;
25249b6b563cSPaul Mackerras 	}
25259b6b563cSPaul Mackerras 	return 0;
25269b6b563cSPaul Mackerras }
25279b6b563cSPaul Mackerras 
25289b6b563cSPaul Mackerras /*
25299b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
25309b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
25319b6b563cSPaul Mackerras  */
25329b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
25339b6b563cSPaul Mackerras 
25349b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
25359b6b563cSPaul Mackerras 					 unsigned long *mem_start,
25369b6b563cSPaul Mackerras 					 unsigned long *mem_end)
25379b6b563cSPaul Mackerras {
25389b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
25399b6b563cSPaul Mackerras 	unsigned long soff;
25409b6b563cSPaul Mackerras 	phandle child;
25419b6b563cSPaul Mackerras 
25425827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
25439b6b563cSPaul Mackerras 
25449b6b563cSPaul Mackerras 	/* get and store all property names */
25455827d416SAnton Blanchard 	prev_name = "";
25469b6b563cSPaul Mackerras 	for (;;) {
25479b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
25489b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
25499b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
25509b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
25519b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
25529b6b563cSPaul Mackerras 			break;
25539b6b563cSPaul Mackerras 		}
25549b6b563cSPaul Mackerras 
25559b6b563cSPaul Mackerras  		/* skip "name" */
2556450e7dd4SChristophe Leroy 		if (prom_strcmp(namep, "name") == 0) {
25579b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
25585827d416SAnton Blanchard  			prev_name = "name";
25599b6b563cSPaul Mackerras  			continue;
25609b6b563cSPaul Mackerras  		}
25619b6b563cSPaul Mackerras 		/* get/create string entry */
25629b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
25639b6b563cSPaul Mackerras 		if (soff != 0) {
25649b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
25659b6b563cSPaul Mackerras 			namep = sstart + soff;
25669b6b563cSPaul Mackerras 		} else {
25679b6b563cSPaul Mackerras 			/* Trim off some if we can */
2568450e7dd4SChristophe Leroy 			*mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
25695827d416SAnton Blanchard 			dt_string_end = *mem_start;
25709b6b563cSPaul Mackerras 		}
25719b6b563cSPaul Mackerras 		prev_name = namep;
25729b6b563cSPaul Mackerras 	}
25739b6b563cSPaul Mackerras 
25749b6b563cSPaul Mackerras 	/* do all our children */
25759b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
25769b6b563cSPaul Mackerras 	while (child != 0) {
25779b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
25789b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
25799b6b563cSPaul Mackerras 	}
25809b6b563cSPaul Mackerras }
25819b6b563cSPaul Mackerras 
25829b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
25839b6b563cSPaul Mackerras 					unsigned long *mem_end)
25849b6b563cSPaul Mackerras {
25859b6b563cSPaul Mackerras 	phandle child;
25869b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
25879b6b563cSPaul Mackerras 	unsigned long soff;
25889b6b563cSPaul Mackerras 	unsigned char *valp;
25898ca2d515SBenjamin Herrenschmidt 	static char pname[MAX_PROPERTY_NAME] __prombss;
25906e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
25919b6b563cSPaul Mackerras 
25929b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
25939b6b563cSPaul Mackerras 
25949b6b563cSPaul Mackerras 	/* get the node's full name */
25959b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2596c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2597c4988820SPaul Mackerras 	if (room > 255)
2598c4988820SPaul Mackerras 		room = 255;
2599c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
26009b6b563cSPaul Mackerras 	if (l >= 0) {
26019b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2602c4988820SPaul Mackerras 		if (l >= room) {
2603c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
26049b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
26059b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
26069b6b563cSPaul Mackerras 		}
26079b6b563cSPaul Mackerras 		namep[l] = '\0';
26089b6b563cSPaul Mackerras 
26099b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2610a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2611a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
26129b6b563cSPaul Mackerras 		 */
2613a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
26149b6b563cSPaul Mackerras 			if (*p == '/')
2615a575b807SPaul Mackerras 				lp = namep;
2616a575b807SPaul Mackerras 			else if (*p != 0)
2617a575b807SPaul Mackerras 				*lp++ = *p;
2618a575b807SPaul Mackerras 		}
2619a575b807SPaul Mackerras 		*lp = 0;
2620d3f3d3bfSChristophe Leroy 		*mem_start = ALIGN((unsigned long)lp + 1, 4);
26219b6b563cSPaul Mackerras 	}
26229b6b563cSPaul Mackerras 
26239b6b563cSPaul Mackerras 	/* get it again for debugging */
26245827d416SAnton Blanchard 	path = prom_scratch;
2625d7fbe2a0SChristophe Leroy 	memset(path, 0, sizeof(prom_scratch));
2626d7fbe2a0SChristophe Leroy 	call_prom("package-to-path", 3, 1, node, path, sizeof(prom_scratch) - 1);
26279b6b563cSPaul Mackerras 
26289b6b563cSPaul Mackerras 	/* get and store all properties */
26295827d416SAnton Blanchard 	prev_name = "";
26305827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
26319b6b563cSPaul Mackerras 	for (;;) {
26329b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
26335827d416SAnton Blanchard 			      pname) != 1)
26349b6b563cSPaul Mackerras 			break;
26359b6b563cSPaul Mackerras 
26369b6b563cSPaul Mackerras  		/* skip "name" */
2637450e7dd4SChristophe Leroy 		if (prom_strcmp(pname, "name") == 0) {
26385827d416SAnton Blanchard  			prev_name = "name";
26399b6b563cSPaul Mackerras  			continue;
26409b6b563cSPaul Mackerras  		}
26419b6b563cSPaul Mackerras 
26429b6b563cSPaul Mackerras 		/* find string offset */
26435827d416SAnton Blanchard 		soff = dt_find_string(pname);
26449b6b563cSPaul Mackerras 		if (soff == 0) {
26459b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
26465827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
26479b6b563cSPaul Mackerras 			break;
26489b6b563cSPaul Mackerras 		}
26499b6b563cSPaul Mackerras 		prev_name = sstart + soff;
26509b6b563cSPaul Mackerras 
26519b6b563cSPaul Mackerras 		/* get length */
26525827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
26539b6b563cSPaul Mackerras 
26549b6b563cSPaul Mackerras 		/* sanity checks */
26559b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
26569b6b563cSPaul Mackerras 			continue;
26579b6b563cSPaul Mackerras 
26589b6b563cSPaul Mackerras 		/* push property head */
26599b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
26609b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
26619b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
26629b6b563cSPaul Mackerras 
26639b6b563cSPaul Mackerras 		/* push property content */
26649b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
26655827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
2666d3f3d3bfSChristophe Leroy 		*mem_start = ALIGN(*mem_start, 4);
26676e35d5daSBenjamin Herrenschmidt 
2668450e7dd4SChristophe Leroy 		if (!prom_strcmp(pname, "phandle"))
26696e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
26709b6b563cSPaul Mackerras 	}
26719b6b563cSPaul Mackerras 
2672f1f208e5SBenjamin Herrenschmidt 	/* Add a "phandle" property if none already exist */
26736e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
2674f1f208e5SBenjamin Herrenschmidt 		soff = dt_find_string("phandle");
26759b6b563cSPaul Mackerras 		if (soff == 0)
2676f1f208e5SBenjamin Herrenschmidt 			prom_printf("WARNING: Can't find string index for <phandle> node %s\n", path);
26779b6b563cSPaul Mackerras 		else {
26789b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
26799b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
26809b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
26819b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
2682493adffcSBenjamin Herrenschmidt 			*(__be32 *)valp = cpu_to_be32(node);
26839b6b563cSPaul Mackerras 		}
26846e35d5daSBenjamin Herrenschmidt 	}
26859b6b563cSPaul Mackerras 
26869b6b563cSPaul Mackerras 	/* do all our children */
26879b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
26889b6b563cSPaul Mackerras 	while (child != 0) {
26899b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
26909b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
26919b6b563cSPaul Mackerras 	}
26929b6b563cSPaul Mackerras 
26939b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
26949b6b563cSPaul Mackerras }
26959b6b563cSPaul Mackerras 
26969b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
26979b6b563cSPaul Mackerras {
26989b6b563cSPaul Mackerras 	phandle root;
26999b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
27009b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
27019b6b563cSPaul Mackerras 	char *namep;
27029b6b563cSPaul Mackerras 	u64 *rsvmap;
27039b6b563cSPaul Mackerras 
27049b6b563cSPaul Mackerras 	/*
27059b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2706fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
27079b6b563cSPaul Mackerras 	 */
27085827d416SAnton Blanchard 	room = alloc_top - alloc_bottom - 0x4000;
27099b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
27109b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
2711eae5f709SMathieu Malaterre 	prom_debug("starting device tree allocs at %lx\n", alloc_bottom);
27129b6b563cSPaul Mackerras 
27139b6b563cSPaul Mackerras 	/* Now try to claim that */
27149b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
27159b6b563cSPaul Mackerras 	if (mem_start == 0)
27169b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2717966728ddSAnton Blanchard 	mem_end = mem_start + room;
27189b6b563cSPaul Mackerras 
27199b6b563cSPaul Mackerras 	/* Get root of tree */
27209b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
27219b6b563cSPaul Mackerras 	if (root == (phandle)0)
27229b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
27239b6b563cSPaul Mackerras 
27249b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
2725d3f3d3bfSChristophe Leroy 	mem_start = ALIGN(mem_start, 4);
27269b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
27279b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
27285827d416SAnton Blanchard 	dt_header_start = (unsigned long)hdr;
27299b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
27309b6b563cSPaul Mackerras 
27319b6b563cSPaul Mackerras 	/* Start of strings */
27329b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
27335827d416SAnton Blanchard 	dt_string_start = mem_start;
27349b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
27359b6b563cSPaul Mackerras 
2736f1f208e5SBenjamin Herrenschmidt 	/* Add "phandle" in there, we'll need it */
27379b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
2738f47d5a4fSMichael Ellerman 	prom_strscpy_pad(namep, "phandle", sizeof("phandle"));
2739450e7dd4SChristophe Leroy 	mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
27409b6b563cSPaul Mackerras 
27419b6b563cSPaul Mackerras 	/* Build string array */
27429b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
27439b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
27445827d416SAnton Blanchard 	dt_string_end = mem_start;
27459b6b563cSPaul Mackerras 
27469b6b563cSPaul Mackerras 	/* Build structure */
27479b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
27485827d416SAnton Blanchard 	dt_struct_start = mem_start;
27499b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
27509b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
27519b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
27525827d416SAnton Blanchard 	dt_struct_end = PAGE_ALIGN(mem_start);
27539b6b563cSPaul Mackerras 
27549b6b563cSPaul Mackerras 	/* Finish header */
2755493adffcSBenjamin Herrenschmidt 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2756493adffcSBenjamin Herrenschmidt 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2757493adffcSBenjamin Herrenschmidt 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2758493adffcSBenjamin Herrenschmidt 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2759493adffcSBenjamin Herrenschmidt 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2760493adffcSBenjamin Herrenschmidt 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2761493adffcSBenjamin Herrenschmidt 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2762493adffcSBenjamin Herrenschmidt 	hdr->version = cpu_to_be32(OF_DT_VERSION);
27639b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
2764493adffcSBenjamin Herrenschmidt 	hdr->last_comp_version = cpu_to_be32(0x10);
27659b6b563cSPaul Mackerras 
27664d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
27675827d416SAnton Blanchard 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
27689b6b563cSPaul Mackerras 
27699b6b563cSPaul Mackerras #ifdef DEBUG_PROM
27709b6b563cSPaul Mackerras 	{
27719b6b563cSPaul Mackerras 		int i;
27729b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
27735827d416SAnton Blanchard 		for (i = 0; i < mem_reserve_cnt; i++)
2774eae5f709SMathieu Malaterre 			prom_printf("  %llx - %llx\n",
2775493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].base),
2776493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].size));
27779b6b563cSPaul Mackerras 	}
27789b6b563cSPaul Mackerras #endif
27794d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
27804d1f3f25SJimi Xenidis 	 * since it's too late.
27814d1f3f25SJimi Xenidis 	 */
27825827d416SAnton Blanchard 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
27839b6b563cSPaul Mackerras 
2784eae5f709SMathieu Malaterre 	prom_printf("Device tree strings 0x%lx -> 0x%lx\n",
27855827d416SAnton Blanchard 		    dt_string_start, dt_string_end);
2786eae5f709SMathieu Malaterre 	prom_printf("Device tree struct  0x%lx -> 0x%lx\n",
27875827d416SAnton Blanchard 		    dt_struct_start, dt_struct_end);
27889b6b563cSPaul Mackerras }
27899b6b563cSPaul Mackerras 
279054f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
279154f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
279254f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
279354f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
27949b6b563cSPaul Mackerras {
279554f4ee18SHollis Blanchard 	phandle isa;
2796980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
279754f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2798980a6513SBenjamin Herrenschmidt 	char *name;
279954f4ee18SHollis Blanchard 
2800980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2801980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2802980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2803980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2804980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2805980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2806980a6513SBenjamin Herrenschmidt 	}
280754f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
280854f4ee18SHollis Blanchard 		return;
280954f4ee18SHollis Blanchard 
2810980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2811980a6513SBenjamin Herrenschmidt 		return;
281254f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
281354f4ee18SHollis Blanchard 		== PROM_ERROR)
281454f4ee18SHollis Blanchard 		return;
281554f4ee18SHollis Blanchard 
281654f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
281754f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
281854f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
281954f4ee18SHollis Blanchard 		return;
282054f4ee18SHollis Blanchard 
2821980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
282254f4ee18SHollis Blanchard 
282354f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
282454f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2825980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
282654f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
282754f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
282854f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2829980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
283054f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
283154f4ee18SHollis Blanchard }
28328f101a05SHarry Ciao 
28338f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
28348f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
28358f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
28368f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
28378f101a05SHarry Ciao {
28388f101a05SHarry Ciao 	phandle mc;
28398f101a05SHarry Ciao 	u32 mc_reg[4];
28408f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
28418f101a05SHarry Ciao 	u32 ac, sc;
28428f101a05SHarry Ciao 
28438f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
28448f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
28458f101a05SHarry Ciao 		return;
28468f101a05SHarry Ciao 
28478f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
28488f101a05SHarry Ciao 		return;
28498f101a05SHarry Ciao 
28505827d416SAnton Blanchard 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
28515827d416SAnton Blanchard 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
28528f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
28538f101a05SHarry Ciao 		return;
28548f101a05SHarry Ciao 
28558f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
28568f101a05SHarry Ciao 		return;
28578f101a05SHarry Ciao 
28588f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
28598f101a05SHarry Ciao 		return;
28608f101a05SHarry Ciao 
28618f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
28628f101a05SHarry Ciao 
28638f101a05SHarry Ciao 	mc_reg[0] = 0x0;
28648f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
28658f101a05SHarry Ciao 	mc_reg[2] = 0x0;
28668f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
28678f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
28688f101a05SHarry Ciao }
286954f4ee18SHollis Blanchard #else
287054f4ee18SHollis Blanchard #define fixup_device_tree_maple()
28718f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
287254f4ee18SHollis Blanchard #endif
287354f4ee18SHollis Blanchard 
2874e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2875e4805922SOlaf Hering /*
2876e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2877e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2878556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2879e4805922SOlaf Hering  */
2880e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2881e8c0acf9SBenjamin Herrenschmidt {
2882e4805922SOlaf Hering 	phandle ph;
2883e4805922SOlaf Hering 	u32 prop[6];
288426c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2885e8c0acf9SBenjamin Herrenschmidt 	char *name;
2886e8c0acf9SBenjamin Herrenschmidt 	int rc;
2887e8c0acf9SBenjamin Herrenschmidt 
2888e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2889e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2890e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
289126c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2892e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
289326c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
289426c5032eSBenjamin Herrenschmidt 	}
2895e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2896e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2897e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2898e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2899e8c0acf9SBenjamin Herrenschmidt 
2900e4805922SOlaf Hering 			prop[0] = 0x1;
2901e4805922SOlaf Hering 			prop[1] = 0x0;
2902e4805922SOlaf Hering 			prop[2] = rloc;
2903e4805922SOlaf Hering 			prop[3] = 0x0;
2904e4805922SOlaf Hering 			prop[4] = 0x0;
2905e4805922SOlaf Hering 			prop[5] = 0x00010000;
2906e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2907e4805922SOlaf Hering 		}
2908e4805922SOlaf Hering 	}
2909e4805922SOlaf Hering 
2910e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2911e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2912e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2913e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2914e4805922SOlaf Hering 		prop[0] = 14;
2915e4805922SOlaf Hering 		prop[1] = 0x0;
2916556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2917556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2918556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2919556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2920556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2921556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2922556ecf9bSOlaf Hering 		}
2923e4805922SOlaf Hering 	}
2924e8c0acf9SBenjamin Herrenschmidt }
2925e8c0acf9SBenjamin Herrenschmidt #else
2926e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2927e8c0acf9SBenjamin Herrenschmidt #endif
2928e8c0acf9SBenjamin Herrenschmidt 
29299b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
293054f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
293154f4ee18SHollis Blanchard {
29329b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
29339b6b563cSPaul Mackerras 	u32 u3_rev;
29349b6b563cSPaul Mackerras 	u32 interrupts[2];
29359b6b563cSPaul Mackerras 	u32 parent;
29369b6b563cSPaul Mackerras 
29379b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
29389b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
29399b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
29409b6b563cSPaul Mackerras 		return;
29419b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
29429b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
29439b6b563cSPaul Mackerras 		return;
29449b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
29459b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
29469b6b563cSPaul Mackerras 		return;
29479b6b563cSPaul Mackerras 
29489b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
29499b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
29509b6b563cSPaul Mackerras 	    == PROM_ERROR)
29519b6b563cSPaul Mackerras 		return;
29527d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
29539b6b563cSPaul Mackerras 		return;
29549b6b563cSPaul Mackerras 	/* does it need fixup ? */
29559b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
29569b6b563cSPaul Mackerras 		return;
29579b6b563cSPaul Mackerras 
29589b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
29599b6b563cSPaul Mackerras 
29609b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
29619b6b563cSPaul Mackerras 	interrupts[0] = 0;
29629b6b563cSPaul Mackerras 	interrupts[1] = 1;
2963a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2964a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
29659b6b563cSPaul Mackerras 	parent = (u32)mpic;
2966a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2967a23414beSPaul Mackerras 		     &parent, sizeof(parent));
29689b6b563cSPaul Mackerras }
296954f4ee18SHollis Blanchard #else
297054f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
297154f4ee18SHollis Blanchard #endif
29729b6b563cSPaul Mackerras 
297388fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
297494d2dde7SGrant Likely /*
297594d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
297694d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
297794d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
297894d2dde7SGrant Likely  * ethernet node.
297994d2dde7SGrant Likely  */
298094d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
298188fd2a9dSSylvain Munaut {
298288fd2a9dSSylvain Munaut 	u32 node;
298388fd2a9dSSylvain Munaut 	char prop[64];
298494d2dde7SGrant Likely 	int rv;
298588fd2a9dSSylvain Munaut 
298694d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
298794d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
298888fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
298988fd2a9dSSylvain Munaut 		return;
299088fd2a9dSSylvain Munaut 
299194d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
299294d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
299394d2dde7SGrant Likely 	if (!rv)
299488fd2a9dSSylvain Munaut 		return;
299588fd2a9dSSylvain Munaut 
299694d2dde7SGrant Likely 	/*
299794d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
299894d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
299994d2dde7SGrant Likely 	 */
300088fd2a9dSSylvain Munaut 
300194d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
30026f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
30036f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
30046f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
30056f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
30066f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
30076f4347c9SOlaf Hering 			" new-device"
30086f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
30096f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
301094d2dde7SGrant Likely 				" s\" mdio\" device-name"
301194d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
30126f4347c9SOlaf Hering 				" s\" compatible\" property"
30136f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
30146f4347c9SOlaf Hering 				" 0x2 encode-int"
30156f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
30166f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
30176f4347c9SOlaf Hering 				" s\" interrupts\" property"
30186f4347c9SOlaf Hering 			" finish-device");
30194f46d57cSJiapeng Chong 	}
30206f4347c9SOlaf Hering 
302194d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
302294d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
302394d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
302494d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
30256f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
30266f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
30276f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
30286f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
30296f4347c9SOlaf Hering 			" new-device"
30306f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
30316f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
30326f4347c9SOlaf Hering 				" my-self"
30336f4347c9SOlaf Hering 				" ihandle>phandle"
30346f4347c9SOlaf Hering 			" finish-device"
30356f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
30366f4347c9SOlaf Hering 				" encode-int"
30376f4347c9SOlaf Hering 				" s\" phy-handle\" property"
30386f4347c9SOlaf Hering 			" device-end");
30396f4347c9SOlaf Hering 	}
304094d2dde7SGrant Likely }
30416f4347c9SOlaf Hering 
304294d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
304394d2dde7SGrant Likely {
304494d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
304594d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
304694d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
304794d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
304894d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
304994d2dde7SGrant Likely 	u32 node;
305094d2dde7SGrant Likely 	char prop[64];
305194d2dde7SGrant Likely 	int rv, len;
305294d2dde7SGrant Likely 
305394d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
305494d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
305594d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
305694d2dde7SGrant Likely 		return;
305794d2dde7SGrant Likely 
305894d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
305994d2dde7SGrant Likely 	if (rv == PROM_ERROR)
306094d2dde7SGrant Likely 		return;
3061450e7dd4SChristophe Leroy 	if (prom_strcmp(prop, "EFIKA5K2"))
306294d2dde7SGrant Likely 		return;
306394d2dde7SGrant Likely 
306494d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
306594d2dde7SGrant Likely 
306694d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
306794d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
306894d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
3069450e7dd4SChristophe Leroy 	if (rv != PROM_ERROR && (prom_strcmp(prop, "chrp") == 0))
307094d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
307194d2dde7SGrant Likely 
30727f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
30737f4392cdSDavid Woodhouse 	   fix that too */
30747f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
3075450e7dd4SChristophe Leroy 	if (rv != PROM_ERROR && (prom_strstr(prop, "CHRP")))
30767f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
30777f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
30787f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
30797f4392cdSDavid Woodhouse 
308094d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
308194d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
308294d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
308394d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
308494d2dde7SGrant Likely 		if (len == 12) {
308594d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
308694d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
308794d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
308894d2dde7SGrant Likely 		}
308994d2dde7SGrant Likely 	}
309094d2dde7SGrant Likely 
309194d2dde7SGrant Likely 	/* Fixup sound interrupts property */
309294d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
309394d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
309494d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
309594d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
309694d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
309794d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
309894d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
309994d2dde7SGrant Likely 		}
310094d2dde7SGrant Likely 	}
310194d2dde7SGrant Likely 
310294d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
310394d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
310488fd2a9dSSylvain Munaut }
310588fd2a9dSSylvain Munaut #else
310688fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
310788fd2a9dSSylvain Munaut #endif
310888fd2a9dSSylvain Munaut 
310950246787SDarren Stevens #ifdef CONFIG_PPC_PASEMI_NEMO
311050246787SDarren Stevens /*
311150246787SDarren Stevens  * CFE supplied on Nemo is broken in several ways, biggest
311250246787SDarren Stevens  * problem is that it reassigns ISA interrupts to unused mpic ints.
311350246787SDarren Stevens  * Add an interrupt-controller property for the io-bridge to use
311450246787SDarren Stevens  * and correct the ints so we can attach them to an irq_domain
311550246787SDarren Stevens  */
311650246787SDarren Stevens static void __init fixup_device_tree_pasemi(void)
311750246787SDarren Stevens {
311850246787SDarren Stevens 	u32 interrupts[2], parent, rval, val = 0;
311950246787SDarren Stevens 	char *name, *pci_name;
312050246787SDarren Stevens 	phandle iob, node;
312150246787SDarren Stevens 
312250246787SDarren Stevens 	/* Find the root pci node */
312350246787SDarren Stevens 	name = "/pxp@0,e0000000";
312450246787SDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
312550246787SDarren Stevens 	if (!PHANDLE_VALID(iob))
312650246787SDarren Stevens 		return;
312750246787SDarren Stevens 
312850246787SDarren Stevens 	/* check if interrupt-controller node set yet */
312950246787SDarren Stevens 	if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
313050246787SDarren Stevens 		return;
313150246787SDarren Stevens 
313250246787SDarren Stevens 	prom_printf("adding interrupt-controller property for SB600...\n");
313350246787SDarren Stevens 
313450246787SDarren Stevens 	prom_setprop(iob, name, "interrupt-controller", &val, 0);
313550246787SDarren Stevens 
313650246787SDarren Stevens 	pci_name = "/pxp@0,e0000000/pci@11";
313750246787SDarren Stevens 	node = call_prom("finddevice", 1, 1, ADDR(pci_name));
313850246787SDarren Stevens 	parent = ADDR(iob);
313950246787SDarren Stevens 
314050246787SDarren Stevens 	for( ; prom_next_node(&node); ) {
314150246787SDarren Stevens 		/* scan each node for one with an interrupt */
314250246787SDarren Stevens 		if (!PHANDLE_VALID(node))
314350246787SDarren Stevens 			continue;
314450246787SDarren Stevens 
314550246787SDarren Stevens 		rval = prom_getproplen(node, "interrupts");
314650246787SDarren Stevens 		if (rval == 0 || rval == PROM_ERROR)
314750246787SDarren Stevens 			continue;
314850246787SDarren Stevens 
314950246787SDarren Stevens 		prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
315050246787SDarren Stevens 		if ((interrupts[0] < 212) || (interrupts[0] > 222))
315150246787SDarren Stevens 			continue;
315250246787SDarren Stevens 
315350246787SDarren Stevens 		/* found a node, update both interrupts and interrupt-parent */
315450246787SDarren Stevens 		if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
315550246787SDarren Stevens 			interrupts[0] -= 203;
315650246787SDarren Stevens 		if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
315750246787SDarren Stevens 			interrupts[0] -= 213;
315850246787SDarren Stevens 		if (interrupts[0] == 221)
315950246787SDarren Stevens 			interrupts[0] = 14;
316050246787SDarren Stevens 		if (interrupts[0] == 222)
316150246787SDarren Stevens 			interrupts[0] = 8;
316250246787SDarren Stevens 
316350246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupts", interrupts,
316450246787SDarren Stevens 					sizeof(interrupts));
316550246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupt-parent", &parent,
316650246787SDarren Stevens 					sizeof(parent));
316750246787SDarren Stevens 	}
3168687e16bcSDarren Stevens 
3169687e16bcSDarren Stevens 	/*
3170687e16bcSDarren Stevens 	 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
3171687e16bcSDarren Stevens 	 * so that generic isa-bridge code can add the SB600 and its on-board
3172687e16bcSDarren Stevens 	 * peripherals.
3173687e16bcSDarren Stevens 	 */
3174687e16bcSDarren Stevens 	name = "/pxp@0,e0000000/io-bridge@0";
3175687e16bcSDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
3176687e16bcSDarren Stevens 	if (!PHANDLE_VALID(iob))
3177687e16bcSDarren Stevens 		return;
3178687e16bcSDarren Stevens 
3179687e16bcSDarren Stevens 	/* device_type is already set, just change it. */
3180687e16bcSDarren Stevens 
3181687e16bcSDarren Stevens 	prom_printf("Changing device_type of SB600 node...\n");
3182687e16bcSDarren Stevens 
3183687e16bcSDarren Stevens 	prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
318450246787SDarren Stevens }
318550246787SDarren Stevens #else	/* !CONFIG_PPC_PASEMI_NEMO */
318650246787SDarren Stevens static inline void fixup_device_tree_pasemi(void) { }
318750246787SDarren Stevens #endif
318850246787SDarren Stevens 
318954f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
319054f4ee18SHollis Blanchard {
319154f4ee18SHollis Blanchard 	fixup_device_tree_maple();
31928f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
3193e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
319454f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
319588fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
319650246787SDarren Stevens 	fixup_device_tree_pasemi();
319754f4ee18SHollis Blanchard }
31989b6b563cSPaul Mackerras 
31999b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
32009b6b563cSPaul Mackerras {
3201493adffcSBenjamin Herrenschmidt 	__be32 rval;
32029b6b563cSPaul Mackerras 	ihandle prom_cpu;
32039b6b563cSPaul Mackerras 	phandle cpu_pkg;
32049b6b563cSPaul Mackerras 
3205493adffcSBenjamin Herrenschmidt 	rval = 0;
3206493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
3207a575b807SPaul Mackerras 		return;
3208493adffcSBenjamin Herrenschmidt 	prom_cpu = be32_to_cpu(rval);
32099b6b563cSPaul Mackerras 
32109b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
32119b6b563cSPaul Mackerras 
3212af2b7fa1SDarren Stevens 	if (!PHANDLE_VALID(cpu_pkg))
3213af2b7fa1SDarren Stevens 		return;
3214af2b7fa1SDarren Stevens 
3215493adffcSBenjamin Herrenschmidt 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
3216493adffcSBenjamin Herrenschmidt 	prom.cpu = be32_to_cpu(rval);
32179b6b563cSPaul Mackerras 
3218eae5f709SMathieu Malaterre 	prom_debug("Booting CPU hw index = %d\n", prom.cpu);
32199b6b563cSPaul Mackerras }
32209b6b563cSPaul Mackerras 
32219b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
32229b6b563cSPaul Mackerras {
32239b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
32249b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
3225493adffcSBenjamin Herrenschmidt 		__be64 val;
32269b6b563cSPaul Mackerras 
32275827d416SAnton Blanchard 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
32285827d416SAnton Blanchard 		prom_initrd_end = prom_initrd_start + r4;
32299b6b563cSPaul Mackerras 
3230493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_start);
32315827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
3232a23414beSPaul Mackerras 			     &val, sizeof(val));
3233493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_end);
32345827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
3235a23414beSPaul Mackerras 			     &val, sizeof(val));
32369b6b563cSPaul Mackerras 
32375827d416SAnton Blanchard 		reserve_mem(prom_initrd_start,
32385827d416SAnton Blanchard 			    prom_initrd_end - prom_initrd_start);
32399b6b563cSPaul Mackerras 
3240eae5f709SMathieu Malaterre 		prom_debug("initrd_start=0x%lx\n", prom_initrd_start);
3241eae5f709SMathieu Malaterre 		prom_debug("initrd_end=0x%lx\n", prom_initrd_end);
32429b6b563cSPaul Mackerras 	}
32439b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
32449b6b563cSPaul Mackerras }
32459b6b563cSPaul Mackerras 
32465ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64
32475ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE
32485ac47f7aSAnton Blanchard static void reloc_toc(void)
32495ac47f7aSAnton Blanchard {
32505ac47f7aSAnton Blanchard }
32515ac47f7aSAnton Blanchard 
32525ac47f7aSAnton Blanchard static void unreloc_toc(void)
32535ac47f7aSAnton Blanchard {
32545ac47f7aSAnton Blanchard }
32555ac47f7aSAnton Blanchard #else
32561674400aSAnton Blanchard static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
32575ac47f7aSAnton Blanchard {
32585ac47f7aSAnton Blanchard 	unsigned long i;
32591674400aSAnton Blanchard 	unsigned long *toc_entry;
32601674400aSAnton Blanchard 
32611674400aSAnton Blanchard 	/* Get the start of the TOC by using r2 directly. */
32621674400aSAnton Blanchard 	asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
32635ac47f7aSAnton Blanchard 
32645ac47f7aSAnton Blanchard 	for (i = 0; i < nr_entries; i++) {
32655ac47f7aSAnton Blanchard 		*toc_entry = *toc_entry + offset;
32665ac47f7aSAnton Blanchard 		toc_entry++;
32675ac47f7aSAnton Blanchard 	}
32685ac47f7aSAnton Blanchard }
32695ac47f7aSAnton Blanchard 
32705ac47f7aSAnton Blanchard static void reloc_toc(void)
32715ac47f7aSAnton Blanchard {
32725ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
32735ac47f7aSAnton Blanchard 	unsigned long nr_entries =
32745ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
32755ac47f7aSAnton Blanchard 
32761674400aSAnton Blanchard 	__reloc_toc(offset, nr_entries);
32775ac47f7aSAnton Blanchard 
32785ac47f7aSAnton Blanchard 	mb();
32795ac47f7aSAnton Blanchard }
32805ac47f7aSAnton Blanchard 
32815ac47f7aSAnton Blanchard static void unreloc_toc(void)
32825ac47f7aSAnton Blanchard {
32835ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
32845ac47f7aSAnton Blanchard 	unsigned long nr_entries =
32855ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
32865ac47f7aSAnton Blanchard 
32875ac47f7aSAnton Blanchard 	mb();
32885ac47f7aSAnton Blanchard 
32891674400aSAnton Blanchard 	__reloc_toc(-offset, nr_entries);
32905ac47f7aSAnton Blanchard }
32915ac47f7aSAnton Blanchard #endif
32925ac47f7aSAnton Blanchard #endif
329327f44888SBenjamin Herrenschmidt 
32946a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
32956a9c930bSRam Pai /*
32966a9c930bSRam Pai  * Perform the Enter Secure Mode ultracall.
32976a9c930bSRam Pai  */
32986a9c930bSRam Pai static int enter_secure_mode(unsigned long kbase, unsigned long fdt)
32996a9c930bSRam Pai {
33006a9c930bSRam Pai 	register unsigned long r3 asm("r3") = UV_ESM;
33016a9c930bSRam Pai 	register unsigned long r4 asm("r4") = kbase;
33026a9c930bSRam Pai 	register unsigned long r5 asm("r5") = fdt;
33036a9c930bSRam Pai 
33046a9c930bSRam Pai 	asm volatile("sc 2" : "+r"(r3) : "r"(r4), "r"(r5));
33056a9c930bSRam Pai 
33066a9c930bSRam Pai 	return r3;
33076a9c930bSRam Pai }
33086a9c930bSRam Pai 
33096a9c930bSRam Pai /*
33106a9c930bSRam Pai  * Call the Ultravisor to transfer us to secure memory if we have an ESM blob.
33116a9c930bSRam Pai  */
3312aff77951SVladis Dronov static void __init setup_secure_guest(unsigned long kbase, unsigned long fdt)
33136a9c930bSRam Pai {
33146a9c930bSRam Pai 	int ret;
33156a9c930bSRam Pai 
33166a9c930bSRam Pai 	if (!prom_svm_enable)
33176a9c930bSRam Pai 		return;
33186a9c930bSRam Pai 
33196a9c930bSRam Pai 	/* Switch to secure mode. */
33206a9c930bSRam Pai 	prom_printf("Switching to secure mode.\n");
33216a9c930bSRam Pai 
332205d9a952SThiago Jung Bauermann 	/*
332305d9a952SThiago Jung Bauermann 	 * The ultravisor will do an integrity check of the kernel image but we
332405d9a952SThiago Jung Bauermann 	 * relocated it so the check will fail. Restore the original image by
332505d9a952SThiago Jung Bauermann 	 * relocating it back to the kernel virtual base address.
332605d9a952SThiago Jung Bauermann 	 */
332705d9a952SThiago Jung Bauermann 	if (IS_ENABLED(CONFIG_RELOCATABLE))
332805d9a952SThiago Jung Bauermann 		relocate(KERNELBASE);
332905d9a952SThiago Jung Bauermann 
33306a9c930bSRam Pai 	ret = enter_secure_mode(kbase, fdt);
333105d9a952SThiago Jung Bauermann 
333205d9a952SThiago Jung Bauermann 	/* Relocate the kernel again. */
333305d9a952SThiago Jung Bauermann 	if (IS_ENABLED(CONFIG_RELOCATABLE))
333405d9a952SThiago Jung Bauermann 		relocate(kbase);
333505d9a952SThiago Jung Bauermann 
33366a9c930bSRam Pai 	if (ret != U_SUCCESS) {
33376a9c930bSRam Pai 		prom_printf("Returned %d from switching to secure mode.\n", ret);
33386a9c930bSRam Pai 		prom_rtas_os_term("Switch to secure mode failed.\n");
33396a9c930bSRam Pai 	}
33406a9c930bSRam Pai }
33416a9c930bSRam Pai #else
3342aff77951SVladis Dronov static void __init setup_secure_guest(unsigned long kbase, unsigned long fdt)
33436a9c930bSRam Pai {
33446a9c930bSRam Pai }
33456a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
33466a9c930bSRam Pai 
33479b6b563cSPaul Mackerras /*
33489b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
33499b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
33509b6b563cSPaul Mackerras  */
33519b6b563cSPaul Mackerras 
33529b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
33539b6b563cSPaul Mackerras 			       unsigned long pp,
3354549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
3355549e8152SPaul Mackerras 			       unsigned long kbase)
33569b6b563cSPaul Mackerras {
33579b6b563cSPaul Mackerras 	unsigned long hdr;
33589b6b563cSPaul Mackerras 
33599b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
3360549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
33619b6b563cSPaul Mackerras 	reloc_got2(offset);
33625ac47f7aSAnton Blanchard #else
33635ac47f7aSAnton Blanchard 	reloc_toc();
33649b6b563cSPaul Mackerras #endif
33659b6b563cSPaul Mackerras 
33669b6b563cSPaul Mackerras 	/*
33679b6b563cSPaul Mackerras 	 * First zero the BSS
33689b6b563cSPaul Mackerras 	 */
33695827d416SAnton Blanchard 	memset(&__bss_start, 0, __bss_stop - __bss_start);
33709b6b563cSPaul Mackerras 
33719b6b563cSPaul Mackerras 	/*
33729b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
33739b6b563cSPaul Mackerras 	 * like /chosen
33749b6b563cSPaul Mackerras 	 */
33759b6b563cSPaul Mackerras 	prom_init_client_services(pp);
33769b6b563cSPaul Mackerras 
33779b6b563cSPaul Mackerras 	/*
3378a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
3379a23414beSPaul Mackerras 	 * and other workarounds
3380a23414beSPaul Mackerras 	 */
3381a23414beSPaul Mackerras 	prom_find_mmu();
3382a23414beSPaul Mackerras 
3383a23414beSPaul Mackerras 	/*
33849b6b563cSPaul Mackerras 	 * Init prom stdout device
33859b6b563cSPaul Mackerras 	 */
33869b6b563cSPaul Mackerras 	prom_init_stdout();
33879b6b563cSPaul Mackerras 
33885827d416SAnton Blanchard 	prom_printf("Preparing to boot %s", linux_banner);
3389e7943fbbSMichael Ellerman 
33909b6b563cSPaul Mackerras 	/*
33919b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
33929b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
33939b6b563cSPaul Mackerras 	 */
33945827d416SAnton Blanchard 	of_platform = prom_find_machine_type();
33955827d416SAnton Blanchard 	prom_printf("Detected machine type: %x\n", of_platform);
33969b6b563cSPaul Mackerras 
33970f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
3398add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
3399add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
3400add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
3401549e8152SPaul Mackerras #endif
3402add60ef3SOlaf Hering 
3403add60ef3SOlaf Hering 	/*
3404add60ef3SOlaf Hering 	 * Check for an initrd
3405add60ef3SOlaf Hering 	 */
3406add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
3407add60ef3SOlaf Hering 
340812cc9fd6SSuraj Jitindar Singh 	/*
340912cc9fd6SSuraj Jitindar Singh 	 * Do early parsing of command line
341012cc9fd6SSuraj Jitindar Singh 	 */
341112cc9fd6SSuraj Jitindar Singh 	early_cmdline_parse();
341212cc9fd6SSuraj Jitindar Singh 
341311fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
34149b6b563cSPaul Mackerras 	/*
34159b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
34169b6b563cSPaul Mackerras 	 */
34175827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES ||
34185827d416SAnton Blanchard 	    of_platform == PLATFORM_PSERIES_LPAR)
34199b6b563cSPaul Mackerras 		prom_send_capabilities();
34209b6b563cSPaul Mackerras #endif
34219b6b563cSPaul Mackerras 
34229b6b563cSPaul Mackerras 	/*
3423f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
34249b6b563cSPaul Mackerras 	 */
34255827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC)
3426549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
34279b6b563cSPaul Mackerras 
34289b6b563cSPaul Mackerras 	/*
34299b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
34309b6b563cSPaul Mackerras 	 */
34319b6b563cSPaul Mackerras 	prom_init_mem();
34329b6b563cSPaul Mackerras 
34339b6b563cSPaul Mackerras 	/*
34349b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
34359b6b563cSPaul Mackerras 	 */
34369b6b563cSPaul Mackerras 	prom_find_boot_cpu();
34379b6b563cSPaul Mackerras 
34389b6b563cSPaul Mackerras 	/*
34399b6b563cSPaul Mackerras 	 * Initialize display devices
34409b6b563cSPaul Mackerras 	 */
34419b6b563cSPaul Mackerras 	prom_check_displays();
34429b6b563cSPaul Mackerras 
3443493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
34449b6b563cSPaul Mackerras 	/*
34459b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
34469b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
34479b6b563cSPaul Mackerras 	 * available for us here...
34489b6b563cSPaul Mackerras 	 */
34495827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES)
34509b6b563cSPaul Mackerras 		prom_initialize_tce_table();
34519b6b563cSPaul Mackerras #endif
34529b6b563cSPaul Mackerras 
34539b6b563cSPaul Mackerras 	/*
345427f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
345527f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
34569b6b563cSPaul Mackerras 	 */
345711fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
34589b6b563cSPaul Mackerras 		prom_instantiate_rtas();
345927f44888SBenjamin Herrenschmidt 
34604a727429SAshley Lai #ifdef CONFIG_PPC64
34614a727429SAshley Lai 	/* instantiate sml */
34624a727429SAshley Lai 	prom_instantiate_sml();
34634a727429SAshley Lai #endif
34644a727429SAshley Lai 
346527f44888SBenjamin Herrenschmidt 	/*
346627f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
346727f44888SBenjamin Herrenschmidt 	 *
346827f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
3469dbe78b40SBenjamin Herrenschmidt 	 *
3470dbe78b40SBenjamin Herrenschmidt 	 * (This must be done after instanciating RTAS)
347127f44888SBenjamin Herrenschmidt 	 */
347211fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
347327f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
34749b6b563cSPaul Mackerras 
34759b6b563cSPaul Mackerras 	/*
34769b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
34779b6b563cSPaul Mackerras 	 */
3478493adffcSBenjamin Herrenschmidt 	if (prom_memory_limit) {
3479493adffcSBenjamin Herrenschmidt 		__be64 val = cpu_to_be64(prom_memory_limit);
34805827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3481493adffcSBenjamin Herrenschmidt 			     &val, sizeof(val));
3482493adffcSBenjamin Herrenschmidt 	}
34839b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
34845827d416SAnton Blanchard 	if (prom_iommu_off)
34855827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3486a23414beSPaul Mackerras 			     NULL, 0);
34879b6b563cSPaul Mackerras 
34885827d416SAnton Blanchard 	if (prom_iommu_force_on)
34895827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3490a23414beSPaul Mackerras 			     NULL, 0);
34919b6b563cSPaul Mackerras 
34925827d416SAnton Blanchard 	if (prom_tce_alloc_start) {
34935827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
34945827d416SAnton Blanchard 			     &prom_tce_alloc_start,
34959b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
34965827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
34975827d416SAnton Blanchard 			     &prom_tce_alloc_end,
34989b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
34999b6b563cSPaul Mackerras 	}
35009b6b563cSPaul Mackerras #endif
35019b6b563cSPaul Mackerras 
35029b6b563cSPaul Mackerras 	/*
35039b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
35049b6b563cSPaul Mackerras 	 */
35059b6b563cSPaul Mackerras 	fixup_device_tree();
35069b6b563cSPaul Mackerras 
35079b6b563cSPaul Mackerras 	/*
35089b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
35099b6b563cSPaul Mackerras 	 */
35109b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
35119b6b563cSPaul Mackerras 	flatten_device_tree();
35129b6b563cSPaul Mackerras 
35133825ac0eSPaul Mackerras 	/*
35143825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
35153825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
351611fdb309SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101).
35173825ac0eSPaul Mackerras 	 */
351811fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
35199b6b563cSPaul Mackerras 		prom_close_stdin();
35209b6b563cSPaul Mackerras 
35219b6b563cSPaul Mackerras 	/*
35229b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
35239b6b563cSPaul Mackerras 	 * devices etc...
35249b6b563cSPaul Mackerras 	 */
35257e862d7eSMichael Ellerman 	prom_printf("Quiescing Open Firmware ...\n");
35269b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
35279b6b563cSPaul Mackerras 
35289b6b563cSPaul Mackerras 	/*
35299b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
35309b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
35319b6b563cSPaul Mackerras 	 * is common to us and kexec
35329b6b563cSPaul Mackerras 	 */
35335827d416SAnton Blanchard 	hdr = dt_header_start;
353440dfef66SBenjamin Herrenschmidt 
35357d70c63cSBenjamin Herrenschmidt 	prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
3536eae5f709SMathieu Malaterre 	prom_debug("->dt_header_start=0x%lx\n", hdr);
35379b6b563cSPaul Mackerras 
35389b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
35399b6b563cSPaul Mackerras 	reloc_got2(-offset);
35405ac47f7aSAnton Blanchard #else
35415ac47f7aSAnton Blanchard 	unreloc_toc();
35429b6b563cSPaul Mackerras #endif
35439b6b563cSPaul Mackerras 
35446a9c930bSRam Pai 	/* Move to secure memory if we're supposed to be secure guests. */
35456a9c930bSRam Pai 	setup_secure_guest(kbase, hdr);
35466a9c930bSRam Pai 
35476e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
35489b6b563cSPaul Mackerras 
35499b6b563cSPaul Mackerras 	return 0;
35509b6b563cSPaul Mackerras }
3551