xref: /linux/arch/powerpc/kernel/prom_init.c (revision 5499b45190237ca90dd2ac86395cf464fe1f4cc7)
1 /*
2  * Procedures for interfacing to Open Firmware.
3  *
4  * Paul Mackerras	August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  *
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15 
16 #undef DEBUG_PROM
17 
18 #include <stdarg.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
22 #include <linux/threads.h>
23 #include <linux/spinlock.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/proc_fs.h>
27 #include <linux/stringify.h>
28 #include <linux/delay.h>
29 #include <linux/initrd.h>
30 #include <linux/bitops.h>
31 #include <asm/prom.h>
32 #include <asm/rtas.h>
33 #include <asm/page.h>
34 #include <asm/processor.h>
35 #include <asm/irq.h>
36 #include <asm/io.h>
37 #include <asm/smp.h>
38 #include <asm/system.h>
39 #include <asm/mmu.h>
40 #include <asm/pgtable.h>
41 #include <asm/pci.h>
42 #include <asm/iommu.h>
43 #include <asm/btext.h>
44 #include <asm/sections.h>
45 #include <asm/machdep.h>
46 
47 #include <linux/linux_logo.h>
48 
49 /*
50  * Properties whose value is longer than this get excluded from our
51  * copy of the device tree. This value does need to be big enough to
52  * ensure that we don't lose things like the interrupt-map property
53  * on a PCI-PCI bridge.
54  */
55 #define MAX_PROPERTY_LENGTH	(1UL * 1024 * 1024)
56 
57 /*
58  * Eventually bump that one up
59  */
60 #define DEVTREE_CHUNK_SIZE	0x100000
61 
62 /*
63  * This is the size of the local memory reserve map that gets copied
64  * into the boot params passed to the kernel. That size is totally
65  * flexible as the kernel just reads the list until it encounters an
66  * entry with size 0, so it can be changed without breaking binary
67  * compatibility
68  */
69 #define MEM_RESERVE_MAP_SIZE	8
70 
71 /*
72  * prom_init() is called very early on, before the kernel text
73  * and data have been mapped to KERNELBASE.  At this point the code
74  * is running at whatever address it has been loaded at.
75  * On ppc32 we compile with -mrelocatable, which means that references
76  * to extern and static variables get relocated automatically.
77  * On ppc64 we have to relocate the references explicitly with
78  * RELOC.  (Note that strings count as static variables.)
79  *
80  * Because OF may have mapped I/O devices into the area starting at
81  * KERNELBASE, particularly on CHRP machines, we can't safely call
82  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
83  * OF calls must be done within prom_init().
84  *
85  * ADDR is used in calls to call_prom.  The 4th and following
86  * arguments to call_prom should be 32-bit values.
87  * On ppc64, 64 bit values are truncated to 32 bits (and
88  * fortunately don't get interpreted as two arguments).
89  */
90 #ifdef CONFIG_PPC64
91 #define RELOC(x)        (*PTRRELOC(&(x)))
92 #define ADDR(x)		(u32) add_reloc_offset((unsigned long)(x))
93 #define OF_WORKAROUNDS	0
94 #else
95 #define RELOC(x)	(x)
96 #define ADDR(x)		(u32) (x)
97 #define OF_WORKAROUNDS	of_workarounds
98 int of_workarounds;
99 #endif
100 
101 #define OF_WA_CLAIM	1	/* do phys/virt claim separately, then map */
102 #define OF_WA_LONGTRAIL	2	/* work around longtrail bugs */
103 
104 #define PROM_BUG() do {						\
105         prom_printf("kernel BUG at %s line 0x%x!\n",		\
106 		    RELOC(__FILE__), __LINE__);			\
107         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);	\
108 } while (0)
109 
110 #ifdef DEBUG_PROM
111 #define prom_debug(x...)	prom_printf(x)
112 #else
113 #define prom_debug(x...)
114 #endif
115 
116 
117 typedef u32 prom_arg_t;
118 
119 struct prom_args {
120         u32 service;
121         u32 nargs;
122         u32 nret;
123         prom_arg_t args[10];
124 };
125 
126 struct prom_t {
127 	ihandle root;
128 	phandle chosen;
129 	int cpu;
130 	ihandle stdout;
131 	ihandle mmumap;
132 	ihandle memory;
133 };
134 
135 struct mem_map_entry {
136 	u64	base;
137 	u64	size;
138 };
139 
140 typedef u32 cell_t;
141 
142 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
143 
144 #ifdef CONFIG_PPC64
145 extern int enter_prom(struct prom_args *args, unsigned long entry);
146 #else
147 static inline int enter_prom(struct prom_args *args, unsigned long entry)
148 {
149 	return ((int (*)(struct prom_args *))entry)(args);
150 }
151 #endif
152 
153 extern void copy_and_flush(unsigned long dest, unsigned long src,
154 			   unsigned long size, unsigned long offset);
155 
156 /* prom structure */
157 static struct prom_t __initdata prom;
158 
159 static unsigned long prom_entry __initdata;
160 
161 #define PROM_SCRATCH_SIZE 256
162 
163 static char __initdata of_stdout_device[256];
164 static char __initdata prom_scratch[PROM_SCRATCH_SIZE];
165 
166 static unsigned long __initdata dt_header_start;
167 static unsigned long __initdata dt_struct_start, dt_struct_end;
168 static unsigned long __initdata dt_string_start, dt_string_end;
169 
170 static unsigned long __initdata prom_initrd_start, prom_initrd_end;
171 
172 #ifdef CONFIG_PPC64
173 static int __initdata prom_iommu_force_on;
174 static int __initdata prom_iommu_off;
175 static unsigned long __initdata prom_tce_alloc_start;
176 static unsigned long __initdata prom_tce_alloc_end;
177 #endif
178 
179 /* Platforms codes are now obsolete in the kernel. Now only used within this
180  * file and ultimately gone too. Feel free to change them if you need, they
181  * are not shared with anything outside of this file anymore
182  */
183 #define PLATFORM_PSERIES	0x0100
184 #define PLATFORM_PSERIES_LPAR	0x0101
185 #define PLATFORM_LPAR		0x0001
186 #define PLATFORM_POWERMAC	0x0400
187 #define PLATFORM_GENERIC	0x0500
188 
189 static int __initdata of_platform;
190 
191 static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
192 
193 static unsigned long __initdata prom_memory_limit;
194 
195 static unsigned long __initdata alloc_top;
196 static unsigned long __initdata alloc_top_high;
197 static unsigned long __initdata alloc_bottom;
198 static unsigned long __initdata rmo_top;
199 static unsigned long __initdata ram_top;
200 
201 static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
202 static int __initdata mem_reserve_cnt;
203 
204 static cell_t __initdata regbuf[1024];
205 
206 
207 /*
208  * Error results ... some OF calls will return "-1" on error, some
209  * will return 0, some will return either. To simplify, here are
210  * macros to use with any ihandle or phandle return value to check if
211  * it is valid
212  */
213 
214 #define PROM_ERROR		(-1u)
215 #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
216 #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
217 
218 
219 /* This is the one and *ONLY* place where we actually call open
220  * firmware.
221  */
222 
223 static int __init call_prom(const char *service, int nargs, int nret, ...)
224 {
225 	int i;
226 	struct prom_args args;
227 	va_list list;
228 
229 	args.service = ADDR(service);
230 	args.nargs = nargs;
231 	args.nret = nret;
232 
233 	va_start(list, nret);
234 	for (i = 0; i < nargs; i++)
235 		args.args[i] = va_arg(list, prom_arg_t);
236 	va_end(list);
237 
238 	for (i = 0; i < nret; i++)
239 		args.args[nargs+i] = 0;
240 
241 	if (enter_prom(&args, RELOC(prom_entry)) < 0)
242 		return PROM_ERROR;
243 
244 	return (nret > 0) ? args.args[nargs] : 0;
245 }
246 
247 static int __init call_prom_ret(const char *service, int nargs, int nret,
248 				prom_arg_t *rets, ...)
249 {
250 	int i;
251 	struct prom_args args;
252 	va_list list;
253 
254 	args.service = ADDR(service);
255 	args.nargs = nargs;
256 	args.nret = nret;
257 
258 	va_start(list, rets);
259 	for (i = 0; i < nargs; i++)
260 		args.args[i] = va_arg(list, prom_arg_t);
261 	va_end(list);
262 
263 	for (i = 0; i < nret; i++)
264 		args.args[nargs+i] = 0;
265 
266 	if (enter_prom(&args, RELOC(prom_entry)) < 0)
267 		return PROM_ERROR;
268 
269 	if (rets != NULL)
270 		for (i = 1; i < nret; ++i)
271 			rets[i-1] = args.args[nargs+i];
272 
273 	return (nret > 0) ? args.args[nargs] : 0;
274 }
275 
276 
277 static void __init prom_print(const char *msg)
278 {
279 	const char *p, *q;
280 	struct prom_t *_prom = &RELOC(prom);
281 
282 	if (_prom->stdout == 0)
283 		return;
284 
285 	for (p = msg; *p != 0; p = q) {
286 		for (q = p; *q != 0 && *q != '\n'; ++q)
287 			;
288 		if (q > p)
289 			call_prom("write", 3, 1, _prom->stdout, p, q - p);
290 		if (*q == 0)
291 			break;
292 		++q;
293 		call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
294 	}
295 }
296 
297 
298 static void __init prom_print_hex(unsigned long val)
299 {
300 	int i, nibbles = sizeof(val)*2;
301 	char buf[sizeof(val)*2+1];
302 	struct prom_t *_prom = &RELOC(prom);
303 
304 	for (i = nibbles-1;  i >= 0;  i--) {
305 		buf[i] = (val & 0xf) + '0';
306 		if (buf[i] > '9')
307 			buf[i] += ('a'-'0'-10);
308 		val >>= 4;
309 	}
310 	buf[nibbles] = '\0';
311 	call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
312 }
313 
314 
315 static void __init prom_printf(const char *format, ...)
316 {
317 	const char *p, *q, *s;
318 	va_list args;
319 	unsigned long v;
320 	struct prom_t *_prom = &RELOC(prom);
321 
322 	va_start(args, format);
323 #ifdef CONFIG_PPC64
324 	format = PTRRELOC(format);
325 #endif
326 	for (p = format; *p != 0; p = q) {
327 		for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
328 			;
329 		if (q > p)
330 			call_prom("write", 3, 1, _prom->stdout, p, q - p);
331 		if (*q == 0)
332 			break;
333 		if (*q == '\n') {
334 			++q;
335 			call_prom("write", 3, 1, _prom->stdout,
336 				  ADDR("\r\n"), 2);
337 			continue;
338 		}
339 		++q;
340 		if (*q == 0)
341 			break;
342 		switch (*q) {
343 		case 's':
344 			++q;
345 			s = va_arg(args, const char *);
346 			prom_print(s);
347 			break;
348 		case 'x':
349 			++q;
350 			v = va_arg(args, unsigned long);
351 			prom_print_hex(v);
352 			break;
353 		}
354 	}
355 }
356 
357 
358 static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
359 				unsigned long align)
360 {
361 	struct prom_t *_prom = &RELOC(prom);
362 
363 	if (align == 0 && (OF_WORKAROUNDS & OF_WA_CLAIM)) {
364 		/*
365 		 * Old OF requires we claim physical and virtual separately
366 		 * and then map explicitly (assuming virtual mode)
367 		 */
368 		int ret;
369 		prom_arg_t result;
370 
371 		ret = call_prom_ret("call-method", 5, 2, &result,
372 				    ADDR("claim"), _prom->memory,
373 				    align, size, virt);
374 		if (ret != 0 || result == -1)
375 			return -1;
376 		ret = call_prom_ret("call-method", 5, 2, &result,
377 				    ADDR("claim"), _prom->mmumap,
378 				    align, size, virt);
379 		if (ret != 0) {
380 			call_prom("call-method", 4, 1, ADDR("release"),
381 				  _prom->memory, size, virt);
382 			return -1;
383 		}
384 		/* the 0x12 is M (coherence) + PP == read/write */
385 		call_prom("call-method", 6, 1,
386 			  ADDR("map"), _prom->mmumap, 0x12, size, virt, virt);
387 		return virt;
388 	}
389 	return call_prom("claim", 3, 1, (prom_arg_t)virt, (prom_arg_t)size,
390 			 (prom_arg_t)align);
391 }
392 
393 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
394 {
395 #ifdef CONFIG_PPC64
396 	reason = PTRRELOC(reason);
397 #endif
398 	prom_print(reason);
399 	/* Do not call exit because it clears the screen on pmac
400 	 * it also causes some sort of double-fault on early pmacs */
401 	if (RELOC(of_platform) == PLATFORM_POWERMAC)
402 		asm("trap\n");
403 
404 	/* ToDo: should put up an SRC here on p/iSeries */
405 	call_prom("exit", 0, 0);
406 
407 	for (;;)			/* should never get here */
408 		;
409 }
410 
411 
412 static int __init prom_next_node(phandle *nodep)
413 {
414 	phandle node;
415 
416 	if ((node = *nodep) != 0
417 	    && (*nodep = call_prom("child", 1, 1, node)) != 0)
418 		return 1;
419 	if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
420 		return 1;
421 	for (;;) {
422 		if ((node = call_prom("parent", 1, 1, node)) == 0)
423 			return 0;
424 		if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
425 			return 1;
426 	}
427 }
428 
429 static int inline prom_getprop(phandle node, const char *pname,
430 			       void *value, size_t valuelen)
431 {
432 	return call_prom("getprop", 4, 1, node, ADDR(pname),
433 			 (u32)(unsigned long) value, (u32) valuelen);
434 }
435 
436 static int inline prom_getproplen(phandle node, const char *pname)
437 {
438 	return call_prom("getproplen", 2, 1, node, ADDR(pname));
439 }
440 
441 static void add_string(char **str, const char *q)
442 {
443 	char *p = *str;
444 
445 	while (*q)
446 		*p++ = *q++;
447 	*p++ = ' ';
448 	*str = p;
449 }
450 
451 static char *tohex(unsigned int x)
452 {
453 	static char digits[] = "0123456789abcdef";
454 	static char result[9];
455 	int i;
456 
457 	result[8] = 0;
458 	i = 8;
459 	do {
460 		--i;
461 		result[i] = digits[x & 0xf];
462 		x >>= 4;
463 	} while (x != 0 && i > 0);
464 	return &result[i];
465 }
466 
467 static int __init prom_setprop(phandle node, const char *nodename,
468 			       const char *pname, void *value, size_t valuelen)
469 {
470 	char cmd[256], *p;
471 
472 	if (!(OF_WORKAROUNDS & OF_WA_LONGTRAIL))
473 		return call_prom("setprop", 4, 1, node, ADDR(pname),
474 				 (u32)(unsigned long) value, (u32) valuelen);
475 
476 	/* gah... setprop doesn't work on longtrail, have to use interpret */
477 	p = cmd;
478 	add_string(&p, "dev");
479 	add_string(&p, nodename);
480 	add_string(&p, tohex((u32)(unsigned long) value));
481 	add_string(&p, tohex(valuelen));
482 	add_string(&p, tohex(ADDR(pname)));
483 	add_string(&p, tohex(strlen(RELOC(pname))));
484 	add_string(&p, "property");
485 	*p = 0;
486 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
487 }
488 
489 /* We can't use the standard versions because of RELOC headaches. */
490 #define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
491 			 || ('a' <= (c) && (c) <= 'f') \
492 			 || ('A' <= (c) && (c) <= 'F'))
493 
494 #define isdigit(c)	('0' <= (c) && (c) <= '9')
495 #define islower(c)	('a' <= (c) && (c) <= 'z')
496 #define toupper(c)	(islower(c) ? ((c) - 'a' + 'A') : (c))
497 
498 unsigned long prom_strtoul(const char *cp, const char **endp)
499 {
500 	unsigned long result = 0, base = 10, value;
501 
502 	if (*cp == '0') {
503 		base = 8;
504 		cp++;
505 		if (toupper(*cp) == 'X') {
506 			cp++;
507 			base = 16;
508 		}
509 	}
510 
511 	while (isxdigit(*cp) &&
512 	       (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
513 		result = result * base + value;
514 		cp++;
515 	}
516 
517 	if (endp)
518 		*endp = cp;
519 
520 	return result;
521 }
522 
523 unsigned long prom_memparse(const char *ptr, const char **retptr)
524 {
525 	unsigned long ret = prom_strtoul(ptr, retptr);
526 	int shift = 0;
527 
528 	/*
529 	 * We can't use a switch here because GCC *may* generate a
530 	 * jump table which won't work, because we're not running at
531 	 * the address we're linked at.
532 	 */
533 	if ('G' == **retptr || 'g' == **retptr)
534 		shift = 30;
535 
536 	if ('M' == **retptr || 'm' == **retptr)
537 		shift = 20;
538 
539 	if ('K' == **retptr || 'k' == **retptr)
540 		shift = 10;
541 
542 	if (shift) {
543 		ret <<= shift;
544 		(*retptr)++;
545 	}
546 
547 	return ret;
548 }
549 
550 /*
551  * Early parsing of the command line passed to the kernel, used for
552  * "mem=x" and the options that affect the iommu
553  */
554 static void __init early_cmdline_parse(void)
555 {
556 	struct prom_t *_prom = &RELOC(prom);
557 	const char *opt;
558 
559 	char *p;
560 	int l = 0;
561 
562 	RELOC(prom_cmd_line[0]) = 0;
563 	p = RELOC(prom_cmd_line);
564 	if ((long)_prom->chosen > 0)
565 		l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
566 #ifdef CONFIG_CMDLINE
567 	if (l <= 0 || p[0] == '\0') /* dbl check */
568 		strlcpy(RELOC(prom_cmd_line),
569 			RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
570 #endif /* CONFIG_CMDLINE */
571 	prom_printf("command line: %s\n", RELOC(prom_cmd_line));
572 
573 #ifdef CONFIG_PPC64
574 	opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
575 	if (opt) {
576 		prom_printf("iommu opt is: %s\n", opt);
577 		opt += 6;
578 		while (*opt && *opt == ' ')
579 			opt++;
580 		if (!strncmp(opt, RELOC("off"), 3))
581 			RELOC(prom_iommu_off) = 1;
582 		else if (!strncmp(opt, RELOC("force"), 5))
583 			RELOC(prom_iommu_force_on) = 1;
584 	}
585 #endif
586 	opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
587 	if (opt) {
588 		opt += 4;
589 		RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
590 #ifdef CONFIG_PPC64
591 		/* Align to 16 MB == size of ppc64 large page */
592 		RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
593 #endif
594 	}
595 }
596 
597 #ifdef CONFIG_PPC_PSERIES
598 /*
599  * There are two methods for telling firmware what our capabilities are.
600  * Newer machines have an "ibm,client-architecture-support" method on the
601  * root node.  For older machines, we have to call the "process-elf-header"
602  * method in the /packages/elf-loader node, passing it a fake 32-bit
603  * ELF header containing a couple of PT_NOTE sections that contain
604  * structures that contain various information.
605  */
606 
607 /*
608  * New method - extensible architecture description vector.
609  *
610  * Because the description vector contains a mix of byte and word
611  * values, we declare it as an unsigned char array, and use this
612  * macro to put word values in.
613  */
614 #define W(x)	((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
615 		((x) >> 8) & 0xff, (x) & 0xff
616 
617 /* Option vector bits - generic bits in byte 1 */
618 #define OV_IGNORE		0x80	/* ignore this vector */
619 #define OV_CESSATION_POLICY	0x40	/* halt if unsupported option present*/
620 
621 /* Option vector 1: processor architectures supported */
622 #define OV1_PPC_2_00		0x80	/* set if we support PowerPC 2.00 */
623 #define OV1_PPC_2_01		0x40	/* set if we support PowerPC 2.01 */
624 #define OV1_PPC_2_02		0x20	/* set if we support PowerPC 2.02 */
625 #define OV1_PPC_2_03		0x10	/* set if we support PowerPC 2.03 */
626 #define OV1_PPC_2_04		0x08	/* set if we support PowerPC 2.04 */
627 #define OV1_PPC_2_05		0x04	/* set if we support PowerPC 2.05 */
628 #define OV1_PPC_2_06		0x02	/* set if we support PowerPC 2.06 */
629 
630 /* Option vector 2: Open Firmware options supported */
631 #define OV2_REAL_MODE		0x20	/* set if we want OF in real mode */
632 
633 /* Option vector 3: processor options supported */
634 #define OV3_FP			0x80	/* floating point */
635 #define OV3_VMX			0x40	/* VMX/Altivec */
636 #define OV3_DFP			0x20	/* decimal FP */
637 
638 /* Option vector 5: PAPR/OF options supported */
639 #define OV5_LPAR		0x80	/* logical partitioning supported */
640 #define OV5_SPLPAR		0x40	/* shared-processor LPAR supported */
641 /* ibm,dynamic-reconfiguration-memory property supported */
642 #define OV5_DRCONF_MEMORY	0x20
643 #define OV5_LARGE_PAGES		0x10	/* large pages supported */
644 #define OV5_DONATE_DEDICATE_CPU 0x02	/* donate dedicated CPU support */
645 /* PCIe/MSI support.  Without MSI full PCIe is not supported */
646 #ifdef CONFIG_PCI_MSI
647 #define OV5_MSI			0x01	/* PCIe/MSI support */
648 #else
649 #define OV5_MSI			0x00
650 #endif /* CONFIG_PCI_MSI */
651 #ifdef CONFIG_PPC_SMLPAR
652 #define OV5_CMO			0x80	/* Cooperative Memory Overcommitment */
653 #else
654 #define OV5_CMO			0x00
655 #endif
656 
657 /* Option Vector 6: IBM PAPR hints */
658 #define OV6_LINUX		0x02	/* Linux is our OS */
659 
660 /*
661  * The architecture vector has an array of PVR mask/value pairs,
662  * followed by # option vectors - 1, followed by the option vectors.
663  */
664 static unsigned char ibm_architecture_vec[] = {
665 	W(0xfffe0000), W(0x003a0000),	/* POWER5/POWER5+ */
666 	W(0xffff0000), W(0x003e0000),	/* POWER6 */
667 	W(0xffff0000), W(0x003f0000),	/* POWER7 */
668 	W(0xffffffff), W(0x0f000003),	/* all 2.06-compliant */
669 	W(0xffffffff), W(0x0f000002),	/* all 2.05-compliant */
670 	W(0xfffffffe), W(0x0f000001),	/* all 2.04-compliant and earlier */
671 	6 - 1,				/* 6 option vectors */
672 
673 	/* option vector 1: processor architectures supported */
674 	3 - 2,				/* length */
675 	0,				/* don't ignore, don't halt */
676 	OV1_PPC_2_00 | OV1_PPC_2_01 | OV1_PPC_2_02 | OV1_PPC_2_03 |
677 	OV1_PPC_2_04 | OV1_PPC_2_05 | OV1_PPC_2_06,
678 
679 	/* option vector 2: Open Firmware options supported */
680 	34 - 2,				/* length */
681 	OV2_REAL_MODE,
682 	0, 0,
683 	W(0xffffffff),			/* real_base */
684 	W(0xffffffff),			/* real_size */
685 	W(0xffffffff),			/* virt_base */
686 	W(0xffffffff),			/* virt_size */
687 	W(0xffffffff),			/* load_base */
688 	W(64),				/* 64MB min RMA */
689 	W(0xffffffff),			/* full client load */
690 	0,				/* min RMA percentage of total RAM */
691 	48,				/* max log_2(hash table size) */
692 
693 	/* option vector 3: processor options supported */
694 	3 - 2,				/* length */
695 	0,				/* don't ignore, don't halt */
696 	OV3_FP | OV3_VMX | OV3_DFP,
697 
698 	/* option vector 4: IBM PAPR implementation */
699 	2 - 2,				/* length */
700 	0,				/* don't halt */
701 
702 	/* option vector 5: PAPR/OF options */
703 	13 - 2,				/* length */
704 	0,				/* don't ignore, don't halt */
705 	OV5_LPAR | OV5_SPLPAR | OV5_LARGE_PAGES | OV5_DRCONF_MEMORY |
706 	OV5_DONATE_DEDICATE_CPU | OV5_MSI,
707 	0,
708 	OV5_CMO,
709 	0,
710 	0,
711 	0,
712 	0,
713 	/* WARNING: The offset of the "number of cores" field below
714 	 * must match by the macro below. Update the definition if
715 	 * the structure layout changes.
716 	 */
717 #define IBM_ARCH_VEC_NRCORES_OFFSET	100
718 	W(NR_CPUS),			/* number of cores supported */
719 
720 	/* option vector 6: IBM PAPR hints */
721 	4 - 2,				/* length */
722 	0,
723 	0,
724 	OV6_LINUX,
725 
726 };
727 
728 /* Old method - ELF header with PT_NOTE sections */
729 static struct fake_elf {
730 	Elf32_Ehdr	elfhdr;
731 	Elf32_Phdr	phdr[2];
732 	struct chrpnote {
733 		u32	namesz;
734 		u32	descsz;
735 		u32	type;
736 		char	name[8];	/* "PowerPC" */
737 		struct chrpdesc {
738 			u32	real_mode;
739 			u32	real_base;
740 			u32	real_size;
741 			u32	virt_base;
742 			u32	virt_size;
743 			u32	load_base;
744 		} chrpdesc;
745 	} chrpnote;
746 	struct rpanote {
747 		u32	namesz;
748 		u32	descsz;
749 		u32	type;
750 		char	name[24];	/* "IBM,RPA-Client-Config" */
751 		struct rpadesc {
752 			u32	lpar_affinity;
753 			u32	min_rmo_size;
754 			u32	min_rmo_percent;
755 			u32	max_pft_size;
756 			u32	splpar;
757 			u32	min_load;
758 			u32	new_mem_def;
759 			u32	ignore_me;
760 		} rpadesc;
761 	} rpanote;
762 } fake_elf = {
763 	.elfhdr = {
764 		.e_ident = { 0x7f, 'E', 'L', 'F',
765 			     ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
766 		.e_type = ET_EXEC,	/* yeah right */
767 		.e_machine = EM_PPC,
768 		.e_version = EV_CURRENT,
769 		.e_phoff = offsetof(struct fake_elf, phdr),
770 		.e_phentsize = sizeof(Elf32_Phdr),
771 		.e_phnum = 2
772 	},
773 	.phdr = {
774 		[0] = {
775 			.p_type = PT_NOTE,
776 			.p_offset = offsetof(struct fake_elf, chrpnote),
777 			.p_filesz = sizeof(struct chrpnote)
778 		}, [1] = {
779 			.p_type = PT_NOTE,
780 			.p_offset = offsetof(struct fake_elf, rpanote),
781 			.p_filesz = sizeof(struct rpanote)
782 		}
783 	},
784 	.chrpnote = {
785 		.namesz = sizeof("PowerPC"),
786 		.descsz = sizeof(struct chrpdesc),
787 		.type = 0x1275,
788 		.name = "PowerPC",
789 		.chrpdesc = {
790 			.real_mode = ~0U,	/* ~0 means "don't care" */
791 			.real_base = ~0U,
792 			.real_size = ~0U,
793 			.virt_base = ~0U,
794 			.virt_size = ~0U,
795 			.load_base = ~0U
796 		},
797 	},
798 	.rpanote = {
799 		.namesz = sizeof("IBM,RPA-Client-Config"),
800 		.descsz = sizeof(struct rpadesc),
801 		.type = 0x12759999,
802 		.name = "IBM,RPA-Client-Config",
803 		.rpadesc = {
804 			.lpar_affinity = 0,
805 			.min_rmo_size = 64,	/* in megabytes */
806 			.min_rmo_percent = 0,
807 			.max_pft_size = 48,	/* 2^48 bytes max PFT size */
808 			.splpar = 1,
809 			.min_load = ~0U,
810 			.new_mem_def = 0
811 		}
812 	}
813 };
814 
815 static int __init prom_count_smt_threads(void)
816 {
817 	phandle node;
818 	char type[64];
819 	unsigned int plen;
820 
821 	/* Pick up th first CPU node we can find */
822 	for (node = 0; prom_next_node(&node); ) {
823 		type[0] = 0;
824 		prom_getprop(node, "device_type", type, sizeof(type));
825 
826 		if (strcmp(type, RELOC("cpu")))
827 			continue;
828 		/*
829 		 * There is an entry for each smt thread, each entry being
830 		 * 4 bytes long.  All cpus should have the same number of
831 		 * smt threads, so return after finding the first.
832 		 */
833 		plen = prom_getproplen(node, "ibm,ppc-interrupt-server#s");
834 		if (plen == PROM_ERROR)
835 			break;
836 		plen >>= 2;
837 		prom_debug("Found 0x%x smt threads per core\n", (unsigned long)plen);
838 
839 		/* Sanity check */
840 		if (plen < 1 || plen > 64) {
841 			prom_printf("Threads per core 0x%x out of bounds, assuming 1\n",
842 				    (unsigned long)plen);
843 			return 1;
844 		}
845 		return plen;
846 	}
847 	prom_debug("No threads found, assuming 1 per core\n");
848 
849 	return 1;
850 
851 }
852 
853 
854 static void __init prom_send_capabilities(void)
855 {
856 	ihandle elfloader, root;
857 	prom_arg_t ret;
858 	u32 *cores;
859 
860 	root = call_prom("open", 1, 1, ADDR("/"));
861 	if (root != 0) {
862 		/* We need to tell the FW about the number of cores we support.
863 		 *
864 		 * To do that, we count the number of threads on the first core
865 		 * (we assume this is the same for all cores) and use it to
866 		 * divide NR_CPUS.
867 		 */
868 		cores = (u32 *)PTRRELOC(&ibm_architecture_vec[IBM_ARCH_VEC_NRCORES_OFFSET]);
869 		if (*cores != NR_CPUS) {
870 			prom_printf("WARNING ! "
871 				    "ibm_architecture_vec structure inconsistent: 0x%x !\n",
872 				    *cores);
873 		} else {
874 			*cores = NR_CPUS / prom_count_smt_threads();
875 			prom_printf("Max number of cores passed to firmware: 0x%x\n",
876 				    (unsigned long)*cores);
877 		}
878 
879 		/* try calling the ibm,client-architecture-support method */
880 		prom_printf("Calling ibm,client-architecture-support...");
881 		if (call_prom_ret("call-method", 3, 2, &ret,
882 				  ADDR("ibm,client-architecture-support"),
883 				  root,
884 				  ADDR(ibm_architecture_vec)) == 0) {
885 			/* the call exists... */
886 			if (ret)
887 				prom_printf("\nWARNING: ibm,client-architecture"
888 					    "-support call FAILED!\n");
889 			call_prom("close", 1, 0, root);
890 			prom_printf(" done\n");
891 			return;
892 		}
893 		call_prom("close", 1, 0, root);
894 		prom_printf(" not implemented\n");
895 	}
896 
897 	/* no ibm,client-architecture-support call, try the old way */
898 	elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
899 	if (elfloader == 0) {
900 		prom_printf("couldn't open /packages/elf-loader\n");
901 		return;
902 	}
903 	call_prom("call-method", 3, 1, ADDR("process-elf-header"),
904 			elfloader, ADDR(&fake_elf));
905 	call_prom("close", 1, 0, elfloader);
906 }
907 #endif
908 
909 /*
910  * Memory allocation strategy... our layout is normally:
911  *
912  *  at 14Mb or more we have vmlinux, then a gap and initrd.  In some
913  *  rare cases, initrd might end up being before the kernel though.
914  *  We assume this won't override the final kernel at 0, we have no
915  *  provision to handle that in this version, but it should hopefully
916  *  never happen.
917  *
918  *  alloc_top is set to the top of RMO, eventually shrink down if the
919  *  TCEs overlap
920  *
921  *  alloc_bottom is set to the top of kernel/initrd
922  *
923  *  from there, allocations are done this way : rtas is allocated
924  *  topmost, and the device-tree is allocated from the bottom. We try
925  *  to grow the device-tree allocation as we progress. If we can't,
926  *  then we fail, we don't currently have a facility to restart
927  *  elsewhere, but that shouldn't be necessary.
928  *
929  *  Note that calls to reserve_mem have to be done explicitly, memory
930  *  allocated with either alloc_up or alloc_down isn't automatically
931  *  reserved.
932  */
933 
934 
935 /*
936  * Allocates memory in the RMO upward from the kernel/initrd
937  *
938  * When align is 0, this is a special case, it means to allocate in place
939  * at the current location of alloc_bottom or fail (that is basically
940  * extending the previous allocation). Used for the device-tree flattening
941  */
942 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
943 {
944 	unsigned long base = RELOC(alloc_bottom);
945 	unsigned long addr = 0;
946 
947 	if (align)
948 		base = _ALIGN_UP(base, align);
949 	prom_debug("alloc_up(%x, %x)\n", size, align);
950 	if (RELOC(ram_top) == 0)
951 		prom_panic("alloc_up() called with mem not initialized\n");
952 
953 	if (align)
954 		base = _ALIGN_UP(RELOC(alloc_bottom), align);
955 	else
956 		base = RELOC(alloc_bottom);
957 
958 	for(; (base + size) <= RELOC(alloc_top);
959 	    base = _ALIGN_UP(base + 0x100000, align)) {
960 		prom_debug("    trying: 0x%x\n\r", base);
961 		addr = (unsigned long)prom_claim(base, size, 0);
962 		if (addr != PROM_ERROR && addr != 0)
963 			break;
964 		addr = 0;
965 		if (align == 0)
966 			break;
967 	}
968 	if (addr == 0)
969 		return 0;
970 	RELOC(alloc_bottom) = addr;
971 
972 	prom_debug(" -> %x\n", addr);
973 	prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
974 	prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
975 	prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
976 	prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
977 	prom_debug("  ram_top      : %x\n", RELOC(ram_top));
978 
979 	return addr;
980 }
981 
982 /*
983  * Allocates memory downward, either from top of RMO, or if highmem
984  * is set, from the top of RAM.  Note that this one doesn't handle
985  * failures.  It does claim memory if highmem is not set.
986  */
987 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
988 				       int highmem)
989 {
990 	unsigned long base, addr = 0;
991 
992 	prom_debug("alloc_down(%x, %x, %s)\n", size, align,
993 		   highmem ? RELOC("(high)") : RELOC("(low)"));
994 	if (RELOC(ram_top) == 0)
995 		prom_panic("alloc_down() called with mem not initialized\n");
996 
997 	if (highmem) {
998 		/* Carve out storage for the TCE table. */
999 		addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
1000 		if (addr <= RELOC(alloc_bottom))
1001 			return 0;
1002 		/* Will we bump into the RMO ? If yes, check out that we
1003 		 * didn't overlap existing allocations there, if we did,
1004 		 * we are dead, we must be the first in town !
1005 		 */
1006 		if (addr < RELOC(rmo_top)) {
1007 			/* Good, we are first */
1008 			if (RELOC(alloc_top) == RELOC(rmo_top))
1009 				RELOC(alloc_top) = RELOC(rmo_top) = addr;
1010 			else
1011 				return 0;
1012 		}
1013 		RELOC(alloc_top_high) = addr;
1014 		goto bail;
1015 	}
1016 
1017 	base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
1018 	for (; base > RELOC(alloc_bottom);
1019 	     base = _ALIGN_DOWN(base - 0x100000, align))  {
1020 		prom_debug("    trying: 0x%x\n\r", base);
1021 		addr = (unsigned long)prom_claim(base, size, 0);
1022 		if (addr != PROM_ERROR && addr != 0)
1023 			break;
1024 		addr = 0;
1025 	}
1026 	if (addr == 0)
1027 		return 0;
1028 	RELOC(alloc_top) = addr;
1029 
1030  bail:
1031 	prom_debug(" -> %x\n", addr);
1032 	prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
1033 	prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
1034 	prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
1035 	prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
1036 	prom_debug("  ram_top      : %x\n", RELOC(ram_top));
1037 
1038 	return addr;
1039 }
1040 
1041 /*
1042  * Parse a "reg" cell
1043  */
1044 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
1045 {
1046 	cell_t *p = *cellp;
1047 	unsigned long r = 0;
1048 
1049 	/* Ignore more than 2 cells */
1050 	while (s > sizeof(unsigned long) / 4) {
1051 		p++;
1052 		s--;
1053 	}
1054 	r = *p++;
1055 #ifdef CONFIG_PPC64
1056 	if (s > 1) {
1057 		r <<= 32;
1058 		r |= *(p++);
1059 	}
1060 #endif
1061 	*cellp = p;
1062 	return r;
1063 }
1064 
1065 /*
1066  * Very dumb function for adding to the memory reserve list, but
1067  * we don't need anything smarter at this point
1068  *
1069  * XXX Eventually check for collisions.  They should NEVER happen.
1070  * If problems seem to show up, it would be a good start to track
1071  * them down.
1072  */
1073 static void __init reserve_mem(u64 base, u64 size)
1074 {
1075 	u64 top = base + size;
1076 	unsigned long cnt = RELOC(mem_reserve_cnt);
1077 
1078 	if (size == 0)
1079 		return;
1080 
1081 	/* We need to always keep one empty entry so that we
1082 	 * have our terminator with "size" set to 0 since we are
1083 	 * dumb and just copy this entire array to the boot params
1084 	 */
1085 	base = _ALIGN_DOWN(base, PAGE_SIZE);
1086 	top = _ALIGN_UP(top, PAGE_SIZE);
1087 	size = top - base;
1088 
1089 	if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
1090 		prom_panic("Memory reserve map exhausted !\n");
1091 	RELOC(mem_reserve_map)[cnt].base = base;
1092 	RELOC(mem_reserve_map)[cnt].size = size;
1093 	RELOC(mem_reserve_cnt) = cnt + 1;
1094 }
1095 
1096 /*
1097  * Initialize memory allocation mechanism, parse "memory" nodes and
1098  * obtain that way the top of memory and RMO to setup out local allocator
1099  */
1100 static void __init prom_init_mem(void)
1101 {
1102 	phandle node;
1103 	char *path, type[64];
1104 	unsigned int plen;
1105 	cell_t *p, *endp;
1106 	struct prom_t *_prom = &RELOC(prom);
1107 	u32 rac, rsc;
1108 
1109 	/*
1110 	 * We iterate the memory nodes to find
1111 	 * 1) top of RMO (first node)
1112 	 * 2) top of memory
1113 	 */
1114 	rac = 2;
1115 	prom_getprop(_prom->root, "#address-cells", &rac, sizeof(rac));
1116 	rsc = 1;
1117 	prom_getprop(_prom->root, "#size-cells", &rsc, sizeof(rsc));
1118 	prom_debug("root_addr_cells: %x\n", (unsigned long) rac);
1119 	prom_debug("root_size_cells: %x\n", (unsigned long) rsc);
1120 
1121 	prom_debug("scanning memory:\n");
1122 	path = RELOC(prom_scratch);
1123 
1124 	for (node = 0; prom_next_node(&node); ) {
1125 		type[0] = 0;
1126 		prom_getprop(node, "device_type", type, sizeof(type));
1127 
1128 		if (type[0] == 0) {
1129 			/*
1130 			 * CHRP Longtrail machines have no device_type
1131 			 * on the memory node, so check the name instead...
1132 			 */
1133 			prom_getprop(node, "name", type, sizeof(type));
1134 		}
1135 		if (strcmp(type, RELOC("memory")))
1136 			continue;
1137 
1138 		plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
1139 		if (plen > sizeof(regbuf)) {
1140 			prom_printf("memory node too large for buffer !\n");
1141 			plen = sizeof(regbuf);
1142 		}
1143 		p = RELOC(regbuf);
1144 		endp = p + (plen / sizeof(cell_t));
1145 
1146 #ifdef DEBUG_PROM
1147 		memset(path, 0, PROM_SCRATCH_SIZE);
1148 		call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1149 		prom_debug("  node %s :\n", path);
1150 #endif /* DEBUG_PROM */
1151 
1152 		while ((endp - p) >= (rac + rsc)) {
1153 			unsigned long base, size;
1154 
1155 			base = prom_next_cell(rac, &p);
1156 			size = prom_next_cell(rsc, &p);
1157 
1158 			if (size == 0)
1159 				continue;
1160 			prom_debug("    %x %x\n", base, size);
1161 			if (base == 0 && (RELOC(of_platform) & PLATFORM_LPAR))
1162 				RELOC(rmo_top) = size;
1163 			if ((base + size) > RELOC(ram_top))
1164 				RELOC(ram_top) = base + size;
1165 		}
1166 	}
1167 
1168 	RELOC(alloc_bottom) = PAGE_ALIGN((unsigned long)&RELOC(_end) + 0x4000);
1169 
1170 	/* Check if we have an initrd after the kernel, if we do move our bottom
1171 	 * point to after it
1172 	 */
1173 	if (RELOC(prom_initrd_start)) {
1174 		if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
1175 			RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
1176 	}
1177 
1178 	/*
1179 	 * If prom_memory_limit is set we reduce the upper limits *except* for
1180 	 * alloc_top_high. This must be the real top of RAM so we can put
1181 	 * TCE's up there.
1182 	 */
1183 
1184 	RELOC(alloc_top_high) = RELOC(ram_top);
1185 
1186 	if (RELOC(prom_memory_limit)) {
1187 		if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
1188 			prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
1189 				RELOC(prom_memory_limit));
1190 			RELOC(prom_memory_limit) = 0;
1191 		} else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
1192 			prom_printf("Ignoring mem=%x >= ram_top.\n",
1193 				RELOC(prom_memory_limit));
1194 			RELOC(prom_memory_limit) = 0;
1195 		} else {
1196 			RELOC(ram_top) = RELOC(prom_memory_limit);
1197 			RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
1198 		}
1199 	}
1200 
1201 	/*
1202 	 * Setup our top alloc point, that is top of RMO or top of
1203 	 * segment 0 when running non-LPAR.
1204 	 * Some RS64 machines have buggy firmware where claims up at
1205 	 * 1GB fail.  Cap at 768MB as a workaround.
1206 	 * Since 768MB is plenty of room, and we need to cap to something
1207 	 * reasonable on 32-bit, cap at 768MB on all machines.
1208 	 */
1209 	if (!RELOC(rmo_top))
1210 		RELOC(rmo_top) = RELOC(ram_top);
1211 	RELOC(rmo_top) = min(0x30000000ul, RELOC(rmo_top));
1212 	RELOC(alloc_top) = RELOC(rmo_top);
1213 	RELOC(alloc_top_high) = RELOC(ram_top);
1214 
1215 	prom_printf("memory layout at init:\n");
1216 	prom_printf("  memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
1217 	prom_printf("  alloc_bottom : %x\n", RELOC(alloc_bottom));
1218 	prom_printf("  alloc_top    : %x\n", RELOC(alloc_top));
1219 	prom_printf("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
1220 	prom_printf("  rmo_top      : %x\n", RELOC(rmo_top));
1221 	prom_printf("  ram_top      : %x\n", RELOC(ram_top));
1222 }
1223 
1224 
1225 /*
1226  * Allocate room for and instantiate RTAS
1227  */
1228 static void __init prom_instantiate_rtas(void)
1229 {
1230 	phandle rtas_node;
1231 	ihandle rtas_inst;
1232 	u32 base, entry = 0;
1233 	u32 size = 0;
1234 
1235 	prom_debug("prom_instantiate_rtas: start...\n");
1236 
1237 	rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1238 	prom_debug("rtas_node: %x\n", rtas_node);
1239 	if (!PHANDLE_VALID(rtas_node))
1240 		return;
1241 
1242 	prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
1243 	if (size == 0)
1244 		return;
1245 
1246 	base = alloc_down(size, PAGE_SIZE, 0);
1247 	if (base == 0) {
1248 		prom_printf("RTAS allocation failed !\n");
1249 		return;
1250 	}
1251 
1252 	rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
1253 	if (!IHANDLE_VALID(rtas_inst)) {
1254 		prom_printf("opening rtas package failed (%x)\n", rtas_inst);
1255 		return;
1256 	}
1257 
1258 	prom_printf("instantiating rtas at 0x%x...", base);
1259 
1260 	if (call_prom_ret("call-method", 3, 2, &entry,
1261 			  ADDR("instantiate-rtas"),
1262 			  rtas_inst, base) != 0
1263 	    || entry == 0) {
1264 		prom_printf(" failed\n");
1265 		return;
1266 	}
1267 	prom_printf(" done\n");
1268 
1269 	reserve_mem(base, size);
1270 
1271 	prom_setprop(rtas_node, "/rtas", "linux,rtas-base",
1272 		     &base, sizeof(base));
1273 	prom_setprop(rtas_node, "/rtas", "linux,rtas-entry",
1274 		     &entry, sizeof(entry));
1275 
1276 	prom_debug("rtas base     = 0x%x\n", base);
1277 	prom_debug("rtas entry    = 0x%x\n", entry);
1278 	prom_debug("rtas size     = 0x%x\n", (long)size);
1279 
1280 	prom_debug("prom_instantiate_rtas: end...\n");
1281 }
1282 
1283 #ifdef CONFIG_PPC64
1284 /*
1285  * Allocate room for and initialize TCE tables
1286  */
1287 static void __init prom_initialize_tce_table(void)
1288 {
1289 	phandle node;
1290 	ihandle phb_node;
1291 	char compatible[64], type[64], model[64];
1292 	char *path = RELOC(prom_scratch);
1293 	u64 base, align;
1294 	u32 minalign, minsize;
1295 	u64 tce_entry, *tce_entryp;
1296 	u64 local_alloc_top, local_alloc_bottom;
1297 	u64 i;
1298 
1299 	if (RELOC(prom_iommu_off))
1300 		return;
1301 
1302 	prom_debug("starting prom_initialize_tce_table\n");
1303 
1304 	/* Cache current top of allocs so we reserve a single block */
1305 	local_alloc_top = RELOC(alloc_top_high);
1306 	local_alloc_bottom = local_alloc_top;
1307 
1308 	/* Search all nodes looking for PHBs. */
1309 	for (node = 0; prom_next_node(&node); ) {
1310 		compatible[0] = 0;
1311 		type[0] = 0;
1312 		model[0] = 0;
1313 		prom_getprop(node, "compatible",
1314 			     compatible, sizeof(compatible));
1315 		prom_getprop(node, "device_type", type, sizeof(type));
1316 		prom_getprop(node, "model", model, sizeof(model));
1317 
1318 		if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1319 			continue;
1320 
1321 		/* Keep the old logic intact to avoid regression. */
1322 		if (compatible[0] != 0) {
1323 			if ((strstr(compatible, RELOC("python")) == NULL) &&
1324 			    (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1325 			    (strstr(compatible, RELOC("Winnipeg")) == NULL))
1326 				continue;
1327 		} else if (model[0] != 0) {
1328 			if ((strstr(model, RELOC("ython")) == NULL) &&
1329 			    (strstr(model, RELOC("peedwagon")) == NULL) &&
1330 			    (strstr(model, RELOC("innipeg")) == NULL))
1331 				continue;
1332 		}
1333 
1334 		if (prom_getprop(node, "tce-table-minalign", &minalign,
1335 				 sizeof(minalign)) == PROM_ERROR)
1336 			minalign = 0;
1337 		if (prom_getprop(node, "tce-table-minsize", &minsize,
1338 				 sizeof(minsize)) == PROM_ERROR)
1339 			minsize = 4UL << 20;
1340 
1341 		/*
1342 		 * Even though we read what OF wants, we just set the table
1343 		 * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
1344 		 * By doing this, we avoid the pitfalls of trying to DMA to
1345 		 * MMIO space and the DMA alias hole.
1346 		 *
1347 		 * On POWER4, firmware sets the TCE region by assuming
1348 		 * each TCE table is 8MB. Using this memory for anything
1349 		 * else will impact performance, so we always allocate 8MB.
1350 		 * Anton
1351 		 */
1352 		if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1353 			minsize = 8UL << 20;
1354 		else
1355 			minsize = 4UL << 20;
1356 
1357 		/* Align to the greater of the align or size */
1358 		align = max(minalign, minsize);
1359 		base = alloc_down(minsize, align, 1);
1360 		if (base == 0)
1361 			prom_panic("ERROR, cannot find space for TCE table.\n");
1362 		if (base < local_alloc_bottom)
1363 			local_alloc_bottom = base;
1364 
1365 		/* It seems OF doesn't null-terminate the path :-( */
1366 		memset(path, 0, PROM_SCRATCH_SIZE);
1367 		/* Call OF to setup the TCE hardware */
1368 		if (call_prom("package-to-path", 3, 1, node,
1369 			      path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1370 			prom_printf("package-to-path failed\n");
1371 		}
1372 
1373 		/* Save away the TCE table attributes for later use. */
1374 		prom_setprop(node, path, "linux,tce-base", &base, sizeof(base));
1375 		prom_setprop(node, path, "linux,tce-size", &minsize, sizeof(minsize));
1376 
1377 		prom_debug("TCE table: %s\n", path);
1378 		prom_debug("\tnode = 0x%x\n", node);
1379 		prom_debug("\tbase = 0x%x\n", base);
1380 		prom_debug("\tsize = 0x%x\n", minsize);
1381 
1382 		/* Initialize the table to have a one-to-one mapping
1383 		 * over the allocated size.
1384 		 */
1385 		tce_entryp = (u64 *)base;
1386 		for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1387 			tce_entry = (i << PAGE_SHIFT);
1388 			tce_entry |= 0x3;
1389 			*tce_entryp = tce_entry;
1390 		}
1391 
1392 		prom_printf("opening PHB %s", path);
1393 		phb_node = call_prom("open", 1, 1, path);
1394 		if (phb_node == 0)
1395 			prom_printf("... failed\n");
1396 		else
1397 			prom_printf("... done\n");
1398 
1399 		call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1400 			  phb_node, -1, minsize,
1401 			  (u32) base, (u32) (base >> 32));
1402 		call_prom("close", 1, 0, phb_node);
1403 	}
1404 
1405 	reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1406 
1407 	/* These are only really needed if there is a memory limit in
1408 	 * effect, but we don't know so export them always. */
1409 	RELOC(prom_tce_alloc_start) = local_alloc_bottom;
1410 	RELOC(prom_tce_alloc_end) = local_alloc_top;
1411 
1412 	/* Flag the first invalid entry */
1413 	prom_debug("ending prom_initialize_tce_table\n");
1414 }
1415 #endif
1416 
1417 /*
1418  * With CHRP SMP we need to use the OF to start the other processors.
1419  * We can't wait until smp_boot_cpus (the OF is trashed by then)
1420  * so we have to put the processors into a holding pattern controlled
1421  * by the kernel (not OF) before we destroy the OF.
1422  *
1423  * This uses a chunk of low memory, puts some holding pattern
1424  * code there and sends the other processors off to there until
1425  * smp_boot_cpus tells them to do something.  The holding pattern
1426  * checks that address until its cpu # is there, when it is that
1427  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
1428  * of setting those values.
1429  *
1430  * We also use physical address 0x4 here to tell when a cpu
1431  * is in its holding pattern code.
1432  *
1433  * -- Cort
1434  */
1435 /*
1436  * We want to reference the copy of __secondary_hold_* in the
1437  * 0 - 0x100 address range
1438  */
1439 #define LOW_ADDR(x)	(((unsigned long) &(x)) & 0xff)
1440 
1441 static void __init prom_hold_cpus(void)
1442 {
1443 	unsigned long i;
1444 	unsigned int reg;
1445 	phandle node;
1446 	char type[64];
1447 	struct prom_t *_prom = &RELOC(prom);
1448 	unsigned long *spinloop
1449 		= (void *) LOW_ADDR(__secondary_hold_spinloop);
1450 	unsigned long *acknowledge
1451 		= (void *) LOW_ADDR(__secondary_hold_acknowledge);
1452 	unsigned long secondary_hold = LOW_ADDR(__secondary_hold);
1453 
1454 	prom_debug("prom_hold_cpus: start...\n");
1455 	prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
1456 	prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
1457 	prom_debug("    1) acknowledge    = 0x%x\n",
1458 		   (unsigned long)acknowledge);
1459 	prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
1460 	prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
1461 
1462 	/* Set the common spinloop variable, so all of the secondary cpus
1463 	 * will block when they are awakened from their OF spinloop.
1464 	 * This must occur for both SMP and non SMP kernels, since OF will
1465 	 * be trashed when we move the kernel.
1466 	 */
1467 	*spinloop = 0;
1468 
1469 	/* look for cpus */
1470 	for (node = 0; prom_next_node(&node); ) {
1471 		type[0] = 0;
1472 		prom_getprop(node, "device_type", type, sizeof(type));
1473 		if (strcmp(type, RELOC("cpu")) != 0)
1474 			continue;
1475 
1476 		/* Skip non-configured cpus. */
1477 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1478 			if (strcmp(type, RELOC("okay")) != 0)
1479 				continue;
1480 
1481 		reg = -1;
1482 		prom_getprop(node, "reg", &reg, sizeof(reg));
1483 
1484 		prom_debug("cpu hw idx   = 0x%x\n", reg);
1485 
1486 		/* Init the acknowledge var which will be reset by
1487 		 * the secondary cpu when it awakens from its OF
1488 		 * spinloop.
1489 		 */
1490 		*acknowledge = (unsigned long)-1;
1491 
1492 		if (reg != _prom->cpu) {
1493 			/* Primary Thread of non-boot cpu */
1494 			prom_printf("starting cpu hw idx %x... ", reg);
1495 			call_prom("start-cpu", 3, 0, node,
1496 				  secondary_hold, reg);
1497 
1498 			for (i = 0; (i < 100000000) &&
1499 			     (*acknowledge == ((unsigned long)-1)); i++ )
1500 				mb();
1501 
1502 			if (*acknowledge == reg)
1503 				prom_printf("done\n");
1504 			else
1505 				prom_printf("failed: %x\n", *acknowledge);
1506 		}
1507 #ifdef CONFIG_SMP
1508 		else
1509 			prom_printf("boot cpu hw idx %x\n", reg);
1510 #endif /* CONFIG_SMP */
1511 	}
1512 
1513 	prom_debug("prom_hold_cpus: end...\n");
1514 }
1515 
1516 
1517 static void __init prom_init_client_services(unsigned long pp)
1518 {
1519 	struct prom_t *_prom = &RELOC(prom);
1520 
1521 	/* Get a handle to the prom entry point before anything else */
1522 	RELOC(prom_entry) = pp;
1523 
1524 	/* get a handle for the stdout device */
1525 	_prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1526 	if (!PHANDLE_VALID(_prom->chosen))
1527 		prom_panic("cannot find chosen"); /* msg won't be printed :( */
1528 
1529 	/* get device tree root */
1530 	_prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1531 	if (!PHANDLE_VALID(_prom->root))
1532 		prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1533 
1534 	_prom->mmumap = 0;
1535 }
1536 
1537 #ifdef CONFIG_PPC32
1538 /*
1539  * For really old powermacs, we need to map things we claim.
1540  * For that, we need the ihandle of the mmu.
1541  * Also, on the longtrail, we need to work around other bugs.
1542  */
1543 static void __init prom_find_mmu(void)
1544 {
1545 	struct prom_t *_prom = &RELOC(prom);
1546 	phandle oprom;
1547 	char version[64];
1548 
1549 	oprom = call_prom("finddevice", 1, 1, ADDR("/openprom"));
1550 	if (!PHANDLE_VALID(oprom))
1551 		return;
1552 	if (prom_getprop(oprom, "model", version, sizeof(version)) <= 0)
1553 		return;
1554 	version[sizeof(version) - 1] = 0;
1555 	/* XXX might need to add other versions here */
1556 	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
1557 		of_workarounds = OF_WA_CLAIM;
1558 	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
1559 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
1560 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
1561 	} else
1562 		return;
1563 	_prom->memory = call_prom("open", 1, 1, ADDR("/memory"));
1564 	prom_getprop(_prom->chosen, "mmu", &_prom->mmumap,
1565 		     sizeof(_prom->mmumap));
1566 	if (!IHANDLE_VALID(_prom->memory) || !IHANDLE_VALID(_prom->mmumap))
1567 		of_workarounds &= ~OF_WA_CLAIM;		/* hmmm */
1568 }
1569 #else
1570 #define prom_find_mmu()
1571 #endif
1572 
1573 static void __init prom_init_stdout(void)
1574 {
1575 	struct prom_t *_prom = &RELOC(prom);
1576 	char *path = RELOC(of_stdout_device);
1577 	char type[16];
1578 	u32 val;
1579 
1580 	if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1581 		prom_panic("cannot find stdout");
1582 
1583 	_prom->stdout = val;
1584 
1585 	/* Get the full OF pathname of the stdout device */
1586 	memset(path, 0, 256);
1587 	call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1588 	val = call_prom("instance-to-package", 1, 1, _prom->stdout);
1589 	prom_setprop(_prom->chosen, "/chosen", "linux,stdout-package",
1590 		     &val, sizeof(val));
1591 	prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
1592 	prom_setprop(_prom->chosen, "/chosen", "linux,stdout-path",
1593 		     path, strlen(path) + 1);
1594 
1595 	/* If it's a display, note it */
1596 	memset(type, 0, sizeof(type));
1597 	prom_getprop(val, "device_type", type, sizeof(type));
1598 	if (strcmp(type, RELOC("display")) == 0)
1599 		prom_setprop(val, path, "linux,boot-display", NULL, 0);
1600 }
1601 
1602 static void __init prom_close_stdin(void)
1603 {
1604 	struct prom_t *_prom = &RELOC(prom);
1605 	ihandle val;
1606 
1607 	if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1608 		call_prom("close", 1, 0, val);
1609 }
1610 
1611 static int __init prom_find_machine_type(void)
1612 {
1613 	struct prom_t *_prom = &RELOC(prom);
1614 	char compat[256];
1615 	int len, i = 0;
1616 #ifdef CONFIG_PPC64
1617 	phandle rtas;
1618 	int x;
1619 #endif
1620 
1621 	/* Look for a PowerMac */
1622 	len = prom_getprop(_prom->root, "compatible",
1623 			   compat, sizeof(compat)-1);
1624 	if (len > 0) {
1625 		compat[len] = 0;
1626 		while (i < len) {
1627 			char *p = &compat[i];
1628 			int sl = strlen(p);
1629 			if (sl == 0)
1630 				break;
1631 			if (strstr(p, RELOC("Power Macintosh")) ||
1632 			    strstr(p, RELOC("MacRISC")))
1633 				return PLATFORM_POWERMAC;
1634 #ifdef CONFIG_PPC64
1635 			/* We must make sure we don't detect the IBM Cell
1636 			 * blades as pSeries due to some firmware issues,
1637 			 * so we do it here.
1638 			 */
1639 			if (strstr(p, RELOC("IBM,CBEA")) ||
1640 			    strstr(p, RELOC("IBM,CPBW-1.0")))
1641 				return PLATFORM_GENERIC;
1642 #endif /* CONFIG_PPC64 */
1643 			i += sl + 1;
1644 		}
1645 	}
1646 #ifdef CONFIG_PPC64
1647 	/* If not a mac, try to figure out if it's an IBM pSeries or any other
1648 	 * PAPR compliant platform. We assume it is if :
1649 	 *  - /device_type is "chrp" (please, do NOT use that for future
1650 	 *    non-IBM designs !
1651 	 *  - it has /rtas
1652 	 */
1653 	len = prom_getprop(_prom->root, "device_type",
1654 			   compat, sizeof(compat)-1);
1655 	if (len <= 0)
1656 		return PLATFORM_GENERIC;
1657 	if (strcmp(compat, RELOC("chrp")))
1658 		return PLATFORM_GENERIC;
1659 
1660 	/* Default to pSeries. We need to know if we are running LPAR */
1661 	rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1662 	if (!PHANDLE_VALID(rtas))
1663 		return PLATFORM_GENERIC;
1664 	x = prom_getproplen(rtas, "ibm,hypertas-functions");
1665 	if (x != PROM_ERROR) {
1666 		prom_debug("Hypertas detected, assuming LPAR !\n");
1667 		return PLATFORM_PSERIES_LPAR;
1668 	}
1669 	return PLATFORM_PSERIES;
1670 #else
1671 	return PLATFORM_GENERIC;
1672 #endif
1673 }
1674 
1675 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1676 {
1677 	return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1678 }
1679 
1680 /*
1681  * If we have a display that we don't know how to drive,
1682  * we will want to try to execute OF's open method for it
1683  * later.  However, OF will probably fall over if we do that
1684  * we've taken over the MMU.
1685  * So we check whether we will need to open the display,
1686  * and if so, open it now.
1687  */
1688 static void __init prom_check_displays(void)
1689 {
1690 	char type[16], *path;
1691 	phandle node;
1692 	ihandle ih;
1693 	int i;
1694 
1695 	static unsigned char default_colors[] = {
1696 		0x00, 0x00, 0x00,
1697 		0x00, 0x00, 0xaa,
1698 		0x00, 0xaa, 0x00,
1699 		0x00, 0xaa, 0xaa,
1700 		0xaa, 0x00, 0x00,
1701 		0xaa, 0x00, 0xaa,
1702 		0xaa, 0xaa, 0x00,
1703 		0xaa, 0xaa, 0xaa,
1704 		0x55, 0x55, 0x55,
1705 		0x55, 0x55, 0xff,
1706 		0x55, 0xff, 0x55,
1707 		0x55, 0xff, 0xff,
1708 		0xff, 0x55, 0x55,
1709 		0xff, 0x55, 0xff,
1710 		0xff, 0xff, 0x55,
1711 		0xff, 0xff, 0xff
1712 	};
1713 	const unsigned char *clut;
1714 
1715 	prom_debug("Looking for displays\n");
1716 	for (node = 0; prom_next_node(&node); ) {
1717 		memset(type, 0, sizeof(type));
1718 		prom_getprop(node, "device_type", type, sizeof(type));
1719 		if (strcmp(type, RELOC("display")) != 0)
1720 			continue;
1721 
1722 		/* It seems OF doesn't null-terminate the path :-( */
1723 		path = RELOC(prom_scratch);
1724 		memset(path, 0, PROM_SCRATCH_SIZE);
1725 
1726 		/*
1727 		 * leave some room at the end of the path for appending extra
1728 		 * arguments
1729 		 */
1730 		if (call_prom("package-to-path", 3, 1, node, path,
1731 			      PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1732 			continue;
1733 		prom_printf("found display   : %s, opening... ", path);
1734 
1735 		ih = call_prom("open", 1, 1, path);
1736 		if (ih == 0) {
1737 			prom_printf("failed\n");
1738 			continue;
1739 		}
1740 
1741 		/* Success */
1742 		prom_printf("done\n");
1743 		prom_setprop(node, path, "linux,opened", NULL, 0);
1744 
1745 		/* Setup a usable color table when the appropriate
1746 		 * method is available. Should update this to set-colors */
1747 		clut = RELOC(default_colors);
1748 		for (i = 0; i < 32; i++, clut += 3)
1749 			if (prom_set_color(ih, i, clut[0], clut[1],
1750 					   clut[2]) != 0)
1751 				break;
1752 
1753 #ifdef CONFIG_LOGO_LINUX_CLUT224
1754 		clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1755 		for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1756 			if (prom_set_color(ih, i + 32, clut[0], clut[1],
1757 					   clut[2]) != 0)
1758 				break;
1759 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
1760 	}
1761 }
1762 
1763 
1764 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1765 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
1766 			      unsigned long needed, unsigned long align)
1767 {
1768 	void *ret;
1769 
1770 	*mem_start = _ALIGN(*mem_start, align);
1771 	while ((*mem_start + needed) > *mem_end) {
1772 		unsigned long room, chunk;
1773 
1774 		prom_debug("Chunk exhausted, claiming more at %x...\n",
1775 			   RELOC(alloc_bottom));
1776 		room = RELOC(alloc_top) - RELOC(alloc_bottom);
1777 		if (room > DEVTREE_CHUNK_SIZE)
1778 			room = DEVTREE_CHUNK_SIZE;
1779 		if (room < PAGE_SIZE)
1780 			prom_panic("No memory for flatten_device_tree (no room)");
1781 		chunk = alloc_up(room, 0);
1782 		if (chunk == 0)
1783 			prom_panic("No memory for flatten_device_tree (claim failed)");
1784 		*mem_end = RELOC(alloc_top);
1785 	}
1786 
1787 	ret = (void *)*mem_start;
1788 	*mem_start += needed;
1789 
1790 	return ret;
1791 }
1792 
1793 #define dt_push_token(token, mem_start, mem_end) \
1794 	do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
1795 
1796 static unsigned long __init dt_find_string(char *str)
1797 {
1798 	char *s, *os;
1799 
1800 	s = os = (char *)RELOC(dt_string_start);
1801 	s += 4;
1802 	while (s <  (char *)RELOC(dt_string_end)) {
1803 		if (strcmp(s, str) == 0)
1804 			return s - os;
1805 		s += strlen(s) + 1;
1806 	}
1807 	return 0;
1808 }
1809 
1810 /*
1811  * The Open Firmware 1275 specification states properties must be 31 bytes or
1812  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
1813  */
1814 #define MAX_PROPERTY_NAME 64
1815 
1816 static void __init scan_dt_build_strings(phandle node,
1817 					 unsigned long *mem_start,
1818 					 unsigned long *mem_end)
1819 {
1820 	char *prev_name, *namep, *sstart;
1821 	unsigned long soff;
1822 	phandle child;
1823 
1824 	sstart =  (char *)RELOC(dt_string_start);
1825 
1826 	/* get and store all property names */
1827 	prev_name = RELOC("");
1828 	for (;;) {
1829 		/* 64 is max len of name including nul. */
1830 		namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
1831 		if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
1832 			/* No more nodes: unwind alloc */
1833 			*mem_start = (unsigned long)namep;
1834 			break;
1835 		}
1836 
1837  		/* skip "name" */
1838  		if (strcmp(namep, RELOC("name")) == 0) {
1839  			*mem_start = (unsigned long)namep;
1840  			prev_name = RELOC("name");
1841  			continue;
1842  		}
1843 		/* get/create string entry */
1844 		soff = dt_find_string(namep);
1845 		if (soff != 0) {
1846 			*mem_start = (unsigned long)namep;
1847 			namep = sstart + soff;
1848 		} else {
1849 			/* Trim off some if we can */
1850 			*mem_start = (unsigned long)namep + strlen(namep) + 1;
1851 			RELOC(dt_string_end) = *mem_start;
1852 		}
1853 		prev_name = namep;
1854 	}
1855 
1856 	/* do all our children */
1857 	child = call_prom("child", 1, 1, node);
1858 	while (child != 0) {
1859 		scan_dt_build_strings(child, mem_start, mem_end);
1860 		child = call_prom("peer", 1, 1, child);
1861 	}
1862 }
1863 
1864 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
1865 					unsigned long *mem_end)
1866 {
1867 	phandle child;
1868 	char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
1869 	unsigned long soff;
1870 	unsigned char *valp;
1871 	static char pname[MAX_PROPERTY_NAME];
1872 	int l, room;
1873 
1874 	dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
1875 
1876 	/* get the node's full name */
1877 	namep = (char *)*mem_start;
1878 	room = *mem_end - *mem_start;
1879 	if (room > 255)
1880 		room = 255;
1881 	l = call_prom("package-to-path", 3, 1, node, namep, room);
1882 	if (l >= 0) {
1883 		/* Didn't fit?  Get more room. */
1884 		if (l >= room) {
1885 			if (l >= *mem_end - *mem_start)
1886 				namep = make_room(mem_start, mem_end, l+1, 1);
1887 			call_prom("package-to-path", 3, 1, node, namep, l);
1888 		}
1889 		namep[l] = '\0';
1890 
1891 		/* Fixup an Apple bug where they have bogus \0 chars in the
1892 		 * middle of the path in some properties, and extract
1893 		 * the unit name (everything after the last '/').
1894 		 */
1895 		for (lp = p = namep, ep = namep + l; p < ep; p++) {
1896 			if (*p == '/')
1897 				lp = namep;
1898 			else if (*p != 0)
1899 				*lp++ = *p;
1900 		}
1901 		*lp = 0;
1902 		*mem_start = _ALIGN((unsigned long)lp + 1, 4);
1903 	}
1904 
1905 	/* get it again for debugging */
1906 	path = RELOC(prom_scratch);
1907 	memset(path, 0, PROM_SCRATCH_SIZE);
1908 	call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1909 
1910 	/* get and store all properties */
1911 	prev_name = RELOC("");
1912 	sstart = (char *)RELOC(dt_string_start);
1913 	for (;;) {
1914 		if (call_prom("nextprop", 3, 1, node, prev_name,
1915 			      RELOC(pname)) != 1)
1916 			break;
1917 
1918  		/* skip "name" */
1919  		if (strcmp(RELOC(pname), RELOC("name")) == 0) {
1920  			prev_name = RELOC("name");
1921  			continue;
1922  		}
1923 
1924 		/* find string offset */
1925 		soff = dt_find_string(RELOC(pname));
1926 		if (soff == 0) {
1927 			prom_printf("WARNING: Can't find string index for"
1928 				    " <%s>, node %s\n", RELOC(pname), path);
1929 			break;
1930 		}
1931 		prev_name = sstart + soff;
1932 
1933 		/* get length */
1934 		l = call_prom("getproplen", 2, 1, node, RELOC(pname));
1935 
1936 		/* sanity checks */
1937 		if (l == PROM_ERROR)
1938 			continue;
1939 		if (l > MAX_PROPERTY_LENGTH) {
1940 			prom_printf("WARNING: ignoring large property ");
1941 			/* It seems OF doesn't null-terminate the path :-( */
1942 			prom_printf("[%s] ", path);
1943 			prom_printf("%s length 0x%x\n", RELOC(pname), l);
1944 			continue;
1945 		}
1946 
1947 		/* push property head */
1948 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
1949 		dt_push_token(l, mem_start, mem_end);
1950 		dt_push_token(soff, mem_start, mem_end);
1951 
1952 		/* push property content */
1953 		valp = make_room(mem_start, mem_end, l, 4);
1954 		call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
1955 		*mem_start = _ALIGN(*mem_start, 4);
1956 	}
1957 
1958 	/* Add a "linux,phandle" property. */
1959 	soff = dt_find_string(RELOC("linux,phandle"));
1960 	if (soff == 0)
1961 		prom_printf("WARNING: Can't find string index for"
1962 			    " <linux-phandle> node %s\n", path);
1963 	else {
1964 		dt_push_token(OF_DT_PROP, mem_start, mem_end);
1965 		dt_push_token(4, mem_start, mem_end);
1966 		dt_push_token(soff, mem_start, mem_end);
1967 		valp = make_room(mem_start, mem_end, 4, 4);
1968 		*(u32 *)valp = node;
1969 	}
1970 
1971 	/* do all our children */
1972 	child = call_prom("child", 1, 1, node);
1973 	while (child != 0) {
1974 		scan_dt_build_struct(child, mem_start, mem_end);
1975 		child = call_prom("peer", 1, 1, child);
1976 	}
1977 
1978 	dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
1979 }
1980 
1981 static void __init flatten_device_tree(void)
1982 {
1983 	phandle root;
1984 	unsigned long mem_start, mem_end, room;
1985 	struct boot_param_header *hdr;
1986 	struct prom_t *_prom = &RELOC(prom);
1987 	char *namep;
1988 	u64 *rsvmap;
1989 
1990 	/*
1991 	 * Check how much room we have between alloc top & bottom (+/- a
1992 	 * few pages), crop to 4Mb, as this is our "chuck" size
1993 	 */
1994 	room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
1995 	if (room > DEVTREE_CHUNK_SIZE)
1996 		room = DEVTREE_CHUNK_SIZE;
1997 	prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
1998 
1999 	/* Now try to claim that */
2000 	mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
2001 	if (mem_start == 0)
2002 		prom_panic("Can't allocate initial device-tree chunk\n");
2003 	mem_end = RELOC(alloc_top);
2004 
2005 	/* Get root of tree */
2006 	root = call_prom("peer", 1, 1, (phandle)0);
2007 	if (root == (phandle)0)
2008 		prom_panic ("couldn't get device tree root\n");
2009 
2010 	/* Build header and make room for mem rsv map */
2011 	mem_start = _ALIGN(mem_start, 4);
2012 	hdr = make_room(&mem_start, &mem_end,
2013 			sizeof(struct boot_param_header), 4);
2014 	RELOC(dt_header_start) = (unsigned long)hdr;
2015 	rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
2016 
2017 	/* Start of strings */
2018 	mem_start = PAGE_ALIGN(mem_start);
2019 	RELOC(dt_string_start) = mem_start;
2020 	mem_start += 4; /* hole */
2021 
2022 	/* Add "linux,phandle" in there, we'll need it */
2023 	namep = make_room(&mem_start, &mem_end, 16, 1);
2024 	strcpy(namep, RELOC("linux,phandle"));
2025 	mem_start = (unsigned long)namep + strlen(namep) + 1;
2026 
2027 	/* Build string array */
2028 	prom_printf("Building dt strings...\n");
2029 	scan_dt_build_strings(root, &mem_start, &mem_end);
2030 	RELOC(dt_string_end) = mem_start;
2031 
2032 	/* Build structure */
2033 	mem_start = PAGE_ALIGN(mem_start);
2034 	RELOC(dt_struct_start) = mem_start;
2035 	prom_printf("Building dt structure...\n");
2036 	scan_dt_build_struct(root, &mem_start, &mem_end);
2037 	dt_push_token(OF_DT_END, &mem_start, &mem_end);
2038 	RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
2039 
2040 	/* Finish header */
2041 	hdr->boot_cpuid_phys = _prom->cpu;
2042 	hdr->magic = OF_DT_HEADER;
2043 	hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
2044 	hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
2045 	hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
2046 	hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
2047 	hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
2048 	hdr->version = OF_DT_VERSION;
2049 	/* Version 16 is not backward compatible */
2050 	hdr->last_comp_version = 0x10;
2051 
2052 	/* Copy the reserve map in */
2053 	memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
2054 
2055 #ifdef DEBUG_PROM
2056 	{
2057 		int i;
2058 		prom_printf("reserved memory map:\n");
2059 		for (i = 0; i < RELOC(mem_reserve_cnt); i++)
2060 			prom_printf("  %x - %x\n",
2061 				    RELOC(mem_reserve_map)[i].base,
2062 				    RELOC(mem_reserve_map)[i].size);
2063 	}
2064 #endif
2065 	/* Bump mem_reserve_cnt to cause further reservations to fail
2066 	 * since it's too late.
2067 	 */
2068 	RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
2069 
2070 	prom_printf("Device tree strings 0x%x -> 0x%x\n",
2071 		    RELOC(dt_string_start), RELOC(dt_string_end));
2072 	prom_printf("Device tree struct  0x%x -> 0x%x\n",
2073 		    RELOC(dt_struct_start), RELOC(dt_struct_end));
2074 
2075 }
2076 
2077 #ifdef CONFIG_PPC_MAPLE
2078 /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges property.
2079  * The values are bad, and it doesn't even have the right number of cells. */
2080 static void __init fixup_device_tree_maple(void)
2081 {
2082 	phandle isa;
2083 	u32 rloc = 0x01002000; /* IO space; PCI device = 4 */
2084 	u32 isa_ranges[6];
2085 	char *name;
2086 
2087 	name = "/ht@0/isa@4";
2088 	isa = call_prom("finddevice", 1, 1, ADDR(name));
2089 	if (!PHANDLE_VALID(isa)) {
2090 		name = "/ht@0/isa@6";
2091 		isa = call_prom("finddevice", 1, 1, ADDR(name));
2092 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2093 	}
2094 	if (!PHANDLE_VALID(isa))
2095 		return;
2096 
2097 	if (prom_getproplen(isa, "ranges") != 12)
2098 		return;
2099 	if (prom_getprop(isa, "ranges", isa_ranges, sizeof(isa_ranges))
2100 		== PROM_ERROR)
2101 		return;
2102 
2103 	if (isa_ranges[0] != 0x1 ||
2104 		isa_ranges[1] != 0xf4000000 ||
2105 		isa_ranges[2] != 0x00010000)
2106 		return;
2107 
2108 	prom_printf("Fixing up bogus ISA range on Maple/Apache...\n");
2109 
2110 	isa_ranges[0] = 0x1;
2111 	isa_ranges[1] = 0x0;
2112 	isa_ranges[2] = rloc;
2113 	isa_ranges[3] = 0x0;
2114 	isa_ranges[4] = 0x0;
2115 	isa_ranges[5] = 0x00010000;
2116 	prom_setprop(isa, name, "ranges",
2117 			isa_ranges, sizeof(isa_ranges));
2118 }
2119 
2120 #define CPC925_MC_START		0xf8000000
2121 #define CPC925_MC_LENGTH	0x1000000
2122 /* The values for memory-controller don't have right number of cells */
2123 static void __init fixup_device_tree_maple_memory_controller(void)
2124 {
2125 	phandle mc;
2126 	u32 mc_reg[4];
2127 	char *name = "/hostbridge@f8000000";
2128 	struct prom_t *_prom = &RELOC(prom);
2129 	u32 ac, sc;
2130 
2131 	mc = call_prom("finddevice", 1, 1, ADDR(name));
2132 	if (!PHANDLE_VALID(mc))
2133 		return;
2134 
2135 	if (prom_getproplen(mc, "reg") != 8)
2136 		return;
2137 
2138 	prom_getprop(_prom->root, "#address-cells", &ac, sizeof(ac));
2139 	prom_getprop(_prom->root, "#size-cells", &sc, sizeof(sc));
2140 	if ((ac != 2) || (sc != 2))
2141 		return;
2142 
2143 	if (prom_getprop(mc, "reg", mc_reg, sizeof(mc_reg)) == PROM_ERROR)
2144 		return;
2145 
2146 	if (mc_reg[0] != CPC925_MC_START || mc_reg[1] != CPC925_MC_LENGTH)
2147 		return;
2148 
2149 	prom_printf("Fixing up bogus hostbridge on Maple...\n");
2150 
2151 	mc_reg[0] = 0x0;
2152 	mc_reg[1] = CPC925_MC_START;
2153 	mc_reg[2] = 0x0;
2154 	mc_reg[3] = CPC925_MC_LENGTH;
2155 	prom_setprop(mc, name, "reg", mc_reg, sizeof(mc_reg));
2156 }
2157 #else
2158 #define fixup_device_tree_maple()
2159 #define fixup_device_tree_maple_memory_controller()
2160 #endif
2161 
2162 #ifdef CONFIG_PPC_CHRP
2163 /*
2164  * Pegasos and BriQ lacks the "ranges" property in the isa node
2165  * Pegasos needs decimal IRQ 14/15, not hexadecimal
2166  * Pegasos has the IDE configured in legacy mode, but advertised as native
2167  */
2168 static void __init fixup_device_tree_chrp(void)
2169 {
2170 	phandle ph;
2171 	u32 prop[6];
2172 	u32 rloc = 0x01006000; /* IO space; PCI device = 12 */
2173 	char *name;
2174 	int rc;
2175 
2176 	name = "/pci@80000000/isa@c";
2177 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2178 	if (!PHANDLE_VALID(ph)) {
2179 		name = "/pci@ff500000/isa@6";
2180 		ph = call_prom("finddevice", 1, 1, ADDR(name));
2181 		rloc = 0x01003000; /* IO space; PCI device = 6 */
2182 	}
2183 	if (PHANDLE_VALID(ph)) {
2184 		rc = prom_getproplen(ph, "ranges");
2185 		if (rc == 0 || rc == PROM_ERROR) {
2186 			prom_printf("Fixing up missing ISA range on Pegasos...\n");
2187 
2188 			prop[0] = 0x1;
2189 			prop[1] = 0x0;
2190 			prop[2] = rloc;
2191 			prop[3] = 0x0;
2192 			prop[4] = 0x0;
2193 			prop[5] = 0x00010000;
2194 			prom_setprop(ph, name, "ranges", prop, sizeof(prop));
2195 		}
2196 	}
2197 
2198 	name = "/pci@80000000/ide@C,1";
2199 	ph = call_prom("finddevice", 1, 1, ADDR(name));
2200 	if (PHANDLE_VALID(ph)) {
2201 		prom_printf("Fixing up IDE interrupt on Pegasos...\n");
2202 		prop[0] = 14;
2203 		prop[1] = 0x0;
2204 		prom_setprop(ph, name, "interrupts", prop, 2*sizeof(u32));
2205 		prom_printf("Fixing up IDE class-code on Pegasos...\n");
2206 		rc = prom_getprop(ph, "class-code", prop, sizeof(u32));
2207 		if (rc == sizeof(u32)) {
2208 			prop[0] &= ~0x5;
2209 			prom_setprop(ph, name, "class-code", prop, sizeof(u32));
2210 		}
2211 	}
2212 }
2213 #else
2214 #define fixup_device_tree_chrp()
2215 #endif
2216 
2217 #if defined(CONFIG_PPC64) && defined(CONFIG_PPC_PMAC)
2218 static void __init fixup_device_tree_pmac(void)
2219 {
2220 	phandle u3, i2c, mpic;
2221 	u32 u3_rev;
2222 	u32 interrupts[2];
2223 	u32 parent;
2224 
2225 	/* Some G5s have a missing interrupt definition, fix it up here */
2226 	u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
2227 	if (!PHANDLE_VALID(u3))
2228 		return;
2229 	i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
2230 	if (!PHANDLE_VALID(i2c))
2231 		return;
2232 	mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
2233 	if (!PHANDLE_VALID(mpic))
2234 		return;
2235 
2236 	/* check if proper rev of u3 */
2237 	if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
2238 	    == PROM_ERROR)
2239 		return;
2240 	if (u3_rev < 0x35 || u3_rev > 0x39)
2241 		return;
2242 	/* does it need fixup ? */
2243 	if (prom_getproplen(i2c, "interrupts") > 0)
2244 		return;
2245 
2246 	prom_printf("fixing up bogus interrupts for u3 i2c...\n");
2247 
2248 	/* interrupt on this revision of u3 is number 0 and level */
2249 	interrupts[0] = 0;
2250 	interrupts[1] = 1;
2251 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupts",
2252 		     &interrupts, sizeof(interrupts));
2253 	parent = (u32)mpic;
2254 	prom_setprop(i2c, "/u3@0,f8000000/i2c@f8001000", "interrupt-parent",
2255 		     &parent, sizeof(parent));
2256 }
2257 #else
2258 #define fixup_device_tree_pmac()
2259 #endif
2260 
2261 #ifdef CONFIG_PPC_EFIKA
2262 /*
2263  * The MPC5200 FEC driver requires an phy-handle property to tell it how
2264  * to talk to the phy.  If the phy-handle property is missing, then this
2265  * function is called to add the appropriate nodes and link it to the
2266  * ethernet node.
2267  */
2268 static void __init fixup_device_tree_efika_add_phy(void)
2269 {
2270 	u32 node;
2271 	char prop[64];
2272 	int rv;
2273 
2274 	/* Check if /builtin/ethernet exists - bail if it doesn't */
2275 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/ethernet"));
2276 	if (!PHANDLE_VALID(node))
2277 		return;
2278 
2279 	/* Check if the phy-handle property exists - bail if it does */
2280 	rv = prom_getprop(node, "phy-handle", prop, sizeof(prop));
2281 	if (!rv)
2282 		return;
2283 
2284 	/*
2285 	 * At this point the ethernet device doesn't have a phy described.
2286 	 * Now we need to add the missing phy node and linkage
2287 	 */
2288 
2289 	/* Check for an MDIO bus node - if missing then create one */
2290 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/mdio"));
2291 	if (!PHANDLE_VALID(node)) {
2292 		prom_printf("Adding Ethernet MDIO node\n");
2293 		call_prom("interpret", 1, 1,
2294 			" s\" /builtin\" find-device"
2295 			" new-device"
2296 				" 1 encode-int s\" #address-cells\" property"
2297 				" 0 encode-int s\" #size-cells\" property"
2298 				" s\" mdio\" device-name"
2299 				" s\" fsl,mpc5200b-mdio\" encode-string"
2300 				" s\" compatible\" property"
2301 				" 0xf0003000 0x400 reg"
2302 				" 0x2 encode-int"
2303 				" 0x5 encode-int encode+"
2304 				" 0x3 encode-int encode+"
2305 				" s\" interrupts\" property"
2306 			" finish-device");
2307 	};
2308 
2309 	/* Check for a PHY device node - if missing then create one and
2310 	 * give it's phandle to the ethernet node */
2311 	node = call_prom("finddevice", 1, 1,
2312 			 ADDR("/builtin/mdio/ethernet-phy"));
2313 	if (!PHANDLE_VALID(node)) {
2314 		prom_printf("Adding Ethernet PHY node\n");
2315 		call_prom("interpret", 1, 1,
2316 			" s\" /builtin/mdio\" find-device"
2317 			" new-device"
2318 				" s\" ethernet-phy\" device-name"
2319 				" 0x10 encode-int s\" reg\" property"
2320 				" my-self"
2321 				" ihandle>phandle"
2322 			" finish-device"
2323 			" s\" /builtin/ethernet\" find-device"
2324 				" encode-int"
2325 				" s\" phy-handle\" property"
2326 			" device-end");
2327 	}
2328 }
2329 
2330 static void __init fixup_device_tree_efika(void)
2331 {
2332 	int sound_irq[3] = { 2, 2, 0 };
2333 	int bcomm_irq[3*16] = { 3,0,0, 3,1,0, 3,2,0, 3,3,0,
2334 				3,4,0, 3,5,0, 3,6,0, 3,7,0,
2335 				3,8,0, 3,9,0, 3,10,0, 3,11,0,
2336 				3,12,0, 3,13,0, 3,14,0, 3,15,0 };
2337 	u32 node;
2338 	char prop[64];
2339 	int rv, len;
2340 
2341 	/* Check if we're really running on a EFIKA */
2342 	node = call_prom("finddevice", 1, 1, ADDR("/"));
2343 	if (!PHANDLE_VALID(node))
2344 		return;
2345 
2346 	rv = prom_getprop(node, "model", prop, sizeof(prop));
2347 	if (rv == PROM_ERROR)
2348 		return;
2349 	if (strcmp(prop, "EFIKA5K2"))
2350 		return;
2351 
2352 	prom_printf("Applying EFIKA device tree fixups\n");
2353 
2354 	/* Claiming to be 'chrp' is death */
2355 	node = call_prom("finddevice", 1, 1, ADDR("/"));
2356 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
2357 	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
2358 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
2359 
2360 	/* CODEGEN,description is exposed in /proc/cpuinfo so
2361 	   fix that too */
2362 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
2363 	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
2364 		prom_setprop(node, "/", "CODEGEN,description",
2365 			     "Efika 5200B PowerPC System",
2366 			     sizeof("Efika 5200B PowerPC System"));
2367 
2368 	/* Fixup bestcomm interrupts property */
2369 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/bestcomm"));
2370 	if (PHANDLE_VALID(node)) {
2371 		len = prom_getproplen(node, "interrupts");
2372 		if (len == 12) {
2373 			prom_printf("Fixing bestcomm interrupts property\n");
2374 			prom_setprop(node, "/builtin/bestcom", "interrupts",
2375 				     bcomm_irq, sizeof(bcomm_irq));
2376 		}
2377 	}
2378 
2379 	/* Fixup sound interrupts property */
2380 	node = call_prom("finddevice", 1, 1, ADDR("/builtin/sound"));
2381 	if (PHANDLE_VALID(node)) {
2382 		rv = prom_getprop(node, "interrupts", prop, sizeof(prop));
2383 		if (rv == PROM_ERROR) {
2384 			prom_printf("Adding sound interrupts property\n");
2385 			prom_setprop(node, "/builtin/sound", "interrupts",
2386 				     sound_irq, sizeof(sound_irq));
2387 		}
2388 	}
2389 
2390 	/* Make sure ethernet phy-handle property exists */
2391 	fixup_device_tree_efika_add_phy();
2392 }
2393 #else
2394 #define fixup_device_tree_efika()
2395 #endif
2396 
2397 static void __init fixup_device_tree(void)
2398 {
2399 	fixup_device_tree_maple();
2400 	fixup_device_tree_maple_memory_controller();
2401 	fixup_device_tree_chrp();
2402 	fixup_device_tree_pmac();
2403 	fixup_device_tree_efika();
2404 }
2405 
2406 static void __init prom_find_boot_cpu(void)
2407 {
2408 	struct prom_t *_prom = &RELOC(prom);
2409 	u32 getprop_rval;
2410 	ihandle prom_cpu;
2411 	phandle cpu_pkg;
2412 
2413 	_prom->cpu = 0;
2414 	if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
2415 		return;
2416 
2417 	cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
2418 
2419 	prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
2420 	_prom->cpu = getprop_rval;
2421 
2422 	prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
2423 }
2424 
2425 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
2426 {
2427 #ifdef CONFIG_BLK_DEV_INITRD
2428 	struct prom_t *_prom = &RELOC(prom);
2429 
2430 	if (r3 && r4 && r4 != 0xdeadbeef) {
2431 		unsigned long val;
2432 
2433 		RELOC(prom_initrd_start) = is_kernel_addr(r3) ? __pa(r3) : r3;
2434 		RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
2435 
2436 		val = RELOC(prom_initrd_start);
2437 		prom_setprop(_prom->chosen, "/chosen", "linux,initrd-start",
2438 			     &val, sizeof(val));
2439 		val = RELOC(prom_initrd_end);
2440 		prom_setprop(_prom->chosen, "/chosen", "linux,initrd-end",
2441 			     &val, sizeof(val));
2442 
2443 		reserve_mem(RELOC(prom_initrd_start),
2444 			    RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
2445 
2446 		prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
2447 		prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
2448 	}
2449 #endif /* CONFIG_BLK_DEV_INITRD */
2450 }
2451 
2452 /*
2453  * We enter here early on, when the Open Firmware prom is still
2454  * handling exceptions and the MMU hash table for us.
2455  */
2456 
2457 unsigned long __init prom_init(unsigned long r3, unsigned long r4,
2458 			       unsigned long pp,
2459 			       unsigned long r6, unsigned long r7,
2460 			       unsigned long kbase)
2461 {
2462 	struct prom_t *_prom;
2463 	unsigned long hdr;
2464 
2465 #ifdef CONFIG_PPC32
2466 	unsigned long offset = reloc_offset();
2467 	reloc_got2(offset);
2468 #endif
2469 
2470 	_prom = &RELOC(prom);
2471 
2472 	/*
2473 	 * First zero the BSS
2474 	 */
2475 	memset(&RELOC(__bss_start), 0, __bss_stop - __bss_start);
2476 
2477 	/*
2478 	 * Init interface to Open Firmware, get some node references,
2479 	 * like /chosen
2480 	 */
2481 	prom_init_client_services(pp);
2482 
2483 	/*
2484 	 * See if this OF is old enough that we need to do explicit maps
2485 	 * and other workarounds
2486 	 */
2487 	prom_find_mmu();
2488 
2489 	/*
2490 	 * Init prom stdout device
2491 	 */
2492 	prom_init_stdout();
2493 
2494 	prom_printf("Preparing to boot %s", RELOC(linux_banner));
2495 
2496 	/*
2497 	 * Get default machine type. At this point, we do not differentiate
2498 	 * between pSeries SMP and pSeries LPAR
2499 	 */
2500 	RELOC(of_platform) = prom_find_machine_type();
2501 
2502 #ifndef CONFIG_RELOCATABLE
2503 	/* Bail if this is a kdump kernel. */
2504 	if (PHYSICAL_START > 0)
2505 		prom_panic("Error: You can't boot a kdump kernel from OF!\n");
2506 #endif
2507 
2508 	/*
2509 	 * Check for an initrd
2510 	 */
2511 	prom_check_initrd(r3, r4);
2512 
2513 #ifdef CONFIG_PPC_PSERIES
2514 	/*
2515 	 * On pSeries, inform the firmware about our capabilities
2516 	 */
2517 	if (RELOC(of_platform) == PLATFORM_PSERIES ||
2518 	    RELOC(of_platform) == PLATFORM_PSERIES_LPAR)
2519 		prom_send_capabilities();
2520 #endif
2521 
2522 	/*
2523 	 * Copy the CPU hold code
2524 	 */
2525 	if (RELOC(of_platform) != PLATFORM_POWERMAC)
2526 		copy_and_flush(0, kbase, 0x100, 0);
2527 
2528 	/*
2529 	 * Do early parsing of command line
2530 	 */
2531 	early_cmdline_parse();
2532 
2533 	/*
2534 	 * Initialize memory management within prom_init
2535 	 */
2536 	prom_init_mem();
2537 
2538 	/*
2539 	 * Determine which cpu is actually running right _now_
2540 	 */
2541 	prom_find_boot_cpu();
2542 
2543 	/*
2544 	 * Initialize display devices
2545 	 */
2546 	prom_check_displays();
2547 
2548 #ifdef CONFIG_PPC64
2549 	/*
2550 	 * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
2551 	 * that uses the allocator, we need to make sure we get the top of memory
2552 	 * available for us here...
2553 	 */
2554 	if (RELOC(of_platform) == PLATFORM_PSERIES)
2555 		prom_initialize_tce_table();
2556 #endif
2557 
2558 	/*
2559 	 * On non-powermacs, try to instantiate RTAS and puts all CPUs
2560 	 * in spin-loops. PowerMacs don't have a working RTAS and use
2561 	 * a different way to spin CPUs
2562 	 */
2563 	if (RELOC(of_platform) != PLATFORM_POWERMAC) {
2564 		prom_instantiate_rtas();
2565 		prom_hold_cpus();
2566 	}
2567 
2568 	/*
2569 	 * Fill in some infos for use by the kernel later on
2570 	 */
2571 	if (RELOC(prom_memory_limit))
2572 		prom_setprop(_prom->chosen, "/chosen", "linux,memory-limit",
2573 			     &RELOC(prom_memory_limit),
2574 			     sizeof(prom_memory_limit));
2575 #ifdef CONFIG_PPC64
2576 	if (RELOC(prom_iommu_off))
2577 		prom_setprop(_prom->chosen, "/chosen", "linux,iommu-off",
2578 			     NULL, 0);
2579 
2580 	if (RELOC(prom_iommu_force_on))
2581 		prom_setprop(_prom->chosen, "/chosen", "linux,iommu-force-on",
2582 			     NULL, 0);
2583 
2584 	if (RELOC(prom_tce_alloc_start)) {
2585 		prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-start",
2586 			     &RELOC(prom_tce_alloc_start),
2587 			     sizeof(prom_tce_alloc_start));
2588 		prom_setprop(_prom->chosen, "/chosen", "linux,tce-alloc-end",
2589 			     &RELOC(prom_tce_alloc_end),
2590 			     sizeof(prom_tce_alloc_end));
2591 	}
2592 #endif
2593 
2594 	/*
2595 	 * Fixup any known bugs in the device-tree
2596 	 */
2597 	fixup_device_tree();
2598 
2599 	/*
2600 	 * Now finally create the flattened device-tree
2601 	 */
2602 	prom_printf("copying OF device tree...\n");
2603 	flatten_device_tree();
2604 
2605 	/*
2606 	 * in case stdin is USB and still active on IBM machines...
2607 	 * Unfortunately quiesce crashes on some powermacs if we have
2608 	 * closed stdin already (in particular the powerbook 101).
2609 	 */
2610 	if (RELOC(of_platform) != PLATFORM_POWERMAC)
2611 		prom_close_stdin();
2612 
2613 	/*
2614 	 * Call OF "quiesce" method to shut down pending DMA's from
2615 	 * devices etc...
2616 	 */
2617 	prom_printf("Calling quiesce...\n");
2618 	call_prom("quiesce", 0, 0);
2619 
2620 	/*
2621 	 * And finally, call the kernel passing it the flattened device
2622 	 * tree and NULL as r5, thus triggering the new entry point which
2623 	 * is common to us and kexec
2624 	 */
2625 	hdr = RELOC(dt_header_start);
2626 	prom_printf("returning from prom_init\n");
2627 	prom_debug("->dt_header_start=0x%x\n", hdr);
2628 
2629 #ifdef CONFIG_PPC32
2630 	reloc_got2(-offset);
2631 #endif
2632 
2633 	__start(hdr, kbase, 0);
2634 
2635 	return 0;
2636 }
2637