2 * ec.c - ACPI Embedded Controller Driver (v3)
4 * Copyright (C) 2001-2015 Intel Corporation
5 * Author: 2014, 2015 Lv Zheng <lv.zheng@intel.com>
6 * 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
7 * 2006 Denis Sadykov <denis.m.sadykov@intel.com>
8 * 2004 Luming Yu <luming.yu@intel.com>
9 * 2001, 2002 Andy Grover <andrew.grover@intel.com>
10 * 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
11 * Copyright (C) 2008 Alexey Starikovskiy <astarikovskiy@suse.de>
13 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or (at
18 * your option) any later version.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32 /* Uncomment next line to get verbose printout */
35 #define pr_fmt(fmt) "ACPI : EC: " fmt
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/delay.h>
42 #include <linux/interrupt.h>
43 #include <linux/list.h>
44 #include <linux/spinlock.h>
45 #include <linux/slab.h>
46 #include <linux/acpi.h>
47 #include <linux/dmi.h>
52 #define ACPI_EC_CLASS "embedded_controller"
53 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
54 #define ACPI_EC_FILE_INFO "info"
56 /* EC status register */
57 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
58 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
59 #define ACPI_EC_FLAG_CMD 0x08 /* Input buffer contains a command */
60 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
61 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
65 ACPI_EC_COMMAND_READ = 0x80,
66 ACPI_EC_COMMAND_WRITE = 0x81,
67 ACPI_EC_BURST_ENABLE = 0x82,
68 ACPI_EC_BURST_DISABLE = 0x83,
69 ACPI_EC_COMMAND_QUERY = 0x84,
72 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
73 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
74 #define ACPI_EC_MSI_UDELAY 550 /* Wait 550us for MSI EC */
75 #define ACPI_EC_UDELAY_POLL 1000 /* Wait 1ms for EC transaction polling */
76 #define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query
77 * when trying to clear the EC */
80 EC_FLAGS_EVENT_ENABLED, /* Event is enabled */
81 EC_FLAGS_EVENT_PENDING, /* Event is pending */
82 EC_FLAGS_EVENT_DETECTED, /* Event is detected */
83 EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and
84 * OpReg are installed */
85 EC_FLAGS_STARTED, /* Driver is started */
86 EC_FLAGS_STOPPED, /* Driver is stopped */
87 EC_FLAGS_COMMAND_STORM, /* GPE storms occurred to the
88 * current command processing */
91 #define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */
92 #define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */
94 #define ec_debug_ref(ec, fmt, ...) \
97 pr_debug("%lu: " fmt, ec->reference_count, \
101 /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
102 static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
103 module_param(ec_delay, uint, 0644);
104 MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
107 * If the number of false interrupts per one transaction exceeds
108 * this threshold, will think there is a GPE storm happened and
109 * will disable the GPE for normal transaction.
111 static unsigned int ec_storm_threshold __read_mostly = 8;
112 module_param(ec_storm_threshold, uint, 0644);
113 MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
115 struct acpi_ec_query_handler {
116 struct list_head node;
117 acpi_ec_query_func func;
127 unsigned short irq_count;
134 unsigned long timestamp;
137 static int acpi_ec_query(struct acpi_ec *ec, u8 *data);
138 static void advance_transaction(struct acpi_ec *ec);
140 struct acpi_ec *boot_ec, *first_ec;
141 EXPORT_SYMBOL(first_ec);
143 static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
144 static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
145 static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
146 static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */
147 static int EC_FLAGS_QUERY_HANDSHAKE; /* Needs QR_EC issued when SCI_EVT set */
149 /* --------------------------------------------------------------------------
151 * -------------------------------------------------------------------------- */
153 static bool acpi_ec_started(struct acpi_ec *ec)
155 return test_bit(EC_FLAGS_STARTED, &ec->flags) &&
156 !test_bit(EC_FLAGS_STOPPED, &ec->flags);
159 static bool acpi_ec_flushed(struct acpi_ec *ec)
161 return ec->reference_count == 1;
164 static bool acpi_ec_has_pending_event(struct acpi_ec *ec)
166 return test_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags) ||
167 test_bit(EC_FLAGS_EVENT_PENDING, &ec->flags);
170 /* --------------------------------------------------------------------------
172 * -------------------------------------------------------------------------- */
174 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
176 u8 x = inb(ec->command_addr);
178 pr_debug("EC_SC(R) = 0x%2.2x "
179 "SCI_EVT=%d BURST=%d CMD=%d IBF=%d OBF=%d\n",
181 !!(x & ACPI_EC_FLAG_SCI),
182 !!(x & ACPI_EC_FLAG_BURST),
183 !!(x & ACPI_EC_FLAG_CMD),
184 !!(x & ACPI_EC_FLAG_IBF),
185 !!(x & ACPI_EC_FLAG_OBF));
189 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
191 u8 x = inb(ec->data_addr);
193 ec->curr->timestamp = jiffies;
194 pr_debug("EC_DATA(R) = 0x%2.2x\n", x);
198 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
200 pr_debug("EC_SC(W) = 0x%2.2x\n", command);
201 outb(command, ec->command_addr);
202 ec->curr->timestamp = jiffies;
205 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
207 pr_debug("EC_DATA(W) = 0x%2.2x\n", data);
208 outb(data, ec->data_addr);
209 ec->curr->timestamp = jiffies;
213 static const char *acpi_ec_cmd_string(u8 cmd)
230 #define acpi_ec_cmd_string(cmd) "UNDEF"
233 /* --------------------------------------------------------------------------
235 * -------------------------------------------------------------------------- */
237 static inline bool acpi_ec_is_gpe_raised(struct acpi_ec *ec)
239 acpi_event_status gpe_status = 0;
241 (void)acpi_get_gpe_status(NULL, ec->gpe, &gpe_status);
242 return (gpe_status & ACPI_EVENT_FLAG_SET) ? true : false;
245 static inline void acpi_ec_enable_gpe(struct acpi_ec *ec, bool open)
248 acpi_enable_gpe(NULL, ec->gpe);
250 BUG_ON(ec->reference_count < 1);
251 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
253 if (acpi_ec_is_gpe_raised(ec)) {
255 * On some platforms, EN=1 writes cannot trigger GPE. So
256 * software need to manually trigger a pseudo GPE event on
259 pr_debug("***** Polling quirk *****\n");
260 advance_transaction(ec);
264 static inline void acpi_ec_disable_gpe(struct acpi_ec *ec, bool close)
267 acpi_disable_gpe(NULL, ec->gpe);
269 BUG_ON(ec->reference_count < 1);
270 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
274 static inline void acpi_ec_clear_gpe(struct acpi_ec *ec)
277 * GPE STS is a W1C register, which means:
278 * 1. Software can clear it without worrying about clearing other
279 * GPEs' STS bits when the hardware sets them in parallel.
280 * 2. As long as software can ensure only clearing it when it is
281 * set, hardware won't set it in parallel.
282 * So software can clear GPE in any contexts.
283 * Warning: do not move the check into advance_transaction() as the
284 * EC commands will be sent without GPE raised.
286 if (!acpi_ec_is_gpe_raised(ec))
288 acpi_clear_gpe(NULL, ec->gpe);
291 /* --------------------------------------------------------------------------
292 * Transaction Management
293 * -------------------------------------------------------------------------- */
295 static void acpi_ec_submit_request(struct acpi_ec *ec)
297 ec->reference_count++;
298 if (ec->reference_count == 1)
299 acpi_ec_enable_gpe(ec, true);
302 static void acpi_ec_complete_request(struct acpi_ec *ec)
304 bool flushed = false;
306 ec->reference_count--;
307 if (ec->reference_count == 0)
308 acpi_ec_disable_gpe(ec, true);
309 flushed = acpi_ec_flushed(ec);
314 static void acpi_ec_set_storm(struct acpi_ec *ec, u8 flag)
316 if (!test_bit(flag, &ec->flags)) {
317 acpi_ec_disable_gpe(ec, false);
318 pr_debug("+++++ Polling enabled +++++\n");
319 set_bit(flag, &ec->flags);
323 static void acpi_ec_clear_storm(struct acpi_ec *ec, u8 flag)
325 if (test_bit(flag, &ec->flags)) {
326 clear_bit(flag, &ec->flags);
327 acpi_ec_enable_gpe(ec, false);
328 pr_debug("+++++ Polling disabled +++++\n");
333 * acpi_ec_submit_flushable_request() - Increase the reference count unless
334 * the flush operation is not in
337 * @allow_event: whether event should be handled
339 * This function must be used before taking a new action that should hold
340 * the reference count. If this function returns false, then the action
341 * must be discarded or it will prevent the flush operation from being
344 * During flushing, QR_EC command need to pass this check when there is a
345 * pending event, so that the reference count held for the pending event
346 * can be decreased by the completion of the QR_EC command.
348 static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec,
351 if (!acpi_ec_started(ec)) {
352 if (!allow_event || !acpi_ec_has_pending_event(ec))
355 acpi_ec_submit_request(ec);
359 static void acpi_ec_submit_event(struct acpi_ec *ec)
361 if (!test_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags) ||
362 !test_bit(EC_FLAGS_EVENT_ENABLED, &ec->flags))
364 /* Hold reference for pending event */
365 if (!acpi_ec_submit_flushable_request(ec, true))
367 ec_debug_ref(ec, "Increase event\n");
368 if (!test_and_set_bit(EC_FLAGS_EVENT_PENDING, &ec->flags)) {
369 pr_debug("***** Event query started *****\n");
370 schedule_work(&ec->work);
373 acpi_ec_complete_request(ec);
374 ec_debug_ref(ec, "Decrease event\n");
377 static void acpi_ec_complete_event(struct acpi_ec *ec)
379 if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
380 clear_bit(EC_FLAGS_EVENT_PENDING, &ec->flags);
381 pr_debug("***** Event query stopped *****\n");
382 /* Unhold reference for pending event */
383 acpi_ec_complete_request(ec);
384 ec_debug_ref(ec, "Decrease event\n");
385 /* Check if there is another SCI_EVT detected */
386 acpi_ec_submit_event(ec);
390 static void acpi_ec_submit_detection(struct acpi_ec *ec)
392 /* Hold reference for query submission */
393 if (!acpi_ec_submit_flushable_request(ec, false))
395 ec_debug_ref(ec, "Increase query\n");
396 if (!test_and_set_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags)) {
397 pr_debug("***** Event detection blocked *****\n");
398 acpi_ec_submit_event(ec);
401 acpi_ec_complete_request(ec);
402 ec_debug_ref(ec, "Decrease query\n");
405 static void acpi_ec_complete_detection(struct acpi_ec *ec)
407 if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
408 clear_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags);
409 pr_debug("***** Event detetion unblocked *****\n");
410 /* Unhold reference for query submission */
411 acpi_ec_complete_request(ec);
412 ec_debug_ref(ec, "Decrease query\n");
416 static void acpi_ec_enable_event(struct acpi_ec *ec)
420 spin_lock_irqsave(&ec->lock, flags);
421 set_bit(EC_FLAGS_EVENT_ENABLED, &ec->flags);
423 * An event may be pending even with SCI_EVT=0, so QR_EC should
424 * always be issued right after started.
426 acpi_ec_submit_detection(ec);
427 spin_unlock_irqrestore(&ec->lock, flags);
430 static int ec_transaction_completed(struct acpi_ec *ec)
435 spin_lock_irqsave(&ec->lock, flags);
436 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
438 spin_unlock_irqrestore(&ec->lock, flags);
442 static void advance_transaction(struct acpi_ec *ec)
444 struct transaction *t;
448 pr_debug("===== %s (%d) =====\n",
449 in_interrupt() ? "IRQ" : "TASK", smp_processor_id());
451 * By always clearing STS before handling all indications, we can
452 * ensure a hardware STS 0->1 change after this clearing can always
453 * trigger a GPE interrupt.
455 acpi_ec_clear_gpe(ec);
456 status = acpi_ec_read_status(ec);
460 if (t->flags & ACPI_EC_COMMAND_POLL) {
461 if (t->wlen > t->wi) {
462 if ((status & ACPI_EC_FLAG_IBF) == 0)
463 acpi_ec_write_data(ec, t->wdata[t->wi++]);
466 } else if (t->rlen > t->ri) {
467 if ((status & ACPI_EC_FLAG_OBF) == 1) {
468 t->rdata[t->ri++] = acpi_ec_read_data(ec);
469 if (t->rlen == t->ri) {
470 t->flags |= ACPI_EC_COMMAND_COMPLETE;
471 acpi_ec_complete_event(ec);
472 if (t->command == ACPI_EC_COMMAND_QUERY)
473 pr_debug("***** Command(%s) hardware completion *****\n",
474 acpi_ec_cmd_string(t->command));
479 } else if (t->wlen == t->wi &&
480 (status & ACPI_EC_FLAG_IBF) == 0) {
481 t->flags |= ACPI_EC_COMMAND_COMPLETE;
482 acpi_ec_complete_event(ec);
487 if (EC_FLAGS_QUERY_HANDSHAKE &&
488 !(status & ACPI_EC_FLAG_SCI) &&
489 (t->command == ACPI_EC_COMMAND_QUERY)) {
490 t->flags |= ACPI_EC_COMMAND_POLL;
491 acpi_ec_complete_detection(ec);
492 t->rdata[t->ri++] = 0x00;
493 t->flags |= ACPI_EC_COMMAND_COMPLETE;
494 acpi_ec_complete_event(ec);
495 pr_debug("***** Command(%s) software completion *****\n",
496 acpi_ec_cmd_string(t->command));
498 } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
499 acpi_ec_write_cmd(ec, t->command);
500 t->flags |= ACPI_EC_COMMAND_POLL;
501 acpi_ec_complete_detection(ec);
508 * If SCI bit is set, then don't think it's a false IRQ
509 * otherwise will take a not handled IRQ as a false one.
511 if (!(status & ACPI_EC_FLAG_SCI)) {
512 if (in_interrupt() && t) {
513 if (t->irq_count < ec_storm_threshold)
515 /* Allow triggering on 0 threshold */
516 if (t->irq_count == ec_storm_threshold)
517 acpi_ec_set_storm(ec, EC_FLAGS_COMMAND_STORM);
521 if (status & ACPI_EC_FLAG_SCI)
522 acpi_ec_submit_detection(ec);
523 if (wakeup && in_interrupt())
527 static void start_transaction(struct acpi_ec *ec)
529 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
531 ec->curr->timestamp = jiffies;
532 advance_transaction(ec);
535 static int ec_poll(struct acpi_ec *ec)
538 int repeat = 5; /* number of command restarts */
541 unsigned long delay = jiffies +
542 msecs_to_jiffies(ec_delay);
543 unsigned long usecs = ACPI_EC_UDELAY_POLL;
545 /* don't sleep with disabled interrupts */
546 if (EC_FLAGS_MSI || irqs_disabled()) {
547 usecs = ACPI_EC_MSI_UDELAY;
549 if (ec_transaction_completed(ec))
552 if (wait_event_timeout(ec->wait,
553 ec_transaction_completed(ec),
554 usecs_to_jiffies(usecs)))
557 spin_lock_irqsave(&ec->lock, flags);
558 if (time_after(jiffies,
559 ec->curr->timestamp +
560 usecs_to_jiffies(usecs)))
561 advance_transaction(ec);
562 spin_unlock_irqrestore(&ec->lock, flags);
563 } while (time_before(jiffies, delay));
564 pr_debug("controller reset, restart transaction\n");
565 spin_lock_irqsave(&ec->lock, flags);
566 start_transaction(ec);
567 spin_unlock_irqrestore(&ec->lock, flags);
572 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
573 struct transaction *t)
579 udelay(ACPI_EC_MSI_UDELAY);
580 /* start transaction */
581 spin_lock_irqsave(&ec->lock, tmp);
582 /* Enable GPE for command processing (IBF=0/OBF=1) */
583 if (!acpi_ec_submit_flushable_request(ec, true)) {
587 ec_debug_ref(ec, "Increase command\n");
588 /* following two actions should be kept atomic */
590 pr_debug("***** Command(%s) started *****\n",
591 acpi_ec_cmd_string(t->command));
592 start_transaction(ec);
593 spin_unlock_irqrestore(&ec->lock, tmp);
595 spin_lock_irqsave(&ec->lock, tmp);
596 if (t->irq_count == ec_storm_threshold)
597 acpi_ec_clear_storm(ec, EC_FLAGS_COMMAND_STORM);
598 pr_debug("***** Command(%s) stopped *****\n",
599 acpi_ec_cmd_string(t->command));
601 /* Disable GPE for command processing (IBF=0/OBF=1) */
602 acpi_ec_complete_request(ec);
603 ec_debug_ref(ec, "Decrease command\n");
605 spin_unlock_irqrestore(&ec->lock, tmp);
609 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
614 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
617 memset(t->rdata, 0, t->rlen);
618 mutex_lock(&ec->mutex);
619 if (ec->global_lock) {
620 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
621 if (ACPI_FAILURE(status)) {
627 status = acpi_ec_transaction_unlocked(ec, t);
629 if (test_bit(EC_FLAGS_COMMAND_STORM, &ec->flags))
632 acpi_release_global_lock(glk);
634 mutex_unlock(&ec->mutex);
638 static int acpi_ec_burst_enable(struct acpi_ec *ec)
641 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
642 .wdata = NULL, .rdata = &d,
643 .wlen = 0, .rlen = 1};
645 return acpi_ec_transaction(ec, &t);
648 static int acpi_ec_burst_disable(struct acpi_ec *ec)
650 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
651 .wdata = NULL, .rdata = NULL,
652 .wlen = 0, .rlen = 0};
654 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
655 acpi_ec_transaction(ec, &t) : 0;
658 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
662 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
663 .wdata = &address, .rdata = &d,
664 .wlen = 1, .rlen = 1};
666 result = acpi_ec_transaction(ec, &t);
671 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
673 u8 wdata[2] = { address, data };
674 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
675 .wdata = wdata, .rdata = NULL,
676 .wlen = 2, .rlen = 0};
678 return acpi_ec_transaction(ec, &t);
681 int ec_read(u8 addr, u8 *val)
689 err = acpi_ec_read(first_ec, addr, &temp_data);
697 EXPORT_SYMBOL(ec_read);
699 int ec_write(u8 addr, u8 val)
706 err = acpi_ec_write(first_ec, addr, val);
710 EXPORT_SYMBOL(ec_write);
712 int ec_transaction(u8 command,
713 const u8 *wdata, unsigned wdata_len,
714 u8 *rdata, unsigned rdata_len)
716 struct transaction t = {.command = command,
717 .wdata = wdata, .rdata = rdata,
718 .wlen = wdata_len, .rlen = rdata_len};
723 return acpi_ec_transaction(first_ec, &t);
725 EXPORT_SYMBOL(ec_transaction);
727 /* Get the handle to the EC device */
728 acpi_handle ec_get_handle(void)
732 return first_ec->handle;
734 EXPORT_SYMBOL(ec_get_handle);
737 * Process _Q events that might have accumulated in the EC.
738 * Run with locked ec mutex.
740 static void acpi_ec_clear(struct acpi_ec *ec)
745 for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
746 status = acpi_ec_query(ec, &value);
747 if (status || !value)
751 if (unlikely(i == ACPI_EC_CLEAR_MAX))
752 pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
754 pr_info("%d stale EC events cleared\n", i);
757 static void acpi_ec_start(struct acpi_ec *ec, bool resuming)
761 spin_lock_irqsave(&ec->lock, flags);
762 if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) {
763 pr_debug("+++++ Starting EC +++++\n");
764 /* Enable GPE for event processing (SCI_EVT=1) */
766 acpi_ec_submit_request(ec);
767 ec_debug_ref(ec, "Increase driver\n");
769 pr_info("+++++ EC started +++++\n");
771 spin_unlock_irqrestore(&ec->lock, flags);
774 static bool acpi_ec_stopped(struct acpi_ec *ec)
779 spin_lock_irqsave(&ec->lock, flags);
780 flushed = acpi_ec_flushed(ec);
781 spin_unlock_irqrestore(&ec->lock, flags);
785 static void acpi_ec_stop(struct acpi_ec *ec, bool suspending)
789 spin_lock_irqsave(&ec->lock, flags);
790 if (acpi_ec_started(ec)) {
791 pr_debug("+++++ Stopping EC +++++\n");
792 set_bit(EC_FLAGS_STOPPED, &ec->flags);
793 spin_unlock_irqrestore(&ec->lock, flags);
794 wait_event(ec->wait, acpi_ec_stopped(ec));
795 spin_lock_irqsave(&ec->lock, flags);
796 /* Disable GPE for event processing (SCI_EVT=1) */
798 acpi_ec_complete_request(ec);
799 ec_debug_ref(ec, "Decrease driver\n");
801 clear_bit(EC_FLAGS_STARTED, &ec->flags);
802 clear_bit(EC_FLAGS_STOPPED, &ec->flags);
803 pr_info("+++++ EC stopped +++++\n");
805 spin_unlock_irqrestore(&ec->lock, flags);
808 void acpi_ec_block_transactions(void)
810 struct acpi_ec *ec = first_ec;
815 mutex_lock(&ec->mutex);
816 /* Prevent transactions from being carried out */
817 acpi_ec_stop(ec, true);
818 mutex_unlock(&ec->mutex);
821 void acpi_ec_unblock_transactions(void)
823 struct acpi_ec *ec = first_ec;
828 /* Allow transactions to be carried out again */
829 acpi_ec_start(ec, true);
831 if (EC_FLAGS_CLEAR_ON_RESUME)
835 void acpi_ec_unblock_transactions_early(void)
838 * Allow transactions to happen again (this function is called from
839 * atomic context during wakeup, so we don't need to acquire the mutex).
842 acpi_ec_start(first_ec, true);
845 /* --------------------------------------------------------------------------
847 -------------------------------------------------------------------------- */
848 static struct acpi_ec_query_handler *
849 acpi_ec_get_query_handler(struct acpi_ec_query_handler *handler)
852 kref_get(&handler->kref);
856 static void acpi_ec_query_handler_release(struct kref *kref)
858 struct acpi_ec_query_handler *handler =
859 container_of(kref, struct acpi_ec_query_handler, kref);
864 static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler)
866 kref_put(&handler->kref, acpi_ec_query_handler_release);
869 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
870 acpi_handle handle, acpi_ec_query_func func,
873 struct acpi_ec_query_handler *handler =
874 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
879 handler->query_bit = query_bit;
880 handler->handle = handle;
881 handler->func = func;
882 handler->data = data;
883 mutex_lock(&ec->mutex);
884 kref_init(&handler->kref);
885 list_add(&handler->node, &ec->list);
886 mutex_unlock(&ec->mutex);
889 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
891 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
893 struct acpi_ec_query_handler *handler, *tmp;
894 LIST_HEAD(free_list);
896 mutex_lock(&ec->mutex);
897 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
898 if (query_bit == handler->query_bit) {
899 list_del_init(&handler->node);
900 list_add(&handler->node, &free_list);
903 mutex_unlock(&ec->mutex);
904 list_for_each_entry(handler, &free_list, node)
905 acpi_ec_put_query_handler(handler);
907 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
909 static void acpi_ec_run(void *cxt)
911 struct acpi_ec_query_handler *handler = cxt;
915 pr_debug("##### Query(0x%02x) started #####\n", handler->query_bit);
917 handler->func(handler->data);
918 else if (handler->handle)
919 acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
920 pr_debug("##### Query(0x%02x) stopped #####\n", handler->query_bit);
921 acpi_ec_put_query_handler(handler);
924 static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
929 struct acpi_ec_query_handler *handler;
930 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
931 .wdata = NULL, .rdata = &value,
932 .wlen = 0, .rlen = 1};
935 * Query the EC to find out which _Qxx method we need to evaluate.
936 * Note that successful completion of the query causes the ACPI_EC_SCI
937 * bit to be cleared (and thus clearing the interrupt source).
939 result = acpi_ec_transaction(ec, &t);
947 mutex_lock(&ec->mutex);
948 list_for_each_entry(handler, &ec->list, node) {
949 if (value == handler->query_bit) {
950 /* have custom handler for this bit */
951 handler = acpi_ec_get_query_handler(handler);
952 pr_debug("##### Query(0x%02x) scheduled #####\n",
954 status = acpi_os_execute((handler->func) ?
955 OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
956 acpi_ec_run, handler);
957 if (ACPI_FAILURE(status))
962 mutex_unlock(&ec->mutex);
966 static void acpi_ec_gpe_poller(struct work_struct *work)
968 struct acpi_ec *ec = container_of(work, struct acpi_ec, work);
970 pr_debug("***** Event poller started *****\n");
971 acpi_ec_query(ec, NULL);
972 pr_debug("***** Event poller stopped *****\n");
975 static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
976 u32 gpe_number, void *data)
979 struct acpi_ec *ec = data;
981 spin_lock_irqsave(&ec->lock, flags);
982 advance_transaction(ec);
983 spin_unlock_irqrestore(&ec->lock, flags);
984 return ACPI_INTERRUPT_HANDLED;
987 /* --------------------------------------------------------------------------
988 * Address Space Management
989 * -------------------------------------------------------------------------- */
992 acpi_ec_space_handler(u32 function, acpi_physical_address address,
993 u32 bits, u64 *value64,
994 void *handler_context, void *region_context)
996 struct acpi_ec *ec = handler_context;
997 int result = 0, i, bytes = bits / 8;
998 u8 *value = (u8 *)value64;
1000 if ((address > 0xFF) || !value || !handler_context)
1001 return AE_BAD_PARAMETER;
1003 if (function != ACPI_READ && function != ACPI_WRITE)
1004 return AE_BAD_PARAMETER;
1006 if (EC_FLAGS_MSI || bits > 8)
1007 acpi_ec_burst_enable(ec);
1009 for (i = 0; i < bytes; ++i, ++address, ++value)
1010 result = (function == ACPI_READ) ?
1011 acpi_ec_read(ec, address, value) :
1012 acpi_ec_write(ec, address, *value);
1014 if (EC_FLAGS_MSI || bits > 8)
1015 acpi_ec_burst_disable(ec);
1019 return AE_BAD_PARAMETER;
1021 return AE_NOT_FOUND;
1029 /* --------------------------------------------------------------------------
1031 * -------------------------------------------------------------------------- */
1034 ec_parse_io_ports(struct acpi_resource *resource, void *context);
1036 static struct acpi_ec *make_acpi_ec(void)
1038 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1042 mutex_init(&ec->mutex);
1043 init_waitqueue_head(&ec->wait);
1044 INIT_LIST_HEAD(&ec->list);
1045 spin_lock_init(&ec->lock);
1046 INIT_WORK(&ec->work, acpi_ec_gpe_poller);
1051 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
1052 void *context, void **return_value)
1055 struct acpi_buffer buffer = { sizeof(node_name), node_name };
1056 struct acpi_ec *ec = context;
1060 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
1062 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1)
1063 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
1068 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
1071 unsigned long long tmp = 0;
1072 struct acpi_ec *ec = context;
1074 /* clear addr values, ec_parse_io_ports depend on it */
1075 ec->command_addr = ec->data_addr = 0;
1077 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1078 ec_parse_io_ports, ec);
1079 if (ACPI_FAILURE(status))
1082 /* Get GPE bit assignment (EC events). */
1083 /* TODO: Add support for _GPE returning a package */
1084 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
1085 if (ACPI_FAILURE(status))
1088 /* Use the global lock for all EC transactions? */
1090 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
1091 ec->global_lock = tmp;
1092 ec->handle = handle;
1093 return AE_CTRL_TERMINATE;
1096 static int ec_install_handlers(struct acpi_ec *ec)
1100 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
1102 status = acpi_install_gpe_raw_handler(NULL, ec->gpe,
1103 ACPI_GPE_EDGE_TRIGGERED,
1104 &acpi_ec_gpe_handler, ec);
1105 if (ACPI_FAILURE(status))
1108 acpi_ec_start(ec, false);
1109 status = acpi_install_address_space_handler(ec->handle,
1111 &acpi_ec_space_handler,
1113 if (ACPI_FAILURE(status)) {
1114 if (status == AE_NOT_FOUND) {
1116 * Maybe OS fails in evaluating the _REG object.
1117 * The AE_NOT_FOUND error will be ignored and OS
1118 * continue to initialize EC.
1120 pr_err("Fail in evaluating the _REG object"
1121 " of EC device. Broken bios is suspected.\n");
1123 acpi_ec_stop(ec, false);
1124 acpi_remove_gpe_handler(NULL, ec->gpe,
1125 &acpi_ec_gpe_handler);
1130 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
1134 static void ec_remove_handlers(struct acpi_ec *ec)
1136 if (!test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
1138 acpi_ec_stop(ec, false);
1139 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
1140 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
1141 pr_err("failed to remove space handler\n");
1142 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
1143 &acpi_ec_gpe_handler)))
1144 pr_err("failed to remove gpe handler\n");
1145 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
1148 static int acpi_ec_add(struct acpi_device *device)
1150 struct acpi_ec *ec = NULL;
1153 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1154 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1156 /* Check for boot EC */
1158 (boot_ec->handle == device->handle ||
1159 boot_ec->handle == ACPI_ROOT_OBJECT)) {
1163 ec = make_acpi_ec();
1167 if (ec_parse_device(device->handle, 0, ec, NULL) !=
1168 AE_CTRL_TERMINATE) {
1173 /* Find and register all query methods */
1174 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
1175 acpi_ec_register_query_methods, NULL, ec, NULL);
1179 device->driver_data = ec;
1181 ret = !!request_region(ec->data_addr, 1, "EC data");
1182 WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
1183 ret = !!request_region(ec->command_addr, 1, "EC cmd");
1184 WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
1186 pr_info("GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
1187 ec->gpe, ec->command_addr, ec->data_addr);
1189 ret = ec_install_handlers(ec);
1191 /* EC is fully operational, allow queries */
1192 acpi_ec_enable_event(ec);
1194 /* Clear stale _Q events if hardware might require that */
1195 if (EC_FLAGS_CLEAR_ON_RESUME)
1200 static int acpi_ec_remove(struct acpi_device *device)
1203 struct acpi_ec_query_handler *handler, *tmp;
1208 ec = acpi_driver_data(device);
1209 ec_remove_handlers(ec);
1210 mutex_lock(&ec->mutex);
1211 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
1212 list_del(&handler->node);
1215 mutex_unlock(&ec->mutex);
1216 release_region(ec->data_addr, 1);
1217 release_region(ec->command_addr, 1);
1218 device->driver_data = NULL;
1226 ec_parse_io_ports(struct acpi_resource *resource, void *context)
1228 struct acpi_ec *ec = context;
1230 if (resource->type != ACPI_RESOURCE_TYPE_IO)
1234 * The first address region returned is the data port, and
1235 * the second address region returned is the status/command
1238 if (ec->data_addr == 0)
1239 ec->data_addr = resource->data.io.minimum;
1240 else if (ec->command_addr == 0)
1241 ec->command_addr = resource->data.io.minimum;
1243 return AE_CTRL_TERMINATE;
1248 int __init acpi_boot_ec_enable(void)
1250 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
1252 if (!ec_install_handlers(boot_ec)) {
1259 static const struct acpi_device_id ec_device_ids[] = {
1264 /* Some BIOS do not survive early DSDT scan, skip it */
1265 static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
1267 EC_FLAGS_SKIP_DSDT_SCAN = 1;
1271 /* ASUStek often supplies us with broken ECDT, validate it */
1272 static int ec_validate_ecdt(const struct dmi_system_id *id)
1274 EC_FLAGS_VALIDATE_ECDT = 1;
1278 /* MSI EC needs special treatment, enable it */
1279 static int ec_flag_msi(const struct dmi_system_id *id)
1281 pr_debug("Detected MSI hardware, enabling workarounds.\n");
1283 EC_FLAGS_VALIDATE_ECDT = 1;
1288 * Clevo M720 notebook actually works ok with IRQ mode, if we lifted
1289 * the GPE storm threshold back to 20
1291 static int ec_enlarge_storm_threshold(const struct dmi_system_id *id)
1293 pr_debug("Setting the EC GPE storm threshold to 20\n");
1294 ec_storm_threshold = 20;
1299 * Acer EC firmware refuses to respond QR_EC when SCI_EVT is not set, for
1300 * which case, we complete the QR_EC without issuing it to the firmware.
1301 * https://bugzilla.kernel.org/show_bug.cgi?id=86211
1303 static int ec_flag_query_handshake(const struct dmi_system_id *id)
1305 pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
1306 EC_FLAGS_QUERY_HANDSHAKE = 1;
1311 * On some hardware it is necessary to clear events accumulated by the EC during
1312 * sleep. These ECs stop reporting GPEs until they are manually polled, if too
1313 * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
1315 * https://bugzilla.kernel.org/show_bug.cgi?id=44161
1317 * Ideally, the EC should also be instructed NOT to accumulate events during
1318 * sleep (which Windows seems to do somehow), but the interface to control this
1319 * behaviour is not known at this time.
1321 * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
1322 * however it is very likely that other Samsung models are affected.
1324 * On systems which don't accumulate _Q events during sleep, this extra check
1325 * should be harmless.
1327 static int ec_clear_on_resume(const struct dmi_system_id *id)
1329 pr_debug("Detected system needing EC poll on resume.\n");
1330 EC_FLAGS_CLEAR_ON_RESUME = 1;
1334 static struct dmi_system_id ec_dmi_table[] __initdata = {
1336 ec_skip_dsdt_scan, "Compal JFL92", {
1337 DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
1338 DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
1340 ec_flag_msi, "MSI hardware", {
1341 DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
1343 ec_flag_msi, "MSI hardware", {
1344 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
1346 ec_flag_msi, "MSI hardware", {
1347 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
1349 ec_flag_msi, "MSI hardware", {
1350 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
1352 ec_flag_msi, "Quanta hardware", {
1353 DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1354 DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
1356 ec_flag_msi, "Quanta hardware", {
1357 DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1358 DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
1360 ec_flag_msi, "Clevo W350etq", {
1361 DMI_MATCH(DMI_SYS_VENDOR, "CLEVO CO."),
1362 DMI_MATCH(DMI_PRODUCT_NAME, "W35_37ET"),}, NULL},
1364 ec_validate_ecdt, "ASUS hardware", {
1365 DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
1367 ec_validate_ecdt, "ASUS hardware", {
1368 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
1370 ec_enlarge_storm_threshold, "CLEVO hardware", {
1371 DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
1372 DMI_MATCH(DMI_PRODUCT_NAME, "M720T/M730T"),}, NULL},
1374 ec_skip_dsdt_scan, "HP Folio 13", {
1375 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1376 DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13"),}, NULL},
1378 ec_validate_ecdt, "ASUS hardware", {
1379 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."),
1380 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL},
1382 ec_clear_on_resume, "Samsung hardware", {
1383 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
1385 ec_flag_query_handshake, "Acer hardware", {
1386 DMI_MATCH(DMI_SYS_VENDOR, "Acer"), }, NULL},
1390 int __init acpi_ec_ecdt_probe(void)
1393 struct acpi_ec *saved_ec = NULL;
1394 struct acpi_table_ecdt *ecdt_ptr;
1396 boot_ec = make_acpi_ec();
1400 * Generate a boot ec context
1402 dmi_check_system(ec_dmi_table);
1403 status = acpi_get_table(ACPI_SIG_ECDT, 1,
1404 (struct acpi_table_header **)&ecdt_ptr);
1405 if (ACPI_SUCCESS(status)) {
1406 pr_info("EC description table is found, configuring boot EC\n");
1407 boot_ec->command_addr = ecdt_ptr->control.address;
1408 boot_ec->data_addr = ecdt_ptr->data.address;
1409 boot_ec->gpe = ecdt_ptr->gpe;
1410 boot_ec->handle = ACPI_ROOT_OBJECT;
1411 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id,
1413 /* Don't trust ECDT, which comes from ASUSTek */
1414 if (!EC_FLAGS_VALIDATE_ECDT)
1416 saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
1422 if (EC_FLAGS_SKIP_DSDT_SCAN) {
1427 /* This workaround is needed only on some broken machines,
1428 * which require early EC, but fail to provide ECDT */
1429 pr_debug("Look up EC in DSDT\n");
1430 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
1432 /* Check that acpi_get_devices actually find something */
1433 if (ACPI_FAILURE(status) || !boot_ec->handle)
1436 /* try to find good ECDT from ASUSTek */
1437 if (saved_ec->command_addr != boot_ec->command_addr ||
1438 saved_ec->data_addr != boot_ec->data_addr ||
1439 saved_ec->gpe != boot_ec->gpe ||
1440 saved_ec->handle != boot_ec->handle)
1441 pr_info("ASUSTek keeps feeding us with broken "
1442 "ECDT tables, which are very hard to workaround. "
1443 "Trying to use DSDT EC info instead. Please send "
1444 "output of acpidump to linux-acpi@vger.kernel.org\n");
1448 /* We really need to limit this workaround, the only ASUS,
1449 * which needs it, has fake EC._INI method, so use it as flag.
1450 * Keep boot_ec struct as it will be needed soon.
1452 if (!dmi_name_in_vendors("ASUS") ||
1453 !acpi_has_method(boot_ec->handle, "_INI"))
1457 if (!ec_install_handlers(boot_ec)) {
1468 static struct acpi_driver acpi_ec_driver = {
1470 .class = ACPI_EC_CLASS,
1471 .ids = ec_device_ids,
1474 .remove = acpi_ec_remove,
1478 int __init acpi_ec_init(void)
1482 /* Now register the driver for the EC */
1483 result = acpi_bus_register_driver(&acpi_ec_driver);
1490 /* EC driver currently not unloadable */
1492 static void __exit acpi_ec_exit(void)
1495 acpi_bus_unregister_driver(&acpi_ec_driver);