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