early.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. 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. 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. sprintf(defsys_cmd, "DEFSYS %s 00000-%.5X EW %.5X-%.5X SR %.5X-%.5X",
  108. kernel_nss_name, stext_pfn - 1, stext_pfn, eshared_pfn - 1,
  109. eshared_pfn, end_pfn);
  110. #ifdef CONFIG_BLK_DEV_INITRD
  111. if (INITRD_START && INITRD_SIZE) {
  112. sinitrd_pfn = PFN_DOWN(__pa(INITRD_START));
  113. einitrd_pfn = PFN_UP(__pa(INITRD_START + INITRD_SIZE));
  114. min_size = einitrd_pfn << 2;
  115. sprintf(defsys_cmd, "%s EW %.5X-%.5X", defsys_cmd,
  116. sinitrd_pfn, einitrd_pfn);
  117. }
  118. #endif
  119. sprintf(defsys_cmd, "%s EW MINSIZE=%.7iK PARMREGS=0-13",
  120. defsys_cmd, min_size);
  121. sprintf(savesys_cmd, "SAVESYS %s \n IPL %s",
  122. kernel_nss_name, kernel_nss_name);
  123. __cpcmd(defsys_cmd, NULL, 0, &response);
  124. if (response != 0) {
  125. pr_err("Defining the Linux kernel NSS failed with rc=%d\n",
  126. response);
  127. kernel_nss_name[0] = '\0';
  128. return;
  129. }
  130. len = strlen(savesys_cmd);
  131. ASCEBC(savesys_cmd, len);
  132. response = savesys_ipl_nss(savesys_cmd, len);
  133. /* On success: response is equal to the command size,
  134. * max SAVESYS_CMD_SIZE
  135. * On error: response contains the numeric portion of cp error message.
  136. * for SAVESYS it will be >= 263
  137. * for missing privilege class, it will be 1
  138. */
  139. if (response > SAVESYS_CMD_SIZE || response == 1) {
  140. pr_err("Saving the Linux kernel NSS failed with rc=%d\n",
  141. response);
  142. kernel_nss_name[0] = '\0';
  143. return;
  144. }
  145. /* re-initialize cputime accounting. */
  146. sched_clock_base_cc = get_clock();
  147. S390_lowcore.last_update_clock = sched_clock_base_cc;
  148. S390_lowcore.last_update_timer = 0x7fffffffffffffffULL;
  149. S390_lowcore.user_timer = 0;
  150. S390_lowcore.system_timer = 0;
  151. asm volatile("SPT 0(%0)" : : "a" (&S390_lowcore.last_update_timer));
  152. /* re-setup boot command line with new ipl vm parms */
  153. ipl_update_parameters();
  154. setup_boot_command_line();
  155. ipl_flags = IPL_NSS_VALID;
  156. }
  157. #else /* CONFIG_SHARED_KERNEL */
  158. static inline void create_kernel_nss(void) { }
  159. #endif /* CONFIG_SHARED_KERNEL */
  160. /*
  161. * Clear bss memory
  162. */
  163. static noinline __init void clear_bss_section(void)
  164. {
  165. memset(__bss_start, 0, __bss_stop - __bss_start);
  166. }
  167. /*
  168. * Initialize storage key for kernel pages
  169. */
  170. static noinline __init void init_kernel_storage_key(void)
  171. {
  172. unsigned long end_pfn, init_pfn;
  173. end_pfn = PFN_UP(__pa(&_end));
  174. for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
  175. page_set_storage_key(init_pfn << PAGE_SHIFT, PAGE_DEFAULT_KEY);
  176. }
  177. static __initdata struct sysinfo_3_2_2 vmms __aligned(PAGE_SIZE);
  178. static noinline __init void detect_machine_type(void)
  179. {
  180. /* Check current-configuration-level */
  181. if ((stsi(NULL, 0, 0, 0) >> 28) <= 2) {
  182. S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
  183. return;
  184. }
  185. /* Get virtual-machine cpu information. */
  186. if (stsi(&vmms, 3, 2, 2) == -ENOSYS || !vmms.count)
  187. return;
  188. /* Running under KVM? If not we assume z/VM */
  189. if (!memcmp(vmms.vm[0].cpi, "\xd2\xe5\xd4", 3))
  190. S390_lowcore.machine_flags |= MACHINE_FLAG_KVM;
  191. else
  192. S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
  193. }
  194. static __init void early_pgm_check_handler(void)
  195. {
  196. unsigned long addr;
  197. const struct exception_table_entry *fixup;
  198. addr = S390_lowcore.program_old_psw.addr;
  199. fixup = search_exception_tables(addr & PSW_ADDR_INSN);
  200. if (!fixup)
  201. disabled_wait(0);
  202. S390_lowcore.program_old_psw.addr = fixup->fixup | PSW_ADDR_AMODE;
  203. }
  204. static noinline __init void setup_lowcore_early(void)
  205. {
  206. psw_t psw;
  207. psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
  208. psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_ext_handler;
  209. S390_lowcore.external_new_psw = psw;
  210. psw.addr = PSW_ADDR_AMODE | (unsigned long) s390_base_pgm_handler;
  211. S390_lowcore.program_new_psw = psw;
  212. s390_base_pgm_handler_fn = early_pgm_check_handler;
  213. }
  214. static noinline __init void setup_hpage(void)
  215. {
  216. #ifndef CONFIG_DEBUG_PAGEALLOC
  217. unsigned int facilities;
  218. facilities = stfl();
  219. if (!(facilities & (1UL << 23)) || !(facilities & (1UL << 29)))
  220. return;
  221. S390_lowcore.machine_flags |= MACHINE_FLAG_HPAGE;
  222. __ctl_set_bit(0, 23);
  223. #endif
  224. }
  225. static __init void detect_mvpg(void)
  226. {
  227. #ifndef CONFIG_64BIT
  228. int rc;
  229. asm volatile(
  230. " la 0,0\n"
  231. " mvpg %2,%2\n"
  232. "0: la %0,0\n"
  233. "1:\n"
  234. EX_TABLE(0b,1b)
  235. : "=d" (rc) : "0" (-EOPNOTSUPP), "a" (0) : "memory", "cc", "0");
  236. if (!rc)
  237. S390_lowcore.machine_flags |= MACHINE_FLAG_MVPG;
  238. #endif
  239. }
  240. static __init void detect_ieee(void)
  241. {
  242. #ifndef CONFIG_64BIT
  243. int rc, tmp;
  244. asm volatile(
  245. " efpc %1,0\n"
  246. "0: la %0,0\n"
  247. "1:\n"
  248. EX_TABLE(0b,1b)
  249. : "=d" (rc), "=d" (tmp): "0" (-EOPNOTSUPP) : "cc");
  250. if (!rc)
  251. S390_lowcore.machine_flags |= MACHINE_FLAG_IEEE;
  252. #endif
  253. }
  254. static __init void detect_csp(void)
  255. {
  256. #ifndef CONFIG_64BIT
  257. int rc;
  258. asm volatile(
  259. " la 0,0\n"
  260. " la 1,0\n"
  261. " la 2,4\n"
  262. " csp 0,2\n"
  263. "0: la %0,0\n"
  264. "1:\n"
  265. EX_TABLE(0b,1b)
  266. : "=d" (rc) : "0" (-EOPNOTSUPP) : "cc", "0", "1", "2");
  267. if (!rc)
  268. S390_lowcore.machine_flags |= MACHINE_FLAG_CSP;
  269. #endif
  270. }
  271. static __init void detect_diag9c(void)
  272. {
  273. unsigned int cpu_address;
  274. int rc;
  275. cpu_address = stap();
  276. asm volatile(
  277. " diag %2,0,0x9c\n"
  278. "0: la %0,0\n"
  279. "1:\n"
  280. EX_TABLE(0b,1b)
  281. : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
  282. if (!rc)
  283. S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
  284. }
  285. static __init void detect_diag44(void)
  286. {
  287. #ifdef CONFIG_64BIT
  288. int rc;
  289. asm volatile(
  290. " diag 0,0,0x44\n"
  291. "0: la %0,0\n"
  292. "1:\n"
  293. EX_TABLE(0b,1b)
  294. : "=d" (rc) : "0" (-EOPNOTSUPP) : "cc");
  295. if (!rc)
  296. S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG44;
  297. #endif
  298. }
  299. static __init void detect_machine_facilities(void)
  300. {
  301. #ifdef CONFIG_64BIT
  302. unsigned int facilities;
  303. facilities = stfl();
  304. if (facilities & (1 << 28))
  305. S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
  306. if (facilities & (1 << 23))
  307. S390_lowcore.machine_flags |= MACHINE_FLAG_PFMF;
  308. if (facilities & (1 << 4))
  309. S390_lowcore.machine_flags |= MACHINE_FLAG_MVCOS;
  310. #endif
  311. }
  312. static __init void rescue_initrd(void)
  313. {
  314. #ifdef CONFIG_BLK_DEV_INITRD
  315. /*
  316. * Move the initrd right behind the bss section in case it starts
  317. * within the bss section. So we don't overwrite it when the bss
  318. * section gets cleared.
  319. */
  320. if (!INITRD_START || !INITRD_SIZE)
  321. return;
  322. if (INITRD_START >= (unsigned long) __bss_stop)
  323. return;
  324. memmove(__bss_stop, (void *) INITRD_START, INITRD_SIZE);
  325. INITRD_START = (unsigned long) __bss_stop;
  326. #endif
  327. }
  328. /* Set up boot command line */
  329. static void __init append_to_cmdline(size_t (*ipl_data)(char *, size_t))
  330. {
  331. char *parm, *delim;
  332. size_t rc, len;
  333. len = strlen(boot_command_line);
  334. delim = boot_command_line + len; /* '\0' character position */
  335. parm = boot_command_line + len + 1; /* append right after '\0' */
  336. rc = ipl_data(parm, COMMAND_LINE_SIZE - len - 1);
  337. if (rc) {
  338. if (*parm == '=')
  339. memmove(boot_command_line, parm + 1, rc);
  340. else
  341. *delim = ' '; /* replace '\0' with space */
  342. }
  343. }
  344. static void __init setup_boot_command_line(void)
  345. {
  346. int i;
  347. /* convert arch command line to ascii */
  348. for (i = 0; i < ARCH_COMMAND_LINE_SIZE; i++)
  349. if (COMMAND_LINE[i] & 0x80)
  350. break;
  351. if (i < ARCH_COMMAND_LINE_SIZE)
  352. EBCASC(COMMAND_LINE, ARCH_COMMAND_LINE_SIZE);
  353. COMMAND_LINE[ARCH_COMMAND_LINE_SIZE-1] = 0;
  354. /* copy arch command line */
  355. strlcpy(boot_command_line, strstrip(COMMAND_LINE),
  356. ARCH_COMMAND_LINE_SIZE);
  357. /* append IPL PARM data to the boot command line */
  358. if (MACHINE_IS_VM)
  359. append_to_cmdline(append_ipl_vmparm);
  360. append_to_cmdline(append_ipl_scpdata);
  361. }
  362. /*
  363. * Save ipl parameters, clear bss memory, initialize storage keys
  364. * and create a kernel NSS at startup if the SAVESYS= parm is defined
  365. */
  366. void __init startup_init(void)
  367. {
  368. reset_tod_clock();
  369. ipl_save_parameters();
  370. rescue_initrd();
  371. clear_bss_section();
  372. init_kernel_storage_key();
  373. lockdep_init();
  374. lockdep_off();
  375. sort_main_extable();
  376. setup_lowcore_early();
  377. detect_machine_type();
  378. ipl_update_parameters();
  379. setup_boot_command_line();
  380. create_kernel_nss();
  381. detect_mvpg();
  382. detect_ieee();
  383. detect_csp();
  384. detect_diag9c();
  385. detect_diag44();
  386. detect_machine_facilities();
  387. setup_hpage();
  388. sclp_facilities_detect();
  389. detect_memory_layout(memory_chunk);
  390. #ifdef CONFIG_DYNAMIC_FTRACE
  391. S390_lowcore.ftrace_func = (unsigned long)ftrace_caller;
  392. #endif
  393. lockdep_on();
  394. }