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