cpu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Suspend support specific for i386/x86-64.
  4. *
  5. * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl>
  6. * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz>
  7. * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
  8. */
  9. #include <linux/suspend.h>
  10. #include <linux/export.h>
  11. #include <linux/smp.h>
  12. #include <linux/perf_event.h>
  13. #include <linux/tboot.h>
  14. #include <linux/dmi.h>
  15. #include <linux/pgtable.h>
  16. #include <asm/proto.h>
  17. #include <asm/mtrr.h>
  18. #include <asm/page.h>
  19. #include <asm/mce.h>
  20. #include <asm/suspend.h>
  21. #include <asm/fpu/api.h>
  22. #include <asm/debugreg.h>
  23. #include <asm/cpu.h>
  24. #include <asm/cacheinfo.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/cpu_device_id.h>
  27. #include <asm/microcode.h>
  28. #ifdef CONFIG_X86_32
  29. __visible unsigned long saved_context_ebx;
  30. __visible unsigned long saved_context_esp, saved_context_ebp;
  31. __visible unsigned long saved_context_esi, saved_context_edi;
  32. __visible unsigned long saved_context_eflags;
  33. #endif
  34. struct saved_context saved_context;
  35. static void msr_save_context(struct saved_context *ctxt)
  36. {
  37. struct saved_msr *msr = ctxt->saved_msrs.array;
  38. struct saved_msr *end = msr + ctxt->saved_msrs.num;
  39. while (msr < end) {
  40. if (msr->valid)
  41. rdmsrl(msr->info.msr_no, msr->info.reg.q);
  42. msr++;
  43. }
  44. }
  45. static void msr_restore_context(struct saved_context *ctxt)
  46. {
  47. struct saved_msr *msr = ctxt->saved_msrs.array;
  48. struct saved_msr *end = msr + ctxt->saved_msrs.num;
  49. while (msr < end) {
  50. if (msr->valid)
  51. wrmsrl(msr->info.msr_no, msr->info.reg.q);
  52. msr++;
  53. }
  54. }
  55. /**
  56. * __save_processor_state() - Save CPU registers before creating a
  57. * hibernation image and before restoring
  58. * the memory state from it
  59. * @ctxt: Structure to store the registers contents in.
  60. *
  61. * NOTE: If there is a CPU register the modification of which by the
  62. * boot kernel (ie. the kernel used for loading the hibernation image)
  63. * might affect the operations of the restored target kernel (ie. the one
  64. * saved in the hibernation image), then its contents must be saved by this
  65. * function. In other words, if kernel A is hibernated and different
  66. * kernel B is used for loading the hibernation image into memory, the
  67. * kernel A's __save_processor_state() function must save all registers
  68. * needed by kernel A, so that it can operate correctly after the resume
  69. * regardless of what kernel B does in the meantime.
  70. */
  71. static void __save_processor_state(struct saved_context *ctxt)
  72. {
  73. #ifdef CONFIG_X86_32
  74. mtrr_save_fixed_ranges(NULL);
  75. #endif
  76. kernel_fpu_begin();
  77. /*
  78. * descriptor tables
  79. */
  80. store_idt(&ctxt->idt);
  81. /*
  82. * We save it here, but restore it only in the hibernate case.
  83. * For ACPI S3 resume, this is loaded via 'early_gdt_desc' in 64-bit
  84. * mode in "secondary_startup_64". In 32-bit mode it is done via
  85. * 'pmode_gdt' in wakeup_start.
  86. */
  87. ctxt->gdt_desc.size = GDT_SIZE - 1;
  88. ctxt->gdt_desc.address = (unsigned long)get_cpu_gdt_rw(smp_processor_id());
  89. store_tr(ctxt->tr);
  90. /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
  91. /*
  92. * segment registers
  93. */
  94. savesegment(gs, ctxt->gs);
  95. #ifdef CONFIG_X86_64
  96. savesegment(fs, ctxt->fs);
  97. savesegment(ds, ctxt->ds);
  98. savesegment(es, ctxt->es);
  99. rdmsrl(MSR_FS_BASE, ctxt->fs_base);
  100. rdmsrl(MSR_GS_BASE, ctxt->kernelmode_gs_base);
  101. rdmsrl(MSR_KERNEL_GS_BASE, ctxt->usermode_gs_base);
  102. mtrr_save_fixed_ranges(NULL);
  103. rdmsrl(MSR_EFER, ctxt->efer);
  104. #endif
  105. /*
  106. * control registers
  107. */
  108. ctxt->cr0 = read_cr0();
  109. ctxt->cr2 = read_cr2();
  110. ctxt->cr3 = __read_cr3();
  111. ctxt->cr4 = __read_cr4();
  112. ctxt->misc_enable_saved = !rdmsrl_safe(MSR_IA32_MISC_ENABLE,
  113. &ctxt->misc_enable);
  114. msr_save_context(ctxt);
  115. }
  116. /* Needed by apm.c */
  117. void save_processor_state(void)
  118. {
  119. __save_processor_state(&saved_context);
  120. x86_platform.save_sched_clock_state();
  121. }
  122. #ifdef CONFIG_X86_32
  123. EXPORT_SYMBOL(save_processor_state);
  124. #endif
  125. static void do_fpu_end(void)
  126. {
  127. /*
  128. * Restore FPU regs if necessary.
  129. */
  130. kernel_fpu_end();
  131. }
  132. static void fix_processor_context(void)
  133. {
  134. int cpu = smp_processor_id();
  135. #ifdef CONFIG_X86_64
  136. struct desc_struct *desc = get_cpu_gdt_rw(cpu);
  137. tss_desc tss;
  138. #endif
  139. /*
  140. * We need to reload TR, which requires that we change the
  141. * GDT entry to indicate "available" first.
  142. *
  143. * XXX: This could probably all be replaced by a call to
  144. * force_reload_TR().
  145. */
  146. set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
  147. #ifdef CONFIG_X86_64
  148. memcpy(&tss, &desc[GDT_ENTRY_TSS], sizeof(tss_desc));
  149. tss.type = 0x9; /* The available 64-bit TSS (see AMD vol 2, pg 91 */
  150. write_gdt_entry(desc, GDT_ENTRY_TSS, &tss, DESC_TSS);
  151. syscall_init(); /* This sets MSR_*STAR and related */
  152. #else
  153. if (boot_cpu_has(X86_FEATURE_SEP))
  154. enable_sep_cpu();
  155. #endif
  156. load_TR_desc(); /* This does ltr */
  157. load_mm_ldt(current->active_mm); /* This does lldt */
  158. initialize_tlbstate_and_flush();
  159. fpu__resume_cpu();
  160. /* The processor is back on the direct GDT, load back the fixmap */
  161. load_fixmap_gdt(cpu);
  162. }
  163. /**
  164. * __restore_processor_state() - Restore the contents of CPU registers saved
  165. * by __save_processor_state()
  166. * @ctxt: Structure to load the registers contents from.
  167. *
  168. * The asm code that gets us here will have restored a usable GDT, although
  169. * it will be pointing to the wrong alias.
  170. */
  171. static void notrace __restore_processor_state(struct saved_context *ctxt)
  172. {
  173. struct cpuinfo_x86 *c;
  174. if (ctxt->misc_enable_saved)
  175. wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable);
  176. /*
  177. * control registers
  178. */
  179. /* cr4 was introduced in the Pentium CPU */
  180. #ifdef CONFIG_X86_32
  181. if (ctxt->cr4)
  182. __write_cr4(ctxt->cr4);
  183. #else
  184. /* CONFIG X86_64 */
  185. wrmsrl(MSR_EFER, ctxt->efer);
  186. __write_cr4(ctxt->cr4);
  187. #endif
  188. write_cr3(ctxt->cr3);
  189. write_cr2(ctxt->cr2);
  190. write_cr0(ctxt->cr0);
  191. /* Restore the IDT. */
  192. load_idt(&ctxt->idt);
  193. /*
  194. * Just in case the asm code got us here with the SS, DS, or ES
  195. * out of sync with the GDT, update them.
  196. */
  197. loadsegment(ss, __KERNEL_DS);
  198. loadsegment(ds, __USER_DS);
  199. loadsegment(es, __USER_DS);
  200. /*
  201. * Restore percpu access. Percpu access can happen in exception
  202. * handlers or in complicated helpers like load_gs_index().
  203. */
  204. #ifdef CONFIG_X86_64
  205. wrmsrl(MSR_GS_BASE, ctxt->kernelmode_gs_base);
  206. #else
  207. loadsegment(fs, __KERNEL_PERCPU);
  208. #endif
  209. /* Restore the TSS, RO GDT, LDT, and usermode-relevant MSRs. */
  210. fix_processor_context();
  211. /*
  212. * Now that we have descriptor tables fully restored and working
  213. * exception handling, restore the usermode segments.
  214. */
  215. #ifdef CONFIG_X86_64
  216. loadsegment(ds, ctxt->es);
  217. loadsegment(es, ctxt->es);
  218. loadsegment(fs, ctxt->fs);
  219. load_gs_index(ctxt->gs);
  220. /*
  221. * Restore FSBASE and GSBASE after restoring the selectors, since
  222. * restoring the selectors clobbers the bases. Keep in mind
  223. * that MSR_KERNEL_GS_BASE is horribly misnamed.
  224. */
  225. wrmsrl(MSR_FS_BASE, ctxt->fs_base);
  226. wrmsrl(MSR_KERNEL_GS_BASE, ctxt->usermode_gs_base);
  227. #else
  228. loadsegment(gs, ctxt->gs);
  229. #endif
  230. do_fpu_end();
  231. tsc_verify_tsc_adjust(true);
  232. x86_platform.restore_sched_clock_state();
  233. cache_bp_restore();
  234. perf_restore_debug_store();
  235. c = &cpu_data(smp_processor_id());
  236. if (cpu_has(c, X86_FEATURE_MSR_IA32_FEAT_CTL))
  237. init_ia32_feat_ctl(c);
  238. microcode_bsp_resume();
  239. /*
  240. * This needs to happen after the microcode has been updated upon resume
  241. * because some of the MSRs are "emulated" in microcode.
  242. */
  243. msr_restore_context(ctxt);
  244. }
  245. /* Needed by apm.c */
  246. void notrace restore_processor_state(void)
  247. {
  248. __restore_processor_state(&saved_context);
  249. }
  250. #ifdef CONFIG_X86_32
  251. EXPORT_SYMBOL(restore_processor_state);
  252. #endif
  253. #if defined(CONFIG_HIBERNATION) && defined(CONFIG_HOTPLUG_CPU)
  254. static void __noreturn resume_play_dead(void)
  255. {
  256. play_dead_common();
  257. tboot_shutdown(TB_SHUTDOWN_WFS);
  258. hlt_play_dead();
  259. }
  260. int hibernate_resume_nonboot_cpu_disable(void)
  261. {
  262. void (*play_dead)(void) = smp_ops.play_dead;
  263. int ret;
  264. /*
  265. * Ensure that MONITOR/MWAIT will not be used in the "play dead" loop
  266. * during hibernate image restoration, because it is likely that the
  267. * monitored address will be actually written to at that time and then
  268. * the "dead" CPU will attempt to execute instructions again, but the
  269. * address in its instruction pointer may not be possible to resolve
  270. * any more at that point (the page tables used by it previously may
  271. * have been overwritten by hibernate image data).
  272. *
  273. * First, make sure that we wake up all the potentially disabled SMT
  274. * threads which have been initially brought up and then put into
  275. * mwait/cpuidle sleep.
  276. * Those will be put to proper (not interfering with hibernation
  277. * resume) sleep afterwards, and the resumed kernel will decide itself
  278. * what to do with them.
  279. */
  280. ret = cpuhp_smt_enable();
  281. if (ret)
  282. return ret;
  283. smp_ops.play_dead = resume_play_dead;
  284. ret = freeze_secondary_cpus(0);
  285. smp_ops.play_dead = play_dead;
  286. return ret;
  287. }
  288. #endif
  289. /*
  290. * When bsp_check() is called in hibernate and suspend, cpu hotplug
  291. * is disabled already. So it's unnecessary to handle race condition between
  292. * cpumask query and cpu hotplug.
  293. */
  294. static int bsp_check(void)
  295. {
  296. if (cpumask_first(cpu_online_mask) != 0) {
  297. pr_warn("CPU0 is offline.\n");
  298. return -ENODEV;
  299. }
  300. return 0;
  301. }
  302. static int bsp_pm_callback(struct notifier_block *nb, unsigned long action,
  303. void *ptr)
  304. {
  305. int ret = 0;
  306. switch (action) {
  307. case PM_SUSPEND_PREPARE:
  308. case PM_HIBERNATION_PREPARE:
  309. ret = bsp_check();
  310. break;
  311. default:
  312. break;
  313. }
  314. return notifier_from_errno(ret);
  315. }
  316. static int __init bsp_pm_check_init(void)
  317. {
  318. /*
  319. * Set this bsp_pm_callback as lower priority than
  320. * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called
  321. * earlier to disable cpu hotplug before bsp online check.
  322. */
  323. pm_notifier(bsp_pm_callback, -INT_MAX);
  324. return 0;
  325. }
  326. core_initcall(bsp_pm_check_init);
  327. static int msr_build_context(const u32 *msr_id, const int num)
  328. {
  329. struct saved_msrs *saved_msrs = &saved_context.saved_msrs;
  330. struct saved_msr *msr_array;
  331. int total_num;
  332. int i, j;
  333. total_num = saved_msrs->num + num;
  334. msr_array = kmalloc_array(total_num, sizeof(struct saved_msr), GFP_KERNEL);
  335. if (!msr_array) {
  336. pr_err("x86/pm: Can not allocate memory to save/restore MSRs during suspend.\n");
  337. return -ENOMEM;
  338. }
  339. if (saved_msrs->array) {
  340. /*
  341. * Multiple callbacks can invoke this function, so copy any
  342. * MSR save requests from previous invocations.
  343. */
  344. memcpy(msr_array, saved_msrs->array,
  345. sizeof(struct saved_msr) * saved_msrs->num);
  346. kfree(saved_msrs->array);
  347. }
  348. for (i = saved_msrs->num, j = 0; i < total_num; i++, j++) {
  349. u64 dummy;
  350. msr_array[i].info.msr_no = msr_id[j];
  351. msr_array[i].valid = !rdmsrl_safe(msr_id[j], &dummy);
  352. msr_array[i].info.reg.q = 0;
  353. }
  354. saved_msrs->num = total_num;
  355. saved_msrs->array = msr_array;
  356. return 0;
  357. }
  358. /*
  359. * The following sections are a quirk framework for problematic BIOSen:
  360. * Sometimes MSRs are modified by the BIOSen after suspended to
  361. * RAM, this might cause unexpected behavior after wakeup.
  362. * Thus we save/restore these specified MSRs across suspend/resume
  363. * in order to work around it.
  364. *
  365. * For any further problematic BIOSen/platforms,
  366. * please add your own function similar to msr_initialize_bdw.
  367. */
  368. static int msr_initialize_bdw(const struct dmi_system_id *d)
  369. {
  370. /* Add any extra MSR ids into this array. */
  371. u32 bdw_msr_id[] = { MSR_IA32_THERM_CONTROL };
  372. pr_info("x86/pm: %s detected, MSR saving is needed during suspending.\n", d->ident);
  373. return msr_build_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
  374. }
  375. static const struct dmi_system_id msr_save_dmi_table[] = {
  376. {
  377. .callback = msr_initialize_bdw,
  378. .ident = "BROADWELL BDX_EP",
  379. .matches = {
  380. DMI_MATCH(DMI_PRODUCT_NAME, "GRANTLEY"),
  381. DMI_MATCH(DMI_PRODUCT_VERSION, "E63448-400"),
  382. },
  383. },
  384. {}
  385. };
  386. static int msr_save_cpuid_features(const struct x86_cpu_id *c)
  387. {
  388. u32 cpuid_msr_id[] = {
  389. MSR_AMD64_CPUID_FN_1,
  390. };
  391. pr_info("x86/pm: family %#hx cpu detected, MSR saving is needed during suspending.\n",
  392. c->family);
  393. return msr_build_context(cpuid_msr_id, ARRAY_SIZE(cpuid_msr_id));
  394. }
  395. static const struct x86_cpu_id msr_save_cpu_table[] = {
  396. X86_MATCH_VENDOR_FAM(AMD, 0x15, &msr_save_cpuid_features),
  397. X86_MATCH_VENDOR_FAM(AMD, 0x16, &msr_save_cpuid_features),
  398. {}
  399. };
  400. typedef int (*pm_cpu_match_t)(const struct x86_cpu_id *);
  401. static int pm_cpu_check(const struct x86_cpu_id *c)
  402. {
  403. const struct x86_cpu_id *m;
  404. int ret = 0;
  405. m = x86_match_cpu(msr_save_cpu_table);
  406. if (m) {
  407. pm_cpu_match_t fn;
  408. fn = (pm_cpu_match_t)m->driver_data;
  409. ret = fn(m);
  410. }
  411. return ret;
  412. }
  413. static void pm_save_spec_msr(void)
  414. {
  415. struct msr_enumeration {
  416. u32 msr_no;
  417. u32 feature;
  418. } msr_enum[] = {
  419. { MSR_IA32_SPEC_CTRL, X86_FEATURE_MSR_SPEC_CTRL },
  420. { MSR_IA32_TSX_CTRL, X86_FEATURE_MSR_TSX_CTRL },
  421. { MSR_TSX_FORCE_ABORT, X86_FEATURE_TSX_FORCE_ABORT },
  422. { MSR_IA32_MCU_OPT_CTRL, X86_FEATURE_SRBDS_CTRL },
  423. { MSR_AMD64_LS_CFG, X86_FEATURE_LS_CFG_SSBD },
  424. { MSR_AMD64_DE_CFG, X86_FEATURE_LFENCE_RDTSC },
  425. };
  426. int i;
  427. for (i = 0; i < ARRAY_SIZE(msr_enum); i++) {
  428. if (boot_cpu_has(msr_enum[i].feature))
  429. msr_build_context(&msr_enum[i].msr_no, 1);
  430. }
  431. }
  432. static int pm_check_save_msr(void)
  433. {
  434. dmi_check_system(msr_save_dmi_table);
  435. pm_cpu_check(msr_save_cpu_table);
  436. pm_save_spec_msr();
  437. return 0;
  438. }
  439. device_initcall(pm_check_save_msr);