1 /* $OpenBSD: apmprobe.c,v 1.19 2016/06/10 18:36:06 jcs 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 * which 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 <dev/isa/isareg.h>
58
59 #include <machine/apmvar.h>
60 #include <machine/biosvar.h>
61
62 #include "debug.h"
63
64 extern int debug;
65
66 static __inline u_int
apm_check(void)67 apm_check(void)
68 {
69 register u_int detail;
70 register u_int8_t f;
71
72 __asm volatile(DOINT(0x15) "\n\t"
73 "setc %b1\n\t"
74 "movzwl %%ax, %0\n\t"
75 "shll $16, %%ecx\n\t"
76 "orl %%ecx, %0"
77 : "=a" (detail), "=b" (f)
78 : "0" (APM_INSTCHECK), "1" (APM_DEV_APM_BIOS)
79 : "%ecx", "cc");
80
81 if (f || BIOS_regs.biosr_bx != 0x504d /* "PM" */ ) {
82 #ifdef DEBUG
83 if (debug)
84 printf("apm_check: %x, %x, %x\n",
85 f, BIOS_regs.biosr_bx, detail);
86 #endif
87 return 0;
88 } else
89 return detail;
90 }
91
92 static __inline int
apm_disconnect(void)93 apm_disconnect(void)
94 {
95 register u_int16_t rv;
96
97 __asm volatile(DOINT(0x15) "\n\t"
98 "setc %b0"
99 : "=a" (rv)
100 : "0" (APM_DISCONNECT), "b" (APM_DEV_APM_BIOS)
101 : "%ecx", "%edx", "cc");
102
103 return ((rv & 0xff)? rv >> 8 : 0);
104 }
105
106 static __inline int
apm_connect(bios_apminfo_t * ai)107 apm_connect(bios_apminfo_t *ai)
108 {
109 register u_int16_t f;
110
111 __asm volatile (DOINT(0x15) "\n\t"
112 "setc %b1\n\t"
113 "movb %%ah, %h1\n\t"
114 "movzwl %%ax, %%eax\n\tshll $4, %0\n\t"
115 "movzwl %%cx, %%ecx\n\tshll $4, %2\n\t"
116 "movzwl %%dx, %%edx\n\tshll $4, %3\n\t"
117 : "=a" (ai->apm_code32_base),
118 "=b" (f),
119 "=c" (ai->apm_code16_base),
120 "=d" (ai->apm_data_base)
121 : "0" (APM_PROT32_CONNECT), "1" (APM_DEV_APM_BIOS)
122 : "cc");
123
124 if (f & 0xff)
125 return (f >> 8);
126
127 ai->apm_entry = BIOS_regs.biosr_bx;
128 #if 0
129 ai->apm_code_len = BIOS_regs.biosr_si & 0xffff;
130 ai->apm_code16_len = BIOS_regs.biosr_si & 0xffff;
131 ai->apm_data_len = BIOS_regs.biosr_di & 0xffff;
132 #else
133 ai->apm_code_len = 0xffff - (ai->apm_code32_base & 0xffff);
134 ai->apm_code16_len = 0xffff - (ai->apm_code16_base & 0xffff);
135 ai->apm_data_len = 0xffff - (ai->apm_data_base & 0xffff);
136 #endif
137 if (ai->apm_data_base < BOOTARG_OFF)
138 ai->apm_data_len = PAGE_SIZE - (ai->apm_data_base & PAGE_MASK) - 1;
139
140 #ifdef DEBUG
141 if (debug)
142 printf("cs=%x:%x/%x:%x, ds=%x:%x\n",
143 ai->apm_code32_base, ai->apm_code_len,
144 ai->apm_code16_base, ai->apm_code16_len,
145 ai->apm_data_base, ai->apm_data_len);
146 #endif
147 /* inform apm bios about our driver version */
148 __asm volatile (DOINT(0x15) "\n\t"
149 "setc %b1\n\t"
150 "movb %%ah, %h1"
151 : "=b" (f)
152 : "a" (APM_DRIVER_VERSION),
153 "0" (APM_DEV_APM_BIOS),
154 "c" (APM_VERSION)
155 : "cc");
156
157 return 0;
158 }
159
160 static bios_apminfo_t ai;
161
162 void
apmprobe(void)163 apmprobe(void)
164 {
165 if ((ai.apm_detail = apm_check())) {
166
167 apm_disconnect();
168
169 if (apm_connect(&ai) != 0) {
170 #ifdef DEBUG
171 printf("\napm: connect error\n");
172 #endif
173 return;
174 }
175 #ifdef DEBUG
176 if (debug)
177 printf("apm[%x cs=%x[%x]/%x[%x] ds=%x[%x] @ %x]",
178 ai.apm_detail,
179 ai.apm_code32_base, ai.apm_code_len,
180 ai.apm_code16_base, ai.apm_code16_len,
181 ai.apm_data_base, ai.apm_data_len,
182 ai.apm_entry);
183 else
184 printf(" apm");
185 #else
186 printf(" apm");
187 #endif
188 addbootarg(BOOTARG_APMINFO, sizeof(ai), &ai);
189 }
190 }
191
192 #define round_page(x) (((x) + PAGE_MASK) & ~PAGE_MASK)
193 #define trunc_page(x) ((x) & ~PAGE_MASK)
194
195 void
apmfixmem(void)196 apmfixmem(void)
197 {
198 #ifdef DEBUG
199 printf("apmremove (%d)", ai.apm_detail);
200 #endif
201 if (ai.apm_detail)
202 mem_delete(trunc_page(ai.apm_data_base),
203 round_page(ai.apm_data_base + ai.apm_data_len));
204 }
205