patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / ppc / syslib / prom_init.c
1 /*
2  * Note that prom_init() and anything called from prom_init()
3  * may be running at an address that is different from the address
4  * that it was linked at.  References to static data items are
5  * handled by compiling this file with -mrelocatable-lib.
6  */
7
8 #include <linux/config.h>
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/init.h>
12 #include <linux/version.h>
13 #include <linux/threads.h>
14 #include <linux/spinlock.h>
15 #include <linux/ioport.h>
16 #include <linux/pci.h>
17 #include <linux/slab.h>
18
19 #include <asm/sections.h>
20 #include <asm/prom.h>
21 #include <asm/page.h>
22 #include <asm/irq.h>
23 #include <asm/io.h>
24 #include <asm/smp.h>
25 #include <asm/bootx.h>
26 #include <asm/system.h>
27 #include <asm/mmu.h>
28 #include <asm/pgtable.h>
29 #include <asm/bitops.h>
30 #include <asm/bootinfo.h>
31 #include <asm/btext.h>
32 #include <asm/pci-bridge.h>
33 #include <asm/open_pic.h>
34 #include <asm/cacheflush.h>
35
36 #ifdef CONFIG_LOGO_LINUX_CLUT224
37 #include <linux/linux_logo.h>
38 extern const struct linux_logo logo_linux_clut224;
39 #endif
40
41 /*
42  * Properties whose value is longer than this get excluded from our
43  * copy of the device tree.  This way we don't waste space storing
44  * things like "driver,AAPL,MacOS,PowerPC" properties.  But this value
45  * does need to be big enough to ensure that we don't lose things
46  * like the interrupt-map property on a PCI-PCI bridge.
47  */
48 #define MAX_PROPERTY_LENGTH     4096
49
50 #ifndef FB_MAX                  /* avoid pulling in all of the fb stuff */
51 #define FB_MAX  8
52 #endif
53
54 #define ALIGNUL(x) (((x) + sizeof(unsigned long)-1) & -sizeof(unsigned long))
55
56 struct prom_args {
57         const char *service;
58         int nargs;
59         int nret;
60         void *args[10];
61 };
62
63 struct pci_address {
64         unsigned a_hi;
65         unsigned a_mid;
66         unsigned a_lo;
67 };
68
69 struct pci_reg_property {
70         struct pci_address addr;
71         unsigned size_hi;
72         unsigned size_lo;
73 };
74
75 struct pci_range {
76         struct pci_address addr;
77         unsigned phys;
78         unsigned size_hi;
79         unsigned size_lo;
80 };
81
82 struct isa_reg_property {
83         unsigned space;
84         unsigned address;
85         unsigned size;
86 };
87
88 struct pci_intr_map {
89         struct pci_address addr;
90         unsigned dunno;
91         phandle int_ctrler;
92         unsigned intr;
93 };
94
95 static void prom_exit(void);
96 static void *call_prom(const char *service, int nargs, int nret, ...);
97 static void *call_prom_ret(const char *service, int nargs, int nret,
98                            void **rets, ...);
99 static void prom_print_hex(unsigned int v);
100 static int  prom_set_color(ihandle ih, int i, int r, int g, int b);
101 static int  prom_next_node(phandle *nodep);
102 static unsigned long check_display(unsigned long mem);
103 static void setup_disp_fake_bi(ihandle dp);
104 static unsigned long copy_device_tree(unsigned long mem_start,
105                                 unsigned long mem_end);
106 static unsigned long inspect_node(phandle node, struct device_node *dad,
107                                 unsigned long mem_start, unsigned long mem_end,
108                                 struct device_node ***allnextpp);
109 static void prom_hold_cpus(unsigned long mem);
110 static void prom_instantiate_rtas(void);
111 static void * early_get_property(unsigned long base, unsigned long node,
112                                 char *prop);
113
114 prom_entry prom __initdata = 0;
115 ihandle prom_chosen __initdata = 0;
116 ihandle prom_stdout __initdata = 0;
117
118 char *prom_display_paths[FB_MAX] __initdata = { 0, };
119 phandle prom_display_nodes[FB_MAX] __initdata;
120 unsigned int prom_num_displays __initdata = 0;
121 static char *of_stdout_device __initdata = 0;
122 static ihandle prom_disp_node __initdata = 0;
123
124 unsigned int rtas_data;   /* physical pointer */
125 unsigned int rtas_entry;  /* physical pointer */
126 unsigned int rtas_size;
127 unsigned int old_rtas;
128
129 boot_infos_t *boot_infos;
130 char *bootpath;
131 char *bootdevice;
132 struct device_node *allnodes;
133
134 extern char *klimit;
135
136 static void __init
137 prom_exit(void)
138 {
139         struct prom_args args;
140
141         args.service = "exit";
142         args.nargs = 0;
143         args.nret = 0;
144         prom(&args);
145         for (;;)                        /* should never get here */
146                 ;
147 }
148
149 static void * __init
150 call_prom(const char *service, int nargs, int nret, ...)
151 {
152         va_list list;
153         int i;
154         struct prom_args prom_args;
155
156         prom_args.service = service;
157         prom_args.nargs = nargs;
158         prom_args.nret = nret;
159         va_start(list, nret);
160         for (i = 0; i < nargs; ++i)
161                 prom_args.args[i] = va_arg(list, void *);
162         va_end(list);
163         for (i = 0; i < nret; ++i)
164                 prom_args.args[i + nargs] = 0;
165         prom(&prom_args);
166         return prom_args.args[nargs];
167 }
168
169 static void * __init
170 call_prom_ret(const char *service, int nargs, int nret, void **rets, ...)
171 {
172         va_list list;
173         int i;
174         struct prom_args prom_args;
175
176         prom_args.service = service;
177         prom_args.nargs = nargs;
178         prom_args.nret = nret;
179         va_start(list, rets);
180         for (i = 0; i < nargs; ++i)
181                 prom_args.args[i] = va_arg(list, void *);
182         va_end(list);
183         for (i = 0; i < nret; ++i)
184                 prom_args.args[i + nargs] = 0;
185         prom(&prom_args);
186         for (i = 1; i < nret; ++i)
187                 rets[i-1] = prom_args.args[nargs + i];
188         return prom_args.args[nargs];
189 }
190
191 void __init
192 prom_print(const char *msg)
193 {
194         const char *p, *q;
195
196         if (prom_stdout == 0)
197                 return;
198
199         for (p = msg; *p != 0; p = q) {
200                 for (q = p; *q != 0 && *q != '\n'; ++q)
201                         ;
202                 if (q > p)
203                         call_prom("write", 3, 1, prom_stdout, p, q - p);
204                 if (*q != 0) {
205                         ++q;
206                         call_prom("write", 3, 1, prom_stdout, "\r\n", 2);
207                 }
208         }
209 }
210
211 static void __init
212 prom_print_hex(unsigned int v)
213 {
214         char buf[16];
215         int i, c;
216
217         for (i = 0; i < 8; ++i) {
218                 c = (v >> ((7-i)*4)) & 0xf;
219                 c += (c >= 10)? ('a' - 10): '0';
220                 buf[i] = c;
221         }
222         buf[i] = ' ';
223         buf[i+1] = 0;
224         prom_print(buf);
225 }
226
227 static int __init
228 prom_set_color(ihandle ih, int i, int r, int g, int b)
229 {
230         struct prom_args prom_args;
231
232         prom_args.service = "call-method";
233         prom_args.nargs = 6;
234         prom_args.nret = 1;
235         prom_args.args[0] = "color!";
236         prom_args.args[1] = ih;
237         prom_args.args[2] = (void *) i;
238         prom_args.args[3] = (void *) b;
239         prom_args.args[4] = (void *) g;
240         prom_args.args[5] = (void *) r;
241         prom(&prom_args);
242         return (int) prom_args.args[6];
243 }
244
245 static int __init
246 prom_next_node(phandle *nodep)
247 {
248         phandle node;
249
250         if ((node = *nodep) != 0
251             && (*nodep = call_prom("child", 1, 1, node)) != 0)
252                 return 1;
253         if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
254                 return 1;
255         for (;;) {
256                 if ((node = call_prom("parent", 1, 1, node)) == 0)
257                         return 0;
258                 if ((*nodep = call_prom("peer", 1, 1, node)) != 0)
259                         return 1;
260         }
261 }
262
263 #ifdef CONFIG_POWER4
264 /*
265  * Set up a hash table with a set of entries in it to map the
266  * first 64MB of RAM.  This is used on 64-bit machines since
267  * some of them don't have BATs.
268  */
269
270 static inline void make_pte(unsigned long htab, unsigned int hsize,
271                             unsigned int va, unsigned int pa, int mode)
272 {
273         unsigned int *pteg;
274         unsigned int hash, i, vsid;
275
276         vsid = ((va >> 28) * 0x111) << 12;
277         hash = ((va ^ vsid) >> 5) & 0x7fff80;
278         pteg = (unsigned int *)(htab + (hash & (hsize - 1)));
279         for (i = 0; i < 8; ++i, pteg += 4) {
280                 if ((pteg[1] & 1) == 0) {
281                         pteg[1] = vsid | ((va >> 16) & 0xf80) | 1;
282                         pteg[3] = pa | mode;
283                         break;
284                 }
285         }
286 }
287
288 extern unsigned long _SDR1;
289 extern PTE *Hash;
290 extern unsigned long Hash_size;
291
292 static void __init
293 prom_alloc_htab(void)
294 {
295         unsigned int hsize;
296         unsigned long htab;
297         unsigned int addr;
298
299         /*
300          * Because of OF bugs we can't use the "claim" client
301          * interface to allocate memory for the hash table.
302          * This code is only used on 64-bit PPCs, and the only
303          * 64-bit PPCs at the moment are RS/6000s, and their
304          * OF is based at 0xc00000 (the 12M point), so we just
305          * arbitrarily use the 0x800000 - 0xc00000 region for the
306          * hash table.
307          *  -- paulus.
308          */
309         hsize = 4 << 20;        /* POWER4 has no BATs */
310         htab = (8 << 20);
311         call_prom("claim", 3, 1, htab, hsize, 0);
312         Hash = (void *)(htab + KERNELBASE);
313         Hash_size = hsize;
314         _SDR1 = htab + __ilog2(hsize) - 18;
315
316         /*
317          * Put in PTEs for the first 64MB of RAM
318          */
319         memset((void *)htab, 0, hsize);
320         for (addr = 0; addr < 0x4000000; addr += 0x1000)
321                 make_pte(htab, hsize, addr + KERNELBASE, addr,
322                          _PAGE_ACCESSED | _PAGE_COHERENT | PP_RWXX);
323 #if 0 /* DEBUG stuff mapping the SCC */
324         make_pte(htab, hsize, 0x80013000, 0x80013000,
325                  _PAGE_ACCESSED | _PAGE_NO_CACHE | _PAGE_GUARDED | PP_RWXX);
326 #endif
327 }
328 #endif /* CONFIG_POWER4 */
329
330
331 /*
332  * If we have a display that we don't know how to drive,
333  * we will want to try to execute OF's open method for it
334  * later.  However, OF will probably fall over if we do that
335  * we've taken over the MMU.
336  * So we check whether we will need to open the display,
337  * and if so, open it now.
338  */
339 static unsigned long __init
340 check_display(unsigned long mem)
341 {
342         phandle node;
343         ihandle ih;
344         int i, j;
345         char type[16], *path;
346         static unsigned char default_colors[] = {
347                 0x00, 0x00, 0x00,
348                 0x00, 0x00, 0xaa,
349                 0x00, 0xaa, 0x00,
350                 0x00, 0xaa, 0xaa,
351                 0xaa, 0x00, 0x00,
352                 0xaa, 0x00, 0xaa,
353                 0xaa, 0xaa, 0x00,
354                 0xaa, 0xaa, 0xaa,
355                 0x55, 0x55, 0x55,
356                 0x55, 0x55, 0xff,
357                 0x55, 0xff, 0x55,
358                 0x55, 0xff, 0xff,
359                 0xff, 0x55, 0x55,
360                 0xff, 0x55, 0xff,
361                 0xff, 0xff, 0x55,
362                 0xff, 0xff, 0xff
363         };
364         const unsigned char *clut;
365
366         prom_disp_node = 0;
367
368         for (node = 0; prom_next_node(&node); ) {
369                 type[0] = 0;
370                 call_prom("getprop", 4, 1, node, "device_type",
371                           type, sizeof(type));
372                 if (strcmp(type, "display") != 0)
373                         continue;
374                 /* It seems OF doesn't null-terminate the path :-( */
375                 path = (char *) mem;
376                 memset(path, 0, 256);
377                 if ((int) call_prom("package-to-path", 3, 1,
378                                     node, path, 255) < 0)
379                         continue;
380
381                 /*
382                  * If this display is the device that OF is using for stdout,
383                  * move it to the front of the list.
384                  */
385                 mem += strlen(path) + 1;
386                 i = prom_num_displays++;
387                 if (of_stdout_device != 0 && i > 0
388                     && strcmp(of_stdout_device, path) == 0) {
389                         for (; i > 0; --i) {
390                                 prom_display_paths[i]
391                                         = prom_display_paths[i-1];
392                                 prom_display_nodes[i]
393                                         = prom_display_nodes[i-1];
394                         }
395                 }
396                 prom_display_paths[i] = path;
397                 prom_display_nodes[i] = node;
398                 if (i == 0)
399                         prom_disp_node = node;
400                 if (prom_num_displays >= FB_MAX)
401                         break;
402         }
403
404         for (j=0; j<prom_num_displays; j++) {
405                 path = prom_display_paths[j];
406                 prom_print("opening display ");
407                 prom_print(path);
408                 ih = call_prom("open", 1, 1, path);
409                 if (ih == 0 || ih == (ihandle) -1) {
410                         prom_print("... failed\n");
411                         for (i=j+1; i<prom_num_displays; i++) {
412                                 prom_display_paths[i-1] = prom_display_paths[i];
413                                 prom_display_nodes[i-1] = prom_display_nodes[i];
414                         }
415                         if (--prom_num_displays > 0) {
416                                 prom_disp_node = prom_display_nodes[j];
417                                 j--;
418                         } else
419                                 prom_disp_node = NULL;
420                         continue;
421                 } else {
422                         prom_print("... ok\n");
423                         /*
424                          * Setup a usable color table when the appropriate
425                          * method is available.
426                          * Should update this to use set-colors.
427                          */
428                         clut = default_colors;
429                         for (i = 0; i < 32; i++, clut += 3)
430                                 if (prom_set_color(ih, i, clut[0], clut[1],
431                                                    clut[2]) != 0)
432                                         break;
433
434 #ifdef CONFIG_LOGO_LINUX_CLUT224
435                         clut = PTRRELOC(logo_linux_clut224.clut);
436                         for (i = 0; i < logo_linux_clut224.clutsize;
437                              i++, clut += 3)
438                                 if (prom_set_color(ih, i + 32, clut[0],
439                                                    clut[1], clut[2]) != 0)
440                                         break;
441 #endif /* CONFIG_LOGO_LINUX_CLUT224 */
442                 }
443         }
444
445         return ALIGNUL(mem);
446 }
447
448 /* This function will enable the early boot text when doing OF booting. This
449  * way, xmon output should work too
450  */
451 static void __init
452 setup_disp_fake_bi(ihandle dp)
453 {
454 #ifdef CONFIG_BOOTX_TEXT
455         int width = 640, height = 480, depth = 8, pitch;
456         unsigned address;
457         struct pci_reg_property addrs[8];
458         int i, naddrs;
459         char name[32];
460         char *getprop = "getprop";
461
462         prom_print("Initializing fake screen: ");
463
464         memset(name, 0, sizeof(name));
465         call_prom(getprop, 4, 1, dp, "name", name, sizeof(name));
466         name[sizeof(name)-1] = 0;
467         prom_print(name);
468         prom_print("\n");
469         call_prom(getprop, 4, 1, dp, "width", &width, sizeof(width));
470         call_prom(getprop, 4, 1, dp, "height", &height, sizeof(height));
471         call_prom(getprop, 4, 1, dp, "depth", &depth, sizeof(depth));
472         pitch = width * ((depth + 7) / 8);
473         call_prom(getprop, 4, 1, dp, "linebytes",
474                   &pitch, sizeof(pitch));
475         if (pitch == 1)
476                 pitch = 0x1000;         /* for strange IBM display */
477         address = 0;
478         call_prom(getprop, 4, 1, dp, "address",
479                   &address, sizeof(address));
480         if (address == 0) {
481                 /* look for an assigned address with a size of >= 1MB */
482                 naddrs = (int) call_prom(getprop, 4, 1, dp,
483                                 "assigned-addresses",
484                                 addrs, sizeof(addrs));
485                 naddrs /= sizeof(struct pci_reg_property);
486                 for (i = 0; i < naddrs; ++i) {
487                         if (addrs[i].size_lo >= (1 << 20)) {
488                                 address = addrs[i].addr.a_lo;
489                                 /* use the BE aperture if possible */
490                                 if (addrs[i].size_lo >= (16 << 20))
491                                         address += (8 << 20);
492                                 break;
493                         }
494                 }
495                 if (address == 0) {
496                         prom_print("Failed to get address\n");
497                         return;
498                 }
499         }
500         /* kludge for valkyrie */
501         if (strcmp(name, "valkyrie") == 0)
502                 address += 0x1000;
503
504 #ifdef CONFIG_POWER4
505 #if CONFIG_TASK_SIZE > 0x80000000
506 #error CONFIG_TASK_SIZE cannot be above 0x80000000 with BOOTX_TEXT on G5
507 #endif
508         {
509                 extern boot_infos_t disp_bi;
510                 unsigned long va, pa, i, offset;
511                 va = 0x90000000;
512                 pa = address & 0xfffff000ul;
513                 offset = address & 0x00000fff;
514
515                 for (i=0; i<0x4000; i++) {  
516                         make_pte((unsigned long)Hash - KERNELBASE, Hash_size, va, pa, 
517                                  _PAGE_ACCESSED | _PAGE_NO_CACHE |
518                                  _PAGE_GUARDED | PP_RWXX);
519                         va += 0x1000;
520                         pa += 0x1000;
521                 }
522                 btext_setup_display(width, height, depth, pitch, 0x90000000 | offset);
523                 disp_bi.dispDeviceBase = (u8 *)address;
524         }
525 #else /* CONFIG_POWER4 */
526         btext_setup_display(width, height, depth, pitch, address);
527         btext_prepare_BAT();
528 #endif /* CONFIG_POWER4 */
529 #endif /* CONFIG_BOOTX_TEXT */
530 }
531
532 /*
533  * Make a copy of the device tree from the PROM.
534  */
535 static unsigned long __init
536 copy_device_tree(unsigned long mem_start, unsigned long mem_end)
537 {
538         phandle root;
539         unsigned long new_start;
540         struct device_node **allnextp;
541
542         root = call_prom("peer", 1, 1, (phandle)0);
543         if (root == (phandle)0) {
544                 prom_print("couldn't get device tree root\n");
545                 prom_exit();
546         }
547         allnextp = &allnodes;
548         mem_start = ALIGNUL(mem_start);
549         new_start = inspect_node(root, 0, mem_start, mem_end, &allnextp);
550         *allnextp = 0;
551         return new_start;
552 }
553
554 static unsigned long __init
555 inspect_node(phandle node, struct device_node *dad,
556              unsigned long mem_start, unsigned long mem_end,
557              struct device_node ***allnextpp)
558 {
559         int l;
560         phandle child;
561         struct device_node *np;
562         struct property *pp, **prev_propp;
563         char *prev_name, *namep;
564         unsigned char *valp;
565
566         np = (struct device_node *) mem_start;
567         mem_start += sizeof(struct device_node);
568         memset(np, 0, sizeof(*np));
569         np->node = node;
570         **allnextpp = PTRUNRELOC(np);
571         *allnextpp = &np->allnext;
572         if (dad != 0) {
573                 np->parent = PTRUNRELOC(dad);
574                 /* we temporarily use the `next' field as `last_child'. */
575                 if (dad->next == 0)
576                         dad->child = PTRUNRELOC(np);
577                 else
578                         dad->next->sibling = PTRUNRELOC(np);
579                 dad->next = np;
580         }
581
582         /* get and store all properties */
583         prev_propp = &np->properties;
584         prev_name = "";
585         for (;;) {
586                 pp = (struct property *) mem_start;
587                 namep = (char *) (pp + 1);
588                 pp->name = PTRUNRELOC(namep);
589                 if ((int) call_prom("nextprop", 3, 1, node, prev_name,
590                                     namep) <= 0)
591                         break;
592                 mem_start = ALIGNUL((unsigned long)namep + strlen(namep) + 1);
593                 prev_name = namep;
594                 valp = (unsigned char *) mem_start;
595                 pp->value = PTRUNRELOC(valp);
596                 pp->length = (int)
597                         call_prom("getprop", 4, 1, node, namep,
598                                   valp, mem_end - mem_start);
599                 if (pp->length < 0)
600                         continue;
601 #ifdef MAX_PROPERTY_LENGTH
602                 if (pp->length > MAX_PROPERTY_LENGTH)
603                         continue; /* ignore this property */
604 #endif
605                 mem_start = ALIGNUL(mem_start + pp->length);
606                 *prev_propp = PTRUNRELOC(pp);
607                 prev_propp = &pp->next;
608         }
609         if (np->node != NULL) {
610                 /* Add a "linux,phandle" property" */
611                 pp = (struct property *) mem_start;
612                 *prev_propp = PTRUNRELOC(pp);
613                 prev_propp = &pp->next;
614                 namep = (char *) (pp + 1);
615                 pp->name = PTRUNRELOC(namep);
616                 strcpy(namep, "linux,phandle");
617                 mem_start = ALIGNUL((unsigned long)namep + strlen(namep) + 1);
618                 pp->value = (unsigned char *) PTRUNRELOC(&np->node);
619                 pp->length = sizeof(np->node);
620         }
621         *prev_propp = NULL;
622
623         /* get the node's full name */
624         l = (int) call_prom("package-to-path", 3, 1, node,
625                             (char *) mem_start, mem_end - mem_start);
626         if (l >= 0) {
627                 np->full_name = PTRUNRELOC((char *) mem_start);
628                 *(char *)(mem_start + l) = 0;
629                 mem_start = ALIGNUL(mem_start + l + 1);
630         }
631
632         /* do all our children */
633         child = call_prom("child", 1, 1, node);
634         while (child != (void *)0) {
635                 mem_start = inspect_node(child, np, mem_start, mem_end,
636                                          allnextpp);
637                 child = call_prom("peer", 1, 1, child);
638         }
639
640         return mem_start;
641 }
642
643 unsigned long smp_chrp_cpu_nr __initdata = 0;
644
645 /*
646  * With CHRP SMP we need to use the OF to start the other
647  * processors so we can't wait until smp_boot_cpus (the OF is
648  * trashed by then) so we have to put the processors into
649  * a holding pattern controlled by the kernel (not OF) before
650  * we destroy the OF.
651  *
652  * This uses a chunk of high memory, puts some holding pattern
653  * code there and sends the other processors off to there until
654  * smp_boot_cpus tells them to do something.  We do that by using
655  * physical address 0x0.  The holding pattern checks that address
656  * until its cpu # is there, when it is that cpu jumps to
657  * __secondary_start().  smp_boot_cpus() takes care of setting those
658  * values.
659  *
660  * We also use physical address 0x4 here to tell when a cpu
661  * is in its holding pattern code.
662  *
663  * -- Cort
664  *
665  * Note that we have to do this if we have more than one CPU,
666  * even if this is a UP kernel.  Otherwise when we trash OF
667  * the other CPUs will start executing some random instructions
668  * and crash the system.  -- paulus
669  */
670 static void __init
671 prom_hold_cpus(unsigned long mem)
672 {
673         extern void __secondary_hold(void);
674         unsigned long i;
675         int cpu;
676         phandle node;
677         char type[16], *path;
678         unsigned int reg;
679
680         /*
681          * XXX: hack to make sure we're chrp, assume that if we're
682          *      chrp we have a device_type property -- Cort
683          */
684         node = call_prom("finddevice", 1, 1, "/");
685         if ((int)call_prom("getprop", 4, 1, node,
686                            "device_type",type, sizeof(type)) <= 0)
687                 return;
688
689         /* copy the holding pattern code to someplace safe (0) */
690         /* the holding pattern is now within the first 0x100
691            bytes of the kernel image -- paulus */
692         memcpy((void *)0, _stext, 0x100);
693         flush_icache_range(0, 0x100);
694
695         /* look for cpus */
696         *(unsigned long *)(0x0) = 0;
697         asm volatile("dcbf 0,%0": : "r" (0) : "memory");
698         for (node = 0; prom_next_node(&node); ) {
699                 type[0] = 0;
700                 call_prom("getprop", 4, 1, node, "device_type",
701                           type, sizeof(type));
702                 if (strcmp(type, "cpu") != 0)
703                         continue;
704                 path = (char *) mem;
705                 memset(path, 0, 256);
706                 if ((int) call_prom("package-to-path", 3, 1,
707                                     node, path, 255) < 0)
708                         continue;
709                 reg = -1;
710                 call_prom("getprop", 4, 1, node, "reg", &reg, sizeof(reg));
711                 cpu = smp_chrp_cpu_nr++;
712 #ifdef CONFIG_SMP
713                 smp_hw_index[cpu] = reg;
714 #endif /* CONFIG_SMP */
715                 /* XXX: hack - don't start cpu 0, this cpu -- Cort */
716                 if (cpu == 0)
717                         continue;
718                 prom_print("starting cpu ");
719                 prom_print(path);
720                 *(ulong *)(0x4) = 0;
721                 call_prom("start-cpu", 3, 0, node,
722                           (char *)__secondary_hold - _stext, cpu);
723                 prom_print("...");
724                 for ( i = 0 ; (i < 10000) && (*(ulong *)(0x4) == 0); i++ )
725                         ;
726                 if (*(ulong *)(0x4) == cpu)
727                         prom_print("ok\n");
728                 else {
729                         prom_print("failed: ");
730                         prom_print_hex(*(ulong *)0x4);
731                         prom_print("\n");
732                 }
733         }
734 }
735
736 static void __init
737 prom_instantiate_rtas(void)
738 {
739         ihandle prom_rtas;
740         unsigned int i;
741         struct prom_args prom_args;
742
743         prom_rtas = call_prom("finddevice", 1, 1, "/rtas");
744         if (prom_rtas == (void *) -1)
745                 return;
746
747         rtas_size = 0;
748         call_prom("getprop", 4, 1, prom_rtas,
749                   "rtas-size", &rtas_size, sizeof(rtas_size));
750         prom_print("instantiating rtas");
751         if (rtas_size == 0) {
752                 rtas_data = 0;
753         } else {
754                 /*
755                  * Ask OF for some space for RTAS.
756                  * Actually OF has bugs so we just arbitrarily
757                  * use memory at the 6MB point.
758                  */
759                 rtas_data = 6 << 20;
760                 prom_print(" at ");
761                 prom_print_hex(rtas_data);
762         }
763
764         prom_rtas = call_prom("open", 1, 1, "/rtas");
765         prom_print("...");
766         prom_args.service = "call-method";
767         prom_args.nargs = 3;
768         prom_args.nret = 2;
769         prom_args.args[0] = "instantiate-rtas";
770         prom_args.args[1] = prom_rtas;
771         prom_args.args[2] = (void *) rtas_data;
772         prom(&prom_args);
773         i = 0;
774         if (prom_args.args[3] == 0)
775                 i = (unsigned int)prom_args.args[4];
776         rtas_entry = i;
777         if ((rtas_entry == -1) || (rtas_entry == 0))
778                 prom_print(" failed\n");
779         else
780                 prom_print(" done\n");
781 }
782
783 /*
784  * We enter here early on, when the Open Firmware prom is still
785  * handling exceptions and the MMU hash table for us.
786  */
787 unsigned long __init
788 prom_init(int r3, int r4, prom_entry pp)
789 {
790         unsigned long mem;
791         ihandle prom_mmu;
792         unsigned long offset = reloc_offset();
793         int i, l;
794         char *p, *d;
795         unsigned long phys;
796         void *result[3];
797
798         /* Default */
799         phys = (unsigned long) &_stext;
800
801         /* First get a handle for the stdout device */
802         prom = pp;
803         prom_chosen = call_prom("finddevice", 1, 1, "/chosen");
804         if (prom_chosen == (void *)-1)
805                 prom_exit();
806         if ((int) call_prom("getprop", 4, 1, prom_chosen,
807                             "stdout", &prom_stdout,
808                             sizeof(prom_stdout)) <= 0)
809                 prom_exit();
810
811         /* Get the full OF pathname of the stdout device */
812         mem = (unsigned long) klimit + offset;
813         p = (char *) mem;
814         memset(p, 0, 256);
815         call_prom("instance-to-path", 3, 1, prom_stdout, p, 255);
816         of_stdout_device = p;
817         mem += strlen(p) + 1;
818
819         /* Get the boot device and translate it to a full OF pathname. */
820         p = (char *) mem;
821         l = (int) call_prom("getprop", 4, 1, prom_chosen,
822                             "bootpath", p, 1<<20);
823         if (l > 0) {
824                 p[l] = 0;       /* should already be null-terminated */
825                 bootpath = PTRUNRELOC(p);
826                 mem += l + 1;
827                 d = (char *) mem;
828                 *d = 0;
829                 call_prom("canon", 3, 1, p, d, 1<<20);
830                 bootdevice = PTRUNRELOC(d);
831                 mem = ALIGNUL(mem + strlen(d) + 1);
832         }
833
834         prom_instantiate_rtas();
835
836 #ifdef CONFIG_POWER4
837         /*
838          * Find out how much memory we have and allocate a
839          * suitably-sized hash table.
840          */
841         prom_alloc_htab();
842 #endif
843         mem = check_display(mem);
844
845         prom_print("copying OF device tree...");
846         mem = copy_device_tree(mem, mem + (1<<20));
847         prom_print("done\n");
848
849         prom_hold_cpus(mem);
850
851         klimit = (char *) (mem - offset);
852
853         /* If we are already running at 0xc0000000, we assume we were
854          * loaded by an OF bootloader which did set a BAT for us.
855          * This breaks OF translate so we force phys to be 0.
856          */
857         if (offset == 0) {
858                 prom_print("(already at 0xc0000000) phys=0\n");
859                 phys = 0;
860         } else if ((int) call_prom("getprop", 4, 1, prom_chosen, "mmu",
861                                  &prom_mmu, sizeof(prom_mmu)) <= 0) {
862                 prom_print(" no MMU found\n");
863         } else if ((int)call_prom_ret("call-method", 4, 4, result, "translate",
864                                       prom_mmu, &_stext, 1) != 0) {
865                 prom_print(" (translate failed)\n");
866         } else {
867                 /* We assume the phys. address size is 3 cells */
868                 phys = (unsigned long)result[2];
869         }
870
871         if (prom_disp_node != 0)
872                 setup_disp_fake_bi(prom_disp_node);
873
874         /* Use quiesce call to get OF to shut down any devices it's using */
875         prom_print("Calling quiesce ...\n");
876         call_prom("quiesce", 0, 0);
877
878         /* Relocate various pointers which will be used once the
879            kernel is running at the address it was linked at. */
880         for (i = 0; i < prom_num_displays; ++i)
881                 prom_display_paths[i] = PTRUNRELOC(prom_display_paths[i]);
882
883         prom_print("returning 0x");
884         prom_print_hex(phys);
885         prom_print("from prom_init\n");
886         prom_stdout = 0;
887
888         return phys;
889 }
890
891 /*
892  * early_get_property is used to access the device tree image prepared
893  * by BootX very early on, before the pointers in it have been relocated.
894  */
895 static void * __init
896 early_get_property(unsigned long base, unsigned long node, char *prop)
897 {
898         struct device_node *np = (struct device_node *)(base + node);
899         struct property *pp;
900
901         for (pp = np->properties; pp != 0; pp = pp->next) {
902                 pp = (struct property *) (base + (unsigned long)pp);
903                 if (strcmp((char *)((unsigned long)pp->name + base),
904                            prop) == 0) {
905                         return (void *)((unsigned long)pp->value + base);
906                 }
907         }
908         return 0;
909 }
910
911 /* Is boot-info compatible ? */
912 #define BOOT_INFO_IS_COMPATIBLE(bi)             ((bi)->compatible_version <= BOOT_INFO_VERSION)
913 #define BOOT_INFO_IS_V2_COMPATIBLE(bi)  ((bi)->version >= 2)
914 #define BOOT_INFO_IS_V4_COMPATIBLE(bi)  ((bi)->version >= 4)
915
916 void __init
917 bootx_init(unsigned long r4, unsigned long phys)
918 {
919         boot_infos_t *bi = (boot_infos_t *) r4;
920         unsigned long space;
921         unsigned long ptr, x;
922         char *model;
923
924         boot_infos = PTRUNRELOC(bi);
925         if (!BOOT_INFO_IS_V2_COMPATIBLE(bi))
926                 bi->logicalDisplayBase = 0;
927
928 #ifdef CONFIG_BOOTX_TEXT
929         btext_init(bi);
930
931         /*
932          * Test if boot-info is compatible.  Done only in config
933          * CONFIG_BOOTX_TEXT since there is nothing much we can do
934          * with an incompatible version, except display a message
935          * and eventually hang the processor...
936          *
937          * I'll try to keep enough of boot-info compatible in the
938          * future to always allow display of this message;
939          */
940         if (!BOOT_INFO_IS_COMPATIBLE(bi)) {
941                 btext_drawstring(" !!! WARNING - Incompatible version of BootX !!!\n\n\n");
942                 btext_flushscreen();
943         }
944 #endif  /* CONFIG_BOOTX_TEXT */
945
946         /* New BootX enters kernel with MMU off, i/os are not allowed
947            here. This hack will have been done by the boostrap anyway.
948         */
949         if (bi->version < 4) {
950                 /*
951                  * XXX If this is an iMac, turn off the USB controller.
952                  */
953                 model = (char *) early_get_property
954                         (r4 + bi->deviceTreeOffset, 4, "model");
955                 if (model
956                     && (strcmp(model, "iMac,1") == 0
957                         || strcmp(model, "PowerMac1,1") == 0)) {
958                         out_le32((unsigned *)0x80880008, 1);    /* XXX */
959                 }
960         }
961
962         /* Move klimit to enclose device tree, args, ramdisk, etc... */
963         if (bi->version < 5) {
964                 space = bi->deviceTreeOffset + bi->deviceTreeSize;
965                 if (bi->ramDisk)
966                         space = bi->ramDisk + bi->ramDiskSize;
967         } else
968                 space = bi->totalParamsSize;
969         klimit = PTRUNRELOC((char *) bi + space);
970
971         /* New BootX will have flushed all TLBs and enters kernel with
972            MMU switched OFF, so this should not be useful anymore.
973         */
974         if (bi->version < 4) {
975                 /*
976                  * Touch each page to make sure the PTEs for them
977                  * are in the hash table - the aim is to try to avoid
978                  * getting DSI exceptions while copying the kernel image.
979                  */
980                 for (ptr = ((unsigned long) &_stext) & PAGE_MASK;
981                      ptr < (unsigned long)bi + space; ptr += PAGE_SIZE)
982                         x = *(volatile unsigned long *)ptr;
983         }
984
985 #ifdef CONFIG_BOOTX_TEXT
986         /*
987          * Note that after we call btext_prepare_BAT, we can't do
988          * prom_draw*, flushscreen or clearscreen until we turn the MMU
989          * on, since btext_prepare_BAT sets disp_bi.logicalDisplayBase
990          * to a virtual address.
991          */
992         btext_prepare_BAT();
993 #endif
994 }