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