i386 - Get completely rid of APIC_IO
[dragonfly.git] / sys / platform / pc32 / acpica5 / acpi_machdep.c
CommitLineData
5ed44076
MD
1/*-
2 * Copyright (c) 2001 Mitsuru IWASAKI
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
49e48b8a 26 * $FreeBSD: src/sys/i386/acpica/acpi_machdep.c,v 1.20 2004/05/05 19:51:15 njl Exp $
7d58d14b 27 * $DragonFly: src/sys/platform/pc32/acpica5/acpi_machdep.c,v 1.14 2008/09/29 06:59:45 hasso Exp $
5ed44076
MD
28 */
29
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/conf.h>
fef8985e 33#include <sys/device.h>
5ed44076
MD
34#include <sys/fcntl.h>
35#include <sys/kernel.h>
36#include <sys/sysctl.h>
37#include <sys/uio.h>
38
39#include "acpi.h"
58c6428b
JS
40#include <dev/acpica5/acpivar.h>
41#include <dev/acpica5/acpiio.h>
5ed44076
MD
42
43static device_t acpi_dev;
44
45/*
46 * APM driver emulation
47 */
48
f6d3b0c1 49#include <sys/event.h>
5ed44076
MD
50
51#include <machine/apm_bios.h>
52#include <machine/pc/bios.h>
a9295349 53#include <machine_base/apm/apm.h>
79b62055 54#include <machine/smp.h>
5ed44076 55
f9d8cd12 56uint32_t acpi_reset_video = 1;
5ed44076
MD
57TUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video);
58
79b62055
MN
59/*
60 * -1 == unset. Use acpi_GetDefaultIntrModel()
61 */
62#ifdef SMP /* APIC-IO */
63static int intr_model = -1;
3c12d9ec 64#else
f9d8cd12 65static int intr_model = ACPI_INTR_PIC;
3c12d9ec 66#endif
79b62055 67
5ed44076
MD
68static struct apm_softc apm_softc;
69
70static d_open_t apmopen;
71static d_close_t apmclose;
72static d_write_t apmwrite;
73static d_ioctl_t apmioctl;
f6d3b0c1 74static d_kqfilter_t apmkqfilter;
5ed44076
MD
75
76#define CDEV_MAJOR 39
fef8985e 77static struct dev_ops apm_ops = {
d4b8aec4 78 { "apm", CDEV_MAJOR, 0 },
fef8985e
MD
79 .d_open = apmopen,
80 .d_close = apmclose,
81 .d_write = apmwrite,
82 .d_ioctl = apmioctl,
f6d3b0c1 83 .d_kqfilter = apmkqfilter
5ed44076
MD
84};
85
5ed44076
MD
86static int
87acpi_capm_convert_battstate(struct acpi_battinfo *battp)
88{
89 int state;
90
91 state = 0xff; /* XXX unknown */
92
93 if (battp->state & ACPI_BATT_STAT_DISCHARG) {
94 if (battp->cap >= 50)
95 state = 0; /* high */
96 else
97 state = 1; /* low */
98 }
99 if (battp->state & ACPI_BATT_STAT_CRITICAL)
100 state = 2; /* critical */
101 if (battp->state & ACPI_BATT_STAT_CHARGING)
102 state = 3; /* charging */
103
104 /* If still unknown, determine it based on the battery capacity. */
105 if (state == 0xff) {
f9d8cd12 106 if (battp->cap >= 50)
5ed44076 107 state = 0; /* high */
f9d8cd12 108 else
5ed44076 109 state = 1; /* low */
5ed44076
MD
110 }
111
112 return (state);
113}
114
115static int
116acpi_capm_convert_battflags(struct acpi_battinfo *battp)
117{
118 int flags;
119
120 flags = 0;
121
f9d8cd12 122 if (battp->cap >= 50)
5ed44076 123 flags |= APM_BATT_HIGH;
f9d8cd12 124 else {
5ed44076
MD
125 if (battp->state & ACPI_BATT_STAT_CRITICAL)
126 flags |= APM_BATT_CRITICAL;
127 else
128 flags |= APM_BATT_LOW;
129 }
130 if (battp->state & ACPI_BATT_STAT_CHARGING)
131 flags |= APM_BATT_CHARGING;
132 if (battp->state == ACPI_BATT_STAT_NOT_PRESENT)
133 flags = APM_BATT_NOT_PRESENT;
134
135 return (flags);
136}
137
138static int
139acpi_capm_get_info(apm_info_t aip)
140{
141 int acline;
142 struct acpi_battinfo batt;
143
144 aip->ai_infoversion = 1;
145 aip->ai_major = 1;
146 aip->ai_minor = 2;
147 aip->ai_status = apm_softc.active;
148 aip->ai_capabilities= 0xff00; /* XXX unknown */
149
150 if (acpi_acad_get_acline(&acline))
151 aip->ai_acline = 0xff; /* unknown */
152 else
153 aip->ai_acline = acline; /* on/off */
154
7d58d14b 155 if (acpi_battery_get_battinfo(NULL, &batt)) {
5ed44076
MD
156 aip->ai_batt_stat = 0xff; /* unknown */
157 aip->ai_batt_life = 0xff; /* unknown */
158 aip->ai_batt_time = -1; /* unknown */
159 aip->ai_batteries = 0;
160 } else {
161 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt);
162 aip->ai_batt_life = batt.cap;
163 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
164 aip->ai_batteries = acpi_battery_get_units();
165 }
166
167 return (0);
168}
169
170static int
171acpi_capm_get_pwstatus(apm_pwstatus_t app)
172{
7d58d14b
HT
173 device_t dev;
174 int acline, unit, error;
5ed44076
MD
175 struct acpi_battinfo batt;
176
177 if (app->ap_device != PMDV_ALLDEV &&
f9d8cd12 178 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
5ed44076 179 return (1);
5ed44076
MD
180
181 if (app->ap_device == PMDV_ALLDEV)
7d58d14b
HT
182 error = acpi_battery_get_battinfo(NULL, &batt);
183 else {
184 unit = app->ap_device - PMDV_BATT0;
185 dev = devclass_get_device(devclass_find("battery"), unit);
186 if (dev != NULL)
187 error = acpi_battery_get_battinfo(dev, &batt);
188 else
189 error = ENXIO;
190 }
191 if (error)
5ed44076
MD
192 return (1);
193
194 app->ap_batt_stat = acpi_capm_convert_battstate(&batt);
195 app->ap_batt_flag = acpi_capm_convert_battflags(&batt);
196 app->ap_batt_life = batt.cap;
197 app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
198
199 if (acpi_acad_get_acline(&acline))
200 app->ap_acline = 0xff; /* unknown */
201 else
202 app->ap_acline = acline; /* on/off */
203
204 return (0);
205}
206
207static int
fef8985e 208apmopen(struct dev_open_args *ap)
5ed44076
MD
209{
210 return (0);
211}
212
213static int
fef8985e 214apmclose(struct dev_close_args *ap)
5ed44076
MD
215{
216 return (0);
217}
218
219static int
fef8985e 220apmioctl(struct dev_ioctl_args *ap)
5ed44076
MD
221{
222 int error = 0;
223 struct acpi_softc *acpi_sc;
224 struct apm_info info;
225 apm_info_old_t aiop;
226
f9d8cd12 227 acpi_sc = device_get_softc(acpi_dev);
5ed44076 228
fef8985e 229 switch (ap->a_cmd) {
5ed44076 230 case APMIO_SUSPEND:
fef8985e 231 if ((ap->a_fflag & FWRITE) == 0)
5ed44076
MD
232 return (EPERM);
233 if (apm_softc.active)
234 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_suspend_sx);
235 else
236 error = EINVAL;
237 break;
238 case APMIO_STANDBY:
fef8985e 239 if ((ap->a_fflag & FWRITE) == 0)
5ed44076
MD
240 return (EPERM);
241 if (apm_softc.active)
242 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_standby_sx);
243 else
244 error = EINVAL;
245 break;
246 case APMIO_GETINFO_OLD:
247 if (acpi_capm_get_info(&info))
248 error = ENXIO;
fef8985e 249 aiop = (apm_info_old_t)ap->a_data;
5ed44076
MD
250 aiop->ai_major = info.ai_major;
251 aiop->ai_minor = info.ai_minor;
252 aiop->ai_acline = info.ai_acline;
253 aiop->ai_batt_stat = info.ai_batt_stat;
254 aiop->ai_batt_life = info.ai_batt_life;
255 aiop->ai_status = info.ai_status;
256 break;
257 case APMIO_GETINFO:
fef8985e 258 if (acpi_capm_get_info((apm_info_t)ap->a_data))
5ed44076
MD
259 error = ENXIO;
260 break;
261 case APMIO_GETPWSTATUS:
fef8985e 262 if (acpi_capm_get_pwstatus((apm_pwstatus_t)ap->a_data))
5ed44076
MD
263 error = ENXIO;
264 break;
265 case APMIO_ENABLE:
fef8985e 266 if ((ap->a_fflag & FWRITE) == 0)
5ed44076
MD
267 return (EPERM);
268 apm_softc.active = 1;
269 break;
270 case APMIO_DISABLE:
fef8985e 271 if ((ap->a_fflag & FWRITE) == 0)
5ed44076
MD
272 return (EPERM);
273 apm_softc.active = 0;
274 break;
275 case APMIO_HALTCPU:
276 break;
277 case APMIO_NOTHALTCPU:
278 break;
279 case APMIO_DISPLAY:
fef8985e 280 if ((ap->a_fflag & FWRITE) == 0)
5ed44076
MD
281 return (EPERM);
282 break;
283 case APMIO_BIOS:
fef8985e 284 if ((ap->a_fflag & FWRITE) == 0)
5ed44076 285 return (EPERM);
fef8985e 286 bzero(ap->a_data, sizeof(struct apm_bios_arg));
5ed44076
MD
287 break;
288 default:
289 error = EINVAL;
290 break;
291 }
292
293 return (error);
294}
295
296static int
fef8985e 297apmwrite(struct dev_write_args *ap)
5ed44076 298{
fef8985e 299 return (ap->a_uio->uio_resid);
5ed44076
MD
300}
301
f6d3b0c1
SG
302static void apmfilter_detach(struct knote *);
303static int apmfilter(struct knote *, long);
304
305static struct filterops apmfilterops =
4c91dbc9 306 { FILTEROP_ISFD, NULL, apmfilter_detach, apmfilter };
f6d3b0c1
SG
307
308static int
309apmkqfilter(struct dev_kqfilter_args *ap)
310{
311 struct knote *kn = ap->a_kn;
312
313 kn->kn_fop = &apmfilterops;
314 ap->a_result = 0;
315 return (0);
316}
317
318static void
319apmfilter_detach(struct knote *kn) {}
320
321static int
322apmfilter(struct knote *kn, long hint)
323{
324 return (0);
325}
326
5ed44076
MD
327static void
328acpi_capm_init(struct acpi_softc *sc)
329{
fef8985e
MD
330 make_dev(&apm_ops, 0, 0, 5, 0664, "apm");
331 make_dev(&apm_ops, 8, 0, 5, 0664, "apm");
26be20a0 332 kprintf("Warning: ACPI is disabling APM's device. You can't run both\n");
5ed44076
MD
333}
334
79b62055
MN
335/*
336 * Lazy initialize intr_model
337 */
338static int
339acpi_GetDefaultIntrModel(void)
340{
341 if (intr_model == -1) {
342#ifdef SMP /* APIC-IO */
343 if (apic_io_enable)
344 intr_model = ACPI_INTR_APIC;
345 else
346 intr_model = ACPI_INTR_PIC;
347#else
348 intr_model = ACPI_INTR_PIC;
349#endif
350 }
351 return intr_model;
352}
353
5ed44076
MD
354int
355acpi_machdep_init(device_t dev)
356{
357 struct acpi_softc *sc;
358
359 acpi_dev = dev;
f9d8cd12 360 sc = device_get_softc(acpi_dev);
5ed44076
MD
361
362 /*
363 * XXX: Prevent the PnP BIOS code from interfering with
364 * our own scan of ISA devices.
365 */
366 PnPBIOStable = NULL;
367
368 acpi_capm_init(sc);
369
370 acpi_install_wakeup_handler(sc);
371
79b62055 372 if (acpi_GetDefaultIntrModel() == ACPI_INTR_PIC)
05065648 373 BUS_CONFIG_INTR(dev, dev, AcpiGbl_FADT.SciInterrupt,
e1eeedd0 374 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
f9d8cd12 375 else
79b62055 376 acpi_SetIntrModel(acpi_GetDefaultIntrModel());
5ed44076
MD
377
378 SYSCTL_ADD_UINT(&sc->acpi_sysctl_ctx,
379 SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO,
380 "reset_video", CTLFLAG_RD | CTLFLAG_RW, &acpi_reset_video, 0,
381 "Call the VESA reset BIOS vector on the resume path");
382
383 return (0);
384}
385
386void
387acpi_SetDefaultIntrModel(int model)
388{
389
390 intr_model = model;
391}