acpi_wakecode.S revision 1.9
1/*	$NetBSD: acpi_wakecode.S,v 1.9 2007/04/28 14:03:00 joerg 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@jp.freebsd.org>
44 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@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) */
80wakeup_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	/* Only beep on reset if machdep.acpi_beep_on_reset=1 */
98	cmpb	$1,beep_on_reset
99	jne	nobeepon
100	call	beepon
101nobeepon:
102
103	/* Only reset the VBIOS if machdep.acpi_vbios_reset=1 */
104	cmpb	$1,vbios_reset
105	jne	novbiosreset
106
107	/* Kick the VBIOS. */
108	lcall	$0xc000,$3
109
110	movw	%cs,%ax
111	movw	%ax,%ds
112	movw	%ax,%ss
113novbiosreset:
114
115	/* Only beep on reset if machdep.acpi_beep_on_reset=1 */
116	cmpb	$1,beep_on_reset
117	jne	nobeepoff
118	call beepoff
119nobeepoff:
120
121	/* Get physical address of the code */
122	xorl	%esi,%esi
123	movw	%cs,%si
124	shll	$4,%esi
125
126	/* Fill 16->32 address */
127	movl	%esi,%eax
128	addl	$wakeup_32,%eax
129	movl	%eax,wakeup_sw32+2
130	jmp	1f			/* flush prefetch queue */
1311:	jmp	1f
1321:
133
134	/* Load GDT while non-paging */
135	movl	%esi,%eax
136	addl	$tmp_gdtable,%eax
137	movl	%eax,tmp_gdt+2
138	lgdt	tmp_gdt
139
140	/* Enable protected mode */
141	mov	%cr0,%eax
142	orl	$(CR0_PE),%eax
143	mov	%eax,%cr0
144
145wakeup_sw32:
146	/* Switch to protected mode by intersegmental jump */
147	ljmpl	$0x8,$0x12345678	/* Code location, to be replaced */
148
149
150	.code32
151	.align	16
152wakeup_32:
153	/*
154	 * Switched to protected mode w/o paging
155	 */
156
157	nop
158	/* Set up segment registers for protected mode */
159	movw	$GSEL(GDATA_SEL,SEL_KPL),%ax
160	movw	%ax,%ds
161	movw	%ax,%es
162	movw	%ax,%gs
163	movw	%ax,%ss
164	movw	%ax,%fs
165
166	/* Fixup TSS type field; 386 busy TSS (11) -> 386 available TSS (9) */
167#define TSS_TYPEFIX_MASK	0xf9
168	movl	physical_gdt+2(%esi),%ebx
169	movzxw	previous_tr(%esi),%ecx
170	leal	(%ebx,%ecx),%eax	/* get TSS segment descriptor */
171	andb	$TSS_TYPEFIX_MASK,5(%eax)
172
173	/* Enable paging (assumes identical mapping) */
174        movl	previous_cr3(%esi),%eax
175	movl	%eax,%cr3
176	movl	previous_cr0(%esi),%eax
177	movl	%eax,%cr0
178
179	/* Flush the prefetch queue */
180	jmp	1f
1811:	jmp	1f
1821:
183
184	nop
185
186        /* Restore registers */
187	lgdt	previous_gdt(%esi)
188	lidt	previous_idt(%esi)
189        lldt	previous_ldt(%esi)
190#if 0
191        ltr     previous_tr(%esi)
192#endif
193
194	mov	previous_cr2(%esi),%eax
195	mov	%eax,%cr2
196	mov	previous_cr4(%esi),%eax
197	mov	%eax,%cr4
198
199        movw	previous_es(%esi),%ax
200        movw	%ax,%es
201        movw	previous_fs(%esi),%ax
202        movw	%ax,%fs
203        movw	previous_gs(%esi),%ax
204        movw	%ax,%gs
205        movw	previous_ss(%esi),%ax
206        movw	%ax,%ss
207        movl	where_to_recover(%esi),%ebx
208        movw	previous_ds(%esi),%ax
209        movw	%ax,%ds
210        jmp	*%ebx
211
212beepon:
213	movb	$0xc0,%al
214	outb	%al,$0x42
215	movb	$0x04,%al
216	outb	%al,$0x42
217	inb	$0x61,%al
218	orb	$0x3,%al
219	outb	%al,$0x61
220	ret
221
222beepoff:
223	inb	$0x61,%al
224	andb	$0xfc,%al
225	outb	%al,$0x61
226	ret
227
228	.align	8
229tmp_gdt:
230	.word	0xffff
231	.long	0
232
233	.align	8, 0
234tmp_gdtable:
235	/* null */
236	.word	0, 0
237	.byte	0, 0, 0, 0
238	/* code */
239	.word	0xffff, 0
240	.byte	0, 0x9f, 0xcf, 0
241	/* data */
242	.word	0xffff, 0
243	.byte	0, 0x93, 0xcf, 0
244
245	.align	16, 0
246physical_gdt:		.word 0
247			.long 0
248previous_cr2:		.long 0
249previous_cr3:		.long 0
250previous_cr4:		.long 0
251previous_cr0:		.long 0
252previous_tr:		.word 0
253previous_gdt:		.word 0
254			.long 0
255previous_ldt:		.word 0
256previous_idt:		.word 0
257			.long 0
258previous_ds:		.word 0
259previous_es:		.word 0
260previous_fs:		.word 0
261previous_gs:		.word 0
262previous_ss:		.word 0
263where_to_recover:	.long 0
264vbios_reset:		.byte 0
265beep_on_reset:		.byte 0
266