1/*        $NetBSD: acpi_wakecode.S,v 1.18 2016/07/24 14:09:22 maxv 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * This code is derived from FreeBSD.  Original copyrights:
34 *
35 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
36 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
37 * All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 *    notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 *    notice, this list of conditions and the following disclaimer in the
46 *    documentation and/or other materials provided with the distribution.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 *        FreeBSD: src/sys/i386/acpica/acpi_wakecode.S,v 1.1 2001/07/20 06:07:31 takawata Exp
61 */
62
63#define _LOCORE
64
65#include <machine/specialreg.h>
66#include <machine/segments.h>
67
68#define   ACPI_WAKEUP_ADDR    0x3000
69
70          .text
71          .code16
72          .org 0    /* ACPI spec says: cs==(phys>>8), ip==(phys&0x000F) */
73          .globl wakeup_16
74wakeup_16:
75          nop
76          cli
77          cld
78
79          /* Set up segment registers for real mode */
80          movw      %cs,%ax
81          movw      %ax,%ds
82          movw      %ax,%ss
83
84          /* Small call stack */
85          mov       $0x1000,%sp
86
87          /* Clear flags */
88          pushl     $0
89          popfl
90
91          /* Only beep on reset if machdep.acpi_beep_on_reset=1 */
92          cmpb      $1,WAKEUP_beep_on_reset
93          jne       1f
94          movb      $0xc0,%al
95          outb      %al,$0x42
96          movb      $0x04,%al
97          outb      %al,$0x42
98          inb       $0x61,%al
99          orb       $0x3,%al
100          outb      %al,$0x61
1011:
102
103          /* Only reset the VBIOS if machdep.acpi_vbios_reset=1 */
104          cmpb      $1,WAKEUP_vbios_reset
105          jne       1f
106
107          /* Kick the VBIOS. */
108          lcall     $0xc000,$3
109
110          movw      %cs,%ax
111          movw      %ax,%ds
112          movw      %ax,%ss
113
114          /* If we need to restore a VESA VBE mode, do it now */
115          cmpb      $0,WAKEUP_vesa_modenum
116          je        1f
117          movw      WAKEUP_vesa_modenum,%bx
118          orw       $0x4000,%bx
119          movw      $0x4f02,%ax
120          int       $0x10
121
122          movw      %cs,%ax
123          movw      %ax,%ds
124          movw      %ax,%ss
1251:
126
127          /* Disable beep again if machdep.acpi_beep_on_reset=1 */
128          cmpb      $1,WAKEUP_beep_on_reset
129          jne       1f
130          inb       $0x61,%al
131          andb      $0xfc,%al
132          outb      %al,$0x61
1331:
134
135          /* Load GDT while non-paging */
136          lgdt      tmp_gdt
137
138          /* Enable protected mode without paging */
139          mov       %cr0,%eax
140          orl       $(CR0_PE),%eax
141          mov       %eax,%cr0
142
143          /* Switch to protected mode by intersegmental jump */
144          ljmpl     $0x8,$wakeup_32 + ACPI_WAKEUP_ADDR
145
146          .code32
147          .align    16
148wakeup_32:
149          /*
150           * Switched to protected mode w/o paging
151           */
152          nop
153
154          /* Set up segment registers for protected mode */
155          movw      $GSEL(GDATA_SEL,SEL_KPL),%ax
156          movw      %ax,%ds
157
158          /* Enable potentially PSE and PAE */
159          movl      WAKEUP_r_cr4 + ACPI_WAKEUP_ADDR,%eax
160          movl      %eax,%cr4
161
162          /*
163           * Load the correct MSR EFER value now to not depend on the
164           * data segment register. After this point, no instruction is
165           * allowed to clobber %ebx until wrmsr.
166           */
167          movl      WAKEUP_efer + ACPI_WAKEUP_ADDR,%ebx
168
169          /* Load temporary page table, we will switch to full page table later */
170          movl      WAKEUP_r_cr3 + ACPI_WAKEUP_ADDR,%eax
171          movl      %eax,%cr3
172
173          /* Enable paging */
174          movl      %cr0,%eax
175          orl       $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM),%eax
176          movl      %eax,%cr0
177
178          /* Flush the prefetch queue */
179          jmp       1f
1801:        jmp       1f
1811:
182
183          nop
184
185          /*
186           * Load the normal system value of MSR EFER.  This includes
187           * enabling NXE (if supported).
188           */
189          movl      %ebx,%eax
190          movl      $0,%edx
191          movl      $MSR_EFER,%ecx
192          wrmsr
193
194          /* Restore registers */
195          movl      WAKEUP_curcpu + ACPI_WAKEUP_ADDR,%edx
196          movl      WAKEUP_restorecpu + ACPI_WAKEUP_ADDR,%ebx
197
198          /* Continue with wakeup in the high-level wakeup code */
199          jmp       *%ebx
200
201          .align    8
202tmp_gdt:
203          .word     0xffff
204          .long     tmp_gdtable + ACPI_WAKEUP_ADDR
205
206          .align    8, 0
207tmp_gdtable:
208          /* null */
209          .word     0, 0
210          .byte     0, 0, 0, 0
211          /* code */
212          .word     0xffff, 0
213          .byte     0, 0x9f, 0xcf, 0
214          /* data */
215          .word     0xffff, 0
216          .byte     0, 0x93, 0xcf, 0
217
218          .align    16, 0
219          .global WAKEUP_r_cr3
220WAKEUP_r_cr3:                 .long 0
221          .global WAKEUP_r_cr4
222WAKEUP_r_cr4:                 .long 0
223
224          .global WAKEUP_curcpu
225WAKEUP_curcpu:                .long 0
226          .global WAKEUP_restorecpu
227WAKEUP_restorecpu:  .long 0
228          .global WAKEUP_efer
229WAKEUP_efer:                  .long 0
230
231          .global WAKEUP_vbios_reset
232WAKEUP_vbios_reset: .byte 0
233          .global WAKEUP_vesa_modenum
234WAKEUP_vesa_modenum:          .word 0
235          .global WAKEUP_beep_on_reset
236WAKEUP_beep_on_reset:         .byte 0
237