ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / s390 / kernel / setup.c
1 /*
2  *  arch/s390/kernel/setup.c
3  *
4  *  S390 version
5  *    Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
6  *    Author(s): Hartmut Penner (hp@de.ibm.com),
7  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
8  *
9  *  Derived from "arch/i386/kernel/setup.c"
10  *    Copyright (C) 1995, Linus Torvalds
11  */
12
13 /*
14  * This file handles the architecture-dependent parts of initialization
15  */
16
17 #include <linux/errno.h>
18 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/stddef.h>
23 #include <linux/unistd.h>
24 #include <linux/ptrace.h>
25 #include <linux/slab.h>
26 #include <linux/user.h>
27 #include <linux/a.out.h>
28 #include <linux/tty.h>
29 #include <linux/ioport.h>
30 #include <linux/delay.h>
31 #include <linux/config.h>
32 #include <linux/init.h>
33 #include <linux/initrd.h>
34 #include <linux/bootmem.h>
35 #include <linux/root_dev.h>
36 #include <linux/console.h>
37 #include <linux/seq_file.h>
38 #include <linux/kernel_stat.h>
39
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
42 #include <asm/smp.h>
43 #include <asm/mmu_context.h>
44 #include <asm/cpcmd.h>
45 #include <asm/lowcore.h>
46 #include <asm/irq.h>
47
48 /*
49  * Machine setup..
50  */
51 unsigned int console_mode = 0;
52 unsigned int console_device = -1;
53 unsigned int console_irq = -1;
54 unsigned long memory_size = 0;
55 unsigned long machine_flags = 0;
56 struct { unsigned long addr, size, type; } memory_chunk[16] = { { 0 } };
57 #define CHUNK_READ_WRITE 0
58 #define CHUNK_READ_ONLY 1
59 int cpus_initialized = 0;
60 static cpumask_t cpu_initialized;
61 volatile int __cpu_logical_map[NR_CPUS]; /* logical cpu to cpu address */
62
63 /*
64  * Setup options
65  */
66 extern int _text,_etext, _edata, _end;
67
68 /*
69  * This is set up by the setup-routine at boot-time
70  * for S390 need to find out, what we have to setup
71  * using address 0x10400 ...
72  */
73
74 #include <asm/setup.h>
75
76 static char command_line[COMMAND_LINE_SIZE] = { 0, };
77        char saved_command_line[COMMAND_LINE_SIZE];
78
79 static struct resource code_resource = { "Kernel code", 0x100000, 0 };
80 static struct resource data_resource = { "Kernel data", 0, 0 };
81
82 /*
83  * cpu_init() initializes state that is per-CPU.
84  */
85 void __devinit cpu_init (void)
86 {
87         int nr = smp_processor_id();
88         int addr = hard_smp_processor_id();
89
90         if (cpu_test_and_set(nr,cpu_initialized)) {
91                 printk("CPU#%d ALREADY INITIALIZED!!!!!!!!!\n", nr);
92                 for (;;) local_irq_enable();
93         }
94         cpus_initialized++;
95
96         /*
97          * Store processor id in lowcore (used e.g. in timer_interrupt)
98          */
99         asm volatile ("stidp %0": "=m" (S390_lowcore.cpu_data.cpu_id));
100         S390_lowcore.cpu_data.cpu_addr = addr;
101
102         /*
103          * Force FPU initialization:
104          */
105         clear_thread_flag(TIF_USEDFPU);
106         current->used_math = 0;
107
108         /* Setup active_mm for idle_task  */
109         atomic_inc(&init_mm.mm_count);
110         current->active_mm = &init_mm;
111         if (current->mm)
112                 BUG();
113         enter_lazy_tlb(&init_mm, current);
114 }
115
116 /*
117  * VM halt and poweroff setup routines
118  */
119 char vmhalt_cmd[128] = "";
120 char vmpoff_cmd[128] = "";
121
122 static inline void strncpy_skip_quote(char *dst, char *src, int n)
123 {
124         int sx, dx;
125
126         dx = 0;
127         for (sx = 0; src[sx] != 0; sx++) {
128                 if (src[sx] == '"') continue;
129                 dst[dx++] = src[sx];
130                 if (dx >= n) break;
131         }
132 }
133
134 static int __init vmhalt_setup(char *str)
135 {
136         strncpy_skip_quote(vmhalt_cmd, str, 127);
137         vmhalt_cmd[127] = 0;
138         return 1;
139 }
140
141 __setup("vmhalt=", vmhalt_setup);
142
143 static int __init vmpoff_setup(char *str)
144 {
145         strncpy_skip_quote(vmpoff_cmd, str, 127);
146         vmpoff_cmd[127] = 0;
147         return 1;
148 }
149
150 __setup("vmpoff=", vmpoff_setup);
151
152 /*
153  * condev= and conmode= setup parameter.
154  */
155
156 static int __init condev_setup(char *str)
157 {
158         int vdev;
159
160         vdev = simple_strtoul(str, &str, 0);
161         if (vdev >= 0 && vdev < 65536) {
162                 console_device = vdev;
163                 console_irq = -1;
164         }
165         return 1;
166 }
167
168 __setup("condev=", condev_setup);
169
170 static int __init conmode_setup(char *str)
171 {
172 #if defined(CONFIG_SCLP_CONSOLE)
173         if (strncmp(str, "hwc", 4) == 0 || strncmp(str, "sclp", 5) == 0)
174                 SET_CONSOLE_SCLP;
175 #endif
176 #if defined(CONFIG_TN3215_CONSOLE)
177         if (strncmp(str, "3215", 5) == 0)
178                 SET_CONSOLE_3215;
179 #endif
180 #if defined(CONFIG_TN3270_CONSOLE)
181         if (strncmp(str, "3270", 5) == 0)
182                 SET_CONSOLE_3270;
183 #endif
184         return 1;
185 }
186
187 __setup("conmode=", conmode_setup);
188
189 static void __init conmode_default(void)
190 {
191         char query_buffer[1024];
192         char *ptr;
193
194         if (MACHINE_IS_VM) {
195                 cpcmd("QUERY CONSOLE", query_buffer, 1024);
196                 console_device = simple_strtoul(query_buffer + 5, NULL, 16);
197                 ptr = strstr(query_buffer, "SUBCHANNEL =");
198                 console_irq = simple_strtoul(ptr + 13, NULL, 16);
199                 cpcmd("QUERY TERM", query_buffer, 1024);
200                 ptr = strstr(query_buffer, "CONMODE");
201                 /*
202                  * Set the conmode to 3215 so that the device recognition 
203                  * will set the cu_type of the console to 3215. If the
204                  * conmode is 3270 and we don't set it back then both
205                  * 3215 and the 3270 driver will try to access the console
206                  * device (3215 as console and 3270 as normal tty).
207                  */
208                 cpcmd("TERM CONMODE 3215", NULL, 0);
209                 if (ptr == NULL) {
210 #if defined(CONFIG_SCLP_CONSOLE)
211                         SET_CONSOLE_SCLP;
212 #endif
213                         return;
214                 }
215                 if (strncmp(ptr + 8, "3270", 4) == 0) {
216 #if defined(CONFIG_TN3270_CONSOLE)
217                         SET_CONSOLE_3270;
218 #elif defined(CONFIG_TN3215_CONSOLE)
219                         SET_CONSOLE_3215;
220 #elif defined(CONFIG_SCLP_CONSOLE)
221                         SET_CONSOLE_SCLP;
222 #endif
223                 } else if (strncmp(ptr + 8, "3215", 4) == 0) {
224 #if defined(CONFIG_TN3215_CONSOLE)
225                         SET_CONSOLE_3215;
226 #elif defined(CONFIG_TN3270_CONSOLE)
227                         SET_CONSOLE_3270;
228 #elif defined(CONFIG_SCLP_CONSOLE)
229                         SET_CONSOLE_SCLP;
230 #endif
231                 }
232         } else if (MACHINE_IS_P390) {
233 #if defined(CONFIG_TN3215_CONSOLE)
234                 SET_CONSOLE_3215;
235 #elif defined(CONFIG_TN3270_CONSOLE)
236                 SET_CONSOLE_3270;
237 #endif
238         } else {
239 #if defined(CONFIG_SCLP_CONSOLE)
240                 SET_CONSOLE_SCLP;
241 #endif
242         }
243 }
244
245 #ifdef CONFIG_SMP
246 extern void machine_restart_smp(char *);
247 extern void machine_halt_smp(void);
248 extern void machine_power_off_smp(void);
249
250 void (*_machine_restart)(char *command) = machine_restart_smp;
251 void (*_machine_halt)(void) = machine_halt_smp;
252 void (*_machine_power_off)(void) = machine_power_off_smp;
253 #else
254 /*
255  * Reboot, halt and power_off routines for non SMP.
256  */
257 extern void reipl(unsigned long devno);
258 static void do_machine_restart_nonsmp(char * __unused)
259 {
260         if (MACHINE_IS_VM)
261                 cpcmd ("IPL", NULL, 0);
262         else
263                 reipl (0x10000 | S390_lowcore.ipl_device);
264 }
265
266 static void do_machine_halt_nonsmp(void)
267 {
268         if (MACHINE_IS_VM && strlen(vmhalt_cmd) > 0)
269                 cpcmd(vmhalt_cmd, NULL, 0);
270         signal_processor(smp_processor_id(), sigp_stop_and_store_status);
271 }
272
273 static void do_machine_power_off_nonsmp(void)
274 {
275         if (MACHINE_IS_VM && strlen(vmpoff_cmd) > 0)
276                 cpcmd(vmpoff_cmd, NULL, 0);
277         signal_processor(smp_processor_id(), sigp_stop_and_store_status);
278 }
279
280 void (*_machine_restart)(char *command) = do_machine_restart_nonsmp;
281 void (*_machine_halt)(void) = do_machine_halt_nonsmp;
282 void (*_machine_power_off)(void) = do_machine_power_off_nonsmp;
283 #endif
284
285  /*
286  * Reboot, halt and power_off stubs. They just call _machine_restart,
287  * _machine_halt or _machine_power_off. 
288  */
289
290 void machine_restart(char *command)
291 {
292         console_unblank();
293         _machine_restart(command);
294 }
295
296 EXPORT_SYMBOL(machine_restart);
297
298 void machine_halt(void)
299 {
300         console_unblank();
301         _machine_halt();
302 }
303
304 EXPORT_SYMBOL(machine_halt);
305
306 void machine_power_off(void)
307 {
308         console_unblank();
309         _machine_power_off();
310 }
311
312 EXPORT_SYMBOL(machine_power_off);
313
314 /*
315  * Setup function called from init/main.c just after the banner
316  * was printed.
317  */
318 extern char _pstart, _pend, _stext;
319
320 void __init setup_arch(char **cmdline_p)
321 {
322         unsigned long bootmap_size;
323         unsigned long memory_start, memory_end;
324         char c = ' ', cn, *to = command_line, *from = COMMAND_LINE;
325         struct resource *res;
326         unsigned long start_pfn, end_pfn;
327         static unsigned int smptrap=0;
328         unsigned long delay = 0;
329         struct _lowcore *lc;
330         int i;
331
332         if (smptrap)
333                 return;
334         smptrap=1;
335
336         /*
337          * print what head.S has found out about the machine 
338          */
339 #ifndef CONFIG_ARCH_S390X
340         printk((MACHINE_IS_VM) ?
341                "We are running under VM (31 bit mode)\n" :
342                "We are running native (31 bit mode)\n");
343         printk((MACHINE_HAS_IEEE) ?
344                "This machine has an IEEE fpu\n" :
345                "This machine has no IEEE fpu\n");
346 #else /* CONFIG_ARCH_S390X */
347         printk((MACHINE_IS_VM) ?
348                "We are running under VM (64 bit mode)\n" :
349                "We are running native (64 bit mode)\n");
350 #endif /* CONFIG_ARCH_S390X */
351
352         ROOT_DEV = Root_RAM0;
353         memory_start = (unsigned long) &_end;    /* fixit if use $CODELO etc*/
354 #ifndef CONFIG_ARCH_S390X
355         memory_end = memory_size & ~0x400000UL;  /* align memory end to 4MB */
356         /*
357          * We need some free virtual space to be able to do vmalloc.
358          * On a machine with 2GB memory we make sure that we have at
359          * least 128 MB free space for vmalloc.
360          */
361         if (memory_end > 1920*1024*1024)
362                 memory_end = 1920*1024*1024;
363 #else /* CONFIG_ARCH_S390X */
364         memory_end = memory_size & ~0x200000UL;  /* detected in head.s */
365 #endif /* CONFIG_ARCH_S390X */
366         init_mm.start_code = PAGE_OFFSET;
367         init_mm.end_code = (unsigned long) &_etext;
368         init_mm.end_data = (unsigned long) &_edata;
369         init_mm.brk = (unsigned long) &_end;
370
371         code_resource.start = (unsigned long) &_text;
372         code_resource.end = (unsigned long) &_etext - 1;
373         data_resource.start = (unsigned long) &_etext;
374         data_resource.end = (unsigned long) &_edata - 1;
375
376         /* Save unparsed command line copy for /proc/cmdline */
377         memcpy(saved_command_line, COMMAND_LINE, COMMAND_LINE_SIZE);
378         saved_command_line[COMMAND_LINE_SIZE-1] = '\0';
379
380         for (;;) {
381                 /*
382                  * "mem=XXX[kKmM]" sets memsize 
383                  */
384                 if (c == ' ' && strncmp(from, "mem=", 4) == 0) {
385                         memory_end = simple_strtoul(from+4, &from, 0);
386                         if ( *from == 'K' || *from == 'k' ) {
387                                 memory_end = memory_end << 10;
388                                 from++;
389                         } else if ( *from == 'M' || *from == 'm' ) {
390                                 memory_end = memory_end << 20;
391                                 from++;
392                         }
393                 }
394                 /*
395                  * "ipldelay=XXX[sm]" sets ipl delay in seconds or minutes
396                  */
397                 if (c == ' ' && strncmp(from, "ipldelay=", 9) == 0) {
398                         delay = simple_strtoul(from+9, &from, 0);
399                         if (*from == 's' || *from == 'S') {
400                                 delay = delay*1000000;
401                                 from++;
402                         } else if (*from == 'm' || *from == 'M') {
403                                 delay = delay*60*1000000;
404                                 from++;
405                         }
406                         /* now wait for the requested amount of time */
407                         udelay(delay);
408                 }
409                 cn = *(from++);
410                 if (!cn)
411                         break;
412                 if (cn == '\n')
413                         cn = ' ';  /* replace newlines with space */
414                 if (cn == 0x0d)
415                         cn = ' ';  /* replace 0x0d with space */
416                 if (cn == ' ' && c == ' ')
417                         continue;  /* remove additional spaces */
418                 c = cn;
419                 if (to - command_line >= COMMAND_LINE_SIZE)
420                         break;
421                 *(to++) = c;
422         }
423         if (c == ' ' && to > command_line) to--;
424         *to = '\0';
425         *cmdline_p = command_line;
426
427         /*
428          * partially used pages are not usable - thus
429          * we are rounding upwards:
430          */
431         start_pfn = (__pa(&_end) + PAGE_SIZE - 1) >> PAGE_SHIFT;
432         end_pfn = max_pfn = memory_end >> PAGE_SHIFT;
433
434         /*
435          * Initialize the boot-time allocator (with low memory only):
436          */
437         bootmap_size = init_bootmem(start_pfn, end_pfn);
438
439         /*
440          * Register RAM areas with the bootmem allocator.
441          */
442         for (i = 0; i < 16 && memory_chunk[i].size > 0; i++) {
443                 unsigned long start_chunk, end_chunk;
444
445                 if (memory_chunk[i].type != CHUNK_READ_WRITE)
446                         continue;
447                 start_chunk = (memory_chunk[i].addr + PAGE_SIZE - 1);
448                 start_chunk >>= PAGE_SHIFT;
449                 end_chunk = (memory_chunk[i].addr + memory_chunk[i].size);
450                 end_chunk >>= PAGE_SHIFT;
451                 if (start_chunk < start_pfn)
452                         start_chunk = start_pfn;
453                 if (end_chunk > end_pfn)
454                         end_chunk = end_pfn;
455                 if (start_chunk < end_chunk)
456                         free_bootmem(start_chunk << PAGE_SHIFT,
457                                      (end_chunk - start_chunk) << PAGE_SHIFT);
458         }
459
460         /*
461          * Reserve the bootmem bitmap itself as well. We do this in two
462          * steps (first step was init_bootmem()) because this catches
463          * the (very unlikely) case of us accidentally initializing the
464          * bootmem allocator with an invalid RAM area.
465          */
466         reserve_bootmem(start_pfn << PAGE_SHIFT, bootmap_size);
467
468 #ifdef CONFIG_BLK_DEV_INITRD
469         if (INITRD_START) {
470                 if (INITRD_START + INITRD_SIZE <= memory_end) {
471                         reserve_bootmem(INITRD_START, INITRD_SIZE);
472                         initrd_start = INITRD_START;
473                         initrd_end = initrd_start + INITRD_SIZE;
474                 } else {
475                         printk("initrd extends beyond end of memory "
476                                "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
477                                initrd_start + INITRD_SIZE, memory_end);
478                         initrd_start = initrd_end = 0;
479                 }
480         }
481 #endif
482
483         /*
484          * Setup lowcore for boot cpu
485          */
486 #ifndef CONFIG_ARCH_S390X
487         lc = (struct _lowcore *) __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, 0);
488         memset(lc, 0, PAGE_SIZE);
489 #else /* CONFIG_ARCH_S390X */
490         lc = (struct _lowcore *) __alloc_bootmem(2*PAGE_SIZE, 2*PAGE_SIZE, 0);
491         memset(lc, 0, 2*PAGE_SIZE);
492 #endif /* CONFIG_ARCH_S390X */
493         lc->restart_psw.mask = PSW_BASE_BITS;
494         lc->restart_psw.addr =
495                 PSW_ADDR_AMODE | (unsigned long) restart_int_handler;
496         lc->external_new_psw.mask = PSW_KERNEL_BITS;
497         lc->external_new_psw.addr =
498                 PSW_ADDR_AMODE | (unsigned long) ext_int_handler;
499         lc->svc_new_psw.mask = PSW_KERNEL_BITS | PSW_MASK_IO | PSW_MASK_EXT;
500         lc->svc_new_psw.addr = PSW_ADDR_AMODE | (unsigned long) system_call;
501         lc->program_new_psw.mask = PSW_KERNEL_BITS;
502         lc->program_new_psw.addr =
503                 PSW_ADDR_AMODE | (unsigned long)pgm_check_handler;
504         lc->mcck_new_psw.mask = PSW_KERNEL_BITS;
505         lc->mcck_new_psw.addr =
506                 PSW_ADDR_AMODE | (unsigned long) mcck_int_handler;
507         lc->io_new_psw.mask = PSW_KERNEL_BITS;
508         lc->io_new_psw.addr = PSW_ADDR_AMODE | (unsigned long) io_int_handler;
509         lc->ipl_device = S390_lowcore.ipl_device;
510         lc->jiffy_timer = -1LL;
511         lc->kernel_stack = ((unsigned long) &init_thread_union) + THREAD_SIZE;
512         lc->async_stack = (unsigned long)
513                 __alloc_bootmem(ASYNC_SIZE, ASYNC_SIZE, 0) + ASYNC_SIZE;
514         lc->current_task = (unsigned long) init_thread_union.thread_info.task;
515         lc->thread_info = (unsigned long) &init_thread_union;
516 #ifdef CONFIG_ARCH_S390X
517         if (MACHINE_HAS_DIAG44)
518                 lc->diag44_opcode = 0x83000044;
519         else
520                 lc->diag44_opcode = 0x07000700;
521 #endif /* CONFIG_ARCH_S390X */
522         set_prefix((u32)(unsigned long) lc);
523         cpu_init();
524         __cpu_logical_map[0] = S390_lowcore.cpu_data.cpu_addr;
525
526         /*
527          * Create kernel page tables and switch to virtual addressing.
528          */
529         paging_init();
530
531         res = alloc_bootmem_low(sizeof(struct resource));
532         res->start = 0;
533         res->end = memory_end;
534         res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
535         request_resource(&iomem_resource, res);
536         request_resource(res, &code_resource);
537         request_resource(res, &data_resource);
538
539         /* Setup default console */
540         conmode_default();
541 }
542
543 void print_cpu_info(struct cpuinfo_S390 *cpuinfo)
544 {
545    printk("cpu %d "
546 #ifdef CONFIG_SMP
547            "phys_idx=%d "
548 #endif
549            "vers=%02X ident=%06X machine=%04X unused=%04X\n",
550            cpuinfo->cpu_nr,
551 #ifdef CONFIG_SMP
552            cpuinfo->cpu_addr,
553 #endif
554            cpuinfo->cpu_id.version,
555            cpuinfo->cpu_id.ident,
556            cpuinfo->cpu_id.machine,
557            cpuinfo->cpu_id.unused);
558 }
559
560 /*
561  * show_cpuinfo - Get information on one CPU for use by procfs.
562  */
563
564 static int show_cpuinfo(struct seq_file *m, void *v)
565 {
566         struct cpuinfo_S390 *cpuinfo;
567         unsigned long n = (unsigned long) v - 1;
568
569         if (!n) {
570                 seq_printf(m, "vendor_id       : IBM/S390\n"
571                                "# processors    : %i\n"
572                                "bogomips per cpu: %lu.%02lu\n",
573                                num_online_cpus(), loops_per_jiffy/(500000/HZ),
574                                (loops_per_jiffy/(5000/HZ))%100);
575         }
576         if (cpu_online(n)) {
577 #ifdef CONFIG_SMP
578                 if (smp_processor_id() == n)
579                         cpuinfo = &S390_lowcore.cpu_data;
580                 else
581                         cpuinfo = &lowcore_ptr[n]->cpu_data;
582 #else
583                 cpuinfo = &S390_lowcore.cpu_data;
584 #endif
585                 seq_printf(m, "processor %li: "
586                                "version = %02X,  "
587                                "identification = %06X,  "
588                                "machine = %04X\n",
589                                n, cpuinfo->cpu_id.version,
590                                cpuinfo->cpu_id.ident,
591                                cpuinfo->cpu_id.machine);
592         }
593         return 0;
594 }
595
596 static void *c_start(struct seq_file *m, loff_t *pos)
597 {
598         return *pos < NR_CPUS ? (void *)((unsigned long) *pos + 1) : NULL;
599 }
600 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
601 {
602         ++*pos;
603         return c_start(m, pos);
604 }
605 static void c_stop(struct seq_file *m, void *v)
606 {
607 }
608 struct seq_operations cpuinfo_op = {
609         .start  = c_start,
610         .next   = c_next,
611         .stop   = c_stop,
612         .show   = show_cpuinfo,
613 };
614
615 /*
616  * show_interrupts is needed by /proc/interrupts.
617  */
618
619 static const char *intrclass_names[] = {
620         "EXT",
621         "I/O",
622 };
623
624 int show_interrupts(struct seq_file *p, void *v)
625 {
626         int i = *(loff_t *) v, j;
627         
628         if (i == 0) {
629                 seq_puts(p, "           ");
630                 for (j=0; j<NR_CPUS; j++)
631                         if (cpu_online(j))
632                                 seq_printf(p, "CPU%d       ",j);
633                 seq_putc(p, '\n');
634         }
635         
636         if (i < NR_IRQS) {
637                 seq_printf(p, "%s: ", intrclass_names[i]);
638 #ifndef CONFIG_SMP
639                 seq_printf(p, "%10u ", kstat_irqs(i));
640 #else
641                 for (j = 0; j < NR_CPUS; j++)
642                         if (cpu_online(j))
643                                 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
644 #endif
645                 seq_putc(p, '\n');
646                 
647         }
648         
649         return 0;
650 }