Merge branch 'fix/rt5645' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[deliverable/linux.git] / arch / s390 / kernel / early.c
CommitLineData
ab14de6c 1/*
155af2f9 2 * Copyright IBM Corp. 2007, 2009
ab14de6c
HC
3 * Author(s): Hongjie Yang <hongjie@us.ibm.com>,
4 * Heiko Carstens <heiko.carstens@de.ibm.com>
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>
16#include <linux/module.h>
17#include <linux/pfn.h>
18#include <linux/uaccess.h>
11af97e1 19#include <linux/kernel.h>
a0443fbb 20#include <asm/ebcdic.h>
46b05d26 21#include <asm/ipl.h>
ab14de6c
HC
22#include <asm/lowcore.h>
23#include <asm/processor.h>
24#include <asm/sections.h>
25#include <asm/setup.h>
92e6ecf3 26#include <asm/sysinfo.h>
ab14de6c
HC
27#include <asm/cpcmd.h>
28#include <asm/sclp.h>
a0616cde 29#include <asm/facility.h>
a806170e 30#include "entry.h"
ab14de6c
HC
31
32/*
33 * Create a Kernel NSS if the SAVESYS= parameter is defined
34 */
a0443fbb 35#define DEFSYS_CMD_SIZE 128
ab14de6c
HC
36#define SAVESYS_CMD_SIZE 32
37
ab14de6c
HC
38char kernel_nss_name[NSS_NAME_SIZE + 1];
39
a0443fbb
HB
40static void __init setup_boot_command_line(void);
41
b6112ccb
MS
42/*
43 * Get the TOD clock running.
44 */
45static void __init reset_tod_clock(void)
46{
47 u64 time;
48
1aae0560 49 if (store_tod_clock(&time) == 0)
b6112ccb
MS
50 return;
51 /* TOD clock not running. Set the clock to Unix Epoch. */
1aae0560 52 if (set_tod_clock(TOD_UNIX_EPOCH) != 0 || store_tod_clock(&time) != 0)
b6112ccb
MS
53 disabled_wait(0);
54
55 sched_clock_base_cc = TOD_UNIX_EPOCH;
b89031e0 56 S390_lowcore.last_update_clock = sched_clock_base_cc;
b6112ccb 57}
a0443fbb 58
ab14de6c 59#ifdef CONFIG_SHARED_KERNEL
a0443fbb
HB
60int __init savesys_ipl_nss(char *cmd, const int cmdlen);
61
62asm(
63 " .section .init.text,\"ax\",@progbits\n"
64 " .align 4\n"
65 " .type savesys_ipl_nss, @function\n"
66 "savesys_ipl_nss:\n"
a0443fbb
HB
67 " stmg 6,15,48(15)\n"
68 " lgr 14,3\n"
69 " sam31\n"
70 " diag 2,14,0x8\n"
71 " sam64\n"
72 " lgr 2,14\n"
73 " lmg 6,15,48(15)\n"
a0443fbb 74 " br 14\n"
18764463
HC
75 " .size savesys_ipl_nss, .-savesys_ipl_nss\n"
76 " .previous\n");
a0443fbb 77
684d2fd4
HB
78static __initdata char upper_command_line[COMMAND_LINE_SIZE];
79
ab14de6c
HC
80static noinline __init void create_kernel_nss(void)
81{
82 unsigned int i, stext_pfn, eshared_pfn, end_pfn, min_size;
83#ifdef CONFIG_BLK_DEV_INITRD
84 unsigned int sinitrd_pfn, einitrd_pfn;
85#endif
86 int response;
69ac43b0 87 int hlen;
a0443fbb 88 size_t len;
ab14de6c 89 char *savesys_ptr;
ab14de6c
HC
90 char defsys_cmd[DEFSYS_CMD_SIZE];
91 char savesys_cmd[SAVESYS_CMD_SIZE];
92
93 /* Do nothing if we are not running under VM */
94 if (!MACHINE_IS_VM)
95 return;
96
97 /* Convert COMMAND_LINE to upper case */
a0443fbb
HB
98 for (i = 0; i < strlen(boot_command_line); i++)
99 upper_command_line[i] = toupper(boot_command_line[i]);
ab14de6c
HC
100
101 savesys_ptr = strstr(upper_command_line, "SAVESYS=");
102
103 if (!savesys_ptr)
104 return;
105
106 savesys_ptr += 8; /* Point to the beginning of the NSS name */
107 for (i = 0; i < NSS_NAME_SIZE; i++) {
108 if (savesys_ptr[i] == ' ' || savesys_ptr[i] == '\0')
109 break;
110 kernel_nss_name[i] = savesys_ptr[i];
111 }
112
113 stext_pfn = PFN_DOWN(__pa(&_stext));
114 eshared_pfn = PFN_DOWN(__pa(&_eshared));
115 end_pfn = PFN_UP(__pa(&_end));
116 min_size = end_pfn << 2;
117
69ac43b0
CL
118 hlen = snprintf(defsys_cmd, DEFSYS_CMD_SIZE,
119 "DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X",
120 kernel_nss_name, stext_pfn - 1, stext_pfn,
121 eshared_pfn - 1, eshared_pfn, end_pfn);
ab14de6c
HC
122
123#ifdef CONFIG_BLK_DEV_INITRD
124 if (INITRD_START && INITRD_SIZE) {
125 sinitrd_pfn = PFN_DOWN(__pa(INITRD_START));
126 einitrd_pfn = PFN_UP(__pa(INITRD_START + INITRD_SIZE));
127 min_size = einitrd_pfn << 2;
69ac43b0
CL
128 hlen += snprintf(defsys_cmd + hlen, DEFSYS_CMD_SIZE - hlen,
129 " EW %.5X-%.5X", sinitrd_pfn, einitrd_pfn);
ab14de6c
HC
130 }
131#endif
132
69ac43b0
CL
133 snprintf(defsys_cmd + hlen, DEFSYS_CMD_SIZE - hlen,
134 " EW MINSIZE=%.7iK PARMREGS=0-13", min_size);
135 defsys_cmd[DEFSYS_CMD_SIZE - 1] = '\0';
894e491e
HC
136 snprintf(savesys_cmd, SAVESYS_CMD_SIZE, "SAVESYS %s \n IPL %s",
137 kernel_nss_name, kernel_nss_name);
138 savesys_cmd[SAVESYS_CMD_SIZE - 1] = '\0';
ab14de6c
HC
139
140 __cpcmd(defsys_cmd, NULL, 0, &response);
141
583b33bc 142 if (response != 0) {
11af97e1
HB
143 pr_err("Defining the Linux kernel NSS failed with rc=%d\n",
144 response);
583b33bc 145 kernel_nss_name[0] = '\0';
ab14de6c 146 return;
583b33bc 147 }
ab14de6c 148
a0443fbb
HB
149 len = strlen(savesys_cmd);
150 ASCEBC(savesys_cmd, len);
151 response = savesys_ipl_nss(savesys_cmd, len);
ab14de6c 152
a0443fbb
HB
153 /* On success: response is equal to the command size,
154 * max SAVESYS_CMD_SIZE
155 * On error: response contains the numeric portion of cp error message.
156 * for SAVESYS it will be >= 263
11af97e1 157 * for missing privilege class, it will be 1
a0443fbb 158 */
11af97e1
HB
159 if (response > SAVESYS_CMD_SIZE || response == 1) {
160 pr_err("Saving the Linux kernel NSS failed with rc=%d\n",
161 response);
583b33bc 162 kernel_nss_name[0] = '\0';
ab14de6c 163 return;
583b33bc 164 }
ab14de6c 165
b89031e0 166 /* re-initialize cputime accounting. */
1aae0560 167 sched_clock_base_cc = get_tod_clock();
b89031e0
MS
168 S390_lowcore.last_update_clock = sched_clock_base_cc;
169 S390_lowcore.last_update_timer = 0x7fffffffffffffffULL;
170 S390_lowcore.user_timer = 0;
171 S390_lowcore.system_timer = 0;
172 asm volatile("SPT 0(%0)" : : "a" (&S390_lowcore.last_update_timer));
173
a0443fbb
HB
174 /* re-setup boot command line with new ipl vm parms */
175 ipl_update_parameters();
176 setup_boot_command_line();
177
ab14de6c
HC
178 ipl_flags = IPL_NSS_VALID;
179}
180
181#else /* CONFIG_SHARED_KERNEL */
182
183static inline void create_kernel_nss(void) { }
184
185#endif /* CONFIG_SHARED_KERNEL */
186
187/*
188 * Clear bss memory
189 */
190static noinline __init void clear_bss_section(void)
191{
229d9c6d 192 memset(__bss_start, 0, __bss_stop - __bss_start);
ab14de6c
HC
193}
194
195/*
196 * Initialize storage key for kernel pages
197 */
198static noinline __init void init_kernel_storage_key(void)
199{
127c1fef 200#if PAGE_DEFAULT_KEY
ab14de6c
HC
201 unsigned long end_pfn, init_pfn;
202
203 end_pfn = PFN_UP(__pa(&_end));
204
205 for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
e2b8d7af
MS
206 page_set_storage_key(init_pfn << PAGE_SHIFT,
207 PAGE_DEFAULT_KEY, 0);
127c1fef 208#endif
ab14de6c
HC
209}
210
fade4dc4 211static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
92e6ecf3 212
ab14de6c
HC
213static noinline __init void detect_machine_type(void)
214{
fade4dc4
HC
215 struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;
216
27d71602 217 /* Check current-configuration-level */
caf757c6 218 if (stsi(NULL, 0, 0, 0) <= 2) {
27d71602 219 S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
92e6ecf3 220 return;
27d71602
MS
221 }
222 /* Get virtual-machine cpu information. */
caf757c6 223 if (stsi(vmms, 3, 2, 2) || !vmms->count)
92e6ecf3 224 return;
ab14de6c 225
92e6ecf3 226 /* Running under KVM? If not we assume z/VM */
fade4dc4 227 if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3))
d3135e0c 228 S390_lowcore.machine_flags |= MACHINE_FLAG_KVM;
92e6ecf3 229 else
d3135e0c 230 S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
ab14de6c
HC
231}
232
fade4dc4
HC
233static __init void setup_topology(void)
234{
fade4dc4
HC
235 int max_mnest;
236
237 if (!test_facility(11))
238 return;
239 S390_lowcore.machine_flags |= MACHINE_FLAG_TOPOLOGY;
240 for (max_mnest = 6; max_mnest > 1; max_mnest--) {
caf757c6 241 if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0)
fade4dc4
HC
242 break;
243 }
244 topology_max_mnest = max_mnest;
fade4dc4
HC
245}
246
a3415703 247static void early_pgm_check_handler(void)
ab14de6c 248{
ab14de6c 249 const struct exception_table_entry *fixup;
50be6345 250 unsigned long cr0, cr0_new;
eb608fb3 251 unsigned long addr;
ab14de6c
HC
252
253 addr = S390_lowcore.program_old_psw.addr;
254 fixup = search_exception_tables(addr & PSW_ADDR_INSN);
255 if (!fixup)
256 disabled_wait(0);
50be6345
PH
257 /* Disable low address protection before storing into lowcore. */
258 __ctl_store(cr0, 0, 0);
259 cr0_new = cr0 & ~(1UL << 28);
260 __ctl_load(cr0_new, 0, 0);
eb608fb3 261 S390_lowcore.program_old_psw.addr = extable_fixup(fixup)|PSW_ADDR_AMODE;
50be6345 262 __ctl_load(cr0, 0, 0);
ab14de6c
HC
263}
264
5f954c34 265static noinline __init void setup_lowcore_early(void)
ab14de6c
HC
266{
267 psw_t psw;
268
b50511e4 269 psw.mask = PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA;
ab14de6c
HC
270 psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_ext_handler;
271 S390_lowcore.external_new_psw = psw;
272 psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_pgm_handler;
273 S390_lowcore.program_new_psw = psw;
274 s390_base_pgm_handler_fn = early_pgm_check_handler;
275}
276
14375bc4
MS
277static noinline __init void setup_facility_list(void)
278{
3ab121ab
MH
279 stfle(S390_lowcore.stfle_fac_list,
280 ARRAY_SIZE(S390_lowcore.stfle_fac_list));
14375bc4
MS
281}
282
2e5061e4
HC
283static __init void detect_diag9c(void)
284{
285 unsigned int cpu_address;
286 int rc;
287
288 cpu_address = stap();
289 asm volatile(
290 " diag %2,0,0x9c\n"
291 "0: la %0,0\n"
292 "1:\n"
293 EX_TABLE(0b,1b)
294 : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
295 if (!rc)
d3135e0c 296 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
2e5061e4
HC
297}
298
299static __init void detect_diag44(void)
300{
2e5061e4
HC
301 int rc;
302
303 asm volatile(
304 " diag 0,0,0x44\n"
305 "0: la %0,0\n"
306 "1:\n"
307 EX_TABLE(0b,1b)
308 : "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
309 if (!rc)
d3135e0c 310 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44;
2e5061e4
HC
311}
312
313static __init void detect_machine_facilities(void)
314{
3c7ef08b
HC
315 if (test_facility(8)) {
316 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
317 __ctl_set_bit(0, 23);
318 }
85e9d0e5
HC
319 if (test_facility(78))
320 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2;
14375bc4 321 if (test_facility(3))
d3135e0c 322 S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
14375bc4 323 if (test_facility(40))
23d18e8d 324 S390_lowcore.machine_flags |= MACHINE_FLAG_LPP;
d35339a4
MS
325 if (test_facility(50) && test_facility(73))
326 S390_lowcore.machine_flags |= MACHINE_FLAG_TE;
1b948d6c
MS
327 if (test_facility(51))
328 S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC;
80703617
MS
329 if (test_facility(129))
330 S390_lowcore.machine_flags |= MACHINE_FLAG_VX;
2e5061e4
HC
331}
332
61b0b016 333static int __init cad_setup(char *str)
2c72a44e 334{
61b0b016
MS
335 int val;
336
337 get_option(&str, &val);
338 if (val && test_facility(128))
339 S390_lowcore.machine_flags |= MACHINE_FLAG_CAD;
2c72a44e
MS
340 return 0;
341}
61b0b016 342early_param("cad", cad_setup);
2c72a44e
MS
343
344static int __init cad_init(void)
345{
346 if (MACHINE_HAS_CAD)
347 /* Enable problem state CAD. */
348 __ctl_set_bit(2, 3);
349 return 0;
350}
351early_initcall(cad_init);
352
761cdf6a
HC
353static __init void rescue_initrd(void)
354{
355#ifdef CONFIG_BLK_DEV_INITRD
27e7318c 356 unsigned long min_initrd_addr = (unsigned long) _end + (4UL << 20);
761cdf6a 357 /*
27e7318c
HC
358 * Just like in case of IPL from VM reader we make sure there is a
359 * gap of 4MB between end of kernel and start of initrd.
360 * That way we can also be sure that saving an NSS will succeed,
361 * which however only requires different segments.
761cdf6a
HC
362 */
363 if (!INITRD_START || !INITRD_SIZE)
364 return;
27e7318c 365 if (INITRD_START >= min_initrd_addr)
761cdf6a 366 return;
27e7318c
HC
367 memmove((void *) min_initrd_addr, (void *) INITRD_START, INITRD_SIZE);
368 INITRD_START = min_initrd_addr;
761cdf6a
HC
369#endif
370}
371
a0443fbb 372/* Set up boot command line */
684d2fd4 373static void __init append_to_cmdline(size_t (*ipl_data)(char *, size_t))
a0443fbb 374{
684d2fd4
HB
375 char *parm, *delim;
376 size_t rc, len;
377
378 len = strlen(boot_command_line);
a0443fbb 379
684d2fd4
HB
380 delim = boot_command_line + len; /* '\0' character position */
381 parm = boot_command_line + len + 1; /* append right after '\0' */
382
383 rc = ipl_data(parm, COMMAND_LINE_SIZE - len - 1);
384 if (rc) {
385 if (*parm == '=')
386 memmove(boot_command_line, parm + 1, rc);
387 else
388 *delim = ' '; /* replace '\0' with space */
389 }
390}
391
1fb81057 392static inline int has_ebcdic_char(const char *str)
684d2fd4 393{
61fd330d
MS
394 int i;
395
1fb81057
MH
396 for (i = 0; str[i]; i++)
397 if (str[i] & 0x80)
398 return 1;
399 return 0;
400}
61fd330d 401
1fb81057
MH
402static void __init setup_boot_command_line(void)
403{
404 COMMAND_LINE[ARCH_COMMAND_LINE_SIZE - 1] = 0;
405 /* convert arch command line to ascii if necessary */
406 if (has_ebcdic_char(COMMAND_LINE))
407 EBCASC(COMMAND_LINE, ARCH_COMMAND_LINE_SIZE);
a0443fbb 408 /* copy arch command line */
61fd330d
MS
409 strlcpy(boot_command_line, strstrip(COMMAND_LINE),
410 ARCH_COMMAND_LINE_SIZE);
a0443fbb
HB
411
412 /* append IPL PARM data to the boot command line */
684d2fd4
HB
413 if (MACHINE_IS_VM)
414 append_to_cmdline(append_ipl_vmparm);
415
416 append_to_cmdline(append_ipl_scpdata);
a0443fbb
HB
417}
418
ab14de6c
HC
419/*
420 * Save ipl parameters, clear bss memory, initialize storage keys
421 * and create a kernel NSS at startup if the SAVESYS= parm is defined
422 */
423void __init startup_init(void)
424{
b6112ccb 425 reset_tod_clock();
ab14de6c 426 ipl_save_parameters();
761cdf6a 427 rescue_initrd();
ab14de6c
HC
428 clear_bss_section();
429 init_kernel_storage_key();
430 lockdep_init();
431 lockdep_off();
ab14de6c 432 setup_lowcore_early();
14375bc4 433 setup_facility_list();
a0443fbb
HB
434 detect_machine_type();
435 ipl_update_parameters();
436 setup_boot_command_line();
437 create_kernel_nss();
2e5061e4
HC
438 detect_diag9c();
439 detect_diag44();
440 detect_machine_facilities();
fade4dc4 441 setup_topology();
7b50da53 442 sclp_early_detect();
ab14de6c
HC
443 lockdep_on();
444}
This page took 0.835685 seconds and 5 git commands to generate.