Home | History | Annotate | Line # | Download | only in acpi
acpi_wakecode.S revision 1.1.2.4
      1 /* $NetBSD: acpi_wakecode.S,v 1.1.2.4 2007/09/08 02:43:24 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2002, 2007 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, and by Andrew Doran.
      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 (at) jp.freebsd.org>
     44  * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki (at) 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 #define WAKECODE_VERBOSE
     74 
     75 #include <machine/asm.h>
     76 #include <machine/specialreg.h>
     77 #include <machine/param.h>
     78 #include <machine/segments.h>
     79 #include <machine/psl.h>
     80 
     81 	.text
     82 	.code16
     83 	.org	0
     84 	.globl wakeup_16
     85 wakeup_16:
     86 	nop
     87 	cli
     88 	cld
     89 
     90 	/* Set up segment registers for real mode */
     91 	movw	%cs, %ax
     92 	movw	%ax, %ds
     93 	movw	%ax, %ss
     94 
     95 	/* Small call stack XXXAD Where is this? Is it in the ACPI spec? */
     96 	mov	$0x1000, %sp
     97 
     98 	/* Clear flags */
     99 	pushl	$0
    100 	popfl
    101 
    102 	/* Only beep on reset if machdep.acpi_beep_on_reset=1 */
    103 	cmpb	$1, beep_on_reset
    104 	jne	nobeepon
    105 	call	beepon
    106 
    107 nobeepon:
    108 
    109 	/* Only reset the VBIOS if machdep.acpi_vbios_reset=1 */
    110 	cmpb	$1, vbios_reset
    111 	jne	novbiosreset
    112 
    113 	/* Kick the VBIOS. */
    114 	lcall	$0xc000, $3
    115 
    116 	/* Paranoid, restore segment registers. */
    117 	movw	%cs, %ax
    118 	movw	%ax, %ds
    119 	movw	%ax, %ss
    120 
    121 novbiosreset:
    122 
    123 	/* Only beep on reset if machdep.acpi_beep_on_reset=1 */
    124 	cmpb	$1, beep_on_reset
    125 	jne	nobeepoff
    126 	call	beepoff
    127 
    128 nobeepoff:
    129 
    130 #ifdef WAKECODE_VERBOSE
    131 	movw	$0xb800, %ax
    132 	movw	%ax, %fs
    133 	movw	$0x0200 + '0', %fs:(0x00)
    134 #endif
    135 
    136 	/* Get physical address of the code */
    137 	mov	%cs, %ax
    138 	movzx	%ax, %esi
    139 	shll	$4, %esi
    140 
    141 	/* Fill 16->32 address */
    142 	addl	%esi, wakeup_sw32+2	/* 1b opcode, 1b prefix */
    143 	addl	%esi, wakeup_sw64+1	/* 1b opcode */
    144 	addl	%esi, tmp_gdt+2
    145 	addl	%esi, tmp_gdt64+2
    146 	wbinvd					/* flush uop/trace cache */
    147 	jmp	1f
    148 1:	jmp	1f
    149 1:
    150 
    151 
    152 #ifdef WAKECODE_VERBOSE
    153 	movw	$0xb800, %ax
    154 	movw	%ax, %fs
    155 	movw	$0x0200 + '1', %fs:(0x02)
    156 #endif
    157 
    158 	/* Load GDT, enable protected mode */
    159 	lgdt	tmp_gdt
    160 
    161 	/* Enable protected mode */
    162 	movl	%eax, %cr0
    163 	orb	$(CR0_PE), %al
    164 	movl	%eax, %cr0
    165 
    166 wakeup_sw32:
    167 
    168 	/* Switch to protected mode by intersegmental jump */
    169 	ljmpl	$GSEL(GCODE_SEL,SEL_KPL), $wakeup_32
    170 
    171 	.align	16
    172 	.code32
    173 wakeup_32:
    174 
    175 	nop
    176 	movl	$GSEL(GDATA_SEL,SEL_KPL), %eax
    177 	movw	%ax, %ds
    178 	movw	%ax, %ss
    179 	movw	%ax, %es
    180 	movw	%ax, %fs
    181 	movw	%ax, %gs
    182 
    183 	movl	$0x1000, %esp	/* XXXJDM is this needed? */
    184 
    185 #ifdef WAKECODE_VERBOSE
    186 	movw	$0x0200 + '2', (0xb8004)
    187 #endif
    188 
    189 	pushl	$PSL_MBO
    190 	popfl
    191 
    192 	movl	%cr4, %eax
    193 	orl	$(CR4_PAE|CR4_OSFXSR|CR4_OSXMMEXCPT), %eax
    194 	movl	%eax, %cr4
    195 
    196 	/* Set long mode enable in EFER and enable syscall extensions */
    197 	movl	$MSR_EFER, %ecx
    198 	rdmsr
    199 	xorl	%eax, %eax
    200 	orl	$(EFER_LME|EFER_SCE), %eax
    201 	wrmsr
    202 
    203 #ifdef WAKECODE_VERBOSE
    204 	movw	$0x0200 + '3', (0xb8006)
    205 #endif
    206 
    207 	movl	tmp_pml4(%esi), %ecx
    208 	movl	%ecx, %cr3
    209 
    210 #ifdef WAKECODE_VERBOSE
    211 	movw	$0x0200 + '4', (0xb8008)
    212 #endif
    213 
    214 	movl	%cr0, %eax
    215 	orl	$(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_MP|CR0_WP), %eax
    216 	movl	%eax, %cr0
    217 	jmp	compat
    218 compat:
    219 
    220 	movl	$GSEL(GDATA_SEL,SEL_KPL), %eax
    221 	movl	%eax, %ds
    222 	movl	%eax, %es
    223 	movl	%eax, %ss
    224 
    225 #ifdef WAKECODE_VERBOSE
    226 	movw	$0x0200 + '5', (0xb800a)
    227 #endif
    228 
    229 	/* Load 64-bit GDT. */
    230 	lgdt	tmp_gdt64(%esi)
    231 
    232 #ifdef WAKECODE_VERBOSE
    233 	movw	$0x0200 + '6', (0xb800c)
    234 #endif
    235 
    236 wakeup_sw64:
    237 
    238 	/* Jump to long mode segment. */
    239 	ljmpl	$GSEL(GCODE_SEL,SEL_KPL), $wakeup_64
    240 
    241 	.align	16
    242 	.code64
    243 wakeup_64:
    244 
    245 	nop
    246 	nop
    247 
    248 	/* Re-initialize segment regsisters to be on the safe side. */
    249 	movl	$GSEL(GDATA_SEL,SEL_KPL), %eax
    250 	movl	%eax, %ds
    251 	movl	%eax, %es
    252 	movl	%eax, %gs
    253 	movl	%eax, %ss
    254 	movl	%eax, %fs
    255 
    256 #ifdef WAKECODE_VERBOSE
    257 	movw	$0x0200 + '7', (0xb800e)
    258 #endif
    259 
    260 	movq	previous_rsp(%rsi), %rsp
    261 
    262 #ifdef WAKECODE_VERBOSE
    263 	movw	$0x0200 + '8', (0xb8010)
    264 #endif
    265 
    266 #if 1	/* XXXAD ?? */
    267 	/* Fixup TSS type field; 386 busy TSS (11) -> 386 available TSS (9) */
    268 #define TSS_TYPEFIX_MASK	0xf9
    269 	movq	physical_gdt+2(%rsi), %rbx
    270 	movzxw	previous_tr(%rsi), %rcx
    271 	leaq	(%rbx,%rcx),%rax	/* get TSS segment descriptor */
    272 	andb	$TSS_TYPEFIX_MASK, 5(%rax)
    273 #endif
    274 
    275 #ifdef WAKECODE_VERBOSE
    276 	movw	$0x0200 + '9', (0xb8012)
    277 #endif
    278 
    279         /* Restore registers */
    280 	lgdt	previous_gdt(%rsi)
    281 	lidt	previous_idt(%rsi)
    282         lldt	previous_ldt(%rsi)
    283 #if 0
    284         ltr     previous_tr(%rsi)
    285 #endif
    286 
    287 #ifdef WAKECODE_VERBOSE
    288 	movw	$0x0200 + 'a', (0xb8014)
    289 #endif
    290 
    291 	movq	previous_cr2(%rsi), %rax
    292 	movq	%rax, %cr2
    293 	movq	previous_cr3(%rsi), %rdx
    294 	movq	previous_cr4(%rsi), %rax
    295 	movq	%rax, %cr4
    296 
    297 #ifdef WAKECODE_VERBOSE
    298 	movw	$0x0200 + 'b', (0xb8016)
    299 #endif
    300 
    301         movw	previous_es(%rsi), %ax
    302         movw	%ax, %es
    303         movw	previous_fs(%rsi), %ax
    304         movw	%ax, %fs
    305         movw	previous_gs(%rsi), %ax
    306         movw	%ax, %gs
    307         movw	previous_ss(%rsi), %ax
    308         movw	%ax, %ss
    309         movq	where_to_recover(%rsi), %rbx
    310         movw	previous_ds(%rsi), %ax
    311 
    312 #ifdef WAKECODE_VERBOSE
    313 	movw	$0x0200 + 'c', (0xb8018)
    314 #endif
    315 
    316 	/* Re-enable paging, %rdx loaded above */
    317 	movq	%rdx, %cr3
    318 	movq	previous_cr0(%rsi), %rcx
    319 	movq	%rcx, %cr0
    320 	jmp	1f
    321 1:	jmp	1f
    322 1:
    323 
    324 	/* New address space active - reload %ds */
    325         movw	%ax, %ds
    326         jmp	*%rbx
    327 
    328 beepon:
    329 	movb	$0xc0, %al
    330 	outb	%al, $0x42
    331 	movb	$0x04, %al
    332 	outb	%al, $0x42
    333 	inb	$0x61, %al
    334 	orb	$0x3, %al
    335 	outb	%al, $0x61
    336 	ret
    337 
    338 beepoff:
    339 	inb	$0x61, %al
    340 	andb	$0xfc, %al
    341 	outb	%al, $0x61
    342 	ret
    343 
    344 tmp_gdt:
    345 	.word	0xffff
    346 	.long	tmp_gdtable
    347 
    348 tmp_gdtable:
    349 	/* null */
    350 	.word	0, 0
    351 	.byte	0, 0, 0, 0
    352 	/* code */
    353 	.word	0xffff, 0
    354 	.byte	0, 0x9f, 0xcf, 0
    355 	/* data */
    356 	.word	0xffff, 0
    357 	.byte	0, 0x93, 0xcf, 0
    358 
    359 tmp_gdt64:
    360 	.word	0xffff
    361 	.long	tmp_gdtable64
    362 
    363 tmp_gdtable64:
    364 	/* null */
    365 	.word	0, 0
    366 	.byte	0, 0, 0, 0
    367 	/* code */
    368 	.word	0xffff, 0
    369 	.byte	0, 0x9a, 0xaf, 0
    370 	/* data */
    371 	.word	0xffff, 0
    372 	.byte	0, 0x92, 0xcf, 0
    373 
    374 	.align	16, 0
    375 	.global	WAKEUP_physical_gdt
    376 WAKEUP_physical_gdt:
    377 physical_gdt:		.word 0
    378 			.quad 0
    379 	.global WAKEUP_previous_cr2
    380 WAKEUP_previous_cr2:
    381 previous_cr2:		.quad 0
    382 	.global	WAKEUP_previous_cr3
    383 WAKEUP_previous_cr3:
    384 previous_cr3:		.quad 0
    385 	.global	WAKEUP_previous_cr4
    386 WAKEUP_previous_cr4:
    387 previous_cr4:		.quad 0
    388 	.global	WAKEUP_previous_cr0
    389 WAKEUP_previous_cr0:
    390 previous_cr0:		.quad 0
    391 	.global	WAKEUP_previous_tr
    392 WAKEUP_previous_tr:
    393 previous_tr:		.word 0
    394 	.global	WAKEUP_previous_gdt
    395 WAKEUP_previous_gdt:
    396 previous_gdt:		.word 0
    397 			.quad 0
    398 	.global	WAKEUP_previous_ldt
    399 WAKEUP_previous_ldt:
    400 previous_ldt:		.word 0
    401 	.global WAKEUP_previous_idt
    402 WAKEUP_previous_idt:
    403 previous_idt:		.word 0
    404 			.quad 0
    405 	.global WAKEUP_previous_ds
    406 WAKEUP_previous_ds:
    407 previous_ds:		.word 0
    408 	.global	WAKEUP_previous_es
    409 WAKEUP_previous_es:
    410 previous_es:		.word 0
    411 	.global	WAKEUP_previous_fs
    412 WAKEUP_previous_fs:
    413 previous_fs:		.word 0
    414 	.global	WAKEUP_previous_gs
    415 WAKEUP_previous_gs:
    416 previous_gs:		.word 0
    417 	.global	WAKEUP_previous_ss
    418 WAKEUP_previous_ss:
    419 previous_ss:		.word 0
    420 	.global	WAKEUP_previous_rsp
    421 WAKEUP_previous_rsp:
    422 previous_rsp:		.quad 0
    423 	.global WAKEUP_where_to_recover
    424 WAKEUP_where_to_recover:
    425 where_to_recover:	.quad 0
    426 	.global WAKEUP_tmp_pml4
    427 WAKEUP_tmp_pml4:
    428 tmp_pml4:		.quad 0
    429 	.global WAKEUP_vbios_reset
    430 WAKEUP_vbios_reset:
    431 vbios_reset:		.byte 0
    432 	.global	WAKEUP_beep_on_reset
    433 WAKEUP_beep_on_reset:
    434 beep_on_reset:		.byte 0
    435