blob: 146a49c763a49085b50d5b3b6babcda74650463b [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
Konrad Rzeszutek Wilkcd742572012-04-22 23:03:18 -040027asmlinkage void acpi_enter_s3(void)
28{
29 acpi_enter_sleep_state(3, wake_sleep_flags);
30}
Pavel Machek4fc2fba2008-01-30 13:32:54 +010031/**
Rafael J. Wysockif1a20032011-02-08 23:42:22 +010032 * acpi_suspend_lowlevel - save kernel state
Pavel Machek4fc2fba2008-01-30 13:32:54 +010033 *
34 * Create an identity mapped page table and copy the wakeup routine to
35 * low memory.
36 */
Rafael J. Wysockif1a20032011-02-08 23:42:22 +010037int acpi_suspend_lowlevel(void)
Pavel Machek4fc2fba2008-01-30 13:32:54 +010038{
Pavel Macheke44b7b72008-04-10 23:28:10 +020039 struct wakeup_header *header;
H. Peter Anvind1ee4332011-02-14 15:42:46 -080040 /* address in low memory of the wakeup routine. */
41 char *acpi_realmode;
Pavel Macheke44b7b72008-04-10 23:28:10 +020042
H. Peter Anvind1ee4332011-02-14 15:42:46 -080043 acpi_realmode = TRAMPOLINE_SYM(acpi_wakeup_code);
Pavel Macheke44b7b72008-04-10 23:28:10 +020044
H. Peter Anvind1ee4332011-02-14 15:42:46 -080045 header = (struct wakeup_header *)(acpi_realmode + WAKEUP_HEADER_OFFSET);
46 if (header->signature != WAKEUP_HEADER_SIGNATURE) {
Pavel Macheke44b7b72008-04-10 23:28:10 +020047 printk(KERN_ERR "wakeup header does not match\n");
48 return -EINVAL;
49 }
50
51 header->video_mode = saved_video_mode;
52
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020053 header->wakeup_jmp_seg = acpi_wakeup_address >> 4;
H. Peter Anvin065cb3d2008-07-14 11:44:26 -070054
55 /*
56 * Set up the wakeup GDT. We set these up as Big Real Mode,
57 * that is, with limits set to 4 GB. At least the Lenovo
58 * Thinkpad X61 is known to need this for the video BIOS
59 * initialization quirk to work; this is likely to also
60 * be the case for other laptops or integrated video devices.
61 */
62
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020063 /* GDT[0]: GDT self-pointer */
64 header->wakeup_gdt[0] =
65 (u64)(sizeof(header->wakeup_gdt) - 1) +
H. Peter Anvind1ee4332011-02-14 15:42:46 -080066 ((u64)__pa(&header->wakeup_gdt) << 16);
H. Peter Anvin065cb3d2008-07-14 11:44:26 -070067 /* GDT[1]: big real mode-like code segment */
H. Peter Anvin3bf2e772008-07-13 21:18:02 -070068 header->wakeup_gdt[1] =
69 GDT_ENTRY(0x809b, acpi_wakeup_address, 0xfffff);
H. Peter Anvin065cb3d2008-07-14 11:44:26 -070070 /* GDT[2]: big real mode-like data segment */
H. Peter Anvin3bf2e772008-07-13 21:18:02 -070071 header->wakeup_gdt[2] =
72 GDT_ENTRY(0x8093, acpi_wakeup_address, 0xfffff);
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020073
Pavel Macheke44b7b72008-04-10 23:28:10 +020074#ifndef CONFIG_64BIT
75 store_gdt((struct desc_ptr *)&header->pmode_gdt);
76
H. Peter Anvina7c4c0d2009-11-13 15:28:14 -080077 if (rdmsr_safe(MSR_EFER, &header->pmode_efer_low,
78 &header->pmode_efer_high))
79 header->pmode_efer_low = header->pmode_efer_high = 0;
Pavel Macheke44b7b72008-04-10 23:28:10 +020080#endif /* !CONFIG_64BIT */
81
82 header->pmode_cr0 = read_cr0();
David Friese532c062008-08-17 23:03:40 -050083 header->pmode_cr4 = read_cr4_safe();
Kees Cook7a313662011-07-06 18:10:34 -070084 header->pmode_behavior = 0;
85 if (!rdmsr_safe(MSR_IA32_MISC_ENABLE,
86 &header->pmode_misc_en_low,
87 &header->pmode_misc_en_high))
88 header->pmode_behavior |=
89 (1 << WAKEUP_BEHAVIOR_RESTORE_MISC_ENABLE);
Pavel Macheke44b7b72008-04-10 23:28:10 +020090 header->realmode_flags = acpi_realmode_flags;
91 header->real_magic = 0x12345678;
92
93#ifndef CONFIG_64BIT
94 header->pmode_entry = (u32)&wakeup_pmode_return;
Borislav Petkovb40827fa2010-08-28 15:58:33 +020095 header->pmode_cr3 = (u32)__pa(&initial_page_table);
Pavel Macheke44b7b72008-04-10 23:28:10 +020096 saved_magic = 0x12345678;
97#else /* CONFIG_64BIT */
H. Peter Anvind1ee4332011-02-14 15:42:46 -080098 header->trampoline_segment = trampoline_address() >> 4;
Ingo Molnar1ea598c2008-06-13 20:31:54 +020099#ifdef CONFIG_SMP
H. Peter Anvin11d4c3f2011-02-04 16:14:11 -0800100 stack_start = (unsigned long)temp_stack + sizeof(temp_stack);
Rafael J. Wysocki3038eda2008-10-17 01:26:27 +0200101 early_gdt_descr.address =
102 (unsigned long)get_cpu_gdt_table(smp_processor_id());
Tejun Heo004aa322009-01-13 20:41:35 +0900103 initial_gs = per_cpu_offset(smp_processor_id());
Ingo Molnar1ea598c2008-06-13 20:31:54 +0200104#endif
Pavel Macheke44b7b72008-04-10 23:28:10 +0200105 initial_code = (unsigned long)wakeup_long64;
Jaswinder Singh Rajputce4b3c52009-04-18 13:44:57 +0200106 saved_magic = 0x123456789abcdef0L;
Pavel Macheke44b7b72008-04-10 23:28:10 +0200107#endif /* CONFIG_64BIT */
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100108
Rafael J. Wysockif1a20032011-02-08 23:42:22 +0100109 do_suspend_lowlevel();
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100110 return 0;
111}
112
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100113static int __init acpi_sleep_setup(char *str)
114{
115 while ((str != NULL) && (*str != '\0')) {
116 if (strncmp(str, "s3_bios", 7) == 0)
117 acpi_realmode_flags |= 1;
118 if (strncmp(str, "s3_mode", 7) == 0)
119 acpi_realmode_flags |= 2;
120 if (strncmp(str, "s3_beep", 7) == 0)
121 acpi_realmode_flags |= 4;
Shaohua Libdfe6b72008-07-23 21:28:41 -0700122#ifdef CONFIG_HIBERNATION
123 if (strncmp(str, "s4_nohwsig", 10) == 0)
124 acpi_no_s4_hw_signature();
125#endif
Rafael J. Wysocki72ad5d72010-07-23 22:59:09 +0200126 if (strncmp(str, "nonvs", 5) == 0)
127 acpi_nvs_nosave();
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200128 if (strncmp(str, "old_ordering", 12) == 0)
129 acpi_old_suspend_ordering();
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100130 str = strchr(str, ',');
131 if (str != NULL)
132 str += strspn(str, ", \t");
133 }
134 return 1;
135}
136
137__setup("acpi_sleep=", acpi_sleep_setup);