x86, suspend, acpi: enter Big Real Mode
[linux-2.6-block.git] / arch / x86 / kernel / acpi / sleep.c
CommitLineData
4fc2fba8
PM
1/*
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
e44b7b75
PM
13#include "realmode/wakeup.h"
14#include "sleep.h"
4fc2fba8 15
f49688d4 16unsigned long acpi_wakeup_address;
4fc2fba8 17unsigned long acpi_realmode_flags;
4fc2fba8 18
e44b7b75
PM
19/* 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
4fc2fba8 25
3bf2e774
PA
26/* 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
4fc2fba8
PM
35/**
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.
e44b7b75
PM
40 *
41 * Note that this is too late to change acpi_wakeup_address.
4fc2fba8
PM
42 */
43int acpi_save_state_mem(void)
44{
e44b7b75
PM
45 struct wakeup_header *header;
46
47 if (!acpi_realmode) {
48 printk(KERN_ERR "Could not allocate memory during boot, "
49 "S3 disabled\n");
4fc2fba8
PM
50 return -ENOMEM;
51 }
e44b7b75
PM
52 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
4b4f7280
PA
62 header->wakeup_jmp_seg = acpi_wakeup_address >> 4;
63 /* GDT[0]: GDT self-pointer */
64 header->wakeup_gdt[0] =
65 (u64)(sizeof(header->wakeup_gdt) - 1) +
66 ((u64)(acpi_wakeup_address +
67 ((char *)&header->wakeup_gdt - (char *)acpi_realmode))
68 << 16);
69 /* GDT[1]: real-mode-like code segment */
3bf2e774
PA
70 header->wakeup_gdt[1] =
71 GDT_ENTRY(0x809b, acpi_wakeup_address, 0xfffff);
4b4f7280 72 /* GDT[2]: real-mode-like data segment */
3bf2e774
PA
73 header->wakeup_gdt[2] =
74 GDT_ENTRY(0x8093, acpi_wakeup_address, 0xfffff);
4b4f7280 75
e44b7b75
PM
76#ifndef CONFIG_64BIT
77 store_gdt((struct desc_ptr *)&header->pmode_gdt);
78
79 header->pmode_efer_low = nx_enabled;
80 if (header->pmode_efer_low & 1) {
81 /* This is strange, why not save efer, always? */
82 rdmsr(MSR_EFER, header->pmode_efer_low,
83 header->pmode_efer_high);
84 }
85#endif /* !CONFIG_64BIT */
86
87 header->pmode_cr0 = read_cr0();
88 header->pmode_cr4 = read_cr4();
89 header->realmode_flags = acpi_realmode_flags;
90 header->real_magic = 0x12345678;
91
92#ifndef CONFIG_64BIT
93 header->pmode_entry = (u32)&wakeup_pmode_return;
94 header->pmode_cr3 = (u32)(swsusp_pg_dir - __PAGE_OFFSET);
95 saved_magic = 0x12345678;
96#else /* CONFIG_64BIT */
97 header->trampoline_segment = setup_trampoline() >> 4;
98 init_rsp = (unsigned long)temp_stack + 4096;
99 initial_code = (unsigned long)wakeup_long64;
100 saved_magic = 0x123456789abcdef0;
101#endif /* CONFIG_64BIT */
4fc2fba8
PM
102
103 return 0;
104}
105
106/*
107 * acpi_restore_state - undo effects of acpi_save_state_mem
108 */
109void acpi_restore_state_mem(void)
110{
111}
112
113
114/**
115 * acpi_reserve_bootmem - do _very_ early ACPI initialisation
116 *
117 * We allocate a page from the first 1MB of memory for the wakeup
118 * routine for when we come back from a sleep state. The
119 * runtime allocator allows specification of <16MB pages, but not
120 * <1MB pages.
121 */
122void __init acpi_reserve_bootmem(void)
123{
e44b7b75 124 if ((&wakeup_code_end - &wakeup_code_start) > WAKEUP_SIZE) {
4fc2fba8
PM
125 printk(KERN_ERR
126 "ACPI: Wakeup code way too big, S3 disabled.\n");
127 return;
128 }
129
e44b7b75
PM
130 acpi_realmode = (unsigned long)alloc_bootmem_low(WAKEUP_SIZE);
131
132 if (!acpi_realmode) {
4fc2fba8 133 printk(KERN_ERR "ACPI: Cannot allocate lowmem, S3 disabled.\n");
e44b7b75
PM
134 return;
135 }
136
4b4f7280 137 acpi_wakeup_address = virt_to_phys((void *)acpi_realmode);
4fc2fba8
PM
138}
139
140
141static int __init acpi_sleep_setup(char *str)
142{
143 while ((str != NULL) && (*str != '\0')) {
144 if (strncmp(str, "s3_bios", 7) == 0)
145 acpi_realmode_flags |= 1;
146 if (strncmp(str, "s3_mode", 7) == 0)
147 acpi_realmode_flags |= 2;
148 if (strncmp(str, "s3_beep", 7) == 0)
149 acpi_realmode_flags |= 4;
150 str = strchr(str, ',');
151 if (str != NULL)
152 str += strspn(str, ", \t");
153 }
154 return 1;
155}
156
157__setup("acpi_sleep=", acpi_sleep_setup);