Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * sleep.c - x86-specific ACPI sleep support. |
| 3 | * |
| 4 | * Copyright (C) 2001-2003 Patrick Mochel |
Pavel Machek | a253129 | 2010-07-18 14:27:13 +0200 | [diff] [blame] | 5 | * Copyright (C) 2001-2003 Pavel Machek <pavel@ucw.cz> |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/acpi.h> |
| 9 | #include <linux/bootmem.h> |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 10 | #include <linux/memblock.h> |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 11 | #include <linux/dmi.h> |
| 12 | #include <linux/cpumask.h> |
H. Peter Anvin | 4fdf08b | 2008-07-17 11:29:24 -0700 | [diff] [blame] | 13 | #include <asm/segment.h> |
Rafael J. Wysocki | 3038eda | 2008-10-17 01:26:27 +0200 | [diff] [blame] | 14 | #include <asm/desc.h> |
Borislav Petkov | b40827fa | 2010-08-28 15:58:33 +0200 | [diff] [blame] | 15 | #include <asm/pgtable.h> |
H. Peter Anvin | d344e38 | 2011-02-06 21:16:09 -0800 | [diff] [blame] | 16 | #include <asm/cacheflush.h> |
Jarkko Sakkinen | c9b77cc | 2012-05-08 21:22:29 +0300 | [diff] [blame] | 17 | #include <asm/realmode.h> |
Borislav Petkov | b40827fa | 2010-08-28 15:58:33 +0200 | [diff] [blame] | 18 | |
Todd E Brandt | 92f9e17 | 2016-03-02 16:05:29 -0800 | [diff] [blame] | 19 | #include <linux/ftrace.h> |
Jarkko Sakkinen | c484547 | 2012-05-08 21:22:42 +0300 | [diff] [blame] | 20 | #include "../../realmode/rm/wakeup.h" |
Pavel Machek | e44b7b7 | 2008-04-10 23:28:10 +0200 | [diff] [blame] | 21 | #include "sleep.h" |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 22 | |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 23 | unsigned long acpi_realmode_flags; |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 24 | |
Marcin Slusarz | 9744f5a | 2008-08-03 19:25:48 +0200 | [diff] [blame] | 25 | #if defined(CONFIG_SMP) && defined(CONFIG_64BIT) |
Matt Mackall | 5000cad | 2008-10-09 11:56:21 -0500 | [diff] [blame] | 26 | static char temp_stack[4096]; |
Pavel Machek | e44b7b7 | 2008-04-10 23:28:10 +0200 | [diff] [blame] | 27 | #endif |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 28 | |
| 29 | /** |
Lv Zheng | 40bce10 | 2013-10-31 09:31:18 +0800 | [diff] [blame] | 30 | * x86_acpi_enter_sleep_state - enter sleep state |
| 31 | * @state: Sleep state to enter. |
| 32 | * |
| 33 | * Wrapper around acpi_enter_sleep_state() to be called by assmebly. |
| 34 | */ |
Andi Kleen | 2605fc2 | 2014-05-02 00:44:37 +0200 | [diff] [blame] | 35 | acpi_status asmlinkage __visible x86_acpi_enter_sleep_state(u8 state) |
Lv Zheng | 40bce10 | 2013-10-31 09:31:18 +0800 | [diff] [blame] | 36 | { |
| 37 | return acpi_enter_sleep_state(state); |
| 38 | } |
| 39 | |
| 40 | /** |
Konrad Rzeszutek Wilk | d6a77ea | 2013-05-14 17:09:16 +0000 | [diff] [blame] | 41 | * x86_acpi_suspend_lowlevel - save kernel state |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 42 | * |
| 43 | * Create an identity mapped page table and copy the wakeup routine to |
| 44 | * low memory. |
| 45 | */ |
Konrad Rzeszutek Wilk | d6a77ea | 2013-05-14 17:09:16 +0000 | [diff] [blame] | 46 | int x86_acpi_suspend_lowlevel(void) |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 47 | { |
Jarkko Sakkinen | c9b77cc | 2012-05-08 21:22:29 +0300 | [diff] [blame] | 48 | struct wakeup_header *header = |
Jarkko Sakkinen | b429dbf | 2012-05-08 21:22:41 +0300 | [diff] [blame] | 49 | (struct wakeup_header *) __va(real_mode_header->wakeup_header); |
Pavel Machek | e44b7b7 | 2008-04-10 23:28:10 +0200 | [diff] [blame] | 50 | |
H. Peter Anvin | d1ee433 | 2011-02-14 15:42:46 -0800 | [diff] [blame] | 51 | if (header->signature != WAKEUP_HEADER_SIGNATURE) { |
Pavel Machek | e44b7b7 | 2008-04-10 23:28:10 +0200 | [diff] [blame] | 52 | printk(KERN_ERR "wakeup header does not match\n"); |
| 53 | return -EINVAL; |
| 54 | } |
| 55 | |
| 56 | header->video_mode = saved_video_mode; |
| 57 | |
H. Peter Anvin | 73201db | 2012-09-26 15:02:34 -0700 | [diff] [blame] | 58 | header->pmode_behavior = 0; |
| 59 | |
Pavel Machek | e44b7b7 | 2008-04-10 23:28:10 +0200 | [diff] [blame] | 60 | #ifndef CONFIG_64BIT |
Konrad Rzeszutek Wilk | 357d122 | 2013-04-05 16:42:23 -0400 | [diff] [blame] | 61 | native_store_gdt((struct desc_ptr *)&header->pmode_gdt); |
Pavel Machek | e44b7b7 | 2008-04-10 23:28:10 +0200 | [diff] [blame] | 62 | |
H. Peter Anvin | 5ff560f | 2013-07-12 16:48:12 -0700 | [diff] [blame] | 63 | /* |
| 64 | * We have to check that we can write back the value, and not |
| 65 | * just read it. At least on 90 nm Pentium M (Family 6, Model |
| 66 | * 13), reading an invalid MSR is not guaranteed to trap, see |
| 67 | * Erratum X4 in "Intel Pentium M Processor on 90 nm Process |
| 68 | * with 2-MB L2 Cache and IntelĀ® Processor A100 and A110 on 90 |
| 69 | * nm process with 512-KB L2 Cache Specification Update". |
| 70 | */ |
H. Peter Anvin | 73201db | 2012-09-26 15:02:34 -0700 | [diff] [blame] | 71 | if (!rdmsr_safe(MSR_EFER, |
| 72 | &header->pmode_efer_low, |
H. Peter Anvin | 5ff560f | 2013-07-12 16:48:12 -0700 | [diff] [blame] | 73 | &header->pmode_efer_high) && |
| 74 | !wrmsr_safe(MSR_EFER, |
| 75 | header->pmode_efer_low, |
| 76 | header->pmode_efer_high)) |
H. Peter Anvin | 73201db | 2012-09-26 15:02:34 -0700 | [diff] [blame] | 77 | header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_EFER); |
Pavel Machek | e44b7b7 | 2008-04-10 23:28:10 +0200 | [diff] [blame] | 78 | #endif /* !CONFIG_64BIT */ |
| 79 | |
| 80 | header->pmode_cr0 = read_cr0(); |
H. Peter Anvin | 73201db | 2012-09-26 15:02:34 -0700 | [diff] [blame] | 81 | if (__this_cpu_read(cpu_info.cpuid_level) >= 0) { |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 82 | header->pmode_cr4 = __read_cr4(); |
H. Peter Anvin | 73201db | 2012-09-26 15:02:34 -0700 | [diff] [blame] | 83 | header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_CR4); |
| 84 | } |
Kees Cook | 7a31366 | 2011-07-06 18:10:34 -0700 | [diff] [blame] | 85 | if (!rdmsr_safe(MSR_IA32_MISC_ENABLE, |
| 86 | &header->pmode_misc_en_low, |
H. Peter Anvin | 5ff560f | 2013-07-12 16:48:12 -0700 | [diff] [blame] | 87 | &header->pmode_misc_en_high) && |
| 88 | !wrmsr_safe(MSR_IA32_MISC_ENABLE, |
| 89 | header->pmode_misc_en_low, |
| 90 | header->pmode_misc_en_high)) |
Kees Cook | 7a31366 | 2011-07-06 18:10:34 -0700 | [diff] [blame] | 91 | header->pmode_behavior |= |
| 92 | (1 << WAKEUP_BEHAVIOR_RESTORE_MISC_ENABLE); |
Pavel Machek | e44b7b7 | 2008-04-10 23:28:10 +0200 | [diff] [blame] | 93 | header->realmode_flags = acpi_realmode_flags; |
| 94 | header->real_magic = 0x12345678; |
| 95 | |
| 96 | #ifndef CONFIG_64BIT |
| 97 | header->pmode_entry = (u32)&wakeup_pmode_return; |
Alexander Duyck | afd51a0 | 2012-11-16 13:57:43 -0800 | [diff] [blame] | 98 | header->pmode_cr3 = (u32)__pa_symbol(initial_page_table); |
Pavel Machek | e44b7b7 | 2008-04-10 23:28:10 +0200 | [diff] [blame] | 99 | saved_magic = 0x12345678; |
| 100 | #else /* CONFIG_64BIT */ |
Ingo Molnar | 1ea598c | 2008-06-13 20:31:54 +0200 | [diff] [blame] | 101 | #ifdef CONFIG_SMP |
Josh Poimboeuf | b32f96c | 2016-08-18 10:59:03 -0500 | [diff] [blame] | 102 | initial_stack = (unsigned long)temp_stack + sizeof(temp_stack); |
Rafael J. Wysocki | 3038eda | 2008-10-17 01:26:27 +0200 | [diff] [blame] | 103 | early_gdt_descr.address = |
| 104 | (unsigned long)get_cpu_gdt_table(smp_processor_id()); |
Tejun Heo | 004aa32 | 2009-01-13 20:41:35 +0900 | [diff] [blame] | 105 | initial_gs = per_cpu_offset(smp_processor_id()); |
Ingo Molnar | 1ea598c | 2008-06-13 20:31:54 +0200 | [diff] [blame] | 106 | #endif |
Pavel Machek | e44b7b7 | 2008-04-10 23:28:10 +0200 | [diff] [blame] | 107 | initial_code = (unsigned long)wakeup_long64; |
Jaswinder Singh Rajput | ce4b3c5 | 2009-04-18 13:44:57 +0200 | [diff] [blame] | 108 | saved_magic = 0x123456789abcdef0L; |
Pavel Machek | e44b7b7 | 2008-04-10 23:28:10 +0200 | [diff] [blame] | 109 | #endif /* CONFIG_64BIT */ |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 110 | |
Todd E Brandt | 92f9e17 | 2016-03-02 16:05:29 -0800 | [diff] [blame] | 111 | /* |
| 112 | * Pause/unpause graph tracing around do_suspend_lowlevel as it has |
| 113 | * inconsistent call/return info after it jumps to the wakeup vector. |
| 114 | */ |
| 115 | pause_graph_tracing(); |
Rafael J. Wysocki | f1a2003 | 2011-02-08 23:42:22 +0100 | [diff] [blame] | 116 | do_suspend_lowlevel(); |
Todd E Brandt | 92f9e17 | 2016-03-02 16:05:29 -0800 | [diff] [blame] | 117 | unpause_graph_tracing(); |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 121 | static int __init acpi_sleep_setup(char *str) |
| 122 | { |
| 123 | while ((str != NULL) && (*str != '\0')) { |
| 124 | if (strncmp(str, "s3_bios", 7) == 0) |
| 125 | acpi_realmode_flags |= 1; |
| 126 | if (strncmp(str, "s3_mode", 7) == 0) |
| 127 | acpi_realmode_flags |= 2; |
| 128 | if (strncmp(str, "s3_beep", 7) == 0) |
| 129 | acpi_realmode_flags |= 4; |
Shaohua Li | bdfe6b7 | 2008-07-23 21:28:41 -0700 | [diff] [blame] | 130 | #ifdef CONFIG_HIBERNATION |
| 131 | if (strncmp(str, "s4_nohwsig", 10) == 0) |
| 132 | acpi_no_s4_hw_signature(); |
| 133 | #endif |
Rafael J. Wysocki | 72ad5d7 | 2010-07-23 22:59:09 +0200 | [diff] [blame] | 134 | if (strncmp(str, "nonvs", 5) == 0) |
| 135 | acpi_nvs_nosave(); |
Kristen Carlson Accardi | 1bad2f1 | 2012-10-26 13:39:15 +0200 | [diff] [blame] | 136 | if (strncmp(str, "nonvs_s3", 8) == 0) |
| 137 | acpi_nvs_nosave_s3(); |
Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 138 | if (strncmp(str, "old_ordering", 12) == 0) |
| 139 | acpi_old_suspend_ordering(); |
Pavel Machek | 4fc2fba | 2008-01-30 13:32:54 +0100 | [diff] [blame] | 140 | str = strchr(str, ','); |
| 141 | if (str != NULL) |
| 142 | str += strspn(str, ", \t"); |
| 143 | } |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | __setup("acpi_sleep=", acpi_sleep_setup); |