xref: /linux/arch/powerpc/kernel/prom_init.c (revision d03d1d65b5570ab6fba462a09c2b3274dca80ba9)
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/iommu.h>
419b6b563cSPaul Mackerras #include <asm/btext.h>
429b6b563cSPaul Mackerras #include <asm/sections.h>
439b6b563cSPaul Mackerras #include <asm/machdep.h>
4427f44888SBenjamin Herrenschmidt #include <asm/opal.h>
450545d543SDaniel Axtens #include <asm/asm-prototypes.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 	}
3931b855e16SDaniel Axtens 	va_end(args);
3949b6b563cSPaul Mackerras }
3959b6b563cSPaul Mackerras 
3969b6b563cSPaul Mackerras 
397a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
398a575b807SPaul Mackerras 				unsigned long align)
399a575b807SPaul Mackerras {
400a575b807SPaul Mackerras 
401a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
402a23414beSPaul Mackerras 		/*
403a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
404a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
405a23414beSPaul Mackerras 		 */
406a23414beSPaul Mackerras 		int ret;
407a23414beSPaul Mackerras 		prom_arg_t result;
408a23414beSPaul Mackerras 
409a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
4105827d416SAnton Blanchard 				    ADDR("claim"), prom.memory,
411a23414beSPaul Mackerras 				    align, size, virt);
412a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
413a23414beSPaul Mackerras 			return -1;
414a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
4155827d416SAnton Blanchard 				    ADDR("claim"), prom.mmumap,
416a23414beSPaul Mackerras 				    align, size, virt);
417a23414beSPaul Mackerras 		if (ret != 0) {
418a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
4195827d416SAnton Blanchard 				  prom.memory, size, virt);
420a23414beSPaul Mackerras 			return -1;
421a23414beSPaul Mackerras 		}
422a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
423a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
4245827d416SAnton Blanchard 			  ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
425a23414beSPaul Mackerras 		return virt;
426a23414beSPaul Mackerras 	}
427a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
428a23414beSPaul Mackerras 			 (prom_arg_t)align);
429a575b807SPaul Mackerras }
430a575b807SPaul Mackerras 
4319b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
4329b6b563cSPaul Mackerras {
4339b6b563cSPaul Mackerras 	prom_print(reason);
434add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
435add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
4365827d416SAnton Blanchard 	if (of_platform == PLATFORM_POWERMAC)
437add60ef3SOlaf Hering 		asm("trap\n");
438add60ef3SOlaf Hering 
4391d9a4731SStephen Rothwell 	/* ToDo: should put up an SRC here on pSeries */
4409b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
4419b6b563cSPaul Mackerras 
4429b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
4439b6b563cSPaul Mackerras 		;
4449b6b563cSPaul Mackerras }
4459b6b563cSPaul Mackerras 
4469b6b563cSPaul Mackerras 
4479b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
4489b6b563cSPaul Mackerras {
4499b6b563cSPaul Mackerras 	phandle node;
4509b6b563cSPaul Mackerras 
4519b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
4529b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
4539b6b563cSPaul Mackerras 		return 1;
4549b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4559b6b563cSPaul Mackerras 		return 1;
4569b6b563cSPaul Mackerras 	for (;;) {
4579b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
4589b6b563cSPaul Mackerras 			return 0;
4599b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4609b6b563cSPaul Mackerras 			return 1;
4619b6b563cSPaul Mackerras 	}
4629b6b563cSPaul Mackerras }
4639b6b563cSPaul Mackerras 
46460d862e5STobias Klauser static inline int prom_getprop(phandle node, const char *pname,
4659b6b563cSPaul Mackerras 			       void *value, size_t valuelen)
4669b6b563cSPaul Mackerras {
4679b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
4689b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
4699b6b563cSPaul Mackerras }
4709b6b563cSPaul Mackerras 
47160d862e5STobias Klauser static inline int prom_getproplen(phandle node, const char *pname)
4729b6b563cSPaul Mackerras {
4739b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
4749b6b563cSPaul Mackerras }
4759b6b563cSPaul Mackerras 
476a23414beSPaul Mackerras static void add_string(char **str, const char *q)
4779b6b563cSPaul Mackerras {
478a23414beSPaul Mackerras 	char *p = *str;
479a23414beSPaul Mackerras 
480a23414beSPaul Mackerras 	while (*q)
481a23414beSPaul Mackerras 		*p++ = *q++;
482a23414beSPaul Mackerras 	*p++ = ' ';
483a23414beSPaul Mackerras 	*str = p;
484a23414beSPaul Mackerras }
485a23414beSPaul Mackerras 
486a23414beSPaul Mackerras static char *tohex(unsigned int x)
487a23414beSPaul Mackerras {
488a23414beSPaul Mackerras 	static char digits[] = "0123456789abcdef";
489a23414beSPaul Mackerras 	static char result[9];
490a23414beSPaul Mackerras 	int i;
491a23414beSPaul Mackerras 
492a23414beSPaul Mackerras 	result[8] = 0;
493a23414beSPaul Mackerras 	i = 8;
494a23414beSPaul Mackerras 	do {
495a23414beSPaul Mackerras 		--i;
496a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
497a23414beSPaul Mackerras 		x >>= 4;
498a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
499a23414beSPaul Mackerras 	return &result[i];
500a23414beSPaul Mackerras }
501a23414beSPaul Mackerras 
502a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
503a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
504a23414beSPaul Mackerras {
505a23414beSPaul Mackerras 	char cmd[256], *p;
506a23414beSPaul Mackerras 
507a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
5089b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
5099b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
510a23414beSPaul Mackerras 
511a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
512a23414beSPaul Mackerras 	p = cmd;
513a23414beSPaul Mackerras 	add_string(&p, "dev");
514a23414beSPaul Mackerras 	add_string(&p, nodename);
515a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
516a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
517a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
5185827d416SAnton Blanchard 	add_string(&p, tohex(strlen(pname)));
519a23414beSPaul Mackerras 	add_string(&p, "property");
520a23414beSPaul Mackerras 	*p = 0;
521a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
5229b6b563cSPaul Mackerras }
5239b6b563cSPaul Mackerras 
5245827d416SAnton Blanchard /* We can't use the standard versions because of relocation headaches. */
525cf68787bSBenjamin Krill #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
526cf68787bSBenjamin Krill 			 || ('a' <= (c) && (c) <= 'f') \
527cf68787bSBenjamin Krill 			 || ('A' <= (c) && (c) <= 'F'))
528cf68787bSBenjamin Krill 
529cf68787bSBenjamin Krill #define isdigit(c)	('0' <= (c) && (c) <= '9')
530cf68787bSBenjamin Krill #define islower(c)	('a' <= (c) && (c) <= 'z')
531cf68787bSBenjamin Krill #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
532cf68787bSBenjamin Krill 
533493adffcSBenjamin Herrenschmidt static unsigned long prom_strtoul(const char *cp, const char **endp)
534cf68787bSBenjamin Krill {
535cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
536cf68787bSBenjamin Krill 
537cf68787bSBenjamin Krill 	if (*cp == '0') {
538cf68787bSBenjamin Krill 		base = 8;
539cf68787bSBenjamin Krill 		cp++;
540cf68787bSBenjamin Krill 		if (toupper(*cp) == 'X') {
541cf68787bSBenjamin Krill 			cp++;
542cf68787bSBenjamin Krill 			base = 16;
543cf68787bSBenjamin Krill 		}
544cf68787bSBenjamin Krill 	}
545cf68787bSBenjamin Krill 
546cf68787bSBenjamin Krill 	while (isxdigit(*cp) &&
547cf68787bSBenjamin Krill 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
548cf68787bSBenjamin Krill 		result = result * base + value;
549cf68787bSBenjamin Krill 		cp++;
550cf68787bSBenjamin Krill 	}
551cf68787bSBenjamin Krill 
552cf68787bSBenjamin Krill 	if (endp)
553cf68787bSBenjamin Krill 		*endp = cp;
554cf68787bSBenjamin Krill 
555cf68787bSBenjamin Krill 	return result;
556cf68787bSBenjamin Krill }
557cf68787bSBenjamin Krill 
558493adffcSBenjamin Herrenschmidt static unsigned long prom_memparse(const char *ptr, const char **retptr)
559cf68787bSBenjamin Krill {
560cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
561cf68787bSBenjamin Krill 	int shift = 0;
562cf68787bSBenjamin Krill 
563cf68787bSBenjamin Krill 	/*
564cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
565cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
566cf68787bSBenjamin Krill 	 * the address we're linked at.
567cf68787bSBenjamin Krill 	 */
568cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
569cf68787bSBenjamin Krill 		shift = 30;
570cf68787bSBenjamin Krill 
571cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
572cf68787bSBenjamin Krill 		shift = 20;
573cf68787bSBenjamin Krill 
574cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
575cf68787bSBenjamin Krill 		shift = 10;
576cf68787bSBenjamin Krill 
577cf68787bSBenjamin Krill 	if (shift) {
578cf68787bSBenjamin Krill 		ret <<= shift;
579cf68787bSBenjamin Krill 		(*retptr)++;
580cf68787bSBenjamin Krill 	}
581cf68787bSBenjamin Krill 
582cf68787bSBenjamin Krill 	return ret;
583cf68787bSBenjamin Krill }
584cf68787bSBenjamin Krill 
5859b6b563cSPaul Mackerras /*
5869b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
5879b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
5889b6b563cSPaul Mackerras  */
5899b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
5909b6b563cSPaul Mackerras {
591cc5d0189SBenjamin Herrenschmidt 	const char *opt;
592cf68787bSBenjamin Krill 
593cc5d0189SBenjamin Herrenschmidt 	char *p;
5949b6b563cSPaul Mackerras 	int l = 0;
5959b6b563cSPaul Mackerras 
5965827d416SAnton Blanchard 	prom_cmd_line[0] = 0;
5975827d416SAnton Blanchard 	p = prom_cmd_line;
5985827d416SAnton Blanchard 	if ((long)prom.chosen > 0)
5995827d416SAnton Blanchard 		l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
6009b6b563cSPaul Mackerras #ifdef CONFIG_CMDLINE
6010e4aa9c2SAmos Waterland 	if (l <= 0 || p[0] == '\0') /* dbl check */
6025827d416SAnton Blanchard 		strlcpy(prom_cmd_line,
6035827d416SAnton Blanchard 			CONFIG_CMDLINE, sizeof(prom_cmd_line));
6049b6b563cSPaul Mackerras #endif /* CONFIG_CMDLINE */
6055827d416SAnton Blanchard 	prom_printf("command line: %s\n", prom_cmd_line);
6069b6b563cSPaul Mackerras 
6079b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
6085827d416SAnton Blanchard 	opt = strstr(prom_cmd_line, "iommu=");
6099b6b563cSPaul Mackerras 	if (opt) {
6109b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
6119b6b563cSPaul Mackerras 		opt += 6;
6129b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
6139b6b563cSPaul Mackerras 			opt++;
6145827d416SAnton Blanchard 		if (!strncmp(opt, "off", 3))
6155827d416SAnton Blanchard 			prom_iommu_off = 1;
6165827d416SAnton Blanchard 		else if (!strncmp(opt, "force", 5))
6175827d416SAnton Blanchard 			prom_iommu_force_on = 1;
6189b6b563cSPaul Mackerras 	}
6199b6b563cSPaul Mackerras #endif
6205827d416SAnton Blanchard 	opt = strstr(prom_cmd_line, "mem=");
621cf68787bSBenjamin Krill 	if (opt) {
622cf68787bSBenjamin Krill 		opt += 4;
6235827d416SAnton Blanchard 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
624cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
625cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
6265827d416SAnton Blanchard 		prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
627cf68787bSBenjamin Krill #endif
628cf68787bSBenjamin Krill 	}
6299b6b563cSPaul Mackerras }
6309b6b563cSPaul Mackerras 
63127f44888SBenjamin Herrenschmidt #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
6329b6b563cSPaul Mackerras /*
633530b5e14SNathan Fontenot  * The architecture vector has an array of PVR mask/value pairs,
634530b5e14SNathan Fontenot  * followed by # option vectors - 1, followed by the option vectors.
635530b5e14SNathan Fontenot  *
636530b5e14SNathan Fontenot  * See prom.h for the definition of the bits specified in the
637530b5e14SNathan Fontenot  * architecture vector.
638f709bfacSPaul Mackerras  *
639f709bfacSPaul Mackerras  * Because the description vector contains a mix of byte and word
640f709bfacSPaul Mackerras  * values, we declare it as an unsigned char array, and use this
641f709bfacSPaul Mackerras  * macro to put word values in.
642f709bfacSPaul Mackerras  */
643f709bfacSPaul Mackerras #define W(x)	((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
644f709bfacSPaul Mackerras 		((x) >> 8) & 0xff, (x) & 0xff
645f709bfacSPaul Mackerras 
646e8a4fd0aSMichael Ellerman /* Firmware expects the value to be n - 1, where n is the # of vectors */
647e8a4fd0aSMichael Ellerman #define NUM_VECTORS(n)		((n) - 1)
648e8a4fd0aSMichael Ellerman 
649e8a4fd0aSMichael Ellerman /*
650e8a4fd0aSMichael Ellerman  * Firmware expects 1 + n - 2, where n is the length of the option vector in
651e8a4fd0aSMichael Ellerman  * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
652e8a4fd0aSMichael Ellerman  */
653e8a4fd0aSMichael Ellerman #define VECTOR_LENGTH(n)	(1 + (n) - 2)
654e8a4fd0aSMichael Ellerman 
655530b5e14SNathan Fontenot unsigned char ibm_architecture_vec[] = {
656f709bfacSPaul Mackerras 	W(0xfffe0000), W(0x003a0000),	/* POWER5/POWER5+ */
65703054d51SAnton Blanchard 	W(0xffff0000), W(0x003e0000),	/* POWER6 */
658e952e6c4SMichael Neuling 	W(0xffff0000), W(0x003f0000),	/* POWER7 */
65933959f88SMichael Neuling 	W(0xffff0000), W(0x004b0000),	/* POWER8E */
6607cc85103SThomas Huth 	W(0xffff0000), W(0x004c0000),   /* POWER8NVL */
66133959f88SMichael Neuling 	W(0xffff0000), W(0x004d0000),	/* POWER8 */
662df77c799SMichael Neuling 	W(0xffffffff), W(0x0f000004),	/* all 2.07-compliant */
6630cb99013SJoel Schopp 	W(0xffffffff), W(0x0f000003),	/* all 2.06-compliant */
6640efbc18aSPaul Mackerras 	W(0xffffffff), W(0x0f000002),	/* all 2.05-compliant */
665f709bfacSPaul Mackerras 	W(0xfffffffe), W(0x0f000001),	/* all 2.04-compliant and earlier */
666e8a4fd0aSMichael Ellerman 	NUM_VECTORS(6),			/* 6 option vectors */
667f709bfacSPaul Mackerras 
668f709bfacSPaul Mackerras 	/* option vector 1: processor architectures supported */
669e8a4fd0aSMichael Ellerman 	VECTOR_LENGTH(2),		/* length */
670f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
671f709bfacSPaul Mackerras 	OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
672df77c799SMichael Neuling 	OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
673f709bfacSPaul Mackerras 
674f709bfacSPaul Mackerras 	/* option vector 2: Open Firmware options supported */
675e8a4fd0aSMichael Ellerman 	VECTOR_LENGTH(33),		/* length */
676f709bfacSPaul Mackerras 	OV2_REAL_MODE,
677f709bfacSPaul Mackerras 	0, 0,
678f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_base */
679f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_size */
680f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_base */
681f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_size */
682f709bfacSPaul Mackerras 	W(0xffffffff),			/* load_base */
68333392640SAnton Blanchard 	W(256),				/* 256MB min RMA */
684f709bfacSPaul Mackerras 	W(0xffffffff),			/* full client load */
685f709bfacSPaul Mackerras 	0,				/* min RMA percentage of total RAM */
686f709bfacSPaul Mackerras 	48,				/* max log_2(hash table size) */
687f709bfacSPaul Mackerras 
688f709bfacSPaul Mackerras 	/* option vector 3: processor options supported */
689e8a4fd0aSMichael Ellerman 	VECTOR_LENGTH(2),		/* length */
690f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
691974a76f5SPaul Mackerras 	OV3_FP | OV3_VMX | OV3_DFP,
692f709bfacSPaul Mackerras 
693f709bfacSPaul Mackerras 	/* option vector 4: IBM PAPR implementation */
694e8a4fd0aSMichael Ellerman 	VECTOR_LENGTH(2),		/* length */
695f709bfacSPaul Mackerras 	0,				/* don't halt */
696404e32e4SRobert Jennings 	OV4_MIN_ENT_CAP,		/* minimum VP entitled capacity */
697f709bfacSPaul Mackerras 
698f709bfacSPaul Mackerras 	/* option vector 5: PAPR/OF options */
69966443efaSMichael Ellerman 	VECTOR_LENGTH(21),		/* length */
700f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
701f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
702f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
703f0ff7eb4SNathan Fontenot #ifdef CONFIG_PCI_MSI
704f0ff7eb4SNathan Fontenot 	/* PCIe/MSI support.  Without MSI full PCIe is not supported */
705f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_MSI),
706f0ff7eb4SNathan Fontenot #else
7078391e42aSNathan Fontenot 	0,
708f0ff7eb4SNathan Fontenot #endif
709f0ff7eb4SNathan Fontenot 	0,
710f0ff7eb4SNathan Fontenot #ifdef CONFIG_PPC_SMLPAR
711f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
712f0ff7eb4SNathan Fontenot #else
713f0ff7eb4SNathan Fontenot 	0,
714f0ff7eb4SNathan Fontenot #endif
7151b1218d3SNathan Fontenot 	OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
71628bb9ee1Sjschopp@austin.ibm.com 	0,
71728bb9ee1Sjschopp@austin.ibm.com 	0,
71828bb9ee1Sjschopp@austin.ibm.com 	0,
719efec959fSBenjamin Herrenschmidt 	/* WARNING: The offset of the "number of cores" field below
720efec959fSBenjamin Herrenschmidt 	 * must match by the macro below. Update the definition if
721efec959fSBenjamin Herrenschmidt 	 * the structure layout changes.
722efec959fSBenjamin Herrenschmidt 	 */
7232c2a63e3SMichael Ellerman #define IBM_ARCH_VEC_NRCORES_OFFSET	133
72428bb9ee1Sjschopp@austin.ibm.com 	W(NR_CPUS),			/* number of cores supported */
725828d2b59SKent Yoder 	0,
726828d2b59SKent Yoder 	0,
727828d2b59SKent Yoder 	0,
728828d2b59SKent Yoder 	0,
729f0ff7eb4SNathan Fontenot 	OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) |
73066443efaSMichael Ellerman 	OV5_FEAT(OV5_PFO_HW_842),				/* Byte 17 */
73166443efaSMichael Ellerman 	0,							/* Byte 18 */
73266443efaSMichael Ellerman 	0,							/* Byte 19 */
73366443efaSMichael Ellerman 	0,							/* Byte 20 */
73466443efaSMichael Ellerman 	OV5_FEAT(OV5_SUB_PROCESSORS),				/* Byte 21 */
735e8a4fd0aSMichael Ellerman 
73628bb9ee1Sjschopp@austin.ibm.com 	/* option vector 6: IBM PAPR hints */
737e8a4fd0aSMichael Ellerman 	VECTOR_LENGTH(3),		/* length */
73828bb9ee1Sjschopp@austin.ibm.com 	0,
73928bb9ee1Sjschopp@austin.ibm.com 	0,
74028bb9ee1Sjschopp@austin.ibm.com 	OV6_LINUX,
741f709bfacSPaul Mackerras };
742f709bfacSPaul Mackerras 
743*d03d1d65SMichael Ellerman struct option_vector1 {
744*d03d1d65SMichael Ellerman 	u8 byte1;
745*d03d1d65SMichael Ellerman 	u8 arch_versions;
746*d03d1d65SMichael Ellerman } __packed;
747*d03d1d65SMichael Ellerman 
748*d03d1d65SMichael Ellerman struct option_vector2 {
749*d03d1d65SMichael Ellerman 	u8 byte1;
750*d03d1d65SMichael Ellerman 	__be16 reserved;
751*d03d1d65SMichael Ellerman 	__be32 real_base;
752*d03d1d65SMichael Ellerman 	__be32 real_size;
753*d03d1d65SMichael Ellerman 	__be32 virt_base;
754*d03d1d65SMichael Ellerman 	__be32 virt_size;
755*d03d1d65SMichael Ellerman 	__be32 load_base;
756*d03d1d65SMichael Ellerman 	__be32 min_rma;
757*d03d1d65SMichael Ellerman 	__be32 min_load;
758*d03d1d65SMichael Ellerman 	u8 min_rma_percent;
759*d03d1d65SMichael Ellerman 	u8 max_pft_size;
760*d03d1d65SMichael Ellerman } __packed;
761*d03d1d65SMichael Ellerman 
762*d03d1d65SMichael Ellerman struct option_vector3 {
763*d03d1d65SMichael Ellerman 	u8 byte1;
764*d03d1d65SMichael Ellerman 	u8 byte2;
765*d03d1d65SMichael Ellerman } __packed;
766*d03d1d65SMichael Ellerman 
767*d03d1d65SMichael Ellerman struct option_vector4 {
768*d03d1d65SMichael Ellerman 	u8 byte1;
769*d03d1d65SMichael Ellerman 	u8 min_vp_cap;
770*d03d1d65SMichael Ellerman } __packed;
771*d03d1d65SMichael Ellerman 
772*d03d1d65SMichael Ellerman struct option_vector5 {
773*d03d1d65SMichael Ellerman 	u8 byte1;
774*d03d1d65SMichael Ellerman 	u8 byte2;
775*d03d1d65SMichael Ellerman 	u8 byte3;
776*d03d1d65SMichael Ellerman 	u8 cmo;
777*d03d1d65SMichael Ellerman 	u8 associativity;
778*d03d1d65SMichael Ellerman 	u8 bin_opts;
779*d03d1d65SMichael Ellerman 	u8 micro_checkpoint;
780*d03d1d65SMichael Ellerman 	u8 reserved0;
781*d03d1d65SMichael Ellerman 	__be32 max_cpus;
782*d03d1d65SMichael Ellerman 	__be16 papr_level;
783*d03d1d65SMichael Ellerman 	__be16 reserved1;
784*d03d1d65SMichael Ellerman 	u8 platform_facilities;
785*d03d1d65SMichael Ellerman 	u8 reserved2;
786*d03d1d65SMichael Ellerman 	__be16 reserved3;
787*d03d1d65SMichael Ellerman 	u8 subprocessors;
788*d03d1d65SMichael Ellerman } __packed;
789*d03d1d65SMichael Ellerman 
790*d03d1d65SMichael Ellerman struct option_vector6 {
791*d03d1d65SMichael Ellerman 	u8 reserved;
792*d03d1d65SMichael Ellerman 	u8 secondary_pteg;
793*d03d1d65SMichael Ellerman 	u8 os_name;
794*d03d1d65SMichael Ellerman } __packed;
795*d03d1d65SMichael Ellerman 
796493adffcSBenjamin Herrenschmidt /* Old method - ELF header with PT_NOTE sections only works on BE */
797493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
7989b6b563cSPaul Mackerras static struct fake_elf {
7999b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
8009b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
8019b6b563cSPaul Mackerras 	struct chrpnote {
8029b6b563cSPaul Mackerras 		u32	namesz;
8039b6b563cSPaul Mackerras 		u32	descsz;
8049b6b563cSPaul Mackerras 		u32	type;
8059b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
8069b6b563cSPaul Mackerras 		struct chrpdesc {
8079b6b563cSPaul Mackerras 			u32	real_mode;
8089b6b563cSPaul Mackerras 			u32	real_base;
8099b6b563cSPaul Mackerras 			u32	real_size;
8109b6b563cSPaul Mackerras 			u32	virt_base;
8119b6b563cSPaul Mackerras 			u32	virt_size;
8129b6b563cSPaul Mackerras 			u32	load_base;
8139b6b563cSPaul Mackerras 		} chrpdesc;
8149b6b563cSPaul Mackerras 	} chrpnote;
8159b6b563cSPaul Mackerras 	struct rpanote {
8169b6b563cSPaul Mackerras 		u32	namesz;
8179b6b563cSPaul Mackerras 		u32	descsz;
8189b6b563cSPaul Mackerras 		u32	type;
8199b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
8209b6b563cSPaul Mackerras 		struct rpadesc {
8219b6b563cSPaul Mackerras 			u32	lpar_affinity;
8229b6b563cSPaul Mackerras 			u32	min_rmo_size;
8239b6b563cSPaul Mackerras 			u32	min_rmo_percent;
8249b6b563cSPaul Mackerras 			u32	max_pft_size;
8259b6b563cSPaul Mackerras 			u32	splpar;
8269b6b563cSPaul Mackerras 			u32	min_load;
8279b6b563cSPaul Mackerras 			u32	new_mem_def;
8289b6b563cSPaul Mackerras 			u32	ignore_me;
8299b6b563cSPaul Mackerras 		} rpadesc;
8309b6b563cSPaul Mackerras 	} rpanote;
8315663a123SPaul Mackerras } fake_elf = {
8329b6b563cSPaul Mackerras 	.elfhdr = {
8339b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
8349b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
8359b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
8369b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
8379b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
8389b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
8399b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
8409b6b563cSPaul Mackerras 		.e_phnum = 2
8419b6b563cSPaul Mackerras 	},
8429b6b563cSPaul Mackerras 	.phdr = {
8439b6b563cSPaul Mackerras 		[0] = {
8449b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
8459b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
8469b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
8479b6b563cSPaul Mackerras 		}, [1] = {
8489b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
8499b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
8509b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
8519b6b563cSPaul Mackerras 		}
8529b6b563cSPaul Mackerras 	},
8539b6b563cSPaul Mackerras 	.chrpnote = {
8549b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
8559b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
8569b6b563cSPaul Mackerras 		.type = 0x1275,
8579b6b563cSPaul Mackerras 		.name = "PowerPC",
8589b6b563cSPaul Mackerras 		.chrpdesc = {
8599b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
8609b6b563cSPaul Mackerras 			.real_base = ~0U,
8619b6b563cSPaul Mackerras 			.real_size = ~0U,
8629b6b563cSPaul Mackerras 			.virt_base = ~0U,
8639b6b563cSPaul Mackerras 			.virt_size = ~0U,
8649b6b563cSPaul Mackerras 			.load_base = ~0U
8659b6b563cSPaul Mackerras 		},
8669b6b563cSPaul Mackerras 	},
8679b6b563cSPaul Mackerras 	.rpanote = {
8689b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
8699b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
8709b6b563cSPaul Mackerras 		.type = 0x12759999,
8719b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
8729b6b563cSPaul Mackerras 		.rpadesc = {
8735663a123SPaul Mackerras 			.lpar_affinity = 0,
8745663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
8759b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
8765663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
8779b6b563cSPaul Mackerras 			.splpar = 1,
8789b6b563cSPaul Mackerras 			.min_load = ~0U,
8795663a123SPaul Mackerras 			.new_mem_def = 0
8809b6b563cSPaul Mackerras 		}
8819b6b563cSPaul Mackerras 	}
8829b6b563cSPaul Mackerras };
883493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
8849b6b563cSPaul Mackerras 
885efec959fSBenjamin Herrenschmidt static int __init prom_count_smt_threads(void)
886efec959fSBenjamin Herrenschmidt {
887efec959fSBenjamin Herrenschmidt 	phandle node;
888efec959fSBenjamin Herrenschmidt 	char type[64];
889efec959fSBenjamin Herrenschmidt 	unsigned int plen;
890efec959fSBenjamin Herrenschmidt 
891efec959fSBenjamin Herrenschmidt 	/* Pick up th first CPU node we can find */
892efec959fSBenjamin Herrenschmidt 	for (node = 0; prom_next_node(&node); ) {
893efec959fSBenjamin Herrenschmidt 		type[0] = 0;
894efec959fSBenjamin Herrenschmidt 		prom_getprop(node, "device_type", type, sizeof(type));
895efec959fSBenjamin Herrenschmidt 
8965827d416SAnton Blanchard 		if (strcmp(type, "cpu"))
897efec959fSBenjamin Herrenschmidt 			continue;
898efec959fSBenjamin Herrenschmidt 		/*
899efec959fSBenjamin Herrenschmidt 		 * There is an entry for each smt thread, each entry being
900efec959fSBenjamin Herrenschmidt 		 * 4 bytes long.  All cpus should have the same number of
901efec959fSBenjamin Herrenschmidt 		 * smt threads, so return after finding the first.
902efec959fSBenjamin Herrenschmidt 		 */
903efec959fSBenjamin Herrenschmidt 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
904efec959fSBenjamin Herrenschmidt 		if (plen == PROM_ERROR)
905efec959fSBenjamin Herrenschmidt 			break;
906efec959fSBenjamin Herrenschmidt 		plen >>= 2;
9072c48a7d6SMichael Neuling 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
908efec959fSBenjamin Herrenschmidt 
909efec959fSBenjamin Herrenschmidt 		/* Sanity check */
910efec959fSBenjamin Herrenschmidt 		if (plen < 1 || plen > 64) {
9112c48a7d6SMichael Neuling 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
912efec959fSBenjamin Herrenschmidt 				    (unsigned long)plen);
913efec959fSBenjamin Herrenschmidt 			return 1;
914efec959fSBenjamin Herrenschmidt 		}
915efec959fSBenjamin Herrenschmidt 		return plen;
916efec959fSBenjamin Herrenschmidt 	}
917efec959fSBenjamin Herrenschmidt 	prom_debug("No threads found, assuming 1 per core\n");
918efec959fSBenjamin Herrenschmidt 
919efec959fSBenjamin Herrenschmidt 	return 1;
920efec959fSBenjamin Herrenschmidt 
921efec959fSBenjamin Herrenschmidt }
922efec959fSBenjamin Herrenschmidt 
923efec959fSBenjamin Herrenschmidt 
9249b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
9259b6b563cSPaul Mackerras {
926493adffcSBenjamin Herrenschmidt 	ihandle root;
927f709bfacSPaul Mackerras 	prom_arg_t ret;
928dbd0c5d5SLaurent Dufour 	u32 cores;
929dbd0c5d5SLaurent Dufour 	unsigned char *ptcores;
9309b6b563cSPaul Mackerras 
931f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
932f709bfacSPaul Mackerras 	if (root != 0) {
933efec959fSBenjamin Herrenschmidt 		/* We need to tell the FW about the number of cores we support.
934efec959fSBenjamin Herrenschmidt 		 *
935efec959fSBenjamin Herrenschmidt 		 * To do that, we count the number of threads on the first core
936efec959fSBenjamin Herrenschmidt 		 * (we assume this is the same for all cores) and use it to
937efec959fSBenjamin Herrenschmidt 		 * divide NR_CPUS.
938efec959fSBenjamin Herrenschmidt 		 */
939dbd0c5d5SLaurent Dufour 
940dbd0c5d5SLaurent Dufour 		/* The core value may start at an odd address. If such a word
941dbd0c5d5SLaurent Dufour 		 * access is made at a cache line boundary, this leads to an
942dbd0c5d5SLaurent Dufour 		 * exception which may not be handled at this time.
943dbd0c5d5SLaurent Dufour 		 * Forcing a per byte access to avoid exception.
944dbd0c5d5SLaurent Dufour 		 */
945dbd0c5d5SLaurent Dufour 		ptcores = &ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET];
946dbd0c5d5SLaurent Dufour 		cores = 0;
947dbd0c5d5SLaurent Dufour 		cores |= ptcores[0] << 24;
948dbd0c5d5SLaurent Dufour 		cores |= ptcores[1] << 16;
949dbd0c5d5SLaurent Dufour 		cores |= ptcores[2] << 8;
950dbd0c5d5SLaurent Dufour 		cores |= ptcores[3];
951dbd0c5d5SLaurent Dufour 		if (cores != NR_CPUS) {
952efec959fSBenjamin Herrenschmidt 			prom_printf("WARNING ! "
9532c48a7d6SMichael Neuling 				    "ibm_architecture_vec structure inconsistent: %lu!\n",
954dbd0c5d5SLaurent Dufour 				    cores);
955efec959fSBenjamin Herrenschmidt 		} else {
956dbd0c5d5SLaurent Dufour 			cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
9572c48a7d6SMichael Neuling 			prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
958dbd0c5d5SLaurent Dufour 				    cores, NR_CPUS);
959dbd0c5d5SLaurent Dufour 			ptcores[0] = (cores >> 24) & 0xff;
960dbd0c5d5SLaurent Dufour 			ptcores[1] = (cores >> 16) & 0xff;
961dbd0c5d5SLaurent Dufour 			ptcores[2] = (cores >> 8) & 0xff;
962dbd0c5d5SLaurent Dufour 			ptcores[3] = cores & 0xff;
963efec959fSBenjamin Herrenschmidt 		}
964efec959fSBenjamin Herrenschmidt 
965f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
966049d0497SAnton Blanchard 		prom_printf("Calling ibm,client-architecture-support...");
967f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
968f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
96933b74977SBenjamin Herrenschmidt 				  root,
970f709bfacSPaul Mackerras 				  ADDR(ibm_architecture_vec)) == 0) {
971f709bfacSPaul Mackerras 			/* the call exists... */
972f709bfacSPaul Mackerras 			if (ret)
9734da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
974f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
975f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
9764da727aeSAnton Blanchard 			prom_printf(" done\n");
977f709bfacSPaul Mackerras 			return;
978f709bfacSPaul Mackerras 		}
979f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
980049d0497SAnton Blanchard 		prom_printf(" not implemented\n");
981f709bfacSPaul Mackerras 	}
982f709bfacSPaul Mackerras 
983493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
984493adffcSBenjamin Herrenschmidt 	{
985493adffcSBenjamin Herrenschmidt 		ihandle elfloader;
986493adffcSBenjamin Herrenschmidt 
987f709bfacSPaul Mackerras 		/* no ibm,client-architecture-support call, try the old way */
988493adffcSBenjamin Herrenschmidt 		elfloader = call_prom("open", 1, 1,
989493adffcSBenjamin Herrenschmidt 				      ADDR("/packages/elf-loader"));
9909b6b563cSPaul Mackerras 		if (elfloader == 0) {
9919b6b563cSPaul Mackerras 			prom_printf("couldn't open /packages/elf-loader\n");
9929b6b563cSPaul Mackerras 			return;
9939b6b563cSPaul Mackerras 		}
9949b6b563cSPaul Mackerras 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
9959b6b563cSPaul Mackerras 			  elfloader, ADDR(&fake_elf));
9969b6b563cSPaul Mackerras 		call_prom("close", 1, 0, elfloader);
9979b6b563cSPaul Mackerras 	}
998493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
999493adffcSBenjamin Herrenschmidt }
1000493adffcSBenjamin Herrenschmidt #endif /* #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV) */
10019b6b563cSPaul Mackerras 
10029b6b563cSPaul Mackerras /*
10039b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
10049b6b563cSPaul Mackerras  *
10059b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
10069b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
10079b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
10089b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
10099b6b563cSPaul Mackerras  *  never happen.
10109b6b563cSPaul Mackerras  *
10119b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
10129b6b563cSPaul Mackerras  *  TCEs overlap
10139b6b563cSPaul Mackerras  *
10149b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
10159b6b563cSPaul Mackerras  *
10169b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
10179b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
10189b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
10199b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
10209b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
10219b6b563cSPaul Mackerras  *
10229b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
10239b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
10249b6b563cSPaul Mackerras  *  reserved.
10259b6b563cSPaul Mackerras  */
10269b6b563cSPaul Mackerras 
10279b6b563cSPaul Mackerras 
10289b6b563cSPaul Mackerras /*
10299b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
10309b6b563cSPaul Mackerras  *
10319b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
10329b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
10339b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
10349b6b563cSPaul Mackerras  */
10359b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
10369b6b563cSPaul Mackerras {
10375827d416SAnton Blanchard 	unsigned long base = alloc_bottom;
10389b6b563cSPaul Mackerras 	unsigned long addr = 0;
10399b6b563cSPaul Mackerras 
1040c4988820SPaul Mackerras 	if (align)
1041c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
10429b6b563cSPaul Mackerras 	prom_debug("alloc_up(%x, %x)\n", size, align);
10435827d416SAnton Blanchard 	if (ram_top == 0)
10449b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
10459b6b563cSPaul Mackerras 
10469b6b563cSPaul Mackerras 	if (align)
10475827d416SAnton Blanchard 		base = _ALIGN_UP(alloc_bottom, align);
10489b6b563cSPaul Mackerras 	else
10495827d416SAnton Blanchard 		base = alloc_bottom;
10509b6b563cSPaul Mackerras 
10515827d416SAnton Blanchard 	for(; (base + size) <= alloc_top;
10529b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
10539b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
10549b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1055c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
10569b6b563cSPaul Mackerras 			break;
10579b6b563cSPaul Mackerras 		addr = 0;
10589b6b563cSPaul Mackerras 		if (align == 0)
10599b6b563cSPaul Mackerras 			break;
10609b6b563cSPaul Mackerras 	}
10619b6b563cSPaul Mackerras 	if (addr == 0)
10629b6b563cSPaul Mackerras 		return 0;
10635827d416SAnton Blanchard 	alloc_bottom = addr + size;
10649b6b563cSPaul Mackerras 
10659b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
10665827d416SAnton Blanchard 	prom_debug("  alloc_bottom : %x\n", alloc_bottom);
10675827d416SAnton Blanchard 	prom_debug("  alloc_top    : %x\n", alloc_top);
10685827d416SAnton Blanchard 	prom_debug("  alloc_top_hi : %x\n", alloc_top_high);
10695827d416SAnton Blanchard 	prom_debug("  rmo_top      : %x\n", rmo_top);
10705827d416SAnton Blanchard 	prom_debug("  ram_top      : %x\n", ram_top);
10719b6b563cSPaul Mackerras 
10729b6b563cSPaul Mackerras 	return addr;
10739b6b563cSPaul Mackerras }
10749b6b563cSPaul Mackerras 
10759b6b563cSPaul Mackerras /*
10769b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
10779b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
10789b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
10799b6b563cSPaul Mackerras  */
10809b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
10819b6b563cSPaul Mackerras 				       int highmem)
10829b6b563cSPaul Mackerras {
10839b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
10849b6b563cSPaul Mackerras 
10859b6b563cSPaul Mackerras 	prom_debug("alloc_down(%x, %x, %s)\n", size, align,
10865827d416SAnton Blanchard 		   highmem ? "(high)" : "(low)");
10875827d416SAnton Blanchard 	if (ram_top == 0)
10889b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
10899b6b563cSPaul Mackerras 
10909b6b563cSPaul Mackerras 	if (highmem) {
10919b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
10925827d416SAnton Blanchard 		addr = _ALIGN_DOWN(alloc_top_high - size, align);
10935827d416SAnton Blanchard 		if (addr <= alloc_bottom)
10949b6b563cSPaul Mackerras 			return 0;
10959b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
10969b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
10979b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
10989b6b563cSPaul Mackerras 		 */
10995827d416SAnton Blanchard 		if (addr < rmo_top) {
11009b6b563cSPaul Mackerras 			/* Good, we are first */
11015827d416SAnton Blanchard 			if (alloc_top == rmo_top)
11025827d416SAnton Blanchard 				alloc_top = rmo_top = addr;
11039b6b563cSPaul Mackerras 			else
11049b6b563cSPaul Mackerras 				return 0;
11059b6b563cSPaul Mackerras 		}
11065827d416SAnton Blanchard 		alloc_top_high = addr;
11079b6b563cSPaul Mackerras 		goto bail;
11089b6b563cSPaul Mackerras 	}
11099b6b563cSPaul Mackerras 
11105827d416SAnton Blanchard 	base = _ALIGN_DOWN(alloc_top - size, align);
11115827d416SAnton Blanchard 	for (; base > alloc_bottom;
11129b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
11139b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
11149b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
1115c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
11169b6b563cSPaul Mackerras 			break;
11179b6b563cSPaul Mackerras 		addr = 0;
11189b6b563cSPaul Mackerras 	}
11199b6b563cSPaul Mackerras 	if (addr == 0)
11209b6b563cSPaul Mackerras 		return 0;
11215827d416SAnton Blanchard 	alloc_top = addr;
11229b6b563cSPaul Mackerras 
11239b6b563cSPaul Mackerras  bail:
11249b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
11255827d416SAnton Blanchard 	prom_debug("  alloc_bottom : %x\n", alloc_bottom);
11265827d416SAnton Blanchard 	prom_debug("  alloc_top    : %x\n", alloc_top);
11275827d416SAnton Blanchard 	prom_debug("  alloc_top_hi : %x\n", alloc_top_high);
11285827d416SAnton Blanchard 	prom_debug("  rmo_top      : %x\n", rmo_top);
11295827d416SAnton Blanchard 	prom_debug("  ram_top      : %x\n", ram_top);
11309b6b563cSPaul Mackerras 
11319b6b563cSPaul Mackerras 	return addr;
11329b6b563cSPaul Mackerras }
11339b6b563cSPaul Mackerras 
11349b6b563cSPaul Mackerras /*
11359b6b563cSPaul Mackerras  * Parse a "reg" cell
11369b6b563cSPaul Mackerras  */
11379b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
11389b6b563cSPaul Mackerras {
11399b6b563cSPaul Mackerras 	cell_t *p = *cellp;
11409b6b563cSPaul Mackerras 	unsigned long r = 0;
11419b6b563cSPaul Mackerras 
11429b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
11439b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
11449b6b563cSPaul Mackerras 		p++;
11459b6b563cSPaul Mackerras 		s--;
11469b6b563cSPaul Mackerras 	}
1147493adffcSBenjamin Herrenschmidt 	r = be32_to_cpu(*p++);
11489b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
114935499c01SPaul Mackerras 	if (s > 1) {
11509b6b563cSPaul Mackerras 		r <<= 32;
1151493adffcSBenjamin Herrenschmidt 		r |= be32_to_cpu(*(p++));
11529b6b563cSPaul Mackerras 	}
11539b6b563cSPaul Mackerras #endif
11549b6b563cSPaul Mackerras 	*cellp = p;
11559b6b563cSPaul Mackerras 	return r;
11569b6b563cSPaul Mackerras }
11579b6b563cSPaul Mackerras 
11589b6b563cSPaul Mackerras /*
11599b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
11609b6b563cSPaul Mackerras  * we don't need anything smarter at this point
11619b6b563cSPaul Mackerras  *
11629b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
11639b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
11649b6b563cSPaul Mackerras  * them down.
11659b6b563cSPaul Mackerras  */
11660108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
11679b6b563cSPaul Mackerras {
1168cbbcf340SKumar Gala 	u64 top = base + size;
11695827d416SAnton Blanchard 	unsigned long cnt = mem_reserve_cnt;
11709b6b563cSPaul Mackerras 
11719b6b563cSPaul Mackerras 	if (size == 0)
11729b6b563cSPaul Mackerras 		return;
11739b6b563cSPaul Mackerras 
11749b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
11759b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
11769b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
11779b6b563cSPaul Mackerras 	 */
11789b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
11799b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
11809b6b563cSPaul Mackerras 	size = top - base;
11819b6b563cSPaul Mackerras 
11829b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
11839b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
1184493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1185493adffcSBenjamin Herrenschmidt 	mem_reserve_map[cnt].size = cpu_to_be64(size);
11865827d416SAnton Blanchard 	mem_reserve_cnt = cnt + 1;
11879b6b563cSPaul Mackerras }
11889b6b563cSPaul Mackerras 
11899b6b563cSPaul Mackerras /*
1190b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
11919b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
11929b6b563cSPaul Mackerras  */
11939b6b563cSPaul Mackerras static void __init prom_init_mem(void)
11949b6b563cSPaul Mackerras {
11959b6b563cSPaul Mackerras 	phandle node;
11969b6b563cSPaul Mackerras 	char *path, type[64];
11979b6b563cSPaul Mackerras 	unsigned int plen;
11989b6b563cSPaul Mackerras 	cell_t *p, *endp;
1199493adffcSBenjamin Herrenschmidt 	__be32 val;
12009b6b563cSPaul Mackerras 	u32 rac, rsc;
12019b6b563cSPaul Mackerras 
12029b6b563cSPaul Mackerras 	/*
12039b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
12049b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
12059b6b563cSPaul Mackerras 	 * 2) top of memory
12069b6b563cSPaul Mackerras 	 */
1207493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(2);
1208493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1209493adffcSBenjamin Herrenschmidt 	rac = be32_to_cpu(val);
1210493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(1);
1211493adffcSBenjamin Herrenschmidt 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1212493adffcSBenjamin Herrenschmidt 	rsc = be32_to_cpu(val);
1213493adffcSBenjamin Herrenschmidt 	prom_debug("root_addr_cells: %x\n", rac);
1214493adffcSBenjamin Herrenschmidt 	prom_debug("root_size_cells: %x\n", rsc);
12159b6b563cSPaul Mackerras 
12169b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
12175827d416SAnton Blanchard 	path = prom_scratch;
12189b6b563cSPaul Mackerras 
12199b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
12209b6b563cSPaul Mackerras 		type[0] = 0;
12219b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
12229b6b563cSPaul Mackerras 
1223c4988820SPaul Mackerras 		if (type[0] == 0) {
1224c4988820SPaul Mackerras 			/*
1225c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1226c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1227c4988820SPaul Mackerras 			 */
1228c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1229c4988820SPaul Mackerras 		}
12305827d416SAnton Blanchard 		if (strcmp(type, "memory"))
12319b6b563cSPaul Mackerras 			continue;
12329b6b563cSPaul Mackerras 
12335827d416SAnton Blanchard 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
12349b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
12359b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
12369b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
12379b6b563cSPaul Mackerras 		}
12385827d416SAnton Blanchard 		p = regbuf;
12399b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
12409b6b563cSPaul Mackerras 
12419b6b563cSPaul Mackerras #ifdef DEBUG_PROM
12429b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
12439b6b563cSPaul Mackerras 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
12449b6b563cSPaul Mackerras 		prom_debug("  node %s :\n", path);
12459b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
12469b6b563cSPaul Mackerras 
12479b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
12489b6b563cSPaul Mackerras 			unsigned long base, size;
12499b6b563cSPaul Mackerras 
12509b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
12519b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
12529b6b563cSPaul Mackerras 
12539b6b563cSPaul Mackerras 			if (size == 0)
12549b6b563cSPaul Mackerras 				continue;
12559b6b563cSPaul Mackerras 			prom_debug("    %x %x\n", base, size);
12565827d416SAnton Blanchard 			if (base == 0 && (of_platform & PLATFORM_LPAR))
12575827d416SAnton Blanchard 				rmo_top = size;
12585827d416SAnton Blanchard 			if ((base + size) > ram_top)
12595827d416SAnton Blanchard 				ram_top = base + size;
12609b6b563cSPaul Mackerras 		}
12619b6b563cSPaul Mackerras 	}
12629b6b563cSPaul Mackerras 
12635827d416SAnton Blanchard 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
12649b6b563cSPaul Mackerras 
12659b6b563cSPaul Mackerras 	/*
1266cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1267cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1268cf68787bSBenjamin Krill 	 * TCE's up there.
1269cf68787bSBenjamin Krill 	 */
1270cf68787bSBenjamin Krill 
12715827d416SAnton Blanchard 	alloc_top_high = ram_top;
1272cf68787bSBenjamin Krill 
12735827d416SAnton Blanchard 	if (prom_memory_limit) {
12745827d416SAnton Blanchard 		if (prom_memory_limit <= alloc_bottom) {
1275cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
12765827d416SAnton Blanchard 				prom_memory_limit);
12775827d416SAnton Blanchard 			prom_memory_limit = 0;
12785827d416SAnton Blanchard 		} else if (prom_memory_limit >= ram_top) {
1279cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x >= ram_top.\n",
12805827d416SAnton Blanchard 				prom_memory_limit);
12815827d416SAnton Blanchard 			prom_memory_limit = 0;
1282cf68787bSBenjamin Krill 		} else {
12835827d416SAnton Blanchard 			ram_top = prom_memory_limit;
12845827d416SAnton Blanchard 			rmo_top = min(rmo_top, prom_memory_limit);
1285cf68787bSBenjamin Krill 		}
1286cf68787bSBenjamin Krill 	}
1287cf68787bSBenjamin Krill 
1288cf68787bSBenjamin Krill 	/*
12899b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
12909b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
12919b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
12929b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
12939b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
12949b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
12959b6b563cSPaul Mackerras 	 */
12965827d416SAnton Blanchard 	if (!rmo_top)
12975827d416SAnton Blanchard 		rmo_top = ram_top;
12985827d416SAnton Blanchard 	rmo_top = min(0x30000000ul, rmo_top);
12995827d416SAnton Blanchard 	alloc_top = rmo_top;
13005827d416SAnton Blanchard 	alloc_top_high = ram_top;
13019b6b563cSPaul Mackerras 
130264968f60SPaul Mackerras 	/*
130364968f60SPaul Mackerras 	 * Check if we have an initrd after the kernel but still inside
130464968f60SPaul Mackerras 	 * the RMO.  If we do move our bottom point to after it.
130564968f60SPaul Mackerras 	 */
13065827d416SAnton Blanchard 	if (prom_initrd_start &&
13075827d416SAnton Blanchard 	    prom_initrd_start < rmo_top &&
13085827d416SAnton Blanchard 	    prom_initrd_end > alloc_bottom)
13095827d416SAnton Blanchard 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
131064968f60SPaul Mackerras 
13119b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
13125827d416SAnton Blanchard 	prom_printf("  memory_limit : %x (16 MB aligned)\n", prom_memory_limit);
13135827d416SAnton Blanchard 	prom_printf("  alloc_bottom : %x\n", alloc_bottom);
13145827d416SAnton Blanchard 	prom_printf("  alloc_top    : %x\n", alloc_top);
13155827d416SAnton Blanchard 	prom_printf("  alloc_top_hi : %x\n", alloc_top_high);
13165827d416SAnton Blanchard 	prom_printf("  rmo_top      : %x\n", rmo_top);
13175827d416SAnton Blanchard 	prom_printf("  ram_top      : %x\n", ram_top);
13189b6b563cSPaul Mackerras }
13199b6b563cSPaul Mackerras 
132027f44888SBenjamin Herrenschmidt static void __init prom_close_stdin(void)
132127f44888SBenjamin Herrenschmidt {
1322493adffcSBenjamin Herrenschmidt 	__be32 val;
1323493adffcSBenjamin Herrenschmidt 	ihandle stdin;
132427f44888SBenjamin Herrenschmidt 
1325493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1326493adffcSBenjamin Herrenschmidt 		stdin = be32_to_cpu(val);
1327493adffcSBenjamin Herrenschmidt 		call_prom("close", 1, 0, stdin);
1328493adffcSBenjamin Herrenschmidt 	}
132927f44888SBenjamin Herrenschmidt }
133027f44888SBenjamin Herrenschmidt 
133127f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
133227f44888SBenjamin Herrenschmidt 
13336e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
13346e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_base;
13356e35d5daSBenjamin Herrenschmidt static u64 __initdata prom_opal_entry;
13366e35d5daSBenjamin Herrenschmidt #endif
13376e35d5daSBenjamin Herrenschmidt 
13386e35d5daSBenjamin Herrenschmidt /*
13396e35d5daSBenjamin Herrenschmidt  * Allocate room for and instantiate OPAL
13406e35d5daSBenjamin Herrenschmidt  */
13416e35d5daSBenjamin Herrenschmidt static void __init prom_instantiate_opal(void)
13426e35d5daSBenjamin Herrenschmidt {
13436e35d5daSBenjamin Herrenschmidt 	phandle opal_node;
13446e35d5daSBenjamin Herrenschmidt 	ihandle opal_inst;
13456e35d5daSBenjamin Herrenschmidt 	u64 base, entry;
13466e35d5daSBenjamin Herrenschmidt 	u64 size = 0, align = 0x10000;
1347493adffcSBenjamin Herrenschmidt 	__be64 val64;
13486e35d5daSBenjamin Herrenschmidt 	u32 rets[2];
13496e35d5daSBenjamin Herrenschmidt 
13506e35d5daSBenjamin Herrenschmidt 	prom_debug("prom_instantiate_opal: start...\n");
13516e35d5daSBenjamin Herrenschmidt 
13526e35d5daSBenjamin Herrenschmidt 	opal_node = call_prom("finddevice", 1, 1, ADDR("/ibm,opal"));
13536e35d5daSBenjamin Herrenschmidt 	prom_debug("opal_node: %x\n", opal_node);
13546e35d5daSBenjamin Herrenschmidt 	if (!PHANDLE_VALID(opal_node))
13556e35d5daSBenjamin Herrenschmidt 		return;
13566e35d5daSBenjamin Herrenschmidt 
1357493adffcSBenjamin Herrenschmidt 	val64 = 0;
1358493adffcSBenjamin Herrenschmidt 	prom_getprop(opal_node, "opal-runtime-size", &val64, sizeof(val64));
1359493adffcSBenjamin Herrenschmidt 	size = be64_to_cpu(val64);
13606e35d5daSBenjamin Herrenschmidt 	if (size == 0)
13616e35d5daSBenjamin Herrenschmidt 		return;
1362493adffcSBenjamin Herrenschmidt 	val64 = 0;
1363493adffcSBenjamin Herrenschmidt 	prom_getprop(opal_node, "opal-runtime-alignment", &val64,sizeof(val64));
1364493adffcSBenjamin Herrenschmidt 	align = be64_to_cpu(val64);
13656e35d5daSBenjamin Herrenschmidt 
13666e35d5daSBenjamin Herrenschmidt 	base = alloc_down(size, align, 0);
13676e35d5daSBenjamin Herrenschmidt 	if (base == 0) {
13686e35d5daSBenjamin Herrenschmidt 		prom_printf("OPAL allocation failed !\n");
13696e35d5daSBenjamin Herrenschmidt 		return;
13706e35d5daSBenjamin Herrenschmidt 	}
13716e35d5daSBenjamin Herrenschmidt 
13726e35d5daSBenjamin Herrenschmidt 	opal_inst = call_prom("open", 1, 1, ADDR("/ibm,opal"));
13736e35d5daSBenjamin Herrenschmidt 	if (!IHANDLE_VALID(opal_inst)) {
13746e35d5daSBenjamin Herrenschmidt 		prom_printf("opening opal package failed (%x)\n", opal_inst);
13756e35d5daSBenjamin Herrenschmidt 		return;
13766e35d5daSBenjamin Herrenschmidt 	}
13776e35d5daSBenjamin Herrenschmidt 
13786e35d5daSBenjamin Herrenschmidt 	prom_printf("instantiating opal at 0x%x...", base);
13796e35d5daSBenjamin Herrenschmidt 
13806e35d5daSBenjamin Herrenschmidt 	if (call_prom_ret("call-method", 4, 3, rets,
13816e35d5daSBenjamin Herrenschmidt 			  ADDR("load-opal-runtime"),
13826e35d5daSBenjamin Herrenschmidt 			  opal_inst,
13836e35d5daSBenjamin Herrenschmidt 			  base >> 32, base & 0xffffffff) != 0
13846e35d5daSBenjamin Herrenschmidt 	    || (rets[0] == 0 && rets[1] == 0)) {
13856e35d5daSBenjamin Herrenschmidt 		prom_printf(" failed\n");
13866e35d5daSBenjamin Herrenschmidt 		return;
13876e35d5daSBenjamin Herrenschmidt 	}
13886e35d5daSBenjamin Herrenschmidt 	entry = (((u64)rets[0]) << 32) | rets[1];
13896e35d5daSBenjamin Herrenschmidt 
13906e35d5daSBenjamin Herrenschmidt 	prom_printf(" done\n");
13916e35d5daSBenjamin Herrenschmidt 
13926e35d5daSBenjamin Herrenschmidt 	reserve_mem(base, size);
13936e35d5daSBenjamin Herrenschmidt 
13946e35d5daSBenjamin Herrenschmidt 	prom_debug("opal base     = 0x%x\n", base);
13956e35d5daSBenjamin Herrenschmidt 	prom_debug("opal align    = 0x%x\n", align);
13966e35d5daSBenjamin Herrenschmidt 	prom_debug("opal entry    = 0x%x\n", entry);
13976e35d5daSBenjamin Herrenschmidt 	prom_debug("opal size     = 0x%x\n", (long)size);
13986e35d5daSBenjamin Herrenschmidt 
13996e35d5daSBenjamin Herrenschmidt 	prom_setprop(opal_node, "/ibm,opal", "opal-base-address",
14006e35d5daSBenjamin Herrenschmidt 		     &base, sizeof(base));
14016e35d5daSBenjamin Herrenschmidt 	prom_setprop(opal_node, "/ibm,opal", "opal-entry-address",
14026e35d5daSBenjamin Herrenschmidt 		     &entry, sizeof(entry));
14036e35d5daSBenjamin Herrenschmidt 
14046e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
14055827d416SAnton Blanchard 	prom_opal_base = base;
14065827d416SAnton Blanchard 	prom_opal_entry = entry;
14076e35d5daSBenjamin Herrenschmidt #endif
14086e35d5daSBenjamin Herrenschmidt 	prom_debug("prom_instantiate_opal: end...\n");
14096e35d5daSBenjamin Herrenschmidt }
14106e35d5daSBenjamin Herrenschmidt 
141127f44888SBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV */
14129b6b563cSPaul Mackerras 
14139b6b563cSPaul Mackerras /*
14149b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
14159b6b563cSPaul Mackerras  */
14169b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
14179b6b563cSPaul Mackerras {
14189b6b563cSPaul Mackerras 	phandle rtas_node;
14199b6b563cSPaul Mackerras 	ihandle rtas_inst;
14209b6b563cSPaul Mackerras 	u32 base, entry = 0;
1421493adffcSBenjamin Herrenschmidt 	__be32 val;
14229b6b563cSPaul Mackerras 	u32 size = 0;
14239b6b563cSPaul Mackerras 
14249b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
14259b6b563cSPaul Mackerras 
14269b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
14279b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
14289b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
14299b6b563cSPaul Mackerras 		return;
14309b6b563cSPaul Mackerras 
1431493adffcSBenjamin Herrenschmidt 	val = 0;
1432493adffcSBenjamin Herrenschmidt 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1433493adffcSBenjamin Herrenschmidt 	size = be32_to_cpu(val);
14349b6b563cSPaul Mackerras 	if (size == 0)
14359b6b563cSPaul Mackerras 		return;
14369b6b563cSPaul Mackerras 
14379b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
14386d1e2c6cSAnton Blanchard 	if (base == 0)
14396d1e2c6cSAnton Blanchard 		prom_panic("Could not allocate memory for RTAS\n");
14409b6b563cSPaul Mackerras 
14419b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
14429b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1443a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
14449b6b563cSPaul Mackerras 		return;
14459b6b563cSPaul Mackerras 	}
14469b6b563cSPaul Mackerras 
14479b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
14489b6b563cSPaul Mackerras 
14499b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
14509b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1451a23414beSPaul Mackerras 			  rtas_inst, base) != 0
14529b6b563cSPaul Mackerras 	    || entry == 0) {
14539b6b563cSPaul Mackerras 		prom_printf(" failed\n");
14549b6b563cSPaul Mackerras 		return;
14559b6b563cSPaul Mackerras 	}
14569b6b563cSPaul Mackerras 	prom_printf(" done\n");
14579b6b563cSPaul Mackerras 
14589b6b563cSPaul Mackerras 	reserve_mem(base, size);
14599b6b563cSPaul Mackerras 
1460493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(base);
1461a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1462493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
1463493adffcSBenjamin Herrenschmidt 	val = cpu_to_be32(entry);
1464a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1465493adffcSBenjamin Herrenschmidt 		     &val, sizeof(val));
14669b6b563cSPaul Mackerras 
1467dbe78b40SBenjamin Herrenschmidt 	/* Check if it supports "query-cpu-stopped-state" */
1468dbe78b40SBenjamin Herrenschmidt 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1469dbe78b40SBenjamin Herrenschmidt 			 &val, sizeof(val)) != PROM_ERROR)
1470dbe78b40SBenjamin Herrenschmidt 		rtas_has_query_cpu_stopped = true;
1471dbe78b40SBenjamin Herrenschmidt 
14729b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
14739b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
14749b6b563cSPaul Mackerras 	prom_debug("rtas size     = 0x%x\n", (long)size);
14759b6b563cSPaul Mackerras 
14769b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
14779b6b563cSPaul Mackerras }
14789b6b563cSPaul Mackerras 
14799b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
14809b6b563cSPaul Mackerras /*
14814a727429SAshley Lai  * Allocate room for and instantiate Stored Measurement Log (SML)
14824a727429SAshley Lai  */
14834a727429SAshley Lai static void __init prom_instantiate_sml(void)
14844a727429SAshley Lai {
14854a727429SAshley Lai 	phandle ibmvtpm_node;
14864a727429SAshley Lai 	ihandle ibmvtpm_inst;
1487b4ed0469SHon Ching \(Vicky\) Lo 	u32 entry = 0, size = 0, succ = 0;
14884a727429SAshley Lai 	u64 base;
1489b4ed0469SHon Ching \(Vicky\) Lo 	__be32 val;
14904a727429SAshley Lai 
14914a727429SAshley Lai 	prom_debug("prom_instantiate_sml: start...\n");
14924a727429SAshley Lai 
14932f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
14944a727429SAshley Lai 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
14954a727429SAshley Lai 	if (!PHANDLE_VALID(ibmvtpm_node))
14964a727429SAshley Lai 		return;
14974a727429SAshley Lai 
14982f82e982SHon Ching \(Vicky\) Lo 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
14994a727429SAshley Lai 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
15004a727429SAshley Lai 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
15014a727429SAshley Lai 		return;
15024a727429SAshley Lai 	}
15034a727429SAshley Lai 
1504b4ed0469SHon Ching \(Vicky\) Lo 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1505b4ed0469SHon Ching \(Vicky\) Lo 			 &val, sizeof(val)) != PROM_ERROR) {
1506b4ed0469SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &succ,
1507b4ed0469SHon Ching \(Vicky\) Lo 				  ADDR("reformat-sml-to-efi-alignment"),
1508b4ed0469SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || succ == 0) {
1509b4ed0469SHon Ching \(Vicky\) Lo 			prom_printf("Reformat SML to EFI alignment failed\n");
1510b4ed0469SHon Ching \(Vicky\) Lo 			return;
1511b4ed0469SHon Ching \(Vicky\) Lo 		}
1512b4ed0469SHon Ching \(Vicky\) Lo 
15134a727429SAshley Lai 		if (call_prom_ret("call-method", 2, 2, &size,
15149e5d4af4SHon Ching \(Vicky\) Lo 				  ADDR("sml-get-allocated-size"),
15159e5d4af4SHon Ching \(Vicky\) Lo 				  ibmvtpm_inst) != 0 || size == 0) {
15169e5d4af4SHon Ching \(Vicky\) Lo 			prom_printf("SML get allocated size failed\n");
15179e5d4af4SHon Ching \(Vicky\) Lo 			return;
15189e5d4af4SHon Ching \(Vicky\) Lo 		}
15199e5d4af4SHon Ching \(Vicky\) Lo 	} else {
15209e5d4af4SHon Ching \(Vicky\) Lo 		if (call_prom_ret("call-method", 2, 2, &size,
15214a727429SAshley Lai 				  ADDR("sml-get-handover-size"),
15224a727429SAshley Lai 				  ibmvtpm_inst) != 0 || size == 0) {
15234a727429SAshley Lai 			prom_printf("SML get handover size failed\n");
15244a727429SAshley Lai 			return;
15254a727429SAshley Lai 		}
15269e5d4af4SHon Ching \(Vicky\) Lo 	}
15274a727429SAshley Lai 
15284a727429SAshley Lai 	base = alloc_down(size, PAGE_SIZE, 0);
15294a727429SAshley Lai 	if (base == 0)
15304a727429SAshley Lai 		prom_panic("Could not allocate memory for sml\n");
15314a727429SAshley Lai 
15324a727429SAshley Lai 	prom_printf("instantiating sml at 0x%x...", base);
15334a727429SAshley Lai 
15349e5d4af4SHon Ching \(Vicky\) Lo 	memset((void *)base, 0, size);
15359e5d4af4SHon Ching \(Vicky\) Lo 
15364a727429SAshley Lai 	if (call_prom_ret("call-method", 4, 2, &entry,
15374a727429SAshley Lai 			  ADDR("sml-handover"),
15384a727429SAshley Lai 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
15394a727429SAshley Lai 		prom_printf("SML handover failed\n");
15404a727429SAshley Lai 		return;
15414a727429SAshley Lai 	}
15424a727429SAshley Lai 	prom_printf(" done\n");
15434a727429SAshley Lai 
15444a727429SAshley Lai 	reserve_mem(base, size);
15454a727429SAshley Lai 
15462f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
15474a727429SAshley Lai 		     &base, sizeof(base));
15482f82e982SHon Ching \(Vicky\) Lo 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
15494a727429SAshley Lai 		     &size, sizeof(size));
15504a727429SAshley Lai 
15514a727429SAshley Lai 	prom_debug("sml base     = 0x%x\n", base);
15524a727429SAshley Lai 	prom_debug("sml size     = 0x%x\n", (long)size);
15534a727429SAshley Lai 
15544a727429SAshley Lai 	prom_debug("prom_instantiate_sml: end...\n");
15554a727429SAshley Lai }
15564a727429SAshley Lai 
15574a727429SAshley Lai /*
15589b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
15599b6b563cSPaul Mackerras  */
1560493adffcSBenjamin Herrenschmidt #ifdef __BIG_ENDIAN__
15619b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
15629b6b563cSPaul Mackerras {
15639b6b563cSPaul Mackerras 	phandle node;
15649b6b563cSPaul Mackerras 	ihandle phb_node;
15659b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
15665827d416SAnton Blanchard 	char *path = prom_scratch;
15679b6b563cSPaul Mackerras 	u64 base, align;
15689b6b563cSPaul Mackerras 	u32 minalign, minsize;
15699b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
15709b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
15719b6b563cSPaul Mackerras 	u64 i;
15729b6b563cSPaul Mackerras 
15735827d416SAnton Blanchard 	if (prom_iommu_off)
15749b6b563cSPaul Mackerras 		return;
15759b6b563cSPaul Mackerras 
15769b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
15779b6b563cSPaul Mackerras 
15789b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
15795827d416SAnton Blanchard 	local_alloc_top = alloc_top_high;
15809b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
15819b6b563cSPaul Mackerras 
15829b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
15839b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
15849b6b563cSPaul Mackerras 		compatible[0] = 0;
15859b6b563cSPaul Mackerras 		type[0] = 0;
15869b6b563cSPaul Mackerras 		model[0] = 0;
15879b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
15889b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
15899b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
15909b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
15919b6b563cSPaul Mackerras 
15925827d416SAnton Blanchard 		if ((type[0] == 0) || (strstr(type, "pci") == NULL))
15939b6b563cSPaul Mackerras 			continue;
15949b6b563cSPaul Mackerras 
1595e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
15969b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
15975827d416SAnton Blanchard 			if ((strstr(compatible, "python") == NULL) &&
15985827d416SAnton Blanchard 			    (strstr(compatible, "Speedwagon") == NULL) &&
15995827d416SAnton Blanchard 			    (strstr(compatible, "Winnipeg") == NULL))
16009b6b563cSPaul Mackerras 				continue;
16019b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
16025827d416SAnton Blanchard 			if ((strstr(model, "ython") == NULL) &&
16035827d416SAnton Blanchard 			    (strstr(model, "peedwagon") == NULL) &&
16045827d416SAnton Blanchard 			    (strstr(model, "innipeg") == NULL))
16059b6b563cSPaul Mackerras 				continue;
16069b6b563cSPaul Mackerras 		}
16079b6b563cSPaul Mackerras 
16089b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
16099b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
16109b6b563cSPaul Mackerras 			minalign = 0;
16119b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
16129b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
16139b6b563cSPaul Mackerras 			minsize = 4UL << 20;
16149b6b563cSPaul Mackerras 
16159b6b563cSPaul Mackerras 		/*
16169b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
16179b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
16189b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
16199b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
16209b6b563cSPaul Mackerras 		 *
16219b6b563cSPaul Mackerras 		 * On POWER4, firmware sets the TCE region by assuming
16229b6b563cSPaul Mackerras 		 * each TCE table is 8MB. Using this memory for anything
16239b6b563cSPaul Mackerras 		 * else will impact performance, so we always allocate 8MB.
16249b6b563cSPaul Mackerras 		 * Anton
16259b6b563cSPaul Mackerras 		 */
1626d3dbeef6SMichael Ellerman 		if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p))
16279b6b563cSPaul Mackerras 			minsize = 8UL << 20;
16289b6b563cSPaul Mackerras 		else
16299b6b563cSPaul Mackerras 			minsize = 4UL << 20;
16309b6b563cSPaul Mackerras 
16319b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
16329b6b563cSPaul Mackerras 		align = max(minalign, minsize);
16339b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
16349b6b563cSPaul Mackerras 		if (base == 0)
16359b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
16369b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
16379b6b563cSPaul Mackerras 			local_alloc_bottom = base;
16389b6b563cSPaul Mackerras 
16399b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
1640aca71ef8SLi Zefan 		memset(path, 0, PROM_SCRATCH_SIZE);
16419b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
16429b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
16439b6b563cSPaul Mackerras 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
16449b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
16459b6b563cSPaul Mackerras 		}
16469b6b563cSPaul Mackerras 
1647a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
1648a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1649a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1650a23414beSPaul Mackerras 
16519b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
16529b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
16539b6b563cSPaul Mackerras 		prom_debug("\tbase = 0x%x\n", base);
16549b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
16559b6b563cSPaul Mackerras 
16569b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
16579b6b563cSPaul Mackerras 		 * over the allocated size.
16589b6b563cSPaul Mackerras 		 */
16592b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
16609b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
16619b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
16629b6b563cSPaul Mackerras 			tce_entry |= 0x3;
16639b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
16649b6b563cSPaul Mackerras 		}
16659b6b563cSPaul Mackerras 
16669b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
16679b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
16689b6b563cSPaul Mackerras 		if (phb_node == 0)
16699b6b563cSPaul Mackerras 			prom_printf("... failed\n");
16709b6b563cSPaul Mackerras 		else
16719b6b563cSPaul Mackerras 			prom_printf("... done\n");
16729b6b563cSPaul Mackerras 
16739b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
16749b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
16759b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
16769b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
16779b6b563cSPaul Mackerras 	}
16789b6b563cSPaul Mackerras 
16799b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
16809b6b563cSPaul Mackerras 
16812babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
16822babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
16835827d416SAnton Blanchard 	prom_tce_alloc_start = local_alloc_bottom;
16845827d416SAnton Blanchard 	prom_tce_alloc_end = local_alloc_top;
16859b6b563cSPaul Mackerras 
16869b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
16879b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
16889b6b563cSPaul Mackerras }
1689493adffcSBenjamin Herrenschmidt #endif /* __BIG_ENDIAN__ */
1690493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
16919b6b563cSPaul Mackerras 
16929b6b563cSPaul Mackerras /*
16939b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
16949b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
16959b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
16969b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
16979b6b563cSPaul Mackerras  *
16989b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
16999b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
17009b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
17019b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
17029b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
17039b6b563cSPaul Mackerras  * of setting those values.
17049b6b563cSPaul Mackerras  *
17059b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
17069b6b563cSPaul Mackerras  * is in its holding pattern code.
17079b6b563cSPaul Mackerras  *
17089b6b563cSPaul Mackerras  * -- Cort
17099b6b563cSPaul Mackerras  */
1710bbd0abdaSPaul Mackerras /*
1711bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
1712bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
1713bbd0abdaSPaul Mackerras  */
1714bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1715bbd0abdaSPaul Mackerras 
17169b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
17179b6b563cSPaul Mackerras {
17189b6b563cSPaul Mackerras 	unsigned long i;
17199b6b563cSPaul Mackerras 	phandle node;
17209b6b563cSPaul Mackerras 	char type[64];
17219b6b563cSPaul Mackerras 	unsigned long *spinloop
1722bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
17239b6b563cSPaul Mackerras 	unsigned long *acknowledge
1724bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1725bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
17269b6b563cSPaul Mackerras 
1727dbe78b40SBenjamin Herrenschmidt 	/*
1728dbe78b40SBenjamin Herrenschmidt 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
1729dbe78b40SBenjamin Herrenschmidt 	 * we skip this stage, the CPUs will be started by the
1730dbe78b40SBenjamin Herrenschmidt 	 * kernel using RTAS.
1731dbe78b40SBenjamin Herrenschmidt 	 */
1732dbe78b40SBenjamin Herrenschmidt 	if ((of_platform == PLATFORM_PSERIES ||
1733dbe78b40SBenjamin Herrenschmidt 	     of_platform == PLATFORM_PSERIES_LPAR) &&
1734dbe78b40SBenjamin Herrenschmidt 	    rtas_has_query_cpu_stopped) {
1735dbe78b40SBenjamin Herrenschmidt 		prom_printf("prom_hold_cpus: skipped\n");
1736dbe78b40SBenjamin Herrenschmidt 		return;
1737dbe78b40SBenjamin Herrenschmidt 	}
1738dbe78b40SBenjamin Herrenschmidt 
17399b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
17409b6b563cSPaul Mackerras 	prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
17419b6b563cSPaul Mackerras 	prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
17429b6b563cSPaul Mackerras 	prom_debug("    1) acknowledge    = 0x%x\n",
17439b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
17449b6b563cSPaul Mackerras 	prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
17459b6b563cSPaul Mackerras 	prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
17469b6b563cSPaul Mackerras 
17479b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
17489b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
17499b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
17509b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
17519b6b563cSPaul Mackerras 	 */
17529b6b563cSPaul Mackerras 	*spinloop = 0;
17539b6b563cSPaul Mackerras 
17549b6b563cSPaul Mackerras 	/* look for cpus */
17559b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
1756493adffcSBenjamin Herrenschmidt 		unsigned int cpu_no;
1757493adffcSBenjamin Herrenschmidt 		__be32 reg;
1758493adffcSBenjamin Herrenschmidt 
17599b6b563cSPaul Mackerras 		type[0] = 0;
17609b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
17615827d416SAnton Blanchard 		if (strcmp(type, "cpu") != 0)
17629b6b563cSPaul Mackerras 			continue;
17639b6b563cSPaul Mackerras 
17649b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
17659b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
17665827d416SAnton Blanchard 			if (strcmp(type, "okay") != 0)
17679b6b563cSPaul Mackerras 				continue;
17689b6b563cSPaul Mackerras 
1769493adffcSBenjamin Herrenschmidt 		reg = cpu_to_be32(-1); /* make sparse happy */
17709b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
1771493adffcSBenjamin Herrenschmidt 		cpu_no = be32_to_cpu(reg);
17729b6b563cSPaul Mackerras 
1773493adffcSBenjamin Herrenschmidt 		prom_debug("cpu hw idx   = %lu\n", cpu_no);
17749b6b563cSPaul Mackerras 
17759b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
17769b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
17779b6b563cSPaul Mackerras 		 * spinloop.
17789b6b563cSPaul Mackerras 		 */
17799b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
17809b6b563cSPaul Mackerras 
1781493adffcSBenjamin Herrenschmidt 		if (cpu_no != prom.cpu) {
178227f44888SBenjamin Herrenschmidt 			/* Primary Thread of non-boot cpu or any thread */
1783493adffcSBenjamin Herrenschmidt 			prom_printf("starting cpu hw idx %lu... ", cpu_no);
17849b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
1785493adffcSBenjamin Herrenschmidt 				  secondary_hold, cpu_no);
17869b6b563cSPaul Mackerras 
17879b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
17889b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
17899b6b563cSPaul Mackerras 				mb();
17909b6b563cSPaul Mackerras 
1791493adffcSBenjamin Herrenschmidt 			if (*acknowledge == cpu_no)
17929b6b563cSPaul Mackerras 				prom_printf("done\n");
1793bbd0abdaSPaul Mackerras 			else
17949b6b563cSPaul Mackerras 				prom_printf("failed: %x\n", *acknowledge);
17959b6b563cSPaul Mackerras 		}
17969b6b563cSPaul Mackerras #ifdef CONFIG_SMP
17979b6b563cSPaul Mackerras 		else
1798493adffcSBenjamin Herrenschmidt 			prom_printf("boot cpu hw idx %lu\n", cpu_no);
17999b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
18009b6b563cSPaul Mackerras 	}
18019b6b563cSPaul Mackerras 
18029b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
18039b6b563cSPaul Mackerras }
18049b6b563cSPaul Mackerras 
18059b6b563cSPaul Mackerras 
18069b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
18079b6b563cSPaul Mackerras {
18089b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
18095827d416SAnton Blanchard 	prom_entry = pp;
18109b6b563cSPaul Mackerras 
18119b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
18125827d416SAnton Blanchard 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
18135827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.chosen))
18149b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
18159b6b563cSPaul Mackerras 
18169b6b563cSPaul Mackerras 	/* get device tree root */
18175827d416SAnton Blanchard 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
18185827d416SAnton Blanchard 	if (!PHANDLE_VALID(prom.root))
18199b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1820a575b807SPaul Mackerras 
18215827d416SAnton Blanchard 	prom.mmumap = 0;
18229b6b563cSPaul Mackerras }
18239b6b563cSPaul Mackerras 
1824a575b807SPaul Mackerras #ifdef CONFIG_PPC32
1825a575b807SPaul Mackerras /*
1826a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
1827a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
1828a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
1829a575b807SPaul Mackerras  */
1830a575b807SPaul Mackerras static void __init prom_find_mmu(void)
1831a575b807SPaul Mackerras {
1832a575b807SPaul Mackerras 	phandle oprom;
1833a575b807SPaul Mackerras 	char version[64];
1834a575b807SPaul Mackerras 
1835a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1836a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
1837a575b807SPaul Mackerras 		return;
1838a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1839a575b807SPaul Mackerras 		return;
1840a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
1841a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
1842a23414beSPaul Mackerras 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1843a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
1844a23414beSPaul Mackerras 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1845a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1846a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1847a23414beSPaul Mackerras 	} else
1848a575b807SPaul Mackerras 		return;
18495827d416SAnton Blanchard 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
18505827d416SAnton Blanchard 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
18515827d416SAnton Blanchard 		     sizeof(prom.mmumap));
1852493adffcSBenjamin Herrenschmidt 	prom.mmumap = be32_to_cpu(prom.mmumap);
18535827d416SAnton Blanchard 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
1854a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
1855a575b807SPaul Mackerras }
1856a575b807SPaul Mackerras #else
1857a575b807SPaul Mackerras #define prom_find_mmu()
1858a575b807SPaul Mackerras #endif
1859a575b807SPaul Mackerras 
18609b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
18619b6b563cSPaul Mackerras {
18625827d416SAnton Blanchard 	char *path = of_stdout_device;
18639b6b563cSPaul Mackerras 	char type[16];
1864493adffcSBenjamin Herrenschmidt 	phandle stdout_node;
1865493adffcSBenjamin Herrenschmidt 	__be32 val;
18669b6b563cSPaul Mackerras 
18675827d416SAnton Blanchard 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
18689b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
18699b6b563cSPaul Mackerras 
1870493adffcSBenjamin Herrenschmidt 	prom.stdout = be32_to_cpu(val);
18719b6b563cSPaul Mackerras 
18729b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
18739b6b563cSPaul Mackerras 	memset(path, 0, 256);
18745827d416SAnton Blanchard 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
18755827d416SAnton Blanchard 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
18765827d416SAnton Blanchard 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
1877a23414beSPaul Mackerras 		     path, strlen(path) + 1);
18789b6b563cSPaul Mackerras 
187910348f59SBenjamin Herrenschmidt 	/* instance-to-package fails on PA-Semi */
188010348f59SBenjamin Herrenschmidt 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
188110348f59SBenjamin Herrenschmidt 	if (stdout_node != PROM_ERROR) {
188210348f59SBenjamin Herrenschmidt 		val = cpu_to_be32(stdout_node);
188310348f59SBenjamin Herrenschmidt 		prom_setprop(prom.chosen, "/chosen", "linux,stdout-package",
188410348f59SBenjamin Herrenschmidt 			     &val, sizeof(val));
188510348f59SBenjamin Herrenschmidt 
18869b6b563cSPaul Mackerras 		/* If it's a display, note it */
18879b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
1888493adffcSBenjamin Herrenschmidt 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
18895827d416SAnton Blanchard 		if (strcmp(type, "display") == 0)
1890493adffcSBenjamin Herrenschmidt 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
18919b6b563cSPaul Mackerras 	}
189210348f59SBenjamin Herrenschmidt }
18939b6b563cSPaul Mackerras 
18949b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
18959b6b563cSPaul Mackerras {
18969b6b563cSPaul Mackerras 	char compat[256];
18979b6b563cSPaul Mackerras 	int len, i = 0;
189821fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
18999b6b563cSPaul Mackerras 	phandle rtas;
1900e8222502SBenjamin Herrenschmidt 	int x;
190121fe3301SBenjamin Herrenschmidt #endif
1902e8222502SBenjamin Herrenschmidt 
19036e35d5daSBenjamin Herrenschmidt 	/* Look for a PowerMac or a Cell */
19045827d416SAnton Blanchard 	len = prom_getprop(prom.root, "compatible",
19059b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
19069b6b563cSPaul Mackerras 	if (len > 0) {
19079b6b563cSPaul Mackerras 		compat[len] = 0;
19089b6b563cSPaul Mackerras 		while (i < len) {
19099b6b563cSPaul Mackerras 			char *p = &compat[i];
19109b6b563cSPaul Mackerras 			int sl = strlen(p);
19119b6b563cSPaul Mackerras 			if (sl == 0)
19129b6b563cSPaul Mackerras 				break;
19135827d416SAnton Blanchard 			if (strstr(p, "Power Macintosh") ||
19145827d416SAnton Blanchard 			    strstr(p, "MacRISC"))
19159b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
1916133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
1917133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
1918133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
1919133dda1eSArnd Bergmann 			 * so we do it here.
1920133dda1eSArnd Bergmann 			 */
19215827d416SAnton Blanchard 			if (strstr(p, "IBM,CBEA") ||
19225827d416SAnton Blanchard 			    strstr(p, "IBM,CPBW-1.0"))
1923133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
1924133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
19259b6b563cSPaul Mackerras 			i += sl + 1;
19269b6b563cSPaul Mackerras 		}
19279b6b563cSPaul Mackerras 	}
19289b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
19296e35d5daSBenjamin Herrenschmidt 	/* Try to detect OPAL */
19306e35d5daSBenjamin Herrenschmidt 	if (PHANDLE_VALID(call_prom("finddevice", 1, 1, ADDR("/ibm,opal"))))
19316e35d5daSBenjamin Herrenschmidt 		return PLATFORM_OPAL;
19326e35d5daSBenjamin Herrenschmidt 
19336e35d5daSBenjamin Herrenschmidt 	/* Try to figure out if it's an IBM pSeries or any other
1934e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
1935e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
1936e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
1937e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
1938e8222502SBenjamin Herrenschmidt 	 */
19395827d416SAnton Blanchard 	len = prom_getprop(prom.root, "device_type",
1940e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
1941e8222502SBenjamin Herrenschmidt 	if (len <= 0)
1942e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
19435827d416SAnton Blanchard 	if (strcmp(compat, "chrp"))
1944e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
1945e8222502SBenjamin Herrenschmidt 
19469b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
19479b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1948e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
1949e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
1950e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
19519b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
19524da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
19539b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
19549b6b563cSPaul Mackerras 	}
19559b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
19569b6b563cSPaul Mackerras #else
1957e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
19589b6b563cSPaul Mackerras #endif
19599b6b563cSPaul Mackerras }
19609b6b563cSPaul Mackerras 
19619b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
19629b6b563cSPaul Mackerras {
19639b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
19649b6b563cSPaul Mackerras }
19659b6b563cSPaul Mackerras 
19669b6b563cSPaul Mackerras /*
19679b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
19689b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
19699b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
19709b6b563cSPaul Mackerras  * we've taken over the MMU.
19719b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
19729b6b563cSPaul Mackerras  * and if so, open it now.
19739b6b563cSPaul Mackerras  */
19749b6b563cSPaul Mackerras static void __init prom_check_displays(void)
19759b6b563cSPaul Mackerras {
19769b6b563cSPaul Mackerras 	char type[16], *path;
19779b6b563cSPaul Mackerras 	phandle node;
19789b6b563cSPaul Mackerras 	ihandle ih;
19799b6b563cSPaul Mackerras 	int i;
19809b6b563cSPaul Mackerras 
19819b6b563cSPaul Mackerras 	static unsigned char default_colors[] = {
19829b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
19839b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
19849b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
19859b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
19869b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
19879b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
19889b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
19899b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
19909b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
19919b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
19929b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
19939b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
19949b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
19959b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
19969b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
19979b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
19989b6b563cSPaul Mackerras 	};
19999b6b563cSPaul Mackerras 	const unsigned char *clut;
20009b6b563cSPaul Mackerras 
20014da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
20029b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
20039b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
20049b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
20055827d416SAnton Blanchard 		if (strcmp(type, "display") != 0)
20069b6b563cSPaul Mackerras 			continue;
20079b6b563cSPaul Mackerras 
20089b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
20095827d416SAnton Blanchard 		path = prom_scratch;
20109b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
20119b6b563cSPaul Mackerras 
20129b6b563cSPaul Mackerras 		/*
20139b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
20149b6b563cSPaul Mackerras 		 * arguments
20159b6b563cSPaul Mackerras 		 */
20169b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
20179b6b563cSPaul Mackerras 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
20189b6b563cSPaul Mackerras 			continue;
20199b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
20209b6b563cSPaul Mackerras 
20219b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
20229b6b563cSPaul Mackerras 		if (ih == 0) {
20239b6b563cSPaul Mackerras 			prom_printf("failed\n");
20249b6b563cSPaul Mackerras 			continue;
20259b6b563cSPaul Mackerras 		}
20269b6b563cSPaul Mackerras 
20279b6b563cSPaul Mackerras 		/* Success */
20289b6b563cSPaul Mackerras 		prom_printf("done\n");
2029a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
20309b6b563cSPaul Mackerras 
20319b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
20329b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
20335827d416SAnton Blanchard 		clut = default_colors;
20343f53638cSBenjamin Herrenschmidt 		for (i = 0; i < 16; i++, clut += 3)
20359b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
20369b6b563cSPaul Mackerras 					   clut[2]) != 0)
20379b6b563cSPaul Mackerras 				break;
20389b6b563cSPaul Mackerras 
20399b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
20405827d416SAnton Blanchard 		clut = PTRRELOC(logo_linux_clut224.clut);
20415827d416SAnton Blanchard 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
20429b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
20439b6b563cSPaul Mackerras 					   clut[2]) != 0)
20449b6b563cSPaul Mackerras 				break;
20459b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
20467191b615SBenjamin Herrenschmidt 
20477191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
20487191b615SBenjamin Herrenschmidt 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
20497191b615SBenjamin Herrenschmidt 		    PROM_ERROR) {
20507191b615SBenjamin Herrenschmidt 			u32 width, height, pitch, addr;
20517191b615SBenjamin Herrenschmidt 
20527191b615SBenjamin Herrenschmidt 			prom_printf("Setting btext !\n");
20537191b615SBenjamin Herrenschmidt 			prom_getprop(node, "width", &width, 4);
20547191b615SBenjamin Herrenschmidt 			prom_getprop(node, "height", &height, 4);
20557191b615SBenjamin Herrenschmidt 			prom_getprop(node, "linebytes", &pitch, 4);
20567191b615SBenjamin Herrenschmidt 			prom_getprop(node, "address", &addr, 4);
20577191b615SBenjamin Herrenschmidt 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
20587191b615SBenjamin Herrenschmidt 				    width, height, pitch, addr);
20597191b615SBenjamin Herrenschmidt 			btext_setup_display(width, height, 8, pitch, addr);
20607191b615SBenjamin Herrenschmidt 		}
20617191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
20629b6b563cSPaul Mackerras 	}
20639b6b563cSPaul Mackerras }
20649b6b563cSPaul Mackerras 
20659b6b563cSPaul Mackerras 
20669b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
20679b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
20689b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
20699b6b563cSPaul Mackerras {
20709b6b563cSPaul Mackerras 	void *ret;
20719b6b563cSPaul Mackerras 
20729b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
20739b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
20749b6b563cSPaul Mackerras 		unsigned long room, chunk;
20759b6b563cSPaul Mackerras 
20769b6b563cSPaul Mackerras 		prom_debug("Chunk exhausted, claiming more at %x...\n",
20775827d416SAnton Blanchard 			   alloc_bottom);
20785827d416SAnton Blanchard 		room = alloc_top - alloc_bottom;
20799b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
20809b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
20819b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
2082fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2083fbafd728SAnton Blanchard 				   "(no room)\n");
20849b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
20859b6b563cSPaul Mackerras 		if (chunk == 0)
2086fbafd728SAnton Blanchard 			prom_panic("No memory for flatten_device_tree "
2087fbafd728SAnton Blanchard 				   "(claim failed)\n");
2088966728ddSAnton Blanchard 		*mem_end = chunk + room;
20899b6b563cSPaul Mackerras 	}
20909b6b563cSPaul Mackerras 
20919b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
20929b6b563cSPaul Mackerras 	*mem_start += needed;
20939b6b563cSPaul Mackerras 
20949b6b563cSPaul Mackerras 	return ret;
20959b6b563cSPaul Mackerras }
20969b6b563cSPaul Mackerras 
2097493adffcSBenjamin Herrenschmidt #define dt_push_token(token, mem_start, mem_end) do { 			\
2098493adffcSBenjamin Herrenschmidt 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2099493adffcSBenjamin Herrenschmidt 		*(__be32 *)room = cpu_to_be32(token);			\
2100493adffcSBenjamin Herrenschmidt 	} while(0)
21019b6b563cSPaul Mackerras 
21029b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
21039b6b563cSPaul Mackerras {
21049b6b563cSPaul Mackerras 	char *s, *os;
21059b6b563cSPaul Mackerras 
21065827d416SAnton Blanchard 	s = os = (char *)dt_string_start;
21079b6b563cSPaul Mackerras 	s += 4;
21085827d416SAnton Blanchard 	while (s <  (char *)dt_string_end) {
21099b6b563cSPaul Mackerras 		if (strcmp(s, str) == 0)
21109b6b563cSPaul Mackerras 			return s - os;
21119b6b563cSPaul Mackerras 		s += strlen(s) + 1;
21129b6b563cSPaul Mackerras 	}
21139b6b563cSPaul Mackerras 	return 0;
21149b6b563cSPaul Mackerras }
21159b6b563cSPaul Mackerras 
21169b6b563cSPaul Mackerras /*
21179b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
21189b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
21199b6b563cSPaul Mackerras  */
21209b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
21219b6b563cSPaul Mackerras 
21229b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
21239b6b563cSPaul Mackerras 					 unsigned long *mem_start,
21249b6b563cSPaul Mackerras 					 unsigned long *mem_end)
21259b6b563cSPaul Mackerras {
21269b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
21279b6b563cSPaul Mackerras 	unsigned long soff;
21289b6b563cSPaul Mackerras 	phandle child;
21299b6b563cSPaul Mackerras 
21305827d416SAnton Blanchard 	sstart =  (char *)dt_string_start;
21319b6b563cSPaul Mackerras 
21329b6b563cSPaul Mackerras 	/* get and store all property names */
21335827d416SAnton Blanchard 	prev_name = "";
21349b6b563cSPaul Mackerras 	for (;;) {
21359b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
21369b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
21379b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
21389b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
21399b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
21409b6b563cSPaul Mackerras 			break;
21419b6b563cSPaul Mackerras 		}
21429b6b563cSPaul Mackerras 
21439b6b563cSPaul Mackerras  		/* skip "name" */
21445827d416SAnton Blanchard  		if (strcmp(namep, "name") == 0) {
21459b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
21465827d416SAnton Blanchard  			prev_name = "name";
21479b6b563cSPaul Mackerras  			continue;
21489b6b563cSPaul Mackerras  		}
21499b6b563cSPaul Mackerras 		/* get/create string entry */
21509b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
21519b6b563cSPaul Mackerras 		if (soff != 0) {
21529b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
21539b6b563cSPaul Mackerras 			namep = sstart + soff;
21549b6b563cSPaul Mackerras 		} else {
21559b6b563cSPaul Mackerras 			/* Trim off some if we can */
21569b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
21575827d416SAnton Blanchard 			dt_string_end = *mem_start;
21589b6b563cSPaul Mackerras 		}
21599b6b563cSPaul Mackerras 		prev_name = namep;
21609b6b563cSPaul Mackerras 	}
21619b6b563cSPaul Mackerras 
21629b6b563cSPaul Mackerras 	/* do all our children */
21639b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
21649b6b563cSPaul Mackerras 	while (child != 0) {
21659b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
21669b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
21679b6b563cSPaul Mackerras 	}
21689b6b563cSPaul Mackerras }
21699b6b563cSPaul Mackerras 
21709b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
21719b6b563cSPaul Mackerras 					unsigned long *mem_end)
21729b6b563cSPaul Mackerras {
21739b6b563cSPaul Mackerras 	phandle child;
21749b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
21759b6b563cSPaul Mackerras 	unsigned long soff;
21769b6b563cSPaul Mackerras 	unsigned char *valp;
21779b6b563cSPaul Mackerras 	static char pname[MAX_PROPERTY_NAME];
21786e35d5daSBenjamin Herrenschmidt 	int l, room, has_phandle = 0;
21799b6b563cSPaul Mackerras 
21809b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
21819b6b563cSPaul Mackerras 
21829b6b563cSPaul Mackerras 	/* get the node's full name */
21839b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
2184c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
2185c4988820SPaul Mackerras 	if (room > 255)
2186c4988820SPaul Mackerras 		room = 255;
2187c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
21889b6b563cSPaul Mackerras 	if (l >= 0) {
21899b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
2190c4988820SPaul Mackerras 		if (l >= room) {
2191c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
21929b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
21939b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
21949b6b563cSPaul Mackerras 		}
21959b6b563cSPaul Mackerras 		namep[l] = '\0';
21969b6b563cSPaul Mackerras 
21979b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
2198a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
2199a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
22009b6b563cSPaul Mackerras 		 */
2201a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
22029b6b563cSPaul Mackerras 			if (*p == '/')
2203a575b807SPaul Mackerras 				lp = namep;
2204a575b807SPaul Mackerras 			else if (*p != 0)
2205a575b807SPaul Mackerras 				*lp++ = *p;
2206a575b807SPaul Mackerras 		}
2207a575b807SPaul Mackerras 		*lp = 0;
2208a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
22099b6b563cSPaul Mackerras 	}
22109b6b563cSPaul Mackerras 
22119b6b563cSPaul Mackerras 	/* get it again for debugging */
22125827d416SAnton Blanchard 	path = prom_scratch;
22139b6b563cSPaul Mackerras 	memset(path, 0, PROM_SCRATCH_SIZE);
22149b6b563cSPaul Mackerras 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
22159b6b563cSPaul Mackerras 
22169b6b563cSPaul Mackerras 	/* get and store all properties */
22175827d416SAnton Blanchard 	prev_name = "";
22185827d416SAnton Blanchard 	sstart = (char *)dt_string_start;
22199b6b563cSPaul Mackerras 	for (;;) {
22209b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
22215827d416SAnton Blanchard 			      pname) != 1)
22229b6b563cSPaul Mackerras 			break;
22239b6b563cSPaul Mackerras 
22249b6b563cSPaul Mackerras  		/* skip "name" */
22255827d416SAnton Blanchard  		if (strcmp(pname, "name") == 0) {
22265827d416SAnton Blanchard  			prev_name = "name";
22279b6b563cSPaul Mackerras  			continue;
22289b6b563cSPaul Mackerras  		}
22299b6b563cSPaul Mackerras 
22309b6b563cSPaul Mackerras 		/* find string offset */
22315827d416SAnton Blanchard 		soff = dt_find_string(pname);
22329b6b563cSPaul Mackerras 		if (soff == 0) {
22339b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
22345827d416SAnton Blanchard 				    " <%s>, node %s\n", pname, path);
22359b6b563cSPaul Mackerras 			break;
22369b6b563cSPaul Mackerras 		}
22379b6b563cSPaul Mackerras 		prev_name = sstart + soff;
22389b6b563cSPaul Mackerras 
22399b6b563cSPaul Mackerras 		/* get length */
22405827d416SAnton Blanchard 		l = call_prom("getproplen", 2, 1, node, pname);
22419b6b563cSPaul Mackerras 
22429b6b563cSPaul Mackerras 		/* sanity checks */
22439b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
22449b6b563cSPaul Mackerras 			continue;
22459b6b563cSPaul Mackerras 
22469b6b563cSPaul Mackerras 		/* push property head */
22479b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
22489b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
22499b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
22509b6b563cSPaul Mackerras 
22519b6b563cSPaul Mackerras 		/* push property content */
22529b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
22535827d416SAnton Blanchard 		call_prom("getprop", 4, 1, node, pname, valp, l);
22549b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
22556e35d5daSBenjamin Herrenschmidt 
22565827d416SAnton Blanchard 		if (!strcmp(pname, "phandle"))
22576e35d5daSBenjamin Herrenschmidt 			has_phandle = 1;
22589b6b563cSPaul Mackerras 	}
22599b6b563cSPaul Mackerras 
22606e35d5daSBenjamin Herrenschmidt 	/* Add a "linux,phandle" property if no "phandle" property already
22616e35d5daSBenjamin Herrenschmidt 	 * existed (can happen with OPAL)
22626e35d5daSBenjamin Herrenschmidt 	 */
22636e35d5daSBenjamin Herrenschmidt 	if (!has_phandle) {
22645827d416SAnton Blanchard 		soff = dt_find_string("linux,phandle");
22659b6b563cSPaul Mackerras 		if (soff == 0)
22669b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
22679b6b563cSPaul Mackerras 				    " <linux-phandle> node %s\n", path);
22689b6b563cSPaul Mackerras 		else {
22699b6b563cSPaul Mackerras 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
22709b6b563cSPaul Mackerras 			dt_push_token(4, mem_start, mem_end);
22719b6b563cSPaul Mackerras 			dt_push_token(soff, mem_start, mem_end);
22729b6b563cSPaul Mackerras 			valp = make_room(mem_start, mem_end, 4, 4);
2273493adffcSBenjamin Herrenschmidt 			*(__be32 *)valp = cpu_to_be32(node);
22749b6b563cSPaul Mackerras 		}
22756e35d5daSBenjamin Herrenschmidt 	}
22769b6b563cSPaul Mackerras 
22779b6b563cSPaul Mackerras 	/* do all our children */
22789b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
22799b6b563cSPaul Mackerras 	while (child != 0) {
22809b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
22819b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
22829b6b563cSPaul Mackerras 	}
22839b6b563cSPaul Mackerras 
22849b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
22859b6b563cSPaul Mackerras }
22869b6b563cSPaul Mackerras 
22879b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
22889b6b563cSPaul Mackerras {
22899b6b563cSPaul Mackerras 	phandle root;
22909b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
22919b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
22929b6b563cSPaul Mackerras 	char *namep;
22939b6b563cSPaul Mackerras 	u64 *rsvmap;
22949b6b563cSPaul Mackerras 
22959b6b563cSPaul Mackerras 	/*
22969b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
2297fbafd728SAnton Blanchard 	 * few pages), crop to 1MB, as this is our "chunk" size
22989b6b563cSPaul Mackerras 	 */
22995827d416SAnton Blanchard 	room = alloc_top - alloc_bottom - 0x4000;
23009b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
23019b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
23025827d416SAnton Blanchard 	prom_debug("starting device tree allocs at %x\n", alloc_bottom);
23039b6b563cSPaul Mackerras 
23049b6b563cSPaul Mackerras 	/* Now try to claim that */
23059b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
23069b6b563cSPaul Mackerras 	if (mem_start == 0)
23079b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
2308966728ddSAnton Blanchard 	mem_end = mem_start + room;
23099b6b563cSPaul Mackerras 
23109b6b563cSPaul Mackerras 	/* Get root of tree */
23119b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
23129b6b563cSPaul Mackerras 	if (root == (phandle)0)
23139b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
23149b6b563cSPaul Mackerras 
23159b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
23169b6b563cSPaul Mackerras 	mem_start = _ALIGN(mem_start, 4);
23179b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
23189b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
23195827d416SAnton Blanchard 	dt_header_start = (unsigned long)hdr;
23209b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
23219b6b563cSPaul Mackerras 
23229b6b563cSPaul Mackerras 	/* Start of strings */
23239b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
23245827d416SAnton Blanchard 	dt_string_start = mem_start;
23259b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
23269b6b563cSPaul Mackerras 
23279b6b563cSPaul Mackerras 	/* Add "linux,phandle" in there, we'll need it */
23289b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
23295827d416SAnton Blanchard 	strcpy(namep, "linux,phandle");
23309b6b563cSPaul Mackerras 	mem_start = (unsigned long)namep + strlen(namep) + 1;
23319b6b563cSPaul Mackerras 
23329b6b563cSPaul Mackerras 	/* Build string array */
23339b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
23349b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
23355827d416SAnton Blanchard 	dt_string_end = mem_start;
23369b6b563cSPaul Mackerras 
23379b6b563cSPaul Mackerras 	/* Build structure */
23389b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
23395827d416SAnton Blanchard 	dt_struct_start = mem_start;
23409b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
23419b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
23429b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
23435827d416SAnton Blanchard 	dt_struct_end = PAGE_ALIGN(mem_start);
23449b6b563cSPaul Mackerras 
23459b6b563cSPaul Mackerras 	/* Finish header */
2346493adffcSBenjamin Herrenschmidt 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2347493adffcSBenjamin Herrenschmidt 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2348493adffcSBenjamin Herrenschmidt 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2349493adffcSBenjamin Herrenschmidt 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2350493adffcSBenjamin Herrenschmidt 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2351493adffcSBenjamin Herrenschmidt 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2352493adffcSBenjamin Herrenschmidt 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2353493adffcSBenjamin Herrenschmidt 	hdr->version = cpu_to_be32(OF_DT_VERSION);
23549b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
2355493adffcSBenjamin Herrenschmidt 	hdr->last_comp_version = cpu_to_be32(0x10);
23569b6b563cSPaul Mackerras 
23574d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
23585827d416SAnton Blanchard 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
23599b6b563cSPaul Mackerras 
23609b6b563cSPaul Mackerras #ifdef DEBUG_PROM
23619b6b563cSPaul Mackerras 	{
23629b6b563cSPaul Mackerras 		int i;
23639b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
23645827d416SAnton Blanchard 		for (i = 0; i < mem_reserve_cnt; i++)
23659b6b563cSPaul Mackerras 			prom_printf("  %x - %x\n",
2366493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].base),
2367493adffcSBenjamin Herrenschmidt 				    be64_to_cpu(mem_reserve_map[i].size));
23689b6b563cSPaul Mackerras 	}
23699b6b563cSPaul Mackerras #endif
23704d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
23714d1f3f25SJimi Xenidis 	 * since it's too late.
23724d1f3f25SJimi Xenidis 	 */
23735827d416SAnton Blanchard 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
23749b6b563cSPaul Mackerras 
23759b6b563cSPaul Mackerras 	prom_printf("Device tree strings 0x%x -> 0x%x\n",
23765827d416SAnton Blanchard 		    dt_string_start, dt_string_end);
23779b6b563cSPaul Mackerras 	prom_printf("Device tree struct  0x%x -> 0x%x\n",
23785827d416SAnton Blanchard 		    dt_struct_start, dt_struct_end);
23799b6b563cSPaul Mackerras }
23809b6b563cSPaul Mackerras 
238154f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
238254f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
238354f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
238454f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
23859b6b563cSPaul Mackerras {
238654f4ee18SHollis Blanchard 	phandle isa;
2387980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
238854f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2389980a6513SBenjamin Herrenschmidt 	char *name;
239054f4ee18SHollis Blanchard 
2391980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2392980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2393980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2394980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2395980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2396980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2397980a6513SBenjamin Herrenschmidt 	}
239854f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
239954f4ee18SHollis Blanchard 		return;
240054f4ee18SHollis Blanchard 
2401980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2402980a6513SBenjamin Herrenschmidt 		return;
240354f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
240454f4ee18SHollis Blanchard 		== PROM_ERROR)
240554f4ee18SHollis Blanchard 		return;
240654f4ee18SHollis Blanchard 
240754f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
240854f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
240954f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
241054f4ee18SHollis Blanchard 		return;
241154f4ee18SHollis Blanchard 
2412980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
241354f4ee18SHollis Blanchard 
241454f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
241554f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2416980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
241754f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
241854f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
241954f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2420980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
242154f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
242254f4ee18SHollis Blanchard }
24238f101a05SHarry Ciao 
24248f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
24258f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
24268f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
24278f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
24288f101a05SHarry Ciao {
24298f101a05SHarry Ciao 	phandle mc;
24308f101a05SHarry Ciao 	u32 mc_reg[4];
24318f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
24328f101a05SHarry Ciao 	u32 ac, sc;
24338f101a05SHarry Ciao 
24348f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
24358f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
24368f101a05SHarry Ciao 		return;
24378f101a05SHarry Ciao 
24388f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
24398f101a05SHarry Ciao 		return;
24408f101a05SHarry Ciao 
24415827d416SAnton Blanchard 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
24425827d416SAnton Blanchard 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
24438f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
24448f101a05SHarry Ciao 		return;
24458f101a05SHarry Ciao 
24468f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
24478f101a05SHarry Ciao 		return;
24488f101a05SHarry Ciao 
24498f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
24508f101a05SHarry Ciao 		return;
24518f101a05SHarry Ciao 
24528f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
24538f101a05SHarry Ciao 
24548f101a05SHarry Ciao 	mc_reg[0] = 0x0;
24558f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
24568f101a05SHarry Ciao 	mc_reg[2] = 0x0;
24578f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
24588f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
24598f101a05SHarry Ciao }
246054f4ee18SHollis Blanchard #else
246154f4ee18SHollis Blanchard #define fixup_device_tree_maple()
24628f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
246354f4ee18SHollis Blanchard #endif
246454f4ee18SHollis Blanchard 
2465e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2466e4805922SOlaf Hering /*
2467e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2468e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2469556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2470e4805922SOlaf Hering  */
2471e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2472e8c0acf9SBenjamin Herrenschmidt {
2473e4805922SOlaf Hering 	phandle ph;
2474e4805922SOlaf Hering 	u32 prop[6];
247526c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2476e8c0acf9SBenjamin Herrenschmidt 	char *name;
2477e8c0acf9SBenjamin Herrenschmidt 	int rc;
2478e8c0acf9SBenjamin Herrenschmidt 
2479e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2480e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2481e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
248226c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2483e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
248426c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
248526c5032eSBenjamin Herrenschmidt 	}
2486e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2487e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2488e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2489e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2490e8c0acf9SBenjamin Herrenschmidt 
2491e4805922SOlaf Hering 			prop[0] = 0x1;
2492e4805922SOlaf Hering 			prop[1] = 0x0;
2493e4805922SOlaf Hering 			prop[2] = rloc;
2494e4805922SOlaf Hering 			prop[3] = 0x0;
2495e4805922SOlaf Hering 			prop[4] = 0x0;
2496e4805922SOlaf Hering 			prop[5] = 0x00010000;
2497e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2498e4805922SOlaf Hering 		}
2499e4805922SOlaf Hering 	}
2500e4805922SOlaf Hering 
2501e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2502e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2503e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2504e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2505e4805922SOlaf Hering 		prop[0] = 14;
2506e4805922SOlaf Hering 		prop[1] = 0x0;
2507556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2508556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2509556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2510556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2511556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2512556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2513556ecf9bSOlaf Hering 		}
2514e4805922SOlaf Hering 	}
2515e8c0acf9SBenjamin Herrenschmidt }
2516e8c0acf9SBenjamin Herrenschmidt #else
2517e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2518e8c0acf9SBenjamin Herrenschmidt #endif
2519e8c0acf9SBenjamin Herrenschmidt 
25209b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
252154f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
252254f4ee18SHollis Blanchard {
25239b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
25249b6b563cSPaul Mackerras 	u32 u3_rev;
25259b6b563cSPaul Mackerras 	u32 interrupts[2];
25269b6b563cSPaul Mackerras 	u32 parent;
25279b6b563cSPaul Mackerras 
25289b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
25299b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
25309b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
25319b6b563cSPaul Mackerras 		return;
25329b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
25339b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
25349b6b563cSPaul Mackerras 		return;
25359b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
25369b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
25379b6b563cSPaul Mackerras 		return;
25389b6b563cSPaul Mackerras 
25399b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
25409b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
25419b6b563cSPaul Mackerras 	    == PROM_ERROR)
25429b6b563cSPaul Mackerras 		return;
25437d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
25449b6b563cSPaul Mackerras 		return;
25459b6b563cSPaul Mackerras 	/* does it need fixup ? */
25469b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
25479b6b563cSPaul Mackerras 		return;
25489b6b563cSPaul Mackerras 
25499b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
25509b6b563cSPaul Mackerras 
25519b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
25529b6b563cSPaul Mackerras 	interrupts[0] = 0;
25539b6b563cSPaul Mackerras 	interrupts[1] = 1;
2554a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2555a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
25569b6b563cSPaul Mackerras 	parent = (u32)mpic;
2557a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2558a23414beSPaul Mackerras 		     &parent, sizeof(parent));
25599b6b563cSPaul Mackerras }
256054f4ee18SHollis Blanchard #else
256154f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
256254f4ee18SHollis Blanchard #endif
25639b6b563cSPaul Mackerras 
256488fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
256594d2dde7SGrant Likely /*
256694d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
256794d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
256894d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
256994d2dde7SGrant Likely  * ethernet node.
257094d2dde7SGrant Likely  */
257194d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
257288fd2a9dSSylvain Munaut {
257388fd2a9dSSylvain Munaut 	u32 node;
257488fd2a9dSSylvain Munaut 	char prop[64];
257594d2dde7SGrant Likely 	int rv;
257688fd2a9dSSylvain Munaut 
257794d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
257894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
257988fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
258088fd2a9dSSylvain Munaut 		return;
258188fd2a9dSSylvain Munaut 
258294d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
258394d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
258494d2dde7SGrant Likely 	if (!rv)
258588fd2a9dSSylvain Munaut 		return;
258688fd2a9dSSylvain Munaut 
258794d2dde7SGrant Likely 	/*
258894d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
258994d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
259094d2dde7SGrant Likely 	 */
259188fd2a9dSSylvain Munaut 
259294d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
25936f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
25946f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
25956f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
25966f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
25976f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
25986f4347c9SOlaf Hering 			" new-device"
25996f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
26006f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
260194d2dde7SGrant Likely 				" s\" mdio\" device-name"
260294d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
26036f4347c9SOlaf Hering 				" s\" compatible\" property"
26046f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
26056f4347c9SOlaf Hering 				" 0x2 encode-int"
26066f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
26076f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
26086f4347c9SOlaf Hering 				" s\" interrupts\" property"
26096f4347c9SOlaf Hering 			" finish-device");
26106f4347c9SOlaf Hering 	};
26116f4347c9SOlaf Hering 
261294d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
261394d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
261494d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
261594d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
26166f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
26176f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
26186f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
26196f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
26206f4347c9SOlaf Hering 			" new-device"
26216f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
26226f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
26236f4347c9SOlaf Hering 				" my-self"
26246f4347c9SOlaf Hering 				" ihandle>phandle"
26256f4347c9SOlaf Hering 			" finish-device"
26266f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
26276f4347c9SOlaf Hering 				" encode-int"
26286f4347c9SOlaf Hering 				" s\" phy-handle\" property"
26296f4347c9SOlaf Hering 			" device-end");
26306f4347c9SOlaf Hering 	}
263194d2dde7SGrant Likely }
26326f4347c9SOlaf Hering 
263394d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
263494d2dde7SGrant Likely {
263594d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
263694d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
263794d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
263894d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
263994d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
264094d2dde7SGrant Likely 	u32 node;
264194d2dde7SGrant Likely 	char prop[64];
264294d2dde7SGrant Likely 	int rv, len;
264394d2dde7SGrant Likely 
264494d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
264594d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
264694d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
264794d2dde7SGrant Likely 		return;
264894d2dde7SGrant Likely 
264994d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
265094d2dde7SGrant Likely 	if (rv == PROM_ERROR)
265194d2dde7SGrant Likely 		return;
265294d2dde7SGrant Likely 	if (strcmp(prop, "EFIKA5K2"))
265394d2dde7SGrant Likely 		return;
265494d2dde7SGrant Likely 
265594d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
265694d2dde7SGrant Likely 
265794d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
265894d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
265994d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
266094d2dde7SGrant Likely 	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
266194d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
266294d2dde7SGrant Likely 
26637f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
26647f4392cdSDavid Woodhouse 	   fix that too */
26657f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
26667f4392cdSDavid Woodhouse 	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
26677f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
26687f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
26697f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
26707f4392cdSDavid Woodhouse 
267194d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
267294d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
267394d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
267494d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
267594d2dde7SGrant Likely 		if (len == 12) {
267694d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
267794d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
267894d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
267994d2dde7SGrant Likely 		}
268094d2dde7SGrant Likely 	}
268194d2dde7SGrant Likely 
268294d2dde7SGrant Likely 	/* Fixup sound interrupts property */
268394d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
268494d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
268594d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
268694d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
268794d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
268894d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
268994d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
269094d2dde7SGrant Likely 		}
269194d2dde7SGrant Likely 	}
269294d2dde7SGrant Likely 
269394d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
269494d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
269588fd2a9dSSylvain Munaut }
269688fd2a9dSSylvain Munaut #else
269788fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
269888fd2a9dSSylvain Munaut #endif
269988fd2a9dSSylvain Munaut 
270050246787SDarren Stevens #ifdef CONFIG_PPC_PASEMI_NEMO
270150246787SDarren Stevens /*
270250246787SDarren Stevens  * CFE supplied on Nemo is broken in several ways, biggest
270350246787SDarren Stevens  * problem is that it reassigns ISA interrupts to unused mpic ints.
270450246787SDarren Stevens  * Add an interrupt-controller property for the io-bridge to use
270550246787SDarren Stevens  * and correct the ints so we can attach them to an irq_domain
270650246787SDarren Stevens  */
270750246787SDarren Stevens static void __init fixup_device_tree_pasemi(void)
270850246787SDarren Stevens {
270950246787SDarren Stevens 	u32 interrupts[2], parent, rval, val = 0;
271050246787SDarren Stevens 	char *name, *pci_name;
271150246787SDarren Stevens 	phandle iob, node;
271250246787SDarren Stevens 
271350246787SDarren Stevens 	/* Find the root pci node */
271450246787SDarren Stevens 	name = "/pxp@0,e0000000";
271550246787SDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
271650246787SDarren Stevens 	if (!PHANDLE_VALID(iob))
271750246787SDarren Stevens 		return;
271850246787SDarren Stevens 
271950246787SDarren Stevens 	/* check if interrupt-controller node set yet */
272050246787SDarren Stevens 	if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
272150246787SDarren Stevens 		return;
272250246787SDarren Stevens 
272350246787SDarren Stevens 	prom_printf("adding interrupt-controller property for SB600...\n");
272450246787SDarren Stevens 
272550246787SDarren Stevens 	prom_setprop(iob, name, "interrupt-controller", &val, 0);
272650246787SDarren Stevens 
272750246787SDarren Stevens 	pci_name = "/pxp@0,e0000000/pci@11";
272850246787SDarren Stevens 	node = call_prom("finddevice", 1, 1, ADDR(pci_name));
272950246787SDarren Stevens 	parent = ADDR(iob);
273050246787SDarren Stevens 
273150246787SDarren Stevens 	for( ; prom_next_node(&node); ) {
273250246787SDarren Stevens 		/* scan each node for one with an interrupt */
273350246787SDarren Stevens 		if (!PHANDLE_VALID(node))
273450246787SDarren Stevens 			continue;
273550246787SDarren Stevens 
273650246787SDarren Stevens 		rval = prom_getproplen(node, "interrupts");
273750246787SDarren Stevens 		if (rval == 0 || rval == PROM_ERROR)
273850246787SDarren Stevens 			continue;
273950246787SDarren Stevens 
274050246787SDarren Stevens 		prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
274150246787SDarren Stevens 		if ((interrupts[0] < 212) || (interrupts[0] > 222))
274250246787SDarren Stevens 			continue;
274350246787SDarren Stevens 
274450246787SDarren Stevens 		/* found a node, update both interrupts and interrupt-parent */
274550246787SDarren Stevens 		if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
274650246787SDarren Stevens 			interrupts[0] -= 203;
274750246787SDarren Stevens 		if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
274850246787SDarren Stevens 			interrupts[0] -= 213;
274950246787SDarren Stevens 		if (interrupts[0] == 221)
275050246787SDarren Stevens 			interrupts[0] = 14;
275150246787SDarren Stevens 		if (interrupts[0] == 222)
275250246787SDarren Stevens 			interrupts[0] = 8;
275350246787SDarren Stevens 
275450246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupts", interrupts,
275550246787SDarren Stevens 					sizeof(interrupts));
275650246787SDarren Stevens 		prom_setprop(node, pci_name, "interrupt-parent", &parent,
275750246787SDarren Stevens 					sizeof(parent));
275850246787SDarren Stevens 	}
2759687e16bcSDarren Stevens 
2760687e16bcSDarren Stevens 	/*
2761687e16bcSDarren Stevens 	 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
2762687e16bcSDarren Stevens 	 * so that generic isa-bridge code can add the SB600 and its on-board
2763687e16bcSDarren Stevens 	 * peripherals.
2764687e16bcSDarren Stevens 	 */
2765687e16bcSDarren Stevens 	name = "/pxp@0,e0000000/io-bridge@0";
2766687e16bcSDarren Stevens 	iob = call_prom("finddevice", 1, 1, ADDR(name));
2767687e16bcSDarren Stevens 	if (!PHANDLE_VALID(iob))
2768687e16bcSDarren Stevens 		return;
2769687e16bcSDarren Stevens 
2770687e16bcSDarren Stevens 	/* device_type is already set, just change it. */
2771687e16bcSDarren Stevens 
2772687e16bcSDarren Stevens 	prom_printf("Changing device_type of SB600 node...\n");
2773687e16bcSDarren Stevens 
2774687e16bcSDarren Stevens 	prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
277550246787SDarren Stevens }
277650246787SDarren Stevens #else	/* !CONFIG_PPC_PASEMI_NEMO */
277750246787SDarren Stevens static inline void fixup_device_tree_pasemi(void) { }
277850246787SDarren Stevens #endif
277950246787SDarren Stevens 
278054f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
278154f4ee18SHollis Blanchard {
278254f4ee18SHollis Blanchard 	fixup_device_tree_maple();
27838f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
2784e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
278554f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
278688fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
278750246787SDarren Stevens 	fixup_device_tree_pasemi();
278854f4ee18SHollis Blanchard }
27899b6b563cSPaul Mackerras 
27909b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
27919b6b563cSPaul Mackerras {
2792493adffcSBenjamin Herrenschmidt 	__be32 rval;
27939b6b563cSPaul Mackerras 	ihandle prom_cpu;
27949b6b563cSPaul Mackerras 	phandle cpu_pkg;
27959b6b563cSPaul Mackerras 
2796493adffcSBenjamin Herrenschmidt 	rval = 0;
2797493adffcSBenjamin Herrenschmidt 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
2798a575b807SPaul Mackerras 		return;
2799493adffcSBenjamin Herrenschmidt 	prom_cpu = be32_to_cpu(rval);
28009b6b563cSPaul Mackerras 
28019b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
28029b6b563cSPaul Mackerras 
2803493adffcSBenjamin Herrenschmidt 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
2804493adffcSBenjamin Herrenschmidt 	prom.cpu = be32_to_cpu(rval);
28059b6b563cSPaul Mackerras 
28065827d416SAnton Blanchard 	prom_debug("Booting CPU hw index = %lu\n", prom.cpu);
28079b6b563cSPaul Mackerras }
28089b6b563cSPaul Mackerras 
28099b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
28109b6b563cSPaul Mackerras {
28119b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
28129b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
2813493adffcSBenjamin Herrenschmidt 		__be64 val;
28149b6b563cSPaul Mackerras 
28155827d416SAnton Blanchard 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
28165827d416SAnton Blanchard 		prom_initrd_end = prom_initrd_start + r4;
28179b6b563cSPaul Mackerras 
2818493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_start);
28195827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
2820a23414beSPaul Mackerras 			     &val, sizeof(val));
2821493adffcSBenjamin Herrenschmidt 		val = cpu_to_be64(prom_initrd_end);
28225827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
2823a23414beSPaul Mackerras 			     &val, sizeof(val));
28249b6b563cSPaul Mackerras 
28255827d416SAnton Blanchard 		reserve_mem(prom_initrd_start,
28265827d416SAnton Blanchard 			    prom_initrd_end - prom_initrd_start);
28279b6b563cSPaul Mackerras 
28285827d416SAnton Blanchard 		prom_debug("initrd_start=0x%x\n", prom_initrd_start);
28295827d416SAnton Blanchard 		prom_debug("initrd_end=0x%x\n", prom_initrd_end);
28309b6b563cSPaul Mackerras 	}
28319b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
28329b6b563cSPaul Mackerras }
28339b6b563cSPaul Mackerras 
28345ac47f7aSAnton Blanchard #ifdef CONFIG_PPC64
28355ac47f7aSAnton Blanchard #ifdef CONFIG_RELOCATABLE
28365ac47f7aSAnton Blanchard static void reloc_toc(void)
28375ac47f7aSAnton Blanchard {
28385ac47f7aSAnton Blanchard }
28395ac47f7aSAnton Blanchard 
28405ac47f7aSAnton Blanchard static void unreloc_toc(void)
28415ac47f7aSAnton Blanchard {
28425ac47f7aSAnton Blanchard }
28435ac47f7aSAnton Blanchard #else
28441674400aSAnton Blanchard static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
28455ac47f7aSAnton Blanchard {
28465ac47f7aSAnton Blanchard 	unsigned long i;
28471674400aSAnton Blanchard 	unsigned long *toc_entry;
28481674400aSAnton Blanchard 
28491674400aSAnton Blanchard 	/* Get the start of the TOC by using r2 directly. */
28501674400aSAnton Blanchard 	asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
28515ac47f7aSAnton Blanchard 
28525ac47f7aSAnton Blanchard 	for (i = 0; i < nr_entries; i++) {
28535ac47f7aSAnton Blanchard 		*toc_entry = *toc_entry + offset;
28545ac47f7aSAnton Blanchard 		toc_entry++;
28555ac47f7aSAnton Blanchard 	}
28565ac47f7aSAnton Blanchard }
28575ac47f7aSAnton Blanchard 
28585ac47f7aSAnton Blanchard static void reloc_toc(void)
28595ac47f7aSAnton Blanchard {
28605ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
28615ac47f7aSAnton Blanchard 	unsigned long nr_entries =
28625ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
28635ac47f7aSAnton Blanchard 
28641674400aSAnton Blanchard 	__reloc_toc(offset, nr_entries);
28655ac47f7aSAnton Blanchard 
28665ac47f7aSAnton Blanchard 	mb();
28675ac47f7aSAnton Blanchard }
28685ac47f7aSAnton Blanchard 
28695ac47f7aSAnton Blanchard static void unreloc_toc(void)
28705ac47f7aSAnton Blanchard {
28715ac47f7aSAnton Blanchard 	unsigned long offset = reloc_offset();
28725ac47f7aSAnton Blanchard 	unsigned long nr_entries =
28735ac47f7aSAnton Blanchard 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
28745ac47f7aSAnton Blanchard 
28755ac47f7aSAnton Blanchard 	mb();
28765ac47f7aSAnton Blanchard 
28771674400aSAnton Blanchard 	__reloc_toc(-offset, nr_entries);
28785ac47f7aSAnton Blanchard }
28795ac47f7aSAnton Blanchard #endif
28805ac47f7aSAnton Blanchard #endif
288127f44888SBenjamin Herrenschmidt 
28829b6b563cSPaul Mackerras /*
28839b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
28849b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
28859b6b563cSPaul Mackerras  */
28869b6b563cSPaul Mackerras 
28879b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
28889b6b563cSPaul Mackerras 			       unsigned long pp,
2889549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
2890549e8152SPaul Mackerras 			       unsigned long kbase)
28919b6b563cSPaul Mackerras {
28929b6b563cSPaul Mackerras 	unsigned long hdr;
28939b6b563cSPaul Mackerras 
28949b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
2895549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
28969b6b563cSPaul Mackerras 	reloc_got2(offset);
28975ac47f7aSAnton Blanchard #else
28985ac47f7aSAnton Blanchard 	reloc_toc();
28999b6b563cSPaul Mackerras #endif
29009b6b563cSPaul Mackerras 
29019b6b563cSPaul Mackerras 	/*
29029b6b563cSPaul Mackerras 	 * First zero the BSS
29039b6b563cSPaul Mackerras 	 */
29045827d416SAnton Blanchard 	memset(&__bss_start, 0, __bss_stop - __bss_start);
29059b6b563cSPaul Mackerras 
29069b6b563cSPaul Mackerras 	/*
29079b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
29089b6b563cSPaul Mackerras 	 * like /chosen
29099b6b563cSPaul Mackerras 	 */
29109b6b563cSPaul Mackerras 	prom_init_client_services(pp);
29119b6b563cSPaul Mackerras 
29129b6b563cSPaul Mackerras 	/*
2913a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
2914a23414beSPaul Mackerras 	 * and other workarounds
2915a23414beSPaul Mackerras 	 */
2916a23414beSPaul Mackerras 	prom_find_mmu();
2917a23414beSPaul Mackerras 
2918a23414beSPaul Mackerras 	/*
29199b6b563cSPaul Mackerras 	 * Init prom stdout device
29209b6b563cSPaul Mackerras 	 */
29219b6b563cSPaul Mackerras 	prom_init_stdout();
29229b6b563cSPaul Mackerras 
29235827d416SAnton Blanchard 	prom_printf("Preparing to boot %s", linux_banner);
2924e7943fbbSMichael Ellerman 
29259b6b563cSPaul Mackerras 	/*
29269b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
29279b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
29289b6b563cSPaul Mackerras 	 */
29295827d416SAnton Blanchard 	of_platform = prom_find_machine_type();
29305827d416SAnton Blanchard 	prom_printf("Detected machine type: %x\n", of_platform);
29319b6b563cSPaul Mackerras 
29320f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
2933add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
2934add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
2935add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
2936549e8152SPaul Mackerras #endif
2937add60ef3SOlaf Hering 
2938add60ef3SOlaf Hering 	/*
2939add60ef3SOlaf Hering 	 * Check for an initrd
2940add60ef3SOlaf Hering 	 */
2941add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
2942add60ef3SOlaf Hering 
294327f44888SBenjamin Herrenschmidt #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
29449b6b563cSPaul Mackerras 	/*
29459b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
29469b6b563cSPaul Mackerras 	 */
29475827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES ||
29485827d416SAnton Blanchard 	    of_platform == PLATFORM_PSERIES_LPAR)
29499b6b563cSPaul Mackerras 		prom_send_capabilities();
29509b6b563cSPaul Mackerras #endif
29519b6b563cSPaul Mackerras 
29529b6b563cSPaul Mackerras 	/*
2953f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
29549b6b563cSPaul Mackerras 	 */
29555827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC)
2956549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
29579b6b563cSPaul Mackerras 
29589b6b563cSPaul Mackerras 	/*
29599b6b563cSPaul Mackerras 	 * Do early parsing of command line
29609b6b563cSPaul Mackerras 	 */
29619b6b563cSPaul Mackerras 	early_cmdline_parse();
29629b6b563cSPaul Mackerras 
29639b6b563cSPaul Mackerras 	/*
29649b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
29659b6b563cSPaul Mackerras 	 */
29669b6b563cSPaul Mackerras 	prom_init_mem();
29679b6b563cSPaul Mackerras 
29689b6b563cSPaul Mackerras 	/*
29699b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
29709b6b563cSPaul Mackerras 	 */
29719b6b563cSPaul Mackerras 	prom_find_boot_cpu();
29729b6b563cSPaul Mackerras 
29739b6b563cSPaul Mackerras 	/*
29749b6b563cSPaul Mackerras 	 * Initialize display devices
29759b6b563cSPaul Mackerras 	 */
29769b6b563cSPaul Mackerras 	prom_check_displays();
29779b6b563cSPaul Mackerras 
2978493adffcSBenjamin Herrenschmidt #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
29799b6b563cSPaul Mackerras 	/*
29809b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
29819b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
29829b6b563cSPaul Mackerras 	 * available for us here...
29839b6b563cSPaul Mackerras 	 */
29845827d416SAnton Blanchard 	if (of_platform == PLATFORM_PSERIES)
29859b6b563cSPaul Mackerras 		prom_initialize_tce_table();
29869b6b563cSPaul Mackerras #endif
29879b6b563cSPaul Mackerras 
29889b6b563cSPaul Mackerras 	/*
298927f44888SBenjamin Herrenschmidt 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
299027f44888SBenjamin Herrenschmidt 	 * have a usable RTAS implementation.
29919b6b563cSPaul Mackerras 	 */
29925827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
29935827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
29949b6b563cSPaul Mackerras 		prom_instantiate_rtas();
299527f44888SBenjamin Herrenschmidt 
299627f44888SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
2997493adffcSBenjamin Herrenschmidt 	if (of_platform == PLATFORM_OPAL)
29986e35d5daSBenjamin Herrenschmidt 		prom_instantiate_opal();
2999493adffcSBenjamin Herrenschmidt #endif /* CONFIG_PPC_POWERNV */
300027f44888SBenjamin Herrenschmidt 
30014a727429SAshley Lai #ifdef CONFIG_PPC64
30024a727429SAshley Lai 	/* instantiate sml */
30034a727429SAshley Lai 	prom_instantiate_sml();
30044a727429SAshley Lai #endif
30054a727429SAshley Lai 
300627f44888SBenjamin Herrenschmidt 	/*
300727f44888SBenjamin Herrenschmidt 	 * On non-powermacs, put all CPUs in spin-loops.
300827f44888SBenjamin Herrenschmidt 	 *
300927f44888SBenjamin Herrenschmidt 	 * PowerMacs use a different mechanism to spin CPUs
3010dbe78b40SBenjamin Herrenschmidt 	 *
3011dbe78b40SBenjamin Herrenschmidt 	 * (This must be done after instanciating RTAS)
301227f44888SBenjamin Herrenschmidt 	 */
30135827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
30145827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
301527f44888SBenjamin Herrenschmidt 		prom_hold_cpus();
30169b6b563cSPaul Mackerras 
30179b6b563cSPaul Mackerras 	/*
30189b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
30199b6b563cSPaul Mackerras 	 */
3020493adffcSBenjamin Herrenschmidt 	if (prom_memory_limit) {
3021493adffcSBenjamin Herrenschmidt 		__be64 val = cpu_to_be64(prom_memory_limit);
30225827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3023493adffcSBenjamin Herrenschmidt 			     &val, sizeof(val));
3024493adffcSBenjamin Herrenschmidt 	}
30259b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
30265827d416SAnton Blanchard 	if (prom_iommu_off)
30275827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3028a23414beSPaul Mackerras 			     NULL, 0);
30299b6b563cSPaul Mackerras 
30305827d416SAnton Blanchard 	if (prom_iommu_force_on)
30315827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3032a23414beSPaul Mackerras 			     NULL, 0);
30339b6b563cSPaul Mackerras 
30345827d416SAnton Blanchard 	if (prom_tce_alloc_start) {
30355827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
30365827d416SAnton Blanchard 			     &prom_tce_alloc_start,
30379b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
30385827d416SAnton Blanchard 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
30395827d416SAnton Blanchard 			     &prom_tce_alloc_end,
30409b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
30419b6b563cSPaul Mackerras 	}
30429b6b563cSPaul Mackerras #endif
30439b6b563cSPaul Mackerras 
30449b6b563cSPaul Mackerras 	/*
30459b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
30469b6b563cSPaul Mackerras 	 */
30479b6b563cSPaul Mackerras 	fixup_device_tree();
30489b6b563cSPaul Mackerras 
30499b6b563cSPaul Mackerras 	/*
30509b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
30519b6b563cSPaul Mackerras 	 */
30529b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
30539b6b563cSPaul Mackerras 	flatten_device_tree();
30549b6b563cSPaul Mackerras 
30553825ac0eSPaul Mackerras 	/*
30563825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
30573825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
305840dfef66SBenjamin Herrenschmidt 	 * closed stdin already (in particular the powerbook 101). It
305940dfef66SBenjamin Herrenschmidt 	 * appears that the OPAL version of OFW doesn't like it either.
30603825ac0eSPaul Mackerras 	 */
30615827d416SAnton Blanchard 	if (of_platform != PLATFORM_POWERMAC &&
30625827d416SAnton Blanchard 	    of_platform != PLATFORM_OPAL)
30639b6b563cSPaul Mackerras 		prom_close_stdin();
30649b6b563cSPaul Mackerras 
30659b6b563cSPaul Mackerras 	/*
30669b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
30679b6b563cSPaul Mackerras 	 * devices etc...
30689b6b563cSPaul Mackerras 	 */
30697e862d7eSMichael Ellerman 	prom_printf("Quiescing Open Firmware ...\n");
30709b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
30719b6b563cSPaul Mackerras 
30729b6b563cSPaul Mackerras 	/*
30739b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
30749b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
30759b6b563cSPaul Mackerras 	 * is common to us and kexec
30769b6b563cSPaul Mackerras 	 */
30775827d416SAnton Blanchard 	hdr = dt_header_start;
307840dfef66SBenjamin Herrenschmidt 
307940dfef66SBenjamin Herrenschmidt 	/* Don't print anything after quiesce under OPAL, it crashes OFW */
30805827d416SAnton Blanchard 	if (of_platform != PLATFORM_OPAL) {
30817d70c63cSBenjamin Herrenschmidt 		prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
30829b6b563cSPaul Mackerras 		prom_debug("->dt_header_start=0x%x\n", hdr);
308340dfef66SBenjamin Herrenschmidt 	}
30849b6b563cSPaul Mackerras 
30859b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
30869b6b563cSPaul Mackerras 	reloc_got2(-offset);
30875ac47f7aSAnton Blanchard #else
30885ac47f7aSAnton Blanchard 	unreloc_toc();
30899b6b563cSPaul Mackerras #endif
30909b6b563cSPaul Mackerras 
30916e35d5daSBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
30926e35d5daSBenjamin Herrenschmidt 	/* OPAL early debug gets the OPAL base & entry in r8 and r9 */
30936e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0,
30945827d416SAnton Blanchard 		prom_opal_base, prom_opal_entry);
30956e35d5daSBenjamin Herrenschmidt #else
30966e35d5daSBenjamin Herrenschmidt 	__start(hdr, kbase, 0, 0, 0, 0, 0);
30976e35d5daSBenjamin Herrenschmidt #endif
30989b6b563cSPaul Mackerras 
30999b6b563cSPaul Mackerras 	return 0;
31009b6b563cSPaul Mackerras }
3101