Home | History | Annotate | Line # | Download | only in acpi
acpi_wakecode.S revision 1.1.2.5
      1 /*	$NetBSD: acpi_wakecode.S,v 1.1.2.5 2007/09/10 20:38:59 joerg 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 (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 #include <machine/psl.h>
     74 #include <machine/specialreg.h>
     75 #include <machine/segments.h>
     76 
     77 	.text
     78 	.code16
     79 	.org 0	/* ACPI spec says: cs==(phys>>8), ip==(phys&0x000F) */
     80 	.globl wakeup_16
     81 wakeup_16:
     82 	nop
     83 	cli
     84 	cld
     85 
     86 	/* Set up segment registers for real mode */
     87 	movw	%cs,%ax
     88 	movw	%ax,%ds
     89 	movw	%ax,%ss
     90 
     91 	/* Small call stack */
     92 	mov	$0x1000,%sp
     93 
     94 	/* Clear flags */
     95 	pushl	$0
     96 	popfl
     97 
     98 	/* Only beep on reset if machdep.acpi_beep_on_reset=1 */
     99 	cmpb	$1,WAKEUP_beep_on_reset
    100 	jne	1f
    101 	movb	$0xc0,%al
    102 	outb	%al,$0x42
    103 	movb	$0x04,%al
    104 	outb	%al,$0x42
    105 	inb	$0x61,%al
    106 	orb	$0x3,%al
    107 	outb	%al,$0x61
    108 1:
    109 
    110 	/* Only reset the VBIOS if machdep.acpi_vbios_reset=1 */
    111 	cmpb	$1,WAKEUP_vbios_reset
    112 	jne	1f
    113 
    114 	/* Kick the VBIOS. */
    115 	lcall	$0xc000,$3
    116 
    117 	movw	%cs,%ax
    118 	movw	%ax,%ds
    119 	movw	%ax,%ss
    120 1:
    121 
    122 	/* Disable beep again if machdep.acpi_beep_on_reset=1 */
    123 	cmpb	$1,WAKEUP_beep_on_reset
    124 	jne	1f
    125 	inb	$0x61,%al
    126 	andb	$0xfc,%al
    127 	outb	%al,$0x61
    128 1:
    129 
    130 	/* Get physical address of the code */
    131 	xorl	%esi,%esi
    132 	movw	%cs,%si
    133 	shll	$4,%esi
    134 
    135 	/* Prepare ljmpl into 32bit mode */
    136 	movl	%esi,%eax
    137 	addl	$wakeup_32,%eax
    138 	movl	%eax,wakeup_sw32+2
    139 
    140 	/* Flush prefetch queue
    141 	jmp	1f
    142 1:	jmp	1f
    143 1:
    144 
    145 	/* Prepare ljmpl into 64bit mode */
    146 	addl	%esi,wakeup_64_ptr
    147 	/* Prepare GDT64 */
    148 	movl	%esi,%eax
    149 	addl	$tmp_gdtable64,%eax
    150 	movl	%eax,tmp_gdt64+2
    151 	/* Prepare GDT */
    152 	movl	%esi,%eax
    153 	addl	$tmp_gdtable,%eax
    154 	movl	%eax,tmp_gdt+2
    155 	/* Load 32bit GDT */
    156 	data32 addr32 lgdt	tmp_gdt
    157 
    158 	/* Enable protected mode */
    159 	mov	%cr0,%eax
    160 	orl	$(CR0_PE),%eax
    161 	mov	%eax,%cr0
    162 
    163 
    164 wakeup_sw32:
    165 	/* Switch to protected mode by intersegmental jump */
    166 	ljmpl	$0x8,$0x12345678	/* Code location, to be replaced */
    167 
    168 
    169 	.code32
    170 	.align	16
    171 wakeup_32:
    172 	/*
    173 	 * Switched to protected mode w/o paging
    174 	 */
    175 
    176 	nop
    177 	/* Set up segment registers for protected mode */
    178 	movw	$GSEL(GDATA_SEL,SEL_KPL),%ax
    179 	movw	%ax,%ds
    180 
    181 	movl	%esi,%esp
    182 	addl	4096,%esp
    183 
    184 	/* First, reset the PSL. */
    185 	pushl	$PSL_MBO
    186 	popfl
    187 
    188 	movl	%cr4,%eax
    189 	orl	$(CR4_PAE|CR4_OSFXSR|CR4_OSXMMEXCPT),%eax
    190 	movl	%eax,%cr4
    191 
    192 	movl	$MSR_EFER,%ecx
    193 	rdmsr
    194 	xorl	%eax,%eax
    195 	orl	$(EFER_LME|EFER_SCE),%eax
    196 	wrmsr
    197 
    198 	movl	%esi,%edx
    199 	addl	$wakeup_64_ptr,%edx
    200 	movl	%esi,%ecx
    201 	addl	$tmp_gdt64,%ecx
    202 	movl	WAKEUP_r_cr3(%esi),%eax
    203 	movl	%eax,%cr3
    204 
    205 	movl	WAKEUP_r_cr0(%esi),%eax
    206         movl	%eax,%cr0
    207 	jmp	1f
    208 1:
    209 	lgdt	(%ecx)
    210 
    211 	ljmp	*(%edx)
    212 
    213 wakeup_64_ptr:
    214 	.long   wakeup_64
    215 	.word   GSEL(GCODE_SEL, SEL_KPL)
    216 
    217 	.code64
    218 wakeup_64:
    219 
    220 	movw	$GSEL(GDATA_SEL, SEL_KPL),%ax 	#switch to new segment
    221 	movw	%ax,%ds
    222 
    223 	xorq	%rdi,%rdi
    224 	movl	%esi,%edi
    225 
    226 	movq	WAKEUP_restorecpu(%rdi),%rbx
    227 
    228 	lgdt	WAKEUP_r_gdt(%rdi)
    229 
    230 	jmp	*%rbx
    231 
    232 	.align	8
    233 tmp_gdt:
    234 	.word	0xffff
    235 	.long	0
    236 
    237 	.align	8, 0
    238 tmp_gdtable:
    239 	/* null */
    240 	.word	0, 0
    241 	.byte	0, 0, 0, 0
    242 	/* code */
    243 	.word	0xffff, 0
    244 	.byte	0, 0x9f, 0xcf, 0
    245 	/* data */
    246 	.word	0xffff, 0
    247 	.byte	0, 0x93, 0xcf, 0
    248 
    249 tmp_gdt64:
    250 	.word 0xffff
    251 	.long 0
    252 
    253 tmp_gdtable64:
    254 	.quad 0x0000000000000000
    255 	.quad 0x00af9a000000ffff
    256 	.quad 0x00cf92000000ffff
    257 
    258 	.align	16, 0
    259 	.global WAKEUP_r_cr0
    260 WAKEUP_r_cr0:		.quad 0
    261 	.global WAKEUP_r_cr3
    262 WAKEUP_r_cr3:		.quad 0
    263 
    264 	.global WAKEUP_r_gdt
    265 WAKEUP_r_gdt:		.word 0
    266 			.quad 0
    267 	.global WAKEUP_restorecpu
    268 WAKEUP_restorecpu:	.quad 0
    269 
    270 	.global WAKEUP_vbios_reset
    271 WAKEUP_vbios_reset:	.byte 0
    272 	.global WAKEUP_beep_on_reset
    273 WAKEUP_beep_on_reset:	.byte 0
    274