xref: /netbsd-src/sys/arch/i386/acpi/acpi_wakecode.S (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1/*	$NetBSD: acpi_wakecode.S,v 1.10 2007/12/09 20:27:44 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/specialreg.h>
74#include <machine/segments.h>
75
76#define	ACPI_WAKEUP_ADDR	0x3000
77
78	.text
79	.code16
80	.org 0	/* ACPI spec says: cs==(phys>>8), ip==(phys&0x000F) */
81	.globl wakeup_16
82wakeup_16:
83	nop
84	cli
85	cld
86
87	/* Set up segment registers for real mode */
88	movw	%cs,%ax
89	movw	%ax,%ds
90	movw	%ax,%ss
91
92	/* Small call stack */
93	mov	$0x1000,%sp
94
95	/* Clear flags */
96	pushl	$0
97	popfl
98
99	/* Only beep on reset if machdep.acpi_beep_on_reset=1 */
100	cmpb	$1,WAKEUP_beep_on_reset
101	jne	1f
102	movb	$0xc0,%al
103	outb	%al,$0x42
104	movb	$0x04,%al
105	outb	%al,$0x42
106	inb	$0x61,%al
107	orb	$0x3,%al
108	outb	%al,$0x61
1091:
110
111	/* Only reset the VBIOS if machdep.acpi_vbios_reset=1 */
112	cmpb	$1,WAKEUP_vbios_reset
113	jne	1f
114
115	/* Kick the VBIOS. */
116	lcall	$0xc000,$3
117
118	movw	%cs,%ax
119	movw	%ax,%ds
120	movw	%ax,%ss
1211:
122
123	/* Disable beep again if machdep.acpi_beep_on_reset=1 */
124	cmpb	$1,WAKEUP_beep_on_reset
125	jne	1
126	inb	$0x61,%al
127	andb	$0xfc,%al
128	outb	%al,$0x61
1291:
130
131	/* Load GDT while non-paging */
132	lgdt	tmp_gdt
133
134	/* Enable protected mode */
135	mov	%cr0,%eax
136	orl	$(CR0_PE),%eax
137	mov	%eax,%cr0
138
139	/* Switch to protected mode by intersegmental jump */
140	ljmpl	$0x8,$wakeup_32	+ ACPI_WAKEUP_ADDR
141
142
143	.code32
144	.align	16
145wakeup_32:
146	/*
147	 * Switched to protected mode w/o paging
148	 */
149
150	nop
151	/* Set up segment registers for protected mode */
152	movw	$GSEL(GDATA_SEL,SEL_KPL),%ax
153	movw	%ax,%ds
154
155	/* Restore PSE and other settings before enabling paging. */
156	movl	WAKEUP_r_cr4 + ACPI_WAKEUP_ADDR,%eax
157	movl	%eax,%cr4
158
159	/* Enable paging (assumes identical mapping) */
160	movl	WAKEUP_r_cr3 + ACPI_WAKEUP_ADDR,%eax
161	movl	%eax,%cr3
162	movl	WAKEUP_r_cr0 + ACPI_WAKEUP_ADDR,%eax
163	movl	%eax,%cr0
164
165	/* Flush the prefetch queue */
166	jmp	1f
1671:	jmp	1f
1681:
169
170	nop
171
172	/* Restore registers */
173	lgdt	WAKEUP_r_gdt + ACPI_WAKEUP_ADDR
174
175	movl	WAKEUP_restorecpu + ACPI_WAKEUP_ADDR,%ebx
176	movw	WAKEUP_r_ds + ACPI_WAKEUP_ADDR,%ax
177	movw	%ax,%ds
178	jmp	*%ebx
179
180	.align	8
181tmp_gdt:
182	.word	0xffff
183	.long	tmp_gdtable + ACPI_WAKEUP_ADDR
184
185	.align	8, 0
186tmp_gdtable:
187	/* null */
188	.word	0, 0
189	.byte	0, 0, 0, 0
190	/* code */
191	.word	0xffff, 0
192	.byte	0, 0x9f, 0xcf, 0
193	/* data */
194	.word	0xffff, 0
195	.byte	0, 0x93, 0xcf, 0
196
197	.align	16, 0
198	.global WAKEUP_r_cr0
199WAKEUP_r_cr0:		.long 0
200	.global WAKEUP_r_cr3
201WAKEUP_r_cr3:		.long 0
202	.global WAKEUP_r_cr4
203WAKEUP_r_cr4:		.long 0
204
205	.global WAKEUP_r_gdt
206WAKEUP_r_gdt:		.word 0
207			.long 0
208	.global WAKEUP_r_ds
209WAKEUP_r_ds:		.word 0
210	.global WAKEUP_restorecpu
211WAKEUP_restorecpu:	.long 0
212
213	.global WAKEUP_vbios_reset
214WAKEUP_vbios_reset:	.byte 0
215	.global WAKEUP_beep_on_reset
216WAKEUP_beep_on_reset:	.byte 0
217