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