xref: /linux/arch/powerpc/kernel/prom_init.c (revision c49888203d7a316cb947bb8a1cf2ae191f28bcd3)
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/config.h>
209b6b563cSPaul Mackerras #include <linux/kernel.h>
219b6b563cSPaul Mackerras #include <linux/string.h>
229b6b563cSPaul Mackerras #include <linux/init.h>
239b6b563cSPaul Mackerras #include <linux/threads.h>
249b6b563cSPaul Mackerras #include <linux/spinlock.h>
259b6b563cSPaul Mackerras #include <linux/types.h>
269b6b563cSPaul Mackerras #include <linux/pci.h>
279b6b563cSPaul Mackerras #include <linux/proc_fs.h>
289b6b563cSPaul Mackerras #include <linux/stringify.h>
299b6b563cSPaul Mackerras #include <linux/delay.h>
309b6b563cSPaul Mackerras #include <linux/initrd.h>
319b6b563cSPaul Mackerras #include <linux/bitops.h>
329b6b563cSPaul Mackerras #include <asm/prom.h>
339b6b563cSPaul Mackerras #include <asm/rtas.h>
349b6b563cSPaul Mackerras #include <asm/page.h>
359b6b563cSPaul Mackerras #include <asm/processor.h>
369b6b563cSPaul Mackerras #include <asm/irq.h>
379b6b563cSPaul Mackerras #include <asm/io.h>
389b6b563cSPaul Mackerras #include <asm/smp.h>
399b6b563cSPaul Mackerras #include <asm/system.h>
409b6b563cSPaul Mackerras #include <asm/mmu.h>
419b6b563cSPaul Mackerras #include <asm/pgtable.h>
429b6b563cSPaul Mackerras #include <asm/pci.h>
439b6b563cSPaul Mackerras #include <asm/iommu.h>
449b6b563cSPaul Mackerras #include <asm/bootinfo.h>
459b6b563cSPaul Mackerras #include <asm/btext.h>
469b6b563cSPaul Mackerras #include <asm/sections.h>
479b6b563cSPaul Mackerras #include <asm/machdep.h>
489b6b563cSPaul Mackerras 
499b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
509b6b563cSPaul Mackerras #include <linux/linux_logo.h>
519b6b563cSPaul Mackerras extern const struct linux_logo logo_linux_clut224;
529b6b563cSPaul Mackerras #endif
539b6b563cSPaul Mackerras 
549b6b563cSPaul Mackerras /*
559b6b563cSPaul Mackerras  * Properties whose value is longer than this get excluded from our
569b6b563cSPaul Mackerras  * copy of the device tree. This value does need to be big enough to
579b6b563cSPaul Mackerras  * ensure that we don't lose things like the interrupt-map property
589b6b563cSPaul Mackerras  * on a PCI-PCI bridge.
599b6b563cSPaul Mackerras  */
609b6b563cSPaul Mackerras #define MAX_PROPERTY_LENGTH	(1UL * 1024 * 1024)
619b6b563cSPaul Mackerras 
629b6b563cSPaul Mackerras /*
639b6b563cSPaul Mackerras  * Eventually bump that one up
649b6b563cSPaul Mackerras  */
659b6b563cSPaul Mackerras #define DEVTREE_CHUNK_SIZE	0x100000
669b6b563cSPaul Mackerras 
679b6b563cSPaul Mackerras /*
689b6b563cSPaul Mackerras  * This is the size of the local memory reserve map that gets copied
699b6b563cSPaul Mackerras  * into the boot params passed to the kernel. That size is totally
709b6b563cSPaul Mackerras  * flexible as the kernel just reads the list until it encounters an
719b6b563cSPaul Mackerras  * entry with size 0, so it can be changed without breaking binary
729b6b563cSPaul Mackerras  * compatibility
739b6b563cSPaul Mackerras  */
749b6b563cSPaul Mackerras #define MEM_RESERVE_MAP_SIZE	8
759b6b563cSPaul Mackerras 
769b6b563cSPaul Mackerras /*
779b6b563cSPaul Mackerras  * prom_init() is called very early on, before the kernel text
789b6b563cSPaul Mackerras  * and data have been mapped to KERNELBASE.  At this point the code
799b6b563cSPaul Mackerras  * is running at whatever address it has been loaded at.
809b6b563cSPaul Mackerras  * On ppc32 we compile with -mrelocatable, which means that references
819b6b563cSPaul Mackerras  * to extern and static variables get relocated automatically.
829b6b563cSPaul Mackerras  * On ppc64 we have to relocate the references explicitly with
839b6b563cSPaul Mackerras  * RELOC.  (Note that strings count as static variables.)
849b6b563cSPaul Mackerras  *
859b6b563cSPaul Mackerras  * Because OF may have mapped I/O devices into the area starting at
869b6b563cSPaul Mackerras  * KERNELBASE, particularly on CHRP machines, we can't safely call
879b6b563cSPaul Mackerras  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
889b6b563cSPaul Mackerras  * OF calls must be done within prom_init().
899b6b563cSPaul Mackerras  *
909b6b563cSPaul Mackerras  * ADDR is used in calls to call_prom.  The 4th and following
919b6b563cSPaul Mackerras  * arguments to call_prom should be 32-bit values.
929b6b563cSPaul Mackerras  * On ppc64, 64 bit values are truncated to 32 bits (and
939b6b563cSPaul Mackerras  * fortunately don't get interpreted as two arguments).
949b6b563cSPaul Mackerras  */
959b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
969b6b563cSPaul Mackerras #define RELOC(x)        (*PTRRELOC(&(x)))
979b6b563cSPaul Mackerras #define ADDR(x)		(u32) add_reloc_offset((unsigned long)(x))
989b6b563cSPaul Mackerras #else
999b6b563cSPaul Mackerras #define RELOC(x)	(x)
1009b6b563cSPaul Mackerras #define ADDR(x)		(u32) (x)
1019b6b563cSPaul Mackerras #endif
1029b6b563cSPaul Mackerras 
1039b6b563cSPaul Mackerras #define PROM_BUG() do {						\
1049b6b563cSPaul Mackerras         prom_printf("kernel BUG at %s line 0x%x!\n",		\
1059b6b563cSPaul Mackerras 		    RELOC(__FILE__), __LINE__);			\
1069b6b563cSPaul Mackerras         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
1079b6b563cSPaul Mackerras } while (0)
1089b6b563cSPaul Mackerras 
1099b6b563cSPaul Mackerras #ifdef DEBUG_PROM
1109b6b563cSPaul Mackerras #define prom_debug(x...)	prom_printf(x)
1119b6b563cSPaul Mackerras #else
1129b6b563cSPaul Mackerras #define prom_debug(x...)
1139b6b563cSPaul Mackerras #endif
1149b6b563cSPaul Mackerras 
1159b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
1169b6b563cSPaul Mackerras #define PLATFORM_POWERMAC	_MACH_Pmac
1179b6b563cSPaul Mackerras #define PLATFORM_CHRP		_MACH_chrp
1189b6b563cSPaul Mackerras #endif
1199b6b563cSPaul Mackerras 
1209b6b563cSPaul Mackerras 
1219b6b563cSPaul Mackerras typedef u32 prom_arg_t;
1229b6b563cSPaul Mackerras 
1239b6b563cSPaul Mackerras struct prom_args {
1249b6b563cSPaul Mackerras         u32 service;
1259b6b563cSPaul Mackerras         u32 nargs;
1269b6b563cSPaul Mackerras         u32 nret;
1279b6b563cSPaul Mackerras         prom_arg_t args[10];
1289b6b563cSPaul Mackerras };
1299b6b563cSPaul Mackerras 
1309b6b563cSPaul Mackerras struct prom_t {
1319b6b563cSPaul Mackerras 	ihandle root;
1329b6b563cSPaul Mackerras 	ihandle chosen;
1339b6b563cSPaul Mackerras 	int cpu;
1349b6b563cSPaul Mackerras 	ihandle stdout;
135a575b807SPaul Mackerras 	ihandle mmumap;
1369b6b563cSPaul Mackerras };
1379b6b563cSPaul Mackerras 
1389b6b563cSPaul Mackerras struct mem_map_entry {
1399b6b563cSPaul Mackerras 	unsigned long	base;
1409b6b563cSPaul Mackerras 	unsigned long	size;
1419b6b563cSPaul Mackerras };
1429b6b563cSPaul Mackerras 
1439b6b563cSPaul Mackerras typedef u32 cell_t;
1449b6b563cSPaul Mackerras 
1459b6b563cSPaul Mackerras extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
1469b6b563cSPaul Mackerras 
1479b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
148*c4988820SPaul Mackerras extern int enter_prom(struct prom_args *args, unsigned long entry);
1499b6b563cSPaul Mackerras #else
150*c4988820SPaul Mackerras static inline int enter_prom(struct prom_args *args, unsigned long entry)
1519b6b563cSPaul Mackerras {
152*c4988820SPaul Mackerras 	return ((int (*)(struct prom_args *))entry)(args);
1539b6b563cSPaul Mackerras }
1549b6b563cSPaul Mackerras #endif
1559b6b563cSPaul Mackerras 
1569b6b563cSPaul Mackerras extern void copy_and_flush(unsigned long dest, unsigned long src,
1579b6b563cSPaul Mackerras 			   unsigned long size, unsigned long offset);
1589b6b563cSPaul Mackerras 
1599b6b563cSPaul Mackerras /* prom structure */
1609b6b563cSPaul Mackerras static struct prom_t __initdata prom;
1619b6b563cSPaul Mackerras 
1629b6b563cSPaul Mackerras static unsigned long prom_entry __initdata;
1639b6b563cSPaul Mackerras 
1649b6b563cSPaul Mackerras #define PROM_SCRATCH_SIZE 256
1659b6b563cSPaul Mackerras 
1669b6b563cSPaul Mackerras static char __initdata of_stdout_device[256];
1679b6b563cSPaul Mackerras static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
1689b6b563cSPaul Mackerras 
1699b6b563cSPaul Mackerras static unsigned long __initdata dt_header_start;
1709b6b563cSPaul Mackerras static unsigned long __initdata dt_struct_start, dt_struct_end;
1719b6b563cSPaul Mackerras static unsigned long __initdata dt_string_start, dt_string_end;
1729b6b563cSPaul Mackerras 
1739b6b563cSPaul Mackerras static unsigned long __initdata prom_initrd_start, prom_initrd_end;
1749b6b563cSPaul Mackerras 
1759b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
1769b6b563cSPaul Mackerras static int __initdata iommu_force_on;
1779b6b563cSPaul Mackerras static int __initdata ppc64_iommu_off;
1789b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_start;
1799b6b563cSPaul Mackerras static unsigned long __initdata prom_tce_alloc_end;
1809b6b563cSPaul Mackerras #endif
1819b6b563cSPaul Mackerras 
1829b6b563cSPaul Mackerras static int __initdata of_platform;
1839b6b563cSPaul Mackerras 
1849b6b563cSPaul Mackerras static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
1859b6b563cSPaul Mackerras 
1869b6b563cSPaul Mackerras static unsigned long __initdata prom_memory_limit;
1879b6b563cSPaul Mackerras 
1889b6b563cSPaul Mackerras static unsigned long __initdata alloc_top;
1899b6b563cSPaul Mackerras static unsigned long __initdata alloc_top_high;
1909b6b563cSPaul Mackerras static unsigned long __initdata alloc_bottom;
1919b6b563cSPaul Mackerras static unsigned long __initdata rmo_top;
1929b6b563cSPaul Mackerras static unsigned long __initdata ram_top;
1939b6b563cSPaul Mackerras 
1949b6b563cSPaul Mackerras static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
1959b6b563cSPaul Mackerras static int __initdata mem_reserve_cnt;
1969b6b563cSPaul Mackerras 
1979b6b563cSPaul Mackerras static cell_t __initdata regbuf[1024];
1989b6b563cSPaul Mackerras 
1999b6b563cSPaul Mackerras 
2009b6b563cSPaul Mackerras #define MAX_CPU_THREADS 2
2019b6b563cSPaul Mackerras 
2029b6b563cSPaul Mackerras /* TO GO */
2039b6b563cSPaul Mackerras #ifdef CONFIG_HMT
2049b6b563cSPaul Mackerras struct {
2059b6b563cSPaul Mackerras 	unsigned int pir;
2069b6b563cSPaul Mackerras 	unsigned int threadid;
2079b6b563cSPaul Mackerras } hmt_thread_data[NR_CPUS];
2089b6b563cSPaul Mackerras #endif /* CONFIG_HMT */
2099b6b563cSPaul Mackerras 
2109b6b563cSPaul Mackerras /*
2119b6b563cSPaul Mackerras  * Error results ... some OF calls will return "-1" on error, some
2129b6b563cSPaul Mackerras  * will return 0, some will return either. To simplify, here are
2139b6b563cSPaul Mackerras  * macros to use with any ihandle or phandle return value to check if
2149b6b563cSPaul Mackerras  * it is valid
2159b6b563cSPaul Mackerras  */
2169b6b563cSPaul Mackerras 
2179b6b563cSPaul Mackerras #define PROM_ERROR		(-1u)
2189b6b563cSPaul Mackerras #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
2199b6b563cSPaul Mackerras #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
2209b6b563cSPaul Mackerras 
2219b6b563cSPaul Mackerras 
2229b6b563cSPaul Mackerras /* This is the one and *ONLY* place where we actually call open
2239b6b563cSPaul Mackerras  * firmware.
2249b6b563cSPaul Mackerras  */
2259b6b563cSPaul Mackerras 
2269b6b563cSPaul Mackerras static int __init call_prom(const char *service, int nargs, int nret, ...)
2279b6b563cSPaul Mackerras {
2289b6b563cSPaul Mackerras 	int i;
2299b6b563cSPaul Mackerras 	struct prom_args args;
2309b6b563cSPaul Mackerras 	va_list list;
2319b6b563cSPaul Mackerras 
2329b6b563cSPaul Mackerras 	args.service = ADDR(service);
2339b6b563cSPaul Mackerras 	args.nargs = nargs;
2349b6b563cSPaul Mackerras 	args.nret = nret;
2359b6b563cSPaul Mackerras 
2369b6b563cSPaul Mackerras 	va_start(list, nret);
2379b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
2389b6b563cSPaul Mackerras 		args.args[i] = va_arg(list, prom_arg_t);
2399b6b563cSPaul Mackerras 	va_end(list);
2409b6b563cSPaul Mackerras 
2419b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
2429b6b563cSPaul Mackerras 		args.args[nargs+i] = 0;
2439b6b563cSPaul Mackerras 
244*c4988820SPaul Mackerras 	if (enter_prom(&args, RELOC(prom_entry)) < 0)
245*c4988820SPaul Mackerras 		return PROM_ERROR;
2469b6b563cSPaul Mackerras 
2479b6b563cSPaul Mackerras 	return (nret > 0) ? args.args[nargs] : 0;
2489b6b563cSPaul Mackerras }
2499b6b563cSPaul Mackerras 
2509b6b563cSPaul Mackerras static int __init call_prom_ret(const char *service, int nargs, int nret,
2519b6b563cSPaul Mackerras 				prom_arg_t *rets, ...)
2529b6b563cSPaul Mackerras {
2539b6b563cSPaul Mackerras 	int i;
2549b6b563cSPaul Mackerras 	struct prom_args args;
2559b6b563cSPaul Mackerras 	va_list list;
2569b6b563cSPaul Mackerras 
2579b6b563cSPaul Mackerras 	args.service = ADDR(service);
2589b6b563cSPaul Mackerras 	args.nargs = nargs;
2599b6b563cSPaul Mackerras 	args.nret = nret;
2609b6b563cSPaul Mackerras 
2619b6b563cSPaul Mackerras 	va_start(list, rets);
2629b6b563cSPaul Mackerras 	for (i = 0; i < nargs; i++)
2639b6b563cSPaul Mackerras 		args.args[i] = va_arg(list, prom_arg_t);
2649b6b563cSPaul Mackerras 	va_end(list);
2659b6b563cSPaul Mackerras 
2669b6b563cSPaul Mackerras 	for (i = 0; i < nret; i++)
2679b6b563cSPaul Mackerras 		rets[nargs+i] = 0;
2689b6b563cSPaul Mackerras 
269*c4988820SPaul Mackerras 	if (enter_prom(&args, RELOC(prom_entry)) < 0)
270*c4988820SPaul Mackerras 		return PROM_ERROR;
2719b6b563cSPaul Mackerras 
2729b6b563cSPaul Mackerras 	if (rets != NULL)
2739b6b563cSPaul Mackerras 		for (i = 1; i < nret; ++i)
274c5200c90SPaul Mackerras 			rets[i-1] = args.args[nargs+i];
2759b6b563cSPaul Mackerras 
2769b6b563cSPaul Mackerras 	return (nret > 0) ? args.args[nargs] : 0;
2779b6b563cSPaul Mackerras }
2789b6b563cSPaul Mackerras 
2799b6b563cSPaul Mackerras 
2809b6b563cSPaul Mackerras static void __init prom_print(const char *msg)
2819b6b563cSPaul Mackerras {
2829b6b563cSPaul Mackerras 	const char *p, *q;
2839b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
2849b6b563cSPaul Mackerras 
2859b6b563cSPaul Mackerras 	if (_prom->stdout == 0)
2869b6b563cSPaul Mackerras 		return;
2879b6b563cSPaul Mackerras 
2889b6b563cSPaul Mackerras 	for (p = msg; *p != 0; p = q) {
2899b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n'; ++q)
2909b6b563cSPaul Mackerras 			;
2919b6b563cSPaul Mackerras 		if (q > p)
2929b6b563cSPaul Mackerras 			call_prom("write", 3, 1, _prom->stdout, p, q - p);
2939b6b563cSPaul Mackerras 		if (*q == 0)
2949b6b563cSPaul Mackerras 			break;
2959b6b563cSPaul Mackerras 		++q;
2969b6b563cSPaul Mackerras 		call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
2979b6b563cSPaul Mackerras 	}
2989b6b563cSPaul Mackerras }
2999b6b563cSPaul Mackerras 
3009b6b563cSPaul Mackerras 
3019b6b563cSPaul Mackerras static void __init prom_print_hex(unsigned long val)
3029b6b563cSPaul Mackerras {
3039b6b563cSPaul Mackerras 	int i, nibbles = sizeof(val)*2;
3049b6b563cSPaul Mackerras 	char buf[sizeof(val)*2+1];
3059b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
3069b6b563cSPaul Mackerras 
3079b6b563cSPaul Mackerras 	for (i = nibbles-1;  i >= 0;  i--) {
3089b6b563cSPaul Mackerras 		buf[i] = (val & 0xf) + '0';
3099b6b563cSPaul Mackerras 		if (buf[i] > '9')
3109b6b563cSPaul Mackerras 			buf[i] += ('a'-'0'-10);
3119b6b563cSPaul Mackerras 		val >>= 4;
3129b6b563cSPaul Mackerras 	}
3139b6b563cSPaul Mackerras 	buf[nibbles] = '\0';
3149b6b563cSPaul Mackerras 	call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
3159b6b563cSPaul Mackerras }
3169b6b563cSPaul Mackerras 
3179b6b563cSPaul Mackerras 
3189b6b563cSPaul Mackerras static void __init prom_printf(const char *format, ...)
3199b6b563cSPaul Mackerras {
3209b6b563cSPaul Mackerras 	const char *p, *q, *s;
3219b6b563cSPaul Mackerras 	va_list args;
3229b6b563cSPaul Mackerras 	unsigned long v;
3239b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
3249b6b563cSPaul Mackerras 
3259b6b563cSPaul Mackerras 	va_start(args, format);
3269b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
3279b6b563cSPaul Mackerras 	format = PTRRELOC(format);
3289b6b563cSPaul Mackerras #endif
3299b6b563cSPaul Mackerras 	for (p = format; *p != 0; p = q) {
3309b6b563cSPaul Mackerras 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
3319b6b563cSPaul Mackerras 			;
3329b6b563cSPaul Mackerras 		if (q > p)
3339b6b563cSPaul Mackerras 			call_prom("write", 3, 1, _prom->stdout, p, q - p);
3349b6b563cSPaul Mackerras 		if (*q == 0)
3359b6b563cSPaul Mackerras 			break;
3369b6b563cSPaul Mackerras 		if (*q == '\n') {
3379b6b563cSPaul Mackerras 			++q;
3389b6b563cSPaul Mackerras 			call_prom("write", 3, 1, _prom->stdout,
3399b6b563cSPaul Mackerras 				  ADDR("\r\n"), 2);
3409b6b563cSPaul Mackerras 			continue;
3419b6b563cSPaul Mackerras 		}
3429b6b563cSPaul Mackerras 		++q;
3439b6b563cSPaul Mackerras 		if (*q == 0)
3449b6b563cSPaul Mackerras 			break;
3459b6b563cSPaul Mackerras 		switch (*q) {
3469b6b563cSPaul Mackerras 		case 's':
3479b6b563cSPaul Mackerras 			++q;
3489b6b563cSPaul Mackerras 			s = va_arg(args, const char *);
3499b6b563cSPaul Mackerras 			prom_print(s);
3509b6b563cSPaul Mackerras 			break;
3519b6b563cSPaul Mackerras 		case 'x':
3529b6b563cSPaul Mackerras 			++q;
3539b6b563cSPaul Mackerras 			v = va_arg(args, unsigned long);
3549b6b563cSPaul Mackerras 			prom_print_hex(v);
3559b6b563cSPaul Mackerras 			break;
3569b6b563cSPaul Mackerras 		}
3579b6b563cSPaul Mackerras 	}
3589b6b563cSPaul Mackerras }
3599b6b563cSPaul Mackerras 
3609b6b563cSPaul Mackerras 
361a575b807SPaul Mackerras static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
362a575b807SPaul Mackerras 				unsigned long align)
363a575b807SPaul Mackerras {
364a575b807SPaul Mackerras 	int ret;
365a575b807SPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
366a575b807SPaul Mackerras 
367a575b807SPaul Mackerras 	ret = call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
368a575b807SPaul Mackerras 			(prom_arg_t)align);
369a575b807SPaul Mackerras 	if (ret != -1 && _prom->mmumap != 0)
370a575b807SPaul Mackerras 		/* old pmacs need us to map as well */
371a575b807SPaul Mackerras 		call_prom("call-method", 6, 1,
372a575b807SPaul Mackerras 			  ADDR("map"), _prom->mmumap, 0, size, virt, virt);
373a575b807SPaul Mackerras 	return ret;
374a575b807SPaul Mackerras }
375a575b807SPaul Mackerras 
3769b6b563cSPaul Mackerras static void __init __attribute__((noreturn)) prom_panic(const char *reason)
3779b6b563cSPaul Mackerras {
3789b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
3799b6b563cSPaul Mackerras 	reason = PTRRELOC(reason);
3809b6b563cSPaul Mackerras #endif
3819b6b563cSPaul Mackerras 	prom_print(reason);
3829b6b563cSPaul Mackerras 	/* ToDo: should put up an SRC here on p/iSeries */
3839b6b563cSPaul Mackerras 	call_prom("exit", 0, 0);
3849b6b563cSPaul Mackerras 
3859b6b563cSPaul Mackerras 	for (;;)			/* should never get here */
3869b6b563cSPaul Mackerras 		;
3879b6b563cSPaul Mackerras }
3889b6b563cSPaul Mackerras 
3899b6b563cSPaul Mackerras 
3909b6b563cSPaul Mackerras static int __init prom_next_node(phandle *nodep)
3919b6b563cSPaul Mackerras {
3929b6b563cSPaul Mackerras 	phandle node;
3939b6b563cSPaul Mackerras 
3949b6b563cSPaul Mackerras 	if ((node = *nodep) != 0
3959b6b563cSPaul Mackerras 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
3969b6b563cSPaul Mackerras 		return 1;
3979b6b563cSPaul Mackerras 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
3989b6b563cSPaul Mackerras 		return 1;
3999b6b563cSPaul Mackerras 	for (;;) {
4009b6b563cSPaul Mackerras 		if ((node = call_prom("parent", 1, 1, node)) == 0)
4019b6b563cSPaul Mackerras 			return 0;
4029b6b563cSPaul Mackerras 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
4039b6b563cSPaul Mackerras 			return 1;
4049b6b563cSPaul Mackerras 	}
4059b6b563cSPaul Mackerras }
4069b6b563cSPaul Mackerras 
4079b6b563cSPaul Mackerras static int __init prom_getprop(phandle node, const char *pname,
4089b6b563cSPaul Mackerras 			       void *value, size_t valuelen)
4099b6b563cSPaul Mackerras {
4109b6b563cSPaul Mackerras 	return call_prom("getprop", 4, 1, node, ADDR(pname),
4119b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
4129b6b563cSPaul Mackerras }
4139b6b563cSPaul Mackerras 
4149b6b563cSPaul Mackerras static int __init prom_getproplen(phandle node, const char *pname)
4159b6b563cSPaul Mackerras {
4169b6b563cSPaul Mackerras 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
4179b6b563cSPaul Mackerras }
4189b6b563cSPaul Mackerras 
4199b6b563cSPaul Mackerras static int __init prom_setprop(phandle node, const char *pname,
4209b6b563cSPaul Mackerras 			       void *value, size_t valuelen)
4219b6b563cSPaul Mackerras {
4229b6b563cSPaul Mackerras 	return call_prom("setprop", 4, 1, node, ADDR(pname),
4239b6b563cSPaul Mackerras 			 (u32)(unsigned long) value, (u32) valuelen);
4249b6b563cSPaul Mackerras }
4259b6b563cSPaul Mackerras 
4269b6b563cSPaul Mackerras /* We can't use the standard versions because of RELOC headaches. */
4279b6b563cSPaul Mackerras #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
4289b6b563cSPaul Mackerras 			 || ('a' <= (c) && (c) <= 'f') \
4299b6b563cSPaul Mackerras 			 || ('A' <= (c) && (c) <= 'F'))
4309b6b563cSPaul Mackerras 
4319b6b563cSPaul Mackerras #define isdigit(c)	('0' <= (c) && (c) <= '9')
4329b6b563cSPaul Mackerras #define islower(c)	('a' <= (c) && (c) <= 'z')
4339b6b563cSPaul Mackerras #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
4349b6b563cSPaul Mackerras 
4359b6b563cSPaul Mackerras unsigned long prom_strtoul(const char *cp, const char **endp)
4369b6b563cSPaul Mackerras {
4379b6b563cSPaul Mackerras 	unsigned long result = 0, base = 10, value;
4389b6b563cSPaul Mackerras 
4399b6b563cSPaul Mackerras 	if (*cp == '0') {
4409b6b563cSPaul Mackerras 		base = 8;
4419b6b563cSPaul Mackerras 		cp++;
4429b6b563cSPaul Mackerras 		if (toupper(*cp) == 'X') {
4439b6b563cSPaul Mackerras 			cp++;
4449b6b563cSPaul Mackerras 			base = 16;
4459b6b563cSPaul Mackerras 		}
4469b6b563cSPaul Mackerras 	}
4479b6b563cSPaul Mackerras 
4489b6b563cSPaul Mackerras 	while (isxdigit(*cp) &&
4499b6b563cSPaul Mackerras 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
4509b6b563cSPaul Mackerras 		result = result * base + value;
4519b6b563cSPaul Mackerras 		cp++;
4529b6b563cSPaul Mackerras 	}
4539b6b563cSPaul Mackerras 
4549b6b563cSPaul Mackerras 	if (endp)
4559b6b563cSPaul Mackerras 		*endp = cp;
4569b6b563cSPaul Mackerras 
4579b6b563cSPaul Mackerras 	return result;
4589b6b563cSPaul Mackerras }
4599b6b563cSPaul Mackerras 
4609b6b563cSPaul Mackerras unsigned long prom_memparse(const char *ptr, const char **retptr)
4619b6b563cSPaul Mackerras {
4629b6b563cSPaul Mackerras 	unsigned long ret = prom_strtoul(ptr, retptr);
4639b6b563cSPaul Mackerras 	int shift = 0;
4649b6b563cSPaul Mackerras 
4659b6b563cSPaul Mackerras 	/*
4669b6b563cSPaul Mackerras 	 * We can't use a switch here because GCC *may* generate a
4679b6b563cSPaul Mackerras 	 * jump table which won't work, because we're not running at
4689b6b563cSPaul Mackerras 	 * the address we're linked at.
4699b6b563cSPaul Mackerras 	 */
4709b6b563cSPaul Mackerras 	if ('G' == **retptr || 'g' == **retptr)
4719b6b563cSPaul Mackerras 		shift = 30;
4729b6b563cSPaul Mackerras 
4739b6b563cSPaul Mackerras 	if ('M' == **retptr || 'm' == **retptr)
4749b6b563cSPaul Mackerras 		shift = 20;
4759b6b563cSPaul Mackerras 
4769b6b563cSPaul Mackerras 	if ('K' == **retptr || 'k' == **retptr)
4779b6b563cSPaul Mackerras 		shift = 10;
4789b6b563cSPaul Mackerras 
4799b6b563cSPaul Mackerras 	if (shift) {
4809b6b563cSPaul Mackerras 		ret <<= shift;
4819b6b563cSPaul Mackerras 		(*retptr)++;
4829b6b563cSPaul Mackerras 	}
4839b6b563cSPaul Mackerras 
4849b6b563cSPaul Mackerras 	return ret;
4859b6b563cSPaul Mackerras }
4869b6b563cSPaul Mackerras 
4879b6b563cSPaul Mackerras /*
4889b6b563cSPaul Mackerras  * Early parsing of the command line passed to the kernel, used for
4899b6b563cSPaul Mackerras  * "mem=x" and the options that affect the iommu
4909b6b563cSPaul Mackerras  */
4919b6b563cSPaul Mackerras static void __init early_cmdline_parse(void)
4929b6b563cSPaul Mackerras {
4939b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
4949b6b563cSPaul Mackerras 	char *opt, *p;
4959b6b563cSPaul Mackerras 	int l = 0;
4969b6b563cSPaul Mackerras 
4979b6b563cSPaul Mackerras 	RELOC(prom_cmd_line[0]) = 0;
4989b6b563cSPaul Mackerras 	p = RELOC(prom_cmd_line);
4999b6b563cSPaul Mackerras 	if ((long)_prom->chosen > 0)
5009b6b563cSPaul Mackerras 		l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
5019b6b563cSPaul Mackerras #ifdef CONFIG_CMDLINE
5029b6b563cSPaul Mackerras 	if (l == 0) /* dbl check */
5039b6b563cSPaul Mackerras 		strlcpy(RELOC(prom_cmd_line),
5049b6b563cSPaul Mackerras 			RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
5059b6b563cSPaul Mackerras #endif /* CONFIG_CMDLINE */
5069b6b563cSPaul Mackerras 	prom_printf("command line: %s\n", RELOC(prom_cmd_line));
5079b6b563cSPaul Mackerras 
5089b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
5099b6b563cSPaul Mackerras 	opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
5109b6b563cSPaul Mackerras 	if (opt) {
5119b6b563cSPaul Mackerras 		prom_printf("iommu opt is: %s\n", opt);
5129b6b563cSPaul Mackerras 		opt += 6;
5139b6b563cSPaul Mackerras 		while (*opt && *opt == ' ')
5149b6b563cSPaul Mackerras 			opt++;
5159b6b563cSPaul Mackerras 		if (!strncmp(opt, RELOC("off"), 3))
5169b6b563cSPaul Mackerras 			RELOC(ppc64_iommu_off) = 1;
5179b6b563cSPaul Mackerras 		else if (!strncmp(opt, RELOC("force"), 5))
5189b6b563cSPaul Mackerras 			RELOC(iommu_force_on) = 1;
5199b6b563cSPaul Mackerras 	}
5209b6b563cSPaul Mackerras #endif
5219b6b563cSPaul Mackerras 
5229b6b563cSPaul Mackerras 	opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
5239b6b563cSPaul Mackerras 	if (opt) {
5249b6b563cSPaul Mackerras 		opt += 4;
5259b6b563cSPaul Mackerras 		RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
5269b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
5279b6b563cSPaul Mackerras 		/* Align to 16 MB == size of ppc64 large page */
5289b6b563cSPaul Mackerras 		RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
5299b6b563cSPaul Mackerras #endif
5309b6b563cSPaul Mackerras 	}
5319b6b563cSPaul Mackerras }
5329b6b563cSPaul Mackerras 
5339b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
5349b6b563cSPaul Mackerras /*
5359b6b563cSPaul Mackerras  * To tell the firmware what our capabilities are, we have to pass
5369b6b563cSPaul Mackerras  * it a fake 32-bit ELF header containing a couple of PT_NOTE sections
5379b6b563cSPaul Mackerras  * that contain structures that contain the actual values.
5389b6b563cSPaul Mackerras  */
5399b6b563cSPaul Mackerras static struct fake_elf {
5409b6b563cSPaul Mackerras 	Elf32_Ehdr	elfhdr;
5419b6b563cSPaul Mackerras 	Elf32_Phdr	phdr[2];
5429b6b563cSPaul Mackerras 	struct chrpnote {
5439b6b563cSPaul Mackerras 		u32	namesz;
5449b6b563cSPaul Mackerras 		u32	descsz;
5459b6b563cSPaul Mackerras 		u32	type;
5469b6b563cSPaul Mackerras 		char	name[8];	/* "PowerPC" */
5479b6b563cSPaul Mackerras 		struct chrpdesc {
5489b6b563cSPaul Mackerras 			u32	real_mode;
5499b6b563cSPaul Mackerras 			u32	real_base;
5509b6b563cSPaul Mackerras 			u32	real_size;
5519b6b563cSPaul Mackerras 			u32	virt_base;
5529b6b563cSPaul Mackerras 			u32	virt_size;
5539b6b563cSPaul Mackerras 			u32	load_base;
5549b6b563cSPaul Mackerras 		} chrpdesc;
5559b6b563cSPaul Mackerras 	} chrpnote;
5569b6b563cSPaul Mackerras 	struct rpanote {
5579b6b563cSPaul Mackerras 		u32	namesz;
5589b6b563cSPaul Mackerras 		u32	descsz;
5599b6b563cSPaul Mackerras 		u32	type;
5609b6b563cSPaul Mackerras 		char	name[24];	/* "IBM,RPA-Client-Config" */
5619b6b563cSPaul Mackerras 		struct rpadesc {
5629b6b563cSPaul Mackerras 			u32	lpar_affinity;
5639b6b563cSPaul Mackerras 			u32	min_rmo_size;
5649b6b563cSPaul Mackerras 			u32	min_rmo_percent;
5659b6b563cSPaul Mackerras 			u32	max_pft_size;
5669b6b563cSPaul Mackerras 			u32	splpar;
5679b6b563cSPaul Mackerras 			u32	min_load;
5689b6b563cSPaul Mackerras 			u32	new_mem_def;
5699b6b563cSPaul Mackerras 			u32	ignore_me;
5709b6b563cSPaul Mackerras 		} rpadesc;
5719b6b563cSPaul Mackerras 	} rpanote;
5729b6b563cSPaul Mackerras } fake_elf = {
5739b6b563cSPaul Mackerras 	.elfhdr = {
5749b6b563cSPaul Mackerras 		.e_ident = { 0x7f, 'E', 'L', 'F',
5759b6b563cSPaul Mackerras 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
5769b6b563cSPaul Mackerras 		.e_type = ET_EXEC,	/* yeah right */
5779b6b563cSPaul Mackerras 		.e_machine = EM_PPC,
5789b6b563cSPaul Mackerras 		.e_version = EV_CURRENT,
5799b6b563cSPaul Mackerras 		.e_phoff = offsetof(struct fake_elf, phdr),
5809b6b563cSPaul Mackerras 		.e_phentsize = sizeof(Elf32_Phdr),
5819b6b563cSPaul Mackerras 		.e_phnum = 2
5829b6b563cSPaul Mackerras 	},
5839b6b563cSPaul Mackerras 	.phdr = {
5849b6b563cSPaul Mackerras 		[0] = {
5859b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
5869b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, chrpnote),
5879b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct chrpnote)
5889b6b563cSPaul Mackerras 		}, [1] = {
5899b6b563cSPaul Mackerras 			.p_type = PT_NOTE,
5909b6b563cSPaul Mackerras 			.p_offset = offsetof(struct fake_elf, rpanote),
5919b6b563cSPaul Mackerras 			.p_filesz = sizeof(struct rpanote)
5929b6b563cSPaul Mackerras 		}
5939b6b563cSPaul Mackerras 	},
5949b6b563cSPaul Mackerras 	.chrpnote = {
5959b6b563cSPaul Mackerras 		.namesz = sizeof("PowerPC"),
5969b6b563cSPaul Mackerras 		.descsz = sizeof(struct chrpdesc),
5979b6b563cSPaul Mackerras 		.type = 0x1275,
5989b6b563cSPaul Mackerras 		.name = "PowerPC",
5999b6b563cSPaul Mackerras 		.chrpdesc = {
6009b6b563cSPaul Mackerras 			.real_mode = ~0U,	/* ~0 means "don't care" */
6019b6b563cSPaul Mackerras 			.real_base = ~0U,
6029b6b563cSPaul Mackerras 			.real_size = ~0U,
6039b6b563cSPaul Mackerras 			.virt_base = ~0U,
6049b6b563cSPaul Mackerras 			.virt_size = ~0U,
6059b6b563cSPaul Mackerras 			.load_base = ~0U
6069b6b563cSPaul Mackerras 		},
6079b6b563cSPaul Mackerras 	},
6089b6b563cSPaul Mackerras 	.rpanote = {
6099b6b563cSPaul Mackerras 		.namesz = sizeof("IBM,RPA-Client-Config"),
6109b6b563cSPaul Mackerras 		.descsz = sizeof(struct rpadesc),
6119b6b563cSPaul Mackerras 		.type = 0x12759999,
6129b6b563cSPaul Mackerras 		.name = "IBM,RPA-Client-Config",
6139b6b563cSPaul Mackerras 		.rpadesc = {
6149b6b563cSPaul Mackerras 			.lpar_affinity = 0,
6159b6b563cSPaul Mackerras 			.min_rmo_size = 64,	/* in megabytes */
6169b6b563cSPaul Mackerras 			.min_rmo_percent = 0,
6179b6b563cSPaul Mackerras 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
6189b6b563cSPaul Mackerras 			.splpar = 1,
6199b6b563cSPaul Mackerras 			.min_load = ~0U,
6209b6b563cSPaul Mackerras 			.new_mem_def = 0
6219b6b563cSPaul Mackerras 		}
6229b6b563cSPaul Mackerras 	}
6239b6b563cSPaul Mackerras };
6249b6b563cSPaul Mackerras 
6259b6b563cSPaul Mackerras static void __init prom_send_capabilities(void)
6269b6b563cSPaul Mackerras {
6279b6b563cSPaul Mackerras 	ihandle elfloader;
6289b6b563cSPaul Mackerras 
6299b6b563cSPaul Mackerras 	elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
6309b6b563cSPaul Mackerras 	if (elfloader == 0) {
6319b6b563cSPaul Mackerras 		prom_printf("couldn't open /packages/elf-loader\n");
6329b6b563cSPaul Mackerras 		return;
6339b6b563cSPaul Mackerras 	}
6349b6b563cSPaul Mackerras 	call_prom("call-method", 3, 1, ADDR("process-elf-header"),
6359b6b563cSPaul Mackerras 			elfloader, ADDR(&fake_elf));
6369b6b563cSPaul Mackerras 	call_prom("close", 1, 0, elfloader);
6379b6b563cSPaul Mackerras }
6389b6b563cSPaul Mackerras #endif
6399b6b563cSPaul Mackerras 
6409b6b563cSPaul Mackerras /*
6419b6b563cSPaul Mackerras  * Memory allocation strategy... our layout is normally:
6429b6b563cSPaul Mackerras  *
6439b6b563cSPaul Mackerras  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
6449b6b563cSPaul Mackerras  *  rare cases, initrd might end up being before the kernel though.
6459b6b563cSPaul Mackerras  *  We assume this won't override the final kernel at 0, we have no
6469b6b563cSPaul Mackerras  *  provision to handle that in this version, but it should hopefully
6479b6b563cSPaul Mackerras  *  never happen.
6489b6b563cSPaul Mackerras  *
6499b6b563cSPaul Mackerras  *  alloc_top is set to the top of RMO, eventually shrink down if the
6509b6b563cSPaul Mackerras  *  TCEs overlap
6519b6b563cSPaul Mackerras  *
6529b6b563cSPaul Mackerras  *  alloc_bottom is set to the top of kernel/initrd
6539b6b563cSPaul Mackerras  *
6549b6b563cSPaul Mackerras  *  from there, allocations are done this way : rtas is allocated
6559b6b563cSPaul Mackerras  *  topmost, and the device-tree is allocated from the bottom. We try
6569b6b563cSPaul Mackerras  *  to grow the device-tree allocation as we progress. If we can't,
6579b6b563cSPaul Mackerras  *  then we fail, we don't currently have a facility to restart
6589b6b563cSPaul Mackerras  *  elsewhere, but that shouldn't be necessary.
6599b6b563cSPaul Mackerras  *
6609b6b563cSPaul Mackerras  *  Note that calls to reserve_mem have to be done explicitly, memory
6619b6b563cSPaul Mackerras  *  allocated with either alloc_up or alloc_down isn't automatically
6629b6b563cSPaul Mackerras  *  reserved.
6639b6b563cSPaul Mackerras  */
6649b6b563cSPaul Mackerras 
6659b6b563cSPaul Mackerras 
6669b6b563cSPaul Mackerras /*
6679b6b563cSPaul Mackerras  * Allocates memory in the RMO upward from the kernel/initrd
6689b6b563cSPaul Mackerras  *
6699b6b563cSPaul Mackerras  * When align is 0, this is a special case, it means to allocate in place
6709b6b563cSPaul Mackerras  * at the current location of alloc_bottom or fail (that is basically
6719b6b563cSPaul Mackerras  * extending the previous allocation). Used for the device-tree flattening
6729b6b563cSPaul Mackerras  */
6739b6b563cSPaul Mackerras static unsigned long __init alloc_up(unsigned long size, unsigned long align)
6749b6b563cSPaul Mackerras {
675*c4988820SPaul Mackerras 	unsigned long base = RELOC(alloc_bottom);
6769b6b563cSPaul Mackerras 	unsigned long addr = 0;
6779b6b563cSPaul Mackerras 
678*c4988820SPaul Mackerras 	if (align)
679*c4988820SPaul Mackerras 		base = _ALIGN_UP(base, align);
6809b6b563cSPaul Mackerras 	prom_debug("alloc_up(%x, %x)\n", size, align);
6819b6b563cSPaul Mackerras 	if (RELOC(ram_top) == 0)
6829b6b563cSPaul Mackerras 		prom_panic("alloc_up() called with mem not initialized\n");
6839b6b563cSPaul Mackerras 
6849b6b563cSPaul Mackerras 	if (align)
6859b6b563cSPaul Mackerras 		base = _ALIGN_UP(RELOC(alloc_bottom), align);
6869b6b563cSPaul Mackerras 	else
6879b6b563cSPaul Mackerras 		base = RELOC(alloc_bottom);
6889b6b563cSPaul Mackerras 
6899b6b563cSPaul Mackerras 	for(; (base + size) <= RELOC(alloc_top);
6909b6b563cSPaul Mackerras 	    base = _ALIGN_UP(base + 0x100000, align)) {
6919b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
6929b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
693*c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
6949b6b563cSPaul Mackerras 			break;
6959b6b563cSPaul Mackerras 		addr = 0;
6969b6b563cSPaul Mackerras 		if (align == 0)
6979b6b563cSPaul Mackerras 			break;
6989b6b563cSPaul Mackerras 	}
6999b6b563cSPaul Mackerras 	if (addr == 0)
7009b6b563cSPaul Mackerras 		return 0;
7019b6b563cSPaul Mackerras 	RELOC(alloc_bottom) = addr;
7029b6b563cSPaul Mackerras 
7039b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
7049b6b563cSPaul Mackerras 	prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
7059b6b563cSPaul Mackerras 	prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
7069b6b563cSPaul Mackerras 	prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
7079b6b563cSPaul Mackerras 	prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
7089b6b563cSPaul Mackerras 	prom_debug("  ram_top      : %x\n", RELOC(ram_top));
7099b6b563cSPaul Mackerras 
7109b6b563cSPaul Mackerras 	return addr;
7119b6b563cSPaul Mackerras }
7129b6b563cSPaul Mackerras 
7139b6b563cSPaul Mackerras /*
7149b6b563cSPaul Mackerras  * Allocates memory downward, either from top of RMO, or if highmem
7159b6b563cSPaul Mackerras  * is set, from the top of RAM.  Note that this one doesn't handle
7169b6b563cSPaul Mackerras  * failures.  It does claim memory if highmem is not set.
7179b6b563cSPaul Mackerras  */
7189b6b563cSPaul Mackerras static unsigned long __init alloc_down(unsigned long size, unsigned long align,
7199b6b563cSPaul Mackerras 				       int highmem)
7209b6b563cSPaul Mackerras {
7219b6b563cSPaul Mackerras 	unsigned long base, addr = 0;
7229b6b563cSPaul Mackerras 
7239b6b563cSPaul Mackerras 	prom_debug("alloc_down(%x, %x, %s)\n", size, align,
7249b6b563cSPaul Mackerras 		   highmem ? RELOC("(high)") : RELOC("(low)"));
7259b6b563cSPaul Mackerras 	if (RELOC(ram_top) == 0)
7269b6b563cSPaul Mackerras 		prom_panic("alloc_down() called with mem not initialized\n");
7279b6b563cSPaul Mackerras 
7289b6b563cSPaul Mackerras 	if (highmem) {
7299b6b563cSPaul Mackerras 		/* Carve out storage for the TCE table. */
7309b6b563cSPaul Mackerras 		addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
7319b6b563cSPaul Mackerras 		if (addr <= RELOC(alloc_bottom))
7329b6b563cSPaul Mackerras 			return 0;
7339b6b563cSPaul Mackerras 		/* Will we bump into the RMO ? If yes, check out that we
7349b6b563cSPaul Mackerras 		 * didn't overlap existing allocations there, if we did,
7359b6b563cSPaul Mackerras 		 * we are dead, we must be the first in town !
7369b6b563cSPaul Mackerras 		 */
7379b6b563cSPaul Mackerras 		if (addr < RELOC(rmo_top)) {
7389b6b563cSPaul Mackerras 			/* Good, we are first */
7399b6b563cSPaul Mackerras 			if (RELOC(alloc_top) == RELOC(rmo_top))
7409b6b563cSPaul Mackerras 				RELOC(alloc_top) = RELOC(rmo_top) = addr;
7419b6b563cSPaul Mackerras 			else
7429b6b563cSPaul Mackerras 				return 0;
7439b6b563cSPaul Mackerras 		}
7449b6b563cSPaul Mackerras 		RELOC(alloc_top_high) = addr;
7459b6b563cSPaul Mackerras 		goto bail;
7469b6b563cSPaul Mackerras 	}
7479b6b563cSPaul Mackerras 
7489b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
7499b6b563cSPaul Mackerras 	for (; base > RELOC(alloc_bottom);
7509b6b563cSPaul Mackerras 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
7519b6b563cSPaul Mackerras 		prom_debug("    trying: 0x%x\n\r", base);
7529b6b563cSPaul Mackerras 		addr = (unsigned long)prom_claim(base, size, 0);
753*c4988820SPaul Mackerras 		if (addr != PROM_ERROR && addr != 0)
7549b6b563cSPaul Mackerras 			break;
7559b6b563cSPaul Mackerras 		addr = 0;
7569b6b563cSPaul Mackerras 	}
7579b6b563cSPaul Mackerras 	if (addr == 0)
7589b6b563cSPaul Mackerras 		return 0;
7599b6b563cSPaul Mackerras 	RELOC(alloc_top) = addr;
7609b6b563cSPaul Mackerras 
7619b6b563cSPaul Mackerras  bail:
7629b6b563cSPaul Mackerras 	prom_debug(" -> %x\n", addr);
7639b6b563cSPaul Mackerras 	prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
7649b6b563cSPaul Mackerras 	prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
7659b6b563cSPaul Mackerras 	prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
7669b6b563cSPaul Mackerras 	prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
7679b6b563cSPaul Mackerras 	prom_debug("  ram_top      : %x\n", RELOC(ram_top));
7689b6b563cSPaul Mackerras 
7699b6b563cSPaul Mackerras 	return addr;
7709b6b563cSPaul Mackerras }
7719b6b563cSPaul Mackerras 
7729b6b563cSPaul Mackerras /*
7739b6b563cSPaul Mackerras  * Parse a "reg" cell
7749b6b563cSPaul Mackerras  */
7759b6b563cSPaul Mackerras static unsigned long __init prom_next_cell(int s, cell_t **cellp)
7769b6b563cSPaul Mackerras {
7779b6b563cSPaul Mackerras 	cell_t *p = *cellp;
7789b6b563cSPaul Mackerras 	unsigned long r = 0;
7799b6b563cSPaul Mackerras 
7809b6b563cSPaul Mackerras 	/* Ignore more than 2 cells */
7819b6b563cSPaul Mackerras 	while (s > sizeof(unsigned long) / 4) {
7829b6b563cSPaul Mackerras 		p++;
7839b6b563cSPaul Mackerras 		s--;
7849b6b563cSPaul Mackerras 	}
7859b6b563cSPaul Mackerras 	r = *p++;
7869b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
78735499c01SPaul Mackerras 	if (s > 1) {
7889b6b563cSPaul Mackerras 		r <<= 32;
7899b6b563cSPaul Mackerras 		r |= *(p++);
7909b6b563cSPaul Mackerras 	}
7919b6b563cSPaul Mackerras #endif
7929b6b563cSPaul Mackerras 	*cellp = p;
7939b6b563cSPaul Mackerras 	return r;
7949b6b563cSPaul Mackerras }
7959b6b563cSPaul Mackerras 
7969b6b563cSPaul Mackerras /*
7979b6b563cSPaul Mackerras  * Very dumb function for adding to the memory reserve list, but
7989b6b563cSPaul Mackerras  * we don't need anything smarter at this point
7999b6b563cSPaul Mackerras  *
8009b6b563cSPaul Mackerras  * XXX Eventually check for collisions.  They should NEVER happen.
8019b6b563cSPaul Mackerras  * If problems seem to show up, it would be a good start to track
8029b6b563cSPaul Mackerras  * them down.
8039b6b563cSPaul Mackerras  */
8049b6b563cSPaul Mackerras static void reserve_mem(unsigned long base, unsigned long size)
8059b6b563cSPaul Mackerras {
8069b6b563cSPaul Mackerras 	unsigned long top = base + size;
8079b6b563cSPaul Mackerras 	unsigned long cnt = RELOC(mem_reserve_cnt);
8089b6b563cSPaul Mackerras 
8099b6b563cSPaul Mackerras 	if (size == 0)
8109b6b563cSPaul Mackerras 		return;
8119b6b563cSPaul Mackerras 
8129b6b563cSPaul Mackerras 	/* We need to always keep one empty entry so that we
8139b6b563cSPaul Mackerras 	 * have our terminator with "size" set to 0 since we are
8149b6b563cSPaul Mackerras 	 * dumb and just copy this entire array to the boot params
8159b6b563cSPaul Mackerras 	 */
8169b6b563cSPaul Mackerras 	base = _ALIGN_DOWN(base, PAGE_SIZE);
8179b6b563cSPaul Mackerras 	top = _ALIGN_UP(top, PAGE_SIZE);
8189b6b563cSPaul Mackerras 	size = top - base;
8199b6b563cSPaul Mackerras 
8209b6b563cSPaul Mackerras 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
8219b6b563cSPaul Mackerras 		prom_panic("Memory reserve map exhausted !\n");
8229b6b563cSPaul Mackerras 	RELOC(mem_reserve_map)[cnt].base = base;
8239b6b563cSPaul Mackerras 	RELOC(mem_reserve_map)[cnt].size = size;
8249b6b563cSPaul Mackerras 	RELOC(mem_reserve_cnt) = cnt + 1;
8259b6b563cSPaul Mackerras }
8269b6b563cSPaul Mackerras 
8279b6b563cSPaul Mackerras /*
8289b6b563cSPaul Mackerras  * Initialize memory allocation mecanism, parse "memory" nodes and
8299b6b563cSPaul Mackerras  * obtain that way the top of memory and RMO to setup out local allocator
8309b6b563cSPaul Mackerras  */
8319b6b563cSPaul Mackerras static void __init prom_init_mem(void)
8329b6b563cSPaul Mackerras {
8339b6b563cSPaul Mackerras 	phandle node;
8349b6b563cSPaul Mackerras 	char *path, type[64];
8359b6b563cSPaul Mackerras 	unsigned int plen;
8369b6b563cSPaul Mackerras 	cell_t *p, *endp;
8379b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
8389b6b563cSPaul Mackerras 	u32 rac, rsc;
8399b6b563cSPaul Mackerras 
8409b6b563cSPaul Mackerras 	/*
8419b6b563cSPaul Mackerras 	 * We iterate the memory nodes to find
8429b6b563cSPaul Mackerras 	 * 1) top of RMO (first node)
8439b6b563cSPaul Mackerras 	 * 2) top of memory
8449b6b563cSPaul Mackerras 	 */
8459b6b563cSPaul Mackerras 	rac = 2;
8469b6b563cSPaul Mackerras 	prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
8479b6b563cSPaul Mackerras 	rsc = 1;
8489b6b563cSPaul Mackerras 	prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
8499b6b563cSPaul Mackerras 	prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
8509b6b563cSPaul Mackerras 	prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
8519b6b563cSPaul Mackerras 
8529b6b563cSPaul Mackerras 	prom_debug("scanning memory:\n");
8539b6b563cSPaul Mackerras 	path = RELOC(prom_scratch);
8549b6b563cSPaul Mackerras 
8559b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
8569b6b563cSPaul Mackerras 		type[0] = 0;
8579b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
8589b6b563cSPaul Mackerras 
859*c4988820SPaul Mackerras 		if (type[0] == 0) {
860*c4988820SPaul Mackerras 			/*
861*c4988820SPaul Mackerras 			 * CHRP Longtrail machines have no device_type
862*c4988820SPaul Mackerras 			 * on the memory node, so check the name instead...
863*c4988820SPaul Mackerras 			 */
864*c4988820SPaul Mackerras 			prom_getprop(node, "name", type, sizeof(type));
865*c4988820SPaul Mackerras 		}
8669b6b563cSPaul Mackerras 		if (strcmp(type, RELOC("memory")))
8679b6b563cSPaul Mackerras 			continue;
8689b6b563cSPaul Mackerras 
8699b6b563cSPaul Mackerras 		plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
8709b6b563cSPaul Mackerras 		if (plen > sizeof(regbuf)) {
8719b6b563cSPaul Mackerras 			prom_printf("memory node too large for buffer !\n");
8729b6b563cSPaul Mackerras 			plen = sizeof(regbuf);
8739b6b563cSPaul Mackerras 		}
8749b6b563cSPaul Mackerras 		p = RELOC(regbuf);
8759b6b563cSPaul Mackerras 		endp = p + (plen / sizeof(cell_t));
8769b6b563cSPaul Mackerras 
8779b6b563cSPaul Mackerras #ifdef DEBUG_PROM
8789b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
8799b6b563cSPaul Mackerras 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
8809b6b563cSPaul Mackerras 		prom_debug("  node %s :\n", path);
8819b6b563cSPaul Mackerras #endif /* DEBUG_PROM */
8829b6b563cSPaul Mackerras 
8839b6b563cSPaul Mackerras 		while ((endp - p) >= (rac + rsc)) {
8849b6b563cSPaul Mackerras 			unsigned long base, size;
8859b6b563cSPaul Mackerras 
8869b6b563cSPaul Mackerras 			base = prom_next_cell(rac, &p);
8879b6b563cSPaul Mackerras 			size = prom_next_cell(rsc, &p);
8889b6b563cSPaul Mackerras 
8899b6b563cSPaul Mackerras 			if (size == 0)
8909b6b563cSPaul Mackerras 				continue;
8919b6b563cSPaul Mackerras 			prom_debug("    %x %x\n", base, size);
8929b6b563cSPaul Mackerras 			if (base == 0)
8939b6b563cSPaul Mackerras 				RELOC(rmo_top) = size;
8949b6b563cSPaul Mackerras 			if ((base + size) > RELOC(ram_top))
8959b6b563cSPaul Mackerras 				RELOC(ram_top) = base + size;
8969b6b563cSPaul Mackerras 		}
8979b6b563cSPaul Mackerras 	}
8989b6b563cSPaul Mackerras 
8999b6b563cSPaul Mackerras 	RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
9009b6b563cSPaul Mackerras 
9019b6b563cSPaul Mackerras 	/* Check if we have an initrd after the kernel, if we do move our bottom
9029b6b563cSPaul Mackerras 	 * point to after it
9039b6b563cSPaul Mackerras 	 */
9049b6b563cSPaul Mackerras 	if (RELOC(prom_initrd_start)) {
9059b6b563cSPaul Mackerras 		if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
9069b6b563cSPaul Mackerras 			RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
9079b6b563cSPaul Mackerras 	}
9089b6b563cSPaul Mackerras 
9099b6b563cSPaul Mackerras 	/*
9109b6b563cSPaul Mackerras 	 * If prom_memory_limit is set we reduce the upper limits *except* for
9119b6b563cSPaul Mackerras 	 * alloc_top_high. This must be the real top of RAM so we can put
9129b6b563cSPaul Mackerras 	 * TCE's up there.
9139b6b563cSPaul Mackerras 	 */
9149b6b563cSPaul Mackerras 
9159b6b563cSPaul Mackerras 	RELOC(alloc_top_high) = RELOC(ram_top);
9169b6b563cSPaul Mackerras 
9179b6b563cSPaul Mackerras 	if (RELOC(prom_memory_limit)) {
9189b6b563cSPaul Mackerras 		if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
9199b6b563cSPaul Mackerras 			prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
9209b6b563cSPaul Mackerras 				RELOC(prom_memory_limit));
9219b6b563cSPaul Mackerras 			RELOC(prom_memory_limit) = 0;
9229b6b563cSPaul Mackerras 		} else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
9239b6b563cSPaul Mackerras 			prom_printf("Ignoring mem=%x >= ram_top.\n",
9249b6b563cSPaul Mackerras 				RELOC(prom_memory_limit));
9259b6b563cSPaul Mackerras 			RELOC(prom_memory_limit) = 0;
9269b6b563cSPaul Mackerras 		} else {
9279b6b563cSPaul Mackerras 			RELOC(ram_top) = RELOC(prom_memory_limit);
9289b6b563cSPaul Mackerras 			RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
9299b6b563cSPaul Mackerras 		}
9309b6b563cSPaul Mackerras 	}
9319b6b563cSPaul Mackerras 
9329b6b563cSPaul Mackerras 	/*
9339b6b563cSPaul Mackerras 	 * Setup our top alloc point, that is top of RMO or top of
9349b6b563cSPaul Mackerras 	 * segment 0 when running non-LPAR.
9359b6b563cSPaul Mackerras 	 * Some RS64 machines have buggy firmware where claims up at
9369b6b563cSPaul Mackerras 	 * 1GB fail.  Cap at 768MB as a workaround.
9379b6b563cSPaul Mackerras 	 * Since 768MB is plenty of room, and we need to cap to something
9389b6b563cSPaul Mackerras 	 * reasonable on 32-bit, cap at 768MB on all machines.
9399b6b563cSPaul Mackerras 	 */
9409b6b563cSPaul Mackerras 	if (!RELOC(rmo_top))
9419b6b563cSPaul Mackerras 		RELOC(rmo_top) = RELOC(ram_top);
9429b6b563cSPaul Mackerras 	RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
9439b6b563cSPaul Mackerras 	RELOC(alloc_top) = RELOC(rmo_top);
9449b6b563cSPaul Mackerras 
9459b6b563cSPaul Mackerras 	prom_printf("memory layout at init:\n");
9469b6b563cSPaul Mackerras 	prom_printf("  memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
9479b6b563cSPaul Mackerras 	prom_printf("  alloc_bottom : %x\n", RELOC(alloc_bottom));
9489b6b563cSPaul Mackerras 	prom_printf("  alloc_top    : %x\n", RELOC(alloc_top));
9499b6b563cSPaul Mackerras 	prom_printf("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
9509b6b563cSPaul Mackerras 	prom_printf("  rmo_top      : %x\n", RELOC(rmo_top));
9519b6b563cSPaul Mackerras 	prom_printf("  ram_top      : %x\n", RELOC(ram_top));
9529b6b563cSPaul Mackerras }
9539b6b563cSPaul Mackerras 
9549b6b563cSPaul Mackerras 
9559b6b563cSPaul Mackerras /*
9569b6b563cSPaul Mackerras  * Allocate room for and instantiate RTAS
9579b6b563cSPaul Mackerras  */
9589b6b563cSPaul Mackerras static void __init prom_instantiate_rtas(void)
9599b6b563cSPaul Mackerras {
9609b6b563cSPaul Mackerras 	phandle rtas_node;
9619b6b563cSPaul Mackerras 	ihandle rtas_inst;
9629b6b563cSPaul Mackerras 	u32 base, entry = 0;
9639b6b563cSPaul Mackerras 	u32 size = 0;
9649b6b563cSPaul Mackerras 
9659b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: start...\n");
9669b6b563cSPaul Mackerras 
9679b6b563cSPaul Mackerras 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
9689b6b563cSPaul Mackerras 	prom_debug("rtas_node: %x\n", rtas_node);
9699b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(rtas_node))
9709b6b563cSPaul Mackerras 		return;
9719b6b563cSPaul Mackerras 
9729b6b563cSPaul Mackerras 	prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
9739b6b563cSPaul Mackerras 	if (size == 0)
9749b6b563cSPaul Mackerras 		return;
9759b6b563cSPaul Mackerras 
9769b6b563cSPaul Mackerras 	base = alloc_down(size, PAGE_SIZE, 0);
9779b6b563cSPaul Mackerras 	if (base == 0) {
9789b6b563cSPaul Mackerras 		prom_printf("RTAS allocation failed !\n");
9799b6b563cSPaul Mackerras 		return;
9809b6b563cSPaul Mackerras 	}
9819b6b563cSPaul Mackerras 
9829b6b563cSPaul Mackerras 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
9839b6b563cSPaul Mackerras 	if (!IHANDLE_VALID(rtas_inst)) {
9849b6b563cSPaul Mackerras 		prom_printf("opening rtas package failed");
9859b6b563cSPaul Mackerras 		return;
9869b6b563cSPaul Mackerras 	}
9879b6b563cSPaul Mackerras 
9889b6b563cSPaul Mackerras 	prom_printf("instantiating rtas at 0x%x ...", base);
9899b6b563cSPaul Mackerras 
9909b6b563cSPaul Mackerras 	if (call_prom_ret("call-method", 3, 2, &entry,
9919b6b563cSPaul Mackerras 			  ADDR("instantiate-rtas"),
9929b6b563cSPaul Mackerras 			  rtas_inst, base) == PROM_ERROR
9939b6b563cSPaul Mackerras 	    || entry == 0) {
9949b6b563cSPaul Mackerras 		prom_printf(" failed\n");
9959b6b563cSPaul Mackerras 		return;
9969b6b563cSPaul Mackerras 	}
9979b6b563cSPaul Mackerras 	prom_printf(" done\n");
9989b6b563cSPaul Mackerras 
9999b6b563cSPaul Mackerras 	reserve_mem(base, size);
10009b6b563cSPaul Mackerras 
10019b6b563cSPaul Mackerras 	prom_setprop(rtas_node, "linux,rtas-base", &base, sizeof(base));
10029b6b563cSPaul Mackerras 	prom_setprop(rtas_node, "linux,rtas-entry", &entry, sizeof(entry));
10039b6b563cSPaul Mackerras 
10049b6b563cSPaul Mackerras 	prom_debug("rtas base     = 0x%x\n", base);
10059b6b563cSPaul Mackerras 	prom_debug("rtas entry    = 0x%x\n", entry);
10069b6b563cSPaul Mackerras 	prom_debug("rtas size     = 0x%x\n", (long)size);
10079b6b563cSPaul Mackerras 
10089b6b563cSPaul Mackerras 	prom_debug("prom_instantiate_rtas: end...\n");
10099b6b563cSPaul Mackerras }
10109b6b563cSPaul Mackerras 
10119b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
10129b6b563cSPaul Mackerras /*
10139b6b563cSPaul Mackerras  * Allocate room for and initialize TCE tables
10149b6b563cSPaul Mackerras  */
10159b6b563cSPaul Mackerras static void __init prom_initialize_tce_table(void)
10169b6b563cSPaul Mackerras {
10179b6b563cSPaul Mackerras 	phandle node;
10189b6b563cSPaul Mackerras 	ihandle phb_node;
10199b6b563cSPaul Mackerras 	char compatible[64], type[64], model[64];
10209b6b563cSPaul Mackerras 	char *path = RELOC(prom_scratch);
10219b6b563cSPaul Mackerras 	u64 base, align;
10229b6b563cSPaul Mackerras 	u32 minalign, minsize;
10239b6b563cSPaul Mackerras 	u64 tce_entry, *tce_entryp;
10249b6b563cSPaul Mackerras 	u64 local_alloc_top, local_alloc_bottom;
10259b6b563cSPaul Mackerras 	u64 i;
10269b6b563cSPaul Mackerras 
10279b6b563cSPaul Mackerras 	if (RELOC(ppc64_iommu_off))
10289b6b563cSPaul Mackerras 		return;
10299b6b563cSPaul Mackerras 
10309b6b563cSPaul Mackerras 	prom_debug("starting prom_initialize_tce_table\n");
10319b6b563cSPaul Mackerras 
10329b6b563cSPaul Mackerras 	/* Cache current top of allocs so we reserve a single block */
10339b6b563cSPaul Mackerras 	local_alloc_top = RELOC(alloc_top_high);
10349b6b563cSPaul Mackerras 	local_alloc_bottom = local_alloc_top;
10359b6b563cSPaul Mackerras 
10369b6b563cSPaul Mackerras 	/* Search all nodes looking for PHBs. */
10379b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
10389b6b563cSPaul Mackerras 		compatible[0] = 0;
10399b6b563cSPaul Mackerras 		type[0] = 0;
10409b6b563cSPaul Mackerras 		model[0] = 0;
10419b6b563cSPaul Mackerras 		prom_getprop(node, "compatible",
10429b6b563cSPaul Mackerras 			     compatible, sizeof(compatible));
10439b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
10449b6b563cSPaul Mackerras 		prom_getprop(node, "model", model, sizeof(model));
10459b6b563cSPaul Mackerras 
10469b6b563cSPaul Mackerras 		if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
10479b6b563cSPaul Mackerras 			continue;
10489b6b563cSPaul Mackerras 
10499b6b563cSPaul Mackerras 		/* Keep the old logic in tack to avoid regression. */
10509b6b563cSPaul Mackerras 		if (compatible[0] != 0) {
10519b6b563cSPaul Mackerras 			if ((strstr(compatible, RELOC("python")) == NULL) &&
10529b6b563cSPaul Mackerras 			    (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
10539b6b563cSPaul Mackerras 			    (strstr(compatible, RELOC("Winnipeg")) == NULL))
10549b6b563cSPaul Mackerras 				continue;
10559b6b563cSPaul Mackerras 		} else if (model[0] != 0) {
10569b6b563cSPaul Mackerras 			if ((strstr(model, RELOC("ython")) == NULL) &&
10579b6b563cSPaul Mackerras 			    (strstr(model, RELOC("peedwagon")) == NULL) &&
10589b6b563cSPaul Mackerras 			    (strstr(model, RELOC("innipeg")) == NULL))
10599b6b563cSPaul Mackerras 				continue;
10609b6b563cSPaul Mackerras 		}
10619b6b563cSPaul Mackerras 
10629b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minalign", &minalign,
10639b6b563cSPaul Mackerras 				 sizeof(minalign)) == PROM_ERROR)
10649b6b563cSPaul Mackerras 			minalign = 0;
10659b6b563cSPaul Mackerras 		if (prom_getprop(node, "tce-table-minsize", &minsize,
10669b6b563cSPaul Mackerras 				 sizeof(minsize)) == PROM_ERROR)
10679b6b563cSPaul Mackerras 			minsize = 4UL << 20;
10689b6b563cSPaul Mackerras 
10699b6b563cSPaul Mackerras 		/*
10709b6b563cSPaul Mackerras 		 * Even though we read what OF wants, we just set the table
10719b6b563cSPaul Mackerras 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
10729b6b563cSPaul Mackerras 		 * By doing this, we avoid the pitfalls of trying to DMA to
10739b6b563cSPaul Mackerras 		 * MMIO space and the DMA alias hole.
10749b6b563cSPaul Mackerras 		 *
10759b6b563cSPaul Mackerras 		 * On POWER4, firmware sets the TCE region by assuming
10769b6b563cSPaul Mackerras 		 * each TCE table is 8MB. Using this memory for anything
10779b6b563cSPaul Mackerras 		 * else will impact performance, so we always allocate 8MB.
10789b6b563cSPaul Mackerras 		 * Anton
10799b6b563cSPaul Mackerras 		 */
10809b6b563cSPaul Mackerras 		if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
10819b6b563cSPaul Mackerras 			minsize = 8UL << 20;
10829b6b563cSPaul Mackerras 		else
10839b6b563cSPaul Mackerras 			minsize = 4UL << 20;
10849b6b563cSPaul Mackerras 
10859b6b563cSPaul Mackerras 		/* Align to the greater of the align or size */
10869b6b563cSPaul Mackerras 		align = max(minalign, minsize);
10879b6b563cSPaul Mackerras 		base = alloc_down(minsize, align, 1);
10889b6b563cSPaul Mackerras 		if (base == 0)
10899b6b563cSPaul Mackerras 			prom_panic("ERROR, cannot find space for TCE table.\n");
10909b6b563cSPaul Mackerras 		if (base < local_alloc_bottom)
10919b6b563cSPaul Mackerras 			local_alloc_bottom = base;
10929b6b563cSPaul Mackerras 
10939b6b563cSPaul Mackerras 		/* Save away the TCE table attributes for later use. */
10949b6b563cSPaul Mackerras 		prom_setprop(node, "linux,tce-base", &base, sizeof(base));
10959b6b563cSPaul Mackerras 		prom_setprop(node, "linux,tce-size", &minsize, sizeof(minsize));
10969b6b563cSPaul Mackerras 
10979b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
10989b6b563cSPaul Mackerras 		memset(path, 0, sizeof(path));
10999b6b563cSPaul Mackerras 		/* Call OF to setup the TCE hardware */
11009b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node,
11019b6b563cSPaul Mackerras 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
11029b6b563cSPaul Mackerras 			prom_printf("package-to-path failed\n");
11039b6b563cSPaul Mackerras 		}
11049b6b563cSPaul Mackerras 
11059b6b563cSPaul Mackerras 		prom_debug("TCE table: %s\n", path);
11069b6b563cSPaul Mackerras 		prom_debug("\tnode = 0x%x\n", node);
11079b6b563cSPaul Mackerras 		prom_debug("\tbase = 0x%x\n", base);
11089b6b563cSPaul Mackerras 		prom_debug("\tsize = 0x%x\n", minsize);
11099b6b563cSPaul Mackerras 
11109b6b563cSPaul Mackerras 		/* Initialize the table to have a one-to-one mapping
11119b6b563cSPaul Mackerras 		 * over the allocated size.
11129b6b563cSPaul Mackerras 		 */
11139b6b563cSPaul Mackerras 		tce_entryp = (unsigned long *)base;
11149b6b563cSPaul Mackerras 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
11159b6b563cSPaul Mackerras 			tce_entry = (i << PAGE_SHIFT);
11169b6b563cSPaul Mackerras 			tce_entry |= 0x3;
11179b6b563cSPaul Mackerras 			*tce_entryp = tce_entry;
11189b6b563cSPaul Mackerras 		}
11199b6b563cSPaul Mackerras 
11209b6b563cSPaul Mackerras 		prom_printf("opening PHB %s", path);
11219b6b563cSPaul Mackerras 		phb_node = call_prom("open", 1, 1, path);
11229b6b563cSPaul Mackerras 		if (phb_node == 0)
11239b6b563cSPaul Mackerras 			prom_printf("... failed\n");
11249b6b563cSPaul Mackerras 		else
11259b6b563cSPaul Mackerras 			prom_printf("... done\n");
11269b6b563cSPaul Mackerras 
11279b6b563cSPaul Mackerras 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
11289b6b563cSPaul Mackerras 			  phb_node, -1, minsize,
11299b6b563cSPaul Mackerras 			  (u32) base, (u32) (base >> 32));
11309b6b563cSPaul Mackerras 		call_prom("close", 1, 0, phb_node);
11319b6b563cSPaul Mackerras 	}
11329b6b563cSPaul Mackerras 
11339b6b563cSPaul Mackerras 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
11349b6b563cSPaul Mackerras 
11359b6b563cSPaul Mackerras 	if (RELOC(prom_memory_limit)) {
11369b6b563cSPaul Mackerras 		/*
11379b6b563cSPaul Mackerras 		 * We align the start to a 16MB boundary so we can map
11389b6b563cSPaul Mackerras 		 * the TCE area using large pages if possible.
11399b6b563cSPaul Mackerras 		 * The end should be the top of RAM so no need to align it.
11409b6b563cSPaul Mackerras 		 */
11419b6b563cSPaul Mackerras 		RELOC(prom_tce_alloc_start) = _ALIGN_DOWN(local_alloc_bottom,
11429b6b563cSPaul Mackerras 							  0x1000000);
11439b6b563cSPaul Mackerras 		RELOC(prom_tce_alloc_end) = local_alloc_top;
11449b6b563cSPaul Mackerras 	}
11459b6b563cSPaul Mackerras 
11469b6b563cSPaul Mackerras 	/* Flag the first invalid entry */
11479b6b563cSPaul Mackerras 	prom_debug("ending prom_initialize_tce_table\n");
11489b6b563cSPaul Mackerras }
11499b6b563cSPaul Mackerras #endif
11509b6b563cSPaul Mackerras 
11519b6b563cSPaul Mackerras /*
11529b6b563cSPaul Mackerras  * With CHRP SMP we need to use the OF to start the other processors.
11539b6b563cSPaul Mackerras  * We can't wait until smp_boot_cpus (the OF is trashed by then)
11549b6b563cSPaul Mackerras  * so we have to put the processors into a holding pattern controlled
11559b6b563cSPaul Mackerras  * by the kernel (not OF) before we destroy the OF.
11569b6b563cSPaul Mackerras  *
11579b6b563cSPaul Mackerras  * This uses a chunk of low memory, puts some holding pattern
11589b6b563cSPaul Mackerras  * code there and sends the other processors off to there until
11599b6b563cSPaul Mackerras  * smp_boot_cpus tells them to do something.  The holding pattern
11609b6b563cSPaul Mackerras  * checks that address until its cpu # is there, when it is that
11619b6b563cSPaul Mackerras  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
11629b6b563cSPaul Mackerras  * of setting those values.
11639b6b563cSPaul Mackerras  *
11649b6b563cSPaul Mackerras  * We also use physical address 0x4 here to tell when a cpu
11659b6b563cSPaul Mackerras  * is in its holding pattern code.
11669b6b563cSPaul Mackerras  *
11679b6b563cSPaul Mackerras  * -- Cort
11689b6b563cSPaul Mackerras  */
1169bbd0abdaSPaul Mackerras extern void __secondary_hold(void);
1170bbd0abdaSPaul Mackerras extern unsigned long __secondary_hold_spinloop;
1171bbd0abdaSPaul Mackerras extern unsigned long __secondary_hold_acknowledge;
1172bbd0abdaSPaul Mackerras 
1173bbd0abdaSPaul Mackerras /*
1174bbd0abdaSPaul Mackerras  * We want to reference the copy of __secondary_hold_* in the
1175bbd0abdaSPaul Mackerras  * 0 - 0x100 address range
1176bbd0abdaSPaul Mackerras  */
1177bbd0abdaSPaul Mackerras #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1178bbd0abdaSPaul Mackerras 
11799b6b563cSPaul Mackerras static void __init prom_hold_cpus(void)
11809b6b563cSPaul Mackerras {
11819b6b563cSPaul Mackerras 	unsigned long i;
11829b6b563cSPaul Mackerras 	unsigned int reg;
11839b6b563cSPaul Mackerras 	phandle node;
11849b6b563cSPaul Mackerras 	char type[64];
11859b6b563cSPaul Mackerras 	int cpuid = 0;
11869b6b563cSPaul Mackerras 	unsigned int interrupt_server[MAX_CPU_THREADS];
11879b6b563cSPaul Mackerras 	unsigned int cpu_threads, hw_cpu_num;
11889b6b563cSPaul Mackerras 	int propsize;
1189bbd0abdaSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
11909b6b563cSPaul Mackerras 	unsigned long *spinloop
1191bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
11929b6b563cSPaul Mackerras 	unsigned long *acknowledge
1193bbd0abdaSPaul Mackerras 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
11949b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
1195bbd0abdaSPaul Mackerras 	/* __secondary_hold is actually a descriptor, not the text address */
11969b6b563cSPaul Mackerras 	unsigned long secondary_hold
11979b6b563cSPaul Mackerras 		= __pa(*PTRRELOC((unsigned long *)__secondary_hold));
11989b6b563cSPaul Mackerras #else
1199bbd0abdaSPaul Mackerras 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
12009b6b563cSPaul Mackerras #endif
12019b6b563cSPaul Mackerras 
12029b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: start...\n");
12039b6b563cSPaul Mackerras 	prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
12049b6b563cSPaul Mackerras 	prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
12059b6b563cSPaul Mackerras 	prom_debug("    1) acknowledge    = 0x%x\n",
12069b6b563cSPaul Mackerras 		   (unsigned long)acknowledge);
12079b6b563cSPaul Mackerras 	prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
12089b6b563cSPaul Mackerras 	prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
12099b6b563cSPaul Mackerras 
12109b6b563cSPaul Mackerras 	/* Set the common spinloop variable, so all of the secondary cpus
12119b6b563cSPaul Mackerras 	 * will block when they are awakened from their OF spinloop.
12129b6b563cSPaul Mackerras 	 * This must occur for both SMP and non SMP kernels, since OF will
12139b6b563cSPaul Mackerras 	 * be trashed when we move the kernel.
12149b6b563cSPaul Mackerras 	 */
12159b6b563cSPaul Mackerras 	*spinloop = 0;
12169b6b563cSPaul Mackerras 
12179b6b563cSPaul Mackerras #ifdef CONFIG_HMT
1218bbd0abdaSPaul Mackerras 	for (i = 0; i < NR_CPUS; i++)
12199b6b563cSPaul Mackerras 		RELOC(hmt_thread_data)[i].pir = 0xdeadbeef;
12209b6b563cSPaul Mackerras #endif
12219b6b563cSPaul Mackerras 	/* look for cpus */
12229b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
12239b6b563cSPaul Mackerras 		type[0] = 0;
12249b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
12259b6b563cSPaul Mackerras 		if (strcmp(type, RELOC("cpu")) != 0)
12269b6b563cSPaul Mackerras 			continue;
12279b6b563cSPaul Mackerras 
12289b6b563cSPaul Mackerras 		/* Skip non-configured cpus. */
12299b6b563cSPaul Mackerras 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
12309b6b563cSPaul Mackerras 			if (strcmp(type, RELOC("okay")) != 0)
12319b6b563cSPaul Mackerras 				continue;
12329b6b563cSPaul Mackerras 
12339b6b563cSPaul Mackerras 		reg = -1;
12349b6b563cSPaul Mackerras 		prom_getprop(node, "reg", &reg, sizeof(reg));
12359b6b563cSPaul Mackerras 
12369b6b563cSPaul Mackerras 		prom_debug("\ncpuid        = 0x%x\n", cpuid);
12379b6b563cSPaul Mackerras 		prom_debug("cpu hw idx   = 0x%x\n", reg);
12389b6b563cSPaul Mackerras 
12399b6b563cSPaul Mackerras 		/* Init the acknowledge var which will be reset by
12409b6b563cSPaul Mackerras 		 * the secondary cpu when it awakens from its OF
12419b6b563cSPaul Mackerras 		 * spinloop.
12429b6b563cSPaul Mackerras 		 */
12439b6b563cSPaul Mackerras 		*acknowledge = (unsigned long)-1;
12449b6b563cSPaul Mackerras 
12459b6b563cSPaul Mackerras 		propsize = prom_getprop(node, "ibm,ppc-interrupt-server#s",
12469b6b563cSPaul Mackerras 					&interrupt_server,
12479b6b563cSPaul Mackerras 					sizeof(interrupt_server));
12489b6b563cSPaul Mackerras 		if (propsize < 0) {
12499b6b563cSPaul Mackerras 			/* no property.  old hardware has no SMT */
12509b6b563cSPaul Mackerras 			cpu_threads = 1;
12519b6b563cSPaul Mackerras 			interrupt_server[0] = reg; /* fake it with phys id */
12529b6b563cSPaul Mackerras 		} else {
12539b6b563cSPaul Mackerras 			/* We have a threaded processor */
12549b6b563cSPaul Mackerras 			cpu_threads = propsize / sizeof(u32);
12559b6b563cSPaul Mackerras 			if (cpu_threads > MAX_CPU_THREADS) {
12569b6b563cSPaul Mackerras 				prom_printf("SMT: too many threads!\n"
12579b6b563cSPaul Mackerras 					    "SMT: found %x, max is %x\n",
12589b6b563cSPaul Mackerras 					    cpu_threads, MAX_CPU_THREADS);
12599b6b563cSPaul Mackerras 				cpu_threads = 1; /* ToDo: panic? */
12609b6b563cSPaul Mackerras 			}
12619b6b563cSPaul Mackerras 		}
12629b6b563cSPaul Mackerras 
12639b6b563cSPaul Mackerras 		hw_cpu_num = interrupt_server[0];
12649b6b563cSPaul Mackerras 		if (hw_cpu_num != _prom->cpu) {
12659b6b563cSPaul Mackerras 			/* Primary Thread of non-boot cpu */
12669b6b563cSPaul Mackerras 			prom_printf("%x : starting cpu hw idx %x... ", cpuid, reg);
12679b6b563cSPaul Mackerras 			call_prom("start-cpu", 3, 0, node,
12689b6b563cSPaul Mackerras 				  secondary_hold, reg);
12699b6b563cSPaul Mackerras 
12709b6b563cSPaul Mackerras 			for (i = 0; (i < 100000000) &&
12719b6b563cSPaul Mackerras 			     (*acknowledge == ((unsigned long)-1)); i++ )
12729b6b563cSPaul Mackerras 				mb();
12739b6b563cSPaul Mackerras 
1274bbd0abdaSPaul Mackerras 			if (*acknowledge == reg)
12759b6b563cSPaul Mackerras 				prom_printf("done\n");
1276bbd0abdaSPaul Mackerras 			else
12779b6b563cSPaul Mackerras 				prom_printf("failed: %x\n", *acknowledge);
12789b6b563cSPaul Mackerras 		}
12799b6b563cSPaul Mackerras #ifdef CONFIG_SMP
12809b6b563cSPaul Mackerras 		else
12819b6b563cSPaul Mackerras 			prom_printf("%x : boot cpu     %x\n", cpuid, reg);
12829b6b563cSPaul Mackerras #endif /* CONFIG_SMP */
1283bbd0abdaSPaul Mackerras 
1284bbd0abdaSPaul Mackerras 		/* Reserve cpu #s for secondary threads.   They start later. */
1285bbd0abdaSPaul Mackerras 		cpuid += cpu_threads;
12869b6b563cSPaul Mackerras 	}
12879b6b563cSPaul Mackerras #ifdef CONFIG_HMT
12889b6b563cSPaul Mackerras 	/* Only enable HMT on processors that provide support. */
12899b6b563cSPaul Mackerras 	if (__is_processor(PV_PULSAR) ||
12909b6b563cSPaul Mackerras 	    __is_processor(PV_ICESTAR) ||
12919b6b563cSPaul Mackerras 	    __is_processor(PV_SSTAR)) {
12929b6b563cSPaul Mackerras 		prom_printf("    starting secondary threads\n");
12939b6b563cSPaul Mackerras 
12949b6b563cSPaul Mackerras 		for (i = 0; i < NR_CPUS; i += 2) {
12959b6b563cSPaul Mackerras 			if (!cpu_online(i))
12969b6b563cSPaul Mackerras 				continue;
12979b6b563cSPaul Mackerras 
12989b6b563cSPaul Mackerras 			if (i == 0) {
12999b6b563cSPaul Mackerras 				unsigned long pir = mfspr(SPRN_PIR);
13009b6b563cSPaul Mackerras 				if (__is_processor(PV_PULSAR)) {
13019b6b563cSPaul Mackerras 					RELOC(hmt_thread_data)[i].pir =
13029b6b563cSPaul Mackerras 						pir & 0x1f;
13039b6b563cSPaul Mackerras 				} else {
13049b6b563cSPaul Mackerras 					RELOC(hmt_thread_data)[i].pir =
13059b6b563cSPaul Mackerras 						pir & 0x3ff;
13069b6b563cSPaul Mackerras 				}
13079b6b563cSPaul Mackerras 			}
13089b6b563cSPaul Mackerras 		}
13099b6b563cSPaul Mackerras 	} else {
13109b6b563cSPaul Mackerras 		prom_printf("Processor is not HMT capable\n");
13119b6b563cSPaul Mackerras 	}
13129b6b563cSPaul Mackerras #endif
13139b6b563cSPaul Mackerras 
13149b6b563cSPaul Mackerras 	if (cpuid > NR_CPUS)
13159b6b563cSPaul Mackerras 		prom_printf("WARNING: maximum CPUs (" __stringify(NR_CPUS)
13169b6b563cSPaul Mackerras 			    ") exceeded: ignoring extras\n");
13179b6b563cSPaul Mackerras 
13189b6b563cSPaul Mackerras 	prom_debug("prom_hold_cpus: end...\n");
13199b6b563cSPaul Mackerras }
13209b6b563cSPaul Mackerras 
13219b6b563cSPaul Mackerras 
13229b6b563cSPaul Mackerras static void __init prom_init_client_services(unsigned long pp)
13239b6b563cSPaul Mackerras {
13249b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
13259b6b563cSPaul Mackerras 
13269b6b563cSPaul Mackerras 	/* Get a handle to the prom entry point before anything else */
13279b6b563cSPaul Mackerras 	RELOC(prom_entry) = pp;
13289b6b563cSPaul Mackerras 
13299b6b563cSPaul Mackerras 	/* get a handle for the stdout device */
13309b6b563cSPaul Mackerras 	_prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
13319b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(_prom->chosen))
13329b6b563cSPaul Mackerras 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
13339b6b563cSPaul Mackerras 
13349b6b563cSPaul Mackerras 	/* get device tree root */
13359b6b563cSPaul Mackerras 	_prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
13369b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(_prom->root))
13379b6b563cSPaul Mackerras 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1338a575b807SPaul Mackerras 
1339a575b807SPaul Mackerras 	_prom->mmumap = 0;
13409b6b563cSPaul Mackerras }
13419b6b563cSPaul Mackerras 
1342a575b807SPaul Mackerras #ifdef CONFIG_PPC32
1343a575b807SPaul Mackerras /*
1344a575b807SPaul Mackerras  * For really old powermacs, we need to map things we claim.
1345a575b807SPaul Mackerras  * For that, we need the ihandle of the mmu.
1346a575b807SPaul Mackerras  */
1347a575b807SPaul Mackerras static void __init prom_find_mmu(void)
1348a575b807SPaul Mackerras {
1349a575b807SPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
1350a575b807SPaul Mackerras 	phandle oprom;
1351a575b807SPaul Mackerras 	char version[64];
1352a575b807SPaul Mackerras 
1353a575b807SPaul Mackerras 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1354a575b807SPaul Mackerras 	if (!PHANDLE_VALID(oprom))
1355a575b807SPaul Mackerras 		return;
1356a575b807SPaul Mackerras 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1357a575b807SPaul Mackerras 		return;
1358a575b807SPaul Mackerras 	version[sizeof(version) - 1] = 0;
1359a575b807SPaul Mackerras 	prom_printf("OF version is '%s'\n", version);
1360a575b807SPaul Mackerras 	/* XXX might need to add other versions here */
1361a575b807SPaul Mackerras 	if (strcmp(version, "Open Firmware, 1.0.5") != 0)
1362a575b807SPaul Mackerras 		return;
1363a575b807SPaul Mackerras 	prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1364a575b807SPaul Mackerras 		     sizeof(_prom->mmumap));
1365a575b807SPaul Mackerras }
1366a575b807SPaul Mackerras #else
1367a575b807SPaul Mackerras #define prom_find_mmu()
1368a575b807SPaul Mackerras #endif
1369a575b807SPaul Mackerras 
13709b6b563cSPaul Mackerras static void __init prom_init_stdout(void)
13719b6b563cSPaul Mackerras {
13729b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
13739b6b563cSPaul Mackerras 	char *path = RELOC(of_stdout_device);
13749b6b563cSPaul Mackerras 	char type[16];
13759b6b563cSPaul Mackerras 	u32 val;
13769b6b563cSPaul Mackerras 
13779b6b563cSPaul Mackerras 	if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
13789b6b563cSPaul Mackerras 		prom_panic("cannot find stdout");
13799b6b563cSPaul Mackerras 
13809b6b563cSPaul Mackerras 	_prom->stdout = val;
13819b6b563cSPaul Mackerras 
13829b6b563cSPaul Mackerras 	/* Get the full OF pathname of the stdout device */
13839b6b563cSPaul Mackerras 	memset(path, 0, 256);
13849b6b563cSPaul Mackerras 	call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
13859b6b563cSPaul Mackerras 	val = call_prom("instance-to-package", 1, 1, _prom->stdout);
13869b6b563cSPaul Mackerras 	prom_setprop(_prom->chosen, "linux,stdout-package", &val, sizeof(val));
13879b6b563cSPaul Mackerras 	prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
13889b6b563cSPaul Mackerras 	prom_setprop(_prom->chosen, "linux,stdout-path",
13899b6b563cSPaul Mackerras 		     RELOC(of_stdout_device), strlen(RELOC(of_stdout_device))+1);
13909b6b563cSPaul Mackerras 
13919b6b563cSPaul Mackerras 	/* If it's a display, note it */
13929b6b563cSPaul Mackerras 	memset(type, 0, sizeof(type));
13939b6b563cSPaul Mackerras 	prom_getprop(val, "device_type", type, sizeof(type));
13949b6b563cSPaul Mackerras 	if (strcmp(type, RELOC("display")) == 0)
13959b6b563cSPaul Mackerras 		prom_setprop(val, "linux,boot-display", NULL, 0);
13969b6b563cSPaul Mackerras }
13979b6b563cSPaul Mackerras 
13989b6b563cSPaul Mackerras static void __init prom_close_stdin(void)
13999b6b563cSPaul Mackerras {
14009b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
14019b6b563cSPaul Mackerras 	ihandle val;
14029b6b563cSPaul Mackerras 
14039b6b563cSPaul Mackerras 	if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
14049b6b563cSPaul Mackerras 		call_prom("close", 1, 0, val);
14059b6b563cSPaul Mackerras }
14069b6b563cSPaul Mackerras 
14079b6b563cSPaul Mackerras static int __init prom_find_machine_type(void)
14089b6b563cSPaul Mackerras {
14099b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
14109b6b563cSPaul Mackerras 	char compat[256];
14119b6b563cSPaul Mackerras 	int len, i = 0;
14129b6b563cSPaul Mackerras 	phandle rtas;
14139b6b563cSPaul Mackerras 
14149b6b563cSPaul Mackerras 	len = prom_getprop(_prom->root, "compatible",
14159b6b563cSPaul Mackerras 			   compat, sizeof(compat)-1);
14169b6b563cSPaul Mackerras 	if (len > 0) {
14179b6b563cSPaul Mackerras 		compat[len] = 0;
14189b6b563cSPaul Mackerras 		while (i < len) {
14199b6b563cSPaul Mackerras 			char *p = &compat[i];
14209b6b563cSPaul Mackerras 			int sl = strlen(p);
14219b6b563cSPaul Mackerras 			if (sl == 0)
14229b6b563cSPaul Mackerras 				break;
14239b6b563cSPaul Mackerras 			if (strstr(p, RELOC("Power Macintosh")) ||
1424a575b807SPaul Mackerras 			    strstr(p, RELOC("MacRISC")))
14259b6b563cSPaul Mackerras 				return PLATFORM_POWERMAC;
14269b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
14279b6b563cSPaul Mackerras 			if (strstr(p, RELOC("Momentum,Maple")))
14289b6b563cSPaul Mackerras 				return PLATFORM_MAPLE;
14299b6b563cSPaul Mackerras #endif
14309b6b563cSPaul Mackerras 			i += sl + 1;
14319b6b563cSPaul Mackerras 		}
14329b6b563cSPaul Mackerras 	}
14339b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
14349b6b563cSPaul Mackerras 	/* Default to pSeries. We need to know if we are running LPAR */
14359b6b563cSPaul Mackerras 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
14369b6b563cSPaul Mackerras 	if (PHANDLE_VALID(rtas)) {
14379b6b563cSPaul Mackerras 		int x = prom_getproplen(rtas, "ibm,hypertas-functions");
14389b6b563cSPaul Mackerras 		if (x != PROM_ERROR) {
14399b6b563cSPaul Mackerras 			prom_printf("Hypertas detected, assuming LPAR !\n");
14409b6b563cSPaul Mackerras 			return PLATFORM_PSERIES_LPAR;
14419b6b563cSPaul Mackerras 		}
14429b6b563cSPaul Mackerras 	}
14439b6b563cSPaul Mackerras 	return PLATFORM_PSERIES;
14449b6b563cSPaul Mackerras #else
14459b6b563cSPaul Mackerras 	return PLATFORM_CHRP;
14469b6b563cSPaul Mackerras #endif
14479b6b563cSPaul Mackerras }
14489b6b563cSPaul Mackerras 
14499b6b563cSPaul Mackerras static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
14509b6b563cSPaul Mackerras {
14519b6b563cSPaul Mackerras 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
14529b6b563cSPaul Mackerras }
14539b6b563cSPaul Mackerras 
14549b6b563cSPaul Mackerras /*
14559b6b563cSPaul Mackerras  * If we have a display that we don't know how to drive,
14569b6b563cSPaul Mackerras  * we will want to try to execute OF's open method for it
14579b6b563cSPaul Mackerras  * later.  However, OF will probably fall over if we do that
14589b6b563cSPaul Mackerras  * we've taken over the MMU.
14599b6b563cSPaul Mackerras  * So we check whether we will need to open the display,
14609b6b563cSPaul Mackerras  * and if so, open it now.
14619b6b563cSPaul Mackerras  */
14629b6b563cSPaul Mackerras static void __init prom_check_displays(void)
14639b6b563cSPaul Mackerras {
14649b6b563cSPaul Mackerras 	char type[16], *path;
14659b6b563cSPaul Mackerras 	phandle node;
14669b6b563cSPaul Mackerras 	ihandle ih;
14679b6b563cSPaul Mackerras 	int i;
14689b6b563cSPaul Mackerras 
14699b6b563cSPaul Mackerras 	static unsigned char default_colors[] = {
14709b6b563cSPaul Mackerras 		0x00, 0x00, 0x00,
14719b6b563cSPaul Mackerras 		0x00, 0x00, 0xaa,
14729b6b563cSPaul Mackerras 		0x00, 0xaa, 0x00,
14739b6b563cSPaul Mackerras 		0x00, 0xaa, 0xaa,
14749b6b563cSPaul Mackerras 		0xaa, 0x00, 0x00,
14759b6b563cSPaul Mackerras 		0xaa, 0x00, 0xaa,
14769b6b563cSPaul Mackerras 		0xaa, 0xaa, 0x00,
14779b6b563cSPaul Mackerras 		0xaa, 0xaa, 0xaa,
14789b6b563cSPaul Mackerras 		0x55, 0x55, 0x55,
14799b6b563cSPaul Mackerras 		0x55, 0x55, 0xff,
14809b6b563cSPaul Mackerras 		0x55, 0xff, 0x55,
14819b6b563cSPaul Mackerras 		0x55, 0xff, 0xff,
14829b6b563cSPaul Mackerras 		0xff, 0x55, 0x55,
14839b6b563cSPaul Mackerras 		0xff, 0x55, 0xff,
14849b6b563cSPaul Mackerras 		0xff, 0xff, 0x55,
14859b6b563cSPaul Mackerras 		0xff, 0xff, 0xff
14869b6b563cSPaul Mackerras 	};
14879b6b563cSPaul Mackerras 	const unsigned char *clut;
14889b6b563cSPaul Mackerras 
14899b6b563cSPaul Mackerras 	prom_printf("Looking for displays\n");
14909b6b563cSPaul Mackerras 	for (node = 0; prom_next_node(&node); ) {
14919b6b563cSPaul Mackerras 		memset(type, 0, sizeof(type));
14929b6b563cSPaul Mackerras 		prom_getprop(node, "device_type", type, sizeof(type));
14939b6b563cSPaul Mackerras 		if (strcmp(type, RELOC("display")) != 0)
14949b6b563cSPaul Mackerras 			continue;
14959b6b563cSPaul Mackerras 
14969b6b563cSPaul Mackerras 		/* It seems OF doesn't null-terminate the path :-( */
14979b6b563cSPaul Mackerras 		path = RELOC(prom_scratch);
14989b6b563cSPaul Mackerras 		memset(path, 0, PROM_SCRATCH_SIZE);
14999b6b563cSPaul Mackerras 
15009b6b563cSPaul Mackerras 		/*
15019b6b563cSPaul Mackerras 		 * leave some room at the end of the path for appending extra
15029b6b563cSPaul Mackerras 		 * arguments
15039b6b563cSPaul Mackerras 		 */
15049b6b563cSPaul Mackerras 		if (call_prom("package-to-path", 3, 1, node, path,
15059b6b563cSPaul Mackerras 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
15069b6b563cSPaul Mackerras 			continue;
15079b6b563cSPaul Mackerras 		prom_printf("found display   : %s, opening ... ", path);
15089b6b563cSPaul Mackerras 
15099b6b563cSPaul Mackerras 		ih = call_prom("open", 1, 1, path);
15109b6b563cSPaul Mackerras 		if (ih == 0) {
15119b6b563cSPaul Mackerras 			prom_printf("failed\n");
15129b6b563cSPaul Mackerras 			continue;
15139b6b563cSPaul Mackerras 		}
15149b6b563cSPaul Mackerras 
15159b6b563cSPaul Mackerras 		/* Success */
15169b6b563cSPaul Mackerras 		prom_printf("done\n");
15179b6b563cSPaul Mackerras 		prom_setprop(node, "linux,opened", NULL, 0);
15189b6b563cSPaul Mackerras 
15199b6b563cSPaul Mackerras 		/* Setup a usable color table when the appropriate
15209b6b563cSPaul Mackerras 		 * method is available. Should update this to set-colors */
15219b6b563cSPaul Mackerras 		clut = RELOC(default_colors);
15229b6b563cSPaul Mackerras 		for (i = 0; i < 32; i++, clut += 3)
15239b6b563cSPaul Mackerras 			if (prom_set_color(ih, i, clut[0], clut[1],
15249b6b563cSPaul Mackerras 					   clut[2]) != 0)
15259b6b563cSPaul Mackerras 				break;
15269b6b563cSPaul Mackerras 
15279b6b563cSPaul Mackerras #ifdef CONFIG_LOGO_LINUX_CLUT224
15289b6b563cSPaul Mackerras 		clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
15299b6b563cSPaul Mackerras 		for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
15309b6b563cSPaul Mackerras 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
15319b6b563cSPaul Mackerras 					   clut[2]) != 0)
15329b6b563cSPaul Mackerras 				break;
15339b6b563cSPaul Mackerras #endif /* CONFIG_LOGO_LINUX_CLUT224 */
15349b6b563cSPaul Mackerras 	}
15359b6b563cSPaul Mackerras }
15369b6b563cSPaul Mackerras 
15379b6b563cSPaul Mackerras 
15389b6b563cSPaul Mackerras /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
15399b6b563cSPaul Mackerras static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
15409b6b563cSPaul Mackerras 			      unsigned long needed, unsigned long align)
15419b6b563cSPaul Mackerras {
15429b6b563cSPaul Mackerras 	void *ret;
15439b6b563cSPaul Mackerras 
15449b6b563cSPaul Mackerras 	*mem_start = _ALIGN(*mem_start, align);
15459b6b563cSPaul Mackerras 	while ((*mem_start + needed) > *mem_end) {
15469b6b563cSPaul Mackerras 		unsigned long room, chunk;
15479b6b563cSPaul Mackerras 
15489b6b563cSPaul Mackerras 		prom_debug("Chunk exhausted, claiming more at %x...\n",
15499b6b563cSPaul Mackerras 			   RELOC(alloc_bottom));
15509b6b563cSPaul Mackerras 		room = RELOC(alloc_top) - RELOC(alloc_bottom);
15519b6b563cSPaul Mackerras 		if (room > DEVTREE_CHUNK_SIZE)
15529b6b563cSPaul Mackerras 			room = DEVTREE_CHUNK_SIZE;
15539b6b563cSPaul Mackerras 		if (room < PAGE_SIZE)
15549b6b563cSPaul Mackerras 			prom_panic("No memory for flatten_device_tree (no room)");
15559b6b563cSPaul Mackerras 		chunk = alloc_up(room, 0);
15569b6b563cSPaul Mackerras 		if (chunk == 0)
15579b6b563cSPaul Mackerras 			prom_panic("No memory for flatten_device_tree (claim failed)");
15589b6b563cSPaul Mackerras 		*mem_end = RELOC(alloc_top);
15599b6b563cSPaul Mackerras 	}
15609b6b563cSPaul Mackerras 
15619b6b563cSPaul Mackerras 	ret = (void *)*mem_start;
15629b6b563cSPaul Mackerras 	*mem_start += needed;
15639b6b563cSPaul Mackerras 
15649b6b563cSPaul Mackerras 	return ret;
15659b6b563cSPaul Mackerras }
15669b6b563cSPaul Mackerras 
15679b6b563cSPaul Mackerras #define dt_push_token(token, mem_start, mem_end) \
15689b6b563cSPaul Mackerras 	do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
15699b6b563cSPaul Mackerras 
15709b6b563cSPaul Mackerras static unsigned long __init dt_find_string(char *str)
15719b6b563cSPaul Mackerras {
15729b6b563cSPaul Mackerras 	char *s, *os;
15739b6b563cSPaul Mackerras 
15749b6b563cSPaul Mackerras 	s = os = (char *)RELOC(dt_string_start);
15759b6b563cSPaul Mackerras 	s += 4;
15769b6b563cSPaul Mackerras 	while (s <  (char *)RELOC(dt_string_end)) {
15779b6b563cSPaul Mackerras 		if (strcmp(s, str) == 0)
15789b6b563cSPaul Mackerras 			return s - os;
15799b6b563cSPaul Mackerras 		s += strlen(s) + 1;
15809b6b563cSPaul Mackerras 	}
15819b6b563cSPaul Mackerras 	return 0;
15829b6b563cSPaul Mackerras }
15839b6b563cSPaul Mackerras 
15849b6b563cSPaul Mackerras /*
15859b6b563cSPaul Mackerras  * The Open Firmware 1275 specification states properties must be 31 bytes or
15869b6b563cSPaul Mackerras  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
15879b6b563cSPaul Mackerras  */
15889b6b563cSPaul Mackerras #define MAX_PROPERTY_NAME 64
15899b6b563cSPaul Mackerras 
15909b6b563cSPaul Mackerras static void __init scan_dt_build_strings(phandle node,
15919b6b563cSPaul Mackerras 					 unsigned long *mem_start,
15929b6b563cSPaul Mackerras 					 unsigned long *mem_end)
15939b6b563cSPaul Mackerras {
15949b6b563cSPaul Mackerras 	char *prev_name, *namep, *sstart;
15959b6b563cSPaul Mackerras 	unsigned long soff;
15969b6b563cSPaul Mackerras 	phandle child;
15979b6b563cSPaul Mackerras 
15989b6b563cSPaul Mackerras 	sstart =  (char *)RELOC(dt_string_start);
15999b6b563cSPaul Mackerras 
16009b6b563cSPaul Mackerras 	/* get and store all property names */
16019b6b563cSPaul Mackerras 	prev_name = RELOC("");
16029b6b563cSPaul Mackerras 	for (;;) {
16039b6b563cSPaul Mackerras 		/* 64 is max len of name including nul. */
16049b6b563cSPaul Mackerras 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
16059b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
16069b6b563cSPaul Mackerras 			/* No more nodes: unwind alloc */
16079b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
16089b6b563cSPaul Mackerras 			break;
16099b6b563cSPaul Mackerras 		}
16109b6b563cSPaul Mackerras 
16119b6b563cSPaul Mackerras  		/* skip "name" */
16129b6b563cSPaul Mackerras  		if (strcmp(namep, RELOC("name")) == 0) {
16139b6b563cSPaul Mackerras  			*mem_start = (unsigned long)namep;
16149b6b563cSPaul Mackerras  			prev_name = RELOC("name");
16159b6b563cSPaul Mackerras  			continue;
16169b6b563cSPaul Mackerras  		}
16179b6b563cSPaul Mackerras 		/* get/create string entry */
16189b6b563cSPaul Mackerras 		soff = dt_find_string(namep);
16199b6b563cSPaul Mackerras 		if (soff != 0) {
16209b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep;
16219b6b563cSPaul Mackerras 			namep = sstart + soff;
16229b6b563cSPaul Mackerras 		} else {
16239b6b563cSPaul Mackerras 			/* Trim off some if we can */
16249b6b563cSPaul Mackerras 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
16259b6b563cSPaul Mackerras 			RELOC(dt_string_end) = *mem_start;
16269b6b563cSPaul Mackerras 		}
16279b6b563cSPaul Mackerras 		prev_name = namep;
16289b6b563cSPaul Mackerras 	}
16299b6b563cSPaul Mackerras 
16309b6b563cSPaul Mackerras 	/* do all our children */
16319b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
16329b6b563cSPaul Mackerras 	while (child != 0) {
16339b6b563cSPaul Mackerras 		scan_dt_build_strings(child, mem_start, mem_end);
16349b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
16359b6b563cSPaul Mackerras 	}
16369b6b563cSPaul Mackerras }
16379b6b563cSPaul Mackerras 
16389b6b563cSPaul Mackerras static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
16399b6b563cSPaul Mackerras 					unsigned long *mem_end)
16409b6b563cSPaul Mackerras {
16419b6b563cSPaul Mackerras 	phandle child;
16429b6b563cSPaul Mackerras 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
16439b6b563cSPaul Mackerras 	unsigned long soff;
16449b6b563cSPaul Mackerras 	unsigned char *valp;
16459b6b563cSPaul Mackerras 	static char pname[MAX_PROPERTY_NAME];
1646*c4988820SPaul Mackerras 	int l, room;
16479b6b563cSPaul Mackerras 
16489b6b563cSPaul Mackerras 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
16499b6b563cSPaul Mackerras 
16509b6b563cSPaul Mackerras 	/* get the node's full name */
16519b6b563cSPaul Mackerras 	namep = (char *)*mem_start;
1652*c4988820SPaul Mackerras 	room = *mem_end - *mem_start;
1653*c4988820SPaul Mackerras 	if (room > 255)
1654*c4988820SPaul Mackerras 		room = 255;
1655*c4988820SPaul Mackerras 	l = call_prom("package-to-path", 3, 1, node, namep, room);
16569b6b563cSPaul Mackerras 	if (l >= 0) {
16579b6b563cSPaul Mackerras 		/* Didn't fit?  Get more room. */
1658*c4988820SPaul Mackerras 		if (l >= room) {
1659*c4988820SPaul Mackerras 			if (l >= *mem_end - *mem_start)
16609b6b563cSPaul Mackerras 				namep = make_room(mem_start, mem_end, l+1, 1);
16619b6b563cSPaul Mackerras 			call_prom("package-to-path", 3, 1, node, namep, l);
16629b6b563cSPaul Mackerras 		}
16639b6b563cSPaul Mackerras 		namep[l] = '\0';
16649b6b563cSPaul Mackerras 
16659b6b563cSPaul Mackerras 		/* Fixup an Apple bug where they have bogus \0 chars in the
1666a575b807SPaul Mackerras 		 * middle of the path in some properties, and extract
1667a575b807SPaul Mackerras 		 * the unit name (everything after the last '/').
16689b6b563cSPaul Mackerras 		 */
1669a575b807SPaul Mackerras 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
16709b6b563cSPaul Mackerras 			if (*p == '/')
1671a575b807SPaul Mackerras 				lp = namep;
1672a575b807SPaul Mackerras 			else if (*p != 0)
1673a575b807SPaul Mackerras 				*lp++ = *p;
1674a575b807SPaul Mackerras 		}
1675a575b807SPaul Mackerras 		*lp = 0;
1676a575b807SPaul Mackerras 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
16779b6b563cSPaul Mackerras 	}
16789b6b563cSPaul Mackerras 
16799b6b563cSPaul Mackerras 	/* get it again for debugging */
16809b6b563cSPaul Mackerras 	path = RELOC(prom_scratch);
16819b6b563cSPaul Mackerras 	memset(path, 0, PROM_SCRATCH_SIZE);
16829b6b563cSPaul Mackerras 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
16839b6b563cSPaul Mackerras 
16849b6b563cSPaul Mackerras 	/* get and store all properties */
16859b6b563cSPaul Mackerras 	prev_name = RELOC("");
16869b6b563cSPaul Mackerras 	sstart = (char *)RELOC(dt_string_start);
16879b6b563cSPaul Mackerras 	for (;;) {
16889b6b563cSPaul Mackerras 		if (call_prom("nextprop", 3, 1, node, prev_name,
16899b6b563cSPaul Mackerras 			      RELOC(pname)) != 1)
16909b6b563cSPaul Mackerras 			break;
16919b6b563cSPaul Mackerras 
16929b6b563cSPaul Mackerras  		/* skip "name" */
16939b6b563cSPaul Mackerras  		if (strcmp(RELOC(pname), RELOC("name")) == 0) {
16949b6b563cSPaul Mackerras  			prev_name = RELOC("name");
16959b6b563cSPaul Mackerras  			continue;
16969b6b563cSPaul Mackerras  		}
16979b6b563cSPaul Mackerras 
16989b6b563cSPaul Mackerras 		/* find string offset */
16999b6b563cSPaul Mackerras 		soff = dt_find_string(RELOC(pname));
17009b6b563cSPaul Mackerras 		if (soff == 0) {
17019b6b563cSPaul Mackerras 			prom_printf("WARNING: Can't find string index for"
17029b6b563cSPaul Mackerras 				    " <%s>, node %s\n", RELOC(pname), path);
17039b6b563cSPaul Mackerras 			break;
17049b6b563cSPaul Mackerras 		}
17059b6b563cSPaul Mackerras 		prev_name = sstart + soff;
17069b6b563cSPaul Mackerras 
17079b6b563cSPaul Mackerras 		/* get length */
17089b6b563cSPaul Mackerras 		l = call_prom("getproplen", 2, 1, node, RELOC(pname));
17099b6b563cSPaul Mackerras 
17109b6b563cSPaul Mackerras 		/* sanity checks */
17119b6b563cSPaul Mackerras 		if (l == PROM_ERROR)
17129b6b563cSPaul Mackerras 			continue;
17139b6b563cSPaul Mackerras 		if (l > MAX_PROPERTY_LENGTH) {
17149b6b563cSPaul Mackerras 			prom_printf("WARNING: ignoring large property ");
17159b6b563cSPaul Mackerras 			/* It seems OF doesn't null-terminate the path :-( */
17169b6b563cSPaul Mackerras 			prom_printf("[%s] ", path);
17179b6b563cSPaul Mackerras 			prom_printf("%s length 0x%x\n", RELOC(pname), l);
17189b6b563cSPaul Mackerras 			continue;
17199b6b563cSPaul Mackerras 		}
17209b6b563cSPaul Mackerras 
17219b6b563cSPaul Mackerras 		/* push property head */
17229b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
17239b6b563cSPaul Mackerras 		dt_push_token(l, mem_start, mem_end);
17249b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
17259b6b563cSPaul Mackerras 
17269b6b563cSPaul Mackerras 		/* push property content */
17279b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, l, 4);
17289b6b563cSPaul Mackerras 		call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
17299b6b563cSPaul Mackerras 		*mem_start = _ALIGN(*mem_start, 4);
17309b6b563cSPaul Mackerras 	}
17319b6b563cSPaul Mackerras 
17329b6b563cSPaul Mackerras 	/* Add a "linux,phandle" property. */
17339b6b563cSPaul Mackerras 	soff = dt_find_string(RELOC("linux,phandle"));
17349b6b563cSPaul Mackerras 	if (soff == 0)
17359b6b563cSPaul Mackerras 		prom_printf("WARNING: Can't find string index for"
17369b6b563cSPaul Mackerras 			    " <linux-phandle> node %s\n", path);
17379b6b563cSPaul Mackerras 	else {
17389b6b563cSPaul Mackerras 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
17399b6b563cSPaul Mackerras 		dt_push_token(4, mem_start, mem_end);
17409b6b563cSPaul Mackerras 		dt_push_token(soff, mem_start, mem_end);
17419b6b563cSPaul Mackerras 		valp = make_room(mem_start, mem_end, 4, 4);
17429b6b563cSPaul Mackerras 		*(u32 *)valp = node;
17439b6b563cSPaul Mackerras 	}
17449b6b563cSPaul Mackerras 
17459b6b563cSPaul Mackerras 	/* do all our children */
17469b6b563cSPaul Mackerras 	child = call_prom("child", 1, 1, node);
17479b6b563cSPaul Mackerras 	while (child != 0) {
17489b6b563cSPaul Mackerras 		scan_dt_build_struct(child, mem_start, mem_end);
17499b6b563cSPaul Mackerras 		child = call_prom("peer", 1, 1, child);
17509b6b563cSPaul Mackerras 	}
17519b6b563cSPaul Mackerras 
17529b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
17539b6b563cSPaul Mackerras }
17549b6b563cSPaul Mackerras 
17559b6b563cSPaul Mackerras static void __init flatten_device_tree(void)
17569b6b563cSPaul Mackerras {
17579b6b563cSPaul Mackerras 	phandle root;
17589b6b563cSPaul Mackerras 	unsigned long mem_start, mem_end, room;
17599b6b563cSPaul Mackerras 	struct boot_param_header *hdr;
17609b6b563cSPaul Mackerras 	struct prom_t *_prom = &RELOC(prom);
17619b6b563cSPaul Mackerras 	char *namep;
17629b6b563cSPaul Mackerras 	u64 *rsvmap;
17639b6b563cSPaul Mackerras 
17649b6b563cSPaul Mackerras 	/*
17659b6b563cSPaul Mackerras 	 * Check how much room we have between alloc top & bottom (+/- a
17669b6b563cSPaul Mackerras 	 * few pages), crop to 4Mb, as this is our "chuck" size
17679b6b563cSPaul Mackerras 	 */
17689b6b563cSPaul Mackerras 	room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
17699b6b563cSPaul Mackerras 	if (room > DEVTREE_CHUNK_SIZE)
17709b6b563cSPaul Mackerras 		room = DEVTREE_CHUNK_SIZE;
17719b6b563cSPaul Mackerras 	prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
17729b6b563cSPaul Mackerras 
17739b6b563cSPaul Mackerras 	/* Now try to claim that */
17749b6b563cSPaul Mackerras 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
17759b6b563cSPaul Mackerras 	if (mem_start == 0)
17769b6b563cSPaul Mackerras 		prom_panic("Can't allocate initial device-tree chunk\n");
17779b6b563cSPaul Mackerras 	mem_end = RELOC(alloc_top);
17789b6b563cSPaul Mackerras 
17799b6b563cSPaul Mackerras 	/* Get root of tree */
17809b6b563cSPaul Mackerras 	root = call_prom("peer", 1, 1, (phandle)0);
17819b6b563cSPaul Mackerras 	if (root == (phandle)0)
17829b6b563cSPaul Mackerras 		prom_panic ("couldn't get device tree root\n");
17839b6b563cSPaul Mackerras 
17849b6b563cSPaul Mackerras 	/* Build header and make room for mem rsv map */
17859b6b563cSPaul Mackerras 	mem_start = _ALIGN(mem_start, 4);
17869b6b563cSPaul Mackerras 	hdr = make_room(&mem_start, &mem_end,
17879b6b563cSPaul Mackerras 			sizeof(struct boot_param_header), 4);
17889b6b563cSPaul Mackerras 	RELOC(dt_header_start) = (unsigned long)hdr;
17899b6b563cSPaul Mackerras 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
17909b6b563cSPaul Mackerras 
17919b6b563cSPaul Mackerras 	/* Start of strings */
17929b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
17939b6b563cSPaul Mackerras 	RELOC(dt_string_start) = mem_start;
17949b6b563cSPaul Mackerras 	mem_start += 4; /* hole */
17959b6b563cSPaul Mackerras 
17969b6b563cSPaul Mackerras 	/* Add "linux,phandle" in there, we'll need it */
17979b6b563cSPaul Mackerras 	namep = make_room(&mem_start, &mem_end, 16, 1);
17989b6b563cSPaul Mackerras 	strcpy(namep, RELOC("linux,phandle"));
17999b6b563cSPaul Mackerras 	mem_start = (unsigned long)namep + strlen(namep) + 1;
18009b6b563cSPaul Mackerras 
18019b6b563cSPaul Mackerras 	/* Build string array */
18029b6b563cSPaul Mackerras 	prom_printf("Building dt strings...\n");
18039b6b563cSPaul Mackerras 	scan_dt_build_strings(root, &mem_start, &mem_end);
18049b6b563cSPaul Mackerras 	RELOC(dt_string_end) = mem_start;
18059b6b563cSPaul Mackerras 
18069b6b563cSPaul Mackerras 	/* Build structure */
18079b6b563cSPaul Mackerras 	mem_start = PAGE_ALIGN(mem_start);
18089b6b563cSPaul Mackerras 	RELOC(dt_struct_start) = mem_start;
18099b6b563cSPaul Mackerras 	prom_printf("Building dt structure...\n");
18109b6b563cSPaul Mackerras 	scan_dt_build_struct(root, &mem_start, &mem_end);
18119b6b563cSPaul Mackerras 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
18129b6b563cSPaul Mackerras 	RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
18139b6b563cSPaul Mackerras 
18149b6b563cSPaul Mackerras 	/* Finish header */
18159b6b563cSPaul Mackerras 	hdr->boot_cpuid_phys = _prom->cpu;
18169b6b563cSPaul Mackerras 	hdr->magic = OF_DT_HEADER;
18179b6b563cSPaul Mackerras 	hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
18189b6b563cSPaul Mackerras 	hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
18199b6b563cSPaul Mackerras 	hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
18209b6b563cSPaul Mackerras 	hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
18219b6b563cSPaul Mackerras 	hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
18229b6b563cSPaul Mackerras 	hdr->version = OF_DT_VERSION;
18239b6b563cSPaul Mackerras 	/* Version 16 is not backward compatible */
18249b6b563cSPaul Mackerras 	hdr->last_comp_version = 0x10;
18259b6b563cSPaul Mackerras 
18269b6b563cSPaul Mackerras 	/* Reserve the whole thing and copy the reserve map in, we
18279b6b563cSPaul Mackerras 	 * also bump mem_reserve_cnt to cause further reservations to
18289b6b563cSPaul Mackerras 	 * fail since it's too late.
18299b6b563cSPaul Mackerras 	 */
18309b6b563cSPaul Mackerras 	reserve_mem(RELOC(dt_header_start), hdr->totalsize);
18319b6b563cSPaul Mackerras 	memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
18329b6b563cSPaul Mackerras 
18339b6b563cSPaul Mackerras #ifdef DEBUG_PROM
18349b6b563cSPaul Mackerras 	{
18359b6b563cSPaul Mackerras 		int i;
18369b6b563cSPaul Mackerras 		prom_printf("reserved memory map:\n");
18379b6b563cSPaul Mackerras 		for (i = 0; i < RELOC(mem_reserve_cnt); i++)
18389b6b563cSPaul Mackerras 			prom_printf("  %x - %x\n",
18399b6b563cSPaul Mackerras 				    RELOC(mem_reserve_map)[i].base,
18409b6b563cSPaul Mackerras 				    RELOC(mem_reserve_map)[i].size);
18419b6b563cSPaul Mackerras 	}
18429b6b563cSPaul Mackerras #endif
18439b6b563cSPaul Mackerras 	RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
18449b6b563cSPaul Mackerras 
18459b6b563cSPaul Mackerras 	prom_printf("Device tree strings 0x%x -> 0x%x\n",
18469b6b563cSPaul Mackerras 		    RELOC(dt_string_start), RELOC(dt_string_end));
18479b6b563cSPaul Mackerras 	prom_printf("Device tree struct  0x%x -> 0x%x\n",
18489b6b563cSPaul Mackerras 		    RELOC(dt_struct_start), RELOC(dt_struct_end));
18499b6b563cSPaul Mackerras 
18509b6b563cSPaul Mackerras }
18519b6b563cSPaul Mackerras 
18529b6b563cSPaul Mackerras 
18539b6b563cSPaul Mackerras static void __init fixup_device_tree(void)
18549b6b563cSPaul Mackerras {
18559b6b563cSPaul Mackerras #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
18569b6b563cSPaul Mackerras 	phandle u3, i2c, mpic;
18579b6b563cSPaul Mackerras 	u32 u3_rev;
18589b6b563cSPaul Mackerras 	u32 interrupts[2];
18599b6b563cSPaul Mackerras 	u32 parent;
18609b6b563cSPaul Mackerras 
18619b6b563cSPaul Mackerras 	/* Some G5s have a missing interrupt definition, fix it up here */
18629b6b563cSPaul Mackerras 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
18639b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(u3))
18649b6b563cSPaul Mackerras 		return;
18659b6b563cSPaul Mackerras 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
18669b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(i2c))
18679b6b563cSPaul Mackerras 		return;
18689b6b563cSPaul Mackerras 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
18699b6b563cSPaul Mackerras 	if (!PHANDLE_VALID(mpic))
18709b6b563cSPaul Mackerras 		return;
18719b6b563cSPaul Mackerras 
18729b6b563cSPaul Mackerras 	/* check if proper rev of u3 */
18739b6b563cSPaul Mackerras 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
18749b6b563cSPaul Mackerras 	    == PROM_ERROR)
18759b6b563cSPaul Mackerras 		return;
18769b6b563cSPaul Mackerras 	if (u3_rev != 0x35 && u3_rev != 0x37)
18779b6b563cSPaul Mackerras 		return;
18789b6b563cSPaul Mackerras 	/* does it need fixup ? */
18799b6b563cSPaul Mackerras 	if (prom_getproplen(i2c, "interrupts") > 0)
18809b6b563cSPaul Mackerras 		return;
18819b6b563cSPaul Mackerras 
18829b6b563cSPaul Mackerras 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
18839b6b563cSPaul Mackerras 
18849b6b563cSPaul Mackerras 	/* interrupt on this revision of u3 is number 0 and level */
18859b6b563cSPaul Mackerras 	interrupts[0] = 0;
18869b6b563cSPaul Mackerras 	interrupts[1] = 1;
18879b6b563cSPaul Mackerras 	prom_setprop(i2c, "interrupts", &interrupts, sizeof(interrupts));
18889b6b563cSPaul Mackerras 	parent = (u32)mpic;
18899b6b563cSPaul Mackerras 	prom_setprop(i2c, "interrupt-parent", &parent, sizeof(parent));
18909b6b563cSPaul Mackerras #endif
18919b6b563cSPaul Mackerras }
18929b6b563cSPaul Mackerras 
18939b6b563cSPaul Mackerras 
18949b6b563cSPaul Mackerras static void __init prom_find_boot_cpu(void)
18959b6b563cSPaul Mackerras {
18969b6b563cSPaul Mackerras        	struct prom_t *_prom = &RELOC(prom);
18979b6b563cSPaul Mackerras 	u32 getprop_rval;
18989b6b563cSPaul Mackerras 	ihandle prom_cpu;
18999b6b563cSPaul Mackerras 	phandle cpu_pkg;
19009b6b563cSPaul Mackerras 
1901a575b807SPaul Mackerras 	_prom->cpu = 0;
19029b6b563cSPaul Mackerras 	if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
1903a575b807SPaul Mackerras 		return;
19049b6b563cSPaul Mackerras 
19059b6b563cSPaul Mackerras 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
19069b6b563cSPaul Mackerras 
19079b6b563cSPaul Mackerras 	prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
19089b6b563cSPaul Mackerras 	_prom->cpu = getprop_rval;
19099b6b563cSPaul Mackerras 
19109b6b563cSPaul Mackerras 	prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
19119b6b563cSPaul Mackerras }
19129b6b563cSPaul Mackerras 
19139b6b563cSPaul Mackerras static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
19149b6b563cSPaul Mackerras {
19159b6b563cSPaul Mackerras #ifdef CONFIG_BLK_DEV_INITRD
19169b6b563cSPaul Mackerras        	struct prom_t *_prom = &RELOC(prom);
19179b6b563cSPaul Mackerras 
19189b6b563cSPaul Mackerras 	if (r3 && r4 && r4 != 0xdeadbeef) {
19199b6b563cSPaul Mackerras 		unsigned long val;
19209b6b563cSPaul Mackerras 
19219b6b563cSPaul Mackerras 		RELOC(prom_initrd_start) = (r3 >= KERNELBASE) ? __pa(r3) : r3;
19229b6b563cSPaul Mackerras 		RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
19239b6b563cSPaul Mackerras 
19249b6b563cSPaul Mackerras 		val = RELOC(prom_initrd_start);
19259b6b563cSPaul Mackerras 		prom_setprop(_prom->chosen, "linux,initrd-start", &val,
19269b6b563cSPaul Mackerras 			     sizeof(val));
19279b6b563cSPaul Mackerras 		val = RELOC(prom_initrd_end);
19289b6b563cSPaul Mackerras 		prom_setprop(_prom->chosen, "linux,initrd-end", &val,
19299b6b563cSPaul Mackerras 			     sizeof(val));
19309b6b563cSPaul Mackerras 
19319b6b563cSPaul Mackerras 		reserve_mem(RELOC(prom_initrd_start),
19329b6b563cSPaul Mackerras 			    RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
19339b6b563cSPaul Mackerras 
19349b6b563cSPaul Mackerras 		prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
19359b6b563cSPaul Mackerras 		prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
19369b6b563cSPaul Mackerras 	}
19379b6b563cSPaul Mackerras #endif /* CONFIG_BLK_DEV_INITRD */
19389b6b563cSPaul Mackerras }
19399b6b563cSPaul Mackerras 
19409b6b563cSPaul Mackerras /*
19419b6b563cSPaul Mackerras  * We enter here early on, when the Open Firmware prom is still
19429b6b563cSPaul Mackerras  * handling exceptions and the MMU hash table for us.
19439b6b563cSPaul Mackerras  */
19449b6b563cSPaul Mackerras 
19459b6b563cSPaul Mackerras unsigned long __init prom_init(unsigned long r3, unsigned long r4,
19469b6b563cSPaul Mackerras 			       unsigned long pp,
19479b6b563cSPaul Mackerras 			       unsigned long r6, unsigned long r7)
19489b6b563cSPaul Mackerras {
19499b6b563cSPaul Mackerras        	struct prom_t *_prom;
19509b6b563cSPaul Mackerras 	unsigned long hdr;
19519b6b563cSPaul Mackerras 	u32 getprop_rval;
1952b42b6617SPaul Mackerras 	unsigned long offset = reloc_offset();
19539b6b563cSPaul Mackerras 
19549b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
19559b6b563cSPaul Mackerras 	reloc_got2(offset);
19569b6b563cSPaul Mackerras #endif
19579b6b563cSPaul Mackerras 
19589b6b563cSPaul Mackerras 	_prom = &RELOC(prom);
19599b6b563cSPaul Mackerras 
19609b6b563cSPaul Mackerras 	/*
19619b6b563cSPaul Mackerras 	 * First zero the BSS
19629b6b563cSPaul Mackerras 	 */
19639b6b563cSPaul Mackerras 	memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
19649b6b563cSPaul Mackerras 
19659b6b563cSPaul Mackerras 	/*
19669b6b563cSPaul Mackerras 	 * Init interface to Open Firmware, get some node references,
19679b6b563cSPaul Mackerras 	 * like /chosen
19689b6b563cSPaul Mackerras 	 */
19699b6b563cSPaul Mackerras 	prom_init_client_services(pp);
19709b6b563cSPaul Mackerras 
19719b6b563cSPaul Mackerras 	/*
19729b6b563cSPaul Mackerras 	 * Init prom stdout device
19739b6b563cSPaul Mackerras 	 */
19749b6b563cSPaul Mackerras 	prom_init_stdout();
19759b6b563cSPaul Mackerras 
19769b6b563cSPaul Mackerras 	/*
1977a575b807SPaul Mackerras 	 * See if this OF is old enough that we need to do explicit maps
1978a575b807SPaul Mackerras 	 */
1979a575b807SPaul Mackerras 	prom_find_mmu();
1980a575b807SPaul Mackerras 
1981a575b807SPaul Mackerras 	/*
19829b6b563cSPaul Mackerras 	 * Check for an initrd
19839b6b563cSPaul Mackerras 	 */
19849b6b563cSPaul Mackerras 	prom_check_initrd(r3, r4);
19859b6b563cSPaul Mackerras 
19869b6b563cSPaul Mackerras 	/*
19879b6b563cSPaul Mackerras 	 * Get default machine type. At this point, we do not differentiate
19889b6b563cSPaul Mackerras 	 * between pSeries SMP and pSeries LPAR
19899b6b563cSPaul Mackerras 	 */
19909b6b563cSPaul Mackerras 	RELOC(of_platform) = prom_find_machine_type();
19919b6b563cSPaul Mackerras 	getprop_rval = RELOC(of_platform);
19929b6b563cSPaul Mackerras 	prom_setprop(_prom->chosen, "linux,platform",
19939b6b563cSPaul Mackerras 		     &getprop_rval, sizeof(getprop_rval));
19949b6b563cSPaul Mackerras 
19959b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
19969b6b563cSPaul Mackerras 	/*
19979b6b563cSPaul Mackerras 	 * On pSeries, inform the firmware about our capabilities
19989b6b563cSPaul Mackerras 	 */
19999b6b563cSPaul Mackerras 	if (RELOC(of_platform) & PLATFORM_PSERIES)
20009b6b563cSPaul Mackerras 		prom_send_capabilities();
20019b6b563cSPaul Mackerras #endif
20029b6b563cSPaul Mackerras 
20039b6b563cSPaul Mackerras 	/*
20049b6b563cSPaul Mackerras 	 * On pSeries and BPA, copy the CPU hold code
20059b6b563cSPaul Mackerras 	 */
200655d36339SDavid Gibson        	if (RELOC(of_platform) != PLATFORM_POWERMAC)
20075a408329SPaul Mackerras        		copy_and_flush(0, KERNELBASE + offset, 0x100, 0);
20089b6b563cSPaul Mackerras 
20099b6b563cSPaul Mackerras 	/*
20109b6b563cSPaul Mackerras 	 * Do early parsing of command line
20119b6b563cSPaul Mackerras 	 */
20129b6b563cSPaul Mackerras 	early_cmdline_parse();
20139b6b563cSPaul Mackerras 
20149b6b563cSPaul Mackerras 	/*
20159b6b563cSPaul Mackerras 	 * Initialize memory management within prom_init
20169b6b563cSPaul Mackerras 	 */
20179b6b563cSPaul Mackerras 	prom_init_mem();
20189b6b563cSPaul Mackerras 
20199b6b563cSPaul Mackerras 	/*
20209b6b563cSPaul Mackerras 	 * Determine which cpu is actually running right _now_
20219b6b563cSPaul Mackerras 	 */
20229b6b563cSPaul Mackerras 	prom_find_boot_cpu();
20239b6b563cSPaul Mackerras 
20249b6b563cSPaul Mackerras 	/*
20259b6b563cSPaul Mackerras 	 * Initialize display devices
20269b6b563cSPaul Mackerras 	 */
20279b6b563cSPaul Mackerras 	prom_check_displays();
20289b6b563cSPaul Mackerras 
20299b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
20309b6b563cSPaul Mackerras 	/*
20319b6b563cSPaul Mackerras 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
20329b6b563cSPaul Mackerras 	 * that uses the allocator, we need to make sure we get the top of memory
20339b6b563cSPaul Mackerras 	 * available for us here...
20349b6b563cSPaul Mackerras 	 */
20359b6b563cSPaul Mackerras 	if (RELOC(of_platform) == PLATFORM_PSERIES)
20369b6b563cSPaul Mackerras 		prom_initialize_tce_table();
20379b6b563cSPaul Mackerras #endif
20389b6b563cSPaul Mackerras 
20399b6b563cSPaul Mackerras 	/*
20409b6b563cSPaul Mackerras 	 * On non-powermacs, try to instantiate RTAS and puts all CPUs
20419b6b563cSPaul Mackerras 	 * in spin-loops. PowerMacs don't have a working RTAS and use
20429b6b563cSPaul Mackerras 	 * a different way to spin CPUs
20439b6b563cSPaul Mackerras 	 */
20449b6b563cSPaul Mackerras 	if (RELOC(of_platform) != PLATFORM_POWERMAC) {
20459b6b563cSPaul Mackerras 		prom_instantiate_rtas();
20469b6b563cSPaul Mackerras 		prom_hold_cpus();
20479b6b563cSPaul Mackerras 	}
20489b6b563cSPaul Mackerras 
20499b6b563cSPaul Mackerras 	/*
20509b6b563cSPaul Mackerras 	 * Fill in some infos for use by the kernel later on
20519b6b563cSPaul Mackerras 	 */
20529b6b563cSPaul Mackerras 	if (RELOC(prom_memory_limit))
20539b6b563cSPaul Mackerras 		prom_setprop(_prom->chosen, "linux,memory-limit",
20549b6b563cSPaul Mackerras 			     &RELOC(prom_memory_limit),
20559b6b563cSPaul Mackerras 			     sizeof(prom_memory_limit));
20569b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
20579b6b563cSPaul Mackerras 	if (RELOC(ppc64_iommu_off))
20589b6b563cSPaul Mackerras 		prom_setprop(_prom->chosen, "linux,iommu-off", NULL, 0);
20599b6b563cSPaul Mackerras 
20609b6b563cSPaul Mackerras 	if (RELOC(iommu_force_on))
20619b6b563cSPaul Mackerras 		prom_setprop(_prom->chosen, "linux,iommu-force-on", NULL, 0);
20629b6b563cSPaul Mackerras 
20639b6b563cSPaul Mackerras 	if (RELOC(prom_tce_alloc_start)) {
20649b6b563cSPaul Mackerras 		prom_setprop(_prom->chosen, "linux,tce-alloc-start",
20659b6b563cSPaul Mackerras 			     &RELOC(prom_tce_alloc_start),
20669b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_start));
20679b6b563cSPaul Mackerras 		prom_setprop(_prom->chosen, "linux,tce-alloc-end",
20689b6b563cSPaul Mackerras 			     &RELOC(prom_tce_alloc_end),
20699b6b563cSPaul Mackerras 			     sizeof(prom_tce_alloc_end));
20709b6b563cSPaul Mackerras 	}
20719b6b563cSPaul Mackerras #endif
20729b6b563cSPaul Mackerras 
20739b6b563cSPaul Mackerras 	/*
20749b6b563cSPaul Mackerras 	 * Fixup any known bugs in the device-tree
20759b6b563cSPaul Mackerras 	 */
20769b6b563cSPaul Mackerras 	fixup_device_tree();
20779b6b563cSPaul Mackerras 
20789b6b563cSPaul Mackerras 	/*
20799b6b563cSPaul Mackerras 	 * Now finally create the flattened device-tree
20809b6b563cSPaul Mackerras 	 */
20819b6b563cSPaul Mackerras 	prom_printf("copying OF device tree ...\n");
20829b6b563cSPaul Mackerras 	flatten_device_tree();
20839b6b563cSPaul Mackerras 
20849b6b563cSPaul Mackerras 	/* in case stdin is USB and still active on IBM machines... */
20859b6b563cSPaul Mackerras 	prom_close_stdin();
20869b6b563cSPaul Mackerras 
20879b6b563cSPaul Mackerras 	/*
20889b6b563cSPaul Mackerras 	 * Call OF "quiesce" method to shut down pending DMA's from
20899b6b563cSPaul Mackerras 	 * devices etc...
20909b6b563cSPaul Mackerras 	 */
20919b6b563cSPaul Mackerras 	prom_printf("Calling quiesce ...\n");
20929b6b563cSPaul Mackerras 	call_prom("quiesce", 0, 0);
20939b6b563cSPaul Mackerras 
20949b6b563cSPaul Mackerras 	/*
20959b6b563cSPaul Mackerras 	 * And finally, call the kernel passing it the flattened device
20969b6b563cSPaul Mackerras 	 * tree and NULL as r5, thus triggering the new entry point which
20979b6b563cSPaul Mackerras 	 * is common to us and kexec
20989b6b563cSPaul Mackerras 	 */
20999b6b563cSPaul Mackerras 	hdr = RELOC(dt_header_start);
21009b6b563cSPaul Mackerras 	prom_printf("returning from prom_init\n");
21019b6b563cSPaul Mackerras 	prom_debug("->dt_header_start=0x%x\n", hdr);
21029b6b563cSPaul Mackerras 
21039b6b563cSPaul Mackerras #ifdef CONFIG_PPC32
21049b6b563cSPaul Mackerras 	reloc_got2(-offset);
21059b6b563cSPaul Mackerras #endif
21069b6b563cSPaul Mackerras 
210735499c01SPaul Mackerras 	__start(hdr, KERNELBASE + offset, 0);
21089b6b563cSPaul Mackerras 
21099b6b563cSPaul Mackerras 	return 0;
21109b6b563cSPaul Mackerras }
2111