blob: cd241ee66eff4feb6d39842ac9972136394e23c1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Heiko Carstensab14de62007-02-05 21:18:37 +01002/*
Hans-Joachim Picht155af2f2009-06-16 10:30:52 +02003 * Copyright IBM Corp. 2007, 2009
Heiko Carstensab14de62007-02-05 21:18:37 +01004 * Author(s): Hongjie Yang <hongjie@us.ibm.com>,
5 * Heiko Carstens <heiko.carstens@de.ibm.com>
6 */
7
Hendrik Brueckner11af97e2009-09-11 10:28:53 +02008#define KMSG_COMPONENT "setup"
9#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10
Christian Borntraeger92e6ecf2009-03-26 15:23:58 +010011#include <linux/compiler.h>
Heiko Carstensab14de62007-02-05 21:18:37 +010012#include <linux/init.h>
13#include <linux/errno.h>
14#include <linux/string.h>
15#include <linux/ctype.h>
16#include <linux/lockdep.h>
Paul Gortmakerdcc096c2016-09-19 17:54:56 -040017#include <linux/extable.h>
Heiko Carstensab14de62007-02-05 21:18:37 +010018#include <linux/pfn.h>
19#include <linux/uaccess.h>
Hendrik Brueckner11af97e2009-09-11 10:28:53 +020020#include <linux/kernel.h>
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020021#include <asm/diag.h>
Hendrik Bruecknera0443fb2008-07-14 09:59:09 +020022#include <asm/ebcdic.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010023#include <asm/ipl.h>
Heiko Carstensab14de62007-02-05 21:18:37 +010024#include <asm/lowcore.h>
25#include <asm/processor.h>
26#include <asm/sections.h>
27#include <asm/setup.h>
Christian Borntraeger92e6ecf2009-03-26 15:23:58 +010028#include <asm/sysinfo.h>
Heiko Carstensab14de62007-02-05 21:18:37 +010029#include <asm/cpcmd.h>
30#include <asm/sclp.h>
David Howellsa0616cd2012-03-28 18:30:02 +010031#include <asm/facility.h>
Vasily Gorbik49698742018-05-15 13:28:53 +020032#include <asm/boot_data.h>
Vasily Gorbikc2313592019-11-14 14:08:28 +010033#include <asm/switch_to.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020034#include "entry.h"
Heiko Carstensab14de62007-02-05 21:18:37 +010035
Vasily Gorbik2e83e0e2019-08-11 20:55:18 +020036static void __init reset_tod_clock(void)
37{
38 u64 time;
39
40 if (store_tod_clock(&time) == 0)
41 return;
42 /* TOD clock not running. Set the clock to Unix Epoch. */
43 if (set_tod_clock(TOD_UNIX_EPOCH) != 0 || store_tod_clock(&time) != 0)
44 disabled_wait();
45
46 memset(tod_clock_base, 0, 16);
47 *(__u64 *) &tod_clock_base[1] = TOD_UNIX_EPOCH;
48 S390_lowcore.last_update_clock = TOD_UNIX_EPOCH;
49}
50
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +020051/*
Heiko Carstensab14de62007-02-05 21:18:37 +010052 * Initialize storage key for kernel pages
53 */
54static noinline __init void init_kernel_storage_key(void)
55{
Martin Schwidefsky127c1fe2013-10-07 12:12:32 +020056#if PAGE_DEFAULT_KEY
Heiko Carstensab14de62007-02-05 21:18:37 +010057 unsigned long end_pfn, init_pfn;
58
Vasily Gorbik320d9552018-02-20 13:28:33 +010059 end_pfn = PFN_UP(__pa(_end));
Heiko Carstensab14de62007-02-05 21:18:37 +010060
61 for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
Martin Schwidefskye2b8d7af2010-10-25 16:10:14 +020062 page_set_storage_key(init_pfn << PAGE_SHIFT,
63 PAGE_DEFAULT_KEY, 0);
Martin Schwidefsky127c1fe2013-10-07 12:12:32 +020064#endif
Heiko Carstensab14de62007-02-05 21:18:37 +010065}
66
Heiko Carstensfade4dc42012-09-04 14:26:03 +020067static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
Christian Borntraeger92e6ecf2009-03-26 15:23:58 +010068
Heiko Carstensab14de62007-02-05 21:18:37 +010069static noinline __init void detect_machine_type(void)
70{
Heiko Carstensfade4dc42012-09-04 14:26:03 +020071 struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;
72
Martin Schwidefsky27d71602010-02-26 22:37:38 +010073 /* Check current-configuration-level */
Heiko Carstenscaf757c2012-09-06 14:42:13 +020074 if (stsi(NULL, 0, 0, 0) <= 2) {
Martin Schwidefsky27d71602010-02-26 22:37:38 +010075 S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
Christian Borntraeger92e6ecf2009-03-26 15:23:58 +010076 return;
Martin Schwidefsky27d71602010-02-26 22:37:38 +010077 }
78 /* Get virtual-machine cpu information. */
Heiko Carstenscaf757c2012-09-06 14:42:13 +020079 if (stsi(vmms, 3, 2, 2) || !vmms->count)
Christian Borntraeger92e6ecf2009-03-26 15:23:58 +010080 return;
Heiko Carstensab14de62007-02-05 21:18:37 +010081
Christian Borntraeger03aa0472018-11-09 09:21:47 +010082 /* Detect known hypervisors */
Heiko Carstensfade4dc42012-09-04 14:26:03 +020083 if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3))
Heiko Carstensd3135e02009-09-11 10:28:45 +020084 S390_lowcore.machine_flags |= MACHINE_FLAG_KVM;
Christian Borntraeger03aa0472018-11-09 09:21:47 +010085 else if (!memcmp(vmms->vm[0].cpi, "\xa9\x61\xe5\xd4", 4))
Heiko Carstensd3135e02009-09-11 10:28:45 +020086 S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
Heiko Carstensab14de62007-02-05 21:18:37 +010087}
88
Heiko Carstensd2f03972017-03-11 11:58:27 +010089/* Remove leading, trailing and double whitespace. */
90static inline void strim_all(char *str)
91{
92 char *s;
93
94 s = strim(str);
95 if (s != str)
96 memmove(str, s, strlen(s));
97 while (*str) {
98 if (!isspace(*str++))
99 continue;
100 if (isspace(*str)) {
101 s = skip_spaces(str);
102 memmove(str, s, strlen(s) + 1);
103 }
104 }
105}
106
Christian Borntraeger4b8fe772016-05-24 15:23:20 +0200107static noinline __init void setup_arch_string(void)
108{
109 struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page;
Heiko Carstens2f8876f2017-03-11 12:22:11 +0100110 struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page;
111 char mstr[80], hvstr[17];
Christian Borntraeger4b8fe772016-05-24 15:23:20 +0200112
113 if (stsi(mach, 1, 1, 1))
114 return;
115 EBCASC(mach->manufacturer, sizeof(mach->manufacturer));
116 EBCASC(mach->type, sizeof(mach->type));
117 EBCASC(mach->model, sizeof(mach->model));
118 EBCASC(mach->model_capacity, sizeof(mach->model_capacity));
Heiko Carstensd2f03972017-03-11 11:58:27 +0100119 sprintf(mstr, "%-16.16s %-4.4s %-16.16s %-16.16s",
120 mach->manufacturer, mach->type,
121 mach->model, mach->model_capacity);
122 strim_all(mstr);
Heiko Carstens2f8876f2017-03-11 12:22:11 +0100123 if (stsi(vm, 3, 2, 2) == 0 && vm->count) {
124 EBCASC(vm->vm[0].cpi, sizeof(vm->vm[0].cpi));
125 sprintf(hvstr, "%-16.16s", vm->vm[0].cpi);
126 strim_all(hvstr);
127 } else {
128 sprintf(hvstr, "%s",
129 MACHINE_IS_LPAR ? "LPAR" :
130 MACHINE_IS_VM ? "z/VM" :
131 MACHINE_IS_KVM ? "KVM" : "unknown");
132 }
133 dump_stack_set_arch_desc("%s (%s)", mstr, hvstr);
Christian Borntraeger4b8fe772016-05-24 15:23:20 +0200134}
135
Heiko Carstensfade4dc42012-09-04 14:26:03 +0200136static __init void setup_topology(void)
137{
Heiko Carstensfade4dc42012-09-04 14:26:03 +0200138 int max_mnest;
139
140 if (!test_facility(11))
141 return;
142 S390_lowcore.machine_flags |= MACHINE_FLAG_TOPOLOGY;
143 for (max_mnest = 6; max_mnest > 1; max_mnest--) {
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200144 if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0)
Heiko Carstensfade4dc42012-09-04 14:26:03 +0200145 break;
146 }
147 topology_max_mnest = max_mnest;
Heiko Carstensfade4dc42012-09-04 14:26:03 +0200148}
149
Michael Holzheua3415702012-05-21 18:11:29 +0200150static void early_pgm_check_handler(void)
Heiko Carstensab14de62007-02-05 21:18:37 +0100151{
Heiko Carstensab14de62007-02-05 21:18:37 +0100152 const struct exception_table_entry *fixup;
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100153 unsigned long cr0, cr0_new;
Heiko Carstenseb608fb2012-09-05 13:26:11 +0200154 unsigned long addr;
Heiko Carstensab14de62007-02-05 21:18:37 +0100155
156 addr = S390_lowcore.program_old_psw.addr;
Gerald Schaefera80313f2019-02-03 21:37:20 +0100157 fixup = s390_search_extables(addr);
Heiko Carstensab14de62007-02-05 21:18:37 +0100158 if (!fixup)
Martin Schwidefsky98587c22019-04-30 12:33:45 +0200159 disabled_wait();
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100160 /* Disable low address protection before storing into lowcore. */
161 __ctl_store(cr0, 0, 0);
162 cr0_new = cr0 & ~(1UL << 28);
163 __ctl_load(cr0_new, 0, 0);
Heiko Carstensfecc868a2016-01-18 12:49:44 +0100164 S390_lowcore.program_old_psw.addr = extable_fixup(fixup);
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100165 __ctl_load(cr0, 0, 0);
Heiko Carstensab14de62007-02-05 21:18:37 +0100166}
167
Heiko Carstens5f954c32009-07-24 12:39:49 +0200168static noinline __init void setup_lowcore_early(void)
Heiko Carstensab14de62007-02-05 21:18:37 +0100169{
170 psw_t psw;
171
Martin Schwidefskyb50511e2011-10-30 15:16:50 +0100172 psw.mask = PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA;
Heiko Carstensfecc868a2016-01-18 12:49:44 +0100173 psw.addr = (unsigned long) s390_base_ext_handler;
Heiko Carstensab14de62007-02-05 21:18:37 +0100174 S390_lowcore.external_new_psw = psw;
Heiko Carstensfecc868a2016-01-18 12:49:44 +0100175 psw.addr = (unsigned long) s390_base_pgm_handler;
Heiko Carstensab14de62007-02-05 21:18:37 +0100176 S390_lowcore.program_new_psw = psw;
177 s390_base_pgm_handler_fn = early_pgm_check_handler;
Martin Schwidefskyc3601922016-10-25 12:21:44 +0200178 S390_lowcore.preempt_count = INIT_PREEMPT_COUNT;
Heiko Carstensab14de62007-02-05 21:18:37 +0100179}
180
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200181static noinline __init void setup_facility_list(void)
182{
Martin Schwidefskycf148992018-01-16 07:03:44 +0100183 memcpy(S390_lowcore.alt_stfle_fac_list,
184 S390_lowcore.stfle_fac_list,
185 sizeof(S390_lowcore.alt_stfle_fac_list));
Martin Schwidefskyd768bd82018-01-16 07:11:45 +0100186 if (!IS_ENABLED(CONFIG_KERNEL_NOBP))
187 __clear_facility(82, S390_lowcore.alt_stfle_fac_list);
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200188}
189
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200190static __init void detect_diag9c(void)
191{
192 unsigned int cpu_address;
193 int rc;
194
195 cpu_address = stap();
Martin Schwidefsky1ec27722015-08-20 17:28:44 +0200196 diag_stat_inc(DIAG_STAT_X09C);
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200197 asm volatile(
198 " diag %2,0,0x9c\n"
199 "0: la %0,0\n"
200 "1:\n"
201 EX_TABLE(0b,1b)
202 : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
203 if (!rc)
Heiko Carstensd3135e02009-09-11 10:28:45 +0200204 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200205}
206
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200207static __init void detect_machine_facilities(void)
208{
Heiko Carstens3c7ef082012-09-28 15:06:41 +0200209 if (test_facility(8)) {
210 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
211 __ctl_set_bit(0, 23);
212 }
Heiko Carstens85e9d0e52012-09-27 10:45:06 +0200213 if (test_facility(78))
214 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2;
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200215 if (test_facility(3))
Heiko Carstensd3135e02009-09-11 10:28:45 +0200216 S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
Heiko Carstensa1c5bef2017-11-09 12:29:34 +0100217 if (test_facility(50) && test_facility(73)) {
Martin Schwidefskyd35339a2012-07-31 11:03:04 +0200218 S390_lowcore.machine_flags |= MACHINE_FLAG_TE;
Heiko Carstensa1c5bef2017-11-09 12:29:34 +0100219 __ctl_set_bit(0, 55);
220 }
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200221 if (test_facility(51))
222 S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC;
Hendrik Bruecknerb5510d92015-09-29 10:04:41 +0200223 if (test_facility(129)) {
Martin Schwidefsky80703612014-10-06 17:53:53 +0200224 S390_lowcore.machine_flags |= MACHINE_FLAG_VX;
Hendrik Bruecknerb5510d92015-09-29 10:04:41 +0200225 __ctl_set_bit(0, 17);
226 }
Vasily Gorbikd3baaeb2019-10-22 17:37:51 +0200227 if (test_facility(130) && !noexec_disabled) {
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100228 S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
229 __ctl_set_bit(0, 20);
230 }
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100231 if (test_facility(133))
232 S390_lowcore.machine_flags |= MACHINE_FLAG_GS;
Martin Schwidefsky6e2ef5e2016-10-27 12:41:39 +0200233 if (test_facility(139) && (tod_clock_base[1] & 0x80)) {
234 /* Enabled signed clock comparator comparisons */
235 S390_lowcore.machine_flags |= MACHINE_FLAG_SCC;
236 clock_comparator_max = -1ULL >> 1;
237 __ctl_set_bit(0, 53);
238 }
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200239}
240
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100241static inline void save_vector_registers(void)
242{
243#ifdef CONFIG_CRASH_DUMP
244 if (test_facility(129))
245 save_vx_regs(boot_cpu_vector_save_area);
246#endif
247}
248
Vasily Gorbikc02ee6a2019-11-12 17:35:28 +0100249static inline void setup_control_registers(void)
250{
251 unsigned long reg;
252
253 __ctl_store(reg, 0, 0);
254 reg |= CR0_LOW_ADDRESS_PROTECTION;
255 reg |= CR0_EMERGENCY_SIGNAL_SUBMASK;
256 reg |= CR0_EXTERNAL_CALL_SUBMASK;
257 __ctl_load(reg, 0, 0);
258}
259
Vasily Gorbikc2313592019-11-14 14:08:28 +0100260static inline void setup_access_registers(void)
261{
262 unsigned int acrs[NUM_ACRS] = { 0 };
263
264 restore_access_regs(acrs);
265}
266
Hendrik Bruecknerb5510d92015-09-29 10:04:41 +0200267static int __init disable_vector_extension(char *str)
268{
269 S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
270 __ctl_clear_bit(0, 17);
Heiko Carstens673cfdd2017-08-11 15:54:16 +0200271 return 0;
Hendrik Bruecknerb5510d92015-09-29 10:04:41 +0200272}
273early_param("novx", disable_vector_extension);
274
Martin Schwidefsky61b0b012015-02-19 17:53:16 +0100275static int __init cad_setup(char *str)
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100276{
Martin Schwidefskyb13de4b2017-03-24 17:00:45 +0100277 bool enabled;
278 int rc;
Martin Schwidefsky61b0b012015-02-19 17:53:16 +0100279
Martin Schwidefskyb13de4b2017-03-24 17:00:45 +0100280 rc = kstrtobool(str, &enabled);
281 if (!rc && enabled && test_facility(128))
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100282 /* Enable problem state CAD. */
283 __ctl_set_bit(2, 3);
Martin Schwidefskyb13de4b2017-03-24 17:00:45 +0100284 return rc;
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100285}
Martin Schwidefskyb13de4b2017-03-24 17:00:45 +0100286early_param("cad", cad_setup);
Martin Schwidefsky2c72a442015-01-14 17:52:33 +0100287
Vasily Gorbik49698742018-05-15 13:28:53 +0200288char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
Michael Holzheu1fb81052011-12-27 11:25:46 +0100289static void __init setup_boot_command_line(void)
290{
Hendrik Bruecknera0443fb2008-07-14 09:59:09 +0200291 /* copy arch command line */
Vasily Gorbik49698742018-05-15 13:28:53 +0200292 strlcpy(boot_command_line, early_command_line, ARCH_COMMAND_LINE_SIZE);
Hendrik Bruecknera0443fb2008-07-14 09:59:09 +0200293}
294
Vasily Gorbik627c9b62018-06-25 13:59:35 +0200295static void __init check_image_bootable(void)
296{
297 if (!memcmp(EP_STRING, (void *)EP_OFFSET, strlen(EP_STRING)))
298 return;
299
300 sclp_early_printk("Linux kernel boot failure: An attempt to boot a vmlinux ELF image failed.\n");
301 sclp_early_printk("This image does not contain all parts necessary for starting up. Use\n");
302 sclp_early_printk("bzImage or arch/s390/boot/compressed/vmlinux instead.\n");
Martin Schwidefsky98587c22019-04-30 12:33:45 +0200303 disabled_wait();
Vasily Gorbik627c9b62018-06-25 13:59:35 +0200304}
305
Heiko Carstensab14de62007-02-05 21:18:37 +0100306void __init startup_init(void)
307{
Vasily Gorbik2e83e0e2019-08-11 20:55:18 +0200308 reset_tod_clock();
Vasily Gorbik627c9b62018-06-25 13:59:35 +0200309 check_image_bootable();
Martin Schwidefskyb1c08542016-10-10 09:43:41 +0200310 time_early_init();
Heiko Carstensab14de62007-02-05 21:18:37 +0100311 init_kernel_storage_key();
Heiko Carstensab14de62007-02-05 21:18:37 +0100312 lockdep_off();
Heiko Carstensab14de62007-02-05 21:18:37 +0100313 setup_lowcore_early();
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200314 setup_facility_list();
Hendrik Bruecknera0443fb2008-07-14 09:59:09 +0200315 detect_machine_type();
Christian Borntraeger4b8fe772016-05-24 15:23:20 +0200316 setup_arch_string();
Hendrik Bruecknera0443fb2008-07-14 09:59:09 +0200317 setup_boot_command_line();
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200318 detect_diag9c();
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200319 detect_machine_facilities();
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100320 save_vector_registers();
Heiko Carstensfade4dc42012-09-04 14:26:03 +0200321 setup_topology();
Michael Holzheu7b50da52013-11-13 10:38:27 +0100322 sclp_early_detect();
Vasily Gorbikc02ee6a2019-11-12 17:35:28 +0100323 setup_control_registers();
Vasily Gorbikc2313592019-11-14 14:08:28 +0100324 setup_access_registers();
Heiko Carstensab14de62007-02-05 21:18:37 +0100325 lockdep_on();
326}