early.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2007, 2009
  4. * Author(s): Hongjie Yang <[email protected]>,
  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/extable.h>
  15. #include <linux/pfn.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/kernel.h>
  18. #include <asm/asm-extable.h>
  19. #include <asm/diag.h>
  20. #include <asm/ebcdic.h>
  21. #include <asm/ipl.h>
  22. #include <asm/lowcore.h>
  23. #include <asm/processor.h>
  24. #include <asm/sections.h>
  25. #include <asm/setup.h>
  26. #include <asm/sysinfo.h>
  27. #include <asm/cpcmd.h>
  28. #include <asm/sclp.h>
  29. #include <asm/facility.h>
  30. #include <asm/boot_data.h>
  31. #include <asm/switch_to.h>
  32. #include "entry.h"
  33. int __bootdata(is_full_image);
  34. static void __init reset_tod_clock(void)
  35. {
  36. union tod_clock clk;
  37. if (store_tod_clock_ext_cc(&clk) == 0)
  38. return;
  39. /* TOD clock not running. Set the clock to Unix Epoch. */
  40. if (set_tod_clock(TOD_UNIX_EPOCH) || store_tod_clock_ext_cc(&clk))
  41. disabled_wait();
  42. memset(&tod_clock_base, 0, sizeof(tod_clock_base));
  43. tod_clock_base.tod = TOD_UNIX_EPOCH;
  44. S390_lowcore.last_update_clock = TOD_UNIX_EPOCH;
  45. }
  46. /*
  47. * Initialize storage key for kernel pages
  48. */
  49. static noinline __init void init_kernel_storage_key(void)
  50. {
  51. #if PAGE_DEFAULT_KEY
  52. unsigned long end_pfn, init_pfn;
  53. end_pfn = PFN_UP(__pa(_end));
  54. for (init_pfn = 0 ; init_pfn < end_pfn; init_pfn++)
  55. page_set_storage_key(init_pfn << PAGE_SHIFT,
  56. PAGE_DEFAULT_KEY, 0);
  57. #endif
  58. }
  59. static __initdata char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
  60. static noinline __init void detect_machine_type(void)
  61. {
  62. struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;
  63. /* Check current-configuration-level */
  64. if (stsi(NULL, 0, 0, 0) <= 2) {
  65. S390_lowcore.machine_flags |= MACHINE_FLAG_LPAR;
  66. return;
  67. }
  68. /* Get virtual-machine cpu information. */
  69. if (stsi(vmms, 3, 2, 2) || !vmms->count)
  70. return;
  71. /* Detect known hypervisors */
  72. if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3))
  73. S390_lowcore.machine_flags |= MACHINE_FLAG_KVM;
  74. else if (!memcmp(vmms->vm[0].cpi, "\xa9\x61\xe5\xd4", 4))
  75. S390_lowcore.machine_flags |= MACHINE_FLAG_VM;
  76. }
  77. /* Remove leading, trailing and double whitespace. */
  78. static inline void strim_all(char *str)
  79. {
  80. char *s;
  81. s = strim(str);
  82. if (s != str)
  83. memmove(str, s, strlen(s));
  84. while (*str) {
  85. if (!isspace(*str++))
  86. continue;
  87. if (isspace(*str)) {
  88. s = skip_spaces(str);
  89. memmove(str, s, strlen(s) + 1);
  90. }
  91. }
  92. }
  93. static noinline __init void setup_arch_string(void)
  94. {
  95. struct sysinfo_1_1_1 *mach = (struct sysinfo_1_1_1 *)&sysinfo_page;
  96. struct sysinfo_3_2_2 *vm = (struct sysinfo_3_2_2 *)&sysinfo_page;
  97. char mstr[80], hvstr[17];
  98. if (stsi(mach, 1, 1, 1))
  99. return;
  100. EBCASC(mach->manufacturer, sizeof(mach->manufacturer));
  101. EBCASC(mach->type, sizeof(mach->type));
  102. EBCASC(mach->model, sizeof(mach->model));
  103. EBCASC(mach->model_capacity, sizeof(mach->model_capacity));
  104. sprintf(mstr, "%-16.16s %-4.4s %-16.16s %-16.16s",
  105. mach->manufacturer, mach->type,
  106. mach->model, mach->model_capacity);
  107. strim_all(mstr);
  108. if (stsi(vm, 3, 2, 2) == 0 && vm->count) {
  109. EBCASC(vm->vm[0].cpi, sizeof(vm->vm[0].cpi));
  110. sprintf(hvstr, "%-16.16s", vm->vm[0].cpi);
  111. strim_all(hvstr);
  112. } else {
  113. sprintf(hvstr, "%s",
  114. MACHINE_IS_LPAR ? "LPAR" :
  115. MACHINE_IS_VM ? "z/VM" :
  116. MACHINE_IS_KVM ? "KVM" : "unknown");
  117. }
  118. dump_stack_set_arch_desc("%s (%s)", mstr, hvstr);
  119. }
  120. static __init void setup_topology(void)
  121. {
  122. int max_mnest;
  123. if (!test_facility(11))
  124. return;
  125. S390_lowcore.machine_flags |= MACHINE_FLAG_TOPOLOGY;
  126. for (max_mnest = 6; max_mnest > 1; max_mnest--) {
  127. if (stsi(&sysinfo_page, 15, 1, max_mnest) == 0)
  128. break;
  129. }
  130. topology_max_mnest = max_mnest;
  131. }
  132. void __do_early_pgm_check(struct pt_regs *regs)
  133. {
  134. if (!fixup_exception(regs))
  135. disabled_wait();
  136. }
  137. static noinline __init void setup_lowcore_early(void)
  138. {
  139. psw_t psw;
  140. psw.addr = (unsigned long)early_pgm_check_handler;
  141. psw.mask = PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA;
  142. if (IS_ENABLED(CONFIG_KASAN))
  143. psw.mask |= PSW_MASK_DAT;
  144. S390_lowcore.program_new_psw = psw;
  145. S390_lowcore.preempt_count = INIT_PREEMPT_COUNT;
  146. }
  147. static noinline __init void setup_facility_list(void)
  148. {
  149. memcpy(alt_stfle_fac_list, stfle_fac_list, sizeof(alt_stfle_fac_list));
  150. if (!IS_ENABLED(CONFIG_KERNEL_NOBP))
  151. __clear_facility(82, alt_stfle_fac_list);
  152. }
  153. static __init void detect_diag9c(void)
  154. {
  155. unsigned int cpu_address;
  156. int rc;
  157. cpu_address = stap();
  158. diag_stat_inc(DIAG_STAT_X09C);
  159. asm volatile(
  160. " diag %2,0,0x9c\n"
  161. "0: la %0,0\n"
  162. "1:\n"
  163. EX_TABLE(0b,1b)
  164. : "=d" (rc) : "0" (-EOPNOTSUPP), "d" (cpu_address) : "cc");
  165. if (!rc)
  166. S390_lowcore.machine_flags |= MACHINE_FLAG_DIAG9C;
  167. }
  168. static __init void detect_machine_facilities(void)
  169. {
  170. if (test_facility(8)) {
  171. S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT1;
  172. __ctl_set_bit(0, 23);
  173. }
  174. if (test_facility(78))
  175. S390_lowcore.machine_flags |= MACHINE_FLAG_EDAT2;
  176. if (test_facility(3))
  177. S390_lowcore.machine_flags |= MACHINE_FLAG_IDTE;
  178. if (test_facility(50) && test_facility(73)) {
  179. S390_lowcore.machine_flags |= MACHINE_FLAG_TE;
  180. __ctl_set_bit(0, 55);
  181. }
  182. if (test_facility(51))
  183. S390_lowcore.machine_flags |= MACHINE_FLAG_TLB_LC;
  184. if (test_facility(129)) {
  185. S390_lowcore.machine_flags |= MACHINE_FLAG_VX;
  186. __ctl_set_bit(0, 17);
  187. }
  188. if (test_facility(130) && !noexec_disabled) {
  189. S390_lowcore.machine_flags |= MACHINE_FLAG_NX;
  190. __ctl_set_bit(0, 20);
  191. }
  192. if (test_facility(133))
  193. S390_lowcore.machine_flags |= MACHINE_FLAG_GS;
  194. if (test_facility(139) && (tod_clock_base.tod >> 63)) {
  195. /* Enabled signed clock comparator comparisons */
  196. S390_lowcore.machine_flags |= MACHINE_FLAG_SCC;
  197. clock_comparator_max = -1ULL >> 1;
  198. __ctl_set_bit(0, 53);
  199. }
  200. if (IS_ENABLED(CONFIG_PCI) && test_facility(153)) {
  201. S390_lowcore.machine_flags |= MACHINE_FLAG_PCI_MIO;
  202. /* the control bit is set during PCI initialization */
  203. }
  204. }
  205. static inline void save_vector_registers(void)
  206. {
  207. #ifdef CONFIG_CRASH_DUMP
  208. if (test_facility(129))
  209. save_vx_regs(boot_cpu_vector_save_area);
  210. #endif
  211. }
  212. static inline void setup_control_registers(void)
  213. {
  214. unsigned long reg;
  215. __ctl_store(reg, 0, 0);
  216. reg |= CR0_LOW_ADDRESS_PROTECTION;
  217. reg |= CR0_EMERGENCY_SIGNAL_SUBMASK;
  218. reg |= CR0_EXTERNAL_CALL_SUBMASK;
  219. __ctl_load(reg, 0, 0);
  220. }
  221. static inline void setup_access_registers(void)
  222. {
  223. unsigned int acrs[NUM_ACRS] = { 0 };
  224. restore_access_regs(acrs);
  225. }
  226. static int __init disable_vector_extension(char *str)
  227. {
  228. S390_lowcore.machine_flags &= ~MACHINE_FLAG_VX;
  229. __ctl_clear_bit(0, 17);
  230. return 0;
  231. }
  232. early_param("novx", disable_vector_extension);
  233. char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
  234. static void __init setup_boot_command_line(void)
  235. {
  236. /* copy arch command line */
  237. strscpy(boot_command_line, early_command_line, COMMAND_LINE_SIZE);
  238. }
  239. static void __init check_image_bootable(void)
  240. {
  241. if (is_full_image)
  242. return;
  243. sclp_early_printk("Linux kernel boot failure: An attempt to boot a vmlinux ELF image failed.\n");
  244. sclp_early_printk("This image does not contain all parts necessary for starting up. Use\n");
  245. sclp_early_printk("bzImage or arch/s390/boot/compressed/vmlinux instead.\n");
  246. disabled_wait();
  247. }
  248. static void __init sort_amode31_extable(void)
  249. {
  250. sort_extable(__start_amode31_ex_table, __stop_amode31_ex_table);
  251. }
  252. void __init startup_init(void)
  253. {
  254. reset_tod_clock();
  255. check_image_bootable();
  256. time_early_init();
  257. init_kernel_storage_key();
  258. lockdep_off();
  259. sort_amode31_extable();
  260. setup_lowcore_early();
  261. setup_facility_list();
  262. detect_machine_type();
  263. setup_arch_string();
  264. setup_boot_command_line();
  265. detect_diag9c();
  266. detect_machine_facilities();
  267. save_vector_registers();
  268. setup_topology();
  269. sclp_early_detect();
  270. setup_control_registers();
  271. setup_access_registers();
  272. lockdep_on();
  273. }