xref: /openbmc/linux/arch/powerpc/kernel/prom_init.c (revision a09d2831)
1 /*
2  * Procedures for interfacing to Open Firmware.
3  *
4  * Paul Mackerras	August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  *
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15 
16 #undef DEBUG_PROM
17 
18 #include <stdarg.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
22 #include <linux/threads.h>
23 #include <linux/spinlock.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/proc_fs.h>
27 #include <linux/stringify.h>
28 #include <linux/delay.h>
29 #include <linux/initrd.h>
30 #include <linux/bitops.h>
31 #include <asm/prom.h>
32 #include <asm/rtas.h>
33 #include <asm/page.h>
34 #include <asm/processor.h>
35 #include <asm/irq.h>
36 #include <asm/io.h>
37 #include <asm/smp.h>
38 #include <asm/system.h>
39 #include <asm/mmu.h>
40 #include <asm/pgtable.h>
41 #include <asm/pci.h>
42 #include <asm/iommu.h>
43 #include <asm/btext.h>
44 #include <asm/sections.h>
45 #include <asm/machdep.h>
46 
47 #include <linux/linux_logo.h>
48 
49 /*
50  * Properties whose value is longer than this get excluded from our
51  * copy of the device tree. This value does need to be big enough to
52  * ensure that we don't lose things like the interrupt-map property
53  * on a PCI-PCI bridge.
54  */
55 #define MAX_PROPERTY_LENGTH	(1UL * 1024 * 1024)
56 
57 /*
58  * Eventually bump that one up
59  */
60 #define DEVTREE_CHUNK_SIZE	0x100000
61 
62 /*
63  * This is the size of the local memory reserve map that gets copied
64  * into the boot params passed to the kernel. That size is totally
65  * flexible as the kernel just reads the list until it encounters an
66  * entry with size 0, so it can be changed without breaking binary
67  * compatibility
68  */
69 #define MEM_RESERVE_MAP_SIZE	8
70 
71 /*
72  * prom_init() is called very early on, before the kernel text
73  * and data have been mapped to KERNELBASE.  At this point the code
74  * is running at whatever address it has been loaded at.
75  * On ppc32 we compile with -mrelocatable, which means that references
76  * to extern and static variables get relocated automatically.
77  * On ppc64 we have to relocate the references explicitly with
78  * RELOC.  (Note that strings count as static variables.)
79  *
80  * Because OF may have mapped I/O devices into the area starting at
81  * KERNELBASE, particularly on CHRP machines, we can't safely call
82  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
83  * OF calls must be done within prom_init().
84  *
85  * ADDR is used in calls to call_prom.  The 4th and following
86  * arguments to call_prom should be 32-bit values.
87  * On ppc64, 64 bit values are truncated to 32 bits (and
88  * fortunately don't get interpreted as two arguments).
89  */
90 #ifdef CONFIG_PPC64
91 #define RELOC(x)        (*PTRRELOC(&(x)))
92 #define ADDR(x)		(u32) add_reloc_offset((unsigned long)(x))
93 #define OF_WORKAROUNDS	0
94 #else
95 #define RELOC(x)	(x)
96 #define ADDR(x)		(u32) (x)
97 #define OF_WORKAROUNDS	of_workarounds
98 int of_workarounds;
99 #endif
100 
101 #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
102 #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
103 
104 #define PROM_BUG() do {						\
105         prom_printf("kernel BUG at %s line 0x%x!\n",		\
106 		    RELOC(__FILE__), __LINE__);			\
107         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
108 } while (0)
109 
110 #ifdef DEBUG_PROM
111 #define prom_debug(x...)	prom_printf(x)
112 #else
113 #define prom_debug(x...)
114 #endif
115 
116 
117 typedef u32 prom_arg_t;
118 
119 struct prom_args {
120         u32 service;
121         u32 nargs;
122         u32 nret;
123         prom_arg_t args[10];
124 };
125 
126 struct prom_t {
127 	ihandle root;
128 	phandle chosen;
129 	int cpu;
130 	ihandle stdout;
131 	ihandle mmumap;
132 	ihandle memory;
133 };
134 
135 struct mem_map_entry {
136 	u64	base;
137 	u64	size;
138 };
139 
140 typedef u32 cell_t;
141 
142 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
143 
144 #ifdef CONFIG_PPC64
145 extern int enter_prom(struct prom_args *args, unsigned long entry);
146 #else
147 static inline int enter_prom(struct prom_args *args, unsigned long entry)
148 {
149 	return ((int (*)(struct prom_args *))entry)(args);
150 }
151 #endif
152 
153 extern void copy_and_flush(unsigned long dest, unsigned long src,
154 			   unsigned long size, unsigned long offset);
155 
156 /* prom structure */
157 static struct prom_t __initdata prom;
158 
159 static unsigned long prom_entry __initdata;
160 
161 #define PROM_SCRATCH_SIZE 256
162 
163 static char __initdata of_stdout_device[256];
164 static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
165 
166 static unsigned long __initdata dt_header_start;
167 static unsigned long __initdata dt_struct_start, dt_struct_end;
168 static unsigned long __initdata dt_string_start, dt_string_end;
169 
170 static unsigned long __initdata prom_initrd_start, prom_initrd_end;
171 
172 #ifdef CONFIG_PPC64
173 static int __initdata prom_iommu_force_on;
174 static int __initdata prom_iommu_off;
175 static unsigned long __initdata prom_tce_alloc_start;
176 static unsigned long __initdata prom_tce_alloc_end;
177 #endif
178 
179 /* Platforms codes are now obsolete in the kernel. Now only used within this
180  * file and ultimately gone too. Feel free to change them if you need, they
181  * are not shared with anything outside of this file anymore
182  */
183 #define PLATFORM_PSERIES	0x0100
184 #define PLATFORM_PSERIES_LPAR	0x0101
185 #define PLATFORM_LPAR		0x0001
186 #define PLATFORM_POWERMAC	0x0400
187 #define PLATFORM_GENERIC	0x0500
188 
189 static int __initdata of_platform;
190 
191 static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
192 
193 static unsigned long __initdata prom_memory_limit;
194 
195 static unsigned long __initdata alloc_top;
196 static unsigned long __initdata alloc_top_high;
197 static unsigned long __initdata alloc_bottom;
198 static unsigned long __initdata rmo_top;
199 static unsigned long __initdata ram_top;
200 
201 static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
202 static int __initdata mem_reserve_cnt;
203 
204 static cell_t __initdata regbuf[1024];
205 
206 
207 /*
208  * Error results ... some OF calls will return "-1" on error, some
209  * will return 0, some will return either. To simplify, here are
210  * macros to use with any ihandle or phandle return value to check if
211  * it is valid
212  */
213 
214 #define PROM_ERROR		(-1u)
215 #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
216 #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
217 
218 
219 /* This is the one and *ONLY* place where we actually call open
220  * firmware.
221  */
222 
223 static int __init call_prom(const char *service, int nargs, int nret, ...)
224 {
225 	int i;
226 	struct prom_args args;
227 	va_list list;
228 
229 	args.service = ADDR(service);
230 	args.nargs = nargs;
231 	args.nret = nret;
232 
233 	va_start(list, nret);
234 	for (i = 0; i < nargs; i++)
235 		args.args[i] = va_arg(list, prom_arg_t);
236 	va_end(list);
237 
238 	for (i = 0; i < nret; i++)
239 		args.args[nargs+i] = 0;
240 
241 	if (enter_prom(&args, RELOC(prom_entry)) < 0)
242 		return PROM_ERROR;
243 
244 	return (nret > 0) ? args.args[nargs] : 0;
245 }
246 
247 static int __init call_prom_ret(const char *service, int nargs, int nret,
248 				prom_arg_t *rets, ...)
249 {
250 	int i;
251 	struct prom_args args;
252 	va_list list;
253 
254 	args.service = ADDR(service);
255 	args.nargs = nargs;
256 	args.nret = nret;
257 
258 	va_start(list, rets);
259 	for (i = 0; i < nargs; i++)
260 		args.args[i] = va_arg(list, prom_arg_t);
261 	va_end(list);
262 
263 	for (i = 0; i < nret; i++)
264 		args.args[nargs+i] = 0;
265 
266 	if (enter_prom(&args, RELOC(prom_entry)) < 0)
267 		return PROM_ERROR;
268 
269 	if (rets != NULL)
270 		for (i = 1; i < nret; ++i)
271 			rets[i-1] = args.args[nargs+i];
272 
273 	return (nret > 0) ? args.args[nargs] : 0;
274 }
275 
276 
277 static void __init prom_print(const char *msg)
278 {
279 	const char *p, *q;
280 	struct prom_t *_prom = &RELOC(prom);
281 
282 	if (_prom->stdout == 0)
283 		return;
284 
285 	for (p = msg; *p != 0; p = q) {
286 		for (q = p; *q != 0 && *q != '\n'; ++q)
287 			;
288 		if (q > p)
289 			call_prom("write", 3, 1, _prom->stdout, p, q - p);
290 		if (*q == 0)
291 			break;
292 		++q;
293 		call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
294 	}
295 }
296 
297 
298 static void __init prom_print_hex(unsigned long val)
299 {
300 	int i, nibbles = sizeof(val)*2;
301 	char buf[sizeof(val)*2+1];
302 	struct prom_t *_prom = &RELOC(prom);
303 
304 	for (i = nibbles-1;  i >= 0;  i--) {
305 		buf[i] = (val & 0xf) + '0';
306 		if (buf[i] > '9')
307 			buf[i] += ('a'-'0'-10);
308 		val >>= 4;
309 	}
310 	buf[nibbles] = '\0';
311 	call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
312 }
313 
314 
315 static void __init prom_printf(const char *format, ...)
316 {
317 	const char *p, *q, *s;
318 	va_list args;
319 	unsigned long v;
320 	struct prom_t *_prom = &RELOC(prom);
321 
322 	va_start(args, format);
323 #ifdef CONFIG_PPC64
324 	format = PTRRELOC(format);
325 #endif
326 	for (p = format; *p != 0; p = q) {
327 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
328 			;
329 		if (q > p)
330 			call_prom("write", 3, 1, _prom->stdout, p, q - p);
331 		if (*q == 0)
332 			break;
333 		if (*q == '\n') {
334 			++q;
335 			call_prom("write", 3, 1, _prom->stdout,
336 				  ADDR("\r\n"), 2);
337 			continue;
338 		}
339 		++q;
340 		if (*q == 0)
341 			break;
342 		switch (*q) {
343 		case 's':
344 			++q;
345 			s = va_arg(args, const char *);
346 			prom_print(s);
347 			break;
348 		case 'x':
349 			++q;
350 			v = va_arg(args, unsigned long);
351 			prom_print_hex(v);
352 			break;
353 		}
354 	}
355 }
356 
357 
358 static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
359 				unsigned long align)
360 {
361 	struct prom_t *_prom = &RELOC(prom);
362 
363 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
364 		/*
365 		 * Old OF requires we claim physical and virtual separately
366 		 * and then map explicitly (assuming virtual mode)
367 		 */
368 		int ret;
369 		prom_arg_t result;
370 
371 		ret = call_prom_ret("call-method", 5, 2, &result,
372 				    ADDR("claim"), _prom->memory,
373 				    align, size, virt);
374 		if (ret != 0 || result == -1)
375 			return -1;
376 		ret = call_prom_ret("call-method", 5, 2, &result,
377 				    ADDR("claim"), _prom->mmumap,
378 				    align, size, virt);
379 		if (ret != 0) {
380 			call_prom("call-method", 4, 1, ADDR("release"),
381 				  _prom->memory, size, virt);
382 			return -1;
383 		}
384 		/* the 0x12 is M (coherence) + PP == read/write */
385 		call_prom("call-method", 6, 1,
386 			  ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
387 		return virt;
388 	}
389 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
390 			 (prom_arg_t)align);
391 }
392 
393 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
394 {
395 #ifdef CONFIG_PPC64
396 	reason = PTRRELOC(reason);
397 #endif
398 	prom_print(reason);
399 	/* Do not call exit because it clears the screen on pmac
400 	 * it also causes some sort of double-fault on early pmacs */
401 	if (RELOC(of_platform) == PLATFORM_POWERMAC)
402 		asm("trap\n");
403 
404 	/* ToDo: should put up an SRC here on p/iSeries */
405 	call_prom("exit", 0, 0);
406 
407 	for (;;)			/* should never get here */
408 		;
409 }
410 
411 
412 static int __init prom_next_node(phandle *nodep)
413 {
414 	phandle node;
415 
416 	if ((node = *nodep) != 0
417 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
418 		return 1;
419 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
420 		return 1;
421 	for (;;) {
422 		if ((node = call_prom("parent", 1, 1, node)) == 0)
423 			return 0;
424 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
425 			return 1;
426 	}
427 }
428 
429 static int inline prom_getprop(phandle node, const char *pname,
430 			       void *value, size_t valuelen)
431 {
432 	return call_prom("getprop", 4, 1, node, ADDR(pname),
433 			 (u32)(unsigned long) value, (u32) valuelen);
434 }
435 
436 static int inline prom_getproplen(phandle node, const char *pname)
437 {
438 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
439 }
440 
441 static void add_string(char **str, const char *q)
442 {
443 	char *p = *str;
444 
445 	while (*q)
446 		*p++ = *q++;
447 	*p++ = ' ';
448 	*str = p;
449 }
450 
451 static char *tohex(unsigned int x)
452 {
453 	static char digits[] = "0123456789abcdef";
454 	static char result[9];
455 	int i;
456 
457 	result[8] = 0;
458 	i = 8;
459 	do {
460 		--i;
461 		result[i] = digits[x & 0xf];
462 		x >>= 4;
463 	} while (x != 0 && i > 0);
464 	return &result[i];
465 }
466 
467 static int __init prom_setprop(phandle node, const char *nodename,
468 			       const char *pname, void *value, size_t valuelen)
469 {
470 	char cmd[256], *p;
471 
472 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
473 		return call_prom("setprop", 4, 1, node, ADDR(pname),
474 				 (u32)(unsigned long) value, (u32) valuelen);
475 
476 	/* gah... setprop doesn't work on longtrail, have to use interpret */
477 	p = cmd;
478 	add_string(&p, "dev");
479 	add_string(&p, nodename);
480 	add_string(&p, tohex((u32)(unsigned long) value));
481 	add_string(&p, tohex(valuelen));
482 	add_string(&p, tohex(ADDR(pname)));
483 	add_string(&p, tohex(strlen(RELOC(pname))));
484 	add_string(&p, "property");
485 	*p = 0;
486 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
487 }
488 
489 /* We can't use the standard versions because of RELOC headaches. */
490 #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
491 			 || ('a' <= (c) && (c) <= 'f') \
492 			 || ('A' <= (c) && (c) <= 'F'))
493 
494 #define isdigit(c)	('0' <= (c) && (c) <= '9')
495 #define islower(c)	('a' <= (c) && (c) <= 'z')
496 #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
497 
498 unsigned long prom_strtoul(const char *cp, const char **endp)
499 {
500 	unsigned long result = 0, base = 10, value;
501 
502 	if (*cp == '0') {
503 		base = 8;
504 		cp++;
505 		if (toupper(*cp) == 'X') {
506 			cp++;
507 			base = 16;
508 		}
509 	}
510 
511 	while (isxdigit(*cp) &&
512 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
513 		result = result * base + value;
514 		cp++;
515 	}
516 
517 	if (endp)
518 		*endp = cp;
519 
520 	return result;
521 }
522 
523 unsigned long prom_memparse(const char *ptr, const char **retptr)
524 {
525 	unsigned long ret = prom_strtoul(ptr, retptr);
526 	int shift = 0;
527 
528 	/*
529 	 * We can't use a switch here because GCC *may* generate a
530 	 * jump table which won't work, because we're not running at
531 	 * the address we're linked at.
532 	 */
533 	if ('G' == **retptr || 'g' == **retptr)
534 		shift = 30;
535 
536 	if ('M' == **retptr || 'm' == **retptr)
537 		shift = 20;
538 
539 	if ('K' == **retptr || 'k' == **retptr)
540 		shift = 10;
541 
542 	if (shift) {
543 		ret <<= shift;
544 		(*retptr)++;
545 	}
546 
547 	return ret;
548 }
549 
550 /*
551  * Early parsing of the command line passed to the kernel, used for
552  * "mem=x" and the options that affect the iommu
553  */
554 static void __init early_cmdline_parse(void)
555 {
556 	struct prom_t *_prom = &RELOC(prom);
557 	const char *opt;
558 
559 	char *p;
560 	int l = 0;
561 
562 	RELOC(prom_cmd_line[0]) = 0;
563 	p = RELOC(prom_cmd_line);
564 	if ((long)_prom->chosen > 0)
565 		l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
566 #ifdef CONFIG_CMDLINE
567 	if (l <= 0 || p[0] == '\0') /* dbl check */
568 		strlcpy(RELOC(prom_cmd_line),
569 			RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
570 #endif /* CONFIG_CMDLINE */
571 	prom_printf("command line: %s\n", RELOC(prom_cmd_line));
572 
573 #ifdef CONFIG_PPC64
574 	opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
575 	if (opt) {
576 		prom_printf("iommu opt is: %s\n", opt);
577 		opt += 6;
578 		while (*opt && *opt == ' ')
579 			opt++;
580 		if (!strncmp(opt, RELOC("off"), 3))
581 			RELOC(prom_iommu_off) = 1;
582 		else if (!strncmp(opt, RELOC("force"), 5))
583 			RELOC(prom_iommu_force_on) = 1;
584 	}
585 #endif
586 	opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
587 	if (opt) {
588 		opt += 4;
589 		RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
590 #ifdef CONFIG_PPC64
591 		/* Align to 16 MB == size of ppc64 large page */
592 		RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
593 #endif
594 	}
595 }
596 
597 #ifdef CONFIG_PPC_PSERIES
598 /*
599  * There are two methods for telling firmware what our capabilities are.
600  * Newer machines have an "ibm,client-architecture-support" method on the
601  * root node.  For older machines, we have to call the "process-elf-header"
602  * method in the /packages/elf-loader node, passing it a fake 32-bit
603  * ELF header containing a couple of PT_NOTE sections that contain
604  * structures that contain various information.
605  */
606 
607 /*
608  * New method - extensible architecture description vector.
609  *
610  * Because the description vector contains a mix of byte and word
611  * values, we declare it as an unsigned char array, and use this
612  * macro to put word values in.
613  */
614 #define W(x)	((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
615 		((x) >> 8) & 0xff, (x) & 0xff
616 
617 /* Option vector bits - generic bits in byte 1 */
618 #define OV_IGNORE		0x80	/* ignore this vector */
619 #define OV_CESSATION_POLICY	0x40	/* halt if unsupported option present*/
620 
621 /* Option vector 1: processor architectures supported */
622 #define OV1_PPC_2_00		0x80	/* set if we support PowerPC 2.00 */
623 #define OV1_PPC_2_01		0x40	/* set if we support PowerPC 2.01 */
624 #define OV1_PPC_2_02		0x20	/* set if we support PowerPC 2.02 */
625 #define OV1_PPC_2_03		0x10	/* set if we support PowerPC 2.03 */
626 #define OV1_PPC_2_04		0x08	/* set if we support PowerPC 2.04 */
627 #define OV1_PPC_2_05		0x04	/* set if we support PowerPC 2.05 */
628 #define OV1_PPC_2_06		0x02	/* set if we support PowerPC 2.06 */
629 
630 /* Option vector 2: Open Firmware options supported */
631 #define OV2_REAL_MODE		0x20	/* set if we want OF in real mode */
632 
633 /* Option vector 3: processor options supported */
634 #define OV3_FP			0x80	/* floating point */
635 #define OV3_VMX			0x40	/* VMX/Altivec */
636 #define OV3_DFP			0x20	/* decimal FP */
637 
638 /* Option vector 5: PAPR/OF options supported */
639 #define OV5_LPAR		0x80	/* logical partitioning supported */
640 #define OV5_SPLPAR		0x40	/* shared-processor LPAR supported */
641 /* ibm,dynamic-reconfiguration-memory property supported */
642 #define OV5_DRCONF_MEMORY	0x20
643 #define OV5_LARGE_PAGES		0x10	/* large pages supported */
644 #define OV5_DONATE_DEDICATE_CPU 0x02	/* donate dedicated CPU support */
645 /* PCIe/MSI support.  Without MSI full PCIe is not supported */
646 #ifdef CONFIG_PCI_MSI
647 #define OV5_MSI			0x01	/* PCIe/MSI support */
648 #else
649 #define OV5_MSI			0x00
650 #endif /* CONFIG_PCI_MSI */
651 #ifdef CONFIG_PPC_SMLPAR
652 #define OV5_CMO			0x80	/* Cooperative Memory Overcommitment */
653 #else
654 #define OV5_CMO			0x00
655 #endif
656 
657 /*
658  * The architecture vector has an array of PVR mask/value pairs,
659  * followed by # option vectors - 1, followed by the option vectors.
660  */
661 static unsigned char ibm_architecture_vec[] = {
662 	W(0xfffe0000), W(0x003a0000),	/* POWER5/POWER5+ */
663 	W(0xffff0000), W(0x003e0000),	/* POWER6 */
664 	W(0xffff0000), W(0x003f0000),	/* POWER7 */
665 	W(0xffffffff), W(0x0f000003),	/* all 2.06-compliant */
666 	W(0xffffffff), W(0x0f000002),	/* all 2.05-compliant */
667 	W(0xfffffffe), W(0x0f000001),	/* all 2.04-compliant and earlier */
668 	5 - 1,				/* 5 option vectors */
669 
670 	/* option vector 1: processor architectures supported */
671 	3 - 2,				/* length */
672 	0,				/* don't ignore, don't halt */
673 	OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
674 	OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
675 
676 	/* option vector 2: Open Firmware options supported */
677 	34 - 2,				/* length */
678 	OV2_REAL_MODE,
679 	0, 0,
680 	W(0xffffffff),			/* real_base */
681 	W(0xffffffff),			/* real_size */
682 	W(0xffffffff),			/* virt_base */
683 	W(0xffffffff),			/* virt_size */
684 	W(0xffffffff),			/* load_base */
685 	W(64),				/* 64MB min RMA */
686 	W(0xffffffff),			/* full client load */
687 	0,				/* min RMA percentage of total RAM */
688 	48,				/* max log_2(hash table size) */
689 
690 	/* option vector 3: processor options supported */
691 	3 - 2,				/* length */
692 	0,				/* don't ignore, don't halt */
693 	OV3_FP | OV3_VMX | OV3_DFP,
694 
695 	/* option vector 4: IBM PAPR implementation */
696 	2 - 2,				/* length */
697 	0,				/* don't halt */
698 
699 	/* option vector 5: PAPR/OF options */
700 	5 - 2,				/* length */
701 	0,				/* don't ignore, don't halt */
702 	OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
703 	OV5_DONATE_DEDICATE_CPU | OV5_MSI,
704 	0,
705 	OV5_CMO,
706 };
707 
708 /* Old method - ELF header with PT_NOTE sections */
709 static struct fake_elf {
710 	Elf32_Ehdr	elfhdr;
711 	Elf32_Phdr	phdr[2];
712 	struct chrpnote {
713 		u32	namesz;
714 		u32	descsz;
715 		u32	type;
716 		char	name[8];	/* "PowerPC" */
717 		struct chrpdesc {
718 			u32	real_mode;
719 			u32	real_base;
720 			u32	real_size;
721 			u32	virt_base;
722 			u32	virt_size;
723 			u32	load_base;
724 		} chrpdesc;
725 	} chrpnote;
726 	struct rpanote {
727 		u32	namesz;
728 		u32	descsz;
729 		u32	type;
730 		char	name[24];	/* "IBM,RPA-Client-Config" */
731 		struct rpadesc {
732 			u32	lpar_affinity;
733 			u32	min_rmo_size;
734 			u32	min_rmo_percent;
735 			u32	max_pft_size;
736 			u32	splpar;
737 			u32	min_load;
738 			u32	new_mem_def;
739 			u32	ignore_me;
740 		} rpadesc;
741 	} rpanote;
742 } fake_elf = {
743 	.elfhdr = {
744 		.e_ident = { 0x7f, 'E', 'L', 'F',
745 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
746 		.e_type = ET_EXEC,	/* yeah right */
747 		.e_machine = EM_PPC,
748 		.e_version = EV_CURRENT,
749 		.e_phoff = offsetof(struct fake_elf, phdr),
750 		.e_phentsize = sizeof(Elf32_Phdr),
751 		.e_phnum = 2
752 	},
753 	.phdr = {
754 		[0] = {
755 			.p_type = PT_NOTE,
756 			.p_offset = offsetof(struct fake_elf, chrpnote),
757 			.p_filesz = sizeof(struct chrpnote)
758 		}, [1] = {
759 			.p_type = PT_NOTE,
760 			.p_offset = offsetof(struct fake_elf, rpanote),
761 			.p_filesz = sizeof(struct rpanote)
762 		}
763 	},
764 	.chrpnote = {
765 		.namesz = sizeof("PowerPC"),
766 		.descsz = sizeof(struct chrpdesc),
767 		.type = 0x1275,
768 		.name = "PowerPC",
769 		.chrpdesc = {
770 			.real_mode = ~0U,	/* ~0 means "don't care" */
771 			.real_base = ~0U,
772 			.real_size = ~0U,
773 			.virt_base = ~0U,
774 			.virt_size = ~0U,
775 			.load_base = ~0U
776 		},
777 	},
778 	.rpanote = {
779 		.namesz = sizeof("IBM,RPA-Client-Config"),
780 		.descsz = sizeof(struct rpadesc),
781 		.type = 0x12759999,
782 		.name = "IBM,RPA-Client-Config",
783 		.rpadesc = {
784 			.lpar_affinity = 0,
785 			.min_rmo_size = 64,	/* in megabytes */
786 			.min_rmo_percent = 0,
787 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
788 			.splpar = 1,
789 			.min_load = ~0U,
790 			.new_mem_def = 0
791 		}
792 	}
793 };
794 
795 static void __init prom_send_capabilities(void)
796 {
797 	ihandle elfloader, root;
798 	prom_arg_t ret;
799 
800 	root = call_prom("open", 1, 1, ADDR("/"));
801 	if (root != 0) {
802 		/* try calling the ibm,client-architecture-support method */
803 		prom_printf("Calling ibm,client-architecture-support...");
804 		if (call_prom_ret("call-method", 3, 2, &ret,
805 				  ADDR("ibm,client-architecture-support"),
806 				  root,
807 				  ADDR(ibm_architecture_vec)) == 0) {
808 			/* the call exists... */
809 			if (ret)
810 				prom_printf("\nWARNING: ibm,client-architecture"
811 					    "-support call FAILED!\n");
812 			call_prom("close", 1, 0, root);
813 			prom_printf(" done\n");
814 			return;
815 		}
816 		call_prom("close", 1, 0, root);
817 		prom_printf(" not implemented\n");
818 	}
819 
820 	/* no ibm,client-architecture-support call, try the old way */
821 	elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
822 	if (elfloader == 0) {
823 		prom_printf("couldn't open /packages/elf-loader\n");
824 		return;
825 	}
826 	call_prom("call-method", 3, 1, ADDR("process-elf-header"),
827 			elfloader, ADDR(&fake_elf));
828 	call_prom("close", 1, 0, elfloader);
829 }
830 #endif
831 
832 /*
833  * Memory allocation strategy... our layout is normally:
834  *
835  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
836  *  rare cases, initrd might end up being before the kernel though.
837  *  We assume this won't override the final kernel at 0, we have no
838  *  provision to handle that in this version, but it should hopefully
839  *  never happen.
840  *
841  *  alloc_top is set to the top of RMO, eventually shrink down if the
842  *  TCEs overlap
843  *
844  *  alloc_bottom is set to the top of kernel/initrd
845  *
846  *  from there, allocations are done this way : rtas is allocated
847  *  topmost, and the device-tree is allocated from the bottom. We try
848  *  to grow the device-tree allocation as we progress. If we can't,
849  *  then we fail, we don't currently have a facility to restart
850  *  elsewhere, but that shouldn't be necessary.
851  *
852  *  Note that calls to reserve_mem have to be done explicitly, memory
853  *  allocated with either alloc_up or alloc_down isn't automatically
854  *  reserved.
855  */
856 
857 
858 /*
859  * Allocates memory in the RMO upward from the kernel/initrd
860  *
861  * When align is 0, this is a special case, it means to allocate in place
862  * at the current location of alloc_bottom or fail (that is basically
863  * extending the previous allocation). Used for the device-tree flattening
864  */
865 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
866 {
867 	unsigned long base = RELOC(alloc_bottom);
868 	unsigned long addr = 0;
869 
870 	if (align)
871 		base = _ALIGN_UP(base, align);
872 	prom_debug("alloc_up(%x, %x)\n", size, align);
873 	if (RELOC(ram_top) == 0)
874 		prom_panic("alloc_up() called with mem not initialized\n");
875 
876 	if (align)
877 		base = _ALIGN_UP(RELOC(alloc_bottom), align);
878 	else
879 		base = RELOC(alloc_bottom);
880 
881 	for(; (base + size) <= RELOC(alloc_top);
882 	    base = _ALIGN_UP(base + 0x100000, align)) {
883 		prom_debug("    trying: 0x%x\n\r", base);
884 		addr = (unsigned long)prom_claim(base, size, 0);
885 		if (addr != PROM_ERROR && addr != 0)
886 			break;
887 		addr = 0;
888 		if (align == 0)
889 			break;
890 	}
891 	if (addr == 0)
892 		return 0;
893 	RELOC(alloc_bottom) = addr;
894 
895 	prom_debug(" -> %x\n", addr);
896 	prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
897 	prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
898 	prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
899 	prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
900 	prom_debug("  ram_top      : %x\n", RELOC(ram_top));
901 
902 	return addr;
903 }
904 
905 /*
906  * Allocates memory downward, either from top of RMO, or if highmem
907  * is set, from the top of RAM.  Note that this one doesn't handle
908  * failures.  It does claim memory if highmem is not set.
909  */
910 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
911 				       int highmem)
912 {
913 	unsigned long base, addr = 0;
914 
915 	prom_debug("alloc_down(%x, %x, %s)\n", size, align,
916 		   highmem ? RELOC("(high)") : RELOC("(low)"));
917 	if (RELOC(ram_top) == 0)
918 		prom_panic("alloc_down() called with mem not initialized\n");
919 
920 	if (highmem) {
921 		/* Carve out storage for the TCE table. */
922 		addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
923 		if (addr <= RELOC(alloc_bottom))
924 			return 0;
925 		/* Will we bump into the RMO ? If yes, check out that we
926 		 * didn't overlap existing allocations there, if we did,
927 		 * we are dead, we must be the first in town !
928 		 */
929 		if (addr < RELOC(rmo_top)) {
930 			/* Good, we are first */
931 			if (RELOC(alloc_top) == RELOC(rmo_top))
932 				RELOC(alloc_top) = RELOC(rmo_top) = addr;
933 			else
934 				return 0;
935 		}
936 		RELOC(alloc_top_high) = addr;
937 		goto bail;
938 	}
939 
940 	base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
941 	for (; base > RELOC(alloc_bottom);
942 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
943 		prom_debug("    trying: 0x%x\n\r", base);
944 		addr = (unsigned long)prom_claim(base, size, 0);
945 		if (addr != PROM_ERROR && addr != 0)
946 			break;
947 		addr = 0;
948 	}
949 	if (addr == 0)
950 		return 0;
951 	RELOC(alloc_top) = addr;
952 
953  bail:
954 	prom_debug(" -> %x\n", addr);
955 	prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
956 	prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
957 	prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
958 	prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
959 	prom_debug("  ram_top      : %x\n", RELOC(ram_top));
960 
961 	return addr;
962 }
963 
964 /*
965  * Parse a "reg" cell
966  */
967 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
968 {
969 	cell_t *p = *cellp;
970 	unsigned long r = 0;
971 
972 	/* Ignore more than 2 cells */
973 	while (s > sizeof(unsigned long) / 4) {
974 		p++;
975 		s--;
976 	}
977 	r = *p++;
978 #ifdef CONFIG_PPC64
979 	if (s > 1) {
980 		r <<= 32;
981 		r |= *(p++);
982 	}
983 #endif
984 	*cellp = p;
985 	return r;
986 }
987 
988 /*
989  * Very dumb function for adding to the memory reserve list, but
990  * we don't need anything smarter at this point
991  *
992  * XXX Eventually check for collisions.  They should NEVER happen.
993  * If problems seem to show up, it would be a good start to track
994  * them down.
995  */
996 static void __init reserve_mem(u64 base, u64 size)
997 {
998 	u64 top = base + size;
999 	unsigned long cnt = RELOC(mem_reserve_cnt);
1000 
1001 	if (size == 0)
1002 		return;
1003 
1004 	/* We need to always keep one empty entry so that we
1005 	 * have our terminator with "size" set to 0 since we are
1006 	 * dumb and just copy this entire array to the boot params
1007 	 */
1008 	base = _ALIGN_DOWN(base, PAGE_SIZE);
1009 	top = _ALIGN_UP(top, PAGE_SIZE);
1010 	size = top - base;
1011 
1012 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
1013 		prom_panic("Memory reserve map exhausted !\n");
1014 	RELOC(mem_reserve_map)[cnt].base = base;
1015 	RELOC(mem_reserve_map)[cnt].size = size;
1016 	RELOC(mem_reserve_cnt) = cnt + 1;
1017 }
1018 
1019 /*
1020  * Initialize memory allocation mechanism, parse "memory" nodes and
1021  * obtain that way the top of memory and RMO to setup out local allocator
1022  */
1023 static void __init prom_init_mem(void)
1024 {
1025 	phandle node;
1026 	char *path, type[64];
1027 	unsigned int plen;
1028 	cell_t *p, *endp;
1029 	struct prom_t *_prom = &RELOC(prom);
1030 	u32 rac, rsc;
1031 
1032 	/*
1033 	 * We iterate the memory nodes to find
1034 	 * 1) top of RMO (first node)
1035 	 * 2) top of memory
1036 	 */
1037 	rac = 2;
1038 	prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
1039 	rsc = 1;
1040 	prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
1041 	prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
1042 	prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
1043 
1044 	prom_debug("scanning memory:\n");
1045 	path = RELOC(prom_scratch);
1046 
1047 	for (node = 0; prom_next_node(&node); ) {
1048 		type[0] = 0;
1049 		prom_getprop(node, "device_type", type, sizeof(type));
1050 
1051 		if (type[0] == 0) {
1052 			/*
1053 			 * CHRP Longtrail machines have no device_type
1054 			 * on the memory node, so check the name instead...
1055 			 */
1056 			prom_getprop(node, "name", type, sizeof(type));
1057 		}
1058 		if (strcmp(type, RELOC("memory")))
1059 			continue;
1060 
1061 		plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
1062 		if (plen > sizeof(regbuf)) {
1063 			prom_printf("memory node too large for buffer !\n");
1064 			plen = sizeof(regbuf);
1065 		}
1066 		p = RELOC(regbuf);
1067 		endp = p + (plen / sizeof(cell_t));
1068 
1069 #ifdef DEBUG_PROM
1070 		memset(path, 0, PROM_SCRATCH_SIZE);
1071 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1072 		prom_debug("  node %s :\n", path);
1073 #endif /* DEBUG_PROM */
1074 
1075 		while ((endp - p) >= (rac + rsc)) {
1076 			unsigned long base, size;
1077 
1078 			base = prom_next_cell(rac, &p);
1079 			size = prom_next_cell(rsc, &p);
1080 
1081 			if (size == 0)
1082 				continue;
1083 			prom_debug("    %x %x\n", base, size);
1084 			if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
1085 				RELOC(rmo_top) = size;
1086 			if ((base + size) > RELOC(ram_top))
1087 				RELOC(ram_top) = base + size;
1088 		}
1089 	}
1090 
1091 	RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
1092 
1093 	/* Check if we have an initrd after the kernel, if we do move our bottom
1094 	 * point to after it
1095 	 */
1096 	if (RELOC(prom_initrd_start)) {
1097 		if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
1098 			RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
1099 	}
1100 
1101 	/*
1102 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1103 	 * alloc_top_high. This must be the real top of RAM so we can put
1104 	 * TCE's up there.
1105 	 */
1106 
1107 	RELOC(alloc_top_high) = RELOC(ram_top);
1108 
1109 	if (RELOC(prom_memory_limit)) {
1110 		if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
1111 			prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
1112 				RELOC(prom_memory_limit));
1113 			RELOC(prom_memory_limit) = 0;
1114 		} else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
1115 			prom_printf("Ignoring mem=%x >= ram_top.\n",
1116 				RELOC(prom_memory_limit));
1117 			RELOC(prom_memory_limit) = 0;
1118 		} else {
1119 			RELOC(ram_top) = RELOC(prom_memory_limit);
1120 			RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
1121 		}
1122 	}
1123 
1124 	/*
1125 	 * Setup our top alloc point, that is top of RMO or top of
1126 	 * segment 0 when running non-LPAR.
1127 	 * Some RS64 machines have buggy firmware where claims up at
1128 	 * 1GB fail.  Cap at 768MB as a workaround.
1129 	 * Since 768MB is plenty of room, and we need to cap to something
1130 	 * reasonable on 32-bit, cap at 768MB on all machines.
1131 	 */
1132 	if (!RELOC(rmo_top))
1133 		RELOC(rmo_top) = RELOC(ram_top);
1134 	RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
1135 	RELOC(alloc_top) = RELOC(rmo_top);
1136 	RELOC(alloc_top_high) = RELOC(ram_top);
1137 
1138 	prom_printf("memory layout at init:\n");
1139 	prom_printf("  memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
1140 	prom_printf("  alloc_bottom : %x\n", RELOC(alloc_bottom));
1141 	prom_printf("  alloc_top    : %x\n", RELOC(alloc_top));
1142 	prom_printf("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
1143 	prom_printf("  rmo_top      : %x\n", RELOC(rmo_top));
1144 	prom_printf("  ram_top      : %x\n", RELOC(ram_top));
1145 }
1146 
1147 
1148 /*
1149  * Allocate room for and instantiate RTAS
1150  */
1151 static void __init prom_instantiate_rtas(void)
1152 {
1153 	phandle rtas_node;
1154 	ihandle rtas_inst;
1155 	u32 base, entry = 0;
1156 	u32 size = 0;
1157 
1158 	prom_debug("prom_instantiate_rtas: start...\n");
1159 
1160 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1161 	prom_debug("rtas_node: %x\n", rtas_node);
1162 	if (!PHANDLE_VALID(rtas_node))
1163 		return;
1164 
1165 	prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1166 	if (size == 0)
1167 		return;
1168 
1169 	base = alloc_down(size, PAGE_SIZE, 0);
1170 	if (base == 0) {
1171 		prom_printf("RTAS allocation failed !\n");
1172 		return;
1173 	}
1174 
1175 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1176 	if (!IHANDLE_VALID(rtas_inst)) {
1177 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
1178 		return;
1179 	}
1180 
1181 	prom_printf("instantiating rtas at 0x%x...", base);
1182 
1183 	if (call_prom_ret("call-method", 3, 2, &entry,
1184 			  ADDR("instantiate-rtas"),
1185 			  rtas_inst, base) != 0
1186 	    || entry == 0) {
1187 		prom_printf(" failed\n");
1188 		return;
1189 	}
1190 	prom_printf(" done\n");
1191 
1192 	reserve_mem(base, size);
1193 
1194 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1195 		     &base, sizeof(base));
1196 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1197 		     &entry, sizeof(entry));
1198 
1199 	prom_debug("rtas base     = 0x%x\n", base);
1200 	prom_debug("rtas entry    = 0x%x\n", entry);
1201 	prom_debug("rtas size     = 0x%x\n", (long)size);
1202 
1203 	prom_debug("prom_instantiate_rtas: end...\n");
1204 }
1205 
1206 #ifdef CONFIG_PPC64
1207 /*
1208  * Allocate room for and initialize TCE tables
1209  */
1210 static void __init prom_initialize_tce_table(void)
1211 {
1212 	phandle node;
1213 	ihandle phb_node;
1214 	char compatible[64], type[64], model[64];
1215 	char *path = RELOC(prom_scratch);
1216 	u64 base, align;
1217 	u32 minalign, minsize;
1218 	u64 tce_entry, *tce_entryp;
1219 	u64 local_alloc_top, local_alloc_bottom;
1220 	u64 i;
1221 
1222 	if (RELOC(prom_iommu_off))
1223 		return;
1224 
1225 	prom_debug("starting prom_initialize_tce_table\n");
1226 
1227 	/* Cache current top of allocs so we reserve a single block */
1228 	local_alloc_top = RELOC(alloc_top_high);
1229 	local_alloc_bottom = local_alloc_top;
1230 
1231 	/* Search all nodes looking for PHBs. */
1232 	for (node = 0; prom_next_node(&node); ) {
1233 		compatible[0] = 0;
1234 		type[0] = 0;
1235 		model[0] = 0;
1236 		prom_getprop(node, "compatible",
1237 			     compatible, sizeof(compatible));
1238 		prom_getprop(node, "device_type", type, sizeof(type));
1239 		prom_getprop(node, "model", model, sizeof(model));
1240 
1241 		if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1242 			continue;
1243 
1244 		/* Keep the old logic intact to avoid regression. */
1245 		if (compatible[0] != 0) {
1246 			if ((strstr(compatible, RELOC("python")) == NULL) &&
1247 			    (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1248 			    (strstr(compatible, RELOC("Winnipeg")) == NULL))
1249 				continue;
1250 		} else if (model[0] != 0) {
1251 			if ((strstr(model, RELOC("ython")) == NULL) &&
1252 			    (strstr(model, RELOC("peedwagon")) == NULL) &&
1253 			    (strstr(model, RELOC("innipeg")) == NULL))
1254 				continue;
1255 		}
1256 
1257 		if (prom_getprop(node, "tce-table-minalign", &minalign,
1258 				 sizeof(minalign)) == PROM_ERROR)
1259 			minalign = 0;
1260 		if (prom_getprop(node, "tce-table-minsize", &minsize,
1261 				 sizeof(minsize)) == PROM_ERROR)
1262 			minsize = 4UL << 20;
1263 
1264 		/*
1265 		 * Even though we read what OF wants, we just set the table
1266 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
1267 		 * By doing this, we avoid the pitfalls of trying to DMA to
1268 		 * MMIO space and the DMA alias hole.
1269 		 *
1270 		 * On POWER4, firmware sets the TCE region by assuming
1271 		 * each TCE table is 8MB. Using this memory for anything
1272 		 * else will impact performance, so we always allocate 8MB.
1273 		 * Anton
1274 		 */
1275 		if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1276 			minsize = 8UL << 20;
1277 		else
1278 			minsize = 4UL << 20;
1279 
1280 		/* Align to the greater of the align or size */
1281 		align = max(minalign, minsize);
1282 		base = alloc_down(minsize, align, 1);
1283 		if (base == 0)
1284 			prom_panic("ERROR, cannot find space for TCE table.\n");
1285 		if (base < local_alloc_bottom)
1286 			local_alloc_bottom = base;
1287 
1288 		/* It seems OF doesn't null-terminate the path :-( */
1289 		memset(path, 0, PROM_SCRATCH_SIZE);
1290 		/* Call OF to setup the TCE hardware */
1291 		if (call_prom("package-to-path", 3, 1, node,
1292 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1293 			prom_printf("package-to-path failed\n");
1294 		}
1295 
1296 		/* Save away the TCE table attributes for later use. */
1297 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1298 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1299 
1300 		prom_debug("TCE table: %s\n", path);
1301 		prom_debug("\tnode = 0x%x\n", node);
1302 		prom_debug("\tbase = 0x%x\n", base);
1303 		prom_debug("\tsize = 0x%x\n", minsize);
1304 
1305 		/* Initialize the table to have a one-to-one mapping
1306 		 * over the allocated size.
1307 		 */
1308 		tce_entryp = (u64 *)base;
1309 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1310 			tce_entry = (i << PAGE_SHIFT);
1311 			tce_entry |= 0x3;
1312 			*tce_entryp = tce_entry;
1313 		}
1314 
1315 		prom_printf("opening PHB %s", path);
1316 		phb_node = call_prom("open", 1, 1, path);
1317 		if (phb_node == 0)
1318 			prom_printf("... failed\n");
1319 		else
1320 			prom_printf("... done\n");
1321 
1322 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1323 			  phb_node, -1, minsize,
1324 			  (u32) base, (u32) (base >> 32));
1325 		call_prom("close", 1, 0, phb_node);
1326 	}
1327 
1328 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1329 
1330 	/* These are only really needed if there is a memory limit in
1331 	 * effect, but we don't know so export them always. */
1332 	RELOC(prom_tce_alloc_start) = local_alloc_bottom;
1333 	RELOC(prom_tce_alloc_end) = local_alloc_top;
1334 
1335 	/* Flag the first invalid entry */
1336 	prom_debug("ending prom_initialize_tce_table\n");
1337 }
1338 #endif
1339 
1340 /*
1341  * With CHRP SMP we need to use the OF to start the other processors.
1342  * We can't wait until smp_boot_cpus (the OF is trashed by then)
1343  * so we have to put the processors into a holding pattern controlled
1344  * by the kernel (not OF) before we destroy the OF.
1345  *
1346  * This uses a chunk of low memory, puts some holding pattern
1347  * code there and sends the other processors off to there until
1348  * smp_boot_cpus tells them to do something.  The holding pattern
1349  * checks that address until its cpu # is there, when it is that
1350  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
1351  * of setting those values.
1352  *
1353  * We also use physical address 0x4 here to tell when a cpu
1354  * is in its holding pattern code.
1355  *
1356  * -- Cort
1357  */
1358 /*
1359  * We want to reference the copy of __secondary_hold_* in the
1360  * 0 - 0x100 address range
1361  */
1362 #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1363 
1364 static void __init prom_hold_cpus(void)
1365 {
1366 	unsigned long i;
1367 	unsigned int reg;
1368 	phandle node;
1369 	char type[64];
1370 	struct prom_t *_prom = &RELOC(prom);
1371 	unsigned long *spinloop
1372 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
1373 	unsigned long *acknowledge
1374 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1375 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
1376 
1377 	prom_debug("prom_hold_cpus: start...\n");
1378 	prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
1379 	prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
1380 	prom_debug("    1) acknowledge    = 0x%x\n",
1381 		   (unsigned long)acknowledge);
1382 	prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
1383 	prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
1384 
1385 	/* Set the common spinloop variable, so all of the secondary cpus
1386 	 * will block when they are awakened from their OF spinloop.
1387 	 * This must occur for both SMP and non SMP kernels, since OF will
1388 	 * be trashed when we move the kernel.
1389 	 */
1390 	*spinloop = 0;
1391 
1392 	/* look for cpus */
1393 	for (node = 0; prom_next_node(&node); ) {
1394 		type[0] = 0;
1395 		prom_getprop(node, "device_type", type, sizeof(type));
1396 		if (strcmp(type, RELOC("cpu")) != 0)
1397 			continue;
1398 
1399 		/* Skip non-configured cpus. */
1400 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1401 			if (strcmp(type, RELOC("okay")) != 0)
1402 				continue;
1403 
1404 		reg = -1;
1405 		prom_getprop(node, "reg", &reg, sizeof(reg));
1406 
1407 		prom_debug("cpu hw idx   = 0x%x\n", reg);
1408 
1409 		/* Init the acknowledge var which will be reset by
1410 		 * the secondary cpu when it awakens from its OF
1411 		 * spinloop.
1412 		 */
1413 		*acknowledge = (unsigned long)-1;
1414 
1415 		if (reg != _prom->cpu) {
1416 			/* Primary Thread of non-boot cpu */
1417 			prom_printf("starting cpu hw idx %x... ", reg);
1418 			call_prom("start-cpu", 3, 0, node,
1419 				  secondary_hold, reg);
1420 
1421 			for (i = 0; (i < 100000000) &&
1422 			     (*acknowledge == ((unsigned long)-1)); i++ )
1423 				mb();
1424 
1425 			if (*acknowledge == reg)
1426 				prom_printf("done\n");
1427 			else
1428 				prom_printf("failed: %x\n", *acknowledge);
1429 		}
1430 #ifdef CONFIG_SMP
1431 		else
1432 			prom_printf("boot cpu hw idx %x\n", reg);
1433 #endif /* CONFIG_SMP */
1434 	}
1435 
1436 	prom_debug("prom_hold_cpus: end...\n");
1437 }
1438 
1439 
1440 static void __init prom_init_client_services(unsigned long pp)
1441 {
1442 	struct prom_t *_prom = &RELOC(prom);
1443 
1444 	/* Get a handle to the prom entry point before anything else */
1445 	RELOC(prom_entry) = pp;
1446 
1447 	/* get a handle for the stdout device */
1448 	_prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1449 	if (!PHANDLE_VALID(_prom->chosen))
1450 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
1451 
1452 	/* get device tree root */
1453 	_prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1454 	if (!PHANDLE_VALID(_prom->root))
1455 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1456 
1457 	_prom->mmumap = 0;
1458 }
1459 
1460 #ifdef CONFIG_PPC32
1461 /*
1462  * For really old powermacs, we need to map things we claim.
1463  * For that, we need the ihandle of the mmu.
1464  * Also, on the longtrail, we need to work around other bugs.
1465  */
1466 static void __init prom_find_mmu(void)
1467 {
1468 	struct prom_t *_prom = &RELOC(prom);
1469 	phandle oprom;
1470 	char version[64];
1471 
1472 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1473 	if (!PHANDLE_VALID(oprom))
1474 		return;
1475 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1476 		return;
1477 	version[sizeof(version) - 1] = 0;
1478 	/* XXX might need to add other versions here */
1479 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1480 		of_workarounds = OF_WA_CLAIM;
1481 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1482 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1483 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1484 	} else
1485 		return;
1486 	_prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
1487 	prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1488 		     sizeof(_prom->mmumap));
1489 	if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1490 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
1491 }
1492 #else
1493 #define prom_find_mmu()
1494 #endif
1495 
1496 static void __init prom_init_stdout(void)
1497 {
1498 	struct prom_t *_prom = &RELOC(prom);
1499 	char *path = RELOC(of_stdout_device);
1500 	char type[16];
1501 	u32 val;
1502 
1503 	if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1504 		prom_panic("cannot find stdout");
1505 
1506 	_prom->stdout = val;
1507 
1508 	/* Get the full OF pathname of the stdout device */
1509 	memset(path, 0, 256);
1510 	call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1511 	val = call_prom("instance-to-package", 1, 1, _prom->stdout);
1512 	prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1513 		     &val, sizeof(val));
1514 	prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
1515 	prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1516 		     path, strlen(path) + 1);
1517 
1518 	/* If it's a display, note it */
1519 	memset(type, 0, sizeof(type));
1520 	prom_getprop(val, "device_type", type, sizeof(type));
1521 	if (strcmp(type, RELOC("display")) == 0)
1522 		prom_setprop(val, path, "linux,boot-display", NULL, 0);
1523 }
1524 
1525 static void __init prom_close_stdin(void)
1526 {
1527 	struct prom_t *_prom = &RELOC(prom);
1528 	ihandle val;
1529 
1530 	if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1531 		call_prom("close", 1, 0, val);
1532 }
1533 
1534 static int __init prom_find_machine_type(void)
1535 {
1536 	struct prom_t *_prom = &RELOC(prom);
1537 	char compat[256];
1538 	int len, i = 0;
1539 #ifdef CONFIG_PPC64
1540 	phandle rtas;
1541 	int x;
1542 #endif
1543 
1544 	/* Look for a PowerMac */
1545 	len = prom_getprop(_prom->root, "compatible",
1546 			   compat, sizeof(compat)-1);
1547 	if (len > 0) {
1548 		compat[len] = 0;
1549 		while (i < len) {
1550 			char *p = &compat[i];
1551 			int sl = strlen(p);
1552 			if (sl == 0)
1553 				break;
1554 			if (strstr(p, RELOC("Power Macintosh")) ||
1555 			    strstr(p, RELOC("MacRISC")))
1556 				return PLATFORM_POWERMAC;
1557 #ifdef CONFIG_PPC64
1558 			/* We must make sure we don't detect the IBM Cell
1559 			 * blades as pSeries due to some firmware issues,
1560 			 * so we do it here.
1561 			 */
1562 			if (strstr(p, RELOC("IBM,CBEA")) ||
1563 			    strstr(p, RELOC("IBM,CPBW-1.0")))
1564 				return PLATFORM_GENERIC;
1565 #endif /* CONFIG_PPC64 */
1566 			i += sl + 1;
1567 		}
1568 	}
1569 #ifdef CONFIG_PPC64
1570 	/* If not a mac, try to figure out if it's an IBM pSeries or any other
1571 	 * PAPR compliant platform. We assume it is if :
1572 	 *  - /device_type is "chrp" (please, do NOT use that for future
1573 	 *    non-IBM designs !
1574 	 *  - it has /rtas
1575 	 */
1576 	len = prom_getprop(_prom->root, "device_type",
1577 			   compat, sizeof(compat)-1);
1578 	if (len <= 0)
1579 		return PLATFORM_GENERIC;
1580 	if (strcmp(compat, RELOC("chrp")))
1581 		return PLATFORM_GENERIC;
1582 
1583 	/* Default to pSeries. We need to know if we are running LPAR */
1584 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1585 	if (!PHANDLE_VALID(rtas))
1586 		return PLATFORM_GENERIC;
1587 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
1588 	if (x != PROM_ERROR) {
1589 		prom_debug("Hypertas detected, assuming LPAR !\n");
1590 		return PLATFORM_PSERIES_LPAR;
1591 	}
1592 	return PLATFORM_PSERIES;
1593 #else
1594 	return PLATFORM_GENERIC;
1595 #endif
1596 }
1597 
1598 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1599 {
1600 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1601 }
1602 
1603 /*
1604  * If we have a display that we don't know how to drive,
1605  * we will want to try to execute OF's open method for it
1606  * later.  However, OF will probably fall over if we do that
1607  * we've taken over the MMU.
1608  * So we check whether we will need to open the display,
1609  * and if so, open it now.
1610  */
1611 static void __init prom_check_displays(void)
1612 {
1613 	char type[16], *path;
1614 	phandle node;
1615 	ihandle ih;
1616 	int i;
1617 
1618 	static unsigned char default_colors[] = {
1619 		0x00, 0x00, 0x00,
1620 		0x00, 0x00, 0xaa,
1621 		0x00, 0xaa, 0x00,
1622 		0x00, 0xaa, 0xaa,
1623 		0xaa, 0x00, 0x00,
1624 		0xaa, 0x00, 0xaa,
1625 		0xaa, 0xaa, 0x00,
1626 		0xaa, 0xaa, 0xaa,
1627 		0x55, 0x55, 0x55,
1628 		0x55, 0x55, 0xff,
1629 		0x55, 0xff, 0x55,
1630 		0x55, 0xff, 0xff,
1631 		0xff, 0x55, 0x55,
1632 		0xff, 0x55, 0xff,
1633 		0xff, 0xff, 0x55,
1634 		0xff, 0xff, 0xff
1635 	};
1636 	const unsigned char *clut;
1637 
1638 	prom_debug("Looking for displays\n");
1639 	for (node = 0; prom_next_node(&node); ) {
1640 		memset(type, 0, sizeof(type));
1641 		prom_getprop(node, "device_type", type, sizeof(type));
1642 		if (strcmp(type, RELOC("display")) != 0)
1643 			continue;
1644 
1645 		/* It seems OF doesn't null-terminate the path :-( */
1646 		path = RELOC(prom_scratch);
1647 		memset(path, 0, PROM_SCRATCH_SIZE);
1648 
1649 		/*
1650 		 * leave some room at the end of the path for appending extra
1651 		 * arguments
1652 		 */
1653 		if (call_prom("package-to-path", 3, 1, node, path,
1654 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1655 			continue;
1656 		prom_printf("found display   : %s, opening... ", path);
1657 
1658 		ih = call_prom("open", 1, 1, path);
1659 		if (ih == 0) {
1660 			prom_printf("failed\n");
1661 			continue;
1662 		}
1663 
1664 		/* Success */
1665 		prom_printf("done\n");
1666 		prom_setprop(node, path, "linux,opened", NULL, 0);
1667 
1668 		/* Setup a usable color table when the appropriate
1669 		 * method is available. Should update this to set-colors */
1670 		clut = RELOC(default_colors);
1671 		for (i = 0; i < 32; i++, clut += 3)
1672 			if (prom_set_color(ih, i, clut[0], clut[1],
1673 					   clut[2]) != 0)
1674 				break;
1675 
1676 #ifdef CONFIG_LOGO_LINUX_CLUT224
1677 		clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1678 		for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1679 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
1680 					   clut[2]) != 0)
1681 				break;
1682 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
1683 	}
1684 }
1685 
1686 
1687 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1688 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
1689 			      unsigned long needed, unsigned long align)
1690 {
1691 	void *ret;
1692 
1693 	*mem_start = _ALIGN(*mem_start, align);
1694 	while ((*mem_start + needed) > *mem_end) {
1695 		unsigned long room, chunk;
1696 
1697 		prom_debug("Chunk exhausted, claiming more at %x...\n",
1698 			   RELOC(alloc_bottom));
1699 		room = RELOC(alloc_top) - RELOC(alloc_bottom);
1700 		if (room > DEVTREE_CHUNK_SIZE)
1701 			room = DEVTREE_CHUNK_SIZE;
1702 		if (room < PAGE_SIZE)
1703 			prom_panic("No memory for flatten_device_tree (no room)");
1704 		chunk = alloc_up(room, 0);
1705 		if (chunk == 0)
1706 			prom_panic("No memory for flatten_device_tree (claim failed)");
1707 		*mem_end = RELOC(alloc_top);
1708 	}
1709 
1710 	ret = (void *)*mem_start;
1711 	*mem_start += needed;
1712 
1713 	return ret;
1714 }
1715 
1716 #define dt_push_token(token, mem_start, mem_end) \
1717 	do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
1718 
1719 static unsigned long __init dt_find_string(char *str)
1720 {
1721 	char *s, *os;
1722 
1723 	s = os = (char *)RELOC(dt_string_start);
1724 	s += 4;
1725 	while (s <  (char *)RELOC(dt_string_end)) {
1726 		if (strcmp(s, str) == 0)
1727 			return s - os;
1728 		s += strlen(s) + 1;
1729 	}
1730 	return 0;
1731 }
1732 
1733 /*
1734  * The Open Firmware 1275 specification states properties must be 31 bytes or
1735  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
1736  */
1737 #define MAX_PROPERTY_NAME 64
1738 
1739 static void __init scan_dt_build_strings(phandle node,
1740 					 unsigned long *mem_start,
1741 					 unsigned long *mem_end)
1742 {
1743 	char *prev_name, *namep, *sstart;
1744 	unsigned long soff;
1745 	phandle child;
1746 
1747 	sstart =  (char *)RELOC(dt_string_start);
1748 
1749 	/* get and store all property names */
1750 	prev_name = RELOC("");
1751 	for (;;) {
1752 		/* 64 is max len of name including nul. */
1753 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
1754 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
1755 			/* No more nodes: unwind alloc */
1756 			*mem_start = (unsigned long)namep;
1757 			break;
1758 		}
1759 
1760  		/* skip "name" */
1761  		if (strcmp(namep, RELOC("name")) == 0) {
1762  			*mem_start = (unsigned long)namep;
1763  			prev_name = RELOC("name");
1764  			continue;
1765  		}
1766 		/* get/create string entry */
1767 		soff = dt_find_string(namep);
1768 		if (soff != 0) {
1769 			*mem_start = (unsigned long)namep;
1770 			namep = sstart + soff;
1771 		} else {
1772 			/* Trim off some if we can */
1773 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
1774 			RELOC(dt_string_end) = *mem_start;
1775 		}
1776 		prev_name = namep;
1777 	}
1778 
1779 	/* do all our children */
1780 	child = call_prom("child", 1, 1, node);
1781 	while (child != 0) {
1782 		scan_dt_build_strings(child, mem_start, mem_end);
1783 		child = call_prom("peer", 1, 1, child);
1784 	}
1785 }
1786 
1787 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
1788 					unsigned long *mem_end)
1789 {
1790 	phandle child;
1791 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
1792 	unsigned long soff;
1793 	unsigned char *valp;
1794 	static char pname[MAX_PROPERTY_NAME];
1795 	int l, room;
1796 
1797 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
1798 
1799 	/* get the node's full name */
1800 	namep = (char *)*mem_start;
1801 	room = *mem_end - *mem_start;
1802 	if (room > 255)
1803 		room = 255;
1804 	l = call_prom("package-to-path", 3, 1, node, namep, room);
1805 	if (l >= 0) {
1806 		/* Didn't fit?  Get more room. */
1807 		if (l >= room) {
1808 			if (l >= *mem_end - *mem_start)
1809 				namep = make_room(mem_start, mem_end, l+1, 1);
1810 			call_prom("package-to-path", 3, 1, node, namep, l);
1811 		}
1812 		namep[l] = '\0';
1813 
1814 		/* Fixup an Apple bug where they have bogus \0 chars in the
1815 		 * middle of the path in some properties, and extract
1816 		 * the unit name (everything after the last '/').
1817 		 */
1818 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
1819 			if (*p == '/')
1820 				lp = namep;
1821 			else if (*p != 0)
1822 				*lp++ = *p;
1823 		}
1824 		*lp = 0;
1825 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
1826 	}
1827 
1828 	/* get it again for debugging */
1829 	path = RELOC(prom_scratch);
1830 	memset(path, 0, PROM_SCRATCH_SIZE);
1831 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1832 
1833 	/* get and store all properties */
1834 	prev_name = RELOC("");
1835 	sstart = (char *)RELOC(dt_string_start);
1836 	for (;;) {
1837 		if (call_prom("nextprop", 3, 1, node, prev_name,
1838 			      RELOC(pname)) != 1)
1839 			break;
1840 
1841  		/* skip "name" */
1842  		if (strcmp(RELOC(pname), RELOC("name")) == 0) {
1843  			prev_name = RELOC("name");
1844  			continue;
1845  		}
1846 
1847 		/* find string offset */
1848 		soff = dt_find_string(RELOC(pname));
1849 		if (soff == 0) {
1850 			prom_printf("WARNING: Can't find string index for"
1851 				    " <%s>, node %s\n", RELOC(pname), path);
1852 			break;
1853 		}
1854 		prev_name = sstart + soff;
1855 
1856 		/* get length */
1857 		l = call_prom("getproplen", 2, 1, node, RELOC(pname));
1858 
1859 		/* sanity checks */
1860 		if (l == PROM_ERROR)
1861 			continue;
1862 		if (l > MAX_PROPERTY_LENGTH) {
1863 			prom_printf("WARNING: ignoring large property ");
1864 			/* It seems OF doesn't null-terminate the path :-( */
1865 			prom_printf("[%s] ", path);
1866 			prom_printf("%s length 0x%x\n", RELOC(pname), l);
1867 			continue;
1868 		}
1869 
1870 		/* push property head */
1871 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
1872 		dt_push_token(l, mem_start, mem_end);
1873 		dt_push_token(soff, mem_start, mem_end);
1874 
1875 		/* push property content */
1876 		valp = make_room(mem_start, mem_end, l, 4);
1877 		call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
1878 		*mem_start = _ALIGN(*mem_start, 4);
1879 	}
1880 
1881 	/* Add a "linux,phandle" property. */
1882 	soff = dt_find_string(RELOC("linux,phandle"));
1883 	if (soff == 0)
1884 		prom_printf("WARNING: Can't find string index for"
1885 			    " <linux-phandle> node %s\n", path);
1886 	else {
1887 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
1888 		dt_push_token(4, mem_start, mem_end);
1889 		dt_push_token(soff, mem_start, mem_end);
1890 		valp = make_room(mem_start, mem_end, 4, 4);
1891 		*(u32 *)valp = node;
1892 	}
1893 
1894 	/* do all our children */
1895 	child = call_prom("child", 1, 1, node);
1896 	while (child != 0) {
1897 		scan_dt_build_struct(child, mem_start, mem_end);
1898 		child = call_prom("peer", 1, 1, child);
1899 	}
1900 
1901 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
1902 }
1903 
1904 static void __init flatten_device_tree(void)
1905 {
1906 	phandle root;
1907 	unsigned long mem_start, mem_end, room;
1908 	struct boot_param_header *hdr;
1909 	struct prom_t *_prom = &RELOC(prom);
1910 	char *namep;
1911 	u64 *rsvmap;
1912 
1913 	/*
1914 	 * Check how much room we have between alloc top & bottom (+/- a
1915 	 * few pages), crop to 4Mb, as this is our "chuck" size
1916 	 */
1917 	room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
1918 	if (room > DEVTREE_CHUNK_SIZE)
1919 		room = DEVTREE_CHUNK_SIZE;
1920 	prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
1921 
1922 	/* Now try to claim that */
1923 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
1924 	if (mem_start == 0)
1925 		prom_panic("Can't allocate initial device-tree chunk\n");
1926 	mem_end = RELOC(alloc_top);
1927 
1928 	/* Get root of tree */
1929 	root = call_prom("peer", 1, 1, (phandle)0);
1930 	if (root == (phandle)0)
1931 		prom_panic ("couldn't get device tree root\n");
1932 
1933 	/* Build header and make room for mem rsv map */
1934 	mem_start = _ALIGN(mem_start, 4);
1935 	hdr = make_room(&mem_start, &mem_end,
1936 			sizeof(struct boot_param_header), 4);
1937 	RELOC(dt_header_start) = (unsigned long)hdr;
1938 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
1939 
1940 	/* Start of strings */
1941 	mem_start = PAGE_ALIGN(mem_start);
1942 	RELOC(dt_string_start) = mem_start;
1943 	mem_start += 4; /* hole */
1944 
1945 	/* Add "linux,phandle" in there, we'll need it */
1946 	namep = make_room(&mem_start, &mem_end, 16, 1);
1947 	strcpy(namep, RELOC("linux,phandle"));
1948 	mem_start = (unsigned long)namep + strlen(namep) + 1;
1949 
1950 	/* Build string array */
1951 	prom_printf("Building dt strings...\n");
1952 	scan_dt_build_strings(root, &mem_start, &mem_end);
1953 	RELOC(dt_string_end) = mem_start;
1954 
1955 	/* Build structure */
1956 	mem_start = PAGE_ALIGN(mem_start);
1957 	RELOC(dt_struct_start) = mem_start;
1958 	prom_printf("Building dt structure...\n");
1959 	scan_dt_build_struct(root, &mem_start, &mem_end);
1960 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
1961 	RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
1962 
1963 	/* Finish header */
1964 	hdr->boot_cpuid_phys = _prom->cpu;
1965 	hdr->magic = OF_DT_HEADER;
1966 	hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
1967 	hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
1968 	hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
1969 	hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
1970 	hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
1971 	hdr->version = OF_DT_VERSION;
1972 	/* Version 16 is not backward compatible */
1973 	hdr->last_comp_version = 0x10;
1974 
1975 	/* Copy the reserve map in */
1976 	memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
1977 
1978 #ifdef DEBUG_PROM
1979 	{
1980 		int i;
1981 		prom_printf("reserved memory map:\n");
1982 		for (i = 0; i < RELOC(mem_reserve_cnt); i++)
1983 			prom_printf("  %x - %x\n",
1984 				    RELOC(mem_reserve_map)[i].base,
1985 				    RELOC(mem_reserve_map)[i].size);
1986 	}
1987 #endif
1988 	/* Bump mem_reserve_cnt to cause further reservations to fail
1989 	 * since it's too late.
1990 	 */
1991 	RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
1992 
1993 	prom_printf("Device tree strings 0x%x -> 0x%x\n",
1994 		    RELOC(dt_string_start), RELOC(dt_string_end));
1995 	prom_printf("Device tree struct  0x%x -> 0x%x\n",
1996 		    RELOC(dt_struct_start), RELOC(dt_struct_end));
1997 
1998 }
1999 
2000 #ifdef CONFIG_PPC_MAPLE
2001 /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2002  * The values are bad, and it doesn't even have the right number of cells. */
2003 static void __init fixup_device_tree_maple(void)
2004 {
2005 	phandle isa;
2006 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
2007 	u32 isa_ranges[6];
2008 	char *name;
2009 
2010 	name = "/ht@0/isa@4";
2011 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2012 	if (!PHANDLE_VALID(isa)) {
2013 		name = "/ht@0/isa@6";
2014 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2015 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2016 	}
2017 	if (!PHANDLE_VALID(isa))
2018 		return;
2019 
2020 	if (prom_getproplen(isa, "ranges") != 12)
2021 		return;
2022 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2023 		== PROM_ERROR)
2024 		return;
2025 
2026 	if (isa_ranges[0] != 0x1 ||
2027 		isa_ranges[1] != 0xf4000000 ||
2028 		isa_ranges[2] != 0x00010000)
2029 		return;
2030 
2031 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
2032 
2033 	isa_ranges[0] = 0x1;
2034 	isa_ranges[1] = 0x0;
2035 	isa_ranges[2] = rloc;
2036 	isa_ranges[3] = 0x0;
2037 	isa_ranges[4] = 0x0;
2038 	isa_ranges[5] = 0x00010000;
2039 	prom_setprop(isa, name, "ranges",
2040 			isa_ranges, sizeof(isa_ranges));
2041 }
2042 
2043 #define CPC925_MC_START		0xf8000000
2044 #define CPC925_MC_LENGTH	0x1000000
2045 /* The values for memory-controller don't have right number of cells */
2046 static void __init fixup_device_tree_maple_memory_controller(void)
2047 {
2048 	phandle mc;
2049 	u32 mc_reg[4];
2050 	char *name = "/hostbridge@f8000000";
2051 	struct prom_t *_prom = &RELOC(prom);
2052 	u32 ac, sc;
2053 
2054 	mc = call_prom("finddevice", 1, 1, ADDR(name));
2055 	if (!PHANDLE_VALID(mc))
2056 		return;
2057 
2058 	if (prom_getproplen(mc, "reg") != 8)
2059 		return;
2060 
2061 	prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
2062 	prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
2063 	if ((ac != 2) || (sc != 2))
2064 		return;
2065 
2066 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
2067 		return;
2068 
2069 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
2070 		return;
2071 
2072 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
2073 
2074 	mc_reg[0] = 0x0;
2075 	mc_reg[1] = CPC925_MC_START;
2076 	mc_reg[2] = 0x0;
2077 	mc_reg[3] = CPC925_MC_LENGTH;
2078 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
2079 }
2080 #else
2081 #define fixup_device_tree_maple()
2082 #define fixup_device_tree_maple_memory_controller()
2083 #endif
2084 
2085 #ifdef CONFIG_PPC_CHRP
2086 /*
2087  * Pegasos and BriQ lacks the "ranges" property in the isa node
2088  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2089  * Pegasos has the IDE configured in legacy mode, but advertised as native
2090  */
2091 static void __init fixup_device_tree_chrp(void)
2092 {
2093 	phandle ph;
2094 	u32 prop[6];
2095 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2096 	char *name;
2097 	int rc;
2098 
2099 	name = "/pci@80000000/isa@c";
2100 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2101 	if (!PHANDLE_VALID(ph)) {
2102 		name = "/pci@ff500000/isa@6";
2103 		ph = call_prom("finddevice", 1, 1, ADDR(name));
2104 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2105 	}
2106 	if (PHANDLE_VALID(ph)) {
2107 		rc = prom_getproplen(ph, "ranges");
2108 		if (rc == 0 || rc == PROM_ERROR) {
2109 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2110 
2111 			prop[0] = 0x1;
2112 			prop[1] = 0x0;
2113 			prop[2] = rloc;
2114 			prop[3] = 0x0;
2115 			prop[4] = 0x0;
2116 			prop[5] = 0x00010000;
2117 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2118 		}
2119 	}
2120 
2121 	name = "/pci@80000000/ide@C,1";
2122 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2123 	if (PHANDLE_VALID(ph)) {
2124 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2125 		prop[0] = 14;
2126 		prop[1] = 0x0;
2127 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2128 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2129 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2130 		if (rc == sizeof(u32)) {
2131 			prop[0] &= ~0x5;
2132 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2133 		}
2134 	}
2135 }
2136 #else
2137 #define fixup_device_tree_chrp()
2138 #endif
2139 
2140 #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
2141 static void __init fixup_device_tree_pmac(void)
2142 {
2143 	phandle u3, i2c, mpic;
2144 	u32 u3_rev;
2145 	u32 interrupts[2];
2146 	u32 parent;
2147 
2148 	/* Some G5s have a missing interrupt definition, fix it up here */
2149 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2150 	if (!PHANDLE_VALID(u3))
2151 		return;
2152 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2153 	if (!PHANDLE_VALID(i2c))
2154 		return;
2155 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2156 	if (!PHANDLE_VALID(mpic))
2157 		return;
2158 
2159 	/* check if proper rev of u3 */
2160 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2161 	    == PROM_ERROR)
2162 		return;
2163 	if (u3_rev < 0x35 || u3_rev > 0x39)
2164 		return;
2165 	/* does it need fixup ? */
2166 	if (prom_getproplen(i2c, "interrupts") > 0)
2167 		return;
2168 
2169 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2170 
2171 	/* interrupt on this revision of u3 is number 0 and level */
2172 	interrupts[0] = 0;
2173 	interrupts[1] = 1;
2174 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2175 		     &interrupts, sizeof(interrupts));
2176 	parent = (u32)mpic;
2177 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2178 		     &parent, sizeof(parent));
2179 }
2180 #else
2181 #define fixup_device_tree_pmac()
2182 #endif
2183 
2184 #ifdef CONFIG_PPC_EFIKA
2185 /*
2186  * The MPC5200 FEC driver requires an phy-handle property to tell it how
2187  * to talk to the phy.  If the phy-handle property is missing, then this
2188  * function is called to add the appropriate nodes and link it to the
2189  * ethernet node.
2190  */
2191 static void __init fixup_device_tree_efika_add_phy(void)
2192 {
2193 	u32 node;
2194 	char prop[64];
2195 	int rv;
2196 
2197 	/* Check if /builtin/ethernet exists - bail if it doesn't */
2198 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
2199 	if (!PHANDLE_VALID(node))
2200 		return;
2201 
2202 	/* Check if the phy-handle property exists - bail if it does */
2203 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2204 	if (!rv)
2205 		return;
2206 
2207 	/*
2208 	 * At this point the ethernet device doesn't have a phy described.
2209 	 * Now we need to add the missing phy node and linkage
2210 	 */
2211 
2212 	/* Check for an MDIO bus node - if missing then create one */
2213 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2214 	if (!PHANDLE_VALID(node)) {
2215 		prom_printf("Adding Ethernet MDIO node\n");
2216 		call_prom("interpret", 1, 1,
2217 			" s\" /builtin\" find-device"
2218 			" new-device"
2219 				" 1 encode-int s\" #address-cells\" property"
2220 				" 0 encode-int s\" #size-cells\" property"
2221 				" s\" mdio\" device-name"
2222 				" s\" fsl,mpc5200b-mdio\" encode-string"
2223 				" s\" compatible\" property"
2224 				" 0xf0003000 0x400 reg"
2225 				" 0x2 encode-int"
2226 				" 0x5 encode-int encode+"
2227 				" 0x3 encode-int encode+"
2228 				" s\" interrupts\" property"
2229 			" finish-device");
2230 	};
2231 
2232 	/* Check for a PHY device node - if missing then create one and
2233 	 * give it's phandle to the ethernet node */
2234 	node = call_prom("finddevice", 1, 1,
2235 			 ADDR("/builtin/mdio/ethernet-phy"));
2236 	if (!PHANDLE_VALID(node)) {
2237 		prom_printf("Adding Ethernet PHY node\n");
2238 		call_prom("interpret", 1, 1,
2239 			" s\" /builtin/mdio\" find-device"
2240 			" new-device"
2241 				" s\" ethernet-phy\" device-name"
2242 				" 0x10 encode-int s\" reg\" property"
2243 				" my-self"
2244 				" ihandle>phandle"
2245 			" finish-device"
2246 			" s\" /builtin/ethernet\" find-device"
2247 				" encode-int"
2248 				" s\" phy-handle\" property"
2249 			" device-end");
2250 	}
2251 }
2252 
2253 static void __init fixup_device_tree_efika(void)
2254 {
2255 	int sound_irq[3] = { 2, 2, 0 };
2256 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2257 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
2258 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
2259 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2260 	u32 node;
2261 	char prop[64];
2262 	int rv, len;
2263 
2264 	/* Check if we're really running on a EFIKA */
2265 	node = call_prom("finddevice", 1, 1, ADDR("/"));
2266 	if (!PHANDLE_VALID(node))
2267 		return;
2268 
2269 	rv = prom_getprop(node, "model", prop, sizeof(prop));
2270 	if (rv == PROM_ERROR)
2271 		return;
2272 	if (strcmp(prop, "EFIKA5K2"))
2273 		return;
2274 
2275 	prom_printf("Applying EFIKA device tree fixups\n");
2276 
2277 	/* Claiming to be 'chrp' is death */
2278 	node = call_prom("finddevice", 1, 1, ADDR("/"));
2279 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2280 	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2281 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2282 
2283 	/* CODEGEN,description is exposed in /proc/cpuinfo so
2284 	   fix that too */
2285 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2286 	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2287 		prom_setprop(node, "/", "CODEGEN,description",
2288 			     "Efika 5200B PowerPC System",
2289 			     sizeof("Efika 5200B PowerPC System"));
2290 
2291 	/* Fixup bestcomm interrupts property */
2292 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2293 	if (PHANDLE_VALID(node)) {
2294 		len = prom_getproplen(node, "interrupts");
2295 		if (len == 12) {
2296 			prom_printf("Fixing bestcomm interrupts property\n");
2297 			prom_setprop(node, "/builtin/bestcom", "interrupts",
2298 				     bcomm_irq, sizeof(bcomm_irq));
2299 		}
2300 	}
2301 
2302 	/* Fixup sound interrupts property */
2303 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2304 	if (PHANDLE_VALID(node)) {
2305 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2306 		if (rv == PROM_ERROR) {
2307 			prom_printf("Adding sound interrupts property\n");
2308 			prom_setprop(node, "/builtin/sound", "interrupts",
2309 				     sound_irq, sizeof(sound_irq));
2310 		}
2311 	}
2312 
2313 	/* Make sure ethernet phy-handle property exists */
2314 	fixup_device_tree_efika_add_phy();
2315 }
2316 #else
2317 #define fixup_device_tree_efika()
2318 #endif
2319 
2320 static void __init fixup_device_tree(void)
2321 {
2322 	fixup_device_tree_maple();
2323 	fixup_device_tree_maple_memory_controller();
2324 	fixup_device_tree_chrp();
2325 	fixup_device_tree_pmac();
2326 	fixup_device_tree_efika();
2327 }
2328 
2329 static void __init prom_find_boot_cpu(void)
2330 {
2331 	struct prom_t *_prom = &RELOC(prom);
2332 	u32 getprop_rval;
2333 	ihandle prom_cpu;
2334 	phandle cpu_pkg;
2335 
2336 	_prom->cpu = 0;
2337 	if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
2338 		return;
2339 
2340 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2341 
2342 	prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
2343 	_prom->cpu = getprop_rval;
2344 
2345 	prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
2346 }
2347 
2348 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2349 {
2350 #ifdef CONFIG_BLK_DEV_INITRD
2351 	struct prom_t *_prom = &RELOC(prom);
2352 
2353 	if (r3 && r4 && r4 != 0xdeadbeef) {
2354 		unsigned long val;
2355 
2356 		RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
2357 		RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2358 
2359 		val = RELOC(prom_initrd_start);
2360 		prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2361 			     &val, sizeof(val));
2362 		val = RELOC(prom_initrd_end);
2363 		prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2364 			     &val, sizeof(val));
2365 
2366 		reserve_mem(RELOC(prom_initrd_start),
2367 			    RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
2368 
2369 		prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
2370 		prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
2371 	}
2372 #endif /* CONFIG_BLK_DEV_INITRD */
2373 }
2374 
2375 /*
2376  * We enter here early on, when the Open Firmware prom is still
2377  * handling exceptions and the MMU hash table for us.
2378  */
2379 
2380 unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2381 			       unsigned long pp,
2382 			       unsigned long r6, unsigned long r7,
2383 			       unsigned long kbase)
2384 {
2385 	struct prom_t *_prom;
2386 	unsigned long hdr;
2387 
2388 #ifdef CONFIG_PPC32
2389 	unsigned long offset = reloc_offset();
2390 	reloc_got2(offset);
2391 #endif
2392 
2393 	_prom = &RELOC(prom);
2394 
2395 	/*
2396 	 * First zero the BSS
2397 	 */
2398 	memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
2399 
2400 	/*
2401 	 * Init interface to Open Firmware, get some node references,
2402 	 * like /chosen
2403 	 */
2404 	prom_init_client_services(pp);
2405 
2406 	/*
2407 	 * See if this OF is old enough that we need to do explicit maps
2408 	 * and other workarounds
2409 	 */
2410 	prom_find_mmu();
2411 
2412 	/*
2413 	 * Init prom stdout device
2414 	 */
2415 	prom_init_stdout();
2416 
2417 	prom_printf("Preparing to boot %s", RELOC(linux_banner));
2418 
2419 	/*
2420 	 * Get default machine type. At this point, we do not differentiate
2421 	 * between pSeries SMP and pSeries LPAR
2422 	 */
2423 	RELOC(of_platform) = prom_find_machine_type();
2424 
2425 #ifndef CONFIG_RELOCATABLE
2426 	/* Bail if this is a kdump kernel. */
2427 	if (PHYSICAL_START > 0)
2428 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
2429 #endif
2430 
2431 	/*
2432 	 * Check for an initrd
2433 	 */
2434 	prom_check_initrd(r3, r4);
2435 
2436 #ifdef CONFIG_PPC_PSERIES
2437 	/*
2438 	 * On pSeries, inform the firmware about our capabilities
2439 	 */
2440 	if (RELOC(of_platform) == PLATFORM_PSERIES ||
2441 	    RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
2442 		prom_send_capabilities();
2443 #endif
2444 
2445 	/*
2446 	 * Copy the CPU hold code
2447 	 */
2448 	if (RELOC(of_platform) != PLATFORM_POWERMAC)
2449 		copy_and_flush(0, kbase, 0x100, 0);
2450 
2451 	/*
2452 	 * Do early parsing of command line
2453 	 */
2454 	early_cmdline_parse();
2455 
2456 	/*
2457 	 * Initialize memory management within prom_init
2458 	 */
2459 	prom_init_mem();
2460 
2461 	/*
2462 	 * Determine which cpu is actually running right _now_
2463 	 */
2464 	prom_find_boot_cpu();
2465 
2466 	/*
2467 	 * Initialize display devices
2468 	 */
2469 	prom_check_displays();
2470 
2471 #ifdef CONFIG_PPC64
2472 	/*
2473 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2474 	 * that uses the allocator, we need to make sure we get the top of memory
2475 	 * available for us here...
2476 	 */
2477 	if (RELOC(of_platform) == PLATFORM_PSERIES)
2478 		prom_initialize_tce_table();
2479 #endif
2480 
2481 	/*
2482 	 * On non-powermacs, try to instantiate RTAS and puts all CPUs
2483 	 * in spin-loops. PowerMacs don't have a working RTAS and use
2484 	 * a different way to spin CPUs
2485 	 */
2486 	if (RELOC(of_platform) != PLATFORM_POWERMAC) {
2487 		prom_instantiate_rtas();
2488 		prom_hold_cpus();
2489 	}
2490 
2491 	/*
2492 	 * Fill in some infos for use by the kernel later on
2493 	 */
2494 	if (RELOC(prom_memory_limit))
2495 		prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
2496 			     &RELOC(prom_memory_limit),
2497 			     sizeof(prom_memory_limit));
2498 #ifdef CONFIG_PPC64
2499 	if (RELOC(prom_iommu_off))
2500 		prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2501 			     NULL, 0);
2502 
2503 	if (RELOC(prom_iommu_force_on))
2504 		prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2505 			     NULL, 0);
2506 
2507 	if (RELOC(prom_tce_alloc_start)) {
2508 		prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
2509 			     &RELOC(prom_tce_alloc_start),
2510 			     sizeof(prom_tce_alloc_start));
2511 		prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
2512 			     &RELOC(prom_tce_alloc_end),
2513 			     sizeof(prom_tce_alloc_end));
2514 	}
2515 #endif
2516 
2517 	/*
2518 	 * Fixup any known bugs in the device-tree
2519 	 */
2520 	fixup_device_tree();
2521 
2522 	/*
2523 	 * Now finally create the flattened device-tree
2524 	 */
2525 	prom_printf("copying OF device tree...\n");
2526 	flatten_device_tree();
2527 
2528 	/*
2529 	 * in case stdin is USB and still active on IBM machines...
2530 	 * Unfortunately quiesce crashes on some powermacs if we have
2531 	 * closed stdin already (in particular the powerbook 101).
2532 	 */
2533 	if (RELOC(of_platform) != PLATFORM_POWERMAC)
2534 		prom_close_stdin();
2535 
2536 	/*
2537 	 * Call OF "quiesce" method to shut down pending DMA's from
2538 	 * devices etc...
2539 	 */
2540 	prom_printf("Calling quiesce...\n");
2541 	call_prom("quiesce", 0, 0);
2542 
2543 	/*
2544 	 * And finally, call the kernel passing it the flattened device
2545 	 * tree and NULL as r5, thus triggering the new entry point which
2546 	 * is common to us and kexec
2547 	 */
2548 	hdr = RELOC(dt_header_start);
2549 	prom_printf("returning from prom_init\n");
2550 	prom_debug("->dt_header_start=0x%x\n", hdr);
2551 
2552 #ifdef CONFIG_PPC32
2553 	reloc_got2(-offset);
2554 #endif
2555 
2556 	__start(hdr, kbase, 0);
2557 
2558 	return 0;
2559 }
2560