blob: 48587335ede8e2296b80ff991d1bf4e8e155ad46 [file] [log] [blame]
Pavel Machek4fc2fba2008-01-30 13:32:54 +01001/*
2 * sleep.c - x86-specific ACPI sleep support.
3 *
4 * Copyright (C) 2001-2003 Patrick Mochel
Pavel Macheka2531292010-07-18 14:27:13 +02005 * Copyright (C) 2001-2003 Pavel Machek <pavel@ucw.cz>
Pavel Machek4fc2fba2008-01-30 13:32:54 +01006 */
7
8#include <linux/acpi.h>
9#include <linux/bootmem.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070010#include <linux/memblock.h>
Pavel Machek4fc2fba2008-01-30 13:32:54 +010011#include <linux/dmi.h>
12#include <linux/cpumask.h>
H. Peter Anvin4fdf08b2008-07-17 11:29:24 -070013#include <asm/segment.h>
Rafael J. Wysocki3038eda2008-10-17 01:26:27 +020014#include <asm/desc.h>
Borislav Petkovb40827fa2010-08-28 15:58:33 +020015#include <asm/pgtable.h>
H. Peter Anvind344e382011-02-06 21:16:09 -080016#include <asm/cacheflush.h>
Jarkko Sakkinenc9b77cc2012-05-08 21:22:29 +030017#include <asm/realmode.h>
Borislav Petkovb40827fa2010-08-28 15:58:33 +020018
Todd E Brandt92f9e172016-03-02 16:05:29 -080019#include <linux/ftrace.h>
Jarkko Sakkinenc4845472012-05-08 21:22:42 +030020#include "../../realmode/rm/wakeup.h"
Pavel Macheke44b7b72008-04-10 23:28:10 +020021#include "sleep.h"
Pavel Machek4fc2fba2008-01-30 13:32:54 +010022
Pavel Machek4fc2fba2008-01-30 13:32:54 +010023unsigned long acpi_realmode_flags;
Pavel Machek4fc2fba2008-01-30 13:32:54 +010024
Marcin Slusarz9744f5a2008-08-03 19:25:48 +020025#if defined(CONFIG_SMP) && defined(CONFIG_64BIT)
Matt Mackall5000cad2008-10-09 11:56:21 -050026static char temp_stack[4096];
Pavel Macheke44b7b72008-04-10 23:28:10 +020027#endif
Pavel Machek4fc2fba2008-01-30 13:32:54 +010028
29/**
Lv Zheng40bce102013-10-31 09:31:18 +080030 * 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 Kleen2605fc22014-05-02 00:44:37 +020035acpi_status asmlinkage __visible x86_acpi_enter_sleep_state(u8 state)
Lv Zheng40bce102013-10-31 09:31:18 +080036{
37 return acpi_enter_sleep_state(state);
38}
39
40/**
Konrad Rzeszutek Wilkd6a77ea2013-05-14 17:09:16 +000041 * x86_acpi_suspend_lowlevel - save kernel state
Pavel Machek4fc2fba2008-01-30 13:32:54 +010042 *
43 * Create an identity mapped page table and copy the wakeup routine to
44 * low memory.
45 */
Konrad Rzeszutek Wilkd6a77ea2013-05-14 17:09:16 +000046int x86_acpi_suspend_lowlevel(void)
Pavel Machek4fc2fba2008-01-30 13:32:54 +010047{
Jarkko Sakkinenc9b77cc2012-05-08 21:22:29 +030048 struct wakeup_header *header =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +030049 (struct wakeup_header *) __va(real_mode_header->wakeup_header);
Pavel Macheke44b7b72008-04-10 23:28:10 +020050
H. Peter Anvind1ee4332011-02-14 15:42:46 -080051 if (header->signature != WAKEUP_HEADER_SIGNATURE) {
Pavel Macheke44b7b72008-04-10 23:28:10 +020052 printk(KERN_ERR "wakeup header does not match\n");
53 return -EINVAL;
54 }
55
56 header->video_mode = saved_video_mode;
57
H. Peter Anvin73201db2012-09-26 15:02:34 -070058 header->pmode_behavior = 0;
59
Pavel Macheke44b7b72008-04-10 23:28:10 +020060#ifndef CONFIG_64BIT
Konrad Rzeszutek Wilk357d1222013-04-05 16:42:23 -040061 native_store_gdt((struct desc_ptr *)&header->pmode_gdt);
Pavel Macheke44b7b72008-04-10 23:28:10 +020062
H. Peter Anvin5ff560f2013-07-12 16:48:12 -070063 /*
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 Anvin73201db2012-09-26 15:02:34 -070071 if (!rdmsr_safe(MSR_EFER,
72 &header->pmode_efer_low,
H. Peter Anvin5ff560f2013-07-12 16:48:12 -070073 &header->pmode_efer_high) &&
74 !wrmsr_safe(MSR_EFER,
75 header->pmode_efer_low,
76 header->pmode_efer_high))
H. Peter Anvin73201db2012-09-26 15:02:34 -070077 header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_EFER);
Pavel Macheke44b7b72008-04-10 23:28:10 +020078#endif /* !CONFIG_64BIT */
79
80 header->pmode_cr0 = read_cr0();
H. Peter Anvin73201db2012-09-26 15:02:34 -070081 if (__this_cpu_read(cpu_info.cpuid_level) >= 0) {
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070082 header->pmode_cr4 = __read_cr4();
H. Peter Anvin73201db2012-09-26 15:02:34 -070083 header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_CR4);
84 }
Kees Cook7a313662011-07-06 18:10:34 -070085 if (!rdmsr_safe(MSR_IA32_MISC_ENABLE,
86 &header->pmode_misc_en_low,
H. Peter Anvin5ff560f2013-07-12 16:48:12 -070087 &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 Cook7a313662011-07-06 18:10:34 -070091 header->pmode_behavior |=
92 (1 << WAKEUP_BEHAVIOR_RESTORE_MISC_ENABLE);
Pavel Macheke44b7b72008-04-10 23:28:10 +020093 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 Duyckafd51a02012-11-16 13:57:43 -080098 header->pmode_cr3 = (u32)__pa_symbol(initial_page_table);
Pavel Macheke44b7b72008-04-10 23:28:10 +020099 saved_magic = 0x12345678;
100#else /* CONFIG_64BIT */
Ingo Molnar1ea598c2008-06-13 20:31:54 +0200101#ifdef CONFIG_SMP
Josh Poimboeufb32f96c2016-08-18 10:59:03 -0500102 initial_stack = (unsigned long)temp_stack + sizeof(temp_stack);
Rafael J. Wysocki3038eda2008-10-17 01:26:27 +0200103 early_gdt_descr.address =
104 (unsigned long)get_cpu_gdt_table(smp_processor_id());
Tejun Heo004aa322009-01-13 20:41:35 +0900105 initial_gs = per_cpu_offset(smp_processor_id());
Ingo Molnar1ea598c2008-06-13 20:31:54 +0200106#endif
Pavel Macheke44b7b72008-04-10 23:28:10 +0200107 initial_code = (unsigned long)wakeup_long64;
Jaswinder Singh Rajputce4b3c52009-04-18 13:44:57 +0200108 saved_magic = 0x123456789abcdef0L;
Pavel Macheke44b7b72008-04-10 23:28:10 +0200109#endif /* CONFIG_64BIT */
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100110
Todd E Brandt92f9e172016-03-02 16:05:29 -0800111 /*
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. Wysockif1a20032011-02-08 23:42:22 +0100116 do_suspend_lowlevel();
Todd E Brandt92f9e172016-03-02 16:05:29 -0800117 unpause_graph_tracing();
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100118 return 0;
119}
120
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100121static 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 Libdfe6b72008-07-23 21:28:41 -0700130#ifdef CONFIG_HIBERNATION
131 if (strncmp(str, "s4_nohwsig", 10) == 0)
132 acpi_no_s4_hw_signature();
133#endif
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +0200134 if (strncmp(str, "nonvs", 5) == 0)
135 acpi_nvs_nosave();
Kristen Carlson Accardi1bad2f12012-10-26 13:39:15 +0200136 if (strncmp(str, "nonvs_s3", 8) == 0)
137 acpi_nvs_nosave_s3();
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200138 if (strncmp(str, "old_ordering", 12) == 0)
139 acpi_old_suspend_ordering();
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100140 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);