xref: /linux/arch/powerpc/kernel/prom_init.c (revision 7e862d7e7d118e3becc5b495af10ca076f087180)
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 
189b6b563cSPaul Mackerras #include <stdarg.h>
199b6b563cSPaul Mackerras #include <linux/kernel.h>
209b6b563cSPaul Mackerras #include <linux/string.h>
219b6b563cSPaul Mackerras #include <linux/init.h>
229b6b563cSPaul Mackerras #include <linux/threads.h>
239b6b563cSPaul Mackerras #include <linux/spinlock.h>
249b6b563cSPaul Mackerras #include <linux/types.h>
259b6b563cSPaul Mackerras #include <linux/pci.h>
269b6b563cSPaul Mackerras #include <linux/proc_fs.h>
279b6b563cSPaul Mackerras #include <linux/stringify.h>
289b6b563cSPaul Mackerras #include <linux/delay.h>
299b6b563cSPaul Mackerras #include <linux/initrd.h>
309b6b563cSPaul Mackerras #include <linux/bitops.h>
319b6b563cSPaul Mackerras #include <asm/prom.h>
329b6b563cSPaul Mackerras #include <asm/rtas.h>
339b6b563cSPaul Mackerras #include <asm/page.h>
349b6b563cSPaul Mackerras #include <asm/processor.h>
359b6b563cSPaul Mackerras #include <asm/irq.h>
369b6b563cSPaul Mackerras #include <asm/io.h>
379b6b563cSPaul Mackerras #include <asm/smp.h>
389b6b563cSPaul Mackerras #include <asm/mmu.h>
399b6b563cSPaul Mackerras #include <asm/pgtable.h>
409b6b563cSPaul Mackerras #include <asm/pci.h>
419b6b563cSPaul Mackerras #include <asm/iommu.h>
429b6b563cSPaul Mackerras #include <asm/btext.h>
439b6b563cSPaul Mackerras #include <asm/sections.h>
449b6b563cSPaul Mackerras #include <asm/machdep.h>
4527f44888SBenjamin Herrenschmidt #include <asm/opal.h>
469b6b563cSPaul Mackerras 
479b6b563cSPaul Mackerras #include <linux/linux_logo.h>
489b6b563cSPaul Mackerras 
499b6b563cSPaul Mackerras /*
509b6b563cSPaul Mackerras  * Eventually bump that one up
519b6b563cSPaul Mackerras  */
529b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE	0x100000
539b6b563cSPaul Mackerras 
549b6b563cSPaul Mackerras /*
559b6b563cSPaul Mackerras  * This is the size of the local memory reserve map that gets copied
569b6b563cSPaul Mackerras  * into the boot params passed to the kernel. That size is totally
579b6b563cSPaul Mackerras  * flexible as the kernel just reads the list until it encounters an
589b6b563cSPaul Mackerras  * entry with size 0, so it can be changed without breaking binary
599b6b563cSPaul Mackerras  * compatibility
609b6b563cSPaul Mackerras  */
619b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE	8
629b6b563cSPaul Mackerras 
639b6b563cSPaul Mackerras /*
649b6b563cSPaul Mackerras  * prom_init() is called very early on, before the kernel text
659b6b563cSPaul Mackerras  * and data have been mapped to KERNELBASE.  At this point the code
669b6b563cSPaul Mackerras  * is running at whatever address it has been loaded at.
679b6b563cSPaul Mackerras  * On ppc32 we compile with -mrelocatable, which means that references
689b6b563cSPaul Mackerras  * to extern and static variables get relocated automatically.
695ac47f7aSAnton Blanchard  * ppc64 objects are always relocatable, we just need to relocate the
705ac47f7aSAnton Blanchard  * TOC.
719b6b563cSPaul Mackerras  *
729b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
739b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
749b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
759b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
769b6b563cSPaul Mackerras  *
779b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
789b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
799b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
809b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
819b6b563cSPaul Mackerras  */
825ac47f7aSAnton Blanchard #define ADDR(x)		(u32)(unsigned long)(x)
835ac47f7aSAnton Blanchard 
849b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
85a23414beSPaul Mackerras #define OF_WORKAROUNDS	0
869b6b563cSPaul Mackerras #else
87a23414beSPaul Mackerras #define OF_WORKAROUNDS	of_workarounds
88a23414beSPaul Mackerras int of_workarounds;
899b6b563cSPaul Mackerras #endif
909b6b563cSPaul Mackerras 
91a23414beSPaul Mackerras #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
92a23414beSPaul Mackerras #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
93a23414beSPaul Mackerras 
949b6b563cSPaul Mackerras #define PROM_BUG() do {						\
959b6b563cSPaul Mackerras         prom_printf("kernel BUG at %s line 0x%x!\n",		\
965827d416SAnton Blanchard 		    __FILE__, __LINE__);			\
979b6b563cSPaul Mackerras         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
989b6b563cSPaul Mackerras } while (0)
999b6b563cSPaul Mackerras 
1009b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1019b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1029b6b563cSPaul Mackerras #else
1039b6b563cSPaul Mackerras #define prom_debug(x...)
1049b6b563cSPaul Mackerras #endif
1059b6b563cSPaul Mackerras 
1069b6b563cSPaul Mackerras 
1079b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1089b6b563cSPaul Mackerras 
1099b6b563cSPaul Mackerras struct prom_args {
110493adffcSBenjamin Herrenschmidt         __be32 service;
111493adffcSBenjamin Herrenschmidt         __be32 nargs;
112493adffcSBenjamin Herrenschmidt         __be32 nret;
113493adffcSBenjamin Herrenschmidt         __be32 args[10];
1149b6b563cSPaul Mackerras };
1159b6b563cSPaul Mackerras 
1169b6b563cSPaul Mackerras struct prom_t {
1179b6b563cSPaul Mackerras 	ihandle root;
118a23414beSPaul Mackerras 	phandle chosen;
1199b6b563cSPaul Mackerras 	int cpu;
1209b6b563cSPaul Mackerras 	ihandle stdout;
121a575b807SPaul Mackerras 	ihandle mmumap;
122a23414beSPaul Mackerras 	ihandle memory;
1239b6b563cSPaul Mackerras };
1249b6b563cSPaul Mackerras 
1259b6b563cSPaul Mackerras struct mem_map_entry {
126493adffcSBenjamin Herrenschmidt 	__be64	base;
127493adffcSBenjamin Herrenschmidt 	__be64	size;
1289b6b563cSPaul Mackerras };
1299b6b563cSPaul Mackerras 
130493adffcSBenjamin Herrenschmidt typedef __be32 cell_t;
1319b6b563cSPaul Mackerras 
1326e35d5daSBenjamin Herrenschmidt extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
1336e35d5daSBenjamin Herrenschmidt 		    unsigned long r6, unsigned long r7, unsigned long r8,
1346e35d5daSBenjamin Herrenschmidt 		    unsigned long r9);
1359b6b563cSPaul Mackerras 
1369b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
137c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1389b6b563cSPaul Mackerras #else
139c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1409b6b563cSPaul Mackerras {
141c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1429b6b563cSPaul Mackerras }
1439b6b563cSPaul Mackerras #endif
1449b6b563cSPaul Mackerras 
1459b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1469b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1479b6b563cSPaul Mackerras 
1489b6b563cSPaul Mackerras /* prom structure */
1499b6b563cSPaul Mackerras static struct prom_t __initdata prom;
1509b6b563cSPaul Mackerras 
1519b6b563cSPaul Mackerras static unsigned long prom_entry __initdata;
1529b6b563cSPaul Mackerras 
1539b6b563cSPaul Mackerras #define PROM_SCRATCH_SIZE 256
1549b6b563cSPaul Mackerras 
1559b6b563cSPaul Mackerras static char __initdata of_stdout_device[256];
1569b6b563cSPaul Mackerras static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
1579b6b563cSPaul Mackerras 
1589b6b563cSPaul Mackerras static unsigned long __initdata dt_header_start;
1599b6b563cSPaul Mackerras static unsigned long __initdata dt_struct_start, dt_struct_end;
1609b6b563cSPaul Mackerras static unsigned long __initdata dt_string_start, dt_string_end;
1619b6b563cSPaul Mackerras 
1629b6b563cSPaul Mackerras static unsigned long __initdata prom_initrd_start, prom_initrd_end;
1639b6b563cSPaul Mackerras 
1649b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
165165785e5SJeremy Kerr static int __initdata prom_iommu_force_on;
166165785e5SJeremy Kerr static int __initdata prom_iommu_off;
1679b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_start;
1689b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_end;
1699b6b563cSPaul Mackerras #endif
1709b6b563cSPaul Mackerras 
171e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
172e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
173e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
174e8222502SBenjamin Herrenschmidt  */
175e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
176e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
177e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
178e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
179e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
18027f44888SBenjamin Herrenschmidt #define PLATFORM_OPAL		0x0600
181e8222502SBenjamin Herrenschmidt 
1829b6b563cSPaul Mackerras static int __initdata of_platform;
1839b6b563cSPaul Mackerras 
1849b6b563cSPaul Mackerras static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
1859b6b563cSPaul Mackerras 
186cf68787bSBenjamin Krill static unsigned long __initdata prom_memory_limit;
187cf68787bSBenjamin Krill 
1889b6b563cSPaul Mackerras static unsigned long __initdata alloc_top;
1899b6b563cSPaul Mackerras static unsigned long __initdata alloc_top_high;
1909b6b563cSPaul Mackerras static unsigned long __initdata alloc_bottom;
1919b6b563cSPaul Mackerras static unsigned long __initdata rmo_top;
1929b6b563cSPaul Mackerras static unsigned long __initdata ram_top;
1939b6b563cSPaul Mackerras 
1949b6b563cSPaul Mackerras static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
1959b6b563cSPaul Mackerras static int __initdata mem_reserve_cnt;
1969b6b563cSPaul Mackerras 
1979b6b563cSPaul Mackerras static cell_t __initdata regbuf[1024];
1989b6b563cSPaul Mackerras 
199dbe78b40SBenjamin Herrenschmidt static bool rtas_has_query_cpu_stopped;
200dbe78b40SBenjamin Herrenschmidt 
2019b6b563cSPaul Mackerras 
2029b6b563cSPaul Mackerras /*
2039b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2049b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2059b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2069b6b563cSPaul Mackerras  * it is valid
2079b6b563cSPaul Mackerras  */
2089b6b563cSPaul Mackerras 
2099b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2109b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2119b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2129b6b563cSPaul Mackerras 
2139b6b563cSPaul Mackerras 
2149b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
2159b6b563cSPaul Mackerras  * firmware.
2169b6b563cSPaul Mackerras  */
2179b6b563cSPaul Mackerras 
2189b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
2199b6b563cSPaul Mackerras {
2209b6b563cSPaul Mackerras 	int i;
2219b6b563cSPaul Mackerras 	struct prom_args args;
2229b6b563cSPaul Mackerras 	va_list list;
2239b6b563cSPaul Mackerras 
224493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
225493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
226493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
2279b6b563cSPaul Mackerras 
2289b6b563cSPaul Mackerras 	va_start(list, nret);
2299b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
230493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
2319b6b563cSPaul Mackerras 	va_end(list);
2329b6b563cSPaul Mackerras 
2339b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
2349b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
2359b6b563cSPaul Mackerras 
2365827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
237c4988820SPaul Mackerras 		return PROM_ERROR;
2389b6b563cSPaul Mackerras 
239493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
2409b6b563cSPaul Mackerras }
2419b6b563cSPaul Mackerras 
2429b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
2439b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
2449b6b563cSPaul Mackerras {
2459b6b563cSPaul Mackerras 	int i;
2469b6b563cSPaul Mackerras 	struct prom_args args;
2479b6b563cSPaul Mackerras 	va_list list;
2489b6b563cSPaul Mackerras 
249493adffcSBenjamin Herrenschmidt 	args.service = cpu_to_be32(ADDR(service));
250493adffcSBenjamin Herrenschmidt 	args.nargs = cpu_to_be32(nargs);
251493adffcSBenjamin Herrenschmidt 	args.nret = cpu_to_be32(nret);
2529b6b563cSPaul Mackerras 
2539b6b563cSPaul Mackerras 	va_start(list, rets);
2549b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
255493adffcSBenjamin Herrenschmidt 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
2569b6b563cSPaul Mackerras 	va_end(list);
2579b6b563cSPaul Mackerras 
2589b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
259ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
2609b6b563cSPaul Mackerras 
2615827d416SAnton Blanchard 	if (enter_prom(&args, prom_entry) < 0)
262c4988820SPaul Mackerras 		return PROM_ERROR;
2639b6b563cSPaul Mackerras 
2649b6b563cSPaul Mackerras 	if (rets != NULL)
2659b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
266493adffcSBenjamin Herrenschmidt 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
2679b6b563cSPaul Mackerras 
268493adffcSBenjamin Herrenschmidt 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
2699b6b563cSPaul Mackerras }
2709b6b563cSPaul Mackerras 
2719b6b563cSPaul Mackerras 
2729b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
2739b6b563cSPaul Mackerras {
2749b6b563cSPaul Mackerras 	const char *p, *q;
2759b6b563cSPaul Mackerras 
2765827d416SAnton Blanchard 	if (prom.stdout == 0)
2779b6b563cSPaul Mackerras 		return;
2789b6b563cSPaul Mackerras 
2799b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
2809b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
2819b6b563cSPaul Mackerras 			;
2829b6b563cSPaul Mackerras 		if (q > p)
2835827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
2849b6b563cSPaul Mackerras 		if (*q == 0)
2859b6b563cSPaul Mackerras 			break;
2869b6b563cSPaul Mackerras 		++q;
2875827d416SAnton Blanchard 		call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
2889b6b563cSPaul Mackerras 	}
2899b6b563cSPaul Mackerras }
2909b6b563cSPaul Mackerras 
2919b6b563cSPaul Mackerras 
2929b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
2939b6b563cSPaul Mackerras {
2949b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
2959b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
2969b6b563cSPaul Mackerras 
2979b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
2989b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
2999b6b563cSPaul Mackerras 		if (buf[i] > '9')
3009b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
3019b6b563cSPaul Mackerras 		val >>= 4;
3029b6b563cSPaul Mackerras 	}
3039b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
3045827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf, nibbles);
3059b6b563cSPaul Mackerras }
3069b6b563cSPaul Mackerras 
3072c48a7d6SMichael Neuling /* max number of decimal digits in an unsigned long */
3082c48a7d6SMichael Neuling #define UL_DIGITS 21
3092c48a7d6SMichael Neuling static void __init prom_print_dec(unsigned long val)
3102c48a7d6SMichael Neuling {
3112c48a7d6SMichael Neuling 	int i, size;
3122c48a7d6SMichael Neuling 	char buf[UL_DIGITS+1];
3132c48a7d6SMichael Neuling 
3142c48a7d6SMichael Neuling 	for (i = UL_DIGITS-1; i >= 0;  i--) {
3152c48a7d6SMichael Neuling 		buf[i] = (val % 10) + '0';
3162c48a7d6SMichael Neuling 		val = val/10;
3172c48a7d6SMichael Neuling 		if (val == 0)
3182c48a7d6SMichael Neuling 			break;
3192c48a7d6SMichael Neuling 	}
3202c48a7d6SMichael Neuling 	/* shift stuff down */
3212c48a7d6SMichael Neuling 	size = UL_DIGITS - i;
3225827d416SAnton Blanchard 	call_prom("write", 3, 1, prom.stdout, buf+i, size);
3232c48a7d6SMichael Neuling }
3249b6b563cSPaul Mackerras 
3259b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
3269b6b563cSPaul Mackerras {
3279b6b563cSPaul Mackerras 	const char *p, *q, *s;
3289b6b563cSPaul Mackerras 	va_list args;
3299b6b563cSPaul Mackerras 	unsigned long v;
330af277149SBenjamin Herrenschmidt 	long vs;
3319b6b563cSPaul Mackerras 
3329b6b563cSPaul Mackerras 	va_start(args, format);
3339b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
3349b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
3359b6b563cSPaul Mackerras 			;
3369b6b563cSPaul Mackerras 		if (q > p)
3375827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout, p, q - p);
3389b6b563cSPaul Mackerras 		if (*q == 0)
3399b6b563cSPaul Mackerras 			break;
3409b6b563cSPaul Mackerras 		if (*q == '\n') {
3419b6b563cSPaul Mackerras 			++q;
3425827d416SAnton Blanchard 			call_prom("write", 3, 1, prom.stdout,
3439b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
3449b6b563cSPaul Mackerras 			continue;
3459b6b563cSPaul Mackerras 		}
3469b6b563cSPaul Mackerras 		++q;
3479b6b563cSPaul Mackerras 		if (*q == 0)
3489b6b563cSPaul Mackerras 			break;
3499b6b563cSPaul Mackerras 		switch (*q) {
3509b6b563cSPaul Mackerras 		case 's':
3519b6b563cSPaul Mackerras 			++q;
3529b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
3539b6b563cSPaul Mackerras 			prom_print(s);
3549b6b563cSPaul Mackerras 			break;
3559b6b563cSPaul Mackerras 		case 'x':
3569b6b563cSPaul Mackerras 			++q;
3579b6b563cSPaul Mackerras 			v = va_arg(args, unsigned long);
3589b6b563cSPaul Mackerras 			prom_print_hex(v);
3599b6b563cSPaul Mackerras 			break;
360af277149SBenjamin Herrenschmidt 		case 'd':
361af277149SBenjamin Herrenschmidt 			++q;
362af277149SBenjamin Herrenschmidt 			vs = va_arg(args, int);
363af277149SBenjamin Herrenschmidt 			if (vs < 0) {
3645827d416SAnton Blanchard 				prom_print("-");
365af277149SBenjamin Herrenschmidt 				vs = -vs;
366af277149SBenjamin Herrenschmidt 			}
367af277149SBenjamin Herrenschmidt 			prom_print_dec(vs);
368af277149SBenjamin Herrenschmidt 			break;
3692c48a7d6SMichael Neuling 		case 'l':
3702c48a7d6SMichael Neuling 			++q;
371af277149SBenjamin Herrenschmidt 			if (*q == 0)
372af277149SBenjamin Herrenschmidt 				break;
373af277149SBenjamin Herrenschmidt 			else if (*q == 'x') {
374af277149SBenjamin Herrenschmidt 				++q;
375af277149SBenjamin Herrenschmidt 				v = va_arg(args, unsigned long);
376af277149SBenjamin Herrenschmidt 				prom_print_hex(v);
377af277149SBenjamin Herrenschmidt 			} else if (*q == 'u') { /* '%lu' */
3782c48a7d6SMichael Neuling 				++q;
3792c48a7d6SMichael Neuling 				v = va_arg(args, unsigned long);
3802c48a7d6SMichael Neuling 				prom_print_dec(v);
381af277149SBenjamin Herrenschmidt 			} else if (*q == 'd') { /* %ld */
382af277149SBenjamin Herrenschmidt 				++q;
383af277149SBenjamin Herrenschmidt 				vs = va_arg(args, long);
384af277149SBenjamin Herrenschmidt 				if (vs < 0) {
3855827d416SAnton Blanchard 					prom_print("-");
386af277149SBenjamin Herrenschmidt 					vs = -vs;
387af277149SBenjamin Herrenschmidt 				}
388af277149SBenjamin Herrenschmidt 				prom_print_dec(vs);
3892c48a7d6SMichael Neuling 			}
3902c48a7d6SMichael Neuling 			break;
3919b6b563cSPaul Mackerras 		}
3929b6b563cSPaul Mackerras 	}
3939b6b563cSPaul Mackerras }
3949b6b563cSPaul Mackerras 
3959b6b563cSPaul Mackerras 
396a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
397a575b807SPaul Mackerras 				unsigned long align)
398a575b807SPaul Mackerras {
399a575b807SPaul Mackerras 
400a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
401a23414beSPaul Mackerras 		/*
402a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
403a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
404a23414beSPaul Mackerras 		 */
405a23414beSPaul Mackerras 		int ret;
406a23414beSPaul Mackerras 		prom_arg_t result;
407a23414beSPaul Mackerras 
408a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
4095827d416SAnton Blanchard 				    ADDR("claim"), prom.memory,
410a23414beSPaul Mackerras 				    align, size, virt);
411a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
412a23414beSPaul Mackerras 			return -1;
413a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
4145827d416SAnton Blanchard 				    ADDR("claim"), prom.mmumap,
415a23414beSPaul Mackerras 				    align, size, virt);
416a23414beSPaul Mackerras 		if (ret != 0) {
417a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
4185827d416SAnton Blanchard 				  prom.memory, size, virt);
419a23414beSPaul Mackerras 			return -1;
420a23414beSPaul Mackerras 		}
421a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
422a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
4235827d416SAnton Blanchard 			  ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
424a23414beSPaul Mackerras 		return virt;
425a23414beSPaul Mackerras 	}
426a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
427a23414beSPaul Mackerras 			 (prom_arg_t)align);
428a575b807SPaul Mackerras }
429a575b807SPaul Mackerras 
4309b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
4319b6b563cSPaul Mackerras {
4329b6b563cSPaul Mackerras 	prom_print(reason);
433add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
434add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
4355827d416SAnton Blanchard 	if (of_platform == PLATFORM_POWERMAC)
436add60ef3SOlaf Hering 		asm("trap\n");
437add60ef3SOlaf Hering 
4381d9a4731SStephen Rothwell 	/* ToDo: should put up an SRC here on pSeries */
4399b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
4409b6b563cSPaul Mackerras 
4419b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
4429b6b563cSPaul Mackerras 		;
4439b6b563cSPaul Mackerras }
4449b6b563cSPaul Mackerras 
4459b6b563cSPaul Mackerras 
4469b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
4479b6b563cSPaul Mackerras {
4489b6b563cSPaul Mackerras 	phandle node;
4499b6b563cSPaul Mackerras 
4509b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
4519b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
4529b6b563cSPaul Mackerras 		return 1;
4539b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4549b6b563cSPaul Mackerras 		return 1;
4559b6b563cSPaul Mackerras 	for (;;) {
4569b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
4579b6b563cSPaul Mackerras 			return 0;
4589b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4599b6b563cSPaul Mackerras 			return 1;
4609b6b563cSPaul Mackerras 	}
4619b6b563cSPaul Mackerras }
4629b6b563cSPaul Mackerras 
46321fe3301SBenjamin Herrenschmidt static int inline prom_getprop(phandle node, const char *pname,
4649b6b563cSPaul Mackerras 			       void *value, size_t valuelen)
4659b6b563cSPaul Mackerras {
4669b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
4679b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
4689b6b563cSPaul Mackerras }
4699b6b563cSPaul Mackerras 
47021fe3301SBenjamin Herrenschmidt static int inline prom_getproplen(phandle node, const char *pname)
4719b6b563cSPaul Mackerras {
4729b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
4739b6b563cSPaul Mackerras }
4749b6b563cSPaul Mackerras 
475a23414beSPaul Mackerras static void add_string(char **str, const char *q)
4769b6b563cSPaul Mackerras {
477a23414beSPaul Mackerras 	char *p = *str;
478a23414beSPaul Mackerras 
479a23414beSPaul Mackerras 	while (*q)
480a23414beSPaul Mackerras 		*p++ = *q++;
481a23414beSPaul Mackerras 	*p++ = ' ';
482a23414beSPaul Mackerras 	*str = p;
483a23414beSPaul Mackerras }
484a23414beSPaul Mackerras 
485a23414beSPaul Mackerras static char *tohex(unsigned int x)
486a23414beSPaul Mackerras {
487a23414beSPaul Mackerras 	static char digits[] = "0123456789abcdef";
488a23414beSPaul Mackerras 	static char result[9];
489a23414beSPaul Mackerras 	int i;
490a23414beSPaul Mackerras 
491a23414beSPaul Mackerras 	result[8] = 0;
492a23414beSPaul Mackerras 	i = 8;
493a23414beSPaul Mackerras 	do {
494a23414beSPaul Mackerras 		--i;
495a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
496a23414beSPaul Mackerras 		x >>= 4;
497a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
498a23414beSPaul Mackerras 	return &result[i];
499a23414beSPaul Mackerras }
500a23414beSPaul Mackerras 
501a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
502a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
503a23414beSPaul Mackerras {
504a23414beSPaul Mackerras 	char cmd[256], *p;
505a23414beSPaul Mackerras 
506a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
5079b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
5089b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
509a23414beSPaul Mackerras 
510a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
511a23414beSPaul Mackerras 	p = cmd;
512a23414beSPaul Mackerras 	add_string(&p, "dev");
513a23414beSPaul Mackerras 	add_string(&p, nodename);
514a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
515a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
516a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
5175827d416SAnton Blanchard 	add_string(&p, tohex(strlen(pname)));
518a23414beSPaul Mackerras 	add_string(&p, "property");
519a23414beSPaul Mackerras 	*p = 0;
520a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
5219b6b563cSPaul Mackerras }
5229b6b563cSPaul Mackerras 
5235827d416SAnton Blanchard /* We can't use the standard versions because of relocation headaches. */
524cf68787bSBenjamin Krill #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
525cf68787bSBenjamin Krill 			 || ('a' <= (c) && (c) <= 'f') \
526cf68787bSBenjamin Krill 			 || ('A' <= (c) && (c) <= 'F'))
527cf68787bSBenjamin Krill 
528cf68787bSBenjamin Krill #define isdigit(c)	('0' <= (c) && (c) <= '9')
529cf68787bSBenjamin Krill #define islower(c)	('a' <= (c) && (c) <= 'z')
530cf68787bSBenjamin Krill #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
531cf68787bSBenjamin Krill 
532493adffcSBenjamin Herrenschmidt static unsigned long prom_strtoul(const char *cp, const char **endp)
533cf68787bSBenjamin Krill {
534cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
535cf68787bSBenjamin Krill 
536cf68787bSBenjamin Krill 	if (*cp == '0') {
537cf68787bSBenjamin Krill 		base = 8;
538cf68787bSBenjamin Krill 		cp++;
539cf68787bSBenjamin Krill 		if (toupper(*cp) == 'X') {
540cf68787bSBenjamin Krill 			cp++;
541cf68787bSBenjamin Krill 			base = 16;
542cf68787bSBenjamin Krill 		}
543cf68787bSBenjamin Krill 	}
544cf68787bSBenjamin Krill 
545cf68787bSBenjamin Krill 	while (isxdigit(*cp) &&
546cf68787bSBenjamin Krill 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
547cf68787bSBenjamin Krill 		result = result * base + value;
548cf68787bSBenjamin Krill 		cp++;
549cf68787bSBenjamin Krill 	}
550cf68787bSBenjamin Krill 
551cf68787bSBenjamin Krill 	if (endp)
552cf68787bSBenjamin Krill 		*endp = cp;
553cf68787bSBenjamin Krill 
554cf68787bSBenjamin Krill 	return result;
555cf68787bSBenjamin Krill }
556cf68787bSBenjamin Krill 
557493adffcSBenjamin Herrenschmidt static unsigned long prom_memparse(const char *ptr, const char **retptr)
558cf68787bSBenjamin Krill {
559cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
560cf68787bSBenjamin Krill 	int shift = 0;
561cf68787bSBenjamin Krill 
562cf68787bSBenjamin Krill 	/*
563cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
564cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
565cf68787bSBenjamin Krill 	 * the address we're linked at.
566cf68787bSBenjamin Krill 	 */
567cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
568cf68787bSBenjamin Krill 		shift = 30;
569cf68787bSBenjamin Krill 
570cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
571cf68787bSBenjamin Krill 		shift = 20;
572cf68787bSBenjamin Krill 
573cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
574cf68787bSBenjamin Krill 		shift = 10;
575cf68787bSBenjamin Krill 
576cf68787bSBenjamin Krill 	if (shift) {
577cf68787bSBenjamin Krill 		ret <<= shift;
578cf68787bSBenjamin Krill 		(*retptr)++;
579cf68787bSBenjamin Krill 	}
580cf68787bSBenjamin Krill 
581cf68787bSBenjamin Krill 	return ret;
582cf68787bSBenjamin Krill }
583cf68787bSBenjamin Krill 
5849b6b563cSPaul Mackerras /*
5859b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
5869b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
5879b6b563cSPaul Mackerras  */
5889b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
5899b6b563cSPaul Mackerras {
590cc5d0189SBenjamin Herrenschmidt 	const char *opt;
591cf68787bSBenjamin Krill 
592cc5d0189SBenjamin Herrenschmidt 	char *p;
5939b6b563cSPaul Mackerras 	int l = 0;
5949b6b563cSPaul Mackerras 
5955827d416SAnton Blanchard 	prom_cmd_line[0] = 0;
5965827d416SAnton Blanchard 	p = prom_cmd_line;
5975827d416SAnton Blanchard 	if ((long)prom.chosen > 0)
5985827d416SAnton Blanchard 		l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
5999b6b563cSPaul Mackerras #ifdef CONFIG_CMDLINE
6000e4aa9c2SAmos Waterland 	if (l <= 0 || p[0] == '\0') /* dbl check */
6015827d416SAnton Blanchard 		strlcpy(prom_cmd_line,
6025827d416SAnton Blanchard 			CONFIG_CMDLINE, sizeof(prom_cmd_line));
6039b6b563cSPaul Mackerras #endif /* CONFIG_CMDLINE */
6045827d416SAnton Blanchard 	prom_printf("command line: %s\n", prom_cmd_line);
6059b6b563cSPaul Mackerras 
6069b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
6075827d416SAnton Blanchard 	opt = strstr(prom_cmd_line, "iommu=");
6089b6b563cSPaul Mackerras 	if (opt) {
6099b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
6109b6b563cSPaul Mackerras 		opt += 6;
6119b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
6129b6b563cSPaul Mackerras 			opt++;
6135827d416SAnton Blanchard 		if (!strncmp(opt, "off", 3))
6145827d416SAnton Blanchard 			prom_iommu_off = 1;
6155827d416SAnton Blanchard 		else if (!strncmp(opt, "force", 5))
6165827d416SAnton Blanchard 			prom_iommu_force_on = 1;
6179b6b563cSPaul Mackerras 	}
6189b6b563cSPaul Mackerras #endif
6195827d416SAnton Blanchard 	opt = strstr(prom_cmd_line, "mem=");
620cf68787bSBenjamin Krill 	if (opt) {
621cf68787bSBenjamin Krill 		opt += 4;
6225827d416SAnton Blanchard 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
623cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
624cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
6255827d416SAnton Blanchard 		prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
626cf68787bSBenjamin Krill #endif
627cf68787bSBenjamin Krill 	}
6289b6b563cSPaul Mackerras }
6299b6b563cSPaul Mackerras 
63027f44888SBenjamin Herrenschmidt #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
6319b6b563cSPaul Mackerras /*
632530b5e14SNathan Fontenot  * The architecture vector has an array of PVR mask/value pairs,
633530b5e14SNathan Fontenot  * followed by # option vectors - 1, followed by the option vectors.
634530b5e14SNathan Fontenot  *
635530b5e14SNathan Fontenot  * See prom.h for the definition of the bits specified in the
636530b5e14SNathan Fontenot  * architecture vector.
637f709bfacSPaul Mackerras  *
638f709bfacSPaul Mackerras  * Because the description vector contains a mix of byte and word
639f709bfacSPaul Mackerras  * values, we declare it as an unsigned char array, and use this
640f709bfacSPaul Mackerras  * macro to put word values in.
641f709bfacSPaul Mackerras  */
642f709bfacSPaul Mackerras #define W(x)	((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
643f709bfacSPaul Mackerras 		((x) >> 8) & 0xff, (x) & 0xff
644f709bfacSPaul Mackerras 
645530b5e14SNathan Fontenot unsigned char ibm_architecture_vec[] = {
646f709bfacSPaul Mackerras 	W(0xfffe0000), W(0x003a0000),	/* POWER5/POWER5+ */
64703054d51SAnton Blanchard 	W(0xffff0000), W(0x003e0000),	/* POWER6 */
648e952e6c4SMichael Neuling 	W(0xffff0000), W(0x003f0000),	/* POWER7 */
64933959f88SMichael Neuling 	W(0xffff0000), W(0x004b0000),	/* POWER8E */
65033959f88SMichael Neuling 	W(0xffff0000), W(0x004d0000),	/* POWER8 */
651df77c799SMichael Neuling 	W(0xffffffff), W(0x0f000004),	/* all 2.07-compliant */
6520cb99013SJoel Schopp 	W(0xffffffff), W(0x0f000003),	/* all 2.06-compliant */
6530efbc18aSPaul Mackerras 	W(0xffffffff), W(0x0f000002),	/* all 2.05-compliant */
654f709bfacSPaul Mackerras 	W(0xfffffffe), W(0x0f000001),	/* all 2.04-compliant and earlier */
65528bb9ee1Sjschopp@austin.ibm.com 	6 - 1,				/* 6 option vectors */
656f709bfacSPaul Mackerras 
657f709bfacSPaul Mackerras 	/* option vector 1: processor architectures supported */
65811e9ed43SWill Schmidt 	3 - 2,				/* length */
659f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
660f709bfacSPaul Mackerras 	OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
661df77c799SMichael Neuling 	OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
662f709bfacSPaul Mackerras 
663f709bfacSPaul Mackerras 	/* option vector 2: Open Firmware options supported */
66411e9ed43SWill Schmidt 	34 - 2,				/* length */
665f709bfacSPaul Mackerras 	OV2_REAL_MODE,
666f709bfacSPaul Mackerras 	0, 0,
667f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_base */
668f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_size */
669f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_base */
670f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_size */
671f709bfacSPaul Mackerras 	W(0xffffffff),			/* load_base */
67233392640SAnton Blanchard 	W(256),				/* 256MB min RMA */
673f709bfacSPaul Mackerras 	W(0xffffffff),			/* full client load */
674f709bfacSPaul Mackerras 	0,				/* min RMA percentage of total RAM */
675f709bfacSPaul Mackerras 	48,				/* max log_2(hash table size) */
676f709bfacSPaul Mackerras 
677f709bfacSPaul Mackerras 	/* option vector 3: processor options supported */
67811e9ed43SWill Schmidt 	3 - 2,				/* length */
679f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
680974a76f5SPaul Mackerras 	OV3_FP | OV3_VMX | OV3_DFP,
681f709bfacSPaul Mackerras 
682f709bfacSPaul Mackerras 	/* option vector 4: IBM PAPR implementation */
683404e32e4SRobert Jennings 	3 - 2,				/* length */
684f709bfacSPaul Mackerras 	0,				/* don't halt */
685404e32e4SRobert Jennings 	OV4_MIN_ENT_CAP,		/* minimum VP entitled capacity */
686f709bfacSPaul Mackerras 
687f709bfacSPaul Mackerras 	/* option vector 5: PAPR/OF options */
688df77c799SMichael Neuling 	19 - 2,				/* length */
689f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
690f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
691f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
692f0ff7eb4SNathan Fontenot #ifdef CONFIG_PCI_MSI
693f0ff7eb4SNathan Fontenot 	/* PCIe/MSI support.  Without MSI full PCIe is not supported */
694f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_MSI),
695f0ff7eb4SNathan Fontenot #else
6968391e42aSNathan Fontenot 	0,
697f0ff7eb4SNathan Fontenot #endif
698f0ff7eb4SNathan Fontenot 	0,
699f0ff7eb4SNathan Fontenot #ifdef CONFIG_PPC_SMLPAR
700f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
701f0ff7eb4SNathan Fontenot #else
702f0ff7eb4SNathan Fontenot 	0,
703f0ff7eb4SNathan Fontenot #endif
7041b1218d3SNathan Fontenot 	OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
70528bb9ee1Sjschopp@austin.ibm.com 	0,
70628bb9ee1Sjschopp@austin.ibm.com 	0,
70728bb9ee1Sjschopp@austin.ibm.com 	0,
708efec959fSBenjamin Herrenschmidt 	/* WARNING: The offset of the "number of cores" field below
709efec959fSBenjamin Herrenschmidt 	 * must match by the macro below. Update the definition if
710efec959fSBenjamin Herrenschmidt 	 * the structure layout changes.
711efec959fSBenjamin Herrenschmidt 	 */
71233959f88SMichael Neuling #define IBM_ARCH_VEC_NRCORES_OFFSET	125
71328bb9ee1Sjschopp@austin.ibm.com 	W(NR_CPUS),			/* number of cores supported */
714828d2b59SKent Yoder 	0,
715828d2b59SKent Yoder 	0,
716828d2b59SKent Yoder 	0,
717828d2b59SKent Yoder 	0,
718f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) |
719f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_PFO_HW_842),
720f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_SUB_PROCESSORS),
72128bb9ee1Sjschopp@austin.ibm.com 	/* option vector 6: IBM PAPR hints */
72228bb9ee1Sjschopp@austin.ibm.com 	4 - 2,				/* length */
72328bb9ee1Sjschopp@austin.ibm.com 	0,
72428bb9ee1Sjschopp@austin.ibm.com 	0,
72528bb9ee1Sjschopp@austin.ibm.com 	OV6_LINUX,
72628bb9ee1Sjschopp@austin.ibm.com 
727f709bfacSPaul Mackerras };
728f709bfacSPaul Mackerras 
729493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
730493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
7319b6b563cSPaul Mackerras static struct fake_elf {
7329b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
7339b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
7349b6b563cSPaul Mackerras 	struct chrpnote {
7359b6b563cSPaul Mackerras 		u32	namesz;
7369b6b563cSPaul Mackerras 		u32	descsz;
7379b6b563cSPaul Mackerras 		u32	type;
7389b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
7399b6b563cSPaul Mackerras 		struct chrpdesc {
7409b6b563cSPaul Mackerras 			u32	real_mode;
7419b6b563cSPaul Mackerras 			u32	real_base;
7429b6b563cSPaul Mackerras 			u32	real_size;
7439b6b563cSPaul Mackerras 			u32	virt_base;
7449b6b563cSPaul Mackerras 			u32	virt_size;
7459b6b563cSPaul Mackerras 			u32	load_base;
7469b6b563cSPaul Mackerras 		} chrpdesc;
7479b6b563cSPaul Mackerras 	} chrpnote;
7489b6b563cSPaul Mackerras 	struct rpanote {
7499b6b563cSPaul Mackerras 		u32	namesz;
7509b6b563cSPaul Mackerras 		u32	descsz;
7519b6b563cSPaul Mackerras 		u32	type;
7529b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
7539b6b563cSPaul Mackerras 		struct rpadesc {
7549b6b563cSPaul Mackerras 			u32	lpar_affinity;
7559b6b563cSPaul Mackerras 			u32	min_rmo_size;
7569b6b563cSPaul Mackerras 			u32	min_rmo_percent;
7579b6b563cSPaul Mackerras 			u32	max_pft_size;
7589b6b563cSPaul Mackerras 			u32	splpar;
7599b6b563cSPaul Mackerras 			u32	min_load;
7609b6b563cSPaul Mackerras 			u32	new_mem_def;
7619b6b563cSPaul Mackerras 			u32	ignore_me;
7629b6b563cSPaul Mackerras 		} rpadesc;
7639b6b563cSPaul Mackerras 	} rpanote;
7645663a123SPaul Mackerras } fake_elf = {
7659b6b563cSPaul Mackerras 	.elfhdr = {
7669b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
7679b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
7689b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
7699b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
7709b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
7719b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
7729b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
7739b6b563cSPaul Mackerras 		.e_phnum = 2
7749b6b563cSPaul Mackerras 	},
7759b6b563cSPaul Mackerras 	.phdr = {
7769b6b563cSPaul Mackerras 		[0] = {
7779b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
7789b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
7799b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
7809b6b563cSPaul Mackerras 		}, [1] = {
7819b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
7829b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
7839b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
7849b6b563cSPaul Mackerras 		}
7859b6b563cSPaul Mackerras 	},
7869b6b563cSPaul Mackerras 	.chrpnote = {
7879b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
7889b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
7899b6b563cSPaul Mackerras 		.type = 0x1275,
7909b6b563cSPaul Mackerras 		.name = "PowerPC",
7919b6b563cSPaul Mackerras 		.chrpdesc = {
7929b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
7939b6b563cSPaul Mackerras 			.real_base = ~0U,
7949b6b563cSPaul Mackerras 			.real_size = ~0U,
7959b6b563cSPaul Mackerras 			.virt_base = ~0U,
7969b6b563cSPaul Mackerras 			.virt_size = ~0U,
7979b6b563cSPaul Mackerras 			.load_base = ~0U
7989b6b563cSPaul Mackerras 		},
7999b6b563cSPaul Mackerras 	},
8009b6b563cSPaul Mackerras 	.rpanote = {
8019b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
8029b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
8039b6b563cSPaul Mackerras 		.type = 0x12759999,
8049b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
8059b6b563cSPaul Mackerras 		.rpadesc = {
8065663a123SPaul Mackerras 			.lpar_affinity = 0,
8075663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
8089b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
8095663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
8109b6b563cSPaul Mackerras 			.splpar = 1,
8119b6b563cSPaul Mackerras 			.min_load = ~0U,
8125663a123SPaul Mackerras 			.new_mem_def = 0
8139b6b563cSPaul Mackerras 		}
8149b6b563cSPaul Mackerras 	}
8159b6b563cSPaul Mackerras };
816493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
8179b6b563cSPaul Mackerras 
818efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
819efec959fSBenjamin Herrenschmidt {
820efec959fSBenjamin Herrenschmidt 	phandle node;
821efec959fSBenjamin Herrenschmidt 	char type[64];
822efec959fSBenjamin Herrenschmidt 	unsigned int plen;
823efec959fSBenjamin Herrenschmidt 
824efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
825efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
826efec959fSBenjamin Herrenschmidt 		type[0] = 0;
827efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
828efec959fSBenjamin Herrenschmidt 
8295827d416SAnton Blanchard 		if (strcmp(type, "cpu"))
830efec959fSBenjamin Herrenschmidt 			continue;
831efec959fSBenjamin Herrenschmidt 		/*
832efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
833efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
834efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
835efec959fSBenjamin Herrenschmidt 		 */
836efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
837efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
838efec959fSBenjamin Herrenschmidt 			break;
839efec959fSBenjamin Herrenschmidt 		plen >>= 2;
8402c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
841efec959fSBenjamin Herrenschmidt 
842efec959fSBenjamin Herrenschmidt 		/* Sanity check */
843efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
8442c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
845efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
846efec959fSBenjamin Herrenschmidt 			return 1;
847efec959fSBenjamin Herrenschmidt 		}
848efec959fSBenjamin Herrenschmidt 		return plen;
849efec959fSBenjamin Herrenschmidt 	}
850efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
851efec959fSBenjamin Herrenschmidt 
852efec959fSBenjamin Herrenschmidt 	return 1;
853efec959fSBenjamin Herrenschmidt 
854efec959fSBenjamin Herrenschmidt }
855efec959fSBenjamin Herrenschmidt 
856efec959fSBenjamin Herrenschmidt 
8579b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
8589b6b563cSPaul Mackerras {
859493adffcSBenjamin Herrenschmidt 	ihandle root;
860f709bfacSPaul Mackerras 	prom_arg_t ret;
861dbd0c5d5SLaurent Dufour 	u32 cores;
862dbd0c5d5SLaurent Dufour 	unsigned char *ptcores;
8639b6b563cSPaul Mackerras 
864f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
865f709bfacSPaul Mackerras 	if (root != 0) {
866efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
867efec959fSBenjamin Herrenschmidt 		 *
868efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
869efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
870efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
871efec959fSBenjamin Herrenschmidt 		 */
872dbd0c5d5SLaurent Dufour 
873dbd0c5d5SLaurent Dufour 		/* The core value may start at an odd address. If such a word
874dbd0c5d5SLaurent Dufour 		 * access is made at a cache line boundary, this leads to an
875dbd0c5d5SLaurent Dufour 		 * exception which may not be handled at this time.
876dbd0c5d5SLaurent Dufour 		 * Forcing a per byte access to avoid exception.
877dbd0c5d5SLaurent Dufour 		 */
878dbd0c5d5SLaurent Dufour 		ptcores = &ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET];
879dbd0c5d5SLaurent Dufour 		cores = 0;
880dbd0c5d5SLaurent Dufour 		cores |= ptcores[0] << 24;
881dbd0c5d5SLaurent Dufour 		cores |= ptcores[1] << 16;
882dbd0c5d5SLaurent Dufour 		cores |= ptcores[2] << 8;
883dbd0c5d5SLaurent Dufour 		cores |= ptcores[3];
884dbd0c5d5SLaurent Dufour 		if (cores != NR_CPUS) {
885efec959fSBenjamin Herrenschmidt 			prom_printf("WARNING ! "
8862c48a7d6SMichael Neuling 				    "ibm_architecture_vec structure inconsistent: %lu!\n",
887dbd0c5d5SLaurent Dufour 				    cores);
888efec959fSBenjamin Herrenschmidt 		} else {
889dbd0c5d5SLaurent Dufour 			cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
8902c48a7d6SMichael Neuling 			prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
891dbd0c5d5SLaurent Dufour 				    cores, NR_CPUS);
892dbd0c5d5SLaurent Dufour 			ptcores[0] = (cores >> 24) & 0xff;
893dbd0c5d5SLaurent Dufour 			ptcores[1] = (cores >> 16) & 0xff;
894dbd0c5d5SLaurent Dufour 			ptcores[2] = (cores >> 8) & 0xff;
895dbd0c5d5SLaurent Dufour 			ptcores[3] = cores & 0xff;
896efec959fSBenjamin Herrenschmidt 		}
897efec959fSBenjamin Herrenschmidt 
898f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
899049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
900f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
901f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
90233b74977SBenjamin Herrenschmidt 				  root,
903f709bfacSPaul Mackerras 				  ADDR(ibm_architecture_vec)) == 0) {
904f709bfacSPaul Mackerras 			/* the call exists... */
905f709bfacSPaul Mackerras 			if (ret)
9064da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
907f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
908f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
9094da727aeSAnton Blanchard 			prom_printf(" done\n");
910f709bfacSPaul Mackerras 			return;
911f709bfacSPaul Mackerras 		}
912f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
913049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
914f709bfacSPaul Mackerras 	}
915f709bfacSPaul Mackerras 
916493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
917493adffcSBenjamin Herrenschmidt 	{
918493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
919493adffcSBenjamin Herrenschmidt 
920f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
921493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
922493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
9239b6b563cSPaul Mackerras 		if (elfloader == 0) {
9249b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
9259b6b563cSPaul Mackerras 			return;
9269b6b563cSPaul Mackerras 		}
9279b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
9289b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
9299b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
9309b6b563cSPaul Mackerras 	}
931493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
932493adffcSBenjamin Herrenschmidt }
933493adffcSBenjamin Herrenschmidt #endif /* #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV) */
9349b6b563cSPaul Mackerras 
9359b6b563cSPaul Mackerras /*
9369b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
9379b6b563cSPaul Mackerras  *
9389b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
9399b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
9409b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
9419b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
9429b6b563cSPaul Mackerras  *  never happen.
9439b6b563cSPaul Mackerras  *
9449b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
9459b6b563cSPaul Mackerras  *  TCEs overlap
9469b6b563cSPaul Mackerras  *
9479b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
9489b6b563cSPaul Mackerras  *
9499b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
9509b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
9519b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
9529b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
9539b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
9549b6b563cSPaul Mackerras  *
9559b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
9569b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
9579b6b563cSPaul Mackerras  *  reserved.
9589b6b563cSPaul Mackerras  */
9599b6b563cSPaul Mackerras 
9609b6b563cSPaul Mackerras 
9619b6b563cSPaul Mackerras /*
9629b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
9639b6b563cSPaul Mackerras  *
9649b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
9659b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
9669b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
9679b6b563cSPaul Mackerras  */
9689b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
9699b6b563cSPaul Mackerras {
9705827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
9719b6b563cSPaul Mackerras 	unsigned long addr = 0;
9729b6b563cSPaul Mackerras 
973c4988820SPaul Mackerras 	if (align)
974c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
9759b6b563cSPaul Mackerras 	prom_debug("alloc_up(%x, %x)\n", size, align);
9765827d416SAnton Blanchard 	if (ram_top == 0)
9779b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
9789b6b563cSPaul Mackerras 
9799b6b563cSPaul Mackerras 	if (align)
9805827d416SAnton Blanchard 		base = _ALIGN_UP(alloc_bottom, align);
9819b6b563cSPaul Mackerras 	else
9825827d416SAnton Blanchard 		base = alloc_bottom;
9839b6b563cSPaul Mackerras 
9845827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
9859b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
9869b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
9879b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
988c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
9899b6b563cSPaul Mackerras 			break;
9909b6b563cSPaul Mackerras 		addr = 0;
9919b6b563cSPaul Mackerras 		if (align == 0)
9929b6b563cSPaul Mackerras 			break;
9939b6b563cSPaul Mackerras 	}
9949b6b563cSPaul Mackerras 	if (addr == 0)
9959b6b563cSPaul Mackerras 		return 0;
9965827d416SAnton Blanchard 	alloc_bottom = addr + size;
9979b6b563cSPaul Mackerras 
9989b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
9995827d416SAnton Blanchard 	prom_debug("  alloc_bottom : %x\n", alloc_bottom);
10005827d416SAnton Blanchard 	prom_debug("  alloc_top    : %x\n", alloc_top);
10015827d416SAnton Blanchard 	prom_debug("  alloc_top_hi : %x\n", alloc_top_high);
10025827d416SAnton Blanchard 	prom_debug("  rmo_top      : %x\n", rmo_top);
10035827d416SAnton Blanchard 	prom_debug("  ram_top      : %x\n", ram_top);
10049b6b563cSPaul Mackerras 
10059b6b563cSPaul Mackerras 	return addr;
10069b6b563cSPaul Mackerras }
10079b6b563cSPaul Mackerras 
10089b6b563cSPaul Mackerras /*
10099b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
10109b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
10119b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
10129b6b563cSPaul Mackerras  */
10139b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
10149b6b563cSPaul Mackerras 				       int highmem)
10159b6b563cSPaul Mackerras {
10169b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
10179b6b563cSPaul Mackerras 
10189b6b563cSPaul Mackerras 	prom_debug("alloc_down(%x, %x, %s)\n", size, align,
10195827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
10205827d416SAnton Blanchard 	if (ram_top == 0)
10219b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
10229b6b563cSPaul Mackerras 
10239b6b563cSPaul Mackerras 	if (highmem) {
10249b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
10255827d416SAnton Blanchard 		addr = _ALIGN_DOWN(alloc_top_high - size, align);
10265827d416SAnton Blanchard 		if (addr <= alloc_bottom)
10279b6b563cSPaul Mackerras 			return 0;
10289b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
10299b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
10309b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
10319b6b563cSPaul Mackerras 		 */
10325827d416SAnton Blanchard 		if (addr < rmo_top) {
10339b6b563cSPaul Mackerras 			/* Good, we are first */
10345827d416SAnton Blanchard 			if (alloc_top == rmo_top)
10355827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
10369b6b563cSPaul Mackerras 			else
10379b6b563cSPaul Mackerras 				return 0;
10389b6b563cSPaul Mackerras 		}
10395827d416SAnton Blanchard 		alloc_top_high = addr;
10409b6b563cSPaul Mackerras 		goto bail;
10419b6b563cSPaul Mackerras 	}
10429b6b563cSPaul Mackerras 
10435827d416SAnton Blanchard 	base = _ALIGN_DOWN(alloc_top - size, align);
10445827d416SAnton Blanchard 	for (; base > alloc_bottom;
10459b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
10469b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
10479b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1048c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
10499b6b563cSPaul Mackerras 			break;
10509b6b563cSPaul Mackerras 		addr = 0;
10519b6b563cSPaul Mackerras 	}
10529b6b563cSPaul Mackerras 	if (addr == 0)
10539b6b563cSPaul Mackerras 		return 0;
10545827d416SAnton Blanchard 	alloc_top = addr;
10559b6b563cSPaul Mackerras 
10569b6b563cSPaul Mackerras  bail:
10579b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
10585827d416SAnton Blanchard 	prom_debug("  alloc_bottom : %x\n", alloc_bottom);
10595827d416SAnton Blanchard 	prom_debug("  alloc_top    : %x\n", alloc_top);
10605827d416SAnton Blanchard 	prom_debug("  alloc_top_hi : %x\n", alloc_top_high);
10615827d416SAnton Blanchard 	prom_debug("  rmo_top      : %x\n", rmo_top);
10625827d416SAnton Blanchard 	prom_debug("  ram_top      : %x\n", ram_top);
10639b6b563cSPaul Mackerras 
10649b6b563cSPaul Mackerras 	return addr;
10659b6b563cSPaul Mackerras }
10669b6b563cSPaul Mackerras 
10679b6b563cSPaul Mackerras /*
10689b6b563cSPaul Mackerras  * Parse a "reg" cell
10699b6b563cSPaul Mackerras  */
10709b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
10719b6b563cSPaul Mackerras {
10729b6b563cSPaul Mackerras 	cell_t *p = *cellp;
10739b6b563cSPaul Mackerras 	unsigned long r = 0;
10749b6b563cSPaul Mackerras 
10759b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
10769b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
10779b6b563cSPaul Mackerras 		p++;
10789b6b563cSPaul Mackerras 		s--;
10799b6b563cSPaul Mackerras 	}
1080493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
10819b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
108235499c01SPaul Mackerras 	if (s > 1) {
10839b6b563cSPaul Mackerras 		r <<= 32;
1084493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
10859b6b563cSPaul Mackerras 	}
10869b6b563cSPaul Mackerras #endif
10879b6b563cSPaul Mackerras 	*cellp = p;
10889b6b563cSPaul Mackerras 	return r;
10899b6b563cSPaul Mackerras }
10909b6b563cSPaul Mackerras 
10919b6b563cSPaul Mackerras /*
10929b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
10939b6b563cSPaul Mackerras  * we don't need anything smarter at this point
10949b6b563cSPaul Mackerras  *
10959b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
10969b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
10979b6b563cSPaul Mackerras  * them down.
10989b6b563cSPaul Mackerras  */
10990108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
11009b6b563cSPaul Mackerras {
1101cbbcf340SKumar Gala 	u64 top = base + size;
11025827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
11039b6b563cSPaul Mackerras 
11049b6b563cSPaul Mackerras 	if (size == 0)
11059b6b563cSPaul Mackerras 		return;
11069b6b563cSPaul Mackerras 
11079b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
11089b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
11099b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
11109b6b563cSPaul Mackerras 	 */
11119b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
11129b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
11139b6b563cSPaul Mackerras 	size = top - base;
11149b6b563cSPaul Mackerras 
11159b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
11169b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1117493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1118493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
11195827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
11209b6b563cSPaul Mackerras }
11219b6b563cSPaul Mackerras 
11229b6b563cSPaul Mackerras /*
1123b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
11249b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
11259b6b563cSPaul Mackerras  */
11269b6b563cSPaul Mackerras static void __init prom_init_mem(void)
11279b6b563cSPaul Mackerras {
11289b6b563cSPaul Mackerras 	phandle node;
11299b6b563cSPaul Mackerras 	char *path, type[64];
11309b6b563cSPaul Mackerras 	unsigned int plen;
11319b6b563cSPaul Mackerras 	cell_t *p, *endp;
1132493adffcSBenjamin Herrenschmidt 	__be32 val;
11339b6b563cSPaul Mackerras 	u32 rac, rsc;
11349b6b563cSPaul Mackerras 
11359b6b563cSPaul Mackerras 	/*
11369b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
11379b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
11389b6b563cSPaul Mackerras 	 * 2) top of memory
11399b6b563cSPaul Mackerras 	 */
1140493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1141493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1142493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1143493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1144493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1145493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1146493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1147493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
11489b6b563cSPaul Mackerras 
11499b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
11505827d416SAnton Blanchard 	path = prom_scratch;
11519b6b563cSPaul Mackerras 
11529b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
11539b6b563cSPaul Mackerras 		type[0] = 0;
11549b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
11559b6b563cSPaul Mackerras 
1156c4988820SPaul Mackerras 		if (type[0] == 0) {
1157c4988820SPaul Mackerras 			/*
1158c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1159c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1160c4988820SPaul Mackerras 			 */
1161c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1162c4988820SPaul Mackerras 		}
11635827d416SAnton Blanchard 		if (strcmp(type, "memory"))
11649b6b563cSPaul Mackerras 			continue;
11659b6b563cSPaul Mackerras 
11665827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
11679b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
11689b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
11699b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
11709b6b563cSPaul Mackerras 		}
11715827d416SAnton Blanchard 		p = regbuf;
11729b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
11739b6b563cSPaul Mackerras 
11749b6b563cSPaul Mackerras #ifdef DEBUG_PROM
11759b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
11769b6b563cSPaul Mackerras 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
11779b6b563cSPaul Mackerras 		prom_debug("  node %s :\n", path);
11789b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
11799b6b563cSPaul Mackerras 
11809b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
11819b6b563cSPaul Mackerras 			unsigned long base, size;
11829b6b563cSPaul Mackerras 
11839b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
11849b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
11859b6b563cSPaul Mackerras 
11869b6b563cSPaul Mackerras 			if (size == 0)
11879b6b563cSPaul Mackerras 				continue;
11889b6b563cSPaul Mackerras 			prom_debug("    %x %x\n", base, size);
11895827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
11905827d416SAnton Blanchard 				rmo_top = size;
11915827d416SAnton Blanchard 			if ((base + size) > ram_top)
11925827d416SAnton Blanchard 				ram_top = base + size;
11939b6b563cSPaul Mackerras 		}
11949b6b563cSPaul Mackerras 	}
11959b6b563cSPaul Mackerras 
11965827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
11979b6b563cSPaul Mackerras 
11989b6b563cSPaul Mackerras 	/*
1199cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1200cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1201cf68787bSBenjamin Krill 	 * TCE's up there.
1202cf68787bSBenjamin Krill 	 */
1203cf68787bSBenjamin Krill 
12045827d416SAnton Blanchard 	alloc_top_high = ram_top;
1205cf68787bSBenjamin Krill 
12065827d416SAnton Blanchard 	if (prom_memory_limit) {
12075827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1208cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
12095827d416SAnton Blanchard 				prom_memory_limit);
12105827d416SAnton Blanchard 			prom_memory_limit = 0;
12115827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1212cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x >= ram_top.\n",
12135827d416SAnton Blanchard 				prom_memory_limit);
12145827d416SAnton Blanchard 			prom_memory_limit = 0;
1215cf68787bSBenjamin Krill 		} else {
12165827d416SAnton Blanchard 			ram_top = prom_memory_limit;
12175827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1218cf68787bSBenjamin Krill 		}
1219cf68787bSBenjamin Krill 	}
1220cf68787bSBenjamin Krill 
1221cf68787bSBenjamin Krill 	/*
12229b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
12239b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
12249b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
12259b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
12269b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
12279b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
12289b6b563cSPaul Mackerras 	 */
12295827d416SAnton Blanchard 	if (!rmo_top)
12305827d416SAnton Blanchard 		rmo_top = ram_top;
12315827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
12325827d416SAnton Blanchard 	alloc_top = rmo_top;
12335827d416SAnton Blanchard 	alloc_top_high = ram_top;
12349b6b563cSPaul Mackerras 
123564968f60SPaul Mackerras 	/*
123664968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
123764968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
123864968f60SPaul Mackerras 	 */
12395827d416SAnton Blanchard 	if (prom_initrd_start &&
12405827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
12415827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
12425827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
124364968f60SPaul Mackerras 
12449b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
12455827d416SAnton Blanchard 	prom_printf("  memory_limit : %x (16 MB aligned)\n", prom_memory_limit);
12465827d416SAnton Blanchard 	prom_printf("  alloc_bottom : %x\n", alloc_bottom);
12475827d416SAnton Blanchard 	prom_printf("  alloc_top    : %x\n", alloc_top);
12485827d416SAnton Blanchard 	prom_printf("  alloc_top_hi : %x\n", alloc_top_high);
12495827d416SAnton Blanchard 	prom_printf("  rmo_top      : %x\n", rmo_top);
12505827d416SAnton Blanchard 	prom_printf("  ram_top      : %x\n", ram_top);
12519b6b563cSPaul Mackerras }
12529b6b563cSPaul Mackerras 
125327f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
125427f44888SBenjamin Herrenschmidt {
1255493adffcSBenjamin Herrenschmidt 	__be32 val;
1256493adffcSBenjamin Herrenschmidt 	ihandle stdin;
125727f44888SBenjamin Herrenschmidt 
1258493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1259493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1260493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1261493adffcSBenjamin Herrenschmidt 	}
126227f44888SBenjamin Herrenschmidt }
126327f44888SBenjamin Herrenschmidt 
126427f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
126527f44888SBenjamin Herrenschmidt 
12666e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
12676e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_base;
12686e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_entry;
12696e35d5daSBenjamin Herrenschmidt #endif
12706e35d5daSBenjamin Herrenschmidt 
12716e35d5daSBenjamin Herrenschmidt /*
12726e35d5daSBenjamin Herrenschmidt  * Allocate room for and instantiate OPAL
12736e35d5daSBenjamin Herrenschmidt  */
12746e35d5daSBenjamin Herrenschmidt static void __init prom_instantiate_opal(void)
12756e35d5daSBenjamin Herrenschmidt {
12766e35d5daSBenjamin Herrenschmidt 	phandle opal_node;
12776e35d5daSBenjamin Herrenschmidt 	ihandle opal_inst;
12786e35d5daSBenjamin Herrenschmidt 	u64 base, entry;
12796e35d5daSBenjamin Herrenschmidt 	u64 size = 0, align = 0x10000;
1280493adffcSBenjamin Herrenschmidt 	__be64 val64;
12816e35d5daSBenjamin Herrenschmidt 	u32 rets[2];
12826e35d5daSBenjamin Herrenschmidt 
12836e35d5daSBenjamin Herrenschmidt 	prom_debug("prom_instantiate_opal: start...\n");
12846e35d5daSBenjamin Herrenschmidt 
12856e35d5daSBenjamin Herrenschmidt 	opal_node = call_prom("finddevice", 1, 1, ADDR("/ibm,opal"));
12866e35d5daSBenjamin Herrenschmidt 	prom_debug("opal_node: %x\n", opal_node);
12876e35d5daSBenjamin Herrenschmidt 	if (!PHANDLE_VALID(opal_node))
12886e35d5daSBenjamin Herrenschmidt 		return;
12896e35d5daSBenjamin Herrenschmidt 
1290493adffcSBenjamin Herrenschmidt 	val64 = 0;
1291493adffcSBenjamin Herrenschmidt 	prom_getprop(opal_node, "opal-runtime-size", &val64, sizeof(val64));
1292493adffcSBenjamin Herrenschmidt 	size = be64_to_cpu(val64);
12936e35d5daSBenjamin Herrenschmidt 	if (size == 0)
12946e35d5daSBenjamin Herrenschmidt 		return;
1295493adffcSBenjamin Herrenschmidt 	val64 = 0;
1296493adffcSBenjamin Herrenschmidt 	prom_getprop(opal_node, "opal-runtime-alignment", &val64,sizeof(val64));
1297493adffcSBenjamin Herrenschmidt 	align = be64_to_cpu(val64);
12986e35d5daSBenjamin Herrenschmidt 
12996e35d5daSBenjamin Herrenschmidt 	base = alloc_down(size, align, 0);
13006e35d5daSBenjamin Herrenschmidt 	if (base == 0) {
13016e35d5daSBenjamin Herrenschmidt 		prom_printf("OPAL allocation failed !\n");
13026e35d5daSBenjamin Herrenschmidt 		return;
13036e35d5daSBenjamin Herrenschmidt 	}
13046e35d5daSBenjamin Herrenschmidt 
13056e35d5daSBenjamin Herrenschmidt 	opal_inst = call_prom("open", 1, 1, ADDR("/ibm,opal"));
13066e35d5daSBenjamin Herrenschmidt 	if (!IHANDLE_VALID(opal_inst)) {
13076e35d5daSBenjamin Herrenschmidt 		prom_printf("opening opal package failed (%x)\n", opal_inst);
13086e35d5daSBenjamin Herrenschmidt 		return;
13096e35d5daSBenjamin Herrenschmidt 	}
13106e35d5daSBenjamin Herrenschmidt 
13116e35d5daSBenjamin Herrenschmidt 	prom_printf("instantiating opal at 0x%x...", base);
13126e35d5daSBenjamin Herrenschmidt 
13136e35d5daSBenjamin Herrenschmidt 	if (call_prom_ret("call-method", 4, 3, rets,
13146e35d5daSBenjamin Herrenschmidt 			  ADDR("load-opal-runtime"),
13156e35d5daSBenjamin Herrenschmidt 			  opal_inst,
13166e35d5daSBenjamin Herrenschmidt 			  base >> 32, base & 0xffffffff) != 0
13176e35d5daSBenjamin Herrenschmidt 	    || (rets[0] == 0 && rets[1] == 0)) {
13186e35d5daSBenjamin Herrenschmidt 		prom_printf(" failed\n");
13196e35d5daSBenjamin Herrenschmidt 		return;
13206e35d5daSBenjamin Herrenschmidt 	}
13216e35d5daSBenjamin Herrenschmidt 	entry = (((u64)rets[0]) << 32) | rets[1];
13226e35d5daSBenjamin Herrenschmidt 
13236e35d5daSBenjamin Herrenschmidt 	prom_printf(" done\n");
13246e35d5daSBenjamin Herrenschmidt 
13256e35d5daSBenjamin Herrenschmidt 	reserve_mem(base, size);
13266e35d5daSBenjamin Herrenschmidt 
13276e35d5daSBenjamin Herrenschmidt 	prom_debug("opal base     = 0x%x\n", base);
13286e35d5daSBenjamin Herrenschmidt 	prom_debug("opal align    = 0x%x\n", align);
13296e35d5daSBenjamin Herrenschmidt 	prom_debug("opal entry    = 0x%x\n", entry);
13306e35d5daSBenjamin Herrenschmidt 	prom_debug("opal size     = 0x%x\n", (long)size);
13316e35d5daSBenjamin Herrenschmidt 
13326e35d5daSBenjamin Herrenschmidt 	prom_setprop(opal_node, "/ibm,opal", "opal-base-address",
13336e35d5daSBenjamin Herrenschmidt 		     &base, sizeof(base));
13346e35d5daSBenjamin Herrenschmidt 	prom_setprop(opal_node, "/ibm,opal", "opal-entry-address",
13356e35d5daSBenjamin Herrenschmidt 		     &entry, sizeof(entry));
13366e35d5daSBenjamin Herrenschmidt 
13376e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
13385827d416SAnton Blanchard 	prom_opal_base = base;
13395827d416SAnton Blanchard 	prom_opal_entry = entry;
13406e35d5daSBenjamin Herrenschmidt #endif
13416e35d5daSBenjamin Herrenschmidt 	prom_debug("prom_instantiate_opal: end...\n");
13426e35d5daSBenjamin Herrenschmidt }
13436e35d5daSBenjamin Herrenschmidt 
134427f44888SBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV */
13459b6b563cSPaul Mackerras 
13469b6b563cSPaul Mackerras /*
13479b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
13489b6b563cSPaul Mackerras  */
13499b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
13509b6b563cSPaul Mackerras {
13519b6b563cSPaul Mackerras 	phandle rtas_node;
13529b6b563cSPaul Mackerras 	ihandle rtas_inst;
13539b6b563cSPaul Mackerras 	u32 base, entry = 0;
1354493adffcSBenjamin Herrenschmidt 	__be32 val;
13559b6b563cSPaul Mackerras 	u32 size = 0;
13569b6b563cSPaul Mackerras 
13579b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
13589b6b563cSPaul Mackerras 
13599b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
13609b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
13619b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
13629b6b563cSPaul Mackerras 		return;
13639b6b563cSPaul Mackerras 
1364493adffcSBenjamin Herrenschmidt 	val = 0;
1365493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1366493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
13679b6b563cSPaul Mackerras 	if (size == 0)
13689b6b563cSPaul Mackerras 		return;
13699b6b563cSPaul Mackerras 
13709b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
13716d1e2c6cSAnton Blanchard 	if (base == 0)
13726d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
13739b6b563cSPaul Mackerras 
13749b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
13759b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1376a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
13779b6b563cSPaul Mackerras 		return;
13789b6b563cSPaul Mackerras 	}
13799b6b563cSPaul Mackerras 
13809b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
13819b6b563cSPaul Mackerras 
13829b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
13839b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1384a23414beSPaul Mackerras 			  rtas_inst, base) != 0
13859b6b563cSPaul Mackerras 	    || entry == 0) {
13869b6b563cSPaul Mackerras 		prom_printf(" failed\n");
13879b6b563cSPaul Mackerras 		return;
13889b6b563cSPaul Mackerras 	}
13899b6b563cSPaul Mackerras 	prom_printf(" done\n");
13909b6b563cSPaul Mackerras 
13919b6b563cSPaul Mackerras 	reserve_mem(base, size);
13929b6b563cSPaul Mackerras 
1393493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1394a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1395493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1396493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1397a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1398493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
13999b6b563cSPaul Mackerras 
1400dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1401dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1402dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1403dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1404dbe78b40SBenjamin Herrenschmidt 
14059b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
14069b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
14079b6b563cSPaul Mackerras 	prom_debug("rtas size     = 0x%x\n", (long)size);
14089b6b563cSPaul Mackerras 
14099b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
14109b6b563cSPaul Mackerras }
14119b6b563cSPaul Mackerras 
14129b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
14139b6b563cSPaul Mackerras /*
14144a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
14154a727429SAshley Lai  */
14164a727429SAshley Lai static void __init prom_instantiate_sml(void)
14174a727429SAshley Lai {
14184a727429SAshley Lai 	phandle ibmvtpm_node;
14194a727429SAshley Lai 	ihandle ibmvtpm_inst;
14204a727429SAshley Lai 	u32 entry = 0, size = 0;
14214a727429SAshley Lai 	u64 base;
14224a727429SAshley Lai 
14234a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
14244a727429SAshley Lai 
14254a727429SAshley Lai 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/ibm,vtpm"));
14264a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
14274a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
14284a727429SAshley Lai 		return;
14294a727429SAshley Lai 
14304a727429SAshley Lai 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/ibm,vtpm"));
14314a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
14324a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
14334a727429SAshley Lai 		return;
14344a727429SAshley Lai 	}
14354a727429SAshley Lai 
14364a727429SAshley Lai 	if (call_prom_ret("call-method", 2, 2, &size,
14374a727429SAshley Lai 			  ADDR("sml-get-handover-size"),
14384a727429SAshley Lai 			  ibmvtpm_inst) != 0 || size == 0) {
14394a727429SAshley Lai 		prom_printf("SML get handover size failed\n");
14404a727429SAshley Lai 		return;
14414a727429SAshley Lai 	}
14424a727429SAshley Lai 
14434a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
14444a727429SAshley Lai 	if (base == 0)
14454a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
14464a727429SAshley Lai 
14474a727429SAshley Lai 	prom_printf("instantiating sml at 0x%x...", base);
14484a727429SAshley Lai 
14494a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
14504a727429SAshley Lai 			  ADDR("sml-handover"),
14514a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
14524a727429SAshley Lai 		prom_printf("SML handover failed\n");
14534a727429SAshley Lai 		return;
14544a727429SAshley Lai 	}
14554a727429SAshley Lai 	prom_printf(" done\n");
14564a727429SAshley Lai 
14574a727429SAshley Lai 	reserve_mem(base, size);
14584a727429SAshley Lai 
14594a727429SAshley Lai 	prom_setprop(ibmvtpm_node, "/ibm,vtpm", "linux,sml-base",
14604a727429SAshley Lai 		     &base, sizeof(base));
14614a727429SAshley Lai 	prom_setprop(ibmvtpm_node, "/ibm,vtpm", "linux,sml-size",
14624a727429SAshley Lai 		     &size, sizeof(size));
14634a727429SAshley Lai 
14644a727429SAshley Lai 	prom_debug("sml base     = 0x%x\n", base);
14654a727429SAshley Lai 	prom_debug("sml size     = 0x%x\n", (long)size);
14664a727429SAshley Lai 
14674a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
14684a727429SAshley Lai }
14694a727429SAshley Lai 
14704a727429SAshley Lai /*
14719b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
14729b6b563cSPaul Mackerras  */
1473493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
14749b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
14759b6b563cSPaul Mackerras {
14769b6b563cSPaul Mackerras 	phandle node;
14779b6b563cSPaul Mackerras 	ihandle phb_node;
14789b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
14795827d416SAnton Blanchard 	char *path = prom_scratch;
14809b6b563cSPaul Mackerras 	u64 base, align;
14819b6b563cSPaul Mackerras 	u32 minalign, minsize;
14829b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
14839b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
14849b6b563cSPaul Mackerras 	u64 i;
14859b6b563cSPaul Mackerras 
14865827d416SAnton Blanchard 	if (prom_iommu_off)
14879b6b563cSPaul Mackerras 		return;
14889b6b563cSPaul Mackerras 
14899b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
14909b6b563cSPaul Mackerras 
14919b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
14925827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
14939b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
14949b6b563cSPaul Mackerras 
14959b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
14969b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
14979b6b563cSPaul Mackerras 		compatible[0] = 0;
14989b6b563cSPaul Mackerras 		type[0] = 0;
14999b6b563cSPaul Mackerras 		model[0] = 0;
15009b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
15019b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
15029b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
15039b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
15049b6b563cSPaul Mackerras 
15055827d416SAnton Blanchard 		if ((type[0] == 0) || (strstr(type, "pci") == NULL))
15069b6b563cSPaul Mackerras 			continue;
15079b6b563cSPaul Mackerras 
1508e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
15099b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
15105827d416SAnton Blanchard 			if ((strstr(compatible, "python") == NULL) &&
15115827d416SAnton Blanchard 			    (strstr(compatible, "Speedwagon") == NULL) &&
15125827d416SAnton Blanchard 			    (strstr(compatible, "Winnipeg") == NULL))
15139b6b563cSPaul Mackerras 				continue;
15149b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
15155827d416SAnton Blanchard 			if ((strstr(model, "ython") == NULL) &&
15165827d416SAnton Blanchard 			    (strstr(model, "peedwagon") == NULL) &&
15175827d416SAnton Blanchard 			    (strstr(model, "innipeg") == NULL))
15189b6b563cSPaul Mackerras 				continue;
15199b6b563cSPaul Mackerras 		}
15209b6b563cSPaul Mackerras 
15219b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
15229b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
15239b6b563cSPaul Mackerras 			minalign = 0;
15249b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
15259b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
15269b6b563cSPaul Mackerras 			minsize = 4UL << 20;
15279b6b563cSPaul Mackerras 
15289b6b563cSPaul Mackerras 		/*
15299b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
15309b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
15319b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
15329b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
15339b6b563cSPaul Mackerras 		 *
15349b6b563cSPaul Mackerras 		 * On POWER4, firmware sets the TCE region by assuming
15359b6b563cSPaul Mackerras 		 * each TCE table is 8MB. Using this memory for anything
15369b6b563cSPaul Mackerras 		 * else will impact performance, so we always allocate 8MB.
15379b6b563cSPaul Mackerras 		 * Anton
15389b6b563cSPaul Mackerras 		 */
1539d3dbeef6SMichael Ellerman 		if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p))
15409b6b563cSPaul Mackerras 			minsize = 8UL << 20;
15419b6b563cSPaul Mackerras 		else
15429b6b563cSPaul Mackerras 			minsize = 4UL << 20;
15439b6b563cSPaul Mackerras 
15449b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
15459b6b563cSPaul Mackerras 		align = max(minalign, minsize);
15469b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
15479b6b563cSPaul Mackerras 		if (base == 0)
15489b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
15499b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
15509b6b563cSPaul Mackerras 			local_alloc_bottom = base;
15519b6b563cSPaul Mackerras 
15529b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
1553aca71ef8SLi Zefan 		memset(path, 0, PROM_SCRATCH_SIZE);
15549b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
15559b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
15569b6b563cSPaul Mackerras 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
15579b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
15589b6b563cSPaul Mackerras 		}
15599b6b563cSPaul Mackerras 
1560a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
1561a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1562a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1563a23414beSPaul Mackerras 
15649b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
15659b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
15669b6b563cSPaul Mackerras 		prom_debug("\tbase = 0x%x\n", base);
15679b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
15689b6b563cSPaul Mackerras 
15699b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
15709b6b563cSPaul Mackerras 		 * over the allocated size.
15719b6b563cSPaul Mackerras 		 */
15722b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
15739b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
15749b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
15759b6b563cSPaul Mackerras 			tce_entry |= 0x3;
15769b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
15779b6b563cSPaul Mackerras 		}
15789b6b563cSPaul Mackerras 
15799b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
15809b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
15819b6b563cSPaul Mackerras 		if (phb_node == 0)
15829b6b563cSPaul Mackerras 			prom_printf("... failed\n");
15839b6b563cSPaul Mackerras 		else
15849b6b563cSPaul Mackerras 			prom_printf("... done\n");
15859b6b563cSPaul Mackerras 
15869b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
15879b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
15889b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
15899b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
15909b6b563cSPaul Mackerras 	}
15919b6b563cSPaul Mackerras 
15929b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
15939b6b563cSPaul Mackerras 
15942babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
15952babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
15965827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
15975827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
15989b6b563cSPaul Mackerras 
15999b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
16009b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
16019b6b563cSPaul Mackerras }
1602493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1603493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
16049b6b563cSPaul Mackerras 
16059b6b563cSPaul Mackerras /*
16069b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
16079b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
16089b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
16099b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
16109b6b563cSPaul Mackerras  *
16119b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
16129b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
16139b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
16149b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
16159b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
16169b6b563cSPaul Mackerras  * of setting those values.
16179b6b563cSPaul Mackerras  *
16189b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
16199b6b563cSPaul Mackerras  * is in its holding pattern code.
16209b6b563cSPaul Mackerras  *
16219b6b563cSPaul Mackerras  * -- Cort
16229b6b563cSPaul Mackerras  */
1623bbd0abdaSPaul Mackerras /*
1624bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
1625bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
1626bbd0abdaSPaul Mackerras  */
1627bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1628bbd0abdaSPaul Mackerras 
16299b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
16309b6b563cSPaul Mackerras {
16319b6b563cSPaul Mackerras 	unsigned long i;
16329b6b563cSPaul Mackerras 	phandle node;
16339b6b563cSPaul Mackerras 	char type[64];
16349b6b563cSPaul Mackerras 	unsigned long *spinloop
1635bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
16369b6b563cSPaul Mackerras 	unsigned long *acknowledge
1637bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1638bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
16399b6b563cSPaul Mackerras 
1640dbe78b40SBenjamin Herrenschmidt 	/*
1641dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
1642dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
1643dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
1644dbe78b40SBenjamin Herrenschmidt 	 */
1645dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
1646dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
1647dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
1648dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
1649dbe78b40SBenjamin Herrenschmidt 		return;
1650dbe78b40SBenjamin Herrenschmidt 	}
1651dbe78b40SBenjamin Herrenschmidt 
16529b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
16539b6b563cSPaul Mackerras 	prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
16549b6b563cSPaul Mackerras 	prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
16559b6b563cSPaul Mackerras 	prom_debug("    1) acknowledge    = 0x%x\n",
16569b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
16579b6b563cSPaul Mackerras 	prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
16589b6b563cSPaul Mackerras 	prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
16599b6b563cSPaul Mackerras 
16609b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
16619b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
16629b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
16639b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
16649b6b563cSPaul Mackerras 	 */
16659b6b563cSPaul Mackerras 	*spinloop = 0;
16669b6b563cSPaul Mackerras 
16679b6b563cSPaul Mackerras 	/* look for cpus */
16689b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
1669493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
1670493adffcSBenjamin Herrenschmidt 		__be32 reg;
1671493adffcSBenjamin Herrenschmidt 
16729b6b563cSPaul Mackerras 		type[0] = 0;
16739b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
16745827d416SAnton Blanchard 		if (strcmp(type, "cpu") != 0)
16759b6b563cSPaul Mackerras 			continue;
16769b6b563cSPaul Mackerras 
16779b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
16789b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
16795827d416SAnton Blanchard 			if (strcmp(type, "okay") != 0)
16809b6b563cSPaul Mackerras 				continue;
16819b6b563cSPaul Mackerras 
1682493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
16839b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
1684493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
16859b6b563cSPaul Mackerras 
1686493adffcSBenjamin Herrenschmidt 		prom_debug("cpu hw idx   = %lu\n", cpu_no);
16879b6b563cSPaul Mackerras 
16889b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
16899b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
16909b6b563cSPaul Mackerras 		 * spinloop.
16919b6b563cSPaul Mackerras 		 */
16929b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
16939b6b563cSPaul Mackerras 
1694493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
169527f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
1696493adffcSBenjamin Herrenschmidt 			prom_printf("starting cpu hw idx %lu... ", cpu_no);
16979b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
1698493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
16999b6b563cSPaul Mackerras 
17009b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
17019b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
17029b6b563cSPaul Mackerras 				mb();
17039b6b563cSPaul Mackerras 
1704493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
17059b6b563cSPaul Mackerras 				prom_printf("done\n");
1706bbd0abdaSPaul Mackerras 			else
17079b6b563cSPaul Mackerras 				prom_printf("failed: %x\n", *acknowledge);
17089b6b563cSPaul Mackerras 		}
17099b6b563cSPaul Mackerras #ifdef CONFIG_SMP
17109b6b563cSPaul Mackerras 		else
1711493adffcSBenjamin Herrenschmidt 			prom_printf("boot cpu hw idx %lu\n", cpu_no);
17129b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
17139b6b563cSPaul Mackerras 	}
17149b6b563cSPaul Mackerras 
17159b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
17169b6b563cSPaul Mackerras }
17179b6b563cSPaul Mackerras 
17189b6b563cSPaul Mackerras 
17199b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
17209b6b563cSPaul Mackerras {
17219b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
17225827d416SAnton Blanchard 	prom_entry = pp;
17239b6b563cSPaul Mackerras 
17249b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
17255827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
17265827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
17279b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
17289b6b563cSPaul Mackerras 
17299b6b563cSPaul Mackerras 	/* get device tree root */
17305827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
17315827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
17329b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1733a575b807SPaul Mackerras 
17345827d416SAnton Blanchard 	prom.mmumap = 0;
17359b6b563cSPaul Mackerras }
17369b6b563cSPaul Mackerras 
1737a575b807SPaul Mackerras #ifdef CONFIG_PPC32
1738a575b807SPaul Mackerras /*
1739a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
1740a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
1741a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
1742a575b807SPaul Mackerras  */
1743a575b807SPaul Mackerras static void __init prom_find_mmu(void)
1744a575b807SPaul Mackerras {
1745a575b807SPaul Mackerras 	phandle oprom;
1746a575b807SPaul Mackerras 	char version[64];
1747a575b807SPaul Mackerras 
1748a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1749a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
1750a575b807SPaul Mackerras 		return;
1751a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1752a575b807SPaul Mackerras 		return;
1753a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
1754a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
1755a23414beSPaul Mackerras 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1756a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
1757a23414beSPaul Mackerras 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1758a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1759a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1760a23414beSPaul Mackerras 	} else
1761a575b807SPaul Mackerras 		return;
17625827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
17635827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
17645827d416SAnton Blanchard 		     sizeof(prom.mmumap));
1765493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
17665827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
1767a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
1768a575b807SPaul Mackerras }
1769a575b807SPaul Mackerras #else
1770a575b807SPaul Mackerras #define prom_find_mmu()
1771a575b807SPaul Mackerras #endif
1772a575b807SPaul Mackerras 
17739b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
17749b6b563cSPaul Mackerras {
17755827d416SAnton Blanchard 	char *path = of_stdout_device;
17769b6b563cSPaul Mackerras 	char type[16];
1777493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
1778493adffcSBenjamin Herrenschmidt 	__be32 val;
17799b6b563cSPaul Mackerras 
17805827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
17819b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
17829b6b563cSPaul Mackerras 
1783493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
17849b6b563cSPaul Mackerras 
17859b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
17869b6b563cSPaul Mackerras 	memset(path, 0, 256);
17875827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
17885827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
17895827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
1790a23414beSPaul Mackerras 		     path, strlen(path) + 1);
17919b6b563cSPaul Mackerras 
179210348f59SBenjamin Herrenschmidt 	/* instance-to-package fails on PA-Semi */
179310348f59SBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
179410348f59SBenjamin Herrenschmidt 	if (stdout_node != PROM_ERROR) {
179510348f59SBenjamin Herrenschmidt 		val = cpu_to_be32(stdout_node);
179610348f59SBenjamin Herrenschmidt 		prom_setprop(prom.chosen, "/chosen", "linux,stdout-package",
179710348f59SBenjamin Herrenschmidt 			     &val, sizeof(val));
179810348f59SBenjamin Herrenschmidt 
17999b6b563cSPaul Mackerras 		/* If it's a display, note it */
18009b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
1801493adffcSBenjamin Herrenschmidt 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
18025827d416SAnton Blanchard 		if (strcmp(type, "display") == 0)
1803493adffcSBenjamin Herrenschmidt 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
18049b6b563cSPaul Mackerras 	}
180510348f59SBenjamin Herrenschmidt }
18069b6b563cSPaul Mackerras 
18079b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
18089b6b563cSPaul Mackerras {
18099b6b563cSPaul Mackerras 	char compat[256];
18109b6b563cSPaul Mackerras 	int len, i = 0;
181121fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
18129b6b563cSPaul Mackerras 	phandle rtas;
1813e8222502SBenjamin Herrenschmidt 	int x;
181421fe3301SBenjamin Herrenschmidt #endif
1815e8222502SBenjamin Herrenschmidt 
18166e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
18175827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
18189b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
18199b6b563cSPaul Mackerras 	if (len > 0) {
18209b6b563cSPaul Mackerras 		compat[len] = 0;
18219b6b563cSPaul Mackerras 		while (i < len) {
18229b6b563cSPaul Mackerras 			char *p = &compat[i];
18239b6b563cSPaul Mackerras 			int sl = strlen(p);
18249b6b563cSPaul Mackerras 			if (sl == 0)
18259b6b563cSPaul Mackerras 				break;
18265827d416SAnton Blanchard 			if (strstr(p, "Power Macintosh") ||
18275827d416SAnton Blanchard 			    strstr(p, "MacRISC"))
18289b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
1829133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
1830133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
1831133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
1832133dda1eSArnd Bergmann 			 * so we do it here.
1833133dda1eSArnd Bergmann 			 */
18345827d416SAnton Blanchard 			if (strstr(p, "IBM,CBEA") ||
18355827d416SAnton Blanchard 			    strstr(p, "IBM,CPBW-1.0"))
1836133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
1837133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
18389b6b563cSPaul Mackerras 			i += sl + 1;
18399b6b563cSPaul Mackerras 		}
18409b6b563cSPaul Mackerras 	}
18419b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
18426e35d5daSBenjamin Herrenschmidt 	/* Try to detect OPAL */
18436e35d5daSBenjamin Herrenschmidt 	if (PHANDLE_VALID(call_prom("finddevice", 1, 1, ADDR("/ibm,opal"))))
18446e35d5daSBenjamin Herrenschmidt 		return PLATFORM_OPAL;
18456e35d5daSBenjamin Herrenschmidt 
18466e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
1847e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
1848e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
1849e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
1850e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
1851e8222502SBenjamin Herrenschmidt 	 */
18525827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
1853e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
1854e8222502SBenjamin Herrenschmidt 	if (len <= 0)
1855e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
18565827d416SAnton Blanchard 	if (strcmp(compat, "chrp"))
1857e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
1858e8222502SBenjamin Herrenschmidt 
18599b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
18609b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1861e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
1862e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
1863e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
18649b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
18654da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
18669b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
18679b6b563cSPaul Mackerras 	}
18689b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
18699b6b563cSPaul Mackerras #else
1870e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
18719b6b563cSPaul Mackerras #endif
18729b6b563cSPaul Mackerras }
18739b6b563cSPaul Mackerras 
18749b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
18759b6b563cSPaul Mackerras {
18769b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
18779b6b563cSPaul Mackerras }
18789b6b563cSPaul Mackerras 
18799b6b563cSPaul Mackerras /*
18809b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
18819b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
18829b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
18839b6b563cSPaul Mackerras  * we've taken over the MMU.
18849b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
18859b6b563cSPaul Mackerras  * and if so, open it now.
18869b6b563cSPaul Mackerras  */
18879b6b563cSPaul Mackerras static void __init prom_check_displays(void)
18889b6b563cSPaul Mackerras {
18899b6b563cSPaul Mackerras 	char type[16], *path;
18909b6b563cSPaul Mackerras 	phandle node;
18919b6b563cSPaul Mackerras 	ihandle ih;
18929b6b563cSPaul Mackerras 	int i;
18939b6b563cSPaul Mackerras 
18949b6b563cSPaul Mackerras 	static unsigned char default_colors[] = {
18959b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
18969b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
18979b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
18989b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
18999b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
19009b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
19019b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
19029b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
19039b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
19049b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
19059b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
19069b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
19079b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
19089b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
19099b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
19109b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
19119b6b563cSPaul Mackerras 	};
19129b6b563cSPaul Mackerras 	const unsigned char *clut;
19139b6b563cSPaul Mackerras 
19144da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
19159b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
19169b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
19179b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
19185827d416SAnton Blanchard 		if (strcmp(type, "display") != 0)
19199b6b563cSPaul Mackerras 			continue;
19209b6b563cSPaul Mackerras 
19219b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
19225827d416SAnton Blanchard 		path = prom_scratch;
19239b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
19249b6b563cSPaul Mackerras 
19259b6b563cSPaul Mackerras 		/*
19269b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
19279b6b563cSPaul Mackerras 		 * arguments
19289b6b563cSPaul Mackerras 		 */
19299b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
19309b6b563cSPaul Mackerras 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
19319b6b563cSPaul Mackerras 			continue;
19329b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
19339b6b563cSPaul Mackerras 
19349b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
19359b6b563cSPaul Mackerras 		if (ih == 0) {
19369b6b563cSPaul Mackerras 			prom_printf("failed\n");
19379b6b563cSPaul Mackerras 			continue;
19389b6b563cSPaul Mackerras 		}
19399b6b563cSPaul Mackerras 
19409b6b563cSPaul Mackerras 		/* Success */
19419b6b563cSPaul Mackerras 		prom_printf("done\n");
1942a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
19439b6b563cSPaul Mackerras 
19449b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
19459b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
19465827d416SAnton Blanchard 		clut = default_colors;
19473f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
19489b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
19499b6b563cSPaul Mackerras 					   clut[2]) != 0)
19509b6b563cSPaul Mackerras 				break;
19519b6b563cSPaul Mackerras 
19529b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
19535827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
19545827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
19559b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
19569b6b563cSPaul Mackerras 					   clut[2]) != 0)
19579b6b563cSPaul Mackerras 				break;
19589b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
19597191b615SBenjamin Herrenschmidt 
19607191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
19617191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
19627191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
19637191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
19647191b615SBenjamin Herrenschmidt 
19657191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
19667191b615SBenjamin Herrenschmidt 			prom_getprop(node, "width", &width, 4);
19677191b615SBenjamin Herrenschmidt 			prom_getprop(node, "height", &height, 4);
19687191b615SBenjamin Herrenschmidt 			prom_getprop(node, "linebytes", &pitch, 4);
19697191b615SBenjamin Herrenschmidt 			prom_getprop(node, "address", &addr, 4);
19707191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
19717191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
19727191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
19737191b615SBenjamin Herrenschmidt 		}
19747191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
19759b6b563cSPaul Mackerras 	}
19769b6b563cSPaul Mackerras }
19779b6b563cSPaul Mackerras 
19789b6b563cSPaul Mackerras 
19799b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
19809b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
19819b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
19829b6b563cSPaul Mackerras {
19839b6b563cSPaul Mackerras 	void *ret;
19849b6b563cSPaul Mackerras 
19859b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
19869b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
19879b6b563cSPaul Mackerras 		unsigned long room, chunk;
19889b6b563cSPaul Mackerras 
19899b6b563cSPaul Mackerras 		prom_debug("Chunk exhausted, claiming more at %x...\n",
19905827d416SAnton Blanchard 			   alloc_bottom);
19915827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
19929b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
19939b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
19949b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
1995fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
1996fbafd728SAnton Blanchard 				   "(no room)\n");
19979b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
19989b6b563cSPaul Mackerras 		if (chunk == 0)
1999fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2000fbafd728SAnton Blanchard 				   "(claim failed)\n");
2001966728ddSAnton Blanchard 		*mem_end = chunk + room;
20029b6b563cSPaul Mackerras 	}
20039b6b563cSPaul Mackerras 
20049b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
20059b6b563cSPaul Mackerras 	*mem_start += needed;
20069b6b563cSPaul Mackerras 
20079b6b563cSPaul Mackerras 	return ret;
20089b6b563cSPaul Mackerras }
20099b6b563cSPaul Mackerras 
2010493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2011493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2012493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2013493adffcSBenjamin Herrenschmidt 	} while(0)
20149b6b563cSPaul Mackerras 
20159b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
20169b6b563cSPaul Mackerras {
20179b6b563cSPaul Mackerras 	char *s, *os;
20189b6b563cSPaul Mackerras 
20195827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
20209b6b563cSPaul Mackerras 	s += 4;
20215827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
20229b6b563cSPaul Mackerras 		if (strcmp(s, str) == 0)
20239b6b563cSPaul Mackerras 			return s - os;
20249b6b563cSPaul Mackerras 		s += strlen(s) + 1;
20259b6b563cSPaul Mackerras 	}
20269b6b563cSPaul Mackerras 	return 0;
20279b6b563cSPaul Mackerras }
20289b6b563cSPaul Mackerras 
20299b6b563cSPaul Mackerras /*
20309b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
20319b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
20329b6b563cSPaul Mackerras  */
20339b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
20349b6b563cSPaul Mackerras 
20359b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
20369b6b563cSPaul Mackerras 					 unsigned long *mem_start,
20379b6b563cSPaul Mackerras 					 unsigned long *mem_end)
20389b6b563cSPaul Mackerras {
20399b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
20409b6b563cSPaul Mackerras 	unsigned long soff;
20419b6b563cSPaul Mackerras 	phandle child;
20429b6b563cSPaul Mackerras 
20435827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
20449b6b563cSPaul Mackerras 
20459b6b563cSPaul Mackerras 	/* get and store all property names */
20465827d416SAnton Blanchard 	prev_name = "";
20479b6b563cSPaul Mackerras 	for (;;) {
20489b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
20499b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
20509b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
20519b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
20529b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
20539b6b563cSPaul Mackerras 			break;
20549b6b563cSPaul Mackerras 		}
20559b6b563cSPaul Mackerras 
20569b6b563cSPaul Mackerras  		/* skip "name" */
20575827d416SAnton Blanchard  		if (strcmp(namep, "name") == 0) {
20589b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
20595827d416SAnton Blanchard  			prev_name = "name";
20609b6b563cSPaul Mackerras  			continue;
20619b6b563cSPaul Mackerras  		}
20629b6b563cSPaul Mackerras 		/* get/create string entry */
20639b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
20649b6b563cSPaul Mackerras 		if (soff != 0) {
20659b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
20669b6b563cSPaul Mackerras 			namep = sstart + soff;
20679b6b563cSPaul Mackerras 		} else {
20689b6b563cSPaul Mackerras 			/* Trim off some if we can */
20699b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
20705827d416SAnton Blanchard 			dt_string_end = *mem_start;
20719b6b563cSPaul Mackerras 		}
20729b6b563cSPaul Mackerras 		prev_name = namep;
20739b6b563cSPaul Mackerras 	}
20749b6b563cSPaul Mackerras 
20759b6b563cSPaul Mackerras 	/* do all our children */
20769b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
20779b6b563cSPaul Mackerras 	while (child != 0) {
20789b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
20799b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
20809b6b563cSPaul Mackerras 	}
20819b6b563cSPaul Mackerras }
20829b6b563cSPaul Mackerras 
20839b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
20849b6b563cSPaul Mackerras 					unsigned long *mem_end)
20859b6b563cSPaul Mackerras {
20869b6b563cSPaul Mackerras 	phandle child;
20879b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
20889b6b563cSPaul Mackerras 	unsigned long soff;
20899b6b563cSPaul Mackerras 	unsigned char *valp;
20909b6b563cSPaul Mackerras 	static char pname[MAX_PROPERTY_NAME];
20916e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
20929b6b563cSPaul Mackerras 
20939b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
20949b6b563cSPaul Mackerras 
20959b6b563cSPaul Mackerras 	/* get the node's full name */
20969b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2097c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2098c4988820SPaul Mackerras 	if (room > 255)
2099c4988820SPaul Mackerras 		room = 255;
2100c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
21019b6b563cSPaul Mackerras 	if (l >= 0) {
21029b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2103c4988820SPaul Mackerras 		if (l >= room) {
2104c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
21059b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
21069b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
21079b6b563cSPaul Mackerras 		}
21089b6b563cSPaul Mackerras 		namep[l] = '\0';
21099b6b563cSPaul Mackerras 
21109b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2111a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2112a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
21139b6b563cSPaul Mackerras 		 */
2114a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
21159b6b563cSPaul Mackerras 			if (*p == '/')
2116a575b807SPaul Mackerras 				lp = namep;
2117a575b807SPaul Mackerras 			else if (*p != 0)
2118a575b807SPaul Mackerras 				*lp++ = *p;
2119a575b807SPaul Mackerras 		}
2120a575b807SPaul Mackerras 		*lp = 0;
2121a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
21229b6b563cSPaul Mackerras 	}
21239b6b563cSPaul Mackerras 
21249b6b563cSPaul Mackerras 	/* get it again for debugging */
21255827d416SAnton Blanchard 	path = prom_scratch;
21269b6b563cSPaul Mackerras 	memset(path, 0, PROM_SCRATCH_SIZE);
21279b6b563cSPaul Mackerras 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
21289b6b563cSPaul Mackerras 
21299b6b563cSPaul Mackerras 	/* get and store all properties */
21305827d416SAnton Blanchard 	prev_name = "";
21315827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
21329b6b563cSPaul Mackerras 	for (;;) {
21339b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
21345827d416SAnton Blanchard 			      pname) != 1)
21359b6b563cSPaul Mackerras 			break;
21369b6b563cSPaul Mackerras 
21379b6b563cSPaul Mackerras  		/* skip "name" */
21385827d416SAnton Blanchard  		if (strcmp(pname, "name") == 0) {
21395827d416SAnton Blanchard  			prev_name = "name";
21409b6b563cSPaul Mackerras  			continue;
21419b6b563cSPaul Mackerras  		}
21429b6b563cSPaul Mackerras 
21439b6b563cSPaul Mackerras 		/* find string offset */
21445827d416SAnton Blanchard 		soff = dt_find_string(pname);
21459b6b563cSPaul Mackerras 		if (soff == 0) {
21469b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
21475827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
21489b6b563cSPaul Mackerras 			break;
21499b6b563cSPaul Mackerras 		}
21509b6b563cSPaul Mackerras 		prev_name = sstart + soff;
21519b6b563cSPaul Mackerras 
21529b6b563cSPaul Mackerras 		/* get length */
21535827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
21549b6b563cSPaul Mackerras 
21559b6b563cSPaul Mackerras 		/* sanity checks */
21569b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
21579b6b563cSPaul Mackerras 			continue;
21589b6b563cSPaul Mackerras 
21599b6b563cSPaul Mackerras 		/* push property head */
21609b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
21619b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
21629b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
21639b6b563cSPaul Mackerras 
21649b6b563cSPaul Mackerras 		/* push property content */
21659b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
21665827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
21679b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
21686e35d5daSBenjamin Herrenschmidt 
21695827d416SAnton Blanchard 		if (!strcmp(pname, "phandle"))
21706e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
21719b6b563cSPaul Mackerras 	}
21729b6b563cSPaul Mackerras 
21736e35d5daSBenjamin Herrenschmidt 	/* Add a "linux,phandle" property if no "phandle" property already
21746e35d5daSBenjamin Herrenschmidt 	 * existed (can happen with OPAL)
21756e35d5daSBenjamin Herrenschmidt 	 */
21766e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
21775827d416SAnton Blanchard 		soff = dt_find_string("linux,phandle");
21789b6b563cSPaul Mackerras 		if (soff == 0)
21799b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
21809b6b563cSPaul Mackerras 				    " <linux-phandle> node %s\n", path);
21819b6b563cSPaul Mackerras 		else {
21829b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
21839b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
21849b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
21859b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
2186493adffcSBenjamin Herrenschmidt 			*(__be32 *)valp = cpu_to_be32(node);
21879b6b563cSPaul Mackerras 		}
21886e35d5daSBenjamin Herrenschmidt 	}
21899b6b563cSPaul Mackerras 
21909b6b563cSPaul Mackerras 	/* do all our children */
21919b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
21929b6b563cSPaul Mackerras 	while (child != 0) {
21939b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
21949b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
21959b6b563cSPaul Mackerras 	}
21969b6b563cSPaul Mackerras 
21979b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
21989b6b563cSPaul Mackerras }
21999b6b563cSPaul Mackerras 
22009b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
22019b6b563cSPaul Mackerras {
22029b6b563cSPaul Mackerras 	phandle root;
22039b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
22049b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
22059b6b563cSPaul Mackerras 	char *namep;
22069b6b563cSPaul Mackerras 	u64 *rsvmap;
22079b6b563cSPaul Mackerras 
22089b6b563cSPaul Mackerras 	/*
22099b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2210fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
22119b6b563cSPaul Mackerras 	 */
22125827d416SAnton Blanchard 	room = alloc_top - alloc_bottom - 0x4000;
22139b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
22149b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
22155827d416SAnton Blanchard 	prom_debug("starting device tree allocs at %x\n", alloc_bottom);
22169b6b563cSPaul Mackerras 
22179b6b563cSPaul Mackerras 	/* Now try to claim that */
22189b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
22199b6b563cSPaul Mackerras 	if (mem_start == 0)
22209b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2221966728ddSAnton Blanchard 	mem_end = mem_start + room;
22229b6b563cSPaul Mackerras 
22239b6b563cSPaul Mackerras 	/* Get root of tree */
22249b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
22259b6b563cSPaul Mackerras 	if (root == (phandle)0)
22269b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
22279b6b563cSPaul Mackerras 
22289b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
22299b6b563cSPaul Mackerras 	mem_start = _ALIGN(mem_start, 4);
22309b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
22319b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
22325827d416SAnton Blanchard 	dt_header_start = (unsigned long)hdr;
22339b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
22349b6b563cSPaul Mackerras 
22359b6b563cSPaul Mackerras 	/* Start of strings */
22369b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
22375827d416SAnton Blanchard 	dt_string_start = mem_start;
22389b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
22399b6b563cSPaul Mackerras 
22409b6b563cSPaul Mackerras 	/* Add "linux,phandle" in there, we'll need it */
22419b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
22425827d416SAnton Blanchard 	strcpy(namep, "linux,phandle");
22439b6b563cSPaul Mackerras 	mem_start = (unsigned long)namep + strlen(namep) + 1;
22449b6b563cSPaul Mackerras 
22459b6b563cSPaul Mackerras 	/* Build string array */
22469b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
22479b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
22485827d416SAnton Blanchard 	dt_string_end = mem_start;
22499b6b563cSPaul Mackerras 
22509b6b563cSPaul Mackerras 	/* Build structure */
22519b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
22525827d416SAnton Blanchard 	dt_struct_start = mem_start;
22539b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
22549b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
22559b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
22565827d416SAnton Blanchard 	dt_struct_end = PAGE_ALIGN(mem_start);
22579b6b563cSPaul Mackerras 
22589b6b563cSPaul Mackerras 	/* Finish header */
2259493adffcSBenjamin Herrenschmidt 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2260493adffcSBenjamin Herrenschmidt 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2261493adffcSBenjamin Herrenschmidt 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2262493adffcSBenjamin Herrenschmidt 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2263493adffcSBenjamin Herrenschmidt 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2264493adffcSBenjamin Herrenschmidt 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2265493adffcSBenjamin Herrenschmidt 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2266493adffcSBenjamin Herrenschmidt 	hdr->version = cpu_to_be32(OF_DT_VERSION);
22679b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
2268493adffcSBenjamin Herrenschmidt 	hdr->last_comp_version = cpu_to_be32(0x10);
22699b6b563cSPaul Mackerras 
22704d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
22715827d416SAnton Blanchard 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
22729b6b563cSPaul Mackerras 
22739b6b563cSPaul Mackerras #ifdef DEBUG_PROM
22749b6b563cSPaul Mackerras 	{
22759b6b563cSPaul Mackerras 		int i;
22769b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
22775827d416SAnton Blanchard 		for (i = 0; i < mem_reserve_cnt; i++)
22789b6b563cSPaul Mackerras 			prom_printf("  %x - %x\n",
2279493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].base),
2280493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].size));
22819b6b563cSPaul Mackerras 	}
22829b6b563cSPaul Mackerras #endif
22834d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
22844d1f3f25SJimi Xenidis 	 * since it's too late.
22854d1f3f25SJimi Xenidis 	 */
22865827d416SAnton Blanchard 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
22879b6b563cSPaul Mackerras 
22889b6b563cSPaul Mackerras 	prom_printf("Device tree strings 0x%x -> 0x%x\n",
22895827d416SAnton Blanchard 		    dt_string_start, dt_string_end);
22909b6b563cSPaul Mackerras 	prom_printf("Device tree struct  0x%x -> 0x%x\n",
22915827d416SAnton Blanchard 		    dt_struct_start, dt_struct_end);
22929b6b563cSPaul Mackerras }
22939b6b563cSPaul Mackerras 
229454f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
229554f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
229654f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
229754f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
22989b6b563cSPaul Mackerras {
229954f4ee18SHollis Blanchard 	phandle isa;
2300980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
230154f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2302980a6513SBenjamin Herrenschmidt 	char *name;
230354f4ee18SHollis Blanchard 
2304980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2305980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2306980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2307980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2308980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2309980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2310980a6513SBenjamin Herrenschmidt 	}
231154f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
231254f4ee18SHollis Blanchard 		return;
231354f4ee18SHollis Blanchard 
2314980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2315980a6513SBenjamin Herrenschmidt 		return;
231654f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
231754f4ee18SHollis Blanchard 		== PROM_ERROR)
231854f4ee18SHollis Blanchard 		return;
231954f4ee18SHollis Blanchard 
232054f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
232154f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
232254f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
232354f4ee18SHollis Blanchard 		return;
232454f4ee18SHollis Blanchard 
2325980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
232654f4ee18SHollis Blanchard 
232754f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
232854f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2329980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
233054f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
233154f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
233254f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2333980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
233454f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
233554f4ee18SHollis Blanchard }
23368f101a05SHarry Ciao 
23378f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
23388f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
23398f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
23408f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
23418f101a05SHarry Ciao {
23428f101a05SHarry Ciao 	phandle mc;
23438f101a05SHarry Ciao 	u32 mc_reg[4];
23448f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
23458f101a05SHarry Ciao 	u32 ac, sc;
23468f101a05SHarry Ciao 
23478f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
23488f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
23498f101a05SHarry Ciao 		return;
23508f101a05SHarry Ciao 
23518f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
23528f101a05SHarry Ciao 		return;
23538f101a05SHarry Ciao 
23545827d416SAnton Blanchard 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
23555827d416SAnton Blanchard 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
23568f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
23578f101a05SHarry Ciao 		return;
23588f101a05SHarry Ciao 
23598f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
23608f101a05SHarry Ciao 		return;
23618f101a05SHarry Ciao 
23628f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
23638f101a05SHarry Ciao 		return;
23648f101a05SHarry Ciao 
23658f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
23668f101a05SHarry Ciao 
23678f101a05SHarry Ciao 	mc_reg[0] = 0x0;
23688f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
23698f101a05SHarry Ciao 	mc_reg[2] = 0x0;
23708f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
23718f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
23728f101a05SHarry Ciao }
237354f4ee18SHollis Blanchard #else
237454f4ee18SHollis Blanchard #define fixup_device_tree_maple()
23758f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
237654f4ee18SHollis Blanchard #endif
237754f4ee18SHollis Blanchard 
2378e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2379e4805922SOlaf Hering /*
2380e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2381e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2382556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2383e4805922SOlaf Hering  */
2384e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2385e8c0acf9SBenjamin Herrenschmidt {
2386e4805922SOlaf Hering 	phandle ph;
2387e4805922SOlaf Hering 	u32 prop[6];
238826c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2389e8c0acf9SBenjamin Herrenschmidt 	char *name;
2390e8c0acf9SBenjamin Herrenschmidt 	int rc;
2391e8c0acf9SBenjamin Herrenschmidt 
2392e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2393e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2394e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
239526c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2396e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
239726c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
239826c5032eSBenjamin Herrenschmidt 	}
2399e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2400e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2401e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2402e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2403e8c0acf9SBenjamin Herrenschmidt 
2404e4805922SOlaf Hering 			prop[0] = 0x1;
2405e4805922SOlaf Hering 			prop[1] = 0x0;
2406e4805922SOlaf Hering 			prop[2] = rloc;
2407e4805922SOlaf Hering 			prop[3] = 0x0;
2408e4805922SOlaf Hering 			prop[4] = 0x0;
2409e4805922SOlaf Hering 			prop[5] = 0x00010000;
2410e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2411e4805922SOlaf Hering 		}
2412e4805922SOlaf Hering 	}
2413e4805922SOlaf Hering 
2414e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2415e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2416e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2417e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2418e4805922SOlaf Hering 		prop[0] = 14;
2419e4805922SOlaf Hering 		prop[1] = 0x0;
2420556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2421556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2422556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2423556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2424556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2425556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2426556ecf9bSOlaf Hering 		}
2427e4805922SOlaf Hering 	}
2428e8c0acf9SBenjamin Herrenschmidt }
2429e8c0acf9SBenjamin Herrenschmidt #else
2430e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2431e8c0acf9SBenjamin Herrenschmidt #endif
2432e8c0acf9SBenjamin Herrenschmidt 
24339b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
243454f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
243554f4ee18SHollis Blanchard {
24369b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
24379b6b563cSPaul Mackerras 	u32 u3_rev;
24389b6b563cSPaul Mackerras 	u32 interrupts[2];
24399b6b563cSPaul Mackerras 	u32 parent;
24409b6b563cSPaul Mackerras 
24419b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
24429b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
24439b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
24449b6b563cSPaul Mackerras 		return;
24459b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
24469b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
24479b6b563cSPaul Mackerras 		return;
24489b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
24499b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
24509b6b563cSPaul Mackerras 		return;
24519b6b563cSPaul Mackerras 
24529b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
24539b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
24549b6b563cSPaul Mackerras 	    == PROM_ERROR)
24559b6b563cSPaul Mackerras 		return;
24567d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
24579b6b563cSPaul Mackerras 		return;
24589b6b563cSPaul Mackerras 	/* does it need fixup ? */
24599b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
24609b6b563cSPaul Mackerras 		return;
24619b6b563cSPaul Mackerras 
24629b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
24639b6b563cSPaul Mackerras 
24649b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
24659b6b563cSPaul Mackerras 	interrupts[0] = 0;
24669b6b563cSPaul Mackerras 	interrupts[1] = 1;
2467a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2468a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
24699b6b563cSPaul Mackerras 	parent = (u32)mpic;
2470a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2471a23414beSPaul Mackerras 		     &parent, sizeof(parent));
24729b6b563cSPaul Mackerras }
247354f4ee18SHollis Blanchard #else
247454f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
247554f4ee18SHollis Blanchard #endif
24769b6b563cSPaul Mackerras 
247788fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
247894d2dde7SGrant Likely /*
247994d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
248094d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
248194d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
248294d2dde7SGrant Likely  * ethernet node.
248394d2dde7SGrant Likely  */
248494d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
248588fd2a9dSSylvain Munaut {
248688fd2a9dSSylvain Munaut 	u32 node;
248788fd2a9dSSylvain Munaut 	char prop[64];
248894d2dde7SGrant Likely 	int rv;
248988fd2a9dSSylvain Munaut 
249094d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
249194d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
249288fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
249388fd2a9dSSylvain Munaut 		return;
249488fd2a9dSSylvain Munaut 
249594d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
249694d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
249794d2dde7SGrant Likely 	if (!rv)
249888fd2a9dSSylvain Munaut 		return;
249988fd2a9dSSylvain Munaut 
250094d2dde7SGrant Likely 	/*
250194d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
250294d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
250394d2dde7SGrant Likely 	 */
250488fd2a9dSSylvain Munaut 
250594d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
25066f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
25076f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
25086f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
25096f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
25106f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
25116f4347c9SOlaf Hering 			" new-device"
25126f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
25136f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
251494d2dde7SGrant Likely 				" s\" mdio\" device-name"
251594d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
25166f4347c9SOlaf Hering 				" s\" compatible\" property"
25176f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
25186f4347c9SOlaf Hering 				" 0x2 encode-int"
25196f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
25206f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
25216f4347c9SOlaf Hering 				" s\" interrupts\" property"
25226f4347c9SOlaf Hering 			" finish-device");
25236f4347c9SOlaf Hering 	};
25246f4347c9SOlaf Hering 
252594d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
252694d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
252794d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
252894d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
25296f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
25306f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
25316f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
25326f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
25336f4347c9SOlaf Hering 			" new-device"
25346f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
25356f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
25366f4347c9SOlaf Hering 				" my-self"
25376f4347c9SOlaf Hering 				" ihandle>phandle"
25386f4347c9SOlaf Hering 			" finish-device"
25396f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
25406f4347c9SOlaf Hering 				" encode-int"
25416f4347c9SOlaf Hering 				" s\" phy-handle\" property"
25426f4347c9SOlaf Hering 			" device-end");
25436f4347c9SOlaf Hering 	}
254494d2dde7SGrant Likely }
25456f4347c9SOlaf Hering 
254694d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
254794d2dde7SGrant Likely {
254894d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
254994d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
255094d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
255194d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
255294d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
255394d2dde7SGrant Likely 	u32 node;
255494d2dde7SGrant Likely 	char prop[64];
255594d2dde7SGrant Likely 	int rv, len;
255694d2dde7SGrant Likely 
255794d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
255894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
255994d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
256094d2dde7SGrant Likely 		return;
256194d2dde7SGrant Likely 
256294d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
256394d2dde7SGrant Likely 	if (rv == PROM_ERROR)
256494d2dde7SGrant Likely 		return;
256594d2dde7SGrant Likely 	if (strcmp(prop, "EFIKA5K2"))
256694d2dde7SGrant Likely 		return;
256794d2dde7SGrant Likely 
256894d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
256994d2dde7SGrant Likely 
257094d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
257194d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
257294d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
257394d2dde7SGrant Likely 	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
257494d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
257594d2dde7SGrant Likely 
25767f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
25777f4392cdSDavid Woodhouse 	   fix that too */
25787f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
25797f4392cdSDavid Woodhouse 	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
25807f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
25817f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
25827f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
25837f4392cdSDavid Woodhouse 
258494d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
258594d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
258694d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
258794d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
258894d2dde7SGrant Likely 		if (len == 12) {
258994d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
259094d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
259194d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
259294d2dde7SGrant Likely 		}
259394d2dde7SGrant Likely 	}
259494d2dde7SGrant Likely 
259594d2dde7SGrant Likely 	/* Fixup sound interrupts property */
259694d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
259794d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
259894d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
259994d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
260094d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
260194d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
260294d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
260394d2dde7SGrant Likely 		}
260494d2dde7SGrant Likely 	}
260594d2dde7SGrant Likely 
260694d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
260794d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
260888fd2a9dSSylvain Munaut }
260988fd2a9dSSylvain Munaut #else
261088fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
261188fd2a9dSSylvain Munaut #endif
261288fd2a9dSSylvain Munaut 
261354f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
261454f4ee18SHollis Blanchard {
261554f4ee18SHollis Blanchard 	fixup_device_tree_maple();
26168f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
2617e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
261854f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
261988fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
262054f4ee18SHollis Blanchard }
26219b6b563cSPaul Mackerras 
26229b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
26239b6b563cSPaul Mackerras {
2624493adffcSBenjamin Herrenschmidt 	__be32 rval;
26259b6b563cSPaul Mackerras 	ihandle prom_cpu;
26269b6b563cSPaul Mackerras 	phandle cpu_pkg;
26279b6b563cSPaul Mackerras 
2628493adffcSBenjamin Herrenschmidt 	rval = 0;
2629493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
2630a575b807SPaul Mackerras 		return;
2631493adffcSBenjamin Herrenschmidt 	prom_cpu = be32_to_cpu(rval);
26329b6b563cSPaul Mackerras 
26339b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
26349b6b563cSPaul Mackerras 
2635493adffcSBenjamin Herrenschmidt 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
2636493adffcSBenjamin Herrenschmidt 	prom.cpu = be32_to_cpu(rval);
26379b6b563cSPaul Mackerras 
26385827d416SAnton Blanchard 	prom_debug("Booting CPU hw index = %lu\n", prom.cpu);
26399b6b563cSPaul Mackerras }
26409b6b563cSPaul Mackerras 
26419b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
26429b6b563cSPaul Mackerras {
26439b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
26449b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
2645493adffcSBenjamin Herrenschmidt 		__be64 val;
26469b6b563cSPaul Mackerras 
26475827d416SAnton Blanchard 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
26485827d416SAnton Blanchard 		prom_initrd_end = prom_initrd_start + r4;
26499b6b563cSPaul Mackerras 
2650493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_start);
26515827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
2652a23414beSPaul Mackerras 			     &val, sizeof(val));
2653493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_end);
26545827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
2655a23414beSPaul Mackerras 			     &val, sizeof(val));
26569b6b563cSPaul Mackerras 
26575827d416SAnton Blanchard 		reserve_mem(prom_initrd_start,
26585827d416SAnton Blanchard 			    prom_initrd_end - prom_initrd_start);
26599b6b563cSPaul Mackerras 
26605827d416SAnton Blanchard 		prom_debug("initrd_start=0x%x\n", prom_initrd_start);
26615827d416SAnton Blanchard 		prom_debug("initrd_end=0x%x\n", prom_initrd_end);
26629b6b563cSPaul Mackerras 	}
26639b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
26649b6b563cSPaul Mackerras }
26659b6b563cSPaul Mackerras 
26665ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64
26675ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE
26685ac47f7aSAnton Blanchard static void reloc_toc(void)
26695ac47f7aSAnton Blanchard {
26705ac47f7aSAnton Blanchard }
26715ac47f7aSAnton Blanchard 
26725ac47f7aSAnton Blanchard static void unreloc_toc(void)
26735ac47f7aSAnton Blanchard {
26745ac47f7aSAnton Blanchard }
26755ac47f7aSAnton Blanchard #else
26761674400aSAnton Blanchard static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
26775ac47f7aSAnton Blanchard {
26785ac47f7aSAnton Blanchard 	unsigned long i;
26791674400aSAnton Blanchard 	unsigned long *toc_entry;
26801674400aSAnton Blanchard 
26811674400aSAnton Blanchard 	/* Get the start of the TOC by using r2 directly. */
26821674400aSAnton Blanchard 	asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
26835ac47f7aSAnton Blanchard 
26845ac47f7aSAnton Blanchard 	for (i = 0; i < nr_entries; i++) {
26855ac47f7aSAnton Blanchard 		*toc_entry = *toc_entry + offset;
26865ac47f7aSAnton Blanchard 		toc_entry++;
26875ac47f7aSAnton Blanchard 	}
26885ac47f7aSAnton Blanchard }
26895ac47f7aSAnton Blanchard 
26905ac47f7aSAnton Blanchard static void reloc_toc(void)
26915ac47f7aSAnton Blanchard {
26925ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
26935ac47f7aSAnton Blanchard 	unsigned long nr_entries =
26945ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
26955ac47f7aSAnton Blanchard 
26961674400aSAnton Blanchard 	__reloc_toc(offset, nr_entries);
26975ac47f7aSAnton Blanchard 
26985ac47f7aSAnton Blanchard 	mb();
26995ac47f7aSAnton Blanchard }
27005ac47f7aSAnton Blanchard 
27015ac47f7aSAnton Blanchard static void unreloc_toc(void)
27025ac47f7aSAnton Blanchard {
27035ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
27045ac47f7aSAnton Blanchard 	unsigned long nr_entries =
27055ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
27065ac47f7aSAnton Blanchard 
27075ac47f7aSAnton Blanchard 	mb();
27085ac47f7aSAnton Blanchard 
27091674400aSAnton Blanchard 	__reloc_toc(-offset, nr_entries);
27105ac47f7aSAnton Blanchard }
27115ac47f7aSAnton Blanchard #endif
27125ac47f7aSAnton Blanchard #endif
271327f44888SBenjamin Herrenschmidt 
27149b6b563cSPaul Mackerras /*
27159b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
27169b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
27179b6b563cSPaul Mackerras  */
27189b6b563cSPaul Mackerras 
27199b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
27209b6b563cSPaul Mackerras 			       unsigned long pp,
2721549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
2722549e8152SPaul Mackerras 			       unsigned long kbase)
27239b6b563cSPaul Mackerras {
27249b6b563cSPaul Mackerras 	unsigned long hdr;
27259b6b563cSPaul Mackerras 
27269b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
2727549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
27289b6b563cSPaul Mackerras 	reloc_got2(offset);
27295ac47f7aSAnton Blanchard #else
27305ac47f7aSAnton Blanchard 	reloc_toc();
27319b6b563cSPaul Mackerras #endif
27329b6b563cSPaul Mackerras 
27339b6b563cSPaul Mackerras 	/*
27349b6b563cSPaul Mackerras 	 * First zero the BSS
27359b6b563cSPaul Mackerras 	 */
27365827d416SAnton Blanchard 	memset(&__bss_start, 0, __bss_stop - __bss_start);
27379b6b563cSPaul Mackerras 
27389b6b563cSPaul Mackerras 	/*
27399b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
27409b6b563cSPaul Mackerras 	 * like /chosen
27419b6b563cSPaul Mackerras 	 */
27429b6b563cSPaul Mackerras 	prom_init_client_services(pp);
27439b6b563cSPaul Mackerras 
27449b6b563cSPaul Mackerras 	/*
2745a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
2746a23414beSPaul Mackerras 	 * and other workarounds
2747a23414beSPaul Mackerras 	 */
2748a23414beSPaul Mackerras 	prom_find_mmu();
2749a23414beSPaul Mackerras 
2750a23414beSPaul Mackerras 	/*
27519b6b563cSPaul Mackerras 	 * Init prom stdout device
27529b6b563cSPaul Mackerras 	 */
27539b6b563cSPaul Mackerras 	prom_init_stdout();
27549b6b563cSPaul Mackerras 
27555827d416SAnton Blanchard 	prom_printf("Preparing to boot %s", linux_banner);
2756e7943fbbSMichael Ellerman 
27579b6b563cSPaul Mackerras 	/*
27589b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
27599b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
27609b6b563cSPaul Mackerras 	 */
27615827d416SAnton Blanchard 	of_platform = prom_find_machine_type();
27625827d416SAnton Blanchard 	prom_printf("Detected machine type: %x\n", of_platform);
27639b6b563cSPaul Mackerras 
27640f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
2765add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
2766add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
2767add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
2768549e8152SPaul Mackerras #endif
2769add60ef3SOlaf Hering 
2770add60ef3SOlaf Hering 	/*
2771add60ef3SOlaf Hering 	 * Check for an initrd
2772add60ef3SOlaf Hering 	 */
2773add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
2774add60ef3SOlaf Hering 
277527f44888SBenjamin Herrenschmidt #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
27769b6b563cSPaul Mackerras 	/*
27779b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
27789b6b563cSPaul Mackerras 	 */
27795827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES ||
27805827d416SAnton Blanchard 	    of_platform == PLATFORM_PSERIES_LPAR)
27819b6b563cSPaul Mackerras 		prom_send_capabilities();
27829b6b563cSPaul Mackerras #endif
27839b6b563cSPaul Mackerras 
27849b6b563cSPaul Mackerras 	/*
2785f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
27869b6b563cSPaul Mackerras 	 */
27875827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC)
2788549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
27899b6b563cSPaul Mackerras 
27909b6b563cSPaul Mackerras 	/*
27919b6b563cSPaul Mackerras 	 * Do early parsing of command line
27929b6b563cSPaul Mackerras 	 */
27939b6b563cSPaul Mackerras 	early_cmdline_parse();
27949b6b563cSPaul Mackerras 
27959b6b563cSPaul Mackerras 	/*
27969b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
27979b6b563cSPaul Mackerras 	 */
27989b6b563cSPaul Mackerras 	prom_init_mem();
27999b6b563cSPaul Mackerras 
28009b6b563cSPaul Mackerras 	/*
28019b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
28029b6b563cSPaul Mackerras 	 */
28039b6b563cSPaul Mackerras 	prom_find_boot_cpu();
28049b6b563cSPaul Mackerras 
28059b6b563cSPaul Mackerras 	/*
28069b6b563cSPaul Mackerras 	 * Initialize display devices
28079b6b563cSPaul Mackerras 	 */
28089b6b563cSPaul Mackerras 	prom_check_displays();
28099b6b563cSPaul Mackerras 
2810493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
28119b6b563cSPaul Mackerras 	/*
28129b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
28139b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
28149b6b563cSPaul Mackerras 	 * available for us here...
28159b6b563cSPaul Mackerras 	 */
28165827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES)
28179b6b563cSPaul Mackerras 		prom_initialize_tce_table();
28189b6b563cSPaul Mackerras #endif
28199b6b563cSPaul Mackerras 
28209b6b563cSPaul Mackerras 	/*
282127f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
282227f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
28239b6b563cSPaul Mackerras 	 */
28245827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
28255827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
28269b6b563cSPaul Mackerras 		prom_instantiate_rtas();
282727f44888SBenjamin Herrenschmidt 
282827f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
2829493adffcSBenjamin Herrenschmidt 	if (of_platform == PLATFORM_OPAL)
28306e35d5daSBenjamin Herrenschmidt 		prom_instantiate_opal();
2831493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV */
283227f44888SBenjamin Herrenschmidt 
28334a727429SAshley Lai #ifdef CONFIG_PPC64
28344a727429SAshley Lai 	/* instantiate sml */
28354a727429SAshley Lai 	prom_instantiate_sml();
28364a727429SAshley Lai #endif
28374a727429SAshley Lai 
283827f44888SBenjamin Herrenschmidt 	/*
283927f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
284027f44888SBenjamin Herrenschmidt 	 *
284127f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
2842dbe78b40SBenjamin Herrenschmidt 	 *
2843dbe78b40SBenjamin Herrenschmidt 	 * (This must be done after instanciating RTAS)
284427f44888SBenjamin Herrenschmidt 	 */
28455827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
28465827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
284727f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
28489b6b563cSPaul Mackerras 
28499b6b563cSPaul Mackerras 	/*
28509b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
28519b6b563cSPaul Mackerras 	 */
2852493adffcSBenjamin Herrenschmidt 	if (prom_memory_limit) {
2853493adffcSBenjamin Herrenschmidt 		__be64 val = cpu_to_be64(prom_memory_limit);
28545827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
2855493adffcSBenjamin Herrenschmidt 			     &val, sizeof(val));
2856493adffcSBenjamin Herrenschmidt 	}
28579b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
28585827d416SAnton Blanchard 	if (prom_iommu_off)
28595827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
2860a23414beSPaul Mackerras 			     NULL, 0);
28619b6b563cSPaul Mackerras 
28625827d416SAnton Blanchard 	if (prom_iommu_force_on)
28635827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
2864a23414beSPaul Mackerras 			     NULL, 0);
28659b6b563cSPaul Mackerras 
28665827d416SAnton Blanchard 	if (prom_tce_alloc_start) {
28675827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
28685827d416SAnton Blanchard 			     &prom_tce_alloc_start,
28699b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
28705827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
28715827d416SAnton Blanchard 			     &prom_tce_alloc_end,
28729b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
28739b6b563cSPaul Mackerras 	}
28749b6b563cSPaul Mackerras #endif
28759b6b563cSPaul Mackerras 
28769b6b563cSPaul Mackerras 	/*
28779b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
28789b6b563cSPaul Mackerras 	 */
28799b6b563cSPaul Mackerras 	fixup_device_tree();
28809b6b563cSPaul Mackerras 
28819b6b563cSPaul Mackerras 	/*
28829b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
28839b6b563cSPaul Mackerras 	 */
28849b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
28859b6b563cSPaul Mackerras 	flatten_device_tree();
28869b6b563cSPaul Mackerras 
28873825ac0eSPaul Mackerras 	/*
28883825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
28893825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
289040dfef66SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101). It
289140dfef66SBenjamin Herrenschmidt 	 * appears that the OPAL version of OFW doesn't like it either.
28923825ac0eSPaul Mackerras 	 */
28935827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
28945827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
28959b6b563cSPaul Mackerras 		prom_close_stdin();
28969b6b563cSPaul Mackerras 
28979b6b563cSPaul Mackerras 	/*
28989b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
28999b6b563cSPaul Mackerras 	 * devices etc...
29009b6b563cSPaul Mackerras 	 */
2901*7e862d7eSMichael Ellerman 	prom_printf("Quiescing Open Firmware ...\n");
29029b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
29039b6b563cSPaul Mackerras 
29049b6b563cSPaul Mackerras 	/*
29059b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
29069b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
29079b6b563cSPaul Mackerras 	 * is common to us and kexec
29089b6b563cSPaul Mackerras 	 */
29095827d416SAnton Blanchard 	hdr = dt_header_start;
291040dfef66SBenjamin Herrenschmidt 
291140dfef66SBenjamin Herrenschmidt 	/* Don't print anything after quiesce under OPAL, it crashes OFW */
29125827d416SAnton Blanchard 	if (of_platform != PLATFORM_OPAL) {
2913*7e862d7eSMichael Ellerman 		prom_printf("Booting Linux via __start() ...\n");
29149b6b563cSPaul Mackerras 		prom_debug("->dt_header_start=0x%x\n", hdr);
291540dfef66SBenjamin Herrenschmidt 	}
29169b6b563cSPaul Mackerras 
29179b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
29189b6b563cSPaul Mackerras 	reloc_got2(-offset);
29195ac47f7aSAnton Blanchard #else
29205ac47f7aSAnton Blanchard 	unreloc_toc();
29219b6b563cSPaul Mackerras #endif
29229b6b563cSPaul Mackerras 
29236e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
29246e35d5daSBenjamin Herrenschmidt 	/* OPAL early debug gets the OPAL base & entry in r8 and r9 */
29256e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0,
29265827d416SAnton Blanchard 		prom_opal_base, prom_opal_entry);
29276e35d5daSBenjamin Herrenschmidt #else
29286e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
29296e35d5daSBenjamin Herrenschmidt #endif
29309b6b563cSPaul Mackerras 
29319b6b563cSPaul Mackerras 	return 0;
29329b6b563cSPaul Mackerras }
2933