Merge branch 'irq-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / arch / s390 / kernel / early.c
1 /*
2  *    Copyright IBM Corp. 2007, 2009
3  *    Author(s): Hongjie Yang <hongjie@us.ibm.com>,
4  *               Heiko Carstens <heiko.carstens@de.ibm.com>
5  */
6
7 #define KMSG_COMPONENT "setup"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9
10 #include <linux/compiler.h>
11 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/string.h>
14 #include <linux/ctype.h>
15 #include <linux/lockdep.h>
16 #include <linux/extable.h>
17 #include <linux/pfn.h>
18 #include <linux/uaccess.h>
19 #include <linux/kernel.h>
20 #include <asm/diag.h>
21 #include <asm/ebcdic.h>
22 #include <asm/ipl.h>
23 #include <asm/lowcore.h>
24 #include <asm/processor.h>
25 #include <asm/sections.h>
26 #include <asm/setup.h>
27 #include <asm/sysinfo.h>
28 #include <asm/cpcmd.h>
29 #include <asm/sclp.h>
30 #include <asm/facility.h>
31 #include "entry.h"
32
33 /*
34  * Create a Kernel NSS if the SAVESYS= parameter is defined
35  */
36 #define DEFSYS_CMD_SIZE         128
37 #define SAVESYS_CMD_SIZE        32
38
39 char kernel_nss_name[NSS_NAME_SIZE + 1];
40
41 static void __init setup_boot_command_line(void);
42
43 /*
44  * Get the TOD clock running.
45  */
46 static void __init reset_tod_clock(void)
47 {
48         u64 time;
49
50         if (store_tod_clock(&time) == 0)
51                 return;
52         /* TOD clock not running. Set the clock to Unix Epoch. */
53         if (set_tod_clock(TOD_UNIX_EPOCH) != 0 || store_tod_clock(&time) != 0)
54                 disabled_wait(0);
55
56         memset(tod_clock_base, 0, 16);
57         *(__u64 *) &tod_clock_base[1] = TOD_UNIX_EPOCH;
58         S390_lowcore.last_update_clock = TOD_UNIX_EPOCH;
59 }
60
61 #ifdef CONFIG_SHARED_KERNEL
62 int __init savesys_ipl_nss(char *cmd, const int cmdlen);
63
64 asm(
65         "       .section .init.text,\"ax\",@progbits\n"
66         "       .align  4\n"
67         "       .type   savesys_ipl_nss, @function\n"
68         "savesys_ipl_nss:\n"
69         "       stmg    6,15,48(15)\n"
70         "       lgr     14,3\n"
71         "       sam31\n"
72         "       diag    2,14,0x8\n"
73         "       sam64\n"
74         "       lgr     2,14\n"
75         "       lmg     6,15,48(15)\n"
76         "       br      14\n"
77         "       .size   savesys_ipl_nss, .-savesys_ipl_nss\n"
78         "       .previous\n");
79
80 static __initdata char upper_command_line[COMMAND_LINE_SIZE];
81
82 static noinline __init void create_kernel_nss(void)
83 {
84         unsigned int i, stext_pfn, eshared_pfn, end_pfn, min_size;
85 #ifdef CONFIG_BLK_DEV_INITRD
86         unsigned int sinitrd_pfn, einitrd_pfn;
87 #endif
88         int response;
89         int hlen;
90         size_t len;
91         char *savesys_ptr;
92         char defsys_cmd[DEFSYS_CMD_SIZE];
93         char savesys_cmd[SAVESYS_CMD_SIZE];
94
95         /* Do nothing if we are not running under VM */
96         if (!MACHINE_IS_VM)
97                 return;
98
99         /* Convert COMMAND_LINE to upper case */
100         for (i = 0; i < strlen(boot_command_line); i++)
101                 upper_command_line[i] = toupper(boot_command_line[i]);
102
103         savesys_ptr = strstr(upper_command_line, "SAVESYS=");
104
105         if (!savesys_ptr)
106                 return;
107
108         savesys_ptr += 8;    /* Point to the beginning of the NSS name */
109         for (i = 0; i < NSS_NAME_SIZE; i++) {
110                 if (savesys_ptr[i] == ' ' || savesys_ptr[i] == '\0')
111                         break;
112                 kernel_nss_name[i] = savesys_ptr[i];
113         }
114
115         stext_pfn = PFN_DOWN(__pa(&_stext));
116         eshared_pfn = PFN_DOWN(__pa(&_eshared));
117         end_pfn = PFN_UP(__pa(&_end));
118         min_size = end_pfn << 2;
119
120         hlen = snprintf(defsys_cmd, DEFSYS_CMD_SIZE,
121                         "DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X",
122                         kernel_nss_name, stext_pfn - 1, stext_pfn,
123                         eshared_pfn - 1, eshared_pfn, end_pfn);
124
125 #ifdef CONFIG_BLK_DEV_INITRD
126         if (INITRD_START && INITRD_SIZE) {
127                 sinitrd_pfn = PFN_DOWN(__pa(INITRD_START));
128                 einitrd_pfn = PFN_UP(__pa(INITRD_START + INITRD_SIZE));
129                 min_size = einitrd_pfn << 2;
130                 hlen += snprintf(defsys_cmd + hlen, DEFSYS_CMD_SIZE - hlen,
131                                  " EW %.5X-%.5X", sinitrd_pfn, einitrd_pfn);
132         }
133 #endif
134
135         snprintf(defsys_cmd + hlen, DEFSYS_CMD_SIZE - hlen,
136                  " EW MINSIZE=%.7iK PARMREGS=0-13", min_size);
137         defsys_cmd[DEFSYS_CMD_SIZE - 1] = '\0';
138         snprintf(savesys_cmd, SAVESYS_CMD_SIZE, "SAVESYS %s \n IPL %s",
139                  kernel_nss_name, kernel_nss_name);
140         savesys_cmd[SAVESYS_CMD_SIZE - 1] = '\0';
141
142         __cpcmd(defsys_cmd, NULL, 0, &response);
143
144         if (response != 0) {
145                 pr_err("Defining the Linux kernel NSS failed with rc=%d\n",
146                         response);
147                 kernel_nss_name[0] = '\0';
148                 return;
149         }
150
151         len = strlen(savesys_cmd);
152         ASCEBC(savesys_cmd, len);
153         response = savesys_ipl_nss(savesys_cmd, len);
154
155         /* On success: response is equal to the command size,
156          *             max SAVESYS_CMD_SIZE
157          * On error: response contains the numeric portion of cp error message.
158          *           for SAVESYS it will be >= 263
159          *           for missing privilege class, it will be 1
160          */
161         if (response > SAVESYS_CMD_SIZE || response == 1) {
162                 pr_err("Saving the Linux kernel NSS failed with rc=%d\n",
163                         response);
164                 kernel_nss_name[0] = '\0';
165                 return;
166         }
167
168         /* re-initialize cputime accounting. */
169         get_tod_clock_ext(tod_clock_base);
170         S390_lowcore.last_update_clock = *(__u64 *) &tod_clock_base[1];
171         S390_lowcore.last_update_timer = 0x7fffffffffffffffULL;
172         S390_lowcore.user_timer = 0;
173         S390_lowcore.system_timer = 0;
174         asm volatile("SPT 0(%0)" : : "a" (&S390_lowcore.last_update_timer));
175
176         /* re-setup boot command line with new ipl vm parms */
177         ipl_update_parameters();
178         setup_boot_command_line();
179
180         ipl_flags = IPL_NSS_VALID;
181 }
182
183 #else /* CONFIG_SHARED_KERNEL */
184
185 static inline void create_kernel_nss(void) { }
186
187 #endif /* CONFIG_SHARED_KERNEL */
188
189 /*
190  * Clear bss memory
191  */
192 static noinline __init void clear_bss_section(void)
193 {
194         memset(__bss_start, 0, __bss_stop - __bss_start);
195 }
196
197 /*
198  * Initialize storage key for kernel pages
199  */
200 static noinline __init void init_kernel_storage_key(void)
201 {
202 #if PAGE_DEFAULT_KEY
203         unsigned long end_pfn, init_pfn;
204
205         end_pfn = PFN_UP(__pa(&_end));
206
207         for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
208                 page_set_storage_key(init_pfn << PAGE_SHIFT,
209                                      PAGE_DEFAULT_KEY, 0);
210 #endif
211 }
212
213 static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
214
215 static noinline __init void detect_machine_type(void)
216 {
217         struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;
218
219         /* Check current-configuration-level */
220         if (stsi(NULL, 0, 0, 0) <= 2) {
221                 S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
222                 return;
223         }
224         /* Get virtual-machine cpu information. */
225         if (stsi(vmms, 3, 2, 2) || !vmms->count)
226                 return;
227
228         /* Running under KVM? If not we assume z/VM */
229         if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3))
230                 S390_lowcore.machine_flags |= MACHINE_FLAG_KVM;
231         else
232                 S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
233 }
234
235 /* Remove leading, trailing and double whitespace. */
236 static inline void strim_all(char *str)
237 {
238         char *s;
239
240         s = strim(str);
241         if (s != str)
242                 memmove(str, s, strlen(s));
243         while (*str) {
244                 if (!isspace(*str++))
245                         continue;
246                 if (isspace(*str)) {
247                         s = skip_spaces(str);
248                         memmove(str, s, strlen(s) + 1);
249                 }
250         }
251 }
252
253 static noinline __init void setup_arch_string(void)
254 {
255         struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page;
256         struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page;
257         char mstr[80], hvstr[17];
258
259         if (stsi(mach, 1, 1, 1))
260                 return;
261         EBCASC(mach->manufacturer, sizeof(mach->manufacturer));
262         EBCASC(mach->type, sizeof(mach->type));
263         EBCASC(mach->model, sizeof(mach->model));
264         EBCASC(mach->model_capacity, sizeof(mach->model_capacity));
265         sprintf(mstr, "%-16.16s %-4.4s %-16.16s %-16.16s",
266                 mach->manufacturer, mach->type,
267                 mach->model, mach->model_capacity);
268         strim_all(mstr);
269         if (stsi(vm, 3, 2, 2) == 0 && vm->count) {
270                 EBCASC(vm->vm[0].cpi, sizeof(vm->vm[0].cpi));
271                 sprintf(hvstr, "%-16.16s", vm->vm[0].cpi);
272                 strim_all(hvstr);
273         } else {
274                 sprintf(hvstr, "%s",
275                         MACHINE_IS_LPAR ? "LPAR" :
276                         MACHINE_IS_VM ? "z/VM" :
277                         MACHINE_IS_KVM ? "KVM" : "unknown");
278         }
279         dump_stack_set_arch_desc("%s (%s)", mstr, hvstr);
280 }
281
282 static __init void setup_topology(void)
283 {
284         int max_mnest;
285
286         if (!test_facility(11))
287                 return;
288         S390_lowcore.machine_flags |= MACHINE_FLAG_TOPOLOGY;
289         for (max_mnest = 6; max_mnest > 1; max_mnest--) {
290                 if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0)
291                         break;
292         }
293         topology_max_mnest = max_mnest;
294 }
295
296 static void early_pgm_check_handler(void)
297 {
298         const struct exception_table_entry *fixup;
299         unsigned long cr0, cr0_new;
300         unsigned long addr;
301
302         addr = S390_lowcore.program_old_psw.addr;
303         fixup = search_exception_tables(addr);
304         if (!fixup)
305                 disabled_wait(0);
306         /* Disable low address protection before storing into lowcore. */
307         __ctl_store(cr0, 0, 0);
308         cr0_new = cr0 & ~(1UL << 28);
309         __ctl_load(cr0_new, 0, 0);
310         S390_lowcore.program_old_psw.addr = extable_fixup(fixup);
311         __ctl_load(cr0, 0, 0);
312 }
313
314 static noinline __init void setup_lowcore_early(void)
315 {
316         psw_t psw;
317
318         psw.mask = PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA;
319         psw.addr = (unsigned long) s390_base_ext_handler;
320         S390_lowcore.external_new_psw = psw;
321         psw.addr = (unsigned long) s390_base_pgm_handler;
322         S390_lowcore.program_new_psw = psw;
323         s390_base_pgm_handler_fn = early_pgm_check_handler;
324         S390_lowcore.preempt_count = INIT_PREEMPT_COUNT;
325 }
326
327 static noinline __init void setup_facility_list(void)
328 {
329         stfle(S390_lowcore.stfle_fac_list,
330               ARRAY_SIZE(S390_lowcore.stfle_fac_list));
331 }
332
333 static __init void detect_diag9c(void)
334 {
335         unsigned int cpu_address;
336         int rc;
337
338         cpu_address = stap();
339         diag_stat_inc(DIAG_STAT_X09C);
340         asm volatile(
341                 "       diag    %2,0,0x9c\n"
342                 "0:     la      %0,0\n"
343                 "1:\n"
344                 EX_TABLE(0b,1b)
345                 : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
346         if (!rc)
347                 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
348 }
349
350 static __init void detect_diag44(void)
351 {
352         int rc;
353
354         diag_stat_inc(DIAG_STAT_X044);
355         asm volatile(
356                 "       diag    0,0,0x44\n"
357                 "0:     la      %0,0\n"
358                 "1:\n"
359                 EX_TABLE(0b,1b)
360                 : "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
361         if (!rc)
362                 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44;
363 }
364
365 static __init void detect_machine_facilities(void)
366 {
367         if (test_facility(8)) {
368                 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
369                 __ctl_set_bit(0, 23);
370         }
371         if (test_facility(78))
372                 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2;
373         if (test_facility(3))
374                 S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
375         if (test_facility(40))
376                 S390_lowcore.machine_flags |= MACHINE_FLAG_LPP;
377         if (test_facility(50) && test_facility(73))
378                 S390_lowcore.machine_flags |= MACHINE_FLAG_TE;
379         if (test_facility(51))
380                 S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC;
381         if (test_facility(129)) {
382                 S390_lowcore.machine_flags |= MACHINE_FLAG_VX;
383                 __ctl_set_bit(0, 17);
384         }
385         if (test_facility(130)) {
386                 S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
387                 __ctl_set_bit(0, 20);
388         }
389         if (test_facility(133))
390                 S390_lowcore.machine_flags |= MACHINE_FLAG_GS;
391         if (test_facility(139) && (tod_clock_base[1] & 0x80)) {
392                 /* Enabled signed clock comparator comparisons */
393                 S390_lowcore.machine_flags |= MACHINE_FLAG_SCC;
394                 clock_comparator_max = -1ULL >> 1;
395                 __ctl_set_bit(0, 53);
396         }
397 }
398
399 static inline void save_vector_registers(void)
400 {
401 #ifdef CONFIG_CRASH_DUMP
402         if (test_facility(129))
403                 save_vx_regs(boot_cpu_vector_save_area);
404 #endif
405 }
406
407 static int __init topology_setup(char *str)
408 {
409         bool enabled;
410         int rc;
411
412         rc = kstrtobool(str, &enabled);
413         if (!rc && !enabled)
414                 S390_lowcore.machine_flags &= ~MACHINE_FLAG_TOPOLOGY;
415         return rc;
416 }
417 early_param("topology", topology_setup);
418
419 static int __init disable_vector_extension(char *str)
420 {
421         S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
422         __ctl_clear_bit(0, 17);
423         return 0;
424 }
425 early_param("novx", disable_vector_extension);
426
427 static int __init noexec_setup(char *str)
428 {
429         bool enabled;
430         int rc;
431
432         rc = kstrtobool(str, &enabled);
433         if (!rc && !enabled) {
434                 /* Disable no-execute support */
435                 S390_lowcore.machine_flags &= ~MACHINE_FLAG_NX;
436                 __ctl_clear_bit(0, 20);
437         }
438         return rc;
439 }
440 early_param("noexec", noexec_setup);
441
442 static int __init cad_setup(char *str)
443 {
444         bool enabled;
445         int rc;
446
447         rc = kstrtobool(str, &enabled);
448         if (!rc && enabled && test_facility(128))
449                 /* Enable problem state CAD. */
450                 __ctl_set_bit(2, 3);
451         return rc;
452 }
453 early_param("cad", cad_setup);
454
455 static __init void memmove_early(void *dst, const void *src, size_t n)
456 {
457         unsigned long addr;
458         long incr;
459         psw_t old;
460
461         if (!n)
462                 return;
463         incr = 1;
464         if (dst > src) {
465                 incr = -incr;
466                 dst += n - 1;
467                 src += n - 1;
468         }
469         old = S390_lowcore.program_new_psw;
470         S390_lowcore.program_new_psw.mask = __extract_psw();
471         asm volatile(
472                 "       larl    %[addr],1f\n"
473                 "       stg     %[addr],%[psw_pgm_addr]\n"
474                 "0:     mvc     0(1,%[dst]),0(%[src])\n"
475                 "       agr     %[dst],%[incr]\n"
476                 "       agr     %[src],%[incr]\n"
477                 "       brctg   %[n],0b\n"
478                 "1:\n"
479                 : [addr] "=&d" (addr),
480                   [psw_pgm_addr] "=Q" (S390_lowcore.program_new_psw.addr),
481                   [dst] "+&a" (dst), [src] "+&a" (src),  [n] "+d" (n)
482                 : [incr] "d" (incr)
483                 : "cc", "memory");
484         S390_lowcore.program_new_psw = old;
485 }
486
487 static __init noinline void ipl_save_parameters(void)
488 {
489         void *src, *dst;
490
491         src = (void *)(unsigned long) S390_lowcore.ipl_parmblock_ptr;
492         dst = (void *) IPL_PARMBLOCK_ORIGIN;
493         memmove_early(dst, src, PAGE_SIZE);
494         S390_lowcore.ipl_parmblock_ptr = IPL_PARMBLOCK_ORIGIN;
495 }
496
497 static __init noinline void rescue_initrd(void)
498 {
499 #ifdef CONFIG_BLK_DEV_INITRD
500         unsigned long min_initrd_addr = (unsigned long) _end + (4UL << 20);
501         /*
502          * Just like in case of IPL from VM reader we make sure there is a
503          * gap of 4MB between end of kernel and start of initrd.
504          * That way we can also be sure that saving an NSS will succeed,
505          * which however only requires different segments.
506          */
507         if (!INITRD_START || !INITRD_SIZE)
508                 return;
509         if (INITRD_START >= min_initrd_addr)
510                 return;
511         memmove_early((void *) min_initrd_addr, (void *) INITRD_START, INITRD_SIZE);
512         INITRD_START = min_initrd_addr;
513 #endif
514 }
515
516 /* Set up boot command line */
517 static void __init append_to_cmdline(size_t (*ipl_data)(char *, size_t))
518 {
519         char *parm, *delim;
520         size_t rc, len;
521
522         len = strlen(boot_command_line);
523
524         delim = boot_command_line + len;        /* '\0' character position */
525         parm  = boot_command_line + len + 1;    /* append right after '\0' */
526
527         rc = ipl_data(parm, COMMAND_LINE_SIZE - len - 1);
528         if (rc) {
529                 if (*parm == '=')
530                         memmove(boot_command_line, parm + 1, rc);
531                 else
532                         *delim = ' ';           /* replace '\0' with space */
533         }
534 }
535
536 static inline int has_ebcdic_char(const char *str)
537 {
538         int i;
539
540         for (i = 0; str[i]; i++)
541                 if (str[i] & 0x80)
542                         return 1;
543         return 0;
544 }
545
546 static void __init setup_boot_command_line(void)
547 {
548         COMMAND_LINE[ARCH_COMMAND_LINE_SIZE - 1] = 0;
549         /* convert arch command line to ascii if necessary */
550         if (has_ebcdic_char(COMMAND_LINE))
551                 EBCASC(COMMAND_LINE, ARCH_COMMAND_LINE_SIZE);
552         /* copy arch command line */
553         strlcpy(boot_command_line, strstrip(COMMAND_LINE),
554                 ARCH_COMMAND_LINE_SIZE);
555
556         /* append IPL PARM data to the boot command line */
557         if (MACHINE_IS_VM)
558                 append_to_cmdline(append_ipl_vmparm);
559
560         append_to_cmdline(append_ipl_scpdata);
561 }
562
563 /*
564  * Save ipl parameters, clear bss memory, initialize storage keys
565  * and create a kernel NSS at startup if the SAVESYS= parm is defined
566  */
567 void __init startup_init(void)
568 {
569         reset_tod_clock();
570         ipl_save_parameters();
571         rescue_initrd();
572         clear_bss_section();
573         ipl_verify_parameters();
574         time_early_init();
575         init_kernel_storage_key();
576         lockdep_off();
577         setup_lowcore_early();
578         setup_facility_list();
579         detect_machine_type();
580         setup_arch_string();
581         ipl_update_parameters();
582         setup_boot_command_line();
583         create_kernel_nss();
584         detect_diag9c();
585         detect_diag44();
586         detect_machine_facilities();
587         save_vector_registers();
588         setup_topology();
589         sclp_early_detect();
590         lockdep_on();
591 }