Merge branch 'master' of /home/tglx/work/mtd/git/linux-2.6.git/
[linux-2.6] / arch / powerpc / kernel / setup-common.c
1 /*
2  * Common boot and setup code for both 32-bit and 64-bit.
3  * Extracted from arch/powerpc/kernel/setup_64.c.
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 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/sched.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/reboot.h>
19 #include <linux/delay.h>
20 #include <linux/initrd.h>
21 #include <linux/ide.h>
22 #include <linux/seq_file.h>
23 #include <linux/ioport.h>
24 #include <linux/console.h>
25 #include <linux/utsname.h>
26 #include <linux/tty.h>
27 #include <linux/root_dev.h>
28 #include <linux/notifier.h>
29 #include <linux/cpu.h>
30 #include <linux/unistd.h>
31 #include <linux/serial.h>
32 #include <linux/serial_8250.h>
33 #include <asm/io.h>
34 #include <asm/prom.h>
35 #include <asm/processor.h>
36 #include <asm/pgtable.h>
37 #include <asm/smp.h>
38 #include <asm/elf.h>
39 #include <asm/machdep.h>
40 #include <asm/time.h>
41 #include <asm/cputable.h>
42 #include <asm/sections.h>
43 #include <asm/btext.h>
44 #include <asm/nvram.h>
45 #include <asm/setup.h>
46 #include <asm/system.h>
47 #include <asm/rtas.h>
48 #include <asm/iommu.h>
49 #include <asm/serial.h>
50 #include <asm/cache.h>
51 #include <asm/page.h>
52 #include <asm/mmu.h>
53 #include <asm/lmb.h>
54
55 #undef DEBUG
56
57 #ifdef DEBUG
58 #define DBG(fmt...) udbg_printf(fmt)
59 #else
60 #define DBG(fmt...)
61 #endif
62
63 /*
64  * This still seems to be needed... -- paulus
65  */ 
66 struct screen_info screen_info = {
67         .orig_x = 0,
68         .orig_y = 25,
69         .orig_video_cols = 80,
70         .orig_video_lines = 25,
71         .orig_video_isVGA = 1,
72         .orig_video_points = 16
73 };
74
75 #ifdef __DO_IRQ_CANON
76 /* XXX should go elsewhere eventually */
77 int ppc_do_canonicalize_irqs;
78 EXPORT_SYMBOL(ppc_do_canonicalize_irqs);
79 #endif
80
81 /* also used by kexec */
82 void machine_shutdown(void)
83 {
84         if (ppc_md.nvram_sync)
85                 ppc_md.nvram_sync();
86 }
87
88 void machine_restart(char *cmd)
89 {
90         machine_shutdown();
91         ppc_md.restart(cmd);
92 #ifdef CONFIG_SMP
93         smp_send_stop();
94 #endif
95         printk(KERN_EMERG "System Halted, OK to turn off power\n");
96         local_irq_disable();
97         while (1) ;
98 }
99
100 void machine_power_off(void)
101 {
102         machine_shutdown();
103         ppc_md.power_off();
104 #ifdef CONFIG_SMP
105         smp_send_stop();
106 #endif
107         printk(KERN_EMERG "System Halted, OK to turn off power\n");
108         local_irq_disable();
109         while (1) ;
110 }
111 /* Used by the G5 thermal driver */
112 EXPORT_SYMBOL_GPL(machine_power_off);
113
114 void (*pm_power_off)(void) = machine_power_off;
115 EXPORT_SYMBOL_GPL(pm_power_off);
116
117 void machine_halt(void)
118 {
119         machine_shutdown();
120         ppc_md.halt();
121 #ifdef CONFIG_SMP
122         smp_send_stop();
123 #endif
124         printk(KERN_EMERG "System Halted, OK to turn off power\n");
125         local_irq_disable();
126         while (1) ;
127 }
128
129
130 #ifdef CONFIG_TAU
131 extern u32 cpu_temp(unsigned long cpu);
132 extern u32 cpu_temp_both(unsigned long cpu);
133 #endif /* CONFIG_TAU */
134
135 #ifdef CONFIG_SMP
136 DEFINE_PER_CPU(unsigned int, pvr);
137 #endif
138
139 static int show_cpuinfo(struct seq_file *m, void *v)
140 {
141         unsigned long cpu_id = (unsigned long)v - 1;
142         unsigned int pvr;
143         unsigned short maj;
144         unsigned short min;
145
146         if (cpu_id == NR_CPUS) {
147 #if defined(CONFIG_SMP) && defined(CONFIG_PPC32)
148                 unsigned long bogosum = 0;
149                 int i;
150                 for (i = 0; i < NR_CPUS; ++i)
151                         if (cpu_online(i))
152                                 bogosum += loops_per_jiffy;
153                 seq_printf(m, "total bogomips\t: %lu.%02lu\n",
154                            bogosum/(500000/HZ), bogosum/(5000/HZ) % 100);
155 #endif /* CONFIG_SMP && CONFIG_PPC32 */
156                 seq_printf(m, "timebase\t: %lu\n", ppc_tb_freq);
157
158                 if (ppc_md.show_cpuinfo != NULL)
159                         ppc_md.show_cpuinfo(m);
160
161                 return 0;
162         }
163
164         /* We only show online cpus: disable preempt (overzealous, I
165          * knew) to prevent cpu going down. */
166         preempt_disable();
167         if (!cpu_online(cpu_id)) {
168                 preempt_enable();
169                 return 0;
170         }
171
172 #ifdef CONFIG_SMP
173         pvr = per_cpu(pvr, cpu_id);
174 #else
175         pvr = mfspr(SPRN_PVR);
176 #endif
177         maj = (pvr >> 8) & 0xFF;
178         min = pvr & 0xFF;
179
180         seq_printf(m, "processor\t: %lu\n", cpu_id);
181         seq_printf(m, "cpu\t\t: ");
182
183         if (cur_cpu_spec->pvr_mask)
184                 seq_printf(m, "%s", cur_cpu_spec->cpu_name);
185         else
186                 seq_printf(m, "unknown (%08x)", pvr);
187
188 #ifdef CONFIG_ALTIVEC
189         if (cpu_has_feature(CPU_FTR_ALTIVEC))
190                 seq_printf(m, ", altivec supported");
191 #endif /* CONFIG_ALTIVEC */
192
193         seq_printf(m, "\n");
194
195 #ifdef CONFIG_TAU
196         if (cur_cpu_spec->cpu_features & CPU_FTR_TAU) {
197 #ifdef CONFIG_TAU_AVERAGE
198                 /* more straightforward, but potentially misleading */
199                 seq_printf(m,  "temperature \t: %u C (uncalibrated)\n",
200                            cpu_temp(cpu_id));
201 #else
202                 /* show the actual temp sensor range */
203                 u32 temp;
204                 temp = cpu_temp_both(cpu_id);
205                 seq_printf(m, "temperature \t: %u-%u C (uncalibrated)\n",
206                            temp & 0xff, temp >> 16);
207 #endif
208         }
209 #endif /* CONFIG_TAU */
210
211         /*
212          * Assume here that all clock rates are the same in a
213          * smp system.  -- Cort
214          */
215         if (ppc_proc_freq)
216                 seq_printf(m, "clock\t\t: %lu.%06luMHz\n",
217                            ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
218
219         if (ppc_md.show_percpuinfo != NULL)
220                 ppc_md.show_percpuinfo(m, cpu_id);
221
222         /* If we are a Freescale core do a simple check so
223          * we dont have to keep adding cases in the future */
224         if (PVR_VER(pvr) & 0x8000) {
225                 maj = PVR_MAJ(pvr);
226                 min = PVR_MIN(pvr);
227         } else {
228                 switch (PVR_VER(pvr)) {
229                         case 0x0020:    /* 403 family */
230                                 maj = PVR_MAJ(pvr) + 1;
231                                 min = PVR_MIN(pvr);
232                                 break;
233                         case 0x1008:    /* 740P/750P ?? */
234                                 maj = ((pvr >> 8) & 0xFF) - 1;
235                                 min = pvr & 0xFF;
236                                 break;
237                         default:
238                                 maj = (pvr >> 8) & 0xFF;
239                                 min = pvr & 0xFF;
240                                 break;
241                 }
242         }
243
244         seq_printf(m, "revision\t: %hd.%hd (pvr %04x %04x)\n",
245                    maj, min, PVR_VER(pvr), PVR_REV(pvr));
246
247 #ifdef CONFIG_PPC32
248         seq_printf(m, "bogomips\t: %lu.%02lu\n",
249                    loops_per_jiffy / (500000/HZ),
250                    (loops_per_jiffy / (5000/HZ)) % 100);
251 #endif
252
253 #ifdef CONFIG_SMP
254         seq_printf(m, "\n");
255 #endif
256
257         preempt_enable();
258         return 0;
259 }
260
261 static void *c_start(struct seq_file *m, loff_t *pos)
262 {
263         unsigned long i = *pos;
264
265         return i <= NR_CPUS ? (void *)(i + 1) : NULL;
266 }
267
268 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
269 {
270         ++*pos;
271         return c_start(m, pos);
272 }
273
274 static void c_stop(struct seq_file *m, void *v)
275 {
276 }
277
278 struct seq_operations cpuinfo_op = {
279         .start =c_start,
280         .next = c_next,
281         .stop = c_stop,
282         .show = show_cpuinfo,
283 };
284
285 #ifdef CONFIG_PPC_MULTIPLATFORM
286 static int __init set_preferred_console(void)
287 {
288         struct device_node *prom_stdout = NULL;
289         char *name;
290         u32 *spd;
291         int offset = 0;
292
293         DBG(" -> set_preferred_console()\n");
294
295         /* The user has requested a console so this is already set up. */
296         if (strstr(saved_command_line, "console=")) {
297                 DBG(" console was specified !\n");
298                 return -EBUSY;
299         }
300
301         if (!of_chosen) {
302                 DBG(" of_chosen is NULL !\n");
303                 return -ENODEV;
304         }
305         /* We are getting a weird phandle from OF ... */
306         /* ... So use the full path instead */
307         name = (char *)get_property(of_chosen, "linux,stdout-path", NULL);
308         if (name == NULL) {
309                 DBG(" no linux,stdout-path !\n");
310                 return -ENODEV;
311         }
312         prom_stdout = of_find_node_by_path(name);
313         if (!prom_stdout) {
314                 DBG(" can't find stdout package %s !\n", name);
315                 return -ENODEV;
316         }       
317         DBG("stdout is %s\n", prom_stdout->full_name);
318
319         name = (char *)get_property(prom_stdout, "name", NULL);
320         if (!name) {
321                 DBG(" stdout package has no name !\n");
322                 goto not_found;
323         }
324         spd = (u32 *)get_property(prom_stdout, "current-speed", NULL);
325
326         if (0)
327                 ;
328 #ifdef CONFIG_SERIAL_8250_CONSOLE
329         else if (strcmp(name, "serial") == 0) {
330                 int i;
331                 u32 *reg = (u32 *)get_property(prom_stdout, "reg", &i);
332                 if (i > 8) {
333                         switch (reg[1]) {
334                                 case 0x3f8:
335                                         offset = 0;
336                                         break;
337                                 case 0x2f8:
338                                         offset = 1;
339                                         break;
340                                 case 0x898:
341                                         offset = 2;
342                                         break;
343                                 case 0x890:
344                                         offset = 3;
345                                         break;
346                                 default:
347                                         /* We dont recognise the serial port */
348                                         goto not_found;
349                         }
350                 }
351         }
352 #endif /* CONFIG_SERIAL_8250_CONSOLE */
353 #ifdef CONFIG_PPC_PSERIES
354         else if (strcmp(name, "vty") == 0) {
355                 u32 *reg = (u32 *)get_property(prom_stdout, "reg", NULL);
356                 char *compat = (char *)get_property(prom_stdout, "compatible", NULL);
357
358                 if (reg && compat && (strcmp(compat, "hvterm-protocol") == 0)) {
359                         /* Host Virtual Serial Interface */
360                         switch (reg[0]) {
361                                 case 0x30000000:
362                                         offset = 0;
363                                         break;
364                                 case 0x30000001:
365                                         offset = 1;
366                                         break;
367                                 default:
368                                         goto not_found;
369                         }
370                         of_node_put(prom_stdout);
371                         DBG("Found hvsi console at offset %d\n", offset);
372                         return add_preferred_console("hvsi", offset, NULL);
373                 } else {
374                         /* pSeries LPAR virtual console */
375                         of_node_put(prom_stdout);
376                         DBG("Found hvc console\n");
377                         return add_preferred_console("hvc", 0, NULL);
378                 }
379         }
380 #endif /* CONFIG_PPC_PSERIES */
381 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
382         else if (strcmp(name, "ch-a") == 0)
383                 offset = 0;
384         else if (strcmp(name, "ch-b") == 0)
385                 offset = 1;
386 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
387         else
388                 goto not_found;
389         of_node_put(prom_stdout);
390
391         DBG("Found serial console at ttyS%d\n", offset);
392
393         if (spd) {
394                 static char __initdata opt[16];
395                 sprintf(opt, "%d", *spd);
396                 return add_preferred_console("ttyS", offset, opt);
397         } else
398                 return add_preferred_console("ttyS", offset, NULL);
399
400  not_found:
401         DBG("No preferred console found !\n");
402         of_node_put(prom_stdout);
403         return -ENODEV;
404 }
405 console_initcall(set_preferred_console);
406 #endif /* CONFIG_PPC_MULTIPLATFORM */
407
408 #ifdef CONFIG_SMP
409
410 /**
411  * setup_cpu_maps - initialize the following cpu maps:
412  *                  cpu_possible_map
413  *                  cpu_present_map
414  *                  cpu_sibling_map
415  *
416  * Having the possible map set up early allows us to restrict allocations
417  * of things like irqstacks to num_possible_cpus() rather than NR_CPUS.
418  *
419  * We do not initialize the online map here; cpus set their own bits in
420  * cpu_online_map as they come up.
421  *
422  * This function is valid only for Open Firmware systems.  finish_device_tree
423  * must be called before using this.
424  *
425  * While we're here, we may as well set the "physical" cpu ids in the paca.
426  */
427 void __init smp_setup_cpu_maps(void)
428 {
429         struct device_node *dn = NULL;
430         int cpu = 0;
431         int swap_cpuid = 0;
432
433         while ((dn = of_find_node_by_type(dn, "cpu")) && cpu < NR_CPUS) {
434                 int *intserv;
435                 int j, len = sizeof(u32), nthreads = 1;
436
437                 intserv = (int *)get_property(dn, "ibm,ppc-interrupt-server#s",
438                                               &len);
439                 if (intserv)
440                         nthreads = len / sizeof(int);
441                 else {
442                         intserv = (int *) get_property(dn, "reg", NULL);
443                         if (!intserv)
444                                 intserv = &cpu; /* assume logical == phys */
445                 }
446
447                 for (j = 0; j < nthreads && cpu < NR_CPUS; j++) {
448                         cpu_set(cpu, cpu_present_map);
449                         set_hard_smp_processor_id(cpu, intserv[j]);
450
451                         if (intserv[j] == boot_cpuid_phys)
452                                 swap_cpuid = cpu;
453                         cpu_set(cpu, cpu_possible_map);
454                         cpu++;
455                 }
456         }
457
458         /* Swap CPU id 0 with boot_cpuid_phys, so we can always assume that
459          * boot cpu is logical 0.
460          */
461         if (boot_cpuid_phys != get_hard_smp_processor_id(0)) {
462                 u32 tmp;
463                 tmp = get_hard_smp_processor_id(0);
464                 set_hard_smp_processor_id(0, boot_cpuid_phys);
465                 set_hard_smp_processor_id(swap_cpuid, tmp);
466         }
467
468 #ifdef CONFIG_PPC64
469         /*
470          * On pSeries LPAR, we need to know how many cpus
471          * could possibly be added to this partition.
472          */
473         if (systemcfg->platform == PLATFORM_PSERIES_LPAR &&
474                                 (dn = of_find_node_by_path("/rtas"))) {
475                 int num_addr_cell, num_size_cell, maxcpus;
476                 unsigned int *ireg;
477
478                 num_addr_cell = prom_n_addr_cells(dn);
479                 num_size_cell = prom_n_size_cells(dn);
480
481                 ireg = (unsigned int *)
482                         get_property(dn, "ibm,lrdr-capacity", NULL);
483
484                 if (!ireg)
485                         goto out;
486
487                 maxcpus = ireg[num_addr_cell + num_size_cell];
488
489                 /* Double maxcpus for processors which have SMT capability */
490                 if (cpu_has_feature(CPU_FTR_SMT))
491                         maxcpus *= 2;
492
493                 if (maxcpus > NR_CPUS) {
494                         printk(KERN_WARNING
495                                "Partition configured for %d cpus, "
496                                "operating system maximum is %d.\n",
497                                maxcpus, NR_CPUS);
498                         maxcpus = NR_CPUS;
499                 } else
500                         printk(KERN_INFO "Partition configured for %d cpus.\n",
501                                maxcpus);
502
503                 for (cpu = 0; cpu < maxcpus; cpu++)
504                         cpu_set(cpu, cpu_possible_map);
505         out:
506                 of_node_put(dn);
507         }
508
509         /*
510          * Do the sibling map; assume only two threads per processor.
511          */
512         for_each_cpu(cpu) {
513                 cpu_set(cpu, cpu_sibling_map[cpu]);
514                 if (cpu_has_feature(CPU_FTR_SMT))
515                         cpu_set(cpu ^ 0x1, cpu_sibling_map[cpu]);
516         }
517
518         systemcfg->processorCount = num_present_cpus();
519 #endif /* CONFIG_PPC64 */
520 }
521 #endif /* CONFIG_SMP */