xref: /linux/arch/powerpc/kernel/prom_init.c (revision 74bb84e5117146fa73eb9d01305975c53022b3c3)
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>
299b6b563cSPaul Mackerras #include <asm/prom.h>
309b6b563cSPaul Mackerras #include <asm/rtas.h>
319b6b563cSPaul Mackerras #include <asm/page.h>
329b6b563cSPaul Mackerras #include <asm/processor.h>
339b6b563cSPaul Mackerras #include <asm/irq.h>
349b6b563cSPaul Mackerras #include <asm/io.h>
359b6b563cSPaul Mackerras #include <asm/smp.h>
369b6b563cSPaul Mackerras #include <asm/mmu.h>
379b6b563cSPaul Mackerras #include <asm/pgtable.h>
389b6b563cSPaul Mackerras #include <asm/iommu.h>
399b6b563cSPaul Mackerras #include <asm/btext.h>
409b6b563cSPaul Mackerras #include <asm/sections.h>
419b6b563cSPaul Mackerras #include <asm/machdep.h>
420545d543SDaniel Axtens #include <asm/asm-prototypes.h>
436a9c930bSRam Pai #include <asm/ultravisor-api.h>
449b6b563cSPaul Mackerras 
459b6b563cSPaul Mackerras #include <linux/linux_logo.h>
469b6b563cSPaul Mackerras 
47e63334e5SBenjamin Herrenschmidt /* All of prom_init bss lives here */
485f69e388SBenjamin Herrenschmidt #define __prombss __section(.bss.prominit)
49e63334e5SBenjamin Herrenschmidt 
509b6b563cSPaul Mackerras /*
519b6b563cSPaul Mackerras  * Eventually bump that one up
529b6b563cSPaul Mackerras  */
539b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE	0x100000
549b6b563cSPaul Mackerras 
559b6b563cSPaul Mackerras /*
569b6b563cSPaul Mackerras  * This is the size of the local memory reserve map that gets copied
579b6b563cSPaul Mackerras  * into the boot params passed to the kernel. That size is totally
589b6b563cSPaul Mackerras  * flexible as the kernel just reads the list until it encounters an
599b6b563cSPaul Mackerras  * entry with size 0, so it can be changed without breaking binary
609b6b563cSPaul Mackerras  * compatibility
619b6b563cSPaul Mackerras  */
629b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE	8
639b6b563cSPaul Mackerras 
649b6b563cSPaul Mackerras /*
659b6b563cSPaul Mackerras  * prom_init() is called very early on, before the kernel text
669b6b563cSPaul Mackerras  * and data have been mapped to KERNELBASE.  At this point the code
679b6b563cSPaul Mackerras  * is running at whatever address it has been loaded at.
689b6b563cSPaul Mackerras  * On ppc32 we compile with -mrelocatable, which means that references
699b6b563cSPaul Mackerras  * to extern and static variables get relocated automatically.
705ac47f7aSAnton Blanchard  * ppc64 objects are always relocatable, we just need to relocate the
715ac47f7aSAnton Blanchard  * TOC.
729b6b563cSPaul Mackerras  *
739b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
749b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
759b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
769b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
779b6b563cSPaul Mackerras  *
789b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
799b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
809b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
819b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
829b6b563cSPaul Mackerras  */
835ac47f7aSAnton Blanchard #define ADDR(x)		(u32)(unsigned long)(x)
845ac47f7aSAnton Blanchard 
859b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
86a23414beSPaul Mackerras #define OF_WORKAROUNDS	0
879b6b563cSPaul Mackerras #else
88a23414beSPaul Mackerras #define OF_WORKAROUNDS	of_workarounds
89e63334e5SBenjamin Herrenschmidt static int of_workarounds __prombss;
909b6b563cSPaul Mackerras #endif
919b6b563cSPaul Mackerras 
92a23414beSPaul Mackerras #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
93a23414beSPaul Mackerras #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
94a23414beSPaul Mackerras 
959b6b563cSPaul Mackerras #define PROM_BUG() do {						\
969b6b563cSPaul Mackerras         prom_printf("kernel BUG at %s line 0x%x!\n",		\
975827d416SAnton Blanchard 		    __FILE__, __LINE__);			\
9863ce271bSChristophe Leroy 	__builtin_trap();					\
999b6b563cSPaul Mackerras } while (0)
1009b6b563cSPaul Mackerras 
1019b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1029b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1039b6b563cSPaul Mackerras #else
10485aa4b98SMathieu Malaterre #define prom_debug(x...)	do { } while (0)
1059b6b563cSPaul Mackerras #endif
1069b6b563cSPaul Mackerras 
1079b6b563cSPaul Mackerras 
1089b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1099b6b563cSPaul Mackerras 
1109b6b563cSPaul Mackerras struct prom_args {
111493adffcSBenjamin Herrenschmidt         __be32 service;
112493adffcSBenjamin Herrenschmidt         __be32 nargs;
113493adffcSBenjamin Herrenschmidt         __be32 nret;
114493adffcSBenjamin Herrenschmidt         __be32 args[10];
1159b6b563cSPaul Mackerras };
1169b6b563cSPaul Mackerras 
1179b6b563cSPaul Mackerras struct prom_t {
1189b6b563cSPaul Mackerras 	ihandle root;
119a23414beSPaul Mackerras 	phandle chosen;
1209b6b563cSPaul Mackerras 	int cpu;
1219b6b563cSPaul Mackerras 	ihandle stdout;
122a575b807SPaul Mackerras 	ihandle mmumap;
123a23414beSPaul Mackerras 	ihandle memory;
1249b6b563cSPaul Mackerras };
1259b6b563cSPaul Mackerras 
1269b6b563cSPaul Mackerras struct mem_map_entry {
127493adffcSBenjamin Herrenschmidt 	__be64	base;
128493adffcSBenjamin Herrenschmidt 	__be64	size;
1299b6b563cSPaul Mackerras };
1309b6b563cSPaul Mackerras 
131493adffcSBenjamin Herrenschmidt typedef __be32 cell_t;
1329b6b563cSPaul Mackerras 
1336e35d5daSBenjamin Herrenschmidt extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
1346e35d5daSBenjamin Herrenschmidt 		    unsigned long r6, unsigned long r7, unsigned long r8,
1356e35d5daSBenjamin Herrenschmidt 		    unsigned long r9);
1369b6b563cSPaul Mackerras 
1379b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
138c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1399b6b563cSPaul Mackerras #else
140c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1419b6b563cSPaul Mackerras {
142c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1439b6b563cSPaul Mackerras }
1449b6b563cSPaul Mackerras #endif
1459b6b563cSPaul Mackerras 
1469b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1479b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1489b6b563cSPaul Mackerras 
1499b6b563cSPaul Mackerras /* prom structure */
150e63334e5SBenjamin Herrenschmidt static struct prom_t __prombss prom;
1519b6b563cSPaul Mackerras 
152e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_entry;
1539b6b563cSPaul Mackerras 
154e63334e5SBenjamin Herrenschmidt static char __prombss of_stdout_device[256];
155d7fbe2a0SChristophe Leroy static char __prombss prom_scratch[256];
1569b6b563cSPaul Mackerras 
157e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_header_start;
158e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_struct_start, dt_struct_end;
159e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_string_start, dt_string_end;
1609b6b563cSPaul Mackerras 
161e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_initrd_start, prom_initrd_end;
1629b6b563cSPaul Mackerras 
1639b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
164e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_force_on;
165e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_off;
166e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_start;
167e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_end;
1689b6b563cSPaul Mackerras #endif
1699b6b563cSPaul Mackerras 
170c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
1718ca2d515SBenjamin Herrenschmidt static bool __prombss prom_radix_disable;
172a3bf9fbdSGreg Kurz static bool __prombss prom_xive_disable;
173c886087cSBenjamin Herrenschmidt #endif
174014d02cbSSuraj Jitindar Singh 
1756a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
1766a9c930bSRam Pai static bool __prombss prom_svm_enable;
1776a9c930bSRam Pai #endif
1786a9c930bSRam Pai 
179014d02cbSSuraj Jitindar Singh struct platform_support {
180014d02cbSSuraj Jitindar Singh 	bool hash_mmu;
181014d02cbSSuraj Jitindar Singh 	bool radix_mmu;
182014d02cbSSuraj Jitindar Singh 	bool radix_gtse;
183ac5e5a54SCédric Le Goater 	bool xive;
184014d02cbSSuraj Jitindar Singh };
185014d02cbSSuraj Jitindar Singh 
186e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
187e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
188e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
189e8222502SBenjamin Herrenschmidt  */
190e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
191e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
192e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
193e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
194e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
195e8222502SBenjamin Herrenschmidt 
196e63334e5SBenjamin Herrenschmidt static int __prombss of_platform;
1979b6b563cSPaul Mackerras 
198e63334e5SBenjamin Herrenschmidt static char __prombss prom_cmd_line[COMMAND_LINE_SIZE];
1999b6b563cSPaul Mackerras 
200e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_memory_limit;
201cf68787bSBenjamin Krill 
202e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top;
203e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top_high;
204e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_bottom;
205e63334e5SBenjamin Herrenschmidt static unsigned long __prombss rmo_top;
206e63334e5SBenjamin Herrenschmidt static unsigned long __prombss ram_top;
2079b6b563cSPaul Mackerras 
208e63334e5SBenjamin Herrenschmidt static struct mem_map_entry __prombss mem_reserve_map[MEM_RESERVE_MAP_SIZE];
209e63334e5SBenjamin Herrenschmidt static int __prombss mem_reserve_cnt;
2109b6b563cSPaul Mackerras 
211e63334e5SBenjamin Herrenschmidt static cell_t __prombss regbuf[1024];
2129b6b563cSPaul Mackerras 
2138ca2d515SBenjamin Herrenschmidt static bool  __prombss rtas_has_query_cpu_stopped;
214dbe78b40SBenjamin Herrenschmidt 
2159b6b563cSPaul Mackerras 
2169b6b563cSPaul Mackerras /*
2179b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2189b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2199b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2209b6b563cSPaul Mackerras  * it is valid
2219b6b563cSPaul Mackerras  */
2229b6b563cSPaul Mackerras 
2239b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2249b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2259b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2269b6b563cSPaul Mackerras 
227450e7dd4SChristophe Leroy /* Copied from lib/string.c and lib/kstrtox.c */
228450e7dd4SChristophe Leroy 
229450e7dd4SChristophe Leroy static int __init prom_strcmp(const char *cs, const char *ct)
230450e7dd4SChristophe Leroy {
231450e7dd4SChristophe Leroy 	unsigned char c1, c2;
232450e7dd4SChristophe Leroy 
233450e7dd4SChristophe Leroy 	while (1) {
234450e7dd4SChristophe Leroy 		c1 = *cs++;
235450e7dd4SChristophe Leroy 		c2 = *ct++;
236450e7dd4SChristophe Leroy 		if (c1 != c2)
237450e7dd4SChristophe Leroy 			return c1 < c2 ? -1 : 1;
238450e7dd4SChristophe Leroy 		if (!c1)
239450e7dd4SChristophe Leroy 			break;
240450e7dd4SChristophe Leroy 	}
241450e7dd4SChristophe Leroy 	return 0;
242450e7dd4SChristophe Leroy }
243450e7dd4SChristophe Leroy 
244450e7dd4SChristophe Leroy static char __init *prom_strcpy(char *dest, const char *src)
245450e7dd4SChristophe Leroy {
246450e7dd4SChristophe Leroy 	char *tmp = dest;
247450e7dd4SChristophe Leroy 
248450e7dd4SChristophe Leroy 	while ((*dest++ = *src++) != '\0')
249450e7dd4SChristophe Leroy 		/* nothing */;
250450e7dd4SChristophe Leroy 	return tmp;
251450e7dd4SChristophe Leroy }
252450e7dd4SChristophe Leroy 
253450e7dd4SChristophe Leroy static int __init prom_strncmp(const char *cs, const char *ct, size_t count)
254450e7dd4SChristophe Leroy {
255450e7dd4SChristophe Leroy 	unsigned char c1, c2;
256450e7dd4SChristophe Leroy 
257450e7dd4SChristophe Leroy 	while (count) {
258450e7dd4SChristophe Leroy 		c1 = *cs++;
259450e7dd4SChristophe Leroy 		c2 = *ct++;
260450e7dd4SChristophe Leroy 		if (c1 != c2)
261450e7dd4SChristophe Leroy 			return c1 < c2 ? -1 : 1;
262450e7dd4SChristophe Leroy 		if (!c1)
263450e7dd4SChristophe Leroy 			break;
264450e7dd4SChristophe Leroy 		count--;
265450e7dd4SChristophe Leroy 	}
266450e7dd4SChristophe Leroy 	return 0;
267450e7dd4SChristophe Leroy }
268450e7dd4SChristophe Leroy 
269450e7dd4SChristophe Leroy static size_t __init prom_strlen(const char *s)
270450e7dd4SChristophe Leroy {
271450e7dd4SChristophe Leroy 	const char *sc;
272450e7dd4SChristophe Leroy 
273450e7dd4SChristophe Leroy 	for (sc = s; *sc != '\0'; ++sc)
274450e7dd4SChristophe Leroy 		/* nothing */;
275450e7dd4SChristophe Leroy 	return sc - s;
276450e7dd4SChristophe Leroy }
277450e7dd4SChristophe Leroy 
278450e7dd4SChristophe Leroy static int __init prom_memcmp(const void *cs, const void *ct, size_t count)
279450e7dd4SChristophe Leroy {
280450e7dd4SChristophe Leroy 	const unsigned char *su1, *su2;
281450e7dd4SChristophe Leroy 	int res = 0;
282450e7dd4SChristophe Leroy 
283450e7dd4SChristophe Leroy 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
284450e7dd4SChristophe Leroy 		if ((res = *su1 - *su2) != 0)
285450e7dd4SChristophe Leroy 			break;
286450e7dd4SChristophe Leroy 	return res;
287450e7dd4SChristophe Leroy }
288450e7dd4SChristophe Leroy 
289450e7dd4SChristophe Leroy static char __init *prom_strstr(const char *s1, const char *s2)
290450e7dd4SChristophe Leroy {
291450e7dd4SChristophe Leroy 	size_t l1, l2;
292450e7dd4SChristophe Leroy 
293450e7dd4SChristophe Leroy 	l2 = prom_strlen(s2);
294450e7dd4SChristophe Leroy 	if (!l2)
295450e7dd4SChristophe Leroy 		return (char *)s1;
296450e7dd4SChristophe Leroy 	l1 = prom_strlen(s1);
297450e7dd4SChristophe Leroy 	while (l1 >= l2) {
298450e7dd4SChristophe Leroy 		l1--;
299450e7dd4SChristophe Leroy 		if (!prom_memcmp(s1, s2, l2))
300450e7dd4SChristophe Leroy 			return (char *)s1;
301450e7dd4SChristophe Leroy 		s1++;
302450e7dd4SChristophe Leroy 	}
303450e7dd4SChristophe Leroy 	return NULL;
304450e7dd4SChristophe Leroy }
305450e7dd4SChristophe Leroy 
306d79fbb3aSChris Packham static size_t __init prom_strlcat(char *dest, const char *src, size_t count)
307450e7dd4SChristophe Leroy {
308d79fbb3aSChris Packham 	size_t dsize = prom_strlen(dest);
309d79fbb3aSChris Packham 	size_t len = prom_strlen(src);
310d79fbb3aSChris Packham 	size_t res = dsize + len;
311450e7dd4SChristophe Leroy 
312d79fbb3aSChris Packham 	/* This would be a bug */
313d79fbb3aSChris Packham 	if (dsize >= count)
314d79fbb3aSChris Packham 		return count;
315d79fbb3aSChris Packham 
316d79fbb3aSChris Packham 	dest += dsize;
317d79fbb3aSChris Packham 	count -= dsize;
318d79fbb3aSChris Packham 	if (len >= count)
319d79fbb3aSChris Packham 		len = count-1;
320450e7dd4SChristophe Leroy 	memcpy(dest, src, len);
321d79fbb3aSChris Packham 	dest[len] = 0;
322d79fbb3aSChris Packham 	return res;
323d79fbb3aSChris Packham 
324450e7dd4SChristophe Leroy }
325450e7dd4SChristophe Leroy 
326450e7dd4SChristophe Leroy #ifdef CONFIG_PPC_PSERIES
327450e7dd4SChristophe Leroy static int __init prom_strtobool(const char *s, bool *res)
328450e7dd4SChristophe Leroy {
329450e7dd4SChristophe Leroy 	if (!s)
330450e7dd4SChristophe Leroy 		return -EINVAL;
331450e7dd4SChristophe Leroy 
332450e7dd4SChristophe Leroy 	switch (s[0]) {
333450e7dd4SChristophe Leroy 	case 'y':
334450e7dd4SChristophe Leroy 	case 'Y':
335450e7dd4SChristophe Leroy 	case '1':
336450e7dd4SChristophe Leroy 		*res = true;
337450e7dd4SChristophe Leroy 		return 0;
338450e7dd4SChristophe Leroy 	case 'n':
339450e7dd4SChristophe Leroy 	case 'N':
340450e7dd4SChristophe Leroy 	case '0':
341450e7dd4SChristophe Leroy 		*res = false;
342450e7dd4SChristophe Leroy 		return 0;
343450e7dd4SChristophe Leroy 	case 'o':
344450e7dd4SChristophe Leroy 	case 'O':
345450e7dd4SChristophe Leroy 		switch (s[1]) {
346450e7dd4SChristophe Leroy 		case 'n':
347450e7dd4SChristophe Leroy 		case 'N':
348450e7dd4SChristophe Leroy 			*res = true;
349450e7dd4SChristophe Leroy 			return 0;
350450e7dd4SChristophe Leroy 		case 'f':
351450e7dd4SChristophe Leroy 		case 'F':
352450e7dd4SChristophe Leroy 			*res = false;
353450e7dd4SChristophe Leroy 			return 0;
354450e7dd4SChristophe Leroy 		default:
355450e7dd4SChristophe Leroy 			break;
356450e7dd4SChristophe Leroy 		}
357450e7dd4SChristophe Leroy 	default:
358450e7dd4SChristophe Leroy 		break;
359450e7dd4SChristophe Leroy 	}
360450e7dd4SChristophe Leroy 
361450e7dd4SChristophe Leroy 	return -EINVAL;
362450e7dd4SChristophe Leroy }
363450e7dd4SChristophe Leroy #endif
3649b6b563cSPaul Mackerras 
3659b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
3669b6b563cSPaul Mackerras  * firmware.
3679b6b563cSPaul Mackerras  */
3689b6b563cSPaul Mackerras 
3699b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
3709b6b563cSPaul Mackerras {
3719b6b563cSPaul Mackerras 	int i;
3729b6b563cSPaul Mackerras 	struct prom_args args;
3739b6b563cSPaul Mackerras 	va_list list;
3749b6b563cSPaul Mackerras 
375493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
376493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
377493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
3789b6b563cSPaul Mackerras 
3799b6b563cSPaul Mackerras 	va_start(list, nret);
3809b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
381493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
3829b6b563cSPaul Mackerras 	va_end(list);
3839b6b563cSPaul Mackerras 
3849b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
3859b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
3869b6b563cSPaul Mackerras 
3875827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
388c4988820SPaul Mackerras 		return PROM_ERROR;
3899b6b563cSPaul Mackerras 
390493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
3919b6b563cSPaul Mackerras }
3929b6b563cSPaul Mackerras 
3939b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
3949b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
3959b6b563cSPaul Mackerras {
3969b6b563cSPaul Mackerras 	int i;
3979b6b563cSPaul Mackerras 	struct prom_args args;
3989b6b563cSPaul Mackerras 	va_list list;
3999b6b563cSPaul Mackerras 
400493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
401493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
402493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
4039b6b563cSPaul Mackerras 
4049b6b563cSPaul Mackerras 	va_start(list, rets);
4059b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
406493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
4079b6b563cSPaul Mackerras 	va_end(list);
4089b6b563cSPaul Mackerras 
4099b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
410ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
4119b6b563cSPaul Mackerras 
4125827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
413c4988820SPaul Mackerras 		return PROM_ERROR;
4149b6b563cSPaul Mackerras 
4159b6b563cSPaul Mackerras 	if (rets != NULL)
4169b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
417493adffcSBenjamin Herrenschmidt 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
4189b6b563cSPaul Mackerras 
419493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
4209b6b563cSPaul Mackerras }
4219b6b563cSPaul Mackerras 
4229b6b563cSPaul Mackerras 
4239b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
4249b6b563cSPaul Mackerras {
4259b6b563cSPaul Mackerras 	const char *p, *q;
4269b6b563cSPaul Mackerras 
4275827d416SAnton Blanchard 	if (prom.stdout == 0)
4289b6b563cSPaul Mackerras 		return;
4299b6b563cSPaul Mackerras 
4309b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
4319b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
4329b6b563cSPaul Mackerras 			;
4339b6b563cSPaul Mackerras 		if (q > p)
4345827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
4359b6b563cSPaul Mackerras 		if (*q == 0)
4369b6b563cSPaul Mackerras 			break;
4379b6b563cSPaul Mackerras 		++q;
4385827d416SAnton Blanchard 		call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
4399b6b563cSPaul Mackerras 	}
4409b6b563cSPaul Mackerras }
4419b6b563cSPaul Mackerras 
4429b6b563cSPaul Mackerras 
4438af1da40SMathieu Malaterre /*
4448af1da40SMathieu Malaterre  * Both prom_print_hex & prom_print_dec takes an unsigned long as input so that
4458af1da40SMathieu Malaterre  * we do not need __udivdi3 or __umoddi3 on 32bits.
4468af1da40SMathieu Malaterre  */
4479b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
4489b6b563cSPaul Mackerras {
4499b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
4509b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
4519b6b563cSPaul Mackerras 
4529b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
4539b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
4549b6b563cSPaul Mackerras 		if (buf[i] > '9')
4559b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
4569b6b563cSPaul Mackerras 		val >>= 4;
4579b6b563cSPaul Mackerras 	}
4589b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
4595827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf, nibbles);
4609b6b563cSPaul Mackerras }
4619b6b563cSPaul Mackerras 
4622c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */
4632c48a7d6SMichael Neuling #define UL_DIGITS 21
4642c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val)
4652c48a7d6SMichael Neuling {
4662c48a7d6SMichael Neuling 	int i, size;
4672c48a7d6SMichael Neuling 	char buf[UL_DIGITS+1];
4682c48a7d6SMichael Neuling 
4692c48a7d6SMichael Neuling 	for (i = UL_DIGITS-1; i >= 0;  i--) {
4702c48a7d6SMichael Neuling 		buf[i] = (val % 10) + '0';
4712c48a7d6SMichael Neuling 		val = val/10;
4722c48a7d6SMichael Neuling 		if (val == 0)
4732c48a7d6SMichael Neuling 			break;
4742c48a7d6SMichael Neuling 	}
4752c48a7d6SMichael Neuling 	/* shift stuff down */
4762c48a7d6SMichael Neuling 	size = UL_DIGITS - i;
4775827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf+i, size);
4782c48a7d6SMichael Neuling }
4799b6b563cSPaul Mackerras 
480eae5f709SMathieu Malaterre __printf(1, 2)
4819b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
4829b6b563cSPaul Mackerras {
4839b6b563cSPaul Mackerras 	const char *p, *q, *s;
4849b6b563cSPaul Mackerras 	va_list args;
4859b6b563cSPaul Mackerras 	unsigned long v;
486af277149SBenjamin Herrenschmidt 	long vs;
4878af1da40SMathieu Malaterre 	int n = 0;
4889b6b563cSPaul Mackerras 
4899b6b563cSPaul Mackerras 	va_start(args, format);
4909b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
4919b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
4929b6b563cSPaul Mackerras 			;
4939b6b563cSPaul Mackerras 		if (q > p)
4945827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
4959b6b563cSPaul Mackerras 		if (*q == 0)
4969b6b563cSPaul Mackerras 			break;
4979b6b563cSPaul Mackerras 		if (*q == '\n') {
4989b6b563cSPaul Mackerras 			++q;
4995827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout,
5009b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
5019b6b563cSPaul Mackerras 			continue;
5029b6b563cSPaul Mackerras 		}
5039b6b563cSPaul Mackerras 		++q;
5049b6b563cSPaul Mackerras 		if (*q == 0)
5059b6b563cSPaul Mackerras 			break;
5068af1da40SMathieu Malaterre 		while (*q == 'l') {
5078af1da40SMathieu Malaterre 			++q;
5088af1da40SMathieu Malaterre 			++n;
5098af1da40SMathieu Malaterre 		}
5109b6b563cSPaul Mackerras 		switch (*q) {
5119b6b563cSPaul Mackerras 		case 's':
5129b6b563cSPaul Mackerras 			++q;
5139b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
5149b6b563cSPaul Mackerras 			prom_print(s);
5159b6b563cSPaul Mackerras 			break;
5169b6b563cSPaul Mackerras 		case 'x':
5179b6b563cSPaul Mackerras 			++q;
5188af1da40SMathieu Malaterre 			switch (n) {
5198af1da40SMathieu Malaterre 			case 0:
5208af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
5218af1da40SMathieu Malaterre 				break;
5228af1da40SMathieu Malaterre 			case 1:
5239b6b563cSPaul Mackerras 				v = va_arg(args, unsigned long);
5248af1da40SMathieu Malaterre 				break;
5258af1da40SMathieu Malaterre 			case 2:
5268af1da40SMathieu Malaterre 			default:
5278af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
5288af1da40SMathieu Malaterre 				break;
5298af1da40SMathieu Malaterre 			}
5309b6b563cSPaul Mackerras 			prom_print_hex(v);
5319b6b563cSPaul Mackerras 			break;
5328af1da40SMathieu Malaterre 		case 'u':
5338af1da40SMathieu Malaterre 			++q;
5348af1da40SMathieu Malaterre 			switch (n) {
5358af1da40SMathieu Malaterre 			case 0:
5368af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
5378af1da40SMathieu Malaterre 				break;
5388af1da40SMathieu Malaterre 			case 1:
5398af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long);
5408af1da40SMathieu Malaterre 				break;
5418af1da40SMathieu Malaterre 			case 2:
5428af1da40SMathieu Malaterre 			default:
5438af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
5448af1da40SMathieu Malaterre 				break;
5458af1da40SMathieu Malaterre 			}
5468af1da40SMathieu Malaterre 			prom_print_dec(v);
5478af1da40SMathieu Malaterre 			break;
548af277149SBenjamin Herrenschmidt 		case 'd':
549af277149SBenjamin Herrenschmidt 			++q;
5508af1da40SMathieu Malaterre 			switch (n) {
5518af1da40SMathieu Malaterre 			case 0:
552af277149SBenjamin Herrenschmidt 				vs = va_arg(args, int);
553af277149SBenjamin Herrenschmidt 				break;
5548af1da40SMathieu Malaterre 			case 1:
555af277149SBenjamin Herrenschmidt 				vs = va_arg(args, long);
5568af1da40SMathieu Malaterre 				break;
5578af1da40SMathieu Malaterre 			case 2:
5588af1da40SMathieu Malaterre 			default:
5598af1da40SMathieu Malaterre 				vs = va_arg(args, long long);
5608af1da40SMathieu Malaterre 				break;
5618af1da40SMathieu Malaterre 			}
562af277149SBenjamin Herrenschmidt 			if (vs < 0) {
5635827d416SAnton Blanchard 				prom_print("-");
564af277149SBenjamin Herrenschmidt 				vs = -vs;
565af277149SBenjamin Herrenschmidt 			}
566af277149SBenjamin Herrenschmidt 			prom_print_dec(vs);
5672c48a7d6SMichael Neuling 			break;
5689b6b563cSPaul Mackerras 		}
5699b6b563cSPaul Mackerras 	}
5701b855e16SDaniel Axtens 	va_end(args);
5719b6b563cSPaul Mackerras }
5729b6b563cSPaul Mackerras 
5739b6b563cSPaul Mackerras 
574a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
575a575b807SPaul Mackerras 				unsigned long align)
576a575b807SPaul Mackerras {
577a575b807SPaul Mackerras 
578a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
579a23414beSPaul Mackerras 		/*
580a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
581a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
582a23414beSPaul Mackerras 		 */
583a23414beSPaul Mackerras 		int ret;
584a23414beSPaul Mackerras 		prom_arg_t result;
585a23414beSPaul Mackerras 
586a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
5875827d416SAnton Blanchard 				    ADDR("claim"), prom.memory,
588a23414beSPaul Mackerras 				    align, size, virt);
589a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
590a23414beSPaul Mackerras 			return -1;
591a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
5925827d416SAnton Blanchard 				    ADDR("claim"), prom.mmumap,
593a23414beSPaul Mackerras 				    align, size, virt);
594a23414beSPaul Mackerras 		if (ret != 0) {
595a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
5965827d416SAnton Blanchard 				  prom.memory, size, virt);
597a23414beSPaul Mackerras 			return -1;
598a23414beSPaul Mackerras 		}
599a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
600a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
6015827d416SAnton Blanchard 			  ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
602a23414beSPaul Mackerras 		return virt;
603a23414beSPaul Mackerras 	}
604a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
605a23414beSPaul Mackerras 			 (prom_arg_t)align);
606a575b807SPaul Mackerras }
607a575b807SPaul Mackerras 
6089b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
6099b6b563cSPaul Mackerras {
6109b6b563cSPaul Mackerras 	prom_print(reason);
611add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
612add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
6135827d416SAnton Blanchard 	if (of_platform == PLATFORM_POWERMAC)
614add60ef3SOlaf Hering 		asm("trap\n");
615add60ef3SOlaf Hering 
6161d9a4731SStephen Rothwell 	/* ToDo: should put up an SRC here on pSeries */
6179b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
6189b6b563cSPaul Mackerras 
6199b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
6209b6b563cSPaul Mackerras 		;
6219b6b563cSPaul Mackerras }
6229b6b563cSPaul Mackerras 
6239b6b563cSPaul Mackerras 
6249b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
6259b6b563cSPaul Mackerras {
6269b6b563cSPaul Mackerras 	phandle node;
6279b6b563cSPaul Mackerras 
6289b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
6299b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
6309b6b563cSPaul Mackerras 		return 1;
6319b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
6329b6b563cSPaul Mackerras 		return 1;
6339b6b563cSPaul Mackerras 	for (;;) {
6349b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
6359b6b563cSPaul Mackerras 			return 0;
6369b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
6379b6b563cSPaul Mackerras 			return 1;
6389b6b563cSPaul Mackerras 	}
6399b6b563cSPaul Mackerras }
6409b6b563cSPaul Mackerras 
641480795a0SMasahiro Yamada static inline int __init prom_getprop(phandle node, const char *pname,
6429b6b563cSPaul Mackerras 				      void *value, size_t valuelen)
6439b6b563cSPaul Mackerras {
6449b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
6459b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
6469b6b563cSPaul Mackerras }
6479b6b563cSPaul Mackerras 
648480795a0SMasahiro Yamada static inline int __init prom_getproplen(phandle node, const char *pname)
6499b6b563cSPaul Mackerras {
6509b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
6519b6b563cSPaul Mackerras }
6529b6b563cSPaul Mackerras 
653a23414beSPaul Mackerras static void add_string(char **str, const char *q)
6549b6b563cSPaul Mackerras {
655a23414beSPaul Mackerras 	char *p = *str;
656a23414beSPaul Mackerras 
657a23414beSPaul Mackerras 	while (*q)
658a23414beSPaul Mackerras 		*p++ = *q++;
659a23414beSPaul Mackerras 	*p++ = ' ';
660a23414beSPaul Mackerras 	*str = p;
661a23414beSPaul Mackerras }
662a23414beSPaul Mackerras 
663a23414beSPaul Mackerras static char *tohex(unsigned int x)
664a23414beSPaul Mackerras {
6658ca2d515SBenjamin Herrenschmidt 	static const char digits[] __initconst = "0123456789abcdef";
6668ca2d515SBenjamin Herrenschmidt 	static char result[9] __prombss;
667a23414beSPaul Mackerras 	int i;
668a23414beSPaul Mackerras 
669a23414beSPaul Mackerras 	result[8] = 0;
670a23414beSPaul Mackerras 	i = 8;
671a23414beSPaul Mackerras 	do {
672a23414beSPaul Mackerras 		--i;
673a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
674a23414beSPaul Mackerras 		x >>= 4;
675a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
676a23414beSPaul Mackerras 	return &result[i];
677a23414beSPaul Mackerras }
678a23414beSPaul Mackerras 
679a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
680a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
681a23414beSPaul Mackerras {
682a23414beSPaul Mackerras 	char cmd[256], *p;
683a23414beSPaul Mackerras 
684a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
6859b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
6869b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
687a23414beSPaul Mackerras 
688a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
689a23414beSPaul Mackerras 	p = cmd;
690a23414beSPaul Mackerras 	add_string(&p, "dev");
691a23414beSPaul Mackerras 	add_string(&p, nodename);
692a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
693a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
694a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
695450e7dd4SChristophe Leroy 	add_string(&p, tohex(prom_strlen(pname)));
696a23414beSPaul Mackerras 	add_string(&p, "property");
697a23414beSPaul Mackerras 	*p = 0;
698a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
6999b6b563cSPaul Mackerras }
7009b6b563cSPaul Mackerras 
7015827d416SAnton Blanchard /* We can't use the standard versions because of relocation headaches. */
702cf68787bSBenjamin Krill #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
703cf68787bSBenjamin Krill 			 || ('a' <= (c) && (c) <= 'f') \
704cf68787bSBenjamin Krill 			 || ('A' <= (c) && (c) <= 'F'))
705cf68787bSBenjamin Krill 
706cf68787bSBenjamin Krill #define isdigit(c)	('0' <= (c) && (c) <= '9')
707cf68787bSBenjamin Krill #define islower(c)	('a' <= (c) && (c) <= 'z')
708cf68787bSBenjamin Krill #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
709cf68787bSBenjamin Krill 
710493adffcSBenjamin Herrenschmidt static unsigned long prom_strtoul(const char *cp, const char **endp)
711cf68787bSBenjamin Krill {
712cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
713cf68787bSBenjamin Krill 
714cf68787bSBenjamin Krill 	if (*cp == '0') {
715cf68787bSBenjamin Krill 		base = 8;
716cf68787bSBenjamin Krill 		cp++;
717cf68787bSBenjamin Krill 		if (toupper(*cp) == 'X') {
718cf68787bSBenjamin Krill 			cp++;
719cf68787bSBenjamin Krill 			base = 16;
720cf68787bSBenjamin Krill 		}
721cf68787bSBenjamin Krill 	}
722cf68787bSBenjamin Krill 
723cf68787bSBenjamin Krill 	while (isxdigit(*cp) &&
724cf68787bSBenjamin Krill 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
725cf68787bSBenjamin Krill 		result = result * base + value;
726cf68787bSBenjamin Krill 		cp++;
727cf68787bSBenjamin Krill 	}
728cf68787bSBenjamin Krill 
729cf68787bSBenjamin Krill 	if (endp)
730cf68787bSBenjamin Krill 		*endp = cp;
731cf68787bSBenjamin Krill 
732cf68787bSBenjamin Krill 	return result;
733cf68787bSBenjamin Krill }
734cf68787bSBenjamin Krill 
735493adffcSBenjamin Herrenschmidt static unsigned long prom_memparse(const char *ptr, const char **retptr)
736cf68787bSBenjamin Krill {
737cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
738cf68787bSBenjamin Krill 	int shift = 0;
739cf68787bSBenjamin Krill 
740cf68787bSBenjamin Krill 	/*
741cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
742cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
743cf68787bSBenjamin Krill 	 * the address we're linked at.
744cf68787bSBenjamin Krill 	 */
745cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
746cf68787bSBenjamin Krill 		shift = 30;
747cf68787bSBenjamin Krill 
748cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
749cf68787bSBenjamin Krill 		shift = 20;
750cf68787bSBenjamin Krill 
751cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
752cf68787bSBenjamin Krill 		shift = 10;
753cf68787bSBenjamin Krill 
754cf68787bSBenjamin Krill 	if (shift) {
755cf68787bSBenjamin Krill 		ret <<= shift;
756cf68787bSBenjamin Krill 		(*retptr)++;
757cf68787bSBenjamin Krill 	}
758cf68787bSBenjamin Krill 
759cf68787bSBenjamin Krill 	return ret;
760cf68787bSBenjamin Krill }
761cf68787bSBenjamin Krill 
7629b6b563cSPaul Mackerras /*
7639b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
7649b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
7659b6b563cSPaul Mackerras  */
7669b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
7679b6b563cSPaul Mackerras {
768cc5d0189SBenjamin Herrenschmidt 	const char *opt;
769cf68787bSBenjamin Krill 
770cc5d0189SBenjamin Herrenschmidt 	char *p;
771cbe46bd4SChristophe Leroy 	int l = 0;
7729b6b563cSPaul Mackerras 
7735827d416SAnton Blanchard 	prom_cmd_line[0] = 0;
7745827d416SAnton Blanchard 	p = prom_cmd_line;
775d79fbb3aSChris Packham 
776d79fbb3aSChris Packham 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && (long)prom.chosen > 0)
7775827d416SAnton Blanchard 		l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
778d79fbb3aSChris Packham 
779d79fbb3aSChris Packham 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) || l <= 0 || p[0] == '\0')
780d79fbb3aSChris Packham 		prom_strlcat(prom_cmd_line, " " CONFIG_CMDLINE,
781d79fbb3aSChris Packham 			     sizeof(prom_cmd_line));
782d79fbb3aSChris Packham 
7835827d416SAnton Blanchard 	prom_printf("command line: %s\n", prom_cmd_line);
7849b6b563cSPaul Mackerras 
7859b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
786450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "iommu=");
7879b6b563cSPaul Mackerras 	if (opt) {
7889b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
7899b6b563cSPaul Mackerras 		opt += 6;
7909b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
7919b6b563cSPaul Mackerras 			opt++;
792450e7dd4SChristophe Leroy 		if (!prom_strncmp(opt, "off", 3))
7935827d416SAnton Blanchard 			prom_iommu_off = 1;
794450e7dd4SChristophe Leroy 		else if (!prom_strncmp(opt, "force", 5))
7955827d416SAnton Blanchard 			prom_iommu_force_on = 1;
7969b6b563cSPaul Mackerras 	}
7979b6b563cSPaul Mackerras #endif
798450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "mem=");
799cf68787bSBenjamin Krill 	if (opt) {
800cf68787bSBenjamin Krill 		opt += 4;
8015827d416SAnton Blanchard 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
802cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
803cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
8045827d416SAnton Blanchard 		prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
805cf68787bSBenjamin Krill #endif
806cf68787bSBenjamin Krill 	}
807014d02cbSSuraj Jitindar Singh 
808c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
809c886087cSBenjamin Herrenschmidt 	prom_radix_disable = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
810450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "disable_radix");
811014d02cbSSuraj Jitindar Singh 	if (opt) {
8127a22d632SAneesh Kumar K.V 		opt += 13;
8137a22d632SAneesh Kumar K.V 		if (*opt && *opt == '=') {
8147a22d632SAneesh Kumar K.V 			bool val;
8157a22d632SAneesh Kumar K.V 
816450e7dd4SChristophe Leroy 			if (prom_strtobool(++opt, &val))
8177a22d632SAneesh Kumar K.V 				prom_radix_disable = false;
8187a22d632SAneesh Kumar K.V 			else
8197a22d632SAneesh Kumar K.V 				prom_radix_disable = val;
8207a22d632SAneesh Kumar K.V 		} else
821014d02cbSSuraj Jitindar Singh 			prom_radix_disable = true;
822014d02cbSSuraj Jitindar Singh 	}
8237a22d632SAneesh Kumar K.V 	if (prom_radix_disable)
8247a22d632SAneesh Kumar K.V 		prom_debug("Radix disabled from cmdline\n");
825a3bf9fbdSGreg Kurz 
826a3bf9fbdSGreg Kurz 	opt = prom_strstr(prom_cmd_line, "xive=off");
827a3bf9fbdSGreg Kurz 	if (opt) {
828a3bf9fbdSGreg Kurz 		prom_xive_disable = true;
829a3bf9fbdSGreg Kurz 		prom_debug("XIVE disabled from cmdline\n");
830a3bf9fbdSGreg Kurz 	}
831c886087cSBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
8326a9c930bSRam Pai 
8336a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
8346a9c930bSRam Pai 	opt = prom_strstr(prom_cmd_line, "svm=");
8356a9c930bSRam Pai 	if (opt) {
8366a9c930bSRam Pai 		bool val;
8376a9c930bSRam Pai 
8386a9c930bSRam Pai 		opt += sizeof("svm=") - 1;
8396a9c930bSRam Pai 		if (!prom_strtobool(opt, &val))
8406a9c930bSRam Pai 			prom_svm_enable = val;
8416a9c930bSRam Pai 	}
8426a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
8439b6b563cSPaul Mackerras }
8449b6b563cSPaul Mackerras 
84511fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
8469b6b563cSPaul Mackerras /*
847530b5e14SNathan Fontenot  * The architecture vector has an array of PVR mask/value pairs,
848530b5e14SNathan Fontenot  * followed by # option vectors - 1, followed by the option vectors.
849530b5e14SNathan Fontenot  *
850530b5e14SNathan Fontenot  * See prom.h for the definition of the bits specified in the
851530b5e14SNathan Fontenot  * architecture vector.
852f709bfacSPaul Mackerras  */
853f709bfacSPaul Mackerras 
854e8a4fd0aSMichael Ellerman /* Firmware expects the value to be n - 1, where n is the # of vectors */
855e8a4fd0aSMichael Ellerman #define NUM_VECTORS(n)		((n) - 1)
856e8a4fd0aSMichael Ellerman 
857e8a4fd0aSMichael Ellerman /*
858e8a4fd0aSMichael Ellerman  * Firmware expects 1 + n - 2, where n is the length of the option vector in
859e8a4fd0aSMichael Ellerman  * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
860e8a4fd0aSMichael Ellerman  */
861e8a4fd0aSMichael Ellerman #define VECTOR_LENGTH(n)	(1 + (n) - 2)
862e8a4fd0aSMichael Ellerman 
863d03d1d65SMichael Ellerman struct option_vector1 {
864d03d1d65SMichael Ellerman 	u8 byte1;
865d03d1d65SMichael Ellerman 	u8 arch_versions;
866cc3d2940SPaul Mackerras 	u8 arch_versions3;
867d03d1d65SMichael Ellerman } __packed;
868d03d1d65SMichael Ellerman 
869d03d1d65SMichael Ellerman struct option_vector2 {
870d03d1d65SMichael Ellerman 	u8 byte1;
871d03d1d65SMichael Ellerman 	__be16 reserved;
872d03d1d65SMichael Ellerman 	__be32 real_base;
873d03d1d65SMichael Ellerman 	__be32 real_size;
874d03d1d65SMichael Ellerman 	__be32 virt_base;
875d03d1d65SMichael Ellerman 	__be32 virt_size;
876d03d1d65SMichael Ellerman 	__be32 load_base;
877d03d1d65SMichael Ellerman 	__be32 min_rma;
878d03d1d65SMichael Ellerman 	__be32 min_load;
879d03d1d65SMichael Ellerman 	u8 min_rma_percent;
880d03d1d65SMichael Ellerman 	u8 max_pft_size;
881d03d1d65SMichael Ellerman } __packed;
882d03d1d65SMichael Ellerman 
883d03d1d65SMichael Ellerman struct option_vector3 {
884d03d1d65SMichael Ellerman 	u8 byte1;
885d03d1d65SMichael Ellerman 	u8 byte2;
886d03d1d65SMichael Ellerman } __packed;
887d03d1d65SMichael Ellerman 
888d03d1d65SMichael Ellerman struct option_vector4 {
889d03d1d65SMichael Ellerman 	u8 byte1;
890d03d1d65SMichael Ellerman 	u8 min_vp_cap;
891d03d1d65SMichael Ellerman } __packed;
892d03d1d65SMichael Ellerman 
893d03d1d65SMichael Ellerman struct option_vector5 {
894d03d1d65SMichael Ellerman 	u8 byte1;
895d03d1d65SMichael Ellerman 	u8 byte2;
896d03d1d65SMichael Ellerman 	u8 byte3;
897d03d1d65SMichael Ellerman 	u8 cmo;
898d03d1d65SMichael Ellerman 	u8 associativity;
899d03d1d65SMichael Ellerman 	u8 bin_opts;
900d03d1d65SMichael Ellerman 	u8 micro_checkpoint;
901d03d1d65SMichael Ellerman 	u8 reserved0;
902d03d1d65SMichael Ellerman 	__be32 max_cpus;
903d03d1d65SMichael Ellerman 	__be16 papr_level;
904d03d1d65SMichael Ellerman 	__be16 reserved1;
905d03d1d65SMichael Ellerman 	u8 platform_facilities;
906d03d1d65SMichael Ellerman 	u8 reserved2;
907d03d1d65SMichael Ellerman 	__be16 reserved3;
908d03d1d65SMichael Ellerman 	u8 subprocessors;
909cc3d2940SPaul Mackerras 	u8 byte22;
910cc3d2940SPaul Mackerras 	u8 intarch;
911cc3d2940SPaul Mackerras 	u8 mmu;
912014d02cbSSuraj Jitindar Singh 	u8 hash_ext;
913014d02cbSSuraj Jitindar Singh 	u8 radix_ext;
914d03d1d65SMichael Ellerman } __packed;
915d03d1d65SMichael Ellerman 
916d03d1d65SMichael Ellerman struct option_vector6 {
917d03d1d65SMichael Ellerman 	u8 reserved;
918d03d1d65SMichael Ellerman 	u8 secondary_pteg;
919d03d1d65SMichael Ellerman 	u8 os_name;
920d03d1d65SMichael Ellerman } __packed;
921d03d1d65SMichael Ellerman 
92276ffb578SMichael Ellerman struct ibm_arch_vec {
923cc3d2940SPaul Mackerras 	struct { u32 mask, val; } pvrs[12];
92476ffb578SMichael Ellerman 
92576ffb578SMichael Ellerman 	u8 num_vectors;
92676ffb578SMichael Ellerman 
92776ffb578SMichael Ellerman 	u8 vec1_len;
92876ffb578SMichael Ellerman 	struct option_vector1 vec1;
92976ffb578SMichael Ellerman 
93076ffb578SMichael Ellerman 	u8 vec2_len;
93176ffb578SMichael Ellerman 	struct option_vector2 vec2;
93276ffb578SMichael Ellerman 
93376ffb578SMichael Ellerman 	u8 vec3_len;
93476ffb578SMichael Ellerman 	struct option_vector3 vec3;
93576ffb578SMichael Ellerman 
93676ffb578SMichael Ellerman 	u8 vec4_len;
93776ffb578SMichael Ellerman 	struct option_vector4 vec4;
93876ffb578SMichael Ellerman 
93976ffb578SMichael Ellerman 	u8 vec5_len;
94076ffb578SMichael Ellerman 	struct option_vector5 vec5;
94176ffb578SMichael Ellerman 
94276ffb578SMichael Ellerman 	u8 vec6_len;
94376ffb578SMichael Ellerman 	struct option_vector6 vec6;
94476ffb578SMichael Ellerman } __packed;
94576ffb578SMichael Ellerman 
946d00e34b9SBenjamin Herrenschmidt static const struct ibm_arch_vec ibm_architecture_vec_template __initconst = {
94776ffb578SMichael Ellerman 	.pvrs = {
94876ffb578SMichael Ellerman 		{
94976ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffe0000), /* POWER5/POWER5+ */
95076ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003a0000),
95176ffb578SMichael Ellerman 		},
95276ffb578SMichael Ellerman 		{
95376ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER6 */
95476ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003e0000),
95576ffb578SMichael Ellerman 		},
95676ffb578SMichael Ellerman 		{
95776ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER7 */
95876ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003f0000),
95976ffb578SMichael Ellerman 		},
96076ffb578SMichael Ellerman 		{
96176ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8E */
96276ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004b0000),
96376ffb578SMichael Ellerman 		},
96476ffb578SMichael Ellerman 		{
96576ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8NVL */
96676ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004c0000),
96776ffb578SMichael Ellerman 		},
96876ffb578SMichael Ellerman 		{
96976ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8 */
97076ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004d0000),
97176ffb578SMichael Ellerman 		},
97276ffb578SMichael Ellerman 		{
973cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffff0000), /* POWER9 */
974cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x004e0000),
975cc3d2940SPaul Mackerras 		},
976cc3d2940SPaul Mackerras 		{
977cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffffffff), /* all 3.00-compliant */
978cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x0f000005),
979cc3d2940SPaul Mackerras 		},
980cc3d2940SPaul Mackerras 		{
98176ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.07-compliant */
98276ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000004),
98376ffb578SMichael Ellerman 		},
98476ffb578SMichael Ellerman 		{
98576ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.06-compliant */
98676ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000003),
98776ffb578SMichael Ellerman 		},
98876ffb578SMichael Ellerman 		{
98976ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.05-compliant */
99076ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000002),
99176ffb578SMichael Ellerman 		},
99276ffb578SMichael Ellerman 		{
99376ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffffffe), /* all 2.04-compliant and earlier */
99476ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000001),
99576ffb578SMichael Ellerman 		},
99676ffb578SMichael Ellerman 	},
99776ffb578SMichael Ellerman 
99876ffb578SMichael Ellerman 	.num_vectors = NUM_VECTORS(6),
99976ffb578SMichael Ellerman 
100076ffb578SMichael Ellerman 	.vec1_len = VECTOR_LENGTH(sizeof(struct option_vector1)),
100176ffb578SMichael Ellerman 	.vec1 = {
100276ffb578SMichael Ellerman 		.byte1 = 0,
100376ffb578SMichael Ellerman 		.arch_versions = OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
100476ffb578SMichael Ellerman 				 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
1005cc3d2940SPaul Mackerras 		.arch_versions3 = OV1_PPC_3_00,
100676ffb578SMichael Ellerman 	},
100776ffb578SMichael Ellerman 
100876ffb578SMichael Ellerman 	.vec2_len = VECTOR_LENGTH(sizeof(struct option_vector2)),
100976ffb578SMichael Ellerman 	/* option vector 2: Open Firmware options supported */
101076ffb578SMichael Ellerman 	.vec2 = {
101176ffb578SMichael Ellerman 		.byte1 = OV2_REAL_MODE,
101276ffb578SMichael Ellerman 		.reserved = 0,
101376ffb578SMichael Ellerman 		.real_base = cpu_to_be32(0xffffffff),
101476ffb578SMichael Ellerman 		.real_size = cpu_to_be32(0xffffffff),
101576ffb578SMichael Ellerman 		.virt_base = cpu_to_be32(0xffffffff),
101676ffb578SMichael Ellerman 		.virt_size = cpu_to_be32(0xffffffff),
101776ffb578SMichael Ellerman 		.load_base = cpu_to_be32(0xffffffff),
1018687da8fcSSukadev Bhattiprolu 		.min_rma = cpu_to_be32(512),		/* 512MB min RMA */
101976ffb578SMichael Ellerman 		.min_load = cpu_to_be32(0xffffffff),	/* full client load */
102076ffb578SMichael Ellerman 		.min_rma_percent = 0,	/* min RMA percentage of total RAM */
102176ffb578SMichael Ellerman 		.max_pft_size = 48,	/* max log_2(hash table size) */
102276ffb578SMichael Ellerman 	},
102376ffb578SMichael Ellerman 
102476ffb578SMichael Ellerman 	.vec3_len = VECTOR_LENGTH(sizeof(struct option_vector3)),
102576ffb578SMichael Ellerman 	/* option vector 3: processor options supported */
102676ffb578SMichael Ellerman 	.vec3 = {
102776ffb578SMichael Ellerman 		.byte1 = 0,			/* don't ignore, don't halt */
102876ffb578SMichael Ellerman 		.byte2 = OV3_FP | OV3_VMX | OV3_DFP,
102976ffb578SMichael Ellerman 	},
103076ffb578SMichael Ellerman 
103176ffb578SMichael Ellerman 	.vec4_len = VECTOR_LENGTH(sizeof(struct option_vector4)),
103276ffb578SMichael Ellerman 	/* option vector 4: IBM PAPR implementation */
103376ffb578SMichael Ellerman 	.vec4 = {
103476ffb578SMichael Ellerman 		.byte1 = 0,			/* don't halt */
103576ffb578SMichael Ellerman 		.min_vp_cap = OV4_MIN_ENT_CAP,	/* minimum VP entitled capacity */
103676ffb578SMichael Ellerman 	},
103776ffb578SMichael Ellerman 
103876ffb578SMichael Ellerman 	.vec5_len = VECTOR_LENGTH(sizeof(struct option_vector5)),
103976ffb578SMichael Ellerman 	/* option vector 5: PAPR/OF options */
104076ffb578SMichael Ellerman 	.vec5 = {
104176ffb578SMichael Ellerman 		.byte1 = 0,				/* don't ignore, don't halt */
104276ffb578SMichael Ellerman 		.byte2 = OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
104376ffb578SMichael Ellerman 		OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
104476ffb578SMichael Ellerman #ifdef CONFIG_PCI_MSI
104576ffb578SMichael Ellerman 		/* PCIe/MSI support.  Without MSI full PCIe is not supported */
104676ffb578SMichael Ellerman 		OV5_FEAT(OV5_MSI),
104776ffb578SMichael Ellerman #else
104876ffb578SMichael Ellerman 		0,
104976ffb578SMichael Ellerman #endif
105076ffb578SMichael Ellerman 		.byte3 = 0,
105176ffb578SMichael Ellerman 		.cmo =
105276ffb578SMichael Ellerman #ifdef CONFIG_PPC_SMLPAR
105376ffb578SMichael Ellerman 		OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
105476ffb578SMichael Ellerman #else
105576ffb578SMichael Ellerman 		0,
105676ffb578SMichael Ellerman #endif
105776ffb578SMichael Ellerman 		.associativity = OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
10583dbbaf20SMichael Roth 		.bin_opts = OV5_FEAT(OV5_RESIZE_HPT) | OV5_FEAT(OV5_HP_EVT),
105976ffb578SMichael Ellerman 		.micro_checkpoint = 0,
106076ffb578SMichael Ellerman 		.reserved0 = 0,
106176ffb578SMichael Ellerman 		.max_cpus = cpu_to_be32(NR_CPUS),	/* number of cores supported */
106276ffb578SMichael Ellerman 		.papr_level = 0,
106376ffb578SMichael Ellerman 		.reserved1 = 0,
106476ffb578SMichael Ellerman 		.platform_facilities = OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) | OV5_FEAT(OV5_PFO_HW_842),
106576ffb578SMichael Ellerman 		.reserved2 = 0,
106676ffb578SMichael Ellerman 		.reserved3 = 0,
106776ffb578SMichael Ellerman 		.subprocessors = 1,
10680a87ccd3STyrel Datwyler 		.byte22 = OV5_FEAT(OV5_DRMEM_V2) | OV5_FEAT(OV5_DRC_INFO),
1069cc3d2940SPaul Mackerras 		.intarch = 0,
1070014d02cbSSuraj Jitindar Singh 		.mmu = 0,
1071014d02cbSSuraj Jitindar Singh 		.hash_ext = 0,
1072014d02cbSSuraj Jitindar Singh 		.radix_ext = 0,
107376ffb578SMichael Ellerman 	},
107476ffb578SMichael Ellerman 
107576ffb578SMichael Ellerman 	/* option vector 6: IBM PAPR hints */
107676ffb578SMichael Ellerman 	.vec6_len = VECTOR_LENGTH(sizeof(struct option_vector6)),
107776ffb578SMichael Ellerman 	.vec6 = {
107876ffb578SMichael Ellerman 		.reserved = 0,
107976ffb578SMichael Ellerman 		.secondary_pteg = 0,
108076ffb578SMichael Ellerman 		.os_name = OV6_LINUX,
108176ffb578SMichael Ellerman 	},
108276ffb578SMichael Ellerman };
108376ffb578SMichael Ellerman 
1084a614f52eSBenjamin Herrenschmidt static struct ibm_arch_vec __prombss ibm_architecture_vec  ____cacheline_aligned;
1085a614f52eSBenjamin Herrenschmidt 
1086493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
1087493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
108830c69ca0SBenjamin Herrenschmidt static const struct fake_elf {
10899b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
10909b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
10919b6b563cSPaul Mackerras 	struct chrpnote {
10929b6b563cSPaul Mackerras 		u32	namesz;
10939b6b563cSPaul Mackerras 		u32	descsz;
10949b6b563cSPaul Mackerras 		u32	type;
10959b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
10969b6b563cSPaul Mackerras 		struct chrpdesc {
10979b6b563cSPaul Mackerras 			u32	real_mode;
10989b6b563cSPaul Mackerras 			u32	real_base;
10999b6b563cSPaul Mackerras 			u32	real_size;
11009b6b563cSPaul Mackerras 			u32	virt_base;
11019b6b563cSPaul Mackerras 			u32	virt_size;
11029b6b563cSPaul Mackerras 			u32	load_base;
11039b6b563cSPaul Mackerras 		} chrpdesc;
11049b6b563cSPaul Mackerras 	} chrpnote;
11059b6b563cSPaul Mackerras 	struct rpanote {
11069b6b563cSPaul Mackerras 		u32	namesz;
11079b6b563cSPaul Mackerras 		u32	descsz;
11089b6b563cSPaul Mackerras 		u32	type;
11099b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
11109b6b563cSPaul Mackerras 		struct rpadesc {
11119b6b563cSPaul Mackerras 			u32	lpar_affinity;
11129b6b563cSPaul Mackerras 			u32	min_rmo_size;
11139b6b563cSPaul Mackerras 			u32	min_rmo_percent;
11149b6b563cSPaul Mackerras 			u32	max_pft_size;
11159b6b563cSPaul Mackerras 			u32	splpar;
11169b6b563cSPaul Mackerras 			u32	min_load;
11179b6b563cSPaul Mackerras 			u32	new_mem_def;
11189b6b563cSPaul Mackerras 			u32	ignore_me;
11199b6b563cSPaul Mackerras 		} rpadesc;
11209b6b563cSPaul Mackerras 	} rpanote;
1121d00e34b9SBenjamin Herrenschmidt } fake_elf __initconst = {
11229b6b563cSPaul Mackerras 	.elfhdr = {
11239b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
11249b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
11259b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
11269b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
11279b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
11289b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
11299b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
11309b6b563cSPaul Mackerras 		.e_phnum = 2
11319b6b563cSPaul Mackerras 	},
11329b6b563cSPaul Mackerras 	.phdr = {
11339b6b563cSPaul Mackerras 		[0] = {
11349b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
11359b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
11369b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
11379b6b563cSPaul Mackerras 		}, [1] = {
11389b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
11399b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
11409b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
11419b6b563cSPaul Mackerras 		}
11429b6b563cSPaul Mackerras 	},
11439b6b563cSPaul Mackerras 	.chrpnote = {
11449b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
11459b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
11469b6b563cSPaul Mackerras 		.type = 0x1275,
11479b6b563cSPaul Mackerras 		.name = "PowerPC",
11489b6b563cSPaul Mackerras 		.chrpdesc = {
11499b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
11509b6b563cSPaul Mackerras 			.real_base = ~0U,
11519b6b563cSPaul Mackerras 			.real_size = ~0U,
11529b6b563cSPaul Mackerras 			.virt_base = ~0U,
11539b6b563cSPaul Mackerras 			.virt_size = ~0U,
11549b6b563cSPaul Mackerras 			.load_base = ~0U
11559b6b563cSPaul Mackerras 		},
11569b6b563cSPaul Mackerras 	},
11579b6b563cSPaul Mackerras 	.rpanote = {
11589b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
11599b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
11609b6b563cSPaul Mackerras 		.type = 0x12759999,
11619b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
11629b6b563cSPaul Mackerras 		.rpadesc = {
11635663a123SPaul Mackerras 			.lpar_affinity = 0,
11645663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
11659b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
11665663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
11679b6b563cSPaul Mackerras 			.splpar = 1,
11689b6b563cSPaul Mackerras 			.min_load = ~0U,
11695663a123SPaul Mackerras 			.new_mem_def = 0
11709b6b563cSPaul Mackerras 		}
11719b6b563cSPaul Mackerras 	}
11729b6b563cSPaul Mackerras };
1173493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
11749b6b563cSPaul Mackerras 
1175efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
1176efec959fSBenjamin Herrenschmidt {
1177efec959fSBenjamin Herrenschmidt 	phandle node;
1178efec959fSBenjamin Herrenschmidt 	char type[64];
1179efec959fSBenjamin Herrenschmidt 	unsigned int plen;
1180efec959fSBenjamin Herrenschmidt 
1181efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
1182efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
1183efec959fSBenjamin Herrenschmidt 		type[0] = 0;
1184efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
1185efec959fSBenjamin Herrenschmidt 
1186450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "cpu"))
1187efec959fSBenjamin Herrenschmidt 			continue;
1188efec959fSBenjamin Herrenschmidt 		/*
1189efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
1190efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
1191efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
1192efec959fSBenjamin Herrenschmidt 		 */
1193efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
1194efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
1195efec959fSBenjamin Herrenschmidt 			break;
1196efec959fSBenjamin Herrenschmidt 		plen >>= 2;
11972c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
1198efec959fSBenjamin Herrenschmidt 
1199efec959fSBenjamin Herrenschmidt 		/* Sanity check */
1200efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
12012c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
1202efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
1203efec959fSBenjamin Herrenschmidt 			return 1;
1204efec959fSBenjamin Herrenschmidt 		}
1205efec959fSBenjamin Herrenschmidt 		return plen;
1206efec959fSBenjamin Herrenschmidt 	}
1207efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
1208efec959fSBenjamin Herrenschmidt 
1209efec959fSBenjamin Herrenschmidt 	return 1;
1210efec959fSBenjamin Herrenschmidt 
1211efec959fSBenjamin Herrenschmidt }
1212efec959fSBenjamin Herrenschmidt 
1213014d02cbSSuraj Jitindar Singh static void __init prom_parse_mmu_model(u8 val,
1214014d02cbSSuraj Jitindar Singh 					struct platform_support *support)
1215014d02cbSSuraj Jitindar Singh {
1216014d02cbSSuraj Jitindar Singh 	switch (val) {
1217014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_DYNAMIC):
1218014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_EITHER): /* Either Available */
1219014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - either supported\n");
1220014d02cbSSuraj Jitindar Singh 		support->radix_mmu = !prom_radix_disable;
1221014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1222014d02cbSSuraj Jitindar Singh 		break;
1223014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */
1224014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - radix only\n");
1225014d02cbSSuraj Jitindar Singh 		if (prom_radix_disable) {
1226014d02cbSSuraj Jitindar Singh 			/*
1227014d02cbSSuraj Jitindar Singh 			 * If we __have__ to do radix, we're better off ignoring
1228014d02cbSSuraj Jitindar Singh 			 * the command line rather than not booting.
1229014d02cbSSuraj Jitindar Singh 			 */
1230014d02cbSSuraj Jitindar Singh 			prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
1231014d02cbSSuraj Jitindar Singh 		}
1232014d02cbSSuraj Jitindar Singh 		support->radix_mmu = true;
1233014d02cbSSuraj Jitindar Singh 		break;
1234014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_HASH):
1235014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - hash only\n");
1236014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1237014d02cbSSuraj Jitindar Singh 		break;
1238014d02cbSSuraj Jitindar Singh 	default:
1239014d02cbSSuraj Jitindar Singh 		prom_debug("Unknown mmu support option: 0x%x\n", val);
1240014d02cbSSuraj Jitindar Singh 		break;
1241014d02cbSSuraj Jitindar Singh 	}
1242014d02cbSSuraj Jitindar Singh }
1243014d02cbSSuraj Jitindar Singh 
1244ac5e5a54SCédric Le Goater static void __init prom_parse_xive_model(u8 val,
1245ac5e5a54SCédric Le Goater 					 struct platform_support *support)
1246ac5e5a54SCédric Le Goater {
1247ac5e5a54SCédric Le Goater 	switch (val) {
1248ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EITHER): /* Either Available */
1249ac5e5a54SCédric Le Goater 		prom_debug("XIVE - either mode supported\n");
1250a3bf9fbdSGreg Kurz 		support->xive = !prom_xive_disable;
1251ac5e5a54SCédric Le Goater 		break;
1252ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EXPLOIT): /* Only Exploitation mode */
1253ac5e5a54SCédric Le Goater 		prom_debug("XIVE - exploitation mode supported\n");
1254a3bf9fbdSGreg Kurz 		if (prom_xive_disable) {
1255a3bf9fbdSGreg Kurz 			/*
1256a3bf9fbdSGreg Kurz 			 * If we __have__ to do XIVE, we're better off ignoring
1257a3bf9fbdSGreg Kurz 			 * the command line rather than not booting.
1258a3bf9fbdSGreg Kurz 			 */
1259a3bf9fbdSGreg Kurz 			prom_printf("WARNING: Ignoring cmdline option xive=off\n");
1260a3bf9fbdSGreg Kurz 		}
1261ac5e5a54SCédric Le Goater 		support->xive = true;
1262ac5e5a54SCédric Le Goater 		break;
1263ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_LEGACY): /* Only Legacy mode */
1264ac5e5a54SCédric Le Goater 		prom_debug("XIVE - legacy mode supported\n");
1265ac5e5a54SCédric Le Goater 		break;
1266ac5e5a54SCédric Le Goater 	default:
1267ac5e5a54SCédric Le Goater 		prom_debug("Unknown xive support option: 0x%x\n", val);
1268ac5e5a54SCédric Le Goater 		break;
1269ac5e5a54SCédric Le Goater 	}
1270ac5e5a54SCédric Le Goater }
1271ac5e5a54SCédric Le Goater 
1272014d02cbSSuraj Jitindar Singh static void __init prom_parse_platform_support(u8 index, u8 val,
1273014d02cbSSuraj Jitindar Singh 					       struct platform_support *support)
1274014d02cbSSuraj Jitindar Singh {
1275014d02cbSSuraj Jitindar Singh 	switch (index) {
1276014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_MMU_SUPPORT): /* MMU Model */
1277014d02cbSSuraj Jitindar Singh 		prom_parse_mmu_model(val & OV5_FEAT(OV5_MMU_SUPPORT), support);
1278014d02cbSSuraj Jitindar Singh 		break;
1279014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_RADIX_GTSE): /* Radix Extensions */
1280014d02cbSSuraj Jitindar Singh 		if (val & OV5_FEAT(OV5_RADIX_GTSE)) {
1281014d02cbSSuraj Jitindar Singh 			prom_debug("Radix - GTSE supported\n");
1282014d02cbSSuraj Jitindar Singh 			support->radix_gtse = true;
1283014d02cbSSuraj Jitindar Singh 		}
1284014d02cbSSuraj Jitindar Singh 		break;
1285ac5e5a54SCédric Le Goater 	case OV5_INDX(OV5_XIVE_SUPPORT): /* Interrupt mode */
1286ac5e5a54SCédric Le Goater 		prom_parse_xive_model(val & OV5_FEAT(OV5_XIVE_SUPPORT),
1287ac5e5a54SCédric Le Goater 				      support);
1288ac5e5a54SCédric Le Goater 		break;
1289014d02cbSSuraj Jitindar Singh 	}
1290014d02cbSSuraj Jitindar Singh }
1291014d02cbSSuraj Jitindar Singh 
1292014d02cbSSuraj Jitindar Singh static void __init prom_check_platform_support(void)
1293014d02cbSSuraj Jitindar Singh {
1294014d02cbSSuraj Jitindar Singh 	struct platform_support supported = {
1295014d02cbSSuraj Jitindar Singh 		.hash_mmu = false,
1296014d02cbSSuraj Jitindar Singh 		.radix_mmu = false,
1297ac5e5a54SCédric Le Goater 		.radix_gtse = false,
1298ac5e5a54SCédric Le Goater 		.xive = false
1299014d02cbSSuraj Jitindar Singh 	};
1300014d02cbSSuraj Jitindar Singh 	int prop_len = prom_getproplen(prom.chosen,
1301014d02cbSSuraj Jitindar Singh 				       "ibm,arch-vec-5-platform-support");
1302a614f52eSBenjamin Herrenschmidt 
1303adcf5918SChristophe Leroy 	/*
1304adcf5918SChristophe Leroy 	 * First copy the architecture vec template
1305adcf5918SChristophe Leroy 	 *
1306adcf5918SChristophe Leroy 	 * use memcpy() instead of *vec = *vec_template so that GCC replaces it
1307adcf5918SChristophe Leroy 	 * by __memcpy() when KASAN is active
1308adcf5918SChristophe Leroy 	 */
1309adcf5918SChristophe Leroy 	memcpy(&ibm_architecture_vec, &ibm_architecture_vec_template,
1310adcf5918SChristophe Leroy 	       sizeof(ibm_architecture_vec));
1311a614f52eSBenjamin Herrenschmidt 
1312014d02cbSSuraj Jitindar Singh 	if (prop_len > 1) {
1313014d02cbSSuraj Jitindar Singh 		int i;
1314ab912399SSuraj Jitindar Singh 		u8 vec[8];
1315014d02cbSSuraj Jitindar Singh 		prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1316014d02cbSSuraj Jitindar Singh 			   prop_len);
1317ab912399SSuraj Jitindar Singh 		if (prop_len > sizeof(vec))
1318ab912399SSuraj Jitindar Singh 			prom_printf("WARNING: ibm,arch-vec-5-platform-support longer than expected (len: %d)\n",
1319ab912399SSuraj Jitindar Singh 				    prop_len);
1320014d02cbSSuraj Jitindar Singh 		prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support",
1321014d02cbSSuraj Jitindar Singh 			     &vec, sizeof(vec));
1322ab912399SSuraj Jitindar Singh 		for (i = 0; i < sizeof(vec); i += 2) {
1323014d02cbSSuraj Jitindar Singh 			prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2
1324014d02cbSSuraj Jitindar Singh 								  , vec[i]
1325014d02cbSSuraj Jitindar Singh 								  , vec[i + 1]);
1326014d02cbSSuraj Jitindar Singh 			prom_parse_platform_support(vec[i], vec[i + 1],
1327014d02cbSSuraj Jitindar Singh 						    &supported);
1328014d02cbSSuraj Jitindar Singh 		}
1329014d02cbSSuraj Jitindar Singh 	}
1330014d02cbSSuraj Jitindar Singh 
133179b46868SAlexey Kardashevskiy 	if (supported.radix_mmu && supported.radix_gtse &&
133279b46868SAlexey Kardashevskiy 	    IS_ENABLED(CONFIG_PPC_RADIX_MMU)) {
1333014d02cbSSuraj Jitindar Singh 		/* Radix preferred - but we require GTSE for now */
1334014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for radix with GTSE\n");
1335014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1336014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.radix_ext = OV5_FEAT(OV5_RADIX_GTSE);
1337014d02cbSSuraj Jitindar Singh 	} else if (supported.hash_mmu) {
1338014d02cbSSuraj Jitindar Singh 		/* Default to hash mmu (if we can) */
1339014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for hash\n");
1340014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1341014d02cbSSuraj Jitindar Singh 	} else {
1342014d02cbSSuraj Jitindar Singh 		/* We're probably on a legacy hypervisor */
1343014d02cbSSuraj Jitindar Singh 		prom_debug("Assuming legacy hash support\n");
1344014d02cbSSuraj Jitindar Singh 	}
1345ac5e5a54SCédric Le Goater 
1346ac5e5a54SCédric Le Goater 	if (supported.xive) {
1347ac5e5a54SCédric Le Goater 		prom_debug("Asking for XIVE\n");
1348ac5e5a54SCédric Le Goater 		ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1349ac5e5a54SCédric Le Goater 	}
1350014d02cbSSuraj Jitindar Singh }
1351efec959fSBenjamin Herrenschmidt 
13529b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
13539b6b563cSPaul Mackerras {
1354493adffcSBenjamin Herrenschmidt 	ihandle root;
1355f709bfacSPaul Mackerras 	prom_arg_t ret;
1356dbd0c5d5SLaurent Dufour 	u32 cores;
13579b6b563cSPaul Mackerras 
1358014d02cbSSuraj Jitindar Singh 	/* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1359014d02cbSSuraj Jitindar Singh 	prom_check_platform_support();
1360014d02cbSSuraj Jitindar Singh 
1361f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
1362f709bfacSPaul Mackerras 	if (root != 0) {
1363efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
1364efec959fSBenjamin Herrenschmidt 		 *
1365efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
1366efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
1367efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
1368efec959fSBenjamin Herrenschmidt 		 */
1369dbd0c5d5SLaurent Dufour 
1370dbd0c5d5SLaurent Dufour 		cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1371eae5f709SMathieu Malaterre 		prom_printf("Max number of cores passed to firmware: %u (NR_CPUS = %d)\n",
1372dbd0c5d5SLaurent Dufour 			    cores, NR_CPUS);
137376ffb578SMichael Ellerman 
137476ffb578SMichael Ellerman 		ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1375efec959fSBenjamin Herrenschmidt 
1376f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
1377049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
1378f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
1379f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
138033b74977SBenjamin Herrenschmidt 				  root,
138176ffb578SMichael Ellerman 				  ADDR(&ibm_architecture_vec)) == 0) {
1382f709bfacSPaul Mackerras 			/* the call exists... */
1383f709bfacSPaul Mackerras 			if (ret)
13844da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
1385f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
1386f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
13874da727aeSAnton Blanchard 			prom_printf(" done\n");
1388f709bfacSPaul Mackerras 			return;
1389f709bfacSPaul Mackerras 		}
1390f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
1391049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
1392f709bfacSPaul Mackerras 	}
1393f709bfacSPaul Mackerras 
1394493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
1395493adffcSBenjamin Herrenschmidt 	{
1396493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
1397493adffcSBenjamin Herrenschmidt 
1398f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
1399493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
1400493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
14019b6b563cSPaul Mackerras 		if (elfloader == 0) {
14029b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
14039b6b563cSPaul Mackerras 			return;
14049b6b563cSPaul Mackerras 		}
14059b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
14069b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
14079b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
14089b6b563cSPaul Mackerras 	}
1409493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1410493adffcSBenjamin Herrenschmidt }
141111fdb309SBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
14129b6b563cSPaul Mackerras 
14139b6b563cSPaul Mackerras /*
14149b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
14159b6b563cSPaul Mackerras  *
14169b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
14179b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
14189b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
14199b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
14209b6b563cSPaul Mackerras  *  never happen.
14219b6b563cSPaul Mackerras  *
14229b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
14239b6b563cSPaul Mackerras  *  TCEs overlap
14249b6b563cSPaul Mackerras  *
14259b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
14269b6b563cSPaul Mackerras  *
14279b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
14289b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
14299b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
14309b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
14319b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
14329b6b563cSPaul Mackerras  *
14339b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
14349b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
14359b6b563cSPaul Mackerras  *  reserved.
14369b6b563cSPaul Mackerras  */
14379b6b563cSPaul Mackerras 
14389b6b563cSPaul Mackerras 
14399b6b563cSPaul Mackerras /*
14409b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
14419b6b563cSPaul Mackerras  *
14429b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
14439b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
14449b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
14459b6b563cSPaul Mackerras  */
14469b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
14479b6b563cSPaul Mackerras {
14485827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
14499b6b563cSPaul Mackerras 	unsigned long addr = 0;
14509b6b563cSPaul Mackerras 
1451c4988820SPaul Mackerras 	if (align)
1452c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
1453eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx)\n", __func__, size, align);
14545827d416SAnton Blanchard 	if (ram_top == 0)
14559b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
14569b6b563cSPaul Mackerras 
14579b6b563cSPaul Mackerras 	if (align)
14585827d416SAnton Blanchard 		base = _ALIGN_UP(alloc_bottom, align);
14599b6b563cSPaul Mackerras 	else
14605827d416SAnton Blanchard 		base = alloc_bottom;
14619b6b563cSPaul Mackerras 
14625827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
14639b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
1464eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
14659b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1466c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
14679b6b563cSPaul Mackerras 			break;
14689b6b563cSPaul Mackerras 		addr = 0;
14699b6b563cSPaul Mackerras 		if (align == 0)
14709b6b563cSPaul Mackerras 			break;
14719b6b563cSPaul Mackerras 	}
14729b6b563cSPaul Mackerras 	if (addr == 0)
14739b6b563cSPaul Mackerras 		return 0;
14745827d416SAnton Blanchard 	alloc_bottom = addr + size;
14759b6b563cSPaul Mackerras 
1476eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1477eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1478eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1479eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1480eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1481eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
14829b6b563cSPaul Mackerras 
14839b6b563cSPaul Mackerras 	return addr;
14849b6b563cSPaul Mackerras }
14859b6b563cSPaul Mackerras 
14869b6b563cSPaul Mackerras /*
14879b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
14889b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
14899b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
14909b6b563cSPaul Mackerras  */
14919b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
14929b6b563cSPaul Mackerras 				       int highmem)
14939b6b563cSPaul Mackerras {
14949b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
14959b6b563cSPaul Mackerras 
1496eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx, %s)\n", __func__, size, align,
14975827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
14985827d416SAnton Blanchard 	if (ram_top == 0)
14999b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
15009b6b563cSPaul Mackerras 
15019b6b563cSPaul Mackerras 	if (highmem) {
15029b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
15035827d416SAnton Blanchard 		addr = _ALIGN_DOWN(alloc_top_high - size, align);
15045827d416SAnton Blanchard 		if (addr <= alloc_bottom)
15059b6b563cSPaul Mackerras 			return 0;
15069b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
15079b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
15089b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
15099b6b563cSPaul Mackerras 		 */
15105827d416SAnton Blanchard 		if (addr < rmo_top) {
15119b6b563cSPaul Mackerras 			/* Good, we are first */
15125827d416SAnton Blanchard 			if (alloc_top == rmo_top)
15135827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
15149b6b563cSPaul Mackerras 			else
15159b6b563cSPaul Mackerras 				return 0;
15169b6b563cSPaul Mackerras 		}
15175827d416SAnton Blanchard 		alloc_top_high = addr;
15189b6b563cSPaul Mackerras 		goto bail;
15199b6b563cSPaul Mackerras 	}
15209b6b563cSPaul Mackerras 
15215827d416SAnton Blanchard 	base = _ALIGN_DOWN(alloc_top - size, align);
15225827d416SAnton Blanchard 	for (; base > alloc_bottom;
15239b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
1524eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
15259b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1526c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
15279b6b563cSPaul Mackerras 			break;
15289b6b563cSPaul Mackerras 		addr = 0;
15299b6b563cSPaul Mackerras 	}
15309b6b563cSPaul Mackerras 	if (addr == 0)
15319b6b563cSPaul Mackerras 		return 0;
15325827d416SAnton Blanchard 	alloc_top = addr;
15339b6b563cSPaul Mackerras 
15349b6b563cSPaul Mackerras  bail:
1535eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1536eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1537eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1538eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1539eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1540eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
15419b6b563cSPaul Mackerras 
15429b6b563cSPaul Mackerras 	return addr;
15439b6b563cSPaul Mackerras }
15449b6b563cSPaul Mackerras 
15459b6b563cSPaul Mackerras /*
15469b6b563cSPaul Mackerras  * Parse a "reg" cell
15479b6b563cSPaul Mackerras  */
15489b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
15499b6b563cSPaul Mackerras {
15509b6b563cSPaul Mackerras 	cell_t *p = *cellp;
15519b6b563cSPaul Mackerras 	unsigned long r = 0;
15529b6b563cSPaul Mackerras 
15539b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
15549b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
15559b6b563cSPaul Mackerras 		p++;
15569b6b563cSPaul Mackerras 		s--;
15579b6b563cSPaul Mackerras 	}
1558493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
15599b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
156035499c01SPaul Mackerras 	if (s > 1) {
15619b6b563cSPaul Mackerras 		r <<= 32;
1562493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
15639b6b563cSPaul Mackerras 	}
15649b6b563cSPaul Mackerras #endif
15659b6b563cSPaul Mackerras 	*cellp = p;
15669b6b563cSPaul Mackerras 	return r;
15679b6b563cSPaul Mackerras }
15689b6b563cSPaul Mackerras 
15699b6b563cSPaul Mackerras /*
15709b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
15719b6b563cSPaul Mackerras  * we don't need anything smarter at this point
15729b6b563cSPaul Mackerras  *
15739b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
15749b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
15759b6b563cSPaul Mackerras  * them down.
15769b6b563cSPaul Mackerras  */
15770108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
15789b6b563cSPaul Mackerras {
1579cbbcf340SKumar Gala 	u64 top = base + size;
15805827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
15819b6b563cSPaul Mackerras 
15829b6b563cSPaul Mackerras 	if (size == 0)
15839b6b563cSPaul Mackerras 		return;
15849b6b563cSPaul Mackerras 
15859b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
15869b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
15879b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
15889b6b563cSPaul Mackerras 	 */
15899b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
15909b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
15919b6b563cSPaul Mackerras 	size = top - base;
15929b6b563cSPaul Mackerras 
15939b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
15949b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1595493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1596493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
15975827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
15989b6b563cSPaul Mackerras }
15999b6b563cSPaul Mackerras 
16009b6b563cSPaul Mackerras /*
1601b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
16029b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
16039b6b563cSPaul Mackerras  */
16049b6b563cSPaul Mackerras static void __init prom_init_mem(void)
16059b6b563cSPaul Mackerras {
16069b6b563cSPaul Mackerras 	phandle node;
1607eab00a20SMathieu Malaterre 	char type[64];
16089b6b563cSPaul Mackerras 	unsigned int plen;
16099b6b563cSPaul Mackerras 	cell_t *p, *endp;
1610493adffcSBenjamin Herrenschmidt 	__be32 val;
16119b6b563cSPaul Mackerras 	u32 rac, rsc;
16129b6b563cSPaul Mackerras 
16139b6b563cSPaul Mackerras 	/*
16149b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
16159b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
16169b6b563cSPaul Mackerras 	 * 2) top of memory
16179b6b563cSPaul Mackerras 	 */
1618493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1619493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1620493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1621493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1622493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1623493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1624493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1625493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
16269b6b563cSPaul Mackerras 
16279b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
16289b6b563cSPaul Mackerras 
16299b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
16309b6b563cSPaul Mackerras 		type[0] = 0;
16319b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
16329b6b563cSPaul Mackerras 
1633c4988820SPaul Mackerras 		if (type[0] == 0) {
1634c4988820SPaul Mackerras 			/*
1635c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1636c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1637c4988820SPaul Mackerras 			 */
1638c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1639c4988820SPaul Mackerras 		}
1640450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "memory"))
16419b6b563cSPaul Mackerras 			continue;
16429b6b563cSPaul Mackerras 
16435827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
16449b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
16459b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
16469b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
16479b6b563cSPaul Mackerras 		}
16485827d416SAnton Blanchard 		p = regbuf;
16499b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
16509b6b563cSPaul Mackerras 
16519b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1652c806a6fdSMathieu Malaterre 		memset(prom_scratch, 0, sizeof(prom_scratch));
1653c806a6fdSMathieu Malaterre 		call_prom("package-to-path", 3, 1, node, prom_scratch,
1654c806a6fdSMathieu Malaterre 			  sizeof(prom_scratch) - 1);
1655c806a6fdSMathieu Malaterre 		prom_debug("  node %s :\n", prom_scratch);
16569b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
16579b6b563cSPaul Mackerras 
16589b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
16599b6b563cSPaul Mackerras 			unsigned long base, size;
16609b6b563cSPaul Mackerras 
16619b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
16629b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
16639b6b563cSPaul Mackerras 
16649b6b563cSPaul Mackerras 			if (size == 0)
16659b6b563cSPaul Mackerras 				continue;
1666eae5f709SMathieu Malaterre 			prom_debug("    %lx %lx\n", base, size);
16675827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
16685827d416SAnton Blanchard 				rmo_top = size;
16695827d416SAnton Blanchard 			if ((base + size) > ram_top)
16705827d416SAnton Blanchard 				ram_top = base + size;
16719b6b563cSPaul Mackerras 		}
16729b6b563cSPaul Mackerras 	}
16739b6b563cSPaul Mackerras 
16745827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
16759b6b563cSPaul Mackerras 
16769b6b563cSPaul Mackerras 	/*
1677cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1678cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1679cf68787bSBenjamin Krill 	 * TCE's up there.
1680cf68787bSBenjamin Krill 	 */
1681cf68787bSBenjamin Krill 
16825827d416SAnton Blanchard 	alloc_top_high = ram_top;
1683cf68787bSBenjamin Krill 
16845827d416SAnton Blanchard 	if (prom_memory_limit) {
16855827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1686eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx <= alloc_bottom.\n",
16875827d416SAnton Blanchard 				    prom_memory_limit);
16885827d416SAnton Blanchard 			prom_memory_limit = 0;
16895827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1690eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx >= ram_top.\n",
16915827d416SAnton Blanchard 				    prom_memory_limit);
16925827d416SAnton Blanchard 			prom_memory_limit = 0;
1693cf68787bSBenjamin Krill 		} else {
16945827d416SAnton Blanchard 			ram_top = prom_memory_limit;
16955827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1696cf68787bSBenjamin Krill 		}
1697cf68787bSBenjamin Krill 	}
1698cf68787bSBenjamin Krill 
1699cf68787bSBenjamin Krill 	/*
17009b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
17019b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
17029b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
17039b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
17049b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
17059b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
17069b6b563cSPaul Mackerras 	 */
17075827d416SAnton Blanchard 	if (!rmo_top)
17085827d416SAnton Blanchard 		rmo_top = ram_top;
17095827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
17105827d416SAnton Blanchard 	alloc_top = rmo_top;
17115827d416SAnton Blanchard 	alloc_top_high = ram_top;
17129b6b563cSPaul Mackerras 
171364968f60SPaul Mackerras 	/*
171464968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
171564968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
171664968f60SPaul Mackerras 	 */
17175827d416SAnton Blanchard 	if (prom_initrd_start &&
17185827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
17195827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
17205827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
172164968f60SPaul Mackerras 
17229b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
1723eae5f709SMathieu Malaterre 	prom_printf("  memory_limit : %lx (16 MB aligned)\n",
1724eae5f709SMathieu Malaterre 		    prom_memory_limit);
1725eae5f709SMathieu Malaterre 	prom_printf("  alloc_bottom : %lx\n", alloc_bottom);
1726eae5f709SMathieu Malaterre 	prom_printf("  alloc_top    : %lx\n", alloc_top);
1727eae5f709SMathieu Malaterre 	prom_printf("  alloc_top_hi : %lx\n", alloc_top_high);
1728eae5f709SMathieu Malaterre 	prom_printf("  rmo_top      : %lx\n", rmo_top);
1729eae5f709SMathieu Malaterre 	prom_printf("  ram_top      : %lx\n", ram_top);
17309b6b563cSPaul Mackerras }
17319b6b563cSPaul Mackerras 
173227f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
173327f44888SBenjamin Herrenschmidt {
1734493adffcSBenjamin Herrenschmidt 	__be32 val;
1735493adffcSBenjamin Herrenschmidt 	ihandle stdin;
173627f44888SBenjamin Herrenschmidt 
1737493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1738493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1739493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1740493adffcSBenjamin Herrenschmidt 	}
174127f44888SBenjamin Herrenschmidt }
174227f44888SBenjamin Herrenschmidt 
17436a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
17446a9c930bSRam Pai static int prom_rtas_hcall(uint64_t args)
17456a9c930bSRam Pai {
17466a9c930bSRam Pai 	register uint64_t arg1 asm("r3") = H_RTAS;
17476a9c930bSRam Pai 	register uint64_t arg2 asm("r4") = args;
17486a9c930bSRam Pai 
17496a9c930bSRam Pai 	asm volatile("sc 1\n" : "=r" (arg1) :
17506a9c930bSRam Pai 			"r" (arg1),
17516a9c930bSRam Pai 			"r" (arg2) :);
17526a9c930bSRam Pai 	return arg1;
17536a9c930bSRam Pai }
17546a9c930bSRam Pai 
17556a9c930bSRam Pai static struct rtas_args __prombss os_term_args;
17566a9c930bSRam Pai 
17576a9c930bSRam Pai static void __init prom_rtas_os_term(char *str)
17586a9c930bSRam Pai {
17596a9c930bSRam Pai 	phandle rtas_node;
17606a9c930bSRam Pai 	__be32 val;
17616a9c930bSRam Pai 	u32 token;
17626a9c930bSRam Pai 
17636a9c930bSRam Pai 	prom_debug("%s: start...\n", __func__);
17646a9c930bSRam Pai 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
17656a9c930bSRam Pai 	prom_debug("rtas_node: %x\n", rtas_node);
17666a9c930bSRam Pai 	if (!PHANDLE_VALID(rtas_node))
17676a9c930bSRam Pai 		return;
17686a9c930bSRam Pai 
17696a9c930bSRam Pai 	val = 0;
17706a9c930bSRam Pai 	prom_getprop(rtas_node, "ibm,os-term", &val, sizeof(val));
17716a9c930bSRam Pai 	token = be32_to_cpu(val);
17726a9c930bSRam Pai 	prom_debug("ibm,os-term: %x\n", token);
17736a9c930bSRam Pai 	if (token == 0)
17746a9c930bSRam Pai 		prom_panic("Could not get token for ibm,os-term\n");
17756a9c930bSRam Pai 	os_term_args.token = cpu_to_be32(token);
1776*74bb84e5SAlexey Kardashevskiy 	os_term_args.nargs = cpu_to_be32(1);
1777*74bb84e5SAlexey Kardashevskiy 	os_term_args.nret = cpu_to_be32(1);
1778*74bb84e5SAlexey Kardashevskiy 	os_term_args.args[0] = cpu_to_be32(__pa(str));
17796a9c930bSRam Pai 	prom_rtas_hcall((uint64_t)&os_term_args);
17806a9c930bSRam Pai }
17816a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
17826a9c930bSRam Pai 
17839b6b563cSPaul Mackerras /*
17849b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
17859b6b563cSPaul Mackerras  */
17869b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
17879b6b563cSPaul Mackerras {
17889b6b563cSPaul Mackerras 	phandle rtas_node;
17899b6b563cSPaul Mackerras 	ihandle rtas_inst;
17909b6b563cSPaul Mackerras 	u32 base, entry = 0;
1791493adffcSBenjamin Herrenschmidt 	__be32 val;
17929b6b563cSPaul Mackerras 	u32 size = 0;
17939b6b563cSPaul Mackerras 
17949b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
17959b6b563cSPaul Mackerras 
17969b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
17979b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
17989b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
17999b6b563cSPaul Mackerras 		return;
18009b6b563cSPaul Mackerras 
1801493adffcSBenjamin Herrenschmidt 	val = 0;
1802493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1803493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
18049b6b563cSPaul Mackerras 	if (size == 0)
18059b6b563cSPaul Mackerras 		return;
18069b6b563cSPaul Mackerras 
18079b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
18086d1e2c6cSAnton Blanchard 	if (base == 0)
18096d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
18109b6b563cSPaul Mackerras 
18119b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
18129b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1813a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
18149b6b563cSPaul Mackerras 		return;
18159b6b563cSPaul Mackerras 	}
18169b6b563cSPaul Mackerras 
18179b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
18189b6b563cSPaul Mackerras 
18199b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
18209b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1821a23414beSPaul Mackerras 			  rtas_inst, base) != 0
18229b6b563cSPaul Mackerras 	    || entry == 0) {
18239b6b563cSPaul Mackerras 		prom_printf(" failed\n");
18249b6b563cSPaul Mackerras 		return;
18259b6b563cSPaul Mackerras 	}
18269b6b563cSPaul Mackerras 	prom_printf(" done\n");
18279b6b563cSPaul Mackerras 
18289b6b563cSPaul Mackerras 	reserve_mem(base, size);
18299b6b563cSPaul Mackerras 
1830493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1831a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1832493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1833493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1834a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1835493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
18369b6b563cSPaul Mackerras 
1837dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1838dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1839dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1840dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1841dbe78b40SBenjamin Herrenschmidt 
18429b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
18439b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
1844eae5f709SMathieu Malaterre 	prom_debug("rtas size     = 0x%x\n", size);
18459b6b563cSPaul Mackerras 
18469b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
18479b6b563cSPaul Mackerras }
18489b6b563cSPaul Mackerras 
18499b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
18509b6b563cSPaul Mackerras /*
18514a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
18524a727429SAshley Lai  */
18534a727429SAshley Lai static void __init prom_instantiate_sml(void)
18544a727429SAshley Lai {
18554a727429SAshley Lai 	phandle ibmvtpm_node;
18564a727429SAshley Lai 	ihandle ibmvtpm_inst;
1857b4ed0469SHon Ching \(Vicky\) Lo 	u32 entry = 0, size = 0, succ = 0;
18584a727429SAshley Lai 	u64 base;
1859b4ed0469SHon Ching \(Vicky\) Lo 	__be32 val;
18604a727429SAshley Lai 
18614a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
18624a727429SAshley Lai 
18632f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
18644a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
18654a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
18664a727429SAshley Lai 		return;
18674a727429SAshley Lai 
18682f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
18694a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
18704a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
18714a727429SAshley Lai 		return;
18724a727429SAshley Lai 	}
18734a727429SAshley Lai 
1874b4ed0469SHon Ching \(Vicky\) Lo 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1875b4ed0469SHon Ching \(Vicky\) Lo 			 &val, sizeof(val)) != PROM_ERROR) {
1876b4ed0469SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &succ,
1877b4ed0469SHon Ching \(Vicky\) Lo 				  ADDR("reformat-sml-to-efi-alignment"),
1878b4ed0469SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || succ == 0) {
1879b4ed0469SHon Ching \(Vicky\) Lo 			prom_printf("Reformat SML to EFI alignment failed\n");
1880b4ed0469SHon Ching \(Vicky\) Lo 			return;
1881b4ed0469SHon Ching \(Vicky\) Lo 		}
1882b4ed0469SHon Ching \(Vicky\) Lo 
18834a727429SAshley Lai 		if (call_prom_ret("call-method", 2, 2, &size,
18849e5d4af4SHon Ching \(Vicky\) Lo 				  ADDR("sml-get-allocated-size"),
18859e5d4af4SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || size == 0) {
18869e5d4af4SHon Ching \(Vicky\) Lo 			prom_printf("SML get allocated size failed\n");
18879e5d4af4SHon Ching \(Vicky\) Lo 			return;
18889e5d4af4SHon Ching \(Vicky\) Lo 		}
18899e5d4af4SHon Ching \(Vicky\) Lo 	} else {
18909e5d4af4SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &size,
18914a727429SAshley Lai 				  ADDR("sml-get-handover-size"),
18924a727429SAshley Lai 				  ibmvtpm_inst) != 0 || size == 0) {
18934a727429SAshley Lai 			prom_printf("SML get handover size failed\n");
18944a727429SAshley Lai 			return;
18954a727429SAshley Lai 		}
18969e5d4af4SHon Ching \(Vicky\) Lo 	}
18974a727429SAshley Lai 
18984a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
18994a727429SAshley Lai 	if (base == 0)
19004a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
19014a727429SAshley Lai 
1902eae5f709SMathieu Malaterre 	prom_printf("instantiating sml at 0x%llx...", base);
19034a727429SAshley Lai 
19049e5d4af4SHon Ching \(Vicky\) Lo 	memset((void *)base, 0, size);
19059e5d4af4SHon Ching \(Vicky\) Lo 
19064a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
19074a727429SAshley Lai 			  ADDR("sml-handover"),
19084a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
19094a727429SAshley Lai 		prom_printf("SML handover failed\n");
19104a727429SAshley Lai 		return;
19114a727429SAshley Lai 	}
19124a727429SAshley Lai 	prom_printf(" done\n");
19134a727429SAshley Lai 
19144a727429SAshley Lai 	reserve_mem(base, size);
19154a727429SAshley Lai 
19162f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
19174a727429SAshley Lai 		     &base, sizeof(base));
19182f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
19194a727429SAshley Lai 		     &size, sizeof(size));
19204a727429SAshley Lai 
1921eae5f709SMathieu Malaterre 	prom_debug("sml base     = 0x%llx\n", base);
1922eae5f709SMathieu Malaterre 	prom_debug("sml size     = 0x%x\n", size);
19234a727429SAshley Lai 
19244a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
19254a727429SAshley Lai }
19264a727429SAshley Lai 
19274a727429SAshley Lai /*
19289b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
19299b6b563cSPaul Mackerras  */
1930493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
19319b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
19329b6b563cSPaul Mackerras {
19339b6b563cSPaul Mackerras 	phandle node;
19349b6b563cSPaul Mackerras 	ihandle phb_node;
19359b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
19365827d416SAnton Blanchard 	char *path = prom_scratch;
19379b6b563cSPaul Mackerras 	u64 base, align;
19389b6b563cSPaul Mackerras 	u32 minalign, minsize;
19399b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
19409b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
19419b6b563cSPaul Mackerras 	u64 i;
19429b6b563cSPaul Mackerras 
19435827d416SAnton Blanchard 	if (prom_iommu_off)
19449b6b563cSPaul Mackerras 		return;
19459b6b563cSPaul Mackerras 
19469b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
19479b6b563cSPaul Mackerras 
19489b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
19495827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
19509b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
19519b6b563cSPaul Mackerras 
19529b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
19539b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
19549b6b563cSPaul Mackerras 		compatible[0] = 0;
19559b6b563cSPaul Mackerras 		type[0] = 0;
19569b6b563cSPaul Mackerras 		model[0] = 0;
19579b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
19589b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
19599b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
19609b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
19619b6b563cSPaul Mackerras 
1962450e7dd4SChristophe Leroy 		if ((type[0] == 0) || (prom_strstr(type, "pci") == NULL))
19639b6b563cSPaul Mackerras 			continue;
19649b6b563cSPaul Mackerras 
1965e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
19669b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
1967450e7dd4SChristophe Leroy 			if ((prom_strstr(compatible, "python") == NULL) &&
1968450e7dd4SChristophe Leroy 			    (prom_strstr(compatible, "Speedwagon") == NULL) &&
1969450e7dd4SChristophe Leroy 			    (prom_strstr(compatible, "Winnipeg") == NULL))
19709b6b563cSPaul Mackerras 				continue;
19719b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
1972450e7dd4SChristophe Leroy 			if ((prom_strstr(model, "ython") == NULL) &&
1973450e7dd4SChristophe Leroy 			    (prom_strstr(model, "peedwagon") == NULL) &&
1974450e7dd4SChristophe Leroy 			    (prom_strstr(model, "innipeg") == NULL))
19759b6b563cSPaul Mackerras 				continue;
19769b6b563cSPaul Mackerras 		}
19779b6b563cSPaul Mackerras 
19789b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
19799b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
19809b6b563cSPaul Mackerras 			minalign = 0;
19819b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
19829b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
19839b6b563cSPaul Mackerras 			minsize = 4UL << 20;
19849b6b563cSPaul Mackerras 
19859b6b563cSPaul Mackerras 		/*
19869b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
19879b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
19889b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
19899b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
19909b6b563cSPaul Mackerras 		 */
19919b6b563cSPaul Mackerras 		minsize = 4UL << 20;
19929b6b563cSPaul Mackerras 
19939b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
19949b6b563cSPaul Mackerras 		align = max(minalign, minsize);
19959b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
19969b6b563cSPaul Mackerras 		if (base == 0)
19979b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
19989b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
19999b6b563cSPaul Mackerras 			local_alloc_bottom = base;
20009b6b563cSPaul Mackerras 
20019b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
2002d7fbe2a0SChristophe Leroy 		memset(path, 0, sizeof(prom_scratch));
20039b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
20049b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
2005d7fbe2a0SChristophe Leroy 			      path, sizeof(prom_scratch) - 1) == PROM_ERROR) {
20069b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
20079b6b563cSPaul Mackerras 		}
20089b6b563cSPaul Mackerras 
2009a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
2010a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
2011a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
2012a23414beSPaul Mackerras 
20139b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
20149b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
2015eae5f709SMathieu Malaterre 		prom_debug("\tbase = 0x%llx\n", base);
20169b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
20179b6b563cSPaul Mackerras 
20189b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
20199b6b563cSPaul Mackerras 		 * over the allocated size.
20209b6b563cSPaul Mackerras 		 */
20212b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
20229b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
20239b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
20249b6b563cSPaul Mackerras 			tce_entry |= 0x3;
20259b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
20269b6b563cSPaul Mackerras 		}
20279b6b563cSPaul Mackerras 
20289b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
20299b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
20309b6b563cSPaul Mackerras 		if (phb_node == 0)
20319b6b563cSPaul Mackerras 			prom_printf("... failed\n");
20329b6b563cSPaul Mackerras 		else
20339b6b563cSPaul Mackerras 			prom_printf("... done\n");
20349b6b563cSPaul Mackerras 
20359b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
20369b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
20379b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
20389b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
20399b6b563cSPaul Mackerras 	}
20409b6b563cSPaul Mackerras 
20419b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
20429b6b563cSPaul Mackerras 
20432babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
20442babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
20455827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
20465827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
20479b6b563cSPaul Mackerras 
20489b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
20499b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
20509b6b563cSPaul Mackerras }
2051493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
2052493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
20539b6b563cSPaul Mackerras 
20549b6b563cSPaul Mackerras /*
20559b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
20569b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
20579b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
20589b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
20599b6b563cSPaul Mackerras  *
20609b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
20619b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
20629b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
20639b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
20649b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
20659b6b563cSPaul Mackerras  * of setting those values.
20669b6b563cSPaul Mackerras  *
20679b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
20689b6b563cSPaul Mackerras  * is in its holding pattern code.
20699b6b563cSPaul Mackerras  *
20709b6b563cSPaul Mackerras  * -- Cort
20719b6b563cSPaul Mackerras  */
2072bbd0abdaSPaul Mackerras /*
2073bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
2074bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
2075bbd0abdaSPaul Mackerras  */
2076bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
2077bbd0abdaSPaul Mackerras 
20789b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
20799b6b563cSPaul Mackerras {
20809b6b563cSPaul Mackerras 	unsigned long i;
20819b6b563cSPaul Mackerras 	phandle node;
20829b6b563cSPaul Mackerras 	char type[64];
20839b6b563cSPaul Mackerras 	unsigned long *spinloop
2084bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
20859b6b563cSPaul Mackerras 	unsigned long *acknowledge
2086bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
2087bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
20889b6b563cSPaul Mackerras 
2089dbe78b40SBenjamin Herrenschmidt 	/*
2090dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
2091dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
2092dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
2093dbe78b40SBenjamin Herrenschmidt 	 */
2094dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
2095dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
2096dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
2097dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
2098dbe78b40SBenjamin Herrenschmidt 		return;
2099dbe78b40SBenjamin Herrenschmidt 	}
2100dbe78b40SBenjamin Herrenschmidt 
21019b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
2102eae5f709SMathieu Malaterre 	prom_debug("    1) spinloop       = 0x%lx\n", (unsigned long)spinloop);
2103eae5f709SMathieu Malaterre 	prom_debug("    1) *spinloop      = 0x%lx\n", *spinloop);
2104eae5f709SMathieu Malaterre 	prom_debug("    1) acknowledge    = 0x%lx\n",
21059b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
2106eae5f709SMathieu Malaterre 	prom_debug("    1) *acknowledge   = 0x%lx\n", *acknowledge);
2107eae5f709SMathieu Malaterre 	prom_debug("    1) secondary_hold = 0x%lx\n", secondary_hold);
21089b6b563cSPaul Mackerras 
21099b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
21109b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
21119b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
21129b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
21139b6b563cSPaul Mackerras 	 */
21149b6b563cSPaul Mackerras 	*spinloop = 0;
21159b6b563cSPaul Mackerras 
21169b6b563cSPaul Mackerras 	/* look for cpus */
21179b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
2118493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
2119493adffcSBenjamin Herrenschmidt 		__be32 reg;
2120493adffcSBenjamin Herrenschmidt 
21219b6b563cSPaul Mackerras 		type[0] = 0;
21229b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
2123450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "cpu") != 0)
21249b6b563cSPaul Mackerras 			continue;
21259b6b563cSPaul Mackerras 
21269b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
21279b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
2128450e7dd4SChristophe Leroy 			if (prom_strcmp(type, "okay") != 0)
21299b6b563cSPaul Mackerras 				continue;
21309b6b563cSPaul Mackerras 
2131493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
21329b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
2133493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
21349b6b563cSPaul Mackerras 
2135eae5f709SMathieu Malaterre 		prom_debug("cpu hw idx   = %u\n", cpu_no);
21369b6b563cSPaul Mackerras 
21379b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
21389b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
21399b6b563cSPaul Mackerras 		 * spinloop.
21409b6b563cSPaul Mackerras 		 */
21419b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
21429b6b563cSPaul Mackerras 
2143493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
214427f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
2145eae5f709SMathieu Malaterre 			prom_printf("starting cpu hw idx %u... ", cpu_no);
21469b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
2147493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
21489b6b563cSPaul Mackerras 
21499b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
21509b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
21519b6b563cSPaul Mackerras 				mb();
21529b6b563cSPaul Mackerras 
2153493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
21549b6b563cSPaul Mackerras 				prom_printf("done\n");
2155bbd0abdaSPaul Mackerras 			else
2156eae5f709SMathieu Malaterre 				prom_printf("failed: %lx\n", *acknowledge);
21579b6b563cSPaul Mackerras 		}
21589b6b563cSPaul Mackerras #ifdef CONFIG_SMP
21599b6b563cSPaul Mackerras 		else
2160eae5f709SMathieu Malaterre 			prom_printf("boot cpu hw idx %u\n", cpu_no);
21619b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
21629b6b563cSPaul Mackerras 	}
21639b6b563cSPaul Mackerras 
21649b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
21659b6b563cSPaul Mackerras }
21669b6b563cSPaul Mackerras 
21679b6b563cSPaul Mackerras 
21689b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
21699b6b563cSPaul Mackerras {
21709b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
21715827d416SAnton Blanchard 	prom_entry = pp;
21729b6b563cSPaul Mackerras 
21739b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
21745827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
21755827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
21769b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
21779b6b563cSPaul Mackerras 
21789b6b563cSPaul Mackerras 	/* get device tree root */
21795827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
21805827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
21819b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
2182a575b807SPaul Mackerras 
21835827d416SAnton Blanchard 	prom.mmumap = 0;
21849b6b563cSPaul Mackerras }
21859b6b563cSPaul Mackerras 
2186a575b807SPaul Mackerras #ifdef CONFIG_PPC32
2187a575b807SPaul Mackerras /*
2188a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
2189a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
2190a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
2191a575b807SPaul Mackerras  */
2192a575b807SPaul Mackerras static void __init prom_find_mmu(void)
2193a575b807SPaul Mackerras {
2194a575b807SPaul Mackerras 	phandle oprom;
2195a575b807SPaul Mackerras 	char version[64];
2196a575b807SPaul Mackerras 
2197a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
2198a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
2199a575b807SPaul Mackerras 		return;
2200a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
2201a575b807SPaul Mackerras 		return;
2202a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
2203a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
2204450e7dd4SChristophe Leroy 	if (prom_strcmp(version, "Open Firmware, 1.0.5") == 0)
2205a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
2206450e7dd4SChristophe Leroy 	else if (prom_strncmp(version, "FirmWorks,3.", 12) == 0) {
2207a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
2208a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2209a23414beSPaul Mackerras 	} else
2210a575b807SPaul Mackerras 		return;
22115827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
22125827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
22135827d416SAnton Blanchard 		     sizeof(prom.mmumap));
2214493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
22155827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2216a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
2217a575b807SPaul Mackerras }
2218a575b807SPaul Mackerras #else
2219a575b807SPaul Mackerras #define prom_find_mmu()
2220a575b807SPaul Mackerras #endif
2221a575b807SPaul Mackerras 
22229b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
22239b6b563cSPaul Mackerras {
22245827d416SAnton Blanchard 	char *path = of_stdout_device;
22259b6b563cSPaul Mackerras 	char type[16];
2226493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
2227493adffcSBenjamin Herrenschmidt 	__be32 val;
22289b6b563cSPaul Mackerras 
22295827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
22309b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
22319b6b563cSPaul Mackerras 
2232493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
22339b6b563cSPaul Mackerras 
22349b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
22359b6b563cSPaul Mackerras 	memset(path, 0, 256);
22365827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
22375827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
22385827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2239450e7dd4SChristophe Leroy 		     path, prom_strlen(path) + 1);
22409b6b563cSPaul Mackerras 
224110348f59SBenjamin Herrenschmidt 	/* instance-to-package fails on PA-Semi */
224210348f59SBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
224310348f59SBenjamin Herrenschmidt 	if (stdout_node != PROM_ERROR) {
224410348f59SBenjamin Herrenschmidt 		val = cpu_to_be32(stdout_node);
224510348f59SBenjamin Herrenschmidt 
22469b6b563cSPaul Mackerras 		/* If it's a display, note it */
22479b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
2248493adffcSBenjamin Herrenschmidt 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
2249450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "display") == 0)
2250493adffcSBenjamin Herrenschmidt 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
22519b6b563cSPaul Mackerras 	}
225210348f59SBenjamin Herrenschmidt }
22539b6b563cSPaul Mackerras 
22549b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
22559b6b563cSPaul Mackerras {
22569b6b563cSPaul Mackerras 	char compat[256];
22579b6b563cSPaul Mackerras 	int len, i = 0;
225821fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
22599b6b563cSPaul Mackerras 	phandle rtas;
2260e8222502SBenjamin Herrenschmidt 	int x;
226121fe3301SBenjamin Herrenschmidt #endif
2262e8222502SBenjamin Herrenschmidt 
22636e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
22645827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
22659b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
22669b6b563cSPaul Mackerras 	if (len > 0) {
22679b6b563cSPaul Mackerras 		compat[len] = 0;
22689b6b563cSPaul Mackerras 		while (i < len) {
22699b6b563cSPaul Mackerras 			char *p = &compat[i];
2270450e7dd4SChristophe Leroy 			int sl = prom_strlen(p);
22719b6b563cSPaul Mackerras 			if (sl == 0)
22729b6b563cSPaul Mackerras 				break;
2273450e7dd4SChristophe Leroy 			if (prom_strstr(p, "Power Macintosh") ||
2274450e7dd4SChristophe Leroy 			    prom_strstr(p, "MacRISC"))
22759b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
2276133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
2277133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
2278133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
2279133dda1eSArnd Bergmann 			 * so we do it here.
2280133dda1eSArnd Bergmann 			 */
2281450e7dd4SChristophe Leroy 			if (prom_strstr(p, "IBM,CBEA") ||
2282450e7dd4SChristophe Leroy 			    prom_strstr(p, "IBM,CPBW-1.0"))
2283133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
2284133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
22859b6b563cSPaul Mackerras 			i += sl + 1;
22869b6b563cSPaul Mackerras 		}
22879b6b563cSPaul Mackerras 	}
22889b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
22896e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
2290e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
2291e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
2292e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
2293e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
2294e8222502SBenjamin Herrenschmidt 	 */
22955827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
2296e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
2297e8222502SBenjamin Herrenschmidt 	if (len <= 0)
2298e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2299450e7dd4SChristophe Leroy 	if (prom_strcmp(compat, "chrp"))
2300e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2301e8222502SBenjamin Herrenschmidt 
23029b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
23039b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2304e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
2305e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2306e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
23079b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
23084da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
23099b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
23109b6b563cSPaul Mackerras 	}
23119b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
23129b6b563cSPaul Mackerras #else
2313e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
23149b6b563cSPaul Mackerras #endif
23159b6b563cSPaul Mackerras }
23169b6b563cSPaul Mackerras 
23179b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
23189b6b563cSPaul Mackerras {
23199b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
23209b6b563cSPaul Mackerras }
23219b6b563cSPaul Mackerras 
23229b6b563cSPaul Mackerras /*
23239b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
23249b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
23259b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
23269b6b563cSPaul Mackerras  * we've taken over the MMU.
23279b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
23289b6b563cSPaul Mackerras  * and if so, open it now.
23299b6b563cSPaul Mackerras  */
23309b6b563cSPaul Mackerras static void __init prom_check_displays(void)
23319b6b563cSPaul Mackerras {
23329b6b563cSPaul Mackerras 	char type[16], *path;
23339b6b563cSPaul Mackerras 	phandle node;
23349b6b563cSPaul Mackerras 	ihandle ih;
23359b6b563cSPaul Mackerras 	int i;
23369b6b563cSPaul Mackerras 
2337d00e34b9SBenjamin Herrenschmidt 	static const unsigned char default_colors[] __initconst = {
23389b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
23399b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
23409b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
23419b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
23429b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
23439b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
23449b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
23459b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
23469b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
23479b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
23489b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
23499b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
23509b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
23519b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
23529b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
23539b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
23549b6b563cSPaul Mackerras 	};
23559b6b563cSPaul Mackerras 	const unsigned char *clut;
23569b6b563cSPaul Mackerras 
23574da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
23589b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
23599b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
23609b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
2361450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "display") != 0)
23629b6b563cSPaul Mackerras 			continue;
23639b6b563cSPaul Mackerras 
23649b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
23655827d416SAnton Blanchard 		path = prom_scratch;
2366d7fbe2a0SChristophe Leroy 		memset(path, 0, sizeof(prom_scratch));
23679b6b563cSPaul Mackerras 
23689b6b563cSPaul Mackerras 		/*
23699b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
23709b6b563cSPaul Mackerras 		 * arguments
23719b6b563cSPaul Mackerras 		 */
23729b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
2373d7fbe2a0SChristophe Leroy 			      sizeof(prom_scratch) - 10) == PROM_ERROR)
23749b6b563cSPaul Mackerras 			continue;
23759b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
23769b6b563cSPaul Mackerras 
23779b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
23789b6b563cSPaul Mackerras 		if (ih == 0) {
23799b6b563cSPaul Mackerras 			prom_printf("failed\n");
23809b6b563cSPaul Mackerras 			continue;
23819b6b563cSPaul Mackerras 		}
23829b6b563cSPaul Mackerras 
23839b6b563cSPaul Mackerras 		/* Success */
23849b6b563cSPaul Mackerras 		prom_printf("done\n");
2385a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
23869b6b563cSPaul Mackerras 
23879b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
23889b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
23895827d416SAnton Blanchard 		clut = default_colors;
23903f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
23919b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
23929b6b563cSPaul Mackerras 					   clut[2]) != 0)
23939b6b563cSPaul Mackerras 				break;
23949b6b563cSPaul Mackerras 
23959b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
23965827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
23975827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
23989b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
23999b6b563cSPaul Mackerras 					   clut[2]) != 0)
24009b6b563cSPaul Mackerras 				break;
24019b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
24027191b615SBenjamin Herrenschmidt 
24037191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
24047191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
24057191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
24067191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
24077191b615SBenjamin Herrenschmidt 
24087191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
24097191b615SBenjamin Herrenschmidt 			prom_getprop(node, "width", &width, 4);
24107191b615SBenjamin Herrenschmidt 			prom_getprop(node, "height", &height, 4);
24117191b615SBenjamin Herrenschmidt 			prom_getprop(node, "linebytes", &pitch, 4);
24127191b615SBenjamin Herrenschmidt 			prom_getprop(node, "address", &addr, 4);
24137191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
24147191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
24157191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
2416c21f5a9eSChristophe Leroy 			btext_prepare_BAT();
24177191b615SBenjamin Herrenschmidt 		}
24187191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
24199b6b563cSPaul Mackerras 	}
24209b6b563cSPaul Mackerras }
24219b6b563cSPaul Mackerras 
24229b6b563cSPaul Mackerras 
24239b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
24249b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
24259b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
24269b6b563cSPaul Mackerras {
24279b6b563cSPaul Mackerras 	void *ret;
24289b6b563cSPaul Mackerras 
24299b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
24309b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
24319b6b563cSPaul Mackerras 		unsigned long room, chunk;
24329b6b563cSPaul Mackerras 
2433eae5f709SMathieu Malaterre 		prom_debug("Chunk exhausted, claiming more at %lx...\n",
24345827d416SAnton Blanchard 			   alloc_bottom);
24355827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
24369b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
24379b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
24389b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2439fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2440fbafd728SAnton Blanchard 				   "(no room)\n");
24419b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
24429b6b563cSPaul Mackerras 		if (chunk == 0)
2443fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2444fbafd728SAnton Blanchard 				   "(claim failed)\n");
2445966728ddSAnton Blanchard 		*mem_end = chunk + room;
24469b6b563cSPaul Mackerras 	}
24479b6b563cSPaul Mackerras 
24489b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
24499b6b563cSPaul Mackerras 	*mem_start += needed;
24509b6b563cSPaul Mackerras 
24519b6b563cSPaul Mackerras 	return ret;
24529b6b563cSPaul Mackerras }
24539b6b563cSPaul Mackerras 
2454493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2455493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2456493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2457493adffcSBenjamin Herrenschmidt 	} while(0)
24589b6b563cSPaul Mackerras 
24599b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
24609b6b563cSPaul Mackerras {
24619b6b563cSPaul Mackerras 	char *s, *os;
24629b6b563cSPaul Mackerras 
24635827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
24649b6b563cSPaul Mackerras 	s += 4;
24655827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
2466450e7dd4SChristophe Leroy 		if (prom_strcmp(s, str) == 0)
24679b6b563cSPaul Mackerras 			return s - os;
2468450e7dd4SChristophe Leroy 		s += prom_strlen(s) + 1;
24699b6b563cSPaul Mackerras 	}
24709b6b563cSPaul Mackerras 	return 0;
24719b6b563cSPaul Mackerras }
24729b6b563cSPaul Mackerras 
24739b6b563cSPaul Mackerras /*
24749b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
24759b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
24769b6b563cSPaul Mackerras  */
24779b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
24789b6b563cSPaul Mackerras 
24799b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
24809b6b563cSPaul Mackerras 					 unsigned long *mem_start,
24819b6b563cSPaul Mackerras 					 unsigned long *mem_end)
24829b6b563cSPaul Mackerras {
24839b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
24849b6b563cSPaul Mackerras 	unsigned long soff;
24859b6b563cSPaul Mackerras 	phandle child;
24869b6b563cSPaul Mackerras 
24875827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
24889b6b563cSPaul Mackerras 
24899b6b563cSPaul Mackerras 	/* get and store all property names */
24905827d416SAnton Blanchard 	prev_name = "";
24919b6b563cSPaul Mackerras 	for (;;) {
24929b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
24939b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
24949b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
24959b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
24969b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
24979b6b563cSPaul Mackerras 			break;
24989b6b563cSPaul Mackerras 		}
24999b6b563cSPaul Mackerras 
25009b6b563cSPaul Mackerras  		/* skip "name" */
2501450e7dd4SChristophe Leroy 		if (prom_strcmp(namep, "name") == 0) {
25029b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
25035827d416SAnton Blanchard  			prev_name = "name";
25049b6b563cSPaul Mackerras  			continue;
25059b6b563cSPaul Mackerras  		}
25069b6b563cSPaul Mackerras 		/* get/create string entry */
25079b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
25089b6b563cSPaul Mackerras 		if (soff != 0) {
25099b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
25109b6b563cSPaul Mackerras 			namep = sstart + soff;
25119b6b563cSPaul Mackerras 		} else {
25129b6b563cSPaul Mackerras 			/* Trim off some if we can */
2513450e7dd4SChristophe Leroy 			*mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
25145827d416SAnton Blanchard 			dt_string_end = *mem_start;
25159b6b563cSPaul Mackerras 		}
25169b6b563cSPaul Mackerras 		prev_name = namep;
25179b6b563cSPaul Mackerras 	}
25189b6b563cSPaul Mackerras 
25199b6b563cSPaul Mackerras 	/* do all our children */
25209b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
25219b6b563cSPaul Mackerras 	while (child != 0) {
25229b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
25239b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
25249b6b563cSPaul Mackerras 	}
25259b6b563cSPaul Mackerras }
25269b6b563cSPaul Mackerras 
25279b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
25289b6b563cSPaul Mackerras 					unsigned long *mem_end)
25299b6b563cSPaul Mackerras {
25309b6b563cSPaul Mackerras 	phandle child;
25319b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
25329b6b563cSPaul Mackerras 	unsigned long soff;
25339b6b563cSPaul Mackerras 	unsigned char *valp;
25348ca2d515SBenjamin Herrenschmidt 	static char pname[MAX_PROPERTY_NAME] __prombss;
25356e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
25369b6b563cSPaul Mackerras 
25379b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
25389b6b563cSPaul Mackerras 
25399b6b563cSPaul Mackerras 	/* get the node's full name */
25409b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2541c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2542c4988820SPaul Mackerras 	if (room > 255)
2543c4988820SPaul Mackerras 		room = 255;
2544c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
25459b6b563cSPaul Mackerras 	if (l >= 0) {
25469b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2547c4988820SPaul Mackerras 		if (l >= room) {
2548c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
25499b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
25509b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
25519b6b563cSPaul Mackerras 		}
25529b6b563cSPaul Mackerras 		namep[l] = '\0';
25539b6b563cSPaul Mackerras 
25549b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2555a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2556a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
25579b6b563cSPaul Mackerras 		 */
2558a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
25599b6b563cSPaul Mackerras 			if (*p == '/')
2560a575b807SPaul Mackerras 				lp = namep;
2561a575b807SPaul Mackerras 			else if (*p != 0)
2562a575b807SPaul Mackerras 				*lp++ = *p;
2563a575b807SPaul Mackerras 		}
2564a575b807SPaul Mackerras 		*lp = 0;
2565a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
25669b6b563cSPaul Mackerras 	}
25679b6b563cSPaul Mackerras 
25689b6b563cSPaul Mackerras 	/* get it again for debugging */
25695827d416SAnton Blanchard 	path = prom_scratch;
2570d7fbe2a0SChristophe Leroy 	memset(path, 0, sizeof(prom_scratch));
2571d7fbe2a0SChristophe Leroy 	call_prom("package-to-path", 3, 1, node, path, sizeof(prom_scratch) - 1);
25729b6b563cSPaul Mackerras 
25739b6b563cSPaul Mackerras 	/* get and store all properties */
25745827d416SAnton Blanchard 	prev_name = "";
25755827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
25769b6b563cSPaul Mackerras 	for (;;) {
25779b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
25785827d416SAnton Blanchard 			      pname) != 1)
25799b6b563cSPaul Mackerras 			break;
25809b6b563cSPaul Mackerras 
25819b6b563cSPaul Mackerras  		/* skip "name" */
2582450e7dd4SChristophe Leroy 		if (prom_strcmp(pname, "name") == 0) {
25835827d416SAnton Blanchard  			prev_name = "name";
25849b6b563cSPaul Mackerras  			continue;
25859b6b563cSPaul Mackerras  		}
25869b6b563cSPaul Mackerras 
25879b6b563cSPaul Mackerras 		/* find string offset */
25885827d416SAnton Blanchard 		soff = dt_find_string(pname);
25899b6b563cSPaul Mackerras 		if (soff == 0) {
25909b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
25915827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
25929b6b563cSPaul Mackerras 			break;
25939b6b563cSPaul Mackerras 		}
25949b6b563cSPaul Mackerras 		prev_name = sstart + soff;
25959b6b563cSPaul Mackerras 
25969b6b563cSPaul Mackerras 		/* get length */
25975827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
25989b6b563cSPaul Mackerras 
25999b6b563cSPaul Mackerras 		/* sanity checks */
26009b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
26019b6b563cSPaul Mackerras 			continue;
26029b6b563cSPaul Mackerras 
26039b6b563cSPaul Mackerras 		/* push property head */
26049b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
26059b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
26069b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
26079b6b563cSPaul Mackerras 
26089b6b563cSPaul Mackerras 		/* push property content */
26099b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
26105827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
26119b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
26126e35d5daSBenjamin Herrenschmidt 
2613450e7dd4SChristophe Leroy 		if (!prom_strcmp(pname, "phandle"))
26146e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
26159b6b563cSPaul Mackerras 	}
26169b6b563cSPaul Mackerras 
2617f1f208e5SBenjamin Herrenschmidt 	/* Add a "phandle" property if none already exist */
26186e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
2619f1f208e5SBenjamin Herrenschmidt 		soff = dt_find_string("phandle");
26209b6b563cSPaul Mackerras 		if (soff == 0)
2621f1f208e5SBenjamin Herrenschmidt 			prom_printf("WARNING: Can't find string index for <phandle> node %s\n", path);
26229b6b563cSPaul Mackerras 		else {
26239b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
26249b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
26259b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
26269b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
2627493adffcSBenjamin Herrenschmidt 			*(__be32 *)valp = cpu_to_be32(node);
26289b6b563cSPaul Mackerras 		}
26296e35d5daSBenjamin Herrenschmidt 	}
26309b6b563cSPaul Mackerras 
26319b6b563cSPaul Mackerras 	/* do all our children */
26329b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
26339b6b563cSPaul Mackerras 	while (child != 0) {
26349b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
26359b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
26369b6b563cSPaul Mackerras 	}
26379b6b563cSPaul Mackerras 
26389b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
26399b6b563cSPaul Mackerras }
26409b6b563cSPaul Mackerras 
26419b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
26429b6b563cSPaul Mackerras {
26439b6b563cSPaul Mackerras 	phandle root;
26449b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
26459b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
26469b6b563cSPaul Mackerras 	char *namep;
26479b6b563cSPaul Mackerras 	u64 *rsvmap;
26489b6b563cSPaul Mackerras 
26499b6b563cSPaul Mackerras 	/*
26509b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2651fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
26529b6b563cSPaul Mackerras 	 */
26535827d416SAnton Blanchard 	room = alloc_top - alloc_bottom - 0x4000;
26549b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
26559b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
2656eae5f709SMathieu Malaterre 	prom_debug("starting device tree allocs at %lx\n", alloc_bottom);
26579b6b563cSPaul Mackerras 
26589b6b563cSPaul Mackerras 	/* Now try to claim that */
26599b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
26609b6b563cSPaul Mackerras 	if (mem_start == 0)
26619b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2662966728ddSAnton Blanchard 	mem_end = mem_start + room;
26639b6b563cSPaul Mackerras 
26649b6b563cSPaul Mackerras 	/* Get root of tree */
26659b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
26669b6b563cSPaul Mackerras 	if (root == (phandle)0)
26679b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
26689b6b563cSPaul Mackerras 
26699b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
26709b6b563cSPaul Mackerras 	mem_start = _ALIGN(mem_start, 4);
26719b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
26729b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
26735827d416SAnton Blanchard 	dt_header_start = (unsigned long)hdr;
26749b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
26759b6b563cSPaul Mackerras 
26769b6b563cSPaul Mackerras 	/* Start of strings */
26779b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
26785827d416SAnton Blanchard 	dt_string_start = mem_start;
26799b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
26809b6b563cSPaul Mackerras 
2681f1f208e5SBenjamin Herrenschmidt 	/* Add "phandle" in there, we'll need it */
26829b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
2683450e7dd4SChristophe Leroy 	prom_strcpy(namep, "phandle");
2684450e7dd4SChristophe Leroy 	mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
26859b6b563cSPaul Mackerras 
26869b6b563cSPaul Mackerras 	/* Build string array */
26879b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
26889b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
26895827d416SAnton Blanchard 	dt_string_end = mem_start;
26909b6b563cSPaul Mackerras 
26919b6b563cSPaul Mackerras 	/* Build structure */
26929b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
26935827d416SAnton Blanchard 	dt_struct_start = mem_start;
26949b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
26959b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
26969b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
26975827d416SAnton Blanchard 	dt_struct_end = PAGE_ALIGN(mem_start);
26989b6b563cSPaul Mackerras 
26999b6b563cSPaul Mackerras 	/* Finish header */
2700493adffcSBenjamin Herrenschmidt 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2701493adffcSBenjamin Herrenschmidt 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2702493adffcSBenjamin Herrenschmidt 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2703493adffcSBenjamin Herrenschmidt 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2704493adffcSBenjamin Herrenschmidt 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2705493adffcSBenjamin Herrenschmidt 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2706493adffcSBenjamin Herrenschmidt 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2707493adffcSBenjamin Herrenschmidt 	hdr->version = cpu_to_be32(OF_DT_VERSION);
27089b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
2709493adffcSBenjamin Herrenschmidt 	hdr->last_comp_version = cpu_to_be32(0x10);
27109b6b563cSPaul Mackerras 
27114d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
27125827d416SAnton Blanchard 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
27139b6b563cSPaul Mackerras 
27149b6b563cSPaul Mackerras #ifdef DEBUG_PROM
27159b6b563cSPaul Mackerras 	{
27169b6b563cSPaul Mackerras 		int i;
27179b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
27185827d416SAnton Blanchard 		for (i = 0; i < mem_reserve_cnt; i++)
2719eae5f709SMathieu Malaterre 			prom_printf("  %llx - %llx\n",
2720493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].base),
2721493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].size));
27229b6b563cSPaul Mackerras 	}
27239b6b563cSPaul Mackerras #endif
27244d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
27254d1f3f25SJimi Xenidis 	 * since it's too late.
27264d1f3f25SJimi Xenidis 	 */
27275827d416SAnton Blanchard 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
27289b6b563cSPaul Mackerras 
2729eae5f709SMathieu Malaterre 	prom_printf("Device tree strings 0x%lx -> 0x%lx\n",
27305827d416SAnton Blanchard 		    dt_string_start, dt_string_end);
2731eae5f709SMathieu Malaterre 	prom_printf("Device tree struct  0x%lx -> 0x%lx\n",
27325827d416SAnton Blanchard 		    dt_struct_start, dt_struct_end);
27339b6b563cSPaul Mackerras }
27349b6b563cSPaul Mackerras 
273554f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
273654f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
273754f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
273854f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
27399b6b563cSPaul Mackerras {
274054f4ee18SHollis Blanchard 	phandle isa;
2741980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
274254f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2743980a6513SBenjamin Herrenschmidt 	char *name;
274454f4ee18SHollis Blanchard 
2745980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2746980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2747980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2748980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2749980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2750980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2751980a6513SBenjamin Herrenschmidt 	}
275254f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
275354f4ee18SHollis Blanchard 		return;
275454f4ee18SHollis Blanchard 
2755980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2756980a6513SBenjamin Herrenschmidt 		return;
275754f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
275854f4ee18SHollis Blanchard 		== PROM_ERROR)
275954f4ee18SHollis Blanchard 		return;
276054f4ee18SHollis Blanchard 
276154f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
276254f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
276354f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
276454f4ee18SHollis Blanchard 		return;
276554f4ee18SHollis Blanchard 
2766980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
276754f4ee18SHollis Blanchard 
276854f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
276954f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2770980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
277154f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
277254f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
277354f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2774980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
277554f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
277654f4ee18SHollis Blanchard }
27778f101a05SHarry Ciao 
27788f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
27798f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
27808f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
27818f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
27828f101a05SHarry Ciao {
27838f101a05SHarry Ciao 	phandle mc;
27848f101a05SHarry Ciao 	u32 mc_reg[4];
27858f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
27868f101a05SHarry Ciao 	u32 ac, sc;
27878f101a05SHarry Ciao 
27888f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
27898f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
27908f101a05SHarry Ciao 		return;
27918f101a05SHarry Ciao 
27928f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
27938f101a05SHarry Ciao 		return;
27948f101a05SHarry Ciao 
27955827d416SAnton Blanchard 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
27965827d416SAnton Blanchard 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
27978f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
27988f101a05SHarry Ciao 		return;
27998f101a05SHarry Ciao 
28008f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
28018f101a05SHarry Ciao 		return;
28028f101a05SHarry Ciao 
28038f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
28048f101a05SHarry Ciao 		return;
28058f101a05SHarry Ciao 
28068f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
28078f101a05SHarry Ciao 
28088f101a05SHarry Ciao 	mc_reg[0] = 0x0;
28098f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
28108f101a05SHarry Ciao 	mc_reg[2] = 0x0;
28118f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
28128f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
28138f101a05SHarry Ciao }
281454f4ee18SHollis Blanchard #else
281554f4ee18SHollis Blanchard #define fixup_device_tree_maple()
28168f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
281754f4ee18SHollis Blanchard #endif
281854f4ee18SHollis Blanchard 
2819e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2820e4805922SOlaf Hering /*
2821e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2822e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2823556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2824e4805922SOlaf Hering  */
2825e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2826e8c0acf9SBenjamin Herrenschmidt {
2827e4805922SOlaf Hering 	phandle ph;
2828e4805922SOlaf Hering 	u32 prop[6];
282926c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2830e8c0acf9SBenjamin Herrenschmidt 	char *name;
2831e8c0acf9SBenjamin Herrenschmidt 	int rc;
2832e8c0acf9SBenjamin Herrenschmidt 
2833e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2834e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2835e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
283626c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2837e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
283826c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
283926c5032eSBenjamin Herrenschmidt 	}
2840e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2841e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2842e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2843e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2844e8c0acf9SBenjamin Herrenschmidt 
2845e4805922SOlaf Hering 			prop[0] = 0x1;
2846e4805922SOlaf Hering 			prop[1] = 0x0;
2847e4805922SOlaf Hering 			prop[2] = rloc;
2848e4805922SOlaf Hering 			prop[3] = 0x0;
2849e4805922SOlaf Hering 			prop[4] = 0x0;
2850e4805922SOlaf Hering 			prop[5] = 0x00010000;
2851e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2852e4805922SOlaf Hering 		}
2853e4805922SOlaf Hering 	}
2854e4805922SOlaf Hering 
2855e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2856e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2857e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2858e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2859e4805922SOlaf Hering 		prop[0] = 14;
2860e4805922SOlaf Hering 		prop[1] = 0x0;
2861556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2862556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2863556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2864556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2865556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2866556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2867556ecf9bSOlaf Hering 		}
2868e4805922SOlaf Hering 	}
2869e8c0acf9SBenjamin Herrenschmidt }
2870e8c0acf9SBenjamin Herrenschmidt #else
2871e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2872e8c0acf9SBenjamin Herrenschmidt #endif
2873e8c0acf9SBenjamin Herrenschmidt 
28749b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
287554f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
287654f4ee18SHollis Blanchard {
28779b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
28789b6b563cSPaul Mackerras 	u32 u3_rev;
28799b6b563cSPaul Mackerras 	u32 interrupts[2];
28809b6b563cSPaul Mackerras 	u32 parent;
28819b6b563cSPaul Mackerras 
28829b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
28839b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
28849b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
28859b6b563cSPaul Mackerras 		return;
28869b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
28879b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
28889b6b563cSPaul Mackerras 		return;
28899b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
28909b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
28919b6b563cSPaul Mackerras 		return;
28929b6b563cSPaul Mackerras 
28939b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
28949b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
28959b6b563cSPaul Mackerras 	    == PROM_ERROR)
28969b6b563cSPaul Mackerras 		return;
28977d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
28989b6b563cSPaul Mackerras 		return;
28999b6b563cSPaul Mackerras 	/* does it need fixup ? */
29009b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
29019b6b563cSPaul Mackerras 		return;
29029b6b563cSPaul Mackerras 
29039b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
29049b6b563cSPaul Mackerras 
29059b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
29069b6b563cSPaul Mackerras 	interrupts[0] = 0;
29079b6b563cSPaul Mackerras 	interrupts[1] = 1;
2908a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2909a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
29109b6b563cSPaul Mackerras 	parent = (u32)mpic;
2911a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2912a23414beSPaul Mackerras 		     &parent, sizeof(parent));
29139b6b563cSPaul Mackerras }
291454f4ee18SHollis Blanchard #else
291554f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
291654f4ee18SHollis Blanchard #endif
29179b6b563cSPaul Mackerras 
291888fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
291994d2dde7SGrant Likely /*
292094d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
292194d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
292294d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
292394d2dde7SGrant Likely  * ethernet node.
292494d2dde7SGrant Likely  */
292594d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
292688fd2a9dSSylvain Munaut {
292788fd2a9dSSylvain Munaut 	u32 node;
292888fd2a9dSSylvain Munaut 	char prop[64];
292994d2dde7SGrant Likely 	int rv;
293088fd2a9dSSylvain Munaut 
293194d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
293294d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
293388fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
293488fd2a9dSSylvain Munaut 		return;
293588fd2a9dSSylvain Munaut 
293694d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
293794d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
293894d2dde7SGrant Likely 	if (!rv)
293988fd2a9dSSylvain Munaut 		return;
294088fd2a9dSSylvain Munaut 
294194d2dde7SGrant Likely 	/*
294294d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
294394d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
294494d2dde7SGrant Likely 	 */
294588fd2a9dSSylvain Munaut 
294694d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
29476f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
29486f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
29496f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
29506f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
29516f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
29526f4347c9SOlaf Hering 			" new-device"
29536f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
29546f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
295594d2dde7SGrant Likely 				" s\" mdio\" device-name"
295694d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
29576f4347c9SOlaf Hering 				" s\" compatible\" property"
29586f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
29596f4347c9SOlaf Hering 				" 0x2 encode-int"
29606f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
29616f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
29626f4347c9SOlaf Hering 				" s\" interrupts\" property"
29636f4347c9SOlaf Hering 			" finish-device");
29646f4347c9SOlaf Hering 	};
29656f4347c9SOlaf Hering 
296694d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
296794d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
296894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
296994d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
29706f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
29716f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
29726f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
29736f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
29746f4347c9SOlaf Hering 			" new-device"
29756f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
29766f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
29776f4347c9SOlaf Hering 				" my-self"
29786f4347c9SOlaf Hering 				" ihandle>phandle"
29796f4347c9SOlaf Hering 			" finish-device"
29806f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
29816f4347c9SOlaf Hering 				" encode-int"
29826f4347c9SOlaf Hering 				" s\" phy-handle\" property"
29836f4347c9SOlaf Hering 			" device-end");
29846f4347c9SOlaf Hering 	}
298594d2dde7SGrant Likely }
29866f4347c9SOlaf Hering 
298794d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
298894d2dde7SGrant Likely {
298994d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
299094d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
299194d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
299294d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
299394d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
299494d2dde7SGrant Likely 	u32 node;
299594d2dde7SGrant Likely 	char prop[64];
299694d2dde7SGrant Likely 	int rv, len;
299794d2dde7SGrant Likely 
299894d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
299994d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
300094d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
300194d2dde7SGrant Likely 		return;
300294d2dde7SGrant Likely 
300394d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
300494d2dde7SGrant Likely 	if (rv == PROM_ERROR)
300594d2dde7SGrant Likely 		return;
3006450e7dd4SChristophe Leroy 	if (prom_strcmp(prop, "EFIKA5K2"))
300794d2dde7SGrant Likely 		return;
300894d2dde7SGrant Likely 
300994d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
301094d2dde7SGrant Likely 
301194d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
301294d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
301394d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
3014450e7dd4SChristophe Leroy 	if (rv != PROM_ERROR && (prom_strcmp(prop, "chrp") == 0))
301594d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
301694d2dde7SGrant Likely 
30177f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
30187f4392cdSDavid Woodhouse 	   fix that too */
30197f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
3020450e7dd4SChristophe Leroy 	if (rv != PROM_ERROR && (prom_strstr(prop, "CHRP")))
30217f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
30227f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
30237f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
30247f4392cdSDavid Woodhouse 
302594d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
302694d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
302794d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
302894d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
302994d2dde7SGrant Likely 		if (len == 12) {
303094d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
303194d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
303294d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
303394d2dde7SGrant Likely 		}
303494d2dde7SGrant Likely 	}
303594d2dde7SGrant Likely 
303694d2dde7SGrant Likely 	/* Fixup sound interrupts property */
303794d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
303894d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
303994d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
304094d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
304194d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
304294d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
304394d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
304494d2dde7SGrant Likely 		}
304594d2dde7SGrant Likely 	}
304694d2dde7SGrant Likely 
304794d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
304894d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
304988fd2a9dSSylvain Munaut }
305088fd2a9dSSylvain Munaut #else
305188fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
305288fd2a9dSSylvain Munaut #endif
305388fd2a9dSSylvain Munaut 
305450246787SDarren Stevens #ifdef CONFIG_PPC_PASEMI_NEMO
305550246787SDarren Stevens /*
305650246787SDarren Stevens  * CFE supplied on Nemo is broken in several ways, biggest
305750246787SDarren Stevens  * problem is that it reassigns ISA interrupts to unused mpic ints.
305850246787SDarren Stevens  * Add an interrupt-controller property for the io-bridge to use
305950246787SDarren Stevens  * and correct the ints so we can attach them to an irq_domain
306050246787SDarren Stevens  */
306150246787SDarren Stevens static void __init fixup_device_tree_pasemi(void)
306250246787SDarren Stevens {
306350246787SDarren Stevens 	u32 interrupts[2], parent, rval, val = 0;
306450246787SDarren Stevens 	char *name, *pci_name;
306550246787SDarren Stevens 	phandle iob, node;
306650246787SDarren Stevens 
306750246787SDarren Stevens 	/* Find the root pci node */
306850246787SDarren Stevens 	name = "/pxp@0,e0000000";
306950246787SDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
307050246787SDarren Stevens 	if (!PHANDLE_VALID(iob))
307150246787SDarren Stevens 		return;
307250246787SDarren Stevens 
307350246787SDarren Stevens 	/* check if interrupt-controller node set yet */
307450246787SDarren Stevens 	if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
307550246787SDarren Stevens 		return;
307650246787SDarren Stevens 
307750246787SDarren Stevens 	prom_printf("adding interrupt-controller property for SB600...\n");
307850246787SDarren Stevens 
307950246787SDarren Stevens 	prom_setprop(iob, name, "interrupt-controller", &val, 0);
308050246787SDarren Stevens 
308150246787SDarren Stevens 	pci_name = "/pxp@0,e0000000/pci@11";
308250246787SDarren Stevens 	node = call_prom("finddevice", 1, 1, ADDR(pci_name));
308350246787SDarren Stevens 	parent = ADDR(iob);
308450246787SDarren Stevens 
308550246787SDarren Stevens 	for( ; prom_next_node(&node); ) {
308650246787SDarren Stevens 		/* scan each node for one with an interrupt */
308750246787SDarren Stevens 		if (!PHANDLE_VALID(node))
308850246787SDarren Stevens 			continue;
308950246787SDarren Stevens 
309050246787SDarren Stevens 		rval = prom_getproplen(node, "interrupts");
309150246787SDarren Stevens 		if (rval == 0 || rval == PROM_ERROR)
309250246787SDarren Stevens 			continue;
309350246787SDarren Stevens 
309450246787SDarren Stevens 		prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
309550246787SDarren Stevens 		if ((interrupts[0] < 212) || (interrupts[0] > 222))
309650246787SDarren Stevens 			continue;
309750246787SDarren Stevens 
309850246787SDarren Stevens 		/* found a node, update both interrupts and interrupt-parent */
309950246787SDarren Stevens 		if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
310050246787SDarren Stevens 			interrupts[0] -= 203;
310150246787SDarren Stevens 		if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
310250246787SDarren Stevens 			interrupts[0] -= 213;
310350246787SDarren Stevens 		if (interrupts[0] == 221)
310450246787SDarren Stevens 			interrupts[0] = 14;
310550246787SDarren Stevens 		if (interrupts[0] == 222)
310650246787SDarren Stevens 			interrupts[0] = 8;
310750246787SDarren Stevens 
310850246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupts", interrupts,
310950246787SDarren Stevens 					sizeof(interrupts));
311050246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupt-parent", &parent,
311150246787SDarren Stevens 					sizeof(parent));
311250246787SDarren Stevens 	}
3113687e16bcSDarren Stevens 
3114687e16bcSDarren Stevens 	/*
3115687e16bcSDarren Stevens 	 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
3116687e16bcSDarren Stevens 	 * so that generic isa-bridge code can add the SB600 and its on-board
3117687e16bcSDarren Stevens 	 * peripherals.
3118687e16bcSDarren Stevens 	 */
3119687e16bcSDarren Stevens 	name = "/pxp@0,e0000000/io-bridge@0";
3120687e16bcSDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
3121687e16bcSDarren Stevens 	if (!PHANDLE_VALID(iob))
3122687e16bcSDarren Stevens 		return;
3123687e16bcSDarren Stevens 
3124687e16bcSDarren Stevens 	/* device_type is already set, just change it. */
3125687e16bcSDarren Stevens 
3126687e16bcSDarren Stevens 	prom_printf("Changing device_type of SB600 node...\n");
3127687e16bcSDarren Stevens 
3128687e16bcSDarren Stevens 	prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
312950246787SDarren Stevens }
313050246787SDarren Stevens #else	/* !CONFIG_PPC_PASEMI_NEMO */
313150246787SDarren Stevens static inline void fixup_device_tree_pasemi(void) { }
313250246787SDarren Stevens #endif
313350246787SDarren Stevens 
313454f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
313554f4ee18SHollis Blanchard {
313654f4ee18SHollis Blanchard 	fixup_device_tree_maple();
31378f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
3138e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
313954f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
314088fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
314150246787SDarren Stevens 	fixup_device_tree_pasemi();
314254f4ee18SHollis Blanchard }
31439b6b563cSPaul Mackerras 
31449b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
31459b6b563cSPaul Mackerras {
3146493adffcSBenjamin Herrenschmidt 	__be32 rval;
31479b6b563cSPaul Mackerras 	ihandle prom_cpu;
31489b6b563cSPaul Mackerras 	phandle cpu_pkg;
31499b6b563cSPaul Mackerras 
3150493adffcSBenjamin Herrenschmidt 	rval = 0;
3151493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
3152a575b807SPaul Mackerras 		return;
3153493adffcSBenjamin Herrenschmidt 	prom_cpu = be32_to_cpu(rval);
31549b6b563cSPaul Mackerras 
31559b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
31569b6b563cSPaul Mackerras 
3157af2b7fa1SDarren Stevens 	if (!PHANDLE_VALID(cpu_pkg))
3158af2b7fa1SDarren Stevens 		return;
3159af2b7fa1SDarren Stevens 
3160493adffcSBenjamin Herrenschmidt 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
3161493adffcSBenjamin Herrenschmidt 	prom.cpu = be32_to_cpu(rval);
31629b6b563cSPaul Mackerras 
3163eae5f709SMathieu Malaterre 	prom_debug("Booting CPU hw index = %d\n", prom.cpu);
31649b6b563cSPaul Mackerras }
31659b6b563cSPaul Mackerras 
31669b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
31679b6b563cSPaul Mackerras {
31689b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
31699b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
3170493adffcSBenjamin Herrenschmidt 		__be64 val;
31719b6b563cSPaul Mackerras 
31725827d416SAnton Blanchard 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
31735827d416SAnton Blanchard 		prom_initrd_end = prom_initrd_start + r4;
31749b6b563cSPaul Mackerras 
3175493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_start);
31765827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
3177a23414beSPaul Mackerras 			     &val, sizeof(val));
3178493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_end);
31795827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
3180a23414beSPaul Mackerras 			     &val, sizeof(val));
31819b6b563cSPaul Mackerras 
31825827d416SAnton Blanchard 		reserve_mem(prom_initrd_start,
31835827d416SAnton Blanchard 			    prom_initrd_end - prom_initrd_start);
31849b6b563cSPaul Mackerras 
3185eae5f709SMathieu Malaterre 		prom_debug("initrd_start=0x%lx\n", prom_initrd_start);
3186eae5f709SMathieu Malaterre 		prom_debug("initrd_end=0x%lx\n", prom_initrd_end);
31879b6b563cSPaul Mackerras 	}
31889b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
31899b6b563cSPaul Mackerras }
31909b6b563cSPaul Mackerras 
31915ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64
31925ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE
31935ac47f7aSAnton Blanchard static void reloc_toc(void)
31945ac47f7aSAnton Blanchard {
31955ac47f7aSAnton Blanchard }
31965ac47f7aSAnton Blanchard 
31975ac47f7aSAnton Blanchard static void unreloc_toc(void)
31985ac47f7aSAnton Blanchard {
31995ac47f7aSAnton Blanchard }
32005ac47f7aSAnton Blanchard #else
32011674400aSAnton Blanchard static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
32025ac47f7aSAnton Blanchard {
32035ac47f7aSAnton Blanchard 	unsigned long i;
32041674400aSAnton Blanchard 	unsigned long *toc_entry;
32051674400aSAnton Blanchard 
32061674400aSAnton Blanchard 	/* Get the start of the TOC by using r2 directly. */
32071674400aSAnton Blanchard 	asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
32085ac47f7aSAnton Blanchard 
32095ac47f7aSAnton Blanchard 	for (i = 0; i < nr_entries; i++) {
32105ac47f7aSAnton Blanchard 		*toc_entry = *toc_entry + offset;
32115ac47f7aSAnton Blanchard 		toc_entry++;
32125ac47f7aSAnton Blanchard 	}
32135ac47f7aSAnton Blanchard }
32145ac47f7aSAnton Blanchard 
32155ac47f7aSAnton Blanchard static void reloc_toc(void)
32165ac47f7aSAnton Blanchard {
32175ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
32185ac47f7aSAnton Blanchard 	unsigned long nr_entries =
32195ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
32205ac47f7aSAnton Blanchard 
32211674400aSAnton Blanchard 	__reloc_toc(offset, nr_entries);
32225ac47f7aSAnton Blanchard 
32235ac47f7aSAnton Blanchard 	mb();
32245ac47f7aSAnton Blanchard }
32255ac47f7aSAnton Blanchard 
32265ac47f7aSAnton Blanchard static void unreloc_toc(void)
32275ac47f7aSAnton Blanchard {
32285ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
32295ac47f7aSAnton Blanchard 	unsigned long nr_entries =
32305ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
32315ac47f7aSAnton Blanchard 
32325ac47f7aSAnton Blanchard 	mb();
32335ac47f7aSAnton Blanchard 
32341674400aSAnton Blanchard 	__reloc_toc(-offset, nr_entries);
32355ac47f7aSAnton Blanchard }
32365ac47f7aSAnton Blanchard #endif
32375ac47f7aSAnton Blanchard #endif
323827f44888SBenjamin Herrenschmidt 
32396a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
32406a9c930bSRam Pai /*
32416a9c930bSRam Pai  * Perform the Enter Secure Mode ultracall.
32426a9c930bSRam Pai  */
32436a9c930bSRam Pai static int enter_secure_mode(unsigned long kbase, unsigned long fdt)
32446a9c930bSRam Pai {
32456a9c930bSRam Pai 	register unsigned long r3 asm("r3") = UV_ESM;
32466a9c930bSRam Pai 	register unsigned long r4 asm("r4") = kbase;
32476a9c930bSRam Pai 	register unsigned long r5 asm("r5") = fdt;
32486a9c930bSRam Pai 
32496a9c930bSRam Pai 	asm volatile("sc 2" : "+r"(r3) : "r"(r4), "r"(r5));
32506a9c930bSRam Pai 
32516a9c930bSRam Pai 	return r3;
32526a9c930bSRam Pai }
32536a9c930bSRam Pai 
32546a9c930bSRam Pai /*
32556a9c930bSRam Pai  * Call the Ultravisor to transfer us to secure memory if we have an ESM blob.
32566a9c930bSRam Pai  */
32576a9c930bSRam Pai static void setup_secure_guest(unsigned long kbase, unsigned long fdt)
32586a9c930bSRam Pai {
32596a9c930bSRam Pai 	int ret;
32606a9c930bSRam Pai 
32616a9c930bSRam Pai 	if (!prom_svm_enable)
32626a9c930bSRam Pai 		return;
32636a9c930bSRam Pai 
32646a9c930bSRam Pai 	/* Switch to secure mode. */
32656a9c930bSRam Pai 	prom_printf("Switching to secure mode.\n");
32666a9c930bSRam Pai 
326705d9a952SThiago Jung Bauermann 	/*
326805d9a952SThiago Jung Bauermann 	 * The ultravisor will do an integrity check of the kernel image but we
326905d9a952SThiago Jung Bauermann 	 * relocated it so the check will fail. Restore the original image by
327005d9a952SThiago Jung Bauermann 	 * relocating it back to the kernel virtual base address.
327105d9a952SThiago Jung Bauermann 	 */
327205d9a952SThiago Jung Bauermann 	if (IS_ENABLED(CONFIG_RELOCATABLE))
327305d9a952SThiago Jung Bauermann 		relocate(KERNELBASE);
327405d9a952SThiago Jung Bauermann 
32756a9c930bSRam Pai 	ret = enter_secure_mode(kbase, fdt);
327605d9a952SThiago Jung Bauermann 
327705d9a952SThiago Jung Bauermann 	/* Relocate the kernel again. */
327805d9a952SThiago Jung Bauermann 	if (IS_ENABLED(CONFIG_RELOCATABLE))
327905d9a952SThiago Jung Bauermann 		relocate(kbase);
328005d9a952SThiago Jung Bauermann 
32816a9c930bSRam Pai 	if (ret != U_SUCCESS) {
32826a9c930bSRam Pai 		prom_printf("Returned %d from switching to secure mode.\n", ret);
32836a9c930bSRam Pai 		prom_rtas_os_term("Switch to secure mode failed.\n");
32846a9c930bSRam Pai 	}
32856a9c930bSRam Pai }
32866a9c930bSRam Pai #else
32876a9c930bSRam Pai static void setup_secure_guest(unsigned long kbase, unsigned long fdt)
32886a9c930bSRam Pai {
32896a9c930bSRam Pai }
32906a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
32916a9c930bSRam Pai 
32929b6b563cSPaul Mackerras /*
32939b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
32949b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
32959b6b563cSPaul Mackerras  */
32969b6b563cSPaul Mackerras 
32979b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
32989b6b563cSPaul Mackerras 			       unsigned long pp,
3299549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
3300549e8152SPaul Mackerras 			       unsigned long kbase)
33019b6b563cSPaul Mackerras {
33029b6b563cSPaul Mackerras 	unsigned long hdr;
33039b6b563cSPaul Mackerras 
33049b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
3305549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
33069b6b563cSPaul Mackerras 	reloc_got2(offset);
33075ac47f7aSAnton Blanchard #else
33085ac47f7aSAnton Blanchard 	reloc_toc();
33099b6b563cSPaul Mackerras #endif
33109b6b563cSPaul Mackerras 
33119b6b563cSPaul Mackerras 	/*
33129b6b563cSPaul Mackerras 	 * First zero the BSS
33139b6b563cSPaul Mackerras 	 */
33145827d416SAnton Blanchard 	memset(&__bss_start, 0, __bss_stop - __bss_start);
33159b6b563cSPaul Mackerras 
33169b6b563cSPaul Mackerras 	/*
33179b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
33189b6b563cSPaul Mackerras 	 * like /chosen
33199b6b563cSPaul Mackerras 	 */
33209b6b563cSPaul Mackerras 	prom_init_client_services(pp);
33219b6b563cSPaul Mackerras 
33229b6b563cSPaul Mackerras 	/*
3323a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
3324a23414beSPaul Mackerras 	 * and other workarounds
3325a23414beSPaul Mackerras 	 */
3326a23414beSPaul Mackerras 	prom_find_mmu();
3327a23414beSPaul Mackerras 
3328a23414beSPaul Mackerras 	/*
33299b6b563cSPaul Mackerras 	 * Init prom stdout device
33309b6b563cSPaul Mackerras 	 */
33319b6b563cSPaul Mackerras 	prom_init_stdout();
33329b6b563cSPaul Mackerras 
33335827d416SAnton Blanchard 	prom_printf("Preparing to boot %s", linux_banner);
3334e7943fbbSMichael Ellerman 
33359b6b563cSPaul Mackerras 	/*
33369b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
33379b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
33389b6b563cSPaul Mackerras 	 */
33395827d416SAnton Blanchard 	of_platform = prom_find_machine_type();
33405827d416SAnton Blanchard 	prom_printf("Detected machine type: %x\n", of_platform);
33419b6b563cSPaul Mackerras 
33420f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
3343add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
3344add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
3345add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
3346549e8152SPaul Mackerras #endif
3347add60ef3SOlaf Hering 
3348add60ef3SOlaf Hering 	/*
3349add60ef3SOlaf Hering 	 * Check for an initrd
3350add60ef3SOlaf Hering 	 */
3351add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
3352add60ef3SOlaf Hering 
335312cc9fd6SSuraj Jitindar Singh 	/*
335412cc9fd6SSuraj Jitindar Singh 	 * Do early parsing of command line
335512cc9fd6SSuraj Jitindar Singh 	 */
335612cc9fd6SSuraj Jitindar Singh 	early_cmdline_parse();
335712cc9fd6SSuraj Jitindar Singh 
335811fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
33599b6b563cSPaul Mackerras 	/*
33609b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
33619b6b563cSPaul Mackerras 	 */
33625827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES ||
33635827d416SAnton Blanchard 	    of_platform == PLATFORM_PSERIES_LPAR)
33649b6b563cSPaul Mackerras 		prom_send_capabilities();
33659b6b563cSPaul Mackerras #endif
33669b6b563cSPaul Mackerras 
33679b6b563cSPaul Mackerras 	/*
3368f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
33699b6b563cSPaul Mackerras 	 */
33705827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC)
3371549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
33729b6b563cSPaul Mackerras 
33739b6b563cSPaul Mackerras 	/*
33749b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
33759b6b563cSPaul Mackerras 	 */
33769b6b563cSPaul Mackerras 	prom_init_mem();
33779b6b563cSPaul Mackerras 
33789b6b563cSPaul Mackerras 	/*
33799b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
33809b6b563cSPaul Mackerras 	 */
33819b6b563cSPaul Mackerras 	prom_find_boot_cpu();
33829b6b563cSPaul Mackerras 
33839b6b563cSPaul Mackerras 	/*
33849b6b563cSPaul Mackerras 	 * Initialize display devices
33859b6b563cSPaul Mackerras 	 */
33869b6b563cSPaul Mackerras 	prom_check_displays();
33879b6b563cSPaul Mackerras 
3388493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
33899b6b563cSPaul Mackerras 	/*
33909b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
33919b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
33929b6b563cSPaul Mackerras 	 * available for us here...
33939b6b563cSPaul Mackerras 	 */
33945827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES)
33959b6b563cSPaul Mackerras 		prom_initialize_tce_table();
33969b6b563cSPaul Mackerras #endif
33979b6b563cSPaul Mackerras 
33989b6b563cSPaul Mackerras 	/*
339927f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
340027f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
34019b6b563cSPaul Mackerras 	 */
340211fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
34039b6b563cSPaul Mackerras 		prom_instantiate_rtas();
340427f44888SBenjamin Herrenschmidt 
34054a727429SAshley Lai #ifdef CONFIG_PPC64
34064a727429SAshley Lai 	/* instantiate sml */
34074a727429SAshley Lai 	prom_instantiate_sml();
34084a727429SAshley Lai #endif
34094a727429SAshley Lai 
341027f44888SBenjamin Herrenschmidt 	/*
341127f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
341227f44888SBenjamin Herrenschmidt 	 *
341327f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
3414dbe78b40SBenjamin Herrenschmidt 	 *
3415dbe78b40SBenjamin Herrenschmidt 	 * (This must be done after instanciating RTAS)
341627f44888SBenjamin Herrenschmidt 	 */
341711fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
341827f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
34199b6b563cSPaul Mackerras 
34209b6b563cSPaul Mackerras 	/*
34219b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
34229b6b563cSPaul Mackerras 	 */
3423493adffcSBenjamin Herrenschmidt 	if (prom_memory_limit) {
3424493adffcSBenjamin Herrenschmidt 		__be64 val = cpu_to_be64(prom_memory_limit);
34255827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3426493adffcSBenjamin Herrenschmidt 			     &val, sizeof(val));
3427493adffcSBenjamin Herrenschmidt 	}
34289b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
34295827d416SAnton Blanchard 	if (prom_iommu_off)
34305827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3431a23414beSPaul Mackerras 			     NULL, 0);
34329b6b563cSPaul Mackerras 
34335827d416SAnton Blanchard 	if (prom_iommu_force_on)
34345827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3435a23414beSPaul Mackerras 			     NULL, 0);
34369b6b563cSPaul Mackerras 
34375827d416SAnton Blanchard 	if (prom_tce_alloc_start) {
34385827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
34395827d416SAnton Blanchard 			     &prom_tce_alloc_start,
34409b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
34415827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
34425827d416SAnton Blanchard 			     &prom_tce_alloc_end,
34439b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
34449b6b563cSPaul Mackerras 	}
34459b6b563cSPaul Mackerras #endif
34469b6b563cSPaul Mackerras 
34479b6b563cSPaul Mackerras 	/*
34489b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
34499b6b563cSPaul Mackerras 	 */
34509b6b563cSPaul Mackerras 	fixup_device_tree();
34519b6b563cSPaul Mackerras 
34529b6b563cSPaul Mackerras 	/*
34539b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
34549b6b563cSPaul Mackerras 	 */
34559b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
34569b6b563cSPaul Mackerras 	flatten_device_tree();
34579b6b563cSPaul Mackerras 
34583825ac0eSPaul Mackerras 	/*
34593825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
34603825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
346111fdb309SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101).
34623825ac0eSPaul Mackerras 	 */
346311fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
34649b6b563cSPaul Mackerras 		prom_close_stdin();
34659b6b563cSPaul Mackerras 
34669b6b563cSPaul Mackerras 	/*
34679b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
34689b6b563cSPaul Mackerras 	 * devices etc...
34699b6b563cSPaul Mackerras 	 */
34707e862d7eSMichael Ellerman 	prom_printf("Quiescing Open Firmware ...\n");
34719b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
34729b6b563cSPaul Mackerras 
34739b6b563cSPaul Mackerras 	/*
34749b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
34759b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
34769b6b563cSPaul Mackerras 	 * is common to us and kexec
34779b6b563cSPaul Mackerras 	 */
34785827d416SAnton Blanchard 	hdr = dt_header_start;
347940dfef66SBenjamin Herrenschmidt 
348040dfef66SBenjamin Herrenschmidt 	/* Don't print anything after quiesce under OPAL, it crashes OFW */
34817d70c63cSBenjamin Herrenschmidt 	prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
3482eae5f709SMathieu Malaterre 	prom_debug("->dt_header_start=0x%lx\n", hdr);
34839b6b563cSPaul Mackerras 
34849b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
34859b6b563cSPaul Mackerras 	reloc_got2(-offset);
34865ac47f7aSAnton Blanchard #else
34875ac47f7aSAnton Blanchard 	unreloc_toc();
34889b6b563cSPaul Mackerras #endif
34899b6b563cSPaul Mackerras 
34906a9c930bSRam Pai 	/* Move to secure memory if we're supposed to be secure guests. */
34916a9c930bSRam Pai 	setup_secure_guest(kbase, hdr);
34926a9c930bSRam Pai 
34936e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
34949b6b563cSPaul Mackerras 
34959b6b563cSPaul Mackerras 	return 0;
34969b6b563cSPaul Mackerras }
3497