1 /*- 2 * Copyright (c) 2003-2007 Nate Lawson 3 * Copyright (c) 2000 Michael Smith 4 * Copyright (c) 2000 BSDi 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 * $FreeBSD: head/sys/dev/acpica/acpi_ec.c 246128 2013-01-30 18:01:20Z sbz $ 29 */ 30 31 #include "opt_acpi.h" 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/lock.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/rman.h> 39 #include <sys/ktr.h> 40 41 #include "acpi.h" 42 #include "accommon.h" 43 44 #include <dev/acpica/acpivar.h> 45 46 /* Hooks for the ACPICA debugging infrastructure */ 47 #define _COMPONENT ACPI_EC 48 ACPI_MODULE_NAME("EC") 49 50 #define rebooting 0 51 52 /* 53 * EC_COMMAND: 54 * ----------- 55 */ 56 typedef UINT8 EC_COMMAND; 57 58 #define EC_COMMAND_UNKNOWN ((EC_COMMAND) 0x00) 59 #define EC_COMMAND_READ ((EC_COMMAND) 0x80) 60 #define EC_COMMAND_WRITE ((EC_COMMAND) 0x81) 61 #define EC_COMMAND_BURST_ENABLE ((EC_COMMAND) 0x82) 62 #define EC_COMMAND_BURST_DISABLE ((EC_COMMAND) 0x83) 63 #define EC_COMMAND_QUERY ((EC_COMMAND) 0x84) 64 65 /* 66 * EC_STATUS: 67 * ---------- 68 * The encoding of the EC status register is illustrated below. 69 * Note that a set bit (1) indicates the property is TRUE 70 * (e.g. if bit 0 is set then the output buffer is full). 71 * +-+-+-+-+-+-+-+-+ 72 * |7|6|5|4|3|2|1|0| 73 * +-+-+-+-+-+-+-+-+ 74 * | | | | | | | | 75 * | | | | | | | +- Output Buffer Full? 76 * | | | | | | +--- Input Buffer Full? 77 * | | | | | +----- <reserved> 78 * | | | | +------- Data Register is Command Byte? 79 * | | | +--------- Burst Mode Enabled? 80 * | | +----------- SCI Event? 81 * | +------------- SMI Event? 82 * +--------------- <reserved> 83 * 84 */ 85 typedef UINT8 EC_STATUS; 86 87 #define EC_FLAG_OUTPUT_BUFFER ((EC_STATUS) 0x01) 88 #define EC_FLAG_INPUT_BUFFER ((EC_STATUS) 0x02) 89 #define EC_FLAG_DATA_IS_CMD ((EC_STATUS) 0x08) 90 #define EC_FLAG_BURST_MODE ((EC_STATUS) 0x10) 91 92 /* 93 * EC_EVENT: 94 * --------- 95 */ 96 typedef UINT8 EC_EVENT; 97 98 #define EC_EVENT_UNKNOWN ((EC_EVENT) 0x00) 99 #define EC_EVENT_OUTPUT_BUFFER_FULL ((EC_EVENT) 0x01) 100 #define EC_EVENT_INPUT_BUFFER_EMPTY ((EC_EVENT) 0x02) 101 #define EC_EVENT_SCI ((EC_EVENT) 0x20) 102 #define EC_EVENT_SMI ((EC_EVENT) 0x40) 103 104 /* Data byte returned after burst enable indicating it was successful. */ 105 #define EC_BURST_ACK 0x90 106 107 /* 108 * Register access primitives 109 */ 110 #define EC_GET_DATA(sc) \ 111 bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0) 112 113 #define EC_SET_DATA(sc, v) \ 114 bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v)) 115 116 #define EC_GET_CSR(sc) \ 117 bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0) 118 119 #define EC_SET_CSR(sc, v) \ 120 bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v)) 121 122 /* Additional params to pass from the probe routine */ 123 struct acpi_ec_params { 124 int glk; 125 int gpe_bit; 126 ACPI_HANDLE gpe_handle; 127 int uid; 128 }; 129 130 /* 131 * Driver softc. 132 */ 133 struct acpi_ec_softc { 134 device_t ec_dev; 135 ACPI_HANDLE ec_handle; 136 int ec_uid; 137 ACPI_HANDLE ec_gpehandle; 138 UINT8 ec_gpebit; 139 140 int ec_data_rid; 141 struct resource *ec_data_res; 142 bus_space_tag_t ec_data_tag; 143 bus_space_handle_t ec_data_handle; 144 145 int ec_csr_rid; 146 struct resource *ec_csr_res; 147 bus_space_tag_t ec_csr_tag; 148 bus_space_handle_t ec_csr_handle; 149 150 int ec_glk; 151 int ec_glkhandle; 152 int ec_burstactive; 153 int ec_sci_pend; 154 volatile u_int ec_gencount; 155 int ec_suspending; 156 }; 157 158 /* 159 * XXX njl 160 * I couldn't find it in the spec but other implementations also use a 161 * value of 1 ms for the time to acquire global lock. 162 */ 163 #define EC_LOCK_TIMEOUT 1000 164 165 /* Default delay in microseconds between each run of the status polling loop. */ 166 #define EC_POLL_DELAY 50 167 168 /* Total time in ms spent waiting for a response from EC. */ 169 #define EC_TIMEOUT 750 170 #define EC_TIMEOUT_BACKOFF 100 171 172 #define EVENT_READY(event, status) \ 173 (((event) == EC_EVENT_OUTPUT_BUFFER_FULL && \ 174 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) || \ 175 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && \ 176 ((status) & EC_FLAG_INPUT_BUFFER) == 0)) 177 178 ACPI_SERIAL_DECL(ec, "ACPI embedded controller"); 179 180 extern int acpi_silence_all; 181 182 static SYSCTL_NODE(_debug_acpi, OID_AUTO, ec, CTLFLAG_RD, NULL, "EC debugging"); 183 184 static int ec_burst_mode; 185 TUNABLE_INT("debug.acpi.ec.burst", &ec_burst_mode); 186 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RW, &ec_burst_mode, 0, 187 "Enable use of burst mode (faster for nearly all systems)"); 188 static int ec_polled_mode; 189 TUNABLE_INT("debug.acpi.ec.polled", &ec_polled_mode); 190 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, polled, CTLFLAG_RW, &ec_polled_mode, 0, 191 "Force use of polled mode (only if interrupt mode doesn't work)"); 192 static int ec_timeout = EC_TIMEOUT; 193 TUNABLE_INT("debug.acpi.ec.timeout", &ec_timeout); 194 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RW, &ec_timeout, 195 EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)"); 196 static int ec_timeout_backoff = EC_TIMEOUT_BACKOFF; 197 TUNABLE_INT("debug.acpi.ec.timeout_backoff", &ec_timeout); 198 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout_backoff, CTLFLAG_RW, 199 &ec_timeout_backoff, EC_TIMEOUT_BACKOFF, 200 "Total time spent waiting for a response (poll+sleep)"); 201 static int ec_auto_silence = 10; /* silence after 10 errors */ 202 TUNABLE_INT("debug.acpi.ec.auto_silence", &ec_auto_silence); 203 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, auto_silence, CTLFLAG_RW, 204 &ec_auto_silence, 1, 205 "Total time spent waiting for a response (poll+sleep)"); 206 207 #ifndef KTR_ACPI_EC 208 #define KTR_ACPI_EC KTR_ALL 209 #endif 210 211 KTR_INFO_MASTER(acpi_ec); 212 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstdis, 0, 213 "ec burst disabled in waitevent (%s)", const char *msg); 214 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstdisok, 1, 215 "ec disabled burst ok"); 216 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstenl, 2, 217 "ec burst enabled"); 218 KTR_INFO(KTR_ACPI_EC, acpi_ec, cmdrun, 3, 219 "ec running command %#hhx", EC_COMMAND cmd); 220 KTR_INFO(KTR_ACPI_EC, acpi_ec, gpehdlstart, 4, 221 "ec gpe handler start"); 222 KTR_INFO(KTR_ACPI_EC, acpi_ec, gpequeuehdl, 5, 223 "ec gpe queueing query handler"); 224 KTR_INFO(KTR_ACPI_EC, acpi_ec, gperun, 6, 225 "ec running gpe handler directly"); 226 KTR_INFO(KTR_ACPI_EC, acpi_ec, qryoknotrun, 7, 227 "ec query ok, not running _Q%02hhX", uint8_t Data); 228 KTR_INFO(KTR_ACPI_EC, acpi_ec, qryokrun, 8, 229 "ec query ok, running _Q%02hhX", uint8_t Data); 230 KTR_INFO(KTR_ACPI_EC, acpi_ec, readaddr, 9, 231 "ec read from %#hhx", UINT8 Address); 232 KTR_INFO(KTR_ACPI_EC, acpi_ec, timeout, 10, 233 "error: ec wait timed out"); 234 KTR_INFO(KTR_ACPI_EC, acpi_ec, waitrdy, 11, 235 "ec %s wait ready, status %#hhx", const char *msg, EC_STATUS ec_status); 236 KTR_INFO(KTR_ACPI_EC, acpi_ec, writeaddr, 12, 237 "ec write to %#hhx, data %#hhx", UINT8 Address, UINT8 Data); 238 239 static ACPI_STATUS 240 EcLock(struct acpi_ec_softc *sc) 241 { 242 ACPI_STATUS status; 243 244 /* If _GLK is non-zero, acquire the global lock. */ 245 status = AE_OK; 246 if (sc->ec_glk) { 247 status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle); 248 if (ACPI_FAILURE(status)) 249 return (status); 250 } 251 ACPI_SERIAL_BEGIN(ec); 252 return (status); 253 } 254 255 static void 256 EcUnlock(struct acpi_ec_softc *sc) 257 { 258 ACPI_SERIAL_END(ec); 259 if (sc->ec_glk) 260 AcpiReleaseGlobalLock(sc->ec_glkhandle); 261 } 262 263 static UINT32 EcGpeHandler(ACPI_HANDLE GpeDevice, 264 UINT32 GpeNumber, void *Context); 265 static ACPI_STATUS EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, 266 void *Context, void **return_Context); 267 static ACPI_STATUS EcSpaceHandler(UINT32 Function, 268 ACPI_PHYSICAL_ADDRESS Address, 269 UINT32 Width, UINT64 *Value, 270 void *Context, void *RegionContext); 271 static ACPI_STATUS EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, 272 u_int gen_count); 273 static ACPI_STATUS EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd); 274 static ACPI_STATUS EcRead(struct acpi_ec_softc *sc, UINT8 Address, 275 UINT8 *Data); 276 static ACPI_STATUS EcWrite(struct acpi_ec_softc *sc, UINT8 Address, 277 UINT8 Data); 278 static int acpi_ec_probe(device_t dev); 279 static int acpi_ec_attach(device_t dev); 280 static int acpi_ec_suspend(device_t dev); 281 static int acpi_ec_resume(device_t dev); 282 static int acpi_ec_shutdown(device_t dev); 283 static int acpi_ec_read_method(device_t dev, u_int addr, 284 UINT64 *val, int width); 285 static int acpi_ec_write_method(device_t dev, u_int addr, 286 UINT64 val, int width); 287 288 static device_method_t acpi_ec_methods[] = { 289 /* Device interface */ 290 DEVMETHOD(device_probe, acpi_ec_probe), 291 DEVMETHOD(device_attach, acpi_ec_attach), 292 DEVMETHOD(device_suspend, acpi_ec_suspend), 293 DEVMETHOD(device_resume, acpi_ec_resume), 294 DEVMETHOD(device_shutdown, acpi_ec_shutdown), 295 296 /* Embedded controller interface */ 297 DEVMETHOD(acpi_ec_read, acpi_ec_read_method), 298 DEVMETHOD(acpi_ec_write, acpi_ec_write_method), 299 300 DEVMETHOD_END 301 }; 302 303 static driver_t acpi_ec_driver = { 304 "acpi_ec", 305 acpi_ec_methods, 306 sizeof(struct acpi_ec_softc), 307 .gpri = KOBJ_GPRI_ACPI 308 }; 309 310 static devclass_t acpi_ec_devclass; 311 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, NULL, NULL); 312 MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1); 313 314 /* 315 * Look for an ECDT and if we find one, set up default GPE and 316 * space handlers to catch attempts to access EC space before 317 * we have a real driver instance in place. 318 * 319 * TODO: Some old Gateway laptops need us to fake up an ECDT or 320 * otherwise attach early so that _REG methods can run. 321 */ 322 void 323 acpi_ec_ecdt_probe(device_t parent) 324 { 325 ACPI_TABLE_ECDT *ecdt; 326 ACPI_STATUS status; 327 device_t child; 328 ACPI_HANDLE h; 329 struct acpi_ec_params *params; 330 331 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 332 333 /* Find and validate the ECDT. */ 334 status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt); 335 if (ACPI_FAILURE(status) || 336 ecdt->Control.BitWidth != 8 || 337 ecdt->Data.BitWidth != 8) { 338 return; 339 } 340 341 /* Create the child device with the given unit number. */ 342 child = BUS_ADD_CHILD(parent, parent, 0, "acpi_ec", ecdt->Uid); 343 if (child == NULL) { 344 kprintf("%s: can't add child\n", __func__); 345 return; 346 } 347 348 /* Find and save the ACPI handle for this device. */ 349 status = AcpiGetHandle(NULL, ecdt->Id, &h); 350 if (ACPI_FAILURE(status)) { 351 device_delete_child(parent, child); 352 kprintf("%s: can't get handle\n", __func__); 353 return; 354 } 355 acpi_set_handle(child, h); 356 357 /* Set the data and CSR register addresses. */ 358 bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address, 359 /*count*/1, -1); 360 bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address, 361 /*count*/1, -1); 362 363 /* 364 * Store values for the probe/attach routines to use. Store the 365 * ECDT GPE bit and set the global lock flag according to _GLK. 366 * Note that it is not perfectly correct to be evaluating a method 367 * before initializing devices, but in practice this function 368 * should be safe to call at this point. 369 */ 370 params = kmalloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO); 371 params->gpe_handle = NULL; 372 params->gpe_bit = ecdt->Gpe; 373 params->uid = ecdt->Uid; 374 acpi_GetInteger(h, "_GLK", ¶ms->glk); 375 acpi_set_private(child, params); 376 377 /* Finish the attach process. */ 378 if (device_probe_and_attach(child) != 0) 379 device_delete_child(parent, child); 380 } 381 382 static int 383 acpi_ec_probe(device_t dev) 384 { 385 ACPI_BUFFER buf; 386 ACPI_HANDLE h; 387 ACPI_OBJECT *obj; 388 ACPI_STATUS status; 389 device_t peer; 390 char desc[64]; 391 int ecdt; 392 int ret; 393 struct acpi_ec_params *params; 394 static char *ec_ids[] = { "PNP0C09", NULL }; 395 396 /* Check that this is a device and that EC is not disabled. */ 397 if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec")) 398 return (ENXIO); 399 400 /* 401 * If probed via ECDT, set description and continue. Otherwise, 402 * we can access the namespace and make sure this is not a 403 * duplicate probe. 404 */ 405 ret = ENXIO; 406 ecdt = 0; 407 buf.Pointer = NULL; 408 buf.Length = ACPI_ALLOCATE_BUFFER; 409 params = acpi_get_private(dev); 410 if (params != NULL) { 411 ecdt = 1; 412 ret = 0; 413 } else if (ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) { 414 params = kmalloc(sizeof(struct acpi_ec_params), M_TEMP, 415 M_WAITOK | M_ZERO); 416 h = acpi_get_handle(dev); 417 418 /* 419 * Read the unit ID to check for duplicate attach and the 420 * global lock value to see if we should acquire it when 421 * accessing the EC. 422 */ 423 status = acpi_GetInteger(h, "_UID", ¶ms->uid); 424 if (ACPI_FAILURE(status)) 425 params->uid = 0; 426 status = acpi_GetInteger(h, "_GLK", ¶ms->glk); 427 if (ACPI_FAILURE(status)) 428 params->glk = 0; 429 430 /* 431 * Evaluate the _GPE method to find the GPE bit used by the EC to 432 * signal status (SCI). If it's a package, it contains a reference 433 * and GPE bit, similar to _PRW. 434 */ 435 status = AcpiEvaluateObject(h, "_GPE", NULL, &buf); 436 if (ACPI_FAILURE(status)) { 437 device_printf(dev, "can't evaluate _GPE - %s\n", 438 AcpiFormatException(status)); 439 goto out; 440 } 441 obj = (ACPI_OBJECT *)buf.Pointer; 442 if (obj == NULL) 443 goto out; 444 445 switch (obj->Type) { 446 case ACPI_TYPE_INTEGER: 447 params->gpe_handle = NULL; 448 params->gpe_bit = obj->Integer.Value; 449 break; 450 case ACPI_TYPE_PACKAGE: 451 if (!ACPI_PKG_VALID(obj, 2)) 452 goto out; 453 params->gpe_handle = 454 acpi_GetReference(NULL, &obj->Package.Elements[0]); 455 if (params->gpe_handle == NULL || 456 acpi_PkgInt32(obj, 1, ¶ms->gpe_bit) != 0) 457 goto out; 458 break; 459 default: 460 device_printf(dev, "_GPE has invalid type %d\n", obj->Type); 461 goto out; 462 } 463 464 /* Store the values we got from the namespace for attach. */ 465 acpi_set_private(dev, params); 466 467 /* 468 * Check for a duplicate probe. This can happen when a probe 469 * via ECDT succeeded already. If this is a duplicate, disable 470 * this device. 471 */ 472 peer = devclass_get_device(acpi_ec_devclass, params->uid); 473 if (peer == NULL || !device_is_alive(peer)) 474 ret = 0; 475 else 476 device_disable(dev); 477 } 478 479 out: 480 if (ret == 0) { 481 ksnprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s", 482 params->gpe_bit, (params->glk) ? ", GLK" : "", 483 ecdt ? ", ECDT" : ""); 484 device_set_desc_copy(dev, desc); 485 } 486 487 if (ret > 0 && params) 488 kfree(params, M_TEMP); 489 if (buf.Pointer) 490 AcpiOsFree(buf.Pointer); 491 return (ret); 492 } 493 494 static int 495 acpi_ec_attach(device_t dev) 496 { 497 struct acpi_ec_softc *sc; 498 struct acpi_ec_params *params; 499 ACPI_STATUS Status; 500 501 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 502 503 /* Fetch/initialize softc (assumes softc is pre-zeroed). */ 504 sc = device_get_softc(dev); 505 params = acpi_get_private(dev); 506 sc->ec_dev = dev; 507 sc->ec_handle = acpi_get_handle(dev); 508 ACPI_SERIAL_INIT(ec); 509 510 /* Retrieve previously probed values via device ivars. */ 511 sc->ec_glk = params->glk; 512 sc->ec_gpebit = params->gpe_bit; 513 sc->ec_gpehandle = params->gpe_handle; 514 sc->ec_uid = params->uid; 515 sc->ec_suspending = FALSE; 516 acpi_set_private(dev, NULL); 517 kfree(params, M_TEMP); 518 519 /* Attach bus resources for data and command/status ports. */ 520 sc->ec_data_rid = 0; 521 sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT, 522 &sc->ec_data_rid, RF_ACTIVE); 523 if (sc->ec_data_res == NULL) { 524 device_printf(dev, "can't allocate data port\n"); 525 goto error; 526 } 527 sc->ec_data_tag = rman_get_bustag(sc->ec_data_res); 528 sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res); 529 530 sc->ec_csr_rid = 1; 531 sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT, 532 &sc->ec_csr_rid, RF_ACTIVE); 533 if (sc->ec_csr_res == NULL) { 534 device_printf(dev, "can't allocate command/status port\n"); 535 goto error; 536 } 537 sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res); 538 sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res); 539 540 /* 541 * Install a handler for this EC's GPE bit. We want edge-triggered 542 * behavior. 543 */ 544 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n")); 545 Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, 546 ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, sc); 547 if (ACPI_FAILURE(Status)) { 548 device_printf(dev, "can't install GPE handler for %s - %s\n", 549 acpi_name(sc->ec_handle), AcpiFormatException(Status)); 550 goto error; 551 } 552 553 /* 554 * Install address space handler 555 */ 556 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n")); 557 Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC, 558 &EcSpaceHandler, &EcSpaceSetup, sc); 559 if (ACPI_FAILURE(Status)) { 560 device_printf(dev, "can't install address space handler for %s - %s\n", 561 acpi_name(sc->ec_handle), AcpiFormatException(Status)); 562 goto error; 563 } 564 565 /* Enable runtime GPEs for the handler. */ 566 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit); 567 if (ACPI_FAILURE(Status)) { 568 device_printf(dev, "AcpiEnableGpe failed: %s\n", 569 AcpiFormatException(Status)); 570 goto error; 571 } 572 573 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n")); 574 return (0); 575 576 error: 577 AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, EcGpeHandler); 578 AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC, 579 EcSpaceHandler); 580 if (sc->ec_csr_res) 581 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid, 582 sc->ec_csr_res); 583 if (sc->ec_data_res) 584 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid, 585 sc->ec_data_res); 586 return (ENXIO); 587 } 588 589 static int 590 acpi_ec_suspend(device_t dev) 591 { 592 struct acpi_ec_softc *sc; 593 594 sc = device_get_softc(dev); 595 sc->ec_suspending = TRUE; 596 return (0); 597 } 598 599 static int 600 acpi_ec_resume(device_t dev) 601 { 602 struct acpi_ec_softc *sc; 603 604 sc = device_get_softc(dev); 605 sc->ec_suspending = FALSE; 606 return (0); 607 } 608 609 static int 610 acpi_ec_shutdown(device_t dev) 611 { 612 struct acpi_ec_softc *sc; 613 614 /* Disable the GPE so we don't get EC events during shutdown. */ 615 sc = device_get_softc(dev); 616 AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit); 617 return (0); 618 } 619 620 /* Methods to allow other devices (e.g., smbat) to read/write EC space. */ 621 static int 622 acpi_ec_read_method(device_t dev, u_int addr, UINT64 *val, int width) 623 { 624 struct acpi_ec_softc *sc; 625 ACPI_STATUS status; 626 627 sc = device_get_softc(dev); 628 status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL); 629 if (ACPI_FAILURE(status)) 630 return (ENXIO); 631 return (0); 632 } 633 634 static int 635 acpi_ec_write_method(device_t dev, u_int addr, UINT64 val, int width) 636 { 637 struct acpi_ec_softc *sc; 638 ACPI_STATUS status; 639 640 sc = device_get_softc(dev); 641 status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL); 642 if (ACPI_FAILURE(status)) 643 return (ENXIO); 644 return (0); 645 } 646 647 static ACPI_STATUS 648 EcCheckStatus(struct acpi_ec_softc *sc, const char *msg, EC_EVENT event) 649 { 650 ACPI_STATUS status; 651 EC_STATUS ec_status; 652 653 status = AE_NO_HARDWARE_RESPONSE; 654 ec_status = EC_GET_CSR(sc); 655 if (sc->ec_burstactive && !(ec_status & EC_FLAG_BURST_MODE)) { 656 KTR_LOG(acpi_ec_burstdis, msg); 657 sc->ec_burstactive = FALSE; 658 } 659 if (EVENT_READY(event, ec_status)) { 660 KTR_LOG(acpi_ec_waitrdy, msg, ec_status); 661 status = AE_OK; 662 } 663 return (status); 664 } 665 666 static void 667 EcGpeQueryHandler(void *Context) 668 { 669 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context; 670 UINT8 Data; 671 ACPI_STATUS Status; 672 int retry, sci_enqueued; 673 char qxx[5]; 674 675 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 676 KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL")); 677 678 /* Serialize user access with EcSpaceHandler(). */ 679 Status = EcLock(sc); 680 if (ACPI_FAILURE(Status)) { 681 device_printf(sc->ec_dev, "GpeQuery lock error: %s\n", 682 AcpiFormatException(Status)); 683 return; 684 } 685 686 /* 687 * Send a query command to the EC to find out which _Qxx call it 688 * wants to make. This command clears the SCI bit and also the 689 * interrupt source since we are edge-triggered. To prevent the GPE 690 * that may arise from running the query from causing another query 691 * to be queued, we clear the pending flag only after running it. 692 */ 693 sci_enqueued = sc->ec_sci_pend; 694 for (retry = 0; retry < 2; retry++) { 695 Status = EcCommand(sc, EC_COMMAND_QUERY); 696 if (ACPI_SUCCESS(Status)) 697 break; 698 if (ACPI_SUCCESS(EcCheckStatus(sc, "retr_check", 699 EC_EVENT_INPUT_BUFFER_EMPTY))) 700 continue; 701 else 702 break; 703 } 704 sc->ec_sci_pend = FALSE; 705 if (ACPI_FAILURE(Status)) { 706 EcUnlock(sc); 707 device_printf(sc->ec_dev, "GPE query failed: %s\n", 708 AcpiFormatException(Status)); 709 return; 710 } 711 Data = EC_GET_DATA(sc); 712 713 /* 714 * We have to unlock before running the _Qxx method below since that 715 * method may attempt to read/write from EC address space, causing 716 * recursive acquisition of the lock. 717 */ 718 EcUnlock(sc); 719 720 /* Ignore the value for "no outstanding event". (13.3.5) */ 721 if (Data == 0) { 722 KTR_LOG(acpi_ec_qryoknotrun, Data); 723 return; 724 } else { 725 KTR_LOG(acpi_ec_qryokrun, Data); 726 } 727 728 /* Evaluate _Qxx to respond to the controller. */ 729 ksnprintf(qxx, sizeof(qxx), "_Q%02X", Data); 730 AcpiUtStrupr(qxx); 731 Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL); 732 if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) { 733 device_printf(sc->ec_dev, "evaluation of query method %s failed: %s\n", 734 qxx, AcpiFormatException(Status)); 735 } 736 737 /* Reenable runtime GPE if its execution was deferred. */ 738 if (sci_enqueued) { 739 Status = AcpiFinishGpe(sc->ec_gpehandle, sc->ec_gpebit); 740 if (ACPI_FAILURE(Status)) 741 device_printf(sc->ec_dev, "reenabling runtime GPE failed: %s\n", 742 AcpiFormatException(Status)); 743 } 744 } 745 746 /* 747 * The GPE handler is called when IBE/OBF or SCI events occur. We are 748 * called from an unknown lock context. 749 */ 750 static UINT32 751 EcGpeHandler(ACPI_HANDLE GpeDevice, UINT32 GpeNumber, void *Context) 752 { 753 struct acpi_ec_softc *sc = Context; 754 ACPI_STATUS Status; 755 EC_STATUS EcStatus; 756 757 KASSERT(Context != NULL, ("EcGpeHandler called with NULL")); 758 KTR_LOG(acpi_ec_gpehdlstart); 759 /* 760 * Notify EcWaitEvent() that the status register is now fresh. If we 761 * didn't do this, it wouldn't be possible to distinguish an old IBE 762 * from a new one, for example when doing a write transaction (writing 763 * address and then data values.) 764 */ 765 atomic_add_int(&sc->ec_gencount, 1); 766 wakeup(sc); 767 768 /* 769 * If the EC_SCI bit of the status register is set, queue a query handler. 770 * It will run the query and _Qxx method later, under the lock. 771 */ 772 EcStatus = EC_GET_CSR(sc); 773 if ((EcStatus & EC_EVENT_SCI) && !sc->ec_sci_pend) { 774 KTR_LOG(acpi_ec_gpequeuehdl); 775 Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler, Context); 776 if (ACPI_SUCCESS(Status)) { 777 sc->ec_sci_pend = TRUE; 778 return (0); 779 } else { 780 kprintf("EcGpeHandler: queuing GPE query handler failed\n"); 781 } 782 } 783 return (ACPI_REENABLE_GPE); 784 } 785 786 static ACPI_STATUS 787 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context, 788 void **RegionContext) 789 { 790 791 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 792 793 /* 794 * If deactivating a region, always set the output to NULL. Otherwise, 795 * just pass the context through. 796 */ 797 if (Function == ACPI_REGION_DEACTIVATE) 798 *RegionContext = NULL; 799 else 800 *RegionContext = Context; 801 802 return_ACPI_STATUS (AE_OK); 803 } 804 805 static ACPI_STATUS 806 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 Width, 807 UINT64 *Value, void *Context, void *RegionContext) 808 { 809 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context; 810 ACPI_PHYSICAL_ADDRESS EcAddr; 811 UINT8 *EcData; 812 ACPI_STATUS Status; 813 814 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address); 815 816 if (Function != ACPI_READ && Function != ACPI_WRITE) 817 return_ACPI_STATUS (AE_BAD_PARAMETER); 818 if (Width % 8 != 0 || Value == NULL || Context == NULL) 819 return_ACPI_STATUS (AE_BAD_PARAMETER); 820 if (Address + Width / 8 > 256) 821 return_ACPI_STATUS (AE_BAD_ADDRESS); 822 823 /* 824 * If booting, check if we need to run the query handler. If so, we 825 * we call it directly here since our thread taskq is not active yet. 826 */ 827 if (cold || rebooting || sc->ec_suspending) { 828 if ((EC_GET_CSR(sc) & EC_EVENT_SCI)) { 829 KTR_LOG(acpi_ec_gperun); 830 EcGpeQueryHandler(sc); 831 } 832 } 833 834 /* Serialize with EcGpeQueryHandler() at transaction granularity. */ 835 Status = EcLock(sc); 836 if (ACPI_FAILURE(Status)) 837 return_ACPI_STATUS (Status); 838 839 /* If we can't start burst mode, continue anyway. */ 840 Status = EcCommand(sc, EC_COMMAND_BURST_ENABLE); 841 if (ACPI_SUCCESS(Status)) { 842 if (EC_GET_DATA(sc) == EC_BURST_ACK) { 843 KTR_LOG(acpi_ec_burstenl); 844 sc->ec_burstactive = TRUE; 845 } 846 } 847 848 /* Perform the transaction(s), based on Width. */ 849 EcAddr = Address; 850 EcData = (UINT8 *)Value; 851 if (Function == ACPI_READ) 852 *Value = 0; 853 do { 854 switch (Function) { 855 case ACPI_READ: 856 Status = EcRead(sc, EcAddr, EcData); 857 break; 858 case ACPI_WRITE: 859 Status = EcWrite(sc, EcAddr, *EcData); 860 break; 861 } 862 if (ACPI_FAILURE(Status)) 863 break; 864 EcAddr++; 865 EcData++; 866 } while (EcAddr < Address + Width / 8); 867 868 if (sc->ec_burstactive) { 869 sc->ec_burstactive = FALSE; 870 if (ACPI_SUCCESS(EcCommand(sc, EC_COMMAND_BURST_DISABLE))) 871 KTR_LOG(acpi_ec_burstdisok); 872 } 873 874 EcUnlock(sc); 875 return_ACPI_STATUS (Status); 876 } 877 878 static ACPI_STATUS 879 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, u_int gen_count) 880 { 881 static int no_intr = 0; 882 ACPI_STATUS Status; 883 int count, i, need_poll, slp_ival; 884 885 ACPI_SERIAL_ASSERT(ec); 886 Status = AE_NO_HARDWARE_RESPONSE; 887 need_poll = cold || rebooting || ec_polled_mode || sc->ec_suspending; 888 889 /* Wait for event by polling or GPE (interrupt). */ 890 if (need_poll) { 891 count = (ec_timeout * 1000) / EC_POLL_DELAY; 892 if (count == 0) 893 count = 1; 894 DELAY(10); 895 for (i = 0; i < count; i++) { 896 Status = EcCheckStatus(sc, "poll", Event); 897 if (ACPI_SUCCESS(Status)) 898 break; 899 DELAY(EC_POLL_DELAY); 900 } 901 } else { 902 slp_ival = hz / 1000; 903 if (slp_ival != 0) { 904 count = ec_timeout; 905 } else { 906 /* hz has less than 1 ms resolution so scale timeout. */ 907 slp_ival = 1; 908 count = ec_timeout / (1000 / hz); 909 } 910 911 /* 912 * Wait for the GPE to signal the status changed, checking the 913 * status register each time we get one. It's possible to get a 914 * GPE for an event we're not interested in here (i.e., SCI for 915 * EC query). 916 */ 917 for (i = 0; i < count; i++) { 918 if (gen_count == sc->ec_gencount) 919 tsleep(sc, 0, "ecgpe", slp_ival); 920 /* 921 * Record new generation count. It's possible the GPE was 922 * just to notify us that a query is needed and we need to 923 * wait for a second GPE to signal the completion of the 924 * event we are actually waiting for. 925 */ 926 Status = EcCheckStatus(sc, "sleep", Event); 927 if (ACPI_SUCCESS(Status)) { 928 if (gen_count == sc->ec_gencount) 929 no_intr++; 930 else 931 no_intr = 0; 932 break; 933 } 934 gen_count = sc->ec_gencount; 935 } 936 937 /* 938 * We finished waiting for the GPE and it never arrived. Try to 939 * read the register once and trust whatever value we got. This is 940 * the best we can do at this point. 941 */ 942 if (ACPI_FAILURE(Status)) 943 Status = EcCheckStatus(sc, "sleep_end", Event); 944 } 945 if (!need_poll && no_intr > 10) { 946 device_printf(sc->ec_dev, 947 "not getting interrupts, switched to polled mode\n"); 948 ec_polled_mode = 1; 949 } 950 if (ACPI_FAILURE(Status)) { 951 ec_timeout = ec_timeout_backoff; 952 if (ec_auto_silence > 0) { 953 if (--ec_auto_silence <= 0) 954 acpi_silence_all = 1; 955 } 956 KTR_LOG(acpi_ec_timeout); 957 } 958 return (Status); 959 } 960 961 static ACPI_STATUS 962 EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd) 963 { 964 ACPI_STATUS status; 965 EC_EVENT event; 966 EC_STATUS ec_status; 967 u_int gen_count; 968 969 ACPI_SERIAL_ASSERT(ec); 970 971 /* Don't use burst mode if user disabled it. */ 972 if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE) 973 return (AE_ERROR); 974 975 /* Decide what to wait for based on command type. */ 976 switch (cmd) { 977 case EC_COMMAND_READ: 978 case EC_COMMAND_WRITE: 979 case EC_COMMAND_BURST_DISABLE: 980 event = EC_EVENT_INPUT_BUFFER_EMPTY; 981 break; 982 case EC_COMMAND_QUERY: 983 case EC_COMMAND_BURST_ENABLE: 984 event = EC_EVENT_OUTPUT_BUFFER_FULL; 985 break; 986 default: 987 device_printf(sc->ec_dev, "EcCommand: invalid command %#x\n", cmd); 988 return (AE_BAD_PARAMETER); 989 } 990 991 /* 992 * Ensure empty input buffer before issuing command. 993 * Use generation count of zero to force a quick check. 994 */ 995 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, 0); 996 if (ACPI_FAILURE(status)) 997 return (status); 998 999 /* Run the command and wait for the chosen event. */ 1000 KTR_LOG(acpi_ec_cmdrun, cmd); 1001 gen_count = sc->ec_gencount; 1002 EC_SET_CSR(sc, cmd); 1003 status = EcWaitEvent(sc, event, gen_count); 1004 if (ACPI_SUCCESS(status)) { 1005 /* If we succeeded, burst flag should now be present. */ 1006 if (cmd == EC_COMMAND_BURST_ENABLE) { 1007 ec_status = EC_GET_CSR(sc); 1008 if ((ec_status & EC_FLAG_BURST_MODE) == 0) 1009 status = AE_ERROR; 1010 } 1011 } else 1012 device_printf(sc->ec_dev, "EcCommand: no response to %#x\n", cmd); 1013 return (status); 1014 } 1015 1016 static ACPI_STATUS 1017 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data) 1018 { 1019 ACPI_STATUS status; 1020 u_int gen_count; 1021 int retry; 1022 1023 ACPI_SERIAL_ASSERT(ec); 1024 KTR_LOG(acpi_ec_readaddr, Address); 1025 1026 for (retry = 0; retry < 2; retry++) { 1027 status = EcCommand(sc, EC_COMMAND_READ); 1028 if (ACPI_FAILURE(status)) 1029 return (status); 1030 1031 gen_count = sc->ec_gencount; 1032 EC_SET_DATA(sc, Address); 1033 status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL, gen_count); 1034 if (ACPI_FAILURE(status)) { 1035 if (ACPI_SUCCESS(EcCheckStatus(sc, "retr_check", 1036 EC_EVENT_INPUT_BUFFER_EMPTY))) 1037 continue; 1038 else 1039 break; 1040 } 1041 *Data = EC_GET_DATA(sc); 1042 return (AE_OK); 1043 } 1044 device_printf(sc->ec_dev, "EcRead: failed waiting to get data\n"); 1045 return (status); 1046 } 1047 1048 static ACPI_STATUS 1049 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 Data) 1050 { 1051 ACPI_STATUS status; 1052 u_int gen_count; 1053 1054 ACPI_SERIAL_ASSERT(ec); 1055 KTR_LOG(acpi_ec_writeaddr, Address, Data); 1056 1057 status = EcCommand(sc, EC_COMMAND_WRITE); 1058 if (ACPI_FAILURE(status)) 1059 return (status); 1060 1061 gen_count = sc->ec_gencount; 1062 EC_SET_DATA(sc, Address); 1063 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count); 1064 if (ACPI_FAILURE(status)) { 1065 device_printf(sc->ec_dev, "EcWrite: failed waiting for sent address\n"); 1066 return (status); 1067 } 1068 1069 gen_count = sc->ec_gencount; 1070 EC_SET_DATA(sc, Data); 1071 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count); 1072 if (ACPI_FAILURE(status)) { 1073 device_printf(sc->ec_dev, "EcWrite: failed waiting for sent data\n"); 1074 return (status); 1075 } 1076 1077 return (AE_OK); 1078 } 1079