acpi.c revision 69585
1/*- 2 * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org> 3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4 * Copyright (c) 2000 Michael Smith 5 * Copyright (c) 2000 BSDi 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: head/sys/dev/acpica/acpi.c 69585 2000-12-05 00:19:10Z msmith $ 30 */ 31 32#include "opt_acpi.h" 33#include <sys/param.h> 34#include <sys/kernel.h> 35#include <sys/malloc.h> 36#include <sys/bus.h> 37#include <sys/conf.h> 38#include <sys/ioccom.h> 39#include <sys/reboot.h> 40#include <sys/sysctl.h> 41#include <sys/ctype.h> 42 43#include <machine/clock.h> 44 45#include <machine/resource.h> 46 47#include "acpi.h" 48 49#include <dev/acpica/acpivar.h> 50#include <dev/acpica/acpiio.h> 51 52MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 53 54/* 55 * Character device 56 */ 57 58static d_open_t acpiopen; 59static d_close_t acpiclose; 60static d_ioctl_t acpiioctl; 61 62#define CDEV_MAJOR 152 63static struct cdevsw acpi_cdevsw = { 64 acpiopen, 65 acpiclose, 66 noread, 67 nowrite, 68 acpiioctl, 69 nopoll, 70 nommap, 71 nostrategy, 72 "acpi", 73 CDEV_MAJOR, 74 nodump, 75 nopsize, 76 0, 77 -1 78}; 79 80static void acpi_identify(driver_t *driver, device_t parent); 81static int acpi_probe(device_t dev); 82static int acpi_attach(device_t dev); 83static device_t acpi_add_child(device_t bus, int order, const char *name, int unit); 84static int acpi_print_resources(struct resource_list *rl, const char *name, int type, 85 const char *format); 86static int acpi_print_child(device_t bus, device_t child); 87static int acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result); 88static int acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value); 89static int acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, 90 u_long count); 91static int acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, 92 u_long *countp); 93static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 94 u_long start, u_long end, u_long count, u_int flags); 95static int acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r); 96 97static void acpi_probe_children(device_t bus); 98static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status); 99 100static void acpi_shutdown_pre_sync(void *arg, int howto); 101static void acpi_shutdown_final(void *arg, int howto); 102 103#ifdef ACPI_DEBUG 104static void acpi_set_debugging(void); 105#endif 106 107static void acpi_system_eventhandler_sleep(void *arg, int state); 108static void acpi_system_eventhandler_wakeup(void *arg, int state); 109 110static device_method_t acpi_methods[] = { 111 /* Device interface */ 112 DEVMETHOD(device_identify, acpi_identify), 113 DEVMETHOD(device_probe, acpi_probe), 114 DEVMETHOD(device_attach, acpi_attach), 115 DEVMETHOD(device_shutdown, bus_generic_shutdown), 116 DEVMETHOD(device_suspend, bus_generic_suspend), 117 DEVMETHOD(device_resume, bus_generic_resume), 118 119 /* Bus interface */ 120 DEVMETHOD(bus_add_child, acpi_add_child), 121 DEVMETHOD(bus_print_child, acpi_print_child), 122 DEVMETHOD(bus_read_ivar, acpi_read_ivar), 123 DEVMETHOD(bus_write_ivar, acpi_write_ivar), 124 DEVMETHOD(bus_set_resource, acpi_set_resource), 125 DEVMETHOD(bus_get_resource, acpi_get_resource), 126 DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 127 DEVMETHOD(bus_release_resource, acpi_release_resource), 128 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 129 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 130 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 131 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 132 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 133 134 {0, 0} 135}; 136 137static driver_t acpi_driver = { 138 "acpi", 139 acpi_methods, 140 sizeof(struct acpi_softc), 141}; 142 143devclass_t acpi_devclass; 144DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, 0, 0); 145 146SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, ""); 147SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, ""); 148 149/* 150 * Detect ACPI, perform early initialisation 151 */ 152static void 153acpi_identify(driver_t *driver, device_t parent) 154{ 155 device_t child; 156 ACPI_PHYSICAL_ADDRESS rsdp; 157 int error; 158#ifdef ENABLE_DEBUGGER 159 char *debugpoint = getenv("debug.acpi.debugger"); 160#endif 161 162 if(!cold){ 163 printf("Don't load this driver from userland!!\n"); 164 return ; 165 } 166 167 /* 168 * Make sure we're not being doubly invoked. 169 */ 170 if (device_find_child(parent, "acpi", 0) != NULL) 171 return; 172 173#ifdef ACPI_DEBUG 174 acpi_set_debugging(); 175#endif 176 177 /* 178 * Start up ACPICA 179 */ 180#ifdef ENABLE_DEBUGGER 181 if (debugpoint && !strcmp(debugpoint, "init")) 182 acpi_EnterDebugger(); 183#endif 184 if ((error = AcpiInitializeSubsystem()) != AE_OK) { 185 printf("ACPI: initialisation failed: %s\n", acpi_strerror(error)); 186 return; 187 } 188#ifdef ENABLE_DEBUGGER 189 if (debugpoint && !strcmp(debugpoint, "tables")) 190 acpi_EnterDebugger(); 191#endif 192 if (((error = AcpiFindRootPointer(&rsdp)) != AE_OK) || 193 ((error = AcpiLoadTables(rsdp)) != AE_OK)) { 194 printf("ACPI: table load failed: %s\n", acpi_strerror(error)); 195 return; 196 } 197 198 /* 199 * Attach the actual ACPI device. 200 */ 201 if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 202 device_printf(parent, "ACPI: could not attach\n"); 203 return; 204 } 205} 206 207/* 208 * Fetch some descriptive data from ACPI to put in our attach message 209 */ 210static int 211acpi_probe(device_t dev) 212{ 213 ACPI_TABLE_HEADER th; 214 char buf[20]; 215 int error; 216 217 if ((error = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th)) != AE_OK) { 218 device_printf(dev, "couldn't get XSDT header: %s\n", acpi_strerror(error)); 219 return(ENXIO); 220 } 221 sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId); 222 device_set_desc_copy(dev, buf); 223 224 return(0); 225} 226 227static int 228acpi_attach(device_t dev) 229{ 230 struct acpi_softc *sc; 231 int error; 232#ifdef ENABLE_DEBUGGER 233 char *debugpoint = getenv("debug.acpi.debugger"); 234#endif 235 236 237 sc = device_get_softc(dev); 238 bzero(sc, sizeof(*sc)); 239 sc->acpi_dev = dev; 240 241#ifdef ENABLE_DEBUGGER 242 if (debugpoint && !strcmp(debugpoint, "spaces")) 243 acpi_EnterDebugger(); 244#endif 245 246 /* 247 * Install the default address space handlers. 248 */ 249 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 250 ADDRESS_SPACE_SYSTEM_MEMORY, 251 ACPI_DEFAULT_HANDLER, 252 NULL, NULL)) != AE_OK) { 253 device_printf(dev, "could not initialise SystemMemory handler: %s\n", acpi_strerror(error)); 254 return(ENXIO); 255 } 256 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 257 ADDRESS_SPACE_SYSTEM_IO, 258 ACPI_DEFAULT_HANDLER, 259 NULL, NULL)) != AE_OK) { 260 device_printf(dev, "could not initialise SystemIO handler: %s\n", acpi_strerror(error)); 261 return(ENXIO); 262 } 263 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 264 ADDRESS_SPACE_PCI_CONFIG, 265 ACPI_DEFAULT_HANDLER, 266 NULL, NULL)) != AE_OK) { 267 device_printf(dev, "could not initialise PciConfig handler: %s\n", acpi_strerror(error)); 268 return(ENXIO); 269 } 270 271 /* 272 * Bring ACPI fully online. 273 * 274 * Note that we request that device _STA and _INI methods not be run (ACPI_NO_DEVICE_INIT) 275 * and the final object initialisation pass be skipped (ACPI_NO_OBJECT_INIT). 276 * 277 * XXX We need to arrange for the object init pass after we have attached all our 278 * child devices. 279 */ 280#ifdef ENABLE_DEBUGGER 281 if (debugpoint && !strcmp(debugpoint, "enable")) 282 acpi_EnterDebugger(); 283#endif 284 if ((error = AcpiEnableSubsystem(ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT)) != AE_OK) { 285 device_printf(dev, "could not enable ACPI: %s\n", acpi_strerror(error)); 286 return(ENXIO); 287 } 288 289 /* 290 * Dispatch the default sleep state to devices. 291 * TBD: should be configured from userland policy manager. 292 */ 293 sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX; 294 sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX; 295 sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX; 296 297 /* Enable and clear fixed events and install handlers. */ 298 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 299 AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 300 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 301 AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, acpi_eventhandler_power_button_for_sleep, sc); 302 device_printf(dev, "power button is handled as a fixed feature programming model.\n"); 303 } 304 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 305 AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 306 AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 307 AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, acpi_eventhandler_sleep_button_for_sleep, sc); 308 device_printf(dev, "sleep button is handled as a fixed feature programming model.\n"); 309 } 310 311 /* 312 * Scan the namespace and attach/initialise children. 313 */ 314#ifdef ENABLE_DEBUGGER 315 if (debugpoint && !strcmp(debugpoint, "probe")) 316 acpi_EnterDebugger(); 317#endif 318 acpi_probe_children(dev); 319 320 /* 321 * Register our shutdown handlers 322 */ 323 EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST); 324 EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST); 325 326 /* 327 * Register our acpi event handlers. 328 * XXX should be configurable eg. via userland policy manager. 329 */ 330 EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST); 331 EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST); 332 333 /* 334 * Flag our initial states. 335 */ 336 sc->acpi_enabled = 1; 337 sc->acpi_sstate = ACPI_STATE_S0; 338 339 /* 340 * Create the control device 341 */ 342 sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi"); 343 sc->acpi_dev_t->si_drv1 = sc; 344 345#ifdef ENABLE_DEBUGGER 346 if (debugpoint && !strcmp(debugpoint, "running")) 347 acpi_EnterDebugger(); 348#endif 349 return(0); 350} 351 352/* 353 * Handle a new device being added 354 */ 355static device_t 356acpi_add_child(device_t bus, int order, const char *name, int unit) 357{ 358 struct acpi_device *ad; 359 device_t child; 360 361 if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL) 362 return(NULL); 363 bzero(ad, sizeof(*ad)); 364 365 resource_list_init(&ad->ad_rl); 366 367 child = device_add_child_ordered(bus, order, name, unit); 368 if (child != NULL) 369 device_set_ivars(child, ad); 370 return(child); 371} 372 373/* 374 * Print child device resource usage 375 */ 376static int 377acpi_print_resources(struct resource_list *rl, const char *name, int type, const char *format) 378{ 379 struct resource_list_entry *rle; 380 int printed, retval; 381 382 printed = 0; 383 retval = 0; 384 385 if (!SLIST_FIRST(rl)) 386 return(0); 387 388 /* Yes, this is kinda cheating */ 389 SLIST_FOREACH(rle, rl, link) { 390 if (rle->type == type) { 391 if (printed == 0) 392 retval += printf(" %s ", name); 393 else if (printed > 0) 394 retval += printf(","); 395 printed++; 396 retval += printf(format, rle->start); 397 if (rle->count > 1) { 398 retval += printf("-"); 399 retval += printf(format, rle->start + 400 rle->count - 1); 401 } 402 } 403 } 404 return(retval); 405} 406 407static int 408acpi_print_child(device_t bus, device_t child) 409{ 410 struct acpi_device *adev = device_get_ivars(child); 411 struct resource_list *rl = &adev->ad_rl; 412 int retval = 0; 413 414 retval += bus_print_child_header(bus, child); 415 retval += acpi_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx"); 416 retval += acpi_print_resources(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 417 retval += acpi_print_resources(rl, "irq", SYS_RES_IRQ, "%ld"); 418 retval += bus_print_child_footer(bus, child); 419 420 return(retval); 421} 422 423 424/* 425 * Handle per-device ivars 426 */ 427static int 428acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 429{ 430 struct acpi_device *ad; 431 432 if ((ad = device_get_ivars(child)) == NULL) { 433 printf("device has no ivars\n"); 434 return(ENOENT); 435 } 436 437 switch(index) { 438 /* ACPI ivars */ 439 case ACPI_IVAR_HANDLE: 440 *(ACPI_HANDLE *)result = ad->ad_handle; 441 break; 442 case ACPI_IVAR_MAGIC: 443 *(int *)result = ad->ad_magic; 444 break; 445 case ACPI_IVAR_PRIVATE: 446 *(void **)result = ad->ad_private; 447 break; 448 449 default: 450 panic("bad ivar read request (%d)\n", index); 451 return(ENOENT); 452 } 453 return(0); 454} 455 456static int 457acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 458{ 459 struct acpi_device *ad; 460 461 if ((ad = device_get_ivars(child)) == NULL) { 462 printf("device has no ivars\n"); 463 return(ENOENT); 464 } 465 466 switch(index) { 467 /* ACPI ivars */ 468 case ACPI_IVAR_HANDLE: 469 ad->ad_handle = (ACPI_HANDLE)value; 470 break; 471 case ACPI_IVAR_MAGIC: 472 ad->ad_magic = (int )value; 473 break; 474 case ACPI_IVAR_PRIVATE: 475 ad->ad_private = (void *)value; 476 break; 477 478 default: 479 panic("bad ivar write request (%d)\n", index); 480 return(ENOENT); 481 } 482 return(0); 483} 484 485/* 486 * Handle child resource allocation/removal 487 */ 488static int 489acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count) 490{ 491 struct acpi_device *ad = device_get_ivars(child); 492 struct resource_list *rl = &ad->ad_rl; 493 494 resource_list_add(rl, type, rid, start, start + count -1, count); 495 496 return(0); 497} 498 499static int 500acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) 501{ 502 struct acpi_device *ad = device_get_ivars(child); 503 struct resource_list *rl = &ad->ad_rl; 504 struct resource_list_entry *rle; 505 506 rle = resource_list_find(rl, type, rid); 507 if (!rle) 508 return(ENOENT); 509 510 if (startp) 511 *startp = rle->start; 512 if (countp) 513 *countp = rle->count; 514 515 return(0); 516} 517 518static struct resource * 519acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 520 u_long start, u_long end, u_long count, u_int flags) 521{ 522 struct acpi_device *ad = device_get_ivars(child); 523 struct resource_list *rl = &ad->ad_rl; 524 525 return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags)); 526} 527 528static int 529acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 530{ 531 struct acpi_device *ad = device_get_ivars(child); 532 struct resource_list *rl = &ad->ad_rl; 533 534 return(resource_list_release(rl, bus, child, type, rid, r)); 535} 536 537/* 538 * Scan relevant portions of the ACPI namespace and attach child devices. 539 * 540 * Note that we only expect to find devices in the \_TZ_, \_SI_ and \_SB_ scopes, 541 * and \_TZ_ becomes obsolete in the ACPI 2.0 spec. 542 */ 543static void 544acpi_probe_children(device_t bus) 545{ 546 ACPI_HANDLE parent; 547 static char *scopes[] = {"\\_TZ_", "\\_SI", "\\_SB_", NULL}; 548 int i; 549 550 /* 551 * Create any static children by calling device identify methods. 552 */ 553 bus_generic_probe(bus); 554 555 /* 556 * Scan the namespace and insert placeholders for all the devices that 557 * we find. 558 * 559 * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 560 * we want to create nodes for all devices, not just those that are currently 561 * present. (This assumes that we don't want to create/remove devices as they 562 * appear, which might be smarter.) 563 */ 564 for (i = 0; scopes[i] != NULL; i++) 565 if ((AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent)) == AE_OK) 566 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL); 567 568 /* 569 * Scan all of the child devices we have created and let them probe/attach. 570 */ 571 bus_generic_attach(bus); 572 573 /* 574 * Some of these children may have attached others as part of their attach 575 * process (eg. the root PCI bus driver), so rescan. 576 */ 577 bus_generic_attach(bus); 578} 579 580/* 581 * Evaluate a child device and determine whether we might attach a device to 582 * it. 583 */ 584static ACPI_STATUS 585acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 586{ 587 ACPI_OBJECT_TYPE type; 588 device_t child, bus = (device_t)context; 589 590 if (AcpiGetType(handle, &type) == AE_OK) { 591 switch(type) { 592 case ACPI_TYPE_DEVICE: 593 case ACPI_TYPE_PROCESSOR: 594 case ACPI_TYPE_THERMAL: 595 case ACPI_TYPE_POWER: 596 /* 597 * Create a placeholder device for this node. Sort the placeholder 598 * so that the probe/attach passes will run breadth-first. 599 */ 600 child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 601 acpi_set_handle(child, handle); 602 } 603 } 604 return(AE_OK); 605} 606 607static void 608acpi_shutdown_pre_sync(void *arg, int howto) 609{ 610 /* 611 * disable all of ACPI events before soft off, otherwise the system 612 * will be turned on again on some laptops. 613 * 614 * XXX this should probably be restricted to masking some events just 615 * before powering down, since we may still need ACPI during the 616 * shutdown process. 617 */ 618 acpi_Disable((struct acpi_softc *)arg); 619} 620 621static void 622acpi_shutdown_final(void *arg, int howto) 623{ 624 ACPI_STATUS status; 625 626 if (howto == RB_POWEROFF) { 627 printf("Power system off using ACPI...\n"); 628 if ((status = AcpiSetSystemSleepState(ACPI_STATE_S5)) != AE_OK) { 629 printf("ACPI power-off failed - %s\n", acpi_strerror(status)); 630 } else { 631 DELAY(1000000); 632 printf("ACPI power-off failed - timeout\n"); 633 } 634 } 635} 636 637/* 638 * Match a HID string against a device 639 */ 640BOOLEAN 641acpi_MatchHid(device_t dev, char *hid) 642{ 643 ACPI_HANDLE h; 644 ACPI_DEVICE_INFO devinfo; 645 ACPI_STATUS error; 646 647 if (hid == NULL) 648 return(FALSE); 649 if ((h = acpi_get_handle(dev)) == NULL) 650 return(FALSE); 651 if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK) 652 return(FALSE); 653 if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId)) 654 return(TRUE); 655 return(FALSE); 656} 657 658/* 659 * Perform the tedious double-get procedure required for fetching something into 660 * an ACPI_BUFFER that has not been initialised. 661 */ 662ACPI_STATUS 663acpi_GetIntoBuffer(ACPI_HANDLE handle, ACPI_STATUS (*func)(ACPI_HANDLE, ACPI_BUFFER *), ACPI_BUFFER *buf) 664{ 665 ACPI_STATUS status; 666 667 buf->Length = 0; 668 buf->Pointer = NULL; 669 670 if ((status = func(handle, buf)) != AE_BUFFER_OVERFLOW) 671 return(status); 672 if ((buf->Pointer = AcpiOsCallocate(buf->Length)) == NULL) 673 return(AE_NO_MEMORY); 674 return(func(handle, buf)); 675} 676 677/* 678 * Allocate a buffer with a preset data size. 679 */ 680ACPI_BUFFER * 681acpi_AllocBuffer(int size) 682{ 683 ACPI_BUFFER *buf; 684 685 if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 686 return(NULL); 687 buf->Length = size; 688 buf->Pointer = (void *)(buf + 1); 689 return(buf); 690} 691 692/* 693 * Set the system sleep state 694 * 695 * Currently we only support S1 and S5 696 */ 697ACPI_STATUS 698acpi_SetSleepState(struct acpi_softc *sc, int state) 699{ 700 ACPI_STATUS status = AE_OK; 701 702 switch (state) { 703 case ACPI_STATE_S0: /* XXX only for testing */ 704 status = AcpiSetSystemSleepState((UINT8)state); 705 if (status != AE_OK) { 706 device_printf(sc->acpi_dev, "AcpiSetSystemSleepState failed - %s\n", acpi_strerror(status)); 707 } 708 break; 709 710 case ACPI_STATE_S1: 711 /* 712 * Inform all devices that we are going to sleep. 713 */ 714 if (DEVICE_SUSPEND(root_bus) != 0) { 715 /* 716 * Re-wake the system. 717 * 718 * XXX note that a better two-pass approach with a 'veto' pass 719 * followed by a "real thing" pass would be better, but the 720 * current bus interface does not provide for this. 721 */ 722 DEVICE_RESUME(root_bus); 723 return(AE_ERROR); 724 } 725 sc->acpi_sstate = state; 726 status = AcpiSetSystemSleepState((UINT8)state); 727 if (status != AE_OK) { 728 device_printf(sc->acpi_dev, "AcpiSetSystemSleepState failed - %s\n", acpi_strerror(status)); 729 } 730 DEVICE_RESUME(root_bus); 731 sc->acpi_sstate = ACPI_STATE_S0; 732 break; 733 734 case ACPI_STATE_S5: 735 /* 736 * Shut down cleanly and power off. This will call us back through the 737 * shutdown handlers. 738 */ 739 shutdown_nice(RB_POWEROFF); 740 break; 741 742 default: 743 status = AE_BAD_PARAMETER; 744 break; 745 } 746 return(status); 747} 748 749/* 750 * Enable/Disable ACPI 751 */ 752ACPI_STATUS 753acpi_Enable(struct acpi_softc *sc) 754{ 755 ACPI_STATUS status; 756 u_int32_t flags; 757 758 flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 759 ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 760 if (!sc->acpi_enabled) { 761 status = AcpiEnableSubsystem(flags); 762 } else { 763 status = AE_OK; 764 } 765 if (status == AE_OK) 766 sc->acpi_enabled = 1; 767 return(status); 768} 769 770ACPI_STATUS 771acpi_Disable(struct acpi_softc *sc) 772{ 773 ACPI_STATUS status; 774 775 if (sc->acpi_enabled) { 776 status = AcpiDisable(); 777 } else { 778 status = AE_OK; 779 } 780 if (status == AE_OK) 781 sc->acpi_enabled = 0; 782 return(status); 783} 784 785/* 786 * Returns true if the device is actually present and should 787 * be attached to. This requires the present, enabled, UI-visible 788 * and diagnostics-passed bits to be set. 789 */ 790BOOLEAN 791acpi_DeviceIsPresent(device_t dev) 792{ 793 ACPI_HANDLE h; 794 ACPI_DEVICE_INFO devinfo; 795 ACPI_STATUS error; 796 797 if ((h = acpi_get_handle(dev)) == NULL) 798 return(FALSE); 799 if ((error = AcpiGetObjectInfo(h, &devinfo)) != AE_OK) 800 return(FALSE); 801 if ((devinfo.Valid & ACPI_VALID_HID) && (devinfo.CurrentStatus & 0xf)) 802 return(TRUE); 803 return(FALSE); 804} 805 806/* 807 * Evaluate a path that should return a number 808 */ 809ACPI_STATUS 810acpi_EvaluateNumber(ACPI_HANDLE handle, char *path, int *number) 811{ 812 ACPI_STATUS error; 813 ACPI_BUFFER buf; 814 int param[4]; 815 816 if (handle == NULL) 817 handle = ACPI_ROOT_OBJECT; 818 buf.Pointer = ¶m[0]; 819 buf.Length = sizeof(param); 820 if ((error = AcpiEvaluateObject(handle, path, NULL, &buf)) == AE_OK) { 821 if (param[0] == ACPI_TYPE_NUMBER) { 822 *number = param[1]; 823 } else { 824 error = AE_TYPE; 825 } 826 } 827 return(error); 828} 829 830/* 831 * ACPI Event Handlers 832 */ 833 834/* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 835 836static void 837acpi_system_eventhandler_sleep(void *arg, int state) 838{ 839 if (state < ACPI_STATE_S0 || state > ACPI_STATE_S5) { 840 return; 841 } 842 843 acpi_SetSleepState((struct acpi_softc *)arg, state); 844} 845 846 847static void 848acpi_system_eventhandler_wakeup(void *arg, int state) 849{ 850 /* Well, what to do? :-) */ 851} 852 853/* 854 * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 855 */ 856UINT32 857acpi_eventhandler_power_button_for_sleep(void *context) 858{ 859 struct acpi_softc *sc = (struct acpi_softc *)context; 860 861 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 862 return(INTERRUPT_HANDLED); 863} 864 865UINT32 866acpi_eventhandler_power_button_for_wakeup(void *context) 867{ 868 struct acpi_softc *sc = (struct acpi_softc *)context; 869 870 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 871 return(INTERRUPT_HANDLED); 872} 873 874UINT32 875acpi_eventhandler_sleep_button_for_sleep(void *context) 876{ 877 struct acpi_softc *sc = (struct acpi_softc *)context; 878 879 EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 880 return(INTERRUPT_HANDLED); 881} 882 883UINT32 884acpi_eventhandler_sleep_button_for_wakeup(void *context) 885{ 886 struct acpi_softc *sc = (struct acpi_softc *)context; 887 888 EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 889 return(INTERRUPT_HANDLED); 890} 891 892/* 893 * XXX This is kinda ugly, and should not be here. 894 */ 895struct acpi_staticbuf { 896 ACPI_BUFFER buffer; 897 char data[512]; 898}; 899 900char * 901acpi_strerror(ACPI_STATUS excep) 902{ 903 static struct acpi_staticbuf buf; 904 905 buf.buffer.Length = 512; 906 buf.buffer.Pointer = &buf.data[0]; 907 908 if (AcpiFormatException(excep, &buf.buffer) == AE_OK) 909 return(buf.buffer.Pointer); 910 return("(error formatting exception)"); 911} 912 913char * 914acpi_name(ACPI_HANDLE handle) 915{ 916 static struct acpi_staticbuf buf; 917 918 buf.buffer.Length = 512; 919 buf.buffer.Pointer = &buf.data[0]; 920 921 if (AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer) == AE_OK) 922 return(buf.buffer.Pointer); 923 return("(unknown path)"); 924} 925 926/* 927 * Debugging/bug-avoidance. Avoid trying to fetch info on various 928 * parts of the namespace. 929 */ 930int 931acpi_avoid(ACPI_HANDLE handle) 932{ 933 char *cp, *np; 934 int len; 935 936 np = acpi_name(handle); 937 if (*np == '\\') 938 np++; 939 if ((cp = getenv("debug.acpi.avoid")) == NULL) 940 return(0); 941 942 /* scan the avoid list checking for a match */ 943 for (;;) { 944 while ((*cp != 0) && isspace(*cp)) 945 cp++; 946 if (*cp == 0) 947 break; 948 len = 0; 949 while ((cp[len] != 0) && !isspace(cp[len])) 950 len++; 951 if (!strncmp(cp, np, len)) { 952 printf("avoiding '%s'\n", np); 953 return(1); 954 } 955 cp += len; 956 } 957 return(0); 958} 959 960/* 961 * Control interface. 962 * 963 * XXX this is provided as a temporary measure for 964 * backwards compatibility for now. A better 965 * interface will probably use sysctl or similar. 966 */ 967static int 968acpiopen(dev_t dev, int flag, int fmt, struct proc * p) 969{ 970 return(0); 971} 972 973static int 974acpiclose(dev_t dev, int flag, int fmt, struct proc * p) 975{ 976 return(0); 977} 978 979static int 980acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc * p) 981{ 982 int error, state; 983 struct acpi_softc *sc; 984 985 error = state = 0; 986 sc = dev->si_drv1; 987 988 switch (cmd) { 989 case ACPIIO_ENABLE: 990 if (ACPI_FAILURE(acpi_Enable(sc))) { 991 error = ENXIO; 992 } 993 break; 994 995 case ACPIIO_DISABLE: 996 if (ACPI_FAILURE(acpi_Disable(sc))) { 997 error = ENXIO; 998 } 999 break; 1000 1001 case ACPIIO_SETSLPSTATE: 1002 if (!sc->acpi_enabled) { 1003 error = ENXIO; 1004 break; 1005 } 1006 state = *(int *)addr; 1007 if (state >= ACPI_STATE_S0 && state <= ACPI_STATE_S5) { 1008 acpi_SetSleepState(sc, state); 1009 } else { 1010 error = EINVAL; 1011 } 1012 1013 break; 1014 1015 default: 1016 error = EINVAL; 1017 break; 1018 } 1019 1020 return(error); 1021} 1022 1023#ifdef ACPI_DEBUG 1024struct debugtag 1025{ 1026 char *name; 1027 UINT32 value; 1028}; 1029 1030static struct debugtag dbg_layer[] = { 1031 {"GLOBAL", 0x00000001}, 1032 {"COMMON", 0x00000002}, 1033 {"PARSER", 0x00000004}, 1034 {"DISPATCHER", 0x00000008}, 1035 {"INTERPRETER", 0x00000010}, 1036 {"NAMESPACE", 0x00000020}, 1037 {"RESOURCE_MANAGER", 0x00000040}, 1038 {"TABLE_MANAGER", 0x00000080}, 1039 {"EVENT_HANDLING", 0x00000100}, 1040 {"HARDWARE", 0x00000200}, 1041 {"MISCELLANEOUS", 0x00000400}, 1042 {"OS_DEPENDENT", 0x00000800}, 1043 {"BUS_MANAGER", 0x00001000}, 1044 {"PROCESSOR_CONTROL", 0x00002000}, 1045 {"SYSTEM_CONTROL", 0x00004000}, 1046 {"THERMAL_CONTROL", 0x00008000}, 1047 {"POWER_CONTROL", 0x00010000}, 1048 {"EMBEDDED_CONTROLLER", 0x00020000}, 1049 {"BATTERY", 0x00040000}, 1050 {"DEBUGGER", 0x00100000}, 1051 {"ALL_COMPONENTS", 0x001FFFFF}, 1052 {NULL, 0} 1053}; 1054 1055static struct debugtag dbg_level[] = { 1056 {"ACPI_OK", 0x00000001}, 1057 {"ACPI_INFO", 0x00000002}, 1058 {"ACPI_WARN", 0x00000004}, 1059 {"ACPI_ERROR", 0x00000008}, 1060 {"ACPI_FATAL", 0x00000010}, 1061 {"ACPI_DEBUG_OBJECT", 0x00000020}, 1062 {"ACPI_ALL", 0x0000003F}, 1063 {"TRACE_PARSE", 0x00000100}, 1064 {"TRACE_DISPATCH", 0x00000200}, 1065 {"TRACE_LOAD", 0x00000400}, 1066 {"TRACE_EXEC", 0x00000800}, 1067 {"TRACE_NAMES", 0x00001000}, 1068 {"TRACE_OPREGION", 0x00002000}, 1069 {"TRACE_BFIELD", 0x00004000}, 1070 {"TRACE_TRASH", 0x00008000}, 1071 {"TRACE_TABLES", 0x00010000}, 1072 {"TRACE_FUNCTIONS", 0x00020000}, 1073 {"TRACE_VALUES", 0x00040000}, 1074 {"TRACE_OBJECTS", 0x00080000}, 1075 {"TRACE_ALLOCATIONS", 0x00100000}, 1076 {"TRACE_RESOURCES", 0x00200000}, 1077 {"TRACE_IO", 0x00400000}, 1078 {"TRACE_INTERRUPTS", 0x00800000}, 1079 {"TRACE_USER_REQUESTS", 0x01000000}, 1080 {"TRACE_PACKAGE", 0x02000000}, 1081 {"TRACE_MUTEX", 0x04000000}, 1082 {"TRACE_ALL", 0x0FFFFF00}, 1083 {"VERBOSE_AML_DISASSEMBLE", 0x10000000}, 1084 {"VERBOSE_INFO", 0x20000000}, 1085 {"VERBOSE_TABLES", 0x40000000}, 1086 {"VERBOSE_EVENTS", 0x80000000}, 1087 {"VERBOSE_ALL", 0xF0000000}, 1088 {NULL, 0} 1089}; 1090 1091static void 1092acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 1093{ 1094 char *ep; 1095 int i, l; 1096 1097 while (*cp) { 1098 if (isspace(*cp)) { 1099 cp++; 1100 continue; 1101 } 1102 ep = cp; 1103 while (*ep && !isspace(*ep)) 1104 ep++; 1105 l = ep - cp; 1106 for (i = 0; tag[i].name != NULL; i++) { 1107 if (!strncmp(cp, tag[i].name, l)) { 1108 *flag |= tag[i].value; 1109 printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 1110 } 1111 } 1112 cp = ep; 1113 } 1114} 1115 1116static void 1117acpi_set_debugging(void) 1118{ 1119 char *cp; 1120 1121 if ((cp = getenv("debug.acpi.layer")) != NULL) 1122 acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 1123 if ((cp = getenv("debug.acpi.level")) != NULL) 1124 acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 1125} 1126#endif 1127