1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
24fc2fba8SPavel Machek /*
34fc2fba8SPavel Machek * sleep.c - x86-specific ACPI sleep support.
44fc2fba8SPavel Machek *
54fc2fba8SPavel Machek * Copyright (C) 2001-2003 Patrick Mochel
6a2531293SPavel Machek * Copyright (C) 2001-2003 Pavel Machek <pavel@ucw.cz>
74fc2fba8SPavel Machek */
84fc2fba8SPavel Machek
94fc2fba8SPavel Machek #include <linux/acpi.h>
10a9ce6bc1SYinghai Lu #include <linux/memblock.h>
114fc2fba8SPavel Machek #include <linux/dmi.h>
124fc2fba8SPavel Machek #include <linux/cpumask.h>
1365fddcfcSMike Rapoport #include <linux/pgtable.h>
144fdf08b5SH. Peter Anvin #include <asm/segment.h>
153038edabSRafael J. Wysocki #include <asm/desc.h>
16d344e38bSH. Peter Anvin #include <asm/cacheflush.h>
17c9b77ccbSJarkko Sakkinen #include <asm/realmode.h>
18f6c46b1dSDavid Woodhouse #include <asm/hypervisor.h>
19b40827faSBorislav Petkov #include <asm/smp.h>
2092f9e179STodd E Brandt
21c4845474SJarkko Sakkinen #include <linux/ftrace.h>
22e44b7b75SPavel Machek #include "../../realmode/rm/wakeup.h"
234fc2fba8SPavel Machek #include "sleep.h"
244fc2fba8SPavel Machek
254fc2fba8SPavel Machek unsigned long acpi_realmode_flags;
269744f5a3SMarcin Slusarz
275000cadcSMatt Mackall #if defined(CONFIG_SMP) && defined(CONFIG_64BIT)
28e44b7b75SPavel Machek static char temp_stack[4096];
294fc2fba8SPavel Machek #endif
304fc2fba8SPavel Machek
3196078642SSean Christopherson /**
3296078642SSean Christopherson * acpi_get_wakeup_address - provide physical address for S3 wakeup
3396078642SSean Christopherson *
3496078642SSean Christopherson * Returns the physical address where the kernel should be resumed after the
3596078642SSean Christopherson * system awakes from S3, e.g. for programming into the firmware waking vector.
3696078642SSean Christopherson */
acpi_get_wakeup_address(void)3796078642SSean Christopherson unsigned long acpi_get_wakeup_address(void)
3896078642SSean Christopherson {
3996078642SSean Christopherson return ((unsigned long)(real_mode_header->wakeup_start));
4096078642SSean Christopherson }
4196078642SSean Christopherson
4240bce100SLv Zheng /**
4340bce100SLv Zheng * x86_acpi_enter_sleep_state - enter sleep state
4440bce100SLv Zheng * @state: Sleep state to enter.
45d9f6e12fSIngo Molnar *
4640bce100SLv Zheng * Wrapper around acpi_enter_sleep_state() to be called by assembly.
47fa0fca68SAlexey Dobriyan */
x86_acpi_enter_sleep_state(u8 state)4840bce100SLv Zheng asmlinkage acpi_status __visible x86_acpi_enter_sleep_state(u8 state)
4940bce100SLv Zheng {
5040bce100SLv Zheng return acpi_enter_sleep_state(state);
5140bce100SLv Zheng }
5240bce100SLv Zheng
53d6a77eadSKonrad Rzeszutek Wilk /**
544fc2fba8SPavel Machek * x86_acpi_suspend_lowlevel - save kernel state
554fc2fba8SPavel Machek *
564fc2fba8SPavel Machek * Create an identity mapped page table and copy the wakeup routine to
574fc2fba8SPavel Machek * low memory.
58d6a77eadSKonrad Rzeszutek Wilk */
x86_acpi_suspend_lowlevel(void)594fc2fba8SPavel Machek int x86_acpi_suspend_lowlevel(void)
60c9b77ccbSJarkko Sakkinen {
61b429dbf6SJarkko Sakkinen struct wakeup_header *header =
62e44b7b75SPavel Machek (struct wakeup_header *) __va(real_mode_header->wakeup_header);
63d1ee4335SH. Peter Anvin
64e44b7b75SPavel Machek if (header->signature != WAKEUP_HEADER_SIGNATURE) {
65e44b7b75SPavel Machek printk(KERN_ERR "wakeup header does not match\n");
66e44b7b75SPavel Machek return -EINVAL;
67e44b7b75SPavel Machek }
68e44b7b75SPavel Machek
69e44b7b75SPavel Machek header->video_mode = saved_video_mode;
7073201dbeSH. Peter Anvin
7173201dbeSH. Peter Anvin header->pmode_behavior = 0;
72e44b7b75SPavel Machek
73357d1226SKonrad Rzeszutek Wilk #ifndef CONFIG_64BIT
74e44b7b75SPavel Machek native_store_gdt((struct desc_ptr *)&header->pmode_gdt);
755ff560fdSH. Peter Anvin
765ff560fdSH. Peter Anvin /*
775ff560fdSH. Peter Anvin * We have to check that we can write back the value, and not
785ff560fdSH. Peter Anvin * just read it. At least on 90 nm Pentium M (Family 6, Model
795ff560fdSH. Peter Anvin * 13), reading an invalid MSR is not guaranteed to trap, see
805ff560fdSH. Peter Anvin * Erratum X4 in "Intel Pentium M Processor on 90 nm Process
815ff560fdSH. Peter Anvin * with 2-MB L2 Cache and Intel® Processor A100 and A110 on 90
825ff560fdSH. Peter Anvin * nm process with 512-KB L2 Cache Specification Update".
8373201dbeSH. Peter Anvin */
8473201dbeSH. Peter Anvin if (!rdmsr_safe(MSR_EFER,
855ff560fdSH. Peter Anvin &header->pmode_efer_low,
865ff560fdSH. Peter Anvin &header->pmode_efer_high) &&
875ff560fdSH. Peter Anvin !wrmsr_safe(MSR_EFER,
885ff560fdSH. Peter Anvin header->pmode_efer_low,
8973201dbeSH. Peter Anvin header->pmode_efer_high))
90e44b7b75SPavel Machek header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_EFER);
91e44b7b75SPavel Machek #endif /* !CONFIG_64BIT */
92e44b7b75SPavel Machek
9373201dbeSH. Peter Anvin header->pmode_cr0 = read_cr0();
941e02ce4cSAndy Lutomirski if (__this_cpu_read(cpu_info.cpuid_level) >= 0) {
9573201dbeSH. Peter Anvin header->pmode_cr4 = __read_cr4();
9673201dbeSH. Peter Anvin header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_CR4);
977a313666SKees Cook }
987a313666SKees Cook if (!rdmsr_safe(MSR_IA32_MISC_ENABLE,
995ff560fdSH. Peter Anvin &header->pmode_misc_en_low,
1005ff560fdSH. Peter Anvin &header->pmode_misc_en_high) &&
1015ff560fdSH. Peter Anvin !wrmsr_safe(MSR_IA32_MISC_ENABLE,
1025ff560fdSH. Peter Anvin header->pmode_misc_en_low,
1037a313666SKees Cook header->pmode_misc_en_high))
1047a313666SKees Cook header->pmode_behavior |=
105e44b7b75SPavel Machek (1 << WAKEUP_BEHAVIOR_RESTORE_MISC_ENABLE);
106e44b7b75SPavel Machek header->realmode_flags = acpi_realmode_flags;
107e44b7b75SPavel Machek header->real_magic = 0x12345678;
108e44b7b75SPavel Machek
109e44b7b75SPavel Machek #ifndef CONFIG_64BIT
110afd51a0eSAlexander Duyck header->pmode_entry = (u32)&wakeup_pmode_return;
111e44b7b75SPavel Machek header->pmode_cr3 = (u32)__pa_symbol(initial_page_table);
112e44b7b75SPavel Machek saved_magic = 0x12345678;
1131ea598c2SIngo Molnar #else /* CONFIG_64BIT */
114*3adee777SBrian Gerst #ifdef CONFIG_SMP
115*3adee777SBrian Gerst /*
116*3adee777SBrian Gerst * As each CPU starts up, it will find its own stack pointer
117*3adee777SBrian Gerst * from its current_task->thread.sp. Typically that will be
118*3adee777SBrian Gerst * the idle thread for a newly-started AP, or even the boot
119*3adee777SBrian Gerst * CPU which will find it set to &init_task in the static
120*3adee777SBrian Gerst * per-cpu data.
121*3adee777SBrian Gerst *
122*3adee777SBrian Gerst * Make the resuming CPU use the temporary stack at startup
123*3adee777SBrian Gerst * by setting current->thread.sp to point to that. The true
124*3adee777SBrian Gerst * %rsp will be restored with the rest of the CPU context,
125*3adee777SBrian Gerst * by do_suspend_lowlevel(). And unwinders don't care about
126*3adee777SBrian Gerst * the abuse of ->thread.sp because it's a dead variable
127*3adee777SBrian Gerst * while the thread is running on the CPU anyway; the true
128*3adee777SBrian Gerst * value is in the actual %rsp register.
129*3adee777SBrian Gerst */
130*3adee777SBrian Gerst current->thread.sp = (unsigned long)temp_stack + sizeof(temp_stack);
1311ea598c2SIngo Molnar /*
132e44b7b75SPavel Machek * Ensure the CPU knows which one it is when it comes back, if
133ce4b3c55SJaswinder Singh Rajput * it isn't in parallel mode and expected to work that out for
134e44b7b75SPavel Machek * itself.
1354fc2fba8SPavel Machek */
13692f9e179STodd E Brandt if (!(smpboot_control & STARTUP_PARALLEL_MASK))
13792f9e179STodd E Brandt smpboot_control = smp_processor_id();
13892f9e179STodd E Brandt #endif
13992f9e179STodd E Brandt initial_code = (unsigned long)wakeup_long64;
14092f9e179STodd E Brandt saved_magic = 0x123456789abcdef0L;
141f1a2003eSRafael J. Wysocki #endif /* CONFIG_64BIT */
14292f9e179STodd E Brandt
1434fc2fba8SPavel Machek /*
1444fc2fba8SPavel Machek * Pause/unpause graph tracing around do_suspend_lowlevel as it has
1454fc2fba8SPavel Machek * inconsistent call/return info after it jumps to the wakeup vector.
1464fc2fba8SPavel Machek */
1474fc2fba8SPavel Machek pause_graph_tracing();
1484fc2fba8SPavel Machek do_suspend_lowlevel();
1494fc2fba8SPavel Machek unpause_graph_tracing();
1504fc2fba8SPavel Machek return 0;
1514fc2fba8SPavel Machek }
1524fc2fba8SPavel Machek
acpi_sleep_setup(char * str)1534fc2fba8SPavel Machek static int __init acpi_sleep_setup(char *str)
1544fc2fba8SPavel Machek {
155bdfe6b7cSShaohua Li while ((str != NULL) && (*str != '\0')) {
15674d95555SDavid Woodhouse if (strncmp(str, "s3_bios", 7) == 0)
157f6c46b1dSDavid Woodhouse acpi_realmode_flags |= 1;
158bdfe6b7cSShaohua Li if (strncmp(str, "s3_mode", 7) == 0)
159f6c46b1dSDavid Woodhouse acpi_realmode_flags |= 2;
160bdfe6b7cSShaohua Li if (strncmp(str, "s3_beep", 7) == 0)
16172ad5d77SRafael J. Wysocki acpi_realmode_flags |= 4;
16272ad5d77SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
1631bad2f19SKristen Carlson Accardi if (strncmp(str, "s4_hwsig", 8) == 0)
1641bad2f19SKristen Carlson Accardi acpi_check_s4_hw_signature = 1;
165d8f3de0dSRafael J. Wysocki if (strncmp(str, "s4_nohwsig", 10) == 0)
166d8f3de0dSRafael J. Wysocki acpi_check_s4_hw_signature = 0;
16757044031SRafael J. Wysocki #endif
16857044031SRafael J. Wysocki if (strncmp(str, "nonvs", 5) == 0)
1694fc2fba8SPavel Machek acpi_nvs_nosave();
1704fc2fba8SPavel Machek if (strncmp(str, "nonvs_s3", 8) == 0)
1714fc2fba8SPavel Machek acpi_nvs_nosave_s3();
1724fc2fba8SPavel Machek if (strncmp(str, "old_ordering", 12) == 0)
1734fc2fba8SPavel Machek acpi_old_suspend_ordering();
1744fc2fba8SPavel Machek if (strncmp(str, "nobl", 4) == 0)
1754fc2fba8SPavel Machek acpi_sleep_no_blacklist();
1764fc2fba8SPavel Machek str = strchr(str, ',');
177f6c46b1dSDavid Woodhouse if (str != NULL)
178f6c46b1dSDavid Woodhouse str += strspn(str, ", \t");
179f6c46b1dSDavid Woodhouse }
180f6c46b1dSDavid Woodhouse return 1;
181f6c46b1dSDavid Woodhouse }
182f6c46b1dSDavid Woodhouse
183f6c46b1dSDavid Woodhouse __setup("acpi_sleep=", acpi_sleep_setup);
184f6c46b1dSDavid Woodhouse
185f6c46b1dSDavid Woodhouse #if defined(CONFIG_HIBERNATION) && defined(CONFIG_HYPERVISOR_GUEST)
init_s4_sigcheck(void)186f6c46b1dSDavid Woodhouse static int __init init_s4_sigcheck(void)
187f6c46b1dSDavid Woodhouse {
188f6c46b1dSDavid Woodhouse /*
189f6c46b1dSDavid Woodhouse * If running on a hypervisor, honour the ACPI specification
190f6c46b1dSDavid Woodhouse * by default and trigger a clean reboot when the hardware
191f6c46b1dSDavid Woodhouse * signature in FACS is changed after hibernation.
192f6c46b1dSDavid Woodhouse */
193f6c46b1dSDavid Woodhouse if (acpi_check_s4_hw_signature == -1 &&
194f6c46b1dSDavid Woodhouse !hypervisor_is_type(X86_HYPER_NATIVE))
195 acpi_check_s4_hw_signature = 1;
196
197 return 0;
198 }
199 /* This must happen before acpi_init() which is a subsys initcall */
200 arch_initcall(init_s4_sigcheck);
201 #endif
202