i386 - Get completely rid of APIC_IO
[dragonfly.git] / sys / platform / pc32 / acpica5 / acpi_machdep.c
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  *
26  * $FreeBSD: src/sys/i386/acpica/acpi_machdep.c,v 1.20 2004/05/05 19:51:15 njl Exp $
27  * $DragonFly: src/sys/platform/pc32/acpica5/acpi_machdep.c,v 1.14 2008/09/29 06:59:45 hasso Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/conf.h>
33 #include <sys/device.h>
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"
40 #include <dev/acpica5/acpivar.h>
41 #include <dev/acpica5/acpiio.h>
42
43 static device_t acpi_dev;
44
45 /*
46  * APM driver emulation 
47  */
48
49 #include <sys/event.h>
50
51 #include <machine/apm_bios.h>
52 #include <machine/pc/bios.h>
53 #include <machine_base/apm/apm.h>
54 #include <machine/smp.h>
55
56 uint32_t acpi_reset_video = 1;
57 TUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video);
58
59 /*
60  * -1 == unset. Use acpi_GetDefaultIntrModel()
61  */
62 #ifdef SMP /* APIC-IO */
63 static int intr_model = -1;
64 #else
65 static int intr_model = ACPI_INTR_PIC;
66 #endif
67
68 static struct apm_softc apm_softc;
69
70 static d_open_t apmopen;
71 static d_close_t apmclose;
72 static d_write_t apmwrite;
73 static d_ioctl_t apmioctl;
74 static d_kqfilter_t apmkqfilter;
75
76 #define CDEV_MAJOR 39
77 static struct dev_ops apm_ops = {
78         { "apm", CDEV_MAJOR, 0 },
79         .d_open = apmopen,
80         .d_close = apmclose,
81         .d_write = apmwrite,
82         .d_ioctl = apmioctl,
83         .d_kqfilter = apmkqfilter
84 };
85
86 static int
87 acpi_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) {
106                 if (battp->cap >= 50)
107                         state = 0;      /* high */
108                 else
109                         state = 1;      /* low */
110         }
111
112         return (state);
113 }
114
115 static int
116 acpi_capm_convert_battflags(struct  acpi_battinfo *battp)
117 {
118         int     flags;
119
120         flags = 0;
121
122         if (battp->cap >= 50)
123                 flags |= APM_BATT_HIGH;
124         else {
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
138 static int
139 acpi_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
155         if (acpi_battery_get_battinfo(NULL, &batt)) {
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
170 static int
171 acpi_capm_get_pwstatus(apm_pwstatus_t app)
172 {
173         device_t dev;
174         int     acline, unit, error;
175         struct  acpi_battinfo batt;
176
177         if (app->ap_device != PMDV_ALLDEV &&
178             (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
179                 return (1);
180
181         if (app->ap_device == PMDV_ALLDEV)
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)
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
207 static int
208 apmopen(struct dev_open_args *ap)
209 {
210         return (0);
211 }
212
213 static int
214 apmclose(struct dev_close_args *ap)
215 {
216         return (0);
217 }
218
219 static int
220 apmioctl(struct dev_ioctl_args *ap)
221 {
222         int     error = 0;
223         struct  acpi_softc *acpi_sc;
224         struct apm_info info;
225         apm_info_old_t aiop;
226
227         acpi_sc = device_get_softc(acpi_dev);
228
229         switch (ap->a_cmd) {
230         case APMIO_SUSPEND:
231                 if ((ap->a_fflag & FWRITE) == 0)
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:
239                 if ((ap->a_fflag & FWRITE) == 0)
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;
249                 aiop = (apm_info_old_t)ap->a_data;
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:
258                 if (acpi_capm_get_info((apm_info_t)ap->a_data))
259                         error = ENXIO;
260                 break;
261         case APMIO_GETPWSTATUS:
262                 if (acpi_capm_get_pwstatus((apm_pwstatus_t)ap->a_data))
263                         error = ENXIO;
264                 break;
265         case APMIO_ENABLE:
266                 if ((ap->a_fflag & FWRITE) == 0)
267                         return (EPERM);
268                 apm_softc.active = 1;
269                 break;
270         case APMIO_DISABLE:
271                 if ((ap->a_fflag & FWRITE) == 0)
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:
280                 if ((ap->a_fflag & FWRITE) == 0)
281                         return (EPERM);
282                 break;
283         case APMIO_BIOS:
284                 if ((ap->a_fflag & FWRITE) == 0)
285                         return (EPERM);
286                 bzero(ap->a_data, sizeof(struct apm_bios_arg));
287                 break;
288         default:
289                 error = EINVAL;
290                 break;
291         }
292
293         return (error);
294 }
295
296 static int
297 apmwrite(struct dev_write_args *ap)
298 {
299         return (ap->a_uio->uio_resid);
300 }
301
302 static void apmfilter_detach(struct knote *);
303 static int apmfilter(struct knote *, long);
304
305 static struct filterops apmfilterops =
306         { FILTEROP_ISFD, NULL, apmfilter_detach, apmfilter };
307
308 static int
309 apmkqfilter(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
318 static void
319 apmfilter_detach(struct knote *kn) {}
320
321 static int
322 apmfilter(struct knote *kn, long hint)
323 {
324         return (0);
325 }
326
327 static void
328 acpi_capm_init(struct acpi_softc *sc)
329 {
330         make_dev(&apm_ops, 0, 0, 5, 0664, "apm");
331         make_dev(&apm_ops, 8, 0, 5, 0664, "apm");
332         kprintf("Warning: ACPI is disabling APM's device.  You can't run both\n");
333 }
334
335 /*
336  * Lazy initialize intr_model
337  */
338 static int
339 acpi_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
354 int
355 acpi_machdep_init(device_t dev)
356 {
357         struct  acpi_softc *sc;
358
359         acpi_dev = dev;
360         sc = device_get_softc(acpi_dev);
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
372         if (acpi_GetDefaultIntrModel() == ACPI_INTR_PIC)
373                 BUS_CONFIG_INTR(dev, dev, AcpiGbl_FADT.SciInterrupt,
374                     INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW);
375         else
376                 acpi_SetIntrModel(acpi_GetDefaultIntrModel());
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
386 void
387 acpi_SetDefaultIntrModel(int model)
388 {
389
390         intr_model = model;
391 }