early.c 12 KB

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