blob: 41bb130c31db84d003d504d1a89bbab56a15c4e0 [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
5 * Copyright (C) 2001-2003 Pavel Machek <pavel@suse.cz>
6 */
7
8#include <linux/acpi.h>
9#include <linux/bootmem.h>
10#include <linux/dmi.h>
11#include <linux/cpumask.h>
12
Pavel Macheke44b7b72008-04-10 23:28:10 +020013#include "realmode/wakeup.h"
14#include "sleep.h"
Pavel Machek4fc2fba2008-01-30 13:32:54 +010015
Paolo Ciarrocchif49688d2008-02-22 23:11:39 +010016unsigned long acpi_wakeup_address;
Pavel Machek4fc2fba2008-01-30 13:32:54 +010017unsigned long acpi_realmode_flags;
Pavel Machek4fc2fba2008-01-30 13:32:54 +010018
Pavel Macheke44b7b72008-04-10 23:28:10 +020019/* address in low memory of the wakeup routine. */
20static unsigned long acpi_realmode;
21
22#ifdef CONFIG_64BIT
23static char temp_stack[10240];
24#endif
Pavel Machek4fc2fba2008-01-30 13:32:54 +010025
H. Peter Anvin3bf2e772008-07-13 21:18:02 -070026/* XXX: this macro should move to asm-x86/segment.h and be shared with the
27 boot code... */
28#define GDT_ENTRY(flags, base, limit) \
29 (((u64)(base & 0xff000000) << 32) | \
30 ((u64)flags << 40) | \
31 ((u64)(limit & 0x00ff0000) << 32) | \
32 ((u64)(base & 0x00ffffff) << 16) | \
33 ((u64)(limit & 0x0000ffff)))
34
Pavel Machek4fc2fba2008-01-30 13:32:54 +010035/**
36 * acpi_save_state_mem - save kernel state
37 *
38 * Create an identity mapped page table and copy the wakeup routine to
39 * low memory.
Pavel Macheke44b7b72008-04-10 23:28:10 +020040 *
41 * Note that this is too late to change acpi_wakeup_address.
Pavel Machek4fc2fba2008-01-30 13:32:54 +010042 */
43int acpi_save_state_mem(void)
44{
Pavel Macheke44b7b72008-04-10 23:28:10 +020045 struct wakeup_header *header;
46
47 if (!acpi_realmode) {
48 printk(KERN_ERR "Could not allocate memory during boot, "
49 "S3 disabled\n");
Pavel Machek4fc2fba2008-01-30 13:32:54 +010050 return -ENOMEM;
51 }
Pavel Macheke44b7b72008-04-10 23:28:10 +020052 memcpy((void *)acpi_realmode, &wakeup_code_start, WAKEUP_SIZE);
53
54 header = (struct wakeup_header *)(acpi_realmode + HEADER_OFFSET);
55 if (header->signature != 0x51ee1111) {
56 printk(KERN_ERR "wakeup header does not match\n");
57 return -EINVAL;
58 }
59
60 header->video_mode = saved_video_mode;
61
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020062 header->wakeup_jmp_seg = acpi_wakeup_address >> 4;
H. Peter Anvin065cb3d2008-07-14 11:44:26 -070063
64 /*
65 * Set up the wakeup GDT. We set these up as Big Real Mode,
66 * that is, with limits set to 4 GB. At least the Lenovo
67 * Thinkpad X61 is known to need this for the video BIOS
68 * initialization quirk to work; this is likely to also
69 * be the case for other laptops or integrated video devices.
70 */
71
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020072 /* GDT[0]: GDT self-pointer */
73 header->wakeup_gdt[0] =
74 (u64)(sizeof(header->wakeup_gdt) - 1) +
75 ((u64)(acpi_wakeup_address +
76 ((char *)&header->wakeup_gdt - (char *)acpi_realmode))
77 << 16);
H. Peter Anvin065cb3d2008-07-14 11:44:26 -070078 /* GDT[1]: big real mode-like code segment */
H. Peter Anvin3bf2e772008-07-13 21:18:02 -070079 header->wakeup_gdt[1] =
80 GDT_ENTRY(0x809b, acpi_wakeup_address, 0xfffff);
H. Peter Anvin065cb3d2008-07-14 11:44:26 -070081 /* GDT[2]: big real mode-like data segment */
H. Peter Anvin3bf2e772008-07-13 21:18:02 -070082 header->wakeup_gdt[2] =
83 GDT_ENTRY(0x8093, acpi_wakeup_address, 0xfffff);
H. Peter Anvin4b4f7282008-06-24 23:03:48 +020084
Pavel Macheke44b7b72008-04-10 23:28:10 +020085#ifndef CONFIG_64BIT
86 store_gdt((struct desc_ptr *)&header->pmode_gdt);
87
88 header->pmode_efer_low = nx_enabled;
89 if (header->pmode_efer_low & 1) {
90 /* This is strange, why not save efer, always? */
91 rdmsr(MSR_EFER, header->pmode_efer_low,
92 header->pmode_efer_high);
93 }
94#endif /* !CONFIG_64BIT */
95
96 header->pmode_cr0 = read_cr0();
97 header->pmode_cr4 = read_cr4();
98 header->realmode_flags = acpi_realmode_flags;
99 header->real_magic = 0x12345678;
100
101#ifndef CONFIG_64BIT
102 header->pmode_entry = (u32)&wakeup_pmode_return;
103 header->pmode_cr3 = (u32)(swsusp_pg_dir - __PAGE_OFFSET);
104 saved_magic = 0x12345678;
105#else /* CONFIG_64BIT */
106 header->trampoline_segment = setup_trampoline() >> 4;
107 init_rsp = (unsigned long)temp_stack + 4096;
108 initial_code = (unsigned long)wakeup_long64;
109 saved_magic = 0x123456789abcdef0;
110#endif /* CONFIG_64BIT */
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100111
112 return 0;
113}
114
115/*
116 * acpi_restore_state - undo effects of acpi_save_state_mem
117 */
118void acpi_restore_state_mem(void)
119{
120}
121
122
123/**
124 * acpi_reserve_bootmem - do _very_ early ACPI initialisation
125 *
126 * We allocate a page from the first 1MB of memory for the wakeup
127 * routine for when we come back from a sleep state. The
128 * runtime allocator allows specification of <16MB pages, but not
129 * <1MB pages.
130 */
131void __init acpi_reserve_bootmem(void)
132{
Pavel Macheke44b7b72008-04-10 23:28:10 +0200133 if ((&wakeup_code_end - &wakeup_code_start) > WAKEUP_SIZE) {
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100134 printk(KERN_ERR
135 "ACPI: Wakeup code way too big, S3 disabled.\n");
136 return;
137 }
138
Pavel Macheke44b7b72008-04-10 23:28:10 +0200139 acpi_realmode = (unsigned long)alloc_bootmem_low(WAKEUP_SIZE);
140
141 if (!acpi_realmode) {
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100142 printk(KERN_ERR "ACPI: Cannot allocate lowmem, S3 disabled.\n");
Pavel Macheke44b7b72008-04-10 23:28:10 +0200143 return;
144 }
145
H. Peter Anvin4b4f7282008-06-24 23:03:48 +0200146 acpi_wakeup_address = virt_to_phys((void *)acpi_realmode);
Pavel Machek4fc2fba2008-01-30 13:32:54 +0100147}
148
149
150static int __init acpi_sleep_setup(char *str)
151{
152 while ((str != NULL) && (*str != '\0')) {
153 if (strncmp(str, "s3_bios", 7) == 0)
154 acpi_realmode_flags |= 1;
155 if (strncmp(str, "s3_mode", 7) == 0)
156 acpi_realmode_flags |= 2;
157 if (strncmp(str, "s3_beep", 7) == 0)
158 acpi_realmode_flags |= 4;
159 str = strchr(str, ',');
160 if (str != NULL)
161 str += strspn(str, ", \t");
162 }
163 return 1;
164}
165
166__setup("acpi_sleep=", acpi_sleep_setup);