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