1/* $NetBSD: acpi_wakecode.S,v 1.10 2009/08/24 22:06:50 jmcneill Exp $ */ 2 3/*- 4 * Copyright (c) 2007 Joerg Sonnenberger <joerg@netbsd.org> 5 * 6 * Copyright (c) 2002 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Takuya SHIOZAKI. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 35/* 36 * This code is derived from FreeBSD. Original copyrights: 37 * 38 * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org> 39 * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 40 * All rights reserved. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 * 63 * FreeBSD: src/sys/i386/acpica/acpi_wakecode.S,v 1.1 2001/07/20 06:07:31 takawata Exp 64 */ 65 66#define _LOCORE 67 68#include <machine/psl.h> 69#include <machine/segments.h> 70#include <machine/specialreg.h> 71 72#define ACPI_WAKEUP_ADDR 0x3000 73 74 .text 75 .code16 76 .org 0 /* ACPI spec says: cs==(phys>>8), ip==(phys&0x000F) */ 77 .globl wakeup_16 78wakeup_16: 79 nop 80 cli 81 cld 82 83 /* Set up segment registers for real mode */ 84 movw %cs,%ax 85 movw %ax,%ds 86 movw %ax,%ss 87 88 /* Small call stack */ 89 mov $0x1000,%sp 90 91 /* Clear flags */ 92 pushl $0 93 popfl 94 95 /* Only beep on reset if machdep.acpi_beep_on_reset=1 */ 96 cmpb $1,WAKEUP_beep_on_reset 97 jne 1f 98 movb $0xc0,%al 99 outb %al,$0x42 100 movb $0x04,%al 101 outb %al,$0x42 102 inb $0x61,%al 103 orb $0x3,%al 104 outb %al,$0x61 1051: 106 107 /* Only reset the VBIOS if machdep.acpi_vbios_reset=1 */ 108 cmpb $1,WAKEUP_vbios_reset 109 jne 1f 110 111 /* Kick the VBIOS. */ 112 lcall $0xc000,$3 113 114 /* Reset registers in case the VBIOS changed them. */ 115 movw %cs,%ax 116 movw %ax,%ds 117 movw %ax,%ss 118 119 /* If we need to restore a VESA VBE mode, do it now */ 120 cmpb $0,WAKEUP_vesa_modenum 121 je 1f 122 movw WAKEUP_vesa_modenum,%bx 123 orw $0x4000,%bx 124 movw $0x4f02,%ax 125 int $0x10 126 127 movw %cs,%ax 128 movw %ax,%ds 129 movw %ax,%ss 1301: 131 132 /* Disable beep again if machdep.acpi_beep_on_reset=1 */ 133 cmpb $1,WAKEUP_beep_on_reset 134 jne 1f 135 inb $0x61,%al 136 andb $0xfc,%al 137 outb %al,$0x61 1381: 139 140 /* Load temporary 32bit GDT */ 141 data32 addr32 lgdt tmp_gdt 142 143 /* Enable protected mode w/o paging */ 144 mov %cr0,%eax 145 orl $(CR0_PE),%eax 146 mov %eax,%cr0 147 148wakeup_sw32: 149 /* 150 * Switch to protected mode by intersegmental jump. 151 * Target and everything else has to compensate for the new origin 152 * as this is using the flat memory model now. 153 */ 154 155 ljmpl $0x8,$wakeup_32 + ACPI_WAKEUP_ADDR 156 157 .code32 158 .align 16 159wakeup_32: 160 /* 161 * Switched to protected mode w/o paging 162 */ 163 164 nop 165 /* Set up segment registers and initial stack for protected mode */ 166 movw $0x10, %ax 167 movw %ax,%ds 168 movw %ax,%ss 169 170 movl $(ACPI_WAKEUP_ADDR + 4096),%esp 171 172 /* First, reset the PSL. */ 173 pushl $PSL_MBO 174 popfl 175 176 /* Enable PAE and potentially PSE */ 177 movl $(CR4_PAE|CR4_OSFXSR|CR4_OSXMMEXCPT|CR4_PSE),%eax 178 movl %eax,%cr4 179 180 /* 181 * First switch to Long Mode. Do not restore the original 182 * MSR EFER value directly, as enabling the NX bit without 183 * paging will result in a GPF on AMD CPUs. 184 * 185 * Load the correct MSR EFER value now to not depend on the 186 * data segment register directly after switching to Long Mode. 187 * After this point, no instruction is allowed to clobber %ebx. 188 */ 189 movl WAKEUP_efer + ACPI_WAKEUP_ADDR,%ebx 190 movl $MSR_EFER,%ecx 191 rdmsr 192 orl $EFER_LME, %eax 193 wrmsr 194 195 /* Load temporary PML4, code will switch to full PML4 later */ 196 movl WAKEUP_r_cr3 + ACPI_WAKEUP_ADDR,%eax 197 movl %eax,%cr3 198 199 /* Enable paging */ 200 movl %cr0,%eax 201 orl $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_MP|CR0_WP),%eax 202 movl %eax,%cr0 203 /* Flush prefetch queue */ 204 jmp 1f 2051: 206 /* Switch to temporary 64bit GDT */ 207 lgdt tmp_gdt64 + ACPI_WAKEUP_ADDR 208 209 /* Switch to long mode using intersegmental jump. */ 210 ljmp $0x8, $wakeup_64 + ACPI_WAKEUP_ADDR 211 212 .code64 213wakeup_64: 214 215 /* 216 * Load the normal system value of MSR EFER. This includes 217 * enabling the SYSCALL extension and NXE (if supported). 218 */ 219 movl %ebx, %eax 220 movl $MSR_EFER,%ecx 221 wrmsr 222 223 /* Reload data segment with default value */ 224 movw $GSEL(GDATA_SEL, SEL_KPL),%ax 225 movw %ax,%ds 226 227 movq WAKEUP_curcpu + ACPI_WAKEUP_ADDR,%r8 228 movq WAKEUP_restorecpu + ACPI_WAKEUP_ADDR,%rbx 229 230 /* Continue with wakeup in the high-level wakeup code */ 231 jmp *%rbx 232 233 .align 8 234tmp_gdt: 235 .word 0xffff 236 .long tmp_gdtable + ACPI_WAKEUP_ADDR 237 238 .align 8, 0 239tmp_gdtable: 240 /* null */ 241 .word 0, 0 242 .byte 0, 0, 0, 0 243 /* code */ 244 .word 0xffff, 0 245 .byte 0, 0x9f, 0xcf, 0 246 /* data */ 247 .word 0xffff, 0 248 .byte 0, 0x93, 0xcf, 0 249 250tmp_gdt64: 251 .word 0xffff 252 .long tmp_gdtable64 + ACPI_WAKEUP_ADDR 253 254tmp_gdtable64: 255 .quad 0x0000000000000000 256 .quad 0x00af9a000000ffff 257 .quad 0x00cf92000000ffff 258 259 .align 16, 0 260 .global WAKEUP_r_cr3 261WAKEUP_r_cr3: .quad 0 262 263 .global WAKEUP_restorecpu 264WAKEUP_restorecpu: .quad 0 265 266 .global WAKEUP_vbios_reset 267WAKEUP_vbios_reset: .byte 0 268 .global WAKEUP_vesa_modenum 269WAKEUP_vesa_modenum: .word 0 270 .global WAKEUP_beep_on_reset 271WAKEUP_beep_on_reset: .byte 0 272 273 .global WAKEUP_curcpu 274WAKEUP_curcpu: .quad 0 275 .global WAKEUP_efer 276WAKEUP_efer: .long 0 277