1 /* 2 * Copyright IBM Corp. 2007, 2009 3 * Author(s): Hongjie Yang <hongjie@us.ibm.com>, 4 * Heiko Carstens <heiko.carstens@de.ibm.com> 5 */ 6 7 #define KMSG_COMPONENT "setup" 8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 9 10 #include <linux/compiler.h> 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> 19 #include <linux/kernel.h> 20 #include <asm/ebcdic.h> 21 #include <asm/ipl.h> 22 #include <asm/lowcore.h> 23 #include <asm/processor.h> 24 #include <asm/sections.h> 25 #include <asm/setup.h> 26 #include <asm/sysinfo.h> 27 #include <asm/cpcmd.h> 28 #include <asm/sclp.h> 29 #include <asm/facility.h> 30 #include "entry.h" 31 32 /* 33 * Create a Kernel NSS if the SAVESYS= parameter is defined 34 */ 35 #define DEFSYS_CMD_SIZE 128 36 #define SAVESYS_CMD_SIZE 32 37 38 char kernel_nss_name[NSS_NAME_SIZE + 1]; 39 40 static void __init setup_boot_command_line(void); 41 42 /* 43 * Get the TOD clock running. 44 */ 45 static void __init reset_tod_clock(void) 46 { 47 u64 time; 48 49 if (store_tod_clock(&time) == 0) 50 return; 51 /* TOD clock not running. Set the clock to Unix Epoch. */ 52 if (set_tod_clock(TOD_UNIX_EPOCH) != 0 || store_tod_clock(&time) != 0) 53 disabled_wait(0); 54 55 sched_clock_base_cc = TOD_UNIX_EPOCH; 56 S390_lowcore.last_update_clock = sched_clock_base_cc; 57 } 58 59 #ifdef CONFIG_SHARED_KERNEL 60 int __init savesys_ipl_nss(char *cmd, const int cmdlen); 61 62 asm( 63 " .section .init.text,\"ax\",@progbits\n" 64 " .align 4\n" 65 " .type savesys_ipl_nss, @function\n" 66 "savesys_ipl_nss:\n" 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" 74 " br 14\n" 75 " .size savesys_ipl_nss, .-savesys_ipl_nss\n" 76 " .previous\n"); 77 78 static __initdata char upper_command_line[COMMAND_LINE_SIZE]; 79 80 static 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; 87 int hlen; 88 size_t len; 89 char *savesys_ptr; 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 */ 98 for (i = 0; i < strlen(boot_command_line); i++) 99 upper_command_line[i] = toupper(boot_command_line[i]); 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 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); 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; 128 hlen += snprintf(defsys_cmd + hlen, DEFSYS_CMD_SIZE - hlen, 129 " EW %.5X-%.5X", sinitrd_pfn, einitrd_pfn); 130 } 131 #endif 132 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'; 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'; 139 140 __cpcmd(defsys_cmd, NULL, 0, &response); 141 142 if (response != 0) { 143 pr_err("Defining the Linux kernel NSS failed with rc=%d\n", 144 response); 145 kernel_nss_name[0] = '\0'; 146 return; 147 } 148 149 len = strlen(savesys_cmd); 150 ASCEBC(savesys_cmd, len); 151 response = savesys_ipl_nss(savesys_cmd, len); 152 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 157 * for missing privilege class, it will be 1 158 */ 159 if (response > SAVESYS_CMD_SIZE || response == 1) { 160 pr_err("Saving the Linux kernel NSS failed with rc=%d\n", 161 response); 162 kernel_nss_name[0] = '\0'; 163 return; 164 } 165 166 /* re-initialize cputime accounting. */ 167 sched_clock_base_cc = get_tod_clock(); 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 174 /* re-setup boot command line with new ipl vm parms */ 175 ipl_update_parameters(); 176 setup_boot_command_line(); 177 178 ipl_flags = IPL_NSS_VALID; 179 } 180 181 #else /* CONFIG_SHARED_KERNEL */ 182 183 static inline void create_kernel_nss(void) { } 184 185 #endif /* CONFIG_SHARED_KERNEL */ 186 187 /* 188 * Clear bss memory 189 */ 190 static noinline __init void clear_bss_section(void) 191 { 192 memset(__bss_start, 0, __bss_stop - __bss_start); 193 } 194 195 /* 196 * Initialize storage key for kernel pages 197 */ 198 static noinline __init void init_kernel_storage_key(void) 199 { 200 #if PAGE_DEFAULT_KEY 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++) 206 page_set_storage_key(init_pfn << PAGE_SHIFT, 207 PAGE_DEFAULT_KEY, 0); 208 #endif 209 } 210 211 static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE); 212 213 static noinline __init void detect_machine_type(void) 214 { 215 struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page; 216 217 /* Check current-configuration-level */ 218 if (stsi(NULL, 0, 0, 0) <= 2) { 219 S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR; 220 return; 221 } 222 /* Get virtual-machine cpu information. */ 223 if (stsi(vmms, 3, 2, 2) || !vmms->count) 224 return; 225 226 /* Running under KVM? If not we assume z/VM */ 227 if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3)) 228 S390_lowcore.machine_flags |= MACHINE_FLAG_KVM; 229 else 230 S390_lowcore.machine_flags |= MACHINE_FLAG_VM; 231 } 232 233 static __init void setup_topology(void) 234 { 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--) { 241 if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0) 242 break; 243 } 244 topology_max_mnest = max_mnest; 245 } 246 247 static void early_pgm_check_handler(void) 248 { 249 const struct exception_table_entry *fixup; 250 unsigned long cr0, cr0_new; 251 unsigned long addr; 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); 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); 261 S390_lowcore.program_old_psw.addr = extable_fixup(fixup)|PSW_ADDR_AMODE; 262 __ctl_load(cr0, 0, 0); 263 } 264 265 static noinline __init void setup_lowcore_early(void) 266 { 267 psw_t psw; 268 269 psw.mask = PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA; 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 277 static noinline __init void setup_facility_list(void) 278 { 279 stfle(S390_lowcore.stfle_fac_list, 280 ARRAY_SIZE(S390_lowcore.stfle_fac_list)); 281 } 282 283 static __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) 296 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C; 297 } 298 299 static __init void detect_diag44(void) 300 { 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) 310 S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44; 311 } 312 313 static __init void detect_machine_facilities(void) 314 { 315 if (test_facility(8)) { 316 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1; 317 __ctl_set_bit(0, 23); 318 } 319 if (test_facility(78)) 320 S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2; 321 if (test_facility(3)) 322 S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE; 323 if (test_facility(40)) 324 S390_lowcore.machine_flags |= MACHINE_FLAG_LPP; 325 if (test_facility(50) && test_facility(73)) 326 S390_lowcore.machine_flags |= MACHINE_FLAG_TE; 327 if (test_facility(51)) 328 S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC; 329 if (test_facility(129)) 330 S390_lowcore.machine_flags |= MACHINE_FLAG_VX; 331 } 332 333 static int __init cad_setup(char *str) 334 { 335 int val; 336 337 get_option(&str, &val); 338 if (val && test_facility(128)) 339 S390_lowcore.machine_flags |= MACHINE_FLAG_CAD; 340 return 0; 341 } 342 early_param("cad", cad_setup); 343 344 static 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 } 351 early_initcall(cad_init); 352 353 static __init void rescue_initrd(void) 354 { 355 #ifdef CONFIG_BLK_DEV_INITRD 356 unsigned long min_initrd_addr = (unsigned long) _end + (4UL << 20); 357 /* 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. 362 */ 363 if (!INITRD_START || !INITRD_SIZE) 364 return; 365 if (INITRD_START >= min_initrd_addr) 366 return; 367 memmove((void *) min_initrd_addr, (void *) INITRD_START, INITRD_SIZE); 368 INITRD_START = min_initrd_addr; 369 #endif 370 } 371 372 /* Set up boot command line */ 373 static void __init append_to_cmdline(size_t (*ipl_data)(char *, size_t)) 374 { 375 char *parm, *delim; 376 size_t rc, len; 377 378 len = strlen(boot_command_line); 379 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 392 static inline int has_ebcdic_char(const char *str) 393 { 394 int i; 395 396 for (i = 0; str[i]; i++) 397 if (str[i] & 0x80) 398 return 1; 399 return 0; 400 } 401 402 static 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); 408 /* copy arch command line */ 409 strlcpy(boot_command_line, strstrip(COMMAND_LINE), 410 ARCH_COMMAND_LINE_SIZE); 411 412 /* append IPL PARM data to the boot command line */ 413 if (MACHINE_IS_VM) 414 append_to_cmdline(append_ipl_vmparm); 415 416 append_to_cmdline(append_ipl_scpdata); 417 } 418 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 */ 423 void __init startup_init(void) 424 { 425 reset_tod_clock(); 426 ipl_save_parameters(); 427 rescue_initrd(); 428 clear_bss_section(); 429 init_kernel_storage_key(); 430 lockdep_init(); 431 lockdep_off(); 432 setup_lowcore_early(); 433 setup_facility_list(); 434 detect_machine_type(); 435 ipl_update_parameters(); 436 setup_boot_command_line(); 437 create_kernel_nss(); 438 detect_diag9c(); 439 detect_diag44(); 440 detect_machine_facilities(); 441 setup_topology(); 442 sclp_early_detect(); 443 lockdep_on(); 444 } 445