2 * sleep.c - x86-specific ACPI sleep support.
4 * Copyright (C) 2001-2003 Patrick Mochel
5 * Copyright (C) 2001-2003 Pavel Machek <pavel@suse.cz>
8 #include <linux/acpi.h>
9 #include <linux/bootmem.h>
10 #include <linux/dmi.h>
11 #include <linux/cpumask.h>
13 #include "realmode/wakeup.h"
16 unsigned long acpi_wakeup_address;
17 unsigned long acpi_realmode_flags;
19 /* address in low memory of the wakeup routine. */
20 static unsigned long acpi_realmode;
23 static char temp_stack[10240];
26 /* XXX: this macro should move to asm-x86/segment.h and be shared with the
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)))
36 * acpi_save_state_mem - save kernel state
38 * Create an identity mapped page table and copy the wakeup routine to
41 * Note that this is too late to change acpi_wakeup_address.
43 int acpi_save_state_mem(void)
45 struct wakeup_header *header;
48 printk(KERN_ERR "Could not allocate memory during boot, "
52 memcpy((void *)acpi_realmode, &wakeup_code_start, WAKEUP_SIZE);
54 header = (struct wakeup_header *)(acpi_realmode + HEADER_OFFSET);
55 if (header->signature != 0x51ee1111) {
56 printk(KERN_ERR "wakeup header does not match\n");
60 header->video_mode = saved_video_mode;
62 header->wakeup_jmp_seg = acpi_wakeup_address >> 4;
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.
72 /* 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))
78 /* GDT[1]: big real mode-like code segment */
79 header->wakeup_gdt[1] =
80 GDT_ENTRY(0x809b, acpi_wakeup_address, 0xfffff);
81 /* GDT[2]: big real mode-like data segment */
82 header->wakeup_gdt[2] =
83 GDT_ENTRY(0x8093, acpi_wakeup_address, 0xfffff);
86 store_gdt((struct desc_ptr *)&header->pmode_gdt);
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);
94 #endif /* !CONFIG_64BIT */
96 header->pmode_cr0 = read_cr0();
97 header->pmode_cr4 = read_cr4();
98 header->realmode_flags = acpi_realmode_flags;
99 header->real_magic = 0x12345678;
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;
108 stack_start.sp = temp_stack + 4096;
110 initial_code = (unsigned long)wakeup_long64;
111 saved_magic = 0x123456789abcdef0;
112 #endif /* CONFIG_64BIT */
118 * acpi_restore_state - undo effects of acpi_save_state_mem
120 void acpi_restore_state_mem(void)
126 * acpi_reserve_bootmem - do _very_ early ACPI initialisation
128 * We allocate a page from the first 1MB of memory for the wakeup
129 * routine for when we come back from a sleep state. The
130 * runtime allocator allows specification of <16MB pages, but not
133 void __init acpi_reserve_bootmem(void)
135 if ((&wakeup_code_end - &wakeup_code_start) > WAKEUP_SIZE) {
137 "ACPI: Wakeup code way too big, S3 disabled.\n");
141 acpi_realmode = (unsigned long)alloc_bootmem_low(WAKEUP_SIZE);
143 if (!acpi_realmode) {
144 printk(KERN_ERR "ACPI: Cannot allocate lowmem, S3 disabled.\n");
148 acpi_wakeup_address = virt_to_phys((void *)acpi_realmode);
152 static int __init acpi_sleep_setup(char *str)
154 while ((str != NULL) && (*str != '\0')) {
155 if (strncmp(str, "s3_bios", 7) == 0)
156 acpi_realmode_flags |= 1;
157 if (strncmp(str, "s3_mode", 7) == 0)
158 acpi_realmode_flags |= 2;
159 if (strncmp(str, "s3_beep", 7) == 0)
160 acpi_realmode_flags |= 4;
161 str = strchr(str, ',');
163 str += strspn(str, ", \t");
168 __setup("acpi_sleep=", acpi_sleep_setup);