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