xref: /openbmc/linux/arch/s390/kernel/early.c (revision b8bb76713ec50df2f11efee386e16f93d51e1076)
1 /*
2  *  arch/s390/kernel/early.c
3  *
4  *    Copyright IBM Corp. 2007
5  *    Author(s): Hongjie Yang <hongjie@us.ibm.com>,
6  *		 Heiko Carstens <heiko.carstens@de.ibm.com>
7  */
8 
9 #include <linux/compiler.h>
10 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/ctype.h>
14 #include <linux/lockdep.h>
15 #include <linux/module.h>
16 #include <linux/pfn.h>
17 #include <linux/uaccess.h>
18 #include <asm/ebcdic.h>
19 #include <asm/ipl.h>
20 #include <asm/lowcore.h>
21 #include <asm/processor.h>
22 #include <asm/sections.h>
23 #include <asm/setup.h>
24 #include <asm/sysinfo.h>
25 #include <asm/cpcmd.h>
26 #include <asm/sclp.h>
27 #include "entry.h"
28 
29 /*
30  * Create a Kernel NSS if the SAVESYS= parameter is defined
31  */
32 #define DEFSYS_CMD_SIZE		128
33 #define SAVESYS_CMD_SIZE	32
34 
35 char kernel_nss_name[NSS_NAME_SIZE + 1];
36 
37 static void __init setup_boot_command_line(void);
38 
39 
40 #ifdef CONFIG_SHARED_KERNEL
41 int __init savesys_ipl_nss(char *cmd, const int cmdlen);
42 
43 asm(
44 	"	.section .init.text,\"ax\",@progbits\n"
45 	"	.align	4\n"
46 	"	.type	savesys_ipl_nss, @function\n"
47 	"savesys_ipl_nss:\n"
48 #ifdef CONFIG_64BIT
49 	"	stmg	6,15,48(15)\n"
50 	"	lgr	14,3\n"
51 	"	sam31\n"
52 	"	diag	2,14,0x8\n"
53 	"	sam64\n"
54 	"	lgr	2,14\n"
55 	"	lmg	6,15,48(15)\n"
56 #else
57 	"	stm	6,15,24(15)\n"
58 	"	lr	14,3\n"
59 	"	diag	2,14,0x8\n"
60 	"	lr	2,14\n"
61 	"	lm	6,15,24(15)\n"
62 #endif
63 	"	br	14\n"
64 	"	.size	savesys_ipl_nss, .-savesys_ipl_nss\n");
65 
66 static noinline __init void create_kernel_nss(void)
67 {
68 	unsigned int i, stext_pfn, eshared_pfn, end_pfn, min_size;
69 #ifdef CONFIG_BLK_DEV_INITRD
70 	unsigned int sinitrd_pfn, einitrd_pfn;
71 #endif
72 	int response;
73 	size_t len;
74 	char *savesys_ptr;
75 	char upper_command_line[COMMAND_LINE_SIZE];
76 	char defsys_cmd[DEFSYS_CMD_SIZE];
77 	char savesys_cmd[SAVESYS_CMD_SIZE];
78 
79 	/* Do nothing if we are not running under VM */
80 	if (!MACHINE_IS_VM)
81 		return;
82 
83 	/* Convert COMMAND_LINE to upper case */
84 	for (i = 0; i < strlen(boot_command_line); i++)
85 		upper_command_line[i] = toupper(boot_command_line[i]);
86 
87 	savesys_ptr = strstr(upper_command_line, "SAVESYS=");
88 
89 	if (!savesys_ptr)
90 		return;
91 
92 	savesys_ptr += 8;    /* Point to the beginning of the NSS name */
93 	for (i = 0; i < NSS_NAME_SIZE; i++) {
94 		if (savesys_ptr[i] == ' ' || savesys_ptr[i] == '\0')
95 			break;
96 		kernel_nss_name[i] = savesys_ptr[i];
97 	}
98 
99 	stext_pfn = PFN_DOWN(__pa(&_stext));
100 	eshared_pfn = PFN_DOWN(__pa(&_eshared));
101 	end_pfn = PFN_UP(__pa(&_end));
102 	min_size = end_pfn << 2;
103 
104 	sprintf(defsys_cmd, "DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X",
105 		kernel_nss_name, stext_pfn - 1, stext_pfn, eshared_pfn - 1,
106 		eshared_pfn, end_pfn);
107 
108 #ifdef CONFIG_BLK_DEV_INITRD
109 	if (INITRD_START && INITRD_SIZE) {
110 		sinitrd_pfn = PFN_DOWN(__pa(INITRD_START));
111 		einitrd_pfn = PFN_UP(__pa(INITRD_START + INITRD_SIZE));
112 		min_size = einitrd_pfn << 2;
113 		sprintf(defsys_cmd, "%s EW %.5X-%.5X", defsys_cmd,
114 		sinitrd_pfn, einitrd_pfn);
115 	}
116 #endif
117 
118 	sprintf(defsys_cmd, "%s EW MINSIZE=%.7iK PARMREGS=0-13",
119 		defsys_cmd, min_size);
120 	sprintf(savesys_cmd, "SAVESYS %s \n IPL %s",
121 		kernel_nss_name, kernel_nss_name);
122 
123 	__cpcmd(defsys_cmd, NULL, 0, &response);
124 
125 	if (response != 0) {
126 		kernel_nss_name[0] = '\0';
127 		return;
128 	}
129 
130 	len = strlen(savesys_cmd);
131 	ASCEBC(savesys_cmd, len);
132 	response = savesys_ipl_nss(savesys_cmd, len);
133 
134 	/* On success: response is equal to the command size,
135 	 *	       max SAVESYS_CMD_SIZE
136 	 * On error: response contains the numeric portion of cp error message.
137 	 *	     for SAVESYS it will be >= 263
138 	 */
139 	if (response > SAVESYS_CMD_SIZE) {
140 		kernel_nss_name[0] = '\0';
141 		return;
142 	}
143 
144 	/* re-setup boot command line with new ipl vm parms */
145 	ipl_update_parameters();
146 	setup_boot_command_line();
147 
148 	ipl_flags = IPL_NSS_VALID;
149 }
150 
151 #else /* CONFIG_SHARED_KERNEL */
152 
153 static inline void create_kernel_nss(void) { }
154 
155 #endif /* CONFIG_SHARED_KERNEL */
156 
157 /*
158  * Clear bss memory
159  */
160 static noinline __init void clear_bss_section(void)
161 {
162 	memset(__bss_start, 0, __bss_stop - __bss_start);
163 }
164 
165 /*
166  * Initialize storage key for kernel pages
167  */
168 static noinline __init void init_kernel_storage_key(void)
169 {
170 	unsigned long end_pfn, init_pfn;
171 
172 	end_pfn = PFN_UP(__pa(&_end));
173 
174 	for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
175 		page_set_storage_key(init_pfn << PAGE_SHIFT, PAGE_DEFAULT_KEY);
176 }
177 
178 static __initdata struct sysinfo_3_2_2 vmms __aligned(PAGE_SIZE);
179 
180 static noinline __init void detect_machine_type(void)
181 {
182 	/* No VM information? Looks like LPAR */
183 	if (stsi(&vmms, 3, 2, 2) == -ENOSYS)
184 		return;
185 	if (!vmms.count)
186 		return;
187 
188 	/* Running under KVM? If not we assume z/VM */
189 	if (!memcmp(vmms.vm[0].cpi, "\xd2\xe5\xd4", 3))
190 		machine_flags |= MACHINE_FLAG_KVM;
191 	else
192 		machine_flags |= MACHINE_FLAG_VM;
193 }
194 
195 static __init void early_pgm_check_handler(void)
196 {
197 	unsigned long addr;
198 	const struct exception_table_entry *fixup;
199 
200 	addr = S390_lowcore.program_old_psw.addr;
201 	fixup = search_exception_tables(addr & PSW_ADDR_INSN);
202 	if (!fixup)
203 		disabled_wait(0);
204 	S390_lowcore.program_old_psw.addr = fixup->fixup | PSW_ADDR_AMODE;
205 }
206 
207 static noinline __init void setup_lowcore_early(void)
208 {
209 	psw_t psw;
210 
211 	psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
212 	psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_ext_handler;
213 	S390_lowcore.external_new_psw = psw;
214 	psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_pgm_handler;
215 	S390_lowcore.program_new_psw = psw;
216 	s390_base_pgm_handler_fn = early_pgm_check_handler;
217 }
218 
219 static noinline __init void setup_hpage(void)
220 {
221 #ifndef CONFIG_DEBUG_PAGEALLOC
222 	unsigned int facilities;
223 
224 	facilities = stfl();
225 	if (!(facilities & (1UL << 23)) || !(facilities & (1UL << 29)))
226 		return;
227 	machine_flags |= MACHINE_FLAG_HPAGE;
228 	__ctl_set_bit(0, 23);
229 #endif
230 }
231 
232 static __init void detect_mvpg(void)
233 {
234 #ifndef CONFIG_64BIT
235 	int rc;
236 
237 	asm volatile(
238 		"	la	0,0\n"
239 		"	mvpg	%2,%2\n"
240 		"0:	la	%0,0\n"
241 		"1:\n"
242 		EX_TABLE(0b,1b)
243 		: "=d" (rc) : "0" (-EOPNOTSUPP), "a" (0) : "memory", "cc", "0");
244 	if (!rc)
245 		machine_flags |= MACHINE_FLAG_MVPG;
246 #endif
247 }
248 
249 static __init void detect_ieee(void)
250 {
251 #ifndef CONFIG_64BIT
252 	int rc, tmp;
253 
254 	asm volatile(
255 		"	efpc	%1,0\n"
256 		"0:	la	%0,0\n"
257 		"1:\n"
258 		EX_TABLE(0b,1b)
259 		: "=d" (rc), "=d" (tmp): "0" (-EOPNOTSUPP) : "cc");
260 	if (!rc)
261 		machine_flags |= MACHINE_FLAG_IEEE;
262 #endif
263 }
264 
265 static __init void detect_csp(void)
266 {
267 #ifndef CONFIG_64BIT
268 	int rc;
269 
270 	asm volatile(
271 		"	la	0,0\n"
272 		"	la	1,0\n"
273 		"	la	2,4\n"
274 		"	csp	0,2\n"
275 		"0:	la	%0,0\n"
276 		"1:\n"
277 		EX_TABLE(0b,1b)
278 		: "=d" (rc) : "0" (-EOPNOTSUPP) : "cc", "0", "1", "2");
279 	if (!rc)
280 		machine_flags |= MACHINE_FLAG_CSP;
281 #endif
282 }
283 
284 static __init void detect_diag9c(void)
285 {
286 	unsigned int cpu_address;
287 	int rc;
288 
289 	cpu_address = stap();
290 	asm volatile(
291 		"	diag	%2,0,0x9c\n"
292 		"0:	la	%0,0\n"
293 		"1:\n"
294 		EX_TABLE(0b,1b)
295 		: "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
296 	if (!rc)
297 		machine_flags |= MACHINE_FLAG_DIAG9C;
298 }
299 
300 static __init void detect_diag44(void)
301 {
302 #ifdef CONFIG_64BIT
303 	int rc;
304 
305 	asm volatile(
306 		"	diag	0,0,0x44\n"
307 		"0:	la	%0,0\n"
308 		"1:\n"
309 		EX_TABLE(0b,1b)
310 		: "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
311 	if (!rc)
312 		machine_flags |= MACHINE_FLAG_DIAG44;
313 #endif
314 }
315 
316 static __init void detect_machine_facilities(void)
317 {
318 #ifdef CONFIG_64BIT
319 	unsigned int facilities;
320 
321 	facilities = stfl();
322 	if (facilities & (1 << 28))
323 		machine_flags |= MACHINE_FLAG_IDTE;
324 	if (facilities & (1 << 23))
325 		machine_flags |= MACHINE_FLAG_PFMF;
326 	if (facilities & (1 << 4))
327 		machine_flags |= MACHINE_FLAG_MVCOS;
328 #endif
329 }
330 
331 static __init void rescue_initrd(void)
332 {
333 #ifdef CONFIG_BLK_DEV_INITRD
334 	/*
335 	 * Move the initrd right behind the bss section in case it starts
336 	 * within the bss section. So we don't overwrite it when the bss
337 	 * section gets cleared.
338 	 */
339 	if (!INITRD_START || !INITRD_SIZE)
340 		return;
341 	if (INITRD_START >= (unsigned long) __bss_stop)
342 		return;
343 	memmove(__bss_stop, (void *) INITRD_START, INITRD_SIZE);
344 	INITRD_START = (unsigned long) __bss_stop;
345 #endif
346 }
347 
348 /* Set up boot command line */
349 static void __init setup_boot_command_line(void)
350 {
351 	char *parm = NULL;
352 
353 	/* copy arch command line */
354 	strlcpy(boot_command_line, COMMAND_LINE, ARCH_COMMAND_LINE_SIZE);
355 
356 	/* append IPL PARM data to the boot command line */
357 	if (MACHINE_IS_VM) {
358 		parm = boot_command_line + strlen(boot_command_line);
359 		*parm++ = ' ';
360 		get_ipl_vmparm(parm);
361 		if (parm[0] == '=')
362 			memmove(boot_command_line, parm + 1, strlen(parm));
363 	}
364 }
365 
366 
367 /*
368  * Save ipl parameters, clear bss memory, initialize storage keys
369  * and create a kernel NSS at startup if the SAVESYS= parm is defined
370  */
371 void __init startup_init(void)
372 {
373 	ipl_save_parameters();
374 	rescue_initrd();
375 	clear_bss_section();
376 	init_kernel_storage_key();
377 	lockdep_init();
378 	lockdep_off();
379 	sort_main_extable();
380 	setup_lowcore_early();
381 	detect_machine_type();
382 	ipl_update_parameters();
383 	setup_boot_command_line();
384 	create_kernel_nss();
385 	detect_mvpg();
386 	detect_ieee();
387 	detect_csp();
388 	detect_diag9c();
389 	detect_diag44();
390 	detect_machine_facilities();
391 	setup_hpage();
392 	sclp_facilities_detect();
393 	detect_memory_layout(memory_chunk);
394 	lockdep_on();
395 }
396