1 /*	$OpenBSD: apmprobe.c,v 1.15 2007/09/13 06:58:47 weingart Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-2000 Michael Shalayeff
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 /*
29  * APM derived from: apm_init.S, LP (Laptop Package)
30  * wich contained this:
31  * Copyright (C) 1994 by HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp>
32  *
33  */
34 /*
35  * If you want to know the specification of APM BIOS, see the following
36  * documentations,
37  *
38  * [1] Intel Corporation and Microsoft Corporation, "Advanced Power
39  *     Management, The Next Generation, Version 1.0", Feb.,1992.
40  *
41  * [2] Intel Corporation and Microsoft Corporation, "Advanced Power
42  *     Management (APM) BIOS Interface Specification Revision 1.1",
43  *     Sep.,1993, Intel Order Number: 241704-001, Microsoft Part
44  *     Number: 781-110-X01
45  *
46  * or contact
47  *
48  * APM Support Desk (Intel Corporation, US)
49  *   TEL: (800)628-8686
50  *   FAX: (916)356-6100.
51  */
52 
53 #include <sys/param.h>
54 #include "libsa.h"
55 #include <stand/boot/bootarg.h>
56 
57 #include <uvm/uvm_extern.h>
58 
59 #include <dev/isa/isareg.h>
60 
61 #include <machine/apmvar.h>
62 #include <machine/biosvar.h>
63 
64 #define vm_page_size 4096
65 
66 #include "debug.h"
67 
68 extern int debug;
69 
70 static __inline u_int
apm_check(void)71 apm_check(void)
72 {
73 	register u_int detail;
74 	register u_int8_t f;
75 
76 	__asm __volatile(DOINT(0x15) "\n\t"
77 	    "setc %b1\n\t"
78 	    "movzwl %%ax, %0\n\t"
79 	    "shll $16, %%ecx\n\t"
80 	    "orl %%ecx, %0"
81 	    : "=a" (detail), "=b" (f)
82 	    : "0" (APM_INSTCHECK), "1" (APM_DEV_APM_BIOS)
83 	    : "%ecx", "cc");
84 
85 	if (f || BIOS_regs.biosr_bx != 0x504d /* "PM" */ ) {
86 #ifdef DEBUG
87 		if (debug)
88 			printf("apm_check: %x, %x, %x\n",
89 			    f, BIOS_regs.biosr_bx, detail);
90 #endif
91 		return 0;
92 	} else
93 		return detail;
94 }
95 
96 static __inline int
apm_disconnect(void)97 apm_disconnect(void)
98 {
99 	register u_int16_t rv;
100 
101 	__asm __volatile(DOINT(0x15) "\n\t"
102 	    "setc %b0"
103 	    : "=a" (rv)
104 	    : "0" (APM_DISCONNECT), "b" (APM_DEV_APM_BIOS)
105 	    : "%ecx", "%edx", "cc");
106 
107 	return ((rv & 0xff)? rv >> 8 : 0);
108 }
109 
110 static __inline int
apm_connect(bios_apminfo_t * ai)111 apm_connect(bios_apminfo_t *ai)
112 {
113 	register u_int16_t f;
114 
115 	__asm __volatile (DOINT(0x15) "\n\t"
116 	    "setc %b1\n\t"
117 	    "movb %%ah, %h1\n\t"
118 	    "movzwl %%ax, %%eax\n\tshll $4, %0\n\t"
119 	    "movzwl %%cx, %%ecx\n\tshll $4, %2\n\t"
120 	    "movzwl %%dx, %%edx\n\tshll $4, %3\n\t"
121 	    : "=a" (ai->apm_code32_base),
122 	      "=b" (f),
123 	      "=c" (ai->apm_code16_base),
124 	      "=d" (ai->apm_data_base)
125 	    : "0" (APM_PROT32_CONNECT), "1" (APM_DEV_APM_BIOS)
126 	    : "cc");
127 
128 	if (f & 0xff)
129 		return (f >> 8);
130 
131 	ai->apm_entry      = BIOS_regs.biosr_bx;
132 #if 0
133 	ai->apm_code_len   = BIOS_regs.biosr_si & 0xffff;
134 	ai->apm_code16_len = BIOS_regs.biosr_si & 0xffff;
135 	ai->apm_data_len   = BIOS_regs.biosr_di & 0xffff;
136 #else
137 	ai->apm_code_len   = 0xffff - (ai->apm_code32_base & 0xffff);
138 	ai->apm_code16_len = 0xffff - (ai->apm_code16_base & 0xffff);
139 	ai->apm_data_len   = 0xffff - (ai->apm_data_base & 0xffff);
140 #endif
141 	if (ai->apm_data_base < BOOTARG_OFF)
142 		ai->apm_data_len = NBPG - (ai->apm_data_base & PGOFSET) - 1;
143 
144 #ifdef DEBUG
145 	if (debug)
146 		printf("cs=%x:%x/%x:%x, ds=%x:%x\n",
147 		    ai->apm_code32_base, ai->apm_code_len,
148 		    ai->apm_code16_base, ai->apm_code16_len,
149 		    ai->apm_data_base,   ai->apm_data_len);
150 #endif
151 	/* inform apm bios about our driver version */
152 	__asm __volatile (DOINT(0x15) "\n\t"
153 	    "setc %b1\n\t"
154 	    "movb %%ah, %h1"
155 	    : "=b" (f)
156 	    : "a" (APM_DRIVER_VERSION),
157 	      "0" (APM_DEV_APM_BIOS),
158 	      "c" (APM_VERSION)
159 	    : "cc");
160 
161 	return 0;
162 }
163 
164 static bios_apminfo_t ai;
165 
166 void
apmprobe(void)167 apmprobe(void)
168 {
169 	if ((ai.apm_detail = apm_check())) {
170 
171 		apm_disconnect();
172 
173 		if (apm_connect(&ai) != 0) {
174 #ifdef DEBUG
175 			printf("\napm: connect error\n");
176 #endif
177 			return;
178 		}
179 #ifdef DEBUG
180 		if (debug)
181 			printf("apm[%x cs=%x[%x]/%x[%x] ds=%x[%x] @ %x]",
182 			    ai.apm_detail,
183 			    ai.apm_code32_base, ai.apm_code_len,
184 			    ai.apm_code16_base, ai.apm_code16_len,
185 			    ai.apm_data_base, ai.apm_data_len,
186 			    ai.apm_entry);
187 		else
188 			printf(" apm");
189 #else
190 		printf(" apm");
191 #endif
192 		addbootarg(BOOTARG_APMINFO, sizeof(ai), &ai);
193 	}
194 }
195 
196 void
apmfixmem(void)197 apmfixmem(void)
198 {
199 #ifdef DEBUG
200 	printf("apmremove (%d)", ai.apm_detail);
201 #endif
202 	if (ai.apm_detail)
203 		mem_delete(trunc_page(ai.apm_data_base),
204 		    round_page(ai.apm_data_base + ai.apm_data_len));
205 }
206