early.c 10.0 KB

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