acpi_wakecode.S revision 1.1.2.10 1 /* $NetBSD: acpi_wakecode.S,v 1.1.2.10 2007/10/26 15:42:04 joerg Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Takuya SHIOZAKI.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39
40 /*
41 * This code is derived from FreeBSD. Original copyrights:
42 *
43 * Copyright (c) 2001 Takanori Watanabe <takawata (at) jp.freebsd.org>
44 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki (at) jp.freebsd.org>
45 * All rights reserved.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * FreeBSD: src/sys/i386/acpica/acpi_wakecode.S,v 1.1 2001/07/20 06:07:31 takawata Exp
69 */
70
71 #define _LOCORE
72
73 #include <machine/psl.h>
74 #include <machine/specialreg.h>
75 #include <machine/segments.h>
76
77 #define ACPI_WAKEUP_ADDR 0x3000
78
79 .text
80 .code16
81 .org 0 /* ACPI spec says: cs==(phys>>8), ip==(phys&0x000F) */
82 .globl wakeup_16
83 wakeup_16:
84 nop
85 cli
86 cld
87
88 /* Set up segment registers for real mode */
89 movw %cs,%ax
90 movw %ax,%ds
91 movw %ax,%ss
92
93 /* Small call stack */
94 mov $0x1000,%sp
95
96 /* Clear flags */
97 pushl $0
98 popfl
99
100 /* Only beep on reset if machdep.acpi_beep_on_reset=1 */
101 cmpb $1,WAKEUP_beep_on_reset
102 jne 1f
103 movb $0xc0,%al
104 outb %al,$0x42
105 movb $0x04,%al
106 outb %al,$0x42
107 inb $0x61,%al
108 orb $0x3,%al
109 outb %al,$0x61
110 1:
111
112 /* Only reset the VBIOS if machdep.acpi_vbios_reset=1 */
113 cmpb $1,WAKEUP_vbios_reset
114 jne 1f
115
116 /* Kick the VBIOS. */
117 lcall $0xc000,$3
118
119 /* Reset registers in case the VBIOS changed them. */
120 movw %cs,%ax
121 movw %ax,%ds
122 movw %ax,%ss
123 1:
124
125 /* Disable beep again if machdep.acpi_beep_on_reset=1 */
126 cmpb $1,WAKEUP_beep_on_reset
127 jne 1f
128 inb $0x61,%al
129 andb $0xfc,%al
130 outb %al,$0x61
131 1:
132
133 /* Load temporary 32bit GDT */
134 data32 addr32 lgdt tmp_gdt
135
136 /* Enable protected mode w/o paging */
137 mov %cr0,%eax
138 orl $(CR0_PE),%eax
139 mov %eax,%cr0
140
141 wakeup_sw32:
142 /*
143 * Switch to protected mode by intersegmental jump.
144 * Target and everything else has to compensate for the new origin
145 * as this is using the flat memory model now.
146 */
147
148 ljmpl $0x8,$wakeup_32 + ACPI_WAKEUP_ADDR
149
150 .code32
151 .align 16
152 wakeup_32:
153 /*
154 * Switched to protected mode w/o paging
155 */
156
157 nop
158 /* Set up segment registers and initial stack for protected mode */
159 movw $0x10, %ax
160 movw %ax,%ds
161 movw %ax,%ss
162
163 movl $(ACPI_WAKEUP_ADDR + 4096),%esp
164
165 /* First, reset the PSL. */
166 pushl $PSL_MBO
167 popfl
168
169 /* Enable PAE and potentially PSE */
170 movl WAKEUP_r_cr4 + ACPI_WAKEUP_ADDR,%eax
171 movl %eax,%cr4
172
173 /* Enable SYSCALL extension and Long Mode */
174 movl $MSR_EFER,%ecx
175 rdmsr
176 xorl %eax,%eax
177 orl $(EFER_LME|EFER_SCE),%eax
178 wrmsr
179
180 /* Load temporary PML4, code will switch to full PML4 later */
181 movl WAKEUP_r_cr3 + ACPI_WAKEUP_ADDR,%eax
182 movl %eax,%cr3
183
184 /* Reload cr0 with normal values, activating paging effectively */
185 movl WAKEUP_r_cr0 + ACPI_WAKEUP_ADDR,%eax
186 movl %eax,%cr0
187 /* Flush prefetch queue */
188 jmp 1f
189 1:
190 /* Switch to temporary 64bit GDT */
191 lgdt tmp_gdt64 + ACPI_WAKEUP_ADDR
192
193 /* Switch to long mode using intersegmental jump. */
194 ljmp $0x8, $wakeup_64 + ACPI_WAKEUP_ADDR
195
196 .code64
197 wakeup_64:
198
199 /* Reload normal segment registers with correct values */
200 movw $GSEL(GDATA_SEL, SEL_KPL),%ax
201 movw %ax,%ds
202 movw %ax,%es
203 movw %ax,%ss
204
205 movq WAKEUP_restorecpu + ACPI_WAKEUP_ADDR,%rbx
206
207 /* Switch to the normal kernel GDT */
208 lgdt WAKEUP_r_gdt + ACPI_WAKEUP_ADDR
209
210 /* Continue with wakeup in the high-level wakeup code */
211 jmp *%rbx
212
213 .align 8
214 tmp_gdt:
215 .word 0xffff
216 .long tmp_gdtable + ACPI_WAKEUP_ADDR
217
218 .align 8, 0
219 tmp_gdtable:
220 /* null */
221 .word 0, 0
222 .byte 0, 0, 0, 0
223 /* code */
224 .word 0xffff, 0
225 .byte 0, 0x9f, 0xcf, 0
226 /* data */
227 .word 0xffff, 0
228 .byte 0, 0x93, 0xcf, 0
229
230 tmp_gdt64:
231 .word 0xffff
232 .long tmp_gdtable64 + ACPI_WAKEUP_ADDR
233
234 tmp_gdtable64:
235 .quad 0x0000000000000000
236 .quad 0x00af9a000000ffff
237 .quad 0x00cf92000000ffff
238
239 .align 16, 0
240 .global WAKEUP_r_cr0
241 WAKEUP_r_cr0: .quad 0
242 .global WAKEUP_r_cr3
243 WAKEUP_r_cr3: .quad 0
244 .global WAKEUP_r_cr4
245 WAKEUP_r_cr4: .quad 0
246
247 .global WAKEUP_r_gdt
248 WAKEUP_r_gdt: .word 0
249 .quad 0
250 .global WAKEUP_restorecpu
251 WAKEUP_restorecpu: .quad 0
252
253 .global WAKEUP_vbios_reset
254 WAKEUP_vbios_reset: .byte 0
255 .global WAKEUP_beep_on_reset
256 WAKEUP_beep_on_reset: .byte 0
257