acpi_wakecode.S revision 1.7 1 /* $NetBSD: acpi_wakecode.S,v 1.7 2006/06/17 17:11:53 jmcneill 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/asm.h>
74 #include <machine/specialreg.h>
75 #include <machine/param.h>
76 #include <machine/segments.h>
77
78 .code16
79 .org 0 /* ACPI spec says: cs==(phys>>8), ip==(phys&0x000F) */
80 wakeup_16:
81 nop
82 cli
83 cld
84
85 /* Set up segment registers for real mode */
86 movw %cs,%ax
87 movw %ax,%ds
88 movw %ax,%ss
89
90 /* Small call stack */
91 movw $0x1000,%sp
92
93 /* Clear flags */
94 pushl $0
95 popfl
96
97 call beepon
98
99 /* The following needs to be configurable */
100 /*#ifdef notyet*/
101 /* Kick the VBIOS. */
102 lcall $0xc000,$3
103
104 movw %cs,%ax
105 movw %ax,%ds
106 movw %ax,%ss
107
108 /*#endif*/
109 call beepoff
110
111 /* Get physical address of the code */
112 xorl %esi,%esi
113 movw %cs,%si
114 shll $4,%esi
115
116 /* Fill 16->32 address */
117 movl %esi,%eax
118 addl $wakeup_32,%eax
119 movl %eax,wakeup_sw32+2
120 jmp 1f /* flush prefetch queue */
121 1: jmp 1f
122 1:
123
124 /* Load GDT while non-paging */
125 movl %esi,%eax
126 addl $tmp_gdtable,%eax
127 movl %eax,tmp_gdt+2
128 lgdt tmp_gdt
129
130 /* Enable protected mode */
131 mov %cr0,%eax
132 orl $(CR0_PE),%eax
133 mov %eax,%cr0
134
135 wakeup_sw32:
136 /* Switch to protected mode by intersegmental jump */
137 ljmpl $0x8,$0x12345678 /* Code location, to be replaced */
138
139
140 .code32
141 .align 16
142 wakeup_32:
143 /*
144 * Switched to protected mode w/o paging
145 */
146
147 nop
148 /* Set up segment registers for protected mode */
149 movw $GSEL(GDATA_SEL,SEL_KPL),%ax
150 movw %ax,%ds
151 movw %ax,%es
152 movw %ax,%gs
153 movw %ax,%ss
154 movw %ax,%fs
155
156 /* Fixup TSS type field; 386 busy TSS (11) -> 386 available TSS (9) */
157 #define TSS_TYPEFIX_MASK 0xf9
158 movl physical_gdt+2(%esi),%ebx
159 movzxw previous_tr(%esi),%ecx
160 leal (%ebx,%ecx),%eax /* get TSS segment descriptor */
161 andb $TSS_TYPEFIX_MASK,5(%eax)
162
163 /* Enable paging (assumes identical mapping) */
164 movl previous_cr3(%esi),%eax
165 movl %eax,%cr3
166 movl previous_cr0(%esi),%eax
167 movl %eax,%cr0
168
169 /* Flush the prefetch queue */
170 jmp 1f
171 1: jmp 1f
172 1:
173
174 nop
175
176 /* Restore registers */
177 lgdt previous_gdt(%esi)
178 lidt previous_idt(%esi)
179 lldt previous_ldt(%esi)
180 #if 0
181 ltr previous_tr(%esi)
182 #endif
183
184 mov previous_cr2(%esi),%eax
185 mov %eax,%cr2
186 mov previous_cr4(%esi),%eax
187 mov %eax,%cr4
188
189 movw previous_es(%esi),%ax
190 movw %ax,%es
191 movw previous_fs(%esi),%ax
192 movw %ax,%fs
193 movw previous_gs(%esi),%ax
194 movw %ax,%gs
195 movw previous_ss(%esi),%ax
196 movw %ax,%ss
197 movl where_to_recover(%esi),%ebx
198 movw previous_ds(%esi),%ax
199 movw %ax,%ds
200 jmp *%ebx
201
202 beepon:
203 movb $0xc0,%al
204 outb %al,$0x42
205 movb $0x04,%al
206 outb %al,$0x42
207 inb $0x61,%al
208 orb $0x3,%al
209 outb %al,$0x61
210 ret
211
212 beepoff:
213 inb $0x61,%al
214 andb $0xfc,%al
215 outb %al,$0x61
216 ret
217
218 .align 8
219 tmp_gdt:
220 .word 0xffff
221 .long 0
222
223 .align 8, 0
224 tmp_gdtable:
225 /* null */
226 .word 0, 0
227 .byte 0, 0, 0, 0
228 /* code */
229 .word 0xffff, 0
230 .byte 0, 0x9f, 0xcf, 0
231 /* data */
232 .word 0xffff, 0
233 .byte 0, 0x93, 0xcf, 0
234
235 .align 16, 0
236 physical_gdt: .word 0
237 .long 0
238 previous_cr2: .long 0
239 previous_cr3: .long 0
240 previous_cr4: .long 0
241 previous_cr0: .long 0
242 previous_tr: .word 0
243 previous_gdt: .word 0
244 .long 0
245 previous_ldt: .word 0
246 previous_idt: .word 0
247 .long 0
248 previous_ds: .word 0
249 previous_es: .word 0
250 previous_fs: .word 0
251 previous_gs: .word 0
252 previous_ss: .word 0
253 where_to_recover: .long 0
254