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