108687aecSSergio Luis /* 208687aecSSergio Luis * Suspend support specific for i386/x86-64. 308687aecSSergio Luis * 408687aecSSergio Luis * Distribute under GPLv2 508687aecSSergio Luis * 608687aecSSergio Luis * Copyright (c) 2007 Rafael J. Wysocki <rjw@sisk.pl> 7a2531293SPavel Machek * Copyright (c) 2002 Pavel Machek <pavel@ucw.cz> 808687aecSSergio Luis * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org> 908687aecSSergio Luis */ 1008687aecSSergio Luis 1108687aecSSergio Luis #include <linux/suspend.h> 1269c60c88SPaul Gortmaker #include <linux/export.h> 1308687aecSSergio Luis #include <linux/smp.h> 141d9d8639SStephane Eranian #include <linux/perf_event.h> 1508687aecSSergio Luis 1608687aecSSergio Luis #include <asm/pgtable.h> 1708687aecSSergio Luis #include <asm/proto.h> 1808687aecSSergio Luis #include <asm/mtrr.h> 1908687aecSSergio Luis #include <asm/page.h> 2008687aecSSergio Luis #include <asm/mce.h> 2108687aecSSergio Luis #include <asm/suspend.h> 22eadb8a09SIngo Molnar #include <asm/debugreg.h> 23a71c8bc5SFenghua Yu #include <asm/cpu.h> 2408687aecSSergio Luis 2508687aecSSergio Luis #ifdef CONFIG_X86_32 26d6efc2f7SAndi Kleen __visible unsigned long saved_context_ebx; 27d6efc2f7SAndi Kleen __visible unsigned long saved_context_esp, saved_context_ebp; 28d6efc2f7SAndi Kleen __visible unsigned long saved_context_esi, saved_context_edi; 29d6efc2f7SAndi Kleen __visible unsigned long saved_context_eflags; 3008687aecSSergio Luis #endif 31cc456c4eSKonrad Rzeszutek Wilk struct saved_context saved_context; 3208687aecSSergio Luis 3308687aecSSergio Luis /** 3408687aecSSergio Luis * __save_processor_state - save CPU registers before creating a 3508687aecSSergio Luis * hibernation image and before restoring the memory state from it 3608687aecSSergio Luis * @ctxt - structure to store the registers contents in 3708687aecSSergio Luis * 3808687aecSSergio Luis * NOTE: If there is a CPU register the modification of which by the 3908687aecSSergio Luis * boot kernel (ie. the kernel used for loading the hibernation image) 4008687aecSSergio Luis * might affect the operations of the restored target kernel (ie. the one 4108687aecSSergio Luis * saved in the hibernation image), then its contents must be saved by this 4208687aecSSergio Luis * function. In other words, if kernel A is hibernated and different 4308687aecSSergio Luis * kernel B is used for loading the hibernation image into memory, the 4408687aecSSergio Luis * kernel A's __save_processor_state() function must save all registers 4508687aecSSergio Luis * needed by kernel A, so that it can operate correctly after the resume 4608687aecSSergio Luis * regardless of what kernel B does in the meantime. 4708687aecSSergio Luis */ 4808687aecSSergio Luis static void __save_processor_state(struct saved_context *ctxt) 4908687aecSSergio Luis { 5008687aecSSergio Luis #ifdef CONFIG_X86_32 5108687aecSSergio Luis mtrr_save_fixed_ranges(NULL); 5208687aecSSergio Luis #endif 5308687aecSSergio Luis kernel_fpu_begin(); 5408687aecSSergio Luis 5508687aecSSergio Luis /* 5608687aecSSergio Luis * descriptor tables 5708687aecSSergio Luis */ 5808687aecSSergio Luis #ifdef CONFIG_X86_32 5908687aecSSergio Luis store_idt(&ctxt->idt); 6008687aecSSergio Luis #else 6108687aecSSergio Luis /* CONFIG_X86_64 */ 6208687aecSSergio Luis store_idt((struct desc_ptr *)&ctxt->idt_limit); 6308687aecSSergio Luis #endif 64cc456c4eSKonrad Rzeszutek Wilk /* 65cc456c4eSKonrad Rzeszutek Wilk * We save it here, but restore it only in the hibernate case. 66cc456c4eSKonrad Rzeszutek Wilk * For ACPI S3 resume, this is loaded via 'early_gdt_desc' in 64-bit 67cc456c4eSKonrad Rzeszutek Wilk * mode in "secondary_startup_64". In 32-bit mode it is done via 68cc456c4eSKonrad Rzeszutek Wilk * 'pmode_gdt' in wakeup_start. 69cc456c4eSKonrad Rzeszutek Wilk */ 70cc456c4eSKonrad Rzeszutek Wilk ctxt->gdt_desc.size = GDT_SIZE - 1; 71cc456c4eSKonrad Rzeszutek Wilk ctxt->gdt_desc.address = (unsigned long)get_cpu_gdt_table(smp_processor_id()); 72cc456c4eSKonrad Rzeszutek Wilk 7308687aecSSergio Luis store_tr(ctxt->tr); 7408687aecSSergio Luis 7508687aecSSergio Luis /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */ 7608687aecSSergio Luis /* 7708687aecSSergio Luis * segment registers 7808687aecSSergio Luis */ 7908687aecSSergio Luis #ifdef CONFIG_X86_32 8008687aecSSergio Luis savesegment(es, ctxt->es); 8108687aecSSergio Luis savesegment(fs, ctxt->fs); 8208687aecSSergio Luis savesegment(gs, ctxt->gs); 8308687aecSSergio Luis savesegment(ss, ctxt->ss); 8408687aecSSergio Luis #else 8508687aecSSergio Luis /* CONFIG_X86_64 */ 8608687aecSSergio Luis asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds)); 8708687aecSSergio Luis asm volatile ("movw %%es, %0" : "=m" (ctxt->es)); 8808687aecSSergio Luis asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs)); 8908687aecSSergio Luis asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs)); 9008687aecSSergio Luis asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss)); 9108687aecSSergio Luis 9208687aecSSergio Luis rdmsrl(MSR_FS_BASE, ctxt->fs_base); 9308687aecSSergio Luis rdmsrl(MSR_GS_BASE, ctxt->gs_base); 9408687aecSSergio Luis rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base); 9508687aecSSergio Luis mtrr_save_fixed_ranges(NULL); 9608687aecSSergio Luis 9708687aecSSergio Luis rdmsrl(MSR_EFER, ctxt->efer); 9808687aecSSergio Luis #endif 9908687aecSSergio Luis 10008687aecSSergio Luis /* 10108687aecSSergio Luis * control registers 10208687aecSSergio Luis */ 10308687aecSSergio Luis ctxt->cr0 = read_cr0(); 10408687aecSSergio Luis ctxt->cr2 = read_cr2(); 10508687aecSSergio Luis ctxt->cr3 = read_cr3(); 1061e02ce4cSAndy Lutomirski ctxt->cr4 = __read_cr4_safe(); 1071e02ce4cSAndy Lutomirski #ifdef CONFIG_X86_64 10808687aecSSergio Luis ctxt->cr8 = read_cr8(); 10908687aecSSergio Luis #endif 11085a0e753SOndrej Zary ctxt->misc_enable_saved = !rdmsrl_safe(MSR_IA32_MISC_ENABLE, 11185a0e753SOndrej Zary &ctxt->misc_enable); 11208687aecSSergio Luis } 11308687aecSSergio Luis 11408687aecSSergio Luis /* Needed by apm.c */ 11508687aecSSergio Luis void save_processor_state(void) 11608687aecSSergio Luis { 11708687aecSSergio Luis __save_processor_state(&saved_context); 118b74f05d6SMarcelo Tosatti x86_platform.save_sched_clock_state(); 11908687aecSSergio Luis } 12008687aecSSergio Luis #ifdef CONFIG_X86_32 12108687aecSSergio Luis EXPORT_SYMBOL(save_processor_state); 12208687aecSSergio Luis #endif 12308687aecSSergio Luis 12408687aecSSergio Luis static void do_fpu_end(void) 12508687aecSSergio Luis { 12608687aecSSergio Luis /* 12708687aecSSergio Luis * Restore FPU regs if necessary. 12808687aecSSergio Luis */ 12908687aecSSergio Luis kernel_fpu_end(); 13008687aecSSergio Luis } 13108687aecSSergio Luis 13208687aecSSergio Luis static void fix_processor_context(void) 13308687aecSSergio Luis { 13408687aecSSergio Luis int cpu = smp_processor_id(); 13524933b82SAndy Lutomirski struct tss_struct *t = &per_cpu(cpu_tss, cpu); 1364d681be3Skonrad@kernel.org #ifdef CONFIG_X86_64 1374d681be3Skonrad@kernel.org struct desc_struct *desc = get_cpu_gdt_table(cpu); 1384d681be3Skonrad@kernel.org tss_desc tss; 1394d681be3Skonrad@kernel.org #endif 14008687aecSSergio Luis set_tss_desc(cpu, t); /* 14108687aecSSergio Luis * This just modifies memory; should not be 14208687aecSSergio Luis * necessary. But... This is necessary, because 14308687aecSSergio Luis * 386 hardware has concept of busy TSS or some 14408687aecSSergio Luis * similar stupidity. 14508687aecSSergio Luis */ 14608687aecSSergio Luis 14708687aecSSergio Luis #ifdef CONFIG_X86_64 1484d681be3Skonrad@kernel.org memcpy(&tss, &desc[GDT_ENTRY_TSS], sizeof(tss_desc)); 1494d681be3Skonrad@kernel.org tss.type = 0x9; /* The available 64-bit TSS (see AMD vol 2, pg 91 */ 1504d681be3Skonrad@kernel.org write_gdt_entry(desc, GDT_ENTRY_TSS, &tss, DESC_TSS); 15108687aecSSergio Luis 15208687aecSSergio Luis syscall_init(); /* This sets MSR_*STAR and related */ 15308687aecSSergio Luis #endif 15408687aecSSergio Luis load_TR_desc(); /* This does ltr */ 15508687aecSSergio Luis load_LDT(¤t->active_mm->context); /* This does lldt */ 156*9254aaa0SIngo Molnar 157*9254aaa0SIngo Molnar fpu__resume_cpu(); 15808687aecSSergio Luis } 15908687aecSSergio Luis 16008687aecSSergio Luis /** 16108687aecSSergio Luis * __restore_processor_state - restore the contents of CPU registers saved 16208687aecSSergio Luis * by __save_processor_state() 16308687aecSSergio Luis * @ctxt - structure to load the registers contents from 16408687aecSSergio Luis */ 165b8f99b3eSSteven Rostedt (Red Hat) static void notrace __restore_processor_state(struct saved_context *ctxt) 16608687aecSSergio Luis { 16785a0e753SOndrej Zary if (ctxt->misc_enable_saved) 16885a0e753SOndrej Zary wrmsrl(MSR_IA32_MISC_ENABLE, ctxt->misc_enable); 16908687aecSSergio Luis /* 17008687aecSSergio Luis * control registers 17108687aecSSergio Luis */ 17208687aecSSergio Luis /* cr4 was introduced in the Pentium CPU */ 17308687aecSSergio Luis #ifdef CONFIG_X86_32 17408687aecSSergio Luis if (ctxt->cr4) 1751e02ce4cSAndy Lutomirski __write_cr4(ctxt->cr4); 17608687aecSSergio Luis #else 17708687aecSSergio Luis /* CONFIG X86_64 */ 17808687aecSSergio Luis wrmsrl(MSR_EFER, ctxt->efer); 17908687aecSSergio Luis write_cr8(ctxt->cr8); 1801e02ce4cSAndy Lutomirski __write_cr4(ctxt->cr4); 18108687aecSSergio Luis #endif 18208687aecSSergio Luis write_cr3(ctxt->cr3); 18308687aecSSergio Luis write_cr2(ctxt->cr2); 18408687aecSSergio Luis write_cr0(ctxt->cr0); 18508687aecSSergio Luis 18608687aecSSergio Luis /* 18708687aecSSergio Luis * now restore the descriptor tables to their proper values 18808687aecSSergio Luis * ltr is done i fix_processor_context(). 18908687aecSSergio Luis */ 19008687aecSSergio Luis #ifdef CONFIG_X86_32 19108687aecSSergio Luis load_idt(&ctxt->idt); 19208687aecSSergio Luis #else 19308687aecSSergio Luis /* CONFIG_X86_64 */ 19408687aecSSergio Luis load_idt((const struct desc_ptr *)&ctxt->idt_limit); 19508687aecSSergio Luis #endif 19608687aecSSergio Luis 19708687aecSSergio Luis /* 19808687aecSSergio Luis * segment registers 19908687aecSSergio Luis */ 20008687aecSSergio Luis #ifdef CONFIG_X86_32 20108687aecSSergio Luis loadsegment(es, ctxt->es); 20208687aecSSergio Luis loadsegment(fs, ctxt->fs); 20308687aecSSergio Luis loadsegment(gs, ctxt->gs); 20408687aecSSergio Luis loadsegment(ss, ctxt->ss); 20508687aecSSergio Luis 20608687aecSSergio Luis /* 20708687aecSSergio Luis * sysenter MSRs 20808687aecSSergio Luis */ 20908687aecSSergio Luis if (boot_cpu_has(X86_FEATURE_SEP)) 21008687aecSSergio Luis enable_sep_cpu(); 21108687aecSSergio Luis #else 21208687aecSSergio Luis /* CONFIG_X86_64 */ 21308687aecSSergio Luis asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds)); 21408687aecSSergio Luis asm volatile ("movw %0, %%es" :: "r" (ctxt->es)); 21508687aecSSergio Luis asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs)); 21608687aecSSergio Luis load_gs_index(ctxt->gs); 21708687aecSSergio Luis asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss)); 21808687aecSSergio Luis 21908687aecSSergio Luis wrmsrl(MSR_FS_BASE, ctxt->fs_base); 22008687aecSSergio Luis wrmsrl(MSR_GS_BASE, ctxt->gs_base); 22108687aecSSergio Luis wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base); 22208687aecSSergio Luis #endif 22308687aecSSergio Luis 22408687aecSSergio Luis fix_processor_context(); 22508687aecSSergio Luis 22608687aecSSergio Luis do_fpu_end(); 227dba69d10SMarcelo Tosatti x86_platform.restore_sched_clock_state(); 228d0af9eedSSuresh Siddha mtrr_bp_restore(); 2291d9d8639SStephane Eranian perf_restore_debug_store(); 23008687aecSSergio Luis } 23108687aecSSergio Luis 23208687aecSSergio Luis /* Needed by apm.c */ 233b8f99b3eSSteven Rostedt (Red Hat) void notrace restore_processor_state(void) 23408687aecSSergio Luis { 23508687aecSSergio Luis __restore_processor_state(&saved_context); 23608687aecSSergio Luis } 23708687aecSSergio Luis #ifdef CONFIG_X86_32 23808687aecSSergio Luis EXPORT_SYMBOL(restore_processor_state); 23908687aecSSergio Luis #endif 240209efae1SFenghua Yu 241209efae1SFenghua Yu /* 242209efae1SFenghua Yu * When bsp_check() is called in hibernate and suspend, cpu hotplug 243209efae1SFenghua Yu * is disabled already. So it's unnessary to handle race condition between 244209efae1SFenghua Yu * cpumask query and cpu hotplug. 245209efae1SFenghua Yu */ 246209efae1SFenghua Yu static int bsp_check(void) 247209efae1SFenghua Yu { 248209efae1SFenghua Yu if (cpumask_first(cpu_online_mask) != 0) { 249209efae1SFenghua Yu pr_warn("CPU0 is offline.\n"); 250209efae1SFenghua Yu return -ENODEV; 251209efae1SFenghua Yu } 252209efae1SFenghua Yu 253209efae1SFenghua Yu return 0; 254209efae1SFenghua Yu } 255209efae1SFenghua Yu 256209efae1SFenghua Yu static int bsp_pm_callback(struct notifier_block *nb, unsigned long action, 257209efae1SFenghua Yu void *ptr) 258209efae1SFenghua Yu { 259209efae1SFenghua Yu int ret = 0; 260209efae1SFenghua Yu 261209efae1SFenghua Yu switch (action) { 262209efae1SFenghua Yu case PM_SUSPEND_PREPARE: 263209efae1SFenghua Yu case PM_HIBERNATION_PREPARE: 264209efae1SFenghua Yu ret = bsp_check(); 265209efae1SFenghua Yu break; 266a71c8bc5SFenghua Yu #ifdef CONFIG_DEBUG_HOTPLUG_CPU0 267a71c8bc5SFenghua Yu case PM_RESTORE_PREPARE: 268a71c8bc5SFenghua Yu /* 269a71c8bc5SFenghua Yu * When system resumes from hibernation, online CPU0 because 270a71c8bc5SFenghua Yu * 1. it's required for resume and 271a71c8bc5SFenghua Yu * 2. the CPU was online before hibernation 272a71c8bc5SFenghua Yu */ 273a71c8bc5SFenghua Yu if (!cpu_online(0)) 274a71c8bc5SFenghua Yu _debug_hotplug_cpu(0, 1); 275a71c8bc5SFenghua Yu break; 276a71c8bc5SFenghua Yu case PM_POST_RESTORE: 277a71c8bc5SFenghua Yu /* 278a71c8bc5SFenghua Yu * When a resume really happens, this code won't be called. 279a71c8bc5SFenghua Yu * 280a71c8bc5SFenghua Yu * This code is called only when user space hibernation software 281a71c8bc5SFenghua Yu * prepares for snapshot device during boot time. So we just 282a71c8bc5SFenghua Yu * call _debug_hotplug_cpu() to restore to CPU0's state prior to 283a71c8bc5SFenghua Yu * preparing the snapshot device. 284a71c8bc5SFenghua Yu * 285a71c8bc5SFenghua Yu * This works for normal boot case in our CPU0 hotplug debug 286a71c8bc5SFenghua Yu * mode, i.e. CPU0 is offline and user mode hibernation 287a71c8bc5SFenghua Yu * software initializes during boot time. 288a71c8bc5SFenghua Yu * 289a71c8bc5SFenghua Yu * If CPU0 is online and user application accesses snapshot 290a71c8bc5SFenghua Yu * device after boot time, this will offline CPU0 and user may 291a71c8bc5SFenghua Yu * see different CPU0 state before and after accessing 292a71c8bc5SFenghua Yu * the snapshot device. But hopefully this is not a case when 293a71c8bc5SFenghua Yu * user debugging CPU0 hotplug. Even if users hit this case, 294a71c8bc5SFenghua Yu * they can easily online CPU0 back. 295a71c8bc5SFenghua Yu * 296a71c8bc5SFenghua Yu * To simplify this debug code, we only consider normal boot 297a71c8bc5SFenghua Yu * case. Otherwise we need to remember CPU0's state and restore 298a71c8bc5SFenghua Yu * to that state and resolve racy conditions etc. 299a71c8bc5SFenghua Yu */ 300a71c8bc5SFenghua Yu _debug_hotplug_cpu(0, 0); 301a71c8bc5SFenghua Yu break; 302a71c8bc5SFenghua Yu #endif 303209efae1SFenghua Yu default: 304209efae1SFenghua Yu break; 305209efae1SFenghua Yu } 306209efae1SFenghua Yu return notifier_from_errno(ret); 307209efae1SFenghua Yu } 308209efae1SFenghua Yu 309209efae1SFenghua Yu static int __init bsp_pm_check_init(void) 310209efae1SFenghua Yu { 311209efae1SFenghua Yu /* 312209efae1SFenghua Yu * Set this bsp_pm_callback as lower priority than 313209efae1SFenghua Yu * cpu_hotplug_pm_callback. So cpu_hotplug_pm_callback will be called 314209efae1SFenghua Yu * earlier to disable cpu hotplug before bsp online check. 315209efae1SFenghua Yu */ 316209efae1SFenghua Yu pm_notifier(bsp_pm_callback, -INT_MAX); 317209efae1SFenghua Yu return 0; 318209efae1SFenghua Yu } 319209efae1SFenghua Yu 320209efae1SFenghua Yu core_initcall(bsp_pm_check_init); 321