early.c 12 KB

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