cpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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_table(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_table(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. }
  167. /**
  168. * __restore_processor_state - restore the contents of CPU registers saved
  169. * by __save_processor_state()
  170. * @ctxt - structure to load the registers contents from
  171. */
  172. static void notrace __restore_processor_state(struct saved_context *ctxt)
  173. {
  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_cr8(ctxt->cr8);
  187. __write_cr4(ctxt->cr4);
  188. #endif
  189. write_cr3(ctxt->cr3);
  190. write_cr2(ctxt->cr2);
  191. write_cr0(ctxt->cr0);
  192. /*
  193. * now restore the descriptor tables to their proper values
  194. * ltr is done i fix_processor_context().
  195. */
  196. #ifdef CONFIG_X86_32
  197. load_idt(&ctxt->idt);
  198. #else
  199. /* CONFIG_X86_64 */
  200. load_idt((const struct desc_ptr *)&ctxt->idt_limit);
  201. #endif
  202. /*
  203. * segment registers
  204. */
  205. #ifdef CONFIG_X86_32
  206. loadsegment(es, ctxt->es);
  207. loadsegment(fs, ctxt->fs);
  208. loadsegment(gs, ctxt->gs);
  209. loadsegment(ss, ctxt->ss);
  210. /*
  211. * sysenter MSRs
  212. */
  213. if (boot_cpu_has(X86_FEATURE_SEP))
  214. enable_sep_cpu();
  215. #else
  216. /* CONFIG_X86_64 */
  217. asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
  218. asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
  219. asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
  220. load_gs_index(ctxt->gs);
  221. asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
  222. wrmsrl(MSR_FS_BASE, ctxt->fs_base);
  223. wrmsrl(MSR_GS_BASE, ctxt->gs_base);
  224. wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
  225. #endif
  226. fix_processor_context();
  227. do_fpu_end();
  228. tsc_verify_tsc_adjust(true);
  229. x86_platform.restore_sched_clock_state();
  230. mtrr_bp_restore();
  231. perf_restore_debug_store();
  232. msr_restore_context(ctxt);
  233. }
  234. /* Needed by apm.c */
  235. void notrace restore_processor_state(void)
  236. {
  237. __restore_processor_state(&saved_context);
  238. }
  239. #ifdef CONFIG_X86_32
  240. EXPORT_SYMBOL(restore_processor_state);
  241. #endif
  242. #if defined(CONFIG_HIBERNATION) && defined(CONFIG_HOTPLUG_CPU)
  243. static void resume_play_dead(void)
  244. {
  245. play_dead_common();
  246. tboot_shutdown(TB_SHUTDOWN_WFS);
  247. hlt_play_dead();
  248. }
  249. int hibernate_resume_nonboot_cpu_disable(void)
  250. {
  251. void (*play_dead)(void) = smp_ops.play_dead;
  252. int ret;
  253. /*
  254. * Ensure that MONITOR/MWAIT will not be used in the "play dead" loop
  255. * during hibernate image restoration, because it is likely that the
  256. * monitored address will be actually written to at that time and then
  257. * the "dead" CPU will attempt to execute instructions again, but the
  258. * address in its instruction pointer may not be possible to resolve
  259. * any more at that point (the page tables used by it previously may
  260. * have been overwritten by hibernate image data).
  261. */
  262. smp_ops.play_dead = resume_play_dead;
  263. ret = disable_nonboot_cpus();
  264. smp_ops.play_dead = play_dead;
  265. return ret;
  266. }
  267. #endif
  268. /*
  269. * When bsp_check() is called in hibernate and suspend, cpu hotplug
  270. * is disabled already. So it's unnessary to handle race condition between
  271. * cpumask query and cpu hotplug.
  272. */
  273. static int bsp_check(void)
  274. {
  275. if (cpumask_first(cpu_online_mask) != 0) {
  276. pr_warn("CPU0 is offline.\n");
  277. return -ENODEV;
  278. }
  279. return 0;
  280. }
  281. static int bsp_pm_callback(struct notifier_block *nb, unsigned long action,
  282. void *ptr)
  283. {
  284. int ret = 0;
  285. switch (action) {
  286. case PM_SUSPEND_PREPARE:
  287. case PM_HIBERNATION_PREPARE:
  288. ret = bsp_check();
  289. break;
  290. #ifdef CONFIG_DEBUG_HOTPLUG_CPU0
  291. case PM_RESTORE_PREPARE:
  292. /*
  293. * When system resumes from hibernation, online CPU0 because
  294. * 1. it's required for resume and
  295. * 2. the CPU was online before hibernation
  296. */
  297. if (!cpu_online(0))
  298. _debug_hotplug_cpu(0, 1);
  299. break;
  300. case PM_POST_RESTORE:
  301. /*
  302. * When a resume really happens, this code won't be called.
  303. *
  304. * This code is called only when user space hibernation software
  305. * prepares for snapshot device during boot time. So we just
  306. * call _debug_hotplug_cpu() to restore to CPU0's state prior to
  307. * preparing the snapshot device.
  308. *
  309. * This works for normal boot case in our CPU0 hotplug debug
  310. * mode, i.e. CPU0 is offline and user mode hibernation
  311. * software initializes during boot time.
  312. *
  313. * If CPU0 is online and user application accesses snapshot
  314. * device after boot time, this will offline CPU0 and user may
  315. * see different CPU0 state before and after accessing
  316. * the snapshot device. But hopefully this is not a case when
  317. * user debugging CPU0 hotplug. Even if users hit this case,
  318. * they can easily online CPU0 back.
  319. *
  320. * To simplify this debug code, we only consider normal boot
  321. * case. Otherwise we need to remember CPU0's state and restore
  322. * to that state and resolve racy conditions etc.
  323. */
  324. _debug_hotplug_cpu(0, 0);
  325. break;
  326. #endif
  327. default:
  328. break;
  329. }
  330. return notifier_from_errno(ret);
  331. }
  332. static int __init bsp_pm_check_init(void)
  333. {
  334. /*
  335. * Set this bsp_pm_callback as lower priority than
  336. * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called
  337. * earlier to disable cpu hotplug before bsp online check.
  338. */
  339. pm_notifier(bsp_pm_callback, -INT_MAX);
  340. return 0;
  341. }
  342. core_initcall(bsp_pm_check_init);
  343. static int msr_init_context(const u32 *msr_id, const int total_num)
  344. {
  345. int i = 0;
  346. struct saved_msr *msr_array;
  347. if (saved_context.saved_msrs.array || saved_context.saved_msrs.num > 0) {
  348. pr_err("x86/pm: MSR quirk already applied, please check your DMI match table.\n");
  349. return -EINVAL;
  350. }
  351. msr_array = kmalloc_array(total_num, sizeof(struct saved_msr), GFP_KERNEL);
  352. if (!msr_array) {
  353. pr_err("x86/pm: Can not allocate memory to save/restore MSRs during suspend.\n");
  354. return -ENOMEM;
  355. }
  356. for (i = 0; i < total_num; i++) {
  357. msr_array[i].info.msr_no = msr_id[i];
  358. msr_array[i].valid = false;
  359. msr_array[i].info.reg.q = 0;
  360. }
  361. saved_context.saved_msrs.num = total_num;
  362. saved_context.saved_msrs.array = msr_array;
  363. return 0;
  364. }
  365. /*
  366. * The following section is a quirk framework for problematic BIOSen:
  367. * Sometimes MSRs are modified by the BIOSen after suspended to
  368. * RAM, this might cause unexpected behavior after wakeup.
  369. * Thus we save/restore these specified MSRs across suspend/resume
  370. * in order to work around it.
  371. *
  372. * For any further problematic BIOSen/platforms,
  373. * please add your own function similar to msr_initialize_bdw.
  374. */
  375. static int msr_initialize_bdw(const struct dmi_system_id *d)
  376. {
  377. /* Add any extra MSR ids into this array. */
  378. u32 bdw_msr_id[] = { MSR_IA32_THERM_CONTROL };
  379. pr_info("x86/pm: %s detected, MSR saving is needed during suspending.\n", d->ident);
  380. return msr_init_context(bdw_msr_id, ARRAY_SIZE(bdw_msr_id));
  381. }
  382. static struct dmi_system_id msr_save_dmi_table[] = {
  383. {
  384. .callback = msr_initialize_bdw,
  385. .ident = "BROADWELL BDX_EP",
  386. .matches = {
  387. DMI_MATCH(DMI_PRODUCT_NAME, "GRANTLEY"),
  388. DMI_MATCH(DMI_PRODUCT_VERSION, "E63448-400"),
  389. },
  390. },
  391. {}
  392. };
  393. static int pm_check_save_msr(void)
  394. {
  395. dmi_check_system(msr_save_dmi_table);
  396. return 0;
  397. }
  398. device_initcall(pm_check_save_msr);