blob: 2345ea332b97e21c99442d4e79883510bc9ae679 [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>,
Heiko Carstensab14de62007-02-05 21:18:37 +01005 */
6
Hendrik Brueckner11af97e2009-09-11 10:28:53 +02007#define KMSG_COMPONENT "setup"
8#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9
Christian Borntraeger92e6ecf2009-03-26 15:23:58 +010010#include <linux/compiler.h>
Heiko Carstensab14de62007-02-05 21:18:37 +010011#include <linux/init.h>
12#include <linux/errno.h>
13#include <linux/string.h>
14#include <linux/ctype.h>
15#include <linux/lockdep.h>
Paul Gortmakerdcc096c2016-09-19 17:54:56 -040016#include <linux/extable.h>
Heiko Carstensab14de62007-02-05 21:18:37 +010017#include <linux/pfn.h>
18#include <linux/uaccess.h>
Hendrik Brueckner11af97e2009-09-11 10:28:53 +020019#include <linux/kernel.h>
Heiko Carstensd09a3072022-02-28 11:22:12 +010020#include <asm/asm-extable.h>
Alexander Gordeevbb1520d2022-12-13 11:35:11 +010021#include <linux/memblock.h>
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020022#include <asm/diag.h>
Hendrik Bruecknera0443fb2008-07-14 09:59:09 +020023#include <asm/ebcdic.h>
Michael Holzheu46b05d22007-02-21 10:55:21 +010024#include <asm/ipl.h>
Heiko Carstensab14de62007-02-05 21:18:37 +010025#include <asm/lowcore.h>
26#include <asm/processor.h>
27#include <asm/sections.h>
28#include <asm/setup.h>
Christian Borntraeger92e6ecf2009-03-26 15:23:58 +010029#include <asm/sysinfo.h>
Heiko Carstensab14de62007-02-05 21:18:37 +010030#include <asm/cpcmd.h>
31#include <asm/sclp.h>
David Howellsa0616cd2012-03-28 18:30:02 +010032#include <asm/facility.h>
Vasily Gorbik49698742018-05-15 13:28:53 +020033#include <asm/boot_data.h>
Vasily Gorbikc2313592019-11-14 14:08:28 +010034#include <asm/switch_to.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020035#include "entry.h"
Heiko Carstensab14de62007-02-05 21:18:37 +010036
Vasily Gorbik8ee0d2f2023-02-17 14:00:54 +010037#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);
Vasily Gorbik8ee0d2f2023-02-17 14:00:54 +010047decompressor_handled_param(facilities);
48decompressor_handled_param(nokaslr);
Heiko Carstens468a3bc2023-10-27 14:12:36 +020049decompressor_handled_param(cmma);
Vasily Gorbik8ee0d2f2023-02-17 14:00:54 +010050#if IS_ENABLED(CONFIG_KVM)
51decompressor_handled_param(prot_virt);
52#endif
53
Vasily Gorbik557b1972023-02-09 22:05:11 +010054static 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
Vasily Gorbik2e83e0e2019-08-11 20:55:18 +020062static void __init reset_tod_clock(void)
63{
Heiko Carstens530f6392021-02-08 13:56:49 +010064 union tod_clock clk;
Vasily Gorbik2e83e0e2019-08-11 20:55:18 +020065
Heiko Carstens530f6392021-02-08 13:56:49 +010066 if (store_tod_clock_ext_cc(&clk) == 0)
Vasily Gorbik2e83e0e2019-08-11 20:55:18 +020067 return;
68 /* TOD clock not running. Set the clock to Unix Epoch. */
Heiko Carstens530f6392021-02-08 13:56:49 +010069 if (set_tod_clock(TOD_UNIX_EPOCH) || store_tod_clock_ext_cc(&clk))
Vasily Gorbik2e83e0e2019-08-11 20:55:18 +020070 disabled_wait();
71
Heiko Carstensf8d89772021-02-08 16:06:10 +010072 memset(&tod_clock_base, 0, sizeof(tod_clock_base));
73 tod_clock_base.tod = TOD_UNIX_EPOCH;
Vasily Gorbik2e83e0e2019-08-11 20:55:18 +020074 S390_lowcore.last_update_clock = TOD_UNIX_EPOCH;
75}
76
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +020077/*
Heiko Carstensab14de62007-02-05 21:18:37 +010078 * Initialize storage key for kernel pages
79 */
80static noinline __init void init_kernel_storage_key(void)
81{
Martin Schwidefsky127c1fe2013-10-07 12:12:32 +020082#if PAGE_DEFAULT_KEY
Heiko Carstensab14de62007-02-05 21:18:37 +010083 unsigned long end_pfn, init_pfn;
84
Vasily Gorbik320d9552018-02-20 13:28:33 +010085 end_pfn = PFN_UP(__pa(_end));
Heiko Carstensab14de62007-02-05 21:18:37 +010086
87 for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
Martin Schwidefskye2b8d7af2010-10-25 16:10:14 +020088 page_set_storage_key(init_pfn << PAGE_SHIFT,
89 PAGE_DEFAULT_KEY, 0);
Martin Schwidefsky127c1fe2013-10-07 12:12:32 +020090#endif
Heiko Carstensab14de62007-02-05 21:18:37 +010091}
92
Heiko Carstensfade4dc42012-09-04 14:26:03 +020093static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
Christian Borntraeger92e6ecf2009-03-26 15:23:58 +010094
Heiko Carstensab14de62007-02-05 21:18:37 +010095static noinline __init void detect_machine_type(void)
96{
Heiko Carstensfade4dc42012-09-04 14:26:03 +020097 struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;
98
Martin Schwidefsky27d71602010-02-26 22:37:38 +010099 /* Check current-configuration-level */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200100 if (stsi(NULL, 0, 0, 0) <= 2) {
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100101 S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
Christian Borntraeger92e6ecf2009-03-26 15:23:58 +0100102 return;
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100103 }
104 /* Get virtual-machine cpu information. */
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200105 if (stsi(vmms, 3, 2, 2) || !vmms->count)
Christian Borntraeger92e6ecf2009-03-26 15:23:58 +0100106 return;
Heiko Carstensab14de62007-02-05 21:18:37 +0100107
Christian Borntraeger03aa0472018-11-09 09:21:47 +0100108 /* Detect known hypervisors */
Heiko Carstensfade4dc42012-09-04 14:26:03 +0200109 if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3))
Heiko Carstensd3135e02009-09-11 10:28:45 +0200110 S390_lowcore.machine_flags |= MACHINE_FLAG_KVM;
Christian Borntraeger03aa0472018-11-09 09:21:47 +0100111 else if (!memcmp(vmms->vm[0].cpi, "\xa9\x61\xe5\xd4", 4))
Heiko Carstensd3135e02009-09-11 10:28:45 +0200112 S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
Heiko Carstensab14de62007-02-05 21:18:37 +0100113}
114
Heiko Carstensd2f03972017-03-11 11:58:27 +0100115/* 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
Christian Borntraeger4b8fe772016-05-24 15:23:20 +0200133static noinline __init void setup_arch_string(void)
134{
135 struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page;
Heiko Carstens2f8876f2017-03-11 12:22:11 +0100136 struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page;
137 char mstr[80], hvstr[17];
Christian Borntraeger4b8fe772016-05-24 15:23:20 +0200138
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));
Heiko Carstensd2f03972017-03-11 11:58:27 +0100145 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);
Heiko Carstens2f8876f2017-03-11 12:22:11 +0100149 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);
Christian Borntraeger4b8fe772016-05-24 15:23:20 +0200160}
161
Heiko Carstensfade4dc42012-09-04 14:26:03 +0200162static __init void setup_topology(void)
163{
Heiko Carstensfade4dc42012-09-04 14:26:03 +0200164 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--) {
Heiko Carstenscaf757c2012-09-06 14:42:13 +0200170 if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0)
Heiko Carstensfade4dc42012-09-04 14:26:03 +0200171 break;
172 }
173 topology_max_mnest = max_mnest;
Heiko Carstensfade4dc42012-09-04 14:26:03 +0200174}
175
Heiko Carstens85806012022-05-20 19:23:48 +0200176void __do_early_pgm_check(struct pt_regs *regs)
Heiko Carstensab14de62007-02-05 21:18:37 +0100177{
Heiko Carstens46fee162022-02-28 14:29:25 +0100178 if (!fixup_exception(regs))
Martin Schwidefsky98587c22019-04-30 12:33:45 +0200179 disabled_wait();
Heiko Carstensab14de62007-02-05 21:18:37 +0100180}
181
Heiko Carstens5f954c32009-07-24 12:39:49 +0200182static noinline __init void setup_lowcore_early(void)
Heiko Carstensab14de62007-02-05 21:18:37 +0100183{
184 psw_t psw;
185
Heiko Carstens85806012022-05-20 19:23:48 +0200186 psw.addr = (unsigned long)early_pgm_check_handler;
Alexander Gordeevbb1520d2022-12-13 11:35:11 +0100187 psw.mask = PSW_KERNEL_BITS;
Heiko Carstensab14de62007-02-05 21:18:37 +0100188 S390_lowcore.program_new_psw = psw;
Martin Schwidefskyc3601922016-10-25 12:21:44 +0200189 S390_lowcore.preempt_count = INIT_PREEMPT_COUNT;
Heiko Carstensab14de62007-02-05 21:18:37 +0100190}
191
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200192static noinline __init void setup_facility_list(void)
193{
Sven Schnelle17e89e12021-05-05 22:01:10 +0200194 memcpy(alt_stfle_fac_list, stfle_fac_list, sizeof(alt_stfle_fac_list));
Martin Schwidefskyd768bd82018-01-16 07:11:45 +0100195 if (!IS_ENABLED(CONFIG_KERNEL_NOBP))
Sven Schnelle17e89e12021-05-05 22:01:10 +0200196 __clear_facility(82, alt_stfle_fac_list);
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200197}
198
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200199static __init void detect_diag9c(void)
200{
201 unsigned int cpu_address;
202 int rc;
203
204 cpu_address = stap();
Martin Schwidefsky1ec27722015-08-20 17:28:44 +0200205 diag_stat_inc(DIAG_STAT_X09C);
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200206 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)
Heiko Carstensd3135e02009-09-11 10:28:45 +0200213 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200214}
215
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200216static __init void detect_machine_facilities(void)
217{
Heiko Carstens3c7ef082012-09-28 15:06:41 +0200218 if (test_facility(8)) {
219 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
Heiko Carstens99441a32023-09-11 21:40:13 +0200220 system_ctl_set_bit(0, CR0_EDAT_BIT);
Heiko Carstens3c7ef082012-09-28 15:06:41 +0200221 }
Heiko Carstens85e9d0e52012-09-27 10:45:06 +0200222 if (test_facility(78))
223 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2;
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200224 if (test_facility(3))
Heiko Carstensd3135e02009-09-11 10:28:45 +0200225 S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
Heiko Carstensa1c5bef2017-11-09 12:29:34 +0100226 if (test_facility(50) && test_facility(73)) {
Martin Schwidefskyd35339a2012-07-31 11:03:04 +0200227 S390_lowcore.machine_flags |= MACHINE_FLAG_TE;
Heiko Carstens99441a32023-09-11 21:40:13 +0200228 system_ctl_set_bit(0, CR0_TRANSACTIONAL_EXECUTION_BIT);
Heiko Carstensa1c5bef2017-11-09 12:29:34 +0100229 }
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200230 if (test_facility(51))
231 S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC;
Heiko Carstens18564752023-12-01 15:42:19 +0100232 if (test_facility(129))
Heiko Carstens99441a32023-09-11 21:40:13 +0200233 system_ctl_set_bit(0, CR0_VECTOR_BIT);
Heiko Carstensc0f1d472023-08-25 14:29:48 +0200234 if (test_facility(130))
Martin Schwidefsky57d7f932016-03-22 10:54:24 +0100235 S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100236 if (test_facility(133))
237 S390_lowcore.machine_flags |= MACHINE_FLAG_GS;
Heiko Carstensf8d89772021-02-08 16:06:10 +0100238 if (test_facility(139) && (tod_clock_base.tod >> 63)) {
Martin Schwidefsky6e2ef5e2016-10-27 12:41:39 +0200239 /* Enabled signed clock comparator comparisons */
240 S390_lowcore.machine_flags |= MACHINE_FLAG_SCC;
241 clock_comparator_max = -1ULL >> 1;
Heiko Carstens99441a32023-09-11 21:40:13 +0200242 system_ctl_set_bit(0, CR0_CLOCK_COMPARATOR_SIGN_BIT);
Martin Schwidefsky6e2ef5e2016-10-27 12:41:39 +0200243 }
Niklas Schnelle3322ba02021-07-08 14:55:42 +0200244 if (IS_ENABLED(CONFIG_PCI) && test_facility(153)) {
245 S390_lowcore.machine_flags |= MACHINE_FLAG_PCI_MIO;
246 /* the control bit is set during PCI initialization */
247 }
Gerald Schaefer0807b852023-02-06 17:48:21 +0100248 if (test_facility(194))
249 S390_lowcore.machine_flags |= MACHINE_FLAG_RDP;
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200250}
251
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100252static inline void save_vector_registers(void)
253{
254#ifdef CONFIG_CRASH_DUMP
255 if (test_facility(129))
256 save_vx_regs(boot_cpu_vector_save_area);
257#endif
258}
259
Heiko Carstens4a172522023-09-05 15:49:37 +0200260static inline void setup_low_address_protection(void)
Vasily Gorbikc02ee6a2019-11-12 17:35:28 +0100261{
Heiko Carstens99441a32023-09-11 21:40:13 +0200262 system_ctl_set_bit(0, CR0_LOW_ADDRESS_PROTECTION_BIT);
Vasily Gorbikc02ee6a2019-11-12 17:35:28 +0100263}
264
Vasily Gorbikc2313592019-11-14 14:08:28 +0100265static inline void setup_access_registers(void)
266{
267 unsigned int acrs[NUM_ACRS] = { 0 };
268
269 restore_access_regs(acrs);
270}
271
Vasily Gorbik49698742018-05-15 13:28:53 +0200272char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
Michael Holzheu1fb81052011-12-27 11:25:46 +0100273static void __init setup_boot_command_line(void)
274{
Hendrik Bruecknera0443fb2008-07-14 09:59:09 +0200275 /* copy arch command line */
Wolfram Sang820109f2022-08-18 22:59:47 +0200276 strscpy(boot_command_line, early_command_line, COMMAND_LINE_SIZE);
Hendrik Bruecknera0443fb2008-07-14 09:59:09 +0200277}
278
Heiko Carstensa156f092022-02-28 10:45:43 +0100279static void __init sort_amode31_extable(void)
280{
281 sort_extable(__start_amode31_ex_table, __stop_amode31_ex_table);
282}
283
Heiko Carstensab14de62007-02-05 21:18:37 +0100284void __init startup_init(void)
285{
Vasily Gorbik557b1972023-02-09 22:05:11 +0100286 kasan_early_init();
Vasily Gorbik2e83e0e2019-08-11 20:55:18 +0200287 reset_tod_clock();
Martin Schwidefskyb1c08542016-10-10 09:43:41 +0200288 time_early_init();
Heiko Carstensab14de62007-02-05 21:18:37 +0100289 init_kernel_storage_key();
Heiko Carstensab14de62007-02-05 21:18:37 +0100290 lockdep_off();
Heiko Carstensa156f092022-02-28 10:45:43 +0100291 sort_amode31_extable();
Heiko Carstensab14de62007-02-05 21:18:37 +0100292 setup_lowcore_early();
Martin Schwidefsky14375bc2010-10-25 16:10:51 +0200293 setup_facility_list();
Hendrik Bruecknera0443fb2008-07-14 09:59:09 +0200294 detect_machine_type();
Christian Borntraeger4b8fe772016-05-24 15:23:20 +0200295 setup_arch_string();
Hendrik Bruecknera0443fb2008-07-14 09:59:09 +0200296 setup_boot_command_line();
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200297 detect_diag9c();
Heiko Carstens2e5061e2008-04-30 13:38:45 +0200298 detect_machine_facilities();
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100299 save_vector_registers();
Heiko Carstensfade4dc42012-09-04 14:26:03 +0200300 setup_topology();
Michael Holzheu7b50da52013-11-13 10:38:27 +0100301 sclp_early_detect();
Heiko Carstens4a172522023-09-05 15:49:37 +0200302 setup_low_address_protection();
Vasily Gorbikc2313592019-11-14 14:08:28 +0100303 setup_access_registers();
Heiko Carstensab14de62007-02-05 21:18:37 +0100304 lockdep_on();
305}