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@ucw.cz>
6  */
7 
8 #include <linux/acpi.h>
9 #include <linux/bootmem.h>
10 #include <linux/memblock.h>
11 #include <linux/dmi.h>
12 #include <linux/cpumask.h>
13 #include <asm/segment.h>
14 #include <asm/desc.h>
15 #include <asm/pgtable.h>
16 #include <asm/cacheflush.h>
17 
18 #include "realmode/wakeup.h"
19 #include "sleep.h"
20 
21 unsigned long acpi_realmode_flags;
22 
23 #if defined(CONFIG_SMP) && defined(CONFIG_64BIT)
24 static char temp_stack[4096];
25 #endif
26 
acpi_enter_s3(void)27 asmlinkage void acpi_enter_s3(void)
28 {
29 	acpi_enter_sleep_state(3, wake_sleep_flags);
30 }
31 /**
32  * acpi_suspend_lowlevel - save kernel state
33  *
34  * Create an identity mapped page table and copy the wakeup routine to
35  * low memory.
36  */
acpi_suspend_lowlevel(void)37 int acpi_suspend_lowlevel(void)
38 {
39 	struct wakeup_header *header;
40 	/* address in low memory of the wakeup routine. */
41 	char *acpi_realmode;
42 
43 	acpi_realmode = TRAMPOLINE_SYM(acpi_wakeup_code);
44 
45 	header = (struct wakeup_header *)(acpi_realmode + WAKEUP_HEADER_OFFSET);
46 	if (header->signature != WAKEUP_HEADER_SIGNATURE) {
47 		printk(KERN_ERR "wakeup header does not match\n");
48 		return -EINVAL;
49 	}
50 
51 	header->video_mode = saved_video_mode;
52 
53 	header->wakeup_jmp_seg = acpi_wakeup_address >> 4;
54 
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 
63 	/* GDT[0]: GDT self-pointer */
64 	header->wakeup_gdt[0] =
65 		(u64)(sizeof(header->wakeup_gdt) - 1) +
66 		((u64)__pa(&header->wakeup_gdt) << 16);
67 	/* GDT[1]: big real mode-like code segment */
68 	header->wakeup_gdt[1] =
69 		GDT_ENTRY(0x809b, acpi_wakeup_address, 0xfffff);
70 	/* GDT[2]: big real mode-like data segment */
71 	header->wakeup_gdt[2] =
72 		GDT_ENTRY(0x8093, acpi_wakeup_address, 0xfffff);
73 
74 #ifndef CONFIG_64BIT
75 	store_gdt((struct desc_ptr *)&header->pmode_gdt);
76 
77 	if (rdmsr_safe(MSR_EFER, &header->pmode_efer_low,
78 		       &header->pmode_efer_high))
79 		header->pmode_efer_low = header->pmode_efer_high = 0;
80 #endif /* !CONFIG_64BIT */
81 
82 	header->pmode_cr0 = read_cr0();
83 	header->pmode_cr4 = read_cr4_safe();
84 	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);
90 	header->realmode_flags = acpi_realmode_flags;
91 	header->real_magic = 0x12345678;
92 
93 #ifndef CONFIG_64BIT
94 	header->pmode_entry = (u32)&wakeup_pmode_return;
95 	header->pmode_cr3 = (u32)__pa(&initial_page_table);
96 	saved_magic = 0x12345678;
97 #else /* CONFIG_64BIT */
98 	header->trampoline_segment = trampoline_address() >> 4;
99 #ifdef CONFIG_SMP
100 	stack_start = (unsigned long)temp_stack + sizeof(temp_stack);
101 	early_gdt_descr.address =
102 			(unsigned long)get_cpu_gdt_table(smp_processor_id());
103 	initial_gs = per_cpu_offset(smp_processor_id());
104 #endif
105 	initial_code = (unsigned long)wakeup_long64;
106        saved_magic = 0x123456789abcdef0L;
107 #endif /* CONFIG_64BIT */
108 
109 	do_suspend_lowlevel();
110 	return 0;
111 }
112 
acpi_sleep_setup(char * str)113 static 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;
122 #ifdef CONFIG_HIBERNATION
123 		if (strncmp(str, "s4_nohwsig", 10) == 0)
124 			acpi_no_s4_hw_signature();
125 #endif
126 		if (strncmp(str, "nonvs", 5) == 0)
127 			acpi_nvs_nosave();
128 		if (strncmp(str, "old_ordering", 12) == 0)
129 			acpi_old_suspend_ordering();
130 		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);
138