acpi_apm.c revision 197134
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 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/i386/acpica/acpi_machdep.c 197134 2009-09-12 20:03:45Z rwatson $"); 29 30#include <sys/param.h> 31#include <sys/bus.h> 32#include <sys/condvar.h> 33#include <sys/conf.h> 34#include <sys/fcntl.h> 35#include <sys/kernel.h> 36#include <sys/malloc.h> 37#include <sys/module.h> 38#include <sys/poll.h> 39#include <sys/sysctl.h> 40#include <sys/uio.h> 41#include <vm/vm.h> 42#include <vm/pmap.h> 43 44#include <contrib/dev/acpica/include/acpi.h> 45 46#include <dev/acpica/acpivar.h> 47#include <dev/acpica/acpiio.h> 48 49#include <machine/nexusvar.h> 50 51/* 52 * APM driver emulation 53 */ 54 55#include <machine/apm_bios.h> 56#include <machine/pc/bios.h> 57 58#include <i386/bios/apm.h> 59 60SYSCTL_DECL(_debug_acpi); 61 62uint32_t acpi_resume_beep; 63TUNABLE_INT("debug.acpi.resume_beep", &acpi_resume_beep); 64SYSCTL_UINT(_debug_acpi, OID_AUTO, resume_beep, CTLFLAG_RW, &acpi_resume_beep, 65 0, "Beep the PC speaker when resuming"); 66uint32_t acpi_reset_video; 67TUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video); 68 69static int intr_model = ACPI_INTR_PIC; 70static int apm_active; 71static struct clonedevs *apm_clones; 72 73MALLOC_DEFINE(M_APMDEV, "apmdev", "APM device emulation"); 74 75static d_open_t apmopen; 76static d_close_t apmclose; 77static d_write_t apmwrite; 78static d_ioctl_t apmioctl; 79static d_poll_t apmpoll; 80static d_kqfilter_t apmkqfilter; 81static void apmreadfiltdetach(struct knote *kn); 82static int apmreadfilt(struct knote *kn, long hint); 83static struct filterops apm_readfiltops = { 84 .f_isfd = 1, 85 .f_detach = apmreadfiltdetach, 86 .f_event = apmreadfilt, 87}; 88 89static struct cdevsw apm_cdevsw = { 90 .d_version = D_VERSION, 91 .d_flags = D_TRACKCLOSE | D_NEEDMINOR, 92 .d_open = apmopen, 93 .d_close = apmclose, 94 .d_write = apmwrite, 95 .d_ioctl = apmioctl, 96 .d_poll = apmpoll, 97 .d_name = "apm", 98 .d_kqfilter = apmkqfilter 99}; 100 101static int 102acpi_capm_convert_battstate(struct acpi_battinfo *battp) 103{ 104 int state; 105 106 state = APM_UNKNOWN; 107 108 if (battp->state & ACPI_BATT_STAT_DISCHARG) { 109 if (battp->cap >= 50) 110 state = 0; /* high */ 111 else 112 state = 1; /* low */ 113 } 114 if (battp->state & ACPI_BATT_STAT_CRITICAL) 115 state = 2; /* critical */ 116 if (battp->state & ACPI_BATT_STAT_CHARGING) 117 state = 3; /* charging */ 118 119 /* If still unknown, determine it based on the battery capacity. */ 120 if (state == APM_UNKNOWN) { 121 if (battp->cap >= 50) 122 state = 0; /* high */ 123 else 124 state = 1; /* low */ 125 } 126 127 return (state); 128} 129 130static int 131acpi_capm_convert_battflags(struct acpi_battinfo *battp) 132{ 133 int flags; 134 135 flags = 0; 136 137 if (battp->cap >= 50) 138 flags |= APM_BATT_HIGH; 139 else { 140 if (battp->state & ACPI_BATT_STAT_CRITICAL) 141 flags |= APM_BATT_CRITICAL; 142 else 143 flags |= APM_BATT_LOW; 144 } 145 if (battp->state & ACPI_BATT_STAT_CHARGING) 146 flags |= APM_BATT_CHARGING; 147 if (battp->state == ACPI_BATT_STAT_NOT_PRESENT) 148 flags = APM_BATT_NOT_PRESENT; 149 150 return (flags); 151} 152 153static int 154acpi_capm_get_info(apm_info_t aip) 155{ 156 int acline; 157 struct acpi_battinfo batt; 158 159 aip->ai_infoversion = 1; 160 aip->ai_major = 1; 161 aip->ai_minor = 2; 162 aip->ai_status = apm_active; 163 aip->ai_capabilities= 0xff00; /* unknown */ 164 165 if (acpi_acad_get_acline(&acline)) 166 aip->ai_acline = APM_UNKNOWN; /* unknown */ 167 else 168 aip->ai_acline = acline; /* on/off */ 169 170 if (acpi_battery_get_battinfo(NULL, &batt) != 0) { 171 aip->ai_batt_stat = APM_UNKNOWN; 172 aip->ai_batt_life = APM_UNKNOWN; 173 aip->ai_batt_time = -1; /* unknown */ 174 aip->ai_batteries = ~0U; /* unknown */ 175 } else { 176 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt); 177 aip->ai_batt_life = batt.cap; 178 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 179 aip->ai_batteries = acpi_battery_get_units(); 180 } 181 182 return (0); 183} 184 185static int 186acpi_capm_get_pwstatus(apm_pwstatus_t app) 187{ 188 device_t dev; 189 int acline, unit, error; 190 struct acpi_battinfo batt; 191 192 if (app->ap_device != PMDV_ALLDEV && 193 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL)) 194 return (1); 195 196 if (app->ap_device == PMDV_ALLDEV) 197 error = acpi_battery_get_battinfo(NULL, &batt); 198 else { 199 unit = app->ap_device - PMDV_BATT0; 200 dev = devclass_get_device(devclass_find("battery"), unit); 201 if (dev != NULL) 202 error = acpi_battery_get_battinfo(dev, &batt); 203 else 204 error = ENXIO; 205 } 206 if (error) 207 return (1); 208 209 app->ap_batt_stat = acpi_capm_convert_battstate(&batt); 210 app->ap_batt_flag = acpi_capm_convert_battflags(&batt); 211 app->ap_batt_life = batt.cap; 212 app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 213 214 if (acpi_acad_get_acline(&acline)) 215 app->ap_acline = APM_UNKNOWN; 216 else 217 app->ap_acline = acline; /* on/off */ 218 219 return (0); 220} 221 222/* Create single-use devices for /dev/apm and /dev/apmctl. */ 223static void 224apm_clone(void *arg, struct ucred *cred, char *name, int namelen, 225 struct cdev **dev) 226{ 227 int ctl_dev, unit; 228 229 if (*dev != NULL) 230 return; 231 if (strcmp(name, "apmctl") == 0) 232 ctl_dev = TRUE; 233 else if (strcmp(name, "apm") == 0) 234 ctl_dev = FALSE; 235 else 236 return; 237 238 /* Always create a new device and unit number. */ 239 unit = -1; 240 if (clone_create(&apm_clones, &apm_cdevsw, &unit, dev, 0)) { 241 if (ctl_dev) { 242 *dev = make_dev(&apm_cdevsw, unit, 243 UID_ROOT, GID_OPERATOR, 0660, "apmctl%d", unit); 244 } else { 245 *dev = make_dev(&apm_cdevsw, unit, 246 UID_ROOT, GID_OPERATOR, 0664, "apm%d", unit); 247 } 248 if (*dev != NULL) { 249 dev_ref(*dev); 250 (*dev)->si_flags |= SI_CHEAPCLONE; 251 } 252 } 253} 254 255/* Create a struct for tracking per-device suspend notification. */ 256static struct apm_clone_data * 257apm_create_clone(struct cdev *dev, struct acpi_softc *acpi_sc) 258{ 259 struct apm_clone_data *clone; 260 261 clone = malloc(sizeof(*clone), M_APMDEV, M_WAITOK); 262 clone->cdev = dev; 263 clone->acpi_sc = acpi_sc; 264 clone->notify_status = APM_EV_NONE; 265 bzero(&clone->sel_read, sizeof(clone->sel_read)); 266 knlist_init_mtx(&clone->sel_read.si_note, &acpi_mutex); 267 268 /* 269 * The acpi device is always managed by devd(8) and is considered 270 * writable (i.e., ack is required to allow suspend to proceed.) 271 */ 272 if (strcmp("acpi", devtoname(dev)) == 0) 273 clone->flags = ACPI_EVF_DEVD | ACPI_EVF_WRITE; 274 else 275 clone->flags = ACPI_EVF_NONE; 276 277 ACPI_LOCK(acpi); 278 STAILQ_INSERT_TAIL(&acpi_sc->apm_cdevs, clone, entries); 279 ACPI_UNLOCK(acpi); 280 return (clone); 281} 282 283static int 284apmopen(struct cdev *dev, int flag, int fmt, struct thread *td) 285{ 286 struct acpi_softc *acpi_sc; 287 struct apm_clone_data *clone; 288 289 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0); 290 clone = apm_create_clone(dev, acpi_sc); 291 dev->si_drv1 = clone; 292 293 /* If the device is opened for write, record that. */ 294 if ((flag & FWRITE) != 0) 295 clone->flags |= ACPI_EVF_WRITE; 296 297 return (0); 298} 299 300static int 301apmclose(struct cdev *dev, int flag, int fmt, struct thread *td) 302{ 303 struct apm_clone_data *clone; 304 struct acpi_softc *acpi_sc; 305 306 clone = dev->si_drv1; 307 acpi_sc = clone->acpi_sc; 308 309 /* We are about to lose a reference so check if suspend should occur */ 310 if (acpi_sc->acpi_next_sstate != 0 && 311 clone->notify_status != APM_EV_ACKED) 312 acpi_AckSleepState(clone, 0); 313 314 /* Remove this clone's data from the list and free it. */ 315 ACPI_LOCK(acpi); 316 STAILQ_REMOVE(&acpi_sc->apm_cdevs, clone, apm_clone_data, entries); 317 knlist_destroy(&clone->sel_read.si_note); 318 ACPI_UNLOCK(acpi); 319 free(clone, M_APMDEV); 320 destroy_dev_sched(dev); 321 return (0); 322} 323 324static int 325apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 326{ 327 int error; 328 struct apm_clone_data *clone; 329 struct acpi_softc *acpi_sc; 330 struct apm_info info; 331 struct apm_event_info *ev_info; 332 apm_info_old_t aiop; 333 334 error = 0; 335 clone = dev->si_drv1; 336 acpi_sc = clone->acpi_sc; 337 338 switch (cmd) { 339 case APMIO_SUSPEND: 340 if ((flag & FWRITE) == 0) 341 return (EPERM); 342 if (acpi_sc->acpi_next_sstate == 0) { 343 if (acpi_sc->acpi_suspend_sx != ACPI_STATE_S5) { 344 error = acpi_ReqSleepState(acpi_sc, 345 acpi_sc->acpi_suspend_sx); 346 } else { 347 printf( 348 "power off via apm suspend not supported\n"); 349 error = ENXIO; 350 } 351 } else 352 error = acpi_AckSleepState(clone, 0); 353 break; 354 case APMIO_STANDBY: 355 if ((flag & FWRITE) == 0) 356 return (EPERM); 357 if (acpi_sc->acpi_next_sstate == 0) { 358 if (acpi_sc->acpi_standby_sx != ACPI_STATE_S5) { 359 error = acpi_ReqSleepState(acpi_sc, 360 acpi_sc->acpi_standby_sx); 361 } else { 362 printf( 363 "power off via apm standby not supported\n"); 364 error = ENXIO; 365 } 366 } else 367 error = acpi_AckSleepState(clone, 0); 368 break; 369 case APMIO_NEXTEVENT: 370 printf("apm nextevent start\n"); 371 ACPI_LOCK(acpi); 372 if (acpi_sc->acpi_next_sstate != 0 && clone->notify_status == 373 APM_EV_NONE) { 374 ev_info = (struct apm_event_info *)addr; 375 if (acpi_sc->acpi_next_sstate <= ACPI_STATE_S3) 376 ev_info->type = PMEV_STANDBYREQ; 377 else 378 ev_info->type = PMEV_SUSPENDREQ; 379 ev_info->index = 0; 380 clone->notify_status = APM_EV_NOTIFIED; 381 printf("apm event returning %d\n", ev_info->type); 382 } else 383 error = EAGAIN; 384 ACPI_UNLOCK(acpi); 385 break; 386 case APMIO_GETINFO_OLD: 387 if (acpi_capm_get_info(&info)) 388 error = ENXIO; 389 aiop = (apm_info_old_t)addr; 390 aiop->ai_major = info.ai_major; 391 aiop->ai_minor = info.ai_minor; 392 aiop->ai_acline = info.ai_acline; 393 aiop->ai_batt_stat = info.ai_batt_stat; 394 aiop->ai_batt_life = info.ai_batt_life; 395 aiop->ai_status = info.ai_status; 396 break; 397 case APMIO_GETINFO: 398 if (acpi_capm_get_info((apm_info_t)addr)) 399 error = ENXIO; 400 break; 401 case APMIO_GETPWSTATUS: 402 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr)) 403 error = ENXIO; 404 break; 405 case APMIO_ENABLE: 406 if ((flag & FWRITE) == 0) 407 return (EPERM); 408 apm_active = 1; 409 break; 410 case APMIO_DISABLE: 411 if ((flag & FWRITE) == 0) 412 return (EPERM); 413 apm_active = 0; 414 break; 415 case APMIO_HALTCPU: 416 break; 417 case APMIO_NOTHALTCPU: 418 break; 419 case APMIO_DISPLAY: 420 if ((flag & FWRITE) == 0) 421 return (EPERM); 422 break; 423 case APMIO_BIOS: 424 if ((flag & FWRITE) == 0) 425 return (EPERM); 426 bzero(addr, sizeof(struct apm_bios_arg)); 427 break; 428 default: 429 error = EINVAL; 430 break; 431 } 432 433 return (error); 434} 435 436static int 437apmwrite(struct cdev *dev, struct uio *uio, int ioflag) 438{ 439 return (uio->uio_resid); 440} 441 442static int 443apmpoll(struct cdev *dev, int events, struct thread *td) 444{ 445 struct apm_clone_data *clone; 446 int revents; 447 448 revents = 0; 449 ACPI_LOCK(acpi); 450 clone = dev->si_drv1; 451 if (clone->acpi_sc->acpi_next_sstate) 452 revents |= events & (POLLIN | POLLRDNORM); 453 else 454 selrecord(td, &clone->sel_read); 455 ACPI_UNLOCK(acpi); 456 return (revents); 457} 458 459static int 460apmkqfilter(struct cdev *dev, struct knote *kn) 461{ 462 struct apm_clone_data *clone; 463 464 ACPI_LOCK(acpi); 465 clone = dev->si_drv1; 466 kn->kn_hook = clone; 467 kn->kn_fop = &apm_readfiltops; 468 knlist_add(&clone->sel_read.si_note, kn, 0); 469 ACPI_UNLOCK(acpi); 470 return (0); 471} 472 473static void 474apmreadfiltdetach(struct knote *kn) 475{ 476 struct apm_clone_data *clone; 477 478 ACPI_LOCK(acpi); 479 clone = kn->kn_hook; 480 knlist_remove(&clone->sel_read.si_note, kn, 0); 481 ACPI_UNLOCK(acpi); 482} 483 484static int 485apmreadfilt(struct knote *kn, long hint) 486{ 487 struct apm_clone_data *clone; 488 int sleeping; 489 490 ACPI_LOCK(acpi); 491 clone = kn->kn_hook; 492 sleeping = clone->acpi_sc->acpi_next_sstate ? 1 : 0; 493 ACPI_UNLOCK(acpi); 494 return (sleeping); 495} 496 497int 498acpi_machdep_init(device_t dev) 499{ 500 struct acpi_softc *acpi_sc; 501 502 acpi_sc = devclass_get_softc(devclass_find("acpi"), 0); 503 504 /* Create a clone for /dev/acpi also. */ 505 STAILQ_INIT(&acpi_sc->apm_cdevs); 506 acpi_sc->acpi_clone = apm_create_clone(acpi_sc->acpi_dev_t, acpi_sc); 507 clone_setup(&apm_clones); 508 EVENTHANDLER_REGISTER(dev_clone, apm_clone, 0, 1000); 509 acpi_install_wakeup_handler(acpi_sc); 510 511 if (intr_model == ACPI_INTR_PIC) 512 BUS_CONFIG_INTR(dev, AcpiGbl_FADT.SciInterrupt, 513 INTR_TRIGGER_LEVEL, INTR_POLARITY_LOW); 514 else 515 acpi_SetIntrModel(intr_model); 516 517 SYSCTL_ADD_UINT(&acpi_sc->acpi_sysctl_ctx, 518 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO, 519 "reset_video", CTLFLAG_RW, &acpi_reset_video, 0, 520 "Call the VESA reset BIOS vector on the resume path"); 521 522 return (0); 523} 524 525void 526acpi_SetDefaultIntrModel(int model) 527{ 528 529 intr_model = model; 530} 531 532/* Check BIOS date. If 1998 or older, disable ACPI. */ 533int 534acpi_machdep_quirks(int *quirks) 535{ 536 char *va; 537 int year; 538 539 /* BIOS address 0xffff5 contains the date in the format mm/dd/yy. */ 540 va = pmap_mapbios(0xffff0, 16); 541 sscanf(va + 11, "%2d", &year); 542 pmap_unmapbios((vm_offset_t)va, 16); 543 544 /* 545 * Date must be >= 1/1/1999 or we don't trust ACPI. Note that this 546 * check must be changed by my 114th birthday. 547 */ 548 if (year > 90 && year < 99) 549 *quirks = ACPI_Q_BROKEN; 550 551 return (0); 552} 553 554void 555acpi_cpu_c1() 556{ 557 __asm __volatile("sti; hlt"); 558} 559 560/* 561 * ACPI nexus(4) driver. 562 */ 563static int 564nexus_acpi_probe(device_t dev) 565{ 566 int error; 567 568 error = acpi_identify(); 569 if (error) 570 return (error); 571 572 return (BUS_PROBE_DEFAULT); 573} 574 575static int 576nexus_acpi_attach(device_t dev) 577{ 578 579 nexus_init_resources(); 580 bus_generic_probe(dev); 581 if (BUS_ADD_CHILD(dev, 10, "acpi", 0) == NULL) 582 panic("failed to add acpi0 device"); 583 584 return (bus_generic_attach(dev)); 585} 586 587static device_method_t nexus_acpi_methods[] = { 588 /* Device interface */ 589 DEVMETHOD(device_probe, nexus_acpi_probe), 590 DEVMETHOD(device_attach, nexus_acpi_attach), 591 592 { 0, 0 } 593}; 594 595DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1, nexus_driver); 596static devclass_t nexus_devclass; 597 598DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, nexus_devclass, 0, 0); 599