xref: /linux/arch/powerpc/kernel/prom_init.c (revision cf68787b68a2011664f1670a827c8f202ddb7c3d)
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/system.h>
399b6b563cSPaul Mackerras #include <asm/mmu.h>
409b6b563cSPaul Mackerras #include <asm/pgtable.h>
419b6b563cSPaul Mackerras #include <asm/pci.h>
429b6b563cSPaul Mackerras #include <asm/iommu.h>
439b6b563cSPaul Mackerras #include <asm/btext.h>
449b6b563cSPaul Mackerras #include <asm/sections.h>
459b6b563cSPaul Mackerras #include <asm/machdep.h>
469b6b563cSPaul Mackerras 
479b6b563cSPaul Mackerras #include <linux/linux_logo.h>
489b6b563cSPaul Mackerras 
499b6b563cSPaul Mackerras /*
509b6b563cSPaul Mackerras  * Properties whose value is longer than this get excluded from our
519b6b563cSPaul Mackerras  * copy of the device tree. This value does need to be big enough to
529b6b563cSPaul Mackerras  * ensure that we don't lose things like the interrupt-map property
539b6b563cSPaul Mackerras  * on a PCI-PCI bridge.
549b6b563cSPaul Mackerras  */
559b6b563cSPaul Mackerras #define MAX_PROPERTY_LENGTH	(1UL * 1024 * 1024)
569b6b563cSPaul Mackerras 
579b6b563cSPaul Mackerras /*
589b6b563cSPaul Mackerras  * Eventually bump that one up
599b6b563cSPaul Mackerras  */
609b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE	0x100000
619b6b563cSPaul Mackerras 
629b6b563cSPaul Mackerras /*
639b6b563cSPaul Mackerras  * This is the size of the local memory reserve map that gets copied
649b6b563cSPaul Mackerras  * into the boot params passed to the kernel. That size is totally
659b6b563cSPaul Mackerras  * flexible as the kernel just reads the list until it encounters an
669b6b563cSPaul Mackerras  * entry with size 0, so it can be changed without breaking binary
679b6b563cSPaul Mackerras  * compatibility
689b6b563cSPaul Mackerras  */
699b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE	8
709b6b563cSPaul Mackerras 
719b6b563cSPaul Mackerras /*
729b6b563cSPaul Mackerras  * prom_init() is called very early on, before the kernel text
739b6b563cSPaul Mackerras  * and data have been mapped to KERNELBASE.  At this point the code
749b6b563cSPaul Mackerras  * is running at whatever address it has been loaded at.
759b6b563cSPaul Mackerras  * On ppc32 we compile with -mrelocatable, which means that references
769b6b563cSPaul Mackerras  * to extern and static variables get relocated automatically.
779b6b563cSPaul Mackerras  * On ppc64 we have to relocate the references explicitly with
789b6b563cSPaul Mackerras  * RELOC.  (Note that strings count as static variables.)
799b6b563cSPaul Mackerras  *
809b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
819b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
829b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
839b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
849b6b563cSPaul Mackerras  *
859b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
869b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
879b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
889b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
899b6b563cSPaul Mackerras  */
909b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
919b6b563cSPaul Mackerras #define RELOC(x)        (*PTRRELOC(&(x)))
929b6b563cSPaul Mackerras #define ADDR(x)		(u32) add_reloc_offset((unsigned long)(x))
93a23414beSPaul Mackerras #define OF_WORKAROUNDS	0
949b6b563cSPaul Mackerras #else
959b6b563cSPaul Mackerras #define RELOC(x)	(x)
969b6b563cSPaul Mackerras #define ADDR(x)		(u32) (x)
97a23414beSPaul Mackerras #define OF_WORKAROUNDS	of_workarounds
98a23414beSPaul Mackerras int of_workarounds;
999b6b563cSPaul Mackerras #endif
1009b6b563cSPaul Mackerras 
101a23414beSPaul Mackerras #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
102a23414beSPaul Mackerras #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
103a23414beSPaul Mackerras 
1049b6b563cSPaul Mackerras #define PROM_BUG() do {						\
1059b6b563cSPaul Mackerras         prom_printf("kernel BUG at %s line 0x%x!\n",		\
1069b6b563cSPaul Mackerras 		    RELOC(__FILE__), __LINE__);			\
1079b6b563cSPaul Mackerras         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
1089b6b563cSPaul Mackerras } while (0)
1099b6b563cSPaul Mackerras 
1109b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1119b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1129b6b563cSPaul Mackerras #else
1139b6b563cSPaul Mackerras #define prom_debug(x...)
1149b6b563cSPaul Mackerras #endif
1159b6b563cSPaul Mackerras 
1169b6b563cSPaul Mackerras 
1179b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1189b6b563cSPaul Mackerras 
1199b6b563cSPaul Mackerras struct prom_args {
1209b6b563cSPaul Mackerras         u32 service;
1219b6b563cSPaul Mackerras         u32 nargs;
1229b6b563cSPaul Mackerras         u32 nret;
1239b6b563cSPaul Mackerras         prom_arg_t args[10];
1249b6b563cSPaul Mackerras };
1259b6b563cSPaul Mackerras 
1269b6b563cSPaul Mackerras struct prom_t {
1279b6b563cSPaul Mackerras 	ihandle root;
128a23414beSPaul Mackerras 	phandle chosen;
1299b6b563cSPaul Mackerras 	int cpu;
1309b6b563cSPaul Mackerras 	ihandle stdout;
131a575b807SPaul Mackerras 	ihandle mmumap;
132a23414beSPaul Mackerras 	ihandle memory;
1339b6b563cSPaul Mackerras };
1349b6b563cSPaul Mackerras 
1359b6b563cSPaul Mackerras struct mem_map_entry {
136cbbcf340SKumar Gala 	u64	base;
137cbbcf340SKumar Gala 	u64	size;
1389b6b563cSPaul Mackerras };
1399b6b563cSPaul Mackerras 
1409b6b563cSPaul Mackerras typedef u32 cell_t;
1419b6b563cSPaul Mackerras 
1429b6b563cSPaul Mackerras extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
1439b6b563cSPaul Mackerras 
1449b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
145c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1469b6b563cSPaul Mackerras #else
147c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1489b6b563cSPaul Mackerras {
149c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1509b6b563cSPaul Mackerras }
1519b6b563cSPaul Mackerras #endif
1529b6b563cSPaul Mackerras 
1539b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1549b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1559b6b563cSPaul Mackerras 
1569b6b563cSPaul Mackerras /* prom structure */
1579b6b563cSPaul Mackerras static struct prom_t __initdata prom;
1589b6b563cSPaul Mackerras 
1599b6b563cSPaul Mackerras static unsigned long prom_entry __initdata;
1609b6b563cSPaul Mackerras 
1619b6b563cSPaul Mackerras #define PROM_SCRATCH_SIZE 256
1629b6b563cSPaul Mackerras 
1639b6b563cSPaul Mackerras static char __initdata of_stdout_device[256];
1649b6b563cSPaul Mackerras static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
1659b6b563cSPaul Mackerras 
1669b6b563cSPaul Mackerras static unsigned long __initdata dt_header_start;
1679b6b563cSPaul Mackerras static unsigned long __initdata dt_struct_start, dt_struct_end;
1689b6b563cSPaul Mackerras static unsigned long __initdata dt_string_start, dt_string_end;
1699b6b563cSPaul Mackerras 
1709b6b563cSPaul Mackerras static unsigned long __initdata prom_initrd_start, prom_initrd_end;
1719b6b563cSPaul Mackerras 
1729b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
173165785e5SJeremy Kerr static int __initdata prom_iommu_force_on;
174165785e5SJeremy Kerr static int __initdata prom_iommu_off;
1759b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_start;
1769b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_end;
1779b6b563cSPaul Mackerras #endif
1789b6b563cSPaul Mackerras 
179e8222502SBenjamin Herrenschmidt /* Platforms codes are now obsolete in the kernel. Now only used within this
180e8222502SBenjamin Herrenschmidt  * file and ultimately gone too. Feel free to change them if you need, they
181e8222502SBenjamin Herrenschmidt  * are not shared with anything outside of this file anymore
182e8222502SBenjamin Herrenschmidt  */
183e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES	0x0100
184e8222502SBenjamin Herrenschmidt #define PLATFORM_PSERIES_LPAR	0x0101
185e8222502SBenjamin Herrenschmidt #define PLATFORM_LPAR		0x0001
186e8222502SBenjamin Herrenschmidt #define PLATFORM_POWERMAC	0x0400
187e8222502SBenjamin Herrenschmidt #define PLATFORM_GENERIC	0x0500
188e8222502SBenjamin Herrenschmidt 
1899b6b563cSPaul Mackerras static int __initdata of_platform;
1909b6b563cSPaul Mackerras 
1919b6b563cSPaul Mackerras static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
1929b6b563cSPaul Mackerras 
193*cf68787bSBenjamin Krill static unsigned long __initdata prom_memory_limit;
194*cf68787bSBenjamin Krill 
1959b6b563cSPaul Mackerras static unsigned long __initdata alloc_top;
1969b6b563cSPaul Mackerras static unsigned long __initdata alloc_top_high;
1979b6b563cSPaul Mackerras static unsigned long __initdata alloc_bottom;
1989b6b563cSPaul Mackerras static unsigned long __initdata rmo_top;
1999b6b563cSPaul Mackerras static unsigned long __initdata ram_top;
2009b6b563cSPaul Mackerras 
2019b6b563cSPaul Mackerras static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
2029b6b563cSPaul Mackerras static int __initdata mem_reserve_cnt;
2039b6b563cSPaul Mackerras 
2049b6b563cSPaul Mackerras static cell_t __initdata regbuf[1024];
2059b6b563cSPaul Mackerras 
2069b6b563cSPaul Mackerras 
2079b6b563cSPaul Mackerras /*
2089b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2099b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2109b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2119b6b563cSPaul Mackerras  * it is valid
2129b6b563cSPaul Mackerras  */
2139b6b563cSPaul Mackerras 
2149b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2159b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2169b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2179b6b563cSPaul Mackerras 
2189b6b563cSPaul Mackerras 
2199b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
2209b6b563cSPaul Mackerras  * firmware.
2219b6b563cSPaul Mackerras  */
2229b6b563cSPaul Mackerras 
2239b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
2249b6b563cSPaul Mackerras {
2259b6b563cSPaul Mackerras 	int i;
2269b6b563cSPaul Mackerras 	struct prom_args args;
2279b6b563cSPaul Mackerras 	va_list list;
2289b6b563cSPaul Mackerras 
2299b6b563cSPaul Mackerras 	args.service = ADDR(service);
2309b6b563cSPaul Mackerras 	args.nargs = nargs;
2319b6b563cSPaul Mackerras 	args.nret = nret;
2329b6b563cSPaul Mackerras 
2339b6b563cSPaul Mackerras 	va_start(list, nret);
2349b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
2359b6b563cSPaul Mackerras 		args.args[i] = va_arg(list, prom_arg_t);
2369b6b563cSPaul Mackerras 	va_end(list);
2379b6b563cSPaul Mackerras 
2389b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
2399b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
2409b6b563cSPaul Mackerras 
241c4988820SPaul Mackerras 	if (enter_prom(&args, RELOC(prom_entry)) < 0)
242c4988820SPaul Mackerras 		return PROM_ERROR;
2439b6b563cSPaul Mackerras 
2449b6b563cSPaul Mackerras 	return (nret > 0) ? args.args[nargs] : 0;
2459b6b563cSPaul Mackerras }
2469b6b563cSPaul Mackerras 
2479b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
2489b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
2499b6b563cSPaul Mackerras {
2509b6b563cSPaul Mackerras 	int i;
2519b6b563cSPaul Mackerras 	struct prom_args args;
2529b6b563cSPaul Mackerras 	va_list list;
2539b6b563cSPaul Mackerras 
2549b6b563cSPaul Mackerras 	args.service = ADDR(service);
2559b6b563cSPaul Mackerras 	args.nargs = nargs;
2569b6b563cSPaul Mackerras 	args.nret = nret;
2579b6b563cSPaul Mackerras 
2589b6b563cSPaul Mackerras 	va_start(list, rets);
2599b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
2609b6b563cSPaul Mackerras 		args.args[i] = va_arg(list, prom_arg_t);
2619b6b563cSPaul Mackerras 	va_end(list);
2629b6b563cSPaul Mackerras 
2639b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
264ed1189b7SOlaf Hering 		args.args[nargs+i] = 0;
2659b6b563cSPaul Mackerras 
266c4988820SPaul Mackerras 	if (enter_prom(&args, RELOC(prom_entry)) < 0)
267c4988820SPaul Mackerras 		return PROM_ERROR;
2689b6b563cSPaul Mackerras 
2699b6b563cSPaul Mackerras 	if (rets != NULL)
2709b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
271c5200c90SPaul Mackerras 			rets[i-1] = args.args[nargs+i];
2729b6b563cSPaul Mackerras 
2739b6b563cSPaul Mackerras 	return (nret > 0) ? args.args[nargs] : 0;
2749b6b563cSPaul Mackerras }
2759b6b563cSPaul Mackerras 
2769b6b563cSPaul Mackerras 
2779b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
2789b6b563cSPaul Mackerras {
2799b6b563cSPaul Mackerras 	const char *p, *q;
2809b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
2819b6b563cSPaul Mackerras 
2829b6b563cSPaul Mackerras 	if (_prom->stdout == 0)
2839b6b563cSPaul Mackerras 		return;
2849b6b563cSPaul Mackerras 
2859b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
2869b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
2879b6b563cSPaul Mackerras 			;
2889b6b563cSPaul Mackerras 		if (q > p)
2899b6b563cSPaul Mackerras 			call_prom("write", 3, 1, _prom->stdout, p, q - p);
2909b6b563cSPaul Mackerras 		if (*q == 0)
2919b6b563cSPaul Mackerras 			break;
2929b6b563cSPaul Mackerras 		++q;
2939b6b563cSPaul Mackerras 		call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
2949b6b563cSPaul Mackerras 	}
2959b6b563cSPaul Mackerras }
2969b6b563cSPaul Mackerras 
2979b6b563cSPaul Mackerras 
2989b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
2999b6b563cSPaul Mackerras {
3009b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
3019b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
3029b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
3039b6b563cSPaul Mackerras 
3049b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
3059b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
3069b6b563cSPaul Mackerras 		if (buf[i] > '9')
3079b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
3089b6b563cSPaul Mackerras 		val >>= 4;
3099b6b563cSPaul Mackerras 	}
3109b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
3119b6b563cSPaul Mackerras 	call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
3129b6b563cSPaul Mackerras }
3139b6b563cSPaul Mackerras 
3149b6b563cSPaul Mackerras 
3159b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
3169b6b563cSPaul Mackerras {
3179b6b563cSPaul Mackerras 	const char *p, *q, *s;
3189b6b563cSPaul Mackerras 	va_list args;
3199b6b563cSPaul Mackerras 	unsigned long v;
3209b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
3219b6b563cSPaul Mackerras 
3229b6b563cSPaul Mackerras 	va_start(args, format);
3239b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
3249b6b563cSPaul Mackerras 	format = PTRRELOC(format);
3259b6b563cSPaul Mackerras #endif
3269b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
3279b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
3289b6b563cSPaul Mackerras 			;
3299b6b563cSPaul Mackerras 		if (q > p)
3309b6b563cSPaul Mackerras 			call_prom("write", 3, 1, _prom->stdout, p, q - p);
3319b6b563cSPaul Mackerras 		if (*q == 0)
3329b6b563cSPaul Mackerras 			break;
3339b6b563cSPaul Mackerras 		if (*q == '\n') {
3349b6b563cSPaul Mackerras 			++q;
3359b6b563cSPaul Mackerras 			call_prom("write", 3, 1, _prom->stdout,
3369b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
3379b6b563cSPaul Mackerras 			continue;
3389b6b563cSPaul Mackerras 		}
3399b6b563cSPaul Mackerras 		++q;
3409b6b563cSPaul Mackerras 		if (*q == 0)
3419b6b563cSPaul Mackerras 			break;
3429b6b563cSPaul Mackerras 		switch (*q) {
3439b6b563cSPaul Mackerras 		case 's':
3449b6b563cSPaul Mackerras 			++q;
3459b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
3469b6b563cSPaul Mackerras 			prom_print(s);
3479b6b563cSPaul Mackerras 			break;
3489b6b563cSPaul Mackerras 		case 'x':
3499b6b563cSPaul Mackerras 			++q;
3509b6b563cSPaul Mackerras 			v = va_arg(args, unsigned long);
3519b6b563cSPaul Mackerras 			prom_print_hex(v);
3529b6b563cSPaul Mackerras 			break;
3539b6b563cSPaul Mackerras 		}
3549b6b563cSPaul Mackerras 	}
3559b6b563cSPaul Mackerras }
3569b6b563cSPaul Mackerras 
3579b6b563cSPaul Mackerras 
358a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
359a575b807SPaul Mackerras 				unsigned long align)
360a575b807SPaul Mackerras {
361a575b807SPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
362a575b807SPaul Mackerras 
363a23414beSPaul Mackerras 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
364a23414beSPaul Mackerras 		/*
365a23414beSPaul Mackerras 		 * Old OF requires we claim physical and virtual separately
366a23414beSPaul Mackerras 		 * and then map explicitly (assuming virtual mode)
367a23414beSPaul Mackerras 		 */
368a23414beSPaul Mackerras 		int ret;
369a23414beSPaul Mackerras 		prom_arg_t result;
370a23414beSPaul Mackerras 
371a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
372a23414beSPaul Mackerras 				    ADDR("claim"), _prom->memory,
373a23414beSPaul Mackerras 				    align, size, virt);
374a23414beSPaul Mackerras 		if (ret != 0 || result == -1)
375a23414beSPaul Mackerras 			return -1;
376a23414beSPaul Mackerras 		ret = call_prom_ret("call-method", 5, 2, &result,
377a23414beSPaul Mackerras 				    ADDR("claim"), _prom->mmumap,
378a23414beSPaul Mackerras 				    align, size, virt);
379a23414beSPaul Mackerras 		if (ret != 0) {
380a23414beSPaul Mackerras 			call_prom("call-method", 4, 1, ADDR("release"),
381a23414beSPaul Mackerras 				  _prom->memory, size, virt);
382a23414beSPaul Mackerras 			return -1;
383a23414beSPaul Mackerras 		}
384a23414beSPaul Mackerras 		/* the 0x12 is M (coherence) + PP == read/write */
385a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
386a23414beSPaul Mackerras 			  ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
387a23414beSPaul Mackerras 		return virt;
388a23414beSPaul Mackerras 	}
389a23414beSPaul Mackerras 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
390a23414beSPaul Mackerras 			 (prom_arg_t)align);
391a575b807SPaul Mackerras }
392a575b807SPaul Mackerras 
3939b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
3949b6b563cSPaul Mackerras {
3959b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
3969b6b563cSPaul Mackerras 	reason = PTRRELOC(reason);
3979b6b563cSPaul Mackerras #endif
3989b6b563cSPaul Mackerras 	prom_print(reason);
399add60ef3SOlaf Hering 	/* Do not call exit because it clears the screen on pmac
400add60ef3SOlaf Hering 	 * it also causes some sort of double-fault on early pmacs */
401add60ef3SOlaf Hering 	if (RELOC(of_platform) == PLATFORM_POWERMAC)
402add60ef3SOlaf Hering 		asm("trap\n");
403add60ef3SOlaf Hering 
4049b6b563cSPaul Mackerras 	/* ToDo: should put up an SRC here on p/iSeries */
4059b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
4069b6b563cSPaul Mackerras 
4079b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
4089b6b563cSPaul Mackerras 		;
4099b6b563cSPaul Mackerras }
4109b6b563cSPaul Mackerras 
4119b6b563cSPaul Mackerras 
4129b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
4139b6b563cSPaul Mackerras {
4149b6b563cSPaul Mackerras 	phandle node;
4159b6b563cSPaul Mackerras 
4169b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
4179b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
4189b6b563cSPaul Mackerras 		return 1;
4199b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4209b6b563cSPaul Mackerras 		return 1;
4219b6b563cSPaul Mackerras 	for (;;) {
4229b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
4239b6b563cSPaul Mackerras 			return 0;
4249b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4259b6b563cSPaul Mackerras 			return 1;
4269b6b563cSPaul Mackerras 	}
4279b6b563cSPaul Mackerras }
4289b6b563cSPaul Mackerras 
42921fe3301SBenjamin Herrenschmidt static int inline prom_getprop(phandle node, const char *pname,
4309b6b563cSPaul Mackerras 			       void *value, size_t valuelen)
4319b6b563cSPaul Mackerras {
4329b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
4339b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
4349b6b563cSPaul Mackerras }
4359b6b563cSPaul Mackerras 
43621fe3301SBenjamin Herrenschmidt static int inline prom_getproplen(phandle node, const char *pname)
4379b6b563cSPaul Mackerras {
4389b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
4399b6b563cSPaul Mackerras }
4409b6b563cSPaul Mackerras 
441a23414beSPaul Mackerras static void add_string(char **str, const char *q)
4429b6b563cSPaul Mackerras {
443a23414beSPaul Mackerras 	char *p = *str;
444a23414beSPaul Mackerras 
445a23414beSPaul Mackerras 	while (*q)
446a23414beSPaul Mackerras 		*p++ = *q++;
447a23414beSPaul Mackerras 	*p++ = ' ';
448a23414beSPaul Mackerras 	*str = p;
449a23414beSPaul Mackerras }
450a23414beSPaul Mackerras 
451a23414beSPaul Mackerras static char *tohex(unsigned int x)
452a23414beSPaul Mackerras {
453a23414beSPaul Mackerras 	static char digits[] = "0123456789abcdef";
454a23414beSPaul Mackerras 	static char result[9];
455a23414beSPaul Mackerras 	int i;
456a23414beSPaul Mackerras 
457a23414beSPaul Mackerras 	result[8] = 0;
458a23414beSPaul Mackerras 	i = 8;
459a23414beSPaul Mackerras 	do {
460a23414beSPaul Mackerras 		--i;
461a23414beSPaul Mackerras 		result[i] = digits[x & 0xf];
462a23414beSPaul Mackerras 		x >>= 4;
463a23414beSPaul Mackerras 	} while (x != 0 && i > 0);
464a23414beSPaul Mackerras 	return &result[i];
465a23414beSPaul Mackerras }
466a23414beSPaul Mackerras 
467a23414beSPaul Mackerras static int __init prom_setprop(phandle node, const char *nodename,
468a23414beSPaul Mackerras 			       const char *pname, void *value, size_t valuelen)
469a23414beSPaul Mackerras {
470a23414beSPaul Mackerras 	char cmd[256], *p;
471a23414beSPaul Mackerras 
472a23414beSPaul Mackerras 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
4739b6b563cSPaul Mackerras 		return call_prom("setprop", 4, 1, node, ADDR(pname),
4749b6b563cSPaul Mackerras 				 (u32)(unsigned long) value, (u32) valuelen);
475a23414beSPaul Mackerras 
476a23414beSPaul Mackerras 	/* gah... setprop doesn't work on longtrail, have to use interpret */
477a23414beSPaul Mackerras 	p = cmd;
478a23414beSPaul Mackerras 	add_string(&p, "dev");
479a23414beSPaul Mackerras 	add_string(&p, nodename);
480a23414beSPaul Mackerras 	add_string(&p, tohex((u32)(unsigned long) value));
481a23414beSPaul Mackerras 	add_string(&p, tohex(valuelen));
482a23414beSPaul Mackerras 	add_string(&p, tohex(ADDR(pname)));
483a23414beSPaul Mackerras 	add_string(&p, tohex(strlen(RELOC(pname))));
484a23414beSPaul Mackerras 	add_string(&p, "property");
485a23414beSPaul Mackerras 	*p = 0;
486a23414beSPaul Mackerras 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
4879b6b563cSPaul Mackerras }
4889b6b563cSPaul Mackerras 
489*cf68787bSBenjamin Krill /* We can't use the standard versions because of RELOC headaches. */
490*cf68787bSBenjamin Krill #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
491*cf68787bSBenjamin Krill 			 || ('a' <= (c) && (c) <= 'f') \
492*cf68787bSBenjamin Krill 			 || ('A' <= (c) && (c) <= 'F'))
493*cf68787bSBenjamin Krill 
494*cf68787bSBenjamin Krill #define isdigit(c)	('0' <= (c) && (c) <= '9')
495*cf68787bSBenjamin Krill #define islower(c)	('a' <= (c) && (c) <= 'z')
496*cf68787bSBenjamin Krill #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
497*cf68787bSBenjamin Krill 
498*cf68787bSBenjamin Krill unsigned long prom_strtoul(const char *cp, const char **endp)
499*cf68787bSBenjamin Krill {
500*cf68787bSBenjamin Krill 	unsigned long result = 0, base = 10, value;
501*cf68787bSBenjamin Krill 
502*cf68787bSBenjamin Krill 	if (*cp == '0') {
503*cf68787bSBenjamin Krill 		base = 8;
504*cf68787bSBenjamin Krill 		cp++;
505*cf68787bSBenjamin Krill 		if (toupper(*cp) == 'X') {
506*cf68787bSBenjamin Krill 			cp++;
507*cf68787bSBenjamin Krill 			base = 16;
508*cf68787bSBenjamin Krill 		}
509*cf68787bSBenjamin Krill 	}
510*cf68787bSBenjamin Krill 
511*cf68787bSBenjamin Krill 	while (isxdigit(*cp) &&
512*cf68787bSBenjamin Krill 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
513*cf68787bSBenjamin Krill 		result = result * base + value;
514*cf68787bSBenjamin Krill 		cp++;
515*cf68787bSBenjamin Krill 	}
516*cf68787bSBenjamin Krill 
517*cf68787bSBenjamin Krill 	if (endp)
518*cf68787bSBenjamin Krill 		*endp = cp;
519*cf68787bSBenjamin Krill 
520*cf68787bSBenjamin Krill 	return result;
521*cf68787bSBenjamin Krill }
522*cf68787bSBenjamin Krill 
523*cf68787bSBenjamin Krill unsigned long prom_memparse(const char *ptr, const char **retptr)
524*cf68787bSBenjamin Krill {
525*cf68787bSBenjamin Krill 	unsigned long ret = prom_strtoul(ptr, retptr);
526*cf68787bSBenjamin Krill 	int shift = 0;
527*cf68787bSBenjamin Krill 
528*cf68787bSBenjamin Krill 	/*
529*cf68787bSBenjamin Krill 	 * We can't use a switch here because GCC *may* generate a
530*cf68787bSBenjamin Krill 	 * jump table which won't work, because we're not running at
531*cf68787bSBenjamin Krill 	 * the address we're linked at.
532*cf68787bSBenjamin Krill 	 */
533*cf68787bSBenjamin Krill 	if ('G' == **retptr || 'g' == **retptr)
534*cf68787bSBenjamin Krill 		shift = 30;
535*cf68787bSBenjamin Krill 
536*cf68787bSBenjamin Krill 	if ('M' == **retptr || 'm' == **retptr)
537*cf68787bSBenjamin Krill 		shift = 20;
538*cf68787bSBenjamin Krill 
539*cf68787bSBenjamin Krill 	if ('K' == **retptr || 'k' == **retptr)
540*cf68787bSBenjamin Krill 		shift = 10;
541*cf68787bSBenjamin Krill 
542*cf68787bSBenjamin Krill 	if (shift) {
543*cf68787bSBenjamin Krill 		ret <<= shift;
544*cf68787bSBenjamin Krill 		(*retptr)++;
545*cf68787bSBenjamin Krill 	}
546*cf68787bSBenjamin Krill 
547*cf68787bSBenjamin Krill 	return ret;
548*cf68787bSBenjamin Krill }
549*cf68787bSBenjamin Krill 
5509b6b563cSPaul Mackerras /*
5519b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
5529b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
5539b6b563cSPaul Mackerras  */
5549b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
5559b6b563cSPaul Mackerras {
5569b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
557cc5d0189SBenjamin Herrenschmidt 	const char *opt;
558*cf68787bSBenjamin Krill 
559cc5d0189SBenjamin Herrenschmidt 	char *p;
5609b6b563cSPaul Mackerras 	int l = 0;
5619b6b563cSPaul Mackerras 
5629b6b563cSPaul Mackerras 	RELOC(prom_cmd_line[0]) = 0;
5639b6b563cSPaul Mackerras 	p = RELOC(prom_cmd_line);
5649b6b563cSPaul Mackerras 	if ((long)_prom->chosen > 0)
5659b6b563cSPaul Mackerras 		l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
5669b6b563cSPaul Mackerras #ifdef CONFIG_CMDLINE
5670e4aa9c2SAmos Waterland 	if (l <= 0 || p[0] == '\0') /* dbl check */
5689b6b563cSPaul Mackerras 		strlcpy(RELOC(prom_cmd_line),
5699b6b563cSPaul Mackerras 			RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
5709b6b563cSPaul Mackerras #endif /* CONFIG_CMDLINE */
5719b6b563cSPaul Mackerras 	prom_printf("command line: %s\n", RELOC(prom_cmd_line));
5729b6b563cSPaul Mackerras 
5739b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
5749b6b563cSPaul Mackerras 	opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
5759b6b563cSPaul Mackerras 	if (opt) {
5769b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
5779b6b563cSPaul Mackerras 		opt += 6;
5789b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
5799b6b563cSPaul Mackerras 			opt++;
5809b6b563cSPaul Mackerras 		if (!strncmp(opt, RELOC("off"), 3))
581165785e5SJeremy Kerr 			RELOC(prom_iommu_off) = 1;
5829b6b563cSPaul Mackerras 		else if (!strncmp(opt, RELOC("force"), 5))
583165785e5SJeremy Kerr 			RELOC(prom_iommu_force_on) = 1;
5849b6b563cSPaul Mackerras 	}
5859b6b563cSPaul Mackerras #endif
586*cf68787bSBenjamin Krill 	opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
587*cf68787bSBenjamin Krill 	if (opt) {
588*cf68787bSBenjamin Krill 		opt += 4;
589*cf68787bSBenjamin Krill 		RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
590*cf68787bSBenjamin Krill #ifdef CONFIG_PPC64
591*cf68787bSBenjamin Krill 		/* Align to 16 MB == size of ppc64 large page */
592*cf68787bSBenjamin Krill 		RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
593*cf68787bSBenjamin Krill #endif
594*cf68787bSBenjamin Krill 	}
5959b6b563cSPaul Mackerras }
5969b6b563cSPaul Mackerras 
5979b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
5989b6b563cSPaul Mackerras /*
599f709bfacSPaul Mackerras  * There are two methods for telling firmware what our capabilities are.
600f709bfacSPaul Mackerras  * Newer machines have an "ibm,client-architecture-support" method on the
601f709bfacSPaul Mackerras  * root node.  For older machines, we have to call the "process-elf-header"
602f709bfacSPaul Mackerras  * method in the /packages/elf-loader node, passing it a fake 32-bit
603f709bfacSPaul Mackerras  * ELF header containing a couple of PT_NOTE sections that contain
604f709bfacSPaul Mackerras  * structures that contain various information.
6059b6b563cSPaul Mackerras  */
606f709bfacSPaul Mackerras 
607f709bfacSPaul Mackerras /*
608f709bfacSPaul Mackerras  * New method - extensible architecture description vector.
609f709bfacSPaul Mackerras  *
610f709bfacSPaul Mackerras  * Because the description vector contains a mix of byte and word
611f709bfacSPaul Mackerras  * values, we declare it as an unsigned char array, and use this
612f709bfacSPaul Mackerras  * macro to put word values in.
613f709bfacSPaul Mackerras  */
614f709bfacSPaul Mackerras #define W(x)	((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
615f709bfacSPaul Mackerras 		((x) >> 8) & 0xff, (x) & 0xff
616f709bfacSPaul Mackerras 
617f709bfacSPaul Mackerras /* Option vector bits - generic bits in byte 1 */
618f709bfacSPaul Mackerras #define OV_IGNORE		0x80	/* ignore this vector */
619f709bfacSPaul Mackerras #define OV_CESSATION_POLICY	0x40	/* halt if unsupported option present*/
620f709bfacSPaul Mackerras 
621f709bfacSPaul Mackerras /* Option vector 1: processor architectures supported */
622f709bfacSPaul Mackerras #define OV1_PPC_2_00		0x80	/* set if we support PowerPC 2.00 */
623f709bfacSPaul Mackerras #define OV1_PPC_2_01		0x40	/* set if we support PowerPC 2.01 */
624f709bfacSPaul Mackerras #define OV1_PPC_2_02		0x20	/* set if we support PowerPC 2.02 */
625f709bfacSPaul Mackerras #define OV1_PPC_2_03		0x10	/* set if we support PowerPC 2.03 */
626f709bfacSPaul Mackerras #define OV1_PPC_2_04		0x08	/* set if we support PowerPC 2.04 */
627f709bfacSPaul Mackerras #define OV1_PPC_2_05		0x04	/* set if we support PowerPC 2.05 */
6280cb99013SJoel Schopp #define OV1_PPC_2_06		0x02	/* set if we support PowerPC 2.06 */
629f709bfacSPaul Mackerras 
630f709bfacSPaul Mackerras /* Option vector 2: Open Firmware options supported */
631f709bfacSPaul Mackerras #define OV2_REAL_MODE		0x20	/* set if we want OF in real mode */
632f709bfacSPaul Mackerras 
633f709bfacSPaul Mackerras /* Option vector 3: processor options supported */
634f709bfacSPaul Mackerras #define OV3_FP			0x80	/* floating point */
635f709bfacSPaul Mackerras #define OV3_VMX			0x40	/* VMX/Altivec */
636974a76f5SPaul Mackerras #define OV3_DFP			0x20	/* decimal FP */
637f709bfacSPaul Mackerras 
638f709bfacSPaul Mackerras /* Option vector 5: PAPR/OF options supported */
639f709bfacSPaul Mackerras #define OV5_LPAR		0x80	/* logical partitioning supported */
640f709bfacSPaul Mackerras #define OV5_SPLPAR		0x40	/* shared-processor LPAR supported */
641f709bfacSPaul Mackerras /* ibm,dynamic-reconfiguration-memory property supported */
642f709bfacSPaul Mackerras #define OV5_DRCONF_MEMORY	0x20
643f709bfacSPaul Mackerras #define OV5_LARGE_PAGES		0x10	/* large pages supported */
644d8c391a5SJake Moilanen #define OV5_DONATE_DEDICATE_CPU 0x02	/* donate dedicated CPU support */
645014dad90SMichael Ellerman /* PCIe/MSI support.  Without MSI full PCIe is not supported */
646014dad90SMichael Ellerman #ifdef CONFIG_PCI_MSI
647014dad90SMichael Ellerman #define OV5_MSI			0x01	/* PCIe/MSI support */
648014dad90SMichael Ellerman #else
649014dad90SMichael Ellerman #define OV5_MSI			0x00
650014dad90SMichael Ellerman #endif /* CONFIG_PCI_MSI */
6518391e42aSNathan Fontenot #ifdef CONFIG_PPC_SMLPAR
6528391e42aSNathan Fontenot #define OV5_CMO			0x80	/* Cooperative Memory Overcommitment */
6538391e42aSNathan Fontenot #else
6548391e42aSNathan Fontenot #define OV5_CMO			0x00
6558391e42aSNathan Fontenot #endif
656f709bfacSPaul Mackerras 
657f709bfacSPaul Mackerras /*
658f709bfacSPaul Mackerras  * The architecture vector has an array of PVR mask/value pairs,
659f709bfacSPaul Mackerras  * followed by # option vectors - 1, followed by the option vectors.
660f709bfacSPaul Mackerras  */
661f709bfacSPaul Mackerras static unsigned char ibm_architecture_vec[] = {
662f709bfacSPaul Mackerras 	W(0xfffe0000), W(0x003a0000),	/* POWER5/POWER5+ */
66303054d51SAnton Blanchard 	W(0xffff0000), W(0x003e0000),	/* POWER6 */
664e952e6c4SMichael Neuling 	W(0xffff0000), W(0x003f0000),	/* POWER7 */
6650cb99013SJoel Schopp 	W(0xffffffff), W(0x0f000003),	/* all 2.06-compliant */
6660efbc18aSPaul Mackerras 	W(0xffffffff), W(0x0f000002),	/* all 2.05-compliant */
667f709bfacSPaul Mackerras 	W(0xfffffffe), W(0x0f000001),	/* all 2.04-compliant and earlier */
668f709bfacSPaul Mackerras 	5 - 1,				/* 5 option vectors */
669f709bfacSPaul Mackerras 
670f709bfacSPaul Mackerras 	/* option vector 1: processor architectures supported */
67111e9ed43SWill Schmidt 	3 - 2,				/* length */
672f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
673f709bfacSPaul Mackerras 	OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
6740cb99013SJoel Schopp 	OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
675f709bfacSPaul Mackerras 
676f709bfacSPaul Mackerras 	/* option vector 2: Open Firmware options supported */
67711e9ed43SWill Schmidt 	34 - 2,				/* length */
678f709bfacSPaul Mackerras 	OV2_REAL_MODE,
679f709bfacSPaul Mackerras 	0, 0,
680f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_base */
681f709bfacSPaul Mackerras 	W(0xffffffff),			/* real_size */
682f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_base */
683f709bfacSPaul Mackerras 	W(0xffffffff),			/* virt_size */
684f709bfacSPaul Mackerras 	W(0xffffffff),			/* load_base */
685856cc2f0SAnton Blanchard 	W(64),				/* 64MB min RMA */
686f709bfacSPaul Mackerras 	W(0xffffffff),			/* full client load */
687f709bfacSPaul Mackerras 	0,				/* min RMA percentage of total RAM */
688f709bfacSPaul Mackerras 	48,				/* max log_2(hash table size) */
689f709bfacSPaul Mackerras 
690f709bfacSPaul Mackerras 	/* option vector 3: processor options supported */
69111e9ed43SWill Schmidt 	3 - 2,				/* length */
692f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
693974a76f5SPaul Mackerras 	OV3_FP | OV3_VMX | OV3_DFP,
694f709bfacSPaul Mackerras 
695f709bfacSPaul Mackerras 	/* option vector 4: IBM PAPR implementation */
69611e9ed43SWill Schmidt 	2 - 2,				/* length */
697f709bfacSPaul Mackerras 	0,				/* don't halt */
698f709bfacSPaul Mackerras 
699f709bfacSPaul Mackerras 	/* option vector 5: PAPR/OF options */
7008391e42aSNathan Fontenot 	5 - 2,				/* length */
701f709bfacSPaul Mackerras 	0,				/* don't ignore, don't halt */
702d8c391a5SJake Moilanen 	OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
703d8c391a5SJake Moilanen 	OV5_DONATE_DEDICATE_CPU | OV5_MSI,
7048391e42aSNathan Fontenot 	0,
7058391e42aSNathan Fontenot 	OV5_CMO,
706f709bfacSPaul Mackerras };
707f709bfacSPaul Mackerras 
708f709bfacSPaul Mackerras /* Old method - ELF header with PT_NOTE sections */
7099b6b563cSPaul Mackerras static struct fake_elf {
7109b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
7119b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
7129b6b563cSPaul Mackerras 	struct chrpnote {
7139b6b563cSPaul Mackerras 		u32	namesz;
7149b6b563cSPaul Mackerras 		u32	descsz;
7159b6b563cSPaul Mackerras 		u32	type;
7169b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
7179b6b563cSPaul Mackerras 		struct chrpdesc {
7189b6b563cSPaul Mackerras 			u32	real_mode;
7199b6b563cSPaul Mackerras 			u32	real_base;
7209b6b563cSPaul Mackerras 			u32	real_size;
7219b6b563cSPaul Mackerras 			u32	virt_base;
7229b6b563cSPaul Mackerras 			u32	virt_size;
7239b6b563cSPaul Mackerras 			u32	load_base;
7249b6b563cSPaul Mackerras 		} chrpdesc;
7259b6b563cSPaul Mackerras 	} chrpnote;
7269b6b563cSPaul Mackerras 	struct rpanote {
7279b6b563cSPaul Mackerras 		u32	namesz;
7289b6b563cSPaul Mackerras 		u32	descsz;
7299b6b563cSPaul Mackerras 		u32	type;
7309b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
7319b6b563cSPaul Mackerras 		struct rpadesc {
7329b6b563cSPaul Mackerras 			u32	lpar_affinity;
7339b6b563cSPaul Mackerras 			u32	min_rmo_size;
7349b6b563cSPaul Mackerras 			u32	min_rmo_percent;
7359b6b563cSPaul Mackerras 			u32	max_pft_size;
7369b6b563cSPaul Mackerras 			u32	splpar;
7379b6b563cSPaul Mackerras 			u32	min_load;
7389b6b563cSPaul Mackerras 			u32	new_mem_def;
7399b6b563cSPaul Mackerras 			u32	ignore_me;
7409b6b563cSPaul Mackerras 		} rpadesc;
7419b6b563cSPaul Mackerras 	} rpanote;
7425663a123SPaul Mackerras } fake_elf = {
7439b6b563cSPaul Mackerras 	.elfhdr = {
7449b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
7459b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
7469b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
7479b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
7489b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
7499b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
7509b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
7519b6b563cSPaul Mackerras 		.e_phnum = 2
7529b6b563cSPaul Mackerras 	},
7539b6b563cSPaul Mackerras 	.phdr = {
7549b6b563cSPaul Mackerras 		[0] = {
7559b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
7569b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
7579b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
7589b6b563cSPaul Mackerras 		}, [1] = {
7599b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
7609b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
7619b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
7629b6b563cSPaul Mackerras 		}
7639b6b563cSPaul Mackerras 	},
7649b6b563cSPaul Mackerras 	.chrpnote = {
7659b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
7669b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
7679b6b563cSPaul Mackerras 		.type = 0x1275,
7689b6b563cSPaul Mackerras 		.name = "PowerPC",
7699b6b563cSPaul Mackerras 		.chrpdesc = {
7709b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
7719b6b563cSPaul Mackerras 			.real_base = ~0U,
7729b6b563cSPaul Mackerras 			.real_size = ~0U,
7739b6b563cSPaul Mackerras 			.virt_base = ~0U,
7749b6b563cSPaul Mackerras 			.virt_size = ~0U,
7759b6b563cSPaul Mackerras 			.load_base = ~0U
7769b6b563cSPaul Mackerras 		},
7779b6b563cSPaul Mackerras 	},
7789b6b563cSPaul Mackerras 	.rpanote = {
7799b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
7809b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
7819b6b563cSPaul Mackerras 		.type = 0x12759999,
7829b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
7839b6b563cSPaul Mackerras 		.rpadesc = {
7845663a123SPaul Mackerras 			.lpar_affinity = 0,
7855663a123SPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
7869b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
7875663a123SPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
7889b6b563cSPaul Mackerras 			.splpar = 1,
7899b6b563cSPaul Mackerras 			.min_load = ~0U,
7905663a123SPaul Mackerras 			.new_mem_def = 0
7919b6b563cSPaul Mackerras 		}
7929b6b563cSPaul Mackerras 	}
7939b6b563cSPaul Mackerras };
7949b6b563cSPaul Mackerras 
7959b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
7969b6b563cSPaul Mackerras {
797f709bfacSPaul Mackerras 	ihandle elfloader, root;
798f709bfacSPaul Mackerras 	prom_arg_t ret;
7999b6b563cSPaul Mackerras 
800f709bfacSPaul Mackerras 	root = call_prom("open", 1, 1, ADDR("/"));
801f709bfacSPaul Mackerras 	if (root != 0) {
802f709bfacSPaul Mackerras 		/* try calling the ibm,client-architecture-support method */
8034da727aeSAnton Blanchard 		prom_printf("Calling ibm,client-architecture...");
804f709bfacSPaul Mackerras 		if (call_prom_ret("call-method", 3, 2, &ret,
805f709bfacSPaul Mackerras 				  ADDR("ibm,client-architecture-support"),
80633b74977SBenjamin Herrenschmidt 				  root,
807f709bfacSPaul Mackerras 				  ADDR(ibm_architecture_vec)) == 0) {
808f709bfacSPaul Mackerras 			/* the call exists... */
809f709bfacSPaul Mackerras 			if (ret)
8104da727aeSAnton Blanchard 				prom_printf("\nWARNING: ibm,client-architecture"
811f709bfacSPaul Mackerras 					    "-support call FAILED!\n");
812f709bfacSPaul Mackerras 			call_prom("close", 1, 0, root);
8134da727aeSAnton Blanchard 			prom_printf(" done\n");
814f709bfacSPaul Mackerras 			return;
815f709bfacSPaul Mackerras 		}
816f709bfacSPaul Mackerras 		call_prom("close", 1, 0, root);
817f709bfacSPaul Mackerras 	}
818f709bfacSPaul Mackerras 
819f709bfacSPaul Mackerras 	/* no ibm,client-architecture-support call, try the old way */
8209b6b563cSPaul Mackerras 	elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
8219b6b563cSPaul Mackerras 	if (elfloader == 0) {
8229b6b563cSPaul Mackerras 		prom_printf("couldn't open /packages/elf-loader\n");
8239b6b563cSPaul Mackerras 		return;
8249b6b563cSPaul Mackerras 	}
8259b6b563cSPaul Mackerras 	call_prom("call-method", 3, 1, ADDR("process-elf-header"),
8269b6b563cSPaul Mackerras 			elfloader, ADDR(&fake_elf));
8279b6b563cSPaul Mackerras 	call_prom("close", 1, 0, elfloader);
8289b6b563cSPaul Mackerras }
8299b6b563cSPaul Mackerras #endif
8309b6b563cSPaul Mackerras 
8319b6b563cSPaul Mackerras /*
8329b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
8339b6b563cSPaul Mackerras  *
8349b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
8359b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
8369b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
8379b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
8389b6b563cSPaul Mackerras  *  never happen.
8399b6b563cSPaul Mackerras  *
8409b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
8419b6b563cSPaul Mackerras  *  TCEs overlap
8429b6b563cSPaul Mackerras  *
8439b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
8449b6b563cSPaul Mackerras  *
8459b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
8469b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
8479b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
8489b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
8499b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
8509b6b563cSPaul Mackerras  *
8519b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
8529b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
8539b6b563cSPaul Mackerras  *  reserved.
8549b6b563cSPaul Mackerras  */
8559b6b563cSPaul Mackerras 
8569b6b563cSPaul Mackerras 
8579b6b563cSPaul Mackerras /*
8589b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
8599b6b563cSPaul Mackerras  *
8609b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
8619b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
8629b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
8639b6b563cSPaul Mackerras  */
8649b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
8659b6b563cSPaul Mackerras {
866c4988820SPaul Mackerras 	unsigned long base = RELOC(alloc_bottom);
8679b6b563cSPaul Mackerras 	unsigned long addr = 0;
8689b6b563cSPaul Mackerras 
869c4988820SPaul Mackerras 	if (align)
870c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
8719b6b563cSPaul Mackerras 	prom_debug("alloc_up(%x, %x)\n", size, align);
8729b6b563cSPaul Mackerras 	if (RELOC(ram_top) == 0)
8739b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
8749b6b563cSPaul Mackerras 
8759b6b563cSPaul Mackerras 	if (align)
8769b6b563cSPaul Mackerras 		base = _ALIGN_UP(RELOC(alloc_bottom), align);
8779b6b563cSPaul Mackerras 	else
8789b6b563cSPaul Mackerras 		base = RELOC(alloc_bottom);
8799b6b563cSPaul Mackerras 
8809b6b563cSPaul Mackerras 	for(; (base + size) <= RELOC(alloc_top);
8819b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
8829b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
8839b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
884c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
8859b6b563cSPaul Mackerras 			break;
8869b6b563cSPaul Mackerras 		addr = 0;
8879b6b563cSPaul Mackerras 		if (align == 0)
8889b6b563cSPaul Mackerras 			break;
8899b6b563cSPaul Mackerras 	}
8909b6b563cSPaul Mackerras 	if (addr == 0)
8919b6b563cSPaul Mackerras 		return 0;
8929b6b563cSPaul Mackerras 	RELOC(alloc_bottom) = addr;
8939b6b563cSPaul Mackerras 
8949b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
8959b6b563cSPaul Mackerras 	prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
8969b6b563cSPaul Mackerras 	prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
8979b6b563cSPaul Mackerras 	prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
8989b6b563cSPaul Mackerras 	prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
8999b6b563cSPaul Mackerras 	prom_debug("  ram_top      : %x\n", RELOC(ram_top));
9009b6b563cSPaul Mackerras 
9019b6b563cSPaul Mackerras 	return addr;
9029b6b563cSPaul Mackerras }
9039b6b563cSPaul Mackerras 
9049b6b563cSPaul Mackerras /*
9059b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
9069b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
9079b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
9089b6b563cSPaul Mackerras  */
9099b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
9109b6b563cSPaul Mackerras 				       int highmem)
9119b6b563cSPaul Mackerras {
9129b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
9139b6b563cSPaul Mackerras 
9149b6b563cSPaul Mackerras 	prom_debug("alloc_down(%x, %x, %s)\n", size, align,
9159b6b563cSPaul Mackerras 		   highmem ? RELOC("(high)") : RELOC("(low)"));
9169b6b563cSPaul Mackerras 	if (RELOC(ram_top) == 0)
9179b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
9189b6b563cSPaul Mackerras 
9199b6b563cSPaul Mackerras 	if (highmem) {
9209b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
9219b6b563cSPaul Mackerras 		addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
9229b6b563cSPaul Mackerras 		if (addr <= RELOC(alloc_bottom))
9239b6b563cSPaul Mackerras 			return 0;
9249b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
9259b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
9269b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
9279b6b563cSPaul Mackerras 		 */
9289b6b563cSPaul Mackerras 		if (addr < RELOC(rmo_top)) {
9299b6b563cSPaul Mackerras 			/* Good, we are first */
9309b6b563cSPaul Mackerras 			if (RELOC(alloc_top) == RELOC(rmo_top))
9319b6b563cSPaul Mackerras 				RELOC(alloc_top) = RELOC(rmo_top) = addr;
9329b6b563cSPaul Mackerras 			else
9339b6b563cSPaul Mackerras 				return 0;
9349b6b563cSPaul Mackerras 		}
9359b6b563cSPaul Mackerras 		RELOC(alloc_top_high) = addr;
9369b6b563cSPaul Mackerras 		goto bail;
9379b6b563cSPaul Mackerras 	}
9389b6b563cSPaul Mackerras 
9399b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
9409b6b563cSPaul Mackerras 	for (; base > RELOC(alloc_bottom);
9419b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
9429b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
9439b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
944c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
9459b6b563cSPaul Mackerras 			break;
9469b6b563cSPaul Mackerras 		addr = 0;
9479b6b563cSPaul Mackerras 	}
9489b6b563cSPaul Mackerras 	if (addr == 0)
9499b6b563cSPaul Mackerras 		return 0;
9509b6b563cSPaul Mackerras 	RELOC(alloc_top) = addr;
9519b6b563cSPaul Mackerras 
9529b6b563cSPaul Mackerras  bail:
9539b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
9549b6b563cSPaul Mackerras 	prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
9559b6b563cSPaul Mackerras 	prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
9569b6b563cSPaul Mackerras 	prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
9579b6b563cSPaul Mackerras 	prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
9589b6b563cSPaul Mackerras 	prom_debug("  ram_top      : %x\n", RELOC(ram_top));
9599b6b563cSPaul Mackerras 
9609b6b563cSPaul Mackerras 	return addr;
9619b6b563cSPaul Mackerras }
9629b6b563cSPaul Mackerras 
9639b6b563cSPaul Mackerras /*
9649b6b563cSPaul Mackerras  * Parse a "reg" cell
9659b6b563cSPaul Mackerras  */
9669b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
9679b6b563cSPaul Mackerras {
9689b6b563cSPaul Mackerras 	cell_t *p = *cellp;
9699b6b563cSPaul Mackerras 	unsigned long r = 0;
9709b6b563cSPaul Mackerras 
9719b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
9729b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
9739b6b563cSPaul Mackerras 		p++;
9749b6b563cSPaul Mackerras 		s--;
9759b6b563cSPaul Mackerras 	}
9769b6b563cSPaul Mackerras 	r = *p++;
9779b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
97835499c01SPaul Mackerras 	if (s > 1) {
9799b6b563cSPaul Mackerras 		r <<= 32;
9809b6b563cSPaul Mackerras 		r |= *(p++);
9819b6b563cSPaul Mackerras 	}
9829b6b563cSPaul Mackerras #endif
9839b6b563cSPaul Mackerras 	*cellp = p;
9849b6b563cSPaul Mackerras 	return r;
9859b6b563cSPaul Mackerras }
9869b6b563cSPaul Mackerras 
9879b6b563cSPaul Mackerras /*
9889b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
9899b6b563cSPaul Mackerras  * we don't need anything smarter at this point
9909b6b563cSPaul Mackerras  *
9919b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
9929b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
9939b6b563cSPaul Mackerras  * them down.
9949b6b563cSPaul Mackerras  */
9950108d3feSMichael Ellerman static void __init reserve_mem(u64 base, u64 size)
9969b6b563cSPaul Mackerras {
997cbbcf340SKumar Gala 	u64 top = base + size;
9989b6b563cSPaul Mackerras 	unsigned long cnt = RELOC(mem_reserve_cnt);
9999b6b563cSPaul Mackerras 
10009b6b563cSPaul Mackerras 	if (size == 0)
10019b6b563cSPaul Mackerras 		return;
10029b6b563cSPaul Mackerras 
10039b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
10049b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
10059b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
10069b6b563cSPaul Mackerras 	 */
10079b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
10089b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
10099b6b563cSPaul Mackerras 	size = top - base;
10109b6b563cSPaul Mackerras 
10119b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
10129b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
10139b6b563cSPaul Mackerras 	RELOC(mem_reserve_map)[cnt].base = base;
10149b6b563cSPaul Mackerras 	RELOC(mem_reserve_map)[cnt].size = size;
10159b6b563cSPaul Mackerras 	RELOC(mem_reserve_cnt) = cnt + 1;
10169b6b563cSPaul Mackerras }
10179b6b563cSPaul Mackerras 
10189b6b563cSPaul Mackerras /*
1019b3c2ffd5SAdrian Bunk  * Initialize memory allocation mechanism, parse "memory" nodes and
10209b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
10219b6b563cSPaul Mackerras  */
10229b6b563cSPaul Mackerras static void __init prom_init_mem(void)
10239b6b563cSPaul Mackerras {
10249b6b563cSPaul Mackerras 	phandle node;
10259b6b563cSPaul Mackerras 	char *path, type[64];
10269b6b563cSPaul Mackerras 	unsigned int plen;
10279b6b563cSPaul Mackerras 	cell_t *p, *endp;
10289b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
10299b6b563cSPaul Mackerras 	u32 rac, rsc;
10309b6b563cSPaul Mackerras 
10319b6b563cSPaul Mackerras 	/*
10329b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
10339b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
10349b6b563cSPaul Mackerras 	 * 2) top of memory
10359b6b563cSPaul Mackerras 	 */
10369b6b563cSPaul Mackerras 	rac = 2;
10379b6b563cSPaul Mackerras 	prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
10389b6b563cSPaul Mackerras 	rsc = 1;
10399b6b563cSPaul Mackerras 	prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
10409b6b563cSPaul Mackerras 	prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
10419b6b563cSPaul Mackerras 	prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
10429b6b563cSPaul Mackerras 
10439b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
10449b6b563cSPaul Mackerras 	path = RELOC(prom_scratch);
10459b6b563cSPaul Mackerras 
10469b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
10479b6b563cSPaul Mackerras 		type[0] = 0;
10489b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
10499b6b563cSPaul Mackerras 
1050c4988820SPaul Mackerras 		if (type[0] == 0) {
1051c4988820SPaul Mackerras 			/*
1052c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
1053c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
1054c4988820SPaul Mackerras 			 */
1055c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
1056c4988820SPaul Mackerras 		}
10579b6b563cSPaul Mackerras 		if (strcmp(type, RELOC("memory")))
10589b6b563cSPaul Mackerras 			continue;
10599b6b563cSPaul Mackerras 
10609b6b563cSPaul Mackerras 		plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
10619b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
10629b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
10639b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
10649b6b563cSPaul Mackerras 		}
10659b6b563cSPaul Mackerras 		p = RELOC(regbuf);
10669b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
10679b6b563cSPaul Mackerras 
10689b6b563cSPaul Mackerras #ifdef DEBUG_PROM
10699b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
10709b6b563cSPaul Mackerras 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
10719b6b563cSPaul Mackerras 		prom_debug("  node %s :\n", path);
10729b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
10739b6b563cSPaul Mackerras 
10749b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
10759b6b563cSPaul Mackerras 			unsigned long base, size;
10769b6b563cSPaul Mackerras 
10779b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
10789b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
10799b6b563cSPaul Mackerras 
10809b6b563cSPaul Mackerras 			if (size == 0)
10819b6b563cSPaul Mackerras 				continue;
10829b6b563cSPaul Mackerras 			prom_debug("    %x %x\n", base, size);
1083ab1b55e2SBenjamin Herrenschmidt 			if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
10849b6b563cSPaul Mackerras 				RELOC(rmo_top) = size;
10859b6b563cSPaul Mackerras 			if ((base + size) > RELOC(ram_top))
10869b6b563cSPaul Mackerras 				RELOC(ram_top) = base + size;
10879b6b563cSPaul Mackerras 		}
10889b6b563cSPaul Mackerras 	}
10899b6b563cSPaul Mackerras 
10909b6b563cSPaul Mackerras 	RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
10919b6b563cSPaul Mackerras 
10929b6b563cSPaul Mackerras 	/* Check if we have an initrd after the kernel, if we do move our bottom
10939b6b563cSPaul Mackerras 	 * point to after it
10949b6b563cSPaul Mackerras 	 */
10959b6b563cSPaul Mackerras 	if (RELOC(prom_initrd_start)) {
10969b6b563cSPaul Mackerras 		if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
10979b6b563cSPaul Mackerras 			RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
10989b6b563cSPaul Mackerras 	}
10999b6b563cSPaul Mackerras 
11009b6b563cSPaul Mackerras 	/*
1101*cf68787bSBenjamin Krill 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1102*cf68787bSBenjamin Krill 	 * alloc_top_high. This must be the real top of RAM so we can put
1103*cf68787bSBenjamin Krill 	 * TCE's up there.
1104*cf68787bSBenjamin Krill 	 */
1105*cf68787bSBenjamin Krill 
1106*cf68787bSBenjamin Krill 	RELOC(alloc_top_high) = RELOC(ram_top);
1107*cf68787bSBenjamin Krill 
1108*cf68787bSBenjamin Krill 	if (RELOC(prom_memory_limit)) {
1109*cf68787bSBenjamin Krill 		if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
1110*cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
1111*cf68787bSBenjamin Krill 				RELOC(prom_memory_limit));
1112*cf68787bSBenjamin Krill 			RELOC(prom_memory_limit) = 0;
1113*cf68787bSBenjamin Krill 		} else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
1114*cf68787bSBenjamin Krill 			prom_printf("Ignoring mem=%x >= ram_top.\n",
1115*cf68787bSBenjamin Krill 				RELOC(prom_memory_limit));
1116*cf68787bSBenjamin Krill 			RELOC(prom_memory_limit) = 0;
1117*cf68787bSBenjamin Krill 		} else {
1118*cf68787bSBenjamin Krill 			RELOC(ram_top) = RELOC(prom_memory_limit);
1119*cf68787bSBenjamin Krill 			RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
1120*cf68787bSBenjamin Krill 		}
1121*cf68787bSBenjamin Krill 	}
1122*cf68787bSBenjamin Krill 
1123*cf68787bSBenjamin Krill 	/*
11249b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
11259b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
11269b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
11279b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
11289b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
11299b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
11309b6b563cSPaul Mackerras 	 */
11319b6b563cSPaul Mackerras 	if (!RELOC(rmo_top))
11329b6b563cSPaul Mackerras 		RELOC(rmo_top) = RELOC(ram_top);
11339b6b563cSPaul Mackerras 	RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
11349b6b563cSPaul Mackerras 	RELOC(alloc_top) = RELOC(rmo_top);
11352babf5c2SMichael Ellerman 	RELOC(alloc_top_high) = RELOC(ram_top);
11369b6b563cSPaul Mackerras 
11379b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
1138*cf68787bSBenjamin Krill 	prom_printf("  memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
11399b6b563cSPaul Mackerras 	prom_printf("  alloc_bottom : %x\n", RELOC(alloc_bottom));
11409b6b563cSPaul Mackerras 	prom_printf("  alloc_top    : %x\n", RELOC(alloc_top));
11419b6b563cSPaul Mackerras 	prom_printf("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
11429b6b563cSPaul Mackerras 	prom_printf("  rmo_top      : %x\n", RELOC(rmo_top));
11439b6b563cSPaul Mackerras 	prom_printf("  ram_top      : %x\n", RELOC(ram_top));
11449b6b563cSPaul Mackerras }
11459b6b563cSPaul Mackerras 
11469b6b563cSPaul Mackerras 
11479b6b563cSPaul Mackerras /*
11489b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
11499b6b563cSPaul Mackerras  */
11509b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
11519b6b563cSPaul Mackerras {
11529b6b563cSPaul Mackerras 	phandle rtas_node;
11539b6b563cSPaul Mackerras 	ihandle rtas_inst;
11549b6b563cSPaul Mackerras 	u32 base, entry = 0;
11559b6b563cSPaul Mackerras 	u32 size = 0;
11569b6b563cSPaul Mackerras 
11579b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
11589b6b563cSPaul Mackerras 
11599b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
11609b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
11619b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
11629b6b563cSPaul Mackerras 		return;
11639b6b563cSPaul Mackerras 
11649b6b563cSPaul Mackerras 	prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
11659b6b563cSPaul Mackerras 	if (size == 0)
11669b6b563cSPaul Mackerras 		return;
11679b6b563cSPaul Mackerras 
11689b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
11699b6b563cSPaul Mackerras 	if (base == 0) {
11709b6b563cSPaul Mackerras 		prom_printf("RTAS allocation failed !\n");
11719b6b563cSPaul Mackerras 		return;
11729b6b563cSPaul Mackerras 	}
11739b6b563cSPaul Mackerras 
11749b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
11759b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
1176a23414beSPaul Mackerras 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
11779b6b563cSPaul Mackerras 		return;
11789b6b563cSPaul Mackerras 	}
11799b6b563cSPaul Mackerras 
11809b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x...", base);
11819b6b563cSPaul Mackerras 
11829b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
11839b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
1184a23414beSPaul Mackerras 			  rtas_inst, base) != 0
11859b6b563cSPaul Mackerras 	    || entry == 0) {
11869b6b563cSPaul Mackerras 		prom_printf(" failed\n");
11879b6b563cSPaul Mackerras 		return;
11889b6b563cSPaul Mackerras 	}
11899b6b563cSPaul Mackerras 	prom_printf(" done\n");
11909b6b563cSPaul Mackerras 
11919b6b563cSPaul Mackerras 	reserve_mem(base, size);
11929b6b563cSPaul Mackerras 
1193a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1194a23414beSPaul Mackerras 		     &base, sizeof(base));
1195a23414beSPaul Mackerras 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1196a23414beSPaul Mackerras 		     &entry, sizeof(entry));
11979b6b563cSPaul Mackerras 
11989b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
11999b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
12009b6b563cSPaul Mackerras 	prom_debug("rtas size     = 0x%x\n", (long)size);
12019b6b563cSPaul Mackerras 
12029b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
12039b6b563cSPaul Mackerras }
12049b6b563cSPaul Mackerras 
12059b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
12069b6b563cSPaul Mackerras /*
12079b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
12089b6b563cSPaul Mackerras  */
12099b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
12109b6b563cSPaul Mackerras {
12119b6b563cSPaul Mackerras 	phandle node;
12129b6b563cSPaul Mackerras 	ihandle phb_node;
12139b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
12149b6b563cSPaul Mackerras 	char *path = RELOC(prom_scratch);
12159b6b563cSPaul Mackerras 	u64 base, align;
12169b6b563cSPaul Mackerras 	u32 minalign, minsize;
12179b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
12189b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
12199b6b563cSPaul Mackerras 	u64 i;
12209b6b563cSPaul Mackerras 
1221165785e5SJeremy Kerr 	if (RELOC(prom_iommu_off))
12229b6b563cSPaul Mackerras 		return;
12239b6b563cSPaul Mackerras 
12249b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
12259b6b563cSPaul Mackerras 
12269b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
12279b6b563cSPaul Mackerras 	local_alloc_top = RELOC(alloc_top_high);
12289b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
12299b6b563cSPaul Mackerras 
12309b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
12319b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
12329b6b563cSPaul Mackerras 		compatible[0] = 0;
12339b6b563cSPaul Mackerras 		type[0] = 0;
12349b6b563cSPaul Mackerras 		model[0] = 0;
12359b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
12369b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
12379b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
12389b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
12399b6b563cSPaul Mackerras 
12409b6b563cSPaul Mackerras 		if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
12419b6b563cSPaul Mackerras 			continue;
12429b6b563cSPaul Mackerras 
1243e788ff13SLinas Vepstas 		/* Keep the old logic intact to avoid regression. */
12449b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
12459b6b563cSPaul Mackerras 			if ((strstr(compatible, RELOC("python")) == NULL) &&
12469b6b563cSPaul Mackerras 			    (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
12479b6b563cSPaul Mackerras 			    (strstr(compatible, RELOC("Winnipeg")) == NULL))
12489b6b563cSPaul Mackerras 				continue;
12499b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
12509b6b563cSPaul Mackerras 			if ((strstr(model, RELOC("ython")) == NULL) &&
12519b6b563cSPaul Mackerras 			    (strstr(model, RELOC("peedwagon")) == NULL) &&
12529b6b563cSPaul Mackerras 			    (strstr(model, RELOC("innipeg")) == NULL))
12539b6b563cSPaul Mackerras 				continue;
12549b6b563cSPaul Mackerras 		}
12559b6b563cSPaul Mackerras 
12569b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
12579b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
12589b6b563cSPaul Mackerras 			minalign = 0;
12599b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
12609b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
12619b6b563cSPaul Mackerras 			minsize = 4UL << 20;
12629b6b563cSPaul Mackerras 
12639b6b563cSPaul Mackerras 		/*
12649b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
12659b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
12669b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
12679b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
12689b6b563cSPaul Mackerras 		 *
12699b6b563cSPaul Mackerras 		 * On POWER4, firmware sets the TCE region by assuming
12709b6b563cSPaul Mackerras 		 * each TCE table is 8MB. Using this memory for anything
12719b6b563cSPaul Mackerras 		 * else will impact performance, so we always allocate 8MB.
12729b6b563cSPaul Mackerras 		 * Anton
12739b6b563cSPaul Mackerras 		 */
12749b6b563cSPaul Mackerras 		if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
12759b6b563cSPaul Mackerras 			minsize = 8UL << 20;
12769b6b563cSPaul Mackerras 		else
12779b6b563cSPaul Mackerras 			minsize = 4UL << 20;
12789b6b563cSPaul Mackerras 
12799b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
12809b6b563cSPaul Mackerras 		align = max(minalign, minsize);
12819b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
12829b6b563cSPaul Mackerras 		if (base == 0)
12839b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
12849b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
12859b6b563cSPaul Mackerras 			local_alloc_bottom = base;
12869b6b563cSPaul Mackerras 
12879b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
1288aca71ef8SLi Zefan 		memset(path, 0, PROM_SCRATCH_SIZE);
12899b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
12909b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
12919b6b563cSPaul Mackerras 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
12929b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
12939b6b563cSPaul Mackerras 		}
12949b6b563cSPaul Mackerras 
1295a23414beSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
1296a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1297a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1298a23414beSPaul Mackerras 
12999b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
13009b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
13019b6b563cSPaul Mackerras 		prom_debug("\tbase = 0x%x\n", base);
13029b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
13039b6b563cSPaul Mackerras 
13049b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
13059b6b563cSPaul Mackerras 		 * over the allocated size.
13069b6b563cSPaul Mackerras 		 */
13072b931fb6SIngo Molnar 		tce_entryp = (u64 *)base;
13089b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
13099b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
13109b6b563cSPaul Mackerras 			tce_entry |= 0x3;
13119b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
13129b6b563cSPaul Mackerras 		}
13139b6b563cSPaul Mackerras 
13149b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
13159b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
13169b6b563cSPaul Mackerras 		if (phb_node == 0)
13179b6b563cSPaul Mackerras 			prom_printf("... failed\n");
13189b6b563cSPaul Mackerras 		else
13199b6b563cSPaul Mackerras 			prom_printf("... done\n");
13209b6b563cSPaul Mackerras 
13219b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
13229b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
13239b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
13249b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
13259b6b563cSPaul Mackerras 	}
13269b6b563cSPaul Mackerras 
13279b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
13289b6b563cSPaul Mackerras 
13292babf5c2SMichael Ellerman 	/* These are only really needed if there is a memory limit in
13302babf5c2SMichael Ellerman 	 * effect, but we don't know so export them always. */
13312babf5c2SMichael Ellerman 	RELOC(prom_tce_alloc_start) = local_alloc_bottom;
13329b6b563cSPaul Mackerras 	RELOC(prom_tce_alloc_end) = local_alloc_top;
13339b6b563cSPaul Mackerras 
13349b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
13359b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
13369b6b563cSPaul Mackerras }
13379b6b563cSPaul Mackerras #endif
13389b6b563cSPaul Mackerras 
13399b6b563cSPaul Mackerras /*
13409b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
13419b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
13429b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
13439b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
13449b6b563cSPaul Mackerras  *
13459b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
13469b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
13479b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
13489b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
13499b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
13509b6b563cSPaul Mackerras  * of setting those values.
13519b6b563cSPaul Mackerras  *
13529b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
13539b6b563cSPaul Mackerras  * is in its holding pattern code.
13549b6b563cSPaul Mackerras  *
13559b6b563cSPaul Mackerras  * -- Cort
13569b6b563cSPaul Mackerras  */
1357bbd0abdaSPaul Mackerras /*
1358bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
1359bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
1360bbd0abdaSPaul Mackerras  */
1361bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1362bbd0abdaSPaul Mackerras 
13639b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
13649b6b563cSPaul Mackerras {
13659b6b563cSPaul Mackerras 	unsigned long i;
13669b6b563cSPaul Mackerras 	unsigned int reg;
13679b6b563cSPaul Mackerras 	phandle node;
13689b6b563cSPaul Mackerras 	char type[64];
1369bbd0abdaSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
13709b6b563cSPaul Mackerras 	unsigned long *spinloop
1371bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
13729b6b563cSPaul Mackerras 	unsigned long *acknowledge
1373bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1374bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
13759b6b563cSPaul Mackerras 
13769b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
13779b6b563cSPaul Mackerras 	prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
13789b6b563cSPaul Mackerras 	prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
13799b6b563cSPaul Mackerras 	prom_debug("    1) acknowledge    = 0x%x\n",
13809b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
13819b6b563cSPaul Mackerras 	prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
13829b6b563cSPaul Mackerras 	prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
13839b6b563cSPaul Mackerras 
13849b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
13859b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
13869b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
13879b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
13889b6b563cSPaul Mackerras 	 */
13899b6b563cSPaul Mackerras 	*spinloop = 0;
13909b6b563cSPaul Mackerras 
13919b6b563cSPaul Mackerras 	/* look for cpus */
13929b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
13939b6b563cSPaul Mackerras 		type[0] = 0;
13949b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
13959b6b563cSPaul Mackerras 		if (strcmp(type, RELOC("cpu")) != 0)
13969b6b563cSPaul Mackerras 			continue;
13979b6b563cSPaul Mackerras 
13989b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
13999b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
14009b6b563cSPaul Mackerras 			if (strcmp(type, RELOC("okay")) != 0)
14019b6b563cSPaul Mackerras 				continue;
14029b6b563cSPaul Mackerras 
14039b6b563cSPaul Mackerras 		reg = -1;
14049b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
14059b6b563cSPaul Mackerras 
14069b6b563cSPaul Mackerras 		prom_debug("cpu hw idx   = 0x%x\n", reg);
14079b6b563cSPaul Mackerras 
14089b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
14099b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
14109b6b563cSPaul Mackerras 		 * spinloop.
14119b6b563cSPaul Mackerras 		 */
14129b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
14139b6b563cSPaul Mackerras 
14147d2f6075SNathan Lynch 		if (reg != _prom->cpu) {
14159b6b563cSPaul Mackerras 			/* Primary Thread of non-boot cpu */
14167d2f6075SNathan Lynch 			prom_printf("starting cpu hw idx %x... ", reg);
14179b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
14189b6b563cSPaul Mackerras 				  secondary_hold, reg);
14199b6b563cSPaul Mackerras 
14209b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
14219b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
14229b6b563cSPaul Mackerras 				mb();
14239b6b563cSPaul Mackerras 
1424bbd0abdaSPaul Mackerras 			if (*acknowledge == reg)
14259b6b563cSPaul Mackerras 				prom_printf("done\n");
1426bbd0abdaSPaul Mackerras 			else
14279b6b563cSPaul Mackerras 				prom_printf("failed: %x\n", *acknowledge);
14289b6b563cSPaul Mackerras 		}
14299b6b563cSPaul Mackerras #ifdef CONFIG_SMP
14309b6b563cSPaul Mackerras 		else
14317d2f6075SNathan Lynch 			prom_printf("boot cpu hw idx %x\n", reg);
14329b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
14339b6b563cSPaul Mackerras 	}
14349b6b563cSPaul Mackerras 
14359b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
14369b6b563cSPaul Mackerras }
14379b6b563cSPaul Mackerras 
14389b6b563cSPaul Mackerras 
14399b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
14409b6b563cSPaul Mackerras {
14419b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
14429b6b563cSPaul Mackerras 
14439b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
14449b6b563cSPaul Mackerras 	RELOC(prom_entry) = pp;
14459b6b563cSPaul Mackerras 
14469b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
14479b6b563cSPaul Mackerras 	_prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
14489b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(_prom->chosen))
14499b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
14509b6b563cSPaul Mackerras 
14519b6b563cSPaul Mackerras 	/* get device tree root */
14529b6b563cSPaul Mackerras 	_prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
14539b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(_prom->root))
14549b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1455a575b807SPaul Mackerras 
1456a575b807SPaul Mackerras 	_prom->mmumap = 0;
14579b6b563cSPaul Mackerras }
14589b6b563cSPaul Mackerras 
1459a575b807SPaul Mackerras #ifdef CONFIG_PPC32
1460a575b807SPaul Mackerras /*
1461a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
1462a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
1463a23414beSPaul Mackerras  * Also, on the longtrail, we need to work around other bugs.
1464a575b807SPaul Mackerras  */
1465a575b807SPaul Mackerras static void __init prom_find_mmu(void)
1466a575b807SPaul Mackerras {
1467a575b807SPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
1468a575b807SPaul Mackerras 	phandle oprom;
1469a575b807SPaul Mackerras 	char version[64];
1470a575b807SPaul Mackerras 
1471a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1472a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
1473a575b807SPaul Mackerras 		return;
1474a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1475a575b807SPaul Mackerras 		return;
1476a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
1477a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
1478a23414beSPaul Mackerras 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1479a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM;
1480a23414beSPaul Mackerras 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1481a23414beSPaul Mackerras 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1482a23414beSPaul Mackerras 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1483a23414beSPaul Mackerras 	} else
1484a575b807SPaul Mackerras 		return;
1485a23414beSPaul Mackerras 	_prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
1486a575b807SPaul Mackerras 	prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1487a575b807SPaul Mackerras 		     sizeof(_prom->mmumap));
1488a23414beSPaul Mackerras 	if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1489a23414beSPaul Mackerras 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
1490a575b807SPaul Mackerras }
1491a575b807SPaul Mackerras #else
1492a575b807SPaul Mackerras #define prom_find_mmu()
1493a575b807SPaul Mackerras #endif
1494a575b807SPaul Mackerras 
14959b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
14969b6b563cSPaul Mackerras {
14979b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
14989b6b563cSPaul Mackerras 	char *path = RELOC(of_stdout_device);
14999b6b563cSPaul Mackerras 	char type[16];
15009b6b563cSPaul Mackerras 	u32 val;
15019b6b563cSPaul Mackerras 
15029b6b563cSPaul Mackerras 	if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
15039b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
15049b6b563cSPaul Mackerras 
15059b6b563cSPaul Mackerras 	_prom->stdout = val;
15069b6b563cSPaul Mackerras 
15079b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
15089b6b563cSPaul Mackerras 	memset(path, 0, 256);
15099b6b563cSPaul Mackerras 	call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
15109b6b563cSPaul Mackerras 	val = call_prom("instance-to-package", 1, 1, _prom->stdout);
1511a23414beSPaul Mackerras 	prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1512a23414beSPaul Mackerras 		     &val, sizeof(val));
15139b6b563cSPaul Mackerras 	prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
1514a23414beSPaul Mackerras 	prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1515a23414beSPaul Mackerras 		     path, strlen(path) + 1);
15169b6b563cSPaul Mackerras 
15179b6b563cSPaul Mackerras 	/* If it's a display, note it */
15189b6b563cSPaul Mackerras 	memset(type, 0, sizeof(type));
15199b6b563cSPaul Mackerras 	prom_getprop(val, "device_type", type, sizeof(type));
15209b6b563cSPaul Mackerras 	if (strcmp(type, RELOC("display")) == 0)
1521a23414beSPaul Mackerras 		prom_setprop(val, path, "linux,boot-display", NULL, 0);
15229b6b563cSPaul Mackerras }
15239b6b563cSPaul Mackerras 
15249b6b563cSPaul Mackerras static void __init prom_close_stdin(void)
15259b6b563cSPaul Mackerras {
15269b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
15279b6b563cSPaul Mackerras 	ihandle val;
15289b6b563cSPaul Mackerras 
15299b6b563cSPaul Mackerras 	if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
15309b6b563cSPaul Mackerras 		call_prom("close", 1, 0, val);
15319b6b563cSPaul Mackerras }
15329b6b563cSPaul Mackerras 
15339b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
15349b6b563cSPaul Mackerras {
15359b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
15369b6b563cSPaul Mackerras 	char compat[256];
15379b6b563cSPaul Mackerras 	int len, i = 0;
153821fe3301SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
15399b6b563cSPaul Mackerras 	phandle rtas;
1540e8222502SBenjamin Herrenschmidt 	int x;
154121fe3301SBenjamin Herrenschmidt #endif
1542e8222502SBenjamin Herrenschmidt 
1543e8222502SBenjamin Herrenschmidt 	/* Look for a PowerMac */
15449b6b563cSPaul Mackerras 	len = prom_getprop(_prom->root, "compatible",
15459b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
15469b6b563cSPaul Mackerras 	if (len > 0) {
15479b6b563cSPaul Mackerras 		compat[len] = 0;
15489b6b563cSPaul Mackerras 		while (i < len) {
15499b6b563cSPaul Mackerras 			char *p = &compat[i];
15509b6b563cSPaul Mackerras 			int sl = strlen(p);
15519b6b563cSPaul Mackerras 			if (sl == 0)
15529b6b563cSPaul Mackerras 				break;
15539b6b563cSPaul Mackerras 			if (strstr(p, RELOC("Power Macintosh")) ||
1554a575b807SPaul Mackerras 			    strstr(p, RELOC("MacRISC")))
15559b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
1556133dda1eSArnd Bergmann #ifdef CONFIG_PPC64
1557133dda1eSArnd Bergmann 			/* We must make sure we don't detect the IBM Cell
1558133dda1eSArnd Bergmann 			 * blades as pSeries due to some firmware issues,
1559133dda1eSArnd Bergmann 			 * so we do it here.
1560133dda1eSArnd Bergmann 			 */
1561133dda1eSArnd Bergmann 			if (strstr(p, RELOC("IBM,CBEA")) ||
1562133dda1eSArnd Bergmann 			    strstr(p, RELOC("IBM,CPBW-1.0")))
1563133dda1eSArnd Bergmann 				return PLATFORM_GENERIC;
1564133dda1eSArnd Bergmann #endif /* CONFIG_PPC64 */
15659b6b563cSPaul Mackerras 			i += sl + 1;
15669b6b563cSPaul Mackerras 		}
15679b6b563cSPaul Mackerras 	}
15689b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
1569e8222502SBenjamin Herrenschmidt 	/* If not a mac, try to figure out if it's an IBM pSeries or any other
1570e8222502SBenjamin Herrenschmidt 	 * PAPR compliant platform. We assume it is if :
1571e8222502SBenjamin Herrenschmidt 	 *  - /device_type is "chrp" (please, do NOT use that for future
1572e8222502SBenjamin Herrenschmidt 	 *    non-IBM designs !
1573e8222502SBenjamin Herrenschmidt 	 *  - it has /rtas
1574e8222502SBenjamin Herrenschmidt 	 */
15756f806ceeSMichael Ellerman 	len = prom_getprop(_prom->root, "device_type",
1576e8222502SBenjamin Herrenschmidt 			   compat, sizeof(compat)-1);
1577e8222502SBenjamin Herrenschmidt 	if (len <= 0)
1578e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
1579cb6b2eb9SBenjamin Herrenschmidt 	if (strcmp(compat, RELOC("chrp")))
1580e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
1581e8222502SBenjamin Herrenschmidt 
15829b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
15839b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1584e8222502SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(rtas))
1585e8222502SBenjamin Herrenschmidt 		return PLATFORM_GENERIC;
1586e8222502SBenjamin Herrenschmidt 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
15879b6b563cSPaul Mackerras 	if (x != PROM_ERROR) {
15884da727aeSAnton Blanchard 		prom_debug("Hypertas detected, assuming LPAR !\n");
15899b6b563cSPaul Mackerras 		return PLATFORM_PSERIES_LPAR;
15909b6b563cSPaul Mackerras 	}
15919b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
15929b6b563cSPaul Mackerras #else
1593e8222502SBenjamin Herrenschmidt 	return PLATFORM_GENERIC;
15949b6b563cSPaul Mackerras #endif
15959b6b563cSPaul Mackerras }
15969b6b563cSPaul Mackerras 
15979b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
15989b6b563cSPaul Mackerras {
15999b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
16009b6b563cSPaul Mackerras }
16019b6b563cSPaul Mackerras 
16029b6b563cSPaul Mackerras /*
16039b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
16049b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
16059b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
16069b6b563cSPaul Mackerras  * we've taken over the MMU.
16079b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
16089b6b563cSPaul Mackerras  * and if so, open it now.
16099b6b563cSPaul Mackerras  */
16109b6b563cSPaul Mackerras static void __init prom_check_displays(void)
16119b6b563cSPaul Mackerras {
16129b6b563cSPaul Mackerras 	char type[16], *path;
16139b6b563cSPaul Mackerras 	phandle node;
16149b6b563cSPaul Mackerras 	ihandle ih;
16159b6b563cSPaul Mackerras 	int i;
16169b6b563cSPaul Mackerras 
16179b6b563cSPaul Mackerras 	static unsigned char default_colors[] = {
16189b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
16199b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
16209b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
16219b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
16229b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
16239b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
16249b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
16259b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
16269b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
16279b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
16289b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
16299b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
16309b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
16319b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
16329b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
16339b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
16349b6b563cSPaul Mackerras 	};
16359b6b563cSPaul Mackerras 	const unsigned char *clut;
16369b6b563cSPaul Mackerras 
16374da727aeSAnton Blanchard 	prom_debug("Looking for displays\n");
16389b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
16399b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
16409b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
16419b6b563cSPaul Mackerras 		if (strcmp(type, RELOC("display")) != 0)
16429b6b563cSPaul Mackerras 			continue;
16439b6b563cSPaul Mackerras 
16449b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
16459b6b563cSPaul Mackerras 		path = RELOC(prom_scratch);
16469b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
16479b6b563cSPaul Mackerras 
16489b6b563cSPaul Mackerras 		/*
16499b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
16509b6b563cSPaul Mackerras 		 * arguments
16519b6b563cSPaul Mackerras 		 */
16529b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
16539b6b563cSPaul Mackerras 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
16549b6b563cSPaul Mackerras 			continue;
16559b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening... ", path);
16569b6b563cSPaul Mackerras 
16579b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
16589b6b563cSPaul Mackerras 		if (ih == 0) {
16599b6b563cSPaul Mackerras 			prom_printf("failed\n");
16609b6b563cSPaul Mackerras 			continue;
16619b6b563cSPaul Mackerras 		}
16629b6b563cSPaul Mackerras 
16639b6b563cSPaul Mackerras 		/* Success */
16649b6b563cSPaul Mackerras 		prom_printf("done\n");
1665a23414beSPaul Mackerras 		prom_setprop(node, path, "linux,opened", NULL, 0);
16669b6b563cSPaul Mackerras 
16679b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
16689b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
16699b6b563cSPaul Mackerras 		clut = RELOC(default_colors);
16709b6b563cSPaul Mackerras 		for (i = 0; i < 32; i++, clut += 3)
16719b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
16729b6b563cSPaul Mackerras 					   clut[2]) != 0)
16739b6b563cSPaul Mackerras 				break;
16749b6b563cSPaul Mackerras 
16759b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
16769b6b563cSPaul Mackerras 		clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
16779b6b563cSPaul Mackerras 		for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
16789b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
16799b6b563cSPaul Mackerras 					   clut[2]) != 0)
16809b6b563cSPaul Mackerras 				break;
16819b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
16829b6b563cSPaul Mackerras 	}
16839b6b563cSPaul Mackerras }
16849b6b563cSPaul Mackerras 
16859b6b563cSPaul Mackerras 
16869b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
16879b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
16889b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
16899b6b563cSPaul Mackerras {
16909b6b563cSPaul Mackerras 	void *ret;
16919b6b563cSPaul Mackerras 
16929b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
16939b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
16949b6b563cSPaul Mackerras 		unsigned long room, chunk;
16959b6b563cSPaul Mackerras 
16969b6b563cSPaul Mackerras 		prom_debug("Chunk exhausted, claiming more at %x...\n",
16979b6b563cSPaul Mackerras 			   RELOC(alloc_bottom));
16989b6b563cSPaul Mackerras 		room = RELOC(alloc_top) - RELOC(alloc_bottom);
16999b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
17009b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
17019b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
17029b6b563cSPaul Mackerras 			prom_panic("No memory for flatten_device_tree (no room)");
17039b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
17049b6b563cSPaul Mackerras 		if (chunk == 0)
17059b6b563cSPaul Mackerras 			prom_panic("No memory for flatten_device_tree (claim failed)");
17069b6b563cSPaul Mackerras 		*mem_end = RELOC(alloc_top);
17079b6b563cSPaul Mackerras 	}
17089b6b563cSPaul Mackerras 
17099b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
17109b6b563cSPaul Mackerras 	*mem_start += needed;
17119b6b563cSPaul Mackerras 
17129b6b563cSPaul Mackerras 	return ret;
17139b6b563cSPaul Mackerras }
17149b6b563cSPaul Mackerras 
17159b6b563cSPaul Mackerras #define dt_push_token(token, mem_start, mem_end) \
17169b6b563cSPaul Mackerras 	do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
17179b6b563cSPaul Mackerras 
17189b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
17199b6b563cSPaul Mackerras {
17209b6b563cSPaul Mackerras 	char *s, *os;
17219b6b563cSPaul Mackerras 
17229b6b563cSPaul Mackerras 	s = os = (char *)RELOC(dt_string_start);
17239b6b563cSPaul Mackerras 	s += 4;
17249b6b563cSPaul Mackerras 	while (s <  (char *)RELOC(dt_string_end)) {
17259b6b563cSPaul Mackerras 		if (strcmp(s, str) == 0)
17269b6b563cSPaul Mackerras 			return s - os;
17279b6b563cSPaul Mackerras 		s += strlen(s) + 1;
17289b6b563cSPaul Mackerras 	}
17299b6b563cSPaul Mackerras 	return 0;
17309b6b563cSPaul Mackerras }
17319b6b563cSPaul Mackerras 
17329b6b563cSPaul Mackerras /*
17339b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
17349b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
17359b6b563cSPaul Mackerras  */
17369b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
17379b6b563cSPaul Mackerras 
17389b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
17399b6b563cSPaul Mackerras 					 unsigned long *mem_start,
17409b6b563cSPaul Mackerras 					 unsigned long *mem_end)
17419b6b563cSPaul Mackerras {
17429b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
17439b6b563cSPaul Mackerras 	unsigned long soff;
17449b6b563cSPaul Mackerras 	phandle child;
17459b6b563cSPaul Mackerras 
17469b6b563cSPaul Mackerras 	sstart =  (char *)RELOC(dt_string_start);
17479b6b563cSPaul Mackerras 
17489b6b563cSPaul Mackerras 	/* get and store all property names */
17499b6b563cSPaul Mackerras 	prev_name = RELOC("");
17509b6b563cSPaul Mackerras 	for (;;) {
17519b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
17529b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
17539b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
17549b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
17559b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
17569b6b563cSPaul Mackerras 			break;
17579b6b563cSPaul Mackerras 		}
17589b6b563cSPaul Mackerras 
17599b6b563cSPaul Mackerras  		/* skip "name" */
17609b6b563cSPaul Mackerras  		if (strcmp(namep, RELOC("name")) == 0) {
17619b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
17629b6b563cSPaul Mackerras  			prev_name = RELOC("name");
17639b6b563cSPaul Mackerras  			continue;
17649b6b563cSPaul Mackerras  		}
17659b6b563cSPaul Mackerras 		/* get/create string entry */
17669b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
17679b6b563cSPaul Mackerras 		if (soff != 0) {
17689b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
17699b6b563cSPaul Mackerras 			namep = sstart + soff;
17709b6b563cSPaul Mackerras 		} else {
17719b6b563cSPaul Mackerras 			/* Trim off some if we can */
17729b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
17739b6b563cSPaul Mackerras 			RELOC(dt_string_end) = *mem_start;
17749b6b563cSPaul Mackerras 		}
17759b6b563cSPaul Mackerras 		prev_name = namep;
17769b6b563cSPaul Mackerras 	}
17779b6b563cSPaul Mackerras 
17789b6b563cSPaul Mackerras 	/* do all our children */
17799b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
17809b6b563cSPaul Mackerras 	while (child != 0) {
17819b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
17829b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
17839b6b563cSPaul Mackerras 	}
17849b6b563cSPaul Mackerras }
17859b6b563cSPaul Mackerras 
17869b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
17879b6b563cSPaul Mackerras 					unsigned long *mem_end)
17889b6b563cSPaul Mackerras {
17899b6b563cSPaul Mackerras 	phandle child;
17909b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
17919b6b563cSPaul Mackerras 	unsigned long soff;
17929b6b563cSPaul Mackerras 	unsigned char *valp;
17939b6b563cSPaul Mackerras 	static char pname[MAX_PROPERTY_NAME];
1794c4988820SPaul Mackerras 	int l, room;
17959b6b563cSPaul Mackerras 
17969b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
17979b6b563cSPaul Mackerras 
17989b6b563cSPaul Mackerras 	/* get the node's full name */
17999b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
1800c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
1801c4988820SPaul Mackerras 	if (room > 255)
1802c4988820SPaul Mackerras 		room = 255;
1803c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
18049b6b563cSPaul Mackerras 	if (l >= 0) {
18059b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
1806c4988820SPaul Mackerras 		if (l >= room) {
1807c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
18089b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
18099b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
18109b6b563cSPaul Mackerras 		}
18119b6b563cSPaul Mackerras 		namep[l] = '\0';
18129b6b563cSPaul Mackerras 
18139b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
1814a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
1815a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
18169b6b563cSPaul Mackerras 		 */
1817a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
18189b6b563cSPaul Mackerras 			if (*p == '/')
1819a575b807SPaul Mackerras 				lp = namep;
1820a575b807SPaul Mackerras 			else if (*p != 0)
1821a575b807SPaul Mackerras 				*lp++ = *p;
1822a575b807SPaul Mackerras 		}
1823a575b807SPaul Mackerras 		*lp = 0;
1824a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
18259b6b563cSPaul Mackerras 	}
18269b6b563cSPaul Mackerras 
18279b6b563cSPaul Mackerras 	/* get it again for debugging */
18289b6b563cSPaul Mackerras 	path = RELOC(prom_scratch);
18299b6b563cSPaul Mackerras 	memset(path, 0, PROM_SCRATCH_SIZE);
18309b6b563cSPaul Mackerras 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
18319b6b563cSPaul Mackerras 
18329b6b563cSPaul Mackerras 	/* get and store all properties */
18339b6b563cSPaul Mackerras 	prev_name = RELOC("");
18349b6b563cSPaul Mackerras 	sstart = (char *)RELOC(dt_string_start);
18359b6b563cSPaul Mackerras 	for (;;) {
18369b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
18379b6b563cSPaul Mackerras 			      RELOC(pname)) != 1)
18389b6b563cSPaul Mackerras 			break;
18399b6b563cSPaul Mackerras 
18409b6b563cSPaul Mackerras  		/* skip "name" */
18419b6b563cSPaul Mackerras  		if (strcmp(RELOC(pname), RELOC("name")) == 0) {
18429b6b563cSPaul Mackerras  			prev_name = RELOC("name");
18439b6b563cSPaul Mackerras  			continue;
18449b6b563cSPaul Mackerras  		}
18459b6b563cSPaul Mackerras 
18469b6b563cSPaul Mackerras 		/* find string offset */
18479b6b563cSPaul Mackerras 		soff = dt_find_string(RELOC(pname));
18489b6b563cSPaul Mackerras 		if (soff == 0) {
18499b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
18509b6b563cSPaul Mackerras 				    " <%s>, node %s\n", RELOC(pname), path);
18519b6b563cSPaul Mackerras 			break;
18529b6b563cSPaul Mackerras 		}
18539b6b563cSPaul Mackerras 		prev_name = sstart + soff;
18549b6b563cSPaul Mackerras 
18559b6b563cSPaul Mackerras 		/* get length */
18569b6b563cSPaul Mackerras 		l = call_prom("getproplen", 2, 1, node, RELOC(pname));
18579b6b563cSPaul Mackerras 
18589b6b563cSPaul Mackerras 		/* sanity checks */
18599b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
18609b6b563cSPaul Mackerras 			continue;
18619b6b563cSPaul Mackerras 		if (l > MAX_PROPERTY_LENGTH) {
18629b6b563cSPaul Mackerras 			prom_printf("WARNING: ignoring large property ");
18639b6b563cSPaul Mackerras 			/* It seems OF doesn't null-terminate the path :-( */
18649b6b563cSPaul Mackerras 			prom_printf("[%s] ", path);
18659b6b563cSPaul Mackerras 			prom_printf("%s length 0x%x\n", RELOC(pname), l);
18669b6b563cSPaul Mackerras 			continue;
18679b6b563cSPaul Mackerras 		}
18689b6b563cSPaul Mackerras 
18699b6b563cSPaul Mackerras 		/* push property head */
18709b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
18719b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
18729b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
18739b6b563cSPaul Mackerras 
18749b6b563cSPaul Mackerras 		/* push property content */
18759b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
18769b6b563cSPaul Mackerras 		call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
18779b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
18789b6b563cSPaul Mackerras 	}
18799b6b563cSPaul Mackerras 
18809b6b563cSPaul Mackerras 	/* Add a "linux,phandle" property. */
18819b6b563cSPaul Mackerras 	soff = dt_find_string(RELOC("linux,phandle"));
18829b6b563cSPaul Mackerras 	if (soff == 0)
18839b6b563cSPaul Mackerras 		prom_printf("WARNING: Can't find string index for"
18849b6b563cSPaul Mackerras 			    " <linux-phandle> node %s\n", path);
18859b6b563cSPaul Mackerras 	else {
18869b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
18879b6b563cSPaul Mackerras 		dt_push_token(4, mem_start, mem_end);
18889b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
18899b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, 4, 4);
18909b6b563cSPaul Mackerras 		*(u32 *)valp = node;
18919b6b563cSPaul Mackerras 	}
18929b6b563cSPaul Mackerras 
18939b6b563cSPaul Mackerras 	/* do all our children */
18949b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
18959b6b563cSPaul Mackerras 	while (child != 0) {
18969b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
18979b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
18989b6b563cSPaul Mackerras 	}
18999b6b563cSPaul Mackerras 
19009b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
19019b6b563cSPaul Mackerras }
19029b6b563cSPaul Mackerras 
19039b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
19049b6b563cSPaul Mackerras {
19059b6b563cSPaul Mackerras 	phandle root;
19069b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
19079b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
19089b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
19099b6b563cSPaul Mackerras 	char *namep;
19109b6b563cSPaul Mackerras 	u64 *rsvmap;
19119b6b563cSPaul Mackerras 
19129b6b563cSPaul Mackerras 	/*
19139b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
19149b6b563cSPaul Mackerras 	 * few pages), crop to 4Mb, as this is our "chuck" size
19159b6b563cSPaul Mackerras 	 */
19169b6b563cSPaul Mackerras 	room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
19179b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
19189b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
19199b6b563cSPaul Mackerras 	prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
19209b6b563cSPaul Mackerras 
19219b6b563cSPaul Mackerras 	/* Now try to claim that */
19229b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
19239b6b563cSPaul Mackerras 	if (mem_start == 0)
19249b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
19259b6b563cSPaul Mackerras 	mem_end = RELOC(alloc_top);
19269b6b563cSPaul Mackerras 
19279b6b563cSPaul Mackerras 	/* Get root of tree */
19289b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
19299b6b563cSPaul Mackerras 	if (root == (phandle)0)
19309b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
19319b6b563cSPaul Mackerras 
19329b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
19339b6b563cSPaul Mackerras 	mem_start = _ALIGN(mem_start, 4);
19349b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
19359b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
19369b6b563cSPaul Mackerras 	RELOC(dt_header_start) = (unsigned long)hdr;
19379b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
19389b6b563cSPaul Mackerras 
19399b6b563cSPaul Mackerras 	/* Start of strings */
19409b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
19419b6b563cSPaul Mackerras 	RELOC(dt_string_start) = mem_start;
19429b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
19439b6b563cSPaul Mackerras 
19449b6b563cSPaul Mackerras 	/* Add "linux,phandle" in there, we'll need it */
19459b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
19469b6b563cSPaul Mackerras 	strcpy(namep, RELOC("linux,phandle"));
19479b6b563cSPaul Mackerras 	mem_start = (unsigned long)namep + strlen(namep) + 1;
19489b6b563cSPaul Mackerras 
19499b6b563cSPaul Mackerras 	/* Build string array */
19509b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
19519b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
19529b6b563cSPaul Mackerras 	RELOC(dt_string_end) = mem_start;
19539b6b563cSPaul Mackerras 
19549b6b563cSPaul Mackerras 	/* Build structure */
19559b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
19569b6b563cSPaul Mackerras 	RELOC(dt_struct_start) = mem_start;
19579b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
19589b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
19599b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
19609b6b563cSPaul Mackerras 	RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
19619b6b563cSPaul Mackerras 
19629b6b563cSPaul Mackerras 	/* Finish header */
19639b6b563cSPaul Mackerras 	hdr->boot_cpuid_phys = _prom->cpu;
19649b6b563cSPaul Mackerras 	hdr->magic = OF_DT_HEADER;
19659b6b563cSPaul Mackerras 	hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
19669b6b563cSPaul Mackerras 	hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
19679b6b563cSPaul Mackerras 	hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
19689b6b563cSPaul Mackerras 	hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
19699b6b563cSPaul Mackerras 	hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
19709b6b563cSPaul Mackerras 	hdr->version = OF_DT_VERSION;
19719b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
19729b6b563cSPaul Mackerras 	hdr->last_comp_version = 0x10;
19739b6b563cSPaul Mackerras 
19744d1f3f25SJimi Xenidis 	/* Copy the reserve map in */
19759b6b563cSPaul Mackerras 	memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
19769b6b563cSPaul Mackerras 
19779b6b563cSPaul Mackerras #ifdef DEBUG_PROM
19789b6b563cSPaul Mackerras 	{
19799b6b563cSPaul Mackerras 		int i;
19809b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
19819b6b563cSPaul Mackerras 		for (i = 0; i < RELOC(mem_reserve_cnt); i++)
19829b6b563cSPaul Mackerras 			prom_printf("  %x - %x\n",
19839b6b563cSPaul Mackerras 				    RELOC(mem_reserve_map)[i].base,
19849b6b563cSPaul Mackerras 				    RELOC(mem_reserve_map)[i].size);
19859b6b563cSPaul Mackerras 	}
19869b6b563cSPaul Mackerras #endif
19874d1f3f25SJimi Xenidis 	/* Bump mem_reserve_cnt to cause further reservations to fail
19884d1f3f25SJimi Xenidis 	 * since it's too late.
19894d1f3f25SJimi Xenidis 	 */
19909b6b563cSPaul Mackerras 	RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
19919b6b563cSPaul Mackerras 
19929b6b563cSPaul Mackerras 	prom_printf("Device tree strings 0x%x -> 0x%x\n",
19939b6b563cSPaul Mackerras 		    RELOC(dt_string_start), RELOC(dt_string_end));
19949b6b563cSPaul Mackerras 	prom_printf("Device tree struct  0x%x -> 0x%x\n",
19959b6b563cSPaul Mackerras 		    RELOC(dt_struct_start), RELOC(dt_struct_end));
19969b6b563cSPaul Mackerras 
19979b6b563cSPaul Mackerras }
19989b6b563cSPaul Mackerras 
199954f4ee18SHollis Blanchard #ifdef CONFIG_PPC_MAPLE
200054f4ee18SHollis Blanchard /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
200154f4ee18SHollis Blanchard  * The values are bad, and it doesn't even have the right number of cells. */
200254f4ee18SHollis Blanchard static void __init fixup_device_tree_maple(void)
20039b6b563cSPaul Mackerras {
200454f4ee18SHollis Blanchard 	phandle isa;
2005980a6513SBenjamin Herrenschmidt 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
200654f4ee18SHollis Blanchard 	u32 isa_ranges[6];
2007980a6513SBenjamin Herrenschmidt 	char *name;
200854f4ee18SHollis Blanchard 
2009980a6513SBenjamin Herrenschmidt 	name = "/ht@0/isa@4";
2010980a6513SBenjamin Herrenschmidt 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2011980a6513SBenjamin Herrenschmidt 	if (!PHANDLE_VALID(isa)) {
2012980a6513SBenjamin Herrenschmidt 		name = "/ht@0/isa@6";
2013980a6513SBenjamin Herrenschmidt 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2014980a6513SBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2015980a6513SBenjamin Herrenschmidt 	}
201654f4ee18SHollis Blanchard 	if (!PHANDLE_VALID(isa))
201754f4ee18SHollis Blanchard 		return;
201854f4ee18SHollis Blanchard 
2019980a6513SBenjamin Herrenschmidt 	if (prom_getproplen(isa, "ranges") != 12)
2020980a6513SBenjamin Herrenschmidt 		return;
202154f4ee18SHollis Blanchard 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
202254f4ee18SHollis Blanchard 		== PROM_ERROR)
202354f4ee18SHollis Blanchard 		return;
202454f4ee18SHollis Blanchard 
202554f4ee18SHollis Blanchard 	if (isa_ranges[0] != 0x1 ||
202654f4ee18SHollis Blanchard 		isa_ranges[1] != 0xf4000000 ||
202754f4ee18SHollis Blanchard 		isa_ranges[2] != 0x00010000)
202854f4ee18SHollis Blanchard 		return;
202954f4ee18SHollis Blanchard 
2030980a6513SBenjamin Herrenschmidt 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
203154f4ee18SHollis Blanchard 
203254f4ee18SHollis Blanchard 	isa_ranges[0] = 0x1;
203354f4ee18SHollis Blanchard 	isa_ranges[1] = 0x0;
2034980a6513SBenjamin Herrenschmidt 	isa_ranges[2] = rloc;
203554f4ee18SHollis Blanchard 	isa_ranges[3] = 0x0;
203654f4ee18SHollis Blanchard 	isa_ranges[4] = 0x0;
203754f4ee18SHollis Blanchard 	isa_ranges[5] = 0x00010000;
2038980a6513SBenjamin Herrenschmidt 	prom_setprop(isa, name, "ranges",
203954f4ee18SHollis Blanchard 			isa_ranges, sizeof(isa_ranges));
204054f4ee18SHollis Blanchard }
20418f101a05SHarry Ciao 
20428f101a05SHarry Ciao #define CPC925_MC_START		0xf8000000
20438f101a05SHarry Ciao #define CPC925_MC_LENGTH	0x1000000
20448f101a05SHarry Ciao /* The values for memory-controller don't have right number of cells */
20458f101a05SHarry Ciao static void __init fixup_device_tree_maple_memory_controller(void)
20468f101a05SHarry Ciao {
20478f101a05SHarry Ciao 	phandle mc;
20488f101a05SHarry Ciao 	u32 mc_reg[4];
20498f101a05SHarry Ciao 	char *name = "/hostbridge@f8000000";
20508f101a05SHarry Ciao 	struct prom_t *_prom = &RELOC(prom);
20518f101a05SHarry Ciao 	u32 ac, sc;
20528f101a05SHarry Ciao 
20538f101a05SHarry Ciao 	mc = call_prom("finddevice", 1, 1, ADDR(name));
20548f101a05SHarry Ciao 	if (!PHANDLE_VALID(mc))
20558f101a05SHarry Ciao 		return;
20568f101a05SHarry Ciao 
20578f101a05SHarry Ciao 	if (prom_getproplen(mc, "reg") != 8)
20588f101a05SHarry Ciao 		return;
20598f101a05SHarry Ciao 
20608f101a05SHarry Ciao 	prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
20618f101a05SHarry Ciao 	prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
20628f101a05SHarry Ciao 	if ((ac != 2) || (sc != 2))
20638f101a05SHarry Ciao 		return;
20648f101a05SHarry Ciao 
20658f101a05SHarry Ciao 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
20668f101a05SHarry Ciao 		return;
20678f101a05SHarry Ciao 
20688f101a05SHarry Ciao 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
20698f101a05SHarry Ciao 		return;
20708f101a05SHarry Ciao 
20718f101a05SHarry Ciao 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
20728f101a05SHarry Ciao 
20738f101a05SHarry Ciao 	mc_reg[0] = 0x0;
20748f101a05SHarry Ciao 	mc_reg[1] = CPC925_MC_START;
20758f101a05SHarry Ciao 	mc_reg[2] = 0x0;
20768f101a05SHarry Ciao 	mc_reg[3] = CPC925_MC_LENGTH;
20778f101a05SHarry Ciao 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
20788f101a05SHarry Ciao }
207954f4ee18SHollis Blanchard #else
208054f4ee18SHollis Blanchard #define fixup_device_tree_maple()
20818f101a05SHarry Ciao #define fixup_device_tree_maple_memory_controller()
208254f4ee18SHollis Blanchard #endif
208354f4ee18SHollis Blanchard 
2084e8c0acf9SBenjamin Herrenschmidt #ifdef CONFIG_PPC_CHRP
2085e4805922SOlaf Hering /*
2086e4805922SOlaf Hering  * Pegasos and BriQ lacks the "ranges" property in the isa node
2087e4805922SOlaf Hering  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2088556ecf9bSOlaf Hering  * Pegasos has the IDE configured in legacy mode, but advertised as native
2089e4805922SOlaf Hering  */
2090e8c0acf9SBenjamin Herrenschmidt static void __init fixup_device_tree_chrp(void)
2091e8c0acf9SBenjamin Herrenschmidt {
2092e4805922SOlaf Hering 	phandle ph;
2093e4805922SOlaf Hering 	u32 prop[6];
209426c5032eSBenjamin Herrenschmidt 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2095e8c0acf9SBenjamin Herrenschmidt 	char *name;
2096e8c0acf9SBenjamin Herrenschmidt 	int rc;
2097e8c0acf9SBenjamin Herrenschmidt 
2098e8c0acf9SBenjamin Herrenschmidt 	name = "/pci@80000000/isa@c";
2099e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2100e4805922SOlaf Hering 	if (!PHANDLE_VALID(ph)) {
210126c5032eSBenjamin Herrenschmidt 		name = "/pci@ff500000/isa@6";
2102e4805922SOlaf Hering 		ph = call_prom("finddevice", 1, 1, ADDR(name));
210326c5032eSBenjamin Herrenschmidt 		rloc = 0x01003000; /* IO space; PCI device = 6 */
210426c5032eSBenjamin Herrenschmidt 	}
2105e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2106e4805922SOlaf Hering 		rc = prom_getproplen(ph, "ranges");
2107e4805922SOlaf Hering 		if (rc == 0 || rc == PROM_ERROR) {
2108e8c0acf9SBenjamin Herrenschmidt 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2109e8c0acf9SBenjamin Herrenschmidt 
2110e4805922SOlaf Hering 			prop[0] = 0x1;
2111e4805922SOlaf Hering 			prop[1] = 0x0;
2112e4805922SOlaf Hering 			prop[2] = rloc;
2113e4805922SOlaf Hering 			prop[3] = 0x0;
2114e4805922SOlaf Hering 			prop[4] = 0x0;
2115e4805922SOlaf Hering 			prop[5] = 0x00010000;
2116e4805922SOlaf Hering 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2117e4805922SOlaf Hering 		}
2118e4805922SOlaf Hering 	}
2119e4805922SOlaf Hering 
2120e4805922SOlaf Hering 	name = "/pci@80000000/ide@C,1";
2121e4805922SOlaf Hering 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2122e4805922SOlaf Hering 	if (PHANDLE_VALID(ph)) {
2123e4805922SOlaf Hering 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2124e4805922SOlaf Hering 		prop[0] = 14;
2125e4805922SOlaf Hering 		prop[1] = 0x0;
2126556ecf9bSOlaf Hering 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2127556ecf9bSOlaf Hering 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2128556ecf9bSOlaf Hering 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2129556ecf9bSOlaf Hering 		if (rc == sizeof(u32)) {
2130556ecf9bSOlaf Hering 			prop[0] &= ~0x5;
2131556ecf9bSOlaf Hering 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2132556ecf9bSOlaf Hering 		}
2133e4805922SOlaf Hering 	}
2134e8c0acf9SBenjamin Herrenschmidt }
2135e8c0acf9SBenjamin Herrenschmidt #else
2136e8c0acf9SBenjamin Herrenschmidt #define fixup_device_tree_chrp()
2137e8c0acf9SBenjamin Herrenschmidt #endif
2138e8c0acf9SBenjamin Herrenschmidt 
21399b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
214054f4ee18SHollis Blanchard static void __init fixup_device_tree_pmac(void)
214154f4ee18SHollis Blanchard {
21429b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
21439b6b563cSPaul Mackerras 	u32 u3_rev;
21449b6b563cSPaul Mackerras 	u32 interrupts[2];
21459b6b563cSPaul Mackerras 	u32 parent;
21469b6b563cSPaul Mackerras 
21479b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
21489b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
21499b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
21509b6b563cSPaul Mackerras 		return;
21519b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
21529b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
21539b6b563cSPaul Mackerras 		return;
21549b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
21559b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
21569b6b563cSPaul Mackerras 		return;
21579b6b563cSPaul Mackerras 
21589b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
21599b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
21609b6b563cSPaul Mackerras 	    == PROM_ERROR)
21619b6b563cSPaul Mackerras 		return;
21627d49697eSBenjamin Herrenschmidt 	if (u3_rev < 0x35 || u3_rev > 0x39)
21639b6b563cSPaul Mackerras 		return;
21649b6b563cSPaul Mackerras 	/* does it need fixup ? */
21659b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
21669b6b563cSPaul Mackerras 		return;
21679b6b563cSPaul Mackerras 
21689b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
21699b6b563cSPaul Mackerras 
21709b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
21719b6b563cSPaul Mackerras 	interrupts[0] = 0;
21729b6b563cSPaul Mackerras 	interrupts[1] = 1;
2173a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2174a23414beSPaul Mackerras 		     &interrupts, sizeof(interrupts));
21759b6b563cSPaul Mackerras 	parent = (u32)mpic;
2176a23414beSPaul Mackerras 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2177a23414beSPaul Mackerras 		     &parent, sizeof(parent));
21789b6b563cSPaul Mackerras }
217954f4ee18SHollis Blanchard #else
218054f4ee18SHollis Blanchard #define fixup_device_tree_pmac()
218154f4ee18SHollis Blanchard #endif
21829b6b563cSPaul Mackerras 
218388fd2a9dSSylvain Munaut #ifdef CONFIG_PPC_EFIKA
218494d2dde7SGrant Likely /*
218594d2dde7SGrant Likely  * The MPC5200 FEC driver requires an phy-handle property to tell it how
218694d2dde7SGrant Likely  * to talk to the phy.  If the phy-handle property is missing, then this
218794d2dde7SGrant Likely  * function is called to add the appropriate nodes and link it to the
218894d2dde7SGrant Likely  * ethernet node.
218994d2dde7SGrant Likely  */
219094d2dde7SGrant Likely static void __init fixup_device_tree_efika_add_phy(void)
219188fd2a9dSSylvain Munaut {
219288fd2a9dSSylvain Munaut 	u32 node;
219388fd2a9dSSylvain Munaut 	char prop[64];
219494d2dde7SGrant Likely 	int rv;
219588fd2a9dSSylvain Munaut 
219694d2dde7SGrant Likely 	/* Check if /builtin/ethernet exists - bail if it doesn't */
219794d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
219888fd2a9dSSylvain Munaut 	if (!PHANDLE_VALID(node))
219988fd2a9dSSylvain Munaut 		return;
220088fd2a9dSSylvain Munaut 
220194d2dde7SGrant Likely 	/* Check if the phy-handle property exists - bail if it does */
220294d2dde7SGrant Likely 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
220394d2dde7SGrant Likely 	if (!rv)
220488fd2a9dSSylvain Munaut 		return;
220588fd2a9dSSylvain Munaut 
220694d2dde7SGrant Likely 	/*
220794d2dde7SGrant Likely 	 * At this point the ethernet device doesn't have a phy described.
220894d2dde7SGrant Likely 	 * Now we need to add the missing phy node and linkage
220994d2dde7SGrant Likely 	 */
221088fd2a9dSSylvain Munaut 
221194d2dde7SGrant Likely 	/* Check for an MDIO bus node - if missing then create one */
22126f4347c9SOlaf Hering 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
22136f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
22146f4347c9SOlaf Hering 		prom_printf("Adding Ethernet MDIO node\n");
22156f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
22166f4347c9SOlaf Hering 			" s\" /builtin\" find-device"
22176f4347c9SOlaf Hering 			" new-device"
22186f4347c9SOlaf Hering 				" 1 encode-int s\" #address-cells\" property"
22196f4347c9SOlaf Hering 				" 0 encode-int s\" #size-cells\" property"
222094d2dde7SGrant Likely 				" s\" mdio\" device-name"
222194d2dde7SGrant Likely 				" s\" fsl,mpc5200b-mdio\" encode-string"
22226f4347c9SOlaf Hering 				" s\" compatible\" property"
22236f4347c9SOlaf Hering 				" 0xf0003000 0x400 reg"
22246f4347c9SOlaf Hering 				" 0x2 encode-int"
22256f4347c9SOlaf Hering 				" 0x5 encode-int encode+"
22266f4347c9SOlaf Hering 				" 0x3 encode-int encode+"
22276f4347c9SOlaf Hering 				" s\" interrupts\" property"
22286f4347c9SOlaf Hering 			" finish-device");
22296f4347c9SOlaf Hering 	};
22306f4347c9SOlaf Hering 
223194d2dde7SGrant Likely 	/* Check for a PHY device node - if missing then create one and
223294d2dde7SGrant Likely 	 * give it's phandle to the ethernet node */
223394d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1,
223494d2dde7SGrant Likely 			 ADDR("/builtin/mdio/ethernet-phy"));
22356f4347c9SOlaf Hering 	if (!PHANDLE_VALID(node)) {
22366f4347c9SOlaf Hering 		prom_printf("Adding Ethernet PHY node\n");
22376f4347c9SOlaf Hering 		call_prom("interpret", 1, 1,
22386f4347c9SOlaf Hering 			" s\" /builtin/mdio\" find-device"
22396f4347c9SOlaf Hering 			" new-device"
22406f4347c9SOlaf Hering 				" s\" ethernet-phy\" device-name"
22416f4347c9SOlaf Hering 				" 0x10 encode-int s\" reg\" property"
22426f4347c9SOlaf Hering 				" my-self"
22436f4347c9SOlaf Hering 				" ihandle>phandle"
22446f4347c9SOlaf Hering 			" finish-device"
22456f4347c9SOlaf Hering 			" s\" /builtin/ethernet\" find-device"
22466f4347c9SOlaf Hering 				" encode-int"
22476f4347c9SOlaf Hering 				" s\" phy-handle\" property"
22486f4347c9SOlaf Hering 			" device-end");
22496f4347c9SOlaf Hering 	}
225094d2dde7SGrant Likely }
22516f4347c9SOlaf Hering 
225294d2dde7SGrant Likely static void __init fixup_device_tree_efika(void)
225394d2dde7SGrant Likely {
225494d2dde7SGrant Likely 	int sound_irq[3] = { 2, 2, 0 };
225594d2dde7SGrant Likely 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
225694d2dde7SGrant Likely 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
225794d2dde7SGrant Likely 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
225894d2dde7SGrant Likely 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
225994d2dde7SGrant Likely 	u32 node;
226094d2dde7SGrant Likely 	char prop[64];
226194d2dde7SGrant Likely 	int rv, len;
226294d2dde7SGrant Likely 
226394d2dde7SGrant Likely 	/* Check if we're really running on a EFIKA */
226494d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
226594d2dde7SGrant Likely 	if (!PHANDLE_VALID(node))
226694d2dde7SGrant Likely 		return;
226794d2dde7SGrant Likely 
226894d2dde7SGrant Likely 	rv = prom_getprop(node, "model", prop, sizeof(prop));
226994d2dde7SGrant Likely 	if (rv == PROM_ERROR)
227094d2dde7SGrant Likely 		return;
227194d2dde7SGrant Likely 	if (strcmp(prop, "EFIKA5K2"))
227294d2dde7SGrant Likely 		return;
227394d2dde7SGrant Likely 
227494d2dde7SGrant Likely 	prom_printf("Applying EFIKA device tree fixups\n");
227594d2dde7SGrant Likely 
227694d2dde7SGrant Likely 	/* Claiming to be 'chrp' is death */
227794d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/"));
227894d2dde7SGrant Likely 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
227994d2dde7SGrant Likely 	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
228094d2dde7SGrant Likely 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
228194d2dde7SGrant Likely 
22827f4392cdSDavid Woodhouse 	/* CODEGEN,description is exposed in /proc/cpuinfo so
22837f4392cdSDavid Woodhouse 	   fix that too */
22847f4392cdSDavid Woodhouse 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
22857f4392cdSDavid Woodhouse 	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
22867f4392cdSDavid Woodhouse 		prom_setprop(node, "/", "CODEGEN,description",
22877f4392cdSDavid Woodhouse 			     "Efika 5200B PowerPC System",
22887f4392cdSDavid Woodhouse 			     sizeof("Efika 5200B PowerPC System"));
22897f4392cdSDavid Woodhouse 
229094d2dde7SGrant Likely 	/* Fixup bestcomm interrupts property */
229194d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
229294d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
229394d2dde7SGrant Likely 		len = prom_getproplen(node, "interrupts");
229494d2dde7SGrant Likely 		if (len == 12) {
229594d2dde7SGrant Likely 			prom_printf("Fixing bestcomm interrupts property\n");
229694d2dde7SGrant Likely 			prom_setprop(node, "/builtin/bestcom", "interrupts",
229794d2dde7SGrant Likely 				     bcomm_irq, sizeof(bcomm_irq));
229894d2dde7SGrant Likely 		}
229994d2dde7SGrant Likely 	}
230094d2dde7SGrant Likely 
230194d2dde7SGrant Likely 	/* Fixup sound interrupts property */
230294d2dde7SGrant Likely 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
230394d2dde7SGrant Likely 	if (PHANDLE_VALID(node)) {
230494d2dde7SGrant Likely 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
230594d2dde7SGrant Likely 		if (rv == PROM_ERROR) {
230694d2dde7SGrant Likely 			prom_printf("Adding sound interrupts property\n");
230794d2dde7SGrant Likely 			prom_setprop(node, "/builtin/sound", "interrupts",
230894d2dde7SGrant Likely 				     sound_irq, sizeof(sound_irq));
230994d2dde7SGrant Likely 		}
231094d2dde7SGrant Likely 	}
231194d2dde7SGrant Likely 
231294d2dde7SGrant Likely 	/* Make sure ethernet phy-handle property exists */
231394d2dde7SGrant Likely 	fixup_device_tree_efika_add_phy();
231488fd2a9dSSylvain Munaut }
231588fd2a9dSSylvain Munaut #else
231688fd2a9dSSylvain Munaut #define fixup_device_tree_efika()
231788fd2a9dSSylvain Munaut #endif
231888fd2a9dSSylvain Munaut 
231954f4ee18SHollis Blanchard static void __init fixup_device_tree(void)
232054f4ee18SHollis Blanchard {
232154f4ee18SHollis Blanchard 	fixup_device_tree_maple();
23228f101a05SHarry Ciao 	fixup_device_tree_maple_memory_controller();
2323e8c0acf9SBenjamin Herrenschmidt 	fixup_device_tree_chrp();
232454f4ee18SHollis Blanchard 	fixup_device_tree_pmac();
232588fd2a9dSSylvain Munaut 	fixup_device_tree_efika();
232654f4ee18SHollis Blanchard }
23279b6b563cSPaul Mackerras 
23289b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
23299b6b563cSPaul Mackerras {
23309b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
23319b6b563cSPaul Mackerras 	u32 getprop_rval;
23329b6b563cSPaul Mackerras 	ihandle prom_cpu;
23339b6b563cSPaul Mackerras 	phandle cpu_pkg;
23349b6b563cSPaul Mackerras 
2335a575b807SPaul Mackerras 	_prom->cpu = 0;
23369b6b563cSPaul Mackerras 	if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
2337a575b807SPaul Mackerras 		return;
23389b6b563cSPaul Mackerras 
23399b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
23409b6b563cSPaul Mackerras 
23419b6b563cSPaul Mackerras 	prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
23429b6b563cSPaul Mackerras 	_prom->cpu = getprop_rval;
23439b6b563cSPaul Mackerras 
23449b6b563cSPaul Mackerras 	prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
23459b6b563cSPaul Mackerras }
23469b6b563cSPaul Mackerras 
23479b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
23489b6b563cSPaul Mackerras {
23499b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
23509b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
23519b6b563cSPaul Mackerras 
23529b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
23539b6b563cSPaul Mackerras 		unsigned long val;
23549b6b563cSPaul Mackerras 
235551fae6deSMichael Ellerman 		RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
23569b6b563cSPaul Mackerras 		RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
23579b6b563cSPaul Mackerras 
23589b6b563cSPaul Mackerras 		val = RELOC(prom_initrd_start);
2359a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2360a23414beSPaul Mackerras 			     &val, sizeof(val));
23619b6b563cSPaul Mackerras 		val = RELOC(prom_initrd_end);
2362a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2363a23414beSPaul Mackerras 			     &val, sizeof(val));
23649b6b563cSPaul Mackerras 
23659b6b563cSPaul Mackerras 		reserve_mem(RELOC(prom_initrd_start),
23669b6b563cSPaul Mackerras 			    RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
23679b6b563cSPaul Mackerras 
23689b6b563cSPaul Mackerras 		prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
23699b6b563cSPaul Mackerras 		prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
23709b6b563cSPaul Mackerras 	}
23719b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
23729b6b563cSPaul Mackerras }
23739b6b563cSPaul Mackerras 
23749b6b563cSPaul Mackerras /*
23759b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
23769b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
23779b6b563cSPaul Mackerras  */
23789b6b563cSPaul Mackerras 
23799b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
23809b6b563cSPaul Mackerras 			       unsigned long pp,
2381549e8152SPaul Mackerras 			       unsigned long r6, unsigned long r7,
2382549e8152SPaul Mackerras 			       unsigned long kbase)
23839b6b563cSPaul Mackerras {
23849b6b563cSPaul Mackerras 	struct prom_t *_prom;
23859b6b563cSPaul Mackerras 	unsigned long hdr;
23869b6b563cSPaul Mackerras 
23879b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
2388549e8152SPaul Mackerras 	unsigned long offset = reloc_offset();
23899b6b563cSPaul Mackerras 	reloc_got2(offset);
23909b6b563cSPaul Mackerras #endif
23919b6b563cSPaul Mackerras 
23929b6b563cSPaul Mackerras 	_prom = &RELOC(prom);
23939b6b563cSPaul Mackerras 
23949b6b563cSPaul Mackerras 	/*
23959b6b563cSPaul Mackerras 	 * First zero the BSS
23969b6b563cSPaul Mackerras 	 */
23979b6b563cSPaul Mackerras 	memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
23989b6b563cSPaul Mackerras 
23999b6b563cSPaul Mackerras 	/*
24009b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
24019b6b563cSPaul Mackerras 	 * like /chosen
24029b6b563cSPaul Mackerras 	 */
24039b6b563cSPaul Mackerras 	prom_init_client_services(pp);
24049b6b563cSPaul Mackerras 
24059b6b563cSPaul Mackerras 	/*
2406a23414beSPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
2407a23414beSPaul Mackerras 	 * and other workarounds
2408a23414beSPaul Mackerras 	 */
2409a23414beSPaul Mackerras 	prom_find_mmu();
2410a23414beSPaul Mackerras 
2411a23414beSPaul Mackerras 	/*
24129b6b563cSPaul Mackerras 	 * Init prom stdout device
24139b6b563cSPaul Mackerras 	 */
24149b6b563cSPaul Mackerras 	prom_init_stdout();
24159b6b563cSPaul Mackerras 
2416151a9f4aSBenjamin Herrenschmidt 	prom_printf("Preparing to boot %s", RELOC(linux_banner));
2417e7943fbbSMichael Ellerman 
24189b6b563cSPaul Mackerras 	/*
24199b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
24209b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
24219b6b563cSPaul Mackerras 	 */
24229b6b563cSPaul Mackerras 	RELOC(of_platform) = prom_find_machine_type();
24239b6b563cSPaul Mackerras 
2424549e8152SPaul Mackerras #ifndef CONFIG_RELOCATABLE
2425add60ef3SOlaf Hering 	/* Bail if this is a kdump kernel. */
2426add60ef3SOlaf Hering 	if (PHYSICAL_START > 0)
2427add60ef3SOlaf Hering 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
2428549e8152SPaul Mackerras #endif
2429add60ef3SOlaf Hering 
2430add60ef3SOlaf Hering 	/*
2431add60ef3SOlaf Hering 	 * Check for an initrd
2432add60ef3SOlaf Hering 	 */
2433add60ef3SOlaf Hering 	prom_check_initrd(r3, r4);
2434add60ef3SOlaf Hering 
24359b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
24369b6b563cSPaul Mackerras 	/*
24379b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
24389b6b563cSPaul Mackerras 	 */
2439799d6046SPaul Mackerras 	if (RELOC(of_platform) == PLATFORM_PSERIES ||
2440799d6046SPaul Mackerras 	    RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
24419b6b563cSPaul Mackerras 		prom_send_capabilities();
24429b6b563cSPaul Mackerras #endif
24439b6b563cSPaul Mackerras 
24449b6b563cSPaul Mackerras 	/*
2445f3f66f59SArnd Bergmann 	 * Copy the CPU hold code
24469b6b563cSPaul Mackerras 	 */
244755d36339SDavid Gibson 	if (RELOC(of_platform) != PLATFORM_POWERMAC)
2448549e8152SPaul Mackerras 		copy_and_flush(0, kbase, 0x100, 0);
24499b6b563cSPaul Mackerras 
24509b6b563cSPaul Mackerras 	/*
24519b6b563cSPaul Mackerras 	 * Do early parsing of command line
24529b6b563cSPaul Mackerras 	 */
24539b6b563cSPaul Mackerras 	early_cmdline_parse();
24549b6b563cSPaul Mackerras 
24559b6b563cSPaul Mackerras 	/*
24569b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
24579b6b563cSPaul Mackerras 	 */
24589b6b563cSPaul Mackerras 	prom_init_mem();
24599b6b563cSPaul Mackerras 
24609b6b563cSPaul Mackerras 	/*
24619b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
24629b6b563cSPaul Mackerras 	 */
24639b6b563cSPaul Mackerras 	prom_find_boot_cpu();
24649b6b563cSPaul Mackerras 
24659b6b563cSPaul Mackerras 	/*
24669b6b563cSPaul Mackerras 	 * Initialize display devices
24679b6b563cSPaul Mackerras 	 */
24689b6b563cSPaul Mackerras 	prom_check_displays();
24699b6b563cSPaul Mackerras 
24709b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
24719b6b563cSPaul Mackerras 	/*
24729b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
24739b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
24749b6b563cSPaul Mackerras 	 * available for us here...
24759b6b563cSPaul Mackerras 	 */
24769b6b563cSPaul Mackerras 	if (RELOC(of_platform) == PLATFORM_PSERIES)
24779b6b563cSPaul Mackerras 		prom_initialize_tce_table();
24789b6b563cSPaul Mackerras #endif
24799b6b563cSPaul Mackerras 
24809b6b563cSPaul Mackerras 	/*
24819b6b563cSPaul Mackerras 	 * On non-powermacs, try to instantiate RTAS and puts all CPUs
24829b6b563cSPaul Mackerras 	 * in spin-loops. PowerMacs don't have a working RTAS and use
24839b6b563cSPaul Mackerras 	 * a different way to spin CPUs
24849b6b563cSPaul Mackerras 	 */
24859b6b563cSPaul Mackerras 	if (RELOC(of_platform) != PLATFORM_POWERMAC) {
24869b6b563cSPaul Mackerras 		prom_instantiate_rtas();
24879b6b563cSPaul Mackerras 		prom_hold_cpus();
24889b6b563cSPaul Mackerras 	}
24899b6b563cSPaul Mackerras 
24909b6b563cSPaul Mackerras 	/*
24919b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
24929b6b563cSPaul Mackerras 	 */
2493*cf68787bSBenjamin Krill 	if (RELOC(prom_memory_limit))
2494*cf68787bSBenjamin Krill 		prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
2495*cf68787bSBenjamin Krill 			     &RELOC(prom_memory_limit),
2496*cf68787bSBenjamin Krill 			     sizeof(prom_memory_limit));
24979b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
2498165785e5SJeremy Kerr 	if (RELOC(prom_iommu_off))
2499a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2500a23414beSPaul Mackerras 			     NULL, 0);
25019b6b563cSPaul Mackerras 
2502165785e5SJeremy Kerr 	if (RELOC(prom_iommu_force_on))
2503a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2504a23414beSPaul Mackerras 			     NULL, 0);
25059b6b563cSPaul Mackerras 
25069b6b563cSPaul Mackerras 	if (RELOC(prom_tce_alloc_start)) {
2507a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
25089b6b563cSPaul Mackerras 			     &RELOC(prom_tce_alloc_start),
25099b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
2510a23414beSPaul Mackerras 		prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
25119b6b563cSPaul Mackerras 			     &RELOC(prom_tce_alloc_end),
25129b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
25139b6b563cSPaul Mackerras 	}
25149b6b563cSPaul Mackerras #endif
25159b6b563cSPaul Mackerras 
25169b6b563cSPaul Mackerras 	/*
25179b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
25189b6b563cSPaul Mackerras 	 */
25199b6b563cSPaul Mackerras 	fixup_device_tree();
25209b6b563cSPaul Mackerras 
25219b6b563cSPaul Mackerras 	/*
25229b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
25239b6b563cSPaul Mackerras 	 */
25249b6b563cSPaul Mackerras 	prom_printf("copying OF device tree...\n");
25259b6b563cSPaul Mackerras 	flatten_device_tree();
25269b6b563cSPaul Mackerras 
25273825ac0eSPaul Mackerras 	/*
25283825ac0eSPaul Mackerras 	 * in case stdin is USB and still active on IBM machines...
25293825ac0eSPaul Mackerras 	 * Unfortunately quiesce crashes on some powermacs if we have
25303825ac0eSPaul Mackerras 	 * closed stdin already (in particular the powerbook 101).
25313825ac0eSPaul Mackerras 	 */
25323825ac0eSPaul Mackerras 	if (RELOC(of_platform) != PLATFORM_POWERMAC)
25339b6b563cSPaul Mackerras 		prom_close_stdin();
25349b6b563cSPaul Mackerras 
25359b6b563cSPaul Mackerras 	/*
25369b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
25379b6b563cSPaul Mackerras 	 * devices etc...
25389b6b563cSPaul Mackerras 	 */
25399b6b563cSPaul Mackerras 	prom_printf("Calling quiesce...\n");
25409b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
25419b6b563cSPaul Mackerras 
25429b6b563cSPaul Mackerras 	/*
25439b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
25449b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
25459b6b563cSPaul Mackerras 	 * is common to us and kexec
25469b6b563cSPaul Mackerras 	 */
25479b6b563cSPaul Mackerras 	hdr = RELOC(dt_header_start);
25489b6b563cSPaul Mackerras 	prom_printf("returning from prom_init\n");
25499b6b563cSPaul Mackerras 	prom_debug("->dt_header_start=0x%x\n", hdr);
25509b6b563cSPaul Mackerras 
25519b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
25529b6b563cSPaul Mackerras 	reloc_got2(-offset);
25539b6b563cSPaul Mackerras #endif
25549b6b563cSPaul Mackerras 
2555549e8152SPaul Mackerras 	__start(hdr, kbase, 0);
25569b6b563cSPaul Mackerras 
25579b6b563cSPaul Mackerras 	return 0;
25589b6b563cSPaul Mackerras }
2559