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 */
34 #define pr_fmt(fmt) "ACPI : EC: " fmt
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/types.h>
40 #include <linux/delay.h>
41 #include <linux/interrupt.h>
42 #include <linux/list.h>
43 #include <linux/spinlock.h>
44 #include <linux/slab.h>
45 #include <linux/acpi.h>
46 #include <linux/dmi.h>
51 #define ACPI_EC_CLASS "embedded_controller"
52 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
53 #define ACPI_EC_FILE_INFO "info"
55 /* EC status register */
56 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
57 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
58 #define ACPI_EC_FLAG_CMD 0x08 /* Input buffer contains a command */
59 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
60 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
64 ACPI_EC_COMMAND_READ = 0x80,
65 ACPI_EC_COMMAND_WRITE = 0x81,
66 ACPI_EC_BURST_ENABLE = 0x82,
67 ACPI_EC_BURST_DISABLE = 0x83,
68 ACPI_EC_COMMAND_QUERY = 0x84,
71 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
72 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
73 #define ACPI_EC_UDELAY_POLL 550 /* Wait 1ms for EC transaction polling */
74 #define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query
75 * when trying to clear the EC */
78 EC_FLAGS_QUERY_PENDING, /* Query is pending */
79 EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and
80 * OpReg are installed */
81 EC_FLAGS_STARTED, /* Driver is started */
82 EC_FLAGS_STOPPED, /* Driver is stopped */
83 EC_FLAGS_COMMAND_STORM, /* GPE storms occurred to the
84 * current command processing */
87 #define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */
88 #define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */
90 /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
91 static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
92 module_param(ec_delay, uint, 0644);
93 MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
96 * If the number of false interrupts per one transaction exceeds
97 * this threshold, will think there is a GPE storm happened and
98 * will disable the GPE for normal transaction.
100 static unsigned int ec_storm_threshold __read_mostly = 8;
101 module_param(ec_storm_threshold, uint, 0644);
102 MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
104 struct acpi_ec_query_handler {
105 struct list_head node;
106 acpi_ec_query_func func;
116 unsigned short irq_count;
125 static int acpi_ec_query(struct acpi_ec *ec, u8 *data);
126 static void advance_transaction(struct acpi_ec *ec);
128 struct acpi_ec *boot_ec, *first_ec;
129 EXPORT_SYMBOL(first_ec);
131 static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
132 static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
133 static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
134 static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */
135 static int EC_FLAGS_QUERY_HANDSHAKE; /* Needs QR_EC issued when SCI_EVT set */
137 /* --------------------------------------------------------------------------
139 * -------------------------------------------------------------------------- */
142 * Splitters used by the developers to track the boundary of the EC
143 * handling processes.
146 #define EC_DBG_SEP " "
147 #define EC_DBG_DRV "+++++"
148 #define EC_DBG_STM "====="
149 #define EC_DBG_REQ "*****"
150 #define EC_DBG_EVT "#####"
152 #define EC_DBG_SEP ""
159 #define ec_log_raw(fmt, ...) \
160 pr_info(fmt "\n", ##__VA_ARGS__)
161 #define ec_dbg_raw(fmt, ...) \
162 pr_debug(fmt "\n", ##__VA_ARGS__)
163 #define ec_log(filter, fmt, ...) \
164 ec_log_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__)
165 #define ec_dbg(filter, fmt, ...) \
166 ec_dbg_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__)
168 #define ec_log_drv(fmt, ...) \
169 ec_log(EC_DBG_DRV, fmt, ##__VA_ARGS__)
170 #define ec_dbg_drv(fmt, ...) \
171 ec_dbg(EC_DBG_DRV, fmt, ##__VA_ARGS__)
172 #define ec_dbg_stm(fmt, ...) \
173 ec_dbg(EC_DBG_STM, fmt, ##__VA_ARGS__)
174 #define ec_dbg_req(fmt, ...) \
175 ec_dbg(EC_DBG_REQ, fmt, ##__VA_ARGS__)
176 #define ec_dbg_evt(fmt, ...) \
177 ec_dbg(EC_DBG_EVT, fmt, ##__VA_ARGS__)
178 #define ec_dbg_ref(ec, fmt, ...) \
179 ec_dbg_raw("%lu: " fmt, ec->reference_count, ## __VA_ARGS__)
181 /* --------------------------------------------------------------------------
183 * -------------------------------------------------------------------------- */
185 static bool acpi_ec_started(struct acpi_ec *ec)
187 return test_bit(EC_FLAGS_STARTED, &ec->flags) &&
188 !test_bit(EC_FLAGS_STOPPED, &ec->flags);
191 static bool acpi_ec_flushed(struct acpi_ec *ec)
193 return ec->reference_count == 1;
196 /* --------------------------------------------------------------------------
198 * -------------------------------------------------------------------------- */
200 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
202 u8 x = inb(ec->command_addr);
204 ec_dbg_raw("EC_SC(R) = 0x%2.2x "
205 "SCI_EVT=%d BURST=%d CMD=%d IBF=%d OBF=%d",
207 !!(x & ACPI_EC_FLAG_SCI),
208 !!(x & ACPI_EC_FLAG_BURST),
209 !!(x & ACPI_EC_FLAG_CMD),
210 !!(x & ACPI_EC_FLAG_IBF),
211 !!(x & ACPI_EC_FLAG_OBF));
215 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
217 u8 x = inb(ec->data_addr);
219 ec->timestamp = jiffies;
220 ec_dbg_raw("EC_DATA(R) = 0x%2.2x", x);
224 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
226 ec_dbg_raw("EC_SC(W) = 0x%2.2x", command);
227 outb(command, ec->command_addr);
228 ec->timestamp = jiffies;
231 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
233 ec_dbg_raw("EC_DATA(W) = 0x%2.2x", data);
234 outb(data, ec->data_addr);
235 ec->timestamp = jiffies;
239 static const char *acpi_ec_cmd_string(u8 cmd)
256 #define acpi_ec_cmd_string(cmd) "UNDEF"
259 /* --------------------------------------------------------------------------
261 * -------------------------------------------------------------------------- */
263 static inline bool acpi_ec_is_gpe_raised(struct acpi_ec *ec)
265 acpi_event_status gpe_status = 0;
267 (void)acpi_get_gpe_status(NULL, ec->gpe, &gpe_status);
268 return (gpe_status & ACPI_EVENT_FLAG_STATUS_SET) ? true : false;
271 static inline void acpi_ec_enable_gpe(struct acpi_ec *ec, bool open)
274 acpi_enable_gpe(NULL, ec->gpe);
276 BUG_ON(ec->reference_count < 1);
277 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
279 if (acpi_ec_is_gpe_raised(ec)) {
281 * On some platforms, EN=1 writes cannot trigger GPE. So
282 * software need to manually trigger a pseudo GPE event on
285 ec_dbg_raw("Polling quirk");
286 advance_transaction(ec);
290 static inline void acpi_ec_disable_gpe(struct acpi_ec *ec, bool close)
293 acpi_disable_gpe(NULL, ec->gpe);
295 BUG_ON(ec->reference_count < 1);
296 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
300 static inline void acpi_ec_clear_gpe(struct acpi_ec *ec)
303 * GPE STS is a W1C register, which means:
304 * 1. Software can clear it without worrying about clearing other
305 * GPEs' STS bits when the hardware sets them in parallel.
306 * 2. As long as software can ensure only clearing it when it is
307 * set, hardware won't set it in parallel.
308 * So software can clear GPE in any contexts.
309 * Warning: do not move the check into advance_transaction() as the
310 * EC commands will be sent without GPE raised.
312 if (!acpi_ec_is_gpe_raised(ec))
314 acpi_clear_gpe(NULL, ec->gpe);
317 /* --------------------------------------------------------------------------
318 * Transaction Management
319 * -------------------------------------------------------------------------- */
321 static void acpi_ec_submit_request(struct acpi_ec *ec)
323 ec->reference_count++;
324 if (ec->reference_count == 1)
325 acpi_ec_enable_gpe(ec, true);
328 static void acpi_ec_complete_request(struct acpi_ec *ec)
330 bool flushed = false;
332 ec->reference_count--;
333 if (ec->reference_count == 0)
334 acpi_ec_disable_gpe(ec, true);
335 flushed = acpi_ec_flushed(ec);
340 static void acpi_ec_set_storm(struct acpi_ec *ec, u8 flag)
342 if (!test_bit(flag, &ec->flags)) {
343 acpi_ec_disable_gpe(ec, false);
344 ec_dbg_drv("Polling enabled");
345 set_bit(flag, &ec->flags);
349 static void acpi_ec_clear_storm(struct acpi_ec *ec, u8 flag)
351 if (test_bit(flag, &ec->flags)) {
352 clear_bit(flag, &ec->flags);
353 acpi_ec_enable_gpe(ec, false);
354 ec_dbg_drv("Polling disabled");
359 * acpi_ec_submit_flushable_request() - Increase the reference count unless
360 * the flush operation is not in
364 * This function must be used before taking a new action that should hold
365 * the reference count. If this function returns false, then the action
366 * must be discarded or it will prevent the flush operation from being
369 static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec)
371 if (!acpi_ec_started(ec))
373 acpi_ec_submit_request(ec);
377 static void acpi_ec_submit_query(struct acpi_ec *ec)
379 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
380 ec_dbg_req("Event started");
381 schedule_work(&ec->work);
385 static void acpi_ec_complete_query(struct acpi_ec *ec)
387 if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
388 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
389 ec_dbg_req("Event stopped");
393 static int ec_transaction_polled(struct acpi_ec *ec)
398 spin_lock_irqsave(&ec->lock, flags);
399 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_POLL))
401 spin_unlock_irqrestore(&ec->lock, flags);
405 static int ec_transaction_completed(struct acpi_ec *ec)
410 spin_lock_irqsave(&ec->lock, flags);
411 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
413 spin_unlock_irqrestore(&ec->lock, flags);
417 static void advance_transaction(struct acpi_ec *ec)
419 struct transaction *t;
423 ec_dbg_stm("%s (%d)", in_interrupt() ? "IRQ" : "TASK",
426 * By always clearing STS before handling all indications, we can
427 * ensure a hardware STS 0->1 change after this clearing can always
428 * trigger a GPE interrupt.
430 acpi_ec_clear_gpe(ec);
431 status = acpi_ec_read_status(ec);
435 if (t->flags & ACPI_EC_COMMAND_POLL) {
436 if (t->wlen > t->wi) {
437 if ((status & ACPI_EC_FLAG_IBF) == 0)
438 acpi_ec_write_data(ec, t->wdata[t->wi++]);
441 } else if (t->rlen > t->ri) {
442 if ((status & ACPI_EC_FLAG_OBF) == 1) {
443 t->rdata[t->ri++] = acpi_ec_read_data(ec);
444 if (t->rlen == t->ri) {
445 t->flags |= ACPI_EC_COMMAND_COMPLETE;
446 if (t->command == ACPI_EC_COMMAND_QUERY)
447 ec_dbg_req("Command(%s) hardware completion",
448 acpi_ec_cmd_string(t->command));
453 } else if (t->wlen == t->wi &&
454 (status & ACPI_EC_FLAG_IBF) == 0) {
455 t->flags |= ACPI_EC_COMMAND_COMPLETE;
460 if (EC_FLAGS_QUERY_HANDSHAKE &&
461 !(status & ACPI_EC_FLAG_SCI) &&
462 (t->command == ACPI_EC_COMMAND_QUERY)) {
463 t->flags |= ACPI_EC_COMMAND_POLL;
464 acpi_ec_complete_query(ec);
465 t->rdata[t->ri++] = 0x00;
466 t->flags |= ACPI_EC_COMMAND_COMPLETE;
467 ec_dbg_req("Command(%s) software completion",
468 acpi_ec_cmd_string(t->command));
470 } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
471 acpi_ec_write_cmd(ec, t->command);
472 t->flags |= ACPI_EC_COMMAND_POLL;
473 acpi_ec_complete_query(ec);
480 * If SCI bit is set, then don't think it's a false IRQ
481 * otherwise will take a not handled IRQ as a false one.
483 if (!(status & ACPI_EC_FLAG_SCI)) {
484 if (in_interrupt() && t) {
485 if (t->irq_count < ec_storm_threshold)
487 /* Allow triggering on 0 threshold */
488 if (t->irq_count == ec_storm_threshold)
489 acpi_ec_set_storm(ec, EC_FLAGS_COMMAND_STORM);
493 if (status & ACPI_EC_FLAG_SCI)
494 acpi_ec_submit_query(ec);
495 if (wakeup && in_interrupt())
499 static void start_transaction(struct acpi_ec *ec)
501 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
505 static int ec_guard(struct acpi_ec *ec)
507 unsigned long guard = usecs_to_jiffies(ACPI_EC_UDELAY_POLL);
508 unsigned long timeout = ec->timestamp + guard;
512 /* Perform busy polling */
513 if (ec_transaction_completed(ec))
515 udelay(jiffies_to_usecs(guard));
518 * Perform wait polling
520 * The following check is there to keep the old
521 * logic - no inter-transaction guarding for the
524 if (!ec_transaction_polled(ec))
526 if (wait_event_timeout(ec->wait,
527 ec_transaction_completed(ec),
531 /* Guard the register accesses for the polling modes */
532 } while (time_before(jiffies, timeout));
536 static int ec_poll(struct acpi_ec *ec)
539 int repeat = 5; /* number of command restarts */
542 unsigned long delay = jiffies +
543 msecs_to_jiffies(ec_delay);
547 spin_lock_irqsave(&ec->lock, flags);
548 advance_transaction(ec);
549 spin_unlock_irqrestore(&ec->lock, flags);
550 } while (time_before(jiffies, delay));
551 pr_debug("controller reset, restart transaction\n");
552 spin_lock_irqsave(&ec->lock, flags);
553 start_transaction(ec);
554 spin_unlock_irqrestore(&ec->lock, flags);
559 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
560 struct transaction *t)
565 /* start transaction */
566 spin_lock_irqsave(&ec->lock, tmp);
567 /* Enable GPE for command processing (IBF=0/OBF=1) */
568 if (!acpi_ec_submit_flushable_request(ec)) {
572 ec_dbg_ref(ec, "Increase command");
573 /* following two actions should be kept atomic */
575 ec_dbg_req("Command(%s) started", acpi_ec_cmd_string(t->command));
576 start_transaction(ec);
577 spin_unlock_irqrestore(&ec->lock, tmp);
581 spin_lock_irqsave(&ec->lock, tmp);
582 if (t->irq_count == ec_storm_threshold)
583 acpi_ec_clear_storm(ec, EC_FLAGS_COMMAND_STORM);
584 ec_dbg_req("Command(%s) stopped", acpi_ec_cmd_string(t->command));
586 /* Disable GPE for command processing (IBF=0/OBF=1) */
587 acpi_ec_complete_request(ec);
588 ec_dbg_ref(ec, "Decrease command");
590 spin_unlock_irqrestore(&ec->lock, tmp);
594 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
599 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
602 memset(t->rdata, 0, t->rlen);
604 mutex_lock(&ec->mutex);
605 if (ec->global_lock) {
606 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
607 if (ACPI_FAILURE(status)) {
613 status = acpi_ec_transaction_unlocked(ec, t);
616 acpi_release_global_lock(glk);
618 mutex_unlock(&ec->mutex);
622 static int acpi_ec_burst_enable(struct acpi_ec *ec)
625 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
626 .wdata = NULL, .rdata = &d,
627 .wlen = 0, .rlen = 1};
629 return acpi_ec_transaction(ec, &t);
632 static int acpi_ec_burst_disable(struct acpi_ec *ec)
634 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
635 .wdata = NULL, .rdata = NULL,
636 .wlen = 0, .rlen = 0};
638 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
639 acpi_ec_transaction(ec, &t) : 0;
642 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
646 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
647 .wdata = &address, .rdata = &d,
648 .wlen = 1, .rlen = 1};
650 result = acpi_ec_transaction(ec, &t);
655 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
657 u8 wdata[2] = { address, data };
658 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
659 .wdata = wdata, .rdata = NULL,
660 .wlen = 2, .rlen = 0};
662 return acpi_ec_transaction(ec, &t);
665 int ec_read(u8 addr, u8 *val)
673 err = acpi_ec_read(first_ec, addr, &temp_data);
681 EXPORT_SYMBOL(ec_read);
683 int ec_write(u8 addr, u8 val)
690 err = acpi_ec_write(first_ec, addr, val);
694 EXPORT_SYMBOL(ec_write);
696 int ec_transaction(u8 command,
697 const u8 *wdata, unsigned wdata_len,
698 u8 *rdata, unsigned rdata_len)
700 struct transaction t = {.command = command,
701 .wdata = wdata, .rdata = rdata,
702 .wlen = wdata_len, .rlen = rdata_len};
707 return acpi_ec_transaction(first_ec, &t);
709 EXPORT_SYMBOL(ec_transaction);
711 /* Get the handle to the EC device */
712 acpi_handle ec_get_handle(void)
716 return first_ec->handle;
718 EXPORT_SYMBOL(ec_get_handle);
721 * Process _Q events that might have accumulated in the EC.
722 * Run with locked ec mutex.
724 static void acpi_ec_clear(struct acpi_ec *ec)
729 for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
730 status = acpi_ec_query(ec, &value);
731 if (status || !value)
735 if (unlikely(i == ACPI_EC_CLEAR_MAX))
736 pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
738 pr_info("%d stale EC events cleared\n", i);
741 static void acpi_ec_start(struct acpi_ec *ec, bool resuming)
745 spin_lock_irqsave(&ec->lock, flags);
746 if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) {
747 ec_dbg_drv("Starting EC");
748 /* Enable GPE for event processing (SCI_EVT=1) */
750 acpi_ec_submit_request(ec);
751 ec_dbg_ref(ec, "Increase driver");
753 ec_log_drv("EC started");
755 spin_unlock_irqrestore(&ec->lock, flags);
758 static bool acpi_ec_stopped(struct acpi_ec *ec)
763 spin_lock_irqsave(&ec->lock, flags);
764 flushed = acpi_ec_flushed(ec);
765 spin_unlock_irqrestore(&ec->lock, flags);
769 static void acpi_ec_stop(struct acpi_ec *ec, bool suspending)
773 spin_lock_irqsave(&ec->lock, flags);
774 if (acpi_ec_started(ec)) {
775 ec_dbg_drv("Stopping EC");
776 set_bit(EC_FLAGS_STOPPED, &ec->flags);
777 spin_unlock_irqrestore(&ec->lock, flags);
778 wait_event(ec->wait, acpi_ec_stopped(ec));
779 spin_lock_irqsave(&ec->lock, flags);
780 /* Disable GPE for event processing (SCI_EVT=1) */
782 acpi_ec_complete_request(ec);
783 ec_dbg_ref(ec, "Decrease driver");
785 clear_bit(EC_FLAGS_STARTED, &ec->flags);
786 clear_bit(EC_FLAGS_STOPPED, &ec->flags);
787 ec_log_drv("EC stopped");
789 spin_unlock_irqrestore(&ec->lock, flags);
792 void acpi_ec_block_transactions(void)
794 struct acpi_ec *ec = first_ec;
799 mutex_lock(&ec->mutex);
800 /* Prevent transactions from being carried out */
801 acpi_ec_stop(ec, true);
802 mutex_unlock(&ec->mutex);
805 void acpi_ec_unblock_transactions(void)
807 struct acpi_ec *ec = first_ec;
812 /* Allow transactions to be carried out again */
813 acpi_ec_start(ec, true);
815 if (EC_FLAGS_CLEAR_ON_RESUME)
819 void acpi_ec_unblock_transactions_early(void)
822 * Allow transactions to happen again (this function is called from
823 * atomic context during wakeup, so we don't need to acquire the mutex).
826 acpi_ec_start(first_ec, true);
829 /* --------------------------------------------------------------------------
831 -------------------------------------------------------------------------- */
832 static struct acpi_ec_query_handler *
833 acpi_ec_get_query_handler(struct acpi_ec_query_handler *handler)
836 kref_get(&handler->kref);
840 static void acpi_ec_query_handler_release(struct kref *kref)
842 struct acpi_ec_query_handler *handler =
843 container_of(kref, struct acpi_ec_query_handler, kref);
848 static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler)
850 kref_put(&handler->kref, acpi_ec_query_handler_release);
853 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
854 acpi_handle handle, acpi_ec_query_func func,
857 struct acpi_ec_query_handler *handler =
858 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
863 handler->query_bit = query_bit;
864 handler->handle = handle;
865 handler->func = func;
866 handler->data = data;
867 mutex_lock(&ec->mutex);
868 kref_init(&handler->kref);
869 list_add(&handler->node, &ec->list);
870 mutex_unlock(&ec->mutex);
873 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
875 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
877 struct acpi_ec_query_handler *handler, *tmp;
878 LIST_HEAD(free_list);
880 mutex_lock(&ec->mutex);
881 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
882 if (query_bit == handler->query_bit) {
883 list_del_init(&handler->node);
884 list_add(&handler->node, &free_list);
887 mutex_unlock(&ec->mutex);
888 list_for_each_entry_safe(handler, tmp, &free_list, node)
889 acpi_ec_put_query_handler(handler);
891 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
893 static void acpi_ec_run(void *cxt)
895 struct acpi_ec_query_handler *handler = cxt;
899 ec_dbg_evt("Query(0x%02x) started", handler->query_bit);
901 handler->func(handler->data);
902 else if (handler->handle)
903 acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
904 ec_dbg_evt("Query(0x%02x) stopped", handler->query_bit);
905 acpi_ec_put_query_handler(handler);
908 static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
913 struct acpi_ec_query_handler *handler;
914 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
915 .wdata = NULL, .rdata = &value,
916 .wlen = 0, .rlen = 1};
919 * Query the EC to find out which _Qxx method we need to evaluate.
920 * Note that successful completion of the query causes the ACPI_EC_SCI
921 * bit to be cleared (and thus clearing the interrupt source).
923 result = acpi_ec_transaction(ec, &t);
931 mutex_lock(&ec->mutex);
932 list_for_each_entry(handler, &ec->list, node) {
933 if (value == handler->query_bit) {
934 /* have custom handler for this bit */
935 handler = acpi_ec_get_query_handler(handler);
936 ec_dbg_evt("Query(0x%02x) scheduled",
938 status = acpi_os_execute((handler->func) ?
939 OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
940 acpi_ec_run, handler);
941 if (ACPI_FAILURE(status))
946 mutex_unlock(&ec->mutex);
950 static void acpi_ec_gpe_poller(struct work_struct *work)
952 struct acpi_ec *ec = container_of(work, struct acpi_ec, work);
954 acpi_ec_query(ec, NULL);
957 static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
958 u32 gpe_number, void *data)
961 struct acpi_ec *ec = data;
963 spin_lock_irqsave(&ec->lock, flags);
964 advance_transaction(ec);
965 spin_unlock_irqrestore(&ec->lock, flags);
966 return ACPI_INTERRUPT_HANDLED;
969 /* --------------------------------------------------------------------------
970 * Address Space Management
971 * -------------------------------------------------------------------------- */
974 acpi_ec_space_handler(u32 function, acpi_physical_address address,
975 u32 bits, u64 *value64,
976 void *handler_context, void *region_context)
978 struct acpi_ec *ec = handler_context;
979 int result = 0, i, bytes = bits / 8;
980 u8 *value = (u8 *)value64;
982 if ((address > 0xFF) || !value || !handler_context)
983 return AE_BAD_PARAMETER;
985 if (function != ACPI_READ && function != ACPI_WRITE)
986 return AE_BAD_PARAMETER;
988 if (EC_FLAGS_MSI || bits > 8)
989 acpi_ec_burst_enable(ec);
991 for (i = 0; i < bytes; ++i, ++address, ++value)
992 result = (function == ACPI_READ) ?
993 acpi_ec_read(ec, address, value) :
994 acpi_ec_write(ec, address, *value);
996 if (EC_FLAGS_MSI || bits > 8)
997 acpi_ec_burst_disable(ec);
1001 return AE_BAD_PARAMETER;
1003 return AE_NOT_FOUND;
1011 /* --------------------------------------------------------------------------
1013 * -------------------------------------------------------------------------- */
1016 ec_parse_io_ports(struct acpi_resource *resource, void *context);
1018 static struct acpi_ec *make_acpi_ec(void)
1020 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1024 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
1025 mutex_init(&ec->mutex);
1026 init_waitqueue_head(&ec->wait);
1027 INIT_LIST_HEAD(&ec->list);
1028 spin_lock_init(&ec->lock);
1029 INIT_WORK(&ec->work, acpi_ec_gpe_poller);
1030 ec->timestamp = jiffies;
1035 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
1036 void *context, void **return_value)
1039 struct acpi_buffer buffer = { sizeof(node_name), node_name };
1040 struct acpi_ec *ec = context;
1044 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
1046 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1)
1047 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
1052 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
1055 unsigned long long tmp = 0;
1056 struct acpi_ec *ec = context;
1058 /* clear addr values, ec_parse_io_ports depend on it */
1059 ec->command_addr = ec->data_addr = 0;
1061 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1062 ec_parse_io_ports, ec);
1063 if (ACPI_FAILURE(status))
1066 /* Get GPE bit assignment (EC events). */
1067 /* TODO: Add support for _GPE returning a package */
1068 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
1069 if (ACPI_FAILURE(status))
1072 /* Use the global lock for all EC transactions? */
1074 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
1075 ec->global_lock = tmp;
1076 ec->handle = handle;
1077 return AE_CTRL_TERMINATE;
1080 static int ec_install_handlers(struct acpi_ec *ec)
1084 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
1086 status = acpi_install_gpe_raw_handler(NULL, ec->gpe,
1087 ACPI_GPE_EDGE_TRIGGERED,
1088 &acpi_ec_gpe_handler, ec);
1089 if (ACPI_FAILURE(status))
1092 acpi_ec_start(ec, false);
1093 status = acpi_install_address_space_handler(ec->handle,
1095 &acpi_ec_space_handler,
1097 if (ACPI_FAILURE(status)) {
1098 if (status == AE_NOT_FOUND) {
1100 * Maybe OS fails in evaluating the _REG object.
1101 * The AE_NOT_FOUND error will be ignored and OS
1102 * continue to initialize EC.
1104 pr_err("Fail in evaluating the _REG object"
1105 " of EC device. Broken bios is suspected.\n");
1107 acpi_ec_stop(ec, false);
1108 acpi_remove_gpe_handler(NULL, ec->gpe,
1109 &acpi_ec_gpe_handler);
1114 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
1118 static void ec_remove_handlers(struct acpi_ec *ec)
1120 if (!test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
1122 acpi_ec_stop(ec, false);
1123 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
1124 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
1125 pr_err("failed to remove space handler\n");
1126 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
1127 &acpi_ec_gpe_handler)))
1128 pr_err("failed to remove gpe handler\n");
1129 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
1132 static int acpi_ec_add(struct acpi_device *device)
1134 struct acpi_ec *ec = NULL;
1137 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1138 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1140 /* Check for boot EC */
1142 (boot_ec->handle == device->handle ||
1143 boot_ec->handle == ACPI_ROOT_OBJECT)) {
1147 ec = make_acpi_ec();
1151 if (ec_parse_device(device->handle, 0, ec, NULL) !=
1152 AE_CTRL_TERMINATE) {
1157 /* Find and register all query methods */
1158 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
1159 acpi_ec_register_query_methods, NULL, ec, NULL);
1163 device->driver_data = ec;
1165 ret = !!request_region(ec->data_addr, 1, "EC data");
1166 WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
1167 ret = !!request_region(ec->command_addr, 1, "EC cmd");
1168 WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
1170 pr_info("GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
1171 ec->gpe, ec->command_addr, ec->data_addr);
1173 ret = ec_install_handlers(ec);
1175 /* Reprobe devices depending on the EC */
1176 acpi_walk_dep_device_list(ec->handle);
1178 /* EC is fully operational, allow queries */
1179 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
1181 /* Clear stale _Q events if hardware might require that */
1182 if (EC_FLAGS_CLEAR_ON_RESUME)
1187 static int acpi_ec_remove(struct acpi_device *device)
1190 struct acpi_ec_query_handler *handler, *tmp;
1195 ec = acpi_driver_data(device);
1196 ec_remove_handlers(ec);
1197 mutex_lock(&ec->mutex);
1198 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
1199 list_del(&handler->node);
1202 mutex_unlock(&ec->mutex);
1203 release_region(ec->data_addr, 1);
1204 release_region(ec->command_addr, 1);
1205 device->driver_data = NULL;
1213 ec_parse_io_ports(struct acpi_resource *resource, void *context)
1215 struct acpi_ec *ec = context;
1217 if (resource->type != ACPI_RESOURCE_TYPE_IO)
1221 * The first address region returned is the data port, and
1222 * the second address region returned is the status/command
1225 if (ec->data_addr == 0)
1226 ec->data_addr = resource->data.io.minimum;
1227 else if (ec->command_addr == 0)
1228 ec->command_addr = resource->data.io.minimum;
1230 return AE_CTRL_TERMINATE;
1235 int __init acpi_boot_ec_enable(void)
1237 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
1239 if (!ec_install_handlers(boot_ec)) {
1246 static const struct acpi_device_id ec_device_ids[] = {
1251 /* Some BIOS do not survive early DSDT scan, skip it */
1252 static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
1254 EC_FLAGS_SKIP_DSDT_SCAN = 1;
1258 /* ASUStek often supplies us with broken ECDT, validate it */
1259 static int ec_validate_ecdt(const struct dmi_system_id *id)
1261 EC_FLAGS_VALIDATE_ECDT = 1;
1265 /* MSI EC needs special treatment, enable it */
1266 static int ec_flag_msi(const struct dmi_system_id *id)
1268 pr_debug("Detected MSI hardware, enabling workarounds.\n");
1270 EC_FLAGS_VALIDATE_ECDT = 1;
1275 * Acer EC firmware refuses to respond QR_EC when SCI_EVT is not set, for
1276 * which case, we complete the QR_EC without issuing it to the firmware.
1277 * https://bugzilla.kernel.org/show_bug.cgi?id=86211
1279 static int ec_flag_query_handshake(const struct dmi_system_id *id)
1281 pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
1282 EC_FLAGS_QUERY_HANDSHAKE = 1;
1287 * On some hardware it is necessary to clear events accumulated by the EC during
1288 * sleep. These ECs stop reporting GPEs until they are manually polled, if too
1289 * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
1291 * https://bugzilla.kernel.org/show_bug.cgi?id=44161
1293 * Ideally, the EC should also be instructed NOT to accumulate events during
1294 * sleep (which Windows seems to do somehow), but the interface to control this
1295 * behaviour is not known at this time.
1297 * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
1298 * however it is very likely that other Samsung models are affected.
1300 * On systems which don't accumulate _Q events during sleep, this extra check
1301 * should be harmless.
1303 static int ec_clear_on_resume(const struct dmi_system_id *id)
1305 pr_debug("Detected system needing EC poll on resume.\n");
1306 EC_FLAGS_CLEAR_ON_RESUME = 1;
1310 static struct dmi_system_id ec_dmi_table[] __initdata = {
1312 ec_skip_dsdt_scan, "Compal JFL92", {
1313 DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
1314 DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
1316 ec_flag_msi, "MSI hardware", {
1317 DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
1319 ec_flag_msi, "MSI hardware", {
1320 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
1322 ec_flag_msi, "MSI hardware", {
1323 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
1325 ec_flag_msi, "MSI hardware", {
1326 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
1328 ec_flag_msi, "Quanta hardware", {
1329 DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1330 DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
1332 ec_flag_msi, "Quanta hardware", {
1333 DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1334 DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
1336 ec_flag_msi, "Clevo W350etq", {
1337 DMI_MATCH(DMI_SYS_VENDOR, "CLEVO CO."),
1338 DMI_MATCH(DMI_PRODUCT_NAME, "W35_37ET"),}, NULL},
1340 ec_validate_ecdt, "ASUS hardware", {
1341 DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
1343 ec_validate_ecdt, "ASUS hardware", {
1344 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
1346 ec_skip_dsdt_scan, "HP Folio 13", {
1347 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1348 DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13"),}, NULL},
1350 ec_validate_ecdt, "ASUS hardware", {
1351 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."),
1352 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL},
1354 ec_clear_on_resume, "Samsung hardware", {
1355 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
1357 ec_flag_query_handshake, "Acer hardware", {
1358 DMI_MATCH(DMI_SYS_VENDOR, "Acer"), }, NULL},
1362 int __init acpi_ec_ecdt_probe(void)
1365 struct acpi_ec *saved_ec = NULL;
1366 struct acpi_table_ecdt *ecdt_ptr;
1368 boot_ec = make_acpi_ec();
1372 * Generate a boot ec context
1374 dmi_check_system(ec_dmi_table);
1375 status = acpi_get_table(ACPI_SIG_ECDT, 1,
1376 (struct acpi_table_header **)&ecdt_ptr);
1377 if (ACPI_SUCCESS(status)) {
1378 pr_info("EC description table is found, configuring boot EC\n");
1379 boot_ec->command_addr = ecdt_ptr->control.address;
1380 boot_ec->data_addr = ecdt_ptr->data.address;
1381 boot_ec->gpe = ecdt_ptr->gpe;
1382 boot_ec->handle = ACPI_ROOT_OBJECT;
1383 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id,
1385 /* Don't trust ECDT, which comes from ASUSTek */
1386 if (!EC_FLAGS_VALIDATE_ECDT)
1388 saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
1394 if (EC_FLAGS_SKIP_DSDT_SCAN) {
1399 /* This workaround is needed only on some broken machines,
1400 * which require early EC, but fail to provide ECDT */
1401 pr_debug("Look up EC in DSDT\n");
1402 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
1404 /* Check that acpi_get_devices actually find something */
1405 if (ACPI_FAILURE(status) || !boot_ec->handle)
1408 /* try to find good ECDT from ASUSTek */
1409 if (saved_ec->command_addr != boot_ec->command_addr ||
1410 saved_ec->data_addr != boot_ec->data_addr ||
1411 saved_ec->gpe != boot_ec->gpe ||
1412 saved_ec->handle != boot_ec->handle)
1413 pr_info("ASUSTek keeps feeding us with broken "
1414 "ECDT tables, which are very hard to workaround. "
1415 "Trying to use DSDT EC info instead. Please send "
1416 "output of acpidump to linux-acpi@vger.kernel.org\n");
1420 /* We really need to limit this workaround, the only ASUS,
1421 * which needs it, has fake EC._INI method, so use it as flag.
1422 * Keep boot_ec struct as it will be needed soon.
1424 if (!dmi_name_in_vendors("ASUS") ||
1425 !acpi_has_method(boot_ec->handle, "_INI"))
1429 if (!ec_install_handlers(boot_ec)) {
1440 static struct acpi_driver acpi_ec_driver = {
1442 .class = ACPI_EC_CLASS,
1443 .ids = ec_device_ids,
1446 .remove = acpi_ec_remove,
1450 int __init acpi_ec_init(void)
1454 /* Now register the driver for the EC */
1455 result = acpi_bus_register_driver(&acpi_ec_driver);
1462 /* EC driver currently not unloadable */
1464 static void __exit acpi_ec_exit(void)
1467 acpi_bus_unregister_driver(&acpi_ec_driver);