xref: /linux/arch/powerpc/kernel/prom_init.c (revision c21f5a9ed85ca3e914ca11f421677ae9ae0d04b0)
19b6b563cSPaul Mackerras /*
29b6b563cSPaul Mackerras  * Procedures for interfacing to Open Firmware.
39b6b563cSPaul Mackerras  *
49b6b563cSPaul Mackerras  * Paul Mackerras	August 1996.
59b6b563cSPaul Mackerras  * Copyright (C) 1996-2005 Paul Mackerras.
69b6b563cSPaul Mackerras  *
79b6b563cSPaul Mackerras  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
89b6b563cSPaul Mackerras  *    {engebret|bergner}@us.ibm.com
99b6b563cSPaul Mackerras  *
109b6b563cSPaul Mackerras  *      This program is free software; you can redistribute it and/or
119b6b563cSPaul Mackerras  *      modify it under the terms of the GNU General Public License
129b6b563cSPaul Mackerras  *      as published by the Free Software Foundation; either version
139b6b563cSPaul Mackerras  *      2 of the License, or (at your option) any later version.
149b6b563cSPaul Mackerras  */
159b6b563cSPaul Mackerras 
169b6b563cSPaul Mackerras #undef DEBUG_PROM
179b6b563cSPaul Mackerras 
18054f367aSDaniel Axtens /* we cannot use FORTIFY as it brings in new symbols */
19054f367aSDaniel Axtens #define __NO_FORTIFY
20054f367aSDaniel Axtens 
219b6b563cSPaul Mackerras #include <stdarg.h>
229b6b563cSPaul Mackerras #include <linux/kernel.h>
239b6b563cSPaul Mackerras #include <linux/string.h>
249b6b563cSPaul Mackerras #include <linux/init.h>
259b6b563cSPaul Mackerras #include <linux/threads.h>
269b6b563cSPaul Mackerras #include <linux/spinlock.h>
279b6b563cSPaul Mackerras #include <linux/types.h>
289b6b563cSPaul Mackerras #include <linux/pci.h>
299b6b563cSPaul Mackerras #include <linux/proc_fs.h>
309b6b563cSPaul Mackerras #include <linux/delay.h>
319b6b563cSPaul Mackerras #include <linux/initrd.h>
329b6b563cSPaul Mackerras #include <linux/bitops.h>
339b6b563cSPaul Mackerras #include <asm/prom.h>
349b6b563cSPaul Mackerras #include <asm/rtas.h>
359b6b563cSPaul Mackerras #include <asm/page.h>
369b6b563cSPaul Mackerras #include <asm/processor.h>
379b6b563cSPaul Mackerras #include <asm/irq.h>
389b6b563cSPaul Mackerras #include <asm/io.h>
399b6b563cSPaul Mackerras #include <asm/smp.h>
409b6b563cSPaul Mackerras #include <asm/mmu.h>
419b6b563cSPaul Mackerras #include <asm/pgtable.h>
429b6b563cSPaul Mackerras #include <asm/iommu.h>
439b6b563cSPaul Mackerras #include <asm/btext.h>
449b6b563cSPaul Mackerras #include <asm/sections.h>
459b6b563cSPaul Mackerras #include <asm/machdep.h>
460545d543SDaniel Axtens #include <asm/asm-prototypes.h>
479b6b563cSPaul Mackerras 
489b6b563cSPaul Mackerras #include <linux/linux_logo.h>
499b6b563cSPaul Mackerras 
50e63334e5SBenjamin Herrenschmidt /* All of prom_init bss lives here */
515f69e388SBenjamin Herrenschmidt #define __prombss __section(.bss.prominit)
52e63334e5SBenjamin Herrenschmidt 
539b6b563cSPaul Mackerras /*
549b6b563cSPaul Mackerras  * Eventually bump that one up
559b6b563cSPaul Mackerras  */
569b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE	0x100000
579b6b563cSPaul Mackerras 
589b6b563cSPaul Mackerras /*
599b6b563cSPaul Mackerras  * This is the size of the local memory reserve map that gets copied
609b6b563cSPaul Mackerras  * into the boot params passed to the kernel. That size is totally
619b6b563cSPaul Mackerras  * flexible as the kernel just reads the list until it encounters an
629b6b563cSPaul Mackerras  * entry with size 0, so it can be changed without breaking binary
639b6b563cSPaul Mackerras  * compatibility
649b6b563cSPaul Mackerras  */
659b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE	8
669b6b563cSPaul Mackerras 
679b6b563cSPaul Mackerras /*
689b6b563cSPaul Mackerras  * prom_init() is called very early on, before the kernel text
699b6b563cSPaul Mackerras  * and data have been mapped to KERNELBASE.  At this point the code
709b6b563cSPaul Mackerras  * is running at whatever address it has been loaded at.
719b6b563cSPaul Mackerras  * On ppc32 we compile with -mrelocatable, which means that references
729b6b563cSPaul Mackerras  * to extern and static variables get relocated automatically.
735ac47f7aSAnton Blanchard  * ppc64 objects are always relocatable, we just need to relocate the
745ac47f7aSAnton Blanchard  * TOC.
759b6b563cSPaul Mackerras  *
769b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
779b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
789b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
799b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
809b6b563cSPaul Mackerras  *
819b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
829b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
839b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
849b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
859b6b563cSPaul Mackerras  */
865ac47f7aSAnton Blanchard #define ADDR(x)		(u32)(unsigned long)(x)
875ac47f7aSAnton Blanchard 
889b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
89a23414beSPaul Mackerras #define OF_WORKAROUNDS	0
909b6b563cSPaul Mackerras #else
91a23414beSPaul Mackerras #define OF_WORKAROUNDS	of_workarounds
92e63334e5SBenjamin Herrenschmidt static int of_workarounds __prombss;
939b6b563cSPaul Mackerras #endif
949b6b563cSPaul Mackerras 
95a23414beSPaul Mackerras #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
96a23414beSPaul Mackerras #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
97a23414beSPaul Mackerras 
989b6b563cSPaul Mackerras #define PROM_BUG() do {						\
999b6b563cSPaul Mackerras         prom_printf("kernel BUG at %s line 0x%x!\n",		\
1005827d416SAnton Blanchard 		    __FILE__, __LINE__);			\
1019b6b563cSPaul Mackerras         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
1029b6b563cSPaul Mackerras } while (0)
1039b6b563cSPaul Mackerras 
1049b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1059b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1069b6b563cSPaul Mackerras #else
10785aa4b98SMathieu Malaterre #define prom_debug(x...)	do { } while (0)
1089b6b563cSPaul Mackerras #endif
1099b6b563cSPaul Mackerras 
1109b6b563cSPaul Mackerras 
1119b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1129b6b563cSPaul Mackerras 
1139b6b563cSPaul Mackerras struct prom_args {
114493adffcSBenjamin Herrenschmidt         __be32 service;
115493adffcSBenjamin Herrenschmidt         __be32 nargs;
116493adffcSBenjamin Herrenschmidt         __be32 nret;
117493adffcSBenjamin Herrenschmidt         __be32 args[10];
1189b6b563cSPaul Mackerras };
1199b6b563cSPaul Mackerras 
1209b6b563cSPaul Mackerras struct prom_t {
1219b6b563cSPaul Mackerras 	ihandle root;
122a23414beSPaul Mackerras 	phandle chosen;
1239b6b563cSPaul Mackerras 	int cpu;
1249b6b563cSPaul Mackerras 	ihandle stdout;
125a575b807SPaul Mackerras 	ihandle mmumap;
126a23414beSPaul Mackerras 	ihandle memory;
1279b6b563cSPaul Mackerras };
1289b6b563cSPaul Mackerras 
1299b6b563cSPaul Mackerras struct mem_map_entry {
130493adffcSBenjamin Herrenschmidt 	__be64	base;
131493adffcSBenjamin Herrenschmidt 	__be64	size;
1329b6b563cSPaul Mackerras };
1339b6b563cSPaul Mackerras 
134493adffcSBenjamin Herrenschmidt typedef __be32 cell_t;
1359b6b563cSPaul Mackerras 
1366e35d5daSBenjamin Herrenschmidt extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
1376e35d5daSBenjamin Herrenschmidt 		    unsigned long r6, unsigned long r7, unsigned long r8,
1386e35d5daSBenjamin Herrenschmidt 		    unsigned long r9);
1399b6b563cSPaul Mackerras 
1409b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
141c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1429b6b563cSPaul Mackerras #else
143c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1449b6b563cSPaul Mackerras {
145c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1469b6b563cSPaul Mackerras }
1479b6b563cSPaul Mackerras #endif
1489b6b563cSPaul Mackerras 
1499b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1509b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1519b6b563cSPaul Mackerras 
1529b6b563cSPaul Mackerras /* prom structure */
153e63334e5SBenjamin Herrenschmidt static struct prom_t __prombss prom;
1549b6b563cSPaul Mackerras 
155e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_entry;
1569b6b563cSPaul Mackerras 
157e63334e5SBenjamin Herrenschmidt static char __prombss of_stdout_device[256];
158d7fbe2a0SChristophe Leroy static char __prombss prom_scratch[256];
1599b6b563cSPaul Mackerras 
160e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_header_start;
161e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_struct_start, dt_struct_end;
162e63334e5SBenjamin Herrenschmidt static unsigned long __prombss dt_string_start, dt_string_end;
1639b6b563cSPaul Mackerras 
164e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_initrd_start, prom_initrd_end;
1659b6b563cSPaul Mackerras 
1669b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
167e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_force_on;
168e63334e5SBenjamin Herrenschmidt static int __prombss prom_iommu_off;
169e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_start;
170e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_tce_alloc_end;
1719b6b563cSPaul Mackerras #endif
1729b6b563cSPaul Mackerras 
173c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
1748ca2d515SBenjamin Herrenschmidt static bool __prombss prom_radix_disable;
175c886087cSBenjamin Herrenschmidt #endif
176014d02cbSSuraj Jitindar Singh 
177014d02cbSSuraj Jitindar Singh struct platform_support {
178014d02cbSSuraj Jitindar Singh 	bool hash_mmu;
179014d02cbSSuraj Jitindar Singh 	bool radix_mmu;
180014d02cbSSuraj Jitindar Singh 	bool radix_gtse;
181ac5e5a54SCédric Le Goater 	bool xive;
182014d02cbSSuraj Jitindar Singh };
183014d02cbSSuraj Jitindar Singh 
184e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
185e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
186e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
187e8222502SBenjamin Herrenschmidt  */
188e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
189e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
190e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
191e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
192e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
193e8222502SBenjamin Herrenschmidt 
194e63334e5SBenjamin Herrenschmidt static int __prombss of_platform;
1959b6b563cSPaul Mackerras 
196e63334e5SBenjamin Herrenschmidt static char __prombss prom_cmd_line[COMMAND_LINE_SIZE];
1979b6b563cSPaul Mackerras 
198e63334e5SBenjamin Herrenschmidt static unsigned long __prombss prom_memory_limit;
199cf68787bSBenjamin Krill 
200e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top;
201e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_top_high;
202e63334e5SBenjamin Herrenschmidt static unsigned long __prombss alloc_bottom;
203e63334e5SBenjamin Herrenschmidt static unsigned long __prombss rmo_top;
204e63334e5SBenjamin Herrenschmidt static unsigned long __prombss ram_top;
2059b6b563cSPaul Mackerras 
206e63334e5SBenjamin Herrenschmidt static struct mem_map_entry __prombss mem_reserve_map[MEM_RESERVE_MAP_SIZE];
207e63334e5SBenjamin Herrenschmidt static int __prombss mem_reserve_cnt;
2089b6b563cSPaul Mackerras 
209e63334e5SBenjamin Herrenschmidt static cell_t __prombss regbuf[1024];
2109b6b563cSPaul Mackerras 
2118ca2d515SBenjamin Herrenschmidt static bool  __prombss rtas_has_query_cpu_stopped;
212dbe78b40SBenjamin Herrenschmidt 
2139b6b563cSPaul Mackerras 
2149b6b563cSPaul Mackerras /*
2159b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2169b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2179b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2189b6b563cSPaul Mackerras  * it is valid
2199b6b563cSPaul Mackerras  */
2209b6b563cSPaul Mackerras 
2219b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2229b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2239b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2249b6b563cSPaul Mackerras 
225450e7dd4SChristophe Leroy /* Copied from lib/string.c and lib/kstrtox.c */
226450e7dd4SChristophe Leroy 
227450e7dd4SChristophe Leroy static int __init prom_strcmp(const char *cs, const char *ct)
228450e7dd4SChristophe Leroy {
229450e7dd4SChristophe Leroy 	unsigned char c1, c2;
230450e7dd4SChristophe Leroy 
231450e7dd4SChristophe Leroy 	while (1) {
232450e7dd4SChristophe Leroy 		c1 = *cs++;
233450e7dd4SChristophe Leroy 		c2 = *ct++;
234450e7dd4SChristophe Leroy 		if (c1 != c2)
235450e7dd4SChristophe Leroy 			return c1 < c2 ? -1 : 1;
236450e7dd4SChristophe Leroy 		if (!c1)
237450e7dd4SChristophe Leroy 			break;
238450e7dd4SChristophe Leroy 	}
239450e7dd4SChristophe Leroy 	return 0;
240450e7dd4SChristophe Leroy }
241450e7dd4SChristophe Leroy 
242450e7dd4SChristophe Leroy static char __init *prom_strcpy(char *dest, const char *src)
243450e7dd4SChristophe Leroy {
244450e7dd4SChristophe Leroy 	char *tmp = dest;
245450e7dd4SChristophe Leroy 
246450e7dd4SChristophe Leroy 	while ((*dest++ = *src++) != '\0')
247450e7dd4SChristophe Leroy 		/* nothing */;
248450e7dd4SChristophe Leroy 	return tmp;
249450e7dd4SChristophe Leroy }
250450e7dd4SChristophe Leroy 
251450e7dd4SChristophe Leroy static int __init prom_strncmp(const char *cs, const char *ct, size_t count)
252450e7dd4SChristophe Leroy {
253450e7dd4SChristophe Leroy 	unsigned char c1, c2;
254450e7dd4SChristophe Leroy 
255450e7dd4SChristophe Leroy 	while (count) {
256450e7dd4SChristophe Leroy 		c1 = *cs++;
257450e7dd4SChristophe Leroy 		c2 = *ct++;
258450e7dd4SChristophe Leroy 		if (c1 != c2)
259450e7dd4SChristophe Leroy 			return c1 < c2 ? -1 : 1;
260450e7dd4SChristophe Leroy 		if (!c1)
261450e7dd4SChristophe Leroy 			break;
262450e7dd4SChristophe Leroy 		count--;
263450e7dd4SChristophe Leroy 	}
264450e7dd4SChristophe Leroy 	return 0;
265450e7dd4SChristophe Leroy }
266450e7dd4SChristophe Leroy 
267450e7dd4SChristophe Leroy static size_t __init prom_strlen(const char *s)
268450e7dd4SChristophe Leroy {
269450e7dd4SChristophe Leroy 	const char *sc;
270450e7dd4SChristophe Leroy 
271450e7dd4SChristophe Leroy 	for (sc = s; *sc != '\0'; ++sc)
272450e7dd4SChristophe Leroy 		/* nothing */;
273450e7dd4SChristophe Leroy 	return sc - s;
274450e7dd4SChristophe Leroy }
275450e7dd4SChristophe Leroy 
276450e7dd4SChristophe Leroy static int __init prom_memcmp(const void *cs, const void *ct, size_t count)
277450e7dd4SChristophe Leroy {
278450e7dd4SChristophe Leroy 	const unsigned char *su1, *su2;
279450e7dd4SChristophe Leroy 	int res = 0;
280450e7dd4SChristophe Leroy 
281450e7dd4SChristophe Leroy 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
282450e7dd4SChristophe Leroy 		if ((res = *su1 - *su2) != 0)
283450e7dd4SChristophe Leroy 			break;
284450e7dd4SChristophe Leroy 	return res;
285450e7dd4SChristophe Leroy }
286450e7dd4SChristophe Leroy 
287450e7dd4SChristophe Leroy static char __init *prom_strstr(const char *s1, const char *s2)
288450e7dd4SChristophe Leroy {
289450e7dd4SChristophe Leroy 	size_t l1, l2;
290450e7dd4SChristophe Leroy 
291450e7dd4SChristophe Leroy 	l2 = prom_strlen(s2);
292450e7dd4SChristophe Leroy 	if (!l2)
293450e7dd4SChristophe Leroy 		return (char *)s1;
294450e7dd4SChristophe Leroy 	l1 = prom_strlen(s1);
295450e7dd4SChristophe Leroy 	while (l1 >= l2) {
296450e7dd4SChristophe Leroy 		l1--;
297450e7dd4SChristophe Leroy 		if (!prom_memcmp(s1, s2, l2))
298450e7dd4SChristophe Leroy 			return (char *)s1;
299450e7dd4SChristophe Leroy 		s1++;
300450e7dd4SChristophe Leroy 	}
301450e7dd4SChristophe Leroy 	return NULL;
302450e7dd4SChristophe Leroy }
303450e7dd4SChristophe Leroy 
304450e7dd4SChristophe Leroy static size_t __init prom_strlcpy(char *dest, const char *src, size_t size)
305450e7dd4SChristophe Leroy {
306450e7dd4SChristophe Leroy 	size_t ret = prom_strlen(src);
307450e7dd4SChristophe Leroy 
308450e7dd4SChristophe Leroy 	if (size) {
309450e7dd4SChristophe Leroy 		size_t len = (ret >= size) ? size - 1 : ret;
310450e7dd4SChristophe Leroy 		memcpy(dest, src, len);
311450e7dd4SChristophe Leroy 		dest[len] = '\0';
312450e7dd4SChristophe Leroy 	}
313450e7dd4SChristophe Leroy 	return ret;
314450e7dd4SChristophe Leroy }
315450e7dd4SChristophe Leroy 
316450e7dd4SChristophe Leroy #ifdef CONFIG_PPC_PSERIES
317450e7dd4SChristophe Leroy static int __init prom_strtobool(const char *s, bool *res)
318450e7dd4SChristophe Leroy {
319450e7dd4SChristophe Leroy 	if (!s)
320450e7dd4SChristophe Leroy 		return -EINVAL;
321450e7dd4SChristophe Leroy 
322450e7dd4SChristophe Leroy 	switch (s[0]) {
323450e7dd4SChristophe Leroy 	case 'y':
324450e7dd4SChristophe Leroy 	case 'Y':
325450e7dd4SChristophe Leroy 	case '1':
326450e7dd4SChristophe Leroy 		*res = true;
327450e7dd4SChristophe Leroy 		return 0;
328450e7dd4SChristophe Leroy 	case 'n':
329450e7dd4SChristophe Leroy 	case 'N':
330450e7dd4SChristophe Leroy 	case '0':
331450e7dd4SChristophe Leroy 		*res = false;
332450e7dd4SChristophe Leroy 		return 0;
333450e7dd4SChristophe Leroy 	case 'o':
334450e7dd4SChristophe Leroy 	case 'O':
335450e7dd4SChristophe Leroy 		switch (s[1]) {
336450e7dd4SChristophe Leroy 		case 'n':
337450e7dd4SChristophe Leroy 		case 'N':
338450e7dd4SChristophe Leroy 			*res = true;
339450e7dd4SChristophe Leroy 			return 0;
340450e7dd4SChristophe Leroy 		case 'f':
341450e7dd4SChristophe Leroy 		case 'F':
342450e7dd4SChristophe Leroy 			*res = false;
343450e7dd4SChristophe Leroy 			return 0;
344450e7dd4SChristophe Leroy 		default:
345450e7dd4SChristophe Leroy 			break;
346450e7dd4SChristophe Leroy 		}
347450e7dd4SChristophe Leroy 	default:
348450e7dd4SChristophe Leroy 		break;
349450e7dd4SChristophe Leroy 	}
350450e7dd4SChristophe Leroy 
351450e7dd4SChristophe Leroy 	return -EINVAL;
352450e7dd4SChristophe Leroy }
353450e7dd4SChristophe Leroy #endif
3549b6b563cSPaul Mackerras 
3559b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
3569b6b563cSPaul Mackerras  * firmware.
3579b6b563cSPaul Mackerras  */
3589b6b563cSPaul Mackerras 
3599b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
3609b6b563cSPaul Mackerras {
3619b6b563cSPaul Mackerras 	int i;
3629b6b563cSPaul Mackerras 	struct prom_args args;
3639b6b563cSPaul Mackerras 	va_list list;
3649b6b563cSPaul Mackerras 
365493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
366493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
367493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
3689b6b563cSPaul Mackerras 
3699b6b563cSPaul Mackerras 	va_start(list, nret);
3709b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
371493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
3729b6b563cSPaul Mackerras 	va_end(list);
3739b6b563cSPaul Mackerras 
3749b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
3759b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
3769b6b563cSPaul Mackerras 
3775827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
378c4988820SPaul Mackerras 		return PROM_ERROR;
3799b6b563cSPaul Mackerras 
380493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
3819b6b563cSPaul Mackerras }
3829b6b563cSPaul Mackerras 
3839b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
3849b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
3859b6b563cSPaul Mackerras {
3869b6b563cSPaul Mackerras 	int i;
3879b6b563cSPaul Mackerras 	struct prom_args args;
3889b6b563cSPaul Mackerras 	va_list list;
3899b6b563cSPaul Mackerras 
390493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
391493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
392493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
3939b6b563cSPaul Mackerras 
3949b6b563cSPaul Mackerras 	va_start(list, rets);
3959b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
396493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
3979b6b563cSPaul Mackerras 	va_end(list);
3989b6b563cSPaul Mackerras 
3999b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
400ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
4019b6b563cSPaul Mackerras 
4025827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
403c4988820SPaul Mackerras 		return PROM_ERROR;
4049b6b563cSPaul Mackerras 
4059b6b563cSPaul Mackerras 	if (rets != NULL)
4069b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
407493adffcSBenjamin Herrenschmidt 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
4089b6b563cSPaul Mackerras 
409493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
4109b6b563cSPaul Mackerras }
4119b6b563cSPaul Mackerras 
4129b6b563cSPaul Mackerras 
4139b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
4149b6b563cSPaul Mackerras {
4159b6b563cSPaul Mackerras 	const char *p, *q;
4169b6b563cSPaul Mackerras 
4175827d416SAnton Blanchard 	if (prom.stdout == 0)
4189b6b563cSPaul Mackerras 		return;
4199b6b563cSPaul Mackerras 
4209b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
4219b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
4229b6b563cSPaul Mackerras 			;
4239b6b563cSPaul Mackerras 		if (q > p)
4245827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
4259b6b563cSPaul Mackerras 		if (*q == 0)
4269b6b563cSPaul Mackerras 			break;
4279b6b563cSPaul Mackerras 		++q;
4285827d416SAnton Blanchard 		call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
4299b6b563cSPaul Mackerras 	}
4309b6b563cSPaul Mackerras }
4319b6b563cSPaul Mackerras 
4329b6b563cSPaul Mackerras 
4338af1da40SMathieu Malaterre /*
4348af1da40SMathieu Malaterre  * Both prom_print_hex & prom_print_dec takes an unsigned long as input so that
4358af1da40SMathieu Malaterre  * we do not need __udivdi3 or __umoddi3 on 32bits.
4368af1da40SMathieu Malaterre  */
4379b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
4389b6b563cSPaul Mackerras {
4399b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
4409b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
4419b6b563cSPaul Mackerras 
4429b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
4439b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
4449b6b563cSPaul Mackerras 		if (buf[i] > '9')
4459b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
4469b6b563cSPaul Mackerras 		val >>= 4;
4479b6b563cSPaul Mackerras 	}
4489b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
4495827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf, nibbles);
4509b6b563cSPaul Mackerras }
4519b6b563cSPaul Mackerras 
4522c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */
4532c48a7d6SMichael Neuling #define UL_DIGITS 21
4542c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val)
4552c48a7d6SMichael Neuling {
4562c48a7d6SMichael Neuling 	int i, size;
4572c48a7d6SMichael Neuling 	char buf[UL_DIGITS+1];
4582c48a7d6SMichael Neuling 
4592c48a7d6SMichael Neuling 	for (i = UL_DIGITS-1; i >= 0;  i--) {
4602c48a7d6SMichael Neuling 		buf[i] = (val % 10) + '0';
4612c48a7d6SMichael Neuling 		val = val/10;
4622c48a7d6SMichael Neuling 		if (val == 0)
4632c48a7d6SMichael Neuling 			break;
4642c48a7d6SMichael Neuling 	}
4652c48a7d6SMichael Neuling 	/* shift stuff down */
4662c48a7d6SMichael Neuling 	size = UL_DIGITS - i;
4675827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf+i, size);
4682c48a7d6SMichael Neuling }
4699b6b563cSPaul Mackerras 
470eae5f709SMathieu Malaterre __printf(1, 2)
4719b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
4729b6b563cSPaul Mackerras {
4739b6b563cSPaul Mackerras 	const char *p, *q, *s;
4749b6b563cSPaul Mackerras 	va_list args;
4759b6b563cSPaul Mackerras 	unsigned long v;
476af277149SBenjamin Herrenschmidt 	long vs;
4778af1da40SMathieu Malaterre 	int n = 0;
4789b6b563cSPaul Mackerras 
4799b6b563cSPaul Mackerras 	va_start(args, format);
4809b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
4819b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
4829b6b563cSPaul Mackerras 			;
4839b6b563cSPaul Mackerras 		if (q > p)
4845827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
4859b6b563cSPaul Mackerras 		if (*q == 0)
4869b6b563cSPaul Mackerras 			break;
4879b6b563cSPaul Mackerras 		if (*q == '\n') {
4889b6b563cSPaul Mackerras 			++q;
4895827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout,
4909b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
4919b6b563cSPaul Mackerras 			continue;
4929b6b563cSPaul Mackerras 		}
4939b6b563cSPaul Mackerras 		++q;
4949b6b563cSPaul Mackerras 		if (*q == 0)
4959b6b563cSPaul Mackerras 			break;
4968af1da40SMathieu Malaterre 		while (*q == 'l') {
4978af1da40SMathieu Malaterre 			++q;
4988af1da40SMathieu Malaterre 			++n;
4998af1da40SMathieu Malaterre 		}
5009b6b563cSPaul Mackerras 		switch (*q) {
5019b6b563cSPaul Mackerras 		case 's':
5029b6b563cSPaul Mackerras 			++q;
5039b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
5049b6b563cSPaul Mackerras 			prom_print(s);
5059b6b563cSPaul Mackerras 			break;
5069b6b563cSPaul Mackerras 		case 'x':
5079b6b563cSPaul Mackerras 			++q;
5088af1da40SMathieu Malaterre 			switch (n) {
5098af1da40SMathieu Malaterre 			case 0:
5108af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
5118af1da40SMathieu Malaterre 				break;
5128af1da40SMathieu Malaterre 			case 1:
5139b6b563cSPaul Mackerras 				v = va_arg(args, unsigned long);
5148af1da40SMathieu Malaterre 				break;
5158af1da40SMathieu Malaterre 			case 2:
5168af1da40SMathieu Malaterre 			default:
5178af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
5188af1da40SMathieu Malaterre 				break;
5198af1da40SMathieu Malaterre 			}
5209b6b563cSPaul Mackerras 			prom_print_hex(v);
5219b6b563cSPaul Mackerras 			break;
5228af1da40SMathieu Malaterre 		case 'u':
5238af1da40SMathieu Malaterre 			++q;
5248af1da40SMathieu Malaterre 			switch (n) {
5258af1da40SMathieu Malaterre 			case 0:
5268af1da40SMathieu Malaterre 				v = va_arg(args, unsigned int);
5278af1da40SMathieu Malaterre 				break;
5288af1da40SMathieu Malaterre 			case 1:
5298af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long);
5308af1da40SMathieu Malaterre 				break;
5318af1da40SMathieu Malaterre 			case 2:
5328af1da40SMathieu Malaterre 			default:
5338af1da40SMathieu Malaterre 				v = va_arg(args, unsigned long long);
5348af1da40SMathieu Malaterre 				break;
5358af1da40SMathieu Malaterre 			}
5368af1da40SMathieu Malaterre 			prom_print_dec(v);
5378af1da40SMathieu Malaterre 			break;
538af277149SBenjamin Herrenschmidt 		case 'd':
539af277149SBenjamin Herrenschmidt 			++q;
5408af1da40SMathieu Malaterre 			switch (n) {
5418af1da40SMathieu Malaterre 			case 0:
542af277149SBenjamin Herrenschmidt 				vs = va_arg(args, int);
543af277149SBenjamin Herrenschmidt 				break;
5448af1da40SMathieu Malaterre 			case 1:
545af277149SBenjamin Herrenschmidt 				vs = va_arg(args, long);
5468af1da40SMathieu Malaterre 				break;
5478af1da40SMathieu Malaterre 			case 2:
5488af1da40SMathieu Malaterre 			default:
5498af1da40SMathieu Malaterre 				vs = va_arg(args, long long);
5508af1da40SMathieu Malaterre 				break;
5518af1da40SMathieu Malaterre 			}
552af277149SBenjamin Herrenschmidt 			if (vs < 0) {
5535827d416SAnton Blanchard 				prom_print("-");
554af277149SBenjamin Herrenschmidt 				vs = -vs;
555af277149SBenjamin Herrenschmidt 			}
556af277149SBenjamin Herrenschmidt 			prom_print_dec(vs);
5572c48a7d6SMichael Neuling 			break;
5589b6b563cSPaul Mackerras 		}
5599b6b563cSPaul Mackerras 	}
5601b855e16SDaniel Axtens 	va_end(args);
5619b6b563cSPaul Mackerras }
5629b6b563cSPaul Mackerras 
5639b6b563cSPaul Mackerras 
564a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
565a575b807SPaul Mackerras 				unsigned long align)
566a575b807SPaul Mackerras {
567a575b807SPaul Mackerras 
568a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
569a23414beSPaul Mackerras 		/*
570a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
571a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
572a23414beSPaul Mackerras 		 */
573a23414beSPaul Mackerras 		int ret;
574a23414beSPaul Mackerras 		prom_arg_t result;
575a23414beSPaul Mackerras 
576a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
5775827d416SAnton Blanchard 				    ADDR("claim"), prom.memory,
578a23414beSPaul Mackerras 				    align, size, virt);
579a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
580a23414beSPaul Mackerras 			return -1;
581a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
5825827d416SAnton Blanchard 				    ADDR("claim"), prom.mmumap,
583a23414beSPaul Mackerras 				    align, size, virt);
584a23414beSPaul Mackerras 		if (ret != 0) {
585a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
5865827d416SAnton Blanchard 				  prom.memory, size, virt);
587a23414beSPaul Mackerras 			return -1;
588a23414beSPaul Mackerras 		}
589a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
590a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
5915827d416SAnton Blanchard 			  ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
592a23414beSPaul Mackerras 		return virt;
593a23414beSPaul Mackerras 	}
594a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
595a23414beSPaul Mackerras 			 (prom_arg_t)align);
596a575b807SPaul Mackerras }
597a575b807SPaul Mackerras 
5989b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
5999b6b563cSPaul Mackerras {
6009b6b563cSPaul Mackerras 	prom_print(reason);
601add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
602add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
6035827d416SAnton Blanchard 	if (of_platform == PLATFORM_POWERMAC)
604add60ef3SOlaf Hering 		asm("trap\n");
605add60ef3SOlaf Hering 
6061d9a4731SStephen Rothwell 	/* ToDo: should put up an SRC here on pSeries */
6079b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
6089b6b563cSPaul Mackerras 
6099b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
6109b6b563cSPaul Mackerras 		;
6119b6b563cSPaul Mackerras }
6129b6b563cSPaul Mackerras 
6139b6b563cSPaul Mackerras 
6149b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
6159b6b563cSPaul Mackerras {
6169b6b563cSPaul Mackerras 	phandle node;
6179b6b563cSPaul Mackerras 
6189b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
6199b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
6209b6b563cSPaul Mackerras 		return 1;
6219b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
6229b6b563cSPaul Mackerras 		return 1;
6239b6b563cSPaul Mackerras 	for (;;) {
6249b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
6259b6b563cSPaul Mackerras 			return 0;
6269b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
6279b6b563cSPaul Mackerras 			return 1;
6289b6b563cSPaul Mackerras 	}
6299b6b563cSPaul Mackerras }
6309b6b563cSPaul Mackerras 
631480795a0SMasahiro Yamada static inline int __init prom_getprop(phandle node, const char *pname,
6329b6b563cSPaul Mackerras 				      void *value, size_t valuelen)
6339b6b563cSPaul Mackerras {
6349b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
6359b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
6369b6b563cSPaul Mackerras }
6379b6b563cSPaul Mackerras 
638480795a0SMasahiro Yamada static inline int __init prom_getproplen(phandle node, const char *pname)
6399b6b563cSPaul Mackerras {
6409b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
6419b6b563cSPaul Mackerras }
6429b6b563cSPaul Mackerras 
643a23414beSPaul Mackerras static void add_string(char **str, const char *q)
6449b6b563cSPaul Mackerras {
645a23414beSPaul Mackerras 	char *p = *str;
646a23414beSPaul Mackerras 
647a23414beSPaul Mackerras 	while (*q)
648a23414beSPaul Mackerras 		*p++ = *q++;
649a23414beSPaul Mackerras 	*p++ = ' ';
650a23414beSPaul Mackerras 	*str = p;
651a23414beSPaul Mackerras }
652a23414beSPaul Mackerras 
653a23414beSPaul Mackerras static char *tohex(unsigned int x)
654a23414beSPaul Mackerras {
6558ca2d515SBenjamin Herrenschmidt 	static const char digits[] __initconst = "0123456789abcdef";
6568ca2d515SBenjamin Herrenschmidt 	static char result[9] __prombss;
657a23414beSPaul Mackerras 	int i;
658a23414beSPaul Mackerras 
659a23414beSPaul Mackerras 	result[8] = 0;
660a23414beSPaul Mackerras 	i = 8;
661a23414beSPaul Mackerras 	do {
662a23414beSPaul Mackerras 		--i;
663a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
664a23414beSPaul Mackerras 		x >>= 4;
665a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
666a23414beSPaul Mackerras 	return &result[i];
667a23414beSPaul Mackerras }
668a23414beSPaul Mackerras 
669a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
670a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
671a23414beSPaul Mackerras {
672a23414beSPaul Mackerras 	char cmd[256], *p;
673a23414beSPaul Mackerras 
674a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
6759b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
6769b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
677a23414beSPaul Mackerras 
678a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
679a23414beSPaul Mackerras 	p = cmd;
680a23414beSPaul Mackerras 	add_string(&p, "dev");
681a23414beSPaul Mackerras 	add_string(&p, nodename);
682a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
683a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
684a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
685450e7dd4SChristophe Leroy 	add_string(&p, tohex(prom_strlen(pname)));
686a23414beSPaul Mackerras 	add_string(&p, "property");
687a23414beSPaul Mackerras 	*p = 0;
688a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
6899b6b563cSPaul Mackerras }
6909b6b563cSPaul Mackerras 
6915827d416SAnton Blanchard /* We can't use the standard versions because of relocation headaches. */
692cf68787bSBenjamin Krill #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
693cf68787bSBenjamin Krill 			 || ('a' <= (c) && (c) <= 'f') \
694cf68787bSBenjamin Krill 			 || ('A' <= (c) && (c) <= 'F'))
695cf68787bSBenjamin Krill 
696cf68787bSBenjamin Krill #define isdigit(c)	('0' <= (c) && (c) <= '9')
697cf68787bSBenjamin Krill #define islower(c)	('a' <= (c) && (c) <= 'z')
698cf68787bSBenjamin Krill #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
699cf68787bSBenjamin Krill 
700493adffcSBenjamin Herrenschmidt static unsigned long prom_strtoul(const char *cp, const char **endp)
701cf68787bSBenjamin Krill {
702cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
703cf68787bSBenjamin Krill 
704cf68787bSBenjamin Krill 	if (*cp == '0') {
705cf68787bSBenjamin Krill 		base = 8;
706cf68787bSBenjamin Krill 		cp++;
707cf68787bSBenjamin Krill 		if (toupper(*cp) == 'X') {
708cf68787bSBenjamin Krill 			cp++;
709cf68787bSBenjamin Krill 			base = 16;
710cf68787bSBenjamin Krill 		}
711cf68787bSBenjamin Krill 	}
712cf68787bSBenjamin Krill 
713cf68787bSBenjamin Krill 	while (isxdigit(*cp) &&
714cf68787bSBenjamin Krill 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
715cf68787bSBenjamin Krill 		result = result * base + value;
716cf68787bSBenjamin Krill 		cp++;
717cf68787bSBenjamin Krill 	}
718cf68787bSBenjamin Krill 
719cf68787bSBenjamin Krill 	if (endp)
720cf68787bSBenjamin Krill 		*endp = cp;
721cf68787bSBenjamin Krill 
722cf68787bSBenjamin Krill 	return result;
723cf68787bSBenjamin Krill }
724cf68787bSBenjamin Krill 
725493adffcSBenjamin Herrenschmidt static unsigned long prom_memparse(const char *ptr, const char **retptr)
726cf68787bSBenjamin Krill {
727cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
728cf68787bSBenjamin Krill 	int shift = 0;
729cf68787bSBenjamin Krill 
730cf68787bSBenjamin Krill 	/*
731cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
732cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
733cf68787bSBenjamin Krill 	 * the address we're linked at.
734cf68787bSBenjamin Krill 	 */
735cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
736cf68787bSBenjamin Krill 		shift = 30;
737cf68787bSBenjamin Krill 
738cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
739cf68787bSBenjamin Krill 		shift = 20;
740cf68787bSBenjamin Krill 
741cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
742cf68787bSBenjamin Krill 		shift = 10;
743cf68787bSBenjamin Krill 
744cf68787bSBenjamin Krill 	if (shift) {
745cf68787bSBenjamin Krill 		ret <<= shift;
746cf68787bSBenjamin Krill 		(*retptr)++;
747cf68787bSBenjamin Krill 	}
748cf68787bSBenjamin Krill 
749cf68787bSBenjamin Krill 	return ret;
750cf68787bSBenjamin Krill }
751cf68787bSBenjamin Krill 
7529b6b563cSPaul Mackerras /*
7539b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
7549b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
7559b6b563cSPaul Mackerras  */
7569b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
7579b6b563cSPaul Mackerras {
758cc5d0189SBenjamin Herrenschmidt 	const char *opt;
759cf68787bSBenjamin Krill 
760cc5d0189SBenjamin Herrenschmidt 	char *p;
761cbe46bd4SChristophe Leroy 	int l = 0;
7629b6b563cSPaul Mackerras 
7635827d416SAnton Blanchard 	prom_cmd_line[0] = 0;
7645827d416SAnton Blanchard 	p = prom_cmd_line;
7655827d416SAnton Blanchard 	if ((long)prom.chosen > 0)
7665827d416SAnton Blanchard 		l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
767cbe46bd4SChristophe Leroy 	if (IS_ENABLED(CONFIG_CMDLINE_BOOL) && (l <= 0 || p[0] == '\0')) /* dbl check */
768450e7dd4SChristophe Leroy 		prom_strlcpy(prom_cmd_line, CONFIG_CMDLINE, sizeof(prom_cmd_line));
7695827d416SAnton Blanchard 	prom_printf("command line: %s\n", prom_cmd_line);
7709b6b563cSPaul Mackerras 
7719b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
772450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "iommu=");
7739b6b563cSPaul Mackerras 	if (opt) {
7749b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
7759b6b563cSPaul Mackerras 		opt += 6;
7769b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
7779b6b563cSPaul Mackerras 			opt++;
778450e7dd4SChristophe Leroy 		if (!prom_strncmp(opt, "off", 3))
7795827d416SAnton Blanchard 			prom_iommu_off = 1;
780450e7dd4SChristophe Leroy 		else if (!prom_strncmp(opt, "force", 5))
7815827d416SAnton Blanchard 			prom_iommu_force_on = 1;
7829b6b563cSPaul Mackerras 	}
7839b6b563cSPaul Mackerras #endif
784450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "mem=");
785cf68787bSBenjamin Krill 	if (opt) {
786cf68787bSBenjamin Krill 		opt += 4;
7875827d416SAnton Blanchard 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
788cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
789cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
7905827d416SAnton Blanchard 		prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
791cf68787bSBenjamin Krill #endif
792cf68787bSBenjamin Krill 	}
793014d02cbSSuraj Jitindar Singh 
794c886087cSBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
795c886087cSBenjamin Herrenschmidt 	prom_radix_disable = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
796450e7dd4SChristophe Leroy 	opt = prom_strstr(prom_cmd_line, "disable_radix");
797014d02cbSSuraj Jitindar Singh 	if (opt) {
7987a22d632SAneesh Kumar K.V 		opt += 13;
7997a22d632SAneesh Kumar K.V 		if (*opt && *opt == '=') {
8007a22d632SAneesh Kumar K.V 			bool val;
8017a22d632SAneesh Kumar K.V 
802450e7dd4SChristophe Leroy 			if (prom_strtobool(++opt, &val))
8037a22d632SAneesh Kumar K.V 				prom_radix_disable = false;
8047a22d632SAneesh Kumar K.V 			else
8057a22d632SAneesh Kumar K.V 				prom_radix_disable = val;
8067a22d632SAneesh Kumar K.V 		} else
807014d02cbSSuraj Jitindar Singh 			prom_radix_disable = true;
808014d02cbSSuraj Jitindar Singh 	}
8097a22d632SAneesh Kumar K.V 	if (prom_radix_disable)
8107a22d632SAneesh Kumar K.V 		prom_debug("Radix disabled from cmdline\n");
811c886087cSBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
8129b6b563cSPaul Mackerras }
8139b6b563cSPaul Mackerras 
81411fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
8159b6b563cSPaul Mackerras /*
816530b5e14SNathan Fontenot  * The architecture vector has an array of PVR mask/value pairs,
817530b5e14SNathan Fontenot  * followed by # option vectors - 1, followed by the option vectors.
818530b5e14SNathan Fontenot  *
819530b5e14SNathan Fontenot  * See prom.h for the definition of the bits specified in the
820530b5e14SNathan Fontenot  * architecture vector.
821f709bfacSPaul Mackerras  */
822f709bfacSPaul Mackerras 
823e8a4fd0aSMichael Ellerman /* Firmware expects the value to be n - 1, where n is the # of vectors */
824e8a4fd0aSMichael Ellerman #define NUM_VECTORS(n)		((n) - 1)
825e8a4fd0aSMichael Ellerman 
826e8a4fd0aSMichael Ellerman /*
827e8a4fd0aSMichael Ellerman  * Firmware expects 1 + n - 2, where n is the length of the option vector in
828e8a4fd0aSMichael Ellerman  * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
829e8a4fd0aSMichael Ellerman  */
830e8a4fd0aSMichael Ellerman #define VECTOR_LENGTH(n)	(1 + (n) - 2)
831e8a4fd0aSMichael Ellerman 
832d03d1d65SMichael Ellerman struct option_vector1 {
833d03d1d65SMichael Ellerman 	u8 byte1;
834d03d1d65SMichael Ellerman 	u8 arch_versions;
835cc3d2940SPaul Mackerras 	u8 arch_versions3;
836d03d1d65SMichael Ellerman } __packed;
837d03d1d65SMichael Ellerman 
838d03d1d65SMichael Ellerman struct option_vector2 {
839d03d1d65SMichael Ellerman 	u8 byte1;
840d03d1d65SMichael Ellerman 	__be16 reserved;
841d03d1d65SMichael Ellerman 	__be32 real_base;
842d03d1d65SMichael Ellerman 	__be32 real_size;
843d03d1d65SMichael Ellerman 	__be32 virt_base;
844d03d1d65SMichael Ellerman 	__be32 virt_size;
845d03d1d65SMichael Ellerman 	__be32 load_base;
846d03d1d65SMichael Ellerman 	__be32 min_rma;
847d03d1d65SMichael Ellerman 	__be32 min_load;
848d03d1d65SMichael Ellerman 	u8 min_rma_percent;
849d03d1d65SMichael Ellerman 	u8 max_pft_size;
850d03d1d65SMichael Ellerman } __packed;
851d03d1d65SMichael Ellerman 
852d03d1d65SMichael Ellerman struct option_vector3 {
853d03d1d65SMichael Ellerman 	u8 byte1;
854d03d1d65SMichael Ellerman 	u8 byte2;
855d03d1d65SMichael Ellerman } __packed;
856d03d1d65SMichael Ellerman 
857d03d1d65SMichael Ellerman struct option_vector4 {
858d03d1d65SMichael Ellerman 	u8 byte1;
859d03d1d65SMichael Ellerman 	u8 min_vp_cap;
860d03d1d65SMichael Ellerman } __packed;
861d03d1d65SMichael Ellerman 
862d03d1d65SMichael Ellerman struct option_vector5 {
863d03d1d65SMichael Ellerman 	u8 byte1;
864d03d1d65SMichael Ellerman 	u8 byte2;
865d03d1d65SMichael Ellerman 	u8 byte3;
866d03d1d65SMichael Ellerman 	u8 cmo;
867d03d1d65SMichael Ellerman 	u8 associativity;
868d03d1d65SMichael Ellerman 	u8 bin_opts;
869d03d1d65SMichael Ellerman 	u8 micro_checkpoint;
870d03d1d65SMichael Ellerman 	u8 reserved0;
871d03d1d65SMichael Ellerman 	__be32 max_cpus;
872d03d1d65SMichael Ellerman 	__be16 papr_level;
873d03d1d65SMichael Ellerman 	__be16 reserved1;
874d03d1d65SMichael Ellerman 	u8 platform_facilities;
875d03d1d65SMichael Ellerman 	u8 reserved2;
876d03d1d65SMichael Ellerman 	__be16 reserved3;
877d03d1d65SMichael Ellerman 	u8 subprocessors;
878cc3d2940SPaul Mackerras 	u8 byte22;
879cc3d2940SPaul Mackerras 	u8 intarch;
880cc3d2940SPaul Mackerras 	u8 mmu;
881014d02cbSSuraj Jitindar Singh 	u8 hash_ext;
882014d02cbSSuraj Jitindar Singh 	u8 radix_ext;
883d03d1d65SMichael Ellerman } __packed;
884d03d1d65SMichael Ellerman 
885d03d1d65SMichael Ellerman struct option_vector6 {
886d03d1d65SMichael Ellerman 	u8 reserved;
887d03d1d65SMichael Ellerman 	u8 secondary_pteg;
888d03d1d65SMichael Ellerman 	u8 os_name;
889d03d1d65SMichael Ellerman } __packed;
890d03d1d65SMichael Ellerman 
89176ffb578SMichael Ellerman struct ibm_arch_vec {
892cc3d2940SPaul Mackerras 	struct { u32 mask, val; } pvrs[12];
89376ffb578SMichael Ellerman 
89476ffb578SMichael Ellerman 	u8 num_vectors;
89576ffb578SMichael Ellerman 
89676ffb578SMichael Ellerman 	u8 vec1_len;
89776ffb578SMichael Ellerman 	struct option_vector1 vec1;
89876ffb578SMichael Ellerman 
89976ffb578SMichael Ellerman 	u8 vec2_len;
90076ffb578SMichael Ellerman 	struct option_vector2 vec2;
90176ffb578SMichael Ellerman 
90276ffb578SMichael Ellerman 	u8 vec3_len;
90376ffb578SMichael Ellerman 	struct option_vector3 vec3;
90476ffb578SMichael Ellerman 
90576ffb578SMichael Ellerman 	u8 vec4_len;
90676ffb578SMichael Ellerman 	struct option_vector4 vec4;
90776ffb578SMichael Ellerman 
90876ffb578SMichael Ellerman 	u8 vec5_len;
90976ffb578SMichael Ellerman 	struct option_vector5 vec5;
91076ffb578SMichael Ellerman 
91176ffb578SMichael Ellerman 	u8 vec6_len;
91276ffb578SMichael Ellerman 	struct option_vector6 vec6;
91376ffb578SMichael Ellerman } __packed;
91476ffb578SMichael Ellerman 
915d00e34b9SBenjamin Herrenschmidt static const struct ibm_arch_vec ibm_architecture_vec_template __initconst = {
91676ffb578SMichael Ellerman 	.pvrs = {
91776ffb578SMichael Ellerman 		{
91876ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffe0000), /* POWER5/POWER5+ */
91976ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003a0000),
92076ffb578SMichael Ellerman 		},
92176ffb578SMichael Ellerman 		{
92276ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER6 */
92376ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003e0000),
92476ffb578SMichael Ellerman 		},
92576ffb578SMichael Ellerman 		{
92676ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER7 */
92776ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x003f0000),
92876ffb578SMichael Ellerman 		},
92976ffb578SMichael Ellerman 		{
93076ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8E */
93176ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004b0000),
93276ffb578SMichael Ellerman 		},
93376ffb578SMichael Ellerman 		{
93476ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8NVL */
93576ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004c0000),
93676ffb578SMichael Ellerman 		},
93776ffb578SMichael Ellerman 		{
93876ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffff0000), /* POWER8 */
93976ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x004d0000),
94076ffb578SMichael Ellerman 		},
94176ffb578SMichael Ellerman 		{
942cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffff0000), /* POWER9 */
943cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x004e0000),
944cc3d2940SPaul Mackerras 		},
945cc3d2940SPaul Mackerras 		{
946cc3d2940SPaul Mackerras 			.mask = cpu_to_be32(0xffffffff), /* all 3.00-compliant */
947cc3d2940SPaul Mackerras 			.val  = cpu_to_be32(0x0f000005),
948cc3d2940SPaul Mackerras 		},
949cc3d2940SPaul Mackerras 		{
95076ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.07-compliant */
95176ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000004),
95276ffb578SMichael Ellerman 		},
95376ffb578SMichael Ellerman 		{
95476ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.06-compliant */
95576ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000003),
95676ffb578SMichael Ellerman 		},
95776ffb578SMichael Ellerman 		{
95876ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xffffffff), /* all 2.05-compliant */
95976ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000002),
96076ffb578SMichael Ellerman 		},
96176ffb578SMichael Ellerman 		{
96276ffb578SMichael Ellerman 			.mask = cpu_to_be32(0xfffffffe), /* all 2.04-compliant and earlier */
96376ffb578SMichael Ellerman 			.val  = cpu_to_be32(0x0f000001),
96476ffb578SMichael Ellerman 		},
96576ffb578SMichael Ellerman 	},
96676ffb578SMichael Ellerman 
96776ffb578SMichael Ellerman 	.num_vectors = NUM_VECTORS(6),
96876ffb578SMichael Ellerman 
96976ffb578SMichael Ellerman 	.vec1_len = VECTOR_LENGTH(sizeof(struct option_vector1)),
97076ffb578SMichael Ellerman 	.vec1 = {
97176ffb578SMichael Ellerman 		.byte1 = 0,
97276ffb578SMichael Ellerman 		.arch_versions = OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
97376ffb578SMichael Ellerman 				 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
974cc3d2940SPaul Mackerras 		.arch_versions3 = OV1_PPC_3_00,
97576ffb578SMichael Ellerman 	},
97676ffb578SMichael Ellerman 
97776ffb578SMichael Ellerman 	.vec2_len = VECTOR_LENGTH(sizeof(struct option_vector2)),
97876ffb578SMichael Ellerman 	/* option vector 2: Open Firmware options supported */
97976ffb578SMichael Ellerman 	.vec2 = {
98076ffb578SMichael Ellerman 		.byte1 = OV2_REAL_MODE,
98176ffb578SMichael Ellerman 		.reserved = 0,
98276ffb578SMichael Ellerman 		.real_base = cpu_to_be32(0xffffffff),
98376ffb578SMichael Ellerman 		.real_size = cpu_to_be32(0xffffffff),
98476ffb578SMichael Ellerman 		.virt_base = cpu_to_be32(0xffffffff),
98576ffb578SMichael Ellerman 		.virt_size = cpu_to_be32(0xffffffff),
98676ffb578SMichael Ellerman 		.load_base = cpu_to_be32(0xffffffff),
987687da8fcSSukadev Bhattiprolu 		.min_rma = cpu_to_be32(512),		/* 512MB min RMA */
98876ffb578SMichael Ellerman 		.min_load = cpu_to_be32(0xffffffff),	/* full client load */
98976ffb578SMichael Ellerman 		.min_rma_percent = 0,	/* min RMA percentage of total RAM */
99076ffb578SMichael Ellerman 		.max_pft_size = 48,	/* max log_2(hash table size) */
99176ffb578SMichael Ellerman 	},
99276ffb578SMichael Ellerman 
99376ffb578SMichael Ellerman 	.vec3_len = VECTOR_LENGTH(sizeof(struct option_vector3)),
99476ffb578SMichael Ellerman 	/* option vector 3: processor options supported */
99576ffb578SMichael Ellerman 	.vec3 = {
99676ffb578SMichael Ellerman 		.byte1 = 0,			/* don't ignore, don't halt */
99776ffb578SMichael Ellerman 		.byte2 = OV3_FP | OV3_VMX | OV3_DFP,
99876ffb578SMichael Ellerman 	},
99976ffb578SMichael Ellerman 
100076ffb578SMichael Ellerman 	.vec4_len = VECTOR_LENGTH(sizeof(struct option_vector4)),
100176ffb578SMichael Ellerman 	/* option vector 4: IBM PAPR implementation */
100276ffb578SMichael Ellerman 	.vec4 = {
100376ffb578SMichael Ellerman 		.byte1 = 0,			/* don't halt */
100476ffb578SMichael Ellerman 		.min_vp_cap = OV4_MIN_ENT_CAP,	/* minimum VP entitled capacity */
100576ffb578SMichael Ellerman 	},
100676ffb578SMichael Ellerman 
100776ffb578SMichael Ellerman 	.vec5_len = VECTOR_LENGTH(sizeof(struct option_vector5)),
100876ffb578SMichael Ellerman 	/* option vector 5: PAPR/OF options */
100976ffb578SMichael Ellerman 	.vec5 = {
101076ffb578SMichael Ellerman 		.byte1 = 0,				/* don't ignore, don't halt */
101176ffb578SMichael Ellerman 		.byte2 = OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
101276ffb578SMichael Ellerman 		OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
101376ffb578SMichael Ellerman #ifdef CONFIG_PCI_MSI
101476ffb578SMichael Ellerman 		/* PCIe/MSI support.  Without MSI full PCIe is not supported */
101576ffb578SMichael Ellerman 		OV5_FEAT(OV5_MSI),
101676ffb578SMichael Ellerman #else
101776ffb578SMichael Ellerman 		0,
101876ffb578SMichael Ellerman #endif
101976ffb578SMichael Ellerman 		.byte3 = 0,
102076ffb578SMichael Ellerman 		.cmo =
102176ffb578SMichael Ellerman #ifdef CONFIG_PPC_SMLPAR
102276ffb578SMichael Ellerman 		OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
102376ffb578SMichael Ellerman #else
102476ffb578SMichael Ellerman 		0,
102576ffb578SMichael Ellerman #endif
102676ffb578SMichael Ellerman 		.associativity = OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
10273dbbaf20SMichael Roth 		.bin_opts = OV5_FEAT(OV5_RESIZE_HPT) | OV5_FEAT(OV5_HP_EVT),
102876ffb578SMichael Ellerman 		.micro_checkpoint = 0,
102976ffb578SMichael Ellerman 		.reserved0 = 0,
103076ffb578SMichael Ellerman 		.max_cpus = cpu_to_be32(NR_CPUS),	/* number of cores supported */
103176ffb578SMichael Ellerman 		.papr_level = 0,
103276ffb578SMichael Ellerman 		.reserved1 = 0,
103376ffb578SMichael Ellerman 		.platform_facilities = OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) | OV5_FEAT(OV5_PFO_HW_842),
103476ffb578SMichael Ellerman 		.reserved2 = 0,
103576ffb578SMichael Ellerman 		.reserved3 = 0,
103676ffb578SMichael Ellerman 		.subprocessors = 1,
10370c38ed6fSNathan Fontenot 		.byte22 = OV5_FEAT(OV5_DRMEM_V2),
1038cc3d2940SPaul Mackerras 		.intarch = 0,
1039014d02cbSSuraj Jitindar Singh 		.mmu = 0,
1040014d02cbSSuraj Jitindar Singh 		.hash_ext = 0,
1041014d02cbSSuraj Jitindar Singh 		.radix_ext = 0,
104276ffb578SMichael Ellerman 	},
104376ffb578SMichael Ellerman 
104476ffb578SMichael Ellerman 	/* option vector 6: IBM PAPR hints */
104576ffb578SMichael Ellerman 	.vec6_len = VECTOR_LENGTH(sizeof(struct option_vector6)),
104676ffb578SMichael Ellerman 	.vec6 = {
104776ffb578SMichael Ellerman 		.reserved = 0,
104876ffb578SMichael Ellerman 		.secondary_pteg = 0,
104976ffb578SMichael Ellerman 		.os_name = OV6_LINUX,
105076ffb578SMichael Ellerman 	},
105176ffb578SMichael Ellerman };
105276ffb578SMichael Ellerman 
1053a614f52eSBenjamin Herrenschmidt static struct ibm_arch_vec __prombss ibm_architecture_vec  ____cacheline_aligned;
1054a614f52eSBenjamin Herrenschmidt 
1055493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
1056493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
105730c69ca0SBenjamin Herrenschmidt static const struct fake_elf {
10589b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
10599b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
10609b6b563cSPaul Mackerras 	struct chrpnote {
10619b6b563cSPaul Mackerras 		u32	namesz;
10629b6b563cSPaul Mackerras 		u32	descsz;
10639b6b563cSPaul Mackerras 		u32	type;
10649b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
10659b6b563cSPaul Mackerras 		struct chrpdesc {
10669b6b563cSPaul Mackerras 			u32	real_mode;
10679b6b563cSPaul Mackerras 			u32	real_base;
10689b6b563cSPaul Mackerras 			u32	real_size;
10699b6b563cSPaul Mackerras 			u32	virt_base;
10709b6b563cSPaul Mackerras 			u32	virt_size;
10719b6b563cSPaul Mackerras 			u32	load_base;
10729b6b563cSPaul Mackerras 		} chrpdesc;
10739b6b563cSPaul Mackerras 	} chrpnote;
10749b6b563cSPaul Mackerras 	struct rpanote {
10759b6b563cSPaul Mackerras 		u32	namesz;
10769b6b563cSPaul Mackerras 		u32	descsz;
10779b6b563cSPaul Mackerras 		u32	type;
10789b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
10799b6b563cSPaul Mackerras 		struct rpadesc {
10809b6b563cSPaul Mackerras 			u32	lpar_affinity;
10819b6b563cSPaul Mackerras 			u32	min_rmo_size;
10829b6b563cSPaul Mackerras 			u32	min_rmo_percent;
10839b6b563cSPaul Mackerras 			u32	max_pft_size;
10849b6b563cSPaul Mackerras 			u32	splpar;
10859b6b563cSPaul Mackerras 			u32	min_load;
10869b6b563cSPaul Mackerras 			u32	new_mem_def;
10879b6b563cSPaul Mackerras 			u32	ignore_me;
10889b6b563cSPaul Mackerras 		} rpadesc;
10899b6b563cSPaul Mackerras 	} rpanote;
1090d00e34b9SBenjamin Herrenschmidt } fake_elf __initconst = {
10919b6b563cSPaul Mackerras 	.elfhdr = {
10929b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
10939b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
10949b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
10959b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
10969b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
10979b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
10989b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
10999b6b563cSPaul Mackerras 		.e_phnum = 2
11009b6b563cSPaul Mackerras 	},
11019b6b563cSPaul Mackerras 	.phdr = {
11029b6b563cSPaul Mackerras 		[0] = {
11039b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
11049b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
11059b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
11069b6b563cSPaul Mackerras 		}, [1] = {
11079b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
11089b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
11099b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
11109b6b563cSPaul Mackerras 		}
11119b6b563cSPaul Mackerras 	},
11129b6b563cSPaul Mackerras 	.chrpnote = {
11139b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
11149b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
11159b6b563cSPaul Mackerras 		.type = 0x1275,
11169b6b563cSPaul Mackerras 		.name = "PowerPC",
11179b6b563cSPaul Mackerras 		.chrpdesc = {
11189b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
11199b6b563cSPaul Mackerras 			.real_base = ~0U,
11209b6b563cSPaul Mackerras 			.real_size = ~0U,
11219b6b563cSPaul Mackerras 			.virt_base = ~0U,
11229b6b563cSPaul Mackerras 			.virt_size = ~0U,
11239b6b563cSPaul Mackerras 			.load_base = ~0U
11249b6b563cSPaul Mackerras 		},
11259b6b563cSPaul Mackerras 	},
11269b6b563cSPaul Mackerras 	.rpanote = {
11279b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
11289b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
11299b6b563cSPaul Mackerras 		.type = 0x12759999,
11309b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
11319b6b563cSPaul Mackerras 		.rpadesc = {
11325663a123SPaul Mackerras 			.lpar_affinity = 0,
11335663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
11349b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
11355663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
11369b6b563cSPaul Mackerras 			.splpar = 1,
11379b6b563cSPaul Mackerras 			.min_load = ~0U,
11385663a123SPaul Mackerras 			.new_mem_def = 0
11399b6b563cSPaul Mackerras 		}
11409b6b563cSPaul Mackerras 	}
11419b6b563cSPaul Mackerras };
1142493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
11439b6b563cSPaul Mackerras 
1144efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
1145efec959fSBenjamin Herrenschmidt {
1146efec959fSBenjamin Herrenschmidt 	phandle node;
1147efec959fSBenjamin Herrenschmidt 	char type[64];
1148efec959fSBenjamin Herrenschmidt 	unsigned int plen;
1149efec959fSBenjamin Herrenschmidt 
1150efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
1151efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
1152efec959fSBenjamin Herrenschmidt 		type[0] = 0;
1153efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
1154efec959fSBenjamin Herrenschmidt 
1155450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "cpu"))
1156efec959fSBenjamin Herrenschmidt 			continue;
1157efec959fSBenjamin Herrenschmidt 		/*
1158efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
1159efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
1160efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
1161efec959fSBenjamin Herrenschmidt 		 */
1162efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
1163efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
1164efec959fSBenjamin Herrenschmidt 			break;
1165efec959fSBenjamin Herrenschmidt 		plen >>= 2;
11662c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
1167efec959fSBenjamin Herrenschmidt 
1168efec959fSBenjamin Herrenschmidt 		/* Sanity check */
1169efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
11702c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
1171efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
1172efec959fSBenjamin Herrenschmidt 			return 1;
1173efec959fSBenjamin Herrenschmidt 		}
1174efec959fSBenjamin Herrenschmidt 		return plen;
1175efec959fSBenjamin Herrenschmidt 	}
1176efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
1177efec959fSBenjamin Herrenschmidt 
1178efec959fSBenjamin Herrenschmidt 	return 1;
1179efec959fSBenjamin Herrenschmidt 
1180efec959fSBenjamin Herrenschmidt }
1181efec959fSBenjamin Herrenschmidt 
1182014d02cbSSuraj Jitindar Singh static void __init prom_parse_mmu_model(u8 val,
1183014d02cbSSuraj Jitindar Singh 					struct platform_support *support)
1184014d02cbSSuraj Jitindar Singh {
1185014d02cbSSuraj Jitindar Singh 	switch (val) {
1186014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_DYNAMIC):
1187014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_EITHER): /* Either Available */
1188014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - either supported\n");
1189014d02cbSSuraj Jitindar Singh 		support->radix_mmu = !prom_radix_disable;
1190014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1191014d02cbSSuraj Jitindar Singh 		break;
1192014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */
1193014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - radix only\n");
1194014d02cbSSuraj Jitindar Singh 		if (prom_radix_disable) {
1195014d02cbSSuraj Jitindar Singh 			/*
1196014d02cbSSuraj Jitindar Singh 			 * If we __have__ to do radix, we're better off ignoring
1197014d02cbSSuraj Jitindar Singh 			 * the command line rather than not booting.
1198014d02cbSSuraj Jitindar Singh 			 */
1199014d02cbSSuraj Jitindar Singh 			prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
1200014d02cbSSuraj Jitindar Singh 		}
1201014d02cbSSuraj Jitindar Singh 		support->radix_mmu = true;
1202014d02cbSSuraj Jitindar Singh 		break;
1203014d02cbSSuraj Jitindar Singh 	case OV5_FEAT(OV5_MMU_HASH):
1204014d02cbSSuraj Jitindar Singh 		prom_debug("MMU - hash only\n");
1205014d02cbSSuraj Jitindar Singh 		support->hash_mmu = true;
1206014d02cbSSuraj Jitindar Singh 		break;
1207014d02cbSSuraj Jitindar Singh 	default:
1208014d02cbSSuraj Jitindar Singh 		prom_debug("Unknown mmu support option: 0x%x\n", val);
1209014d02cbSSuraj Jitindar Singh 		break;
1210014d02cbSSuraj Jitindar Singh 	}
1211014d02cbSSuraj Jitindar Singh }
1212014d02cbSSuraj Jitindar Singh 
1213ac5e5a54SCédric Le Goater static void __init prom_parse_xive_model(u8 val,
1214ac5e5a54SCédric Le Goater 					 struct platform_support *support)
1215ac5e5a54SCédric Le Goater {
1216ac5e5a54SCédric Le Goater 	switch (val) {
1217ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EITHER): /* Either Available */
1218ac5e5a54SCédric Le Goater 		prom_debug("XIVE - either mode supported\n");
1219ac5e5a54SCédric Le Goater 		support->xive = true;
1220ac5e5a54SCédric Le Goater 		break;
1221ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_EXPLOIT): /* Only Exploitation mode */
1222ac5e5a54SCédric Le Goater 		prom_debug("XIVE - exploitation mode supported\n");
1223ac5e5a54SCédric Le Goater 		support->xive = true;
1224ac5e5a54SCédric Le Goater 		break;
1225ac5e5a54SCédric Le Goater 	case OV5_FEAT(OV5_XIVE_LEGACY): /* Only Legacy mode */
1226ac5e5a54SCédric Le Goater 		prom_debug("XIVE - legacy mode supported\n");
1227ac5e5a54SCédric Le Goater 		break;
1228ac5e5a54SCédric Le Goater 	default:
1229ac5e5a54SCédric Le Goater 		prom_debug("Unknown xive support option: 0x%x\n", val);
1230ac5e5a54SCédric Le Goater 		break;
1231ac5e5a54SCédric Le Goater 	}
1232ac5e5a54SCédric Le Goater }
1233ac5e5a54SCédric Le Goater 
1234014d02cbSSuraj Jitindar Singh static void __init prom_parse_platform_support(u8 index, u8 val,
1235014d02cbSSuraj Jitindar Singh 					       struct platform_support *support)
1236014d02cbSSuraj Jitindar Singh {
1237014d02cbSSuraj Jitindar Singh 	switch (index) {
1238014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_MMU_SUPPORT): /* MMU Model */
1239014d02cbSSuraj Jitindar Singh 		prom_parse_mmu_model(val & OV5_FEAT(OV5_MMU_SUPPORT), support);
1240014d02cbSSuraj Jitindar Singh 		break;
1241014d02cbSSuraj Jitindar Singh 	case OV5_INDX(OV5_RADIX_GTSE): /* Radix Extensions */
1242014d02cbSSuraj Jitindar Singh 		if (val & OV5_FEAT(OV5_RADIX_GTSE)) {
1243014d02cbSSuraj Jitindar Singh 			prom_debug("Radix - GTSE supported\n");
1244014d02cbSSuraj Jitindar Singh 			support->radix_gtse = true;
1245014d02cbSSuraj Jitindar Singh 		}
1246014d02cbSSuraj Jitindar Singh 		break;
1247ac5e5a54SCédric Le Goater 	case OV5_INDX(OV5_XIVE_SUPPORT): /* Interrupt mode */
1248ac5e5a54SCédric Le Goater 		prom_parse_xive_model(val & OV5_FEAT(OV5_XIVE_SUPPORT),
1249ac5e5a54SCédric Le Goater 				      support);
1250ac5e5a54SCédric Le Goater 		break;
1251014d02cbSSuraj Jitindar Singh 	}
1252014d02cbSSuraj Jitindar Singh }
1253014d02cbSSuraj Jitindar Singh 
1254014d02cbSSuraj Jitindar Singh static void __init prom_check_platform_support(void)
1255014d02cbSSuraj Jitindar Singh {
1256014d02cbSSuraj Jitindar Singh 	struct platform_support supported = {
1257014d02cbSSuraj Jitindar Singh 		.hash_mmu = false,
1258014d02cbSSuraj Jitindar Singh 		.radix_mmu = false,
1259ac5e5a54SCédric Le Goater 		.radix_gtse = false,
1260ac5e5a54SCédric Le Goater 		.xive = false
1261014d02cbSSuraj Jitindar Singh 	};
1262014d02cbSSuraj Jitindar Singh 	int prop_len = prom_getproplen(prom.chosen,
1263014d02cbSSuraj Jitindar Singh 				       "ibm,arch-vec-5-platform-support");
1264a614f52eSBenjamin Herrenschmidt 
1265adcf5918SChristophe Leroy 	/*
1266adcf5918SChristophe Leroy 	 * First copy the architecture vec template
1267adcf5918SChristophe Leroy 	 *
1268adcf5918SChristophe Leroy 	 * use memcpy() instead of *vec = *vec_template so that GCC replaces it
1269adcf5918SChristophe Leroy 	 * by __memcpy() when KASAN is active
1270adcf5918SChristophe Leroy 	 */
1271adcf5918SChristophe Leroy 	memcpy(&ibm_architecture_vec, &ibm_architecture_vec_template,
1272adcf5918SChristophe Leroy 	       sizeof(ibm_architecture_vec));
1273a614f52eSBenjamin Herrenschmidt 
1274014d02cbSSuraj Jitindar Singh 	if (prop_len > 1) {
1275014d02cbSSuraj Jitindar Singh 		int i;
1276ab912399SSuraj Jitindar Singh 		u8 vec[8];
1277014d02cbSSuraj Jitindar Singh 		prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1278014d02cbSSuraj Jitindar Singh 			   prop_len);
1279ab912399SSuraj Jitindar Singh 		if (prop_len > sizeof(vec))
1280ab912399SSuraj Jitindar Singh 			prom_printf("WARNING: ibm,arch-vec-5-platform-support longer than expected (len: %d)\n",
1281ab912399SSuraj Jitindar Singh 				    prop_len);
1282014d02cbSSuraj Jitindar Singh 		prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support",
1283014d02cbSSuraj Jitindar Singh 			     &vec, sizeof(vec));
1284ab912399SSuraj Jitindar Singh 		for (i = 0; i < sizeof(vec); i += 2) {
1285014d02cbSSuraj Jitindar Singh 			prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2
1286014d02cbSSuraj Jitindar Singh 								  , vec[i]
1287014d02cbSSuraj Jitindar Singh 								  , vec[i + 1]);
1288014d02cbSSuraj Jitindar Singh 			prom_parse_platform_support(vec[i], vec[i + 1],
1289014d02cbSSuraj Jitindar Singh 						    &supported);
1290014d02cbSSuraj Jitindar Singh 		}
1291014d02cbSSuraj Jitindar Singh 	}
1292014d02cbSSuraj Jitindar Singh 
129379b46868SAlexey Kardashevskiy 	if (supported.radix_mmu && supported.radix_gtse &&
129479b46868SAlexey Kardashevskiy 	    IS_ENABLED(CONFIG_PPC_RADIX_MMU)) {
1295014d02cbSSuraj Jitindar Singh 		/* Radix preferred - but we require GTSE for now */
1296014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for radix with GTSE\n");
1297014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1298014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.radix_ext = OV5_FEAT(OV5_RADIX_GTSE);
1299014d02cbSSuraj Jitindar Singh 	} else if (supported.hash_mmu) {
1300014d02cbSSuraj Jitindar Singh 		/* Default to hash mmu (if we can) */
1301014d02cbSSuraj Jitindar Singh 		prom_debug("Asking for hash\n");
1302014d02cbSSuraj Jitindar Singh 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1303014d02cbSSuraj Jitindar Singh 	} else {
1304014d02cbSSuraj Jitindar Singh 		/* We're probably on a legacy hypervisor */
1305014d02cbSSuraj Jitindar Singh 		prom_debug("Assuming legacy hash support\n");
1306014d02cbSSuraj Jitindar Singh 	}
1307ac5e5a54SCédric Le Goater 
1308ac5e5a54SCédric Le Goater 	if (supported.xive) {
1309ac5e5a54SCédric Le Goater 		prom_debug("Asking for XIVE\n");
1310ac5e5a54SCédric Le Goater 		ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1311ac5e5a54SCédric Le Goater 	}
1312014d02cbSSuraj Jitindar Singh }
1313efec959fSBenjamin Herrenschmidt 
13149b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
13159b6b563cSPaul Mackerras {
1316493adffcSBenjamin Herrenschmidt 	ihandle root;
1317f709bfacSPaul Mackerras 	prom_arg_t ret;
1318dbd0c5d5SLaurent Dufour 	u32 cores;
13199b6b563cSPaul Mackerras 
1320014d02cbSSuraj Jitindar Singh 	/* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1321014d02cbSSuraj Jitindar Singh 	prom_check_platform_support();
1322014d02cbSSuraj Jitindar Singh 
1323f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
1324f709bfacSPaul Mackerras 	if (root != 0) {
1325efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
1326efec959fSBenjamin Herrenschmidt 		 *
1327efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
1328efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
1329efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
1330efec959fSBenjamin Herrenschmidt 		 */
1331dbd0c5d5SLaurent Dufour 
1332dbd0c5d5SLaurent Dufour 		cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1333eae5f709SMathieu Malaterre 		prom_printf("Max number of cores passed to firmware: %u (NR_CPUS = %d)\n",
1334dbd0c5d5SLaurent Dufour 			    cores, NR_CPUS);
133576ffb578SMichael Ellerman 
133676ffb578SMichael Ellerman 		ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1337efec959fSBenjamin Herrenschmidt 
1338f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
1339049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
1340f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
1341f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
134233b74977SBenjamin Herrenschmidt 				  root,
134376ffb578SMichael Ellerman 				  ADDR(&ibm_architecture_vec)) == 0) {
1344f709bfacSPaul Mackerras 			/* the call exists... */
1345f709bfacSPaul Mackerras 			if (ret)
13464da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
1347f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
1348f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
13494da727aeSAnton Blanchard 			prom_printf(" done\n");
1350f709bfacSPaul Mackerras 			return;
1351f709bfacSPaul Mackerras 		}
1352f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
1353049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
1354f709bfacSPaul Mackerras 	}
1355f709bfacSPaul Mackerras 
1356493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
1357493adffcSBenjamin Herrenschmidt 	{
1358493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
1359493adffcSBenjamin Herrenschmidt 
1360f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
1361493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
1362493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
13639b6b563cSPaul Mackerras 		if (elfloader == 0) {
13649b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
13659b6b563cSPaul Mackerras 			return;
13669b6b563cSPaul Mackerras 		}
13679b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
13689b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
13699b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
13709b6b563cSPaul Mackerras 	}
1371493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1372493adffcSBenjamin Herrenschmidt }
137311fdb309SBenjamin Herrenschmidt #endif /* CONFIG_PPC_PSERIES */
13749b6b563cSPaul Mackerras 
13759b6b563cSPaul Mackerras /*
13769b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
13779b6b563cSPaul Mackerras  *
13789b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
13799b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
13809b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
13819b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
13829b6b563cSPaul Mackerras  *  never happen.
13839b6b563cSPaul Mackerras  *
13849b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
13859b6b563cSPaul Mackerras  *  TCEs overlap
13869b6b563cSPaul Mackerras  *
13879b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
13889b6b563cSPaul Mackerras  *
13899b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
13909b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
13919b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
13929b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
13939b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
13949b6b563cSPaul Mackerras  *
13959b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
13969b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
13979b6b563cSPaul Mackerras  *  reserved.
13989b6b563cSPaul Mackerras  */
13999b6b563cSPaul Mackerras 
14009b6b563cSPaul Mackerras 
14019b6b563cSPaul Mackerras /*
14029b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
14039b6b563cSPaul Mackerras  *
14049b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
14059b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
14069b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
14079b6b563cSPaul Mackerras  */
14089b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
14099b6b563cSPaul Mackerras {
14105827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
14119b6b563cSPaul Mackerras 	unsigned long addr = 0;
14129b6b563cSPaul Mackerras 
1413c4988820SPaul Mackerras 	if (align)
1414c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
1415eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx)\n", __func__, size, align);
14165827d416SAnton Blanchard 	if (ram_top == 0)
14179b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
14189b6b563cSPaul Mackerras 
14199b6b563cSPaul Mackerras 	if (align)
14205827d416SAnton Blanchard 		base = _ALIGN_UP(alloc_bottom, align);
14219b6b563cSPaul Mackerras 	else
14225827d416SAnton Blanchard 		base = alloc_bottom;
14239b6b563cSPaul Mackerras 
14245827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
14259b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
1426eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
14279b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1428c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
14299b6b563cSPaul Mackerras 			break;
14309b6b563cSPaul Mackerras 		addr = 0;
14319b6b563cSPaul Mackerras 		if (align == 0)
14329b6b563cSPaul Mackerras 			break;
14339b6b563cSPaul Mackerras 	}
14349b6b563cSPaul Mackerras 	if (addr == 0)
14359b6b563cSPaul Mackerras 		return 0;
14365827d416SAnton Blanchard 	alloc_bottom = addr + size;
14379b6b563cSPaul Mackerras 
1438eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1439eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1440eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1441eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1442eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1443eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
14449b6b563cSPaul Mackerras 
14459b6b563cSPaul Mackerras 	return addr;
14469b6b563cSPaul Mackerras }
14479b6b563cSPaul Mackerras 
14489b6b563cSPaul Mackerras /*
14499b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
14509b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
14519b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
14529b6b563cSPaul Mackerras  */
14539b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
14549b6b563cSPaul Mackerras 				       int highmem)
14559b6b563cSPaul Mackerras {
14569b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
14579b6b563cSPaul Mackerras 
1458eae5f709SMathieu Malaterre 	prom_debug("%s(%lx, %lx, %s)\n", __func__, size, align,
14595827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
14605827d416SAnton Blanchard 	if (ram_top == 0)
14619b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
14629b6b563cSPaul Mackerras 
14639b6b563cSPaul Mackerras 	if (highmem) {
14649b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
14655827d416SAnton Blanchard 		addr = _ALIGN_DOWN(alloc_top_high - size, align);
14665827d416SAnton Blanchard 		if (addr <= alloc_bottom)
14679b6b563cSPaul Mackerras 			return 0;
14689b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
14699b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
14709b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
14719b6b563cSPaul Mackerras 		 */
14725827d416SAnton Blanchard 		if (addr < rmo_top) {
14739b6b563cSPaul Mackerras 			/* Good, we are first */
14745827d416SAnton Blanchard 			if (alloc_top == rmo_top)
14755827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
14769b6b563cSPaul Mackerras 			else
14779b6b563cSPaul Mackerras 				return 0;
14789b6b563cSPaul Mackerras 		}
14795827d416SAnton Blanchard 		alloc_top_high = addr;
14809b6b563cSPaul Mackerras 		goto bail;
14819b6b563cSPaul Mackerras 	}
14829b6b563cSPaul Mackerras 
14835827d416SAnton Blanchard 	base = _ALIGN_DOWN(alloc_top - size, align);
14845827d416SAnton Blanchard 	for (; base > alloc_bottom;
14859b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
1486eae5f709SMathieu Malaterre 		prom_debug("    trying: 0x%lx\n\r", base);
14879b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1488c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
14899b6b563cSPaul Mackerras 			break;
14909b6b563cSPaul Mackerras 		addr = 0;
14919b6b563cSPaul Mackerras 	}
14929b6b563cSPaul Mackerras 	if (addr == 0)
14939b6b563cSPaul Mackerras 		return 0;
14945827d416SAnton Blanchard 	alloc_top = addr;
14959b6b563cSPaul Mackerras 
14969b6b563cSPaul Mackerras  bail:
1497eae5f709SMathieu Malaterre 	prom_debug(" -> %lx\n", addr);
1498eae5f709SMathieu Malaterre 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1499eae5f709SMathieu Malaterre 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1500eae5f709SMathieu Malaterre 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1501eae5f709SMathieu Malaterre 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1502eae5f709SMathieu Malaterre 	prom_debug("  ram_top      : %lx\n", ram_top);
15039b6b563cSPaul Mackerras 
15049b6b563cSPaul Mackerras 	return addr;
15059b6b563cSPaul Mackerras }
15069b6b563cSPaul Mackerras 
15079b6b563cSPaul Mackerras /*
15089b6b563cSPaul Mackerras  * Parse a "reg" cell
15099b6b563cSPaul Mackerras  */
15109b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
15119b6b563cSPaul Mackerras {
15129b6b563cSPaul Mackerras 	cell_t *p = *cellp;
15139b6b563cSPaul Mackerras 	unsigned long r = 0;
15149b6b563cSPaul Mackerras 
15159b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
15169b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
15179b6b563cSPaul Mackerras 		p++;
15189b6b563cSPaul Mackerras 		s--;
15199b6b563cSPaul Mackerras 	}
1520493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
15219b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
152235499c01SPaul Mackerras 	if (s > 1) {
15239b6b563cSPaul Mackerras 		r <<= 32;
1524493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
15259b6b563cSPaul Mackerras 	}
15269b6b563cSPaul Mackerras #endif
15279b6b563cSPaul Mackerras 	*cellp = p;
15289b6b563cSPaul Mackerras 	return r;
15299b6b563cSPaul Mackerras }
15309b6b563cSPaul Mackerras 
15319b6b563cSPaul Mackerras /*
15329b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
15339b6b563cSPaul Mackerras  * we don't need anything smarter at this point
15349b6b563cSPaul Mackerras  *
15359b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
15369b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
15379b6b563cSPaul Mackerras  * them down.
15389b6b563cSPaul Mackerras  */
15390108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
15409b6b563cSPaul Mackerras {
1541cbbcf340SKumar Gala 	u64 top = base + size;
15425827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
15439b6b563cSPaul Mackerras 
15449b6b563cSPaul Mackerras 	if (size == 0)
15459b6b563cSPaul Mackerras 		return;
15469b6b563cSPaul Mackerras 
15479b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
15489b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
15499b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
15509b6b563cSPaul Mackerras 	 */
15519b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
15529b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
15539b6b563cSPaul Mackerras 	size = top - base;
15549b6b563cSPaul Mackerras 
15559b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
15569b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1557493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1558493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
15595827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
15609b6b563cSPaul Mackerras }
15619b6b563cSPaul Mackerras 
15629b6b563cSPaul Mackerras /*
1563b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
15649b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
15659b6b563cSPaul Mackerras  */
15669b6b563cSPaul Mackerras static void __init prom_init_mem(void)
15679b6b563cSPaul Mackerras {
15689b6b563cSPaul Mackerras 	phandle node;
1569eab00a20SMathieu Malaterre #ifdef DEBUG_PROM
1570eab00a20SMathieu Malaterre 	char *path;
1571eab00a20SMathieu Malaterre #endif
1572eab00a20SMathieu Malaterre 	char type[64];
15739b6b563cSPaul Mackerras 	unsigned int plen;
15749b6b563cSPaul Mackerras 	cell_t *p, *endp;
1575493adffcSBenjamin Herrenschmidt 	__be32 val;
15769b6b563cSPaul Mackerras 	u32 rac, rsc;
15779b6b563cSPaul Mackerras 
15789b6b563cSPaul Mackerras 	/*
15799b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
15809b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
15819b6b563cSPaul Mackerras 	 * 2) top of memory
15829b6b563cSPaul Mackerras 	 */
1583493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1584493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1585493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1586493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1587493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1588493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1589493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1590493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
15919b6b563cSPaul Mackerras 
15929b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
1593eab00a20SMathieu Malaterre #ifdef DEBUG_PROM
15945827d416SAnton Blanchard 	path = prom_scratch;
1595eab00a20SMathieu Malaterre #endif
15969b6b563cSPaul Mackerras 
15979b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
15989b6b563cSPaul Mackerras 		type[0] = 0;
15999b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
16009b6b563cSPaul Mackerras 
1601c4988820SPaul Mackerras 		if (type[0] == 0) {
1602c4988820SPaul Mackerras 			/*
1603c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1604c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1605c4988820SPaul Mackerras 			 */
1606c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1607c4988820SPaul Mackerras 		}
1608450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "memory"))
16099b6b563cSPaul Mackerras 			continue;
16109b6b563cSPaul Mackerras 
16115827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
16129b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
16139b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
16149b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
16159b6b563cSPaul Mackerras 		}
16165827d416SAnton Blanchard 		p = regbuf;
16179b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
16189b6b563cSPaul Mackerras 
16199b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1620d7fbe2a0SChristophe Leroy 		memset(path, 0, sizeof(prom_scratch));
1621d7fbe2a0SChristophe Leroy 		call_prom("package-to-path", 3, 1, node, path, sizeof(prom_scratch) - 1);
16229b6b563cSPaul Mackerras 		prom_debug("  node %s :\n", path);
16239b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
16249b6b563cSPaul Mackerras 
16259b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
16269b6b563cSPaul Mackerras 			unsigned long base, size;
16279b6b563cSPaul Mackerras 
16289b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
16299b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
16309b6b563cSPaul Mackerras 
16319b6b563cSPaul Mackerras 			if (size == 0)
16329b6b563cSPaul Mackerras 				continue;
1633eae5f709SMathieu Malaterre 			prom_debug("    %lx %lx\n", base, size);
16345827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
16355827d416SAnton Blanchard 				rmo_top = size;
16365827d416SAnton Blanchard 			if ((base + size) > ram_top)
16375827d416SAnton Blanchard 				ram_top = base + size;
16389b6b563cSPaul Mackerras 		}
16399b6b563cSPaul Mackerras 	}
16409b6b563cSPaul Mackerras 
16415827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
16429b6b563cSPaul Mackerras 
16439b6b563cSPaul Mackerras 	/*
1644cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1645cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1646cf68787bSBenjamin Krill 	 * TCE's up there.
1647cf68787bSBenjamin Krill 	 */
1648cf68787bSBenjamin Krill 
16495827d416SAnton Blanchard 	alloc_top_high = ram_top;
1650cf68787bSBenjamin Krill 
16515827d416SAnton Blanchard 	if (prom_memory_limit) {
16525827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1653eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx <= alloc_bottom.\n",
16545827d416SAnton Blanchard 				    prom_memory_limit);
16555827d416SAnton Blanchard 			prom_memory_limit = 0;
16565827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1657eae5f709SMathieu Malaterre 			prom_printf("Ignoring mem=%lx >= ram_top.\n",
16585827d416SAnton Blanchard 				    prom_memory_limit);
16595827d416SAnton Blanchard 			prom_memory_limit = 0;
1660cf68787bSBenjamin Krill 		} else {
16615827d416SAnton Blanchard 			ram_top = prom_memory_limit;
16625827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1663cf68787bSBenjamin Krill 		}
1664cf68787bSBenjamin Krill 	}
1665cf68787bSBenjamin Krill 
1666cf68787bSBenjamin Krill 	/*
16679b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
16689b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
16699b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
16709b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
16719b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
16729b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
16739b6b563cSPaul Mackerras 	 */
16745827d416SAnton Blanchard 	if (!rmo_top)
16755827d416SAnton Blanchard 		rmo_top = ram_top;
16765827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
16775827d416SAnton Blanchard 	alloc_top = rmo_top;
16785827d416SAnton Blanchard 	alloc_top_high = ram_top;
16799b6b563cSPaul Mackerras 
168064968f60SPaul Mackerras 	/*
168164968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
168264968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
168364968f60SPaul Mackerras 	 */
16845827d416SAnton Blanchard 	if (prom_initrd_start &&
16855827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
16865827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
16875827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
168864968f60SPaul Mackerras 
16899b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
1690eae5f709SMathieu Malaterre 	prom_printf("  memory_limit : %lx (16 MB aligned)\n",
1691eae5f709SMathieu Malaterre 		    prom_memory_limit);
1692eae5f709SMathieu Malaterre 	prom_printf("  alloc_bottom : %lx\n", alloc_bottom);
1693eae5f709SMathieu Malaterre 	prom_printf("  alloc_top    : %lx\n", alloc_top);
1694eae5f709SMathieu Malaterre 	prom_printf("  alloc_top_hi : %lx\n", alloc_top_high);
1695eae5f709SMathieu Malaterre 	prom_printf("  rmo_top      : %lx\n", rmo_top);
1696eae5f709SMathieu Malaterre 	prom_printf("  ram_top      : %lx\n", ram_top);
16979b6b563cSPaul Mackerras }
16989b6b563cSPaul Mackerras 
169927f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
170027f44888SBenjamin Herrenschmidt {
1701493adffcSBenjamin Herrenschmidt 	__be32 val;
1702493adffcSBenjamin Herrenschmidt 	ihandle stdin;
170327f44888SBenjamin Herrenschmidt 
1704493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1705493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1706493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1707493adffcSBenjamin Herrenschmidt 	}
170827f44888SBenjamin Herrenschmidt }
170927f44888SBenjamin Herrenschmidt 
17109b6b563cSPaul Mackerras /*
17119b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
17129b6b563cSPaul Mackerras  */
17139b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
17149b6b563cSPaul Mackerras {
17159b6b563cSPaul Mackerras 	phandle rtas_node;
17169b6b563cSPaul Mackerras 	ihandle rtas_inst;
17179b6b563cSPaul Mackerras 	u32 base, entry = 0;
1718493adffcSBenjamin Herrenschmidt 	__be32 val;
17199b6b563cSPaul Mackerras 	u32 size = 0;
17209b6b563cSPaul Mackerras 
17219b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
17229b6b563cSPaul Mackerras 
17239b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
17249b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
17259b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
17269b6b563cSPaul Mackerras 		return;
17279b6b563cSPaul Mackerras 
1728493adffcSBenjamin Herrenschmidt 	val = 0;
1729493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1730493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
17319b6b563cSPaul Mackerras 	if (size == 0)
17329b6b563cSPaul Mackerras 		return;
17339b6b563cSPaul Mackerras 
17349b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
17356d1e2c6cSAnton Blanchard 	if (base == 0)
17366d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
17379b6b563cSPaul Mackerras 
17389b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
17399b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1740a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
17419b6b563cSPaul Mackerras 		return;
17429b6b563cSPaul Mackerras 	}
17439b6b563cSPaul Mackerras 
17449b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
17459b6b563cSPaul Mackerras 
17469b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
17479b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1748a23414beSPaul Mackerras 			  rtas_inst, base) != 0
17499b6b563cSPaul Mackerras 	    || entry == 0) {
17509b6b563cSPaul Mackerras 		prom_printf(" failed\n");
17519b6b563cSPaul Mackerras 		return;
17529b6b563cSPaul Mackerras 	}
17539b6b563cSPaul Mackerras 	prom_printf(" done\n");
17549b6b563cSPaul Mackerras 
17559b6b563cSPaul Mackerras 	reserve_mem(base, size);
17569b6b563cSPaul Mackerras 
1757493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1758a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1759493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1760493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1761a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1762493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
17639b6b563cSPaul Mackerras 
1764dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1765dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1766dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1767dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1768dbe78b40SBenjamin Herrenschmidt 
17699b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
17709b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
1771eae5f709SMathieu Malaterre 	prom_debug("rtas size     = 0x%x\n", size);
17729b6b563cSPaul Mackerras 
17739b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
17749b6b563cSPaul Mackerras }
17759b6b563cSPaul Mackerras 
17769b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
17779b6b563cSPaul Mackerras /*
17784a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
17794a727429SAshley Lai  */
17804a727429SAshley Lai static void __init prom_instantiate_sml(void)
17814a727429SAshley Lai {
17824a727429SAshley Lai 	phandle ibmvtpm_node;
17834a727429SAshley Lai 	ihandle ibmvtpm_inst;
1784b4ed0469SHon Ching \(Vicky\) Lo 	u32 entry = 0, size = 0, succ = 0;
17854a727429SAshley Lai 	u64 base;
1786b4ed0469SHon Ching \(Vicky\) Lo 	__be32 val;
17874a727429SAshley Lai 
17884a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
17894a727429SAshley Lai 
17902f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
17914a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
17924a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
17934a727429SAshley Lai 		return;
17944a727429SAshley Lai 
17952f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
17964a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
17974a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
17984a727429SAshley Lai 		return;
17994a727429SAshley Lai 	}
18004a727429SAshley Lai 
1801b4ed0469SHon Ching \(Vicky\) Lo 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1802b4ed0469SHon Ching \(Vicky\) Lo 			 &val, sizeof(val)) != PROM_ERROR) {
1803b4ed0469SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &succ,
1804b4ed0469SHon Ching \(Vicky\) Lo 				  ADDR("reformat-sml-to-efi-alignment"),
1805b4ed0469SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || succ == 0) {
1806b4ed0469SHon Ching \(Vicky\) Lo 			prom_printf("Reformat SML to EFI alignment failed\n");
1807b4ed0469SHon Ching \(Vicky\) Lo 			return;
1808b4ed0469SHon Ching \(Vicky\) Lo 		}
1809b4ed0469SHon Ching \(Vicky\) Lo 
18104a727429SAshley Lai 		if (call_prom_ret("call-method", 2, 2, &size,
18119e5d4af4SHon Ching \(Vicky\) Lo 				  ADDR("sml-get-allocated-size"),
18129e5d4af4SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || size == 0) {
18139e5d4af4SHon Ching \(Vicky\) Lo 			prom_printf("SML get allocated size failed\n");
18149e5d4af4SHon Ching \(Vicky\) Lo 			return;
18159e5d4af4SHon Ching \(Vicky\) Lo 		}
18169e5d4af4SHon Ching \(Vicky\) Lo 	} else {
18179e5d4af4SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &size,
18184a727429SAshley Lai 				  ADDR("sml-get-handover-size"),
18194a727429SAshley Lai 				  ibmvtpm_inst) != 0 || size == 0) {
18204a727429SAshley Lai 			prom_printf("SML get handover size failed\n");
18214a727429SAshley Lai 			return;
18224a727429SAshley Lai 		}
18239e5d4af4SHon Ching \(Vicky\) Lo 	}
18244a727429SAshley Lai 
18254a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
18264a727429SAshley Lai 	if (base == 0)
18274a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
18284a727429SAshley Lai 
1829eae5f709SMathieu Malaterre 	prom_printf("instantiating sml at 0x%llx...", base);
18304a727429SAshley Lai 
18319e5d4af4SHon Ching \(Vicky\) Lo 	memset((void *)base, 0, size);
18329e5d4af4SHon Ching \(Vicky\) Lo 
18334a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
18344a727429SAshley Lai 			  ADDR("sml-handover"),
18354a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
18364a727429SAshley Lai 		prom_printf("SML handover failed\n");
18374a727429SAshley Lai 		return;
18384a727429SAshley Lai 	}
18394a727429SAshley Lai 	prom_printf(" done\n");
18404a727429SAshley Lai 
18414a727429SAshley Lai 	reserve_mem(base, size);
18424a727429SAshley Lai 
18432f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
18444a727429SAshley Lai 		     &base, sizeof(base));
18452f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
18464a727429SAshley Lai 		     &size, sizeof(size));
18474a727429SAshley Lai 
1848eae5f709SMathieu Malaterre 	prom_debug("sml base     = 0x%llx\n", base);
1849eae5f709SMathieu Malaterre 	prom_debug("sml size     = 0x%x\n", size);
18504a727429SAshley Lai 
18514a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
18524a727429SAshley Lai }
18534a727429SAshley Lai 
18544a727429SAshley Lai /*
18559b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
18569b6b563cSPaul Mackerras  */
1857493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
18589b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
18599b6b563cSPaul Mackerras {
18609b6b563cSPaul Mackerras 	phandle node;
18619b6b563cSPaul Mackerras 	ihandle phb_node;
18629b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
18635827d416SAnton Blanchard 	char *path = prom_scratch;
18649b6b563cSPaul Mackerras 	u64 base, align;
18659b6b563cSPaul Mackerras 	u32 minalign, minsize;
18669b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
18679b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
18689b6b563cSPaul Mackerras 	u64 i;
18699b6b563cSPaul Mackerras 
18705827d416SAnton Blanchard 	if (prom_iommu_off)
18719b6b563cSPaul Mackerras 		return;
18729b6b563cSPaul Mackerras 
18739b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
18749b6b563cSPaul Mackerras 
18759b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
18765827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
18779b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
18789b6b563cSPaul Mackerras 
18799b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
18809b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
18819b6b563cSPaul Mackerras 		compatible[0] = 0;
18829b6b563cSPaul Mackerras 		type[0] = 0;
18839b6b563cSPaul Mackerras 		model[0] = 0;
18849b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
18859b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
18869b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
18879b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
18889b6b563cSPaul Mackerras 
1889450e7dd4SChristophe Leroy 		if ((type[0] == 0) || (prom_strstr(type, "pci") == NULL))
18909b6b563cSPaul Mackerras 			continue;
18919b6b563cSPaul Mackerras 
1892e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
18939b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
1894450e7dd4SChristophe Leroy 			if ((prom_strstr(compatible, "python") == NULL) &&
1895450e7dd4SChristophe Leroy 			    (prom_strstr(compatible, "Speedwagon") == NULL) &&
1896450e7dd4SChristophe Leroy 			    (prom_strstr(compatible, "Winnipeg") == NULL))
18979b6b563cSPaul Mackerras 				continue;
18989b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
1899450e7dd4SChristophe Leroy 			if ((prom_strstr(model, "ython") == NULL) &&
1900450e7dd4SChristophe Leroy 			    (prom_strstr(model, "peedwagon") == NULL) &&
1901450e7dd4SChristophe Leroy 			    (prom_strstr(model, "innipeg") == NULL))
19029b6b563cSPaul Mackerras 				continue;
19039b6b563cSPaul Mackerras 		}
19049b6b563cSPaul Mackerras 
19059b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
19069b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
19079b6b563cSPaul Mackerras 			minalign = 0;
19089b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
19099b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
19109b6b563cSPaul Mackerras 			minsize = 4UL << 20;
19119b6b563cSPaul Mackerras 
19129b6b563cSPaul Mackerras 		/*
19139b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
19149b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
19159b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
19169b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
19179b6b563cSPaul Mackerras 		 */
19189b6b563cSPaul Mackerras 		minsize = 4UL << 20;
19199b6b563cSPaul Mackerras 
19209b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
19219b6b563cSPaul Mackerras 		align = max(minalign, minsize);
19229b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
19239b6b563cSPaul Mackerras 		if (base == 0)
19249b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
19259b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
19269b6b563cSPaul Mackerras 			local_alloc_bottom = base;
19279b6b563cSPaul Mackerras 
19289b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
1929d7fbe2a0SChristophe Leroy 		memset(path, 0, sizeof(prom_scratch));
19309b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
19319b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
1932d7fbe2a0SChristophe Leroy 			      path, sizeof(prom_scratch) - 1) == PROM_ERROR) {
19339b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
19349b6b563cSPaul Mackerras 		}
19359b6b563cSPaul Mackerras 
1936a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
1937a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1938a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1939a23414beSPaul Mackerras 
19409b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
19419b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
1942eae5f709SMathieu Malaterre 		prom_debug("\tbase = 0x%llx\n", base);
19439b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
19449b6b563cSPaul Mackerras 
19459b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
19469b6b563cSPaul Mackerras 		 * over the allocated size.
19479b6b563cSPaul Mackerras 		 */
19482b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
19499b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
19509b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
19519b6b563cSPaul Mackerras 			tce_entry |= 0x3;
19529b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
19539b6b563cSPaul Mackerras 		}
19549b6b563cSPaul Mackerras 
19559b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
19569b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
19579b6b563cSPaul Mackerras 		if (phb_node == 0)
19589b6b563cSPaul Mackerras 			prom_printf("... failed\n");
19599b6b563cSPaul Mackerras 		else
19609b6b563cSPaul Mackerras 			prom_printf("... done\n");
19619b6b563cSPaul Mackerras 
19629b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
19639b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
19649b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
19659b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
19669b6b563cSPaul Mackerras 	}
19679b6b563cSPaul Mackerras 
19689b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
19699b6b563cSPaul Mackerras 
19702babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
19712babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
19725827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
19735827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
19749b6b563cSPaul Mackerras 
19759b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
19769b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
19779b6b563cSPaul Mackerras }
1978493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1979493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
19809b6b563cSPaul Mackerras 
19819b6b563cSPaul Mackerras /*
19829b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
19839b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
19849b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
19859b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
19869b6b563cSPaul Mackerras  *
19879b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
19889b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
19899b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
19909b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
19919b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
19929b6b563cSPaul Mackerras  * of setting those values.
19939b6b563cSPaul Mackerras  *
19949b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
19959b6b563cSPaul Mackerras  * is in its holding pattern code.
19969b6b563cSPaul Mackerras  *
19979b6b563cSPaul Mackerras  * -- Cort
19989b6b563cSPaul Mackerras  */
1999bbd0abdaSPaul Mackerras /*
2000bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
2001bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
2002bbd0abdaSPaul Mackerras  */
2003bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
2004bbd0abdaSPaul Mackerras 
20059b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
20069b6b563cSPaul Mackerras {
20079b6b563cSPaul Mackerras 	unsigned long i;
20089b6b563cSPaul Mackerras 	phandle node;
20099b6b563cSPaul Mackerras 	char type[64];
20109b6b563cSPaul Mackerras 	unsigned long *spinloop
2011bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
20129b6b563cSPaul Mackerras 	unsigned long *acknowledge
2013bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
2014bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
20159b6b563cSPaul Mackerras 
2016dbe78b40SBenjamin Herrenschmidt 	/*
2017dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
2018dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
2019dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
2020dbe78b40SBenjamin Herrenschmidt 	 */
2021dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
2022dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
2023dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
2024dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
2025dbe78b40SBenjamin Herrenschmidt 		return;
2026dbe78b40SBenjamin Herrenschmidt 	}
2027dbe78b40SBenjamin Herrenschmidt 
20289b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
2029eae5f709SMathieu Malaterre 	prom_debug("    1) spinloop       = 0x%lx\n", (unsigned long)spinloop);
2030eae5f709SMathieu Malaterre 	prom_debug("    1) *spinloop      = 0x%lx\n", *spinloop);
2031eae5f709SMathieu Malaterre 	prom_debug("    1) acknowledge    = 0x%lx\n",
20329b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
2033eae5f709SMathieu Malaterre 	prom_debug("    1) *acknowledge   = 0x%lx\n", *acknowledge);
2034eae5f709SMathieu Malaterre 	prom_debug("    1) secondary_hold = 0x%lx\n", secondary_hold);
20359b6b563cSPaul Mackerras 
20369b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
20379b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
20389b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
20399b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
20409b6b563cSPaul Mackerras 	 */
20419b6b563cSPaul Mackerras 	*spinloop = 0;
20429b6b563cSPaul Mackerras 
20439b6b563cSPaul Mackerras 	/* look for cpus */
20449b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
2045493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
2046493adffcSBenjamin Herrenschmidt 		__be32 reg;
2047493adffcSBenjamin Herrenschmidt 
20489b6b563cSPaul Mackerras 		type[0] = 0;
20499b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
2050450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "cpu") != 0)
20519b6b563cSPaul Mackerras 			continue;
20529b6b563cSPaul Mackerras 
20539b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
20549b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
2055450e7dd4SChristophe Leroy 			if (prom_strcmp(type, "okay") != 0)
20569b6b563cSPaul Mackerras 				continue;
20579b6b563cSPaul Mackerras 
2058493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
20599b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
2060493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
20619b6b563cSPaul Mackerras 
2062eae5f709SMathieu Malaterre 		prom_debug("cpu hw idx   = %u\n", cpu_no);
20639b6b563cSPaul Mackerras 
20649b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
20659b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
20669b6b563cSPaul Mackerras 		 * spinloop.
20679b6b563cSPaul Mackerras 		 */
20689b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
20699b6b563cSPaul Mackerras 
2070493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
207127f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
2072eae5f709SMathieu Malaterre 			prom_printf("starting cpu hw idx %u... ", cpu_no);
20739b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
2074493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
20759b6b563cSPaul Mackerras 
20769b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
20779b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
20789b6b563cSPaul Mackerras 				mb();
20799b6b563cSPaul Mackerras 
2080493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
20819b6b563cSPaul Mackerras 				prom_printf("done\n");
2082bbd0abdaSPaul Mackerras 			else
2083eae5f709SMathieu Malaterre 				prom_printf("failed: %lx\n", *acknowledge);
20849b6b563cSPaul Mackerras 		}
20859b6b563cSPaul Mackerras #ifdef CONFIG_SMP
20869b6b563cSPaul Mackerras 		else
2087eae5f709SMathieu Malaterre 			prom_printf("boot cpu hw idx %u\n", cpu_no);
20889b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
20899b6b563cSPaul Mackerras 	}
20909b6b563cSPaul Mackerras 
20919b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
20929b6b563cSPaul Mackerras }
20939b6b563cSPaul Mackerras 
20949b6b563cSPaul Mackerras 
20959b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
20969b6b563cSPaul Mackerras {
20979b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
20985827d416SAnton Blanchard 	prom_entry = pp;
20999b6b563cSPaul Mackerras 
21009b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
21015827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
21025827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
21039b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
21049b6b563cSPaul Mackerras 
21059b6b563cSPaul Mackerras 	/* get device tree root */
21065827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
21075827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
21089b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
2109a575b807SPaul Mackerras 
21105827d416SAnton Blanchard 	prom.mmumap = 0;
21119b6b563cSPaul Mackerras }
21129b6b563cSPaul Mackerras 
2113a575b807SPaul Mackerras #ifdef CONFIG_PPC32
2114a575b807SPaul Mackerras /*
2115a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
2116a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
2117a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
2118a575b807SPaul Mackerras  */
2119a575b807SPaul Mackerras static void __init prom_find_mmu(void)
2120a575b807SPaul Mackerras {
2121a575b807SPaul Mackerras 	phandle oprom;
2122a575b807SPaul Mackerras 	char version[64];
2123a575b807SPaul Mackerras 
2124a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
2125a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
2126a575b807SPaul Mackerras 		return;
2127a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
2128a575b807SPaul Mackerras 		return;
2129a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
2130a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
2131450e7dd4SChristophe Leroy 	if (prom_strcmp(version, "Open Firmware, 1.0.5") == 0)
2132a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
2133450e7dd4SChristophe Leroy 	else if (prom_strncmp(version, "FirmWorks,3.", 12) == 0) {
2134a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
2135a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2136a23414beSPaul Mackerras 	} else
2137a575b807SPaul Mackerras 		return;
21385827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
21395827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
21405827d416SAnton Blanchard 		     sizeof(prom.mmumap));
2141493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
21425827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2143a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
2144a575b807SPaul Mackerras }
2145a575b807SPaul Mackerras #else
2146a575b807SPaul Mackerras #define prom_find_mmu()
2147a575b807SPaul Mackerras #endif
2148a575b807SPaul Mackerras 
21499b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
21509b6b563cSPaul Mackerras {
21515827d416SAnton Blanchard 	char *path = of_stdout_device;
21529b6b563cSPaul Mackerras 	char type[16];
2153493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
2154493adffcSBenjamin Herrenschmidt 	__be32 val;
21559b6b563cSPaul Mackerras 
21565827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
21579b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
21589b6b563cSPaul Mackerras 
2159493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
21609b6b563cSPaul Mackerras 
21619b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
21629b6b563cSPaul Mackerras 	memset(path, 0, 256);
21635827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
21645827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
21655827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2166450e7dd4SChristophe Leroy 		     path, prom_strlen(path) + 1);
21679b6b563cSPaul Mackerras 
216810348f59SBenjamin Herrenschmidt 	/* instance-to-package fails on PA-Semi */
216910348f59SBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
217010348f59SBenjamin Herrenschmidt 	if (stdout_node != PROM_ERROR) {
217110348f59SBenjamin Herrenschmidt 		val = cpu_to_be32(stdout_node);
217210348f59SBenjamin Herrenschmidt 
21739b6b563cSPaul Mackerras 		/* If it's a display, note it */
21749b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
2175493adffcSBenjamin Herrenschmidt 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
2176450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "display") == 0)
2177493adffcSBenjamin Herrenschmidt 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
21789b6b563cSPaul Mackerras 	}
217910348f59SBenjamin Herrenschmidt }
21809b6b563cSPaul Mackerras 
21819b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
21829b6b563cSPaul Mackerras {
21839b6b563cSPaul Mackerras 	char compat[256];
21849b6b563cSPaul Mackerras 	int len, i = 0;
218521fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
21869b6b563cSPaul Mackerras 	phandle rtas;
2187e8222502SBenjamin Herrenschmidt 	int x;
218821fe3301SBenjamin Herrenschmidt #endif
2189e8222502SBenjamin Herrenschmidt 
21906e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
21915827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
21929b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
21939b6b563cSPaul Mackerras 	if (len > 0) {
21949b6b563cSPaul Mackerras 		compat[len] = 0;
21959b6b563cSPaul Mackerras 		while (i < len) {
21969b6b563cSPaul Mackerras 			char *p = &compat[i];
2197450e7dd4SChristophe Leroy 			int sl = prom_strlen(p);
21989b6b563cSPaul Mackerras 			if (sl == 0)
21999b6b563cSPaul Mackerras 				break;
2200450e7dd4SChristophe Leroy 			if (prom_strstr(p, "Power Macintosh") ||
2201450e7dd4SChristophe Leroy 			    prom_strstr(p, "MacRISC"))
22029b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
2203133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
2204133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
2205133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
2206133dda1eSArnd Bergmann 			 * so we do it here.
2207133dda1eSArnd Bergmann 			 */
2208450e7dd4SChristophe Leroy 			if (prom_strstr(p, "IBM,CBEA") ||
2209450e7dd4SChristophe Leroy 			    prom_strstr(p, "IBM,CPBW-1.0"))
2210133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
2211133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
22129b6b563cSPaul Mackerras 			i += sl + 1;
22139b6b563cSPaul Mackerras 		}
22149b6b563cSPaul Mackerras 	}
22159b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
22166e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
2217e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
2218e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
2219e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
2220e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
2221e8222502SBenjamin Herrenschmidt 	 */
22225827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
2223e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
2224e8222502SBenjamin Herrenschmidt 	if (len <= 0)
2225e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2226450e7dd4SChristophe Leroy 	if (prom_strcmp(compat, "chrp"))
2227e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2228e8222502SBenjamin Herrenschmidt 
22299b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
22309b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2231e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
2232e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
2233e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
22349b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
22354da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
22369b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
22379b6b563cSPaul Mackerras 	}
22389b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
22399b6b563cSPaul Mackerras #else
2240e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
22419b6b563cSPaul Mackerras #endif
22429b6b563cSPaul Mackerras }
22439b6b563cSPaul Mackerras 
22449b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
22459b6b563cSPaul Mackerras {
22469b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
22479b6b563cSPaul Mackerras }
22489b6b563cSPaul Mackerras 
22499b6b563cSPaul Mackerras /*
22509b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
22519b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
22529b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
22539b6b563cSPaul Mackerras  * we've taken over the MMU.
22549b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
22559b6b563cSPaul Mackerras  * and if so, open it now.
22569b6b563cSPaul Mackerras  */
22579b6b563cSPaul Mackerras static void __init prom_check_displays(void)
22589b6b563cSPaul Mackerras {
22599b6b563cSPaul Mackerras 	char type[16], *path;
22609b6b563cSPaul Mackerras 	phandle node;
22619b6b563cSPaul Mackerras 	ihandle ih;
22629b6b563cSPaul Mackerras 	int i;
22639b6b563cSPaul Mackerras 
2264d00e34b9SBenjamin Herrenschmidt 	static const unsigned char default_colors[] __initconst = {
22659b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
22669b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
22679b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
22689b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
22699b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
22709b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
22719b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
22729b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
22739b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
22749b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
22759b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
22769b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
22779b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
22789b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
22799b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
22809b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
22819b6b563cSPaul Mackerras 	};
22829b6b563cSPaul Mackerras 	const unsigned char *clut;
22839b6b563cSPaul Mackerras 
22844da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
22859b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
22869b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
22879b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
2288450e7dd4SChristophe Leroy 		if (prom_strcmp(type, "display") != 0)
22899b6b563cSPaul Mackerras 			continue;
22909b6b563cSPaul Mackerras 
22919b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
22925827d416SAnton Blanchard 		path = prom_scratch;
2293d7fbe2a0SChristophe Leroy 		memset(path, 0, sizeof(prom_scratch));
22949b6b563cSPaul Mackerras 
22959b6b563cSPaul Mackerras 		/*
22969b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
22979b6b563cSPaul Mackerras 		 * arguments
22989b6b563cSPaul Mackerras 		 */
22999b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
2300d7fbe2a0SChristophe Leroy 			      sizeof(prom_scratch) - 10) == PROM_ERROR)
23019b6b563cSPaul Mackerras 			continue;
23029b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
23039b6b563cSPaul Mackerras 
23049b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
23059b6b563cSPaul Mackerras 		if (ih == 0) {
23069b6b563cSPaul Mackerras 			prom_printf("failed\n");
23079b6b563cSPaul Mackerras 			continue;
23089b6b563cSPaul Mackerras 		}
23099b6b563cSPaul Mackerras 
23109b6b563cSPaul Mackerras 		/* Success */
23119b6b563cSPaul Mackerras 		prom_printf("done\n");
2312a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
23139b6b563cSPaul Mackerras 
23149b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
23159b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
23165827d416SAnton Blanchard 		clut = default_colors;
23173f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
23189b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
23199b6b563cSPaul Mackerras 					   clut[2]) != 0)
23209b6b563cSPaul Mackerras 				break;
23219b6b563cSPaul Mackerras 
23229b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
23235827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
23245827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
23259b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
23269b6b563cSPaul Mackerras 					   clut[2]) != 0)
23279b6b563cSPaul Mackerras 				break;
23289b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
23297191b615SBenjamin Herrenschmidt 
23307191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
23317191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
23327191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
23337191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
23347191b615SBenjamin Herrenschmidt 
23357191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
23367191b615SBenjamin Herrenschmidt 			prom_getprop(node, "width", &width, 4);
23377191b615SBenjamin Herrenschmidt 			prom_getprop(node, "height", &height, 4);
23387191b615SBenjamin Herrenschmidt 			prom_getprop(node, "linebytes", &pitch, 4);
23397191b615SBenjamin Herrenschmidt 			prom_getprop(node, "address", &addr, 4);
23407191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
23417191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
23427191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
2343*c21f5a9eSChristophe Leroy 			btext_prepare_BAT();
23447191b615SBenjamin Herrenschmidt 		}
23457191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
23469b6b563cSPaul Mackerras 	}
23479b6b563cSPaul Mackerras }
23489b6b563cSPaul Mackerras 
23499b6b563cSPaul Mackerras 
23509b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
23519b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
23529b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
23539b6b563cSPaul Mackerras {
23549b6b563cSPaul Mackerras 	void *ret;
23559b6b563cSPaul Mackerras 
23569b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
23579b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
23589b6b563cSPaul Mackerras 		unsigned long room, chunk;
23599b6b563cSPaul Mackerras 
2360eae5f709SMathieu Malaterre 		prom_debug("Chunk exhausted, claiming more at %lx...\n",
23615827d416SAnton Blanchard 			   alloc_bottom);
23625827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
23639b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
23649b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
23659b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2366fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2367fbafd728SAnton Blanchard 				   "(no room)\n");
23689b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
23699b6b563cSPaul Mackerras 		if (chunk == 0)
2370fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2371fbafd728SAnton Blanchard 				   "(claim failed)\n");
2372966728ddSAnton Blanchard 		*mem_end = chunk + room;
23739b6b563cSPaul Mackerras 	}
23749b6b563cSPaul Mackerras 
23759b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
23769b6b563cSPaul Mackerras 	*mem_start += needed;
23779b6b563cSPaul Mackerras 
23789b6b563cSPaul Mackerras 	return ret;
23799b6b563cSPaul Mackerras }
23809b6b563cSPaul Mackerras 
2381493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2382493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2383493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2384493adffcSBenjamin Herrenschmidt 	} while(0)
23859b6b563cSPaul Mackerras 
23869b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
23879b6b563cSPaul Mackerras {
23889b6b563cSPaul Mackerras 	char *s, *os;
23899b6b563cSPaul Mackerras 
23905827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
23919b6b563cSPaul Mackerras 	s += 4;
23925827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
2393450e7dd4SChristophe Leroy 		if (prom_strcmp(s, str) == 0)
23949b6b563cSPaul Mackerras 			return s - os;
2395450e7dd4SChristophe Leroy 		s += prom_strlen(s) + 1;
23969b6b563cSPaul Mackerras 	}
23979b6b563cSPaul Mackerras 	return 0;
23989b6b563cSPaul Mackerras }
23999b6b563cSPaul Mackerras 
24009b6b563cSPaul Mackerras /*
24019b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
24029b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
24039b6b563cSPaul Mackerras  */
24049b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
24059b6b563cSPaul Mackerras 
24069b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
24079b6b563cSPaul Mackerras 					 unsigned long *mem_start,
24089b6b563cSPaul Mackerras 					 unsigned long *mem_end)
24099b6b563cSPaul Mackerras {
24109b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
24119b6b563cSPaul Mackerras 	unsigned long soff;
24129b6b563cSPaul Mackerras 	phandle child;
24139b6b563cSPaul Mackerras 
24145827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
24159b6b563cSPaul Mackerras 
24169b6b563cSPaul Mackerras 	/* get and store all property names */
24175827d416SAnton Blanchard 	prev_name = "";
24189b6b563cSPaul Mackerras 	for (;;) {
24199b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
24209b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
24219b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
24229b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
24239b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
24249b6b563cSPaul Mackerras 			break;
24259b6b563cSPaul Mackerras 		}
24269b6b563cSPaul Mackerras 
24279b6b563cSPaul Mackerras  		/* skip "name" */
2428450e7dd4SChristophe Leroy 		if (prom_strcmp(namep, "name") == 0) {
24299b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
24305827d416SAnton Blanchard  			prev_name = "name";
24319b6b563cSPaul Mackerras  			continue;
24329b6b563cSPaul Mackerras  		}
24339b6b563cSPaul Mackerras 		/* get/create string entry */
24349b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
24359b6b563cSPaul Mackerras 		if (soff != 0) {
24369b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
24379b6b563cSPaul Mackerras 			namep = sstart + soff;
24389b6b563cSPaul Mackerras 		} else {
24399b6b563cSPaul Mackerras 			/* Trim off some if we can */
2440450e7dd4SChristophe Leroy 			*mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
24415827d416SAnton Blanchard 			dt_string_end = *mem_start;
24429b6b563cSPaul Mackerras 		}
24439b6b563cSPaul Mackerras 		prev_name = namep;
24449b6b563cSPaul Mackerras 	}
24459b6b563cSPaul Mackerras 
24469b6b563cSPaul Mackerras 	/* do all our children */
24479b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
24489b6b563cSPaul Mackerras 	while (child != 0) {
24499b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
24509b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
24519b6b563cSPaul Mackerras 	}
24529b6b563cSPaul Mackerras }
24539b6b563cSPaul Mackerras 
24549b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
24559b6b563cSPaul Mackerras 					unsigned long *mem_end)
24569b6b563cSPaul Mackerras {
24579b6b563cSPaul Mackerras 	phandle child;
24589b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
24599b6b563cSPaul Mackerras 	unsigned long soff;
24609b6b563cSPaul Mackerras 	unsigned char *valp;
24618ca2d515SBenjamin Herrenschmidt 	static char pname[MAX_PROPERTY_NAME] __prombss;
24626e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
24639b6b563cSPaul Mackerras 
24649b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
24659b6b563cSPaul Mackerras 
24669b6b563cSPaul Mackerras 	/* get the node's full name */
24679b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2468c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2469c4988820SPaul Mackerras 	if (room > 255)
2470c4988820SPaul Mackerras 		room = 255;
2471c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
24729b6b563cSPaul Mackerras 	if (l >= 0) {
24739b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2474c4988820SPaul Mackerras 		if (l >= room) {
2475c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
24769b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
24779b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
24789b6b563cSPaul Mackerras 		}
24799b6b563cSPaul Mackerras 		namep[l] = '\0';
24809b6b563cSPaul Mackerras 
24819b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2482a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2483a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
24849b6b563cSPaul Mackerras 		 */
2485a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
24869b6b563cSPaul Mackerras 			if (*p == '/')
2487a575b807SPaul Mackerras 				lp = namep;
2488a575b807SPaul Mackerras 			else if (*p != 0)
2489a575b807SPaul Mackerras 				*lp++ = *p;
2490a575b807SPaul Mackerras 		}
2491a575b807SPaul Mackerras 		*lp = 0;
2492a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
24939b6b563cSPaul Mackerras 	}
24949b6b563cSPaul Mackerras 
24959b6b563cSPaul Mackerras 	/* get it again for debugging */
24965827d416SAnton Blanchard 	path = prom_scratch;
2497d7fbe2a0SChristophe Leroy 	memset(path, 0, sizeof(prom_scratch));
2498d7fbe2a0SChristophe Leroy 	call_prom("package-to-path", 3, 1, node, path, sizeof(prom_scratch) - 1);
24999b6b563cSPaul Mackerras 
25009b6b563cSPaul Mackerras 	/* get and store all properties */
25015827d416SAnton Blanchard 	prev_name = "";
25025827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
25039b6b563cSPaul Mackerras 	for (;;) {
25049b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
25055827d416SAnton Blanchard 			      pname) != 1)
25069b6b563cSPaul Mackerras 			break;
25079b6b563cSPaul Mackerras 
25089b6b563cSPaul Mackerras  		/* skip "name" */
2509450e7dd4SChristophe Leroy 		if (prom_strcmp(pname, "name") == 0) {
25105827d416SAnton Blanchard  			prev_name = "name";
25119b6b563cSPaul Mackerras  			continue;
25129b6b563cSPaul Mackerras  		}
25139b6b563cSPaul Mackerras 
25149b6b563cSPaul Mackerras 		/* find string offset */
25155827d416SAnton Blanchard 		soff = dt_find_string(pname);
25169b6b563cSPaul Mackerras 		if (soff == 0) {
25179b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
25185827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
25199b6b563cSPaul Mackerras 			break;
25209b6b563cSPaul Mackerras 		}
25219b6b563cSPaul Mackerras 		prev_name = sstart + soff;
25229b6b563cSPaul Mackerras 
25239b6b563cSPaul Mackerras 		/* get length */
25245827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
25259b6b563cSPaul Mackerras 
25269b6b563cSPaul Mackerras 		/* sanity checks */
25279b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
25289b6b563cSPaul Mackerras 			continue;
25299b6b563cSPaul Mackerras 
25309b6b563cSPaul Mackerras 		/* push property head */
25319b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
25329b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
25339b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
25349b6b563cSPaul Mackerras 
25359b6b563cSPaul Mackerras 		/* push property content */
25369b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
25375827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
25389b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
25396e35d5daSBenjamin Herrenschmidt 
2540450e7dd4SChristophe Leroy 		if (!prom_strcmp(pname, "phandle"))
25416e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
25429b6b563cSPaul Mackerras 	}
25439b6b563cSPaul Mackerras 
2544f1f208e5SBenjamin Herrenschmidt 	/* Add a "phandle" property if none already exist */
25456e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
2546f1f208e5SBenjamin Herrenschmidt 		soff = dt_find_string("phandle");
25479b6b563cSPaul Mackerras 		if (soff == 0)
2548f1f208e5SBenjamin Herrenschmidt 			prom_printf("WARNING: Can't find string index for <phandle> node %s\n", path);
25499b6b563cSPaul Mackerras 		else {
25509b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
25519b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
25529b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
25539b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
2554493adffcSBenjamin Herrenschmidt 			*(__be32 *)valp = cpu_to_be32(node);
25559b6b563cSPaul Mackerras 		}
25566e35d5daSBenjamin Herrenschmidt 	}
25579b6b563cSPaul Mackerras 
25589b6b563cSPaul Mackerras 	/* do all our children */
25599b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
25609b6b563cSPaul Mackerras 	while (child != 0) {
25619b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
25629b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
25639b6b563cSPaul Mackerras 	}
25649b6b563cSPaul Mackerras 
25659b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
25669b6b563cSPaul Mackerras }
25679b6b563cSPaul Mackerras 
25689b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
25699b6b563cSPaul Mackerras {
25709b6b563cSPaul Mackerras 	phandle root;
25719b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
25729b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
25739b6b563cSPaul Mackerras 	char *namep;
25749b6b563cSPaul Mackerras 	u64 *rsvmap;
25759b6b563cSPaul Mackerras 
25769b6b563cSPaul Mackerras 	/*
25779b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2578fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
25799b6b563cSPaul Mackerras 	 */
25805827d416SAnton Blanchard 	room = alloc_top - alloc_bottom - 0x4000;
25819b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
25829b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
2583eae5f709SMathieu Malaterre 	prom_debug("starting device tree allocs at %lx\n", alloc_bottom);
25849b6b563cSPaul Mackerras 
25859b6b563cSPaul Mackerras 	/* Now try to claim that */
25869b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
25879b6b563cSPaul Mackerras 	if (mem_start == 0)
25889b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2589966728ddSAnton Blanchard 	mem_end = mem_start + room;
25909b6b563cSPaul Mackerras 
25919b6b563cSPaul Mackerras 	/* Get root of tree */
25929b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
25939b6b563cSPaul Mackerras 	if (root == (phandle)0)
25949b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
25959b6b563cSPaul Mackerras 
25969b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
25979b6b563cSPaul Mackerras 	mem_start = _ALIGN(mem_start, 4);
25989b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
25999b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
26005827d416SAnton Blanchard 	dt_header_start = (unsigned long)hdr;
26019b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
26029b6b563cSPaul Mackerras 
26039b6b563cSPaul Mackerras 	/* Start of strings */
26049b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
26055827d416SAnton Blanchard 	dt_string_start = mem_start;
26069b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
26079b6b563cSPaul Mackerras 
2608f1f208e5SBenjamin Herrenschmidt 	/* Add "phandle" in there, we'll need it */
26099b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
2610450e7dd4SChristophe Leroy 	prom_strcpy(namep, "phandle");
2611450e7dd4SChristophe Leroy 	mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
26129b6b563cSPaul Mackerras 
26139b6b563cSPaul Mackerras 	/* Build string array */
26149b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
26159b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
26165827d416SAnton Blanchard 	dt_string_end = mem_start;
26179b6b563cSPaul Mackerras 
26189b6b563cSPaul Mackerras 	/* Build structure */
26199b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
26205827d416SAnton Blanchard 	dt_struct_start = mem_start;
26219b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
26229b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
26239b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
26245827d416SAnton Blanchard 	dt_struct_end = PAGE_ALIGN(mem_start);
26259b6b563cSPaul Mackerras 
26269b6b563cSPaul Mackerras 	/* Finish header */
2627493adffcSBenjamin Herrenschmidt 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2628493adffcSBenjamin Herrenschmidt 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2629493adffcSBenjamin Herrenschmidt 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2630493adffcSBenjamin Herrenschmidt 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2631493adffcSBenjamin Herrenschmidt 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2632493adffcSBenjamin Herrenschmidt 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2633493adffcSBenjamin Herrenschmidt 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2634493adffcSBenjamin Herrenschmidt 	hdr->version = cpu_to_be32(OF_DT_VERSION);
26359b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
2636493adffcSBenjamin Herrenschmidt 	hdr->last_comp_version = cpu_to_be32(0x10);
26379b6b563cSPaul Mackerras 
26384d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
26395827d416SAnton Blanchard 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
26409b6b563cSPaul Mackerras 
26419b6b563cSPaul Mackerras #ifdef DEBUG_PROM
26429b6b563cSPaul Mackerras 	{
26439b6b563cSPaul Mackerras 		int i;
26449b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
26455827d416SAnton Blanchard 		for (i = 0; i < mem_reserve_cnt; i++)
2646eae5f709SMathieu Malaterre 			prom_printf("  %llx - %llx\n",
2647493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].base),
2648493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].size));
26499b6b563cSPaul Mackerras 	}
26509b6b563cSPaul Mackerras #endif
26514d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
26524d1f3f25SJimi Xenidis 	 * since it's too late.
26534d1f3f25SJimi Xenidis 	 */
26545827d416SAnton Blanchard 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
26559b6b563cSPaul Mackerras 
2656eae5f709SMathieu Malaterre 	prom_printf("Device tree strings 0x%lx -> 0x%lx\n",
26575827d416SAnton Blanchard 		    dt_string_start, dt_string_end);
2658eae5f709SMathieu Malaterre 	prom_printf("Device tree struct  0x%lx -> 0x%lx\n",
26595827d416SAnton Blanchard 		    dt_struct_start, dt_struct_end);
26609b6b563cSPaul Mackerras }
26619b6b563cSPaul Mackerras 
266254f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
266354f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
266454f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
266554f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
26669b6b563cSPaul Mackerras {
266754f4ee18SHollis Blanchard 	phandle isa;
2668980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
266954f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2670980a6513SBenjamin Herrenschmidt 	char *name;
267154f4ee18SHollis Blanchard 
2672980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2673980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2674980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2675980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2676980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2677980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2678980a6513SBenjamin Herrenschmidt 	}
267954f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
268054f4ee18SHollis Blanchard 		return;
268154f4ee18SHollis Blanchard 
2682980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2683980a6513SBenjamin Herrenschmidt 		return;
268454f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
268554f4ee18SHollis Blanchard 		== PROM_ERROR)
268654f4ee18SHollis Blanchard 		return;
268754f4ee18SHollis Blanchard 
268854f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
268954f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
269054f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
269154f4ee18SHollis Blanchard 		return;
269254f4ee18SHollis Blanchard 
2693980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
269454f4ee18SHollis Blanchard 
269554f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
269654f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2697980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
269854f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
269954f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
270054f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2701980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
270254f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
270354f4ee18SHollis Blanchard }
27048f101a05SHarry Ciao 
27058f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
27068f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
27078f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
27088f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
27098f101a05SHarry Ciao {
27108f101a05SHarry Ciao 	phandle mc;
27118f101a05SHarry Ciao 	u32 mc_reg[4];
27128f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
27138f101a05SHarry Ciao 	u32 ac, sc;
27148f101a05SHarry Ciao 
27158f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
27168f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
27178f101a05SHarry Ciao 		return;
27188f101a05SHarry Ciao 
27198f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
27208f101a05SHarry Ciao 		return;
27218f101a05SHarry Ciao 
27225827d416SAnton Blanchard 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
27235827d416SAnton Blanchard 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
27248f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
27258f101a05SHarry Ciao 		return;
27268f101a05SHarry Ciao 
27278f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
27288f101a05SHarry Ciao 		return;
27298f101a05SHarry Ciao 
27308f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
27318f101a05SHarry Ciao 		return;
27328f101a05SHarry Ciao 
27338f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
27348f101a05SHarry Ciao 
27358f101a05SHarry Ciao 	mc_reg[0] = 0x0;
27368f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
27378f101a05SHarry Ciao 	mc_reg[2] = 0x0;
27388f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
27398f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
27408f101a05SHarry Ciao }
274154f4ee18SHollis Blanchard #else
274254f4ee18SHollis Blanchard #define fixup_device_tree_maple()
27438f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
274454f4ee18SHollis Blanchard #endif
274554f4ee18SHollis Blanchard 
2746e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2747e4805922SOlaf Hering /*
2748e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2749e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2750556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2751e4805922SOlaf Hering  */
2752e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2753e8c0acf9SBenjamin Herrenschmidt {
2754e4805922SOlaf Hering 	phandle ph;
2755e4805922SOlaf Hering 	u32 prop[6];
275626c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2757e8c0acf9SBenjamin Herrenschmidt 	char *name;
2758e8c0acf9SBenjamin Herrenschmidt 	int rc;
2759e8c0acf9SBenjamin Herrenschmidt 
2760e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2761e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2762e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
276326c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2764e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
276526c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
276626c5032eSBenjamin Herrenschmidt 	}
2767e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2768e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2769e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2770e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2771e8c0acf9SBenjamin Herrenschmidt 
2772e4805922SOlaf Hering 			prop[0] = 0x1;
2773e4805922SOlaf Hering 			prop[1] = 0x0;
2774e4805922SOlaf Hering 			prop[2] = rloc;
2775e4805922SOlaf Hering 			prop[3] = 0x0;
2776e4805922SOlaf Hering 			prop[4] = 0x0;
2777e4805922SOlaf Hering 			prop[5] = 0x00010000;
2778e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2779e4805922SOlaf Hering 		}
2780e4805922SOlaf Hering 	}
2781e4805922SOlaf Hering 
2782e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2783e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2784e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2785e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2786e4805922SOlaf Hering 		prop[0] = 14;
2787e4805922SOlaf Hering 		prop[1] = 0x0;
2788556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2789556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2790556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2791556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2792556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2793556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2794556ecf9bSOlaf Hering 		}
2795e4805922SOlaf Hering 	}
2796e8c0acf9SBenjamin Herrenschmidt }
2797e8c0acf9SBenjamin Herrenschmidt #else
2798e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2799e8c0acf9SBenjamin Herrenschmidt #endif
2800e8c0acf9SBenjamin Herrenschmidt 
28019b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
280254f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
280354f4ee18SHollis Blanchard {
28049b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
28059b6b563cSPaul Mackerras 	u32 u3_rev;
28069b6b563cSPaul Mackerras 	u32 interrupts[2];
28079b6b563cSPaul Mackerras 	u32 parent;
28089b6b563cSPaul Mackerras 
28099b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
28109b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
28119b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
28129b6b563cSPaul Mackerras 		return;
28139b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
28149b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
28159b6b563cSPaul Mackerras 		return;
28169b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
28179b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
28189b6b563cSPaul Mackerras 		return;
28199b6b563cSPaul Mackerras 
28209b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
28219b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
28229b6b563cSPaul Mackerras 	    == PROM_ERROR)
28239b6b563cSPaul Mackerras 		return;
28247d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
28259b6b563cSPaul Mackerras 		return;
28269b6b563cSPaul Mackerras 	/* does it need fixup ? */
28279b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
28289b6b563cSPaul Mackerras 		return;
28299b6b563cSPaul Mackerras 
28309b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
28319b6b563cSPaul Mackerras 
28329b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
28339b6b563cSPaul Mackerras 	interrupts[0] = 0;
28349b6b563cSPaul Mackerras 	interrupts[1] = 1;
2835a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2836a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
28379b6b563cSPaul Mackerras 	parent = (u32)mpic;
2838a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2839a23414beSPaul Mackerras 		     &parent, sizeof(parent));
28409b6b563cSPaul Mackerras }
284154f4ee18SHollis Blanchard #else
284254f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
284354f4ee18SHollis Blanchard #endif
28449b6b563cSPaul Mackerras 
284588fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
284694d2dde7SGrant Likely /*
284794d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
284894d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
284994d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
285094d2dde7SGrant Likely  * ethernet node.
285194d2dde7SGrant Likely  */
285294d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
285388fd2a9dSSylvain Munaut {
285488fd2a9dSSylvain Munaut 	u32 node;
285588fd2a9dSSylvain Munaut 	char prop[64];
285694d2dde7SGrant Likely 	int rv;
285788fd2a9dSSylvain Munaut 
285894d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
285994d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
286088fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
286188fd2a9dSSylvain Munaut 		return;
286288fd2a9dSSylvain Munaut 
286394d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
286494d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
286594d2dde7SGrant Likely 	if (!rv)
286688fd2a9dSSylvain Munaut 		return;
286788fd2a9dSSylvain Munaut 
286894d2dde7SGrant Likely 	/*
286994d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
287094d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
287194d2dde7SGrant Likely 	 */
287288fd2a9dSSylvain Munaut 
287394d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
28746f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
28756f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
28766f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
28776f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
28786f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
28796f4347c9SOlaf Hering 			" new-device"
28806f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
28816f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
288294d2dde7SGrant Likely 				" s\" mdio\" device-name"
288394d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
28846f4347c9SOlaf Hering 				" s\" compatible\" property"
28856f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
28866f4347c9SOlaf Hering 				" 0x2 encode-int"
28876f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
28886f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
28896f4347c9SOlaf Hering 				" s\" interrupts\" property"
28906f4347c9SOlaf Hering 			" finish-device");
28916f4347c9SOlaf Hering 	};
28926f4347c9SOlaf Hering 
289394d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
289494d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
289594d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
289694d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
28976f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
28986f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
28996f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
29006f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
29016f4347c9SOlaf Hering 			" new-device"
29026f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
29036f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
29046f4347c9SOlaf Hering 				" my-self"
29056f4347c9SOlaf Hering 				" ihandle>phandle"
29066f4347c9SOlaf Hering 			" finish-device"
29076f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
29086f4347c9SOlaf Hering 				" encode-int"
29096f4347c9SOlaf Hering 				" s\" phy-handle\" property"
29106f4347c9SOlaf Hering 			" device-end");
29116f4347c9SOlaf Hering 	}
291294d2dde7SGrant Likely }
29136f4347c9SOlaf Hering 
291494d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
291594d2dde7SGrant Likely {
291694d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
291794d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
291894d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
291994d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
292094d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
292194d2dde7SGrant Likely 	u32 node;
292294d2dde7SGrant Likely 	char prop[64];
292394d2dde7SGrant Likely 	int rv, len;
292494d2dde7SGrant Likely 
292594d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
292694d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
292794d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
292894d2dde7SGrant Likely 		return;
292994d2dde7SGrant Likely 
293094d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
293194d2dde7SGrant Likely 	if (rv == PROM_ERROR)
293294d2dde7SGrant Likely 		return;
2933450e7dd4SChristophe Leroy 	if (prom_strcmp(prop, "EFIKA5K2"))
293494d2dde7SGrant Likely 		return;
293594d2dde7SGrant Likely 
293694d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
293794d2dde7SGrant Likely 
293894d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
293994d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
294094d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2941450e7dd4SChristophe Leroy 	if (rv != PROM_ERROR && (prom_strcmp(prop, "chrp") == 0))
294294d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
294394d2dde7SGrant Likely 
29447f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
29457f4392cdSDavid Woodhouse 	   fix that too */
29467f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2947450e7dd4SChristophe Leroy 	if (rv != PROM_ERROR && (prom_strstr(prop, "CHRP")))
29487f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
29497f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
29507f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
29517f4392cdSDavid Woodhouse 
295294d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
295394d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
295494d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
295594d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
295694d2dde7SGrant Likely 		if (len == 12) {
295794d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
295894d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
295994d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
296094d2dde7SGrant Likely 		}
296194d2dde7SGrant Likely 	}
296294d2dde7SGrant Likely 
296394d2dde7SGrant Likely 	/* Fixup sound interrupts property */
296494d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
296594d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
296694d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
296794d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
296894d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
296994d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
297094d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
297194d2dde7SGrant Likely 		}
297294d2dde7SGrant Likely 	}
297394d2dde7SGrant Likely 
297494d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
297594d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
297688fd2a9dSSylvain Munaut }
297788fd2a9dSSylvain Munaut #else
297888fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
297988fd2a9dSSylvain Munaut #endif
298088fd2a9dSSylvain Munaut 
298150246787SDarren Stevens #ifdef CONFIG_PPC_PASEMI_NEMO
298250246787SDarren Stevens /*
298350246787SDarren Stevens  * CFE supplied on Nemo is broken in several ways, biggest
298450246787SDarren Stevens  * problem is that it reassigns ISA interrupts to unused mpic ints.
298550246787SDarren Stevens  * Add an interrupt-controller property for the io-bridge to use
298650246787SDarren Stevens  * and correct the ints so we can attach them to an irq_domain
298750246787SDarren Stevens  */
298850246787SDarren Stevens static void __init fixup_device_tree_pasemi(void)
298950246787SDarren Stevens {
299050246787SDarren Stevens 	u32 interrupts[2], parent, rval, val = 0;
299150246787SDarren Stevens 	char *name, *pci_name;
299250246787SDarren Stevens 	phandle iob, node;
299350246787SDarren Stevens 
299450246787SDarren Stevens 	/* Find the root pci node */
299550246787SDarren Stevens 	name = "/pxp@0,e0000000";
299650246787SDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
299750246787SDarren Stevens 	if (!PHANDLE_VALID(iob))
299850246787SDarren Stevens 		return;
299950246787SDarren Stevens 
300050246787SDarren Stevens 	/* check if interrupt-controller node set yet */
300150246787SDarren Stevens 	if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
300250246787SDarren Stevens 		return;
300350246787SDarren Stevens 
300450246787SDarren Stevens 	prom_printf("adding interrupt-controller property for SB600...\n");
300550246787SDarren Stevens 
300650246787SDarren Stevens 	prom_setprop(iob, name, "interrupt-controller", &val, 0);
300750246787SDarren Stevens 
300850246787SDarren Stevens 	pci_name = "/pxp@0,e0000000/pci@11";
300950246787SDarren Stevens 	node = call_prom("finddevice", 1, 1, ADDR(pci_name));
301050246787SDarren Stevens 	parent = ADDR(iob);
301150246787SDarren Stevens 
301250246787SDarren Stevens 	for( ; prom_next_node(&node); ) {
301350246787SDarren Stevens 		/* scan each node for one with an interrupt */
301450246787SDarren Stevens 		if (!PHANDLE_VALID(node))
301550246787SDarren Stevens 			continue;
301650246787SDarren Stevens 
301750246787SDarren Stevens 		rval = prom_getproplen(node, "interrupts");
301850246787SDarren Stevens 		if (rval == 0 || rval == PROM_ERROR)
301950246787SDarren Stevens 			continue;
302050246787SDarren Stevens 
302150246787SDarren Stevens 		prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
302250246787SDarren Stevens 		if ((interrupts[0] < 212) || (interrupts[0] > 222))
302350246787SDarren Stevens 			continue;
302450246787SDarren Stevens 
302550246787SDarren Stevens 		/* found a node, update both interrupts and interrupt-parent */
302650246787SDarren Stevens 		if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
302750246787SDarren Stevens 			interrupts[0] -= 203;
302850246787SDarren Stevens 		if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
302950246787SDarren Stevens 			interrupts[0] -= 213;
303050246787SDarren Stevens 		if (interrupts[0] == 221)
303150246787SDarren Stevens 			interrupts[0] = 14;
303250246787SDarren Stevens 		if (interrupts[0] == 222)
303350246787SDarren Stevens 			interrupts[0] = 8;
303450246787SDarren Stevens 
303550246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupts", interrupts,
303650246787SDarren Stevens 					sizeof(interrupts));
303750246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupt-parent", &parent,
303850246787SDarren Stevens 					sizeof(parent));
303950246787SDarren Stevens 	}
3040687e16bcSDarren Stevens 
3041687e16bcSDarren Stevens 	/*
3042687e16bcSDarren Stevens 	 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
3043687e16bcSDarren Stevens 	 * so that generic isa-bridge code can add the SB600 and its on-board
3044687e16bcSDarren Stevens 	 * peripherals.
3045687e16bcSDarren Stevens 	 */
3046687e16bcSDarren Stevens 	name = "/pxp@0,e0000000/io-bridge@0";
3047687e16bcSDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
3048687e16bcSDarren Stevens 	if (!PHANDLE_VALID(iob))
3049687e16bcSDarren Stevens 		return;
3050687e16bcSDarren Stevens 
3051687e16bcSDarren Stevens 	/* device_type is already set, just change it. */
3052687e16bcSDarren Stevens 
3053687e16bcSDarren Stevens 	prom_printf("Changing device_type of SB600 node...\n");
3054687e16bcSDarren Stevens 
3055687e16bcSDarren Stevens 	prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
305650246787SDarren Stevens }
305750246787SDarren Stevens #else	/* !CONFIG_PPC_PASEMI_NEMO */
305850246787SDarren Stevens static inline void fixup_device_tree_pasemi(void) { }
305950246787SDarren Stevens #endif
306050246787SDarren Stevens 
306154f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
306254f4ee18SHollis Blanchard {
306354f4ee18SHollis Blanchard 	fixup_device_tree_maple();
30648f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
3065e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
306654f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
306788fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
306850246787SDarren Stevens 	fixup_device_tree_pasemi();
306954f4ee18SHollis Blanchard }
30709b6b563cSPaul Mackerras 
30719b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
30729b6b563cSPaul Mackerras {
3073493adffcSBenjamin Herrenschmidt 	__be32 rval;
30749b6b563cSPaul Mackerras 	ihandle prom_cpu;
30759b6b563cSPaul Mackerras 	phandle cpu_pkg;
30769b6b563cSPaul Mackerras 
3077493adffcSBenjamin Herrenschmidt 	rval = 0;
3078493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
3079a575b807SPaul Mackerras 		return;
3080493adffcSBenjamin Herrenschmidt 	prom_cpu = be32_to_cpu(rval);
30819b6b563cSPaul Mackerras 
30829b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
30839b6b563cSPaul Mackerras 
3084af2b7fa1SDarren Stevens 	if (!PHANDLE_VALID(cpu_pkg))
3085af2b7fa1SDarren Stevens 		return;
3086af2b7fa1SDarren Stevens 
3087493adffcSBenjamin Herrenschmidt 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
3088493adffcSBenjamin Herrenschmidt 	prom.cpu = be32_to_cpu(rval);
30899b6b563cSPaul Mackerras 
3090eae5f709SMathieu Malaterre 	prom_debug("Booting CPU hw index = %d\n", prom.cpu);
30919b6b563cSPaul Mackerras }
30929b6b563cSPaul Mackerras 
30939b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
30949b6b563cSPaul Mackerras {
30959b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
30969b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
3097493adffcSBenjamin Herrenschmidt 		__be64 val;
30989b6b563cSPaul Mackerras 
30995827d416SAnton Blanchard 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
31005827d416SAnton Blanchard 		prom_initrd_end = prom_initrd_start + r4;
31019b6b563cSPaul Mackerras 
3102493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_start);
31035827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
3104a23414beSPaul Mackerras 			     &val, sizeof(val));
3105493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_end);
31065827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
3107a23414beSPaul Mackerras 			     &val, sizeof(val));
31089b6b563cSPaul Mackerras 
31095827d416SAnton Blanchard 		reserve_mem(prom_initrd_start,
31105827d416SAnton Blanchard 			    prom_initrd_end - prom_initrd_start);
31119b6b563cSPaul Mackerras 
3112eae5f709SMathieu Malaterre 		prom_debug("initrd_start=0x%lx\n", prom_initrd_start);
3113eae5f709SMathieu Malaterre 		prom_debug("initrd_end=0x%lx\n", prom_initrd_end);
31149b6b563cSPaul Mackerras 	}
31159b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
31169b6b563cSPaul Mackerras }
31179b6b563cSPaul Mackerras 
31185ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64
31195ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE
31205ac47f7aSAnton Blanchard static void reloc_toc(void)
31215ac47f7aSAnton Blanchard {
31225ac47f7aSAnton Blanchard }
31235ac47f7aSAnton Blanchard 
31245ac47f7aSAnton Blanchard static void unreloc_toc(void)
31255ac47f7aSAnton Blanchard {
31265ac47f7aSAnton Blanchard }
31275ac47f7aSAnton Blanchard #else
31281674400aSAnton Blanchard static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
31295ac47f7aSAnton Blanchard {
31305ac47f7aSAnton Blanchard 	unsigned long i;
31311674400aSAnton Blanchard 	unsigned long *toc_entry;
31321674400aSAnton Blanchard 
31331674400aSAnton Blanchard 	/* Get the start of the TOC by using r2 directly. */
31341674400aSAnton Blanchard 	asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
31355ac47f7aSAnton Blanchard 
31365ac47f7aSAnton Blanchard 	for (i = 0; i < nr_entries; i++) {
31375ac47f7aSAnton Blanchard 		*toc_entry = *toc_entry + offset;
31385ac47f7aSAnton Blanchard 		toc_entry++;
31395ac47f7aSAnton Blanchard 	}
31405ac47f7aSAnton Blanchard }
31415ac47f7aSAnton Blanchard 
31425ac47f7aSAnton Blanchard static void reloc_toc(void)
31435ac47f7aSAnton Blanchard {
31445ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
31455ac47f7aSAnton Blanchard 	unsigned long nr_entries =
31465ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
31475ac47f7aSAnton Blanchard 
31481674400aSAnton Blanchard 	__reloc_toc(offset, nr_entries);
31495ac47f7aSAnton Blanchard 
31505ac47f7aSAnton Blanchard 	mb();
31515ac47f7aSAnton Blanchard }
31525ac47f7aSAnton Blanchard 
31535ac47f7aSAnton Blanchard static void unreloc_toc(void)
31545ac47f7aSAnton Blanchard {
31555ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
31565ac47f7aSAnton Blanchard 	unsigned long nr_entries =
31575ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
31585ac47f7aSAnton Blanchard 
31595ac47f7aSAnton Blanchard 	mb();
31605ac47f7aSAnton Blanchard 
31611674400aSAnton Blanchard 	__reloc_toc(-offset, nr_entries);
31625ac47f7aSAnton Blanchard }
31635ac47f7aSAnton Blanchard #endif
31645ac47f7aSAnton Blanchard #endif
316527f44888SBenjamin Herrenschmidt 
31669b6b563cSPaul Mackerras /*
31679b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
31689b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
31699b6b563cSPaul Mackerras  */
31709b6b563cSPaul Mackerras 
31719b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
31729b6b563cSPaul Mackerras 			       unsigned long pp,
3173549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
3174549e8152SPaul Mackerras 			       unsigned long kbase)
31759b6b563cSPaul Mackerras {
31769b6b563cSPaul Mackerras 	unsigned long hdr;
31779b6b563cSPaul Mackerras 
31789b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
3179549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
31809b6b563cSPaul Mackerras 	reloc_got2(offset);
31815ac47f7aSAnton Blanchard #else
31825ac47f7aSAnton Blanchard 	reloc_toc();
31839b6b563cSPaul Mackerras #endif
31849b6b563cSPaul Mackerras 
31859b6b563cSPaul Mackerras 	/*
31869b6b563cSPaul Mackerras 	 * First zero the BSS
31879b6b563cSPaul Mackerras 	 */
31885827d416SAnton Blanchard 	memset(&__bss_start, 0, __bss_stop - __bss_start);
31899b6b563cSPaul Mackerras 
31909b6b563cSPaul Mackerras 	/*
31919b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
31929b6b563cSPaul Mackerras 	 * like /chosen
31939b6b563cSPaul Mackerras 	 */
31949b6b563cSPaul Mackerras 	prom_init_client_services(pp);
31959b6b563cSPaul Mackerras 
31969b6b563cSPaul Mackerras 	/*
3197a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
3198a23414beSPaul Mackerras 	 * and other workarounds
3199a23414beSPaul Mackerras 	 */
3200a23414beSPaul Mackerras 	prom_find_mmu();
3201a23414beSPaul Mackerras 
3202a23414beSPaul Mackerras 	/*
32039b6b563cSPaul Mackerras 	 * Init prom stdout device
32049b6b563cSPaul Mackerras 	 */
32059b6b563cSPaul Mackerras 	prom_init_stdout();
32069b6b563cSPaul Mackerras 
32075827d416SAnton Blanchard 	prom_printf("Preparing to boot %s", linux_banner);
3208e7943fbbSMichael Ellerman 
32099b6b563cSPaul Mackerras 	/*
32109b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
32119b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
32129b6b563cSPaul Mackerras 	 */
32135827d416SAnton Blanchard 	of_platform = prom_find_machine_type();
32145827d416SAnton Blanchard 	prom_printf("Detected machine type: %x\n", of_platform);
32159b6b563cSPaul Mackerras 
32160f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
3217add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
3218add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
3219add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
3220549e8152SPaul Mackerras #endif
3221add60ef3SOlaf Hering 
3222add60ef3SOlaf Hering 	/*
3223add60ef3SOlaf Hering 	 * Check for an initrd
3224add60ef3SOlaf Hering 	 */
3225add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
3226add60ef3SOlaf Hering 
322712cc9fd6SSuraj Jitindar Singh 	/*
322812cc9fd6SSuraj Jitindar Singh 	 * Do early parsing of command line
322912cc9fd6SSuraj Jitindar Singh 	 */
323012cc9fd6SSuraj Jitindar Singh 	early_cmdline_parse();
323112cc9fd6SSuraj Jitindar Singh 
323211fdb309SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PSERIES
32339b6b563cSPaul Mackerras 	/*
32349b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
32359b6b563cSPaul Mackerras 	 */
32365827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES ||
32375827d416SAnton Blanchard 	    of_platform == PLATFORM_PSERIES_LPAR)
32389b6b563cSPaul Mackerras 		prom_send_capabilities();
32399b6b563cSPaul Mackerras #endif
32409b6b563cSPaul Mackerras 
32419b6b563cSPaul Mackerras 	/*
3242f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
32439b6b563cSPaul Mackerras 	 */
32445827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC)
3245549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
32469b6b563cSPaul Mackerras 
32479b6b563cSPaul Mackerras 	/*
32489b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
32499b6b563cSPaul Mackerras 	 */
32509b6b563cSPaul Mackerras 	prom_init_mem();
32519b6b563cSPaul Mackerras 
32529b6b563cSPaul Mackerras 	/*
32539b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
32549b6b563cSPaul Mackerras 	 */
32559b6b563cSPaul Mackerras 	prom_find_boot_cpu();
32569b6b563cSPaul Mackerras 
32579b6b563cSPaul Mackerras 	/*
32589b6b563cSPaul Mackerras 	 * Initialize display devices
32599b6b563cSPaul Mackerras 	 */
32609b6b563cSPaul Mackerras 	prom_check_displays();
32619b6b563cSPaul Mackerras 
3262493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
32639b6b563cSPaul Mackerras 	/*
32649b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
32659b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
32669b6b563cSPaul Mackerras 	 * available for us here...
32679b6b563cSPaul Mackerras 	 */
32685827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES)
32699b6b563cSPaul Mackerras 		prom_initialize_tce_table();
32709b6b563cSPaul Mackerras #endif
32719b6b563cSPaul Mackerras 
32729b6b563cSPaul Mackerras 	/*
327327f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
327427f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
32759b6b563cSPaul Mackerras 	 */
327611fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
32779b6b563cSPaul Mackerras 		prom_instantiate_rtas();
327827f44888SBenjamin Herrenschmidt 
32794a727429SAshley Lai #ifdef CONFIG_PPC64
32804a727429SAshley Lai 	/* instantiate sml */
32814a727429SAshley Lai 	prom_instantiate_sml();
32824a727429SAshley Lai #endif
32834a727429SAshley Lai 
328427f44888SBenjamin Herrenschmidt 	/*
328527f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
328627f44888SBenjamin Herrenschmidt 	 *
328727f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
3288dbe78b40SBenjamin Herrenschmidt 	 *
3289dbe78b40SBenjamin Herrenschmidt 	 * (This must be done after instanciating RTAS)
329027f44888SBenjamin Herrenschmidt 	 */
329111fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
329227f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
32939b6b563cSPaul Mackerras 
32949b6b563cSPaul Mackerras 	/*
32959b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
32969b6b563cSPaul Mackerras 	 */
3297493adffcSBenjamin Herrenschmidt 	if (prom_memory_limit) {
3298493adffcSBenjamin Herrenschmidt 		__be64 val = cpu_to_be64(prom_memory_limit);
32995827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3300493adffcSBenjamin Herrenschmidt 			     &val, sizeof(val));
3301493adffcSBenjamin Herrenschmidt 	}
33029b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
33035827d416SAnton Blanchard 	if (prom_iommu_off)
33045827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3305a23414beSPaul Mackerras 			     NULL, 0);
33069b6b563cSPaul Mackerras 
33075827d416SAnton Blanchard 	if (prom_iommu_force_on)
33085827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3309a23414beSPaul Mackerras 			     NULL, 0);
33109b6b563cSPaul Mackerras 
33115827d416SAnton Blanchard 	if (prom_tce_alloc_start) {
33125827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
33135827d416SAnton Blanchard 			     &prom_tce_alloc_start,
33149b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
33155827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
33165827d416SAnton Blanchard 			     &prom_tce_alloc_end,
33179b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
33189b6b563cSPaul Mackerras 	}
33199b6b563cSPaul Mackerras #endif
33209b6b563cSPaul Mackerras 
33219b6b563cSPaul Mackerras 	/*
33229b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
33239b6b563cSPaul Mackerras 	 */
33249b6b563cSPaul Mackerras 	fixup_device_tree();
33259b6b563cSPaul Mackerras 
33269b6b563cSPaul Mackerras 	/*
33279b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
33289b6b563cSPaul Mackerras 	 */
33299b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
33309b6b563cSPaul Mackerras 	flatten_device_tree();
33319b6b563cSPaul Mackerras 
33323825ac0eSPaul Mackerras 	/*
33333825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
33343825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
333511fdb309SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101).
33363825ac0eSPaul Mackerras 	 */
333711fdb309SBenjamin Herrenschmidt 	if (of_platform != PLATFORM_POWERMAC)
33389b6b563cSPaul Mackerras 		prom_close_stdin();
33399b6b563cSPaul Mackerras 
33409b6b563cSPaul Mackerras 	/*
33419b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
33429b6b563cSPaul Mackerras 	 * devices etc...
33439b6b563cSPaul Mackerras 	 */
33447e862d7eSMichael Ellerman 	prom_printf("Quiescing Open Firmware ...\n");
33459b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
33469b6b563cSPaul Mackerras 
33479b6b563cSPaul Mackerras 	/*
33489b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
33499b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
33509b6b563cSPaul Mackerras 	 * is common to us and kexec
33519b6b563cSPaul Mackerras 	 */
33525827d416SAnton Blanchard 	hdr = dt_header_start;
335340dfef66SBenjamin Herrenschmidt 
335440dfef66SBenjamin Herrenschmidt 	/* Don't print anything after quiesce under OPAL, it crashes OFW */
33557d70c63cSBenjamin Herrenschmidt 	prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
3356eae5f709SMathieu Malaterre 	prom_debug("->dt_header_start=0x%lx\n", hdr);
33579b6b563cSPaul Mackerras 
33589b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
33599b6b563cSPaul Mackerras 	reloc_got2(-offset);
33605ac47f7aSAnton Blanchard #else
33615ac47f7aSAnton Blanchard 	unreloc_toc();
33629b6b563cSPaul Mackerras #endif
33639b6b563cSPaul Mackerras 
33646e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
33659b6b563cSPaul Mackerras 
33669b6b563cSPaul Mackerras 	return 0;
33679b6b563cSPaul Mackerras }
3368