xref: /openbmc/linux/arch/powerpc/kernel/prom_init.c (revision 49a41365052849be798716b374fabd436cce3ad0)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29b6b563cSPaul Mackerras /*
39b6b563cSPaul Mackerras  * Procedures for interfacing to Open Firmware.
49b6b563cSPaul Mackerras  *
59b6b563cSPaul Mackerras  * Paul Mackerras	August 1996.
69b6b563cSPaul Mackerras  * Copyright (C) 1996-2005 Paul Mackerras.
79b6b563cSPaul Mackerras  *
89b6b563cSPaul Mackerras  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
99b6b563cSPaul Mackerras  *    {engebret|bergner}@us.ibm.com
109b6b563cSPaul Mackerras  */
119b6b563cSPaul Mackerras 
129b6b563cSPaul Mackerras #undef DEBUG_PROM
139b6b563cSPaul Mackerras 
14054f367aSDaniel Axtens /* we cannot use FORTIFY as it brings in new symbols */
15054f367aSDaniel Axtens #define __NO_FORTIFY
16054f367aSDaniel Axtens 
179b6b563cSPaul Mackerras #include <stdarg.h>
189b6b563cSPaul Mackerras #include <linux/kernel.h>
199b6b563cSPaul Mackerras #include <linux/string.h>
209b6b563cSPaul Mackerras #include <linux/init.h>
219b6b563cSPaul Mackerras #include <linux/threads.h>
229b6b563cSPaul Mackerras #include <linux/spinlock.h>
239b6b563cSPaul Mackerras #include <linux/types.h>
249b6b563cSPaul Mackerras #include <linux/pci.h>
259b6b563cSPaul Mackerras #include <linux/proc_fs.h>
269b6b563cSPaul Mackerras #include <linux/delay.h>
279b6b563cSPaul Mackerras #include <linux/initrd.h>
289b6b563cSPaul Mackerras #include <linux/bitops.h>
2965fddcfcSMike Rapoport #include <linux/pgtable.h>
309b6b563cSPaul Mackerras #include <asm/prom.h>
319b6b563cSPaul Mackerras #include <asm/rtas.h>
329b6b563cSPaul Mackerras #include <asm/page.h>
339b6b563cSPaul Mackerras #include <asm/processor.h>
349b6b563cSPaul Mackerras #include <asm/irq.h>
359b6b563cSPaul Mackerras #include <asm/io.h>
369b6b563cSPaul Mackerras #include <asm/smp.h>
379b6b563cSPaul Mackerras #include <asm/mmu.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 */
4833def849SJoe Perches #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;
172bf6b7661SAneesh Kumar K.V static bool __prombss prom_radix_gtse_disable;
173a3bf9fbdSGreg Kurz static bool __prombss prom_xive_disable;
174c886087cSBenjamin Herrenschmidt #endif
175014d02cbSSuraj Jitindar Singh 
1766a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
1776a9c930bSRam Pai static bool __prombss prom_svm_enable;
1786a9c930bSRam Pai #endif
1796a9c930bSRam Pai 
180014d02cbSSuraj Jitindar Singh struct platform_support {
181014d02cbSSuraj Jitindar Singh 	bool hash_mmu;
182014d02cbSSuraj Jitindar Singh 	bool radix_mmu;
183014d02cbSSuraj Jitindar Singh 	bool radix_gtse;
184ac5e5a54SCédric Le Goater 	bool xive;
185014d02cbSSuraj Jitindar Singh };
186014d02cbSSuraj Jitindar Singh 
187e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
188e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
189e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
190e8222502SBenjamin Herrenschmidt  */
191e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
192e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
193e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
194e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
195e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
196e8222502SBenjamin Herrenschmidt 
197e63334e5SBenjamin Herrenschmidt static int __prombss of_platform;
1989b6b563cSPaul Mackerras 
199e63334e5SBenjamin Herrenschmidt static char __prombss prom_cmd_line[COMMAND_LINE_SIZE];
2009b6b563cSPaul Mackerras 
201e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_memory_limit;
202cf68787bSBenjamin Krill 
203e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top;
204e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top_high;
205e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_bottom;
206e63334e5SBenjamin Herrenschmidt static unsigned long __prombss rmo_top;
207e63334e5SBenjamin Herrenschmidt static unsigned long __prombss ram_top;
2089b6b563cSPaul Mackerras 
209e63334e5SBenjamin Herrenschmidt static struct mem_map_entry __prombss mem_reserve_map[MEM_RESERVE_MAP_SIZE];
210e63334e5SBenjamin Herrenschmidt static int __prombss mem_reserve_cnt;
2119b6b563cSPaul Mackerras 
212e63334e5SBenjamin Herrenschmidt static cell_t __prombss regbuf[1024];
2139b6b563cSPaul Mackerras 
2148ca2d515SBenjamin Herrenschmidt static bool  __prombss rtas_has_query_cpu_stopped;
215dbe78b40SBenjamin Herrenschmidt 
2169b6b563cSPaul Mackerras 
2179b6b563cSPaul Mackerras /*
2189b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2199b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2209b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2219b6b563cSPaul Mackerras  * it is valid
2229b6b563cSPaul Mackerras  */
2239b6b563cSPaul Mackerras 
2249b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2259b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2269b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2279b6b563cSPaul Mackerras 
228450e7dd4SChristophe Leroy /* Copied from lib/string.c and lib/kstrtox.c */
229450e7dd4SChristophe Leroy 
230450e7dd4SChristophe Leroy static int __init prom_strcmp(const char *cs, const char *ct)
231450e7dd4SChristophe Leroy {
232450e7dd4SChristophe Leroy 	unsigned char c1, c2;
233450e7dd4SChristophe Leroy 
234450e7dd4SChristophe Leroy 	while (1) {
235450e7dd4SChristophe Leroy 		c1 = *cs++;
236450e7dd4SChristophe Leroy 		c2 = *ct++;
237450e7dd4SChristophe Leroy 		if (c1 != c2)
238450e7dd4SChristophe Leroy 			return c1 < c2 ? -1 : 1;
239450e7dd4SChristophe Leroy 		if (!c1)
240450e7dd4SChristophe Leroy 			break;
241450e7dd4SChristophe Leroy 	}
242450e7dd4SChristophe Leroy 	return 0;
243450e7dd4SChristophe Leroy }
244450e7dd4SChristophe Leroy 
245450e7dd4SChristophe Leroy static char __init *prom_strcpy(char *dest, const char *src)
246450e7dd4SChristophe Leroy {
247450e7dd4SChristophe Leroy 	char *tmp = dest;
248450e7dd4SChristophe Leroy 
249450e7dd4SChristophe Leroy 	while ((*dest++ = *src++) != '\0')
250450e7dd4SChristophe Leroy 		/* nothing */;
251450e7dd4SChristophe Leroy 	return tmp;
252450e7dd4SChristophe Leroy }
253450e7dd4SChristophe Leroy 
254450e7dd4SChristophe Leroy static int __init prom_strncmp(const char *cs, const char *ct, size_t count)
255450e7dd4SChristophe Leroy {
256450e7dd4SChristophe Leroy 	unsigned char c1, c2;
257450e7dd4SChristophe Leroy 
258450e7dd4SChristophe Leroy 	while (count) {
259450e7dd4SChristophe Leroy 		c1 = *cs++;
260450e7dd4SChristophe Leroy 		c2 = *ct++;
261450e7dd4SChristophe Leroy 		if (c1 != c2)
262450e7dd4SChristophe Leroy 			return c1 < c2 ? -1 : 1;
263450e7dd4SChristophe Leroy 		if (!c1)
264450e7dd4SChristophe Leroy 			break;
265450e7dd4SChristophe Leroy 		count--;
266450e7dd4SChristophe Leroy 	}
267450e7dd4SChristophe Leroy 	return 0;
268450e7dd4SChristophe Leroy }
269450e7dd4SChristophe Leroy 
270450e7dd4SChristophe Leroy static size_t __init prom_strlen(const char *s)
271450e7dd4SChristophe Leroy {
272450e7dd4SChristophe Leroy 	const char *sc;
273450e7dd4SChristophe Leroy 
274450e7dd4SChristophe Leroy 	for (sc = s; *sc != '\0'; ++sc)
275450e7dd4SChristophe Leroy 		/* nothing */;
276450e7dd4SChristophe Leroy 	return sc - s;
277450e7dd4SChristophe Leroy }
278450e7dd4SChristophe Leroy 
279450e7dd4SChristophe Leroy static int __init prom_memcmp(const void *cs, const void *ct, size_t count)
280450e7dd4SChristophe Leroy {
281450e7dd4SChristophe Leroy 	const unsigned char *su1, *su2;
282450e7dd4SChristophe Leroy 	int res = 0;
283450e7dd4SChristophe Leroy 
284450e7dd4SChristophe Leroy 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
285450e7dd4SChristophe Leroy 		if ((res = *su1 - *su2) != 0)
286450e7dd4SChristophe Leroy 			break;
287450e7dd4SChristophe Leroy 	return res;
288450e7dd4SChristophe Leroy }
289450e7dd4SChristophe Leroy 
290450e7dd4SChristophe Leroy static char __init *prom_strstr(const char *s1, const char *s2)
291450e7dd4SChristophe Leroy {
292450e7dd4SChristophe Leroy 	size_t l1, l2;
293450e7dd4SChristophe Leroy 
294450e7dd4SChristophe Leroy 	l2 = prom_strlen(s2);
295450e7dd4SChristophe Leroy 	if (!l2)
296450e7dd4SChristophe Leroy 		return (char *)s1;
297450e7dd4SChristophe Leroy 	l1 = prom_strlen(s1);
298450e7dd4SChristophe Leroy 	while (l1 >= l2) {
299450e7dd4SChristophe Leroy 		l1--;
300450e7dd4SChristophe Leroy 		if (!prom_memcmp(s1, s2, l2))
301450e7dd4SChristophe Leroy 			return (char *)s1;
302450e7dd4SChristophe Leroy 		s1++;
303450e7dd4SChristophe Leroy 	}
304450e7dd4SChristophe Leroy 	return NULL;
305450e7dd4SChristophe Leroy }
306450e7dd4SChristophe Leroy 
307d79fbb3aSChris Packham static size_t __init prom_strlcat(char *dest, const char *src, size_t count)
308450e7dd4SChristophe Leroy {
309d79fbb3aSChris Packham 	size_t dsize = prom_strlen(dest);
310d79fbb3aSChris Packham 	size_t len = prom_strlen(src);
311d79fbb3aSChris Packham 	size_t res = dsize + len;
312450e7dd4SChristophe Leroy 
313d79fbb3aSChris Packham 	/* This would be a bug */
314d79fbb3aSChris Packham 	if (dsize >= count)
315d79fbb3aSChris Packham 		return count;
316d79fbb3aSChris Packham 
317d79fbb3aSChris Packham 	dest += dsize;
318d79fbb3aSChris Packham 	count -= dsize;
319d79fbb3aSChris Packham 	if (len >= count)
320d79fbb3aSChris Packham 		len = count-1;
321450e7dd4SChristophe Leroy 	memcpy(dest, src, len);
322d79fbb3aSChris Packham 	dest[len] = 0;
323d79fbb3aSChris Packham 	return res;
324d79fbb3aSChris Packham 
325450e7dd4SChristophe Leroy }
326450e7dd4SChristophe Leroy 
327450e7dd4SChristophe Leroy #ifdef CONFIG_PPC_PSERIES
328450e7dd4SChristophe Leroy static int __init prom_strtobool(const char *s, bool *res)
329450e7dd4SChristophe Leroy {
330450e7dd4SChristophe Leroy 	if (!s)
331450e7dd4SChristophe Leroy 		return -EINVAL;
332450e7dd4SChristophe Leroy 
333450e7dd4SChristophe Leroy 	switch (s[0]) {
334450e7dd4SChristophe Leroy 	case 'y':
335450e7dd4SChristophe Leroy 	case 'Y':
336450e7dd4SChristophe Leroy 	case '1':
337450e7dd4SChristophe Leroy 		*res = true;
338450e7dd4SChristophe Leroy 		return 0;
339450e7dd4SChristophe Leroy 	case 'n':
340450e7dd4SChristophe Leroy 	case 'N':
341450e7dd4SChristophe Leroy 	case '0':
342450e7dd4SChristophe Leroy 		*res = false;
343450e7dd4SChristophe Leroy 		return 0;
344450e7dd4SChristophe Leroy 	case 'o':
345450e7dd4SChristophe Leroy 	case 'O':
346450e7dd4SChristophe Leroy 		switch (s[1]) {
347450e7dd4SChristophe Leroy 		case 'n':
348450e7dd4SChristophe Leroy 		case 'N':
349450e7dd4SChristophe Leroy 			*res = true;
350450e7dd4SChristophe Leroy 			return 0;
351450e7dd4SChristophe Leroy 		case 'f':
352450e7dd4SChristophe Leroy 		case 'F':
353450e7dd4SChristophe Leroy 			*res = false;
354450e7dd4SChristophe Leroy 			return 0;
355450e7dd4SChristophe Leroy 		default:
356450e7dd4SChristophe Leroy 			break;
357450e7dd4SChristophe Leroy 		}
358*49a41365SNick Desaulniers 		break;
359450e7dd4SChristophe Leroy 	default:
360450e7dd4SChristophe Leroy 		break;
361450e7dd4SChristophe Leroy 	}
362450e7dd4SChristophe Leroy 
363450e7dd4SChristophe Leroy 	return -EINVAL;
364450e7dd4SChristophe Leroy }
365450e7dd4SChristophe Leroy #endif
3669b6b563cSPaul Mackerras 
3679b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
3689b6b563cSPaul Mackerras  * firmware.
3699b6b563cSPaul Mackerras  */
3709b6b563cSPaul Mackerras 
3719b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
3729b6b563cSPaul Mackerras {
3739b6b563cSPaul Mackerras 	int i;
3749b6b563cSPaul Mackerras 	struct prom_args args;
3759b6b563cSPaul Mackerras 	va_list list;
3769b6b563cSPaul Mackerras 
377493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
378493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
379493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
3809b6b563cSPaul Mackerras 
3819b6b563cSPaul Mackerras 	va_start(list, nret);
3829b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
383493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
3849b6b563cSPaul Mackerras 	va_end(list);
3859b6b563cSPaul Mackerras 
3869b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
3879b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
3889b6b563cSPaul Mackerras 
3895827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
390c4988820SPaul Mackerras 		return PROM_ERROR;
3919b6b563cSPaul Mackerras 
392493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
3939b6b563cSPaul Mackerras }
3949b6b563cSPaul Mackerras 
3959b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
3969b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
3979b6b563cSPaul Mackerras {
3989b6b563cSPaul Mackerras 	int i;
3999b6b563cSPaul Mackerras 	struct prom_args args;
4009b6b563cSPaul Mackerras 	va_list list;
4019b6b563cSPaul Mackerras 
402493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
403493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
404493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
4059b6b563cSPaul Mackerras 
4069b6b563cSPaul Mackerras 	va_start(list, rets);
4079b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
408493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
4099b6b563cSPaul Mackerras 	va_end(list);
4109b6b563cSPaul Mackerras 
4119b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
412ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
4139b6b563cSPaul Mackerras 
4145827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
415c4988820SPaul Mackerras 		return PROM_ERROR;
4169b6b563cSPaul Mackerras 
4179b6b563cSPaul Mackerras 	if (rets != NULL)
4189b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
419493adffcSBenjamin Herrenschmidt 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
4209b6b563cSPaul Mackerras 
421493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
4229b6b563cSPaul Mackerras }
4239b6b563cSPaul Mackerras 
4249b6b563cSPaul Mackerras 
4259b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
4269b6b563cSPaul Mackerras {
4279b6b563cSPaul Mackerras 	const char *p, *q;
4289b6b563cSPaul Mackerras 
4295827d416SAnton Blanchard 	if (prom.stdout == 0)
4309b6b563cSPaul Mackerras 		return;
4319b6b563cSPaul Mackerras 
4329b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
4339b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
4349b6b563cSPaul Mackerras 			;
4359b6b563cSPaul Mackerras 		if (q > p)
4365827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
4379b6b563cSPaul Mackerras 		if (*q == 0)
4389b6b563cSPaul Mackerras 			break;
4399b6b563cSPaul Mackerras 		++q;
4405827d416SAnton Blanchard 		call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
4419b6b563cSPaul Mackerras 	}
4429b6b563cSPaul Mackerras }
4439b6b563cSPaul Mackerras 
4449b6b563cSPaul Mackerras 
4458af1da40SMathieu Malaterre /*
4468af1da40SMathieu Malaterre  * Both prom_print_hex & prom_print_dec takes an unsigned long as input so that
4478af1da40SMathieu Malaterre  * we do not need __udivdi3 or __umoddi3 on 32bits.
4488af1da40SMathieu Malaterre  */
4499b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
4509b6b563cSPaul Mackerras {
4519b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
4529b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
4539b6b563cSPaul Mackerras 
4549b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
4559b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
4569b6b563cSPaul Mackerras 		if (buf[i] > '9')
4579b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
4589b6b563cSPaul Mackerras 		val >>= 4;
4599b6b563cSPaul Mackerras 	}
4609b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
4615827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf, nibbles);
4629b6b563cSPaul Mackerras }
4639b6b563cSPaul Mackerras 
4642c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */
4652c48a7d6SMichael Neuling #define UL_DIGITS 21
4662c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val)
4672c48a7d6SMichael Neuling {
4682c48a7d6SMichael Neuling 	int i, size;
4692c48a7d6SMichael Neuling 	char buf[UL_DIGITS+1];
4702c48a7d6SMichael Neuling 
4712c48a7d6SMichael Neuling 	for (i = UL_DIGITS-1; i >= 0;  i--) {
4722c48a7d6SMichael Neuling 		buf[i] = (val % 10) + '0';
4732c48a7d6SMichael Neuling 		val = val/10;
4742c48a7d6SMichael Neuling 		if (val == 0)
4752c48a7d6SMichael Neuling 			break;
4762c48a7d6SMichael Neuling 	}
4772c48a7d6SMichael Neuling 	/* shift stuff down */
4782c48a7d6SMichael Neuling 	size = UL_DIGITS - i;
4795827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf+i, size);
4802c48a7d6SMichael Neuling }
4819b6b563cSPaul Mackerras 
482eae5f709SMathieu Malaterre __printf(1, 2)
4839b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
4849b6b563cSPaul Mackerras {
4859b6b563cSPaul Mackerras 	const char *p, *q, *s;
4869b6b563cSPaul Mackerras 	va_list args;
4879b6b563cSPaul Mackerras 	unsigned long v;
488af277149SBenjamin Herrenschmidt 	long vs;
4898af1da40SMathieu Malaterre 	int n = 0;
4909b6b563cSPaul Mackerras 
4919b6b563cSPaul Mackerras 	va_start(args, format);
4929b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
4939b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
4949b6b563cSPaul Mackerras 			;
4959b6b563cSPaul Mackerras 		if (q > p)
4965827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
4979b6b563cSPaul Mackerras 		if (*q == 0)
4989b6b563cSPaul Mackerras 			break;
4999b6b563cSPaul Mackerras 		if (*q == '\n') {
5009b6b563cSPaul Mackerras 			++q;
5015827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout,
5029b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
5039b6b563cSPaul Mackerras 			continue;
5049b6b563cSPaul Mackerras 		}
5059b6b563cSPaul Mackerras 		++q;
5069b6b563cSPaul Mackerras 		if (*q == 0)
5079b6b563cSPaul Mackerras 			break;
5088af1da40SMathieu Malaterre 		while (*q == 'l') {
5098af1da40SMathieu Malaterre 			++q;
5108af1da40SMathieu Malaterre 			++n;
5118af1da40SMathieu Malaterre 		}
5129b6b563cSPaul Mackerras 		switch (*q) {
5139b6b563cSPaul Mackerras 		case 's':
5149b6b563cSPaul Mackerras 			++q;
5159b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
5169b6b563cSPaul Mackerras 			prom_print(s);
5179b6b563cSPaul Mackerras 			break;
5189b6b563cSPaul Mackerras 		case 'x':
5199b6b563cSPaul Mackerras 			++q;
5208af1da40SMathieu Malaterre 			switch (n) {
5218af1da40SMathieu Malaterre 			case 0:
5228af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
5238af1da40SMathieu Malaterre 				break;
5248af1da40SMathieu Malaterre 			case 1:
5259b6b563cSPaul Mackerras 				v = va_arg(args, unsigned long);
5268af1da40SMathieu Malaterre 				break;
5278af1da40SMathieu Malaterre 			case 2:
5288af1da40SMathieu Malaterre 			default:
5298af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
5308af1da40SMathieu Malaterre 				break;
5318af1da40SMathieu Malaterre 			}
5329b6b563cSPaul Mackerras 			prom_print_hex(v);
5339b6b563cSPaul Mackerras 			break;
5348af1da40SMathieu Malaterre 		case 'u':
5358af1da40SMathieu Malaterre 			++q;
5368af1da40SMathieu Malaterre 			switch (n) {
5378af1da40SMathieu Malaterre 			case 0:
5388af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
5398af1da40SMathieu Malaterre 				break;
5408af1da40SMathieu Malaterre 			case 1:
5418af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long);
5428af1da40SMathieu Malaterre 				break;
5438af1da40SMathieu Malaterre 			case 2:
5448af1da40SMathieu Malaterre 			default:
5458af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
5468af1da40SMathieu Malaterre 				break;
5478af1da40SMathieu Malaterre 			}
5488af1da40SMathieu Malaterre 			prom_print_dec(v);
5498af1da40SMathieu Malaterre 			break;
550af277149SBenjamin Herrenschmidt 		case 'd':
551af277149SBenjamin Herrenschmidt 			++q;
5528af1da40SMathieu Malaterre 			switch (n) {
5538af1da40SMathieu Malaterre 			case 0:
554af277149SBenjamin Herrenschmidt 				vs = va_arg(args, int);
555af277149SBenjamin Herrenschmidt 				break;
5568af1da40SMathieu Malaterre 			case 1:
557af277149SBenjamin Herrenschmidt 				vs = va_arg(args, long);
5588af1da40SMathieu Malaterre 				break;
5598af1da40SMathieu Malaterre 			case 2:
5608af1da40SMathieu Malaterre 			default:
5618af1da40SMathieu Malaterre 				vs = va_arg(args, long long);
5628af1da40SMathieu Malaterre 				break;
5638af1da40SMathieu Malaterre 			}
564af277149SBenjamin Herrenschmidt 			if (vs < 0) {
5655827d416SAnton Blanchard 				prom_print("-");
566af277149SBenjamin Herrenschmidt 				vs = -vs;
567af277149SBenjamin Herrenschmidt 			}
568af277149SBenjamin Herrenschmidt 			prom_print_dec(vs);
5692c48a7d6SMichael Neuling 			break;
5709b6b563cSPaul Mackerras 		}
5719b6b563cSPaul Mackerras 	}
5721b855e16SDaniel Axtens 	va_end(args);
5739b6b563cSPaul Mackerras }
5749b6b563cSPaul Mackerras 
5759b6b563cSPaul Mackerras 
576a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
577a575b807SPaul Mackerras 				unsigned long align)
578a575b807SPaul Mackerras {
579a575b807SPaul Mackerras 
580a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
581a23414beSPaul Mackerras 		/*
582a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
583a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
584a23414beSPaul Mackerras 		 */
585a23414beSPaul Mackerras 		int ret;
586a23414beSPaul Mackerras 		prom_arg_t result;
587a23414beSPaul Mackerras 
588a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
5895827d416SAnton Blanchard 				    ADDR("claim"), prom.memory,
590a23414beSPaul Mackerras 				    align, size, virt);
591a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
592a23414beSPaul Mackerras 			return -1;
593a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
5945827d416SAnton Blanchard 				    ADDR("claim"), prom.mmumap,
595a23414beSPaul Mackerras 				    align, size, virt);
596a23414beSPaul Mackerras 		if (ret != 0) {
597a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
5985827d416SAnton Blanchard 				  prom.memory, size, virt);
599a23414beSPaul Mackerras 			return -1;
600a23414beSPaul Mackerras 		}
601a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
602a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
6035827d416SAnton Blanchard 			  ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
604a23414beSPaul Mackerras 		return virt;
605a23414beSPaul Mackerras 	}
606a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
607a23414beSPaul Mackerras 			 (prom_arg_t)align);
608a575b807SPaul Mackerras }
609a575b807SPaul Mackerras 
6109b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
6119b6b563cSPaul Mackerras {
6129b6b563cSPaul Mackerras 	prom_print(reason);
613add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
614add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
6155827d416SAnton Blanchard 	if (of_platform == PLATFORM_POWERMAC)
616add60ef3SOlaf Hering 		asm("trap\n");
617add60ef3SOlaf Hering 
6181d9a4731SStephen Rothwell 	/* ToDo: should put up an SRC here on pSeries */
6199b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
6209b6b563cSPaul Mackerras 
6219b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
6229b6b563cSPaul Mackerras 		;
6239b6b563cSPaul Mackerras }
6249b6b563cSPaul Mackerras 
6259b6b563cSPaul Mackerras 
6269b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
6279b6b563cSPaul Mackerras {
6289b6b563cSPaul Mackerras 	phandle node;
6299b6b563cSPaul Mackerras 
6309b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
6319b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
6329b6b563cSPaul Mackerras 		return 1;
6339b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
6349b6b563cSPaul Mackerras 		return 1;
6359b6b563cSPaul Mackerras 	for (;;) {
6369b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
6379b6b563cSPaul Mackerras 			return 0;
6389b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
6399b6b563cSPaul Mackerras 			return 1;
6409b6b563cSPaul Mackerras 	}
6419b6b563cSPaul Mackerras }
6429b6b563cSPaul Mackerras 
643480795a0SMasahiro Yamada static inline int __init prom_getprop(phandle node, const char *pname,
6449b6b563cSPaul Mackerras 				      void *value, size_t valuelen)
6459b6b563cSPaul Mackerras {
6469b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
6479b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
6489b6b563cSPaul Mackerras }
6499b6b563cSPaul Mackerras 
650480795a0SMasahiro Yamada static inline int __init prom_getproplen(phandle node, const char *pname)
6519b6b563cSPaul Mackerras {
6529b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
6539b6b563cSPaul Mackerras }
6549b6b563cSPaul Mackerras 
655a23414beSPaul Mackerras static void add_string(char **str, const char *q)
6569b6b563cSPaul Mackerras {
657a23414beSPaul Mackerras 	char *p = *str;
658a23414beSPaul Mackerras 
659a23414beSPaul Mackerras 	while (*q)
660a23414beSPaul Mackerras 		*p++ = *q++;
661a23414beSPaul Mackerras 	*p++ = ' ';
662a23414beSPaul Mackerras 	*str = p;
663a23414beSPaul Mackerras }
664a23414beSPaul Mackerras 
665a23414beSPaul Mackerras static char *tohex(unsigned int x)
666a23414beSPaul Mackerras {
6678ca2d515SBenjamin Herrenschmidt 	static const char digits[] __initconst = "0123456789abcdef";
6688ca2d515SBenjamin Herrenschmidt 	static char result[9] __prombss;
669a23414beSPaul Mackerras 	int i;
670a23414beSPaul Mackerras 
671a23414beSPaul Mackerras 	result[8] = 0;
672a23414beSPaul Mackerras 	i = 8;
673a23414beSPaul Mackerras 	do {
674a23414beSPaul Mackerras 		--i;
675a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
676a23414beSPaul Mackerras 		x >>= 4;
677a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
678a23414beSPaul Mackerras 	return &result[i];
679a23414beSPaul Mackerras }
680a23414beSPaul Mackerras 
681a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
682a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
683a23414beSPaul Mackerras {
684a23414beSPaul Mackerras 	char cmd[256], *p;
685a23414beSPaul Mackerras 
686a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
6879b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
6889b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
689a23414beSPaul Mackerras 
690a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
691a23414beSPaul Mackerras 	p = cmd;
692a23414beSPaul Mackerras 	add_string(&p, "dev");
693a23414beSPaul Mackerras 	add_string(&p, nodename);
694a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
695a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
696a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
697450e7dd4SChristophe Leroy 	add_string(&p, tohex(prom_strlen(pname)));
698a23414beSPaul Mackerras 	add_string(&p, "property");
699a23414beSPaul Mackerras 	*p = 0;
700a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
7019b6b563cSPaul Mackerras }
7029b6b563cSPaul Mackerras 
7035827d416SAnton Blanchard /* We can't use the standard versions because of relocation headaches. */
704cf68787bSBenjamin Krill #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
705cf68787bSBenjamin Krill 			 || ('a' <= (c) && (c) <= 'f') \
706cf68787bSBenjamin Krill 			 || ('A' <= (c) && (c) <= 'F'))
707cf68787bSBenjamin Krill 
708cf68787bSBenjamin Krill #define isdigit(c)	('0' <= (c) && (c) <= '9')
709cf68787bSBenjamin Krill #define islower(c)	('a' <= (c) && (c) <= 'z')
710cf68787bSBenjamin Krill #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
711cf68787bSBenjamin Krill 
712493adffcSBenjamin Herrenschmidt static unsigned long prom_strtoul(const char *cp, const char **endp)
713cf68787bSBenjamin Krill {
714cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
715cf68787bSBenjamin Krill 
716cf68787bSBenjamin Krill 	if (*cp == '0') {
717cf68787bSBenjamin Krill 		base = 8;
718cf68787bSBenjamin Krill 		cp++;
719cf68787bSBenjamin Krill 		if (toupper(*cp) == 'X') {
720cf68787bSBenjamin Krill 			cp++;
721cf68787bSBenjamin Krill 			base = 16;
722cf68787bSBenjamin Krill 		}
723cf68787bSBenjamin Krill 	}
724cf68787bSBenjamin Krill 
725cf68787bSBenjamin Krill 	while (isxdigit(*cp) &&
726cf68787bSBenjamin Krill 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
727cf68787bSBenjamin Krill 		result = result * base + value;
728cf68787bSBenjamin Krill 		cp++;
729cf68787bSBenjamin Krill 	}
730cf68787bSBenjamin Krill 
731cf68787bSBenjamin Krill 	if (endp)
732cf68787bSBenjamin Krill 		*endp = cp;
733cf68787bSBenjamin Krill 
734cf68787bSBenjamin Krill 	return result;
735cf68787bSBenjamin Krill }
736cf68787bSBenjamin Krill 
737493adffcSBenjamin Herrenschmidt static unsigned long prom_memparse(const char *ptr, const char **retptr)
738cf68787bSBenjamin Krill {
739cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
740cf68787bSBenjamin Krill 	int shift = 0;
741cf68787bSBenjamin Krill 
742cf68787bSBenjamin Krill 	/*
743cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
744cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
745cf68787bSBenjamin Krill 	 * the address we're linked at.
746cf68787bSBenjamin Krill 	 */
747cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
748cf68787bSBenjamin Krill 		shift = 30;
749cf68787bSBenjamin Krill 
750cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
751cf68787bSBenjamin Krill 		shift = 20;
752cf68787bSBenjamin Krill 
753cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
754cf68787bSBenjamin Krill 		shift = 10;
755cf68787bSBenjamin Krill 
756cf68787bSBenjamin Krill 	if (shift) {
757cf68787bSBenjamin Krill 		ret <<= shift;
758cf68787bSBenjamin Krill 		(*retptr)++;
759cf68787bSBenjamin Krill 	}
760cf68787bSBenjamin Krill 
761cf68787bSBenjamin Krill 	return ret;
762cf68787bSBenjamin Krill }
763cf68787bSBenjamin Krill 
7649b6b563cSPaul Mackerras /*
7659b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
7669b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
7679b6b563cSPaul Mackerras  */
7689b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
7699b6b563cSPaul Mackerras {
770cc5d0189SBenjamin Herrenschmidt 	const char *opt;
771cf68787bSBenjamin Krill 
772cc5d0189SBenjamin Herrenschmidt 	char *p;
773cbe46bd4SChristophe Leroy 	int l = 0;
7749b6b563cSPaul Mackerras 
7755827d416SAnton Blanchard 	prom_cmd_line[0] = 0;
7765827d416SAnton Blanchard 	p = prom_cmd_line;
777d79fbb3aSChris Packham 
778d79fbb3aSChris Packham 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && (long)prom.chosen > 0)
7795827d416SAnton Blanchard 		l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
780d79fbb3aSChris Packham 
781d79fbb3aSChris Packham 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) || l <= 0 || p[0] == '\0')
782d79fbb3aSChris Packham 		prom_strlcat(prom_cmd_line, " " CONFIG_CMDLINE,
783d79fbb3aSChris Packham 			     sizeof(prom_cmd_line));
784d79fbb3aSChris Packham 
7855827d416SAnton Blanchard 	prom_printf("command line: %s\n", prom_cmd_line);
7869b6b563cSPaul Mackerras 
7879b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
788450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "iommu=");
7899b6b563cSPaul Mackerras 	if (opt) {
7909b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
7919b6b563cSPaul Mackerras 		opt += 6;
7929b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
7939b6b563cSPaul Mackerras 			opt++;
794450e7dd4SChristophe Leroy 		if (!prom_strncmp(opt, "off", 3))
7955827d416SAnton Blanchard 			prom_iommu_off = 1;
796450e7dd4SChristophe Leroy 		else if (!prom_strncmp(opt, "force", 5))
7975827d416SAnton Blanchard 			prom_iommu_force_on = 1;
7989b6b563cSPaul Mackerras 	}
7999b6b563cSPaul Mackerras #endif
800450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "mem=");
801cf68787bSBenjamin Krill 	if (opt) {
802cf68787bSBenjamin Krill 		opt += 4;
8035827d416SAnton Blanchard 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
804cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
805cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
8065827d416SAnton Blanchard 		prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
807cf68787bSBenjamin Krill #endif
808cf68787bSBenjamin Krill 	}
809014d02cbSSuraj Jitindar Singh 
810c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
811c886087cSBenjamin Herrenschmidt 	prom_radix_disable = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
812450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "disable_radix");
813014d02cbSSuraj Jitindar Singh 	if (opt) {
8147a22d632SAneesh Kumar K.V 		opt += 13;
8157a22d632SAneesh Kumar K.V 		if (*opt && *opt == '=') {
8167a22d632SAneesh Kumar K.V 			bool val;
8177a22d632SAneesh Kumar K.V 
818450e7dd4SChristophe Leroy 			if (prom_strtobool(++opt, &val))
8197a22d632SAneesh Kumar K.V 				prom_radix_disable = false;
8207a22d632SAneesh Kumar K.V 			else
8217a22d632SAneesh Kumar K.V 				prom_radix_disable = val;
8227a22d632SAneesh Kumar K.V 		} else
823014d02cbSSuraj Jitindar Singh 			prom_radix_disable = true;
824014d02cbSSuraj Jitindar Singh 	}
8257a22d632SAneesh Kumar K.V 	if (prom_radix_disable)
8267a22d632SAneesh Kumar K.V 		prom_debug("Radix disabled from cmdline\n");
827a3bf9fbdSGreg Kurz 
828bf6b7661SAneesh Kumar K.V 	opt = prom_strstr(prom_cmd_line, "radix_hcall_invalidate=on");
829bf6b7661SAneesh Kumar K.V 	if (opt) {
830bf6b7661SAneesh Kumar K.V 		prom_radix_gtse_disable = true;
831bf6b7661SAneesh Kumar K.V 		prom_debug("Radix GTSE disabled from cmdline\n");
832bf6b7661SAneesh Kumar K.V 	}
833bf6b7661SAneesh Kumar K.V 
834a3bf9fbdSGreg Kurz 	opt = prom_strstr(prom_cmd_line, "xive=off");
835a3bf9fbdSGreg Kurz 	if (opt) {
836a3bf9fbdSGreg Kurz 		prom_xive_disable = true;
837a3bf9fbdSGreg Kurz 		prom_debug("XIVE disabled from cmdline\n");
838a3bf9fbdSGreg Kurz 	}
839c886087cSBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
8406a9c930bSRam Pai 
8416a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
8426a9c930bSRam Pai 	opt = prom_strstr(prom_cmd_line, "svm=");
8436a9c930bSRam Pai 	if (opt) {
8446a9c930bSRam Pai 		bool val;
8456a9c930bSRam Pai 
8466a9c930bSRam Pai 		opt += sizeof("svm=") - 1;
8476a9c930bSRam Pai 		if (!prom_strtobool(opt, &val))
8486a9c930bSRam Pai 			prom_svm_enable = val;
8496a9c930bSRam Pai 	}
8506a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
8519b6b563cSPaul Mackerras }
8529b6b563cSPaul Mackerras 
85311fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
8549b6b563cSPaul Mackerras /*
855530b5e14SNathan Fontenot  * The architecture vector has an array of PVR mask/value pairs,
856530b5e14SNathan Fontenot  * followed by # option vectors - 1, followed by the option vectors.
857530b5e14SNathan Fontenot  *
858530b5e14SNathan Fontenot  * See prom.h for the definition of the bits specified in the
859530b5e14SNathan Fontenot  * architecture vector.
860f709bfacSPaul Mackerras  */
861f709bfacSPaul Mackerras 
862e8a4fd0aSMichael Ellerman /* Firmware expects the value to be n - 1, where n is the # of vectors */
863e8a4fd0aSMichael Ellerman #define NUM_VECTORS(n)		((n) - 1)
864e8a4fd0aSMichael Ellerman 
865e8a4fd0aSMichael Ellerman /*
866e8a4fd0aSMichael Ellerman  * Firmware expects 1 + n - 2, where n is the length of the option vector in
867e8a4fd0aSMichael Ellerman  * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
868e8a4fd0aSMichael Ellerman  */
869e8a4fd0aSMichael Ellerman #define VECTOR_LENGTH(n)	(1 + (n) - 2)
870e8a4fd0aSMichael Ellerman 
871d03d1d65SMichael Ellerman struct option_vector1 {
872d03d1d65SMichael Ellerman 	u8 byte1;
873d03d1d65SMichael Ellerman 	u8 arch_versions;
874cc3d2940SPaul Mackerras 	u8 arch_versions3;
875d03d1d65SMichael Ellerman } __packed;
876d03d1d65SMichael Ellerman 
877d03d1d65SMichael Ellerman struct option_vector2 {
878d03d1d65SMichael Ellerman 	u8 byte1;
879d03d1d65SMichael Ellerman 	__be16 reserved;
880d03d1d65SMichael Ellerman 	__be32 real_base;
881d03d1d65SMichael Ellerman 	__be32 real_size;
882d03d1d65SMichael Ellerman 	__be32 virt_base;
883d03d1d65SMichael Ellerman 	__be32 virt_size;
884d03d1d65SMichael Ellerman 	__be32 load_base;
885d03d1d65SMichael Ellerman 	__be32 min_rma;
886d03d1d65SMichael Ellerman 	__be32 min_load;
887d03d1d65SMichael Ellerman 	u8 min_rma_percent;
888d03d1d65SMichael Ellerman 	u8 max_pft_size;
889d03d1d65SMichael Ellerman } __packed;
890d03d1d65SMichael Ellerman 
891d03d1d65SMichael Ellerman struct option_vector3 {
892d03d1d65SMichael Ellerman 	u8 byte1;
893d03d1d65SMichael Ellerman 	u8 byte2;
894d03d1d65SMichael Ellerman } __packed;
895d03d1d65SMichael Ellerman 
896d03d1d65SMichael Ellerman struct option_vector4 {
897d03d1d65SMichael Ellerman 	u8 byte1;
898d03d1d65SMichael Ellerman 	u8 min_vp_cap;
899d03d1d65SMichael Ellerman } __packed;
900d03d1d65SMichael Ellerman 
901d03d1d65SMichael Ellerman struct option_vector5 {
902d03d1d65SMichael Ellerman 	u8 byte1;
903d03d1d65SMichael Ellerman 	u8 byte2;
904d03d1d65SMichael Ellerman 	u8 byte3;
905d03d1d65SMichael Ellerman 	u8 cmo;
906d03d1d65SMichael Ellerman 	u8 associativity;
907d03d1d65SMichael Ellerman 	u8 bin_opts;
908d03d1d65SMichael Ellerman 	u8 micro_checkpoint;
909d03d1d65SMichael Ellerman 	u8 reserved0;
910d03d1d65SMichael Ellerman 	__be32 max_cpus;
911d03d1d65SMichael Ellerman 	__be16 papr_level;
912d03d1d65SMichael Ellerman 	__be16 reserved1;
913d03d1d65SMichael Ellerman 	u8 platform_facilities;
914d03d1d65SMichael Ellerman 	u8 reserved2;
915d03d1d65SMichael Ellerman 	__be16 reserved3;
916d03d1d65SMichael Ellerman 	u8 subprocessors;
917cc3d2940SPaul Mackerras 	u8 byte22;
918cc3d2940SPaul Mackerras 	u8 intarch;
919cc3d2940SPaul Mackerras 	u8 mmu;
920014d02cbSSuraj Jitindar Singh 	u8 hash_ext;
921014d02cbSSuraj Jitindar Singh 	u8 radix_ext;
922d03d1d65SMichael Ellerman } __packed;
923d03d1d65SMichael Ellerman 
924d03d1d65SMichael Ellerman struct option_vector6 {
925d03d1d65SMichael Ellerman 	u8 reserved;
926d03d1d65SMichael Ellerman 	u8 secondary_pteg;
927d03d1d65SMichael Ellerman 	u8 os_name;
928d03d1d65SMichael Ellerman } __packed;
929d03d1d65SMichael Ellerman 
93076ffb578SMichael Ellerman struct ibm_arch_vec {
931a3ea40d5SAlistair Popple 	struct { u32 mask, val; } pvrs[14];
93276ffb578SMichael Ellerman 
93376ffb578SMichael Ellerman 	u8 num_vectors;
93476ffb578SMichael Ellerman 
93576ffb578SMichael Ellerman 	u8 vec1_len;
93676ffb578SMichael Ellerman 	struct option_vector1 vec1;
93776ffb578SMichael Ellerman 
93876ffb578SMichael Ellerman 	u8 vec2_len;
93976ffb578SMichael Ellerman 	struct option_vector2 vec2;
94076ffb578SMichael Ellerman 
94176ffb578SMichael Ellerman 	u8 vec3_len;
94276ffb578SMichael Ellerman 	struct option_vector3 vec3;
94376ffb578SMichael Ellerman 
94476ffb578SMichael Ellerman 	u8 vec4_len;
94576ffb578SMichael Ellerman 	struct option_vector4 vec4;
94676ffb578SMichael Ellerman 
94776ffb578SMichael Ellerman 	u8 vec5_len;
94876ffb578SMichael Ellerman 	struct option_vector5 vec5;
94976ffb578SMichael Ellerman 
95076ffb578SMichael Ellerman 	u8 vec6_len;
95176ffb578SMichael Ellerman 	struct option_vector6 vec6;
95276ffb578SMichael Ellerman } __packed;
95376ffb578SMichael Ellerman 
954d00e34b9SBenjamin Herrenschmidt static const struct ibm_arch_vec ibm_architecture_vec_template __initconst = {
95576ffb578SMichael Ellerman 	.pvrs = {
95676ffb578SMichael Ellerman 		{
95776ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffe0000), /* POWER5/POWER5+ */
95876ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003a0000),
95976ffb578SMichael Ellerman 		},
96076ffb578SMichael Ellerman 		{
96176ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER6 */
96276ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003e0000),
96376ffb578SMichael Ellerman 		},
96476ffb578SMichael Ellerman 		{
96576ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER7 */
96676ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003f0000),
96776ffb578SMichael Ellerman 		},
96876ffb578SMichael Ellerman 		{
96976ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8E */
97076ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004b0000),
97176ffb578SMichael Ellerman 		},
97276ffb578SMichael Ellerman 		{
97376ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8NVL */
97476ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004c0000),
97576ffb578SMichael Ellerman 		},
97676ffb578SMichael Ellerman 		{
97776ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8 */
97876ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004d0000),
97976ffb578SMichael Ellerman 		},
98076ffb578SMichael Ellerman 		{
981cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffff0000), /* POWER9 */
982cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x004e0000),
983cc3d2940SPaul Mackerras 		},
984cc3d2940SPaul Mackerras 		{
985a3ea40d5SAlistair Popple 			.mask = cpu_to_be32(0xffff0000), /* POWER10 */
986a3ea40d5SAlistair Popple 			.val  = cpu_to_be32(0x00800000),
987a3ea40d5SAlistair Popple 		},
988a3ea40d5SAlistair Popple 		{
989a3ea40d5SAlistair Popple 			.mask = cpu_to_be32(0xffffffff), /* all 3.1-compliant */
990a3ea40d5SAlistair Popple 			.val  = cpu_to_be32(0x0f000006),
991a3ea40d5SAlistair Popple 		},
992a3ea40d5SAlistair Popple 		{
993cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffffffff), /* all 3.00-compliant */
994cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x0f000005),
995cc3d2940SPaul Mackerras 		},
996cc3d2940SPaul Mackerras 		{
99776ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.07-compliant */
99876ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000004),
99976ffb578SMichael Ellerman 		},
100076ffb578SMichael Ellerman 		{
100176ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.06-compliant */
100276ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000003),
100376ffb578SMichael Ellerman 		},
100476ffb578SMichael Ellerman 		{
100576ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.05-compliant */
100676ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000002),
100776ffb578SMichael Ellerman 		},
100876ffb578SMichael Ellerman 		{
100976ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffffffe), /* all 2.04-compliant and earlier */
101076ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000001),
101176ffb578SMichael Ellerman 		},
101276ffb578SMichael Ellerman 	},
101376ffb578SMichael Ellerman 
101476ffb578SMichael Ellerman 	.num_vectors = NUM_VECTORS(6),
101576ffb578SMichael Ellerman 
101676ffb578SMichael Ellerman 	.vec1_len = VECTOR_LENGTH(sizeof(struct option_vector1)),
101776ffb578SMichael Ellerman 	.vec1 = {
101876ffb578SMichael Ellerman 		.byte1 = 0,
101976ffb578SMichael Ellerman 		.arch_versions = OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
102076ffb578SMichael Ellerman 				 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
1021a3ea40d5SAlistair Popple 		.arch_versions3 = OV1_PPC_3_00 | OV1_PPC_3_1,
102276ffb578SMichael Ellerman 	},
102376ffb578SMichael Ellerman 
102476ffb578SMichael Ellerman 	.vec2_len = VECTOR_LENGTH(sizeof(struct option_vector2)),
102576ffb578SMichael Ellerman 	/* option vector 2: Open Firmware options supported */
102676ffb578SMichael Ellerman 	.vec2 = {
102776ffb578SMichael Ellerman 		.byte1 = OV2_REAL_MODE,
102876ffb578SMichael Ellerman 		.reserved = 0,
102976ffb578SMichael Ellerman 		.real_base = cpu_to_be32(0xffffffff),
103076ffb578SMichael Ellerman 		.real_size = cpu_to_be32(0xffffffff),
103176ffb578SMichael Ellerman 		.virt_base = cpu_to_be32(0xffffffff),
103276ffb578SMichael Ellerman 		.virt_size = cpu_to_be32(0xffffffff),
103376ffb578SMichael Ellerman 		.load_base = cpu_to_be32(0xffffffff),
1034687da8fcSSukadev Bhattiprolu 		.min_rma = cpu_to_be32(512),		/* 512MB min RMA */
103576ffb578SMichael Ellerman 		.min_load = cpu_to_be32(0xffffffff),	/* full client load */
103676ffb578SMichael Ellerman 		.min_rma_percent = 0,	/* min RMA percentage of total RAM */
103776ffb578SMichael Ellerman 		.max_pft_size = 48,	/* max log_2(hash table size) */
103876ffb578SMichael Ellerman 	},
103976ffb578SMichael Ellerman 
104076ffb578SMichael Ellerman 	.vec3_len = VECTOR_LENGTH(sizeof(struct option_vector3)),
104176ffb578SMichael Ellerman 	/* option vector 3: processor options supported */
104276ffb578SMichael Ellerman 	.vec3 = {
104376ffb578SMichael Ellerman 		.byte1 = 0,			/* don't ignore, don't halt */
104476ffb578SMichael Ellerman 		.byte2 = OV3_FP | OV3_VMX | OV3_DFP,
104576ffb578SMichael Ellerman 	},
104676ffb578SMichael Ellerman 
104776ffb578SMichael Ellerman 	.vec4_len = VECTOR_LENGTH(sizeof(struct option_vector4)),
104876ffb578SMichael Ellerman 	/* option vector 4: IBM PAPR implementation */
104976ffb578SMichael Ellerman 	.vec4 = {
105076ffb578SMichael Ellerman 		.byte1 = 0,			/* don't halt */
105176ffb578SMichael Ellerman 		.min_vp_cap = OV4_MIN_ENT_CAP,	/* minimum VP entitled capacity */
105276ffb578SMichael Ellerman 	},
105376ffb578SMichael Ellerman 
105476ffb578SMichael Ellerman 	.vec5_len = VECTOR_LENGTH(sizeof(struct option_vector5)),
105576ffb578SMichael Ellerman 	/* option vector 5: PAPR/OF options */
105676ffb578SMichael Ellerman 	.vec5 = {
105776ffb578SMichael Ellerman 		.byte1 = 0,				/* don't ignore, don't halt */
105876ffb578SMichael Ellerman 		.byte2 = OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
105976ffb578SMichael Ellerman 		OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
106076ffb578SMichael Ellerman #ifdef CONFIG_PCI_MSI
106176ffb578SMichael Ellerman 		/* PCIe/MSI support.  Without MSI full PCIe is not supported */
106276ffb578SMichael Ellerman 		OV5_FEAT(OV5_MSI),
106376ffb578SMichael Ellerman #else
106476ffb578SMichael Ellerman 		0,
106576ffb578SMichael Ellerman #endif
106676ffb578SMichael Ellerman 		.byte3 = 0,
106776ffb578SMichael Ellerman 		.cmo =
106876ffb578SMichael Ellerman #ifdef CONFIG_PPC_SMLPAR
106976ffb578SMichael Ellerman 		OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
107076ffb578SMichael Ellerman #else
107176ffb578SMichael Ellerman 		0,
107276ffb578SMichael Ellerman #endif
107376ffb578SMichael Ellerman 		.associativity = OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
10743dbbaf20SMichael Roth 		.bin_opts = OV5_FEAT(OV5_RESIZE_HPT) | OV5_FEAT(OV5_HP_EVT),
107576ffb578SMichael Ellerman 		.micro_checkpoint = 0,
107676ffb578SMichael Ellerman 		.reserved0 = 0,
107776ffb578SMichael Ellerman 		.max_cpus = cpu_to_be32(NR_CPUS),	/* number of cores supported */
107876ffb578SMichael Ellerman 		.papr_level = 0,
107976ffb578SMichael Ellerman 		.reserved1 = 0,
108076ffb578SMichael Ellerman 		.platform_facilities = OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) | OV5_FEAT(OV5_PFO_HW_842),
108176ffb578SMichael Ellerman 		.reserved2 = 0,
108276ffb578SMichael Ellerman 		.reserved3 = 0,
108376ffb578SMichael Ellerman 		.subprocessors = 1,
10840a87ccd3STyrel Datwyler 		.byte22 = OV5_FEAT(OV5_DRMEM_V2) | OV5_FEAT(OV5_DRC_INFO),
1085cc3d2940SPaul Mackerras 		.intarch = 0,
1086014d02cbSSuraj Jitindar Singh 		.mmu = 0,
1087014d02cbSSuraj Jitindar Singh 		.hash_ext = 0,
1088014d02cbSSuraj Jitindar Singh 		.radix_ext = 0,
108976ffb578SMichael Ellerman 	},
109076ffb578SMichael Ellerman 
109176ffb578SMichael Ellerman 	/* option vector 6: IBM PAPR hints */
109276ffb578SMichael Ellerman 	.vec6_len = VECTOR_LENGTH(sizeof(struct option_vector6)),
109376ffb578SMichael Ellerman 	.vec6 = {
109476ffb578SMichael Ellerman 		.reserved = 0,
109576ffb578SMichael Ellerman 		.secondary_pteg = 0,
109676ffb578SMichael Ellerman 		.os_name = OV6_LINUX,
109776ffb578SMichael Ellerman 	},
109876ffb578SMichael Ellerman };
109976ffb578SMichael Ellerman 
1100a614f52eSBenjamin Herrenschmidt static struct ibm_arch_vec __prombss ibm_architecture_vec  ____cacheline_aligned;
1101a614f52eSBenjamin Herrenschmidt 
1102493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
1103493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
110430c69ca0SBenjamin Herrenschmidt static const struct fake_elf {
11059b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
11069b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
11079b6b563cSPaul Mackerras 	struct chrpnote {
11089b6b563cSPaul Mackerras 		u32	namesz;
11099b6b563cSPaul Mackerras 		u32	descsz;
11109b6b563cSPaul Mackerras 		u32	type;
11119b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
11129b6b563cSPaul Mackerras 		struct chrpdesc {
11139b6b563cSPaul Mackerras 			u32	real_mode;
11149b6b563cSPaul Mackerras 			u32	real_base;
11159b6b563cSPaul Mackerras 			u32	real_size;
11169b6b563cSPaul Mackerras 			u32	virt_base;
11179b6b563cSPaul Mackerras 			u32	virt_size;
11189b6b563cSPaul Mackerras 			u32	load_base;
11199b6b563cSPaul Mackerras 		} chrpdesc;
11209b6b563cSPaul Mackerras 	} chrpnote;
11219b6b563cSPaul Mackerras 	struct rpanote {
11229b6b563cSPaul Mackerras 		u32	namesz;
11239b6b563cSPaul Mackerras 		u32	descsz;
11249b6b563cSPaul Mackerras 		u32	type;
11259b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
11269b6b563cSPaul Mackerras 		struct rpadesc {
11279b6b563cSPaul Mackerras 			u32	lpar_affinity;
11289b6b563cSPaul Mackerras 			u32	min_rmo_size;
11299b6b563cSPaul Mackerras 			u32	min_rmo_percent;
11309b6b563cSPaul Mackerras 			u32	max_pft_size;
11319b6b563cSPaul Mackerras 			u32	splpar;
11329b6b563cSPaul Mackerras 			u32	min_load;
11339b6b563cSPaul Mackerras 			u32	new_mem_def;
11349b6b563cSPaul Mackerras 			u32	ignore_me;
11359b6b563cSPaul Mackerras 		} rpadesc;
11369b6b563cSPaul Mackerras 	} rpanote;
1137d00e34b9SBenjamin Herrenschmidt } fake_elf __initconst = {
11389b6b563cSPaul Mackerras 	.elfhdr = {
11399b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
11409b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
11419b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
11429b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
11439b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
11449b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
11459b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
11469b6b563cSPaul Mackerras 		.e_phnum = 2
11479b6b563cSPaul Mackerras 	},
11489b6b563cSPaul Mackerras 	.phdr = {
11499b6b563cSPaul Mackerras 		[0] = {
11509b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
11519b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
11529b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
11539b6b563cSPaul Mackerras 		}, [1] = {
11549b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
11559b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
11569b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
11579b6b563cSPaul Mackerras 		}
11589b6b563cSPaul Mackerras 	},
11599b6b563cSPaul Mackerras 	.chrpnote = {
11609b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
11619b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
11629b6b563cSPaul Mackerras 		.type = 0x1275,
11639b6b563cSPaul Mackerras 		.name = "PowerPC",
11649b6b563cSPaul Mackerras 		.chrpdesc = {
11659b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
11669b6b563cSPaul Mackerras 			.real_base = ~0U,
11679b6b563cSPaul Mackerras 			.real_size = ~0U,
11689b6b563cSPaul Mackerras 			.virt_base = ~0U,
11699b6b563cSPaul Mackerras 			.virt_size = ~0U,
11709b6b563cSPaul Mackerras 			.load_base = ~0U
11719b6b563cSPaul Mackerras 		},
11729b6b563cSPaul Mackerras 	},
11739b6b563cSPaul Mackerras 	.rpanote = {
11749b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
11759b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
11769b6b563cSPaul Mackerras 		.type = 0x12759999,
11779b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
11789b6b563cSPaul Mackerras 		.rpadesc = {
11795663a123SPaul Mackerras 			.lpar_affinity = 0,
11805663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
11819b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
11825663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
11839b6b563cSPaul Mackerras 			.splpar = 1,
11849b6b563cSPaul Mackerras 			.min_load = ~0U,
11855663a123SPaul Mackerras 			.new_mem_def = 0
11869b6b563cSPaul Mackerras 		}
11879b6b563cSPaul Mackerras 	}
11889b6b563cSPaul Mackerras };
1189493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
11909b6b563cSPaul Mackerras 
1191efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
1192efec959fSBenjamin Herrenschmidt {
1193efec959fSBenjamin Herrenschmidt 	phandle node;
1194efec959fSBenjamin Herrenschmidt 	char type[64];
1195efec959fSBenjamin Herrenschmidt 	unsigned int plen;
1196efec959fSBenjamin Herrenschmidt 
1197efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
1198efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
1199efec959fSBenjamin Herrenschmidt 		type[0] = 0;
1200efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
1201efec959fSBenjamin Herrenschmidt 
1202450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "cpu"))
1203efec959fSBenjamin Herrenschmidt 			continue;
1204efec959fSBenjamin Herrenschmidt 		/*
1205efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
1206efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
1207efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
1208efec959fSBenjamin Herrenschmidt 		 */
1209efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
1210efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
1211efec959fSBenjamin Herrenschmidt 			break;
1212efec959fSBenjamin Herrenschmidt 		plen >>= 2;
12132c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
1214efec959fSBenjamin Herrenschmidt 
1215efec959fSBenjamin Herrenschmidt 		/* Sanity check */
1216efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
12172c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
1218efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
1219efec959fSBenjamin Herrenschmidt 			return 1;
1220efec959fSBenjamin Herrenschmidt 		}
1221efec959fSBenjamin Herrenschmidt 		return plen;
1222efec959fSBenjamin Herrenschmidt 	}
1223efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
1224efec959fSBenjamin Herrenschmidt 
1225efec959fSBenjamin Herrenschmidt 	return 1;
1226efec959fSBenjamin Herrenschmidt 
1227efec959fSBenjamin Herrenschmidt }
1228efec959fSBenjamin Herrenschmidt 
1229014d02cbSSuraj Jitindar Singh static void __init prom_parse_mmu_model(u8 val,
1230014d02cbSSuraj Jitindar Singh 					struct platform_support *support)
1231014d02cbSSuraj Jitindar Singh {
1232014d02cbSSuraj Jitindar Singh 	switch (val) {
1233014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_DYNAMIC):
1234014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_EITHER): /* Either Available */
1235014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - either supported\n");
1236014d02cbSSuraj Jitindar Singh 		support->radix_mmu = !prom_radix_disable;
1237014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1238014d02cbSSuraj Jitindar Singh 		break;
1239014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */
1240014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - radix only\n");
1241014d02cbSSuraj Jitindar Singh 		if (prom_radix_disable) {
1242014d02cbSSuraj Jitindar Singh 			/*
1243014d02cbSSuraj Jitindar Singh 			 * If we __have__ to do radix, we're better off ignoring
1244014d02cbSSuraj Jitindar Singh 			 * the command line rather than not booting.
1245014d02cbSSuraj Jitindar Singh 			 */
1246014d02cbSSuraj Jitindar Singh 			prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
1247014d02cbSSuraj Jitindar Singh 		}
1248014d02cbSSuraj Jitindar Singh 		support->radix_mmu = true;
1249014d02cbSSuraj Jitindar Singh 		break;
1250014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_HASH):
1251014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - hash only\n");
1252014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1253014d02cbSSuraj Jitindar Singh 		break;
1254014d02cbSSuraj Jitindar Singh 	default:
1255014d02cbSSuraj Jitindar Singh 		prom_debug("Unknown mmu support option: 0x%x\n", val);
1256014d02cbSSuraj Jitindar Singh 		break;
1257014d02cbSSuraj Jitindar Singh 	}
1258014d02cbSSuraj Jitindar Singh }
1259014d02cbSSuraj Jitindar Singh 
1260ac5e5a54SCédric Le Goater static void __init prom_parse_xive_model(u8 val,
1261ac5e5a54SCédric Le Goater 					 struct platform_support *support)
1262ac5e5a54SCédric Le Goater {
1263ac5e5a54SCédric Le Goater 	switch (val) {
1264ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EITHER): /* Either Available */
1265ac5e5a54SCédric Le Goater 		prom_debug("XIVE - either mode supported\n");
1266a3bf9fbdSGreg Kurz 		support->xive = !prom_xive_disable;
1267ac5e5a54SCédric Le Goater 		break;
1268ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EXPLOIT): /* Only Exploitation mode */
1269ac5e5a54SCédric Le Goater 		prom_debug("XIVE - exploitation mode supported\n");
1270a3bf9fbdSGreg Kurz 		if (prom_xive_disable) {
1271a3bf9fbdSGreg Kurz 			/*
1272a3bf9fbdSGreg Kurz 			 * If we __have__ to do XIVE, we're better off ignoring
1273a3bf9fbdSGreg Kurz 			 * the command line rather than not booting.
1274a3bf9fbdSGreg Kurz 			 */
1275a3bf9fbdSGreg Kurz 			prom_printf("WARNING: Ignoring cmdline option xive=off\n");
1276a3bf9fbdSGreg Kurz 		}
1277ac5e5a54SCédric Le Goater 		support->xive = true;
1278ac5e5a54SCédric Le Goater 		break;
1279ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_LEGACY): /* Only Legacy mode */
1280ac5e5a54SCédric Le Goater 		prom_debug("XIVE - legacy mode supported\n");
1281ac5e5a54SCédric Le Goater 		break;
1282ac5e5a54SCédric Le Goater 	default:
1283ac5e5a54SCédric Le Goater 		prom_debug("Unknown xive support option: 0x%x\n", val);
1284ac5e5a54SCédric Le Goater 		break;
1285ac5e5a54SCédric Le Goater 	}
1286ac5e5a54SCédric Le Goater }
1287ac5e5a54SCédric Le Goater 
1288014d02cbSSuraj Jitindar Singh static void __init prom_parse_platform_support(u8 index, u8 val,
1289014d02cbSSuraj Jitindar Singh 					       struct platform_support *support)
1290014d02cbSSuraj Jitindar Singh {
1291014d02cbSSuraj Jitindar Singh 	switch (index) {
1292014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_MMU_SUPPORT): /* MMU Model */
1293014d02cbSSuraj Jitindar Singh 		prom_parse_mmu_model(val & OV5_FEAT(OV5_MMU_SUPPORT), support);
1294014d02cbSSuraj Jitindar Singh 		break;
1295014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_RADIX_GTSE): /* Radix Extensions */
1296bf6b7661SAneesh Kumar K.V 		if (val & OV5_FEAT(OV5_RADIX_GTSE))
1297bf6b7661SAneesh Kumar K.V 			support->radix_gtse = !prom_radix_gtse_disable;
1298014d02cbSSuraj Jitindar Singh 		break;
1299ac5e5a54SCédric Le Goater 	case OV5_INDX(OV5_XIVE_SUPPORT): /* Interrupt mode */
1300ac5e5a54SCédric Le Goater 		prom_parse_xive_model(val & OV5_FEAT(OV5_XIVE_SUPPORT),
1301ac5e5a54SCédric Le Goater 				      support);
1302ac5e5a54SCédric Le Goater 		break;
1303014d02cbSSuraj Jitindar Singh 	}
1304014d02cbSSuraj Jitindar Singh }
1305014d02cbSSuraj Jitindar Singh 
1306014d02cbSSuraj Jitindar Singh static void __init prom_check_platform_support(void)
1307014d02cbSSuraj Jitindar Singh {
1308014d02cbSSuraj Jitindar Singh 	struct platform_support supported = {
1309014d02cbSSuraj Jitindar Singh 		.hash_mmu = false,
1310014d02cbSSuraj Jitindar Singh 		.radix_mmu = false,
1311ac5e5a54SCédric Le Goater 		.radix_gtse = false,
1312ac5e5a54SCédric Le Goater 		.xive = false
1313014d02cbSSuraj Jitindar Singh 	};
1314014d02cbSSuraj Jitindar Singh 	int prop_len = prom_getproplen(prom.chosen,
1315014d02cbSSuraj Jitindar Singh 				       "ibm,arch-vec-5-platform-support");
1316a614f52eSBenjamin Herrenschmidt 
1317adcf5918SChristophe Leroy 	/*
1318adcf5918SChristophe Leroy 	 * First copy the architecture vec template
1319adcf5918SChristophe Leroy 	 *
1320adcf5918SChristophe Leroy 	 * use memcpy() instead of *vec = *vec_template so that GCC replaces it
1321adcf5918SChristophe Leroy 	 * by __memcpy() when KASAN is active
1322adcf5918SChristophe Leroy 	 */
1323adcf5918SChristophe Leroy 	memcpy(&ibm_architecture_vec, &ibm_architecture_vec_template,
1324adcf5918SChristophe Leroy 	       sizeof(ibm_architecture_vec));
1325a614f52eSBenjamin Herrenschmidt 
1326014d02cbSSuraj Jitindar Singh 	if (prop_len > 1) {
1327014d02cbSSuraj Jitindar Singh 		int i;
1328ab912399SSuraj Jitindar Singh 		u8 vec[8];
1329014d02cbSSuraj Jitindar Singh 		prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1330014d02cbSSuraj Jitindar Singh 			   prop_len);
1331ab912399SSuraj Jitindar Singh 		if (prop_len > sizeof(vec))
1332ab912399SSuraj Jitindar Singh 			prom_printf("WARNING: ibm,arch-vec-5-platform-support longer than expected (len: %d)\n",
1333ab912399SSuraj Jitindar Singh 				    prop_len);
1334014d02cbSSuraj Jitindar Singh 		prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support",
1335014d02cbSSuraj Jitindar Singh 			     &vec, sizeof(vec));
1336ab912399SSuraj Jitindar Singh 		for (i = 0; i < sizeof(vec); i += 2) {
1337014d02cbSSuraj Jitindar Singh 			prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2
1338014d02cbSSuraj Jitindar Singh 								  , vec[i]
1339014d02cbSSuraj Jitindar Singh 								  , vec[i + 1]);
1340014d02cbSSuraj Jitindar Singh 			prom_parse_platform_support(vec[i], vec[i + 1],
1341014d02cbSSuraj Jitindar Singh 						    &supported);
1342014d02cbSSuraj Jitindar Singh 		}
1343014d02cbSSuraj Jitindar Singh 	}
1344014d02cbSSuraj Jitindar Singh 
1345029ab30bSBharata B Rao 	if (supported.radix_mmu && IS_ENABLED(CONFIG_PPC_RADIX_MMU)) {
1346029ab30bSBharata B Rao 		/* Radix preferred - Check if GTSE is also supported */
1347029ab30bSBharata B Rao 		prom_debug("Asking for radix\n");
1348014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1349029ab30bSBharata B Rao 		if (supported.radix_gtse)
1350029ab30bSBharata B Rao 			ibm_architecture_vec.vec5.radix_ext =
1351029ab30bSBharata B Rao 					OV5_FEAT(OV5_RADIX_GTSE);
1352029ab30bSBharata B Rao 		else
1353029ab30bSBharata B Rao 			prom_debug("Radix GTSE isn't supported\n");
1354014d02cbSSuraj Jitindar Singh 	} else if (supported.hash_mmu) {
1355014d02cbSSuraj Jitindar Singh 		/* Default to hash mmu (if we can) */
1356014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for hash\n");
1357014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1358014d02cbSSuraj Jitindar Singh 	} else {
1359014d02cbSSuraj Jitindar Singh 		/* We're probably on a legacy hypervisor */
1360014d02cbSSuraj Jitindar Singh 		prom_debug("Assuming legacy hash support\n");
1361014d02cbSSuraj Jitindar Singh 	}
1362ac5e5a54SCédric Le Goater 
1363ac5e5a54SCédric Le Goater 	if (supported.xive) {
1364ac5e5a54SCédric Le Goater 		prom_debug("Asking for XIVE\n");
1365ac5e5a54SCédric Le Goater 		ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1366ac5e5a54SCédric Le Goater 	}
1367014d02cbSSuraj Jitindar Singh }
1368efec959fSBenjamin Herrenschmidt 
13699b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
13709b6b563cSPaul Mackerras {
1371493adffcSBenjamin Herrenschmidt 	ihandle root;
1372f709bfacSPaul Mackerras 	prom_arg_t ret;
1373dbd0c5d5SLaurent Dufour 	u32 cores;
13749b6b563cSPaul Mackerras 
1375014d02cbSSuraj Jitindar Singh 	/* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1376014d02cbSSuraj Jitindar Singh 	prom_check_platform_support();
1377014d02cbSSuraj Jitindar Singh 
1378f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
1379f709bfacSPaul Mackerras 	if (root != 0) {
1380efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
1381efec959fSBenjamin Herrenschmidt 		 *
1382efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
1383efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
1384efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
1385efec959fSBenjamin Herrenschmidt 		 */
1386dbd0c5d5SLaurent Dufour 
1387dbd0c5d5SLaurent Dufour 		cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1388eae5f709SMathieu Malaterre 		prom_printf("Max number of cores passed to firmware: %u (NR_CPUS = %d)\n",
1389dbd0c5d5SLaurent Dufour 			    cores, NR_CPUS);
139076ffb578SMichael Ellerman 
139176ffb578SMichael Ellerman 		ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1392efec959fSBenjamin Herrenschmidt 
1393f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
1394049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
1395f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
1396f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
139733b74977SBenjamin Herrenschmidt 				  root,
139876ffb578SMichael Ellerman 				  ADDR(&ibm_architecture_vec)) == 0) {
1399f709bfacSPaul Mackerras 			/* the call exists... */
1400f709bfacSPaul Mackerras 			if (ret)
14014da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
1402f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
1403f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
14044da727aeSAnton Blanchard 			prom_printf(" done\n");
1405f709bfacSPaul Mackerras 			return;
1406f709bfacSPaul Mackerras 		}
1407f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
1408049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
1409f709bfacSPaul Mackerras 	}
1410f709bfacSPaul Mackerras 
1411493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
1412493adffcSBenjamin Herrenschmidt 	{
1413493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
1414493adffcSBenjamin Herrenschmidt 
1415f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
1416493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
1417493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
14189b6b563cSPaul Mackerras 		if (elfloader == 0) {
14199b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
14209b6b563cSPaul Mackerras 			return;
14219b6b563cSPaul Mackerras 		}
14229b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
14239b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
14249b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
14259b6b563cSPaul Mackerras 	}
1426493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1427493adffcSBenjamin Herrenschmidt }
142811fdb309SBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
14299b6b563cSPaul Mackerras 
14309b6b563cSPaul Mackerras /*
14319b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
14329b6b563cSPaul Mackerras  *
14339b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
14349b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
14359b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
14369b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
14379b6b563cSPaul Mackerras  *  never happen.
14389b6b563cSPaul Mackerras  *
14399b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
14409b6b563cSPaul Mackerras  *  TCEs overlap
14419b6b563cSPaul Mackerras  *
14429b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
14439b6b563cSPaul Mackerras  *
14449b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
14459b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
14469b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
14479b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
14489b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
14499b6b563cSPaul Mackerras  *
14509b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
14519b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
14529b6b563cSPaul Mackerras  *  reserved.
14539b6b563cSPaul Mackerras  */
14549b6b563cSPaul Mackerras 
14559b6b563cSPaul Mackerras 
14569b6b563cSPaul Mackerras /*
14579b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
14589b6b563cSPaul Mackerras  *
14599b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
14609b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
14619b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
14629b6b563cSPaul Mackerras  */
14639b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
14649b6b563cSPaul Mackerras {
14655827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
14669b6b563cSPaul Mackerras 	unsigned long addr = 0;
14679b6b563cSPaul Mackerras 
1468c4988820SPaul Mackerras 	if (align)
1469b7115316SChristophe Leroy 		base = ALIGN(base, align);
1470eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx)\n", __func__, size, align);
14715827d416SAnton Blanchard 	if (ram_top == 0)
14729b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
14739b6b563cSPaul Mackerras 
14749b6b563cSPaul Mackerras 	if (align)
1475b7115316SChristophe Leroy 		base = ALIGN(alloc_bottom, align);
14769b6b563cSPaul Mackerras 	else
14775827d416SAnton Blanchard 		base = alloc_bottom;
14789b6b563cSPaul Mackerras 
14795827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
1480b7115316SChristophe Leroy 	    base = ALIGN(base + 0x100000, align)) {
1481eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
14829b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1483c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
14849b6b563cSPaul Mackerras 			break;
14859b6b563cSPaul Mackerras 		addr = 0;
14869b6b563cSPaul Mackerras 		if (align == 0)
14879b6b563cSPaul Mackerras 			break;
14889b6b563cSPaul Mackerras 	}
14899b6b563cSPaul Mackerras 	if (addr == 0)
14909b6b563cSPaul Mackerras 		return 0;
14915827d416SAnton Blanchard 	alloc_bottom = addr + size;
14929b6b563cSPaul Mackerras 
1493eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1494eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1495eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1496eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1497eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1498eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
14999b6b563cSPaul Mackerras 
15009b6b563cSPaul Mackerras 	return addr;
15019b6b563cSPaul Mackerras }
15029b6b563cSPaul Mackerras 
15039b6b563cSPaul Mackerras /*
15049b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
15059b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
15069b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
15079b6b563cSPaul Mackerras  */
15089b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
15099b6b563cSPaul Mackerras 				       int highmem)
15109b6b563cSPaul Mackerras {
15119b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
15129b6b563cSPaul Mackerras 
1513eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx, %s)\n", __func__, size, align,
15145827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
15155827d416SAnton Blanchard 	if (ram_top == 0)
15169b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
15179b6b563cSPaul Mackerras 
15189b6b563cSPaul Mackerras 	if (highmem) {
15199b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
1520e96d904eSChristophe Leroy 		addr = ALIGN_DOWN(alloc_top_high - size, align);
15215827d416SAnton Blanchard 		if (addr <= alloc_bottom)
15229b6b563cSPaul Mackerras 			return 0;
15239b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
15249b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
15259b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
15269b6b563cSPaul Mackerras 		 */
15275827d416SAnton Blanchard 		if (addr < rmo_top) {
15289b6b563cSPaul Mackerras 			/* Good, we are first */
15295827d416SAnton Blanchard 			if (alloc_top == rmo_top)
15305827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
15319b6b563cSPaul Mackerras 			else
15329b6b563cSPaul Mackerras 				return 0;
15339b6b563cSPaul Mackerras 		}
15345827d416SAnton Blanchard 		alloc_top_high = addr;
15359b6b563cSPaul Mackerras 		goto bail;
15369b6b563cSPaul Mackerras 	}
15379b6b563cSPaul Mackerras 
1538e96d904eSChristophe Leroy 	base = ALIGN_DOWN(alloc_top - size, align);
15395827d416SAnton Blanchard 	for (; base > alloc_bottom;
1540e96d904eSChristophe Leroy 	     base = ALIGN_DOWN(base - 0x100000, align))  {
1541eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
15429b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1543c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
15449b6b563cSPaul Mackerras 			break;
15459b6b563cSPaul Mackerras 		addr = 0;
15469b6b563cSPaul Mackerras 	}
15479b6b563cSPaul Mackerras 	if (addr == 0)
15489b6b563cSPaul Mackerras 		return 0;
15495827d416SAnton Blanchard 	alloc_top = addr;
15509b6b563cSPaul Mackerras 
15519b6b563cSPaul Mackerras  bail:
1552eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1553eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1554eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1555eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1556eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1557eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
15589b6b563cSPaul Mackerras 
15599b6b563cSPaul Mackerras 	return addr;
15609b6b563cSPaul Mackerras }
15619b6b563cSPaul Mackerras 
15629b6b563cSPaul Mackerras /*
15639b6b563cSPaul Mackerras  * Parse a "reg" cell
15649b6b563cSPaul Mackerras  */
15659b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
15669b6b563cSPaul Mackerras {
15679b6b563cSPaul Mackerras 	cell_t *p = *cellp;
15689b6b563cSPaul Mackerras 	unsigned long r = 0;
15699b6b563cSPaul Mackerras 
15709b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
15719b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
15729b6b563cSPaul Mackerras 		p++;
15739b6b563cSPaul Mackerras 		s--;
15749b6b563cSPaul Mackerras 	}
1575493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
15769b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
157735499c01SPaul Mackerras 	if (s > 1) {
15789b6b563cSPaul Mackerras 		r <<= 32;
1579493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
15809b6b563cSPaul Mackerras 	}
15819b6b563cSPaul Mackerras #endif
15829b6b563cSPaul Mackerras 	*cellp = p;
15839b6b563cSPaul Mackerras 	return r;
15849b6b563cSPaul Mackerras }
15859b6b563cSPaul Mackerras 
15869b6b563cSPaul Mackerras /*
15879b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
15889b6b563cSPaul Mackerras  * we don't need anything smarter at this point
15899b6b563cSPaul Mackerras  *
15909b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
15919b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
15929b6b563cSPaul Mackerras  * them down.
15939b6b563cSPaul Mackerras  */
15940108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
15959b6b563cSPaul Mackerras {
1596cbbcf340SKumar Gala 	u64 top = base + size;
15975827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
15989b6b563cSPaul Mackerras 
15999b6b563cSPaul Mackerras 	if (size == 0)
16009b6b563cSPaul Mackerras 		return;
16019b6b563cSPaul Mackerras 
16029b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
16039b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
16049b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
16059b6b563cSPaul Mackerras 	 */
1606e96d904eSChristophe Leroy 	base = ALIGN_DOWN(base, PAGE_SIZE);
1607b7115316SChristophe Leroy 	top = ALIGN(top, PAGE_SIZE);
16089b6b563cSPaul Mackerras 	size = top - base;
16099b6b563cSPaul Mackerras 
16109b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
16119b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1612493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1613493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
16145827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
16159b6b563cSPaul Mackerras }
16169b6b563cSPaul Mackerras 
16179b6b563cSPaul Mackerras /*
1618b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
16199b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
16209b6b563cSPaul Mackerras  */
16219b6b563cSPaul Mackerras static void __init prom_init_mem(void)
16229b6b563cSPaul Mackerras {
16239b6b563cSPaul Mackerras 	phandle node;
1624eab00a20SMathieu Malaterre 	char type[64];
16259b6b563cSPaul Mackerras 	unsigned int plen;
16269b6b563cSPaul Mackerras 	cell_t *p, *endp;
1627493adffcSBenjamin Herrenschmidt 	__be32 val;
16289b6b563cSPaul Mackerras 	u32 rac, rsc;
16299b6b563cSPaul Mackerras 
16309b6b563cSPaul Mackerras 	/*
16319b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
16329b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
16339b6b563cSPaul Mackerras 	 * 2) top of memory
16349b6b563cSPaul Mackerras 	 */
1635493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1636493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1637493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1638493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1639493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1640493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1641493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1642493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
16439b6b563cSPaul Mackerras 
16449b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
16459b6b563cSPaul Mackerras 
16469b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
16479b6b563cSPaul Mackerras 		type[0] = 0;
16489b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
16499b6b563cSPaul Mackerras 
1650c4988820SPaul Mackerras 		if (type[0] == 0) {
1651c4988820SPaul Mackerras 			/*
1652c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1653c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1654c4988820SPaul Mackerras 			 */
1655c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1656c4988820SPaul Mackerras 		}
1657450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "memory"))
16589b6b563cSPaul Mackerras 			continue;
16599b6b563cSPaul Mackerras 
16605827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
16619b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
16629b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
16639b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
16649b6b563cSPaul Mackerras 		}
16655827d416SAnton Blanchard 		p = regbuf;
16669b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
16679b6b563cSPaul Mackerras 
16689b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1669c806a6fdSMathieu Malaterre 		memset(prom_scratch, 0, sizeof(prom_scratch));
1670c806a6fdSMathieu Malaterre 		call_prom("package-to-path", 3, 1, node, prom_scratch,
1671c806a6fdSMathieu Malaterre 			  sizeof(prom_scratch) - 1);
1672c806a6fdSMathieu Malaterre 		prom_debug("  node %s :\n", prom_scratch);
16739b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
16749b6b563cSPaul Mackerras 
16759b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
16769b6b563cSPaul Mackerras 			unsigned long base, size;
16779b6b563cSPaul Mackerras 
16789b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
16799b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
16809b6b563cSPaul Mackerras 
16819b6b563cSPaul Mackerras 			if (size == 0)
16829b6b563cSPaul Mackerras 				continue;
1683eae5f709SMathieu Malaterre 			prom_debug("    %lx %lx\n", base, size);
16845827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
16855827d416SAnton Blanchard 				rmo_top = size;
16865827d416SAnton Blanchard 			if ((base + size) > ram_top)
16875827d416SAnton Blanchard 				ram_top = base + size;
16889b6b563cSPaul Mackerras 		}
16899b6b563cSPaul Mackerras 	}
16909b6b563cSPaul Mackerras 
16915827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
16929b6b563cSPaul Mackerras 
16939b6b563cSPaul Mackerras 	/*
1694cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1695cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1696cf68787bSBenjamin Krill 	 * TCE's up there.
1697cf68787bSBenjamin Krill 	 */
1698cf68787bSBenjamin Krill 
16995827d416SAnton Blanchard 	alloc_top_high = ram_top;
1700cf68787bSBenjamin Krill 
17015827d416SAnton Blanchard 	if (prom_memory_limit) {
17025827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1703eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx <= alloc_bottom.\n",
17045827d416SAnton Blanchard 				    prom_memory_limit);
17055827d416SAnton Blanchard 			prom_memory_limit = 0;
17065827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1707eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx >= ram_top.\n",
17085827d416SAnton Blanchard 				    prom_memory_limit);
17095827d416SAnton Blanchard 			prom_memory_limit = 0;
1710cf68787bSBenjamin Krill 		} else {
17115827d416SAnton Blanchard 			ram_top = prom_memory_limit;
17125827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1713cf68787bSBenjamin Krill 		}
1714cf68787bSBenjamin Krill 	}
1715cf68787bSBenjamin Krill 
1716cf68787bSBenjamin Krill 	/*
17179b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
17189b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
17199b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
17209b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
17219b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
17229b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
17239b6b563cSPaul Mackerras 	 */
17245827d416SAnton Blanchard 	if (!rmo_top)
17255827d416SAnton Blanchard 		rmo_top = ram_top;
17265827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
17275827d416SAnton Blanchard 	alloc_top = rmo_top;
17285827d416SAnton Blanchard 	alloc_top_high = ram_top;
17299b6b563cSPaul Mackerras 
173064968f60SPaul Mackerras 	/*
173164968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
173264968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
173364968f60SPaul Mackerras 	 */
17345827d416SAnton Blanchard 	if (prom_initrd_start &&
17355827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
17365827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
17375827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
173864968f60SPaul Mackerras 
17399b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
1740eae5f709SMathieu Malaterre 	prom_printf("  memory_limit : %lx (16 MB aligned)\n",
1741eae5f709SMathieu Malaterre 		    prom_memory_limit);
1742eae5f709SMathieu Malaterre 	prom_printf("  alloc_bottom : %lx\n", alloc_bottom);
1743eae5f709SMathieu Malaterre 	prom_printf("  alloc_top    : %lx\n", alloc_top);
1744eae5f709SMathieu Malaterre 	prom_printf("  alloc_top_hi : %lx\n", alloc_top_high);
1745eae5f709SMathieu Malaterre 	prom_printf("  rmo_top      : %lx\n", rmo_top);
1746eae5f709SMathieu Malaterre 	prom_printf("  ram_top      : %lx\n", ram_top);
17479b6b563cSPaul Mackerras }
17489b6b563cSPaul Mackerras 
174927f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
175027f44888SBenjamin Herrenschmidt {
1751493adffcSBenjamin Herrenschmidt 	__be32 val;
1752493adffcSBenjamin Herrenschmidt 	ihandle stdin;
175327f44888SBenjamin Herrenschmidt 
1754493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1755493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1756493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1757493adffcSBenjamin Herrenschmidt 	}
175827f44888SBenjamin Herrenschmidt }
175927f44888SBenjamin Herrenschmidt 
17606a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
17616a9c930bSRam Pai static int prom_rtas_hcall(uint64_t args)
17626a9c930bSRam Pai {
17636a9c930bSRam Pai 	register uint64_t arg1 asm("r3") = H_RTAS;
17646a9c930bSRam Pai 	register uint64_t arg2 asm("r4") = args;
17656a9c930bSRam Pai 
17666a9c930bSRam Pai 	asm volatile("sc 1\n" : "=r" (arg1) :
17676a9c930bSRam Pai 			"r" (arg1),
17686a9c930bSRam Pai 			"r" (arg2) :);
17696a9c930bSRam Pai 	return arg1;
17706a9c930bSRam Pai }
17716a9c930bSRam Pai 
17726a9c930bSRam Pai static struct rtas_args __prombss os_term_args;
17736a9c930bSRam Pai 
17746a9c930bSRam Pai static void __init prom_rtas_os_term(char *str)
17756a9c930bSRam Pai {
17766a9c930bSRam Pai 	phandle rtas_node;
17776a9c930bSRam Pai 	__be32 val;
17786a9c930bSRam Pai 	u32 token;
17796a9c930bSRam Pai 
17806a9c930bSRam Pai 	prom_debug("%s: start...\n", __func__);
17816a9c930bSRam Pai 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
17826a9c930bSRam Pai 	prom_debug("rtas_node: %x\n", rtas_node);
17836a9c930bSRam Pai 	if (!PHANDLE_VALID(rtas_node))
17846a9c930bSRam Pai 		return;
17856a9c930bSRam Pai 
17866a9c930bSRam Pai 	val = 0;
17876a9c930bSRam Pai 	prom_getprop(rtas_node, "ibm,os-term", &val, sizeof(val));
17886a9c930bSRam Pai 	token = be32_to_cpu(val);
17896a9c930bSRam Pai 	prom_debug("ibm,os-term: %x\n", token);
17906a9c930bSRam Pai 	if (token == 0)
17916a9c930bSRam Pai 		prom_panic("Could not get token for ibm,os-term\n");
17926a9c930bSRam Pai 	os_term_args.token = cpu_to_be32(token);
179374bb84e5SAlexey Kardashevskiy 	os_term_args.nargs = cpu_to_be32(1);
179474bb84e5SAlexey Kardashevskiy 	os_term_args.nret = cpu_to_be32(1);
179574bb84e5SAlexey Kardashevskiy 	os_term_args.args[0] = cpu_to_be32(__pa(str));
17966a9c930bSRam Pai 	prom_rtas_hcall((uint64_t)&os_term_args);
17976a9c930bSRam Pai }
17986a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
17996a9c930bSRam Pai 
18009b6b563cSPaul Mackerras /*
18019b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
18029b6b563cSPaul Mackerras  */
18039b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
18049b6b563cSPaul Mackerras {
18059b6b563cSPaul Mackerras 	phandle rtas_node;
18069b6b563cSPaul Mackerras 	ihandle rtas_inst;
18079b6b563cSPaul Mackerras 	u32 base, entry = 0;
1808493adffcSBenjamin Herrenschmidt 	__be32 val;
18099b6b563cSPaul Mackerras 	u32 size = 0;
18109b6b563cSPaul Mackerras 
18119b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
18129b6b563cSPaul Mackerras 
18139b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
18149b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
18159b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
18169b6b563cSPaul Mackerras 		return;
18179b6b563cSPaul Mackerras 
1818493adffcSBenjamin Herrenschmidt 	val = 0;
1819493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1820493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
18219b6b563cSPaul Mackerras 	if (size == 0)
18229b6b563cSPaul Mackerras 		return;
18239b6b563cSPaul Mackerras 
18249b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
18256d1e2c6cSAnton Blanchard 	if (base == 0)
18266d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
18279b6b563cSPaul Mackerras 
18289b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
18299b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1830a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
18319b6b563cSPaul Mackerras 		return;
18329b6b563cSPaul Mackerras 	}
18339b6b563cSPaul Mackerras 
18349b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
18359b6b563cSPaul Mackerras 
18369b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
18379b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1838a23414beSPaul Mackerras 			  rtas_inst, base) != 0
18399b6b563cSPaul Mackerras 	    || entry == 0) {
18409b6b563cSPaul Mackerras 		prom_printf(" failed\n");
18419b6b563cSPaul Mackerras 		return;
18429b6b563cSPaul Mackerras 	}
18439b6b563cSPaul Mackerras 	prom_printf(" done\n");
18449b6b563cSPaul Mackerras 
18459b6b563cSPaul Mackerras 	reserve_mem(base, size);
18469b6b563cSPaul Mackerras 
1847493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1848a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1849493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1850493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1851a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1852493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
18539b6b563cSPaul Mackerras 
1854dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1855dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1856dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1857dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1858dbe78b40SBenjamin Herrenschmidt 
18599b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
18609b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
1861eae5f709SMathieu Malaterre 	prom_debug("rtas size     = 0x%x\n", size);
18629b6b563cSPaul Mackerras 
18639b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
18649b6b563cSPaul Mackerras }
18659b6b563cSPaul Mackerras 
18669b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
18679b6b563cSPaul Mackerras /*
18684a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
18694a727429SAshley Lai  */
18704a727429SAshley Lai static void __init prom_instantiate_sml(void)
18714a727429SAshley Lai {
18724a727429SAshley Lai 	phandle ibmvtpm_node;
18734a727429SAshley Lai 	ihandle ibmvtpm_inst;
1874b4ed0469SHon Ching \(Vicky\) Lo 	u32 entry = 0, size = 0, succ = 0;
18754a727429SAshley Lai 	u64 base;
1876b4ed0469SHon Ching \(Vicky\) Lo 	__be32 val;
18774a727429SAshley Lai 
18784a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
18794a727429SAshley Lai 
18802f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
18814a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
18824a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
18834a727429SAshley Lai 		return;
18844a727429SAshley Lai 
18852f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
18864a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
18874a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
18884a727429SAshley Lai 		return;
18894a727429SAshley Lai 	}
18904a727429SAshley Lai 
1891b4ed0469SHon Ching \(Vicky\) Lo 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1892b4ed0469SHon Ching \(Vicky\) Lo 			 &val, sizeof(val)) != PROM_ERROR) {
1893b4ed0469SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &succ,
1894b4ed0469SHon Ching \(Vicky\) Lo 				  ADDR("reformat-sml-to-efi-alignment"),
1895b4ed0469SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || succ == 0) {
1896b4ed0469SHon Ching \(Vicky\) Lo 			prom_printf("Reformat SML to EFI alignment failed\n");
1897b4ed0469SHon Ching \(Vicky\) Lo 			return;
1898b4ed0469SHon Ching \(Vicky\) Lo 		}
1899b4ed0469SHon Ching \(Vicky\) Lo 
19004a727429SAshley Lai 		if (call_prom_ret("call-method", 2, 2, &size,
19019e5d4af4SHon Ching \(Vicky\) Lo 				  ADDR("sml-get-allocated-size"),
19029e5d4af4SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || size == 0) {
19039e5d4af4SHon Ching \(Vicky\) Lo 			prom_printf("SML get allocated size failed\n");
19049e5d4af4SHon Ching \(Vicky\) Lo 			return;
19059e5d4af4SHon Ching \(Vicky\) Lo 		}
19069e5d4af4SHon Ching \(Vicky\) Lo 	} else {
19079e5d4af4SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &size,
19084a727429SAshley Lai 				  ADDR("sml-get-handover-size"),
19094a727429SAshley Lai 				  ibmvtpm_inst) != 0 || size == 0) {
19104a727429SAshley Lai 			prom_printf("SML get handover size failed\n");
19114a727429SAshley Lai 			return;
19124a727429SAshley Lai 		}
19139e5d4af4SHon Ching \(Vicky\) Lo 	}
19144a727429SAshley Lai 
19154a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
19164a727429SAshley Lai 	if (base == 0)
19174a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
19184a727429SAshley Lai 
1919eae5f709SMathieu Malaterre 	prom_printf("instantiating sml at 0x%llx...", base);
19204a727429SAshley Lai 
19219e5d4af4SHon Ching \(Vicky\) Lo 	memset((void *)base, 0, size);
19229e5d4af4SHon Ching \(Vicky\) Lo 
19234a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
19244a727429SAshley Lai 			  ADDR("sml-handover"),
19254a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
19264a727429SAshley Lai 		prom_printf("SML handover failed\n");
19274a727429SAshley Lai 		return;
19284a727429SAshley Lai 	}
19294a727429SAshley Lai 	prom_printf(" done\n");
19304a727429SAshley Lai 
19314a727429SAshley Lai 	reserve_mem(base, size);
19324a727429SAshley Lai 
19332f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
19344a727429SAshley Lai 		     &base, sizeof(base));
19352f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
19364a727429SAshley Lai 		     &size, sizeof(size));
19374a727429SAshley Lai 
1938eae5f709SMathieu Malaterre 	prom_debug("sml base     = 0x%llx\n", base);
1939eae5f709SMathieu Malaterre 	prom_debug("sml size     = 0x%x\n", size);
19404a727429SAshley Lai 
19414a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
19424a727429SAshley Lai }
19434a727429SAshley Lai 
19444a727429SAshley Lai /*
19459b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
19469b6b563cSPaul Mackerras  */
1947493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
19489b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
19499b6b563cSPaul Mackerras {
19509b6b563cSPaul Mackerras 	phandle node;
19519b6b563cSPaul Mackerras 	ihandle phb_node;
19529b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
19535827d416SAnton Blanchard 	char *path = prom_scratch;
19549b6b563cSPaul Mackerras 	u64 base, align;
19559b6b563cSPaul Mackerras 	u32 minalign, minsize;
19569b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
19579b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
19589b6b563cSPaul Mackerras 	u64 i;
19599b6b563cSPaul Mackerras 
19605827d416SAnton Blanchard 	if (prom_iommu_off)
19619b6b563cSPaul Mackerras 		return;
19629b6b563cSPaul Mackerras 
19639b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
19649b6b563cSPaul Mackerras 
19659b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
19665827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
19679b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
19689b6b563cSPaul Mackerras 
19699b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
19709b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
19719b6b563cSPaul Mackerras 		compatible[0] = 0;
19729b6b563cSPaul Mackerras 		type[0] = 0;
19739b6b563cSPaul Mackerras 		model[0] = 0;
19749b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
19759b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
19769b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
19779b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
19789b6b563cSPaul Mackerras 
1979450e7dd4SChristophe Leroy 		if ((type[0] == 0) || (prom_strstr(type, "pci") == NULL))
19809b6b563cSPaul Mackerras 			continue;
19819b6b563cSPaul Mackerras 
1982e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
19839b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
1984450e7dd4SChristophe Leroy 			if ((prom_strstr(compatible, "python") == NULL) &&
1985450e7dd4SChristophe Leroy 			    (prom_strstr(compatible, "Speedwagon") == NULL) &&
1986450e7dd4SChristophe Leroy 			    (prom_strstr(compatible, "Winnipeg") == NULL))
19879b6b563cSPaul Mackerras 				continue;
19889b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
1989450e7dd4SChristophe Leroy 			if ((prom_strstr(model, "ython") == NULL) &&
1990450e7dd4SChristophe Leroy 			    (prom_strstr(model, "peedwagon") == NULL) &&
1991450e7dd4SChristophe Leroy 			    (prom_strstr(model, "innipeg") == NULL))
19929b6b563cSPaul Mackerras 				continue;
19939b6b563cSPaul Mackerras 		}
19949b6b563cSPaul Mackerras 
19959b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
19969b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
19979b6b563cSPaul Mackerras 			minalign = 0;
19989b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
19999b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
20009b6b563cSPaul Mackerras 			minsize = 4UL << 20;
20019b6b563cSPaul Mackerras 
20029b6b563cSPaul Mackerras 		/*
20039b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
20049b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
20059b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
20069b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
20079b6b563cSPaul Mackerras 		 */
20089b6b563cSPaul Mackerras 		minsize = 4UL << 20;
20099b6b563cSPaul Mackerras 
20109b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
20119b6b563cSPaul Mackerras 		align = max(minalign, minsize);
20129b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
20139b6b563cSPaul Mackerras 		if (base == 0)
20149b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
20159b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
20169b6b563cSPaul Mackerras 			local_alloc_bottom = base;
20179b6b563cSPaul Mackerras 
20189b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
2019d7fbe2a0SChristophe Leroy 		memset(path, 0, sizeof(prom_scratch));
20209b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
20219b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
2022d7fbe2a0SChristophe Leroy 			      path, sizeof(prom_scratch) - 1) == PROM_ERROR) {
20239b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
20249b6b563cSPaul Mackerras 		}
20259b6b563cSPaul Mackerras 
2026a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
2027a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
2028a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
2029a23414beSPaul Mackerras 
20309b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
20319b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
2032eae5f709SMathieu Malaterre 		prom_debug("\tbase = 0x%llx\n", base);
20339b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
20349b6b563cSPaul Mackerras 
20359b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
20369b6b563cSPaul Mackerras 		 * over the allocated size.
20379b6b563cSPaul Mackerras 		 */
20382b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
20399b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
20409b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
20419b6b563cSPaul Mackerras 			tce_entry |= 0x3;
20429b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
20439b6b563cSPaul Mackerras 		}
20449b6b563cSPaul Mackerras 
20459b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
20469b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
20479b6b563cSPaul Mackerras 		if (phb_node == 0)
20489b6b563cSPaul Mackerras 			prom_printf("... failed\n");
20499b6b563cSPaul Mackerras 		else
20509b6b563cSPaul Mackerras 			prom_printf("... done\n");
20519b6b563cSPaul Mackerras 
20529b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
20539b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
20549b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
20559b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
20569b6b563cSPaul Mackerras 	}
20579b6b563cSPaul Mackerras 
20589b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
20599b6b563cSPaul Mackerras 
20602babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
20612babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
20625827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
20635827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
20649b6b563cSPaul Mackerras 
20659b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
20669b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
20679b6b563cSPaul Mackerras }
2068493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
2069493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
20709b6b563cSPaul Mackerras 
20719b6b563cSPaul Mackerras /*
20729b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
20739b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
20749b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
20759b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
20769b6b563cSPaul Mackerras  *
20779b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
20789b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
20799b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
20809b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
20819b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
20829b6b563cSPaul Mackerras  * of setting those values.
20839b6b563cSPaul Mackerras  *
20849b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
20859b6b563cSPaul Mackerras  * is in its holding pattern code.
20869b6b563cSPaul Mackerras  *
20879b6b563cSPaul Mackerras  * -- Cort
20889b6b563cSPaul Mackerras  */
2089bbd0abdaSPaul Mackerras /*
2090bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
2091bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
2092bbd0abdaSPaul Mackerras  */
2093bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
2094bbd0abdaSPaul Mackerras 
20959b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
20969b6b563cSPaul Mackerras {
20979b6b563cSPaul Mackerras 	unsigned long i;
20989b6b563cSPaul Mackerras 	phandle node;
20999b6b563cSPaul Mackerras 	char type[64];
21009b6b563cSPaul Mackerras 	unsigned long *spinloop
2101bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
21029b6b563cSPaul Mackerras 	unsigned long *acknowledge
2103bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
2104bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
21059b6b563cSPaul Mackerras 
2106dbe78b40SBenjamin Herrenschmidt 	/*
2107dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
2108dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
2109dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
2110dbe78b40SBenjamin Herrenschmidt 	 */
2111dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
2112dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
2113dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
2114dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
2115dbe78b40SBenjamin Herrenschmidt 		return;
2116dbe78b40SBenjamin Herrenschmidt 	}
2117dbe78b40SBenjamin Herrenschmidt 
21189b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
2119eae5f709SMathieu Malaterre 	prom_debug("    1) spinloop       = 0x%lx\n", (unsigned long)spinloop);
2120eae5f709SMathieu Malaterre 	prom_debug("    1) *spinloop      = 0x%lx\n", *spinloop);
2121eae5f709SMathieu Malaterre 	prom_debug("    1) acknowledge    = 0x%lx\n",
21229b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
2123eae5f709SMathieu Malaterre 	prom_debug("    1) *acknowledge   = 0x%lx\n", *acknowledge);
2124eae5f709SMathieu Malaterre 	prom_debug("    1) secondary_hold = 0x%lx\n", secondary_hold);
21259b6b563cSPaul Mackerras 
21269b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
21279b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
21289b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
21299b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
21309b6b563cSPaul Mackerras 	 */
21319b6b563cSPaul Mackerras 	*spinloop = 0;
21329b6b563cSPaul Mackerras 
21339b6b563cSPaul Mackerras 	/* look for cpus */
21349b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
2135493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
2136493adffcSBenjamin Herrenschmidt 		__be32 reg;
2137493adffcSBenjamin Herrenschmidt 
21389b6b563cSPaul Mackerras 		type[0] = 0;
21399b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
2140450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "cpu") != 0)
21419b6b563cSPaul Mackerras 			continue;
21429b6b563cSPaul Mackerras 
21439b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
21449b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
2145450e7dd4SChristophe Leroy 			if (prom_strcmp(type, "okay") != 0)
21469b6b563cSPaul Mackerras 				continue;
21479b6b563cSPaul Mackerras 
2148493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
21499b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
2150493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
21519b6b563cSPaul Mackerras 
2152eae5f709SMathieu Malaterre 		prom_debug("cpu hw idx   = %u\n", cpu_no);
21539b6b563cSPaul Mackerras 
21549b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
21559b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
21569b6b563cSPaul Mackerras 		 * spinloop.
21579b6b563cSPaul Mackerras 		 */
21589b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
21599b6b563cSPaul Mackerras 
2160493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
216127f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
2162eae5f709SMathieu Malaterre 			prom_printf("starting cpu hw idx %u... ", cpu_no);
21639b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
2164493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
21659b6b563cSPaul Mackerras 
21669b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
21679b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
21689b6b563cSPaul Mackerras 				mb();
21699b6b563cSPaul Mackerras 
2170493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
21719b6b563cSPaul Mackerras 				prom_printf("done\n");
2172bbd0abdaSPaul Mackerras 			else
2173eae5f709SMathieu Malaterre 				prom_printf("failed: %lx\n", *acknowledge);
21749b6b563cSPaul Mackerras 		}
21759b6b563cSPaul Mackerras #ifdef CONFIG_SMP
21769b6b563cSPaul Mackerras 		else
2177eae5f709SMathieu Malaterre 			prom_printf("boot cpu hw idx %u\n", cpu_no);
21789b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
21799b6b563cSPaul Mackerras 	}
21809b6b563cSPaul Mackerras 
21819b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
21829b6b563cSPaul Mackerras }
21839b6b563cSPaul Mackerras 
21849b6b563cSPaul Mackerras 
21859b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
21869b6b563cSPaul Mackerras {
21879b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
21885827d416SAnton Blanchard 	prom_entry = pp;
21899b6b563cSPaul Mackerras 
21909b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
21915827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
21925827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
21939b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
21949b6b563cSPaul Mackerras 
21959b6b563cSPaul Mackerras 	/* get device tree root */
21965827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
21975827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
21989b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
2199a575b807SPaul Mackerras 
22005827d416SAnton Blanchard 	prom.mmumap = 0;
22019b6b563cSPaul Mackerras }
22029b6b563cSPaul Mackerras 
2203a575b807SPaul Mackerras #ifdef CONFIG_PPC32
2204a575b807SPaul Mackerras /*
2205a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
2206a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
2207a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
2208a575b807SPaul Mackerras  */
2209a575b807SPaul Mackerras static void __init prom_find_mmu(void)
2210a575b807SPaul Mackerras {
2211a575b807SPaul Mackerras 	phandle oprom;
2212a575b807SPaul Mackerras 	char version[64];
2213a575b807SPaul Mackerras 
2214a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
2215a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
2216a575b807SPaul Mackerras 		return;
2217a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
2218a575b807SPaul Mackerras 		return;
2219a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
2220a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
2221450e7dd4SChristophe Leroy 	if (prom_strcmp(version, "Open Firmware, 1.0.5") == 0)
2222a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
2223450e7dd4SChristophe Leroy 	else if (prom_strncmp(version, "FirmWorks,3.", 12) == 0) {
2224a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
2225a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2226a23414beSPaul Mackerras 	} else
2227a575b807SPaul Mackerras 		return;
22285827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
22295827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
22305827d416SAnton Blanchard 		     sizeof(prom.mmumap));
2231493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
22325827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2233a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
2234a575b807SPaul Mackerras }
2235a575b807SPaul Mackerras #else
2236a575b807SPaul Mackerras #define prom_find_mmu()
2237a575b807SPaul Mackerras #endif
2238a575b807SPaul Mackerras 
22399b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
22409b6b563cSPaul Mackerras {
22415827d416SAnton Blanchard 	char *path = of_stdout_device;
22429b6b563cSPaul Mackerras 	char type[16];
2243493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
2244493adffcSBenjamin Herrenschmidt 	__be32 val;
22459b6b563cSPaul Mackerras 
22465827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
22479b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
22489b6b563cSPaul Mackerras 
2249493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
22509b6b563cSPaul Mackerras 
22519b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
22529b6b563cSPaul Mackerras 	memset(path, 0, 256);
22535827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
22545827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
22555827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2256450e7dd4SChristophe Leroy 		     path, prom_strlen(path) + 1);
22579b6b563cSPaul Mackerras 
225810348f59SBenjamin Herrenschmidt 	/* instance-to-package fails on PA-Semi */
225910348f59SBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
226010348f59SBenjamin Herrenschmidt 	if (stdout_node != PROM_ERROR) {
226110348f59SBenjamin Herrenschmidt 		val = cpu_to_be32(stdout_node);
226210348f59SBenjamin Herrenschmidt 
22639b6b563cSPaul Mackerras 		/* If it's a display, note it */
22649b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
2265493adffcSBenjamin Herrenschmidt 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
2266450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "display") == 0)
2267493adffcSBenjamin Herrenschmidt 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
22689b6b563cSPaul Mackerras 	}
226910348f59SBenjamin Herrenschmidt }
22709b6b563cSPaul Mackerras 
22719b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
22729b6b563cSPaul Mackerras {
22739b6b563cSPaul Mackerras 	char compat[256];
22749b6b563cSPaul Mackerras 	int len, i = 0;
227521fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
22769b6b563cSPaul Mackerras 	phandle rtas;
2277e8222502SBenjamin Herrenschmidt 	int x;
227821fe3301SBenjamin Herrenschmidt #endif
2279e8222502SBenjamin Herrenschmidt 
22806e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
22815827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
22829b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
22839b6b563cSPaul Mackerras 	if (len > 0) {
22849b6b563cSPaul Mackerras 		compat[len] = 0;
22859b6b563cSPaul Mackerras 		while (i < len) {
22869b6b563cSPaul Mackerras 			char *p = &compat[i];
2287450e7dd4SChristophe Leroy 			int sl = prom_strlen(p);
22889b6b563cSPaul Mackerras 			if (sl == 0)
22899b6b563cSPaul Mackerras 				break;
2290450e7dd4SChristophe Leroy 			if (prom_strstr(p, "Power Macintosh") ||
2291450e7dd4SChristophe Leroy 			    prom_strstr(p, "MacRISC"))
22929b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
2293133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
2294133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
2295133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
2296133dda1eSArnd Bergmann 			 * so we do it here.
2297133dda1eSArnd Bergmann 			 */
2298450e7dd4SChristophe Leroy 			if (prom_strstr(p, "IBM,CBEA") ||
2299450e7dd4SChristophe Leroy 			    prom_strstr(p, "IBM,CPBW-1.0"))
2300133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
2301133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
23029b6b563cSPaul Mackerras 			i += sl + 1;
23039b6b563cSPaul Mackerras 		}
23049b6b563cSPaul Mackerras 	}
23059b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
23066e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
2307e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
2308e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
2309e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
2310e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
2311e8222502SBenjamin Herrenschmidt 	 */
23125827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
2313e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
2314e8222502SBenjamin Herrenschmidt 	if (len <= 0)
2315e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2316450e7dd4SChristophe Leroy 	if (prom_strcmp(compat, "chrp"))
2317e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2318e8222502SBenjamin Herrenschmidt 
23199b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
23209b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2321e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
2322e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2323e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
23249b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
23254da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
23269b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
23279b6b563cSPaul Mackerras 	}
23289b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
23299b6b563cSPaul Mackerras #else
2330e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
23319b6b563cSPaul Mackerras #endif
23329b6b563cSPaul Mackerras }
23339b6b563cSPaul Mackerras 
23349b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
23359b6b563cSPaul Mackerras {
23369b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
23379b6b563cSPaul Mackerras }
23389b6b563cSPaul Mackerras 
23399b6b563cSPaul Mackerras /*
23409b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
23419b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
23429b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
23439b6b563cSPaul Mackerras  * we've taken over the MMU.
23449b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
23459b6b563cSPaul Mackerras  * and if so, open it now.
23469b6b563cSPaul Mackerras  */
23479b6b563cSPaul Mackerras static void __init prom_check_displays(void)
23489b6b563cSPaul Mackerras {
23499b6b563cSPaul Mackerras 	char type[16], *path;
23509b6b563cSPaul Mackerras 	phandle node;
23519b6b563cSPaul Mackerras 	ihandle ih;
23529b6b563cSPaul Mackerras 	int i;
23539b6b563cSPaul Mackerras 
2354d00e34b9SBenjamin Herrenschmidt 	static const unsigned char default_colors[] __initconst = {
23559b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
23569b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
23579b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
23589b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
23599b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
23609b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
23619b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
23629b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
23639b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
23649b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
23659b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
23669b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
23679b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
23689b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
23699b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
23709b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
23719b6b563cSPaul Mackerras 	};
23729b6b563cSPaul Mackerras 	const unsigned char *clut;
23739b6b563cSPaul Mackerras 
23744da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
23759b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
23769b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
23779b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
2378450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "display") != 0)
23799b6b563cSPaul Mackerras 			continue;
23809b6b563cSPaul Mackerras 
23819b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
23825827d416SAnton Blanchard 		path = prom_scratch;
2383d7fbe2a0SChristophe Leroy 		memset(path, 0, sizeof(prom_scratch));
23849b6b563cSPaul Mackerras 
23859b6b563cSPaul Mackerras 		/*
23869b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
23879b6b563cSPaul Mackerras 		 * arguments
23889b6b563cSPaul Mackerras 		 */
23899b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
2390d7fbe2a0SChristophe Leroy 			      sizeof(prom_scratch) - 10) == PROM_ERROR)
23919b6b563cSPaul Mackerras 			continue;
23929b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
23939b6b563cSPaul Mackerras 
23949b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
23959b6b563cSPaul Mackerras 		if (ih == 0) {
23969b6b563cSPaul Mackerras 			prom_printf("failed\n");
23979b6b563cSPaul Mackerras 			continue;
23989b6b563cSPaul Mackerras 		}
23999b6b563cSPaul Mackerras 
24009b6b563cSPaul Mackerras 		/* Success */
24019b6b563cSPaul Mackerras 		prom_printf("done\n");
2402a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
24039b6b563cSPaul Mackerras 
24049b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
24059b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
24065827d416SAnton Blanchard 		clut = default_colors;
24073f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
24089b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
24099b6b563cSPaul Mackerras 					   clut[2]) != 0)
24109b6b563cSPaul Mackerras 				break;
24119b6b563cSPaul Mackerras 
24129b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
24135827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
24145827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
24159b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
24169b6b563cSPaul Mackerras 					   clut[2]) != 0)
24179b6b563cSPaul Mackerras 				break;
24189b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
24197191b615SBenjamin Herrenschmidt 
24207191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
24217191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
24227191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
24237191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
24247191b615SBenjamin Herrenschmidt 
24257191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
24266c71cfccSMichael Ellerman 
24276c71cfccSMichael Ellerman 			if (prom_getprop(node, "width", &width, 4) == PROM_ERROR)
24286c71cfccSMichael Ellerman 				return;
24296c71cfccSMichael Ellerman 
24306c71cfccSMichael Ellerman 			if (prom_getprop(node, "height", &height, 4) == PROM_ERROR)
24316c71cfccSMichael Ellerman 				return;
24326c71cfccSMichael Ellerman 
24336c71cfccSMichael Ellerman 			if (prom_getprop(node, "linebytes", &pitch, 4) == PROM_ERROR)
24346c71cfccSMichael Ellerman 				return;
24356c71cfccSMichael Ellerman 
24366c71cfccSMichael Ellerman 			if (prom_getprop(node, "address", &addr, 4) == PROM_ERROR)
24376c71cfccSMichael Ellerman 				return;
24386c71cfccSMichael Ellerman 
24397191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
24407191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
24417191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
2442c21f5a9eSChristophe Leroy 			btext_prepare_BAT();
24437191b615SBenjamin Herrenschmidt 		}
24447191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
24459b6b563cSPaul Mackerras 	}
24469b6b563cSPaul Mackerras }
24479b6b563cSPaul Mackerras 
24489b6b563cSPaul Mackerras 
24499b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
24509b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
24519b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
24529b6b563cSPaul Mackerras {
24539b6b563cSPaul Mackerras 	void *ret;
24549b6b563cSPaul Mackerras 
2455d3f3d3bfSChristophe Leroy 	*mem_start = ALIGN(*mem_start, align);
24569b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
24579b6b563cSPaul Mackerras 		unsigned long room, chunk;
24589b6b563cSPaul Mackerras 
2459eae5f709SMathieu Malaterre 		prom_debug("Chunk exhausted, claiming more at %lx...\n",
24605827d416SAnton Blanchard 			   alloc_bottom);
24615827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
24629b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
24639b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
24649b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2465fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2466fbafd728SAnton Blanchard 				   "(no room)\n");
24679b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
24689b6b563cSPaul Mackerras 		if (chunk == 0)
2469fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2470fbafd728SAnton Blanchard 				   "(claim failed)\n");
2471966728ddSAnton Blanchard 		*mem_end = chunk + room;
24729b6b563cSPaul Mackerras 	}
24739b6b563cSPaul Mackerras 
24749b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
24759b6b563cSPaul Mackerras 	*mem_start += needed;
24769b6b563cSPaul Mackerras 
24779b6b563cSPaul Mackerras 	return ret;
24789b6b563cSPaul Mackerras }
24799b6b563cSPaul Mackerras 
2480493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2481493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2482493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2483493adffcSBenjamin Herrenschmidt 	} while(0)
24849b6b563cSPaul Mackerras 
24859b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
24869b6b563cSPaul Mackerras {
24879b6b563cSPaul Mackerras 	char *s, *os;
24889b6b563cSPaul Mackerras 
24895827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
24909b6b563cSPaul Mackerras 	s += 4;
24915827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
2492450e7dd4SChristophe Leroy 		if (prom_strcmp(s, str) == 0)
24939b6b563cSPaul Mackerras 			return s - os;
2494450e7dd4SChristophe Leroy 		s += prom_strlen(s) + 1;
24959b6b563cSPaul Mackerras 	}
24969b6b563cSPaul Mackerras 	return 0;
24979b6b563cSPaul Mackerras }
24989b6b563cSPaul Mackerras 
24999b6b563cSPaul Mackerras /*
25009b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
25019b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
25029b6b563cSPaul Mackerras  */
25039b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
25049b6b563cSPaul Mackerras 
25059b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
25069b6b563cSPaul Mackerras 					 unsigned long *mem_start,
25079b6b563cSPaul Mackerras 					 unsigned long *mem_end)
25089b6b563cSPaul Mackerras {
25099b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
25109b6b563cSPaul Mackerras 	unsigned long soff;
25119b6b563cSPaul Mackerras 	phandle child;
25129b6b563cSPaul Mackerras 
25135827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
25149b6b563cSPaul Mackerras 
25159b6b563cSPaul Mackerras 	/* get and store all property names */
25165827d416SAnton Blanchard 	prev_name = "";
25179b6b563cSPaul Mackerras 	for (;;) {
25189b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
25199b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
25209b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
25219b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
25229b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
25239b6b563cSPaul Mackerras 			break;
25249b6b563cSPaul Mackerras 		}
25259b6b563cSPaul Mackerras 
25269b6b563cSPaul Mackerras  		/* skip "name" */
2527450e7dd4SChristophe Leroy 		if (prom_strcmp(namep, "name") == 0) {
25289b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
25295827d416SAnton Blanchard  			prev_name = "name";
25309b6b563cSPaul Mackerras  			continue;
25319b6b563cSPaul Mackerras  		}
25329b6b563cSPaul Mackerras 		/* get/create string entry */
25339b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
25349b6b563cSPaul Mackerras 		if (soff != 0) {
25359b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
25369b6b563cSPaul Mackerras 			namep = sstart + soff;
25379b6b563cSPaul Mackerras 		} else {
25389b6b563cSPaul Mackerras 			/* Trim off some if we can */
2539450e7dd4SChristophe Leroy 			*mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
25405827d416SAnton Blanchard 			dt_string_end = *mem_start;
25419b6b563cSPaul Mackerras 		}
25429b6b563cSPaul Mackerras 		prev_name = namep;
25439b6b563cSPaul Mackerras 	}
25449b6b563cSPaul Mackerras 
25459b6b563cSPaul Mackerras 	/* do all our children */
25469b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
25479b6b563cSPaul Mackerras 	while (child != 0) {
25489b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
25499b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
25509b6b563cSPaul Mackerras 	}
25519b6b563cSPaul Mackerras }
25529b6b563cSPaul Mackerras 
25539b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
25549b6b563cSPaul Mackerras 					unsigned long *mem_end)
25559b6b563cSPaul Mackerras {
25569b6b563cSPaul Mackerras 	phandle child;
25579b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
25589b6b563cSPaul Mackerras 	unsigned long soff;
25599b6b563cSPaul Mackerras 	unsigned char *valp;
25608ca2d515SBenjamin Herrenschmidt 	static char pname[MAX_PROPERTY_NAME] __prombss;
25616e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
25629b6b563cSPaul Mackerras 
25639b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
25649b6b563cSPaul Mackerras 
25659b6b563cSPaul Mackerras 	/* get the node's full name */
25669b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2567c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2568c4988820SPaul Mackerras 	if (room > 255)
2569c4988820SPaul Mackerras 		room = 255;
2570c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
25719b6b563cSPaul Mackerras 	if (l >= 0) {
25729b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2573c4988820SPaul Mackerras 		if (l >= room) {
2574c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
25759b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
25769b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
25779b6b563cSPaul Mackerras 		}
25789b6b563cSPaul Mackerras 		namep[l] = '\0';
25799b6b563cSPaul Mackerras 
25809b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2581a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2582a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
25839b6b563cSPaul Mackerras 		 */
2584a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
25859b6b563cSPaul Mackerras 			if (*p == '/')
2586a575b807SPaul Mackerras 				lp = namep;
2587a575b807SPaul Mackerras 			else if (*p != 0)
2588a575b807SPaul Mackerras 				*lp++ = *p;
2589a575b807SPaul Mackerras 		}
2590a575b807SPaul Mackerras 		*lp = 0;
2591d3f3d3bfSChristophe Leroy 		*mem_start = ALIGN((unsigned long)lp + 1, 4);
25929b6b563cSPaul Mackerras 	}
25939b6b563cSPaul Mackerras 
25949b6b563cSPaul Mackerras 	/* get it again for debugging */
25955827d416SAnton Blanchard 	path = prom_scratch;
2596d7fbe2a0SChristophe Leroy 	memset(path, 0, sizeof(prom_scratch));
2597d7fbe2a0SChristophe Leroy 	call_prom("package-to-path", 3, 1, node, path, sizeof(prom_scratch) - 1);
25989b6b563cSPaul Mackerras 
25999b6b563cSPaul Mackerras 	/* get and store all properties */
26005827d416SAnton Blanchard 	prev_name = "";
26015827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
26029b6b563cSPaul Mackerras 	for (;;) {
26039b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
26045827d416SAnton Blanchard 			      pname) != 1)
26059b6b563cSPaul Mackerras 			break;
26069b6b563cSPaul Mackerras 
26079b6b563cSPaul Mackerras  		/* skip "name" */
2608450e7dd4SChristophe Leroy 		if (prom_strcmp(pname, "name") == 0) {
26095827d416SAnton Blanchard  			prev_name = "name";
26109b6b563cSPaul Mackerras  			continue;
26119b6b563cSPaul Mackerras  		}
26129b6b563cSPaul Mackerras 
26139b6b563cSPaul Mackerras 		/* find string offset */
26145827d416SAnton Blanchard 		soff = dt_find_string(pname);
26159b6b563cSPaul Mackerras 		if (soff == 0) {
26169b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
26175827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
26189b6b563cSPaul Mackerras 			break;
26199b6b563cSPaul Mackerras 		}
26209b6b563cSPaul Mackerras 		prev_name = sstart + soff;
26219b6b563cSPaul Mackerras 
26229b6b563cSPaul Mackerras 		/* get length */
26235827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
26249b6b563cSPaul Mackerras 
26259b6b563cSPaul Mackerras 		/* sanity checks */
26269b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
26279b6b563cSPaul Mackerras 			continue;
26289b6b563cSPaul Mackerras 
26299b6b563cSPaul Mackerras 		/* push property head */
26309b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
26319b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
26329b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
26339b6b563cSPaul Mackerras 
26349b6b563cSPaul Mackerras 		/* push property content */
26359b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
26365827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
2637d3f3d3bfSChristophe Leroy 		*mem_start = ALIGN(*mem_start, 4);
26386e35d5daSBenjamin Herrenschmidt 
2639450e7dd4SChristophe Leroy 		if (!prom_strcmp(pname, "phandle"))
26406e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
26419b6b563cSPaul Mackerras 	}
26429b6b563cSPaul Mackerras 
2643f1f208e5SBenjamin Herrenschmidt 	/* Add a "phandle" property if none already exist */
26446e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
2645f1f208e5SBenjamin Herrenschmidt 		soff = dt_find_string("phandle");
26469b6b563cSPaul Mackerras 		if (soff == 0)
2647f1f208e5SBenjamin Herrenschmidt 			prom_printf("WARNING: Can't find string index for <phandle> node %s\n", path);
26489b6b563cSPaul Mackerras 		else {
26499b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
26509b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
26519b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
26529b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
2653493adffcSBenjamin Herrenschmidt 			*(__be32 *)valp = cpu_to_be32(node);
26549b6b563cSPaul Mackerras 		}
26556e35d5daSBenjamin Herrenschmidt 	}
26569b6b563cSPaul Mackerras 
26579b6b563cSPaul Mackerras 	/* do all our children */
26589b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
26599b6b563cSPaul Mackerras 	while (child != 0) {
26609b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
26619b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
26629b6b563cSPaul Mackerras 	}
26639b6b563cSPaul Mackerras 
26649b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
26659b6b563cSPaul Mackerras }
26669b6b563cSPaul Mackerras 
26679b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
26689b6b563cSPaul Mackerras {
26699b6b563cSPaul Mackerras 	phandle root;
26709b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
26719b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
26729b6b563cSPaul Mackerras 	char *namep;
26739b6b563cSPaul Mackerras 	u64 *rsvmap;
26749b6b563cSPaul Mackerras 
26759b6b563cSPaul Mackerras 	/*
26769b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2677fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
26789b6b563cSPaul Mackerras 	 */
26795827d416SAnton Blanchard 	room = alloc_top - alloc_bottom - 0x4000;
26809b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
26819b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
2682eae5f709SMathieu Malaterre 	prom_debug("starting device tree allocs at %lx\n", alloc_bottom);
26839b6b563cSPaul Mackerras 
26849b6b563cSPaul Mackerras 	/* Now try to claim that */
26859b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
26869b6b563cSPaul Mackerras 	if (mem_start == 0)
26879b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2688966728ddSAnton Blanchard 	mem_end = mem_start + room;
26899b6b563cSPaul Mackerras 
26909b6b563cSPaul Mackerras 	/* Get root of tree */
26919b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
26929b6b563cSPaul Mackerras 	if (root == (phandle)0)
26939b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
26949b6b563cSPaul Mackerras 
26959b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
2696d3f3d3bfSChristophe Leroy 	mem_start = ALIGN(mem_start, 4);
26979b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
26989b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
26995827d416SAnton Blanchard 	dt_header_start = (unsigned long)hdr;
27009b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
27019b6b563cSPaul Mackerras 
27029b6b563cSPaul Mackerras 	/* Start of strings */
27039b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
27045827d416SAnton Blanchard 	dt_string_start = mem_start;
27059b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
27069b6b563cSPaul Mackerras 
2707f1f208e5SBenjamin Herrenschmidt 	/* Add "phandle" in there, we'll need it */
27089b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
2709450e7dd4SChristophe Leroy 	prom_strcpy(namep, "phandle");
2710450e7dd4SChristophe Leroy 	mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
27119b6b563cSPaul Mackerras 
27129b6b563cSPaul Mackerras 	/* Build string array */
27139b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
27149b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
27155827d416SAnton Blanchard 	dt_string_end = mem_start;
27169b6b563cSPaul Mackerras 
27179b6b563cSPaul Mackerras 	/* Build structure */
27189b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
27195827d416SAnton Blanchard 	dt_struct_start = mem_start;
27209b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
27219b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
27229b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
27235827d416SAnton Blanchard 	dt_struct_end = PAGE_ALIGN(mem_start);
27249b6b563cSPaul Mackerras 
27259b6b563cSPaul Mackerras 	/* Finish header */
2726493adffcSBenjamin Herrenschmidt 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2727493adffcSBenjamin Herrenschmidt 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2728493adffcSBenjamin Herrenschmidt 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2729493adffcSBenjamin Herrenschmidt 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2730493adffcSBenjamin Herrenschmidt 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2731493adffcSBenjamin Herrenschmidt 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2732493adffcSBenjamin Herrenschmidt 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2733493adffcSBenjamin Herrenschmidt 	hdr->version = cpu_to_be32(OF_DT_VERSION);
27349b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
2735493adffcSBenjamin Herrenschmidt 	hdr->last_comp_version = cpu_to_be32(0x10);
27369b6b563cSPaul Mackerras 
27374d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
27385827d416SAnton Blanchard 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
27399b6b563cSPaul Mackerras 
27409b6b563cSPaul Mackerras #ifdef DEBUG_PROM
27419b6b563cSPaul Mackerras 	{
27429b6b563cSPaul Mackerras 		int i;
27439b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
27445827d416SAnton Blanchard 		for (i = 0; i < mem_reserve_cnt; i++)
2745eae5f709SMathieu Malaterre 			prom_printf("  %llx - %llx\n",
2746493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].base),
2747493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].size));
27489b6b563cSPaul Mackerras 	}
27499b6b563cSPaul Mackerras #endif
27504d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
27514d1f3f25SJimi Xenidis 	 * since it's too late.
27524d1f3f25SJimi Xenidis 	 */
27535827d416SAnton Blanchard 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
27549b6b563cSPaul Mackerras 
2755eae5f709SMathieu Malaterre 	prom_printf("Device tree strings 0x%lx -> 0x%lx\n",
27565827d416SAnton Blanchard 		    dt_string_start, dt_string_end);
2757eae5f709SMathieu Malaterre 	prom_printf("Device tree struct  0x%lx -> 0x%lx\n",
27585827d416SAnton Blanchard 		    dt_struct_start, dt_struct_end);
27599b6b563cSPaul Mackerras }
27609b6b563cSPaul Mackerras 
276154f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
276254f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
276354f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
276454f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
27659b6b563cSPaul Mackerras {
276654f4ee18SHollis Blanchard 	phandle isa;
2767980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
276854f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2769980a6513SBenjamin Herrenschmidt 	char *name;
277054f4ee18SHollis Blanchard 
2771980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2772980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2773980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2774980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2775980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2776980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2777980a6513SBenjamin Herrenschmidt 	}
277854f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
277954f4ee18SHollis Blanchard 		return;
278054f4ee18SHollis Blanchard 
2781980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2782980a6513SBenjamin Herrenschmidt 		return;
278354f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
278454f4ee18SHollis Blanchard 		== PROM_ERROR)
278554f4ee18SHollis Blanchard 		return;
278654f4ee18SHollis Blanchard 
278754f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
278854f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
278954f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
279054f4ee18SHollis Blanchard 		return;
279154f4ee18SHollis Blanchard 
2792980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
279354f4ee18SHollis Blanchard 
279454f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
279554f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2796980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
279754f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
279854f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
279954f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2800980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
280154f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
280254f4ee18SHollis Blanchard }
28038f101a05SHarry Ciao 
28048f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
28058f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
28068f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
28078f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
28088f101a05SHarry Ciao {
28098f101a05SHarry Ciao 	phandle mc;
28108f101a05SHarry Ciao 	u32 mc_reg[4];
28118f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
28128f101a05SHarry Ciao 	u32 ac, sc;
28138f101a05SHarry Ciao 
28148f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
28158f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
28168f101a05SHarry Ciao 		return;
28178f101a05SHarry Ciao 
28188f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
28198f101a05SHarry Ciao 		return;
28208f101a05SHarry Ciao 
28215827d416SAnton Blanchard 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
28225827d416SAnton Blanchard 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
28238f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
28248f101a05SHarry Ciao 		return;
28258f101a05SHarry Ciao 
28268f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
28278f101a05SHarry Ciao 		return;
28288f101a05SHarry Ciao 
28298f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
28308f101a05SHarry Ciao 		return;
28318f101a05SHarry Ciao 
28328f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
28338f101a05SHarry Ciao 
28348f101a05SHarry Ciao 	mc_reg[0] = 0x0;
28358f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
28368f101a05SHarry Ciao 	mc_reg[2] = 0x0;
28378f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
28388f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
28398f101a05SHarry Ciao }
284054f4ee18SHollis Blanchard #else
284154f4ee18SHollis Blanchard #define fixup_device_tree_maple()
28428f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
284354f4ee18SHollis Blanchard #endif
284454f4ee18SHollis Blanchard 
2845e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2846e4805922SOlaf Hering /*
2847e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2848e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2849556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2850e4805922SOlaf Hering  */
2851e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2852e8c0acf9SBenjamin Herrenschmidt {
2853e4805922SOlaf Hering 	phandle ph;
2854e4805922SOlaf Hering 	u32 prop[6];
285526c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2856e8c0acf9SBenjamin Herrenschmidt 	char *name;
2857e8c0acf9SBenjamin Herrenschmidt 	int rc;
2858e8c0acf9SBenjamin Herrenschmidt 
2859e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2860e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2861e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
286226c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2863e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
286426c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
286526c5032eSBenjamin Herrenschmidt 	}
2866e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2867e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2868e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2869e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2870e8c0acf9SBenjamin Herrenschmidt 
2871e4805922SOlaf Hering 			prop[0] = 0x1;
2872e4805922SOlaf Hering 			prop[1] = 0x0;
2873e4805922SOlaf Hering 			prop[2] = rloc;
2874e4805922SOlaf Hering 			prop[3] = 0x0;
2875e4805922SOlaf Hering 			prop[4] = 0x0;
2876e4805922SOlaf Hering 			prop[5] = 0x00010000;
2877e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2878e4805922SOlaf Hering 		}
2879e4805922SOlaf Hering 	}
2880e4805922SOlaf Hering 
2881e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2882e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2883e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2884e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2885e4805922SOlaf Hering 		prop[0] = 14;
2886e4805922SOlaf Hering 		prop[1] = 0x0;
2887556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2888556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2889556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2890556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2891556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2892556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2893556ecf9bSOlaf Hering 		}
2894e4805922SOlaf Hering 	}
2895e8c0acf9SBenjamin Herrenschmidt }
2896e8c0acf9SBenjamin Herrenschmidt #else
2897e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2898e8c0acf9SBenjamin Herrenschmidt #endif
2899e8c0acf9SBenjamin Herrenschmidt 
29009b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
290154f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
290254f4ee18SHollis Blanchard {
29039b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
29049b6b563cSPaul Mackerras 	u32 u3_rev;
29059b6b563cSPaul Mackerras 	u32 interrupts[2];
29069b6b563cSPaul Mackerras 	u32 parent;
29079b6b563cSPaul Mackerras 
29089b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
29099b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
29109b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
29119b6b563cSPaul Mackerras 		return;
29129b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
29139b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
29149b6b563cSPaul Mackerras 		return;
29159b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
29169b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
29179b6b563cSPaul Mackerras 		return;
29189b6b563cSPaul Mackerras 
29199b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
29209b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
29219b6b563cSPaul Mackerras 	    == PROM_ERROR)
29229b6b563cSPaul Mackerras 		return;
29237d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
29249b6b563cSPaul Mackerras 		return;
29259b6b563cSPaul Mackerras 	/* does it need fixup ? */
29269b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
29279b6b563cSPaul Mackerras 		return;
29289b6b563cSPaul Mackerras 
29299b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
29309b6b563cSPaul Mackerras 
29319b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
29329b6b563cSPaul Mackerras 	interrupts[0] = 0;
29339b6b563cSPaul Mackerras 	interrupts[1] = 1;
2934a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2935a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
29369b6b563cSPaul Mackerras 	parent = (u32)mpic;
2937a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2938a23414beSPaul Mackerras 		     &parent, sizeof(parent));
29399b6b563cSPaul Mackerras }
294054f4ee18SHollis Blanchard #else
294154f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
294254f4ee18SHollis Blanchard #endif
29439b6b563cSPaul Mackerras 
294488fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
294594d2dde7SGrant Likely /*
294694d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
294794d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
294894d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
294994d2dde7SGrant Likely  * ethernet node.
295094d2dde7SGrant Likely  */
295194d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
295288fd2a9dSSylvain Munaut {
295388fd2a9dSSylvain Munaut 	u32 node;
295488fd2a9dSSylvain Munaut 	char prop[64];
295594d2dde7SGrant Likely 	int rv;
295688fd2a9dSSylvain Munaut 
295794d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
295894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
295988fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
296088fd2a9dSSylvain Munaut 		return;
296188fd2a9dSSylvain Munaut 
296294d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
296394d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
296494d2dde7SGrant Likely 	if (!rv)
296588fd2a9dSSylvain Munaut 		return;
296688fd2a9dSSylvain Munaut 
296794d2dde7SGrant Likely 	/*
296894d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
296994d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
297094d2dde7SGrant Likely 	 */
297188fd2a9dSSylvain Munaut 
297294d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
29736f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
29746f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
29756f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
29766f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
29776f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
29786f4347c9SOlaf Hering 			" new-device"
29796f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
29806f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
298194d2dde7SGrant Likely 				" s\" mdio\" device-name"
298294d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
29836f4347c9SOlaf Hering 				" s\" compatible\" property"
29846f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
29856f4347c9SOlaf Hering 				" 0x2 encode-int"
29866f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
29876f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
29886f4347c9SOlaf Hering 				" s\" interrupts\" property"
29896f4347c9SOlaf Hering 			" finish-device");
29906f4347c9SOlaf Hering 	};
29916f4347c9SOlaf Hering 
299294d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
299394d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
299494d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
299594d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
29966f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
29976f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
29986f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
29996f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
30006f4347c9SOlaf Hering 			" new-device"
30016f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
30026f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
30036f4347c9SOlaf Hering 				" my-self"
30046f4347c9SOlaf Hering 				" ihandle>phandle"
30056f4347c9SOlaf Hering 			" finish-device"
30066f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
30076f4347c9SOlaf Hering 				" encode-int"
30086f4347c9SOlaf Hering 				" s\" phy-handle\" property"
30096f4347c9SOlaf Hering 			" device-end");
30106f4347c9SOlaf Hering 	}
301194d2dde7SGrant Likely }
30126f4347c9SOlaf Hering 
301394d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
301494d2dde7SGrant Likely {
301594d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
301694d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
301794d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
301894d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
301994d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
302094d2dde7SGrant Likely 	u32 node;
302194d2dde7SGrant Likely 	char prop[64];
302294d2dde7SGrant Likely 	int rv, len;
302394d2dde7SGrant Likely 
302494d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
302594d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
302694d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
302794d2dde7SGrant Likely 		return;
302894d2dde7SGrant Likely 
302994d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
303094d2dde7SGrant Likely 	if (rv == PROM_ERROR)
303194d2dde7SGrant Likely 		return;
3032450e7dd4SChristophe Leroy 	if (prom_strcmp(prop, "EFIKA5K2"))
303394d2dde7SGrant Likely 		return;
303494d2dde7SGrant Likely 
303594d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
303694d2dde7SGrant Likely 
303794d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
303894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
303994d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
3040450e7dd4SChristophe Leroy 	if (rv != PROM_ERROR && (prom_strcmp(prop, "chrp") == 0))
304194d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
304294d2dde7SGrant Likely 
30437f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
30447f4392cdSDavid Woodhouse 	   fix that too */
30457f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
3046450e7dd4SChristophe Leroy 	if (rv != PROM_ERROR && (prom_strstr(prop, "CHRP")))
30477f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
30487f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
30497f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
30507f4392cdSDavid Woodhouse 
305194d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
305294d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
305394d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
305494d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
305594d2dde7SGrant Likely 		if (len == 12) {
305694d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
305794d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
305894d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
305994d2dde7SGrant Likely 		}
306094d2dde7SGrant Likely 	}
306194d2dde7SGrant Likely 
306294d2dde7SGrant Likely 	/* Fixup sound interrupts property */
306394d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
306494d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
306594d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
306694d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
306794d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
306894d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
306994d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
307094d2dde7SGrant Likely 		}
307194d2dde7SGrant Likely 	}
307294d2dde7SGrant Likely 
307394d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
307494d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
307588fd2a9dSSylvain Munaut }
307688fd2a9dSSylvain Munaut #else
307788fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
307888fd2a9dSSylvain Munaut #endif
307988fd2a9dSSylvain Munaut 
308050246787SDarren Stevens #ifdef CONFIG_PPC_PASEMI_NEMO
308150246787SDarren Stevens /*
308250246787SDarren Stevens  * CFE supplied on Nemo is broken in several ways, biggest
308350246787SDarren Stevens  * problem is that it reassigns ISA interrupts to unused mpic ints.
308450246787SDarren Stevens  * Add an interrupt-controller property for the io-bridge to use
308550246787SDarren Stevens  * and correct the ints so we can attach them to an irq_domain
308650246787SDarren Stevens  */
308750246787SDarren Stevens static void __init fixup_device_tree_pasemi(void)
308850246787SDarren Stevens {
308950246787SDarren Stevens 	u32 interrupts[2], parent, rval, val = 0;
309050246787SDarren Stevens 	char *name, *pci_name;
309150246787SDarren Stevens 	phandle iob, node;
309250246787SDarren Stevens 
309350246787SDarren Stevens 	/* Find the root pci node */
309450246787SDarren Stevens 	name = "/pxp@0,e0000000";
309550246787SDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
309650246787SDarren Stevens 	if (!PHANDLE_VALID(iob))
309750246787SDarren Stevens 		return;
309850246787SDarren Stevens 
309950246787SDarren Stevens 	/* check if interrupt-controller node set yet */
310050246787SDarren Stevens 	if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
310150246787SDarren Stevens 		return;
310250246787SDarren Stevens 
310350246787SDarren Stevens 	prom_printf("adding interrupt-controller property for SB600...\n");
310450246787SDarren Stevens 
310550246787SDarren Stevens 	prom_setprop(iob, name, "interrupt-controller", &val, 0);
310650246787SDarren Stevens 
310750246787SDarren Stevens 	pci_name = "/pxp@0,e0000000/pci@11";
310850246787SDarren Stevens 	node = call_prom("finddevice", 1, 1, ADDR(pci_name));
310950246787SDarren Stevens 	parent = ADDR(iob);
311050246787SDarren Stevens 
311150246787SDarren Stevens 	for( ; prom_next_node(&node); ) {
311250246787SDarren Stevens 		/* scan each node for one with an interrupt */
311350246787SDarren Stevens 		if (!PHANDLE_VALID(node))
311450246787SDarren Stevens 			continue;
311550246787SDarren Stevens 
311650246787SDarren Stevens 		rval = prom_getproplen(node, "interrupts");
311750246787SDarren Stevens 		if (rval == 0 || rval == PROM_ERROR)
311850246787SDarren Stevens 			continue;
311950246787SDarren Stevens 
312050246787SDarren Stevens 		prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
312150246787SDarren Stevens 		if ((interrupts[0] < 212) || (interrupts[0] > 222))
312250246787SDarren Stevens 			continue;
312350246787SDarren Stevens 
312450246787SDarren Stevens 		/* found a node, update both interrupts and interrupt-parent */
312550246787SDarren Stevens 		if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
312650246787SDarren Stevens 			interrupts[0] -= 203;
312750246787SDarren Stevens 		if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
312850246787SDarren Stevens 			interrupts[0] -= 213;
312950246787SDarren Stevens 		if (interrupts[0] == 221)
313050246787SDarren Stevens 			interrupts[0] = 14;
313150246787SDarren Stevens 		if (interrupts[0] == 222)
313250246787SDarren Stevens 			interrupts[0] = 8;
313350246787SDarren Stevens 
313450246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupts", interrupts,
313550246787SDarren Stevens 					sizeof(interrupts));
313650246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupt-parent", &parent,
313750246787SDarren Stevens 					sizeof(parent));
313850246787SDarren Stevens 	}
3139687e16bcSDarren Stevens 
3140687e16bcSDarren Stevens 	/*
3141687e16bcSDarren Stevens 	 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
3142687e16bcSDarren Stevens 	 * so that generic isa-bridge code can add the SB600 and its on-board
3143687e16bcSDarren Stevens 	 * peripherals.
3144687e16bcSDarren Stevens 	 */
3145687e16bcSDarren Stevens 	name = "/pxp@0,e0000000/io-bridge@0";
3146687e16bcSDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
3147687e16bcSDarren Stevens 	if (!PHANDLE_VALID(iob))
3148687e16bcSDarren Stevens 		return;
3149687e16bcSDarren Stevens 
3150687e16bcSDarren Stevens 	/* device_type is already set, just change it. */
3151687e16bcSDarren Stevens 
3152687e16bcSDarren Stevens 	prom_printf("Changing device_type of SB600 node...\n");
3153687e16bcSDarren Stevens 
3154687e16bcSDarren Stevens 	prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
315550246787SDarren Stevens }
315650246787SDarren Stevens #else	/* !CONFIG_PPC_PASEMI_NEMO */
315750246787SDarren Stevens static inline void fixup_device_tree_pasemi(void) { }
315850246787SDarren Stevens #endif
315950246787SDarren Stevens 
316054f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
316154f4ee18SHollis Blanchard {
316254f4ee18SHollis Blanchard 	fixup_device_tree_maple();
31638f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
3164e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
316554f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
316688fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
316750246787SDarren Stevens 	fixup_device_tree_pasemi();
316854f4ee18SHollis Blanchard }
31699b6b563cSPaul Mackerras 
31709b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
31719b6b563cSPaul Mackerras {
3172493adffcSBenjamin Herrenschmidt 	__be32 rval;
31739b6b563cSPaul Mackerras 	ihandle prom_cpu;
31749b6b563cSPaul Mackerras 	phandle cpu_pkg;
31759b6b563cSPaul Mackerras 
3176493adffcSBenjamin Herrenschmidt 	rval = 0;
3177493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
3178a575b807SPaul Mackerras 		return;
3179493adffcSBenjamin Herrenschmidt 	prom_cpu = be32_to_cpu(rval);
31809b6b563cSPaul Mackerras 
31819b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
31829b6b563cSPaul Mackerras 
3183af2b7fa1SDarren Stevens 	if (!PHANDLE_VALID(cpu_pkg))
3184af2b7fa1SDarren Stevens 		return;
3185af2b7fa1SDarren Stevens 
3186493adffcSBenjamin Herrenschmidt 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
3187493adffcSBenjamin Herrenschmidt 	prom.cpu = be32_to_cpu(rval);
31889b6b563cSPaul Mackerras 
3189eae5f709SMathieu Malaterre 	prom_debug("Booting CPU hw index = %d\n", prom.cpu);
31909b6b563cSPaul Mackerras }
31919b6b563cSPaul Mackerras 
31929b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
31939b6b563cSPaul Mackerras {
31949b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
31959b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
3196493adffcSBenjamin Herrenschmidt 		__be64 val;
31979b6b563cSPaul Mackerras 
31985827d416SAnton Blanchard 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
31995827d416SAnton Blanchard 		prom_initrd_end = prom_initrd_start + r4;
32009b6b563cSPaul Mackerras 
3201493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_start);
32025827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
3203a23414beSPaul Mackerras 			     &val, sizeof(val));
3204493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_end);
32055827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
3206a23414beSPaul Mackerras 			     &val, sizeof(val));
32079b6b563cSPaul Mackerras 
32085827d416SAnton Blanchard 		reserve_mem(prom_initrd_start,
32095827d416SAnton Blanchard 			    prom_initrd_end - prom_initrd_start);
32109b6b563cSPaul Mackerras 
3211eae5f709SMathieu Malaterre 		prom_debug("initrd_start=0x%lx\n", prom_initrd_start);
3212eae5f709SMathieu Malaterre 		prom_debug("initrd_end=0x%lx\n", prom_initrd_end);
32139b6b563cSPaul Mackerras 	}
32149b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
32159b6b563cSPaul Mackerras }
32169b6b563cSPaul Mackerras 
32175ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64
32185ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE
32195ac47f7aSAnton Blanchard static void reloc_toc(void)
32205ac47f7aSAnton Blanchard {
32215ac47f7aSAnton Blanchard }
32225ac47f7aSAnton Blanchard 
32235ac47f7aSAnton Blanchard static void unreloc_toc(void)
32245ac47f7aSAnton Blanchard {
32255ac47f7aSAnton Blanchard }
32265ac47f7aSAnton Blanchard #else
32271674400aSAnton Blanchard static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
32285ac47f7aSAnton Blanchard {
32295ac47f7aSAnton Blanchard 	unsigned long i;
32301674400aSAnton Blanchard 	unsigned long *toc_entry;
32311674400aSAnton Blanchard 
32321674400aSAnton Blanchard 	/* Get the start of the TOC by using r2 directly. */
32331674400aSAnton Blanchard 	asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
32345ac47f7aSAnton Blanchard 
32355ac47f7aSAnton Blanchard 	for (i = 0; i < nr_entries; i++) {
32365ac47f7aSAnton Blanchard 		*toc_entry = *toc_entry + offset;
32375ac47f7aSAnton Blanchard 		toc_entry++;
32385ac47f7aSAnton Blanchard 	}
32395ac47f7aSAnton Blanchard }
32405ac47f7aSAnton Blanchard 
32415ac47f7aSAnton Blanchard static void reloc_toc(void)
32425ac47f7aSAnton Blanchard {
32435ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
32445ac47f7aSAnton Blanchard 	unsigned long nr_entries =
32455ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
32465ac47f7aSAnton Blanchard 
32471674400aSAnton Blanchard 	__reloc_toc(offset, nr_entries);
32485ac47f7aSAnton Blanchard 
32495ac47f7aSAnton Blanchard 	mb();
32505ac47f7aSAnton Blanchard }
32515ac47f7aSAnton Blanchard 
32525ac47f7aSAnton Blanchard static void unreloc_toc(void)
32535ac47f7aSAnton Blanchard {
32545ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
32555ac47f7aSAnton Blanchard 	unsigned long nr_entries =
32565ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
32575ac47f7aSAnton Blanchard 
32585ac47f7aSAnton Blanchard 	mb();
32595ac47f7aSAnton Blanchard 
32601674400aSAnton Blanchard 	__reloc_toc(-offset, nr_entries);
32615ac47f7aSAnton Blanchard }
32625ac47f7aSAnton Blanchard #endif
32635ac47f7aSAnton Blanchard #endif
326427f44888SBenjamin Herrenschmidt 
32656a9c930bSRam Pai #ifdef CONFIG_PPC_SVM
32666a9c930bSRam Pai /*
32676a9c930bSRam Pai  * Perform the Enter Secure Mode ultracall.
32686a9c930bSRam Pai  */
32696a9c930bSRam Pai static int enter_secure_mode(unsigned long kbase, unsigned long fdt)
32706a9c930bSRam Pai {
32716a9c930bSRam Pai 	register unsigned long r3 asm("r3") = UV_ESM;
32726a9c930bSRam Pai 	register unsigned long r4 asm("r4") = kbase;
32736a9c930bSRam Pai 	register unsigned long r5 asm("r5") = fdt;
32746a9c930bSRam Pai 
32756a9c930bSRam Pai 	asm volatile("sc 2" : "+r"(r3) : "r"(r4), "r"(r5));
32766a9c930bSRam Pai 
32776a9c930bSRam Pai 	return r3;
32786a9c930bSRam Pai }
32796a9c930bSRam Pai 
32806a9c930bSRam Pai /*
32816a9c930bSRam Pai  * Call the Ultravisor to transfer us to secure memory if we have an ESM blob.
32826a9c930bSRam Pai  */
3283aff77951SVladis Dronov static void __init setup_secure_guest(unsigned long kbase, unsigned long fdt)
32846a9c930bSRam Pai {
32856a9c930bSRam Pai 	int ret;
32866a9c930bSRam Pai 
32876a9c930bSRam Pai 	if (!prom_svm_enable)
32886a9c930bSRam Pai 		return;
32896a9c930bSRam Pai 
32906a9c930bSRam Pai 	/* Switch to secure mode. */
32916a9c930bSRam Pai 	prom_printf("Switching to secure mode.\n");
32926a9c930bSRam Pai 
329305d9a952SThiago Jung Bauermann 	/*
329405d9a952SThiago Jung Bauermann 	 * The ultravisor will do an integrity check of the kernel image but we
329505d9a952SThiago Jung Bauermann 	 * relocated it so the check will fail. Restore the original image by
329605d9a952SThiago Jung Bauermann 	 * relocating it back to the kernel virtual base address.
329705d9a952SThiago Jung Bauermann 	 */
329805d9a952SThiago Jung Bauermann 	if (IS_ENABLED(CONFIG_RELOCATABLE))
329905d9a952SThiago Jung Bauermann 		relocate(KERNELBASE);
330005d9a952SThiago Jung Bauermann 
33016a9c930bSRam Pai 	ret = enter_secure_mode(kbase, fdt);
330205d9a952SThiago Jung Bauermann 
330305d9a952SThiago Jung Bauermann 	/* Relocate the kernel again. */
330405d9a952SThiago Jung Bauermann 	if (IS_ENABLED(CONFIG_RELOCATABLE))
330505d9a952SThiago Jung Bauermann 		relocate(kbase);
330605d9a952SThiago Jung Bauermann 
33076a9c930bSRam Pai 	if (ret != U_SUCCESS) {
33086a9c930bSRam Pai 		prom_printf("Returned %d from switching to secure mode.\n", ret);
33096a9c930bSRam Pai 		prom_rtas_os_term("Switch to secure mode failed.\n");
33106a9c930bSRam Pai 	}
33116a9c930bSRam Pai }
33126a9c930bSRam Pai #else
3313aff77951SVladis Dronov static void __init setup_secure_guest(unsigned long kbase, unsigned long fdt)
33146a9c930bSRam Pai {
33156a9c930bSRam Pai }
33166a9c930bSRam Pai #endif /* CONFIG_PPC_SVM */
33176a9c930bSRam Pai 
33189b6b563cSPaul Mackerras /*
33199b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
33209b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
33219b6b563cSPaul Mackerras  */
33229b6b563cSPaul Mackerras 
33239b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
33249b6b563cSPaul Mackerras 			       unsigned long pp,
3325549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
3326549e8152SPaul Mackerras 			       unsigned long kbase)
33279b6b563cSPaul Mackerras {
33289b6b563cSPaul Mackerras 	unsigned long hdr;
33299b6b563cSPaul Mackerras 
33309b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
3331549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
33329b6b563cSPaul Mackerras 	reloc_got2(offset);
33335ac47f7aSAnton Blanchard #else
33345ac47f7aSAnton Blanchard 	reloc_toc();
33359b6b563cSPaul Mackerras #endif
33369b6b563cSPaul Mackerras 
33379b6b563cSPaul Mackerras 	/*
33389b6b563cSPaul Mackerras 	 * First zero the BSS
33399b6b563cSPaul Mackerras 	 */
33405827d416SAnton Blanchard 	memset(&__bss_start, 0, __bss_stop - __bss_start);
33419b6b563cSPaul Mackerras 
33429b6b563cSPaul Mackerras 	/*
33439b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
33449b6b563cSPaul Mackerras 	 * like /chosen
33459b6b563cSPaul Mackerras 	 */
33469b6b563cSPaul Mackerras 	prom_init_client_services(pp);
33479b6b563cSPaul Mackerras 
33489b6b563cSPaul Mackerras 	/*
3349a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
3350a23414beSPaul Mackerras 	 * and other workarounds
3351a23414beSPaul Mackerras 	 */
3352a23414beSPaul Mackerras 	prom_find_mmu();
3353a23414beSPaul Mackerras 
3354a23414beSPaul Mackerras 	/*
33559b6b563cSPaul Mackerras 	 * Init prom stdout device
33569b6b563cSPaul Mackerras 	 */
33579b6b563cSPaul Mackerras 	prom_init_stdout();
33589b6b563cSPaul Mackerras 
33595827d416SAnton Blanchard 	prom_printf("Preparing to boot %s", linux_banner);
3360e7943fbbSMichael Ellerman 
33619b6b563cSPaul Mackerras 	/*
33629b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
33639b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
33649b6b563cSPaul Mackerras 	 */
33655827d416SAnton Blanchard 	of_platform = prom_find_machine_type();
33665827d416SAnton Blanchard 	prom_printf("Detected machine type: %x\n", of_platform);
33679b6b563cSPaul Mackerras 
33680f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
3369add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
3370add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
3371add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
3372549e8152SPaul Mackerras #endif
3373add60ef3SOlaf Hering 
3374add60ef3SOlaf Hering 	/*
3375add60ef3SOlaf Hering 	 * Check for an initrd
3376add60ef3SOlaf Hering 	 */
3377add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
3378add60ef3SOlaf Hering 
337912cc9fd6SSuraj Jitindar Singh 	/*
338012cc9fd6SSuraj Jitindar Singh 	 * Do early parsing of command line
338112cc9fd6SSuraj Jitindar Singh 	 */
338212cc9fd6SSuraj Jitindar Singh 	early_cmdline_parse();
338312cc9fd6SSuraj Jitindar Singh 
338411fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
33859b6b563cSPaul Mackerras 	/*
33869b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
33879b6b563cSPaul Mackerras 	 */
33885827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES ||
33895827d416SAnton Blanchard 	    of_platform == PLATFORM_PSERIES_LPAR)
33909b6b563cSPaul Mackerras 		prom_send_capabilities();
33919b6b563cSPaul Mackerras #endif
33929b6b563cSPaul Mackerras 
33939b6b563cSPaul Mackerras 	/*
3394f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
33959b6b563cSPaul Mackerras 	 */
33965827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC)
3397549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
33989b6b563cSPaul Mackerras 
33999b6b563cSPaul Mackerras 	/*
34009b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
34019b6b563cSPaul Mackerras 	 */
34029b6b563cSPaul Mackerras 	prom_init_mem();
34039b6b563cSPaul Mackerras 
34049b6b563cSPaul Mackerras 	/*
34059b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
34069b6b563cSPaul Mackerras 	 */
34079b6b563cSPaul Mackerras 	prom_find_boot_cpu();
34089b6b563cSPaul Mackerras 
34099b6b563cSPaul Mackerras 	/*
34109b6b563cSPaul Mackerras 	 * Initialize display devices
34119b6b563cSPaul Mackerras 	 */
34129b6b563cSPaul Mackerras 	prom_check_displays();
34139b6b563cSPaul Mackerras 
3414493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
34159b6b563cSPaul Mackerras 	/*
34169b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
34179b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
34189b6b563cSPaul Mackerras 	 * available for us here...
34199b6b563cSPaul Mackerras 	 */
34205827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES)
34219b6b563cSPaul Mackerras 		prom_initialize_tce_table();
34229b6b563cSPaul Mackerras #endif
34239b6b563cSPaul Mackerras 
34249b6b563cSPaul Mackerras 	/*
342527f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
342627f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
34279b6b563cSPaul Mackerras 	 */
342811fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
34299b6b563cSPaul Mackerras 		prom_instantiate_rtas();
343027f44888SBenjamin Herrenschmidt 
34314a727429SAshley Lai #ifdef CONFIG_PPC64
34324a727429SAshley Lai 	/* instantiate sml */
34334a727429SAshley Lai 	prom_instantiate_sml();
34344a727429SAshley Lai #endif
34354a727429SAshley Lai 
343627f44888SBenjamin Herrenschmidt 	/*
343727f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
343827f44888SBenjamin Herrenschmidt 	 *
343927f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
3440dbe78b40SBenjamin Herrenschmidt 	 *
3441dbe78b40SBenjamin Herrenschmidt 	 * (This must be done after instanciating RTAS)
344227f44888SBenjamin Herrenschmidt 	 */
344311fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
344427f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
34459b6b563cSPaul Mackerras 
34469b6b563cSPaul Mackerras 	/*
34479b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
34489b6b563cSPaul Mackerras 	 */
3449493adffcSBenjamin Herrenschmidt 	if (prom_memory_limit) {
3450493adffcSBenjamin Herrenschmidt 		__be64 val = cpu_to_be64(prom_memory_limit);
34515827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3452493adffcSBenjamin Herrenschmidt 			     &val, sizeof(val));
3453493adffcSBenjamin Herrenschmidt 	}
34549b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
34555827d416SAnton Blanchard 	if (prom_iommu_off)
34565827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3457a23414beSPaul Mackerras 			     NULL, 0);
34589b6b563cSPaul Mackerras 
34595827d416SAnton Blanchard 	if (prom_iommu_force_on)
34605827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3461a23414beSPaul Mackerras 			     NULL, 0);
34629b6b563cSPaul Mackerras 
34635827d416SAnton Blanchard 	if (prom_tce_alloc_start) {
34645827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
34655827d416SAnton Blanchard 			     &prom_tce_alloc_start,
34669b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
34675827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
34685827d416SAnton Blanchard 			     &prom_tce_alloc_end,
34699b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
34709b6b563cSPaul Mackerras 	}
34719b6b563cSPaul Mackerras #endif
34729b6b563cSPaul Mackerras 
34739b6b563cSPaul Mackerras 	/*
34749b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
34759b6b563cSPaul Mackerras 	 */
34769b6b563cSPaul Mackerras 	fixup_device_tree();
34779b6b563cSPaul Mackerras 
34789b6b563cSPaul Mackerras 	/*
34799b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
34809b6b563cSPaul Mackerras 	 */
34819b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
34829b6b563cSPaul Mackerras 	flatten_device_tree();
34839b6b563cSPaul Mackerras 
34843825ac0eSPaul Mackerras 	/*
34853825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
34863825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
348711fdb309SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101).
34883825ac0eSPaul Mackerras 	 */
348911fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
34909b6b563cSPaul Mackerras 		prom_close_stdin();
34919b6b563cSPaul Mackerras 
34929b6b563cSPaul Mackerras 	/*
34939b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
34949b6b563cSPaul Mackerras 	 * devices etc...
34959b6b563cSPaul Mackerras 	 */
34967e862d7eSMichael Ellerman 	prom_printf("Quiescing Open Firmware ...\n");
34979b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
34989b6b563cSPaul Mackerras 
34999b6b563cSPaul Mackerras 	/*
35009b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
35019b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
35029b6b563cSPaul Mackerras 	 * is common to us and kexec
35039b6b563cSPaul Mackerras 	 */
35045827d416SAnton Blanchard 	hdr = dt_header_start;
350540dfef66SBenjamin Herrenschmidt 
35067d70c63cSBenjamin Herrenschmidt 	prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
3507eae5f709SMathieu Malaterre 	prom_debug("->dt_header_start=0x%lx\n", hdr);
35089b6b563cSPaul Mackerras 
35099b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
35109b6b563cSPaul Mackerras 	reloc_got2(-offset);
35115ac47f7aSAnton Blanchard #else
35125ac47f7aSAnton Blanchard 	unreloc_toc();
35139b6b563cSPaul Mackerras #endif
35149b6b563cSPaul Mackerras 
35156a9c930bSRam Pai 	/* Move to secure memory if we're supposed to be secure guests. */
35166a9c930bSRam Pai 	setup_secure_guest(kbase, hdr);
35176a9c930bSRam Pai 
35186e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
35199b6b563cSPaul Mackerras 
35209b6b563cSPaul Mackerras 	return 0;
35219b6b563cSPaul Mackerras }
3522