blob: 33120100ff5e50486e306f35688620ee8c05754f [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
Jarkko Sakkinenc4845472012-05-08 21:22:42 +030019#include "../../realmode/rm/wakeup.h"
Pavel Macheke44b7b72008-04-10 23:28:10 +020020#include "sleep.h"
Pavel Machek4fc2fba2008-01-30 13:32:54 +010021
Pavel Machek4fc2fba2008-01-30 13:32:54 +010022unsigned long acpi_realmode_flags;
Pavel Machek4fc2fba2008-01-30 13:32:54 +010023
Marcin Slusarz9744f5a2008-08-03 19:25:48 +020024#if defined(CONFIG_SMP) && defined(CONFIG_64BIT)
Matt Mackall5000cad2008-10-09 11:56:21 -050025static char temp_stack[4096];
Pavel Macheke44b7b72008-04-10 23:28:10 +020026#endif
Pavel Machek4fc2fba2008-01-30 13:32:54 +010027
28/**
Konrad Rzeszutek Wilkd6a77ea2013-05-14 17:09:16 +000029 * x86_acpi_suspend_lowlevel - save kernel state
Pavel Machek4fc2fba2008-01-30 13:32:54 +010030 *
31 * Create an identity mapped page table and copy the wakeup routine to
32 * low memory.
33 */
Konrad Rzeszutek Wilkd6a77ea2013-05-14 17:09:16 +000034int x86_acpi_suspend_lowlevel(void)
Pavel Machek4fc2fba2008-01-30 13:32:54 +010035{
Jarkko Sakkinenc9b77cc2012-05-08 21:22:29 +030036 struct wakeup_header *header =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +030037 (struct wakeup_header *) __va(real_mode_header->wakeup_header);
Pavel Macheke44b7b72008-04-10 23:28:10 +020038
H. Peter Anvind1ee4332011-02-14 15:42:46 -080039 if (header->signature != WAKEUP_HEADER_SIGNATURE) {
Pavel Macheke44b7b72008-04-10 23:28:10 +020040 printk(KERN_ERR "wakeup header does not match\n");
41 return -EINVAL;
42 }
43
44 header->video_mode = saved_video_mode;
45
H. Peter Anvin73201db2012-09-26 15:02:34 -070046 header->pmode_behavior = 0;
47
Pavel Macheke44b7b72008-04-10 23:28:10 +020048#ifndef CONFIG_64BIT
Konrad Rzeszutek Wilk357d1222013-04-05 16:42:23 -040049 native_store_gdt((struct desc_ptr *)&header->pmode_gdt);
Pavel Macheke44b7b72008-04-10 23:28:10 +020050
H. Peter Anvin5ff560f2013-07-12 16:48:12 -070051 /*
52 * We have to check that we can write back the value, and not
53 * just read it. At least on 90 nm Pentium M (Family 6, Model
54 * 13), reading an invalid MSR is not guaranteed to trap, see
55 * Erratum X4 in "Intel Pentium M Processor on 90 nm Process
56 * with 2-MB L2 Cache and IntelĀ® Processor A100 and A110 on 90
57 * nm process with 512-KB L2 Cache Specification Update".
58 */
H. Peter Anvin73201db2012-09-26 15:02:34 -070059 if (!rdmsr_safe(MSR_EFER,
60 &header->pmode_efer_low,
H. Peter Anvin5ff560f2013-07-12 16:48:12 -070061 &header->pmode_efer_high) &&
62 !wrmsr_safe(MSR_EFER,
63 header->pmode_efer_low,
64 header->pmode_efer_high))
H. Peter Anvin73201db2012-09-26 15:02:34 -070065 header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_EFER);
Pavel Macheke44b7b72008-04-10 23:28:10 +020066#endif /* !CONFIG_64BIT */
67
68 header->pmode_cr0 = read_cr0();
H. Peter Anvin73201db2012-09-26 15:02:34 -070069 if (__this_cpu_read(cpu_info.cpuid_level) >= 0) {
70 header->pmode_cr4 = read_cr4();
71 header->pmode_behavior |= (1 << WAKEUP_BEHAVIOR_RESTORE_CR4);
72 }
Kees Cook7a313662011-07-06 18:10:34 -070073 if (!rdmsr_safe(MSR_IA32_MISC_ENABLE,
74 &header->pmode_misc_en_low,
H. Peter Anvin5ff560f2013-07-12 16:48:12 -070075 &header->pmode_misc_en_high) &&
76 !wrmsr_safe(MSR_IA32_MISC_ENABLE,
77 header->pmode_misc_en_low,
78 header->pmode_misc_en_high))
Kees Cook7a313662011-07-06 18:10:34 -070079 header->pmode_behavior |=
80 (1 << WAKEUP_BEHAVIOR_RESTORE_MISC_ENABLE);
Pavel Macheke44b7b72008-04-10 23:28:10 +020081 header->realmode_flags = acpi_realmode_flags;
82 header->real_magic = 0x12345678;
83
84#ifndef CONFIG_64BIT
85 header->pmode_entry = (u32)&wakeup_pmode_return;
Alexander Duyckafd51a02012-11-16 13:57:43 -080086 header->pmode_cr3 = (u32)__pa_symbol(initial_page_table);
Pavel Macheke44b7b72008-04-10 23:28:10 +020087 saved_magic = 0x12345678;
88#else /* CONFIG_64BIT */
Ingo Molnar1ea598c2008-06-13 20:31:54 +020089#ifdef CONFIG_SMP
H. Peter Anvin11d4c3f2011-02-04 16:14:11 -080090 stack_start = (unsigned long)temp_stack + sizeof(temp_stack);
Rafael J. Wysocki3038eda2008-10-17 01:26:27 +020091 early_gdt_descr.address =
92 (unsigned long)get_cpu_gdt_table(smp_processor_id());
Tejun Heo004aa322009-01-13 20:41:35 +090093 initial_gs = per_cpu_offset(smp_processor_id());
Ingo Molnar1ea598c2008-06-13 20:31:54 +020094#endif
Pavel Macheke44b7b72008-04-10 23:28:10 +020095 initial_code = (unsigned long)wakeup_long64;
Jaswinder Singh Rajputce4b3c52009-04-18 13:44:57 +020096 saved_magic = 0x123456789abcdef0L;
Pavel Macheke44b7b72008-04-10 23:28:10 +020097#endif /* CONFIG_64BIT */
Pavel Machek4fc2fba2008-01-30 13:32:54 +010098
Rafael J. Wysockif1a20032011-02-08 23:42:22 +010099 do_suspend_lowlevel();
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100100 return 0;
101}
102
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100103static int __init acpi_sleep_setup(char *str)
104{
105 while ((str != NULL) && (*str != '\0')) {
106 if (strncmp(str, "s3_bios", 7) == 0)
107 acpi_realmode_flags |= 1;
108 if (strncmp(str, "s3_mode", 7) == 0)
109 acpi_realmode_flags |= 2;
110 if (strncmp(str, "s3_beep", 7) == 0)
111 acpi_realmode_flags |= 4;
Shaohua Libdfe6b72008-07-23 21:28:41 -0700112#ifdef CONFIG_HIBERNATION
113 if (strncmp(str, "s4_nohwsig", 10) == 0)
114 acpi_no_s4_hw_signature();
115#endif
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +0200116 if (strncmp(str, "nonvs", 5) == 0)
117 acpi_nvs_nosave();
Kristen Carlson Accardi1bad2f12012-10-26 13:39:15 +0200118 if (strncmp(str, "nonvs_s3", 8) == 0)
119 acpi_nvs_nosave_s3();
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200120 if (strncmp(str, "old_ordering", 12) == 0)
121 acpi_old_suspend_ordering();
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100122 str = strchr(str, ',');
123 if (str != NULL)
124 str += strspn(str, ", \t");
125 }
126 return 1;
127}
128
129__setup("acpi_sleep=", acpi_sleep_setup);