xref: /openbmc/linux/arch/powerpc/kernel/prom_init.c (revision 711aab1d)
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 /* we cannot use FORTIFY as it brings in new symbols */
19 #define __NO_FORTIFY
20 
21 #include <stdarg.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/init.h>
25 #include <linux/threads.h>
26 #include <linux/spinlock.h>
27 #include <linux/types.h>
28 #include <linux/pci.h>
29 #include <linux/proc_fs.h>
30 #include <linux/stringify.h>
31 #include <linux/delay.h>
32 #include <linux/initrd.h>
33 #include <linux/bitops.h>
34 #include <asm/prom.h>
35 #include <asm/rtas.h>
36 #include <asm/page.h>
37 #include <asm/processor.h>
38 #include <asm/irq.h>
39 #include <asm/io.h>
40 #include <asm/smp.h>
41 #include <asm/mmu.h>
42 #include <asm/pgtable.h>
43 #include <asm/iommu.h>
44 #include <asm/btext.h>
45 #include <asm/sections.h>
46 #include <asm/machdep.h>
47 #include <asm/opal.h>
48 #include <asm/asm-prototypes.h>
49 
50 #include <linux/linux_logo.h>
51 
52 /*
53  * Eventually bump that one up
54  */
55 #define DEVTREE_CHUNK_SIZE	0x100000
56 
57 /*
58  * This is the size of the local memory reserve map that gets copied
59  * into the boot params passed to the kernel. That size is totally
60  * flexible as the kernel just reads the list until it encounters an
61  * entry with size 0, so it can be changed without breaking binary
62  * compatibility
63  */
64 #define MEM_RESERVE_MAP_SIZE	8
65 
66 /*
67  * prom_init() is called very early on, before the kernel text
68  * and data have been mapped to KERNELBASE.  At this point the code
69  * is running at whatever address it has been loaded at.
70  * On ppc32 we compile with -mrelocatable, which means that references
71  * to extern and static variables get relocated automatically.
72  * ppc64 objects are always relocatable, we just need to relocate the
73  * TOC.
74  *
75  * Because OF may have mapped I/O devices into the area starting at
76  * KERNELBASE, particularly on CHRP machines, we can't safely call
77  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
78  * OF calls must be done within prom_init().
79  *
80  * ADDR is used in calls to call_prom.  The 4th and following
81  * arguments to call_prom should be 32-bit values.
82  * On ppc64, 64 bit values are truncated to 32 bits (and
83  * fortunately don't get interpreted as two arguments).
84  */
85 #define ADDR(x)		(u32)(unsigned long)(x)
86 
87 #ifdef CONFIG_PPC64
88 #define OF_WORKAROUNDS	0
89 #else
90 #define OF_WORKAROUNDS	of_workarounds
91 int of_workarounds;
92 #endif
93 
94 #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
95 #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
96 
97 #define PROM_BUG() do {						\
98         prom_printf("kernel BUG at %s line 0x%x!\n",		\
99 		    __FILE__, __LINE__);			\
100         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
101 } while (0)
102 
103 #ifdef DEBUG_PROM
104 #define prom_debug(x...)	prom_printf(x)
105 #else
106 #define prom_debug(x...)
107 #endif
108 
109 
110 typedef u32 prom_arg_t;
111 
112 struct prom_args {
113         __be32 service;
114         __be32 nargs;
115         __be32 nret;
116         __be32 args[10];
117 };
118 
119 struct prom_t {
120 	ihandle root;
121 	phandle chosen;
122 	int cpu;
123 	ihandle stdout;
124 	ihandle mmumap;
125 	ihandle memory;
126 };
127 
128 struct mem_map_entry {
129 	__be64	base;
130 	__be64	size;
131 };
132 
133 typedef __be32 cell_t;
134 
135 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5,
136 		    unsigned long r6, unsigned long r7, unsigned long r8,
137 		    unsigned long r9);
138 
139 #ifdef CONFIG_PPC64
140 extern int enter_prom(struct prom_args *args, unsigned long entry);
141 #else
142 static inline int enter_prom(struct prom_args *args, unsigned long entry)
143 {
144 	return ((int (*)(struct prom_args *))entry)(args);
145 }
146 #endif
147 
148 extern void copy_and_flush(unsigned long dest, unsigned long src,
149 			   unsigned long size, unsigned long offset);
150 
151 /* prom structure */
152 static struct prom_t __initdata prom;
153 
154 static unsigned long prom_entry __initdata;
155 
156 #define PROM_SCRATCH_SIZE 256
157 
158 static char __initdata of_stdout_device[256];
159 static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
160 
161 static unsigned long __initdata dt_header_start;
162 static unsigned long __initdata dt_struct_start, dt_struct_end;
163 static unsigned long __initdata dt_string_start, dt_string_end;
164 
165 static unsigned long __initdata prom_initrd_start, prom_initrd_end;
166 
167 #ifdef CONFIG_PPC64
168 static int __initdata prom_iommu_force_on;
169 static int __initdata prom_iommu_off;
170 static unsigned long __initdata prom_tce_alloc_start;
171 static unsigned long __initdata prom_tce_alloc_end;
172 #endif
173 
174 static bool __initdata prom_radix_disable;
175 
176 struct platform_support {
177 	bool hash_mmu;
178 	bool radix_mmu;
179 	bool radix_gtse;
180 	bool xive;
181 };
182 
183 /* Platforms codes are now obsolete in the kernel. Now only used within this
184  * file and ultimately gone too. Feel free to change them if you need, they
185  * are not shared with anything outside of this file anymore
186  */
187 #define PLATFORM_PSERIES	0x0100
188 #define PLATFORM_PSERIES_LPAR	0x0101
189 #define PLATFORM_LPAR		0x0001
190 #define PLATFORM_POWERMAC	0x0400
191 #define PLATFORM_GENERIC	0x0500
192 #define PLATFORM_OPAL		0x0600
193 
194 static int __initdata of_platform;
195 
196 static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
197 
198 static unsigned long __initdata prom_memory_limit;
199 
200 static unsigned long __initdata alloc_top;
201 static unsigned long __initdata alloc_top_high;
202 static unsigned long __initdata alloc_bottom;
203 static unsigned long __initdata rmo_top;
204 static unsigned long __initdata ram_top;
205 
206 static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
207 static int __initdata mem_reserve_cnt;
208 
209 static cell_t __initdata regbuf[1024];
210 
211 static bool rtas_has_query_cpu_stopped;
212 
213 
214 /*
215  * Error results ... some OF calls will return "-1" on error, some
216  * will return 0, some will return either. To simplify, here are
217  * macros to use with any ihandle or phandle return value to check if
218  * it is valid
219  */
220 
221 #define PROM_ERROR		(-1u)
222 #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
223 #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
224 
225 
226 /* This is the one and *ONLY* place where we actually call open
227  * firmware.
228  */
229 
230 static int __init call_prom(const char *service, int nargs, int nret, ...)
231 {
232 	int i;
233 	struct prom_args args;
234 	va_list list;
235 
236 	args.service = cpu_to_be32(ADDR(service));
237 	args.nargs = cpu_to_be32(nargs);
238 	args.nret = cpu_to_be32(nret);
239 
240 	va_start(list, nret);
241 	for (i = 0; i < nargs; i++)
242 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
243 	va_end(list);
244 
245 	for (i = 0; i < nret; i++)
246 		args.args[nargs+i] = 0;
247 
248 	if (enter_prom(&args, prom_entry) < 0)
249 		return PROM_ERROR;
250 
251 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
252 }
253 
254 static int __init call_prom_ret(const char *service, int nargs, int nret,
255 				prom_arg_t *rets, ...)
256 {
257 	int i;
258 	struct prom_args args;
259 	va_list list;
260 
261 	args.service = cpu_to_be32(ADDR(service));
262 	args.nargs = cpu_to_be32(nargs);
263 	args.nret = cpu_to_be32(nret);
264 
265 	va_start(list, rets);
266 	for (i = 0; i < nargs; i++)
267 		args.args[i] = cpu_to_be32(va_arg(list, prom_arg_t));
268 	va_end(list);
269 
270 	for (i = 0; i < nret; i++)
271 		args.args[nargs+i] = 0;
272 
273 	if (enter_prom(&args, prom_entry) < 0)
274 		return PROM_ERROR;
275 
276 	if (rets != NULL)
277 		for (i = 1; i < nret; ++i)
278 			rets[i-1] = be32_to_cpu(args.args[nargs+i]);
279 
280 	return (nret > 0) ? be32_to_cpu(args.args[nargs]) : 0;
281 }
282 
283 
284 static void __init prom_print(const char *msg)
285 {
286 	const char *p, *q;
287 
288 	if (prom.stdout == 0)
289 		return;
290 
291 	for (p = msg; *p != 0; p = q) {
292 		for (q = p; *q != 0 && *q != '\n'; ++q)
293 			;
294 		if (q > p)
295 			call_prom("write", 3, 1, prom.stdout, p, q - p);
296 		if (*q == 0)
297 			break;
298 		++q;
299 		call_prom("write", 3, 1, prom.stdout, ADDR("\r\n"), 2);
300 	}
301 }
302 
303 
304 static void __init prom_print_hex(unsigned long val)
305 {
306 	int i, nibbles = sizeof(val)*2;
307 	char buf[sizeof(val)*2+1];
308 
309 	for (i = nibbles-1;  i >= 0;  i--) {
310 		buf[i] = (val & 0xf) + '0';
311 		if (buf[i] > '9')
312 			buf[i] += ('a'-'0'-10);
313 		val >>= 4;
314 	}
315 	buf[nibbles] = '\0';
316 	call_prom("write", 3, 1, prom.stdout, buf, nibbles);
317 }
318 
319 /* max number of decimal digits in an unsigned long */
320 #define UL_DIGITS 21
321 static void __init prom_print_dec(unsigned long val)
322 {
323 	int i, size;
324 	char buf[UL_DIGITS+1];
325 
326 	for (i = UL_DIGITS-1; i >= 0;  i--) {
327 		buf[i] = (val % 10) + '0';
328 		val = val/10;
329 		if (val == 0)
330 			break;
331 	}
332 	/* shift stuff down */
333 	size = UL_DIGITS - i;
334 	call_prom("write", 3, 1, prom.stdout, buf+i, size);
335 }
336 
337 static void __init prom_printf(const char *format, ...)
338 {
339 	const char *p, *q, *s;
340 	va_list args;
341 	unsigned long v;
342 	long vs;
343 
344 	va_start(args, format);
345 	for (p = format; *p != 0; p = q) {
346 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
347 			;
348 		if (q > p)
349 			call_prom("write", 3, 1, prom.stdout, p, q - p);
350 		if (*q == 0)
351 			break;
352 		if (*q == '\n') {
353 			++q;
354 			call_prom("write", 3, 1, prom.stdout,
355 				  ADDR("\r\n"), 2);
356 			continue;
357 		}
358 		++q;
359 		if (*q == 0)
360 			break;
361 		switch (*q) {
362 		case 's':
363 			++q;
364 			s = va_arg(args, const char *);
365 			prom_print(s);
366 			break;
367 		case 'x':
368 			++q;
369 			v = va_arg(args, unsigned long);
370 			prom_print_hex(v);
371 			break;
372 		case 'd':
373 			++q;
374 			vs = va_arg(args, int);
375 			if (vs < 0) {
376 				prom_print("-");
377 				vs = -vs;
378 			}
379 			prom_print_dec(vs);
380 			break;
381 		case 'l':
382 			++q;
383 			if (*q == 0)
384 				break;
385 			else if (*q == 'x') {
386 				++q;
387 				v = va_arg(args, unsigned long);
388 				prom_print_hex(v);
389 			} else if (*q == 'u') { /* '%lu' */
390 				++q;
391 				v = va_arg(args, unsigned long);
392 				prom_print_dec(v);
393 			} else if (*q == 'd') { /* %ld */
394 				++q;
395 				vs = va_arg(args, long);
396 				if (vs < 0) {
397 					prom_print("-");
398 					vs = -vs;
399 				}
400 				prom_print_dec(vs);
401 			}
402 			break;
403 		}
404 	}
405 	va_end(args);
406 }
407 
408 
409 static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
410 				unsigned long align)
411 {
412 
413 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
414 		/*
415 		 * Old OF requires we claim physical and virtual separately
416 		 * and then map explicitly (assuming virtual mode)
417 		 */
418 		int ret;
419 		prom_arg_t result;
420 
421 		ret = call_prom_ret("call-method", 5, 2, &result,
422 				    ADDR("claim"), prom.memory,
423 				    align, size, virt);
424 		if (ret != 0 || result == -1)
425 			return -1;
426 		ret = call_prom_ret("call-method", 5, 2, &result,
427 				    ADDR("claim"), prom.mmumap,
428 				    align, size, virt);
429 		if (ret != 0) {
430 			call_prom("call-method", 4, 1, ADDR("release"),
431 				  prom.memory, size, virt);
432 			return -1;
433 		}
434 		/* the 0x12 is M (coherence) + PP == read/write */
435 		call_prom("call-method", 6, 1,
436 			  ADDR("map"), prom.mmumap, 0x12, size, virt, virt);
437 		return virt;
438 	}
439 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
440 			 (prom_arg_t)align);
441 }
442 
443 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
444 {
445 	prom_print(reason);
446 	/* Do not call exit because it clears the screen on pmac
447 	 * it also causes some sort of double-fault on early pmacs */
448 	if (of_platform == PLATFORM_POWERMAC)
449 		asm("trap\n");
450 
451 	/* ToDo: should put up an SRC here on pSeries */
452 	call_prom("exit", 0, 0);
453 
454 	for (;;)			/* should never get here */
455 		;
456 }
457 
458 
459 static int __init prom_next_node(phandle *nodep)
460 {
461 	phandle node;
462 
463 	if ((node = *nodep) != 0
464 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
465 		return 1;
466 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
467 		return 1;
468 	for (;;) {
469 		if ((node = call_prom("parent", 1, 1, node)) == 0)
470 			return 0;
471 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
472 			return 1;
473 	}
474 }
475 
476 static inline int prom_getprop(phandle node, const char *pname,
477 			       void *value, size_t valuelen)
478 {
479 	return call_prom("getprop", 4, 1, node, ADDR(pname),
480 			 (u32)(unsigned long) value, (u32) valuelen);
481 }
482 
483 static inline int prom_getproplen(phandle node, const char *pname)
484 {
485 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
486 }
487 
488 static void add_string(char **str, const char *q)
489 {
490 	char *p = *str;
491 
492 	while (*q)
493 		*p++ = *q++;
494 	*p++ = ' ';
495 	*str = p;
496 }
497 
498 static char *tohex(unsigned int x)
499 {
500 	static char digits[] = "0123456789abcdef";
501 	static char result[9];
502 	int i;
503 
504 	result[8] = 0;
505 	i = 8;
506 	do {
507 		--i;
508 		result[i] = digits[x & 0xf];
509 		x >>= 4;
510 	} while (x != 0 && i > 0);
511 	return &result[i];
512 }
513 
514 static int __init prom_setprop(phandle node, const char *nodename,
515 			       const char *pname, void *value, size_t valuelen)
516 {
517 	char cmd[256], *p;
518 
519 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
520 		return call_prom("setprop", 4, 1, node, ADDR(pname),
521 				 (u32)(unsigned long) value, (u32) valuelen);
522 
523 	/* gah... setprop doesn't work on longtrail, have to use interpret */
524 	p = cmd;
525 	add_string(&p, "dev");
526 	add_string(&p, nodename);
527 	add_string(&p, tohex((u32)(unsigned long) value));
528 	add_string(&p, tohex(valuelen));
529 	add_string(&p, tohex(ADDR(pname)));
530 	add_string(&p, tohex(strlen(pname)));
531 	add_string(&p, "property");
532 	*p = 0;
533 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
534 }
535 
536 /* We can't use the standard versions because of relocation headaches. */
537 #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
538 			 || ('a' <= (c) && (c) <= 'f') \
539 			 || ('A' <= (c) && (c) <= 'F'))
540 
541 #define isdigit(c)	('0' <= (c) && (c) <= '9')
542 #define islower(c)	('a' <= (c) && (c) <= 'z')
543 #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
544 
545 static unsigned long prom_strtoul(const char *cp, const char **endp)
546 {
547 	unsigned long result = 0, base = 10, value;
548 
549 	if (*cp == '0') {
550 		base = 8;
551 		cp++;
552 		if (toupper(*cp) == 'X') {
553 			cp++;
554 			base = 16;
555 		}
556 	}
557 
558 	while (isxdigit(*cp) &&
559 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
560 		result = result * base + value;
561 		cp++;
562 	}
563 
564 	if (endp)
565 		*endp = cp;
566 
567 	return result;
568 }
569 
570 static unsigned long prom_memparse(const char *ptr, const char **retptr)
571 {
572 	unsigned long ret = prom_strtoul(ptr, retptr);
573 	int shift = 0;
574 
575 	/*
576 	 * We can't use a switch here because GCC *may* generate a
577 	 * jump table which won't work, because we're not running at
578 	 * the address we're linked at.
579 	 */
580 	if ('G' == **retptr || 'g' == **retptr)
581 		shift = 30;
582 
583 	if ('M' == **retptr || 'm' == **retptr)
584 		shift = 20;
585 
586 	if ('K' == **retptr || 'k' == **retptr)
587 		shift = 10;
588 
589 	if (shift) {
590 		ret <<= shift;
591 		(*retptr)++;
592 	}
593 
594 	return ret;
595 }
596 
597 /*
598  * Early parsing of the command line passed to the kernel, used for
599  * "mem=x" and the options that affect the iommu
600  */
601 static void __init early_cmdline_parse(void)
602 {
603 	const char *opt;
604 
605 	char *p;
606 	int l = 0;
607 
608 	prom_cmd_line[0] = 0;
609 	p = prom_cmd_line;
610 	if ((long)prom.chosen > 0)
611 		l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
612 #ifdef CONFIG_CMDLINE
613 	if (l <= 0 || p[0] == '\0') /* dbl check */
614 		strlcpy(prom_cmd_line,
615 			CONFIG_CMDLINE, sizeof(prom_cmd_line));
616 #endif /* CONFIG_CMDLINE */
617 	prom_printf("command line: %s\n", prom_cmd_line);
618 
619 #ifdef CONFIG_PPC64
620 	opt = strstr(prom_cmd_line, "iommu=");
621 	if (opt) {
622 		prom_printf("iommu opt is: %s\n", opt);
623 		opt += 6;
624 		while (*opt && *opt == ' ')
625 			opt++;
626 		if (!strncmp(opt, "off", 3))
627 			prom_iommu_off = 1;
628 		else if (!strncmp(opt, "force", 5))
629 			prom_iommu_force_on = 1;
630 	}
631 #endif
632 	opt = strstr(prom_cmd_line, "mem=");
633 	if (opt) {
634 		opt += 4;
635 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
636 #ifdef CONFIG_PPC64
637 		/* Align to 16 MB == size of ppc64 large page */
638 		prom_memory_limit = ALIGN(prom_memory_limit, 0x1000000);
639 #endif
640 	}
641 
642 	opt = strstr(prom_cmd_line, "disable_radix");
643 	if (opt) {
644 		prom_debug("Radix disabled from cmdline\n");
645 		prom_radix_disable = true;
646 	}
647 }
648 
649 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
650 /*
651  * The architecture vector has an array of PVR mask/value pairs,
652  * followed by # option vectors - 1, followed by the option vectors.
653  *
654  * See prom.h for the definition of the bits specified in the
655  * architecture vector.
656  */
657 
658 /* Firmware expects the value to be n - 1, where n is the # of vectors */
659 #define NUM_VECTORS(n)		((n) - 1)
660 
661 /*
662  * Firmware expects 1 + n - 2, where n is the length of the option vector in
663  * bytes. The 1 accounts for the length byte itself, the - 2 .. ?
664  */
665 #define VECTOR_LENGTH(n)	(1 + (n) - 2)
666 
667 struct option_vector1 {
668 	u8 byte1;
669 	u8 arch_versions;
670 	u8 arch_versions3;
671 } __packed;
672 
673 struct option_vector2 {
674 	u8 byte1;
675 	__be16 reserved;
676 	__be32 real_base;
677 	__be32 real_size;
678 	__be32 virt_base;
679 	__be32 virt_size;
680 	__be32 load_base;
681 	__be32 min_rma;
682 	__be32 min_load;
683 	u8 min_rma_percent;
684 	u8 max_pft_size;
685 } __packed;
686 
687 struct option_vector3 {
688 	u8 byte1;
689 	u8 byte2;
690 } __packed;
691 
692 struct option_vector4 {
693 	u8 byte1;
694 	u8 min_vp_cap;
695 } __packed;
696 
697 struct option_vector5 {
698 	u8 byte1;
699 	u8 byte2;
700 	u8 byte3;
701 	u8 cmo;
702 	u8 associativity;
703 	u8 bin_opts;
704 	u8 micro_checkpoint;
705 	u8 reserved0;
706 	__be32 max_cpus;
707 	__be16 papr_level;
708 	__be16 reserved1;
709 	u8 platform_facilities;
710 	u8 reserved2;
711 	__be16 reserved3;
712 	u8 subprocessors;
713 	u8 byte22;
714 	u8 intarch;
715 	u8 mmu;
716 	u8 hash_ext;
717 	u8 radix_ext;
718 } __packed;
719 
720 struct option_vector6 {
721 	u8 reserved;
722 	u8 secondary_pteg;
723 	u8 os_name;
724 } __packed;
725 
726 struct ibm_arch_vec {
727 	struct { u32 mask, val; } pvrs[12];
728 
729 	u8 num_vectors;
730 
731 	u8 vec1_len;
732 	struct option_vector1 vec1;
733 
734 	u8 vec2_len;
735 	struct option_vector2 vec2;
736 
737 	u8 vec3_len;
738 	struct option_vector3 vec3;
739 
740 	u8 vec4_len;
741 	struct option_vector4 vec4;
742 
743 	u8 vec5_len;
744 	struct option_vector5 vec5;
745 
746 	u8 vec6_len;
747 	struct option_vector6 vec6;
748 } __packed;
749 
750 struct ibm_arch_vec __cacheline_aligned ibm_architecture_vec = {
751 	.pvrs = {
752 		{
753 			.mask = cpu_to_be32(0xfffe0000), /* POWER5/POWER5+ */
754 			.val  = cpu_to_be32(0x003a0000),
755 		},
756 		{
757 			.mask = cpu_to_be32(0xffff0000), /* POWER6 */
758 			.val  = cpu_to_be32(0x003e0000),
759 		},
760 		{
761 			.mask = cpu_to_be32(0xffff0000), /* POWER7 */
762 			.val  = cpu_to_be32(0x003f0000),
763 		},
764 		{
765 			.mask = cpu_to_be32(0xffff0000), /* POWER8E */
766 			.val  = cpu_to_be32(0x004b0000),
767 		},
768 		{
769 			.mask = cpu_to_be32(0xffff0000), /* POWER8NVL */
770 			.val  = cpu_to_be32(0x004c0000),
771 		},
772 		{
773 			.mask = cpu_to_be32(0xffff0000), /* POWER8 */
774 			.val  = cpu_to_be32(0x004d0000),
775 		},
776 		{
777 			.mask = cpu_to_be32(0xffff0000), /* POWER9 */
778 			.val  = cpu_to_be32(0x004e0000),
779 		},
780 		{
781 			.mask = cpu_to_be32(0xffffffff), /* all 3.00-compliant */
782 			.val  = cpu_to_be32(0x0f000005),
783 		},
784 		{
785 			.mask = cpu_to_be32(0xffffffff), /* all 2.07-compliant */
786 			.val  = cpu_to_be32(0x0f000004),
787 		},
788 		{
789 			.mask = cpu_to_be32(0xffffffff), /* all 2.06-compliant */
790 			.val  = cpu_to_be32(0x0f000003),
791 		},
792 		{
793 			.mask = cpu_to_be32(0xffffffff), /* all 2.05-compliant */
794 			.val  = cpu_to_be32(0x0f000002),
795 		},
796 		{
797 			.mask = cpu_to_be32(0xfffffffe), /* all 2.04-compliant and earlier */
798 			.val  = cpu_to_be32(0x0f000001),
799 		},
800 	},
801 
802 	.num_vectors = NUM_VECTORS(6),
803 
804 	.vec1_len = VECTOR_LENGTH(sizeof(struct option_vector1)),
805 	.vec1 = {
806 		.byte1 = 0,
807 		.arch_versions = OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
808 				 OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06 | OV1_PPC_2_07,
809 		.arch_versions3 = OV1_PPC_3_00,
810 	},
811 
812 	.vec2_len = VECTOR_LENGTH(sizeof(struct option_vector2)),
813 	/* option vector 2: Open Firmware options supported */
814 	.vec2 = {
815 		.byte1 = OV2_REAL_MODE,
816 		.reserved = 0,
817 		.real_base = cpu_to_be32(0xffffffff),
818 		.real_size = cpu_to_be32(0xffffffff),
819 		.virt_base = cpu_to_be32(0xffffffff),
820 		.virt_size = cpu_to_be32(0xffffffff),
821 		.load_base = cpu_to_be32(0xffffffff),
822 		.min_rma = cpu_to_be32(512),		/* 512MB min RMA */
823 		.min_load = cpu_to_be32(0xffffffff),	/* full client load */
824 		.min_rma_percent = 0,	/* min RMA percentage of total RAM */
825 		.max_pft_size = 48,	/* max log_2(hash table size) */
826 	},
827 
828 	.vec3_len = VECTOR_LENGTH(sizeof(struct option_vector3)),
829 	/* option vector 3: processor options supported */
830 	.vec3 = {
831 		.byte1 = 0,			/* don't ignore, don't halt */
832 		.byte2 = OV3_FP | OV3_VMX | OV3_DFP,
833 	},
834 
835 	.vec4_len = VECTOR_LENGTH(sizeof(struct option_vector4)),
836 	/* option vector 4: IBM PAPR implementation */
837 	.vec4 = {
838 		.byte1 = 0,			/* don't halt */
839 		.min_vp_cap = OV4_MIN_ENT_CAP,	/* minimum VP entitled capacity */
840 	},
841 
842 	.vec5_len = VECTOR_LENGTH(sizeof(struct option_vector5)),
843 	/* option vector 5: PAPR/OF options */
844 	.vec5 = {
845 		.byte1 = 0,				/* don't ignore, don't halt */
846 		.byte2 = OV5_FEAT(OV5_LPAR) | OV5_FEAT(OV5_SPLPAR) | OV5_FEAT(OV5_LARGE_PAGES) |
847 		OV5_FEAT(OV5_DRCONF_MEMORY) | OV5_FEAT(OV5_DONATE_DEDICATE_CPU) |
848 #ifdef CONFIG_PCI_MSI
849 		/* PCIe/MSI support.  Without MSI full PCIe is not supported */
850 		OV5_FEAT(OV5_MSI),
851 #else
852 		0,
853 #endif
854 		.byte3 = 0,
855 		.cmo =
856 #ifdef CONFIG_PPC_SMLPAR
857 		OV5_FEAT(OV5_CMO) | OV5_FEAT(OV5_XCMO),
858 #else
859 		0,
860 #endif
861 		.associativity = OV5_FEAT(OV5_TYPE1_AFFINITY) | OV5_FEAT(OV5_PRRN),
862 		.bin_opts = OV5_FEAT(OV5_RESIZE_HPT) | OV5_FEAT(OV5_HP_EVT),
863 		.micro_checkpoint = 0,
864 		.reserved0 = 0,
865 		.max_cpus = cpu_to_be32(NR_CPUS),	/* number of cores supported */
866 		.papr_level = 0,
867 		.reserved1 = 0,
868 		.platform_facilities = OV5_FEAT(OV5_PFO_HW_RNG) | OV5_FEAT(OV5_PFO_HW_ENCR) | OV5_FEAT(OV5_PFO_HW_842),
869 		.reserved2 = 0,
870 		.reserved3 = 0,
871 		.subprocessors = 1,
872 		.intarch = 0,
873 		.mmu = 0,
874 		.hash_ext = 0,
875 		.radix_ext = 0,
876 	},
877 
878 	/* option vector 6: IBM PAPR hints */
879 	.vec6_len = VECTOR_LENGTH(sizeof(struct option_vector6)),
880 	.vec6 = {
881 		.reserved = 0,
882 		.secondary_pteg = 0,
883 		.os_name = OV6_LINUX,
884 	},
885 };
886 
887 /* Old method - ELF header with PT_NOTE sections only works on BE */
888 #ifdef __BIG_ENDIAN__
889 static struct fake_elf {
890 	Elf32_Ehdr	elfhdr;
891 	Elf32_Phdr	phdr[2];
892 	struct chrpnote {
893 		u32	namesz;
894 		u32	descsz;
895 		u32	type;
896 		char	name[8];	/* "PowerPC" */
897 		struct chrpdesc {
898 			u32	real_mode;
899 			u32	real_base;
900 			u32	real_size;
901 			u32	virt_base;
902 			u32	virt_size;
903 			u32	load_base;
904 		} chrpdesc;
905 	} chrpnote;
906 	struct rpanote {
907 		u32	namesz;
908 		u32	descsz;
909 		u32	type;
910 		char	name[24];	/* "IBM,RPA-Client-Config" */
911 		struct rpadesc {
912 			u32	lpar_affinity;
913 			u32	min_rmo_size;
914 			u32	min_rmo_percent;
915 			u32	max_pft_size;
916 			u32	splpar;
917 			u32	min_load;
918 			u32	new_mem_def;
919 			u32	ignore_me;
920 		} rpadesc;
921 	} rpanote;
922 } fake_elf = {
923 	.elfhdr = {
924 		.e_ident = { 0x7f, 'E', 'L', 'F',
925 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
926 		.e_type = ET_EXEC,	/* yeah right */
927 		.e_machine = EM_PPC,
928 		.e_version = EV_CURRENT,
929 		.e_phoff = offsetof(struct fake_elf, phdr),
930 		.e_phentsize = sizeof(Elf32_Phdr),
931 		.e_phnum = 2
932 	},
933 	.phdr = {
934 		[0] = {
935 			.p_type = PT_NOTE,
936 			.p_offset = offsetof(struct fake_elf, chrpnote),
937 			.p_filesz = sizeof(struct chrpnote)
938 		}, [1] = {
939 			.p_type = PT_NOTE,
940 			.p_offset = offsetof(struct fake_elf, rpanote),
941 			.p_filesz = sizeof(struct rpanote)
942 		}
943 	},
944 	.chrpnote = {
945 		.namesz = sizeof("PowerPC"),
946 		.descsz = sizeof(struct chrpdesc),
947 		.type = 0x1275,
948 		.name = "PowerPC",
949 		.chrpdesc = {
950 			.real_mode = ~0U,	/* ~0 means "don't care" */
951 			.real_base = ~0U,
952 			.real_size = ~0U,
953 			.virt_base = ~0U,
954 			.virt_size = ~0U,
955 			.load_base = ~0U
956 		},
957 	},
958 	.rpanote = {
959 		.namesz = sizeof("IBM,RPA-Client-Config"),
960 		.descsz = sizeof(struct rpadesc),
961 		.type = 0x12759999,
962 		.name = "IBM,RPA-Client-Config",
963 		.rpadesc = {
964 			.lpar_affinity = 0,
965 			.min_rmo_size = 64,	/* in megabytes */
966 			.min_rmo_percent = 0,
967 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
968 			.splpar = 1,
969 			.min_load = ~0U,
970 			.new_mem_def = 0
971 		}
972 	}
973 };
974 #endif /* __BIG_ENDIAN__ */
975 
976 static int __init prom_count_smt_threads(void)
977 {
978 	phandle node;
979 	char type[64];
980 	unsigned int plen;
981 
982 	/* Pick up th first CPU node we can find */
983 	for (node = 0; prom_next_node(&node); ) {
984 		type[0] = 0;
985 		prom_getprop(node, "device_type", type, sizeof(type));
986 
987 		if (strcmp(type, "cpu"))
988 			continue;
989 		/*
990 		 * There is an entry for each smt thread, each entry being
991 		 * 4 bytes long.  All cpus should have the same number of
992 		 * smt threads, so return after finding the first.
993 		 */
994 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
995 		if (plen == PROM_ERROR)
996 			break;
997 		plen >>= 2;
998 		prom_debug("Found %lu smt threads per core\n", (unsigned long)plen);
999 
1000 		/* Sanity check */
1001 		if (plen < 1 || plen > 64) {
1002 			prom_printf("Threads per core %lu out of bounds, assuming 1\n",
1003 				    (unsigned long)plen);
1004 			return 1;
1005 		}
1006 		return plen;
1007 	}
1008 	prom_debug("No threads found, assuming 1 per core\n");
1009 
1010 	return 1;
1011 
1012 }
1013 
1014 static void __init prom_parse_mmu_model(u8 val,
1015 					struct platform_support *support)
1016 {
1017 	switch (val) {
1018 	case OV5_FEAT(OV5_MMU_DYNAMIC):
1019 	case OV5_FEAT(OV5_MMU_EITHER): /* Either Available */
1020 		prom_debug("MMU - either supported\n");
1021 		support->radix_mmu = !prom_radix_disable;
1022 		support->hash_mmu = true;
1023 		break;
1024 	case OV5_FEAT(OV5_MMU_RADIX): /* Only Radix */
1025 		prom_debug("MMU - radix only\n");
1026 		if (prom_radix_disable) {
1027 			/*
1028 			 * If we __have__ to do radix, we're better off ignoring
1029 			 * the command line rather than not booting.
1030 			 */
1031 			prom_printf("WARNING: Ignoring cmdline option disable_radix\n");
1032 		}
1033 		support->radix_mmu = true;
1034 		break;
1035 	case OV5_FEAT(OV5_MMU_HASH):
1036 		prom_debug("MMU - hash only\n");
1037 		support->hash_mmu = true;
1038 		break;
1039 	default:
1040 		prom_debug("Unknown mmu support option: 0x%x\n", val);
1041 		break;
1042 	}
1043 }
1044 
1045 static void __init prom_parse_xive_model(u8 val,
1046 					 struct platform_support *support)
1047 {
1048 	switch (val) {
1049 	case OV5_FEAT(OV5_XIVE_EITHER): /* Either Available */
1050 		prom_debug("XIVE - either mode supported\n");
1051 		support->xive = true;
1052 		break;
1053 	case OV5_FEAT(OV5_XIVE_EXPLOIT): /* Only Exploitation mode */
1054 		prom_debug("XIVE - exploitation mode supported\n");
1055 		support->xive = true;
1056 		break;
1057 	case OV5_FEAT(OV5_XIVE_LEGACY): /* Only Legacy mode */
1058 		prom_debug("XIVE - legacy mode supported\n");
1059 		break;
1060 	default:
1061 		prom_debug("Unknown xive support option: 0x%x\n", val);
1062 		break;
1063 	}
1064 }
1065 
1066 static void __init prom_parse_platform_support(u8 index, u8 val,
1067 					       struct platform_support *support)
1068 {
1069 	switch (index) {
1070 	case OV5_INDX(OV5_MMU_SUPPORT): /* MMU Model */
1071 		prom_parse_mmu_model(val & OV5_FEAT(OV5_MMU_SUPPORT), support);
1072 		break;
1073 	case OV5_INDX(OV5_RADIX_GTSE): /* Radix Extensions */
1074 		if (val & OV5_FEAT(OV5_RADIX_GTSE)) {
1075 			prom_debug("Radix - GTSE supported\n");
1076 			support->radix_gtse = true;
1077 		}
1078 		break;
1079 	case OV5_INDX(OV5_XIVE_SUPPORT): /* Interrupt mode */
1080 		prom_parse_xive_model(val & OV5_FEAT(OV5_XIVE_SUPPORT),
1081 				      support);
1082 		break;
1083 	}
1084 }
1085 
1086 static void __init prom_check_platform_support(void)
1087 {
1088 	struct platform_support supported = {
1089 		.hash_mmu = false,
1090 		.radix_mmu = false,
1091 		.radix_gtse = false,
1092 		.xive = false
1093 	};
1094 	int prop_len = prom_getproplen(prom.chosen,
1095 				       "ibm,arch-vec-5-platform-support");
1096 	if (prop_len > 1) {
1097 		int i;
1098 		u8 vec[prop_len];
1099 		prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1100 			   prop_len);
1101 		prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support",
1102 			     &vec, sizeof(vec));
1103 		for (i = 0; i < prop_len; i += 2) {
1104 			prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2
1105 								  , vec[i]
1106 								  , vec[i + 1]);
1107 			prom_parse_platform_support(vec[i], vec[i + 1],
1108 						    &supported);
1109 		}
1110 	}
1111 
1112 	if (supported.radix_mmu && supported.radix_gtse) {
1113 		/* Radix preferred - but we require GTSE for now */
1114 		prom_debug("Asking for radix with GTSE\n");
1115 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1116 		ibm_architecture_vec.vec5.radix_ext = OV5_FEAT(OV5_RADIX_GTSE);
1117 	} else if (supported.hash_mmu) {
1118 		/* Default to hash mmu (if we can) */
1119 		prom_debug("Asking for hash\n");
1120 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1121 	} else {
1122 		/* We're probably on a legacy hypervisor */
1123 		prom_debug("Assuming legacy hash support\n");
1124 	}
1125 
1126 	if (supported.xive) {
1127 		prom_debug("Asking for XIVE\n");
1128 		ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1129 	}
1130 }
1131 
1132 static void __init prom_send_capabilities(void)
1133 {
1134 	ihandle root;
1135 	prom_arg_t ret;
1136 	u32 cores;
1137 
1138 	/* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1139 	prom_check_platform_support();
1140 
1141 	root = call_prom("open", 1, 1, ADDR("/"));
1142 	if (root != 0) {
1143 		/* We need to tell the FW about the number of cores we support.
1144 		 *
1145 		 * To do that, we count the number of threads on the first core
1146 		 * (we assume this is the same for all cores) and use it to
1147 		 * divide NR_CPUS.
1148 		 */
1149 
1150 		cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1151 		prom_printf("Max number of cores passed to firmware: %lu (NR_CPUS = %lu)\n",
1152 			    cores, NR_CPUS);
1153 
1154 		ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1155 
1156 		/* try calling the ibm,client-architecture-support method */
1157 		prom_printf("Calling ibm,client-architecture-support...");
1158 		if (call_prom_ret("call-method", 3, 2, &ret,
1159 				  ADDR("ibm,client-architecture-support"),
1160 				  root,
1161 				  ADDR(&ibm_architecture_vec)) == 0) {
1162 			/* the call exists... */
1163 			if (ret)
1164 				prom_printf("\nWARNING: ibm,client-architecture"
1165 					    "-support call FAILED!\n");
1166 			call_prom("close", 1, 0, root);
1167 			prom_printf(" done\n");
1168 			return;
1169 		}
1170 		call_prom("close", 1, 0, root);
1171 		prom_printf(" not implemented\n");
1172 	}
1173 
1174 #ifdef __BIG_ENDIAN__
1175 	{
1176 		ihandle elfloader;
1177 
1178 		/* no ibm,client-architecture-support call, try the old way */
1179 		elfloader = call_prom("open", 1, 1,
1180 				      ADDR("/packages/elf-loader"));
1181 		if (elfloader == 0) {
1182 			prom_printf("couldn't open /packages/elf-loader\n");
1183 			return;
1184 		}
1185 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
1186 			  elfloader, ADDR(&fake_elf));
1187 		call_prom("close", 1, 0, elfloader);
1188 	}
1189 #endif /* __BIG_ENDIAN__ */
1190 }
1191 #endif /* #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV) */
1192 
1193 /*
1194  * Memory allocation strategy... our layout is normally:
1195  *
1196  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
1197  *  rare cases, initrd might end up being before the kernel though.
1198  *  We assume this won't override the final kernel at 0, we have no
1199  *  provision to handle that in this version, but it should hopefully
1200  *  never happen.
1201  *
1202  *  alloc_top is set to the top of RMO, eventually shrink down if the
1203  *  TCEs overlap
1204  *
1205  *  alloc_bottom is set to the top of kernel/initrd
1206  *
1207  *  from there, allocations are done this way : rtas is allocated
1208  *  topmost, and the device-tree is allocated from the bottom. We try
1209  *  to grow the device-tree allocation as we progress. If we can't,
1210  *  then we fail, we don't currently have a facility to restart
1211  *  elsewhere, but that shouldn't be necessary.
1212  *
1213  *  Note that calls to reserve_mem have to be done explicitly, memory
1214  *  allocated with either alloc_up or alloc_down isn't automatically
1215  *  reserved.
1216  */
1217 
1218 
1219 /*
1220  * Allocates memory in the RMO upward from the kernel/initrd
1221  *
1222  * When align is 0, this is a special case, it means to allocate in place
1223  * at the current location of alloc_bottom or fail (that is basically
1224  * extending the previous allocation). Used for the device-tree flattening
1225  */
1226 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
1227 {
1228 	unsigned long base = alloc_bottom;
1229 	unsigned long addr = 0;
1230 
1231 	if (align)
1232 		base = _ALIGN_UP(base, align);
1233 	prom_debug("alloc_up(%x, %x)\n", size, align);
1234 	if (ram_top == 0)
1235 		prom_panic("alloc_up() called with mem not initialized\n");
1236 
1237 	if (align)
1238 		base = _ALIGN_UP(alloc_bottom, align);
1239 	else
1240 		base = alloc_bottom;
1241 
1242 	for(; (base + size) <= alloc_top;
1243 	    base = _ALIGN_UP(base + 0x100000, align)) {
1244 		prom_debug("    trying: 0x%x\n\r", base);
1245 		addr = (unsigned long)prom_claim(base, size, 0);
1246 		if (addr != PROM_ERROR && addr != 0)
1247 			break;
1248 		addr = 0;
1249 		if (align == 0)
1250 			break;
1251 	}
1252 	if (addr == 0)
1253 		return 0;
1254 	alloc_bottom = addr + size;
1255 
1256 	prom_debug(" -> %x\n", addr);
1257 	prom_debug("  alloc_bottom : %x\n", alloc_bottom);
1258 	prom_debug("  alloc_top    : %x\n", alloc_top);
1259 	prom_debug("  alloc_top_hi : %x\n", alloc_top_high);
1260 	prom_debug("  rmo_top      : %x\n", rmo_top);
1261 	prom_debug("  ram_top      : %x\n", ram_top);
1262 
1263 	return addr;
1264 }
1265 
1266 /*
1267  * Allocates memory downward, either from top of RMO, or if highmem
1268  * is set, from the top of RAM.  Note that this one doesn't handle
1269  * failures.  It does claim memory if highmem is not set.
1270  */
1271 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
1272 				       int highmem)
1273 {
1274 	unsigned long base, addr = 0;
1275 
1276 	prom_debug("alloc_down(%x, %x, %s)\n", size, align,
1277 		   highmem ? "(high)" : "(low)");
1278 	if (ram_top == 0)
1279 		prom_panic("alloc_down() called with mem not initialized\n");
1280 
1281 	if (highmem) {
1282 		/* Carve out storage for the TCE table. */
1283 		addr = _ALIGN_DOWN(alloc_top_high - size, align);
1284 		if (addr <= alloc_bottom)
1285 			return 0;
1286 		/* Will we bump into the RMO ? If yes, check out that we
1287 		 * didn't overlap existing allocations there, if we did,
1288 		 * we are dead, we must be the first in town !
1289 		 */
1290 		if (addr < rmo_top) {
1291 			/* Good, we are first */
1292 			if (alloc_top == rmo_top)
1293 				alloc_top = rmo_top = addr;
1294 			else
1295 				return 0;
1296 		}
1297 		alloc_top_high = addr;
1298 		goto bail;
1299 	}
1300 
1301 	base = _ALIGN_DOWN(alloc_top - size, align);
1302 	for (; base > alloc_bottom;
1303 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
1304 		prom_debug("    trying: 0x%x\n\r", base);
1305 		addr = (unsigned long)prom_claim(base, size, 0);
1306 		if (addr != PROM_ERROR && addr != 0)
1307 			break;
1308 		addr = 0;
1309 	}
1310 	if (addr == 0)
1311 		return 0;
1312 	alloc_top = addr;
1313 
1314  bail:
1315 	prom_debug(" -> %x\n", addr);
1316 	prom_debug("  alloc_bottom : %x\n", alloc_bottom);
1317 	prom_debug("  alloc_top    : %x\n", alloc_top);
1318 	prom_debug("  alloc_top_hi : %x\n", alloc_top_high);
1319 	prom_debug("  rmo_top      : %x\n", rmo_top);
1320 	prom_debug("  ram_top      : %x\n", ram_top);
1321 
1322 	return addr;
1323 }
1324 
1325 /*
1326  * Parse a "reg" cell
1327  */
1328 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
1329 {
1330 	cell_t *p = *cellp;
1331 	unsigned long r = 0;
1332 
1333 	/* Ignore more than 2 cells */
1334 	while (s > sizeof(unsigned long) / 4) {
1335 		p++;
1336 		s--;
1337 	}
1338 	r = be32_to_cpu(*p++);
1339 #ifdef CONFIG_PPC64
1340 	if (s > 1) {
1341 		r <<= 32;
1342 		r |= be32_to_cpu(*(p++));
1343 	}
1344 #endif
1345 	*cellp = p;
1346 	return r;
1347 }
1348 
1349 /*
1350  * Very dumb function for adding to the memory reserve list, but
1351  * we don't need anything smarter at this point
1352  *
1353  * XXX Eventually check for collisions.  They should NEVER happen.
1354  * If problems seem to show up, it would be a good start to track
1355  * them down.
1356  */
1357 static void __init reserve_mem(u64 base, u64 size)
1358 {
1359 	u64 top = base + size;
1360 	unsigned long cnt = mem_reserve_cnt;
1361 
1362 	if (size == 0)
1363 		return;
1364 
1365 	/* We need to always keep one empty entry so that we
1366 	 * have our terminator with "size" set to 0 since we are
1367 	 * dumb and just copy this entire array to the boot params
1368 	 */
1369 	base = _ALIGN_DOWN(base, PAGE_SIZE);
1370 	top = _ALIGN_UP(top, PAGE_SIZE);
1371 	size = top - base;
1372 
1373 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
1374 		prom_panic("Memory reserve map exhausted !\n");
1375 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1376 	mem_reserve_map[cnt].size = cpu_to_be64(size);
1377 	mem_reserve_cnt = cnt + 1;
1378 }
1379 
1380 /*
1381  * Initialize memory allocation mechanism, parse "memory" nodes and
1382  * obtain that way the top of memory and RMO to setup out local allocator
1383  */
1384 static void __init prom_init_mem(void)
1385 {
1386 	phandle node;
1387 	char *path, type[64];
1388 	unsigned int plen;
1389 	cell_t *p, *endp;
1390 	__be32 val;
1391 	u32 rac, rsc;
1392 
1393 	/*
1394 	 * We iterate the memory nodes to find
1395 	 * 1) top of RMO (first node)
1396 	 * 2) top of memory
1397 	 */
1398 	val = cpu_to_be32(2);
1399 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1400 	rac = be32_to_cpu(val);
1401 	val = cpu_to_be32(1);
1402 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1403 	rsc = be32_to_cpu(val);
1404 	prom_debug("root_addr_cells: %x\n", rac);
1405 	prom_debug("root_size_cells: %x\n", rsc);
1406 
1407 	prom_debug("scanning memory:\n");
1408 	path = prom_scratch;
1409 
1410 	for (node = 0; prom_next_node(&node); ) {
1411 		type[0] = 0;
1412 		prom_getprop(node, "device_type", type, sizeof(type));
1413 
1414 		if (type[0] == 0) {
1415 			/*
1416 			 * CHRP Longtrail machines have no device_type
1417 			 * on the memory node, so check the name instead...
1418 			 */
1419 			prom_getprop(node, "name", type, sizeof(type));
1420 		}
1421 		if (strcmp(type, "memory"))
1422 			continue;
1423 
1424 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
1425 		if (plen > sizeof(regbuf)) {
1426 			prom_printf("memory node too large for buffer !\n");
1427 			plen = sizeof(regbuf);
1428 		}
1429 		p = regbuf;
1430 		endp = p + (plen / sizeof(cell_t));
1431 
1432 #ifdef DEBUG_PROM
1433 		memset(path, 0, PROM_SCRATCH_SIZE);
1434 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1435 		prom_debug("  node %s :\n", path);
1436 #endif /* DEBUG_PROM */
1437 
1438 		while ((endp - p) >= (rac + rsc)) {
1439 			unsigned long base, size;
1440 
1441 			base = prom_next_cell(rac, &p);
1442 			size = prom_next_cell(rsc, &p);
1443 
1444 			if (size == 0)
1445 				continue;
1446 			prom_debug("    %x %x\n", base, size);
1447 			if (base == 0 && (of_platform & PLATFORM_LPAR))
1448 				rmo_top = size;
1449 			if ((base + size) > ram_top)
1450 				ram_top = base + size;
1451 		}
1452 	}
1453 
1454 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
1455 
1456 	/*
1457 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1458 	 * alloc_top_high. This must be the real top of RAM so we can put
1459 	 * TCE's up there.
1460 	 */
1461 
1462 	alloc_top_high = ram_top;
1463 
1464 	if (prom_memory_limit) {
1465 		if (prom_memory_limit <= alloc_bottom) {
1466 			prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
1467 				prom_memory_limit);
1468 			prom_memory_limit = 0;
1469 		} else if (prom_memory_limit >= ram_top) {
1470 			prom_printf("Ignoring mem=%x >= ram_top.\n",
1471 				prom_memory_limit);
1472 			prom_memory_limit = 0;
1473 		} else {
1474 			ram_top = prom_memory_limit;
1475 			rmo_top = min(rmo_top, prom_memory_limit);
1476 		}
1477 	}
1478 
1479 	/*
1480 	 * Setup our top alloc point, that is top of RMO or top of
1481 	 * segment 0 when running non-LPAR.
1482 	 * Some RS64 machines have buggy firmware where claims up at
1483 	 * 1GB fail.  Cap at 768MB as a workaround.
1484 	 * Since 768MB is plenty of room, and we need to cap to something
1485 	 * reasonable on 32-bit, cap at 768MB on all machines.
1486 	 */
1487 	if (!rmo_top)
1488 		rmo_top = ram_top;
1489 	rmo_top = min(0x30000000ul, rmo_top);
1490 	alloc_top = rmo_top;
1491 	alloc_top_high = ram_top;
1492 
1493 	/*
1494 	 * Check if we have an initrd after the kernel but still inside
1495 	 * the RMO.  If we do move our bottom point to after it.
1496 	 */
1497 	if (prom_initrd_start &&
1498 	    prom_initrd_start < rmo_top &&
1499 	    prom_initrd_end > alloc_bottom)
1500 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
1501 
1502 	prom_printf("memory layout at init:\n");
1503 	prom_printf("  memory_limit : %x (16 MB aligned)\n", prom_memory_limit);
1504 	prom_printf("  alloc_bottom : %x\n", alloc_bottom);
1505 	prom_printf("  alloc_top    : %x\n", alloc_top);
1506 	prom_printf("  alloc_top_hi : %x\n", alloc_top_high);
1507 	prom_printf("  rmo_top      : %x\n", rmo_top);
1508 	prom_printf("  ram_top      : %x\n", ram_top);
1509 }
1510 
1511 static void __init prom_close_stdin(void)
1512 {
1513 	__be32 val;
1514 	ihandle stdin;
1515 
1516 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1517 		stdin = be32_to_cpu(val);
1518 		call_prom("close", 1, 0, stdin);
1519 	}
1520 }
1521 
1522 #ifdef CONFIG_PPC_POWERNV
1523 
1524 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
1525 static u64 __initdata prom_opal_base;
1526 static u64 __initdata prom_opal_entry;
1527 #endif
1528 
1529 /*
1530  * Allocate room for and instantiate OPAL
1531  */
1532 static void __init prom_instantiate_opal(void)
1533 {
1534 	phandle opal_node;
1535 	ihandle opal_inst;
1536 	u64 base, entry;
1537 	u64 size = 0, align = 0x10000;
1538 	__be64 val64;
1539 	u32 rets[2];
1540 
1541 	prom_debug("prom_instantiate_opal: start...\n");
1542 
1543 	opal_node = call_prom("finddevice", 1, 1, ADDR("/ibm,opal"));
1544 	prom_debug("opal_node: %x\n", opal_node);
1545 	if (!PHANDLE_VALID(opal_node))
1546 		return;
1547 
1548 	val64 = 0;
1549 	prom_getprop(opal_node, "opal-runtime-size", &val64, sizeof(val64));
1550 	size = be64_to_cpu(val64);
1551 	if (size == 0)
1552 		return;
1553 	val64 = 0;
1554 	prom_getprop(opal_node, "opal-runtime-alignment", &val64,sizeof(val64));
1555 	align = be64_to_cpu(val64);
1556 
1557 	base = alloc_down(size, align, 0);
1558 	if (base == 0) {
1559 		prom_printf("OPAL allocation failed !\n");
1560 		return;
1561 	}
1562 
1563 	opal_inst = call_prom("open", 1, 1, ADDR("/ibm,opal"));
1564 	if (!IHANDLE_VALID(opal_inst)) {
1565 		prom_printf("opening opal package failed (%x)\n", opal_inst);
1566 		return;
1567 	}
1568 
1569 	prom_printf("instantiating opal at 0x%x...", base);
1570 
1571 	if (call_prom_ret("call-method", 4, 3, rets,
1572 			  ADDR("load-opal-runtime"),
1573 			  opal_inst,
1574 			  base >> 32, base & 0xffffffff) != 0
1575 	    || (rets[0] == 0 && rets[1] == 0)) {
1576 		prom_printf(" failed\n");
1577 		return;
1578 	}
1579 	entry = (((u64)rets[0]) << 32) | rets[1];
1580 
1581 	prom_printf(" done\n");
1582 
1583 	reserve_mem(base, size);
1584 
1585 	prom_debug("opal base     = 0x%x\n", base);
1586 	prom_debug("opal align    = 0x%x\n", align);
1587 	prom_debug("opal entry    = 0x%x\n", entry);
1588 	prom_debug("opal size     = 0x%x\n", (long)size);
1589 
1590 	prom_setprop(opal_node, "/ibm,opal", "opal-base-address",
1591 		     &base, sizeof(base));
1592 	prom_setprop(opal_node, "/ibm,opal", "opal-entry-address",
1593 		     &entry, sizeof(entry));
1594 
1595 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
1596 	prom_opal_base = base;
1597 	prom_opal_entry = entry;
1598 #endif
1599 	prom_debug("prom_instantiate_opal: end...\n");
1600 }
1601 
1602 #endif /* CONFIG_PPC_POWERNV */
1603 
1604 /*
1605  * Allocate room for and instantiate RTAS
1606  */
1607 static void __init prom_instantiate_rtas(void)
1608 {
1609 	phandle rtas_node;
1610 	ihandle rtas_inst;
1611 	u32 base, entry = 0;
1612 	__be32 val;
1613 	u32 size = 0;
1614 
1615 	prom_debug("prom_instantiate_rtas: start...\n");
1616 
1617 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1618 	prom_debug("rtas_node: %x\n", rtas_node);
1619 	if (!PHANDLE_VALID(rtas_node))
1620 		return;
1621 
1622 	val = 0;
1623 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1624 	size = be32_to_cpu(val);
1625 	if (size == 0)
1626 		return;
1627 
1628 	base = alloc_down(size, PAGE_SIZE, 0);
1629 	if (base == 0)
1630 		prom_panic("Could not allocate memory for RTAS\n");
1631 
1632 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1633 	if (!IHANDLE_VALID(rtas_inst)) {
1634 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
1635 		return;
1636 	}
1637 
1638 	prom_printf("instantiating rtas at 0x%x...", base);
1639 
1640 	if (call_prom_ret("call-method", 3, 2, &entry,
1641 			  ADDR("instantiate-rtas"),
1642 			  rtas_inst, base) != 0
1643 	    || entry == 0) {
1644 		prom_printf(" failed\n");
1645 		return;
1646 	}
1647 	prom_printf(" done\n");
1648 
1649 	reserve_mem(base, size);
1650 
1651 	val = cpu_to_be32(base);
1652 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1653 		     &val, sizeof(val));
1654 	val = cpu_to_be32(entry);
1655 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1656 		     &val, sizeof(val));
1657 
1658 	/* Check if it supports "query-cpu-stopped-state" */
1659 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1660 			 &val, sizeof(val)) != PROM_ERROR)
1661 		rtas_has_query_cpu_stopped = true;
1662 
1663 	prom_debug("rtas base     = 0x%x\n", base);
1664 	prom_debug("rtas entry    = 0x%x\n", entry);
1665 	prom_debug("rtas size     = 0x%x\n", (long)size);
1666 
1667 	prom_debug("prom_instantiate_rtas: end...\n");
1668 }
1669 
1670 #ifdef CONFIG_PPC64
1671 /*
1672  * Allocate room for and instantiate Stored Measurement Log (SML)
1673  */
1674 static void __init prom_instantiate_sml(void)
1675 {
1676 	phandle ibmvtpm_node;
1677 	ihandle ibmvtpm_inst;
1678 	u32 entry = 0, size = 0, succ = 0;
1679 	u64 base;
1680 	__be32 val;
1681 
1682 	prom_debug("prom_instantiate_sml: start...\n");
1683 
1684 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
1685 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
1686 	if (!PHANDLE_VALID(ibmvtpm_node))
1687 		return;
1688 
1689 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
1690 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
1691 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
1692 		return;
1693 	}
1694 
1695 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1696 			 &val, sizeof(val)) != PROM_ERROR) {
1697 		if (call_prom_ret("call-method", 2, 2, &succ,
1698 				  ADDR("reformat-sml-to-efi-alignment"),
1699 				  ibmvtpm_inst) != 0 || succ == 0) {
1700 			prom_printf("Reformat SML to EFI alignment failed\n");
1701 			return;
1702 		}
1703 
1704 		if (call_prom_ret("call-method", 2, 2, &size,
1705 				  ADDR("sml-get-allocated-size"),
1706 				  ibmvtpm_inst) != 0 || size == 0) {
1707 			prom_printf("SML get allocated size failed\n");
1708 			return;
1709 		}
1710 	} else {
1711 		if (call_prom_ret("call-method", 2, 2, &size,
1712 				  ADDR("sml-get-handover-size"),
1713 				  ibmvtpm_inst) != 0 || size == 0) {
1714 			prom_printf("SML get handover size failed\n");
1715 			return;
1716 		}
1717 	}
1718 
1719 	base = alloc_down(size, PAGE_SIZE, 0);
1720 	if (base == 0)
1721 		prom_panic("Could not allocate memory for sml\n");
1722 
1723 	prom_printf("instantiating sml at 0x%x...", base);
1724 
1725 	memset((void *)base, 0, size);
1726 
1727 	if (call_prom_ret("call-method", 4, 2, &entry,
1728 			  ADDR("sml-handover"),
1729 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
1730 		prom_printf("SML handover failed\n");
1731 		return;
1732 	}
1733 	prom_printf(" done\n");
1734 
1735 	reserve_mem(base, size);
1736 
1737 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
1738 		     &base, sizeof(base));
1739 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
1740 		     &size, sizeof(size));
1741 
1742 	prom_debug("sml base     = 0x%x\n", base);
1743 	prom_debug("sml size     = 0x%x\n", (long)size);
1744 
1745 	prom_debug("prom_instantiate_sml: end...\n");
1746 }
1747 
1748 /*
1749  * Allocate room for and initialize TCE tables
1750  */
1751 #ifdef __BIG_ENDIAN__
1752 static void __init prom_initialize_tce_table(void)
1753 {
1754 	phandle node;
1755 	ihandle phb_node;
1756 	char compatible[64], type[64], model[64];
1757 	char *path = prom_scratch;
1758 	u64 base, align;
1759 	u32 minalign, minsize;
1760 	u64 tce_entry, *tce_entryp;
1761 	u64 local_alloc_top, local_alloc_bottom;
1762 	u64 i;
1763 
1764 	if (prom_iommu_off)
1765 		return;
1766 
1767 	prom_debug("starting prom_initialize_tce_table\n");
1768 
1769 	/* Cache current top of allocs so we reserve a single block */
1770 	local_alloc_top = alloc_top_high;
1771 	local_alloc_bottom = local_alloc_top;
1772 
1773 	/* Search all nodes looking for PHBs. */
1774 	for (node = 0; prom_next_node(&node); ) {
1775 		compatible[0] = 0;
1776 		type[0] = 0;
1777 		model[0] = 0;
1778 		prom_getprop(node, "compatible",
1779 			     compatible, sizeof(compatible));
1780 		prom_getprop(node, "device_type", type, sizeof(type));
1781 		prom_getprop(node, "model", model, sizeof(model));
1782 
1783 		if ((type[0] == 0) || (strstr(type, "pci") == NULL))
1784 			continue;
1785 
1786 		/* Keep the old logic intact to avoid regression. */
1787 		if (compatible[0] != 0) {
1788 			if ((strstr(compatible, "python") == NULL) &&
1789 			    (strstr(compatible, "Speedwagon") == NULL) &&
1790 			    (strstr(compatible, "Winnipeg") == NULL))
1791 				continue;
1792 		} else if (model[0] != 0) {
1793 			if ((strstr(model, "ython") == NULL) &&
1794 			    (strstr(model, "peedwagon") == NULL) &&
1795 			    (strstr(model, "innipeg") == NULL))
1796 				continue;
1797 		}
1798 
1799 		if (prom_getprop(node, "tce-table-minalign", &minalign,
1800 				 sizeof(minalign)) == PROM_ERROR)
1801 			minalign = 0;
1802 		if (prom_getprop(node, "tce-table-minsize", &minsize,
1803 				 sizeof(minsize)) == PROM_ERROR)
1804 			minsize = 4UL << 20;
1805 
1806 		/*
1807 		 * Even though we read what OF wants, we just set the table
1808 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
1809 		 * By doing this, we avoid the pitfalls of trying to DMA to
1810 		 * MMIO space and the DMA alias hole.
1811 		 *
1812 		 * On POWER4, firmware sets the TCE region by assuming
1813 		 * each TCE table is 8MB. Using this memory for anything
1814 		 * else will impact performance, so we always allocate 8MB.
1815 		 * Anton
1816 		 */
1817 		if (pvr_version_is(PVR_POWER4) || pvr_version_is(PVR_POWER4p))
1818 			minsize = 8UL << 20;
1819 		else
1820 			minsize = 4UL << 20;
1821 
1822 		/* Align to the greater of the align or size */
1823 		align = max(minalign, minsize);
1824 		base = alloc_down(minsize, align, 1);
1825 		if (base == 0)
1826 			prom_panic("ERROR, cannot find space for TCE table.\n");
1827 		if (base < local_alloc_bottom)
1828 			local_alloc_bottom = base;
1829 
1830 		/* It seems OF doesn't null-terminate the path :-( */
1831 		memset(path, 0, PROM_SCRATCH_SIZE);
1832 		/* Call OF to setup the TCE hardware */
1833 		if (call_prom("package-to-path", 3, 1, node,
1834 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1835 			prom_printf("package-to-path failed\n");
1836 		}
1837 
1838 		/* Save away the TCE table attributes for later use. */
1839 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1840 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1841 
1842 		prom_debug("TCE table: %s\n", path);
1843 		prom_debug("\tnode = 0x%x\n", node);
1844 		prom_debug("\tbase = 0x%x\n", base);
1845 		prom_debug("\tsize = 0x%x\n", minsize);
1846 
1847 		/* Initialize the table to have a one-to-one mapping
1848 		 * over the allocated size.
1849 		 */
1850 		tce_entryp = (u64 *)base;
1851 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1852 			tce_entry = (i << PAGE_SHIFT);
1853 			tce_entry |= 0x3;
1854 			*tce_entryp = tce_entry;
1855 		}
1856 
1857 		prom_printf("opening PHB %s", path);
1858 		phb_node = call_prom("open", 1, 1, path);
1859 		if (phb_node == 0)
1860 			prom_printf("... failed\n");
1861 		else
1862 			prom_printf("... done\n");
1863 
1864 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1865 			  phb_node, -1, minsize,
1866 			  (u32) base, (u32) (base >> 32));
1867 		call_prom("close", 1, 0, phb_node);
1868 	}
1869 
1870 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1871 
1872 	/* These are only really needed if there is a memory limit in
1873 	 * effect, but we don't know so export them always. */
1874 	prom_tce_alloc_start = local_alloc_bottom;
1875 	prom_tce_alloc_end = local_alloc_top;
1876 
1877 	/* Flag the first invalid entry */
1878 	prom_debug("ending prom_initialize_tce_table\n");
1879 }
1880 #endif /* __BIG_ENDIAN__ */
1881 #endif /* CONFIG_PPC64 */
1882 
1883 /*
1884  * With CHRP SMP we need to use the OF to start the other processors.
1885  * We can't wait until smp_boot_cpus (the OF is trashed by then)
1886  * so we have to put the processors into a holding pattern controlled
1887  * by the kernel (not OF) before we destroy the OF.
1888  *
1889  * This uses a chunk of low memory, puts some holding pattern
1890  * code there and sends the other processors off to there until
1891  * smp_boot_cpus tells them to do something.  The holding pattern
1892  * checks that address until its cpu # is there, when it is that
1893  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
1894  * of setting those values.
1895  *
1896  * We also use physical address 0x4 here to tell when a cpu
1897  * is in its holding pattern code.
1898  *
1899  * -- Cort
1900  */
1901 /*
1902  * We want to reference the copy of __secondary_hold_* in the
1903  * 0 - 0x100 address range
1904  */
1905 #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1906 
1907 static void __init prom_hold_cpus(void)
1908 {
1909 	unsigned long i;
1910 	phandle node;
1911 	char type[64];
1912 	unsigned long *spinloop
1913 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
1914 	unsigned long *acknowledge
1915 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1916 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
1917 
1918 	/*
1919 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
1920 	 * we skip this stage, the CPUs will be started by the
1921 	 * kernel using RTAS.
1922 	 */
1923 	if ((of_platform == PLATFORM_PSERIES ||
1924 	     of_platform == PLATFORM_PSERIES_LPAR) &&
1925 	    rtas_has_query_cpu_stopped) {
1926 		prom_printf("prom_hold_cpus: skipped\n");
1927 		return;
1928 	}
1929 
1930 	prom_debug("prom_hold_cpus: start...\n");
1931 	prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
1932 	prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
1933 	prom_debug("    1) acknowledge    = 0x%x\n",
1934 		   (unsigned long)acknowledge);
1935 	prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
1936 	prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
1937 
1938 	/* Set the common spinloop variable, so all of the secondary cpus
1939 	 * will block when they are awakened from their OF spinloop.
1940 	 * This must occur for both SMP and non SMP kernels, since OF will
1941 	 * be trashed when we move the kernel.
1942 	 */
1943 	*spinloop = 0;
1944 
1945 	/* look for cpus */
1946 	for (node = 0; prom_next_node(&node); ) {
1947 		unsigned int cpu_no;
1948 		__be32 reg;
1949 
1950 		type[0] = 0;
1951 		prom_getprop(node, "device_type", type, sizeof(type));
1952 		if (strcmp(type, "cpu") != 0)
1953 			continue;
1954 
1955 		/* Skip non-configured cpus. */
1956 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1957 			if (strcmp(type, "okay") != 0)
1958 				continue;
1959 
1960 		reg = cpu_to_be32(-1); /* make sparse happy */
1961 		prom_getprop(node, "reg", &reg, sizeof(reg));
1962 		cpu_no = be32_to_cpu(reg);
1963 
1964 		prom_debug("cpu hw idx   = %lu\n", cpu_no);
1965 
1966 		/* Init the acknowledge var which will be reset by
1967 		 * the secondary cpu when it awakens from its OF
1968 		 * spinloop.
1969 		 */
1970 		*acknowledge = (unsigned long)-1;
1971 
1972 		if (cpu_no != prom.cpu) {
1973 			/* Primary Thread of non-boot cpu or any thread */
1974 			prom_printf("starting cpu hw idx %lu... ", cpu_no);
1975 			call_prom("start-cpu", 3, 0, node,
1976 				  secondary_hold, cpu_no);
1977 
1978 			for (i = 0; (i < 100000000) &&
1979 			     (*acknowledge == ((unsigned long)-1)); i++ )
1980 				mb();
1981 
1982 			if (*acknowledge == cpu_no)
1983 				prom_printf("done\n");
1984 			else
1985 				prom_printf("failed: %x\n", *acknowledge);
1986 		}
1987 #ifdef CONFIG_SMP
1988 		else
1989 			prom_printf("boot cpu hw idx %lu\n", cpu_no);
1990 #endif /* CONFIG_SMP */
1991 	}
1992 
1993 	prom_debug("prom_hold_cpus: end...\n");
1994 }
1995 
1996 
1997 static void __init prom_init_client_services(unsigned long pp)
1998 {
1999 	/* Get a handle to the prom entry point before anything else */
2000 	prom_entry = pp;
2001 
2002 	/* get a handle for the stdout device */
2003 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
2004 	if (!PHANDLE_VALID(prom.chosen))
2005 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
2006 
2007 	/* get device tree root */
2008 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
2009 	if (!PHANDLE_VALID(prom.root))
2010 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
2011 
2012 	prom.mmumap = 0;
2013 }
2014 
2015 #ifdef CONFIG_PPC32
2016 /*
2017  * For really old powermacs, we need to map things we claim.
2018  * For that, we need the ihandle of the mmu.
2019  * Also, on the longtrail, we need to work around other bugs.
2020  */
2021 static void __init prom_find_mmu(void)
2022 {
2023 	phandle oprom;
2024 	char version[64];
2025 
2026 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
2027 	if (!PHANDLE_VALID(oprom))
2028 		return;
2029 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
2030 		return;
2031 	version[sizeof(version) - 1] = 0;
2032 	/* XXX might need to add other versions here */
2033 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
2034 		of_workarounds = OF_WA_CLAIM;
2035 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
2036 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
2037 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2038 	} else
2039 		return;
2040 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
2041 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
2042 		     sizeof(prom.mmumap));
2043 	prom.mmumap = be32_to_cpu(prom.mmumap);
2044 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2045 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
2046 }
2047 #else
2048 #define prom_find_mmu()
2049 #endif
2050 
2051 static void __init prom_init_stdout(void)
2052 {
2053 	char *path = of_stdout_device;
2054 	char type[16];
2055 	phandle stdout_node;
2056 	__be32 val;
2057 
2058 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
2059 		prom_panic("cannot find stdout");
2060 
2061 	prom.stdout = be32_to_cpu(val);
2062 
2063 	/* Get the full OF pathname of the stdout device */
2064 	memset(path, 0, 256);
2065 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
2066 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
2067 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2068 		     path, strlen(path) + 1);
2069 
2070 	/* instance-to-package fails on PA-Semi */
2071 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
2072 	if (stdout_node != PROM_ERROR) {
2073 		val = cpu_to_be32(stdout_node);
2074 		prom_setprop(prom.chosen, "/chosen", "linux,stdout-package",
2075 			     &val, sizeof(val));
2076 
2077 		/* If it's a display, note it */
2078 		memset(type, 0, sizeof(type));
2079 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
2080 		if (strcmp(type, "display") == 0)
2081 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
2082 	}
2083 }
2084 
2085 static int __init prom_find_machine_type(void)
2086 {
2087 	char compat[256];
2088 	int len, i = 0;
2089 #ifdef CONFIG_PPC64
2090 	phandle rtas;
2091 	int x;
2092 #endif
2093 
2094 	/* Look for a PowerMac or a Cell */
2095 	len = prom_getprop(prom.root, "compatible",
2096 			   compat, sizeof(compat)-1);
2097 	if (len > 0) {
2098 		compat[len] = 0;
2099 		while (i < len) {
2100 			char *p = &compat[i];
2101 			int sl = strlen(p);
2102 			if (sl == 0)
2103 				break;
2104 			if (strstr(p, "Power Macintosh") ||
2105 			    strstr(p, "MacRISC"))
2106 				return PLATFORM_POWERMAC;
2107 #ifdef CONFIG_PPC64
2108 			/* We must make sure we don't detect the IBM Cell
2109 			 * blades as pSeries due to some firmware issues,
2110 			 * so we do it here.
2111 			 */
2112 			if (strstr(p, "IBM,CBEA") ||
2113 			    strstr(p, "IBM,CPBW-1.0"))
2114 				return PLATFORM_GENERIC;
2115 #endif /* CONFIG_PPC64 */
2116 			i += sl + 1;
2117 		}
2118 	}
2119 #ifdef CONFIG_PPC64
2120 	/* Try to detect OPAL */
2121 	if (PHANDLE_VALID(call_prom("finddevice", 1, 1, ADDR("/ibm,opal"))))
2122 		return PLATFORM_OPAL;
2123 
2124 	/* Try to figure out if it's an IBM pSeries or any other
2125 	 * PAPR compliant platform. We assume it is if :
2126 	 *  - /device_type is "chrp" (please, do NOT use that for future
2127 	 *    non-IBM designs !
2128 	 *  - it has /rtas
2129 	 */
2130 	len = prom_getprop(prom.root, "device_type",
2131 			   compat, sizeof(compat)-1);
2132 	if (len <= 0)
2133 		return PLATFORM_GENERIC;
2134 	if (strcmp(compat, "chrp"))
2135 		return PLATFORM_GENERIC;
2136 
2137 	/* Default to pSeries. We need to know if we are running LPAR */
2138 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2139 	if (!PHANDLE_VALID(rtas))
2140 		return PLATFORM_GENERIC;
2141 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
2142 	if (x != PROM_ERROR) {
2143 		prom_debug("Hypertas detected, assuming LPAR !\n");
2144 		return PLATFORM_PSERIES_LPAR;
2145 	}
2146 	return PLATFORM_PSERIES;
2147 #else
2148 	return PLATFORM_GENERIC;
2149 #endif
2150 }
2151 
2152 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
2153 {
2154 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
2155 }
2156 
2157 /*
2158  * If we have a display that we don't know how to drive,
2159  * we will want to try to execute OF's open method for it
2160  * later.  However, OF will probably fall over if we do that
2161  * we've taken over the MMU.
2162  * So we check whether we will need to open the display,
2163  * and if so, open it now.
2164  */
2165 static void __init prom_check_displays(void)
2166 {
2167 	char type[16], *path;
2168 	phandle node;
2169 	ihandle ih;
2170 	int i;
2171 
2172 	static unsigned char default_colors[] = {
2173 		0x00, 0x00, 0x00,
2174 		0x00, 0x00, 0xaa,
2175 		0x00, 0xaa, 0x00,
2176 		0x00, 0xaa, 0xaa,
2177 		0xaa, 0x00, 0x00,
2178 		0xaa, 0x00, 0xaa,
2179 		0xaa, 0xaa, 0x00,
2180 		0xaa, 0xaa, 0xaa,
2181 		0x55, 0x55, 0x55,
2182 		0x55, 0x55, 0xff,
2183 		0x55, 0xff, 0x55,
2184 		0x55, 0xff, 0xff,
2185 		0xff, 0x55, 0x55,
2186 		0xff, 0x55, 0xff,
2187 		0xff, 0xff, 0x55,
2188 		0xff, 0xff, 0xff
2189 	};
2190 	const unsigned char *clut;
2191 
2192 	prom_debug("Looking for displays\n");
2193 	for (node = 0; prom_next_node(&node); ) {
2194 		memset(type, 0, sizeof(type));
2195 		prom_getprop(node, "device_type", type, sizeof(type));
2196 		if (strcmp(type, "display") != 0)
2197 			continue;
2198 
2199 		/* It seems OF doesn't null-terminate the path :-( */
2200 		path = prom_scratch;
2201 		memset(path, 0, PROM_SCRATCH_SIZE);
2202 
2203 		/*
2204 		 * leave some room at the end of the path for appending extra
2205 		 * arguments
2206 		 */
2207 		if (call_prom("package-to-path", 3, 1, node, path,
2208 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
2209 			continue;
2210 		prom_printf("found display   : %s, opening... ", path);
2211 
2212 		ih = call_prom("open", 1, 1, path);
2213 		if (ih == 0) {
2214 			prom_printf("failed\n");
2215 			continue;
2216 		}
2217 
2218 		/* Success */
2219 		prom_printf("done\n");
2220 		prom_setprop(node, path, "linux,opened", NULL, 0);
2221 
2222 		/* Setup a usable color table when the appropriate
2223 		 * method is available. Should update this to set-colors */
2224 		clut = default_colors;
2225 		for (i = 0; i < 16; i++, clut += 3)
2226 			if (prom_set_color(ih, i, clut[0], clut[1],
2227 					   clut[2]) != 0)
2228 				break;
2229 
2230 #ifdef CONFIG_LOGO_LINUX_CLUT224
2231 		clut = PTRRELOC(logo_linux_clut224.clut);
2232 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
2233 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
2234 					   clut[2]) != 0)
2235 				break;
2236 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
2237 
2238 #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
2239 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
2240 		    PROM_ERROR) {
2241 			u32 width, height, pitch, addr;
2242 
2243 			prom_printf("Setting btext !\n");
2244 			prom_getprop(node, "width", &width, 4);
2245 			prom_getprop(node, "height", &height, 4);
2246 			prom_getprop(node, "linebytes", &pitch, 4);
2247 			prom_getprop(node, "address", &addr, 4);
2248 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
2249 				    width, height, pitch, addr);
2250 			btext_setup_display(width, height, 8, pitch, addr);
2251 		}
2252 #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
2253 	}
2254 }
2255 
2256 
2257 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
2258 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
2259 			      unsigned long needed, unsigned long align)
2260 {
2261 	void *ret;
2262 
2263 	*mem_start = _ALIGN(*mem_start, align);
2264 	while ((*mem_start + needed) > *mem_end) {
2265 		unsigned long room, chunk;
2266 
2267 		prom_debug("Chunk exhausted, claiming more at %x...\n",
2268 			   alloc_bottom);
2269 		room = alloc_top - alloc_bottom;
2270 		if (room > DEVTREE_CHUNK_SIZE)
2271 			room = DEVTREE_CHUNK_SIZE;
2272 		if (room < PAGE_SIZE)
2273 			prom_panic("No memory for flatten_device_tree "
2274 				   "(no room)\n");
2275 		chunk = alloc_up(room, 0);
2276 		if (chunk == 0)
2277 			prom_panic("No memory for flatten_device_tree "
2278 				   "(claim failed)\n");
2279 		*mem_end = chunk + room;
2280 	}
2281 
2282 	ret = (void *)*mem_start;
2283 	*mem_start += needed;
2284 
2285 	return ret;
2286 }
2287 
2288 #define dt_push_token(token, mem_start, mem_end) do { 			\
2289 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2290 		*(__be32 *)room = cpu_to_be32(token);			\
2291 	} while(0)
2292 
2293 static unsigned long __init dt_find_string(char *str)
2294 {
2295 	char *s, *os;
2296 
2297 	s = os = (char *)dt_string_start;
2298 	s += 4;
2299 	while (s <  (char *)dt_string_end) {
2300 		if (strcmp(s, str) == 0)
2301 			return s - os;
2302 		s += strlen(s) + 1;
2303 	}
2304 	return 0;
2305 }
2306 
2307 /*
2308  * The Open Firmware 1275 specification states properties must be 31 bytes or
2309  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
2310  */
2311 #define MAX_PROPERTY_NAME 64
2312 
2313 static void __init scan_dt_build_strings(phandle node,
2314 					 unsigned long *mem_start,
2315 					 unsigned long *mem_end)
2316 {
2317 	char *prev_name, *namep, *sstart;
2318 	unsigned long soff;
2319 	phandle child;
2320 
2321 	sstart =  (char *)dt_string_start;
2322 
2323 	/* get and store all property names */
2324 	prev_name = "";
2325 	for (;;) {
2326 		/* 64 is max len of name including nul. */
2327 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
2328 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
2329 			/* No more nodes: unwind alloc */
2330 			*mem_start = (unsigned long)namep;
2331 			break;
2332 		}
2333 
2334  		/* skip "name" */
2335  		if (strcmp(namep, "name") == 0) {
2336  			*mem_start = (unsigned long)namep;
2337  			prev_name = "name";
2338  			continue;
2339  		}
2340 		/* get/create string entry */
2341 		soff = dt_find_string(namep);
2342 		if (soff != 0) {
2343 			*mem_start = (unsigned long)namep;
2344 			namep = sstart + soff;
2345 		} else {
2346 			/* Trim off some if we can */
2347 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
2348 			dt_string_end = *mem_start;
2349 		}
2350 		prev_name = namep;
2351 	}
2352 
2353 	/* do all our children */
2354 	child = call_prom("child", 1, 1, node);
2355 	while (child != 0) {
2356 		scan_dt_build_strings(child, mem_start, mem_end);
2357 		child = call_prom("peer", 1, 1, child);
2358 	}
2359 }
2360 
2361 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
2362 					unsigned long *mem_end)
2363 {
2364 	phandle child;
2365 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
2366 	unsigned long soff;
2367 	unsigned char *valp;
2368 	static char pname[MAX_PROPERTY_NAME];
2369 	int l, room, has_phandle = 0;
2370 
2371 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
2372 
2373 	/* get the node's full name */
2374 	namep = (char *)*mem_start;
2375 	room = *mem_end - *mem_start;
2376 	if (room > 255)
2377 		room = 255;
2378 	l = call_prom("package-to-path", 3, 1, node, namep, room);
2379 	if (l >= 0) {
2380 		/* Didn't fit?  Get more room. */
2381 		if (l >= room) {
2382 			if (l >= *mem_end - *mem_start)
2383 				namep = make_room(mem_start, mem_end, l+1, 1);
2384 			call_prom("package-to-path", 3, 1, node, namep, l);
2385 		}
2386 		namep[l] = '\0';
2387 
2388 		/* Fixup an Apple bug where they have bogus \0 chars in the
2389 		 * middle of the path in some properties, and extract
2390 		 * the unit name (everything after the last '/').
2391 		 */
2392 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
2393 			if (*p == '/')
2394 				lp = namep;
2395 			else if (*p != 0)
2396 				*lp++ = *p;
2397 		}
2398 		*lp = 0;
2399 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
2400 	}
2401 
2402 	/* get it again for debugging */
2403 	path = prom_scratch;
2404 	memset(path, 0, PROM_SCRATCH_SIZE);
2405 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
2406 
2407 	/* get and store all properties */
2408 	prev_name = "";
2409 	sstart = (char *)dt_string_start;
2410 	for (;;) {
2411 		if (call_prom("nextprop", 3, 1, node, prev_name,
2412 			      pname) != 1)
2413 			break;
2414 
2415  		/* skip "name" */
2416  		if (strcmp(pname, "name") == 0) {
2417  			prev_name = "name";
2418  			continue;
2419  		}
2420 
2421 		/* find string offset */
2422 		soff = dt_find_string(pname);
2423 		if (soff == 0) {
2424 			prom_printf("WARNING: Can't find string index for"
2425 				    " <%s>, node %s\n", pname, path);
2426 			break;
2427 		}
2428 		prev_name = sstart + soff;
2429 
2430 		/* get length */
2431 		l = call_prom("getproplen", 2, 1, node, pname);
2432 
2433 		/* sanity checks */
2434 		if (l == PROM_ERROR)
2435 			continue;
2436 
2437 		/* push property head */
2438 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
2439 		dt_push_token(l, mem_start, mem_end);
2440 		dt_push_token(soff, mem_start, mem_end);
2441 
2442 		/* push property content */
2443 		valp = make_room(mem_start, mem_end, l, 4);
2444 		call_prom("getprop", 4, 1, node, pname, valp, l);
2445 		*mem_start = _ALIGN(*mem_start, 4);
2446 
2447 		if (!strcmp(pname, "phandle"))
2448 			has_phandle = 1;
2449 	}
2450 
2451 	/* Add a "linux,phandle" property if no "phandle" property already
2452 	 * existed (can happen with OPAL)
2453 	 */
2454 	if (!has_phandle) {
2455 		soff = dt_find_string("linux,phandle");
2456 		if (soff == 0)
2457 			prom_printf("WARNING: Can't find string index for"
2458 				    " <linux-phandle> node %s\n", path);
2459 		else {
2460 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
2461 			dt_push_token(4, mem_start, mem_end);
2462 			dt_push_token(soff, mem_start, mem_end);
2463 			valp = make_room(mem_start, mem_end, 4, 4);
2464 			*(__be32 *)valp = cpu_to_be32(node);
2465 		}
2466 	}
2467 
2468 	/* do all our children */
2469 	child = call_prom("child", 1, 1, node);
2470 	while (child != 0) {
2471 		scan_dt_build_struct(child, mem_start, mem_end);
2472 		child = call_prom("peer", 1, 1, child);
2473 	}
2474 
2475 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
2476 }
2477 
2478 static void __init flatten_device_tree(void)
2479 {
2480 	phandle root;
2481 	unsigned long mem_start, mem_end, room;
2482 	struct boot_param_header *hdr;
2483 	char *namep;
2484 	u64 *rsvmap;
2485 
2486 	/*
2487 	 * Check how much room we have between alloc top & bottom (+/- a
2488 	 * few pages), crop to 1MB, as this is our "chunk" size
2489 	 */
2490 	room = alloc_top - alloc_bottom - 0x4000;
2491 	if (room > DEVTREE_CHUNK_SIZE)
2492 		room = DEVTREE_CHUNK_SIZE;
2493 	prom_debug("starting device tree allocs at %x\n", alloc_bottom);
2494 
2495 	/* Now try to claim that */
2496 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
2497 	if (mem_start == 0)
2498 		prom_panic("Can't allocate initial device-tree chunk\n");
2499 	mem_end = mem_start + room;
2500 
2501 	/* Get root of tree */
2502 	root = call_prom("peer", 1, 1, (phandle)0);
2503 	if (root == (phandle)0)
2504 		prom_panic ("couldn't get device tree root\n");
2505 
2506 	/* Build header and make room for mem rsv map */
2507 	mem_start = _ALIGN(mem_start, 4);
2508 	hdr = make_room(&mem_start, &mem_end,
2509 			sizeof(struct boot_param_header), 4);
2510 	dt_header_start = (unsigned long)hdr;
2511 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
2512 
2513 	/* Start of strings */
2514 	mem_start = PAGE_ALIGN(mem_start);
2515 	dt_string_start = mem_start;
2516 	mem_start += 4; /* hole */
2517 
2518 	/* Add "linux,phandle" in there, we'll need it */
2519 	namep = make_room(&mem_start, &mem_end, 16, 1);
2520 	strcpy(namep, "linux,phandle");
2521 	mem_start = (unsigned long)namep + strlen(namep) + 1;
2522 
2523 	/* Build string array */
2524 	prom_printf("Building dt strings...\n");
2525 	scan_dt_build_strings(root, &mem_start, &mem_end);
2526 	dt_string_end = mem_start;
2527 
2528 	/* Build structure */
2529 	mem_start = PAGE_ALIGN(mem_start);
2530 	dt_struct_start = mem_start;
2531 	prom_printf("Building dt structure...\n");
2532 	scan_dt_build_struct(root, &mem_start, &mem_end);
2533 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
2534 	dt_struct_end = PAGE_ALIGN(mem_start);
2535 
2536 	/* Finish header */
2537 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2538 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2539 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2540 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2541 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2542 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2543 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2544 	hdr->version = cpu_to_be32(OF_DT_VERSION);
2545 	/* Version 16 is not backward compatible */
2546 	hdr->last_comp_version = cpu_to_be32(0x10);
2547 
2548 	/* Copy the reserve map in */
2549 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
2550 
2551 #ifdef DEBUG_PROM
2552 	{
2553 		int i;
2554 		prom_printf("reserved memory map:\n");
2555 		for (i = 0; i < mem_reserve_cnt; i++)
2556 			prom_printf("  %x - %x\n",
2557 				    be64_to_cpu(mem_reserve_map[i].base),
2558 				    be64_to_cpu(mem_reserve_map[i].size));
2559 	}
2560 #endif
2561 	/* Bump mem_reserve_cnt to cause further reservations to fail
2562 	 * since it's too late.
2563 	 */
2564 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
2565 
2566 	prom_printf("Device tree strings 0x%x -> 0x%x\n",
2567 		    dt_string_start, dt_string_end);
2568 	prom_printf("Device tree struct  0x%x -> 0x%x\n",
2569 		    dt_struct_start, dt_struct_end);
2570 }
2571 
2572 #ifdef CONFIG_PPC_MAPLE
2573 /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2574  * The values are bad, and it doesn't even have the right number of cells. */
2575 static void __init fixup_device_tree_maple(void)
2576 {
2577 	phandle isa;
2578 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
2579 	u32 isa_ranges[6];
2580 	char *name;
2581 
2582 	name = "/ht@0/isa@4";
2583 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2584 	if (!PHANDLE_VALID(isa)) {
2585 		name = "/ht@0/isa@6";
2586 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2587 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2588 	}
2589 	if (!PHANDLE_VALID(isa))
2590 		return;
2591 
2592 	if (prom_getproplen(isa, "ranges") != 12)
2593 		return;
2594 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2595 		== PROM_ERROR)
2596 		return;
2597 
2598 	if (isa_ranges[0] != 0x1 ||
2599 		isa_ranges[1] != 0xf4000000 ||
2600 		isa_ranges[2] != 0x00010000)
2601 		return;
2602 
2603 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
2604 
2605 	isa_ranges[0] = 0x1;
2606 	isa_ranges[1] = 0x0;
2607 	isa_ranges[2] = rloc;
2608 	isa_ranges[3] = 0x0;
2609 	isa_ranges[4] = 0x0;
2610 	isa_ranges[5] = 0x00010000;
2611 	prom_setprop(isa, name, "ranges",
2612 			isa_ranges, sizeof(isa_ranges));
2613 }
2614 
2615 #define CPC925_MC_START		0xf8000000
2616 #define CPC925_MC_LENGTH	0x1000000
2617 /* The values for memory-controller don't have right number of cells */
2618 static void __init fixup_device_tree_maple_memory_controller(void)
2619 {
2620 	phandle mc;
2621 	u32 mc_reg[4];
2622 	char *name = "/hostbridge@f8000000";
2623 	u32 ac, sc;
2624 
2625 	mc = call_prom("finddevice", 1, 1, ADDR(name));
2626 	if (!PHANDLE_VALID(mc))
2627 		return;
2628 
2629 	if (prom_getproplen(mc, "reg") != 8)
2630 		return;
2631 
2632 	prom_getprop(prom.root, "#address-cells", &ac, sizeof(ac));
2633 	prom_getprop(prom.root, "#size-cells", &sc, sizeof(sc));
2634 	if ((ac != 2) || (sc != 2))
2635 		return;
2636 
2637 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
2638 		return;
2639 
2640 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
2641 		return;
2642 
2643 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
2644 
2645 	mc_reg[0] = 0x0;
2646 	mc_reg[1] = CPC925_MC_START;
2647 	mc_reg[2] = 0x0;
2648 	mc_reg[3] = CPC925_MC_LENGTH;
2649 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
2650 }
2651 #else
2652 #define fixup_device_tree_maple()
2653 #define fixup_device_tree_maple_memory_controller()
2654 #endif
2655 
2656 #ifdef CONFIG_PPC_CHRP
2657 /*
2658  * Pegasos and BriQ lacks the "ranges" property in the isa node
2659  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2660  * Pegasos has the IDE configured in legacy mode, but advertised as native
2661  */
2662 static void __init fixup_device_tree_chrp(void)
2663 {
2664 	phandle ph;
2665 	u32 prop[6];
2666 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2667 	char *name;
2668 	int rc;
2669 
2670 	name = "/pci@80000000/isa@c";
2671 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2672 	if (!PHANDLE_VALID(ph)) {
2673 		name = "/pci@ff500000/isa@6";
2674 		ph = call_prom("finddevice", 1, 1, ADDR(name));
2675 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2676 	}
2677 	if (PHANDLE_VALID(ph)) {
2678 		rc = prom_getproplen(ph, "ranges");
2679 		if (rc == 0 || rc == PROM_ERROR) {
2680 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2681 
2682 			prop[0] = 0x1;
2683 			prop[1] = 0x0;
2684 			prop[2] = rloc;
2685 			prop[3] = 0x0;
2686 			prop[4] = 0x0;
2687 			prop[5] = 0x00010000;
2688 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2689 		}
2690 	}
2691 
2692 	name = "/pci@80000000/ide@C,1";
2693 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2694 	if (PHANDLE_VALID(ph)) {
2695 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2696 		prop[0] = 14;
2697 		prop[1] = 0x0;
2698 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2699 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2700 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2701 		if (rc == sizeof(u32)) {
2702 			prop[0] &= ~0x5;
2703 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2704 		}
2705 	}
2706 }
2707 #else
2708 #define fixup_device_tree_chrp()
2709 #endif
2710 
2711 #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
2712 static void __init fixup_device_tree_pmac(void)
2713 {
2714 	phandle u3, i2c, mpic;
2715 	u32 u3_rev;
2716 	u32 interrupts[2];
2717 	u32 parent;
2718 
2719 	/* Some G5s have a missing interrupt definition, fix it up here */
2720 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2721 	if (!PHANDLE_VALID(u3))
2722 		return;
2723 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2724 	if (!PHANDLE_VALID(i2c))
2725 		return;
2726 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2727 	if (!PHANDLE_VALID(mpic))
2728 		return;
2729 
2730 	/* check if proper rev of u3 */
2731 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2732 	    == PROM_ERROR)
2733 		return;
2734 	if (u3_rev < 0x35 || u3_rev > 0x39)
2735 		return;
2736 	/* does it need fixup ? */
2737 	if (prom_getproplen(i2c, "interrupts") > 0)
2738 		return;
2739 
2740 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2741 
2742 	/* interrupt on this revision of u3 is number 0 and level */
2743 	interrupts[0] = 0;
2744 	interrupts[1] = 1;
2745 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2746 		     &interrupts, sizeof(interrupts));
2747 	parent = (u32)mpic;
2748 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2749 		     &parent, sizeof(parent));
2750 }
2751 #else
2752 #define fixup_device_tree_pmac()
2753 #endif
2754 
2755 #ifdef CONFIG_PPC_EFIKA
2756 /*
2757  * The MPC5200 FEC driver requires an phy-handle property to tell it how
2758  * to talk to the phy.  If the phy-handle property is missing, then this
2759  * function is called to add the appropriate nodes and link it to the
2760  * ethernet node.
2761  */
2762 static void __init fixup_device_tree_efika_add_phy(void)
2763 {
2764 	u32 node;
2765 	char prop[64];
2766 	int rv;
2767 
2768 	/* Check if /builtin/ethernet exists - bail if it doesn't */
2769 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
2770 	if (!PHANDLE_VALID(node))
2771 		return;
2772 
2773 	/* Check if the phy-handle property exists - bail if it does */
2774 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2775 	if (!rv)
2776 		return;
2777 
2778 	/*
2779 	 * At this point the ethernet device doesn't have a phy described.
2780 	 * Now we need to add the missing phy node and linkage
2781 	 */
2782 
2783 	/* Check for an MDIO bus node - if missing then create one */
2784 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2785 	if (!PHANDLE_VALID(node)) {
2786 		prom_printf("Adding Ethernet MDIO node\n");
2787 		call_prom("interpret", 1, 1,
2788 			" s\" /builtin\" find-device"
2789 			" new-device"
2790 				" 1 encode-int s\" #address-cells\" property"
2791 				" 0 encode-int s\" #size-cells\" property"
2792 				" s\" mdio\" device-name"
2793 				" s\" fsl,mpc5200b-mdio\" encode-string"
2794 				" s\" compatible\" property"
2795 				" 0xf0003000 0x400 reg"
2796 				" 0x2 encode-int"
2797 				" 0x5 encode-int encode+"
2798 				" 0x3 encode-int encode+"
2799 				" s\" interrupts\" property"
2800 			" finish-device");
2801 	};
2802 
2803 	/* Check for a PHY device node - if missing then create one and
2804 	 * give it's phandle to the ethernet node */
2805 	node = call_prom("finddevice", 1, 1,
2806 			 ADDR("/builtin/mdio/ethernet-phy"));
2807 	if (!PHANDLE_VALID(node)) {
2808 		prom_printf("Adding Ethernet PHY node\n");
2809 		call_prom("interpret", 1, 1,
2810 			" s\" /builtin/mdio\" find-device"
2811 			" new-device"
2812 				" s\" ethernet-phy\" device-name"
2813 				" 0x10 encode-int s\" reg\" property"
2814 				" my-self"
2815 				" ihandle>phandle"
2816 			" finish-device"
2817 			" s\" /builtin/ethernet\" find-device"
2818 				" encode-int"
2819 				" s\" phy-handle\" property"
2820 			" device-end");
2821 	}
2822 }
2823 
2824 static void __init fixup_device_tree_efika(void)
2825 {
2826 	int sound_irq[3] = { 2, 2, 0 };
2827 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2828 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
2829 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
2830 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2831 	u32 node;
2832 	char prop[64];
2833 	int rv, len;
2834 
2835 	/* Check if we're really running on a EFIKA */
2836 	node = call_prom("finddevice", 1, 1, ADDR("/"));
2837 	if (!PHANDLE_VALID(node))
2838 		return;
2839 
2840 	rv = prom_getprop(node, "model", prop, sizeof(prop));
2841 	if (rv == PROM_ERROR)
2842 		return;
2843 	if (strcmp(prop, "EFIKA5K2"))
2844 		return;
2845 
2846 	prom_printf("Applying EFIKA device tree fixups\n");
2847 
2848 	/* Claiming to be 'chrp' is death */
2849 	node = call_prom("finddevice", 1, 1, ADDR("/"));
2850 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2851 	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2852 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2853 
2854 	/* CODEGEN,description is exposed in /proc/cpuinfo so
2855 	   fix that too */
2856 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2857 	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2858 		prom_setprop(node, "/", "CODEGEN,description",
2859 			     "Efika 5200B PowerPC System",
2860 			     sizeof("Efika 5200B PowerPC System"));
2861 
2862 	/* Fixup bestcomm interrupts property */
2863 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2864 	if (PHANDLE_VALID(node)) {
2865 		len = prom_getproplen(node, "interrupts");
2866 		if (len == 12) {
2867 			prom_printf("Fixing bestcomm interrupts property\n");
2868 			prom_setprop(node, "/builtin/bestcom", "interrupts",
2869 				     bcomm_irq, sizeof(bcomm_irq));
2870 		}
2871 	}
2872 
2873 	/* Fixup sound interrupts property */
2874 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2875 	if (PHANDLE_VALID(node)) {
2876 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2877 		if (rv == PROM_ERROR) {
2878 			prom_printf("Adding sound interrupts property\n");
2879 			prom_setprop(node, "/builtin/sound", "interrupts",
2880 				     sound_irq, sizeof(sound_irq));
2881 		}
2882 	}
2883 
2884 	/* Make sure ethernet phy-handle property exists */
2885 	fixup_device_tree_efika_add_phy();
2886 }
2887 #else
2888 #define fixup_device_tree_efika()
2889 #endif
2890 
2891 #ifdef CONFIG_PPC_PASEMI_NEMO
2892 /*
2893  * CFE supplied on Nemo is broken in several ways, biggest
2894  * problem is that it reassigns ISA interrupts to unused mpic ints.
2895  * Add an interrupt-controller property for the io-bridge to use
2896  * and correct the ints so we can attach them to an irq_domain
2897  */
2898 static void __init fixup_device_tree_pasemi(void)
2899 {
2900 	u32 interrupts[2], parent, rval, val = 0;
2901 	char *name, *pci_name;
2902 	phandle iob, node;
2903 
2904 	/* Find the root pci node */
2905 	name = "/pxp@0,e0000000";
2906 	iob = call_prom("finddevice", 1, 1, ADDR(name));
2907 	if (!PHANDLE_VALID(iob))
2908 		return;
2909 
2910 	/* check if interrupt-controller node set yet */
2911 	if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
2912 		return;
2913 
2914 	prom_printf("adding interrupt-controller property for SB600...\n");
2915 
2916 	prom_setprop(iob, name, "interrupt-controller", &val, 0);
2917 
2918 	pci_name = "/pxp@0,e0000000/pci@11";
2919 	node = call_prom("finddevice", 1, 1, ADDR(pci_name));
2920 	parent = ADDR(iob);
2921 
2922 	for( ; prom_next_node(&node); ) {
2923 		/* scan each node for one with an interrupt */
2924 		if (!PHANDLE_VALID(node))
2925 			continue;
2926 
2927 		rval = prom_getproplen(node, "interrupts");
2928 		if (rval == 0 || rval == PROM_ERROR)
2929 			continue;
2930 
2931 		prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
2932 		if ((interrupts[0] < 212) || (interrupts[0] > 222))
2933 			continue;
2934 
2935 		/* found a node, update both interrupts and interrupt-parent */
2936 		if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
2937 			interrupts[0] -= 203;
2938 		if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
2939 			interrupts[0] -= 213;
2940 		if (interrupts[0] == 221)
2941 			interrupts[0] = 14;
2942 		if (interrupts[0] == 222)
2943 			interrupts[0] = 8;
2944 
2945 		prom_setprop(node, pci_name, "interrupts", interrupts,
2946 					sizeof(interrupts));
2947 		prom_setprop(node, pci_name, "interrupt-parent", &parent,
2948 					sizeof(parent));
2949 	}
2950 
2951 	/*
2952 	 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
2953 	 * so that generic isa-bridge code can add the SB600 and its on-board
2954 	 * peripherals.
2955 	 */
2956 	name = "/pxp@0,e0000000/io-bridge@0";
2957 	iob = call_prom("finddevice", 1, 1, ADDR(name));
2958 	if (!PHANDLE_VALID(iob))
2959 		return;
2960 
2961 	/* device_type is already set, just change it. */
2962 
2963 	prom_printf("Changing device_type of SB600 node...\n");
2964 
2965 	prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
2966 }
2967 #else	/* !CONFIG_PPC_PASEMI_NEMO */
2968 static inline void fixup_device_tree_pasemi(void) { }
2969 #endif
2970 
2971 static void __init fixup_device_tree(void)
2972 {
2973 	fixup_device_tree_maple();
2974 	fixup_device_tree_maple_memory_controller();
2975 	fixup_device_tree_chrp();
2976 	fixup_device_tree_pmac();
2977 	fixup_device_tree_efika();
2978 	fixup_device_tree_pasemi();
2979 }
2980 
2981 static void __init prom_find_boot_cpu(void)
2982 {
2983 	__be32 rval;
2984 	ihandle prom_cpu;
2985 	phandle cpu_pkg;
2986 
2987 	rval = 0;
2988 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
2989 		return;
2990 	prom_cpu = be32_to_cpu(rval);
2991 
2992 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2993 
2994 	if (!PHANDLE_VALID(cpu_pkg))
2995 		return;
2996 
2997 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
2998 	prom.cpu = be32_to_cpu(rval);
2999 
3000 	prom_debug("Booting CPU hw index = %lu\n", prom.cpu);
3001 }
3002 
3003 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
3004 {
3005 #ifdef CONFIG_BLK_DEV_INITRD
3006 	if (r3 && r4 && r4 != 0xdeadbeef) {
3007 		__be64 val;
3008 
3009 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
3010 		prom_initrd_end = prom_initrd_start + r4;
3011 
3012 		val = cpu_to_be64(prom_initrd_start);
3013 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
3014 			     &val, sizeof(val));
3015 		val = cpu_to_be64(prom_initrd_end);
3016 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
3017 			     &val, sizeof(val));
3018 
3019 		reserve_mem(prom_initrd_start,
3020 			    prom_initrd_end - prom_initrd_start);
3021 
3022 		prom_debug("initrd_start=0x%x\n", prom_initrd_start);
3023 		prom_debug("initrd_end=0x%x\n", prom_initrd_end);
3024 	}
3025 #endif /* CONFIG_BLK_DEV_INITRD */
3026 }
3027 
3028 #ifdef CONFIG_PPC64
3029 #ifdef CONFIG_RELOCATABLE
3030 static void reloc_toc(void)
3031 {
3032 }
3033 
3034 static void unreloc_toc(void)
3035 {
3036 }
3037 #else
3038 static void __reloc_toc(unsigned long offset, unsigned long nr_entries)
3039 {
3040 	unsigned long i;
3041 	unsigned long *toc_entry;
3042 
3043 	/* Get the start of the TOC by using r2 directly. */
3044 	asm volatile("addi %0,2,-0x8000" : "=b" (toc_entry));
3045 
3046 	for (i = 0; i < nr_entries; i++) {
3047 		*toc_entry = *toc_entry + offset;
3048 		toc_entry++;
3049 	}
3050 }
3051 
3052 static void reloc_toc(void)
3053 {
3054 	unsigned long offset = reloc_offset();
3055 	unsigned long nr_entries =
3056 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
3057 
3058 	__reloc_toc(offset, nr_entries);
3059 
3060 	mb();
3061 }
3062 
3063 static void unreloc_toc(void)
3064 {
3065 	unsigned long offset = reloc_offset();
3066 	unsigned long nr_entries =
3067 		(__prom_init_toc_end - __prom_init_toc_start) / sizeof(long);
3068 
3069 	mb();
3070 
3071 	__reloc_toc(-offset, nr_entries);
3072 }
3073 #endif
3074 #endif
3075 
3076 /*
3077  * We enter here early on, when the Open Firmware prom is still
3078  * handling exceptions and the MMU hash table for us.
3079  */
3080 
3081 unsigned long __init prom_init(unsigned long r3, unsigned long r4,
3082 			       unsigned long pp,
3083 			       unsigned long r6, unsigned long r7,
3084 			       unsigned long kbase)
3085 {
3086 	unsigned long hdr;
3087 
3088 #ifdef CONFIG_PPC32
3089 	unsigned long offset = reloc_offset();
3090 	reloc_got2(offset);
3091 #else
3092 	reloc_toc();
3093 #endif
3094 
3095 	/*
3096 	 * First zero the BSS
3097 	 */
3098 	memset(&__bss_start, 0, __bss_stop - __bss_start);
3099 
3100 	/*
3101 	 * Init interface to Open Firmware, get some node references,
3102 	 * like /chosen
3103 	 */
3104 	prom_init_client_services(pp);
3105 
3106 	/*
3107 	 * See if this OF is old enough that we need to do explicit maps
3108 	 * and other workarounds
3109 	 */
3110 	prom_find_mmu();
3111 
3112 	/*
3113 	 * Init prom stdout device
3114 	 */
3115 	prom_init_stdout();
3116 
3117 	prom_printf("Preparing to boot %s", linux_banner);
3118 
3119 	/*
3120 	 * Get default machine type. At this point, we do not differentiate
3121 	 * between pSeries SMP and pSeries LPAR
3122 	 */
3123 	of_platform = prom_find_machine_type();
3124 	prom_printf("Detected machine type: %x\n", of_platform);
3125 
3126 #ifndef CONFIG_NONSTATIC_KERNEL
3127 	/* Bail if this is a kdump kernel. */
3128 	if (PHYSICAL_START > 0)
3129 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
3130 #endif
3131 
3132 	/*
3133 	 * Check for an initrd
3134 	 */
3135 	prom_check_initrd(r3, r4);
3136 
3137 	/*
3138 	 * Do early parsing of command line
3139 	 */
3140 	early_cmdline_parse();
3141 
3142 #if defined(CONFIG_PPC_PSERIES) || defined(CONFIG_PPC_POWERNV)
3143 	/*
3144 	 * On pSeries, inform the firmware about our capabilities
3145 	 */
3146 	if (of_platform == PLATFORM_PSERIES ||
3147 	    of_platform == PLATFORM_PSERIES_LPAR)
3148 		prom_send_capabilities();
3149 #endif
3150 
3151 	/*
3152 	 * Copy the CPU hold code
3153 	 */
3154 	if (of_platform != PLATFORM_POWERMAC)
3155 		copy_and_flush(0, kbase, 0x100, 0);
3156 
3157 	/*
3158 	 * Initialize memory management within prom_init
3159 	 */
3160 	prom_init_mem();
3161 
3162 	/*
3163 	 * Determine which cpu is actually running right _now_
3164 	 */
3165 	prom_find_boot_cpu();
3166 
3167 	/*
3168 	 * Initialize display devices
3169 	 */
3170 	prom_check_displays();
3171 
3172 #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
3173 	/*
3174 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
3175 	 * that uses the allocator, we need to make sure we get the top of memory
3176 	 * available for us here...
3177 	 */
3178 	if (of_platform == PLATFORM_PSERIES)
3179 		prom_initialize_tce_table();
3180 #endif
3181 
3182 	/*
3183 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
3184 	 * have a usable RTAS implementation.
3185 	 */
3186 	if (of_platform != PLATFORM_POWERMAC &&
3187 	    of_platform != PLATFORM_OPAL)
3188 		prom_instantiate_rtas();
3189 
3190 #ifdef CONFIG_PPC_POWERNV
3191 	if (of_platform == PLATFORM_OPAL)
3192 		prom_instantiate_opal();
3193 #endif /* CONFIG_PPC_POWERNV */
3194 
3195 #ifdef CONFIG_PPC64
3196 	/* instantiate sml */
3197 	prom_instantiate_sml();
3198 #endif
3199 
3200 	/*
3201 	 * On non-powermacs, put all CPUs in spin-loops.
3202 	 *
3203 	 * PowerMacs use a different mechanism to spin CPUs
3204 	 *
3205 	 * (This must be done after instanciating RTAS)
3206 	 */
3207 	if (of_platform != PLATFORM_POWERMAC &&
3208 	    of_platform != PLATFORM_OPAL)
3209 		prom_hold_cpus();
3210 
3211 	/*
3212 	 * Fill in some infos for use by the kernel later on
3213 	 */
3214 	if (prom_memory_limit) {
3215 		__be64 val = cpu_to_be64(prom_memory_limit);
3216 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3217 			     &val, sizeof(val));
3218 	}
3219 #ifdef CONFIG_PPC64
3220 	if (prom_iommu_off)
3221 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3222 			     NULL, 0);
3223 
3224 	if (prom_iommu_force_on)
3225 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3226 			     NULL, 0);
3227 
3228 	if (prom_tce_alloc_start) {
3229 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
3230 			     &prom_tce_alloc_start,
3231 			     sizeof(prom_tce_alloc_start));
3232 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
3233 			     &prom_tce_alloc_end,
3234 			     sizeof(prom_tce_alloc_end));
3235 	}
3236 #endif
3237 
3238 	/*
3239 	 * Fixup any known bugs in the device-tree
3240 	 */
3241 	fixup_device_tree();
3242 
3243 	/*
3244 	 * Now finally create the flattened device-tree
3245 	 */
3246 	prom_printf("copying OF device tree...\n");
3247 	flatten_device_tree();
3248 
3249 	/*
3250 	 * in case stdin is USB and still active on IBM machines...
3251 	 * Unfortunately quiesce crashes on some powermacs if we have
3252 	 * closed stdin already (in particular the powerbook 101). It
3253 	 * appears that the OPAL version of OFW doesn't like it either.
3254 	 */
3255 	if (of_platform != PLATFORM_POWERMAC &&
3256 	    of_platform != PLATFORM_OPAL)
3257 		prom_close_stdin();
3258 
3259 	/*
3260 	 * Call OF "quiesce" method to shut down pending DMA's from
3261 	 * devices etc...
3262 	 */
3263 	prom_printf("Quiescing Open Firmware ...\n");
3264 	call_prom("quiesce", 0, 0);
3265 
3266 	/*
3267 	 * And finally, call the kernel passing it the flattened device
3268 	 * tree and NULL as r5, thus triggering the new entry point which
3269 	 * is common to us and kexec
3270 	 */
3271 	hdr = dt_header_start;
3272 
3273 	/* Don't print anything after quiesce under OPAL, it crashes OFW */
3274 	if (of_platform != PLATFORM_OPAL) {
3275 		prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
3276 		prom_debug("->dt_header_start=0x%x\n", hdr);
3277 	}
3278 
3279 #ifdef CONFIG_PPC32
3280 	reloc_got2(-offset);
3281 #else
3282 	unreloc_toc();
3283 #endif
3284 
3285 #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
3286 	/* OPAL early debug gets the OPAL base & entry in r8 and r9 */
3287 	__start(hdr, kbase, 0, 0, 0,
3288 		prom_opal_base, prom_opal_entry);
3289 #else
3290 	__start(hdr, kbase, 0, 0, 0, 0, 0);
3291 #endif
3292 
3293 	return 0;
3294 }
3295