blob: 103b6ab368d39315bc752e02a7bdc5b83ede0acf [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>
Borislav Petkovb40827fa2010-08-28 15:58:33 +020017
Pavel Macheke44b7b72008-04-10 23:28:10 +020018#include "realmode/wakeup.h"
19#include "sleep.h"
Pavel Machek4fc2fba2008-01-30 13:32:54 +010020
Pavel Machek4fc2fba2008-01-30 13:32:54 +010021unsigned long acpi_realmode_flags;
Pavel Machek4fc2fba2008-01-30 13:32:54 +010022
Marcin Slusarz9744f5a2008-08-03 19:25:48 +020023#if defined(CONFIG_SMP) && defined(CONFIG_64BIT)
Matt Mackall5000cad2008-10-09 11:56:21 -050024static char temp_stack[4096];
Pavel Macheke44b7b72008-04-10 23:28:10 +020025#endif
Pavel Machek4fc2fba2008-01-30 13:32:54 +010026
27/**
Rafael J. Wysockif1a20032011-02-08 23:42:22 +010028 * acpi_suspend_lowlevel - save kernel state
Pavel Machek4fc2fba2008-01-30 13:32:54 +010029 *
30 * Create an identity mapped page table and copy the wakeup routine to
31 * low memory.
32 */
Rafael J. Wysockif1a20032011-02-08 23:42:22 +010033int acpi_suspend_lowlevel(void)
Pavel Machek4fc2fba2008-01-30 13:32:54 +010034{
Pavel Macheke44b7b72008-04-10 23:28:10 +020035 struct wakeup_header *header;
H. Peter Anvind1ee4332011-02-14 15:42:46 -080036 /* address in low memory of the wakeup routine. */
37 char *acpi_realmode;
Pavel Macheke44b7b72008-04-10 23:28:10 +020038
H. Peter Anvind1ee4332011-02-14 15:42:46 -080039 acpi_realmode = TRAMPOLINE_SYM(acpi_wakeup_code);
Pavel Macheke44b7b72008-04-10 23:28:10 +020040
H. Peter Anvind1ee4332011-02-14 15:42:46 -080041 header = (struct wakeup_header *)(acpi_realmode + WAKEUP_HEADER_OFFSET);
42 if (header->signature != WAKEUP_HEADER_SIGNATURE) {
Pavel Macheke44b7b72008-04-10 23:28:10 +020043 printk(KERN_ERR "wakeup header does not match\n");
44 return -EINVAL;
45 }
46
47 header->video_mode = saved_video_mode;
48
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020049 header->wakeup_jmp_seg = acpi_wakeup_address >> 4;
H. Peter Anvin065cb3d2008-07-14 11:44:26 -070050
51 /*
52 * Set up the wakeup GDT. We set these up as Big Real Mode,
53 * that is, with limits set to 4 GB. At least the Lenovo
54 * Thinkpad X61 is known to need this for the video BIOS
55 * initialization quirk to work; this is likely to also
56 * be the case for other laptops or integrated video devices.
57 */
58
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020059 /* GDT[0]: GDT self-pointer */
60 header->wakeup_gdt[0] =
61 (u64)(sizeof(header->wakeup_gdt) - 1) +
H. Peter Anvind1ee4332011-02-14 15:42:46 -080062 ((u64)__pa(&header->wakeup_gdt) << 16);
H. Peter Anvin065cb3d2008-07-14 11:44:26 -070063 /* GDT[1]: big real mode-like code segment */
H. Peter Anvin3bf2e772008-07-13 21:18:02 -070064 header->wakeup_gdt[1] =
65 GDT_ENTRY(0x809b, acpi_wakeup_address, 0xfffff);
H. Peter Anvin065cb3d2008-07-14 11:44:26 -070066 /* GDT[2]: big real mode-like data segment */
H. Peter Anvin3bf2e772008-07-13 21:18:02 -070067 header->wakeup_gdt[2] =
68 GDT_ENTRY(0x8093, acpi_wakeup_address, 0xfffff);
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020069
Pavel Macheke44b7b72008-04-10 23:28:10 +020070#ifndef CONFIG_64BIT
71 store_gdt((struct desc_ptr *)&header->pmode_gdt);
72
H. Peter Anvina7c4c0d2009-11-13 15:28:14 -080073 if (rdmsr_safe(MSR_EFER, &header->pmode_efer_low,
74 &header->pmode_efer_high))
75 header->pmode_efer_low = header->pmode_efer_high = 0;
Pavel Macheke44b7b72008-04-10 23:28:10 +020076#endif /* !CONFIG_64BIT */
77
78 header->pmode_cr0 = read_cr0();
David Friese532c062008-08-17 23:03:40 -050079 header->pmode_cr4 = read_cr4_safe();
Kees Cook7a313662011-07-06 18:10:34 -070080 header->pmode_behavior = 0;
81 if (!rdmsr_safe(MSR_IA32_MISC_ENABLE,
82 &header->pmode_misc_en_low,
83 &header->pmode_misc_en_high))
84 header->pmode_behavior |=
85 (1 << WAKEUP_BEHAVIOR_RESTORE_MISC_ENABLE);
Pavel Macheke44b7b72008-04-10 23:28:10 +020086 header->realmode_flags = acpi_realmode_flags;
87 header->real_magic = 0x12345678;
88
89#ifndef CONFIG_64BIT
90 header->pmode_entry = (u32)&wakeup_pmode_return;
Borislav Petkovb40827fa2010-08-28 15:58:33 +020091 header->pmode_cr3 = (u32)__pa(&initial_page_table);
Pavel Macheke44b7b72008-04-10 23:28:10 +020092 saved_magic = 0x12345678;
93#else /* CONFIG_64BIT */
H. Peter Anvind1ee4332011-02-14 15:42:46 -080094 header->trampoline_segment = trampoline_address() >> 4;
Ingo Molnar1ea598c2008-06-13 20:31:54 +020095#ifdef CONFIG_SMP
H. Peter Anvin11d4c3f2011-02-04 16:14:11 -080096 stack_start = (unsigned long)temp_stack + sizeof(temp_stack);
Rafael J. Wysocki3038eda2008-10-17 01:26:27 +020097 early_gdt_descr.address =
98 (unsigned long)get_cpu_gdt_table(smp_processor_id());
Tejun Heo004aa322009-01-13 20:41:35 +090099 initial_gs = per_cpu_offset(smp_processor_id());
Ingo Molnar1ea598c2008-06-13 20:31:54 +0200100#endif
Pavel Macheke44b7b72008-04-10 23:28:10 +0200101 initial_code = (unsigned long)wakeup_long64;
Jaswinder Singh Rajputce4b3c52009-04-18 13:44:57 +0200102 saved_magic = 0x123456789abcdef0L;
Pavel Macheke44b7b72008-04-10 23:28:10 +0200103#endif /* CONFIG_64BIT */
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100104
Rafael J. Wysockif1a20032011-02-08 23:42:22 +0100105 do_suspend_lowlevel();
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100106 return 0;
107}
108
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100109static int __init acpi_sleep_setup(char *str)
110{
111 while ((str != NULL) && (*str != '\0')) {
112 if (strncmp(str, "s3_bios", 7) == 0)
113 acpi_realmode_flags |= 1;
114 if (strncmp(str, "s3_mode", 7) == 0)
115 acpi_realmode_flags |= 2;
116 if (strncmp(str, "s3_beep", 7) == 0)
117 acpi_realmode_flags |= 4;
Shaohua Libdfe6b72008-07-23 21:28:41 -0700118#ifdef CONFIG_HIBERNATION
119 if (strncmp(str, "s4_nohwsig", 10) == 0)
120 acpi_no_s4_hw_signature();
121#endif
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +0200122 if (strncmp(str, "nonvs", 5) == 0)
123 acpi_nvs_nosave();
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200124 if (strncmp(str, "old_ordering", 12) == 0)
125 acpi_old_suspend_ordering();
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100126 str = strchr(str, ',');
127 if (str != NULL)
128 str += strspn(str, ", \t");
129 }
130 return 1;
131}
132
133__setup("acpi_sleep=", acpi_sleep_setup);