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