cpu.c 14 KB

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