cpu.c 11 KB

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