early.c 8.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
 *    Copyright IBM Corp. 2007, 2009
4 5 6 7
 *    Author(s): Hongjie Yang <hongjie@us.ibm.com>,
 *		 Heiko Carstens <heiko.carstens@de.ibm.com>
 */

8 9 10
#define KMSG_COMPONENT "setup"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

11
#include <linux/compiler.h>
12 13 14 15 16
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/lockdep.h>
17
#include <linux/extable.h>
18 19
#include <linux/pfn.h>
#include <linux/uaccess.h>
20
#include <linux/kernel.h>
21
#include <asm/diag.h>
22
#include <asm/ebcdic.h>
M
Michael Holzheu 已提交
23
#include <asm/ipl.h>
24 25 26 27
#include <asm/lowcore.h>
#include <asm/processor.h>
#include <asm/sections.h>
#include <asm/setup.h>
28
#include <asm/sysinfo.h>
29 30
#include <asm/cpcmd.h>
#include <asm/sclp.h>
31
#include <asm/facility.h>
32
#include <asm/boot_data.h>
33
#include <asm/pci_insn.h>
34
#include "entry.h"
35 36 37 38 39 40

/*
 * Initialize storage key for kernel pages
 */
static noinline __init void init_kernel_storage_key(void)
{
41
#if PAGE_DEFAULT_KEY
42 43
	unsigned long end_pfn, init_pfn;

V
Vasily Gorbik 已提交
44
	end_pfn = PFN_UP(__pa(_end));
45 46

	for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
47 48
		page_set_storage_key(init_pfn << PAGE_SHIFT,
				     PAGE_DEFAULT_KEY, 0);
49
#endif
50 51
}

52
static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
53

54 55
static noinline __init void detect_machine_type(void)
{
56 57
	struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;

58
	/* Check current-configuration-level */
59
	if (stsi(NULL, 0, 0, 0) <= 2) {
60
		S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
61
		return;
62 63
	}
	/* Get virtual-machine cpu information. */
64
	if (stsi(vmms, 3, 2, 2) || !vmms->count)
65
		return;
66

67
	/* Detect known hypervisors */
68
	if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3))
69
		S390_lowcore.machine_flags |= MACHINE_FLAG_KVM;
70
	else if (!memcmp(vmms->vm[0].cpi, "\xa9\x61\xe5\xd4", 4))
71
		S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
72 73
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/* Remove leading, trailing and double whitespace. */
static inline void strim_all(char *str)
{
	char *s;

	s = strim(str);
	if (s != str)
		memmove(str, s, strlen(s));
	while (*str) {
		if (!isspace(*str++))
			continue;
		if (isspace(*str)) {
			s = skip_spaces(str);
			memmove(str, s, strlen(s) + 1);
		}
	}
}

92 93 94
static noinline __init void setup_arch_string(void)
{
	struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page;
95 96
	struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page;
	char mstr[80], hvstr[17];
97 98 99 100 101 102 103

	if (stsi(mach, 1, 1, 1))
		return;
	EBCASC(mach->manufacturer, sizeof(mach->manufacturer));
	EBCASC(mach->type, sizeof(mach->type));
	EBCASC(mach->model, sizeof(mach->model));
	EBCASC(mach->model_capacity, sizeof(mach->model_capacity));
104 105 106 107
	sprintf(mstr, "%-16.16s %-4.4s %-16.16s %-16.16s",
		mach->manufacturer, mach->type,
		mach->model, mach->model_capacity);
	strim_all(mstr);
108 109 110 111 112 113 114 115 116 117 118
	if (stsi(vm, 3, 2, 2) == 0 && vm->count) {
		EBCASC(vm->vm[0].cpi, sizeof(vm->vm[0].cpi));
		sprintf(hvstr, "%-16.16s", vm->vm[0].cpi);
		strim_all(hvstr);
	} else {
		sprintf(hvstr, "%s",
			MACHINE_IS_LPAR ? "LPAR" :
			MACHINE_IS_VM ? "z/VM" :
			MACHINE_IS_KVM ? "KVM" : "unknown");
	}
	dump_stack_set_arch_desc("%s (%s)", mstr, hvstr);
119 120
}

121 122 123 124 125 126 127 128
static __init void setup_topology(void)
{
	int max_mnest;

	if (!test_facility(11))
		return;
	S390_lowcore.machine_flags |= MACHINE_FLAG_TOPOLOGY;
	for (max_mnest = 6; max_mnest > 1; max_mnest--) {
129
		if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0)
130 131 132 133 134
			break;
	}
	topology_max_mnest = max_mnest;
}

135
static void early_pgm_check_handler(void)
136 137
{
	const struct exception_table_entry *fixup;
138
	unsigned long cr0, cr0_new;
139
	unsigned long addr;
140 141

	addr = S390_lowcore.program_old_psw.addr;
142
	fixup = s390_search_extables(addr);
143
	if (!fixup)
144
		disabled_wait();
145 146 147 148
	/* Disable low address protection before storing into lowcore. */
	__ctl_store(cr0, 0, 0);
	cr0_new = cr0 & ~(1UL << 28);
	__ctl_load(cr0_new, 0, 0);
149
	S390_lowcore.program_old_psw.addr = extable_fixup(fixup);
150
	__ctl_load(cr0, 0, 0);
151 152
}

