]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/acpica/acpi_apm.c
Add more NUMA-specific low memory predicates.
[FreeBSD/FreeBSD.git] / sys / x86 / acpica / acpi_apm.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001 Mitsuru IWASAKI
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/condvar.h>
35 #include <sys/conf.h>
36 #include <sys/fcntl.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/poll.h>
40 #include <sys/uio.h>
41
42 #include <contrib/dev/acpica/include/acpi.h>
43
44 #include <dev/acpica/acpivar.h>
45 #include <dev/acpica/acpiio.h>
46
47 #include <machine/apm_bios.h>
48
49 /*
50  * APM driver emulation 
51  */
52
53 #define APM_UNKNOWN     0xff
54
55 static int apm_active;
56
57 static MALLOC_DEFINE(M_APMDEV, "apmdev", "APM device emulation");
58
59 static d_open_t         apmopen;
60 static d_write_t        apmwrite;
61 static d_ioctl_t        apmioctl;
62 static d_poll_t         apmpoll;
63 static d_kqfilter_t     apmkqfilter;
64 static void             apmreadfiltdetach(struct knote *kn);
65 static int              apmreadfilt(struct knote *kn, long hint);
66 static struct filterops apm_readfiltops = {
67         .f_isfd = 1,
68         .f_detach = apmreadfiltdetach,
69         .f_event = apmreadfilt,
70 };
71
72 static struct cdevsw apm_cdevsw = {
73         .d_version =    D_VERSION,
74         .d_open =       apmopen,
75         .d_write =      apmwrite,
76         .d_ioctl =      apmioctl,
77         .d_poll =       apmpoll,
78         .d_name =       "apm",
79         .d_kqfilter =   apmkqfilter
80 };
81
82 static int
83 acpi_capm_convert_battstate(struct  acpi_battinfo *battp)
84 {
85         int     state;
86
87         state = APM_UNKNOWN;
88
89         if (battp->state & ACPI_BATT_STAT_DISCHARG) {
90                 if (battp->cap >= 50)
91                         state = 0;      /* high */
92                 else
93                         state = 1;      /* low */
94         }
95         if (battp->state & ACPI_BATT_STAT_CRITICAL)
96                 state = 2;              /* critical */
97         if (battp->state & ACPI_BATT_STAT_CHARGING)
98                 state = 3;              /* charging */
99
100         /* If still unknown, determine it based on the battery capacity. */
101         if (state == APM_UNKNOWN) {
102                 if (battp->cap >= 50)
103                         state = 0;      /* high */
104                 else
105                         state = 1;      /* low */
106         }
107
108         return (state);
109 }
110
111 static int
112 acpi_capm_convert_battflags(struct  acpi_battinfo *battp)
113 {
114         int     flags;
115
116         flags = 0;
117
118         if (battp->cap >= 50)
119                 flags |= APM_BATT_HIGH;
120         else {
121                 if (battp->state & ACPI_BATT_STAT_CRITICAL)
122                         flags |= APM_BATT_CRITICAL;
123                 else
124                         flags |= APM_BATT_LOW;
125         }
126         if (battp->state & ACPI_BATT_STAT_CHARGING)
127                 flags |= APM_BATT_CHARGING;
128         if (battp->state == ACPI_BATT_STAT_NOT_PRESENT)
129                 flags = APM_BATT_NOT_PRESENT;
130
131         return (flags);
132 }
133
134 static int
135 acpi_capm_get_info(apm_info_t aip)
136 {
137         int     acline;
138         struct  acpi_battinfo batt;
139
140         aip->ai_infoversion = 1;
141         aip->ai_major       = 1;
142         aip->ai_minor       = 2;
143         aip->ai_status      = apm_active;
144         aip->ai_capabilities= 0xff00;   /* unknown */
145
146         if (acpi_acad_get_acline(&acline))
147                 aip->ai_acline = APM_UNKNOWN;   /* unknown */
148         else
149                 aip->ai_acline = acline;        /* on/off */
150
151         if (acpi_battery_get_battinfo(NULL, &batt) != 0) {
152                 aip->ai_batt_stat = APM_UNKNOWN;
153                 aip->ai_batt_life = APM_UNKNOWN;
154                 aip->ai_batt_time = -1;          /* unknown */
155                 aip->ai_batteries = ~0U;         /* unknown */
156         } else {
157                 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt);
158                 aip->ai_batt_life = batt.cap;
159                 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
160                 aip->ai_batteries = acpi_battery_get_units();
161         }
162
163         return (0);
164 }
165
166 static int
167 acpi_capm_get_pwstatus(apm_pwstatus_t app)
168 {
169         device_t dev;
170         int     acline, unit, error;
171         struct  acpi_battinfo batt;
172
173         if (app->ap_device != PMDV_ALLDEV &&
174             (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
175                 return (1);
176
177         if (app->ap_device == PMDV_ALLDEV)
178                 error = acpi_battery_get_battinfo(NULL, &batt);
179         else {
180                 unit = app->ap_device - PMDV_BATT0;
181                 dev = devclass_get_device(devclass_find("battery"), unit);
182                 if (dev != NULL)
183                         error = acpi_battery_get_battinfo(dev, &batt);
184                 else
185                         error = ENXIO;
186         }
187         if (error)
188                 return (1);
189
190         app->ap_batt_stat = acpi_capm_convert_battstate(&batt);
191         app->ap_batt_flag = acpi_capm_convert_battflags(&batt);
192         app->ap_batt_life = batt.cap;
193         app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
194
195         if (acpi_acad_get_acline(&acline))
196                 app->ap_acline = APM_UNKNOWN;
197         else
198                 app->ap_acline = acline;        /* on/off */
199
200         return (0);
201 }
202
203 /* Create a struct for tracking per-device suspend notification. */
204 static struct apm_clone_data *
205 apm_create_clone(struct cdev *dev, struct acpi_softc *acpi_sc)
206 {
207         struct apm_clone_data *clone;
208
209         clone = malloc(sizeof(*clone), M_APMDEV, M_WAITOK);
210         clone->cdev = dev;
211         clone->acpi_sc = acpi_sc;
212         clone->notify_status = APM_EV_NONE;
213         bzero(&clone->sel_read, sizeof(clone->sel_read));
214         knlist_init_mtx(&clone->sel_read.si_note, &acpi_mutex);
215
216         /*
217          * The acpi device is always managed by devd(8) and is considered
218          * writable (i.e., ack is required to allow suspend to proceed.)
219          */
220         if (strcmp("acpi", devtoname(dev)) == 0)
221                 clone->flags = ACPI_EVF_DEVD | ACPI_EVF_WRITE;
222         else
223                 clone->flags = ACPI_EVF_NONE;
224
225         ACPI_LOCK(acpi);
226         STAILQ_INSERT_TAIL(&acpi_sc->apm_cdevs, clone, entries);
227         ACPI_UNLOCK(acpi);
228         return (clone);
229 }
230
231 static void
232 apmdtor(void *data)
233 {
234         struct  apm_clone_data *clone;
235         struct  acpi_softc *acpi_sc;
236
237         clone = data;
238         acpi_sc = clone->acpi_sc;
239
240         /* We are about to lose a reference so check if suspend should occur */
241         if (acpi_sc->acpi_next_sstate != 0 &&
242             clone->notify_status != APM_EV_ACKED)
243                 acpi_AckSleepState(clone, 0);
244
245         /* Remove this clone's data from the list and free it. */
246         ACPI_LOCK(acpi);
247         STAILQ_REMOVE(&acpi_sc->apm_cdevs, clone, apm_clone_data, entries);
248         seldrain(&clone->sel_read);
249         knlist_destroy(&clone->sel_read.si_note);
250         ACPI_UNLOCK(acpi);
251         free(clone, M_APMDEV);
252 }
253
254 static int
255 apmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
256 {
257         struct  acpi_softc *acpi_sc;
258         struct  apm_clone_data *clone;
259
260         acpi_sc = devclass_get_softc(devclass_find("acpi"), 0);
261         clone = apm_create_clone(dev, acpi_sc);
262         devfs_set_cdevpriv(clone, apmdtor);
263
264         /* If the device is opened for write, record that. */
265         if ((flag & FWRITE) != 0)
266                 clone->flags |= ACPI_EVF_WRITE;
267
268         return (0);
269 }
270
271 static int
272 apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
273 {
274         int     error;
275         struct  apm_clone_data *clone;
276         struct  acpi_softc *acpi_sc;
277         struct  apm_info info;
278         struct  apm_event_info *ev_info;
279         apm_info_old_t aiop;
280
281         error = 0;
282         devfs_get_cdevpriv((void **)&clone);
283         acpi_sc = clone->acpi_sc;
284
285         switch (cmd) {
286         case APMIO_SUSPEND:
287                 if ((flag & FWRITE) == 0)
288                         return (EPERM);
289                 if (acpi_sc->acpi_next_sstate == 0) {
290                         if (acpi_sc->acpi_suspend_sx != ACPI_STATE_S5) {
291                                 error = acpi_ReqSleepState(acpi_sc,
292                                     acpi_sc->acpi_suspend_sx);
293                         } else {
294                                 printf(
295                         "power off via apm suspend not supported\n");
296                                 error = ENXIO;
297                         }
298                 } else
299                         error = acpi_AckSleepState(clone, 0);
300                 break;
301         case APMIO_STANDBY:
302                 if ((flag & FWRITE) == 0)
303                         return (EPERM);
304                 if (acpi_sc->acpi_next_sstate == 0) {
305                         if (acpi_sc->acpi_standby_sx != ACPI_STATE_S5) {
306                                 error = acpi_ReqSleepState(acpi_sc,
307                                     acpi_sc->acpi_standby_sx);
308                         } else {
309                                 printf(
310                         "power off via apm standby not supported\n");
311                                 error = ENXIO;
312                         }
313                 } else
314                         error = acpi_AckSleepState(clone, 0);
315                 break;
316         case APMIO_NEXTEVENT:
317                 printf("apm nextevent start\n");
318                 ACPI_LOCK(acpi);
319                 if (acpi_sc->acpi_next_sstate != 0 && clone->notify_status ==
320                     APM_EV_NONE) {
321                         ev_info = (struct apm_event_info *)addr;
322                         if (acpi_sc->acpi_next_sstate <= ACPI_STATE_S3)
323                                 ev_info->type = PMEV_STANDBYREQ;
324                         else
325                                 ev_info->type = PMEV_SUSPENDREQ;
326                         ev_info->index = 0;
327                         clone->notify_status = APM_EV_NOTIFIED;
328                         printf("apm event returning %d\n", ev_info->type);
329                 } else
330                         error = EAGAIN;
331                 ACPI_UNLOCK(acpi);
332                 break;
333         case APMIO_GETINFO_OLD:
334                 if (acpi_capm_get_info(&info))
335                         error = ENXIO;
336                 aiop = (apm_info_old_t)addr;
337                 aiop->ai_major = info.ai_major;
338                 aiop->ai_minor = info.ai_minor;
339                 aiop->ai_acline = info.ai_acline;
340                 aiop->ai_batt_stat = info.ai_batt_stat;
341                 aiop->ai_batt_life = info.ai_batt_life;
342                 aiop->ai_status = info.ai_status;
343                 break;
344         case APMIO_GETINFO:
345                 if (acpi_capm_get_info((apm_info_t)addr))
346                         error = ENXIO;
347                 break;
348         case APMIO_GETPWSTATUS:
349                 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr))
350                         error = ENXIO;
351                 break;
352         case APMIO_ENABLE:
353                 if ((flag & FWRITE) == 0)
354                         return (EPERM);
355                 apm_active = 1;
356                 break;
357         case APMIO_DISABLE:
358                 if ((flag & FWRITE) == 0)
359                         return (EPERM);
360                 apm_active = 0;
361                 break;
362         case APMIO_HALTCPU:
363                 break;
364         case APMIO_NOTHALTCPU:
365                 break;
366         case APMIO_DISPLAY:
367                 if ((flag & FWRITE) == 0)
368                         return (EPERM);
369                 break;
370         case APMIO_BIOS:
371                 if ((flag & FWRITE) == 0)
372                         return (EPERM);
373                 bzero(addr, sizeof(struct apm_bios_arg));
374                 break;
375         default:
376                 error = EINVAL;
377                 break;
378         }
379
380         return (error);
381 }
382
383 static int
384 apmwrite(struct cdev *dev, struct uio *uio, int ioflag)
385 {
386         return (uio->uio_resid);
387 }
388
389 static int
390 apmpoll(struct cdev *dev, int events, struct thread *td)
391 {
392         struct  apm_clone_data *clone;
393         int revents;
394
395         revents = 0;
396         devfs_get_cdevpriv((void **)&clone);
397         ACPI_LOCK(acpi);
398         if (clone->acpi_sc->acpi_next_sstate)
399                 revents |= events & (POLLIN | POLLRDNORM);
400         else
401                 selrecord(td, &clone->sel_read);
402         ACPI_UNLOCK(acpi);
403         return (revents);
404 }
405
406 static int
407 apmkqfilter(struct cdev *dev, struct knote *kn)
408 {
409         struct  apm_clone_data *clone;
410
411         devfs_get_cdevpriv((void **)&clone);
412         ACPI_LOCK(acpi);
413         kn->kn_hook = clone;
414         kn->kn_fop = &apm_readfiltops;
415         knlist_add(&clone->sel_read.si_note, kn, 0);
416         ACPI_UNLOCK(acpi);
417         return (0);
418 }
419
420 static void
421 apmreadfiltdetach(struct knote *kn)
422 {
423         struct  apm_clone_data *clone;
424
425         ACPI_LOCK(acpi);
426         clone = kn->kn_hook;
427         knlist_remove(&clone->sel_read.si_note, kn, 0);
428         ACPI_UNLOCK(acpi);
429 }
430
431 static int
432 apmreadfilt(struct knote *kn, long hint)
433 {
434         struct  apm_clone_data *clone;
435         int     sleeping;
436
437         ACPI_LOCK(acpi);
438         clone = kn->kn_hook;
439         sleeping = clone->acpi_sc->acpi_next_sstate ? 1 : 0;
440         ACPI_UNLOCK(acpi);
441         return (sleeping);
442 }
443
444 void
445 acpi_apm_init(struct acpi_softc *sc)
446 {
447
448         /* Create a clone for /dev/acpi also. */
449         STAILQ_INIT(&sc->apm_cdevs);
450         sc->acpi_clone = apm_create_clone(sc->acpi_dev_t, sc);
451
452         make_dev(&apm_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0660, "apmctl");
453         make_dev(&apm_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0664, "apm");
454 }