early.c 11 KB

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