]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/ppc64/kernel/prom_init.c
modify defines according to _ASM_POWERPC_ISERIES_
[karo-tx-linux.git] / arch / ppc64 / kernel / prom_init.c
1 /*
2  * 
3  *
4  * Procedures for interfacing to Open Firmware.
5  *
6  * Paul Mackerras       August 1996.
7  * Copyright (C) 1996 Paul Mackerras.
8  * 
9  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
10  *    {engebret|bergner}@us.ibm.com 
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  */
17
18 #undef DEBUG_PROM
19
20 #include <stdarg.h>
21 #include <linux/config.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/init.h>
25 #include <linux/threads.h>
26 #include <linux/spinlock.h>
27 #include <linux/types.h>
28 #include <linux/pci.h>
29 #include <linux/proc_fs.h>
30 #include <linux/stringify.h>
31 #include <linux/delay.h>
32 #include <linux/initrd.h>
33 #include <linux/bitops.h>
34 #include <asm/prom.h>
35 #include <asm/rtas.h>
36 #include <asm/abs_addr.h>
37 #include <asm/page.h>
38 #include <asm/processor.h>
39 #include <asm/irq.h>
40 #include <asm/io.h>
41 #include <asm/smp.h>
42 #include <asm/system.h>
43 #include <asm/mmu.h>
44 #include <asm/pgtable.h>
45 #include <asm/pci.h>
46 #include <asm/iommu.h>
47 #include <asm/ppcdebug.h>
48 #include <asm/btext.h>
49 #include <asm/sections.h>
50 #include <asm/machdep.h>
51
52 #ifdef CONFIG_LOGO_LINUX_CLUT224
53 #include <linux/linux_logo.h>
54 extern const struct linux_logo logo_linux_clut224;
55 #endif
56
57 /*
58  * Properties whose value is longer than this get excluded from our
59  * copy of the device tree. This value does need to be big enough to
60  * ensure that we don't lose things like the interrupt-map property
61  * on a PCI-PCI bridge.
62  */
63 #define MAX_PROPERTY_LENGTH     (1UL * 1024 * 1024)
64
65 /*
66  * Eventually bump that one up
67  */
68 #define DEVTREE_CHUNK_SIZE      0x100000
69
70 /*
71  * This is the size of the local memory reserve map that gets copied
72  * into the boot params passed to the kernel. That size is totally
73  * flexible as the kernel just reads the list until it encounters an
74  * entry with size 0, so it can be changed without breaking binary
75  * compatibility
76  */
77 #define MEM_RESERVE_MAP_SIZE    8
78
79 /*
80  * prom_init() is called very early on, before the kernel text
81  * and data have been mapped to KERNELBASE.  At this point the code
82  * is running at whatever address it has been loaded at, so
83  * references to extern and static variables must be relocated
84  * explicitly.  The procedure reloc_offset() returns the address
85  * we're currently running at minus the address we were linked at.
86  * (Note that strings count as static variables.)
87  *
88  * Because OF may have mapped I/O devices into the area starting at
89  * KERNELBASE, particularly on CHRP machines, we can't safely call
90  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
91  * OF calls should be done within prom_init(), and prom_init()
92  * and all routines called within it must be careful to relocate
93  * references as necessary.
94  *
95  * Note that the bss is cleared *after* prom_init runs, so we have
96  * to make sure that any static or extern variables it accesses
97  * are put in the data segment.
98  */
99
100
101 #define PROM_BUG() do {                                         \
102         prom_printf("kernel BUG at %s line 0x%x!\n",            \
103                     RELOC(__FILE__), __LINE__);                 \
104         __asm__ __volatile__(".long " BUG_ILLEGAL_INSTR);       \
105 } while (0)
106
107 #ifdef DEBUG_PROM
108 #define prom_debug(x...)        prom_printf(x)
109 #else
110 #define prom_debug(x...)
111 #endif
112
113
114 typedef u32 prom_arg_t;
115
116 struct prom_args {
117         u32 service;
118         u32 nargs;
119         u32 nret;
120         prom_arg_t args[10];
121         prom_arg_t *rets;     /* Pointer to return values in args[16]. */
122 };
123
124 struct prom_t {
125         unsigned long entry;
126         ihandle root;
127         ihandle chosen;
128         int cpu;
129         ihandle stdout;
130         ihandle disp_node;
131         struct prom_args args;
132         unsigned long version;
133         unsigned long root_size_cells;
134         unsigned long root_addr_cells;
135 };
136
137 struct pci_reg_property {
138         struct pci_address addr;
139         u32 size_hi;
140         u32 size_lo;
141 };
142
143 struct mem_map_entry {
144         u64     base;
145         u64     size;
146 };
147
148 typedef u32 cell_t;
149
150 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
151
152 extern void enter_prom(struct prom_args *args, unsigned long entry);
153 extern void copy_and_flush(unsigned long dest, unsigned long src,
154                            unsigned long size, unsigned long offset);
155
156 extern unsigned long klimit;
157
158 /* prom structure */
159 static struct prom_t __initdata prom;
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 static int __initdata iommu_force_on;
173 static int __initdata ppc64_iommu_off;
174 static int __initdata of_platform;
175
176 static char __initdata prom_cmd_line[COMMAND_LINE_SIZE];
177
178 static unsigned long __initdata prom_memory_limit;
179 static unsigned long __initdata prom_tce_alloc_start;
180 static unsigned long __initdata prom_tce_alloc_end;
181
182 static unsigned long __initdata alloc_top;
183 static unsigned long __initdata alloc_top_high;
184 static unsigned long __initdata alloc_bottom;
185 static unsigned long __initdata rmo_top;
186 static unsigned long __initdata ram_top;
187
188 static struct mem_map_entry __initdata mem_reserve_map[MEM_RESERVE_MAP_SIZE];
189 static int __initdata mem_reserve_cnt;
190
191 static cell_t __initdata regbuf[1024];
192
193
194 #define MAX_CPU_THREADS 2
195
196 /* TO GO */
197 #ifdef CONFIG_HMT
198 struct {
199         unsigned int pir;
200         unsigned int threadid;
201 } hmt_thread_data[NR_CPUS];
202 #endif /* CONFIG_HMT */
203
204 /*
205  * This are used in calls to call_prom.  The 4th and following
206  * arguments to call_prom should be 32-bit values.  64 bit values
207  * are truncated to 32 bits (and fortunately don't get interpreted
208  * as two arguments).
209  */
210 #define ADDR(x)         (u32) ((unsigned long)(x) - offset)
211
212 /*
213  * Error results ... some OF calls will return "-1" on error, some
214  * will return 0, some will return either. To simplify, here are
215  * macros to use with any ihandle or phandle return value to check if
216  * it is valid
217  */
218
219 #define PROM_ERROR              (-1u)
220 #define PHANDLE_VALID(p)        ((p) != 0 && (p) != PROM_ERROR)
221 #define IHANDLE_VALID(i)        ((i) != 0 && (i) != PROM_ERROR)
222
223
224 /* This is the one and *ONLY* place where we actually call open
225  * firmware from, since we need to make sure we're running in 32b
226  * mode when we do.  We switch back to 64b mode upon return.
227  */
228
229 static int __init call_prom(const char *service, int nargs, int nret, ...)
230 {
231         int i;
232         unsigned long offset = reloc_offset();
233         struct prom_t *_prom = PTRRELOC(&prom);
234         va_list list;
235
236         _prom->args.service = ADDR(service);
237         _prom->args.nargs = nargs;
238         _prom->args.nret = nret;
239         _prom->args.rets = (prom_arg_t *)&(_prom->args.args[nargs]);
240
241         va_start(list, nret);
242         for (i=0; i < nargs; i++)
243                 _prom->args.args[i] = va_arg(list, prom_arg_t);
244         va_end(list);
245
246         for (i=0; i < nret ;i++)
247                 _prom->args.rets[i] = 0;
248
249         enter_prom(&_prom->args, _prom->entry);
250
251         return (nret > 0) ? _prom->args.rets[0] : 0;
252 }
253
254
255 static unsigned int __init prom_claim(unsigned long virt, unsigned long size,
256                                 unsigned long align)
257 {
258         return (unsigned int)call_prom("claim", 3, 1,
259                                        (prom_arg_t)virt, (prom_arg_t)size,
260                                        (prom_arg_t)align);
261 }
262
263 static void __init prom_print(const char *msg)
264 {
265         const char *p, *q;
266         unsigned long offset = reloc_offset();
267         struct prom_t *_prom = PTRRELOC(&prom);
268
269         if (_prom->stdout == 0)
270                 return;
271
272         for (p = msg; *p != 0; p = q) {
273                 for (q = p; *q != 0 && *q != '\n'; ++q)
274                         ;
275                 if (q > p)
276                         call_prom("write", 3, 1, _prom->stdout, p, q - p);
277                 if (*q == 0)
278                         break;
279                 ++q;
280                 call_prom("write", 3, 1, _prom->stdout, ADDR("\r\n"), 2);
281         }
282 }
283
284
285 static void __init prom_print_hex(unsigned long val)
286 {
287         unsigned long offset = reloc_offset();
288         int i, nibbles = sizeof(val)*2;
289         char buf[sizeof(val)*2+1];
290         struct prom_t *_prom = PTRRELOC(&prom);
291
292         for (i = nibbles-1;  i >= 0;  i--) {
293                 buf[i] = (val & 0xf) + '0';
294                 if (buf[i] > '9')
295                         buf[i] += ('a'-'0'-10);
296                 val >>= 4;
297         }
298         buf[nibbles] = '\0';
299         call_prom("write", 3, 1, _prom->stdout, buf, nibbles);
300 }
301
302
303 static void __init prom_printf(const char *format, ...)
304 {
305         unsigned long offset = reloc_offset();
306         const char *p, *q, *s;
307         va_list args;
308         unsigned long v;
309         struct prom_t *_prom = PTRRELOC(&prom);
310
311         va_start(args, format);
312         for (p = PTRRELOC(format); *p != 0; p = q) {
313                 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
314                         ;
315                 if (q > p)
316                         call_prom("write", 3, 1, _prom->stdout, p, q - p);
317                 if (*q == 0)
318                         break;
319                 if (*q == '\n') {
320                         ++q;
321                         call_prom("write", 3, 1, _prom->stdout,
322                                   ADDR("\r\n"), 2);
323                         continue;
324                 }
325                 ++q;
326                 if (*q == 0)
327                         break;
328                 switch (*q) {
329                 case 's':
330                         ++q;
331                         s = va_arg(args, const char *);
332                         prom_print(s);
333                         break;
334                 case 'x':
335                         ++q;
336                         v = va_arg(args, unsigned long);
337                         prom_print_hex(v);
338                         break;
339                 }
340         }
341 }
342
343
344 static void __init __attribute__((noreturn)) prom_panic(const char *reason)
345 {
346         unsigned long offset = reloc_offset();
347
348         prom_print(PTRRELOC(reason));
349         /* ToDo: should put up an SRC here */
350         call_prom("exit", 0, 0);
351
352         for (;;)                        /* should never get here */
353                 ;
354 }
355
356
357 static int __init prom_next_node(phandle *nodep)
358 {
359         phandle node;
360
361         if ((node = *nodep) != 0
362             && (*nodep = call_prom("child", 1, 1, node)) != 0)
363                 return 1;
364         if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
365                 return 1;
366         for (;;) {
367                 if ((node = call_prom("parent", 1, 1, node)) == 0)
368                         return 0;
369                 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
370                         return 1;
371         }
372 }
373
374 static int __init prom_getprop(phandle node, const char *pname,
375                                void *value, size_t valuelen)
376 {
377         unsigned long offset = reloc_offset();
378
379         return call_prom("getprop", 4, 1, node, ADDR(pname),
380                          (u32)(unsigned long) value, (u32) valuelen);
381 }
382
383 static int __init prom_getproplen(phandle node, const char *pname)
384 {
385         unsigned long offset = reloc_offset();
386
387         return call_prom("getproplen", 2, 1, node, ADDR(pname));
388 }
389
390 static int __init prom_setprop(phandle node, const char *pname,
391                                void *value, size_t valuelen)
392 {
393         unsigned long offset = reloc_offset();
394
395         return call_prom("setprop", 4, 1, node, ADDR(pname),
396                          (u32)(unsigned long) value, (u32) valuelen);
397 }
398
399 /* We can't use the standard versions because of RELOC headaches. */
400 #define isxdigit(c)     (('0' <= (c) && (c) <= '9') \
401                          || ('a' <= (c) && (c) <= 'f') \
402                          || ('A' <= (c) && (c) <= 'F'))
403
404 #define isdigit(c)      ('0' <= (c) && (c) <= '9')
405 #define islower(c)      ('a' <= (c) && (c) <= 'z')
406 #define toupper(c)      (islower(c) ? ((c) - 'a' + 'A') : (c))
407
408 unsigned long prom_strtoul(const char *cp, const char **endp)
409 {
410         unsigned long result = 0, base = 10, value;
411
412         if (*cp == '0') {
413                 base = 8;
414                 cp++;
415                 if (toupper(*cp) == 'X') {
416                         cp++;
417                         base = 16;
418                 }
419         }
420
421         while (isxdigit(*cp) &&
422                (value = isdigit(*cp) ? *cp - '0' : toupper(*cp) - 'A' + 10) < base) {
423                 result = result * base + value;
424                 cp++;
425         }
426
427         if (endp)
428                 *endp = cp;
429
430         return result;
431 }
432
433 unsigned long prom_memparse(const char *ptr, const char **retptr)
434 {
435         unsigned long ret = prom_strtoul(ptr, retptr);
436         int shift = 0;
437
438         /*
439          * We can't use a switch here because GCC *may* generate a
440          * jump table which won't work, because we're not running at
441          * the address we're linked at.
442          */
443         if ('G' == **retptr || 'g' == **retptr)
444                 shift = 30;
445
446         if ('M' == **retptr || 'm' == **retptr)
447                 shift = 20;
448
449         if ('K' == **retptr || 'k' == **retptr)
450                 shift = 10;
451
452         if (shift) {
453                 ret <<= shift;
454                 (*retptr)++;
455         }
456
457         return ret;
458 }
459
460 /*
461  * Early parsing of the command line passed to the kernel, used for
462  * "mem=x" and the options that affect the iommu
463  */
464 static void __init early_cmdline_parse(void)
465 {
466         unsigned long offset = reloc_offset();
467         struct prom_t *_prom = PTRRELOC(&prom);
468         char *opt, *p;
469         int l = 0;
470
471         RELOC(prom_cmd_line[0]) = 0;
472         p = RELOC(prom_cmd_line);
473         if ((long)_prom->chosen > 0)
474                 l = prom_getprop(_prom->chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
475 #ifdef CONFIG_CMDLINE
476         if (l == 0) /* dbl check */
477                 strlcpy(RELOC(prom_cmd_line),
478                         RELOC(CONFIG_CMDLINE), sizeof(prom_cmd_line));
479 #endif /* CONFIG_CMDLINE */
480         prom_printf("command line: %s\n", RELOC(prom_cmd_line));
481
482         opt = strstr(RELOC(prom_cmd_line), RELOC("iommu="));
483         if (opt) {
484                 prom_printf("iommu opt is: %s\n", opt);
485                 opt += 6;
486                 while (*opt && *opt == ' ')
487                         opt++;
488                 if (!strncmp(opt, RELOC("off"), 3))
489                         RELOC(ppc64_iommu_off) = 1;
490                 else if (!strncmp(opt, RELOC("force"), 5))
491                         RELOC(iommu_force_on) = 1;
492         }
493
494         opt = strstr(RELOC(prom_cmd_line), RELOC("mem="));
495         if (opt) {
496                 opt += 4;
497                 RELOC(prom_memory_limit) = prom_memparse(opt, (const char **)&opt);
498                 /* Align to 16 MB == size of large page */
499                 RELOC(prom_memory_limit) = ALIGN(RELOC(prom_memory_limit), 0x1000000);
500         }
501 }
502
503 /*
504  * To tell the firmware what our capabilities are, we have to pass
505  * it a fake 32-bit ELF header containing a couple of PT_NOTE sections
506  * that contain structures that contain the actual values.
507  */
508 static struct fake_elf {
509         Elf32_Ehdr      elfhdr;
510         Elf32_Phdr      phdr[2];
511         struct chrpnote {
512                 u32     namesz;
513                 u32     descsz;
514                 u32     type;
515                 char    name[8];        /* "PowerPC" */
516                 struct chrpdesc {
517                         u32     real_mode;
518                         u32     real_base;
519                         u32     real_size;
520                         u32     virt_base;
521                         u32     virt_size;
522                         u32     load_base;
523                 } chrpdesc;
524         } chrpnote;
525         struct rpanote {
526                 u32     namesz;
527                 u32     descsz;
528                 u32     type;
529                 char    name[24];       /* "IBM,RPA-Client-Config" */
530                 struct rpadesc {
531                         u32     lpar_affinity;
532                         u32     min_rmo_size;
533                         u32     min_rmo_percent;
534                         u32     max_pft_size;
535                         u32     splpar;
536                         u32     min_load;
537                         u32     new_mem_def;
538                         u32     ignore_me;
539                 } rpadesc;
540         } rpanote;
541 } fake_elf = {
542         .elfhdr = {
543                 .e_ident = { 0x7f, 'E', 'L', 'F',
544                              ELFCLASS32, ELFDATA2MSB, EV_CURRENT },
545                 .e_type = ET_EXEC,      /* yeah right */
546                 .e_machine = EM_PPC,
547                 .e_version = EV_CURRENT,
548                 .e_phoff = offsetof(struct fake_elf, phdr),
549                 .e_phentsize = sizeof(Elf32_Phdr),
550                 .e_phnum = 2
551         },
552         .phdr = {
553                 [0] = {
554                         .p_type = PT_NOTE,
555                         .p_offset = offsetof(struct fake_elf, chrpnote),
556                         .p_filesz = sizeof(struct chrpnote)
557                 }, [1] = {
558                         .p_type = PT_NOTE,
559                         .p_offset = offsetof(struct fake_elf, rpanote),
560                         .p_filesz = sizeof(struct rpanote)
561                 }
562         },
563         .chrpnote = {
564                 .namesz = sizeof("PowerPC"),
565                 .descsz = sizeof(struct chrpdesc),
566                 .type = 0x1275,
567                 .name = "PowerPC",
568                 .chrpdesc = {
569                         .real_mode = ~0U,       /* ~0 means "don't care" */
570                         .real_base = ~0U,
571                         .real_size = ~0U,
572                         .virt_base = ~0U,
573                         .virt_size = ~0U,
574                         .load_base = ~0U
575                 },
576         },
577         .rpanote = {
578                 .namesz = sizeof("IBM,RPA-Client-Config"),
579                 .descsz = sizeof(struct rpadesc),
580                 .type = 0x12759999,
581                 .name = "IBM,RPA-Client-Config",
582                 .rpadesc = {
583                         .lpar_affinity = 0,
584                         .min_rmo_size = 64,     /* in megabytes */
585                         .min_rmo_percent = 0,
586                         .max_pft_size = 48,     /* 2^48 bytes max PFT size */
587                         .splpar = 1,
588                         .min_load = ~0U,
589                         .new_mem_def = 0
590                 }
591         }
592 };
593
594 static void __init prom_send_capabilities(void)
595 {
596         unsigned long offset = reloc_offset();
597         ihandle elfloader;
598
599         elfloader = call_prom("open", 1, 1, ADDR("/packages/elf-loader"));
600         if (elfloader == 0) {
601                 prom_printf("couldn't open /packages/elf-loader\n");
602                 return;
603         }
604         call_prom("call-method", 3, 1, ADDR("process-elf-header"),
605                         elfloader, ADDR(&fake_elf));
606         call_prom("close", 1, 0, elfloader);
607 }
608
609 /*
610  * Memory allocation strategy... our layout is normally:
611  *
612  *  at 14Mb or more we vmlinux, then a gap and initrd. In some rare cases, initrd
613  *  might end up beeing before the kernel though. We assume this won't override
614  *  the final kernel at 0, we have no provision to handle that in this version,
615  *  but it should hopefully never happen.
616  *
617  *  alloc_top is set to the top of RMO, eventually shrink down if the TCEs overlap
618  *  alloc_bottom is set to the top of kernel/initrd
619  *
620  *  from there, allocations are done that way : rtas is allocated topmost, and
621  *  the device-tree is allocated from the bottom. We try to grow the device-tree
622  *  allocation as we progress. If we can't, then we fail, we don't currently have
623  *  a facility to restart elsewhere, but that shouldn't be necessary neither
624  *
625  *  Note that calls to reserve_mem have to be done explicitely, memory allocated
626  *  with either alloc_up or alloc_down isn't automatically reserved.
627  */
628
629
630 /*
631  * Allocates memory in the RMO upward from the kernel/initrd
632  *
633  * When align is 0, this is a special case, it means to allocate in place
634  * at the current location of alloc_bottom or fail (that is basically
635  * extending the previous allocation). Used for the device-tree flattening
636  */
637 static unsigned long __init alloc_up(unsigned long size, unsigned long align)
638 {
639         unsigned long offset = reloc_offset();
640         unsigned long base = _ALIGN_UP(RELOC(alloc_bottom), align);
641         unsigned long addr = 0;
642
643         prom_debug("alloc_up(%x, %x)\n", size, align);
644         if (RELOC(ram_top) == 0)
645                 prom_panic("alloc_up() called with mem not initialized\n");
646
647         if (align)
648                 base = _ALIGN_UP(RELOC(alloc_bottom), align);
649         else
650                 base = RELOC(alloc_bottom);
651
652         for(; (base + size) <= RELOC(alloc_top); 
653             base = _ALIGN_UP(base + 0x100000, align)) {
654                 prom_debug("    trying: 0x%x\n\r", base);
655                 addr = (unsigned long)prom_claim(base, size, 0);
656                 if (addr != PROM_ERROR)
657                         break;
658                 addr = 0;
659                 if (align == 0)
660                         break;
661         }
662         if (addr == 0)
663                 return 0;
664         RELOC(alloc_bottom) = addr;
665
666         prom_debug(" -> %x\n", addr);
667         prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
668         prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
669         prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
670         prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
671         prom_debug("  ram_top      : %x\n", RELOC(ram_top));
672
673         return addr;
674 }
675
676 /*
677  * Allocates memory downard, either from top of RMO, or if highmem
678  * is set, from the top of RAM. Note that this one doesn't handle
679  * failures. In does claim memory if highmem is not set.
680  */
681 static unsigned long __init alloc_down(unsigned long size, unsigned long align,
682                                        int highmem)
683 {
684         unsigned long offset = reloc_offset();
685         unsigned long base, addr = 0;
686
687         prom_debug("alloc_down(%x, %x, %s)\n", size, align,
688                    highmem ? RELOC("(high)") : RELOC("(low)"));
689         if (RELOC(ram_top) == 0)
690                 prom_panic("alloc_down() called with mem not initialized\n");
691
692         if (highmem) {
693                 /* Carve out storage for the TCE table. */
694                 addr = _ALIGN_DOWN(RELOC(alloc_top_high) - size, align);
695                 if (addr <= RELOC(alloc_bottom))
696                         return 0;
697                 else {
698                         /* Will we bump into the RMO ? If yes, check out that we
699                          * didn't overlap existing allocations there, if we did,
700                          * we are dead, we must be the first in town !
701                          */
702                         if (addr < RELOC(rmo_top)) {
703                                 /* Good, we are first */
704                                 if (RELOC(alloc_top) == RELOC(rmo_top))
705                                         RELOC(alloc_top) = RELOC(rmo_top) = addr;
706                                 else
707                                         return 0;
708                         }
709                         RELOC(alloc_top_high) = addr;
710                 }
711                 goto bail;
712         }
713
714         base = _ALIGN_DOWN(RELOC(alloc_top) - size, align);
715         for(; base > RELOC(alloc_bottom); base = _ALIGN_DOWN(base - 0x100000, align))  {
716                 prom_debug("    trying: 0x%x\n\r", base);
717                 addr = (unsigned long)prom_claim(base, size, 0);
718                 if (addr != PROM_ERROR)
719                         break;
720                 addr = 0;
721         }
722         if (addr == 0)
723                 return 0;
724         RELOC(alloc_top) = addr;
725
726  bail:
727         prom_debug(" -> %x\n", addr);
728         prom_debug("  alloc_bottom : %x\n", RELOC(alloc_bottom));
729         prom_debug("  alloc_top    : %x\n", RELOC(alloc_top));
730         prom_debug("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
731         prom_debug("  rmo_top      : %x\n", RELOC(rmo_top));
732         prom_debug("  ram_top      : %x\n", RELOC(ram_top));
733
734         return addr;
735 }
736
737 /*
738  * Parse a "reg" cell
739  */
740 static unsigned long __init prom_next_cell(int s, cell_t **cellp)
741 {
742         cell_t *p = *cellp;
743         unsigned long r = 0;
744
745         /* Ignore more than 2 cells */
746         while (s > 2) {
747                 p++;
748                 s--;
749         }
750         while (s) {
751                 r <<= 32;
752                 r |= *(p++);
753                 s--;
754         }
755
756         *cellp = p;
757         return r;
758 }
759
760 /*
761  * Very dumb function for adding to the memory reserve list, but
762  * we don't need anything smarter at this point
763  *
764  * XXX Eventually check for collisions. They should NEVER happen
765  * if problems seem to show up, it would be a good start to track
766  * them down.
767  */
768 static void reserve_mem(unsigned long base, unsigned long size)
769 {
770         unsigned long offset = reloc_offset();
771         unsigned long top = base + size;
772         unsigned long cnt = RELOC(mem_reserve_cnt);
773
774         if (size == 0)
775                 return;
776
777         /* We need to always keep one empty entry so that we
778          * have our terminator with "size" set to 0 since we are
779          * dumb and just copy this entire array to the boot params
780          */
781         base = _ALIGN_DOWN(base, PAGE_SIZE);
782         top = _ALIGN_UP(top, PAGE_SIZE);
783         size = top - base;
784
785         if (cnt >= (MEM_RESERVE_MAP_SIZE - 1))
786                 prom_panic("Memory reserve map exhausted !\n");
787         RELOC(mem_reserve_map)[cnt].base = base;
788         RELOC(mem_reserve_map)[cnt].size = size;
789         RELOC(mem_reserve_cnt) = cnt + 1;
790 }
791
792 /*
793  * Initialize memory allocation mecanism, parse "memory" nodes and
794  * obtain that way the top of memory and RMO to setup out local allocator
795  */
796 static void __init prom_init_mem(void)
797 {
798         phandle node;
799         char *path, type[64];
800         unsigned int plen;
801         cell_t *p, *endp;
802         unsigned long offset = reloc_offset();
803         struct prom_t *_prom = PTRRELOC(&prom);
804
805         /*
806          * We iterate the memory nodes to find
807          * 1) top of RMO (first node)
808          * 2) top of memory
809          */
810         prom_debug("root_addr_cells: %x\n", (long)_prom->root_addr_cells);
811         prom_debug("root_size_cells: %x\n", (long)_prom->root_size_cells);
812
813         prom_debug("scanning memory:\n");
814         path = RELOC(prom_scratch);
815
816         for (node = 0; prom_next_node(&node); ) {
817                 type[0] = 0;
818                 prom_getprop(node, "device_type", type, sizeof(type));
819
820                 if (strcmp(type, RELOC("memory")))
821                         continue;
822         
823                 plen = prom_getprop(node, "reg", RELOC(regbuf), sizeof(regbuf));
824                 if (plen > sizeof(regbuf)) {
825                         prom_printf("memory node too large for buffer !\n");
826                         plen = sizeof(regbuf);
827                 }
828                 p = RELOC(regbuf);
829                 endp = p + (plen / sizeof(cell_t));
830
831 #ifdef DEBUG_PROM
832                 memset(path, 0, PROM_SCRATCH_SIZE);
833                 call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
834                 prom_debug("  node %s :\n", path);
835 #endif /* DEBUG_PROM */
836
837                 while ((endp - p) >= (_prom->root_addr_cells + _prom->root_size_cells)) {
838                         unsigned long base, size;
839
840                         base = prom_next_cell(_prom->root_addr_cells, &p);
841                         size = prom_next_cell(_prom->root_size_cells, &p);
842
843                         if (size == 0)
844                                 continue;
845                         prom_debug("    %x %x\n", base, size);
846                         if (base == 0)
847                                 RELOC(rmo_top) = size;
848                         if ((base + size) > RELOC(ram_top))
849                                 RELOC(ram_top) = base + size;
850                 }
851         }
852
853         RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(klimit) - offset + 0x4000);
854
855         /* Check if we have an initrd after the kernel, if we do move our bottom
856          * point to after it
857          */
858         if (RELOC(prom_initrd_start)) {
859                 if (RELOC(prom_initrd_end) > RELOC(alloc_bottom))
860                         RELOC(alloc_bottom) = PAGE_ALIGN(RELOC(prom_initrd_end));
861         }
862
863         /*
864          * If prom_memory_limit is set we reduce the upper limits *except* for
865          * alloc_top_high. This must be the real top of RAM so we can put
866          * TCE's up there.
867          */
868
869         RELOC(alloc_top_high) = RELOC(ram_top);
870
871         if (RELOC(prom_memory_limit)) {
872                 if (RELOC(prom_memory_limit) <= RELOC(alloc_bottom)) {
873                         prom_printf("Ignoring mem=%x <= alloc_bottom.\n",
874                                 RELOC(prom_memory_limit));
875                         RELOC(prom_memory_limit) = 0;
876                 } else if (RELOC(prom_memory_limit) >= RELOC(ram_top)) {
877                         prom_printf("Ignoring mem=%x >= ram_top.\n",
878                                 RELOC(prom_memory_limit));
879                         RELOC(prom_memory_limit) = 0;
880                 } else {
881                         RELOC(ram_top) = RELOC(prom_memory_limit);
882                         RELOC(rmo_top) = min(RELOC(rmo_top), RELOC(prom_memory_limit));
883                 }
884         }
885
886         /*
887          * Setup our top alloc point, that is top of RMO or top of
888          * segment 0 when running non-LPAR.
889          */
890         if ( RELOC(of_platform) == PLATFORM_PSERIES_LPAR )
891                 RELOC(alloc_top) = RELOC(rmo_top);
892         else
893                 /* Some RS64 machines have buggy firmware where claims up at 1GB
894                  * fails. Cap at 768MB as a workaround. Still plenty of room.
895                  */
896                 RELOC(alloc_top) = RELOC(rmo_top) = min(0x30000000ul, RELOC(ram_top));
897
898         prom_printf("memory layout at init:\n");
899         prom_printf("  memory_limit : %x (16 MB aligned)\n", RELOC(prom_memory_limit));
900         prom_printf("  alloc_bottom : %x\n", RELOC(alloc_bottom));
901         prom_printf("  alloc_top    : %x\n", RELOC(alloc_top));
902         prom_printf("  alloc_top_hi : %x\n", RELOC(alloc_top_high));
903         prom_printf("  rmo_top      : %x\n", RELOC(rmo_top));
904         prom_printf("  ram_top      : %x\n", RELOC(ram_top));
905 }
906
907
908 /*
909  * Allocate room for and instanciate RTAS
910  */
911 static void __init prom_instantiate_rtas(void)
912 {
913         unsigned long offset = reloc_offset();
914         struct prom_t *_prom = PTRRELOC(&prom);
915         phandle rtas_node;
916         ihandle rtas_inst;
917         u32 base, entry = 0;
918         u32 size = 0;
919
920         prom_debug("prom_instantiate_rtas: start...\n");
921
922         rtas_node = call_prom("finddevice", 1, 1, ADDR("/rtas"));
923         prom_debug("rtas_node: %x\n", rtas_node);
924         if (!PHANDLE_VALID(rtas_node))
925                 return;
926
927         prom_getprop(rtas_node, "rtas-size", &size, sizeof(size));
928         if (size == 0)
929                 return;
930
931         base = alloc_down(size, PAGE_SIZE, 0);
932         if (base == 0) {
933                 prom_printf("RTAS allocation failed !\n");
934                 return;
935         }
936
937         rtas_inst = call_prom("open", 1, 1, ADDR("/rtas"));
938         if (!IHANDLE_VALID(rtas_inst)) {
939                 prom_printf("opening rtas package failed");
940                 return;
941         }
942
943         prom_printf("instantiating rtas at 0x%x ...", base);
944
945         if (call_prom("call-method", 3, 2,
946                       ADDR("instantiate-rtas"),
947                       rtas_inst, base) != PROM_ERROR) {
948                 entry = (long)_prom->args.rets[1];
949         }
950         if (entry == 0) {
951                 prom_printf(" failed\n");
952                 return;
953         }
954         prom_printf(" done\n");
955
956         reserve_mem(base, size);
957
958         prom_setprop(rtas_node, "linux,rtas-base", &base, sizeof(base));
959         prom_setprop(rtas_node, "linux,rtas-entry", &entry, sizeof(entry));
960
961         prom_debug("rtas base     = 0x%x\n", base);
962         prom_debug("rtas entry    = 0x%x\n", entry);
963         prom_debug("rtas size     = 0x%x\n", (long)size);
964
965         prom_debug("prom_instantiate_rtas: end...\n");
966 }
967
968
969 /*
970  * Allocate room for and initialize TCE tables
971  */
972 static void __init prom_initialize_tce_table(void)
973 {
974         phandle node;
975         ihandle phb_node;
976         unsigned long offset = reloc_offset();
977         char compatible[64], type[64], model[64];
978         char *path = RELOC(prom_scratch);
979         u64 base, align;
980         u32 minalign, minsize;
981         u64 tce_entry, *tce_entryp;
982         u64 local_alloc_top, local_alloc_bottom;
983         u64 i;
984
985         if (RELOC(ppc64_iommu_off))
986                 return;
987
988         prom_debug("starting prom_initialize_tce_table\n");
989
990         /* Cache current top of allocs so we reserve a single block */
991         local_alloc_top = RELOC(alloc_top_high);
992         local_alloc_bottom = local_alloc_top;
993
994         /* Search all nodes looking for PHBs. */
995         for (node = 0; prom_next_node(&node); ) {
996                 compatible[0] = 0;
997                 type[0] = 0;
998                 model[0] = 0;
999                 prom_getprop(node, "compatible",
1000                              compatible, sizeof(compatible));
1001                 prom_getprop(node, "device_type", type, sizeof(type));
1002                 prom_getprop(node, "model", model, sizeof(model));
1003
1004                 if ((type[0] == 0) || (strstr(type, RELOC("pci")) == NULL))
1005                         continue;
1006
1007                 /* Keep the old logic in tack to avoid regression. */
1008                 if (compatible[0] != 0) {
1009                         if ((strstr(compatible, RELOC("python")) == NULL) &&
1010                             (strstr(compatible, RELOC("Speedwagon")) == NULL) &&
1011                             (strstr(compatible, RELOC("Winnipeg")) == NULL))
1012                                 continue;
1013                 } else if (model[0] != 0) {
1014                         if ((strstr(model, RELOC("ython")) == NULL) &&
1015                             (strstr(model, RELOC("peedwagon")) == NULL) &&
1016                             (strstr(model, RELOC("innipeg")) == NULL))
1017                                 continue;
1018                 }
1019
1020                 if (prom_getprop(node, "tce-table-minalign", &minalign,
1021                                  sizeof(minalign)) == PROM_ERROR)
1022                         minalign = 0;
1023                 if (prom_getprop(node, "tce-table-minsize", &minsize,
1024                                  sizeof(minsize)) == PROM_ERROR)
1025                         minsize = 4UL << 20;
1026
1027                 /*
1028                  * Even though we read what OF wants, we just set the table
1029                  * size to 4 MB.  This is enough to map 2GB of PCI DMA space.
1030                  * By doing this, we avoid the pitfalls of trying to DMA to
1031                  * MMIO space and the DMA alias hole.
1032                  *
1033                  * On POWER4, firmware sets the TCE region by assuming
1034                  * each TCE table is 8MB. Using this memory for anything
1035                  * else will impact performance, so we always allocate 8MB.
1036                  * Anton
1037                  */
1038                 if (__is_processor(PV_POWER4) || __is_processor(PV_POWER4p))
1039                         minsize = 8UL << 20;
1040                 else
1041                         minsize = 4UL << 20;
1042
1043                 /* Align to the greater of the align or size */
1044                 align = max(minalign, minsize);
1045                 base = alloc_down(minsize, align, 1);
1046                 if (base == 0)
1047                         prom_panic("ERROR, cannot find space for TCE table.\n");
1048                 if (base < local_alloc_bottom)
1049                         local_alloc_bottom = base;
1050
1051                 /* Save away the TCE table attributes for later use. */
1052                 prom_setprop(node, "linux,tce-base", &base, sizeof(base));
1053                 prom_setprop(node, "linux,tce-size", &minsize, sizeof(minsize));
1054
1055                 /* It seems OF doesn't null-terminate the path :-( */
1056                 memset(path, 0, sizeof(path));
1057                 /* Call OF to setup the TCE hardware */
1058                 if (call_prom("package-to-path", 3, 1, node,
1059                               path, PROM_SCRATCH_SIZE-1) == PROM_ERROR) {
1060                         prom_printf("package-to-path failed\n");
1061                 }
1062
1063                 prom_debug("TCE table: %s\n", path);
1064                 prom_debug("\tnode = 0x%x\n", node);
1065                 prom_debug("\tbase = 0x%x\n", base);
1066                 prom_debug("\tsize = 0x%x\n", minsize);
1067
1068                 /* Initialize the table to have a one-to-one mapping
1069                  * over the allocated size.
1070                  */
1071                 tce_entryp = (unsigned long *)base;
1072                 for (i = 0; i < (minsize >> 3) ;tce_entryp++, i++) {
1073                         tce_entry = (i << PAGE_SHIFT);
1074                         tce_entry |= 0x3;
1075                         *tce_entryp = tce_entry;
1076                 }
1077
1078                 prom_printf("opening PHB %s", path);
1079                 phb_node = call_prom("open", 1, 1, path);
1080                 if (phb_node == 0)
1081                         prom_printf("... failed\n");
1082                 else
1083                         prom_printf("... done\n");
1084
1085                 call_prom("call-method", 6, 0, ADDR("set-64-bit-addressing"),
1086                           phb_node, -1, minsize,
1087                           (u32) base, (u32) (base >> 32));
1088                 call_prom("close", 1, 0, phb_node);
1089         }
1090
1091         reserve_mem(local_alloc_bottom, local_alloc_top - local_alloc_bottom);
1092
1093         if (RELOC(prom_memory_limit)) {
1094                 /*
1095                  * We align the start to a 16MB boundary so we can map the TCE area
1096                  * using large pages if possible. The end should be the top of RAM
1097                  * so no need to align it.
1098                  */
1099                 RELOC(prom_tce_alloc_start) = _ALIGN_DOWN(local_alloc_bottom, 0x1000000);
1100                 RELOC(prom_tce_alloc_end) = local_alloc_top;
1101         }
1102
1103         /* Flag the first invalid entry */
1104         prom_debug("ending prom_initialize_tce_table\n");
1105 }
1106
1107 /*
1108  * With CHRP SMP we need to use the OF to start the other
1109  * processors so we can't wait until smp_boot_cpus (the OF is
1110  * trashed by then) so we have to put the processors into
1111  * a holding pattern controlled by the kernel (not OF) before
1112  * we destroy the OF.
1113  *
1114  * This uses a chunk of low memory, puts some holding pattern
1115  * code there and sends the other processors off to there until
1116  * smp_boot_cpus tells them to do something.  The holding pattern
1117  * checks that address until its cpu # is there, when it is that
1118  * cpu jumps to __secondary_start().  smp_boot_cpus() takes care
1119  * of setting those values.
1120  *
1121  * We also use physical address 0x4 here to tell when a cpu
1122  * is in its holding pattern code.
1123  *
1124  * Fixup comment... DRENG / PPPBBB - Peter
1125  *
1126  * -- Cort
1127  */
1128 static void __init prom_hold_cpus(void)
1129 {
1130         unsigned long i;
1131         unsigned int reg;
1132         phandle node;
1133         unsigned long offset = reloc_offset();
1134         char type[64];
1135         int cpuid = 0;
1136         unsigned int interrupt_server[MAX_CPU_THREADS];
1137         unsigned int cpu_threads, hw_cpu_num;
1138         int propsize;
1139         extern void __secondary_hold(void);
1140         extern unsigned long __secondary_hold_spinloop;
1141         extern unsigned long __secondary_hold_acknowledge;
1142         unsigned long *spinloop
1143                 = (void *)virt_to_abs(&__secondary_hold_spinloop);
1144         unsigned long *acknowledge
1145                 = (void *)virt_to_abs(&__secondary_hold_acknowledge);
1146         unsigned long secondary_hold
1147                 = virt_to_abs(*PTRRELOC((unsigned long *)__secondary_hold));
1148         struct prom_t *_prom = PTRRELOC(&prom);
1149
1150         prom_debug("prom_hold_cpus: start...\n");
1151         prom_debug("    1) spinloop       = 0x%x\n", (unsigned long)spinloop);
1152         prom_debug("    1) *spinloop      = 0x%x\n", *spinloop);
1153         prom_debug("    1) acknowledge    = 0x%x\n",
1154                    (unsigned long)acknowledge);
1155         prom_debug("    1) *acknowledge   = 0x%x\n", *acknowledge);
1156         prom_debug("    1) secondary_hold = 0x%x\n", secondary_hold);
1157
1158         /* Set the common spinloop variable, so all of the secondary cpus
1159          * will block when they are awakened from their OF spinloop.
1160          * This must occur for both SMP and non SMP kernels, since OF will
1161          * be trashed when we move the kernel.
1162          */
1163         *spinloop = 0;
1164
1165 #ifdef CONFIG_HMT
1166         for (i=0; i < NR_CPUS; i++) {
1167                 RELOC(hmt_thread_data)[i].pir = 0xdeadbeef;
1168         }
1169 #endif
1170         /* look for cpus */
1171         for (node = 0; prom_next_node(&node); ) {
1172                 type[0] = 0;
1173                 prom_getprop(node, "device_type", type, sizeof(type));
1174                 if (strcmp(type, RELOC("cpu")) != 0)
1175                         continue;
1176
1177                 /* Skip non-configured cpus. */
1178                 if (prom_getprop(node, "status", type, sizeof(type)) > 0)
1179                         if (strcmp(type, RELOC("okay")) != 0)
1180                                 continue;
1181
1182                 reg = -1;
1183                 prom_getprop(node, "reg", &reg, sizeof(reg));
1184
1185                 prom_debug("\ncpuid        = 0x%x\n", cpuid);
1186                 prom_debug("cpu hw idx   = 0x%x\n", reg);
1187
1188                 /* Init the acknowledge var which will be reset by
1189                  * the secondary cpu when it awakens from its OF
1190                  * spinloop.
1191                  */
1192                 *acknowledge = (unsigned long)-1;
1193
1194                 propsize = prom_getprop(node, "ibm,ppc-interrupt-server#s",
1195                                         &interrupt_server,
1196                                         sizeof(interrupt_server));
1197                 if (propsize < 0) {
1198                         /* no property.  old hardware has no SMT */
1199                         cpu_threads = 1;
1200                         interrupt_server[0] = reg; /* fake it with phys id */
1201                 } else {
1202                         /* We have a threaded processor */
1203                         cpu_threads = propsize / sizeof(u32);
1204                         if (cpu_threads > MAX_CPU_THREADS) {
1205                                 prom_printf("SMT: too many threads!\n"
1206                                             "SMT: found %x, max is %x\n",
1207                                             cpu_threads, MAX_CPU_THREADS);
1208                                 cpu_threads = 1; /* ToDo: panic? */
1209                         }
1210                 }
1211
1212                 hw_cpu_num = interrupt_server[0];
1213                 if (hw_cpu_num != _prom->cpu) {
1214                         /* Primary Thread of non-boot cpu */
1215                         prom_printf("%x : starting cpu hw idx %x... ", cpuid, reg);
1216                         call_prom("start-cpu", 3, 0, node,
1217                                   secondary_hold, reg);
1218
1219                         for ( i = 0 ; (i < 100000000) && 
1220                               (*acknowledge == ((unsigned long)-1)); i++ )
1221                                 mb();
1222
1223                         if (*acknowledge == reg) {
1224                                 prom_printf("done\n");
1225                                 /* We have to get every CPU out of OF,
1226                                  * even if we never start it. */
1227                                 if (cpuid >= NR_CPUS)
1228                                         goto next;
1229                         } else {
1230                                 prom_printf("failed: %x\n", *acknowledge);
1231                         }
1232                 }
1233 #ifdef CONFIG_SMP
1234                 else
1235                         prom_printf("%x : boot cpu     %x\n", cpuid, reg);
1236 #endif
1237 next:
1238 #ifdef CONFIG_SMP
1239                 /* Init paca for secondary threads.   They start later. */
1240                 for (i=1; i < cpu_threads; i++) {
1241                         cpuid++;
1242                         if (cpuid >= NR_CPUS)
1243                                 continue;
1244                 }
1245 #endif /* CONFIG_SMP */
1246                 cpuid++;
1247         }
1248 #ifdef CONFIG_HMT
1249         /* Only enable HMT on processors that provide support. */
1250         if (__is_processor(PV_PULSAR) || 
1251             __is_processor(PV_ICESTAR) ||
1252             __is_processor(PV_SSTAR)) {
1253                 prom_printf("    starting secondary threads\n");
1254
1255                 for (i = 0; i < NR_CPUS; i += 2) {
1256                         if (!cpu_online(i))
1257                                 continue;
1258
1259                         if (i == 0) {
1260                                 unsigned long pir = mfspr(SPRN_PIR);
1261                                 if (__is_processor(PV_PULSAR)) {
1262                                         RELOC(hmt_thread_data)[i].pir = 
1263                                                 pir & 0x1f;
1264                                 } else {
1265                                         RELOC(hmt_thread_data)[i].pir = 
1266                                                 pir & 0x3ff;
1267                                 }
1268                         }
1269                 }
1270         } else {
1271                 prom_printf("Processor is not HMT capable\n");
1272         }
1273 #endif
1274
1275         if (cpuid > NR_CPUS)
1276                 prom_printf("WARNING: maximum CPUs (" __stringify(NR_CPUS)
1277                             ") exceeded: ignoring extras\n");
1278
1279         prom_debug("prom_hold_cpus: end...\n");
1280 }
1281
1282
1283 static void __init prom_init_client_services(unsigned long pp)
1284 {
1285         unsigned long offset = reloc_offset();
1286         struct prom_t *_prom = PTRRELOC(&prom);
1287
1288         /* Get a handle to the prom entry point before anything else */
1289         _prom->entry = pp;
1290
1291         /* Init default value for phys size */
1292         _prom->root_size_cells = 1;
1293         _prom->root_addr_cells = 2;
1294
1295         /* get a handle for the stdout device */
1296         _prom->chosen = call_prom("finddevice", 1, 1, ADDR("/chosen"));
1297         if (!PHANDLE_VALID(_prom->chosen))
1298                 prom_panic("cannot find chosen"); /* msg won't be printed :( */
1299
1300         /* get device tree root */
1301         _prom->root = call_prom("finddevice", 1, 1, ADDR("/"));
1302         if (!PHANDLE_VALID(_prom->root))
1303                 prom_panic("cannot find device tree root"); /* msg won't be printed :( */
1304 }
1305
1306 static void __init prom_init_stdout(void)
1307 {
1308         unsigned long offset = reloc_offset();
1309         struct prom_t *_prom = PTRRELOC(&prom);
1310         char *path = RELOC(of_stdout_device);
1311         char type[16];
1312         u32 val;
1313
1314         if (prom_getprop(_prom->chosen, "stdout", &val, sizeof(val)) <= 0)
1315                 prom_panic("cannot find stdout");
1316
1317         _prom->stdout = val;
1318
1319         /* Get the full OF pathname of the stdout device */
1320         memset(path, 0, 256);
1321         call_prom("instance-to-path", 3, 1, _prom->stdout, path, 255);
1322         val = call_prom("instance-to-package", 1, 1, _prom->stdout);
1323         prom_setprop(_prom->chosen, "linux,stdout-package", &val, sizeof(val));
1324         prom_printf("OF stdout device is: %s\n", RELOC(of_stdout_device));
1325         prom_setprop(_prom->chosen, "linux,stdout-path",
1326                      RELOC(of_stdout_device), strlen(RELOC(of_stdout_device))+1);
1327
1328         /* If it's a display, note it */
1329         memset(type, 0, sizeof(type));
1330         prom_getprop(val, "device_type", type, sizeof(type));
1331         if (strcmp(type, RELOC("display")) == 0) {
1332                 _prom->disp_node = val;
1333                 prom_setprop(val, "linux,boot-display", NULL, 0);
1334         }
1335 }
1336
1337 static void __init prom_close_stdin(void)
1338 {
1339         unsigned long offset = reloc_offset();
1340         struct prom_t *_prom = PTRRELOC(&prom);
1341         ihandle val;
1342
1343         if (prom_getprop(_prom->chosen, "stdin", &val, sizeof(val)) > 0)
1344                 call_prom("close", 1, 0, val);
1345 }
1346
1347 static int __init prom_find_machine_type(void)
1348 {
1349         unsigned long offset = reloc_offset();
1350         struct prom_t *_prom = PTRRELOC(&prom);
1351         char compat[256];
1352         int len, i = 0;
1353         phandle rtas;
1354
1355         len = prom_getprop(_prom->root, "compatible",
1356                            compat, sizeof(compat)-1);
1357         if (len > 0) {
1358                 compat[len] = 0;
1359                 while (i < len) {
1360                         char *p = &compat[i];
1361                         int sl = strlen(p);
1362                         if (sl == 0)
1363                                 break;
1364                         if (strstr(p, RELOC("Power Macintosh")) ||
1365                             strstr(p, RELOC("MacRISC4")))
1366                                 return PLATFORM_POWERMAC;
1367                         if (strstr(p, RELOC("Momentum,Maple")))
1368                                 return PLATFORM_MAPLE;
1369                         i += sl + 1;
1370                 }
1371         }
1372         /* Default to pSeries. We need to know if we are running LPAR */
1373         rtas = call_prom("finddevice", 1, 1, ADDR("/rtas"));
1374         if (PHANDLE_VALID(rtas)) {
1375                 int x = prom_getproplen(rtas, "ibm,hypertas-functions");
1376                 if (x != PROM_ERROR) {
1377                         prom_printf("Hypertas detected, assuming LPAR !\n");
1378                         return PLATFORM_PSERIES_LPAR;
1379                 }
1380         }
1381         return PLATFORM_PSERIES;
1382 }
1383
1384 static int __init prom_set_color(ihandle ih, int i, int r, int g, int b)
1385 {
1386         unsigned long offset = reloc_offset();
1387
1388         return call_prom("call-method", 6, 1, ADDR("color!"), ih, i, b, g, r);
1389 }
1390
1391 /*
1392  * If we have a display that we don't know how to drive,
1393  * we will want to try to execute OF's open method for it
1394  * later.  However, OF will probably fall over if we do that
1395  * we've taken over the MMU.
1396  * So we check whether we will need to open the display,
1397  * and if so, open it now.
1398  */
1399 static void __init prom_check_displays(void)
1400 {
1401         unsigned long offset = reloc_offset();
1402         struct prom_t *_prom = PTRRELOC(&prom);
1403         char type[16], *path;
1404         phandle node;
1405         ihandle ih;
1406         int i;
1407
1408         static unsigned char default_colors[] = {
1409                 0x00, 0x00, 0x00,
1410                 0x00, 0x00, 0xaa,
1411                 0x00, 0xaa, 0x00,
1412                 0x00, 0xaa, 0xaa,
1413                 0xaa, 0x00, 0x00,
1414                 0xaa, 0x00, 0xaa,
1415                 0xaa, 0xaa, 0x00,
1416                 0xaa, 0xaa, 0xaa,
1417                 0x55, 0x55, 0x55,
1418                 0x55, 0x55, 0xff,
1419                 0x55, 0xff, 0x55,
1420                 0x55, 0xff, 0xff,
1421                 0xff, 0x55, 0x55,
1422                 0xff, 0x55, 0xff,
1423                 0xff, 0xff, 0x55,
1424                 0xff, 0xff, 0xff
1425         };
1426         const unsigned char *clut;
1427
1428         prom_printf("Looking for displays\n");
1429         for (node = 0; prom_next_node(&node); ) {
1430                 memset(type, 0, sizeof(type));
1431                 prom_getprop(node, "device_type", type, sizeof(type));
1432                 if (strcmp(type, RELOC("display")) != 0)
1433                         continue;
1434
1435                 /* It seems OF doesn't null-terminate the path :-( */
1436                 path = RELOC(prom_scratch);
1437                 memset(path, 0, PROM_SCRATCH_SIZE);
1438
1439                 /*
1440                  * leave some room at the end of the path for appending extra
1441                  * arguments
1442                  */
1443                 if (call_prom("package-to-path", 3, 1, node, path,
1444                               PROM_SCRATCH_SIZE-10) == PROM_ERROR)
1445                         continue;
1446                 prom_printf("found display   : %s, opening ... ", path);
1447                 
1448                 ih = call_prom("open", 1, 1, path);
1449                 if (ih == 0) {
1450                         prom_printf("failed\n");
1451                         continue;
1452                 }
1453
1454                 /* Success */
1455                 prom_printf("done\n");
1456                 prom_setprop(node, "linux,opened", NULL, 0);
1457
1458                 /*
1459                  * stdout wasn't a display node, pick the first we can find
1460                  * for btext
1461                  */
1462                 if (_prom->disp_node == 0)
1463                         _prom->disp_node = node;
1464
1465                 /* Setup a useable color table when the appropriate
1466                  * method is available. Should update this to set-colors */
1467                 clut = RELOC(default_colors);
1468                 for (i = 0; i < 32; i++, clut += 3)
1469                         if (prom_set_color(ih, i, clut[0], clut[1],
1470                                            clut[2]) != 0)
1471                                 break;
1472
1473 #ifdef CONFIG_LOGO_LINUX_CLUT224
1474                 clut = PTRRELOC(RELOC(logo_linux_clut224.clut));
1475                 for (i = 0; i < RELOC(logo_linux_clut224.clutsize); i++, clut += 3)
1476                         if (prom_set_color(ih, i + 32, clut[0], clut[1],
1477                                            clut[2]) != 0)
1478                                 break;
1479 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
1480         }
1481 }
1482
1483
1484 /* Return (relocated) pointer to this much memory: moves initrd if reqd. */
1485 static void __init *make_room(unsigned long *mem_start, unsigned long *mem_end,
1486                               unsigned long needed, unsigned long align)
1487 {
1488         unsigned long offset = reloc_offset();
1489         void *ret;
1490
1491         *mem_start = _ALIGN(*mem_start, align);
1492         while ((*mem_start + needed) > *mem_end) {
1493                 unsigned long room, chunk;
1494
1495                 prom_debug("Chunk exhausted, claiming more at %x...\n",
1496                            RELOC(alloc_bottom));
1497                 room = RELOC(alloc_top) - RELOC(alloc_bottom);
1498                 if (room > DEVTREE_CHUNK_SIZE)
1499                         room = DEVTREE_CHUNK_SIZE;
1500                 if (room < PAGE_SIZE)
1501                         prom_panic("No memory for flatten_device_tree (no room)");
1502                 chunk = alloc_up(room, 0);
1503                 if (chunk == 0)
1504                         prom_panic("No memory for flatten_device_tree (claim failed)");
1505                 *mem_end = RELOC(alloc_top);
1506         }
1507
1508         ret = (void *)*mem_start;
1509         *mem_start += needed;
1510
1511         return ret;
1512 }
1513
1514 #define dt_push_token(token, mem_start, mem_end) \
1515         do { *((u32 *)make_room(mem_start, mem_end, 4, 4)) = token; } while(0)
1516
1517 static unsigned long __init dt_find_string(char *str)
1518 {
1519         unsigned long offset = reloc_offset();
1520         char *s, *os;
1521
1522         s = os = (char *)RELOC(dt_string_start);
1523         s += 4;
1524         while (s <  (char *)RELOC(dt_string_end)) {
1525                 if (strcmp(s, str) == 0)
1526                         return s - os;
1527                 s += strlen(s) + 1;
1528         }
1529         return 0;
1530 }
1531
1532 /*
1533  * The Open Firmware 1275 specification states properties must be 31 bytes or
1534  * less, however not all firmwares obey this. Make it 64 bytes to be safe.
1535  */
1536 #define MAX_PROPERTY_NAME 64
1537
1538 static void __init scan_dt_build_strings(phandle node,
1539                                          unsigned long *mem_start,
1540                                          unsigned long *mem_end)
1541 {
1542         unsigned long offset = reloc_offset();
1543         char *prev_name, *namep, *sstart;
1544         unsigned long soff;
1545         phandle child;
1546
1547         sstart =  (char *)RELOC(dt_string_start);
1548
1549         /* get and store all property names */
1550         prev_name = RELOC("");
1551         for (;;) {
1552                 /* 64 is max len of name including nul. */
1553                 namep = make_room(mem_start, mem_end, MAX_PROPERTY_NAME, 1);
1554                 if (call_prom("nextprop", 3, 1, node, prev_name, namep) != 1) {
1555                         /* No more nodes: unwind alloc */
1556                         *mem_start = (unsigned long)namep;
1557                         break;
1558                 }
1559
1560                 /* skip "name" */
1561                 if (strcmp(namep, RELOC("name")) == 0) {
1562                         *mem_start = (unsigned long)namep;
1563                         prev_name = RELOC("name");
1564                         continue;
1565                 }
1566                 /* get/create string entry */
1567                 soff = dt_find_string(namep);
1568                 if (soff != 0) {
1569                         *mem_start = (unsigned long)namep;
1570                         namep = sstart + soff;
1571                 } else {
1572                         /* Trim off some if we can */
1573                         *mem_start = (unsigned long)namep + strlen(namep) + 1;
1574                         RELOC(dt_string_end) = *mem_start;
1575                 }
1576                 prev_name = namep;
1577         }
1578
1579         /* do all our children */
1580         child = call_prom("child", 1, 1, node);
1581         while (child != 0) {
1582                 scan_dt_build_strings(child, mem_start, mem_end);
1583                 child = call_prom("peer", 1, 1, child);
1584         }
1585 }
1586
1587 static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
1588                                         unsigned long *mem_end)
1589 {
1590         phandle child;
1591         char *namep, *prev_name, *sstart, *p, *ep, *lp, *path;
1592         unsigned long soff;
1593         unsigned char *valp;
1594         unsigned long offset = reloc_offset();
1595         static char pname[MAX_PROPERTY_NAME];
1596         int l;
1597
1598         dt_push_token(OF_DT_BEGIN_NODE, mem_start, mem_end);
1599
1600         /* get the node's full name */
1601         namep = (char *)*mem_start;
1602         l = call_prom("package-to-path", 3, 1, node,
1603                       namep, *mem_end - *mem_start);
1604         if (l >= 0) {
1605                 /* Didn't fit?  Get more room. */
1606                 if ((l+1) > (*mem_end - *mem_start)) {
1607                         namep = make_room(mem_start, mem_end, l+1, 1);
1608                         call_prom("package-to-path", 3, 1, node, namep, l);
1609                 }
1610                 namep[l] = '\0';
1611
1612                 /* Fixup an Apple bug where they have bogus \0 chars in the
1613                  * middle of the path in some properties
1614                  */
1615                 for (p = namep, ep = namep + l; p < ep; p++)
1616                         if (*p == '\0') {
1617                                 memmove(p, p+1, ep - p);
1618                                 ep--; l--; p--;
1619                         }
1620
1621                 /* now try to extract the unit name in that mess */
1622                 for (p = namep, lp = NULL; *p; p++)
1623                         if (*p == '/')
1624                                 lp = p + 1;
1625                 if (lp != NULL)
1626                         memmove(namep, lp, strlen(lp) + 1);
1627                 *mem_start = _ALIGN(((unsigned long) namep) +
1628                                     strlen(namep) + 1, 4);
1629         }
1630
1631         /* get it again for debugging */
1632         path = RELOC(prom_scratch);
1633         memset(path, 0, PROM_SCRATCH_SIZE);
1634         call_prom("package-to-path", 3, 1, node, path, PROM_SCRATCH_SIZE-1);
1635
1636         /* get and store all properties */
1637         prev_name = RELOC("");
1638         sstart = (char *)RELOC(dt_string_start);
1639         for (;;) {
1640                 if (call_prom("nextprop", 3, 1, node, prev_name,
1641                               RELOC(pname)) != 1)
1642                         break;
1643
1644                 /* skip "name" */
1645                 if (strcmp(RELOC(pname), RELOC("name")) == 0) {
1646                         prev_name = RELOC("name");
1647                         continue;
1648                 }
1649
1650                 /* find string offset */
1651                 soff = dt_find_string(RELOC(pname));
1652                 if (soff == 0) {
1653                         prom_printf("WARNING: Can't find string index for"
1654                                     " <%s>, node %s\n", RELOC(pname), path);
1655                         break;
1656                 }
1657                 prev_name = sstart + soff;
1658
1659                 /* get length */
1660                 l = call_prom("getproplen", 2, 1, node, RELOC(pname));
1661
1662                 /* sanity checks */
1663                 if (l == PROM_ERROR)
1664                         continue;
1665                 if (l > MAX_PROPERTY_LENGTH) {
1666                         prom_printf("WARNING: ignoring large property ");
1667                         /* It seems OF doesn't null-terminate the path :-( */
1668                         prom_printf("[%s] ", path);
1669                         prom_printf("%s length 0x%x\n", RELOC(pname), l);
1670                         continue;
1671                 }
1672
1673                 /* push property head */
1674                 dt_push_token(OF_DT_PROP, mem_start, mem_end);
1675                 dt_push_token(l, mem_start, mem_end);
1676                 dt_push_token(soff, mem_start, mem_end);
1677
1678                 /* push property content */
1679                 valp = make_room(mem_start, mem_end, l, 4);
1680                 call_prom("getprop", 4, 1, node, RELOC(pname), valp, l);
1681                 *mem_start = _ALIGN(*mem_start, 4);
1682         }
1683
1684         /* Add a "linux,phandle" property. */
1685         soff = dt_find_string(RELOC("linux,phandle"));
1686         if (soff == 0)
1687                 prom_printf("WARNING: Can't find string index for"
1688                             " <linux-phandle> node %s\n", path);
1689         else {
1690                 dt_push_token(OF_DT_PROP, mem_start, mem_end);
1691                 dt_push_token(4, mem_start, mem_end);
1692                 dt_push_token(soff, mem_start, mem_end);
1693                 valp = make_room(mem_start, mem_end, 4, 4);
1694                 *(u32 *)valp = node;
1695         }
1696
1697         /* do all our children */
1698         child = call_prom("child", 1, 1, node);
1699         while (child != 0) {
1700                 scan_dt_build_struct(child, mem_start, mem_end);
1701                 child = call_prom("peer", 1, 1, child);
1702         }
1703
1704         dt_push_token(OF_DT_END_NODE, mem_start, mem_end);
1705 }
1706
1707 static void __init flatten_device_tree(void)
1708 {
1709         phandle root;
1710         unsigned long offset = reloc_offset();
1711         unsigned long mem_start, mem_end, room;
1712         struct boot_param_header *hdr;
1713         struct prom_t *_prom = PTRRELOC(&prom);
1714         char *namep;
1715         u64 *rsvmap;
1716
1717         /*
1718          * Check how much room we have between alloc top & bottom (+/- a
1719          * few pages), crop to 4Mb, as this is our "chuck" size
1720          */
1721         room = RELOC(alloc_top) - RELOC(alloc_bottom) - 0x4000;
1722         if (room > DEVTREE_CHUNK_SIZE)
1723                 room = DEVTREE_CHUNK_SIZE;
1724         prom_debug("starting device tree allocs at %x\n", RELOC(alloc_bottom));
1725
1726         /* Now try to claim that */
1727         mem_start = (unsigned long)alloc_up(room, PAGE_SIZE);
1728         if (mem_start == 0)
1729                 prom_panic("Can't allocate initial device-tree chunk\n");
1730         mem_end = RELOC(alloc_top);
1731
1732         /* Get root of tree */
1733         root = call_prom("peer", 1, 1, (phandle)0);
1734         if (root == (phandle)0)
1735                 prom_panic ("couldn't get device tree root\n");
1736
1737         /* Build header and make room for mem rsv map */ 
1738         mem_start = _ALIGN(mem_start, 4);
1739         hdr = make_room(&mem_start, &mem_end,
1740                         sizeof(struct boot_param_header), 4);
1741         RELOC(dt_header_start) = (unsigned long)hdr;
1742         rsvmap = make_room(&mem_start, &mem_end, sizeof(mem_reserve_map), 8);
1743
1744         /* Start of strings */
1745         mem_start = PAGE_ALIGN(mem_start);
1746         RELOC(dt_string_start) = mem_start;
1747         mem_start += 4; /* hole */
1748
1749         /* Add "linux,phandle" in there, we'll need it */
1750         namep = make_room(&mem_start, &mem_end, 16, 1);
1751         strcpy(namep, RELOC("linux,phandle"));
1752         mem_start = (unsigned long)namep + strlen(namep) + 1;
1753
1754         /* Build string array */
1755         prom_printf("Building dt strings...\n"); 
1756         scan_dt_build_strings(root, &mem_start, &mem_end);
1757         RELOC(dt_string_end) = mem_start;
1758
1759         /* Build structure */
1760         mem_start = PAGE_ALIGN(mem_start);
1761         RELOC(dt_struct_start) = mem_start;
1762         prom_printf("Building dt structure...\n"); 
1763         scan_dt_build_struct(root, &mem_start, &mem_end);
1764         dt_push_token(OF_DT_END, &mem_start, &mem_end);
1765         RELOC(dt_struct_end) = PAGE_ALIGN(mem_start);
1766
1767         /* Finish header */
1768         hdr->boot_cpuid_phys = _prom->cpu;
1769         hdr->magic = OF_DT_HEADER;
1770         hdr->totalsize = RELOC(dt_struct_end) - RELOC(dt_header_start);
1771         hdr->off_dt_struct = RELOC(dt_struct_start) - RELOC(dt_header_start);
1772         hdr->off_dt_strings = RELOC(dt_string_start) - RELOC(dt_header_start);
1773         hdr->dt_strings_size = RELOC(dt_string_end) - RELOC(dt_string_start);
1774         hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - RELOC(dt_header_start);
1775         hdr->version = OF_DT_VERSION;
1776         /* Version 16 is not backward compatible */
1777         hdr->last_comp_version = 0x10;
1778
1779         /* Reserve the whole thing and copy the reserve map in, we
1780          * also bump mem_reserve_cnt to cause further reservations to
1781          * fail since it's too late.
1782          */
1783         reserve_mem(RELOC(dt_header_start), hdr->totalsize);
1784         memcpy(rsvmap, RELOC(mem_reserve_map), sizeof(mem_reserve_map));
1785
1786 #ifdef DEBUG_PROM
1787         {
1788                 int i;
1789                 prom_printf("reserved memory map:\n");
1790                 for (i = 0; i < RELOC(mem_reserve_cnt); i++)
1791                         prom_printf("  %x - %x\n", RELOC(mem_reserve_map)[i].base,
1792                                     RELOC(mem_reserve_map)[i].size);
1793         }
1794 #endif
1795         RELOC(mem_reserve_cnt) = MEM_RESERVE_MAP_SIZE;
1796
1797         prom_printf("Device tree strings 0x%x -> 0x%x\n",
1798                     RELOC(dt_string_start), RELOC(dt_string_end)); 
1799         prom_printf("Device tree struct  0x%x -> 0x%x\n",
1800                     RELOC(dt_struct_start), RELOC(dt_struct_end));
1801
1802 }
1803
1804
1805 static void __init fixup_device_tree(void)
1806 {
1807         unsigned long offset = reloc_offset();
1808         phandle u3, i2c, mpic;
1809         u32 u3_rev;
1810         u32 interrupts[2];
1811         u32 parent;
1812
1813         /* Some G5s have a missing interrupt definition, fix it up here */
1814         u3 = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000"));
1815         if (!PHANDLE_VALID(u3))
1816                 return;
1817         i2c = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/i2c@f8001000"));
1818         if (!PHANDLE_VALID(i2c))
1819                 return;
1820         mpic = call_prom("finddevice", 1, 1, ADDR("/u3@0,f8000000/mpic@f8040000"));
1821         if (!PHANDLE_VALID(mpic))
1822                 return;
1823
1824         /* check if proper rev of u3 */
1825         if (prom_getprop(u3, "device-rev", &u3_rev, sizeof(u3_rev))
1826             == PROM_ERROR)
1827                 return;
1828         if (u3_rev != 0x35 && u3_rev != 0x37)
1829                 return;
1830         /* does it need fixup ? */
1831         if (prom_getproplen(i2c, "interrupts") > 0)
1832                 return;
1833
1834         prom_printf("fixing up bogus interrupts for u3 i2c...\n");
1835
1836         /* interrupt on this revision of u3 is number 0 and level */
1837         interrupts[0] = 0;
1838         interrupts[1] = 1;
1839         prom_setprop(i2c, "interrupts", &interrupts, sizeof(interrupts));
1840         parent = (u32)mpic;
1841         prom_setprop(i2c, "interrupt-parent", &parent, sizeof(parent));
1842 }
1843
1844
1845 static void __init prom_find_boot_cpu(void)
1846 {
1847         unsigned long offset = reloc_offset();
1848         struct prom_t *_prom = PTRRELOC(&prom);
1849         u32 getprop_rval;
1850         ihandle prom_cpu;
1851         phandle cpu_pkg;
1852
1853         if (prom_getprop(_prom->chosen, "cpu", &prom_cpu, sizeof(prom_cpu)) <= 0)
1854                 prom_panic("cannot find boot cpu");
1855
1856         cpu_pkg = call_prom("instance-to-package", 1, 1, prom_cpu);
1857
1858         prom_getprop(cpu_pkg, "reg", &getprop_rval, sizeof(getprop_rval));
1859         _prom->cpu = getprop_rval;
1860
1861         prom_debug("Booting CPU hw index = 0x%x\n", _prom->cpu);
1862 }
1863
1864 static void __init prom_check_initrd(unsigned long r3, unsigned long r4)
1865 {
1866 #ifdef CONFIG_BLK_DEV_INITRD
1867         unsigned long offset = reloc_offset();
1868         struct prom_t *_prom = PTRRELOC(&prom);
1869
1870         if ( r3 && r4 && r4 != 0xdeadbeef) {
1871                 u64 val;
1872
1873                 RELOC(prom_initrd_start) = (r3 >= KERNELBASE) ? __pa(r3) : r3;
1874                 RELOC(prom_initrd_end) = RELOC(prom_initrd_start) + r4;
1875
1876                 val = (u64)RELOC(prom_initrd_start);
1877                 prom_setprop(_prom->chosen, "linux,initrd-start", &val, sizeof(val));
1878                 val = (u64)RELOC(prom_initrd_end);
1879                 prom_setprop(_prom->chosen, "linux,initrd-end", &val, sizeof(val));
1880
1881                 reserve_mem(RELOC(prom_initrd_start),
1882                             RELOC(prom_initrd_end) - RELOC(prom_initrd_start));
1883
1884                 prom_debug("initrd_start=0x%x\n", RELOC(prom_initrd_start));
1885                 prom_debug("initrd_end=0x%x\n", RELOC(prom_initrd_end));
1886         }
1887 #endif /* CONFIG_BLK_DEV_INITRD */
1888 }
1889
1890 /*
1891  * We enter here early on, when the Open Firmware prom is still
1892  * handling exceptions and the MMU hash table for us.
1893  */
1894
1895 unsigned long __init prom_init(unsigned long r3, unsigned long r4, unsigned long pp,
1896                                unsigned long r6, unsigned long r7)
1897 {       
1898         unsigned long offset = reloc_offset();
1899         struct prom_t *_prom = PTRRELOC(&prom);
1900         unsigned long phys = KERNELBASE - offset;
1901         u32 getprop_rval;
1902         
1903         /*
1904          * First zero the BSS
1905          */
1906         memset(PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
1907
1908         /*
1909          * Init interface to Open Firmware, get some node references,
1910          * like /chosen
1911          */
1912         prom_init_client_services(pp);
1913
1914         /*
1915          * Init prom stdout device
1916          */
1917         prom_init_stdout();
1918         prom_debug("klimit=0x%x\n", RELOC(klimit));
1919         prom_debug("offset=0x%x\n", offset);
1920
1921         /*
1922          * Check for an initrd
1923          */
1924         prom_check_initrd(r3, r4);
1925
1926         /*
1927          * Get default machine type. At this point, we do not differenciate
1928          * between pSeries SMP and pSeries LPAR
1929          */
1930         RELOC(of_platform) = prom_find_machine_type();
1931         getprop_rval = RELOC(of_platform);
1932         prom_setprop(_prom->chosen, "linux,platform",
1933                      &getprop_rval, sizeof(getprop_rval));
1934
1935         /*
1936          * On pSeries, inform the firmware about our capabilities
1937          */
1938         if (RELOC(of_platform) & PLATFORM_PSERIES)
1939                 prom_send_capabilities();
1940
1941         /*
1942          * On pSeries and BPA, copy the CPU hold code
1943          */
1944         if (RELOC(of_platform) & (PLATFORM_PSERIES | PLATFORM_BPA))
1945                 copy_and_flush(0, KERNELBASE - offset, 0x100, 0);
1946
1947         /*
1948          * Get memory cells format
1949          */
1950         getprop_rval = 1;
1951         prom_getprop(_prom->root, "#size-cells",
1952                      &getprop_rval, sizeof(getprop_rval));
1953         _prom->root_size_cells = getprop_rval;
1954         getprop_rval = 2;
1955         prom_getprop(_prom->root, "#address-cells",
1956                      &getprop_rval, sizeof(getprop_rval));
1957         _prom->root_addr_cells = getprop_rval;
1958
1959         /*
1960          * Do early parsing of command line
1961          */
1962         early_cmdline_parse();
1963
1964         /*
1965          * Initialize memory management within prom_init
1966          */
1967         prom_init_mem();
1968
1969         /*
1970          * Determine which cpu is actually running right _now_
1971          */
1972         prom_find_boot_cpu();
1973
1974         /* 
1975          * Initialize display devices
1976          */
1977         prom_check_displays();
1978
1979         /*
1980          * Initialize IOMMU (TCE tables) on pSeries. Do that before anything else
1981          * that uses the allocator, we need to make sure we get the top of memory
1982          * available for us here...
1983          */
1984         if (RELOC(of_platform) == PLATFORM_PSERIES)
1985                 prom_initialize_tce_table();
1986
1987         /*
1988          * On non-powermacs, try to instantiate RTAS and puts all CPUs
1989          * in spin-loops. PowerMacs don't have a working RTAS and use
1990          * a different way to spin CPUs
1991          */
1992         if (RELOC(of_platform) != PLATFORM_POWERMAC) {
1993                 prom_instantiate_rtas();
1994                 prom_hold_cpus();
1995         }
1996
1997         /*
1998          * Fill in some infos for use by the kernel later on
1999          */
2000         if (RELOC(ppc64_iommu_off))
2001                 prom_setprop(_prom->chosen, "linux,iommu-off", NULL, 0);
2002
2003         if (RELOC(iommu_force_on))
2004                 prom_setprop(_prom->chosen, "linux,iommu-force-on", NULL, 0);
2005
2006         if (RELOC(prom_memory_limit))
2007                 prom_setprop(_prom->chosen, "linux,memory-limit",
2008                         PTRRELOC(&prom_memory_limit), sizeof(RELOC(prom_memory_limit)));
2009
2010         if (RELOC(prom_tce_alloc_start)) {
2011                 prom_setprop(_prom->chosen, "linux,tce-alloc-start",
2012                         PTRRELOC(&prom_tce_alloc_start), sizeof(RELOC(prom_tce_alloc_start)));
2013                 prom_setprop(_prom->chosen, "linux,tce-alloc-end",
2014                         PTRRELOC(&prom_tce_alloc_end), sizeof(RELOC(prom_tce_alloc_end)));
2015         }
2016
2017         /*
2018          * Fixup any known bugs in the device-tree
2019          */
2020         fixup_device_tree();
2021
2022         /*
2023          * Now finally create the flattened device-tree
2024          */
2025         prom_printf("copying OF device tree ...\n");
2026         flatten_device_tree();
2027
2028         /* in case stdin is USB and still active on IBM machines... */
2029         prom_close_stdin();
2030
2031         /*
2032          * Call OF "quiesce" method to shut down pending DMA's from
2033          * devices etc...
2034          */
2035         prom_printf("Calling quiesce ...\n");
2036         call_prom("quiesce", 0, 0);
2037
2038         /*
2039          * And finally, call the kernel passing it the flattened device
2040          * tree and NULL as r5, thus triggering the new entry point which
2041          * is common to us and kexec
2042          */
2043         prom_printf("returning from prom_init\n");
2044         prom_debug("->dt_header_start=0x%x\n", RELOC(dt_header_start));
2045         prom_debug("->phys=0x%x\n", phys);
2046
2047         __start(RELOC(dt_header_start), phys, 0);
2048
2049         return 0;
2050 }
2051