Merge master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.git] / arch / ppc64 / kernel / setup.c
1 /*
2  * 
3  * Common boot and setup code.
4  *
5  * Copyright (C) 2001 PPC64 Team, IBM Corp
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  */
12
13 #undef DEBUG
14
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/string.h>
18 #include <linux/sched.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/reboot.h>
22 #include <linux/delay.h>
23 #include <linux/initrd.h>
24 #include <linux/ide.h>
25 #include <linux/seq_file.h>
26 #include <linux/ioport.h>
27 #include <linux/console.h>
28 #include <linux/version.h>
29 #include <linux/tty.h>
30 #include <linux/root_dev.h>
31 #include <linux/notifier.h>
32 #include <linux/cpu.h>
33 #include <linux/unistd.h>
34 #include <linux/serial.h>
35 #include <linux/serial_8250.h>
36 #include <asm/io.h>
37 #include <asm/prom.h>
38 #include <asm/processor.h>
39 #include <asm/pgtable.h>
40 #include <asm/bootinfo.h>
41 #include <asm/smp.h>
42 #include <asm/elf.h>
43 #include <asm/machdep.h>
44 #include <asm/paca.h>
45 #include <asm/ppcdebug.h>
46 #include <asm/time.h>
47 #include <asm/cputable.h>
48 #include <asm/sections.h>
49 #include <asm/btext.h>
50 #include <asm/nvram.h>
51 #include <asm/setup.h>
52 #include <asm/system.h>
53 #include <asm/rtas.h>
54 #include <asm/iommu.h>
55 #include <asm/serial.h>
56 #include <asm/cache.h>
57 #include <asm/page.h>
58 #include <asm/mmu.h>
59 #include <asm/lmb.h>
60 #include <asm/iSeries/ItLpNaca.h>
61
62 #ifdef DEBUG
63 #define DBG(fmt...) udbg_printf(fmt)
64 #else
65 #define DBG(fmt...)
66 #endif
67
68 /*
69  * Here are some early debugging facilities. You can enable one
70  * but your kernel will not boot on anything else if you do so
71  */
72
73 /* This one is for use on LPAR machines that support an HVC console
74  * on vterm 0
75  */
76 extern void udbg_init_debug_lpar(void);
77 /* This one is for use on Apple G5 machines
78  */
79 extern void udbg_init_pmac_realmode(void);
80 /* That's RTAS panel debug */
81 extern void call_rtas_display_status_delay(unsigned char c);
82 /* Here's maple real mode debug */
83 extern void udbg_init_maple_realmode(void);
84
85 #define EARLY_DEBUG_INIT() do {} while(0)
86
87 #if 0
88 #define EARLY_DEBUG_INIT() udbg_init_debug_lpar()
89 #define EARLY_DEBUG_INIT() udbg_init_maple_realmode()
90 #define EARLY_DEBUG_INIT() udbg_init_pmac_realmode()
91 #define EARLY_DEBUG_INIT()                                              \
92         do { ppc_md.udbg_putc = call_rtas_display_status_delay; } while(0)
93 #endif
94
95 /* extern void *stab; */
96 extern unsigned long klimit;
97
98 extern void mm_init_ppc64(void);
99 extern void stab_initialize(unsigned long stab);
100 extern void htab_initialize(void);
101 extern void early_init_devtree(void *flat_dt);
102 extern void unflatten_device_tree(void);
103
104 extern void smp_release_cpus(void);
105
106 int have_of = 1;
107 int boot_cpuid = 0;
108 int boot_cpuid_phys = 0;
109 dev_t boot_dev;
110 u64 ppc64_pft_size;
111 u64 ppc64_debug_switch;
112
113 struct ppc64_caches ppc64_caches;
114 EXPORT_SYMBOL_GPL(ppc64_caches);
115
116 /*
117  * These are used in binfmt_elf.c to put aux entries on the stack
118  * for each elf executable being started.
119  */
120 int dcache_bsize;
121 int icache_bsize;
122 int ucache_bsize;
123
124 /* The main machine-dep calls structure
125  */
126 struct machdep_calls ppc_md;
127 EXPORT_SYMBOL(ppc_md);
128
129 #ifdef CONFIG_MAGIC_SYSRQ
130 unsigned long SYSRQ_KEY;
131 #endif /* CONFIG_MAGIC_SYSRQ */
132
133
134 static int ppc64_panic_event(struct notifier_block *, unsigned long, void *);
135 static struct notifier_block ppc64_panic_block = {
136         .notifier_call = ppc64_panic_event,
137         .priority = INT_MIN /* may not return; must be done last */
138 };
139
140 /*
141  * Perhaps we can put the pmac screen_info[] here
142  * on pmac as well so we don't need the ifdef's.
143  * Until we get multiple-console support in here
144  * that is.  -- Cort
145  * Maybe tie it to serial consoles, since this is really what
146  * these processors use on existing boards.  -- Dan
147  */ 
148 struct screen_info screen_info = {
149         .orig_x = 0,
150         .orig_y = 25,
151         .orig_video_cols = 80,
152         .orig_video_lines = 25,
153         .orig_video_isVGA = 1,
154         .orig_video_points = 16
155 };
156
157 /*
158  * Initialize the PPCDBG state.  Called before relocation has been enabled.
159  */
160 void __init ppcdbg_initialize(void)
161 {
162         ppc64_debug_switch = PPC_DEBUG_DEFAULT; /* | PPCDBG_BUSWALK | */
163         /* PPCDBG_PHBINIT | PPCDBG_MM | PPCDBG_MMINIT | PPCDBG_TCEINIT | PPCDBG_TCE */;
164 }
165
166 /*
167  * Early boot console based on udbg
168  */
169 static struct console udbg_console = {
170         .name   = "udbg",
171         .write  = udbg_console_write,
172         .flags  = CON_PRINTBUFFER,
173         .index  = -1,
174 };
175 static int early_console_initialized;
176
177 void __init disable_early_printk(void)
178 {
179         if (!early_console_initialized)
180                 return;
181         unregister_console(&udbg_console);
182         early_console_initialized = 0;
183 }
184
185 #if defined(CONFIG_PPC_MULTIPLATFORM) && defined(CONFIG_SMP)
186
187 static int smt_enabled_cmdline;
188
189 /* Look for ibm,smt-enabled OF option */
190 static void check_smt_enabled(void)
191 {
192         struct device_node *dn;
193         char *smt_option;
194
195         /* Allow the command line to overrule the OF option */
196         if (smt_enabled_cmdline)
197                 return;
198
199         dn = of_find_node_by_path("/options");
200
201         if (dn) {
202                 smt_option = (char *)get_property(dn, "ibm,smt-enabled", NULL);
203
204                 if (smt_option) {
205                         if (!strcmp(smt_option, "on"))
206                                 smt_enabled_at_boot = 1;
207                         else if (!strcmp(smt_option, "off"))
208                                 smt_enabled_at_boot = 0;
209                 }
210         }
211 }
212
213 /* Look for smt-enabled= cmdline option */
214 static int __init early_smt_enabled(char *p)
215 {
216         smt_enabled_cmdline = 1;
217
218         if (!p)
219                 return 0;
220
221         if (!strcmp(p, "on") || !strcmp(p, "1"))
222                 smt_enabled_at_boot = 1;
223         else if (!strcmp(p, "off") || !strcmp(p, "0"))
224                 smt_enabled_at_boot = 0;
225
226         return 0;
227 }
228 early_param("smt-enabled", early_smt_enabled);
229
230 /**
231  * setup_cpu_maps - initialize the following cpu maps:
232  *                  cpu_possible_map
233  *                  cpu_present_map
234  *                  cpu_sibling_map
235  *
236  * Having the possible map set up early allows us to restrict allocations
237  * of things like irqstacks to num_possible_cpus() rather than NR_CPUS.
238  *
239  * We do not initialize the online map here; cpus set their own bits in
240  * cpu_online_map as they come up.
241  *
242  * This function is valid only for Open Firmware systems.  finish_device_tree
243  * must be called before using this.
244  *
245  * While we're here, we may as well set the "physical" cpu ids in the paca.
246  */
247 static void __init setup_cpu_maps(void)
248 {
249         struct device_node *dn = NULL;
250         int cpu = 0;
251         int swap_cpuid = 0;
252
253         check_smt_enabled();
254
255         while ((dn = of_find_node_by_type(dn, "cpu")) && cpu < NR_CPUS) {
256                 u32 *intserv;
257                 int j, len = sizeof(u32), nthreads;
258
259                 intserv = (u32 *)get_property(dn, "ibm,ppc-interrupt-server#s",
260                                               &len);
261                 if (!intserv)
262                         intserv = (u32 *)get_property(dn, "reg", NULL);
263
264                 nthreads = len / sizeof(u32);
265
266                 for (j = 0; j < nthreads && cpu < NR_CPUS; j++) {
267                         cpu_set(cpu, cpu_present_map);
268                         set_hard_smp_processor_id(cpu, intserv[j]);
269
270                         if (intserv[j] == boot_cpuid_phys)
271                                 swap_cpuid = cpu;
272                         cpu_set(cpu, cpu_possible_map);
273                         cpu++;
274                 }
275         }
276
277         /* Swap CPU id 0 with boot_cpuid_phys, so we can always assume that
278          * boot cpu is logical 0.
279          */
280         if (boot_cpuid_phys != get_hard_smp_processor_id(0)) {
281                 u32 tmp;
282                 tmp = get_hard_smp_processor_id(0);
283                 set_hard_smp_processor_id(0, boot_cpuid_phys);
284                 set_hard_smp_processor_id(swap_cpuid, tmp);
285         }
286
287         /*
288          * On pSeries LPAR, we need to know how many cpus
289          * could possibly be added to this partition.
290          */
291         if (systemcfg->platform == PLATFORM_PSERIES_LPAR &&
292                                 (dn = of_find_node_by_path("/rtas"))) {
293                 int num_addr_cell, num_size_cell, maxcpus;
294                 unsigned int *ireg;
295
296                 num_addr_cell = prom_n_addr_cells(dn);
297                 num_size_cell = prom_n_size_cells(dn);
298
299                 ireg = (unsigned int *)
300                         get_property(dn, "ibm,lrdr-capacity", NULL);
301
302                 if (!ireg)
303                         goto out;
304
305                 maxcpus = ireg[num_addr_cell + num_size_cell];
306
307                 /* Double maxcpus for processors which have SMT capability */
308                 if (cpu_has_feature(CPU_FTR_SMT))
309                         maxcpus *= 2;
310
311                 if (maxcpus > NR_CPUS) {
312                         printk(KERN_WARNING
313                                "Partition configured for %d cpus, "
314                                "operating system maximum is %d.\n",
315                                maxcpus, NR_CPUS);
316                         maxcpus = NR_CPUS;
317                 } else
318                         printk(KERN_INFO "Partition configured for %d cpus.\n",
319                                maxcpus);
320
321                 for (cpu = 0; cpu < maxcpus; cpu++)
322                         cpu_set(cpu, cpu_possible_map);
323         out:
324                 of_node_put(dn);
325         }
326
327         /*
328          * Do the sibling map; assume only two threads per processor.
329          */
330         for_each_cpu(cpu) {
331                 cpu_set(cpu, cpu_sibling_map[cpu]);
332                 if (cpu_has_feature(CPU_FTR_SMT))
333                         cpu_set(cpu ^ 0x1, cpu_sibling_map[cpu]);
334         }
335
336         systemcfg->processorCount = num_present_cpus();
337 }
338 #endif /* defined(CONFIG_PPC_MULTIPLATFORM) && defined(CONFIG_SMP) */
339
340
341 #ifdef CONFIG_PPC_MULTIPLATFORM
342
343 extern struct machdep_calls pSeries_md;
344 extern struct machdep_calls pmac_md;
345 extern struct machdep_calls maple_md;
346 extern struct machdep_calls bpa_md;
347
348 /* Ultimately, stuff them in an elf section like initcalls... */
349 static struct machdep_calls __initdata *machines[] = {
350 #ifdef CONFIG_PPC_PSERIES
351         &pSeries_md,
352 #endif /* CONFIG_PPC_PSERIES */
353 #ifdef CONFIG_PPC_PMAC
354         &pmac_md,
355 #endif /* CONFIG_PPC_PMAC */
356 #ifdef CONFIG_PPC_MAPLE
357         &maple_md,
358 #endif /* CONFIG_PPC_MAPLE */
359 #ifdef CONFIG_PPC_BPA
360         &bpa_md,
361 #endif
362         NULL
363 };
364
365 /*
366  * Early initialization entry point. This is called by head.S
367  * with MMU translation disabled. We rely on the "feature" of
368  * the CPU that ignores the top 2 bits of the address in real
369  * mode so we can access kernel globals normally provided we
370  * only toy with things in the RMO region. From here, we do
371  * some early parsing of the device-tree to setup out LMB
372  * data structures, and allocate & initialize the hash table
373  * and segment tables so we can start running with translation
374  * enabled.
375  *
376  * It is this function which will call the probe() callback of
377  * the various platform types and copy the matching one to the
378  * global ppc_md structure. Your platform can eventually do
379  * some very early initializations from the probe() routine, but
380  * this is not recommended, be very careful as, for example, the
381  * device-tree is not accessible via normal means at this point.
382  */
383
384 void __init early_setup(unsigned long dt_ptr)
385 {
386         struct paca_struct *lpaca = get_paca();
387         static struct machdep_calls **mach;
388
389         /*
390          * Enable early debugging if any specified (see top of
391          * this file)
392          */
393         EARLY_DEBUG_INIT();
394
395         DBG(" -> early_setup()\n");
396
397         /*
398          * Fill the default DBG level (do we want to keep
399          * that old mecanism around forever ?)
400          */
401         ppcdbg_initialize();
402
403         /*
404          * Do early initializations using the flattened device
405          * tree, like retreiving the physical memory map or
406          * calculating/retreiving the hash table size
407          */
408         early_init_devtree(__va(dt_ptr));
409
410         /*
411          * Iterate all ppc_md structures until we find the proper
412          * one for the current machine type
413          */
414         DBG("Probing machine type for platform %x...\n",
415             systemcfg->platform);
416
417         for (mach = machines; *mach; mach++) {
418                 if ((*mach)->probe(systemcfg->platform))
419                         break;
420         }
421         /* What can we do if we didn't find ? */
422         if (*mach == NULL) {
423                 DBG("No suitable machine found !\n");
424                 for (;;);
425         }
426         ppc_md = **mach;
427
428         /* our udbg callbacks got overriden by the above, let's put them
429          * back in. Ultimately, I want those things to be split from the
430          * main ppc_md
431          */
432         EARLY_DEBUG_INIT();
433
434         DBG("Found, Initializing memory management...\n");
435
436         /*
437          * Initialize stab / SLB management
438          */
439         stab_initialize(lpaca->stab_real);
440
441         /*
442          * Initialize the MMU Hash table and create the linear mapping
443          * of memory
444          */
445         htab_initialize();
446
447         DBG(" <- early_setup()\n");
448 }
449
450
451 /*
452  * Initialize some remaining members of the ppc64_caches and systemcfg structures
453  * (at least until we get rid of them completely). This is mostly some
454  * cache informations about the CPU that will be used by cache flush
455  * routines and/or provided to userland
456  */
457 static void __init initialize_cache_info(void)
458 {
459         struct device_node *np;
460         unsigned long num_cpus = 0;
461
462         DBG(" -> initialize_cache_info()\n");
463
464         for (np = NULL; (np = of_find_node_by_type(np, "cpu"));) {
465                 num_cpus += 1;
466
467                 /* We're assuming *all* of the CPUs have the same
468                  * d-cache and i-cache sizes... -Peter
469                  */
470
471                 if ( num_cpus == 1 ) {
472                         u32 *sizep, *lsizep;
473                         u32 size, lsize;
474                         const char *dc, *ic;
475
476                         /* Then read cache informations */
477                         if (systemcfg->platform == PLATFORM_POWERMAC) {
478                                 dc = "d-cache-block-size";
479                                 ic = "i-cache-block-size";
480                         } else {
481                                 dc = "d-cache-line-size";
482                                 ic = "i-cache-line-size";
483                         }
484
485                         size = 0;
486                         lsize = cur_cpu_spec->dcache_bsize;
487                         sizep = (u32 *)get_property(np, "d-cache-size", NULL);
488                         if (sizep != NULL)
489                                 size = *sizep;
490                         lsizep = (u32 *) get_property(np, dc, NULL);
491                         if (lsizep != NULL)
492                                 lsize = *lsizep;
493                         if (sizep == 0 || lsizep == 0)
494                                 DBG("Argh, can't find dcache properties ! "
495                                     "sizep: %p, lsizep: %p\n", sizep, lsizep);
496
497                         systemcfg->dcache_size = ppc64_caches.dsize = size;
498                         systemcfg->dcache_line_size =
499                                 ppc64_caches.dline_size = lsize;
500                         ppc64_caches.log_dline_size = __ilog2(lsize);
501                         ppc64_caches.dlines_per_page = PAGE_SIZE / lsize;
502
503                         size = 0;
504                         lsize = cur_cpu_spec->icache_bsize;
505                         sizep = (u32 *)get_property(np, "i-cache-size", NULL);
506                         if (sizep != NULL)
507                                 size = *sizep;
508                         lsizep = (u32 *)get_property(np, ic, NULL);
509                         if (lsizep != NULL)
510                                 lsize = *lsizep;
511                         if (sizep == 0 || lsizep == 0)
512                                 DBG("Argh, can't find icache properties ! "
513                                     "sizep: %p, lsizep: %p\n", sizep, lsizep);
514
515                         systemcfg->icache_size = ppc64_caches.isize = size;
516                         systemcfg->icache_line_size =
517                                 ppc64_caches.iline_size = lsize;
518                         ppc64_caches.log_iline_size = __ilog2(lsize);
519                         ppc64_caches.ilines_per_page = PAGE_SIZE / lsize;
520                 }
521         }
522
523         /* Add an eye catcher and the systemcfg layout version number */
524         strcpy(systemcfg->eye_catcher, "SYSTEMCFG:PPC64");
525         systemcfg->version.major = SYSTEMCFG_MAJOR;
526         systemcfg->version.minor = SYSTEMCFG_MINOR;
527         systemcfg->processor = mfspr(SPRN_PVR);
528
529         DBG(" <- initialize_cache_info()\n");
530 }
531
532 static void __init check_for_initrd(void)
533 {
534 #ifdef CONFIG_BLK_DEV_INITRD
535         u64 *prop;
536
537         DBG(" -> check_for_initrd()\n");
538
539         prop = (u64 *)get_property(of_chosen, "linux,initrd-start", NULL);
540         if (prop != NULL) {
541                 initrd_start = (unsigned long)__va(*prop);
542                 prop = (u64 *)get_property(of_chosen, "linux,initrd-end", NULL);
543                 if (prop != NULL) {
544                         initrd_end = (unsigned long)__va(*prop);
545                         initrd_below_start_ok = 1;
546                 } else
547                         initrd_start = 0;
548         }
549
550         /* If we were passed an initrd, set the ROOT_DEV properly if the values
551          * look sensible. If not, clear initrd reference.
552          */
553         if (initrd_start >= KERNELBASE && initrd_end >= KERNELBASE &&
554             initrd_end > initrd_start)
555                 ROOT_DEV = Root_RAM0;
556         else
557                 initrd_start = initrd_end = 0;
558
559         if (initrd_start)
560                 printk("Found initrd at 0x%lx:0x%lx\n", initrd_start, initrd_end);
561
562         DBG(" <- check_for_initrd()\n");
563 #endif /* CONFIG_BLK_DEV_INITRD */
564 }
565
566 #endif /* CONFIG_PPC_MULTIPLATFORM */
567
568 /*
569  * Do some initial setup of the system.  The parameters are those which 
570  * were passed in from the bootloader.
571  */
572 void __init setup_system(void)
573 {
574         DBG(" -> setup_system()\n");
575
576 #ifdef CONFIG_PPC_ISERIES
577         /* pSeries systems are identified in prom.c via OF. */
578         if (itLpNaca.xLparInstalled == 1)
579                 systemcfg->platform = PLATFORM_ISERIES_LPAR;
580
581         ppc_md.init_early();
582 #else /* CONFIG_PPC_ISERIES */
583
584         /*
585          * Unflatten the device-tree passed by prom_init or kexec
586          */
587         unflatten_device_tree();
588
589         /*
590          * Fill the ppc64_caches & systemcfg structures with informations
591          * retreived from the device-tree. Need to be called before
592          * finish_device_tree() since the later requires some of the
593          * informations filled up here to properly parse the interrupt
594          * tree.
595          * It also sets up the cache line sizes which allows to call
596          * routines like flush_icache_range (used by the hash init
597          * later on).
598          */
599         initialize_cache_info();
600
601 #ifdef CONFIG_PPC_RTAS
602         /*
603          * Initialize RTAS if available
604          */
605         rtas_initialize();
606 #endif /* CONFIG_PPC_RTAS */
607
608         /*
609          * Check if we have an initrd provided via the device-tree
610          */
611         check_for_initrd();
612
613         /*
614          * Do some platform specific early initializations, that includes
615          * setting up the hash table pointers. It also sets up some interrupt-mapping
616          * related options that will be used by finish_device_tree()
617          */
618         ppc_md.init_early();
619
620         /*
621          * "Finish" the device-tree, that is do the actual parsing of
622          * some of the properties like the interrupt map
623          */
624         finish_device_tree();
625
626         /*
627          * Initialize xmon
628          */
629 #ifdef CONFIG_XMON_DEFAULT
630         xmon_init();
631 #endif
632         /*
633          * Register early console
634          */
635         early_console_initialized = 1;
636         register_console(&udbg_console);
637
638         /* Save unparsed command line copy for /proc/cmdline */
639         strlcpy(saved_command_line, cmd_line, COMMAND_LINE_SIZE);
640
641         parse_early_param();
642 #endif /* !CONFIG_PPC_ISERIES */
643
644 #if defined(CONFIG_SMP) && !defined(CONFIG_PPC_ISERIES)
645         /*
646          * iSeries has already initialized the cpu maps at this point.
647          */
648         setup_cpu_maps();
649
650         /* Release secondary cpus out of their spinloops at 0x60 now that
651          * we can map physical -> logical CPU ids
652          */
653         smp_release_cpus();
654 #endif /* defined(CONFIG_SMP) && !defined(CONFIG_PPC_ISERIES) */
655
656         printk("Starting Linux PPC64 %s\n", UTS_RELEASE);
657
658         printk("-----------------------------------------------------\n");
659         printk("ppc64_pft_size                = 0x%lx\n", ppc64_pft_size);
660         printk("ppc64_debug_switch            = 0x%lx\n", ppc64_debug_switch);
661         printk("ppc64_interrupt_controller    = 0x%ld\n", ppc64_interrupt_controller);
662         printk("systemcfg                     = 0x%p\n", systemcfg);
663         printk("systemcfg->platform           = 0x%x\n", systemcfg->platform);
664         printk("systemcfg->processorCount     = 0x%lx\n", systemcfg->processorCount);
665         printk("systemcfg->physicalMemorySize = 0x%lx\n", systemcfg->physicalMemorySize);
666         printk("ppc64_caches.dcache_line_size = 0x%x\n",
667                         ppc64_caches.dline_size);
668         printk("ppc64_caches.icache_line_size = 0x%x\n",
669                         ppc64_caches.iline_size);
670         printk("htab_address                  = 0x%p\n", htab_address);
671         printk("htab_hash_mask                = 0x%lx\n", htab_hash_mask);
672         printk("-----------------------------------------------------\n");
673
674         mm_init_ppc64();
675
676         DBG(" <- setup_system()\n");
677 }
678
679 /* also used by kexec */
680 void machine_shutdown(void)
681 {
682         if (ppc_md.nvram_sync)
683                 ppc_md.nvram_sync();
684 }
685
686 void machine_restart(char *cmd)
687 {
688         machine_shutdown();
689         ppc_md.restart(cmd);
690 #ifdef CONFIG_SMP
691         smp_send_stop();
692 #endif
693         printk(KERN_EMERG "System Halted, OK to turn off power\n");
694         local_irq_disable();
695         while (1) ;
696 }
697
698 void machine_power_off(void)
699 {
700         machine_shutdown();
701         ppc_md.power_off();
702 #ifdef CONFIG_SMP
703         smp_send_stop();
704 #endif
705         printk(KERN_EMERG "System Halted, OK to turn off power\n");
706         local_irq_disable();
707         while (1) ;
708 }
709
710 void machine_halt(void)
711 {
712         machine_shutdown();
713         ppc_md.halt();
714 #ifdef CONFIG_SMP
715         smp_send_stop();
716 #endif
717         printk(KERN_EMERG "System Halted, OK to turn off power\n");
718         local_irq_disable();
719         while (1) ;
720 }
721
722 static int ppc64_panic_event(struct notifier_block *this,
723                              unsigned long event, void *ptr)
724 {
725         ppc_md.panic((char *)ptr);  /* May not return */
726         return NOTIFY_DONE;
727 }
728
729
730 #ifdef CONFIG_SMP
731 DEFINE_PER_CPU(unsigned int, pvr);
732 #endif
733
734 static int show_cpuinfo(struct seq_file *m, void *v)
735 {
736         unsigned long cpu_id = (unsigned long)v - 1;
737         unsigned int pvr;
738         unsigned short maj;
739         unsigned short min;
740
741         if (cpu_id == NR_CPUS) {
742                 seq_printf(m, "timebase\t: %lu\n", ppc_tb_freq);
743
744                 if (ppc_md.get_cpuinfo != NULL)
745                         ppc_md.get_cpuinfo(m);
746
747                 return 0;
748         }
749
750         /* We only show online cpus: disable preempt (overzealous, I
751          * knew) to prevent cpu going down. */
752         preempt_disable();
753         if (!cpu_online(cpu_id)) {
754                 preempt_enable();
755                 return 0;
756         }
757
758 #ifdef CONFIG_SMP
759         pvr = per_cpu(pvr, cpu_id);
760 #else
761         pvr = mfspr(SPRN_PVR);
762 #endif
763         maj = (pvr >> 8) & 0xFF;
764         min = pvr & 0xFF;
765
766         seq_printf(m, "processor\t: %lu\n", cpu_id);
767         seq_printf(m, "cpu\t\t: ");
768
769         if (cur_cpu_spec->pvr_mask)
770                 seq_printf(m, "%s", cur_cpu_spec->cpu_name);
771         else
772                 seq_printf(m, "unknown (%08x)", pvr);
773
774 #ifdef CONFIG_ALTIVEC
775         if (cpu_has_feature(CPU_FTR_ALTIVEC))
776                 seq_printf(m, ", altivec supported");
777 #endif /* CONFIG_ALTIVEC */
778
779         seq_printf(m, "\n");
780
781         /*
782          * Assume here that all clock rates are the same in a
783          * smp system.  -- Cort
784          */
785         seq_printf(m, "clock\t\t: %lu.%06luMHz\n", ppc_proc_freq / 1000000,
786                    ppc_proc_freq % 1000000);
787
788         seq_printf(m, "revision\t: %hd.%hd\n\n", maj, min);
789
790         preempt_enable();
791         return 0;
792 }
793
794 static void *c_start(struct seq_file *m, loff_t *pos)
795 {
796         return *pos <= NR_CPUS ? (void *)((*pos)+1) : NULL;
797 }
798 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
799 {
800         ++*pos;
801         return c_start(m, pos);
802 }
803 static void c_stop(struct seq_file *m, void *v)
804 {
805 }
806 struct seq_operations cpuinfo_op = {
807         .start =c_start,
808         .next = c_next,
809         .stop = c_stop,
810         .show = show_cpuinfo,
811 };
812
813 /*
814  * These three variables are used to save values passed to us by prom_init()
815  * via the device tree. The TCE variables are needed because with a memory_limit
816  * in force we may need to explicitly map the TCE are at the top of RAM.
817  */
818 unsigned long memory_limit;
819 unsigned long tce_alloc_start;
820 unsigned long tce_alloc_end;
821
822 #ifdef CONFIG_PPC_ISERIES
823 /*
824  * On iSeries we just parse the mem=X option from the command line.
825  * On pSeries it's a bit more complicated, see prom_init_mem()
826  */
827 static int __init early_parsemem(char *p)
828 {
829         if (!p)
830                 return 0;
831
832         memory_limit = ALIGN(memparse(p, &p), PAGE_SIZE);
833
834         return 0;
835 }
836 early_param("mem", early_parsemem);
837 #endif /* CONFIG_PPC_ISERIES */
838
839 #ifdef CONFIG_PPC_MULTIPLATFORM
840 static int __init set_preferred_console(void)
841 {
842         struct device_node *prom_stdout = NULL;
843         char *name;
844         u32 *spd;
845         int offset = 0;
846
847         DBG(" -> set_preferred_console()\n");
848
849         /* The user has requested a console so this is already set up. */
850         if (strstr(saved_command_line, "console=")) {
851                 DBG(" console was specified !\n");
852                 return -EBUSY;
853         }
854
855         if (!of_chosen) {
856                 DBG(" of_chosen is NULL !\n");
857                 return -ENODEV;
858         }
859         /* We are getting a weird phandle from OF ... */
860         /* ... So use the full path instead */
861         name = (char *)get_property(of_chosen, "linux,stdout-path", NULL);
862         if (name == NULL) {
863                 DBG(" no linux,stdout-path !\n");
864                 return -ENODEV;
865         }
866         prom_stdout = of_find_node_by_path(name);
867         if (!prom_stdout) {
868                 DBG(" can't find stdout package %s !\n", name);
869                 return -ENODEV;
870         }       
871         DBG("stdout is %s\n", prom_stdout->full_name);
872
873         name = (char *)get_property(prom_stdout, "name", NULL);
874         if (!name) {
875                 DBG(" stdout package has no name !\n");
876                 goto not_found;
877         }
878         spd = (u32 *)get_property(prom_stdout, "current-speed", NULL);
879
880         if (0)
881                 ;
882 #ifdef CONFIG_SERIAL_8250_CONSOLE
883         else if (strcmp(name, "serial") == 0) {
884                 int i;
885                 u32 *reg = (u32 *)get_property(prom_stdout, "reg", &i);
886                 if (i > 8) {
887                         switch (reg[1]) {
888                                 case 0x3f8:
889                                         offset = 0;
890                                         break;
891                                 case 0x2f8:
892                                         offset = 1;
893                                         break;
894                                 case 0x898:
895                                         offset = 2;
896                                         break;
897                                 case 0x890:
898                                         offset = 3;
899                                         break;
900                                 default:
901                                         /* We dont recognise the serial port */
902                                         goto not_found;
903                         }
904                 }
905         }
906 #endif /* CONFIG_SERIAL_8250_CONSOLE */
907 #ifdef CONFIG_PPC_PSERIES
908         else if (strcmp(name, "vty") == 0) {
909                 u32 *reg = (u32 *)get_property(prom_stdout, "reg", NULL);
910                 char *compat = (char *)get_property(prom_stdout, "compatible", NULL);
911
912                 if (reg && compat && (strcmp(compat, "hvterm-protocol") == 0)) {
913                         /* Host Virtual Serial Interface */
914                         int offset;
915                         switch (reg[0]) {
916                                 case 0x30000000:
917                                         offset = 0;
918                                         break;
919                                 case 0x30000001:
920                                         offset = 1;
921                                         break;
922                                 default:
923                                         goto not_found;
924                         }
925                         of_node_put(prom_stdout);
926                         DBG("Found hvsi console at offset %d\n", offset);
927                         return add_preferred_console("hvsi", offset, NULL);
928                 } else {
929                         /* pSeries LPAR virtual console */
930                         of_node_put(prom_stdout);
931                         DBG("Found hvc console\n");
932                         return add_preferred_console("hvc", 0, NULL);
933                 }
934         }
935 #endif /* CONFIG_PPC_PSERIES */
936 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
937         else if (strcmp(name, "ch-a") == 0)
938                 offset = 0;
939         else if (strcmp(name, "ch-b") == 0)
940                 offset = 1;
941 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
942         else
943                 goto not_found;
944         of_node_put(prom_stdout);
945
946         DBG("Found serial console at ttyS%d\n", offset);
947
948         if (spd) {
949                 static char __initdata opt[16];
950                 sprintf(opt, "%d", *spd);
951                 return add_preferred_console("ttyS", offset, opt);
952         } else
953                 return add_preferred_console("ttyS", offset, NULL);
954
955  not_found:
956         DBG("No preferred console found !\n");
957         of_node_put(prom_stdout);
958         return -ENODEV;
959 }
960 console_initcall(set_preferred_console);
961 #endif /* CONFIG_PPC_MULTIPLATFORM */
962
963 #ifdef CONFIG_IRQSTACKS
964 static void __init irqstack_early_init(void)
965 {
966         unsigned int i;
967
968         /*
969          * interrupt stacks must be under 256MB, we cannot afford to take
970          * SLB misses on them.
971          */
972         for_each_cpu(i) {
973                 softirq_ctx[i] = (struct thread_info *)__va(lmb_alloc_base(THREAD_SIZE,
974                                         THREAD_SIZE, 0x10000000));
975                 hardirq_ctx[i] = (struct thread_info *)__va(lmb_alloc_base(THREAD_SIZE,
976                                         THREAD_SIZE, 0x10000000));
977         }
978 }
979 #else
980 #define irqstack_early_init()
981 #endif
982
983 /*
984  * Stack space used when we detect a bad kernel stack pointer, and
985  * early in SMP boots before relocation is enabled.
986  */
987 static void __init emergency_stack_init(void)
988 {
989         unsigned long limit;
990         unsigned int i;
991
992         /*
993          * Emergency stacks must be under 256MB, we cannot afford to take
994          * SLB misses on them. The ABI also requires them to be 128-byte
995          * aligned.
996          *
997          * Since we use these as temporary stacks during secondary CPU
998          * bringup, we need to get at them in real mode. This means they
999          * must also be within the RMO region.
1000          */
1001         limit = min(0x10000000UL, lmb.rmo_size);
1002
1003         for_each_cpu(i)
1004                 paca[i].emergency_sp = __va(lmb_alloc_base(PAGE_SIZE, 128,
1005                                                 limit)) + PAGE_SIZE;
1006 }
1007
1008 /*
1009  * Called from setup_arch to initialize the bitmap of available
1010  * syscalls in the systemcfg page
1011  */
1012 void __init setup_syscall_map(void)
1013 {
1014         unsigned int i, count64 = 0, count32 = 0;
1015         extern unsigned long *sys_call_table;
1016         extern unsigned long *sys_call_table32;
1017         extern unsigned long sys_ni_syscall;
1018
1019
1020         for (i = 0; i < __NR_syscalls; i++) {
1021                 if (sys_call_table[i] == sys_ni_syscall)
1022                         continue;
1023                 count64++;
1024                 systemcfg->syscall_map_64[i >> 5] |= 0x80000000UL >> (i & 0x1f);
1025         }
1026         for (i = 0; i < __NR_syscalls; i++) {
1027                 if (sys_call_table32[i] == sys_ni_syscall)
1028                         continue;
1029                 count32++;
1030                 systemcfg->syscall_map_32[i >> 5] |= 0x80000000UL >> (i & 0x1f);
1031         }
1032         printk(KERN_INFO "Syscall map setup, %d 32 bits and %d 64 bits syscalls\n",
1033                count32, count64);
1034 }
1035
1036 /*
1037  * Called into from start_kernel, after lock_kernel has been called.
1038  * Initializes bootmem, which is unsed to manage page allocation until
1039  * mem_init is called.
1040  */
1041 void __init setup_arch(char **cmdline_p)
1042 {
1043         extern void do_init_bootmem(void);
1044
1045         ppc64_boot_msg(0x12, "Setup Arch");
1046
1047         *cmdline_p = cmd_line;
1048
1049         /*
1050          * Set cache line size based on type of cpu as a default.
1051          * Systems with OF can look in the properties on the cpu node(s)
1052          * for a possibly more accurate value.
1053          */
1054         dcache_bsize = ppc64_caches.dline_size;
1055         icache_bsize = ppc64_caches.iline_size;
1056
1057         /* reboot on panic */
1058         panic_timeout = 180;
1059
1060         if (ppc_md.panic)
1061                 notifier_chain_register(&panic_notifier_list, &ppc64_panic_block);
1062
1063         init_mm.start_code = PAGE_OFFSET;
1064         init_mm.end_code = (unsigned long) _etext;
1065         init_mm.end_data = (unsigned long) _edata;
1066         init_mm.brk = klimit;
1067         
1068         irqstack_early_init();
1069         emergency_stack_init();
1070
1071         stabs_alloc();
1072
1073         /* set up the bootmem stuff with available memory */
1074         do_init_bootmem();
1075         sparse_init();
1076
1077         /* initialize the syscall map in systemcfg */
1078         setup_syscall_map();
1079
1080         ppc_md.setup_arch();
1081
1082         /* Use the default idle loop if the platform hasn't provided one. */
1083         if (NULL == ppc_md.idle_loop) {
1084                 ppc_md.idle_loop = default_idle;
1085                 printk(KERN_INFO "Using default idle loop\n");
1086         }
1087
1088         paging_init();
1089         ppc64_boot_msg(0x15, "Setup Done");
1090 }
1091
1092
1093 /* ToDo: do something useful if ppc_md is not yet setup. */
1094 #define PPC64_LINUX_FUNCTION 0x0f000000
1095 #define PPC64_IPL_MESSAGE 0xc0000000
1096 #define PPC64_TERM_MESSAGE 0xb0000000
1097 #define PPC64_ATTN_MESSAGE 0xa0000000
1098 #define PPC64_DUMP_MESSAGE 0xd0000000
1099
1100 static void ppc64_do_msg(unsigned int src, const char *msg)
1101 {
1102         if (ppc_md.progress) {
1103                 char buf[128];
1104
1105                 sprintf(buf, "%08X\n", src);
1106                 ppc_md.progress(buf, 0);
1107                 snprintf(buf, 128, "%s", msg);
1108                 ppc_md.progress(buf, 0);
1109         }
1110 }
1111
1112 /* Print a boot progress message. */
1113 void ppc64_boot_msg(unsigned int src, const char *msg)
1114 {
1115         ppc64_do_msg(PPC64_LINUX_FUNCTION|PPC64_IPL_MESSAGE|src, msg);
1116         printk("[boot]%04x %s\n", src, msg);
1117 }
1118
1119 /* Print a termination message (print only -- does not stop the kernel) */
1120 void ppc64_terminate_msg(unsigned int src, const char *msg)
1121 {
1122         ppc64_do_msg(PPC64_LINUX_FUNCTION|PPC64_TERM_MESSAGE|src, msg);
1123         printk("[terminate]%04x %s\n", src, msg);
1124 }
1125
1126 /* Print something that needs attention (device error, etc) */
1127 void ppc64_attention_msg(unsigned int src, const char *msg)
1128 {
1129         ppc64_do_msg(PPC64_LINUX_FUNCTION|PPC64_ATTN_MESSAGE|src, msg);
1130         printk("[attention]%04x %s\n", src, msg);
1131 }
1132
1133 /* Print a dump progress message. */
1134 void ppc64_dump_msg(unsigned int src, const char *msg)
1135 {
1136         ppc64_do_msg(PPC64_LINUX_FUNCTION|PPC64_DUMP_MESSAGE|src, msg);
1137         printk("[dump]%04x %s\n", src, msg);
1138 }
1139
1140 /* This should only be called on processor 0 during calibrate decr */
1141 void __init setup_default_decr(void)
1142 {
1143         struct paca_struct *lpaca = get_paca();
1144
1145         lpaca->default_decr = tb_ticks_per_jiffy;
1146         lpaca->next_jiffy_update_tb = get_tb() + tb_ticks_per_jiffy;
1147 }
1148
1149 #ifndef CONFIG_PPC_ISERIES
1150 /*
1151  * This function can be used by platforms to "find" legacy serial ports.
1152  * It works for "serial" nodes under an "isa" node, and will try to
1153  * respect the "ibm,aix-loc" property if any. It works with up to 8
1154  * ports.
1155  */
1156
1157 #define MAX_LEGACY_SERIAL_PORTS 8
1158 static struct plat_serial8250_port serial_ports[MAX_LEGACY_SERIAL_PORTS+1];
1159 static unsigned int old_serial_count;
1160
1161 void __init generic_find_legacy_serial_ports(u64 *physport,
1162                 unsigned int *default_speed)
1163 {
1164         struct device_node *np;
1165         u32 *sizeprop;
1166
1167         struct isa_reg_property {
1168                 u32 space;
1169                 u32 address;
1170                 u32 size;
1171         };
1172         struct pci_reg_property {
1173                 struct pci_address addr;
1174                 u32 size_hi;
1175                 u32 size_lo;
1176         };                                                                        
1177
1178         DBG(" -> generic_find_legacy_serial_port()\n");
1179
1180         *physport = 0;
1181         if (default_speed)
1182                 *default_speed = 0;
1183
1184         np = of_find_node_by_path("/");
1185         if (!np)
1186                 return;
1187
1188         /* First fill our array */
1189         for (np = NULL; (np = of_find_node_by_type(np, "serial"));) {
1190                 struct device_node *isa, *pci;
1191                 struct isa_reg_property *reg;
1192                 unsigned long phys_size, addr_size, io_base;
1193                 u32 *rangesp;
1194                 u32 *interrupts, *clk, *spd;
1195                 char *typep;
1196                 int index, rlen, rentsize;
1197
1198                 /* Ok, first check if it's under an "isa" parent */
1199                 isa = of_get_parent(np);
1200                 if (!isa || strcmp(isa->name, "isa")) {
1201                         DBG("%s: no isa parent found\n", np->full_name);
1202                         continue;
1203                 }
1204                 
1205                 /* Now look for an "ibm,aix-loc" property that gives us ordering
1206                  * if any...
1207                  */
1208                 typep = (char *)get_property(np, "ibm,aix-loc", NULL);
1209
1210                 /* Get the ISA port number */
1211                 reg = (struct isa_reg_property *)get_property(np, "reg", NULL); 
1212                 if (reg == NULL)
1213                         goto next_port;
1214                 /* We assume the interrupt number isn't translated ... */
1215                 interrupts = (u32 *)get_property(np, "interrupts", NULL);
1216                 /* get clock freq. if present */
1217                 clk = (u32 *)get_property(np, "clock-frequency", NULL);
1218                 /* get default speed if present */
1219                 spd = (u32 *)get_property(np, "current-speed", NULL);
1220                 /* Default to locate at end of array */
1221                 index = old_serial_count; /* end of the array by default */
1222
1223                 /* If we have a location index, then use it */
1224                 if (typep && *typep == 'S') {
1225                         index = simple_strtol(typep+1, NULL, 0) - 1;
1226                         /* if index is out of range, use end of array instead */
1227                         if (index >= MAX_LEGACY_SERIAL_PORTS)
1228                                 index = old_serial_count;
1229                         /* if our index is still out of range, that mean that
1230                          * array is full, we could scan for a free slot but that
1231                          * make little sense to bother, just skip the port
1232                          */
1233                         if (index >= MAX_LEGACY_SERIAL_PORTS)
1234                                 goto next_port;
1235                         if (index >= old_serial_count)
1236                                 old_serial_count = index + 1;
1237                         /* Check if there is a port who already claimed our slot */
1238                         if (serial_ports[index].iobase != 0) {
1239                                 /* if we still have some room, move it, else override */
1240                                 if (old_serial_count < MAX_LEGACY_SERIAL_PORTS) {
1241                                         DBG("Moved legacy port %d -> %d\n", index,
1242                                             old_serial_count);
1243                                         serial_ports[old_serial_count++] =
1244                                                 serial_ports[index];
1245                                 } else {
1246                                         DBG("Replacing legacy port %d\n", index);
1247                                 }
1248                         }
1249                 }
1250                 if (index >= MAX_LEGACY_SERIAL_PORTS)
1251                         goto next_port;
1252                 if (index >= old_serial_count)
1253                         old_serial_count = index + 1;
1254
1255                 /* Now fill the entry */
1256                 memset(&serial_ports[index], 0, sizeof(struct plat_serial8250_port));
1257                 serial_ports[index].uartclk = clk ? *clk : BASE_BAUD * 16;
1258                 serial_ports[index].iobase = reg->address;
1259                 serial_ports[index].irq = interrupts ? interrupts[0] : 0;
1260                 serial_ports[index].flags = ASYNC_BOOT_AUTOCONF;
1261
1262                 DBG("Added legacy port, index: %d, port: %x, irq: %d, clk: %d\n",
1263                     index,
1264                     serial_ports[index].iobase,
1265                     serial_ports[index].irq,
1266                     serial_ports[index].uartclk);
1267
1268                 /* Get phys address of IO reg for port 1 */
1269                 if (index != 0)
1270                         goto next_port;
1271
1272                 pci = of_get_parent(isa);
1273                 if (!pci) {
1274                         DBG("%s: no pci parent found\n", np->full_name);
1275                         goto next_port;
1276                 }
1277
1278                 rangesp = (u32 *)get_property(pci, "ranges", &rlen);
1279                 if (rangesp == NULL) {
1280                         of_node_put(pci);
1281                         goto next_port;
1282                 }
1283                 rlen /= 4;
1284
1285                 /* we need the #size-cells of the PCI bridge node itself */
1286                 phys_size = 1;
1287                 sizeprop = (u32 *)get_property(pci, "#size-cells", NULL);
1288                 if (sizeprop != NULL)
1289                         phys_size = *sizeprop;
1290                 /* we need the parent #addr-cells */
1291                 addr_size = prom_n_addr_cells(pci);
1292                 rentsize = 3 + addr_size + phys_size;
1293                 io_base = 0;
1294                 for (;rlen >= rentsize; rlen -= rentsize,rangesp += rentsize) {
1295                         if (((rangesp[0] >> 24) & 0x3) != 1)
1296                                 continue; /* not IO space */
1297                         io_base = rangesp[3];
1298                         if (addr_size == 2)
1299                                 io_base = (io_base << 32) | rangesp[4];
1300                 }
1301                 if (io_base != 0) {
1302                         *physport = io_base + reg->address;
1303                         if (default_speed && spd)
1304                                 *default_speed = *spd;
1305                 }
1306                 of_node_put(pci);
1307         next_port:
1308                 of_node_put(isa);
1309         }
1310
1311         DBG(" <- generic_find_legacy_serial_port()\n");
1312 }
1313
1314 static struct platform_device serial_device = {
1315         .name   = "serial8250",
1316         .id     = 0,
1317         .dev    = {
1318                 .platform_data = serial_ports,
1319         },
1320 };
1321
1322 static int __init serial_dev_init(void)
1323 {
1324         return platform_device_register(&serial_device);
1325 }
1326 arch_initcall(serial_dev_init);
1327
1328 #endif /* CONFIG_PPC_ISERIES */
1329
1330 int check_legacy_ioport(unsigned long base_port)
1331 {
1332         if (ppc_md.check_legacy_ioport == NULL)
1333                 return 0;
1334         return ppc_md.check_legacy_ioport(base_port);
1335 }
1336 EXPORT_SYMBOL(check_legacy_ioport);
1337
1338 #ifdef CONFIG_XMON
1339 static int __init early_xmon(char *p)
1340 {
1341         /* ensure xmon is enabled */
1342         if (p) {
1343                 if (strncmp(p, "on", 2) == 0)
1344                         xmon_init();
1345                 if (strncmp(p, "early", 5) != 0)
1346                         return 0;
1347         }
1348         xmon_init();
1349         debugger(NULL);
1350
1351         return 0;
1352 }
1353 early_param("xmon", early_xmon);
1354 #endif
1355
1356 void cpu_die(void)
1357 {
1358         if (ppc_md.cpu_die)
1359                 ppc_md.cpu_die();
1360 }