early.c 12 KB

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