153
static noinline __init void setup_lowcore_early(void)
154 155 156
{
	psw_t psw;

157
	psw.mask = PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA;
158
	psw.addr = (unsigned long) s390_base_ext_handler;
159
	S390_lowcore.external_new_psw = psw;
160
	psw.addr = (unsigned long) s390_base_pgm_handler;
161 162
	S390_lowcore.program_new_psw = psw;
	s390_base_pgm_handler_fn = early_pgm_check_handler;
163
	S390_lowcore.preempt_count = INIT_PREEMPT_COUNT;
164 165
}

166 167
static noinline __init void setup_facility_list(void)
{
168 169 170
	memcpy(S390_lowcore.alt_stfle_fac_list,
	       S390_lowcore.stfle_fac_list,
	       sizeof(S390_lowcore.alt_stfle_fac_list));
171 172
	if (!IS_ENABLED(CONFIG_KERNEL_NOBP))
		__clear_facility(82, S390_lowcore.alt_stfle_fac_list);
173 174
}

175 176 177 178 179 180
static __init void detect_diag9c(void)
{
	unsigned int cpu_address;
	int rc;

	cpu_address = stap();
181
	diag_stat_inc(DIAG_STAT_X09C);
182 183 184 185 186 187 188
	asm volatile(
		"	diag	%2,0,0x9c\n"
		"0:	la	%0,0\n"
		"1:\n"
		EX_TABLE(0b,1b)
		: "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
	if (!rc)
189
		S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
190 191 192 193 194 195
}

static __init void detect_diag44(void)
{
	int rc;

196
	diag_stat_inc(DIAG_STAT_X044);
197 198 199 200 201 202 203
	asm volatile(
		"	diag	0,0,0x44\n"
		"0:	la	%0,0\n"
		"1:\n"
		EX_TABLE(0b,1b)
		: "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
	if (!rc)
204
		S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44;
205 206 207 208
}

static __init void detect_machine_facilities(void)
{
209 210 211 212
	if (test_facility(8)) {
		S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
		__ctl_set_bit(0, 23);
	}
213 214
	if (test_facility(78))
		S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2;
215
	if (test_facility(3))
216
		S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
217
	if (test_facility(50) && test_facility(73)) {
218
		S390_lowcore.machine_flags |= MACHINE_FLAG_TE;
219 220
		__ctl_set_bit(0, 55);
	}
221 222
	if (test_facility(51))
		S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC;
223
	if (test_facility(129)) {
224
		S390_lowcore.machine_flags |= MACHINE_FLAG_VX;
225 226
		__ctl_set_bit(0, 17);
	}
227 228 229 230
	if (test_facility(130)) {
		S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
		__ctl_set_bit(0, 20);
	}
231 232
	if (test_facility(133))
		S390_lowcore.machine_flags |= MACHINE_FLAG_GS;
233 234 235 236 237 238
	if (test_facility(139) && (tod_clock_base[1] & 0x80)) {
		/* Enabled signed clock comparator comparisons */
		S390_lowcore.machine_flags |= MACHINE_FLAG_SCC;
		clock_comparator_max = -1ULL >> 1;
		__ctl_set_bit(0, 53);
	}
239
	enable_mio_ctl();
240 241
}

242 243 244 245 246 247 248 249
static inline void save_vector_registers(void)
{
#ifdef CONFIG_CRASH_DUMP
	if (test_facility(129))
		save_vx_regs(boot_cpu_vector_save_area);
#endif
}

250 251 252 253
static int __init disable_vector_extension(char *str)
{
	S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
	__ctl_clear_bit(0, 17);
254
	return 0;
255
}
256
early_param("novx", disable_vector_extension);
257

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
static int __init noexec_setup(char *str)
{
	bool enabled;
	int rc;

	rc = kstrtobool(str, &enabled);
	if (!rc && !enabled) {
		/* Disable no-execute support */
		S390_lowcore.machine_flags &= ~MACHINE_FLAG_NX;
		__ctl_clear_bit(0, 20);
	}
	return rc;
}
early_param("noexec", noexec_setup);

273
static int __init cad_setup(char *str)
274
{
275 276
	bool enabled;
	int rc;
277

278 279
	rc = kstrtobool(str, &enabled);
	if (!rc && enabled && test_facility(128))
280 281
		/* Enable problem state CAD. */
		__ctl_set_bit(2, 3);
282
	return rc;
283
}
284
early_param("cad", cad_setup);
285

286
char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
287 288
static void __init setup_boot_command_line(void)
{
289
	/* copy arch command line */
290
	strlcpy(boot_command_line, early_command_line, ARCH_COMMAND_LINE_SIZE);
291 292
}

293 294 295 296 297 298 299 300
static void __init check_image_bootable(void)
{
	if (!memcmp(EP_STRING, (void *)EP_OFFSET, strlen(EP_STRING)))
		return;

	sclp_early_printk("Linux kernel boot failure: An attempt to boot a vmlinux ELF image failed.\n");
	sclp_early_printk("This image does not contain all parts necessary for starting up. Use\n");
	sclp_early_printk("bzImage or arch/s390/boot/compressed/vmlinux instead.\n");
301
	disabled_wait();
302 303
}

304 305
void __init startup_init(void)
{
306
	check_image_bootable();
307
	time_early_init();
308 309 310
	init_kernel_storage_key();
	lockdep_off();
	setup_lowcore_early();
311
	setup_facility_list();
312
	detect_machine_type();
313
	setup_arch_string();
314
	setup_boot_command_line();
315 316 317
	detect_diag9c();
	detect_diag44();
	detect_machine_facilities();
318
	save_vector_registers();
319
	setup_topology();
320
	sclp_early_detect();
321 322
	lockdep_on();
}