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