1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ec.c - ACPI Embedded Controller Driver (v3) 4 * 5 * Copyright (C) 2001-2015 Intel Corporation 6 * Author: 2014, 2015 Lv Zheng <lv.zheng@intel.com> 7 * 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com> 8 * 2006 Denis Sadykov <denis.m.sadykov@intel.com> 9 * 2004 Luming Yu <luming.yu@intel.com> 10 * 2001, 2002 Andy Grover <andrew.grover@intel.com> 11 * 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> 12 * Copyright (C) 2008 Alexey Starikovskiy <astarikovskiy@suse.de> 13 */ 14 15 /* Uncomment next line to get verbose printout */ 16 /* #define DEBUG */ 17 #define pr_fmt(fmt) "ACPI: EC: " fmt 18 19 #include <linux/kernel.h> 20 #include <linux/module.h> 21 #include <linux/init.h> 22 #include <linux/types.h> 23 #include <linux/delay.h> 24 #include <linux/interrupt.h> 25 #include <linux/list.h> 26 #include <linux/platform_device.h> 27 #include <linux/printk.h> 28 #include <linux/spinlock.h> 29 #include <linux/slab.h> 30 #include <linux/string.h> 31 #include <linux/suspend.h> 32 #include <linux/acpi.h> 33 #include <linux/dmi.h> 34 #include <asm/io.h> 35 36 #include "internal.h" 37 38 #define ACPI_EC_CLASS "embedded_controller" 39 #define ACPI_EC_DEVICE_NAME "Embedded Controller" 40 41 /* EC status register */ 42 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */ 43 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */ 44 #define ACPI_EC_FLAG_CMD 0x08 /* Input buffer contains a command */ 45 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */ 46 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */ 47 48 /* 49 * The SCI_EVT clearing timing is not defined by the ACPI specification. 50 * This leads to lots of practical timing issues for the host EC driver. 51 * The following variations are defined (from the target EC firmware's 52 * perspective): 53 * STATUS: After indicating SCI_EVT edge triggered IRQ to the host, the 54 * target can clear SCI_EVT at any time so long as the host can see 55 * the indication by reading the status register (EC_SC). So the 56 * host should re-check SCI_EVT after the first time the SCI_EVT 57 * indication is seen, which is the same time the query request 58 * (QR_EC) is written to the command register (EC_CMD). SCI_EVT set 59 * at any later time could indicate another event. Normally such 60 * kind of EC firmware has implemented an event queue and will 61 * return 0x00 to indicate "no outstanding event". 62 * QUERY: After seeing the query request (QR_EC) written to the command 63 * register (EC_CMD) by the host and having prepared the responding 64 * event value in the data register (EC_DATA), the target can safely 65 * clear SCI_EVT because the target can confirm that the current 66 * event is being handled by the host. The host then should check 67 * SCI_EVT right after reading the event response from the data 68 * register (EC_DATA). 69 * EVENT: After seeing the event response read from the data register 70 * (EC_DATA) by the host, the target can clear SCI_EVT. As the 71 * target requires time to notice the change in the data register 72 * (EC_DATA), the host may be required to wait additional guarding 73 * time before checking the SCI_EVT again. Such guarding may not be 74 * necessary if the host is notified via another IRQ. 75 */ 76 #define ACPI_EC_EVT_TIMING_STATUS 0x00 77 #define ACPI_EC_EVT_TIMING_QUERY 0x01 78 #define ACPI_EC_EVT_TIMING_EVENT 0x02 79 80 /* EC commands */ 81 enum ec_command { 82 ACPI_EC_COMMAND_READ = 0x80, 83 ACPI_EC_COMMAND_WRITE = 0x81, 84 ACPI_EC_BURST_ENABLE = 0x82, 85 ACPI_EC_BURST_DISABLE = 0x83, 86 ACPI_EC_COMMAND_QUERY = 0x84, 87 }; 88 89 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */ 90 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */ 91 #define ACPI_EC_UDELAY_POLL 550 /* Wait 1ms for EC transaction polling */ 92 #define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query 93 * when trying to clear the EC */ 94 #define ACPI_EC_MAX_QUERIES 16 /* Maximum number of parallel queries */ 95 96 enum { 97 EC_FLAGS_QUERY_ENABLED, /* Query is enabled */ 98 EC_FLAGS_EVENT_HANDLER_INSTALLED, /* Event handler installed */ 99 EC_FLAGS_EC_HANDLER_INSTALLED, /* OpReg handler installed */ 100 EC_FLAGS_EC_REG_CALLED, /* OpReg ACPI _REG method called */ 101 EC_FLAGS_QUERY_METHODS_INSTALLED, /* _Qxx handlers installed */ 102 EC_FLAGS_STARTED, /* Driver is started */ 103 EC_FLAGS_STOPPED, /* Driver is stopped */ 104 EC_FLAGS_EVENTS_MASKED, /* Events masked */ 105 }; 106 107 #define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */ 108 #define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */ 109 110 /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */ 111 static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY; 112 module_param(ec_delay, uint, 0644); 113 MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes"); 114 115 static unsigned int ec_max_queries __read_mostly = ACPI_EC_MAX_QUERIES; 116 module_param(ec_max_queries, uint, 0644); 117 MODULE_PARM_DESC(ec_max_queries, "Maximum parallel _Qxx evaluations"); 118 119 static bool ec_busy_polling __read_mostly; 120 module_param(ec_busy_polling, bool, 0644); 121 MODULE_PARM_DESC(ec_busy_polling, "Use busy polling to advance EC transaction"); 122 123 static unsigned int ec_polling_guard __read_mostly = ACPI_EC_UDELAY_POLL; 124 module_param(ec_polling_guard, uint, 0644); 125 MODULE_PARM_DESC(ec_polling_guard, "Guard time(us) between EC accesses in polling modes"); 126 127 static unsigned int ec_event_clearing __read_mostly = ACPI_EC_EVT_TIMING_QUERY; 128 129 /* 130 * If the number of false interrupts per one transaction exceeds 131 * this threshold, will think there is a GPE storm happened and 132 * will disable the GPE for normal transaction. 133 */ 134 static unsigned int ec_storm_threshold __read_mostly = 8; 135 module_param(ec_storm_threshold, uint, 0644); 136 MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm"); 137 138 static bool ec_freeze_events __read_mostly; 139 module_param(ec_freeze_events, bool, 0644); 140 MODULE_PARM_DESC(ec_freeze_events, "Disabling event handling during suspend/resume"); 141 142 static bool ec_no_wakeup __read_mostly; 143 module_param(ec_no_wakeup, bool, 0644); 144 MODULE_PARM_DESC(ec_no_wakeup, "Do not wake up from suspend-to-idle"); 145 146 struct acpi_ec_query_handler { 147 struct list_head node; 148 acpi_ec_query_func func; 149 acpi_handle handle; 150 void *data; 151 u8 query_bit; 152 struct kref kref; 153 }; 154 155 struct transaction { 156 const u8 *wdata; 157 u8 *rdata; 158 unsigned short irq_count; 159 u8 command; 160 u8 wi; 161 u8 ri; 162 u8 wlen; 163 u8 rlen; 164 u8 flags; 165 }; 166 167 struct acpi_ec_query { 168 struct transaction transaction; 169 struct work_struct work; 170 struct acpi_ec_query_handler *handler; 171 struct acpi_ec *ec; 172 }; 173 174 static int acpi_ec_submit_query(struct acpi_ec *ec); 175 static void advance_transaction(struct acpi_ec *ec, bool interrupt); 176 static void acpi_ec_event_handler(struct work_struct *work); 177 178 struct acpi_ec *first_ec; 179 EXPORT_SYMBOL(first_ec); 180 181 static struct acpi_ec *boot_ec; 182 static bool boot_ec_is_ecdt; 183 static struct workqueue_struct *ec_wq; 184 static struct workqueue_struct *ec_query_wq; 185 186 static int EC_FLAGS_CORRECT_ECDT; /* Needs ECDT port address correction */ 187 static int EC_FLAGS_TRUST_DSDT_GPE; /* Needs DSDT GPE as correction setting */ 188 static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */ 189 190 /* -------------------------------------------------------------------------- 191 * Logging/Debugging 192 * -------------------------------------------------------------------------- */ 193 194 /* 195 * Splitters used by the developers to track the boundary of the EC 196 * handling processes. 197 */ 198 #ifdef DEBUG 199 #define EC_DBG_SEP " " 200 #define EC_DBG_DRV "+++++" 201 #define EC_DBG_STM "=====" 202 #define EC_DBG_REQ "*****" 203 #define EC_DBG_EVT "#####" 204 #else 205 #define EC_DBG_SEP "" 206 #define EC_DBG_DRV 207 #define EC_DBG_STM 208 #define EC_DBG_REQ 209 #define EC_DBG_EVT 210 #endif 211 212 #define ec_log_raw(fmt, ...) \ 213 pr_info(fmt "\n", ##__VA_ARGS__) 214 #define ec_dbg_raw(fmt, ...) \ 215 pr_debug(fmt "\n", ##__VA_ARGS__) 216 #define ec_log(filter, fmt, ...) \ 217 ec_log_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__) 218 #define ec_dbg(filter, fmt, ...) \ 219 ec_dbg_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__) 220 221 #define ec_log_drv(fmt, ...) \ 222 ec_log(EC_DBG_DRV, fmt, ##__VA_ARGS__) 223 #define ec_dbg_drv(fmt, ...) \ 224 ec_dbg(EC_DBG_DRV, fmt, ##__VA_ARGS__) 225 #define ec_dbg_stm(fmt, ...) \ 226 ec_dbg(EC_DBG_STM, fmt, ##__VA_ARGS__) 227 #define ec_dbg_req(fmt, ...) \ 228 ec_dbg(EC_DBG_REQ, fmt, ##__VA_ARGS__) 229 #define ec_dbg_evt(fmt, ...) \ 230 ec_dbg(EC_DBG_EVT, fmt, ##__VA_ARGS__) 231 #define ec_dbg_ref(ec, fmt, ...) \ 232 ec_dbg_raw("%lu: " fmt, ec->reference_count, ## __VA_ARGS__) 233 234 /* -------------------------------------------------------------------------- 235 * Device Flags 236 * -------------------------------------------------------------------------- */ 237 238 static bool acpi_ec_started(struct acpi_ec *ec) 239 { 240 return test_bit(EC_FLAGS_STARTED, &ec->flags) && 241 !test_bit(EC_FLAGS_STOPPED, &ec->flags); 242 } 243 244 static bool acpi_ec_event_enabled(struct acpi_ec *ec) 245 { 246 /* 247 * There is an OSPM early stage logic. During the early stages 248 * (boot/resume), OSPMs shouldn't enable the event handling, only 249 * the EC transactions are allowed to be performed. 250 */ 251 if (!test_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) 252 return false; 253 /* 254 * However, disabling the event handling is experimental for late 255 * stage (suspend), and is controlled by the boot parameter of 256 * "ec_freeze_events": 257 * 1. true: The EC event handling is disabled before entering 258 * the noirq stage. 259 * 2. false: The EC event handling is automatically disabled as 260 * soon as the EC driver is stopped. 261 */ 262 if (ec_freeze_events) 263 return acpi_ec_started(ec); 264 else 265 return test_bit(EC_FLAGS_STARTED, &ec->flags); 266 } 267 268 static bool acpi_ec_flushed(struct acpi_ec *ec) 269 { 270 return ec->reference_count == 1; 271 } 272 273 /* -------------------------------------------------------------------------- 274 * EC Registers 275 * -------------------------------------------------------------------------- */ 276 277 static inline u8 acpi_ec_read_status(struct acpi_ec *ec) 278 { 279 u8 x = inb(ec->command_addr); 280 281 ec_dbg_raw("EC_SC(R) = 0x%2.2x " 282 "SCI_EVT=%d BURST=%d CMD=%d IBF=%d OBF=%d", 283 x, 284 !!(x & ACPI_EC_FLAG_SCI), 285 !!(x & ACPI_EC_FLAG_BURST), 286 !!(x & ACPI_EC_FLAG_CMD), 287 !!(x & ACPI_EC_FLAG_IBF), 288 !!(x & ACPI_EC_FLAG_OBF)); 289 return x; 290 } 291 292 static inline u8 acpi_ec_read_data(struct acpi_ec *ec) 293 { 294 u8 x = inb(ec->data_addr); 295 296 ec->timestamp = jiffies; 297 ec_dbg_raw("EC_DATA(R) = 0x%2.2x", x); 298 return x; 299 } 300 301 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command) 302 { 303 ec_dbg_raw("EC_SC(W) = 0x%2.2x", command); 304 outb(command, ec->command_addr); 305 ec->timestamp = jiffies; 306 } 307 308 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data) 309 { 310 ec_dbg_raw("EC_DATA(W) = 0x%2.2x", data); 311 outb(data, ec->data_addr); 312 ec->timestamp = jiffies; 313 } 314 315 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG) 316 static const char *acpi_ec_cmd_string(u8 cmd) 317 { 318 switch (cmd) { 319 case 0x80: 320 return "RD_EC"; 321 case 0x81: 322 return "WR_EC"; 323 case 0x82: 324 return "BE_EC"; 325 case 0x83: 326 return "BD_EC"; 327 case 0x84: 328 return "QR_EC"; 329 } 330 return "UNKNOWN"; 331 } 332 #else 333 #define acpi_ec_cmd_string(cmd) "UNDEF" 334 #endif 335 336 /* -------------------------------------------------------------------------- 337 * GPE Registers 338 * -------------------------------------------------------------------------- */ 339 340 static inline bool acpi_ec_gpe_status_set(struct acpi_ec *ec) 341 { 342 acpi_event_status gpe_status = 0; 343 344 (void)acpi_get_gpe_status(NULL, ec->gpe, &gpe_status); 345 return !!(gpe_status & ACPI_EVENT_FLAG_STATUS_SET); 346 } 347 348 static inline void acpi_ec_enable_gpe(struct acpi_ec *ec, bool open) 349 { 350 if (open) 351 acpi_enable_gpe(NULL, ec->gpe); 352 else { 353 BUG_ON(ec->reference_count < 1); 354 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE); 355 } 356 if (acpi_ec_gpe_status_set(ec)) { 357 /* 358 * On some platforms, EN=1 writes cannot trigger GPE. So 359 * software need to manually trigger a pseudo GPE event on 360 * EN=1 writes. 361 */ 362 ec_dbg_raw("Polling quirk"); 363 advance_transaction(ec, false); 364 } 365 } 366 367 static inline void acpi_ec_disable_gpe(struct acpi_ec *ec, bool close) 368 { 369 if (close) 370 acpi_disable_gpe(NULL, ec->gpe); 371 else { 372 BUG_ON(ec->reference_count < 1); 373 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE); 374 } 375 } 376 377 /* -------------------------------------------------------------------------- 378 * Transaction Management 379 * -------------------------------------------------------------------------- */ 380 381 static void acpi_ec_submit_request(struct acpi_ec *ec) 382 { 383 ec->reference_count++; 384 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags) && 385 ec->gpe >= 0 && ec->reference_count == 1) 386 acpi_ec_enable_gpe(ec, true); 387 } 388 389 static void acpi_ec_complete_request(struct acpi_ec *ec) 390 { 391 bool flushed = false; 392 393 ec->reference_count--; 394 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags) && 395 ec->gpe >= 0 && ec->reference_count == 0) 396 acpi_ec_disable_gpe(ec, true); 397 flushed = acpi_ec_flushed(ec); 398 if (flushed) 399 wake_up(&ec->wait); 400 } 401 402 static void acpi_ec_mask_events(struct acpi_ec *ec) 403 { 404 if (!test_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags)) { 405 if (ec->gpe >= 0) 406 acpi_ec_disable_gpe(ec, false); 407 else 408 disable_irq_nosync(ec->irq); 409 410 ec_dbg_drv("Polling enabled"); 411 set_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags); 412 } 413 } 414 415 static void acpi_ec_unmask_events(struct acpi_ec *ec) 416 { 417 if (test_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags)) { 418 clear_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags); 419 if (ec->gpe >= 0) 420 acpi_ec_enable_gpe(ec, false); 421 else 422 enable_irq(ec->irq); 423 424 ec_dbg_drv("Polling disabled"); 425 } 426 } 427 428 /* 429 * acpi_ec_submit_flushable_request() - Increase the reference count unless 430 * the flush operation is not in 431 * progress 432 * @ec: the EC device 433 * 434 * This function must be used before taking a new action that should hold 435 * the reference count. If this function returns false, then the action 436 * must be discarded or it will prevent the flush operation from being 437 * completed. 438 */ 439 static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec) 440 { 441 if (!acpi_ec_started(ec)) 442 return false; 443 acpi_ec_submit_request(ec); 444 return true; 445 } 446 447 static void acpi_ec_submit_event(struct acpi_ec *ec) 448 { 449 /* 450 * It is safe to mask the events here, because acpi_ec_close_event() 451 * will run at least once after this. 452 */ 453 acpi_ec_mask_events(ec); 454 if (!acpi_ec_event_enabled(ec)) 455 return; 456 457 if (ec->event_state != EC_EVENT_READY) 458 return; 459 460 ec_dbg_evt("Command(%s) submitted/blocked", 461 acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY)); 462 463 ec->event_state = EC_EVENT_IN_PROGRESS; 464 /* 465 * If events_to_process is greater than 0 at this point, the while () 466 * loop in acpi_ec_event_handler() is still running and incrementing 467 * events_to_process will cause it to invoke acpi_ec_submit_query() once 468 * more, so it is not necessary to queue up the event work to start the 469 * same loop again. 470 */ 471 if (ec->events_to_process++ > 0) 472 return; 473 474 ec->events_in_progress++; 475 queue_work(ec_wq, &ec->work); 476 } 477 478 static void acpi_ec_complete_event(struct acpi_ec *ec) 479 { 480 if (ec->event_state == EC_EVENT_IN_PROGRESS) 481 ec->event_state = EC_EVENT_COMPLETE; 482 } 483 484 static void acpi_ec_close_event(struct acpi_ec *ec) 485 { 486 if (ec->event_state != EC_EVENT_READY) 487 ec_dbg_evt("Command(%s) unblocked", 488 acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY)); 489 490 ec->event_state = EC_EVENT_READY; 491 acpi_ec_unmask_events(ec); 492 } 493 494 static inline void __acpi_ec_enable_event(struct acpi_ec *ec) 495 { 496 if (!test_and_set_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) 497 ec_log_drv("event unblocked"); 498 /* 499 * Unconditionally invoke this once after enabling the event 500 * handling mechanism to detect the pending events. 501 */ 502 advance_transaction(ec, false); 503 } 504 505 static inline void __acpi_ec_disable_event(struct acpi_ec *ec) 506 { 507 if (test_and_clear_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) 508 ec_log_drv("event blocked"); 509 } 510 511 /* 512 * Process _Q events that might have accumulated in the EC. 513 * Run with locked ec mutex. 514 */ 515 static void acpi_ec_clear(struct acpi_ec *ec) 516 { 517 int i; 518 519 for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) { 520 if (acpi_ec_submit_query(ec)) 521 break; 522 } 523 if (unlikely(i == ACPI_EC_CLEAR_MAX)) 524 pr_warn("Warning: Maximum of %d stale EC events cleared\n", i); 525 else 526 pr_info("%d stale EC events cleared\n", i); 527 } 528 529 static void acpi_ec_enable_event(struct acpi_ec *ec) 530 { 531 unsigned long flags; 532 533 spin_lock_irqsave(&ec->lock, flags); 534 if (acpi_ec_started(ec)) 535 __acpi_ec_enable_event(ec); 536 spin_unlock_irqrestore(&ec->lock, flags); 537 538 /* Drain additional events if hardware requires that */ 539 if (EC_FLAGS_CLEAR_ON_RESUME) 540 acpi_ec_clear(ec); 541 } 542 543 #ifdef CONFIG_PM_SLEEP 544 static void __acpi_ec_flush_work(void) 545 { 546 flush_workqueue(ec_wq); /* flush ec->work */ 547 flush_workqueue(ec_query_wq); /* flush queries */ 548 } 549 550 static void acpi_ec_disable_event(struct acpi_ec *ec) 551 { 552 unsigned long flags; 553 554 spin_lock_irqsave(&ec->lock, flags); 555 __acpi_ec_disable_event(ec); 556 spin_unlock_irqrestore(&ec->lock, flags); 557 558 /* 559 * When ec_freeze_events is true, we need to flush events in 560 * the proper position before entering the noirq stage. 561 */ 562 __acpi_ec_flush_work(); 563 } 564 565 void acpi_ec_flush_work(void) 566 { 567 /* Without ec_wq there is nothing to flush. */ 568 if (!ec_wq) 569 return; 570 571 __acpi_ec_flush_work(); 572 } 573 #endif /* CONFIG_PM_SLEEP */ 574 575 static bool acpi_ec_guard_event(struct acpi_ec *ec) 576 { 577 unsigned long flags; 578 bool guarded; 579 580 spin_lock_irqsave(&ec->lock, flags); 581 /* 582 * If firmware SCI_EVT clearing timing is "event", we actually 583 * don't know when the SCI_EVT will be cleared by firmware after 584 * evaluating _Qxx, so we need to re-check SCI_EVT after waiting an 585 * acceptable period. 586 * 587 * The guarding period is applicable if the event state is not 588 * EC_EVENT_READY, but otherwise if the current transaction is of the 589 * ACPI_EC_COMMAND_QUERY type, the guarding should have elapsed already 590 * and it should not be applied to let the transaction transition into 591 * the ACPI_EC_COMMAND_POLL state immediately. 592 */ 593 guarded = ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT && 594 ec->event_state != EC_EVENT_READY && 595 (!ec->curr || ec->curr->command != ACPI_EC_COMMAND_QUERY); 596 spin_unlock_irqrestore(&ec->lock, flags); 597 return guarded; 598 } 599 600 static int ec_transaction_polled(struct acpi_ec *ec) 601 { 602 unsigned long flags; 603 int ret = 0; 604 605 spin_lock_irqsave(&ec->lock, flags); 606 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_POLL)) 607 ret = 1; 608 spin_unlock_irqrestore(&ec->lock, flags); 609 return ret; 610 } 611 612 static int ec_transaction_completed(struct acpi_ec *ec) 613 { 614 unsigned long flags; 615 int ret = 0; 616 617 spin_lock_irqsave(&ec->lock, flags); 618 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE)) 619 ret = 1; 620 spin_unlock_irqrestore(&ec->lock, flags); 621 return ret; 622 } 623 624 static inline void ec_transaction_transition(struct acpi_ec *ec, unsigned long flag) 625 { 626 ec->curr->flags |= flag; 627 628 if (ec->curr->command != ACPI_EC_COMMAND_QUERY) 629 return; 630 631 switch (ec_event_clearing) { 632 case ACPI_EC_EVT_TIMING_STATUS: 633 if (flag == ACPI_EC_COMMAND_POLL) 634 acpi_ec_close_event(ec); 635 636 return; 637 638 case ACPI_EC_EVT_TIMING_QUERY: 639 if (flag == ACPI_EC_COMMAND_COMPLETE) 640 acpi_ec_close_event(ec); 641 642 return; 643 644 case ACPI_EC_EVT_TIMING_EVENT: 645 if (flag == ACPI_EC_COMMAND_COMPLETE) 646 acpi_ec_complete_event(ec); 647 } 648 } 649 650 static void acpi_ec_spurious_interrupt(struct acpi_ec *ec, struct transaction *t) 651 { 652 if (t->irq_count < ec_storm_threshold) 653 ++t->irq_count; 654 655 /* Trigger if the threshold is 0 too. */ 656 if (t->irq_count == ec_storm_threshold) 657 acpi_ec_mask_events(ec); 658 } 659 660 static void advance_transaction(struct acpi_ec *ec, bool interrupt) 661 { 662 struct transaction *t = ec->curr; 663 bool wakeup = false; 664 u8 status; 665 666 ec_dbg_stm("%s (%d)", interrupt ? "IRQ" : "TASK", smp_processor_id()); 667 668 status = acpi_ec_read_status(ec); 669 670 /* 671 * Another IRQ or a guarded polling mode advancement is detected, 672 * the next QR_EC submission is then allowed. 673 */ 674 if (!t || !(t->flags & ACPI_EC_COMMAND_POLL)) { 675 if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT && 676 ec->event_state == EC_EVENT_COMPLETE) 677 acpi_ec_close_event(ec); 678 679 if (!t) 680 goto out; 681 } 682 683 if (t->flags & ACPI_EC_COMMAND_POLL) { 684 if (t->wlen > t->wi) { 685 if (!(status & ACPI_EC_FLAG_IBF)) 686 acpi_ec_write_data(ec, t->wdata[t->wi++]); 687 else if (interrupt && !(status & ACPI_EC_FLAG_SCI)) 688 acpi_ec_spurious_interrupt(ec, t); 689 } else if (t->rlen > t->ri) { 690 if (status & ACPI_EC_FLAG_OBF) { 691 t->rdata[t->ri++] = acpi_ec_read_data(ec); 692 if (t->rlen == t->ri) { 693 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE); 694 wakeup = true; 695 if (t->command == ACPI_EC_COMMAND_QUERY) 696 ec_dbg_evt("Command(%s) completed by hardware", 697 acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY)); 698 } 699 } else if (interrupt && !(status & ACPI_EC_FLAG_SCI)) { 700 acpi_ec_spurious_interrupt(ec, t); 701 } 702 } else if (t->wlen == t->wi && !(status & ACPI_EC_FLAG_IBF)) { 703 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE); 704 wakeup = true; 705 } 706 } else if (!(status & ACPI_EC_FLAG_IBF)) { 707 acpi_ec_write_cmd(ec, t->command); 708 ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL); 709 } 710 711 out: 712 if (status & ACPI_EC_FLAG_SCI) 713 acpi_ec_submit_event(ec); 714 715 if (wakeup && interrupt) 716 wake_up(&ec->wait); 717 } 718 719 static void start_transaction(struct acpi_ec *ec) 720 { 721 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0; 722 ec->curr->flags = 0; 723 } 724 725 static int ec_guard(struct acpi_ec *ec) 726 { 727 unsigned long guard = usecs_to_jiffies(ec->polling_guard); 728 unsigned long timeout = ec->timestamp + guard; 729 730 /* Ensure guarding period before polling EC status */ 731 do { 732 if (ec->busy_polling) { 733 /* Perform busy polling */ 734 if (ec_transaction_completed(ec)) 735 return 0; 736 udelay(jiffies_to_usecs(guard)); 737 } else { 738 /* 739 * Perform wait polling 740 * 1. Wait the transaction to be completed by the 741 * GPE handler after the transaction enters 742 * ACPI_EC_COMMAND_POLL state. 743 * 2. A special guarding logic is also required 744 * for event clearing mode "event" before the 745 * transaction enters ACPI_EC_COMMAND_POLL 746 * state. 747 */ 748 if (!ec_transaction_polled(ec) && 749 !acpi_ec_guard_event(ec)) 750 break; 751 if (wait_event_timeout(ec->wait, 752 ec_transaction_completed(ec), 753 guard)) 754 return 0; 755 } 756 } while (time_before(jiffies, timeout)); 757 return -ETIME; 758 } 759 760 static int ec_poll(struct acpi_ec *ec) 761 { 762 unsigned long flags; 763 int repeat = 5; /* number of command restarts */ 764 765 while (repeat--) { 766 unsigned long delay = jiffies + 767 msecs_to_jiffies(ec_delay); 768 do { 769 if (!ec_guard(ec)) 770 return 0; 771 spin_lock_irqsave(&ec->lock, flags); 772 advance_transaction(ec, false); 773 spin_unlock_irqrestore(&ec->lock, flags); 774 } while (time_before(jiffies, delay)); 775 pr_debug("controller reset, restart transaction\n"); 776 spin_lock_irqsave(&ec->lock, flags); 777 start_transaction(ec); 778 spin_unlock_irqrestore(&ec->lock, flags); 779 } 780 return -ETIME; 781 } 782 783 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, 784 struct transaction *t) 785 { 786 unsigned long tmp; 787 int ret = 0; 788 789 if (t->rdata) 790 memset(t->rdata, 0, t->rlen); 791 792 /* start transaction */ 793 spin_lock_irqsave(&ec->lock, tmp); 794 /* Enable GPE for command processing (IBF=0/OBF=1) */ 795 if (!acpi_ec_submit_flushable_request(ec)) { 796 ret = -EINVAL; 797 goto unlock; 798 } 799 ec_dbg_ref(ec, "Increase command"); 800 /* following two actions should be kept atomic */ 801 ec->curr = t; 802 ec_dbg_req("Command(%s) started", acpi_ec_cmd_string(t->command)); 803 start_transaction(ec); 804 spin_unlock_irqrestore(&ec->lock, tmp); 805 806 ret = ec_poll(ec); 807 808 spin_lock_irqsave(&ec->lock, tmp); 809 if (t->irq_count == ec_storm_threshold) 810 acpi_ec_unmask_events(ec); 811 ec_dbg_req("Command(%s) stopped", acpi_ec_cmd_string(t->command)); 812 ec->curr = NULL; 813 /* Disable GPE for command processing (IBF=0/OBF=1) */ 814 acpi_ec_complete_request(ec); 815 ec_dbg_ref(ec, "Decrease command"); 816 unlock: 817 spin_unlock_irqrestore(&ec->lock, tmp); 818 return ret; 819 } 820 821 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t) 822 { 823 int status; 824 u32 glk; 825 826 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata)) 827 return -EINVAL; 828 829 mutex_lock(&ec->mutex); 830 if (ec->global_lock) { 831 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk); 832 if (ACPI_FAILURE(status)) { 833 status = -ENODEV; 834 goto unlock; 835 } 836 } 837 838 status = acpi_ec_transaction_unlocked(ec, t); 839 840 if (ec->global_lock) 841 acpi_release_global_lock(glk); 842 unlock: 843 mutex_unlock(&ec->mutex); 844 return status; 845 } 846 847 static int acpi_ec_burst_enable(struct acpi_ec *ec) 848 { 849 u8 d; 850 struct transaction t = {.command = ACPI_EC_BURST_ENABLE, 851 .wdata = NULL, .rdata = &d, 852 .wlen = 0, .rlen = 1}; 853 854 return acpi_ec_transaction_unlocked(ec, &t); 855 } 856 857 static int acpi_ec_burst_disable(struct acpi_ec *ec) 858 { 859 struct transaction t = {.command = ACPI_EC_BURST_DISABLE, 860 .wdata = NULL, .rdata = NULL, 861 .wlen = 0, .rlen = 0}; 862 863 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ? 864 acpi_ec_transaction_unlocked(ec, &t) : 0; 865 } 866 867 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data) 868 { 869 int result; 870 u8 d; 871 struct transaction t = {.command = ACPI_EC_COMMAND_READ, 872 .wdata = &address, .rdata = &d, 873 .wlen = 1, .rlen = 1}; 874 875 result = acpi_ec_transaction(ec, &t); 876 *data = d; 877 return result; 878 } 879 880 static int acpi_ec_read_unlocked(struct acpi_ec *ec, u8 address, u8 *data) 881 { 882 int result; 883 u8 d; 884 struct transaction t = {.command = ACPI_EC_COMMAND_READ, 885 .wdata = &address, .rdata = &d, 886 .wlen = 1, .rlen = 1}; 887 888 result = acpi_ec_transaction_unlocked(ec, &t); 889 *data = d; 890 return result; 891 } 892 893 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data) 894 { 895 u8 wdata[2] = { address, data }; 896 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE, 897 .wdata = wdata, .rdata = NULL, 898 .wlen = 2, .rlen = 0}; 899 900 return acpi_ec_transaction(ec, &t); 901 } 902 903 static int acpi_ec_write_unlocked(struct acpi_ec *ec, u8 address, u8 data) 904 { 905 u8 wdata[2] = { address, data }; 906 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE, 907 .wdata = wdata, .rdata = NULL, 908 .wlen = 2, .rlen = 0}; 909 910 return acpi_ec_transaction_unlocked(ec, &t); 911 } 912 913 int ec_read(u8 addr, u8 *val) 914 { 915 int err; 916 u8 temp_data; 917 918 if (!first_ec) 919 return -ENODEV; 920 921 err = acpi_ec_read(first_ec, addr, &temp_data); 922 923 if (!err) { 924 *val = temp_data; 925 return 0; 926 } 927 return err; 928 } 929 EXPORT_SYMBOL(ec_read); 930 931 int ec_write(u8 addr, u8 val) 932 { 933 if (!first_ec) 934 return -ENODEV; 935 936 return acpi_ec_write(first_ec, addr, val); 937 } 938 EXPORT_SYMBOL(ec_write); 939 940 int ec_transaction(u8 command, 941 const u8 *wdata, unsigned wdata_len, 942 u8 *rdata, unsigned rdata_len) 943 { 944 struct transaction t = {.command = command, 945 .wdata = wdata, .rdata = rdata, 946 .wlen = wdata_len, .rlen = rdata_len}; 947 948 if (!first_ec) 949 return -ENODEV; 950 951 return acpi_ec_transaction(first_ec, &t); 952 } 953 EXPORT_SYMBOL(ec_transaction); 954 955 /* Get the handle to the EC device */ 956 acpi_handle ec_get_handle(void) 957 { 958 if (!first_ec) 959 return NULL; 960 return first_ec->handle; 961 } 962 EXPORT_SYMBOL(ec_get_handle); 963 964 static void acpi_ec_start(struct acpi_ec *ec, bool resuming) 965 { 966 unsigned long flags; 967 968 spin_lock_irqsave(&ec->lock, flags); 969 if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) { 970 ec_dbg_drv("Starting EC"); 971 /* Enable GPE for event processing (SCI_EVT=1) */ 972 if (!resuming) { 973 acpi_ec_submit_request(ec); 974 ec_dbg_ref(ec, "Increase driver"); 975 } 976 ec_log_drv("EC started"); 977 } 978 spin_unlock_irqrestore(&ec->lock, flags); 979 } 980 981 static bool acpi_ec_stopped(struct acpi_ec *ec) 982 { 983 unsigned long flags; 984 bool flushed; 985 986 spin_lock_irqsave(&ec->lock, flags); 987 flushed = acpi_ec_flushed(ec); 988 spin_unlock_irqrestore(&ec->lock, flags); 989 return flushed; 990 } 991 992 static void acpi_ec_stop(struct acpi_ec *ec, bool suspending) 993 { 994 unsigned long flags; 995 996 spin_lock_irqsave(&ec->lock, flags); 997 if (acpi_ec_started(ec)) { 998 ec_dbg_drv("Stopping EC"); 999 set_bit(EC_FLAGS_STOPPED, &ec->flags); 1000 spin_unlock_irqrestore(&ec->lock, flags); 1001 wait_event(ec->wait, acpi_ec_stopped(ec)); 1002 spin_lock_irqsave(&ec->lock, flags); 1003 /* Disable GPE for event processing (SCI_EVT=1) */ 1004 if (!suspending) { 1005 acpi_ec_complete_request(ec); 1006 ec_dbg_ref(ec, "Decrease driver"); 1007 } else if (!ec_freeze_events) 1008 __acpi_ec_disable_event(ec); 1009 clear_bit(EC_FLAGS_STARTED, &ec->flags); 1010 clear_bit(EC_FLAGS_STOPPED, &ec->flags); 1011 ec_log_drv("EC stopped"); 1012 } 1013 spin_unlock_irqrestore(&ec->lock, flags); 1014 } 1015 1016 static void acpi_ec_enter_noirq(struct acpi_ec *ec) 1017 { 1018 unsigned long flags; 1019 1020 spin_lock_irqsave(&ec->lock, flags); 1021 ec->busy_polling = true; 1022 ec->polling_guard = 0; 1023 ec_log_drv("interrupt blocked"); 1024 spin_unlock_irqrestore(&ec->lock, flags); 1025 } 1026 1027 static void acpi_ec_leave_noirq(struct acpi_ec *ec) 1028 { 1029 unsigned long flags; 1030 1031 spin_lock_irqsave(&ec->lock, flags); 1032 ec->busy_polling = ec_busy_polling; 1033 ec->polling_guard = ec_polling_guard; 1034 ec_log_drv("interrupt unblocked"); 1035 spin_unlock_irqrestore(&ec->lock, flags); 1036 } 1037 1038 void acpi_ec_block_transactions(void) 1039 { 1040 struct acpi_ec *ec = first_ec; 1041 1042 if (!ec) 1043 return; 1044 1045 mutex_lock(&ec->mutex); 1046 /* Prevent transactions from being carried out */ 1047 acpi_ec_stop(ec, true); 1048 mutex_unlock(&ec->mutex); 1049 } 1050 1051 void acpi_ec_unblock_transactions(void) 1052 { 1053 /* 1054 * Allow transactions to happen again (this function is called from 1055 * atomic context during wakeup, so we don't need to acquire the mutex). 1056 */ 1057 if (first_ec) 1058 acpi_ec_start(first_ec, true); 1059 } 1060 1061 /* -------------------------------------------------------------------------- 1062 Event Management 1063 -------------------------------------------------------------------------- */ 1064 static struct acpi_ec_query_handler * 1065 acpi_ec_get_query_handler_by_value(struct acpi_ec *ec, u8 value) 1066 { 1067 struct acpi_ec_query_handler *handler; 1068 1069 mutex_lock(&ec->mutex); 1070 list_for_each_entry(handler, &ec->list, node) { 1071 if (value == handler->query_bit) { 1072 kref_get(&handler->kref); 1073 mutex_unlock(&ec->mutex); 1074 return handler; 1075 } 1076 } 1077 mutex_unlock(&ec->mutex); 1078 return NULL; 1079 } 1080 1081 static void acpi_ec_query_handler_release(struct kref *kref) 1082 { 1083 struct acpi_ec_query_handler *handler = 1084 container_of(kref, struct acpi_ec_query_handler, kref); 1085 1086 kfree(handler); 1087 } 1088 1089 static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler) 1090 { 1091 kref_put(&handler->kref, acpi_ec_query_handler_release); 1092 } 1093 1094 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit, 1095 acpi_handle handle, acpi_ec_query_func func, 1096 void *data) 1097 { 1098 struct acpi_ec_query_handler *handler; 1099 1100 if (!handle && !func) 1101 return -EINVAL; 1102 1103 handler = kzalloc_obj(*handler); 1104 if (!handler) 1105 return -ENOMEM; 1106 1107 handler->query_bit = query_bit; 1108 handler->handle = handle; 1109 handler->func = func; 1110 handler->data = data; 1111 mutex_lock(&ec->mutex); 1112 kref_init(&handler->kref); 1113 list_add(&handler->node, &ec->list); 1114 mutex_unlock(&ec->mutex); 1115 1116 return 0; 1117 } 1118 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler); 1119 1120 static void acpi_ec_remove_query_handlers(struct acpi_ec *ec, 1121 bool remove_all, u8 query_bit) 1122 { 1123 struct acpi_ec_query_handler *handler, *tmp; 1124 LIST_HEAD(free_list); 1125 1126 mutex_lock(&ec->mutex); 1127 list_for_each_entry_safe(handler, tmp, &ec->list, node) { 1128 /* 1129 * When remove_all is false, only remove custom query handlers 1130 * which have handler->func set. This is done to preserve query 1131 * handlers discovered thru ACPI, as they should continue handling 1132 * EC queries. 1133 */ 1134 if (remove_all || (handler->func && handler->query_bit == query_bit)) { 1135 list_del_init(&handler->node); 1136 list_add(&handler->node, &free_list); 1137 1138 } 1139 } 1140 mutex_unlock(&ec->mutex); 1141 list_for_each_entry_safe(handler, tmp, &free_list, node) 1142 acpi_ec_put_query_handler(handler); 1143 } 1144 1145 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit) 1146 { 1147 acpi_ec_remove_query_handlers(ec, false, query_bit); 1148 flush_workqueue(ec_query_wq); 1149 } 1150 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler); 1151 1152 static void acpi_ec_event_processor(struct work_struct *work) 1153 { 1154 struct acpi_ec_query *q = container_of(work, struct acpi_ec_query, work); 1155 struct acpi_ec_query_handler *handler = q->handler; 1156 struct acpi_ec *ec = q->ec; 1157 1158 ec_dbg_evt("Query(0x%02x) started", handler->query_bit); 1159 1160 if (handler->func) 1161 handler->func(handler->data); 1162 else if (handler->handle) 1163 acpi_evaluate_object(handler->handle, NULL, NULL, NULL); 1164 1165 ec_dbg_evt("Query(0x%02x) stopped", handler->query_bit); 1166 1167 spin_lock_irq(&ec->lock); 1168 ec->queries_in_progress--; 1169 spin_unlock_irq(&ec->lock); 1170 1171 acpi_ec_put_query_handler(handler); 1172 kfree(q); 1173 } 1174 1175 static struct acpi_ec_query *acpi_ec_create_query(struct acpi_ec *ec, u8 *pval) 1176 { 1177 struct acpi_ec_query *q; 1178 struct transaction *t; 1179 1180 q = kzalloc_obj(struct acpi_ec_query); 1181 if (!q) 1182 return NULL; 1183 1184 INIT_WORK(&q->work, acpi_ec_event_processor); 1185 t = &q->transaction; 1186 t->command = ACPI_EC_COMMAND_QUERY; 1187 t->rdata = pval; 1188 t->rlen = 1; 1189 q->ec = ec; 1190 return q; 1191 } 1192 1193 static int acpi_ec_submit_query(struct acpi_ec *ec) 1194 { 1195 struct acpi_ec_query *q; 1196 u8 value = 0; 1197 int result; 1198 1199 q = acpi_ec_create_query(ec, &value); 1200 if (!q) 1201 return -ENOMEM; 1202 1203 /* 1204 * Query the EC to find out which _Qxx method we need to evaluate. 1205 * Note that successful completion of the query causes the ACPI_EC_SCI 1206 * bit to be cleared (and thus clearing the interrupt source). 1207 */ 1208 result = acpi_ec_transaction(ec, &q->transaction); 1209 if (result) 1210 goto err_exit; 1211 1212 if (!value) { 1213 result = -ENODATA; 1214 goto err_exit; 1215 } 1216 1217 q->handler = acpi_ec_get_query_handler_by_value(ec, value); 1218 if (!q->handler) { 1219 result = -ENODATA; 1220 goto err_exit; 1221 } 1222 1223 /* 1224 * It is reported that _Qxx are evaluated in a parallel way on Windows: 1225 * https://bugzilla.kernel.org/show_bug.cgi?id=94411 1226 * 1227 * Put this log entry before queue_work() to make it appear in the log 1228 * before any other messages emitted during workqueue handling. 1229 */ 1230 ec_dbg_evt("Query(0x%02x) scheduled", value); 1231 1232 spin_lock_irq(&ec->lock); 1233 1234 ec->queries_in_progress++; 1235 queue_work(ec_query_wq, &q->work); 1236 1237 spin_unlock_irq(&ec->lock); 1238 1239 return 0; 1240 1241 err_exit: 1242 kfree(q); 1243 1244 return result; 1245 } 1246 1247 static void acpi_ec_event_handler(struct work_struct *work) 1248 { 1249 struct acpi_ec *ec = container_of(work, struct acpi_ec, work); 1250 1251 ec_dbg_evt("Event started"); 1252 1253 spin_lock_irq(&ec->lock); 1254 1255 while (ec->events_to_process) { 1256 spin_unlock_irq(&ec->lock); 1257 1258 acpi_ec_submit_query(ec); 1259 1260 spin_lock_irq(&ec->lock); 1261 1262 ec->events_to_process--; 1263 } 1264 1265 /* 1266 * Before exit, make sure that the it will be possible to queue up the 1267 * event handling work again regardless of whether or not the query 1268 * queued up above is processed successfully. 1269 */ 1270 if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT) { 1271 bool guard_timeout; 1272 1273 acpi_ec_complete_event(ec); 1274 1275 ec_dbg_evt("Event stopped"); 1276 1277 spin_unlock_irq(&ec->lock); 1278 1279 guard_timeout = !!ec_guard(ec); 1280 1281 spin_lock_irq(&ec->lock); 1282 1283 /* Take care of SCI_EVT unless someone else is doing that. */ 1284 if (guard_timeout && !ec->curr) 1285 advance_transaction(ec, false); 1286 } else { 1287 acpi_ec_close_event(ec); 1288 1289 ec_dbg_evt("Event stopped"); 1290 } 1291 1292 ec->events_in_progress--; 1293 1294 spin_unlock_irq(&ec->lock); 1295 } 1296 1297 static void clear_gpe_and_advance_transaction(struct acpi_ec *ec, bool interrupt) 1298 { 1299 /* 1300 * Clear GPE_STS upfront to allow subsequent hardware GPE_STS 0->1 1301 * changes to always trigger a GPE interrupt. 1302 * 1303 * GPE STS is a W1C register, which means: 1304 * 1305 * 1. Software can clear it without worrying about clearing the other 1306 * GPEs' STS bits when the hardware sets them in parallel. 1307 * 1308 * 2. As long as software can ensure only clearing it when it is set, 1309 * hardware won't set it in parallel. 1310 */ 1311 if (ec->gpe >= 0 && acpi_ec_gpe_status_set(ec)) 1312 acpi_clear_gpe(NULL, ec->gpe); 1313 1314 advance_transaction(ec, true); 1315 } 1316 1317 static void acpi_ec_handle_interrupt(struct acpi_ec *ec) 1318 { 1319 unsigned long flags; 1320 1321 spin_lock_irqsave(&ec->lock, flags); 1322 1323 clear_gpe_and_advance_transaction(ec, true); 1324 1325 spin_unlock_irqrestore(&ec->lock, flags); 1326 } 1327 1328 static u32 acpi_ec_gpe_handler(acpi_handle gpe_device, 1329 u32 gpe_number, void *data) 1330 { 1331 acpi_ec_handle_interrupt(data); 1332 return ACPI_INTERRUPT_HANDLED; 1333 } 1334 1335 static irqreturn_t acpi_ec_irq_handler(int irq, void *data) 1336 { 1337 acpi_ec_handle_interrupt(data); 1338 return IRQ_HANDLED; 1339 } 1340 1341 /* -------------------------------------------------------------------------- 1342 * Address Space Management 1343 * -------------------------------------------------------------------------- */ 1344 1345 static acpi_status 1346 acpi_ec_space_handler(u32 function, acpi_physical_address address, 1347 u32 bits, u64 *value64, 1348 void *handler_context, void *region_context) 1349 { 1350 struct acpi_ec *ec = handler_context; 1351 int result = 0, i, bytes = bits / 8; 1352 u8 *value = (u8 *)value64; 1353 u32 glk; 1354 1355 if ((address > 0xFF) || !value || !handler_context) 1356 return AE_BAD_PARAMETER; 1357 1358 if (function != ACPI_READ && function != ACPI_WRITE) 1359 return AE_BAD_PARAMETER; 1360 1361 mutex_lock(&ec->mutex); 1362 1363 if (ec->global_lock) { 1364 acpi_status status; 1365 1366 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk); 1367 if (ACPI_FAILURE(status)) { 1368 result = -ENODEV; 1369 goto unlock; 1370 } 1371 } 1372 1373 if (ec->busy_polling || bits > 8) 1374 acpi_ec_burst_enable(ec); 1375 1376 for (i = 0; i < bytes; ++i, ++address, ++value) { 1377 result = (function == ACPI_READ) ? 1378 acpi_ec_read_unlocked(ec, address, value) : 1379 acpi_ec_write_unlocked(ec, address, *value); 1380 if (result < 0) 1381 break; 1382 } 1383 1384 if (ec->busy_polling || bits > 8) 1385 acpi_ec_burst_disable(ec); 1386 1387 if (ec->global_lock) 1388 acpi_release_global_lock(glk); 1389 1390 unlock: 1391 mutex_unlock(&ec->mutex); 1392 1393 switch (result) { 1394 case -EINVAL: 1395 return AE_BAD_PARAMETER; 1396 case -ENODEV: 1397 return AE_NOT_FOUND; 1398 case -ETIME: 1399 return AE_TIME; 1400 case 0: 1401 return AE_OK; 1402 default: 1403 return AE_ERROR; 1404 } 1405 } 1406 1407 /* -------------------------------------------------------------------------- 1408 * Driver Interface 1409 * -------------------------------------------------------------------------- */ 1410 1411 static acpi_status 1412 ec_parse_io_ports(struct acpi_resource *resource, void *context); 1413 1414 static void acpi_ec_free(struct acpi_ec *ec) 1415 { 1416 if (first_ec == ec) 1417 first_ec = NULL; 1418 if (boot_ec == ec) 1419 boot_ec = NULL; 1420 kfree(ec); 1421 } 1422 1423 static struct acpi_ec *acpi_ec_alloc(void) 1424 { 1425 struct acpi_ec *ec = kzalloc_obj(struct acpi_ec); 1426 1427 if (!ec) 1428 return NULL; 1429 mutex_init(&ec->mutex); 1430 init_waitqueue_head(&ec->wait); 1431 INIT_LIST_HEAD(&ec->list); 1432 spin_lock_init(&ec->lock); 1433 INIT_WORK(&ec->work, acpi_ec_event_handler); 1434 ec->timestamp = jiffies; 1435 ec->busy_polling = true; 1436 ec->polling_guard = 0; 1437 ec->gpe = -1; 1438 ec->irq = -1; 1439 return ec; 1440 } 1441 1442 static acpi_status 1443 acpi_ec_register_query_methods(acpi_handle handle, u32 level, 1444 void *context, void **return_value) 1445 { 1446 char node_name[5]; 1447 struct acpi_buffer buffer = { sizeof(node_name), node_name }; 1448 struct acpi_ec *ec = context; 1449 int value = 0; 1450 acpi_status status; 1451 1452 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); 1453 1454 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) 1455 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL); 1456 return AE_OK; 1457 } 1458 1459 static acpi_status 1460 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval) 1461 { 1462 acpi_status status; 1463 unsigned long long tmp = 0; 1464 struct acpi_ec *ec = context; 1465 1466 /* clear addr values, ec_parse_io_ports depend on it */ 1467 ec->command_addr = ec->data_addr = 0; 1468 1469 status = acpi_walk_resources(handle, METHOD_NAME__CRS, 1470 ec_parse_io_ports, ec); 1471 if (ACPI_FAILURE(status)) 1472 return status; 1473 if (ec->data_addr == 0 || ec->command_addr == 0) 1474 return AE_OK; 1475 1476 /* Get GPE bit assignment (EC events). */ 1477 /* TODO: Add support for _GPE returning a package */ 1478 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp); 1479 if (ACPI_SUCCESS(status)) 1480 ec->gpe = tmp; 1481 /* 1482 * Errors are non-fatal, allowing for ACPI Reduced Hardware 1483 * platforms which use GpioInt instead of GPE. 1484 */ 1485 1486 /* Use the global lock for all EC transactions? */ 1487 tmp = 0; 1488 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp); 1489 ec->global_lock = tmp; 1490 ec->handle = handle; 1491 return AE_CTRL_TERMINATE; 1492 } 1493 1494 static bool install_gpe_event_handler(struct acpi_ec *ec) 1495 { 1496 acpi_status status; 1497 1498 status = acpi_install_gpe_raw_handler(NULL, ec->gpe, 1499 ACPI_GPE_EDGE_TRIGGERED, 1500 &acpi_ec_gpe_handler, ec); 1501 if (ACPI_FAILURE(status)) 1502 return false; 1503 1504 if (test_bit(EC_FLAGS_STARTED, &ec->flags) && ec->reference_count >= 1) 1505 acpi_ec_enable_gpe(ec, true); 1506 1507 return true; 1508 } 1509 1510 static bool install_gpio_irq_event_handler(struct acpi_ec *ec) 1511 { 1512 return request_threaded_irq(ec->irq, NULL, acpi_ec_irq_handler, 1513 IRQF_SHARED | IRQF_ONESHOT, "ACPI EC", ec) >= 0; 1514 } 1515 1516 /** 1517 * ec_install_handlers - Install service callbacks and register query methods. 1518 * @ec: Target EC. 1519 * @device: ACPI device object corresponding to @ec. 1520 * @call_reg: If _REG should be called to notify OpRegion availability 1521 * 1522 * Install a handler for the EC address space type unless it has been installed 1523 * already. If @device is not NULL, also look for EC query methods in the 1524 * namespace and register them, and install an event (either GPE or GPIO IRQ) 1525 * handler for the EC, if possible. 1526 * 1527 * Return: 1528 * -ENODEV if the address space handler cannot be installed, which means 1529 * "unable to handle transactions", 1530 * -EPROBE_DEFER if GPIO IRQ acquisition needs to be deferred, 1531 * or 0 (success) otherwise. 1532 */ 1533 static int ec_install_handlers(struct acpi_ec *ec, struct acpi_device *device, 1534 bool call_reg) 1535 { 1536 acpi_status status; 1537 1538 acpi_ec_start(ec, false); 1539 1540 if (!test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) { 1541 acpi_handle scope_handle = ec == first_ec ? ACPI_ROOT_OBJECT : ec->handle; 1542 1543 acpi_ec_enter_noirq(ec); 1544 status = acpi_install_address_space_handler_no_reg(scope_handle, 1545 ACPI_ADR_SPACE_EC, 1546 &acpi_ec_space_handler, 1547 NULL, ec); 1548 if (ACPI_FAILURE(status)) { 1549 acpi_ec_stop(ec, false); 1550 return -ENODEV; 1551 } 1552 set_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags); 1553 } 1554 1555 if (call_reg && !test_bit(EC_FLAGS_EC_REG_CALLED, &ec->flags)) { 1556 acpi_execute_reg_methods(ec->handle, ACPI_UINT32_MAX, ACPI_ADR_SPACE_EC); 1557 set_bit(EC_FLAGS_EC_REG_CALLED, &ec->flags); 1558 } 1559 1560 if (!device) 1561 return 0; 1562 1563 if (ec->gpe < 0) { 1564 /* ACPI reduced hardware platforms use a GpioInt from _CRS. */ 1565 int irq = acpi_dev_gpio_irq_get(device, 0); 1566 /* 1567 * Bail out right away for deferred probing or complete the 1568 * initialization regardless of any other errors. 1569 */ 1570 if (irq == -EPROBE_DEFER) 1571 return -EPROBE_DEFER; 1572 else if (irq >= 0) 1573 ec->irq = irq; 1574 } 1575 1576 if (!test_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags)) { 1577 /* Find and register all query methods */ 1578 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1, 1579 acpi_ec_register_query_methods, 1580 NULL, ec, NULL); 1581 set_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags); 1582 } 1583 if (!test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) { 1584 bool ready = false; 1585 1586 if (ec->gpe >= 0) 1587 ready = install_gpe_event_handler(ec); 1588 else if (ec->irq >= 0) 1589 ready = install_gpio_irq_event_handler(ec); 1590 1591 if (ready) { 1592 set_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags); 1593 acpi_ec_leave_noirq(ec); 1594 } 1595 /* 1596 * Failures to install an event handler are not fatal, because 1597 * the EC can be polled for events. 1598 */ 1599 } 1600 /* EC is fully operational, allow queries */ 1601 acpi_ec_enable_event(ec); 1602 1603 return 0; 1604 } 1605 1606 static void ec_remove_handlers(struct acpi_ec *ec) 1607 { 1608 acpi_handle scope_handle = ec == first_ec ? ACPI_ROOT_OBJECT : ec->handle; 1609 1610 if (test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) { 1611 if (ACPI_FAILURE(acpi_remove_address_space_handler( 1612 scope_handle, 1613 ACPI_ADR_SPACE_EC, 1614 &acpi_ec_space_handler))) 1615 pr_err("failed to remove space handler\n"); 1616 clear_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags); 1617 } 1618 1619 /* 1620 * Stops handling the EC transactions after removing the operation 1621 * region handler. This is required because _REG(DISCONNECT) 1622 * invoked during the removal can result in new EC transactions. 1623 * 1624 * Flushes the EC requests and thus disables the GPE before 1625 * removing the GPE handler. This is required by the current ACPICA 1626 * GPE core. ACPICA GPE core will automatically disable a GPE when 1627 * it is indicated but there is no way to handle it. So the drivers 1628 * must disable the GPEs prior to removing the GPE handlers. 1629 */ 1630 acpi_ec_stop(ec, false); 1631 1632 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) { 1633 if (ec->gpe >= 0 && 1634 ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe, 1635 &acpi_ec_gpe_handler))) 1636 pr_err("failed to remove gpe handler\n"); 1637 1638 if (ec->irq >= 0) 1639 free_irq(ec->irq, ec); 1640 1641 clear_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags); 1642 } 1643 if (test_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags)) { 1644 acpi_ec_remove_query_handlers(ec, true, 0); 1645 clear_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags); 1646 } 1647 } 1648 1649 static int acpi_ec_setup(struct acpi_ec *ec, struct acpi_device *device, bool call_reg) 1650 { 1651 int ret; 1652 1653 /* First EC capable of handling transactions */ 1654 if (!first_ec) 1655 first_ec = ec; 1656 1657 ret = ec_install_handlers(ec, device, call_reg); 1658 if (ret) { 1659 if (ec == first_ec) 1660 first_ec = NULL; 1661 1662 return ret; 1663 } 1664 1665 pr_info("EC_CMD/EC_SC=0x%lx, EC_DATA=0x%lx\n", ec->command_addr, 1666 ec->data_addr); 1667 1668 if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) { 1669 if (ec->gpe >= 0) 1670 pr_info("GPE=0x%x\n", ec->gpe); 1671 else 1672 pr_info("IRQ=%d\n", ec->irq); 1673 } 1674 1675 return ret; 1676 } 1677 1678 static int acpi_ec_probe(struct platform_device *pdev) 1679 { 1680 struct acpi_device *device = ACPI_COMPANION(&pdev->dev); 1681 struct acpi_ec *ec; 1682 int ret; 1683 1684 strscpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME); 1685 strscpy(acpi_device_class(device), ACPI_EC_CLASS); 1686 1687 if (boot_ec && (boot_ec->handle == device->handle || 1688 !strcmp(acpi_device_hid(device), ACPI_ECDT_HID))) { 1689 /* Fast path: this device corresponds to the boot EC. */ 1690 ec = boot_ec; 1691 } else { 1692 acpi_status status; 1693 1694 ec = acpi_ec_alloc(); 1695 if (!ec) 1696 return -ENOMEM; 1697 1698 status = ec_parse_device(device->handle, 0, ec, NULL); 1699 if (status != AE_CTRL_TERMINATE) { 1700 ret = -EINVAL; 1701 goto err; 1702 } 1703 1704 if (boot_ec && ec->command_addr == boot_ec->command_addr && 1705 ec->data_addr == boot_ec->data_addr) { 1706 /* 1707 * Trust PNP0C09 namespace location rather than ECDT ID. 1708 * But trust ECDT GPE rather than _GPE because of ASUS 1709 * quirks. So do not change boot_ec->gpe to ec->gpe, 1710 * except when the TRUST_DSDT_GPE quirk is set. 1711 */ 1712 boot_ec->handle = ec->handle; 1713 1714 if (EC_FLAGS_TRUST_DSDT_GPE) 1715 boot_ec->gpe = ec->gpe; 1716 1717 acpi_handle_debug(ec->handle, "duplicated.\n"); 1718 acpi_ec_free(ec); 1719 ec = boot_ec; 1720 } 1721 } 1722 1723 ret = acpi_ec_setup(ec, device, true); 1724 if (ret) 1725 goto err; 1726 1727 if (ec == boot_ec) 1728 acpi_handle_info(boot_ec->handle, 1729 "Boot %s EC initialization complete\n", 1730 boot_ec_is_ecdt ? "ECDT" : "DSDT"); 1731 1732 acpi_handle_info(ec->handle, 1733 "EC: Used to handle transactions and events\n"); 1734 1735 platform_set_drvdata(pdev, ec); 1736 1737 ret = !!request_region(ec->data_addr, 1, "EC data"); 1738 WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr); 1739 ret = !!request_region(ec->command_addr, 1, "EC cmd"); 1740 WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr); 1741 1742 /* Reprobe devices depending on the EC */ 1743 acpi_dev_clear_dependencies(device); 1744 1745 acpi_handle_debug(ec->handle, "enumerated.\n"); 1746 return 0; 1747 1748 err: 1749 if (ec != boot_ec) 1750 acpi_ec_free(ec); 1751 1752 return ret; 1753 } 1754 1755 static void acpi_ec_remove(struct platform_device *pdev) 1756 { 1757 struct acpi_ec *ec = platform_get_drvdata(pdev); 1758 1759 release_region(ec->data_addr, 1); 1760 release_region(ec->command_addr, 1); 1761 if (ec != boot_ec) { 1762 ec_remove_handlers(ec); 1763 acpi_ec_free(ec); 1764 } 1765 } 1766 1767 void acpi_ec_register_opregions(struct acpi_device *adev) 1768 { 1769 if (first_ec && first_ec->handle != adev->handle) 1770 acpi_execute_reg_methods(adev->handle, 1, ACPI_ADR_SPACE_EC); 1771 } 1772 1773 static acpi_status 1774 ec_parse_io_ports(struct acpi_resource *resource, void *context) 1775 { 1776 struct acpi_ec *ec = context; 1777 1778 if (resource->type != ACPI_RESOURCE_TYPE_IO) 1779 return AE_OK; 1780 1781 /* 1782 * The first address region returned is the data port, and 1783 * the second address region returned is the status/command 1784 * port. 1785 */ 1786 if (ec->data_addr == 0) 1787 ec->data_addr = resource->data.io.minimum; 1788 else if (ec->command_addr == 0) 1789 ec->command_addr = resource->data.io.minimum; 1790 else 1791 return AE_CTRL_TERMINATE; 1792 1793 return AE_OK; 1794 } 1795 1796 static const struct acpi_device_id ec_device_ids[] = { 1797 {"PNP0C09", 0}, 1798 {ACPI_ECDT_HID, 0}, 1799 {"", 0}, 1800 }; 1801 1802 /* 1803 * This function is not Windows-compatible as Windows never enumerates the 1804 * namespace EC before the main ACPI device enumeration process. It is 1805 * retained for historical reason and will be deprecated in the future. 1806 */ 1807 void __init acpi_ec_dsdt_probe(void) 1808 { 1809 struct acpi_ec *ec; 1810 acpi_status status; 1811 int ret; 1812 1813 /* 1814 * If a platform has ECDT, there is no need to proceed as the 1815 * following probe is not a part of the ACPI device enumeration, 1816 * executing _STA is not safe, and thus this probe may risk of 1817 * picking up an invalid EC device. 1818 */ 1819 if (boot_ec) 1820 return; 1821 1822 ec = acpi_ec_alloc(); 1823 if (!ec) 1824 return; 1825 1826 /* 1827 * At this point, the namespace is initialized, so start to find 1828 * the namespace objects. 1829 */ 1830 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device, ec, NULL); 1831 if (ACPI_FAILURE(status) || !ec->handle) { 1832 acpi_ec_free(ec); 1833 return; 1834 } 1835 1836 /* 1837 * When the DSDT EC is available, always re-configure boot EC to 1838 * have _REG evaluated. _REG can only be evaluated after the 1839 * namespace initialization. 1840 * At this point, the GPE is not fully initialized, so do not to 1841 * handle the events. 1842 */ 1843 ret = acpi_ec_setup(ec, NULL, true); 1844 if (ret) { 1845 acpi_ec_free(ec); 1846 return; 1847 } 1848 1849 boot_ec = ec; 1850 1851 acpi_handle_info(ec->handle, 1852 "Boot DSDT EC used to handle transactions\n"); 1853 } 1854 1855 /* 1856 * acpi_ec_ecdt_start - Finalize the boot ECDT EC initialization. 1857 * 1858 * First, look for an ACPI handle for the boot ECDT EC if acpi_ec_add() has not 1859 * found a matching object in the namespace. 1860 * 1861 * Next, in case the DSDT EC is not functioning, it is still necessary to 1862 * provide a functional ECDT EC to handle events, so add an extra device object 1863 * to represent it (see https://bugzilla.kernel.org/show_bug.cgi?id=115021). 1864 * 1865 * This is useful on platforms with valid ECDT and invalid DSDT EC settings, 1866 * like ASUS X550ZE (see https://bugzilla.kernel.org/show_bug.cgi?id=196847). 1867 */ 1868 static void __init acpi_ec_ecdt_start(void) 1869 { 1870 struct acpi_table_ecdt *ecdt_ptr; 1871 acpi_handle handle; 1872 acpi_status status; 1873 1874 /* Bail out if a matching EC has been found in the namespace. */ 1875 if (!boot_ec || boot_ec->handle != ACPI_ROOT_OBJECT) 1876 return; 1877 1878 /* Look up the object pointed to from the ECDT in the namespace. */ 1879 status = acpi_get_table(ACPI_SIG_ECDT, 1, 1880 (struct acpi_table_header **)&ecdt_ptr); 1881 if (ACPI_FAILURE(status)) 1882 return; 1883 1884 status = acpi_get_handle(NULL, ecdt_ptr->id, &handle); 1885 if (ACPI_SUCCESS(status)) { 1886 boot_ec->handle = handle; 1887 1888 /* Add a special ACPI device object to represent the boot EC. */ 1889 acpi_bus_register_early_device(ACPI_BUS_TYPE_ECDT_EC); 1890 } 1891 1892 acpi_put_table((struct acpi_table_header *)ecdt_ptr); 1893 } 1894 1895 /* 1896 * On some hardware it is necessary to clear events accumulated by the EC during 1897 * sleep. These ECs stop reporting GPEs until they are manually polled, if too 1898 * many events are accumulated. (e.g. Samsung Series 5/9 notebooks) 1899 * 1900 * https://bugzilla.kernel.org/show_bug.cgi?id=44161 1901 * 1902 * Ideally, the EC should also be instructed NOT to accumulate events during 1903 * sleep (which Windows seems to do somehow), but the interface to control this 1904 * behaviour is not known at this time. 1905 * 1906 * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx, 1907 * however it is very likely that other Samsung models are affected. 1908 * 1909 * On systems which don't accumulate _Q events during sleep, this extra check 1910 * should be harmless. 1911 */ 1912 static int ec_clear_on_resume(const struct dmi_system_id *id) 1913 { 1914 pr_debug("Detected system needing EC poll on resume.\n"); 1915 EC_FLAGS_CLEAR_ON_RESUME = 1; 1916 ec_event_clearing = ACPI_EC_EVT_TIMING_STATUS; 1917 return 0; 1918 } 1919 1920 /* 1921 * Some ECDTs contain wrong register addresses. 1922 * MSI MS-171F 1923 * https://bugzilla.kernel.org/show_bug.cgi?id=12461 1924 */ 1925 static int ec_correct_ecdt(const struct dmi_system_id *id) 1926 { 1927 pr_debug("Detected system needing ECDT address correction.\n"); 1928 EC_FLAGS_CORRECT_ECDT = 1; 1929 return 0; 1930 } 1931 1932 /* 1933 * Some ECDTs contain wrong GPE setting, but they share the same port addresses 1934 * with DSDT EC, don't duplicate the DSDT EC with ECDT EC in this case. 1935 * https://bugzilla.kernel.org/show_bug.cgi?id=209989 1936 */ 1937 static int ec_honor_dsdt_gpe(const struct dmi_system_id *id) 1938 { 1939 pr_debug("Detected system needing DSDT GPE setting.\n"); 1940 EC_FLAGS_TRUST_DSDT_GPE = 1; 1941 return 0; 1942 } 1943 1944 static const struct dmi_system_id ec_dmi_table[] __initconst = { 1945 { 1946 /* 1947 * MSI MS-171F 1948 * https://bugzilla.kernel.org/show_bug.cgi?id=12461 1949 */ 1950 .callback = ec_correct_ecdt, 1951 .matches = { 1952 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star"), 1953 DMI_MATCH(DMI_PRODUCT_NAME, "MS-171F"), 1954 }, 1955 }, 1956 { 1957 /* 1958 * HP Pavilion Gaming Laptop 15-cx0xxx 1959 * https://bugzilla.kernel.org/show_bug.cgi?id=209989 1960 */ 1961 .callback = ec_honor_dsdt_gpe, 1962 .matches = { 1963 DMI_MATCH(DMI_SYS_VENDOR, "HP"), 1964 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion Gaming Laptop 15-cx0xxx"), 1965 }, 1966 }, 1967 { 1968 /* 1969 * HP Pavilion Gaming Laptop 15-cx0041ur 1970 */ 1971 .callback = ec_honor_dsdt_gpe, 1972 .matches = { 1973 DMI_MATCH(DMI_SYS_VENDOR, "HP"), 1974 DMI_MATCH(DMI_PRODUCT_NAME, "HP 15-cx0041ur"), 1975 }, 1976 }, 1977 { 1978 /* 1979 * HP Pavilion Gaming Laptop 15-dk1xxx 1980 * https://github.com/systemd/systemd/issues/28942 1981 */ 1982 .callback = ec_honor_dsdt_gpe, 1983 .matches = { 1984 DMI_MATCH(DMI_SYS_VENDOR, "HP"), 1985 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion Gaming Laptop 15-dk1xxx"), 1986 }, 1987 }, 1988 { 1989 /* 1990 * HP 250 G7 Notebook PC 1991 */ 1992 .callback = ec_honor_dsdt_gpe, 1993 .matches = { 1994 DMI_MATCH(DMI_SYS_VENDOR, "HP"), 1995 DMI_MATCH(DMI_PRODUCT_NAME, "HP 250 G7 Notebook PC"), 1996 }, 1997 }, 1998 { 1999 /* 2000 * Samsung hardware 2001 * https://bugzilla.kernel.org/show_bug.cgi?id=44161 2002 */ 2003 .callback = ec_clear_on_resume, 2004 .matches = { 2005 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."), 2006 }, 2007 }, 2008 {} 2009 }; 2010 2011 void __init acpi_ec_ecdt_probe(void) 2012 { 2013 struct acpi_table_ecdt *ecdt_ptr; 2014 struct acpi_ec *ec; 2015 acpi_status status; 2016 int ret; 2017 2018 /* Generate a boot ec context. */ 2019 dmi_check_system(ec_dmi_table); 2020 status = acpi_get_table(ACPI_SIG_ECDT, 1, 2021 (struct acpi_table_header **)&ecdt_ptr); 2022 if (ACPI_FAILURE(status)) 2023 return; 2024 2025 if (!ecdt_ptr->control.address || !ecdt_ptr->data.address) { 2026 /* 2027 * Asus X50GL: 2028 * https://bugzilla.kernel.org/show_bug.cgi?id=11880 2029 */ 2030 goto out; 2031 } 2032 2033 if (!strlen(ecdt_ptr->id)) { 2034 /* 2035 * The ECDT table on some MSI notebooks contains invalid data, together 2036 * with an empty ID string (""). 2037 * 2038 * Section 5.2.15 of the ACPI specification requires the ID string to be 2039 * a "fully qualified reference to the (...) embedded controller device", 2040 * so this string always has to start with a backslash. 2041 * 2042 * However some ThinkBook machines have a ECDT table with a valid EC 2043 * description but an invalid ID string ("_SB.PC00.LPCB.EC0"). 2044 * 2045 * Because of this we only check if the ID string is empty in order to 2046 * avoid the obvious cases. 2047 */ 2048 pr_err(FW_BUG "Ignoring ECDT due to empty ID string\n"); 2049 goto out; 2050 } 2051 2052 ec = acpi_ec_alloc(); 2053 if (!ec) 2054 goto out; 2055 2056 if (EC_FLAGS_CORRECT_ECDT) { 2057 ec->command_addr = ecdt_ptr->data.address; 2058 ec->data_addr = ecdt_ptr->control.address; 2059 } else { 2060 ec->command_addr = ecdt_ptr->control.address; 2061 ec->data_addr = ecdt_ptr->data.address; 2062 } 2063 2064 /* 2065 * Ignore the GPE value on Reduced Hardware platforms. 2066 * Some products have this set to an erroneous value. 2067 */ 2068 if (!acpi_gbl_reduced_hardware) 2069 ec->gpe = ecdt_ptr->gpe; 2070 2071 ec->handle = ACPI_ROOT_OBJECT; 2072 2073 /* 2074 * At this point, the namespace is not initialized, so do not find 2075 * the namespace objects, or handle the events. 2076 */ 2077 ret = acpi_ec_setup(ec, NULL, false); 2078 if (ret) { 2079 acpi_ec_free(ec); 2080 goto out; 2081 } 2082 2083 boot_ec = ec; 2084 boot_ec_is_ecdt = true; 2085 2086 pr_info("Boot ECDT EC used to handle transactions\n"); 2087 2088 out: 2089 acpi_put_table((struct acpi_table_header *)ecdt_ptr); 2090 } 2091 2092 #ifdef CONFIG_PM_SLEEP 2093 static int acpi_ec_suspend(struct device *dev) 2094 { 2095 struct acpi_ec *ec = dev_get_drvdata(dev); 2096 2097 if (!pm_suspend_no_platform() && ec_freeze_events) 2098 acpi_ec_disable_event(ec); 2099 return 0; 2100 } 2101 2102 static int acpi_ec_suspend_noirq(struct device *dev) 2103 { 2104 struct acpi_ec *ec = dev_get_drvdata(dev); 2105 2106 /* 2107 * The SCI handler doesn't run at this point, so the GPE can be 2108 * masked at the low level without side effects. 2109 */ 2110 if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) && 2111 ec->gpe >= 0 && ec->reference_count >= 1) 2112 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE); 2113 2114 acpi_ec_enter_noirq(ec); 2115 2116 return 0; 2117 } 2118 2119 static int acpi_ec_resume_noirq(struct device *dev) 2120 { 2121 struct acpi_ec *ec = dev_get_drvdata(dev); 2122 2123 acpi_ec_leave_noirq(ec); 2124 2125 if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) && 2126 ec->gpe >= 0 && ec->reference_count >= 1) 2127 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE); 2128 2129 return 0; 2130 } 2131 2132 static int acpi_ec_resume(struct device *dev) 2133 { 2134 struct acpi_ec *ec = dev_get_drvdata(dev); 2135 2136 acpi_ec_enable_event(ec); 2137 return 0; 2138 } 2139 2140 void acpi_ec_mark_gpe_for_wake(void) 2141 { 2142 if (first_ec && !ec_no_wakeup) 2143 acpi_mark_gpe_for_wake(NULL, first_ec->gpe); 2144 } 2145 EXPORT_SYMBOL_GPL(acpi_ec_mark_gpe_for_wake); 2146 2147 void acpi_ec_set_gpe_wake_mask(u8 action) 2148 { 2149 if (pm_suspend_no_platform() && first_ec && !ec_no_wakeup) 2150 acpi_set_gpe_wake_mask(NULL, first_ec->gpe, action); 2151 } 2152 2153 static bool acpi_ec_work_in_progress(struct acpi_ec *ec) 2154 { 2155 return ec->events_in_progress + ec->queries_in_progress > 0; 2156 } 2157 2158 bool acpi_ec_dispatch_gpe(void) 2159 { 2160 bool work_in_progress = false; 2161 2162 if (!first_ec) 2163 return acpi_any_gpe_status_set(U32_MAX); 2164 2165 /* 2166 * Report wakeup if the status bit is set for any enabled GPE other 2167 * than the EC one. 2168 */ 2169 if (acpi_any_gpe_status_set(first_ec->gpe)) 2170 return true; 2171 2172 /* 2173 * Cancel the SCI wakeup and process all pending events in case there 2174 * are any wakeup ones in there. 2175 * 2176 * Note that if any non-EC GPEs are active at this point, the SCI will 2177 * retrigger after the rearming in acpi_s2idle_wake(), so no events 2178 * should be missed by canceling the wakeup here. 2179 */ 2180 pm_system_cancel_wakeup(); 2181 2182 /* 2183 * Dispatch the EC GPE in-band, but do not report wakeup in any case 2184 * to allow the caller to process events properly after that. 2185 */ 2186 spin_lock_irq(&first_ec->lock); 2187 2188 if (acpi_ec_gpe_status_set(first_ec)) { 2189 pm_pr_dbg("ACPI EC GPE status set\n"); 2190 2191 clear_gpe_and_advance_transaction(first_ec, false); 2192 work_in_progress = acpi_ec_work_in_progress(first_ec); 2193 } 2194 2195 spin_unlock_irq(&first_ec->lock); 2196 2197 if (!work_in_progress) 2198 return false; 2199 2200 pm_pr_dbg("ACPI EC GPE dispatched\n"); 2201 2202 /* Drain EC work. */ 2203 do { 2204 acpi_ec_flush_work(); 2205 2206 pm_pr_dbg("ACPI EC work flushed\n"); 2207 2208 spin_lock_irq(&first_ec->lock); 2209 2210 work_in_progress = acpi_ec_work_in_progress(first_ec); 2211 2212 spin_unlock_irq(&first_ec->lock); 2213 } while (work_in_progress && !pm_wakeup_pending()); 2214 2215 return false; 2216 } 2217 #endif /* CONFIG_PM_SLEEP */ 2218 2219 static const struct dev_pm_ops acpi_ec_pm = { 2220 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend_noirq, acpi_ec_resume_noirq) 2221 SET_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend, acpi_ec_resume) 2222 }; 2223 2224 static int param_set_event_clearing(const char *val, 2225 const struct kernel_param *kp) 2226 { 2227 int result = 0; 2228 2229 if (!strncmp(val, "status", sizeof("status") - 1)) { 2230 ec_event_clearing = ACPI_EC_EVT_TIMING_STATUS; 2231 pr_info("Assuming SCI_EVT clearing on EC_SC accesses\n"); 2232 } else if (!strncmp(val, "query", sizeof("query") - 1)) { 2233 ec_event_clearing = ACPI_EC_EVT_TIMING_QUERY; 2234 pr_info("Assuming SCI_EVT clearing on QR_EC writes\n"); 2235 } else if (!strncmp(val, "event", sizeof("event") - 1)) { 2236 ec_event_clearing = ACPI_EC_EVT_TIMING_EVENT; 2237 pr_info("Assuming SCI_EVT clearing on event reads\n"); 2238 } else 2239 result = -EINVAL; 2240 return result; 2241 } 2242 2243 static int param_get_event_clearing(char *buffer, 2244 const struct kernel_param *kp) 2245 { 2246 switch (ec_event_clearing) { 2247 case ACPI_EC_EVT_TIMING_STATUS: 2248 return sprintf(buffer, "status\n"); 2249 case ACPI_EC_EVT_TIMING_QUERY: 2250 return sprintf(buffer, "query\n"); 2251 case ACPI_EC_EVT_TIMING_EVENT: 2252 return sprintf(buffer, "event\n"); 2253 default: 2254 return sprintf(buffer, "invalid\n"); 2255 } 2256 return 0; 2257 } 2258 2259 module_param_call(ec_event_clearing, param_set_event_clearing, param_get_event_clearing, 2260 NULL, 0644); 2261 MODULE_PARM_DESC(ec_event_clearing, "Assumed SCI_EVT clearing timing"); 2262 2263 static struct platform_driver acpi_ec_driver = { 2264 .probe = acpi_ec_probe, 2265 .remove = acpi_ec_remove, 2266 .driver = { 2267 .name = "acpi-ec", 2268 .acpi_match_table = ec_device_ids, 2269 .pm = &acpi_ec_pm, 2270 }, 2271 }; 2272 2273 static void acpi_ec_destroy_workqueues(void) 2274 { 2275 if (ec_wq) { 2276 destroy_workqueue(ec_wq); 2277 ec_wq = NULL; 2278 } 2279 if (ec_query_wq) { 2280 destroy_workqueue(ec_query_wq); 2281 ec_query_wq = NULL; 2282 } 2283 } 2284 2285 static int acpi_ec_init_workqueues(void) 2286 { 2287 if (!ec_wq) 2288 ec_wq = alloc_ordered_workqueue("kec", 0); 2289 2290 if (!ec_query_wq) 2291 ec_query_wq = alloc_workqueue("kec_query", WQ_PERCPU, 2292 ec_max_queries); 2293 2294 if (!ec_wq || !ec_query_wq) { 2295 acpi_ec_destroy_workqueues(); 2296 return -ENODEV; 2297 } 2298 return 0; 2299 } 2300 2301 static const struct dmi_system_id acpi_ec_no_wakeup[] = { 2302 { 2303 .matches = { 2304 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 2305 DMI_MATCH(DMI_PRODUCT_FAMILY, "Thinkpad X1 Carbon 6th"), 2306 }, 2307 }, 2308 { 2309 .matches = { 2310 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 2311 DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Yoga 3rd"), 2312 }, 2313 }, 2314 { 2315 .matches = { 2316 DMI_MATCH(DMI_SYS_VENDOR, "HP"), 2317 DMI_MATCH(DMI_PRODUCT_FAMILY, "103C_5336AN HP ZHAN 66 Pro"), 2318 }, 2319 }, 2320 /* 2321 * Lenovo Legion Go S; touchscreen blocks HW sleep when woken up from EC 2322 * https://gitlab.freedesktop.org/drm/amd/-/issues/3929 2323 */ 2324 { 2325 .matches = { 2326 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"), 2327 DMI_MATCH(DMI_PRODUCT_NAME, "83L3"), 2328 } 2329 }, 2330 { 2331 .matches = { 2332 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"), 2333 DMI_MATCH(DMI_PRODUCT_NAME, "83N6"), 2334 } 2335 }, 2336 { 2337 .matches = { 2338 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"), 2339 DMI_MATCH(DMI_PRODUCT_NAME, "83Q2"), 2340 } 2341 }, 2342 { 2343 .matches = { 2344 DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"), 2345 DMI_MATCH(DMI_PRODUCT_NAME, "83Q3"), 2346 } 2347 }, 2348 { 2349 // TUXEDO InfinityBook Pro AMD Gen9 2350 .matches = { 2351 DMI_MATCH(DMI_BOARD_NAME, "GXxHRXx"), 2352 }, 2353 }, 2354 { }, 2355 }; 2356 2357 void __init acpi_ec_init(void) 2358 { 2359 int result; 2360 2361 result = acpi_ec_init_workqueues(); 2362 if (result) 2363 return; 2364 2365 /* 2366 * Disable EC wakeup on following systems to prevent periodic 2367 * wakeup from EC GPE. 2368 */ 2369 if (dmi_check_system(acpi_ec_no_wakeup)) { 2370 ec_no_wakeup = true; 2371 pr_debug("Disabling EC wakeup on suspend-to-idle\n"); 2372 } 2373 2374 /* Driver must be registered after acpi_ec_init_workqueues(). */ 2375 platform_driver_register(&acpi_ec_driver); 2376 2377 acpi_ec_ecdt_start(); 2378 } 2379