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