Merge tag 'timers-core-2023-09-04-v2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / arch / s390 / kernel / early.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
ab14de6c 2/*
155af2f9 3 * Copyright IBM Corp. 2007, 2009
ab14de6c 4 * Author(s): Hongjie Yang <hongjie@us.ibm.com>,
ab14de6c
HC
5 */
6
11af97e1
HB
7#define KMSG_COMPONENT "setup"
8#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9
92e6ecf3 10#include <linux/compiler.h>
ab14de6c
HC
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>
dcc096c5 16#include <linux/extable.h>
ab14de6c
HC
17#include <linux/pfn.h>
18#include <linux/uaccess.h>
11af97e1 19#include <linux/kernel.h>
d09a307f 20#include <asm/asm-extable.h>
bb1520d5 21#include <linux/memblock.h>
1ec2772e 22#include <asm/diag.h>
a0443fbb 23#include <asm/ebcdic.h>
46b05d26 24#include <asm/ipl.h>
ab14de6c
HC
25#include <asm/lowcore.h>
26#include <asm/processor.h>
27#include <asm/sections.h>
28#include <asm/setup.h>
92e6ecf3 29#include <asm/sysinfo.h>
ab14de6c
HC
30#include <asm/cpcmd.h>
31#include <asm/sclp.h>
a0616cde 32#include <asm/facility.h>
49698745 33#include <asm/boot_data.h>
c2313594 34#include <asm/switch_to.h>
a806170e 35#include "entry.h"
ab14de6c 36
8ee0d2fb
VG
37#define decompressor_handled_param(param) \
38static int __init ignore_decompressor_param_##param(char *s) \
39{ \
40 return 0; \
41} \
42early_param(#param, ignore_decompressor_param_##param)
43
44decompressor_handled_param(mem);
45decompressor_handled_param(vmalloc);
46decompressor_handled_param(dfltcc);
47decompressor_handled_param(noexec);
48decompressor_handled_param(facilities);
49decompressor_handled_param(nokaslr);
50#if IS_ENABLED(CONFIG_KVM)
51decompressor_handled_param(prot_virt);
52#endif
53
557b1970
VG
54static void __init kasan_early_init(void)
55{
56#ifdef CONFIG_KASAN
57 init_task.kasan_depth = 0;
58 sclp_early_printk("KernelAddressSanitizer initialized\n");
59#endif
60}
61
2e83e0eb
VG
62static void __init reset_tod_clock(void)
63{
530f639f 64 union tod_clock clk;
2e83e0eb 65
530f639f 66 if (store_tod_clock_ext_cc(&clk) == 0)
2e83e0eb
VG
67 return;
68 /* TOD clock not running. Set the clock to Unix Epoch. */
530f639f 69 if (set_tod_clock(TOD_UNIX_EPOCH) || store_tod_clock_ext_cc(&clk))
2e83e0eb
VG
70 disabled_wait();
71
f8d8977a
HC
72 memset(&tod_clock_base, 0, sizeof(tod_clock_base));
73 tod_clock_base.tod = TOD_UNIX_EPOCH;
2e83e0eb
VG
74 S390_lowcore.last_update_clock = TOD_UNIX_EPOCH;
75}
76
ab14de6c
HC
77/*
78 * Initialize storage key for kernel pages
79 */
80static noinline __init void init_kernel_storage_key(void)
81{
127c1fef 82#if PAGE_DEFAULT_KEY
ab14de6c
HC
83 unsigned long end_pfn, init_pfn;
84
320d9555 85 end_pfn = PFN_UP(__pa(_end));
ab14de6c
HC
86
87 for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
e2b8d7af
MS
88 page_set_storage_key(init_pfn << PAGE_SHIFT,
89 PAGE_DEFAULT_KEY, 0);
127c1fef 90#endif
ab14de6c
HC
91}
92
fade4dc4 93static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
92e6ecf3 94
ab14de6c
HC
95static noinline __init void detect_machine_type(void)
96{
fade4dc4
HC
97 struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;
98
27d71602 99 /* Check current-configuration-level */
caf757c6 100 if (stsi(NULL, 0, 0, 0) <= 2) {
27d71602 101 S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
92e6ecf3 102 return;
27d71602
MS
103 }
104 /* Get virtual-machine cpu information. */
caf757c6 105 if (stsi(vmms, 3, 2, 2) || !vmms->count)
92e6ecf3 106 return;
ab14de6c 107
03aa047e 108 /* Detect known hypervisors */
fade4dc4 109 if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3))
d3135e0c 110 S390_lowcore.machine_flags |= MACHINE_FLAG_KVM;
03aa047e 111 else if (!memcmp(vmms->vm[0].cpi, "\xa9\x61\xe5\xd4", 4))
d3135e0c 112 S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
ab14de6c
HC
113}
114
d2f03974
HC
115/* Remove leading, trailing and double whitespace. */
116static inline void strim_all(char *str)
117{
118 char *s;
119
120 s = strim(str);
121 if (s != str)
122 memmove(str, s, strlen(s));
123 while (*str) {
124 if (!isspace(*str++))
125 continue;
126 if (isspace(*str)) {
127 s = skip_spaces(str);
128 memmove(str, s, strlen(s) + 1);
129 }
130 }
131}
132
4b8fe77a
CB
133static noinline __init void setup_arch_string(void)
134{
135 struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page;
2f8876f9
HC
136 struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page;
137 char mstr[80], hvstr[17];
4b8fe77a
CB
138
139 if (stsi(mach, 1, 1, 1))
140 return;
141 EBCASC(mach->manufacturer, sizeof(mach->manufacturer));
142 EBCASC(mach->type, sizeof(mach->type));
143 EBCASC(mach->model, sizeof(mach->model));
144 EBCASC(mach->model_capacity, sizeof(mach->model_capacity));
d2f03974
HC
145 sprintf(mstr, "%-16.16s %-4.4s %-16.16s %-16.16s",
146 mach->manufacturer, mach->type,
147 mach->model, mach->model_capacity);
148 strim_all(mstr);
2f8876f9
HC
149 if (stsi(vm, 3, 2, 2) == 0 && vm->count) {
150 EBCASC(vm->vm[0].cpi, sizeof(vm->vm[0].cpi));
151 sprintf(hvstr, "%-16.16s", vm->vm[0].cpi);
152 strim_all(hvstr);
153 } else {
154 sprintf(hvstr, "%s",
155 MACHINE_IS_LPAR ? "LPAR" :
156 MACHINE_IS_VM ? "z/VM" :
157 MACHINE_IS_KVM ? "KVM" : "unknown");
158 }
159 dump_stack_set_arch_desc("%s (%s)", mstr, hvstr);
4b8fe77a
CB
160}
161
fade4dc4
HC
162static __init void setup_topology(void)
163{
fade4dc4
HC
164 int max_mnest;
165
166 if (!test_facility(11))
167 return;
168 S390_lowcore.machine_flags |= MACHINE_FLAG_TOPOLOGY;
169 for (max_mnest = 6; max_mnest > 1; max_mnest--) {
caf757c6 170 if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0)
fade4dc4
HC
171 break;
172 }
173 topology_max_mnest = max_mnest;
fade4dc4
HC
174}
175
85806016 176void __do_early_pgm_check(struct pt_regs *regs)
ab14de6c 177{
46fee16f 178 if (!fixup_exception(regs))
98587c2d 179 disabled_wait();
ab14de6c
HC
180}
181
5f954c34 182static noinline __init void setup_lowcore_early(void)
ab14de6c
HC
183{
184 psw_t psw;
185
85806016 186 psw.addr = (unsigned long)early_pgm_check_handler;
bb1520d5 187 psw.mask = PSW_KERNEL_BITS;
ab14de6c 188 S390_lowcore.program_new_psw = psw;
c360192b 189 S390_lowcore.preempt_count = INIT_PREEMPT_COUNT;
ab14de6c
HC
190}
191
14375bc4
MS
192static noinline __init void setup_facility_list(void)
193{
17e89e13 194 memcpy(alt_stfle_fac_list, stfle_fac_list, sizeof(alt_stfle_fac_list));
d768bd89 195 if (!IS_ENABLED(CONFIG_KERNEL_NOBP))
17e89e13 196 __clear_facility(82, alt_stfle_fac_list);
14375bc4
MS
197}
198
2e5061e4
HC
199static __init void detect_diag9c(void)
200{
201 unsigned int cpu_address;
202 int rc;
203
204 cpu_address = stap();
1ec2772e 205 diag_stat_inc(DIAG_STAT_X09C);
2e5061e4
HC
206 asm volatile(
207 " diag %2,0,0x9c\n"
208 "0: la %0,0\n"
209 "1:\n"
210 EX_TABLE(0b,1b)
211 : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
212 if (!rc)
d3135e0c 213 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
2e5061e4
HC
214}
215
2e5061e4
HC
216static __init void detect_machine_facilities(void)
217{
3c7ef08b
HC
218 if (test_facility(8)) {
219 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
220 __ctl_set_bit(0, 23);
221 }
85e9d0e5
HC
222 if (test_facility(78))
223 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2;
14375bc4 224 if (test_facility(3))
d3135e0c 225 S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
a1c5befc 226 if (test_facility(50) && test_facility(73)) {
d35339a4 227 S390_lowcore.machine_flags |= MACHINE_FLAG_TE;
a1c5befc
HC
228 __ctl_set_bit(0, 55);
229 }
1b948d6c
MS
230 if (test_facility(51))
231 S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC;
b5510d9b 232 if (test_facility(129)) {
80703617 233 S390_lowcore.machine_flags |= MACHINE_FLAG_VX;
b5510d9b
HB
234 __ctl_set_bit(0, 17);
235 }
d3baaeb5 236 if (test_facility(130) && !noexec_disabled) {
57d7f939
MS
237 S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
238 __ctl_set_bit(0, 20);
239 }
916cda1a
MS
240 if (test_facility(133))
241 S390_lowcore.machine_flags |= MACHINE_FLAG_GS;
f8d8977a 242 if (test_facility(139) && (tod_clock_base.tod >> 63)) {
6e2ef5e4
MS
243 /* Enabled signed clock comparator comparisons */
244 S390_lowcore.machine_flags |= MACHINE_FLAG_SCC;
245 clock_comparator_max = -1ULL >> 1;
246 __ctl_set_bit(0, 53);
247 }
3322ba0d
NS
248 if (IS_ENABLED(CONFIG_PCI) && test_facility(153)) {
249 S390_lowcore.machine_flags |= MACHINE_FLAG_PCI_MIO;
250 /* the control bit is set during PCI initialization */
251 }
0807b856
GS
252 if (test_facility(194))
253 S390_lowcore.machine_flags |= MACHINE_FLAG_RDP;
b5510d9b
HB
254}
255
1a36a39e
MS
256static inline void save_vector_registers(void)
257{
258#ifdef CONFIG_CRASH_DUMP
259 if (test_facility(129))
260 save_vx_regs(boot_cpu_vector_save_area);
261#endif
262}
263
c02ee6a1
VG
264static inline void setup_control_registers(void)
265{
266 unsigned long reg;
267
268 __ctl_store(reg, 0, 0);
269 reg |= CR0_LOW_ADDRESS_PROTECTION;
270 reg |= CR0_EMERGENCY_SIGNAL_SUBMASK;
271 reg |= CR0_EXTERNAL_CALL_SUBMASK;
272 __ctl_load(reg, 0, 0);
273}
274
c2313594
VG
275static inline void setup_access_registers(void)
276{
277 unsigned int acrs[NUM_ACRS] = { 0 };
278
279 restore_access_regs(acrs);
280}
281
b5510d9b
HB
282static int __init disable_vector_extension(char *str)
283{
284 S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
285 __ctl_clear_bit(0, 17);
673cfddf 286 return 0;
2e5061e4 287}
b5510d9b 288early_param("novx", disable_vector_extension);
2e5061e4 289
49698745 290char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
1fb81057
MH
291static void __init setup_boot_command_line(void)
292{
a0443fbb 293 /* copy arch command line */
820109fb 294 strscpy(boot_command_line, early_command_line, COMMAND_LINE_SIZE);
a0443fbb
HB
295}
296
a156f09c
HC
297static void __init sort_amode31_extable(void)
298{
299 sort_extable(__start_amode31_ex_table, __stop_amode31_ex_table);
300}
301
ab14de6c
HC
302void __init startup_init(void)
303{
557b1970 304 kasan_early_init();
2e83e0eb 305 reset_tod_clock();
b1c0854d 306 time_early_init();
ab14de6c 307 init_kernel_storage_key();
ab14de6c 308 lockdep_off();
a156f09c 309 sort_amode31_extable();
ab14de6c 310 setup_lowcore_early();
14375bc4 311 setup_facility_list();
a0443fbb 312 detect_machine_type();
4b8fe77a 313 setup_arch_string();
a0443fbb 314 setup_boot_command_line();
2e5061e4 315 detect_diag9c();
2e5061e4 316 detect_machine_facilities();
1a36a39e 317 save_vector_registers();
fade4dc4 318 setup_topology();
7b50da53 319 sclp_early_detect();
c02ee6a1 320 setup_control_registers();
c2313594 321 setup_access_registers();
ab14de6c
HC
322 lockdep_on();
323}