cpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <asm/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/internal.h>
  22. #include <asm/debugreg.h>
  23. #include <asm/cpu.h>
  24. #include <asm/mmu_context.h>
  25. #include <linux/dmi.h>
  26. #ifdef CONFIG_X86_32
  27. __visible unsigned long saved_context_ebx;
  28. __visible unsigned long saved_context_esp, saved_context_ebp;
  29. __visible unsigned long saved_context_esi, saved_context_edi;
  30. __visible unsigned long saved_context_eflags;
  31. #endif
  32. struct saved_context saved_context;
  33. static void msr_save_context(struct saved_context *ctxt)
  34. {
  35. struct saved_msr *msr = ctxt->saved_msrs.array;
  36. struct saved_msr *end = msr + ctxt->saved_msrs.num;
  37. while (msr < end) {
  38. msr->valid = !rdmsrl_safe(msr->info.msr_no, &msr->info.reg.q);
  39. msr++;
  40. }
  41. }
  42. static void msr_restore_context(struct saved_context *ctxt)
  43. {
  44. struct saved_msr *msr = ctxt->saved_msrs.array;
  45. struct saved_msr *end = msr + ctxt->saved_msrs.num;
  46. while (msr < end) {
  47. if (msr->valid)
  48. wrmsrl(msr->info.msr_no, msr->info.reg.q);
  49. msr++;
  50. }
  51. }
  52. /**
  53. * __save_processor_state - save CPU registers before creating a
  54. * hibernation image and before restoring the memory state from it
  55. * @ctxt - structure to store the registers contents in
  56. *
  57. * NOTE: If there is a CPU register the modification of which by the
  58. * boot kernel (ie. the kernel used for loading the hibernation image)
  59. * might affect the operations of the restored target kernel (ie. the one
  60. * saved in the hibernation image), then its contents must be saved by this
  61. * function. In other words, if kernel A is hibernated and different
  62. * kernel B is used for loading the hibernation image into memory, the
  63. * kernel A's __save_processor_state() function must save all registers
  64. * needed by kernel A, so that it can operate correctly after the resume
  65. * regardless of what kernel B does in the meantime.
  66. */
  67. static void __save_processor_state(struct saved_context *ctxt)
  68. {
  69. #ifdef CONFIG_X86_32
  70. mtrr_save_fixed_ranges(NULL);
  71. #endif
  72. kernel_fpu_begin();
  73. /*
  74. * descriptor tables
  75. */
  76. #ifdef CONFIG_X86_32
  77. store_idt(&ctxt->idt);
  78. #else
  79. /* CONFIG_X86_64 */
  80. store_idt((struct desc_ptr *)&ctxt->idt_limit);
  81. #endif
  82. /*
  83. * We save it here, but restore it only in the hibernate case.
  84. * For ACPI S3 resume, this is loaded via 'early_gdt_desc' in 64-bit
  85. * mode in "secondary_startup_64". In 32-bit mode it is done via
  86. * 'pmode_gdt' in wakeup_start.
  87. */
  88. ctxt->gdt_desc.size = GDT_SIZE - 1;
  89. ctxt->gdt_desc.address = (unsigned long)get_cpu_gdt_rw(smp_processor_id());
  90. store_tr(ctxt->tr);
  91. /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
  92. /*
  93. * segment registers
  94. */
  95. #ifdef CONFIG_X86_32
  96. savesegment(es, ctxt->es);
  97. savesegment(fs, ctxt->fs);
  98. savesegment(gs, ctxt->gs);
  99. savesegment(ss, ctxt->ss);
  100. #else
  101. /* CONFIG_X86_64 */
  102. asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
  103. asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
  104. asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
  105. asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
  106. asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
  107. rdmsrl(MSR_FS_BASE, ctxt->fs_base);
  108. rdmsrl(MSR_GS_BASE, ctxt->gs_base);
  109. rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  110. mtrr_save_fixed_ranges(NULL);
  111. rdmsrl(MSR_EFER, ctxt->efer);
  112. #endif
  113. /*
  114. * control registers
  115. */
  116. ctxt->cr0 = read_cr0();
  117. ctxt->cr2 = read_cr2();
  118. ctxt->cr3 = read_cr3();
  119. ctxt->cr4 = __read_cr4();
  120. #ifdef CONFIG_X86_64
  121. ctxt->cr8 = read_cr8();
  122. #endif
  123. ctxt->misc_enable_saved = !rdmsrl_safe(MSR_IA32_MISC_ENABLE,
  124. &ctxt->misc_enable);
  125. msr_save_context(ctxt);
  126. }
  127. /* Needed by apm.c */
  128. void save_processor_state(void)
  129. {
  130. __save_processor_state(&saved_context);
  131. x86_platform.save_sched_clock_state();
  132. }
  133. #ifdef CONFIG_X86_32
  134. EXPORT_SYMBOL(save_processor_state);
  135. #endif
  136. static void do_fpu_end(void)
  137. {
  138. /*
  139. * Restore FPU regs if necessary.
  140. */
  141. kernel_fpu_end();
  142. }
  143. static void fix_processor_context(void)
  144. {
  145. int cpu = smp_processor_id();
  146. struct tss_struct *t = &per_cpu(cpu_tss, cpu);
  147. #ifdef CONFIG_X86_64
  148. struct desc_struct *desc = get_cpu_gdt_rw(cpu);
  149. tss_desc tss;
  150. #endif
  151. set_tss_desc(cpu, t); /*
  152. * This just modifies memory; should not be
  153. * necessary. But... This is necessary, because
  154. * 386 hardware has concept of busy TSS or some
  155. * similar stupidity.
  156. */
  157. #ifdef CONFIG_X86_64
  158. memcpy(&tss, &desc[GDT_ENTRY_TSS], sizeof(tss_desc));
  159. tss.type = 0x9; /* The available 64-bit TSS (see AMD vol 2, pg 91 */
  160. write_gdt_entry(desc, GDT_ENTRY_TSS, &tss, DESC_TSS);
  161. syscall_init(); /* This sets MSR_*STAR and related */
  162. #endif
  163. load_TR_desc(); /* This does ltr */
  164. load_mm_ldt(current->active_mm); /* This does lldt */
  165. fpu__resume_cpu();
  166. /* The processor is back on the direct GDT, load back the fixmap */
  167. load_fixmap_gdt(cpu);
  168. }
  169. /**
  170. * __restore_processor_state - restore the contents of CPU registers saved
  171. * by __save_processor_state()
  172. * @ctxt - structure to load the registers contents from
  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. /*
  195. * now restore the descriptor tables to their proper values
  196. * ltr is done i fix_processor_context().
  197. */
  198. #ifdef CONFIG_X86_32
  199. load_idt(&ctxt->idt);
  200. #else
  201. /* CONFIG_X86_64 */
  202. load_idt((const struct desc_ptr *)&ctxt->idt_limit);
  203. #endif
  204. /*
  205. * segment registers
  206. */
  207. #ifdef CONFIG_X86_32
  208. loadsegment(es, ctxt->es);
  209. loadsegment(fs, ctxt->fs);
  210. loadsegment(gs, ctxt->gs);
  211. loadsegment(ss, ctxt->ss);
  212. /*
  213. * sysenter MSRs
  214. */
  215. if (boot_cpu_has(X86_FEATURE_SEP))
  216. enable_sep_cpu();
  217. #else
  218. /* CONFIG_X86_64 */
  219. asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
  220. asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
  221. asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
  222. load_gs_index(ctxt->gs);
  223. asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
  224. wrmsrl(MSR_FS_BASE, ctxt->fs_base);
  225. wrmsrl(MSR_GS_BASE, ctxt->gs_base);
  226. wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  227. #endif
  228. fix_processor_context();
  229. do_fpu_end();
  230. tsc_verify_tsc_adjust(true);
  231. x86_platform.restore_sched_clock_state();
  232. mtrr_bp_restore();
  233. perf_restore_debug_store();
  234. msr_restore_context(ctxt);
  235. }
  236. /* Needed by apm.c */
  237. void notrace restore_processor_state(void)
  238. {
  239. __restore_processor_state(&saved_context);
  240. }
  241. #ifdef CONFIG_X86_32
  242. EXPORT_SYMBOL(restore_processor_state);
  243. #endif
  244. #if defined(CONFIG_HIBERNATION) && defined(CONFIG_HOTPLUG_CPU)
  245. static void resume_play_dead(void)
  246. {
  247. play_dead_common();
  248. tboot_shutdown(TB_SHUTDOWN_WFS);
  249. hlt_play_dead();
  250. }
  251. int hibernate_resume_nonboot_cpu_disable(void)
  252. {
  253. void (*play_dead)(void) = smp_ops.play_dead;
  254. int ret;
  255. /*
  256. * Ensure that MONITOR/MWAIT will not be used in the "play dead" loop
  257. * during hibernate image restoration, because it is likely that the
  258. * monitored address will be actually written to at that time and then
  259. * the "dead" CPU will attempt to execute instructions again, but the
  260. * address in its instruction pointer may not be possible to resolve
  261. * any more at that point (the page tables used by it previously may
  262. * have been overwritten by hibernate image data).
  263. */
  264. smp_ops.play_dead = resume_play_dead;
  265. ret = disable_nonboot_cpus();
  266. smp_ops.play_dead = play_dead;
  267. return ret;
  268. }
  269. #endif
  270. /*
  271. * When bsp_check() is called in hibernate and suspend, cpu hotplug
  272. * is disabled already. So it's unnessary to handle race condition between
  273. * cpumask query and cpu hotplug.
  274. */
  275. static int bsp_check(void)
  276. {
  277. if (cpumask_first(cpu_online_mask) != 0) {
  278. pr_warn("CPU0 is offline.\n");
  279. return -ENODEV;
  280. }
  281. return 0;
  282. }
  283. static int bsp_pm_callback(struct notifier_block *nb, unsigned long action,
  284. void *ptr)
  285. {
  286. int ret = 0;
  287. switch (action) {
  288. case PM_SUSPEND_PREPARE:
  289. case PM_HIBERNATION_PREPARE:
  290. ret = bsp_check();
  291. break;
  292. #ifdef CONFIG_DEBUG_HOTPLUG_CPU0
  293. case PM_RESTORE_PREPARE:
  294. /*
  295. * When system resumes from hibernation, online CPU0 because
  296. * 1. it's required for resume and
  297. * 2. the CPU was online before hibernation
  298. */
  299. if (!cpu_online(0))
  300. _debug_hotplug_cpu(0, 1);
  301. break;
  302. case PM_POST_RESTORE:
  303. /*
  304. * When a resume really happens, this code won't be called.
  305. *
  306. * This code is called only when user space hibernation software
  307. * prepares for snapshot device during boot time. So we just
  308. * call _debug_hotplug_cpu() to restore to CPU0's state prior to
  309. * preparing the snapshot device.
  310. *
  311. * This works for normal boot case in our CPU0 hotplug debug
  312. * mode, i.e. CPU0 is offline and user mode hibernation
  313. * software initializes during boot time.
  314. *
  315. * If CPU0 is online and user application accesses snapshot
  316. * device after boot time, this will offline CPU0 and user may
  317. * see different CPU0 state before and after accessing
  318. * the snapshot device. But hopefully this is not a case when
  319. * user debugging CPU0 hotplug. Even if users hit this case,
  320. * they can easily online CPU0 back.
  321. *
  322. * To simplify this debug code, we only consider normal boot
  323. * case. Otherwise we need to remember CPU0's state and restore
  324. * to that state and resolve racy conditions etc.
  325. */
  326. _debug_hotplug_cpu(0, 0);
  327. break;
  328. #endif
  329. default:
  330. break;
  331. }
  332. return notifier_from_errno(ret);
  333. }
  334. static int __init bsp_pm_check_init(void)
  335. {
  336. /*
  337. * Set this bsp_pm_callback as lower priority than
  338. * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called
  339. * earlier to disable cpu hotplug before bsp online check.
  340. */
  341. pm_notifier(bsp_pm_callback, -INT_MAX);
  342. return 0;
  343. }
  344. core_initcall(bsp_pm_check_init);
  345. static int msr_init_context(const u32 *msr_id, const int total_num)
  346. {
  347. int i = 0;
  348. struct saved_msr *msr_array;
  349. if (saved_context.saved_msrs.array || saved_context.saved_msrs.num > 0) {
  350. pr_err("x86/pm: MSR quirk already applied, please check your DMI match table.\n");
  351. return -EINVAL;
  352. }
  353. msr_array = kmalloc_array(total_num, sizeof(struct saved_msr), GFP_KERNEL);
  354. if (!msr_array) {
  355. pr_err("x86/pm: Can not allocate memory to save/restore MSRs during suspend.\n");
  356. return -ENOMEM;
  357. }
  358. for (i = 0; i < total_num; i++) {
  359. msr_array[i].info.msr_no = msr_id[i];
  360. msr_array[i].valid = false;
  361. msr_array[i].info.reg.q = 0;
  362. }
  363. saved_context.saved_msrs.num = total_num;
  364. saved_context.saved_msrs.array = msr_array;
  365. return 0;
  366. }
  367. /*
  368. * The following section is a quirk framework for problematic BIOSen:
  369. * Sometimes MSRs are modified by the BIOSen after suspended to
  370. * RAM, this might cause unexpected behavior after wakeup.
  371. * Thus we save/restore these specified MSRs across suspend/resume
  372. * in order to work around it.
  373. *
  374. * For any further problematic BIOSen/platforms,
  375. * please add your own function similar to msr_initialize_bdw.
  376. */
  377. static int msr_initialize_bdw(const struct dmi_system_id *d)
  378. {
  379. /* Add any extra MSR ids into this array. */
  380. u32 bdw_msr_id[] = { MSR_IA32_THERM_CONTROL };
  381. pr_info("x86/pm: %s detected, MSR saving is needed during suspending.\n", d->ident);
  382. return msr_init_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
  383. }
  384. static struct dmi_system_id msr_save_dmi_table[] = {
  385. {
  386. .callback = msr_initialize_bdw,
  387. .ident = "BROADWELL BDX_EP",
  388. .matches = {
  389. DMI_MATCH(DMI_PRODUCT_NAME, "GRANTLEY"),
  390. DMI_MATCH(DMI_PRODUCT_VERSION, "E63448-400"),
  391. },
  392. },
  393. {}
  394. };
  395. static int pm_check_save_msr(void)
  396. {
  397. dmi_check_system(msr_save_dmi_table);
  398. return 0;
  399. }
  400. device_initcall(pm_check_save_msr);