2 * Copyright IBM Corp. 2007, 2009
7 #define KMSG_COMPONENT "setup"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
21 #include <asm/ebcdic.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>
30 #include <asm/facility.h>
34 * Create a Kernel NSS if the SAVESYS= parameter is defined
36 #define DEFSYS_CMD_SIZE 128
37 #define SAVESYS_CMD_SIZE 32
39 char kernel_nss_name[NSS_NAME_SIZE + 1];
41 static void __init setup_boot_command_line(void);
44 * Get the TOD clock running.
46 static void __init reset_tod_clock(void)
50 if (store_tod_clock(&time) == 0)
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)
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;
61 #ifdef CONFIG_SHARED_KERNEL
62 int __init savesys_ipl_nss(char *cmd, const int cmdlen);
65 " .section .init.text,\"ax\",@progbits\n"
67 " .type savesys_ipl_nss, @function\n"
77 " .size savesys_ipl_nss, .-savesys_ipl_nss\n"
80 static __initdata char upper_command_line[COMMAND_LINE_SIZE];
82 static noinline __init void create_kernel_nss(void)
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;
92 char defsys_cmd[DEFSYS_CMD_SIZE];
93 char savesys_cmd[SAVESYS_CMD_SIZE];
95 /* Do nothing if we are not running under VM */
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]);
103 savesys_ptr = strstr(upper_command_line, "SAVESYS=");
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')
112 kernel_nss_name[i] = savesys_ptr[i];
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;
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);
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);
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';
142 __cpcmd(defsys_cmd, NULL, 0, &response);
145 pr_err("Defining the Linux kernel NSS failed with rc=%d\n",
147 kernel_nss_name[0] = '\0';
151 len = strlen(savesys_cmd);
152 ASCEBC(savesys_cmd, len);
153 response = savesys_ipl_nss(savesys_cmd, len);
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
161 if (response > SAVESYS_CMD_SIZE || response == 1) {
162 pr_err("Saving the Linux kernel NSS failed with rc=%d\n",
164 kernel_nss_name[0] = '\0';
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));
176 /* re-setup boot command line with new ipl vm parms */
177 ipl_update_parameters();
178 setup_boot_command_line();
180 ipl_flags = IPL_NSS_VALID;
183 #else /* CONFIG_SHARED_KERNEL */
185 static inline void create_kernel_nss(void) { }
187 #endif /* CONFIG_SHARED_KERNEL */
192 static noinline __init void clear_bss_section(void)
194 memset(__bss_start, 0, __bss_stop - __bss_start);
198 * Initialize storage key for kernel pages
200 static noinline __init void init_kernel_storage_key(void)
203 unsigned long end_pfn, init_pfn;
205 end_pfn = PFN_UP(__pa(&_end));
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);
213 static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
215 static noinline __init void detect_machine_type(void)
217 struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;
219 /* Check current-configuration-level */
220 if (stsi(NULL, 0, 0, 0) <= 2) {
221 S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
224 /* Get virtual-machine cpu information. */
225 if (stsi(vmms, 3, 2, 2) || !vmms->count)
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;
232 S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
235 /* Remove leading, trailing and double whitespace. */
236 static inline void strim_all(char *str)
242 memmove(str, s, strlen(s));
244 if (!isspace(*str++))
247 s = skip_spaces(str);
248 memmove(str, s, strlen(s) + 1);
253 static noinline __init void setup_arch_string(void)
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];
259 if (stsi(mach, 1, 1, 1))
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);
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);
275 MACHINE_IS_LPAR ? "LPAR" :
276 MACHINE_IS_VM ? "z/VM" :
277 MACHINE_IS_KVM ? "KVM" : "unknown");
279 dump_stack_set_arch_desc("%s (%s)", mstr, hvstr);
282 static __init void setup_topology(void)
286 if (!test_facility(11))
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)
293 topology_max_mnest = max_mnest;
296 static void early_pgm_check_handler(void)
298 const struct exception_table_entry *fixup;
299 unsigned long cr0, cr0_new;
302 addr = S390_lowcore.program_old_psw.addr;
303 fixup = search_exception_tables(addr);
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);
314 static noinline __init void setup_lowcore_early(void)
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;
327 static noinline __init void setup_facility_list(void)
329 stfle(S390_lowcore.stfle_fac_list,
330 ARRAY_SIZE(S390_lowcore.stfle_fac_list));
333 static __init void detect_diag9c(void)
335 unsigned int cpu_address;
338 cpu_address = stap();
339 diag_stat_inc(DIAG_STAT_X09C);
345 : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
347 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
350 static __init void detect_diag44(void)
354 diag_stat_inc(DIAG_STAT_X044);
360 : "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
362 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44;
365 static __init void detect_machine_facilities(void)
367 if (test_facility(8)) {
368 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
369 __ctl_set_bit(0, 23);
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);
385 if (test_facility(130)) {
386 S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
387 __ctl_set_bit(0, 20);
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);
399 static inline void save_vector_registers(void)
401 #ifdef CONFIG_CRASH_DUMP
402 if (test_facility(129))
403 save_vx_regs(boot_cpu_vector_save_area);
407 static int __init topology_setup(char *str)
412 rc = kstrtobool(str, &enabled);
414 S390_lowcore.machine_flags &= ~MACHINE_FLAG_TOPOLOGY;
417 early_param("topology", topology_setup);
419 static int __init disable_vector_extension(char *str)
421 S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
422 __ctl_clear_bit(0, 17);
425 early_param("novx", disable_vector_extension);
427 static int __init noexec_setup(char *str)
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);
440 early_param("noexec", noexec_setup);
442 static int __init cad_setup(char *str)
447 rc = kstrtobool(str, &enabled);
448 if (!rc && enabled && test_facility(128))
449 /* Enable problem state CAD. */
453 early_param("cad", cad_setup);
455 static __init void memmove_early(void *dst, const void *src, size_t n)
469 old = S390_lowcore.program_new_psw;
470 S390_lowcore.program_new_psw.mask = __extract_psw();
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"
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)
484 S390_lowcore.program_new_psw = old;
487 static __init noinline void ipl_save_parameters(void)
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;
497 static __init noinline void rescue_initrd(void)
499 #ifdef CONFIG_BLK_DEV_INITRD
500 unsigned long min_initrd_addr = (unsigned long) _end + (4UL << 20);
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.
507 if (!INITRD_START || !INITRD_SIZE)
509 if (INITRD_START >= min_initrd_addr)
511 memmove_early((void *) min_initrd_addr, (void *) INITRD_START, INITRD_SIZE);
512 INITRD_START = min_initrd_addr;
516 /* Set up boot command line */
517 static void __init append_to_cmdline(size_t (*ipl_data)(char *, size_t))
522 len = strlen(boot_command_line);
524 delim = boot_command_line + len; /* '\0' character position */
525 parm = boot_command_line + len + 1; /* append right after '\0' */
527 rc = ipl_data(parm, COMMAND_LINE_SIZE - len - 1);
530 memmove(boot_command_line, parm + 1, rc);
532 *delim = ' '; /* replace '\0' with space */
536 static inline int has_ebcdic_char(const char *str)
540 for (i = 0; str[i]; i++)
546 static void __init setup_boot_command_line(void)
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);
556 /* append IPL PARM data to the boot command line */
558 append_to_cmdline(append_ipl_vmparm);
560 append_to_cmdline(append_ipl_scpdata);
564 * Save ipl parameters, clear bss memory, initialize storage keys
565 * and create a kernel NSS at startup if the SAVESYS= parm is defined
567 void __init startup_init(void)
570 ipl_save_parameters();
573 ipl_verify_parameters();
575 init_kernel_storage_key();
577 setup_lowcore_early();
578 setup_facility_list();
579 detect_machine_type();
581 ipl_update_parameters();
582 setup_boot_command_line();
586 detect_machine_facilities();
587 save_vector_registers();