xref: /linux/arch/powerpc/kernel/prom_init.c (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
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 	/*
1366 	 * First copy the architecture vec template
1367 	 *
1368 	 * use memcpy() instead of *vec = *vec_template so that GCC replaces it
1369 	 * by __memcpy() when KASAN is active
1370 	 */
1371 	memcpy(&ibm_architecture_vec, &ibm_architecture_vec_template,
1372 	       sizeof(ibm_architecture_vec));
1373 
1374 	prom_strscpy_pad(ibm_architecture_vec.vec7.os_id, linux_banner, 256);
1375 
1376 	if (prop_len > 1) {
1377 		int i;
1378 		u8 vec[8];
1379 		prom_debug("Found ibm,arch-vec-5-platform-support, len: %d\n",
1380 			   prop_len);
1381 		if (prop_len > sizeof(vec))
1382 			prom_printf("WARNING: ibm,arch-vec-5-platform-support longer than expected (len: %d)\n",
1383 				    prop_len);
1384 		prom_getprop(prom.chosen, "ibm,arch-vec-5-platform-support", &vec, sizeof(vec));
1385 		for (i = 0; i < prop_len; i += 2) {
1386 			prom_debug("%d: index = 0x%x val = 0x%x\n", i / 2, vec[i], vec[i + 1]);
1387 			prom_parse_platform_support(vec[i], vec[i + 1], &supported);
1388 		}
1389 	}
1390 
1391 	if (supported.radix_mmu && IS_ENABLED(CONFIG_PPC_RADIX_MMU)) {
1392 		/* Radix preferred - Check if GTSE is also supported */
1393 		prom_debug("Asking for radix\n");
1394 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_RADIX);
1395 		if (supported.radix_gtse)
1396 			ibm_architecture_vec.vec5.radix_ext =
1397 					OV5_FEAT(OV5_RADIX_GTSE);
1398 		else
1399 			prom_debug("Radix GTSE isn't supported\n");
1400 	} else if (supported.hash_mmu) {
1401 		/* Default to hash mmu (if we can) */
1402 		prom_debug("Asking for hash\n");
1403 		ibm_architecture_vec.vec5.mmu = OV5_FEAT(OV5_MMU_HASH);
1404 	} else {
1405 		/* We're probably on a legacy hypervisor */
1406 		prom_debug("Assuming legacy hash support\n");
1407 	}
1408 
1409 	if (supported.xive) {
1410 		prom_debug("Asking for XIVE\n");
1411 		ibm_architecture_vec.vec5.intarch = OV5_FEAT(OV5_XIVE_EXPLOIT);
1412 	}
1413 }
1414 
1415 static void __init prom_send_capabilities(void)
1416 {
1417 	ihandle root;
1418 	prom_arg_t ret;
1419 	u32 cores;
1420 	int start_index = 0;
1421 
1422 	/*
1423 	 * Ensure that when running on Power11 or below hardware, the number
1424 	 * of PVR entries passed during CAS negotiation does not exceed the
1425 	 * firmware-imposed limit of 16.
1426 	 *
1427 	 * Compute the start_index to skip the oldest leading pvrs[] entries
1428 	 * when running on Power11 or below hardware, so that the pointer
1429 	 * passed to ibm,client-architecture-support points to
1430 	 * ibm_architecture_vec.pvrs[start_index], presenting exactly 16
1431 	 * entries to firmware.
1432 	 */
1433 	if ((ARRAY_SIZE(ibm_architecture_vec_template.pvrs) > CAS_MAX_PVR_ENTRIES) &&
1434 	    (PVR_VER(mfspr(SPRN_PVR)) <= PVR_POWER11))
1435 		start_index = ARRAY_SIZE(ibm_architecture_vec_template.pvrs) - CAS_MAX_PVR_ENTRIES;
1436 
1437 	/* Check ibm,arch-vec-5-platform-support and fixup vec5 if required */
1438 	prom_check_platform_support();
1439 
1440 	root = call_prom("open", 1, 1, ADDR("/"));
1441 	if (root != 0) {
1442 		/* We need to tell the FW about the number of cores we support.
1443 		 *
1444 		 * To do that, we count the number of threads on the first core
1445 		 * (we assume this is the same for all cores) and use it to
1446 		 * divide NR_CPUS.
1447 		 */
1448 
1449 		cores = DIV_ROUND_UP(NR_CPUS, prom_count_smt_threads());
1450 		prom_printf("Max number of cores passed to firmware: %u (NR_CPUS = %d)\n",
1451 			    cores, NR_CPUS);
1452 
1453 		ibm_architecture_vec.vec5.max_cpus = cpu_to_be32(cores);
1454 
1455 		/* try calling the ibm,client-architecture-support method */
1456 		prom_printf("Calling ibm,client-architecture-support...");
1457 		if (call_prom_ret("call-method", 3, 2, &ret,
1458 				  ADDR("ibm,client-architecture-support"),
1459 				  root,
1460 				  ADDR(&ibm_architecture_vec.pvrs[start_index])) == 0) {
1461 			/* the call exists... */
1462 			if (ret)
1463 				prom_printf("\nWARNING: ibm,client-architecture"
1464 					    "-support call FAILED!\n");
1465 			call_prom("close", 1, 0, root);
1466 			prom_printf(" done\n");
1467 			return;
1468 		}
1469 		call_prom("close", 1, 0, root);
1470 		prom_printf(" not implemented\n");
1471 	}
1472 
1473 #ifdef __BIG_ENDIAN__
1474 	{
1475 		ihandle elfloader;
1476 
1477 		/* no ibm,client-architecture-support call, try the old way */
1478 		elfloader = call_prom("open", 1, 1,
1479 				      ADDR("/packages/elf-loader"));
1480 		if (elfloader == 0) {
1481 			prom_printf("couldn't open /packages/elf-loader\n");
1482 			return;
1483 		}
1484 		call_prom("call-method", 3, 1, ADDR("process-elf-header"),
1485 			  elfloader, ADDR(&fake_elf));
1486 		call_prom("close", 1, 0, elfloader);
1487 	}
1488 #endif /* __BIG_ENDIAN__ */
1489 }
1490 #endif /* CONFIG_PPC_PSERIES */
1491 
1492 /*
1493  * Memory allocation strategy... our layout is normally:
1494  *
1495  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
1496  *  rare cases, initrd might end up being before the kernel though.
1497  *  We assume this won't override the final kernel at 0, we have no
1498  *  provision to handle that in this version, but it should hopefully
1499  *  never happen.
1500  *
1501  *  alloc_top is set to the top of RMO, eventually shrink down if the
1502  *  TCEs overlap
1503  *
1504  *  alloc_bottom is set to the top of kernel/initrd
1505  *
1506  *  from there, allocations are done this way : rtas is allocated
1507  *  topmost, and the device-tree is allocated from the bottom. We try
1508  *  to grow the device-tree allocation as we progress. If we can't,
1509  *  then we fail, we don't currently have a facility to restart
1510  *  elsewhere, but that shouldn't be necessary.
1511  *
1512  *  Note that calls to reserve_mem have to be done explicitly, memory
1513  *  allocated with either alloc_up or alloc_down isn't automatically
1514  *  reserved.
1515  */
1516 
1517 
1518 /*
1519  * Allocates memory in the RMO upward from the kernel/initrd
1520  *
1521  * When align is 0, this is a special case, it means to allocate in place
1522  * at the current location of alloc_bottom or fail (that is basically
1523  * extending the previous allocation). Used for the device-tree flattening
1524  */
1525 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
1526 {
1527 	unsigned long base = alloc_bottom;
1528 	unsigned long addr = 0;
1529 
1530 	if (align)
1531 		base = ALIGN(base, align);
1532 	prom_debug("%s(%lx, %lx)\n", __func__, size, align);
1533 	if (ram_top == 0)
1534 		prom_panic("alloc_up() called with mem not initialized\n");
1535 
1536 	if (align)
1537 		base = ALIGN(alloc_bottom, align);
1538 	else
1539 		base = alloc_bottom;
1540 
1541 	for(; (base + size) <= alloc_top;
1542 	    base = ALIGN(base + 0x100000, align)) {
1543 		prom_debug("    trying: 0x%lx\n\r", base);
1544 		addr = (unsigned long)prom_claim(base, size, 0);
1545 		if (addr != PROM_ERROR && addr != 0)
1546 			break;
1547 		addr = 0;
1548 		if (align == 0)
1549 			break;
1550 	}
1551 	if (addr == 0)
1552 		return 0;
1553 	alloc_bottom = addr + size;
1554 
1555 	prom_debug(" -> %lx\n", addr);
1556 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1557 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1558 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1559 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1560 	prom_debug("  ram_top      : %lx\n", ram_top);
1561 
1562 	return addr;
1563 }
1564 
1565 /*
1566  * Allocates memory downward, either from top of RMO, or if highmem
1567  * is set, from the top of RAM.  Note that this one doesn't handle
1568  * failures.  It does claim memory if highmem is not set.
1569  */
1570 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
1571 				       int highmem)
1572 {
1573 	unsigned long base, addr = 0;
1574 
1575 	prom_debug("%s(%lx, %lx, %s)\n", __func__, size, align,
1576 		   highmem ? "(high)" : "(low)");
1577 	if (ram_top == 0)
1578 		prom_panic("alloc_down() called with mem not initialized\n");
1579 
1580 	if (highmem) {
1581 		/* Carve out storage for the TCE table. */
1582 		addr = ALIGN_DOWN(alloc_top_high - size, align);
1583 		if (addr <= alloc_bottom)
1584 			return 0;
1585 		/* Will we bump into the RMO ? If yes, check out that we
1586 		 * didn't overlap existing allocations there, if we did,
1587 		 * we are dead, we must be the first in town !
1588 		 */
1589 		if (addr < rmo_top) {
1590 			/* Good, we are first */
1591 			if (alloc_top == rmo_top)
1592 				alloc_top = rmo_top = addr;
1593 			else
1594 				return 0;
1595 		}
1596 		alloc_top_high = addr;
1597 		goto bail;
1598 	}
1599 
1600 	base = ALIGN_DOWN(alloc_top - size, align);
1601 	for (; base > alloc_bottom;
1602 	     base = ALIGN_DOWN(base - 0x100000, align))  {
1603 		prom_debug("    trying: 0x%lx\n\r", base);
1604 		addr = (unsigned long)prom_claim(base, size, 0);
1605 		if (addr != PROM_ERROR && addr != 0)
1606 			break;
1607 		addr = 0;
1608 	}
1609 	if (addr == 0)
1610 		return 0;
1611 	alloc_top = addr;
1612 
1613  bail:
1614 	prom_debug(" -> %lx\n", addr);
1615 	prom_debug("  alloc_bottom : %lx\n", alloc_bottom);
1616 	prom_debug("  alloc_top    : %lx\n", alloc_top);
1617 	prom_debug("  alloc_top_hi : %lx\n", alloc_top_high);
1618 	prom_debug("  rmo_top      : %lx\n", rmo_top);
1619 	prom_debug("  ram_top      : %lx\n", ram_top);
1620 
1621 	return addr;
1622 }
1623 
1624 /*
1625  * Parse a "reg" cell
1626  */
1627 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
1628 {
1629 	cell_t *p = *cellp;
1630 	unsigned long r = 0;
1631 
1632 	/* Ignore more than 2 cells */
1633 	while (s > sizeof(unsigned long) / 4) {
1634 		p++;
1635 		s--;
1636 	}
1637 	r = be32_to_cpu(*p++);
1638 #ifdef CONFIG_PPC64
1639 	if (s > 1) {
1640 		r <<= 32;
1641 		r |= be32_to_cpu(*(p++));
1642 	}
1643 #endif
1644 	*cellp = p;
1645 	return r;
1646 }
1647 
1648 /*
1649  * Very dumb function for adding to the memory reserve list, but
1650  * we don't need anything smarter at this point
1651  *
1652  * XXX Eventually check for collisions.  They should NEVER happen.
1653  * If problems seem to show up, it would be a good start to track
1654  * them down.
1655  */
1656 static void __init reserve_mem(u64 base, u64 size)
1657 {
1658 	u64 top = base + size;
1659 	unsigned long cnt = mem_reserve_cnt;
1660 
1661 	if (size == 0)
1662 		return;
1663 
1664 	/* We need to always keep one empty entry so that we
1665 	 * have our terminator with "size" set to 0 since we are
1666 	 * dumb and just copy this entire array to the boot params
1667 	 */
1668 	base = ALIGN_DOWN(base, PAGE_SIZE);
1669 	top = ALIGN(top, PAGE_SIZE);
1670 	size = top - base;
1671 
1672 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
1673 		prom_panic("Memory reserve map exhausted !\n");
1674 	mem_reserve_map[cnt].base = cpu_to_be64(base);
1675 	mem_reserve_map[cnt].size = cpu_to_be64(size);
1676 	mem_reserve_cnt = cnt + 1;
1677 }
1678 
1679 /*
1680  * Initialize memory allocation mechanism, parse "memory" nodes and
1681  * obtain that way the top of memory and RMO to setup out local allocator
1682  */
1683 static void __init prom_init_mem(void)
1684 {
1685 	phandle node;
1686 	char type[64];
1687 	unsigned int plen;
1688 	cell_t *p, *endp;
1689 	__be32 val;
1690 	u32 rac, rsc;
1691 
1692 	/*
1693 	 * We iterate the memory nodes to find
1694 	 * 1) top of RMO (first node)
1695 	 * 2) top of memory
1696 	 */
1697 	val = cpu_to_be32(2);
1698 	prom_getprop(prom.root, "#address-cells", &val, sizeof(val));
1699 	rac = be32_to_cpu(val);
1700 	val = cpu_to_be32(1);
1701 	prom_getprop(prom.root, "#size-cells", &val, sizeof(rsc));
1702 	rsc = be32_to_cpu(val);
1703 	prom_debug("root_addr_cells: %x\n", rac);
1704 	prom_debug("root_size_cells: %x\n", rsc);
1705 
1706 	prom_debug("scanning memory:\n");
1707 
1708 	for (node = 0; prom_next_node(&node); ) {
1709 		type[0] = 0;
1710 		prom_getprop(node, "device_type", type, sizeof(type));
1711 
1712 		if (type[0] == 0) {
1713 			/*
1714 			 * CHRP Longtrail machines have no device_type
1715 			 * on the memory node, so check the name instead...
1716 			 */
1717 			prom_getprop(node, "name", type, sizeof(type));
1718 		}
1719 		if (prom_strcmp(type, "memory"))
1720 			continue;
1721 
1722 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
1723 		if (plen > sizeof(regbuf)) {
1724 			prom_printf("memory node too large for buffer !\n");
1725 			plen = sizeof(regbuf);
1726 		}
1727 		p = regbuf;
1728 		endp = p + (plen / sizeof(cell_t));
1729 
1730 #ifdef DEBUG_PROM
1731 		memset(prom_scratch, 0, sizeof(prom_scratch));
1732 		call_prom("package-to-path", 3, 1, node, prom_scratch,
1733 			  sizeof(prom_scratch) - 1);
1734 		prom_debug("  node %s :\n", prom_scratch);
1735 #endif /* DEBUG_PROM */
1736 
1737 		while ((endp - p) >= (rac + rsc)) {
1738 			unsigned long base, size;
1739 
1740 			base = prom_next_cell(rac, &p);
1741 			size = prom_next_cell(rsc, &p);
1742 
1743 			if (size == 0)
1744 				continue;
1745 			prom_debug("    %lx %lx\n", base, size);
1746 			if (base == 0 && (of_platform & PLATFORM_LPAR))
1747 				rmo_top = size;
1748 			if ((base + size) > ram_top)
1749 				ram_top = base + size;
1750 		}
1751 	}
1752 
1753 	alloc_bottom = PAGE_ALIGN((unsigned long)&_end + 0x4000);
1754 
1755 	/*
1756 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1757 	 * alloc_top_high. This must be the real top of RAM so we can put
1758 	 * TCE's up there.
1759 	 */
1760 
1761 	alloc_top_high = ram_top;
1762 
1763 	if (prom_memory_limit) {
1764 		if (prom_memory_limit <= alloc_bottom) {
1765 			prom_printf("Ignoring mem=%lx <= alloc_bottom.\n",
1766 				    prom_memory_limit);
1767 			prom_memory_limit = 0;
1768 		} else if (prom_memory_limit >= ram_top) {
1769 			prom_printf("Ignoring mem=%lx >= ram_top.\n",
1770 				    prom_memory_limit);
1771 			prom_memory_limit = 0;
1772 		} else {
1773 			ram_top = prom_memory_limit;
1774 			rmo_top = min(rmo_top, prom_memory_limit);
1775 		}
1776 	}
1777 
1778 	/*
1779 	 * Setup our top alloc point, that is top of RMO or top of
1780 	 * segment 0 when running non-LPAR.
1781 	 * Some RS64 machines have buggy firmware where claims up at
1782 	 * 1GB fail.  Cap at 768MB as a workaround.
1783 	 * Since 768MB is plenty of room, and we need to cap to something
1784 	 * reasonable on 32-bit, cap at 768MB on all machines.
1785 	 */
1786 	if (!rmo_top)
1787 		rmo_top = ram_top;
1788 	rmo_top = min(0x30000000ul, rmo_top);
1789 	alloc_top = rmo_top;
1790 	alloc_top_high = ram_top;
1791 
1792 	/*
1793 	 * Check if we have an initrd after the kernel but still inside
1794 	 * the RMO.  If we do move our bottom point to after it.
1795 	 */
1796 	if (prom_initrd_start &&
1797 	    prom_initrd_start < rmo_top &&
1798 	    prom_initrd_end > alloc_bottom)
1799 		alloc_bottom = PAGE_ALIGN(prom_initrd_end);
1800 
1801 	prom_printf("memory layout at init:\n");
1802 	prom_printf("  memory_limit : %lx (16 MB aligned)\n",
1803 		    prom_memory_limit);
1804 	prom_printf("  alloc_bottom : %lx\n", alloc_bottom);
1805 	prom_printf("  alloc_top    : %lx\n", alloc_top);
1806 	prom_printf("  alloc_top_hi : %lx\n", alloc_top_high);
1807 	prom_printf("  rmo_top      : %lx\n", rmo_top);
1808 	prom_printf("  ram_top      : %lx\n", ram_top);
1809 }
1810 
1811 static void __init prom_close_stdin(void)
1812 {
1813 	__be32 val;
1814 	ihandle stdin;
1815 
1816 	if (prom_getprop(prom.chosen, "stdin", &val, sizeof(val)) > 0) {
1817 		stdin = be32_to_cpu(val);
1818 		call_prom("close", 1, 0, stdin);
1819 	}
1820 }
1821 
1822 #ifdef CONFIG_PPC_SVM
1823 static int __init prom_rtas_hcall(uint64_t args)
1824 {
1825 	register uint64_t arg1 asm("r3") = H_RTAS;
1826 	register uint64_t arg2 asm("r4") = args;
1827 
1828 	asm volatile("sc 1\n" : "=r" (arg1) :
1829 			"r" (arg1),
1830 			"r" (arg2) :);
1831 	srr_regs_clobbered();
1832 
1833 	return arg1;
1834 }
1835 
1836 static struct rtas_args __prombss os_term_args;
1837 
1838 static void __init prom_rtas_os_term(char *str)
1839 {
1840 	phandle rtas_node;
1841 	__be32 val;
1842 	u32 token;
1843 
1844 	prom_debug("%s: start...\n", __func__);
1845 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1846 	prom_debug("rtas_node: %x\n", rtas_node);
1847 	if (!PHANDLE_VALID(rtas_node))
1848 		return;
1849 
1850 	val = 0;
1851 	prom_getprop(rtas_node, "ibm,os-term", &val, sizeof(val));
1852 	token = be32_to_cpu(val);
1853 	prom_debug("ibm,os-term: %x\n", token);
1854 	if (token == 0)
1855 		prom_panic("Could not get token for ibm,os-term\n");
1856 	os_term_args.token = cpu_to_be32(token);
1857 	os_term_args.nargs = cpu_to_be32(1);
1858 	os_term_args.nret = cpu_to_be32(1);
1859 	os_term_args.args[0] = cpu_to_be32(__pa(str));
1860 	prom_rtas_hcall((uint64_t)&os_term_args);
1861 }
1862 #endif /* CONFIG_PPC_SVM */
1863 
1864 /*
1865  * Allocate room for and instantiate RTAS
1866  */
1867 static void __init prom_instantiate_rtas(void)
1868 {
1869 	phandle rtas_node;
1870 	ihandle rtas_inst;
1871 	u32 base, entry = 0;
1872 	__be32 val;
1873 	u32 size = 0;
1874 
1875 	prom_debug("prom_instantiate_rtas: start...\n");
1876 
1877 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1878 	prom_debug("rtas_node: %x\n", rtas_node);
1879 	if (!PHANDLE_VALID(rtas_node))
1880 		return;
1881 
1882 	val = 0;
1883 	prom_getprop(rtas_node, "rtas-size", &val, sizeof(size));
1884 	size = be32_to_cpu(val);
1885 	if (size == 0)
1886 		return;
1887 
1888 	base = alloc_down(size, PAGE_SIZE, 0);
1889 	if (base == 0)
1890 		prom_panic("Could not allocate memory for RTAS\n");
1891 
1892 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1893 	if (!IHANDLE_VALID(rtas_inst)) {
1894 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
1895 		return;
1896 	}
1897 
1898 	prom_printf("instantiating rtas at 0x%x...", base);
1899 
1900 	if (call_prom_ret("call-method", 3, 2, &entry,
1901 			  ADDR("instantiate-rtas"),
1902 			  rtas_inst, base) != 0
1903 	    || entry == 0) {
1904 		prom_printf(" failed\n");
1905 		return;
1906 	}
1907 	prom_printf(" done\n");
1908 
1909 	reserve_mem(base, size);
1910 
1911 	val = cpu_to_be32(base);
1912 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1913 		     &val, sizeof(val));
1914 	val = cpu_to_be32(entry);
1915 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1916 		     &val, sizeof(val));
1917 
1918 	/* Check if it supports "query-cpu-stopped-state" */
1919 	if (prom_getprop(rtas_node, "query-cpu-stopped-state",
1920 			 &val, sizeof(val)) != PROM_ERROR)
1921 		rtas_has_query_cpu_stopped = true;
1922 
1923 	prom_debug("rtas base     = 0x%x\n", base);
1924 	prom_debug("rtas entry    = 0x%x\n", entry);
1925 	prom_debug("rtas size     = 0x%x\n", size);
1926 
1927 	prom_debug("prom_instantiate_rtas: end...\n");
1928 }
1929 
1930 #ifdef CONFIG_PPC64
1931 /*
1932  * Allocate room for and instantiate Stored Measurement Log (SML)
1933  */
1934 static void __init prom_instantiate_sml(void)
1935 {
1936 	phandle ibmvtpm_node;
1937 	ihandle ibmvtpm_inst;
1938 	u32 entry = 0, size = 0, succ = 0;
1939 	u64 base;
1940 	__be32 val;
1941 
1942 	prom_debug("prom_instantiate_sml: start...\n");
1943 
1944 	ibmvtpm_node = call_prom("finddevice", 1, 1, ADDR("/vdevice/vtpm"));
1945 	prom_debug("ibmvtpm_node: %x\n", ibmvtpm_node);
1946 	if (!PHANDLE_VALID(ibmvtpm_node))
1947 		return;
1948 
1949 	ibmvtpm_inst = call_prom("open", 1, 1, ADDR("/vdevice/vtpm"));
1950 	if (!IHANDLE_VALID(ibmvtpm_inst)) {
1951 		prom_printf("opening vtpm package failed (%x)\n", ibmvtpm_inst);
1952 		return;
1953 	}
1954 
1955 	if (prom_getprop(ibmvtpm_node, "ibm,sml-efi-reformat-supported",
1956 			 &val, sizeof(val)) != PROM_ERROR) {
1957 		if (call_prom_ret("call-method", 2, 2, &succ,
1958 				  ADDR("reformat-sml-to-efi-alignment"),
1959 				  ibmvtpm_inst) != 0 || succ == 0) {
1960 			prom_printf("Reformat SML to EFI alignment failed\n");
1961 			return;
1962 		}
1963 
1964 		if (call_prom_ret("call-method", 2, 2, &size,
1965 				  ADDR("sml-get-allocated-size"),
1966 				  ibmvtpm_inst) != 0 || size == 0) {
1967 			prom_printf("SML get allocated size failed\n");
1968 			return;
1969 		}
1970 	} else {
1971 		if (call_prom_ret("call-method", 2, 2, &size,
1972 				  ADDR("sml-get-handover-size"),
1973 				  ibmvtpm_inst) != 0 || size == 0) {
1974 			prom_printf("SML get handover size failed\n");
1975 			return;
1976 		}
1977 	}
1978 
1979 	base = alloc_down(size, PAGE_SIZE, 0);
1980 	if (base == 0)
1981 		prom_panic("Could not allocate memory for sml\n");
1982 
1983 	prom_printf("instantiating sml at 0x%llx...", base);
1984 
1985 	memset((void *)base, 0, size);
1986 
1987 	if (call_prom_ret("call-method", 4, 2, &entry,
1988 			  ADDR("sml-handover"),
1989 			  ibmvtpm_inst, size, base) != 0 || entry == 0) {
1990 		prom_printf("SML handover failed\n");
1991 		return;
1992 	}
1993 	prom_printf(" done\n");
1994 
1995 	reserve_mem(base, size);
1996 
1997 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-base",
1998 		     &base, sizeof(base));
1999 	prom_setprop(ibmvtpm_node, "/vdevice/vtpm", "linux,sml-size",
2000 		     &size, sizeof(size));
2001 
2002 	prom_debug("sml base     = 0x%llx\n", base);
2003 	prom_debug("sml size     = 0x%x\n", size);
2004 
2005 	prom_debug("prom_instantiate_sml: end...\n");
2006 }
2007 
2008 /*
2009  * Allocate room for and initialize TCE tables
2010  */
2011 #ifdef __BIG_ENDIAN__
2012 static void __init prom_initialize_tce_table(void)
2013 {
2014 	phandle node;
2015 	ihandle phb_node;
2016 	char compatible[64], type[64], model[64];
2017 	char *path = prom_scratch;
2018 	u64 base, align;
2019 	u32 minalign, minsize;
2020 	u64 tce_entry, *tce_entryp;
2021 	u64 local_alloc_top, local_alloc_bottom;
2022 	u64 i;
2023 
2024 	if (prom_iommu_off)
2025 		return;
2026 
2027 	prom_debug("starting prom_initialize_tce_table\n");
2028 
2029 	/* Cache current top of allocs so we reserve a single block */
2030 	local_alloc_top = alloc_top_high;
2031 	local_alloc_bottom = local_alloc_top;
2032 
2033 	/* Search all nodes looking for PHBs. */
2034 	for (node = 0; prom_next_node(&node); ) {
2035 		compatible[0] = 0;
2036 		type[0] = 0;
2037 		model[0] = 0;
2038 		prom_getprop(node, "compatible",
2039 			     compatible, sizeof(compatible));
2040 		prom_getprop(node, "device_type", type, sizeof(type));
2041 		prom_getprop(node, "model", model, sizeof(model));
2042 
2043 		if ((type[0] == 0) || (prom_strstr(type, "pci") == NULL))
2044 			continue;
2045 
2046 		/* Keep the old logic intact to avoid regression. */
2047 		if (compatible[0] != 0) {
2048 			if ((prom_strstr(compatible, "python") == NULL) &&
2049 			    (prom_strstr(compatible, "Speedwagon") == NULL) &&
2050 			    (prom_strstr(compatible, "Winnipeg") == NULL))
2051 				continue;
2052 		} else if (model[0] != 0) {
2053 			if ((prom_strstr(model, "ython") == NULL) &&
2054 			    (prom_strstr(model, "peedwagon") == NULL) &&
2055 			    (prom_strstr(model, "innipeg") == NULL))
2056 				continue;
2057 		}
2058 
2059 		if (prom_getprop(node, "tce-table-minalign", &minalign,
2060 				 sizeof(minalign)) == PROM_ERROR)
2061 			minalign = 0;
2062 		if (prom_getprop(node, "tce-table-minsize", &minsize,
2063 				 sizeof(minsize)) == PROM_ERROR)
2064 			minsize = 4UL << 20;
2065 
2066 		/*
2067 		 * Even though we read what OF wants, we just set the table
2068 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
2069 		 * By doing this, we avoid the pitfalls of trying to DMA to
2070 		 * MMIO space and the DMA alias hole.
2071 		 */
2072 		minsize = 4UL << 20;
2073 
2074 		/* Align to the greater of the align or size */
2075 		align = max(minalign, minsize);
2076 		base = alloc_down(minsize, align, 1);
2077 		if (base == 0)
2078 			prom_panic("ERROR, cannot find space for TCE table.\n");
2079 		if (base < local_alloc_bottom)
2080 			local_alloc_bottom = base;
2081 
2082 		/* It seems OF doesn't null-terminate the path :-( */
2083 		memset(path, 0, sizeof(prom_scratch));
2084 		/* Call OF to setup the TCE hardware */
2085 		if (call_prom("package-to-path", 3, 1, node,
2086 			      path, sizeof(prom_scratch) - 1) == PROM_ERROR) {
2087 			prom_printf("package-to-path failed\n");
2088 		}
2089 
2090 		/* Save away the TCE table attributes for later use. */
2091 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
2092 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
2093 
2094 		prom_debug("TCE table: %s\n", path);
2095 		prom_debug("\tnode = 0x%x\n", node);
2096 		prom_debug("\tbase = 0x%llx\n", base);
2097 		prom_debug("\tsize = 0x%x\n", minsize);
2098 
2099 		/* Initialize the table to have a one-to-one mapping
2100 		 * over the allocated size.
2101 		 */
2102 		tce_entryp = (u64 *)base;
2103 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
2104 			tce_entry = (i << PAGE_SHIFT);
2105 			tce_entry |= 0x3;
2106 			*tce_entryp = tce_entry;
2107 		}
2108 
2109 		prom_printf("opening PHB %s", path);
2110 		phb_node = call_prom("open", 1, 1, path);
2111 		if (phb_node == 0)
2112 			prom_printf("... failed\n");
2113 		else
2114 			prom_printf("... done\n");
2115 
2116 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
2117 			  phb_node, -1, minsize,
2118 			  (u32) base, (u32) (base >> 32));
2119 		call_prom("close", 1, 0, phb_node);
2120 	}
2121 
2122 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
2123 
2124 	/* These are only really needed if there is a memory limit in
2125 	 * effect, but we don't know so export them always. */
2126 	prom_tce_alloc_start = local_alloc_bottom;
2127 	prom_tce_alloc_end = local_alloc_top;
2128 
2129 	/* Flag the first invalid entry */
2130 	prom_debug("ending prom_initialize_tce_table\n");
2131 }
2132 #endif /* __BIG_ENDIAN__ */
2133 #endif /* CONFIG_PPC64 */
2134 
2135 /*
2136  * With CHRP SMP we need to use the OF to start the other processors.
2137  * We can't wait until smp_boot_cpus (the OF is trashed by then)
2138  * so we have to put the processors into a holding pattern controlled
2139  * by the kernel (not OF) before we destroy the OF.
2140  *
2141  * This uses a chunk of low memory, puts some holding pattern
2142  * code there and sends the other processors off to there until
2143  * smp_boot_cpus tells them to do something.  The holding pattern
2144  * checks that address until its cpu # is there, when it is that
2145  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
2146  * of setting those values.
2147  *
2148  * We also use physical address 0x4 here to tell when a cpu
2149  * is in its holding pattern code.
2150  *
2151  * -- Cort
2152  */
2153 /*
2154  * We want to reference the copy of __secondary_hold_* in the
2155  * 0 - 0x100 address range
2156  */
2157 #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
2158 
2159 static void __init prom_hold_cpus(void)
2160 {
2161 	unsigned long i;
2162 	phandle node;
2163 	char type[64];
2164 	unsigned long *spinloop
2165 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
2166 	unsigned long *acknowledge
2167 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
2168 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
2169 
2170 	/*
2171 	 * On pseries, if RTAS supports "query-cpu-stopped-state",
2172 	 * we skip this stage, the CPUs will be started by the
2173 	 * kernel using RTAS.
2174 	 */
2175 	if ((of_platform == PLATFORM_PSERIES ||
2176 	     of_platform == PLATFORM_PSERIES_LPAR) &&
2177 	    rtas_has_query_cpu_stopped) {
2178 		prom_printf("prom_hold_cpus: skipped\n");
2179 		return;
2180 	}
2181 
2182 	prom_debug("prom_hold_cpus: start...\n");
2183 	prom_debug("    1) spinloop       = 0x%lx\n", (unsigned long)spinloop);
2184 	prom_debug("    1) *spinloop      = 0x%lx\n", *spinloop);
2185 	prom_debug("    1) acknowledge    = 0x%lx\n",
2186 		   (unsigned long)acknowledge);
2187 	prom_debug("    1) *acknowledge   = 0x%lx\n", *acknowledge);
2188 	prom_debug("    1) secondary_hold = 0x%lx\n", secondary_hold);
2189 
2190 	/* Set the common spinloop variable, so all of the secondary cpus
2191 	 * will block when they are awakened from their OF spinloop.
2192 	 * This must occur for both SMP and non SMP kernels, since OF will
2193 	 * be trashed when we move the kernel.
2194 	 */
2195 	*spinloop = 0;
2196 
2197 	/* look for cpus */
2198 	for (node = 0; prom_next_node(&node); ) {
2199 		unsigned int cpu_no;
2200 		__be32 reg;
2201 
2202 		type[0] = 0;
2203 		prom_getprop(node, "device_type", type, sizeof(type));
2204 		if (prom_strcmp(type, "cpu") != 0)
2205 			continue;
2206 
2207 		/* Skip non-configured cpus. */
2208 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
2209 			if (prom_strcmp(type, "okay") != 0)
2210 				continue;
2211 
2212 		reg = cpu_to_be32(-1); /* make sparse happy */
2213 		prom_getprop(node, "reg", &reg, sizeof(reg));
2214 		cpu_no = be32_to_cpu(reg);
2215 
2216 		prom_debug("cpu hw idx   = %u\n", cpu_no);
2217 
2218 		/* Init the acknowledge var which will be reset by
2219 		 * the secondary cpu when it awakens from its OF
2220 		 * spinloop.
2221 		 */
2222 		*acknowledge = (unsigned long)-1;
2223 
2224 		if (cpu_no != prom.cpu) {
2225 			/* Primary Thread of non-boot cpu or any thread */
2226 			prom_printf("starting cpu hw idx %u... ", cpu_no);
2227 			call_prom("start-cpu", 3, 0, node,
2228 				  secondary_hold, cpu_no);
2229 
2230 			for (i = 0; (i < 100000000) &&
2231 			     (*acknowledge == ((unsigned long)-1)); i++ )
2232 				mb();
2233 
2234 			if (*acknowledge == cpu_no)
2235 				prom_printf("done\n");
2236 			else
2237 				prom_printf("failed: %lx\n", *acknowledge);
2238 		}
2239 #ifdef CONFIG_SMP
2240 		else
2241 			prom_printf("boot cpu hw idx %u\n", cpu_no);
2242 #endif /* CONFIG_SMP */
2243 	}
2244 
2245 	prom_debug("prom_hold_cpus: end...\n");
2246 }
2247 
2248 
2249 static void __init prom_init_client_services(unsigned long pp)
2250 {
2251 	/* Get a handle to the prom entry point before anything else */
2252 	prom_entry = pp;
2253 
2254 	/* get a handle for the stdout device */
2255 	prom.chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
2256 	if (!PHANDLE_VALID(prom.chosen))
2257 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
2258 
2259 	/* get device tree root */
2260 	prom.root = call_prom("finddevice", 1, 1, ADDR("/"));
2261 	if (!PHANDLE_VALID(prom.root))
2262 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
2263 
2264 	prom.mmumap = 0;
2265 }
2266 
2267 #ifdef CONFIG_PPC32
2268 /*
2269  * For really old powermacs, we need to map things we claim.
2270  * For that, we need the ihandle of the mmu.
2271  * Also, on the longtrail, we need to work around other bugs.
2272  */
2273 static void __init prom_find_mmu(void)
2274 {
2275 	phandle oprom;
2276 	char version[64];
2277 
2278 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
2279 	if (!PHANDLE_VALID(oprom))
2280 		return;
2281 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
2282 		return;
2283 	version[sizeof(version) - 1] = 0;
2284 	/* XXX might need to add other versions here */
2285 	if (prom_strcmp(version, "Open Firmware, 1.0.5") == 0)
2286 		of_workarounds = OF_WA_CLAIM;
2287 	else if (prom_strncmp(version, "FirmWorks,3.", 12) == 0) {
2288 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
2289 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
2290 	} else
2291 		return;
2292 	prom.memory = call_prom("open", 1, 1, ADDR("/memory"));
2293 	prom_getprop(prom.chosen, "mmu", &prom.mmumap,
2294 		     sizeof(prom.mmumap));
2295 	prom.mmumap = be32_to_cpu(prom.mmumap);
2296 	if (!IHANDLE_VALID(prom.memory) || !IHANDLE_VALID(prom.mmumap))
2297 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
2298 }
2299 #else
2300 #define prom_find_mmu()
2301 #endif
2302 
2303 static void __init prom_init_stdout(void)
2304 {
2305 	char *path = of_stdout_device;
2306 	char type[16];
2307 	phandle stdout_node;
2308 	__be32 val;
2309 
2310 	if (prom_getprop(prom.chosen, "stdout", &val, sizeof(val)) <= 0)
2311 		prom_panic("cannot find stdout");
2312 
2313 	prom.stdout = be32_to_cpu(val);
2314 
2315 	/* Get the full OF pathname of the stdout device */
2316 	memset(path, 0, 256);
2317 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
2318 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
2319 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
2320 		     path, prom_strlen(path) + 1);
2321 
2322 	/* instance-to-package fails on PA-Semi */
2323 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
2324 	if (stdout_node != PROM_ERROR) {
2325 		val = cpu_to_be32(stdout_node);
2326 
2327 		/* If it's a display, note it */
2328 		memset(type, 0, sizeof(type));
2329 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
2330 		if (prom_strcmp(type, "display") == 0)
2331 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
2332 	}
2333 }
2334 
2335 static int __init prom_find_machine_type(void)
2336 {
2337 	static char compat[256] __prombss;
2338 	int len, i = 0;
2339 #ifdef CONFIG_PPC64
2340 	phandle rtas;
2341 	int x;
2342 #endif
2343 
2344 	/* Look for a PowerMac or a Cell */
2345 	len = prom_getprop(prom.root, "compatible",
2346 			   compat, sizeof(compat)-1);
2347 	if (len > 0) {
2348 		compat[len] = 0;
2349 		while (i < len) {
2350 			char *p = &compat[i];
2351 			int sl = prom_strlen(p);
2352 			if (sl == 0)
2353 				break;
2354 			if (prom_strstr(p, "Power Macintosh") ||
2355 			    prom_strstr(p, "MacRISC"))
2356 				return PLATFORM_POWERMAC;
2357 #ifdef CONFIG_PPC64
2358 			/* We must make sure we don't detect the IBM Cell
2359 			 * blades as pSeries due to some firmware issues,
2360 			 * so we do it here.
2361 			 */
2362 			if (prom_strstr(p, "IBM,CBEA") ||
2363 			    prom_strstr(p, "IBM,CPBW-1.0"))
2364 				return PLATFORM_GENERIC;
2365 #endif /* CONFIG_PPC64 */
2366 			i += sl + 1;
2367 		}
2368 	}
2369 #ifdef CONFIG_PPC64
2370 	/* Try to figure out if it's an IBM pSeries or any other
2371 	 * PAPR compliant platform. We assume it is if :
2372 	 *  - /device_type is "chrp" (please, do NOT use that for future
2373 	 *    non-IBM designs !
2374 	 *  - it has /rtas
2375 	 */
2376 	len = prom_getprop(prom.root, "device_type",
2377 			   compat, sizeof(compat)-1);
2378 	if (len <= 0)
2379 		return PLATFORM_GENERIC;
2380 	if (prom_strcmp(compat, "chrp"))
2381 		return PLATFORM_GENERIC;
2382 
2383 	/* Default to pSeries. We need to know if we are running LPAR */
2384 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
2385 	if (!PHANDLE_VALID(rtas))
2386 		return PLATFORM_GENERIC;
2387 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
2388 	if (x != PROM_ERROR) {
2389 		prom_debug("Hypertas detected, assuming LPAR !\n");
2390 		return PLATFORM_PSERIES_LPAR;
2391 	}
2392 	return PLATFORM_PSERIES;
2393 #else
2394 	return PLATFORM_GENERIC;
2395 #endif
2396 }
2397 
2398 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
2399 {
2400 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
2401 }
2402 
2403 /*
2404  * If we have a display that we don't know how to drive,
2405  * we will want to try to execute OF's open method for it
2406  * later.  However, OF will probably fall over if we do that
2407  * we've taken over the MMU.
2408  * So we check whether we will need to open the display,
2409  * and if so, open it now.
2410  */
2411 static void __init prom_check_displays(void)
2412 {
2413 	char type[16], *path;
2414 	phandle node;
2415 	ihandle ih;
2416 	int i;
2417 
2418 	static const unsigned char default_colors[] __initconst = {
2419 		0x00, 0x00, 0x00,
2420 		0x00, 0x00, 0xaa,
2421 		0x00, 0xaa, 0x00,
2422 		0x00, 0xaa, 0xaa,
2423 		0xaa, 0x00, 0x00,
2424 		0xaa, 0x00, 0xaa,
2425 		0xaa, 0xaa, 0x00,
2426 		0xaa, 0xaa, 0xaa,
2427 		0x55, 0x55, 0x55,
2428 		0x55, 0x55, 0xff,
2429 		0x55, 0xff, 0x55,
2430 		0x55, 0xff, 0xff,
2431 		0xff, 0x55, 0x55,
2432 		0xff, 0x55, 0xff,
2433 		0xff, 0xff, 0x55,
2434 		0xff, 0xff, 0xff
2435 	};
2436 	const unsigned char *clut;
2437 
2438 	prom_debug("Looking for displays\n");
2439 	for (node = 0; prom_next_node(&node); ) {
2440 		memset(type, 0, sizeof(type));
2441 		prom_getprop(node, "device_type", type, sizeof(type));
2442 		if (prom_strcmp(type, "display") != 0)
2443 			continue;
2444 
2445 		/* It seems OF doesn't null-terminate the path :-( */
2446 		path = prom_scratch;
2447 		memset(path, 0, sizeof(prom_scratch));
2448 
2449 		/*
2450 		 * leave some room at the end of the path for appending extra
2451 		 * arguments
2452 		 */
2453 		if (call_prom("package-to-path", 3, 1, node, path,
2454 			      sizeof(prom_scratch) - 10) == PROM_ERROR)
2455 			continue;
2456 		prom_printf("found display   : %s, opening... ", path);
2457 
2458 		ih = call_prom("open", 1, 1, path);
2459 		if (ih == 0) {
2460 			prom_printf("failed\n");
2461 			continue;
2462 		}
2463 
2464 		/* Success */
2465 		prom_printf("done\n");
2466 		prom_setprop(node, path, "linux,opened", NULL, 0);
2467 
2468 		/* Setup a usable color table when the appropriate
2469 		 * method is available. Should update this to set-colors */
2470 		clut = default_colors;
2471 		for (i = 0; i < 16; i++, clut += 3)
2472 			if (prom_set_color(ih, i, clut[0], clut[1],
2473 					   clut[2]) != 0)
2474 				break;
2475 
2476 #ifdef CONFIG_LOGO_LINUX_CLUT224
2477 		clut = PTRRELOC(logo_linux_clut224.clut);
2478 		for (i = 0; i < logo_linux_clut224.clutsize; i++, clut += 3)
2479 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
2480 					   clut[2]) != 0)
2481 				break;
2482 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
2483 
2484 #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
2485 		if (prom_getprop(node, "linux,boot-display", NULL, 0) !=
2486 		    PROM_ERROR) {
2487 			u32 width, height, pitch, addr;
2488 
2489 			prom_printf("Setting btext !\n");
2490 
2491 			if (prom_getprop(node, "width", &width, 4) == PROM_ERROR)
2492 				return;
2493 
2494 			if (prom_getprop(node, "height", &height, 4) == PROM_ERROR)
2495 				return;
2496 
2497 			if (prom_getprop(node, "linebytes", &pitch, 4) == PROM_ERROR)
2498 				return;
2499 
2500 			if (prom_getprop(node, "address", &addr, 4) == PROM_ERROR)
2501 				return;
2502 
2503 			prom_printf("W=%d H=%d LB=%d addr=0x%x\n",
2504 				    width, height, pitch, addr);
2505 			btext_setup_display(width, height, 8, pitch, addr);
2506 			btext_prepare_BAT();
2507 		}
2508 #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
2509 	}
2510 }
2511 
2512 
2513 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
2514 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
2515 			      unsigned long needed, unsigned long align)
2516 {
2517 	void *ret;
2518 
2519 	*mem_start = ALIGN(*mem_start, align);
2520 	while ((*mem_start + needed) > *mem_end) {
2521 		unsigned long room, chunk;
2522 
2523 		prom_debug("Chunk exhausted, claiming more at %lx...\n",
2524 			   alloc_bottom);
2525 		room = alloc_top - alloc_bottom;
2526 		if (room > DEVTREE_CHUNK_SIZE)
2527 			room = DEVTREE_CHUNK_SIZE;
2528 		if (room < PAGE_SIZE)
2529 			prom_panic("No memory for flatten_device_tree "
2530 				   "(no room)\n");
2531 		chunk = alloc_up(room, 0);
2532 		if (chunk == 0)
2533 			prom_panic("No memory for flatten_device_tree "
2534 				   "(claim failed)\n");
2535 		*mem_end = chunk + room;
2536 	}
2537 
2538 	ret = (void *)*mem_start;
2539 	*mem_start += needed;
2540 
2541 	return ret;
2542 }
2543 
2544 #define dt_push_token(token, mem_start, mem_end) do { 			\
2545 		void *room = make_room(mem_start, mem_end, 4, 4);	\
2546 		*(__be32 *)room = cpu_to_be32(token);			\
2547 	} while(0)
2548 
2549 static unsigned long __init dt_find_string(char *str)
2550 {
2551 	char *s, *os;
2552 
2553 	s = os = (char *)dt_string_start;
2554 	s += 4;
2555 	while (s <  (char *)dt_string_end) {
2556 		if (prom_strcmp(s, str) == 0)
2557 			return s - os;
2558 		s += prom_strlen(s) + 1;
2559 	}
2560 	return 0;
2561 }
2562 
2563 /*
2564  * The Open Firmware 1275 specification states properties must be 31 bytes or
2565  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
2566  */
2567 #define MAX_PROPERTY_NAME 64
2568 
2569 static void __init scan_dt_build_strings(phandle node,
2570 					 unsigned long *mem_start,
2571 					 unsigned long *mem_end)
2572 {
2573 	char *prev_name, *namep, *sstart;
2574 	unsigned long soff;
2575 	phandle child;
2576 
2577 	sstart =  (char *)dt_string_start;
2578 
2579 	/* get and store all property names */
2580 	prev_name = "";
2581 	for (;;) {
2582 		/* 64 is max len of name including nul. */
2583 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
2584 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
2585 			/* No more nodes: unwind alloc */
2586 			*mem_start = (unsigned long)namep;
2587 			break;
2588 		}
2589 
2590  		/* skip "name" */
2591 		if (prom_strcmp(namep, "name") == 0) {
2592  			*mem_start = (unsigned long)namep;
2593  			prev_name = "name";
2594  			continue;
2595  		}
2596 		/* get/create string entry */
2597 		soff = dt_find_string(namep);
2598 		if (soff != 0) {
2599 			*mem_start = (unsigned long)namep;
2600 			namep = sstart + soff;
2601 		} else {
2602 			/* Trim off some if we can */
2603 			*mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
2604 			dt_string_end = *mem_start;
2605 		}
2606 		prev_name = namep;
2607 	}
2608 
2609 	/* do all our children */
2610 	child = call_prom("child", 1, 1, node);
2611 	while (child != 0) {
2612 		scan_dt_build_strings(child, mem_start, mem_end);
2613 		child = call_prom("peer", 1, 1, child);
2614 	}
2615 }
2616 
2617 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
2618 					unsigned long *mem_end)
2619 {
2620 	phandle child;
2621 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
2622 	unsigned long soff;
2623 	unsigned char *valp;
2624 	static char pname[MAX_PROPERTY_NAME] __prombss;
2625 	int l, room, has_phandle = 0;
2626 
2627 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
2628 
2629 	/* get the node's full name */
2630 	namep = (char *)*mem_start;
2631 	room = *mem_end - *mem_start;
2632 	if (room > 255)
2633 		room = 255;
2634 	l = call_prom("package-to-path", 3, 1, node, namep, room);
2635 	if (l >= 0) {
2636 		/* Didn't fit?  Get more room. */
2637 		if (l >= room) {
2638 			if (l >= *mem_end - *mem_start)
2639 				namep = make_room(mem_start, mem_end, l+1, 1);
2640 			call_prom("package-to-path", 3, 1, node, namep, l);
2641 		}
2642 		namep[l] = '\0';
2643 
2644 		/* Fixup an Apple bug where they have bogus \0 chars in the
2645 		 * middle of the path in some properties, and extract
2646 		 * the unit name (everything after the last '/').
2647 		 */
2648 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
2649 			if (*p == '/')
2650 				lp = namep;
2651 			else if (*p != 0)
2652 				*lp++ = *p;
2653 		}
2654 		*lp = 0;
2655 		*mem_start = ALIGN((unsigned long)lp + 1, 4);
2656 	}
2657 
2658 	/* get it again for debugging */
2659 	path = prom_scratch;
2660 	memset(path, 0, sizeof(prom_scratch));
2661 	call_prom("package-to-path", 3, 1, node, path, sizeof(prom_scratch) - 1);
2662 
2663 	/* get and store all properties */
2664 	prev_name = "";
2665 	sstart = (char *)dt_string_start;
2666 	for (;;) {
2667 		if (call_prom("nextprop", 3, 1, node, prev_name,
2668 			      pname) != 1)
2669 			break;
2670 
2671  		/* skip "name" */
2672 		if (prom_strcmp(pname, "name") == 0) {
2673  			prev_name = "name";
2674  			continue;
2675  		}
2676 
2677 		/* find string offset */
2678 		soff = dt_find_string(pname);
2679 		if (soff == 0) {
2680 			prom_printf("WARNING: Can't find string index for"
2681 				    " <%s>, node %s\n", pname, path);
2682 			break;
2683 		}
2684 		prev_name = sstart + soff;
2685 
2686 		/* get length */
2687 		l = call_prom("getproplen", 2, 1, node, pname);
2688 
2689 		/* sanity checks */
2690 		if (l == PROM_ERROR)
2691 			continue;
2692 
2693 		/* push property head */
2694 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
2695 		dt_push_token(l, mem_start, mem_end);
2696 		dt_push_token(soff, mem_start, mem_end);
2697 
2698 		/* push property content */
2699 		valp = make_room(mem_start, mem_end, l, 4);
2700 		call_prom("getprop", 4, 1, node, pname, valp, l);
2701 		*mem_start = ALIGN(*mem_start, 4);
2702 
2703 		if (!prom_strcmp(pname, "phandle"))
2704 			has_phandle = 1;
2705 	}
2706 
2707 	/* Add a "phandle" property if none already exist */
2708 	if (!has_phandle) {
2709 		soff = dt_find_string("phandle");
2710 		if (soff == 0)
2711 			prom_printf("WARNING: Can't find string index for <phandle> node %s\n", path);
2712 		else {
2713 			dt_push_token(OF_DT_PROP, mem_start, mem_end);
2714 			dt_push_token(4, mem_start, mem_end);
2715 			dt_push_token(soff, mem_start, mem_end);
2716 			valp = make_room(mem_start, mem_end, 4, 4);
2717 			*(__be32 *)valp = cpu_to_be32(node);
2718 		}
2719 	}
2720 
2721 	/* do all our children */
2722 	child = call_prom("child", 1, 1, node);
2723 	while (child != 0) {
2724 		scan_dt_build_struct(child, mem_start, mem_end);
2725 		child = call_prom("peer", 1, 1, child);
2726 	}
2727 
2728 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
2729 }
2730 
2731 static void __init flatten_device_tree(void)
2732 {
2733 	phandle root;
2734 	unsigned long mem_start, mem_end, room;
2735 	struct boot_param_header *hdr;
2736 	char *namep;
2737 	u64 *rsvmap;
2738 
2739 	/*
2740 	 * Check how much room we have between alloc top & bottom (+/- a
2741 	 * few pages), crop to 1MB, as this is our "chunk" size
2742 	 */
2743 	room = alloc_top - alloc_bottom - 0x4000;
2744 	if (room > DEVTREE_CHUNK_SIZE)
2745 		room = DEVTREE_CHUNK_SIZE;
2746 	prom_debug("starting device tree allocs at %lx\n", alloc_bottom);
2747 
2748 	/* Now try to claim that */
2749 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
2750 	if (mem_start == 0)
2751 		prom_panic("Can't allocate initial device-tree chunk\n");
2752 	mem_end = mem_start + room;
2753 
2754 	/* Get root of tree */
2755 	root = call_prom("peer", 1, 1, (phandle)0);
2756 	if (root == (phandle)0)
2757 		prom_panic ("couldn't get device tree root\n");
2758 
2759 	/* Build header and make room for mem rsv map */
2760 	mem_start = ALIGN(mem_start, 4);
2761 	hdr = make_room(&mem_start, &mem_end,
2762 			sizeof(struct boot_param_header), 4);
2763 	dt_header_start = (unsigned long)hdr;
2764 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
2765 
2766 	/* Start of strings */
2767 	mem_start = PAGE_ALIGN(mem_start);
2768 	dt_string_start = mem_start;
2769 	mem_start += 4; /* hole */
2770 
2771 	/* Add "phandle" in there, we'll need it */
2772 	namep = make_room(&mem_start, &mem_end, 16, 1);
2773 	prom_strscpy_pad(namep, "phandle", sizeof("phandle"));
2774 	mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
2775 
2776 	/* Build string array */
2777 	prom_printf("Building dt strings...\n");
2778 	scan_dt_build_strings(root, &mem_start, &mem_end);
2779 	dt_string_end = mem_start;
2780 
2781 	/* Build structure */
2782 	mem_start = PAGE_ALIGN(mem_start);
2783 	dt_struct_start = mem_start;
2784 	prom_printf("Building dt structure...\n");
2785 	scan_dt_build_struct(root, &mem_start, &mem_end);
2786 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
2787 	dt_struct_end = PAGE_ALIGN(mem_start);
2788 
2789 	/* Finish header */
2790 	hdr->boot_cpuid_phys = cpu_to_be32(prom.cpu);
2791 	hdr->magic = cpu_to_be32(OF_DT_HEADER);
2792 	hdr->totalsize = cpu_to_be32(dt_struct_end - dt_header_start);
2793 	hdr->off_dt_struct = cpu_to_be32(dt_struct_start - dt_header_start);
2794 	hdr->off_dt_strings = cpu_to_be32(dt_string_start - dt_header_start);
2795 	hdr->dt_strings_size = cpu_to_be32(dt_string_end - dt_string_start);
2796 	hdr->off_mem_rsvmap = cpu_to_be32(((unsigned long)rsvmap) - dt_header_start);
2797 	hdr->version = cpu_to_be32(OF_DT_VERSION);
2798 	/* Version 16 is not backward compatible */
2799 	hdr->last_comp_version = cpu_to_be32(0x10);
2800 
2801 	/* Copy the reserve map in */
2802 	memcpy(rsvmap, mem_reserve_map, sizeof(mem_reserve_map));
2803 
2804 #ifdef DEBUG_PROM
2805 	{
2806 		int i;
2807 		prom_printf("reserved memory map:\n");
2808 		for (i = 0; i < mem_reserve_cnt; i++)
2809 			prom_printf("  %llx - %llx\n",
2810 				    be64_to_cpu(mem_reserve_map[i].base),
2811 				    be64_to_cpu(mem_reserve_map[i].size));
2812 	}
2813 #endif
2814 	/* Bump mem_reserve_cnt to cause further reservations to fail
2815 	 * since it's too late.
2816 	 */
2817 	mem_reserve_cnt = MEM_RESERVE_MAP_SIZE;
2818 
2819 	prom_printf("Device tree strings 0x%lx -> 0x%lx\n",
2820 		    dt_string_start, dt_string_end);
2821 	prom_printf("Device tree struct  0x%lx -> 0x%lx\n",
2822 		    dt_struct_start, dt_struct_end);
2823 }
2824 
2825 /*
2826  * Pegasos and BriQ lacks the "ranges" property in the isa node
2827  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2828  * Pegasos has the IDE configured in legacy mode, but advertised as native
2829  */
2830 static void __init fixup_device_tree_chrp(void)
2831 {
2832 	phandle ph;
2833 	u32 prop[6];
2834 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2835 	char *name;
2836 	int rc;
2837 
2838 	name = "/pci@80000000/isa@c";
2839 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2840 	if (!PHANDLE_VALID(ph)) {
2841 		name = "/pci@ff500000/isa@6";
2842 		ph = call_prom("finddevice", 1, 1, ADDR(name));
2843 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2844 	}
2845 	if (PHANDLE_VALID(ph)) {
2846 		rc = prom_getproplen(ph, "ranges");
2847 		if (rc == 0 || rc == PROM_ERROR) {
2848 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2849 
2850 			prop[0] = 0x1;
2851 			prop[1] = 0x0;
2852 			prop[2] = rloc;
2853 			prop[3] = 0x0;
2854 			prop[4] = 0x0;
2855 			prop[5] = 0x00010000;
2856 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2857 		}
2858 	}
2859 
2860 	name = "/pci@80000000/ide@C,1";
2861 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2862 	if (PHANDLE_VALID(ph)) {
2863 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2864 		prop[0] = 14;
2865 		prop[1] = 0x0;
2866 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2867 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2868 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2869 		if (rc == sizeof(u32)) {
2870 			prop[0] &= ~0x5;
2871 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2872 		}
2873 	}
2874 }
2875 
2876 static void __init fixup_device_tree_pmac64(void)
2877 {
2878 	phandle u3, i2c, mpic;
2879 	u32 u3_rev;
2880 	u32 interrupts[2];
2881 	u32 parent;
2882 
2883 	/* Some G5s have a missing interrupt definition, fix it up here */
2884 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2885 	if (!PHANDLE_VALID(u3))
2886 		return;
2887 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2888 	if (!PHANDLE_VALID(i2c))
2889 		return;
2890 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2891 	if (!PHANDLE_VALID(mpic))
2892 		return;
2893 
2894 	/* check if proper rev of u3 */
2895 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2896 	    == PROM_ERROR)
2897 		return;
2898 	if (u3_rev < 0x35 || u3_rev > 0x39)
2899 		return;
2900 	/* does it need fixup ? */
2901 	if (prom_getproplen(i2c, "interrupts") > 0)
2902 		return;
2903 
2904 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2905 
2906 	/* interrupt on this revision of u3 is number 0 and level */
2907 	interrupts[0] = 0;
2908 	interrupts[1] = 1;
2909 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2910 		     &interrupts, sizeof(interrupts));
2911 	parent = (u32)mpic;
2912 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2913 		     &parent, sizeof(parent));
2914 }
2915 
2916 static void __init fixup_device_tree_pmac(void)
2917 {
2918 	__be32 val = 1;
2919 	char type[8];
2920 	phandle node;
2921 
2922 	// Some pmacs are missing #size-cells on escc or i2s nodes
2923 	for (node = 0; prom_next_node(&node); ) {
2924 		type[0] = '\0';
2925 		prom_getprop(node, "device_type", type, sizeof(type));
2926 		if (prom_strcmp(type, "escc") && prom_strcmp(type, "i2s") &&
2927 		    prom_strcmp(type, "media-bay"))
2928 			continue;
2929 
2930 		if (prom_getproplen(node, "#size-cells") != PROM_ERROR)
2931 			continue;
2932 
2933 		prom_setprop(node, NULL, "#size-cells", &val, sizeof(val));
2934 	}
2935 }
2936 
2937 /*
2938  * The MPC5200 FEC driver requires an phy-handle property to tell it how
2939  * to talk to the phy.  If the phy-handle property is missing, then this
2940  * function is called to add the appropriate nodes and link it to the
2941  * ethernet node.
2942  */
2943 static void __init fixup_device_tree_efika_add_phy(void)
2944 {
2945 	u32 node;
2946 	char prop[64];
2947 	int rv;
2948 
2949 	/* Check if /builtin/ethernet exists - bail if it doesn't */
2950 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
2951 	if (!PHANDLE_VALID(node))
2952 		return;
2953 
2954 	/* Check if the phy-handle property exists - bail if it does */
2955 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2956 	if (rv <= 0)
2957 		return;
2958 
2959 	/*
2960 	 * At this point the ethernet device doesn't have a phy described.
2961 	 * Now we need to add the missing phy node and linkage
2962 	 */
2963 
2964 	/* Check for an MDIO bus node - if missing then create one */
2965 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2966 	if (!PHANDLE_VALID(node)) {
2967 		prom_printf("Adding Ethernet MDIO node\n");
2968 		call_prom("interpret", 1, 1,
2969 			" s\" /builtin\" find-device"
2970 			" new-device"
2971 				" 1 encode-int s\" #address-cells\" property"
2972 				" 0 encode-int s\" #size-cells\" property"
2973 				" s\" mdio\" device-name"
2974 				" s\" fsl,mpc5200b-mdio\" encode-string"
2975 				" s\" compatible\" property"
2976 				" 0xf0003000 0x400 reg"
2977 				" 0x2 encode-int"
2978 				" 0x5 encode-int encode+"
2979 				" 0x3 encode-int encode+"
2980 				" s\" interrupts\" property"
2981 			" finish-device");
2982 	}
2983 
2984 	/* Check for a PHY device node - if missing then create one and
2985 	 * give it's phandle to the ethernet node */
2986 	node = call_prom("finddevice", 1, 1,
2987 			 ADDR("/builtin/mdio/ethernet-phy"));
2988 	if (!PHANDLE_VALID(node)) {
2989 		prom_printf("Adding Ethernet PHY node\n");
2990 		call_prom("interpret", 1, 1,
2991 			" s\" /builtin/mdio\" find-device"
2992 			" new-device"
2993 				" s\" ethernet-phy\" device-name"
2994 				" 0x10 encode-int s\" reg\" property"
2995 				" my-self"
2996 				" ihandle>phandle"
2997 			" finish-device"
2998 			" s\" /builtin/ethernet\" find-device"
2999 				" encode-int"
3000 				" s\" phy-handle\" property"
3001 			" device-end");
3002 	}
3003 }
3004 
3005 static void __init fixup_device_tree_efika(void)
3006 {
3007 	int sound_irq[3] = { 2, 2, 0 };
3008 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
3009 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
3010 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
3011 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
3012 	u32 node;
3013 	char prop[64];
3014 	int rv, len;
3015 
3016 	/* Check if we're really running on a EFIKA */
3017 	node = call_prom("finddevice", 1, 1, ADDR("/"));
3018 	if (!PHANDLE_VALID(node))
3019 		return;
3020 
3021 	rv = prom_getprop(node, "model", prop, sizeof(prop));
3022 	if (rv == PROM_ERROR)
3023 		return;
3024 	if (prom_strcmp(prop, "EFIKA5K2"))
3025 		return;
3026 
3027 	prom_printf("Applying EFIKA device tree fixups\n");
3028 
3029 	/* Claiming to be 'chrp' is death */
3030 	node = call_prom("finddevice", 1, 1, ADDR("/"));
3031 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
3032 	if (rv != PROM_ERROR && (prom_strcmp(prop, "chrp") == 0))
3033 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
3034 
3035 	/* CODEGEN,description is exposed in /proc/cpuinfo so
3036 	   fix that too */
3037 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
3038 	if (rv != PROM_ERROR && (prom_strstr(prop, "CHRP")))
3039 		prom_setprop(node, "/", "CODEGEN,description",
3040 			     "Efika 5200B PowerPC System",
3041 			     sizeof("Efika 5200B PowerPC System"));
3042 
3043 	/* Fixup bestcomm interrupts property */
3044 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
3045 	if (PHANDLE_VALID(node)) {
3046 		len = prom_getproplen(node, "interrupts");
3047 		if (len == 12) {
3048 			prom_printf("Fixing bestcomm interrupts property\n");
3049 			prom_setprop(node, "/builtin/bestcom", "interrupts",
3050 				     bcomm_irq, sizeof(bcomm_irq));
3051 		}
3052 	}
3053 
3054 	/* Fixup sound interrupts property */
3055 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
3056 	if (PHANDLE_VALID(node)) {
3057 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
3058 		if (rv == PROM_ERROR) {
3059 			prom_printf("Adding sound interrupts property\n");
3060 			prom_setprop(node, "/builtin/sound", "interrupts",
3061 				     sound_irq, sizeof(sound_irq));
3062 		}
3063 	}
3064 
3065 	/* Make sure ethernet phy-handle property exists */
3066 	fixup_device_tree_efika_add_phy();
3067 }
3068 
3069 /*
3070  * CFE supplied on Nemo is broken in several ways, biggest
3071  * problem is that it reassigns ISA interrupts to unused mpic ints.
3072  * Add an interrupt-controller property for the io-bridge to use
3073  * and correct the ints so we can attach them to an irq_domain
3074  */
3075 static void __init fixup_device_tree_pasemi(void)
3076 {
3077 	u32 interrupts[2], parent, rval, val = 0;
3078 	char *name, *pci_name;
3079 	phandle iob, node;
3080 
3081 	/* Find the root pci node */
3082 	name = "/pxp@0,e0000000";
3083 	iob = call_prom("finddevice", 1, 1, ADDR(name));
3084 	if (!PHANDLE_VALID(iob))
3085 		return;
3086 
3087 	/* check if interrupt-controller node set yet */
3088 	if (prom_getproplen(iob, "interrupt-controller") !=PROM_ERROR)
3089 		return;
3090 
3091 	prom_printf("adding interrupt-controller property for SB600...\n");
3092 
3093 	prom_setprop(iob, name, "interrupt-controller", &val, 0);
3094 
3095 	pci_name = "/pxp@0,e0000000/pci@11";
3096 	node = call_prom("finddevice", 1, 1, ADDR(pci_name));
3097 	parent = ADDR(iob);
3098 
3099 	for( ; prom_next_node(&node); ) {
3100 		/* scan each node for one with an interrupt */
3101 		if (!PHANDLE_VALID(node))
3102 			continue;
3103 
3104 		rval = prom_getproplen(node, "interrupts");
3105 		if (rval == 0 || rval == PROM_ERROR)
3106 			continue;
3107 
3108 		prom_getprop(node, "interrupts", &interrupts, sizeof(interrupts));
3109 		if ((interrupts[0] < 212) || (interrupts[0] > 222))
3110 			continue;
3111 
3112 		/* found a node, update both interrupts and interrupt-parent */
3113 		if ((interrupts[0] >= 212) && (interrupts[0] <= 215))
3114 			interrupts[0] -= 203;
3115 		if ((interrupts[0] >= 216) && (interrupts[0] <= 220))
3116 			interrupts[0] -= 213;
3117 		if (interrupts[0] == 221)
3118 			interrupts[0] = 14;
3119 		if (interrupts[0] == 222)
3120 			interrupts[0] = 8;
3121 
3122 		prom_setprop(node, pci_name, "interrupts", interrupts,
3123 					sizeof(interrupts));
3124 		prom_setprop(node, pci_name, "interrupt-parent", &parent,
3125 					sizeof(parent));
3126 	}
3127 
3128 	/*
3129 	 * The io-bridge has device_type set to 'io-bridge' change it to 'isa'
3130 	 * so that generic isa-bridge code can add the SB600 and its on-board
3131 	 * peripherals.
3132 	 */
3133 	name = "/pxp@0,e0000000/io-bridge@0";
3134 	iob = call_prom("finddevice", 1, 1, ADDR(name));
3135 	if (!PHANDLE_VALID(iob))
3136 		return;
3137 
3138 	/* device_type is already set, just change it. */
3139 
3140 	prom_printf("Changing device_type of SB600 node...\n");
3141 
3142 	prom_setprop(iob, name, "device_type", "isa", sizeof("isa"));
3143 }
3144 
3145 static void __init fixup_device_tree(void)
3146 {
3147 	if (IS_ENABLED(CONFIG_PPC_CHRP))
3148 		fixup_device_tree_chrp();
3149 
3150 	if (IS_ENABLED(CONFIG_PPC_PMAC))
3151 		fixup_device_tree_pmac();
3152 
3153 	if (IS_ENABLED(CONFIG_PPC_PMAC) && IS_ENABLED(CONFIG_PPC64))
3154 		fixup_device_tree_pmac64();
3155 
3156 	if (IS_ENABLED(CONFIG_PPC_EFIKA))
3157 		fixup_device_tree_efika();
3158 
3159 	if (IS_ENABLED(CONFIG_PPC_PASEMI_NEMO))
3160 		fixup_device_tree_pasemi();
3161 }
3162 
3163 static void __init prom_find_boot_cpu(void)
3164 {
3165 	__be32 rval;
3166 	ihandle prom_cpu;
3167 	phandle cpu_pkg;
3168 
3169 	rval = 0;
3170 	if (prom_getprop(prom.chosen, "cpu", &rval, sizeof(rval)) <= 0)
3171 		return;
3172 	prom_cpu = be32_to_cpu(rval);
3173 
3174 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
3175 
3176 	if (!PHANDLE_VALID(cpu_pkg))
3177 		return;
3178 
3179 	prom_getprop(cpu_pkg, "reg", &rval, sizeof(rval));
3180 	prom.cpu = be32_to_cpu(rval);
3181 
3182 	prom_debug("Booting CPU hw index = %d\n", prom.cpu);
3183 }
3184 
3185 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
3186 {
3187 #ifdef CONFIG_BLK_DEV_INITRD
3188 	if (r3 && r4 && r4 != 0xdeadbeef) {
3189 		__be64 val;
3190 
3191 		prom_initrd_start = is_kernel_addr(r3) ? __pa(r3) : r3;
3192 		prom_initrd_end = prom_initrd_start + r4;
3193 
3194 		val = cpu_to_be64(prom_initrd_start);
3195 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-start",
3196 			     &val, sizeof(val));
3197 		val = cpu_to_be64(prom_initrd_end);
3198 		prom_setprop(prom.chosen, "/chosen", "linux,initrd-end",
3199 			     &val, sizeof(val));
3200 
3201 		reserve_mem(prom_initrd_start,
3202 			    prom_initrd_end - prom_initrd_start);
3203 
3204 		prom_debug("initrd_start=0x%lx\n", prom_initrd_start);
3205 		prom_debug("initrd_end=0x%lx\n", prom_initrd_end);
3206 	}
3207 #endif /* CONFIG_BLK_DEV_INITRD */
3208 }
3209 
3210 #ifdef CONFIG_PPC_SVM
3211 /*
3212  * Perform the Enter Secure Mode ultracall.
3213  */
3214 static int __init enter_secure_mode(unsigned long kbase, unsigned long fdt)
3215 {
3216 	register unsigned long r3 asm("r3") = UV_ESM;
3217 	register unsigned long r4 asm("r4") = kbase;
3218 	register unsigned long r5 asm("r5") = fdt;
3219 
3220 	asm volatile("sc 2" : "+r"(r3) : "r"(r4), "r"(r5));
3221 
3222 	return r3;
3223 }
3224 
3225 /*
3226  * Call the Ultravisor to transfer us to secure memory if we have an ESM blob.
3227  */
3228 static void __init setup_secure_guest(unsigned long kbase, unsigned long fdt)
3229 {
3230 	int ret;
3231 
3232 	if (!prom_svm_enable)
3233 		return;
3234 
3235 	/* Switch to secure mode. */
3236 	prom_printf("Switching to secure mode.\n");
3237 
3238 	/*
3239 	 * The ultravisor will do an integrity check of the kernel image but we
3240 	 * relocated it so the check will fail. Restore the original image by
3241 	 * relocating it back to the kernel virtual base address.
3242 	 */
3243 	relocate(KERNELBASE);
3244 
3245 	ret = enter_secure_mode(kbase, fdt);
3246 
3247 	/* Relocate the kernel again. */
3248 	relocate(kbase);
3249 
3250 	if (ret != U_SUCCESS) {
3251 		prom_printf("Returned %d from switching to secure mode.\n", ret);
3252 		prom_rtas_os_term("Switch to secure mode failed.\n");
3253 	}
3254 }
3255 #else
3256 static void __init setup_secure_guest(unsigned long kbase, unsigned long fdt)
3257 {
3258 }
3259 #endif /* CONFIG_PPC_SVM */
3260 
3261 /*
3262  * We enter here early on, when the Open Firmware prom is still
3263  * handling exceptions and the MMU hash table for us.
3264  */
3265 
3266 unsigned long __init prom_init(unsigned long r3, unsigned long r4,
3267 			       unsigned long pp,
3268 			       unsigned long r6, unsigned long r7,
3269 			       unsigned long kbase)
3270 {
3271 	unsigned long hdr;
3272 
3273 #ifdef CONFIG_PPC32
3274 	unsigned long offset = reloc_offset();
3275 	reloc_got2(offset);
3276 #endif
3277 
3278 	/*
3279 	 * First zero the BSS
3280 	 */
3281 	memset(&__bss_start, 0, __bss_stop - __bss_start);
3282 
3283 	/*
3284 	 * Init interface to Open Firmware, get some node references,
3285 	 * like /chosen
3286 	 */
3287 	prom_init_client_services(pp);
3288 
3289 	/*
3290 	 * See if this OF is old enough that we need to do explicit maps
3291 	 * and other workarounds
3292 	 */
3293 	prom_find_mmu();
3294 
3295 	/*
3296 	 * Init prom stdout device
3297 	 */
3298 	prom_init_stdout();
3299 
3300 	prom_printf("Preparing to boot %s", linux_banner);
3301 
3302 	/*
3303 	 * Get default machine type. At this point, we do not differentiate
3304 	 * between pSeries SMP and pSeries LPAR
3305 	 */
3306 	of_platform = prom_find_machine_type();
3307 	prom_printf("Detected machine type: %x\n", of_platform);
3308 
3309 #ifndef CONFIG_NONSTATIC_KERNEL
3310 	/* Bail if this is a kdump kernel. */
3311 	if (PHYSICAL_START > 0)
3312 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
3313 #endif
3314 
3315 	/*
3316 	 * Check for an initrd
3317 	 */
3318 	prom_check_initrd(r3, r4);
3319 
3320 	/*
3321 	 * Do early parsing of command line
3322 	 */
3323 	early_cmdline_parse();
3324 
3325 #ifdef CONFIG_PPC_PSERIES
3326 	/*
3327 	 * On pSeries, inform the firmware about our capabilities
3328 	 */
3329 	if (of_platform == PLATFORM_PSERIES ||
3330 	    of_platform == PLATFORM_PSERIES_LPAR)
3331 		prom_send_capabilities();
3332 #endif
3333 
3334 	/*
3335 	 * Copy the CPU hold code
3336 	 */
3337 	if (of_platform != PLATFORM_POWERMAC)
3338 		copy_and_flush(0, kbase, 0x100, 0);
3339 
3340 	/*
3341 	 * Initialize memory management within prom_init
3342 	 */
3343 	prom_init_mem();
3344 
3345 	/*
3346 	 * Determine which cpu is actually running right _now_
3347 	 */
3348 	prom_find_boot_cpu();
3349 
3350 	/*
3351 	 * Initialize display devices
3352 	 */
3353 	prom_check_displays();
3354 
3355 #if defined(CONFIG_PPC64) && defined(__BIG_ENDIAN__)
3356 	/*
3357 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
3358 	 * that uses the allocator, we need to make sure we get the top of memory
3359 	 * available for us here...
3360 	 */
3361 	if (of_platform == PLATFORM_PSERIES)
3362 		prom_initialize_tce_table();
3363 #endif
3364 
3365 	/*
3366 	 * On non-powermacs, try to instantiate RTAS. PowerMacs don't
3367 	 * have a usable RTAS implementation.
3368 	 */
3369 	if (of_platform != PLATFORM_POWERMAC)
3370 		prom_instantiate_rtas();
3371 
3372 #ifdef CONFIG_PPC64
3373 	/* instantiate sml */
3374 	prom_instantiate_sml();
3375 #endif
3376 
3377 	/*
3378 	 * On non-powermacs, put all CPUs in spin-loops.
3379 	 *
3380 	 * PowerMacs use a different mechanism to spin CPUs
3381 	 *
3382 	 * (This must be done after instantiating RTAS)
3383 	 */
3384 	if (of_platform != PLATFORM_POWERMAC)
3385 		prom_hold_cpus();
3386 
3387 	/*
3388 	 * Fill in some infos for use by the kernel later on
3389 	 */
3390 	if (prom_memory_limit) {
3391 		__be64 val = cpu_to_be64(prom_memory_limit);
3392 		prom_setprop(prom.chosen, "/chosen", "linux,memory-limit",
3393 			     &val, sizeof(val));
3394 	}
3395 #ifdef CONFIG_PPC64
3396 	if (prom_iommu_off)
3397 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-off",
3398 			     NULL, 0);
3399 
3400 	if (prom_iommu_force_on)
3401 		prom_setprop(prom.chosen, "/chosen", "linux,iommu-force-on",
3402 			     NULL, 0);
3403 
3404 	if (prom_tce_alloc_start) {
3405 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-start",
3406 			     &prom_tce_alloc_start,
3407 			     sizeof(prom_tce_alloc_start));
3408 		prom_setprop(prom.chosen, "/chosen", "linux,tce-alloc-end",
3409 			     &prom_tce_alloc_end,
3410 			     sizeof(prom_tce_alloc_end));
3411 	}
3412 #endif
3413 
3414 	/*
3415 	 * Fixup any known bugs in the device-tree
3416 	 */
3417 	fixup_device_tree();
3418 
3419 	/*
3420 	 * Now finally create the flattened device-tree
3421 	 */
3422 	prom_printf("copying OF device tree...\n");
3423 	flatten_device_tree();
3424 
3425 	/*
3426 	 * in case stdin is USB and still active on IBM machines...
3427 	 * Unfortunately quiesce crashes on some powermacs if we have
3428 	 * closed stdin already (in particular the powerbook 101).
3429 	 */
3430 	if (of_platform != PLATFORM_POWERMAC)
3431 		prom_close_stdin();
3432 
3433 	/*
3434 	 * Call OF "quiesce" method to shut down pending DMA's from
3435 	 * devices etc...
3436 	 */
3437 	prom_printf("Quiescing Open Firmware ...\n");
3438 	call_prom("quiesce", 0, 0);
3439 
3440 	/*
3441 	 * And finally, call the kernel passing it the flattened device
3442 	 * tree and NULL as r5, thus triggering the new entry point which
3443 	 * is common to us and kexec
3444 	 */
3445 	hdr = dt_header_start;
3446 
3447 	prom_printf("Booting Linux via __start() @ 0x%lx ...\n", kbase);
3448 	prom_debug("->dt_header_start=0x%lx\n", hdr);
3449 
3450 #ifdef CONFIG_PPC32
3451 	reloc_got2(-offset);
3452 #endif
3453 
3454 	/* Move to secure memory if we're supposed to be secure guests. */
3455 	setup_secure_guest(kbase, hdr);
3456 
3457 	__start(hdr, kbase, 0, 0, 0, 0, 0);
3458 
3459 	return 0;
3460 }
3461