blob: 258a8df235cfbc9206118b177ae381eb3e7bffd3 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013 */
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/spinlock.h>
27#include <linux/slab.h>
David Brazdil0f672f62019-12-10 10:32:29 +000028#include <linux/suspend.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029#include <linux/acpi.h>
30#include <linux/dmi.h>
31#include <asm/io.h>
32
33#include "internal.h"
34
35#define ACPI_EC_CLASS "embedded_controller"
36#define ACPI_EC_DEVICE_NAME "Embedded Controller"
37#define ACPI_EC_FILE_INFO "info"
38
39/* EC status register */
40#define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
41#define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
42#define ACPI_EC_FLAG_CMD 0x08 /* Input buffer contains a command */
43#define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
44#define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
45
46/*
47 * The SCI_EVT clearing timing is not defined by the ACPI specification.
48 * This leads to lots of practical timing issues for the host EC driver.
49 * The following variations are defined (from the target EC firmware's
50 * perspective):
51 * STATUS: After indicating SCI_EVT edge triggered IRQ to the host, the
52 * target can clear SCI_EVT at any time so long as the host can see
53 * the indication by reading the status register (EC_SC). So the
54 * host should re-check SCI_EVT after the first time the SCI_EVT
55 * indication is seen, which is the same time the query request
56 * (QR_EC) is written to the command register (EC_CMD). SCI_EVT set
57 * at any later time could indicate another event. Normally such
58 * kind of EC firmware has implemented an event queue and will
59 * return 0x00 to indicate "no outstanding event".
60 * QUERY: After seeing the query request (QR_EC) written to the command
61 * register (EC_CMD) by the host and having prepared the responding
62 * event value in the data register (EC_DATA), the target can safely
63 * clear SCI_EVT because the target can confirm that the current
64 * event is being handled by the host. The host then should check
65 * SCI_EVT right after reading the event response from the data
66 * register (EC_DATA).
67 * EVENT: After seeing the event response read from the data register
68 * (EC_DATA) by the host, the target can clear SCI_EVT. As the
69 * target requires time to notice the change in the data register
70 * (EC_DATA), the host may be required to wait additional guarding
71 * time before checking the SCI_EVT again. Such guarding may not be
72 * necessary if the host is notified via another IRQ.
73 */
74#define ACPI_EC_EVT_TIMING_STATUS 0x00
75#define ACPI_EC_EVT_TIMING_QUERY 0x01
76#define ACPI_EC_EVT_TIMING_EVENT 0x02
77
78/* EC commands */
79enum ec_command {
80 ACPI_EC_COMMAND_READ = 0x80,
81 ACPI_EC_COMMAND_WRITE = 0x81,
82 ACPI_EC_BURST_ENABLE = 0x82,
83 ACPI_EC_BURST_DISABLE = 0x83,
84 ACPI_EC_COMMAND_QUERY = 0x84,
85};
86
87#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
88#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
89#define ACPI_EC_UDELAY_POLL 550 /* Wait 1ms for EC transaction polling */
90#define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query
91 * when trying to clear the EC */
92#define ACPI_EC_MAX_QUERIES 16 /* Maximum number of parallel queries */
93
94enum {
95 EC_FLAGS_QUERY_ENABLED, /* Query is enabled */
96 EC_FLAGS_QUERY_PENDING, /* Query is pending */
97 EC_FLAGS_QUERY_GUARDING, /* Guard for SCI_EVT check */
98 EC_FLAGS_GPE_HANDLER_INSTALLED, /* GPE handler installed */
99 EC_FLAGS_EC_HANDLER_INSTALLED, /* OpReg handler installed */
100 EC_FLAGS_EVT_HANDLER_INSTALLED, /* _Qxx handlers installed */
101 EC_FLAGS_STARTED, /* Driver is started */
102 EC_FLAGS_STOPPED, /* Driver is stopped */
103 EC_FLAGS_GPE_MASKED, /* GPE masked */
104};
105
106#define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */
107#define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */
108
109/* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
110static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
111module_param(ec_delay, uint, 0644);
112MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
113
114static unsigned int ec_max_queries __read_mostly = ACPI_EC_MAX_QUERIES;
115module_param(ec_max_queries, uint, 0644);
116MODULE_PARM_DESC(ec_max_queries, "Maximum parallel _Qxx evaluations");
117
118static bool ec_busy_polling __read_mostly;
119module_param(ec_busy_polling, bool, 0644);
120MODULE_PARM_DESC(ec_busy_polling, "Use busy polling to advance EC transaction");
121
122static unsigned int ec_polling_guard __read_mostly = ACPI_EC_UDELAY_POLL;
123module_param(ec_polling_guard, uint, 0644);
124MODULE_PARM_DESC(ec_polling_guard, "Guard time(us) between EC accesses in polling modes");
125
126static unsigned int ec_event_clearing __read_mostly = ACPI_EC_EVT_TIMING_QUERY;
127
128/*
129 * If the number of false interrupts per one transaction exceeds
130 * this threshold, will think there is a GPE storm happened and
131 * will disable the GPE for normal transaction.
132 */
133static unsigned int ec_storm_threshold __read_mostly = 8;
134module_param(ec_storm_threshold, uint, 0644);
135MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
136
137static bool ec_freeze_events __read_mostly = false;
138module_param(ec_freeze_events, bool, 0644);
139MODULE_PARM_DESC(ec_freeze_events, "Disabling event handling during suspend/resume");
140
141static bool ec_no_wakeup __read_mostly;
142module_param(ec_no_wakeup, bool, 0644);
143MODULE_PARM_DESC(ec_no_wakeup, "Do not wake up from suspend-to-idle");
144
145struct acpi_ec_query_handler {
146 struct list_head node;
147 acpi_ec_query_func func;
148 acpi_handle handle;
149 void *data;
150 u8 query_bit;
151 struct kref kref;
152};
153
154struct transaction {
155 const u8 *wdata;
156 u8 *rdata;
157 unsigned short irq_count;
158 u8 command;
159 u8 wi;
160 u8 ri;
161 u8 wlen;
162 u8 rlen;
163 u8 flags;
164};
165
166struct acpi_ec_query {
167 struct transaction transaction;
168 struct work_struct work;
169 struct acpi_ec_query_handler *handler;
170};
171
172static int acpi_ec_query(struct acpi_ec *ec, u8 *data);
173static void advance_transaction(struct acpi_ec *ec);
174static void acpi_ec_event_handler(struct work_struct *work);
175static void acpi_ec_event_processor(struct work_struct *work);
176
David Brazdil0f672f62019-12-10 10:32:29 +0000177struct acpi_ec *first_ec;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178EXPORT_SYMBOL(first_ec);
David Brazdil0f672f62019-12-10 10:32:29 +0000179
180static struct acpi_ec *boot_ec;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181static bool boot_ec_is_ecdt = false;
Olivier Deprez0e641232021-09-23 10:07:05 +0200182static struct workqueue_struct *ec_wq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000183static struct workqueue_struct *ec_query_wq;
184
185static int EC_FLAGS_QUERY_HANDSHAKE; /* Needs QR_EC issued when SCI_EVT set */
186static int EC_FLAGS_CORRECT_ECDT; /* Needs ECDT port address correction */
187static int EC_FLAGS_IGNORE_DSDT_GPE; /* Needs ECDT GPE as correction setting */
David Brazdil0f672f62019-12-10 10:32:29 +0000188static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000189
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
238static 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
244static 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
268static bool acpi_ec_flushed(struct acpi_ec *ec)
269{
270 return ec->reference_count == 1;
271}
272
273/* --------------------------------------------------------------------------
274 * EC Registers
275 * -------------------------------------------------------------------------- */
276
277static 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
292static 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
301static 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
308static 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)
316static 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
340static inline bool acpi_ec_is_gpe_raised(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) ? true : false;
346}
347
348static 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_is_gpe_raised(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);
364 }
365}
366
367static 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
377static inline void acpi_ec_clear_gpe(struct acpi_ec *ec)
378{
379 /*
380 * GPE STS is a W1C register, which means:
381 * 1. Software can clear it without worrying about clearing other
382 * GPEs' STS bits when the hardware sets them in parallel.
383 * 2. As long as software can ensure only clearing it when it is
384 * set, hardware won't set it in parallel.
385 * So software can clear GPE in any contexts.
386 * Warning: do not move the check into advance_transaction() as the
387 * EC commands will be sent without GPE raised.
388 */
389 if (!acpi_ec_is_gpe_raised(ec))
390 return;
391 acpi_clear_gpe(NULL, ec->gpe);
392}
393
394/* --------------------------------------------------------------------------
395 * Transaction Management
396 * -------------------------------------------------------------------------- */
397
398static void acpi_ec_submit_request(struct acpi_ec *ec)
399{
400 ec->reference_count++;
401 if (test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags) &&
402 ec->reference_count == 1)
403 acpi_ec_enable_gpe(ec, true);
404}
405
406static void acpi_ec_complete_request(struct acpi_ec *ec)
407{
408 bool flushed = false;
409
410 ec->reference_count--;
411 if (test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags) &&
412 ec->reference_count == 0)
413 acpi_ec_disable_gpe(ec, true);
414 flushed = acpi_ec_flushed(ec);
415 if (flushed)
416 wake_up(&ec->wait);
417}
418
419static void acpi_ec_mask_gpe(struct acpi_ec *ec)
420{
421 if (!test_bit(EC_FLAGS_GPE_MASKED, &ec->flags)) {
422 acpi_ec_disable_gpe(ec, false);
423 ec_dbg_drv("Polling enabled");
424 set_bit(EC_FLAGS_GPE_MASKED, &ec->flags);
425 }
426}
427
428static void acpi_ec_unmask_gpe(struct acpi_ec *ec)
429{
430 if (test_bit(EC_FLAGS_GPE_MASKED, &ec->flags)) {
431 clear_bit(EC_FLAGS_GPE_MASKED, &ec->flags);
432 acpi_ec_enable_gpe(ec, false);
433 ec_dbg_drv("Polling disabled");
434 }
435}
436
437/*
438 * acpi_ec_submit_flushable_request() - Increase the reference count unless
439 * the flush operation is not in
440 * progress
441 * @ec: the EC device
442 *
443 * This function must be used before taking a new action that should hold
444 * the reference count. If this function returns false, then the action
445 * must be discarded or it will prevent the flush operation from being
446 * completed.
447 */
448static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec)
449{
450 if (!acpi_ec_started(ec))
451 return false;
452 acpi_ec_submit_request(ec);
453 return true;
454}
455
456static void acpi_ec_submit_query(struct acpi_ec *ec)
457{
458 acpi_ec_mask_gpe(ec);
459 if (!acpi_ec_event_enabled(ec))
460 return;
461 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
462 ec_dbg_evt("Command(%s) submitted/blocked",
463 acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
464 ec->nr_pending_queries++;
Olivier Deprez0e641232021-09-23 10:07:05 +0200465 queue_work(ec_wq, &ec->work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000466 }
467}
468
469static void acpi_ec_complete_query(struct acpi_ec *ec)
470{
471 if (test_and_clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
472 ec_dbg_evt("Command(%s) unblocked",
473 acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
474 acpi_ec_unmask_gpe(ec);
475}
476
477static inline void __acpi_ec_enable_event(struct acpi_ec *ec)
478{
479 if (!test_and_set_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags))
480 ec_log_drv("event unblocked");
481 /*
482 * Unconditionally invoke this once after enabling the event
483 * handling mechanism to detect the pending events.
484 */
485 advance_transaction(ec);
486}
487
488static inline void __acpi_ec_disable_event(struct acpi_ec *ec)
489{
490 if (test_and_clear_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags))
491 ec_log_drv("event blocked");
492}
493
David Brazdil0f672f62019-12-10 10:32:29 +0000494/*
495 * Process _Q events that might have accumulated in the EC.
496 * Run with locked ec mutex.
497 */
498static void acpi_ec_clear(struct acpi_ec *ec)
499{
500 int i, status;
501 u8 value = 0;
502
503 for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
504 status = acpi_ec_query(ec, &value);
505 if (status || !value)
506 break;
507 }
508 if (unlikely(i == ACPI_EC_CLEAR_MAX))
509 pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
510 else
511 pr_info("%d stale EC events cleared\n", i);
512}
513
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000514static void acpi_ec_enable_event(struct acpi_ec *ec)
515{
516 unsigned long flags;
517
518 spin_lock_irqsave(&ec->lock, flags);
519 if (acpi_ec_started(ec))
520 __acpi_ec_enable_event(ec);
521 spin_unlock_irqrestore(&ec->lock, flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000522
523 /* Drain additional events if hardware requires that */
524 if (EC_FLAGS_CLEAR_ON_RESUME)
525 acpi_ec_clear(ec);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000526}
527
528#ifdef CONFIG_PM_SLEEP
Olivier Deprez0e641232021-09-23 10:07:05 +0200529static void __acpi_ec_flush_work(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000530{
Olivier Deprez0e641232021-09-23 10:07:05 +0200531 drain_workqueue(ec_wq); /* flush ec->work */
532 flush_workqueue(ec_query_wq); /* flush queries */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000533}
534
535static void acpi_ec_disable_event(struct acpi_ec *ec)
536{
537 unsigned long flags;
538
539 spin_lock_irqsave(&ec->lock, flags);
540 __acpi_ec_disable_event(ec);
541 spin_unlock_irqrestore(&ec->lock, flags);
Olivier Deprez0e641232021-09-23 10:07:05 +0200542
543 /*
544 * When ec_freeze_events is true, we need to flush events in
545 * the proper position before entering the noirq stage.
546 */
547 __acpi_ec_flush_work();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000548}
549
550void acpi_ec_flush_work(void)
551{
Olivier Deprez0e641232021-09-23 10:07:05 +0200552 /* Without ec_wq there is nothing to flush. */
553 if (!ec_wq)
554 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000555
Olivier Deprez0e641232021-09-23 10:07:05 +0200556 __acpi_ec_flush_work();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000557}
558#endif /* CONFIG_PM_SLEEP */
559
560static bool acpi_ec_guard_event(struct acpi_ec *ec)
561{
562 bool guarded = true;
563 unsigned long flags;
564
565 spin_lock_irqsave(&ec->lock, flags);
566 /*
567 * If firmware SCI_EVT clearing timing is "event", we actually
568 * don't know when the SCI_EVT will be cleared by firmware after
569 * evaluating _Qxx, so we need to re-check SCI_EVT after waiting an
570 * acceptable period.
571 *
572 * The guarding period begins when EC_FLAGS_QUERY_PENDING is
573 * flagged, which means SCI_EVT check has just been performed.
574 * But if the current transaction is ACPI_EC_COMMAND_QUERY, the
575 * guarding should have already been performed (via
576 * EC_FLAGS_QUERY_GUARDING) and should not be applied so that the
577 * ACPI_EC_COMMAND_QUERY transaction can be transitioned into
578 * ACPI_EC_COMMAND_POLL state immediately.
579 */
580 if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS ||
581 ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY ||
582 !test_bit(EC_FLAGS_QUERY_PENDING, &ec->flags) ||
583 (ec->curr && ec->curr->command == ACPI_EC_COMMAND_QUERY))
584 guarded = false;
585 spin_unlock_irqrestore(&ec->lock, flags);
586 return guarded;
587}
588
589static int ec_transaction_polled(struct acpi_ec *ec)
590{
591 unsigned long flags;
592 int ret = 0;
593
594 spin_lock_irqsave(&ec->lock, flags);
595 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_POLL))
596 ret = 1;
597 spin_unlock_irqrestore(&ec->lock, flags);
598 return ret;
599}
600
601static int ec_transaction_completed(struct acpi_ec *ec)
602{
603 unsigned long flags;
604 int ret = 0;
605
606 spin_lock_irqsave(&ec->lock, flags);
607 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
608 ret = 1;
609 spin_unlock_irqrestore(&ec->lock, flags);
610 return ret;
611}
612
613static inline void ec_transaction_transition(struct acpi_ec *ec, unsigned long flag)
614{
615 ec->curr->flags |= flag;
616 if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
617 if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS &&
618 flag == ACPI_EC_COMMAND_POLL)
619 acpi_ec_complete_query(ec);
620 if (ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY &&
621 flag == ACPI_EC_COMMAND_COMPLETE)
622 acpi_ec_complete_query(ec);
623 if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT &&
624 flag == ACPI_EC_COMMAND_COMPLETE)
625 set_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags);
626 }
627}
628
629static void advance_transaction(struct acpi_ec *ec)
630{
631 struct transaction *t;
632 u8 status;
633 bool wakeup = false;
634
635 ec_dbg_stm("%s (%d)", in_interrupt() ? "IRQ" : "TASK",
636 smp_processor_id());
637 /*
638 * By always clearing STS before handling all indications, we can
639 * ensure a hardware STS 0->1 change after this clearing can always
640 * trigger a GPE interrupt.
641 */
642 acpi_ec_clear_gpe(ec);
643 status = acpi_ec_read_status(ec);
644 t = ec->curr;
645 /*
646 * Another IRQ or a guarded polling mode advancement is detected,
647 * the next QR_EC submission is then allowed.
648 */
649 if (!t || !(t->flags & ACPI_EC_COMMAND_POLL)) {
650 if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT &&
651 (!ec->nr_pending_queries ||
652 test_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags))) {
653 clear_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags);
654 acpi_ec_complete_query(ec);
655 }
656 }
657 if (!t)
658 goto err;
659 if (t->flags & ACPI_EC_COMMAND_POLL) {
660 if (t->wlen > t->wi) {
661 if ((status & ACPI_EC_FLAG_IBF) == 0)
662 acpi_ec_write_data(ec, t->wdata[t->wi++]);
663 else
664 goto err;
665 } else if (t->rlen > t->ri) {
666 if ((status & ACPI_EC_FLAG_OBF) == 1) {
667 t->rdata[t->ri++] = acpi_ec_read_data(ec);
668 if (t->rlen == t->ri) {
669 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE);
670 if (t->command == ACPI_EC_COMMAND_QUERY)
671 ec_dbg_evt("Command(%s) completed by hardware",
672 acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
673 wakeup = true;
674 }
675 } else
676 goto err;
677 } else if (t->wlen == t->wi &&
678 (status & ACPI_EC_FLAG_IBF) == 0) {
679 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE);
680 wakeup = true;
681 }
682 goto out;
683 } else {
684 if (EC_FLAGS_QUERY_HANDSHAKE &&
685 !(status & ACPI_EC_FLAG_SCI) &&
686 (t->command == ACPI_EC_COMMAND_QUERY)) {
687 ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL);
688 t->rdata[t->ri++] = 0x00;
689 ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE);
690 ec_dbg_evt("Command(%s) completed by software",
691 acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
692 wakeup = true;
693 } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
694 acpi_ec_write_cmd(ec, t->command);
695 ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL);
696 } else
697 goto err;
698 goto out;
699 }
700err:
701 /*
702 * If SCI bit is set, then don't think it's a false IRQ
703 * otherwise will take a not handled IRQ as a false one.
704 */
705 if (!(status & ACPI_EC_FLAG_SCI)) {
706 if (in_interrupt() && t) {
707 if (t->irq_count < ec_storm_threshold)
708 ++t->irq_count;
709 /* Allow triggering on 0 threshold */
710 if (t->irq_count == ec_storm_threshold)
711 acpi_ec_mask_gpe(ec);
712 }
713 }
714out:
715 if (status & ACPI_EC_FLAG_SCI)
716 acpi_ec_submit_query(ec);
717 if (wakeup && in_interrupt())
718 wake_up(&ec->wait);
719}
720
721static void start_transaction(struct acpi_ec *ec)
722{
723 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
724 ec->curr->flags = 0;
725}
726
727static int ec_guard(struct acpi_ec *ec)
728{
729 unsigned long guard = usecs_to_jiffies(ec->polling_guard);
730 unsigned long timeout = ec->timestamp + guard;
731
732 /* Ensure guarding period before polling EC status */
733 do {
734 if (ec->busy_polling) {
735 /* Perform busy polling */
736 if (ec_transaction_completed(ec))
737 return 0;
738 udelay(jiffies_to_usecs(guard));
739 } else {
740 /*
741 * Perform wait polling
742 * 1. Wait the transaction to be completed by the
743 * GPE handler after the transaction enters
744 * ACPI_EC_COMMAND_POLL state.
745 * 2. A special guarding logic is also required
746 * for event clearing mode "event" before the
747 * transaction enters ACPI_EC_COMMAND_POLL
748 * state.
749 */
750 if (!ec_transaction_polled(ec) &&
751 !acpi_ec_guard_event(ec))
752 break;
753 if (wait_event_timeout(ec->wait,
754 ec_transaction_completed(ec),
755 guard))
756 return 0;
757 }
758 } while (time_before(jiffies, timeout));
759 return -ETIME;
760}
761
762static int ec_poll(struct acpi_ec *ec)
763{
764 unsigned long flags;
765 int repeat = 5; /* number of command restarts */
766
767 while (repeat--) {
768 unsigned long delay = jiffies +
769 msecs_to_jiffies(ec_delay);
770 do {
771 if (!ec_guard(ec))
772 return 0;
773 spin_lock_irqsave(&ec->lock, flags);
774 advance_transaction(ec);
775 spin_unlock_irqrestore(&ec->lock, flags);
776 } while (time_before(jiffies, delay));
777 pr_debug("controller reset, restart transaction\n");
778 spin_lock_irqsave(&ec->lock, flags);
779 start_transaction(ec);
780 spin_unlock_irqrestore(&ec->lock, flags);
781 }
782 return -ETIME;
783}
784
785static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
786 struct transaction *t)
787{
788 unsigned long tmp;
789 int ret = 0;
790
791 /* start transaction */
792 spin_lock_irqsave(&ec->lock, tmp);
793 /* Enable GPE for command processing (IBF=0/OBF=1) */
794 if (!acpi_ec_submit_flushable_request(ec)) {
795 ret = -EINVAL;
796 goto unlock;
797 }
798 ec_dbg_ref(ec, "Increase command");
799 /* following two actions should be kept atomic */
800 ec->curr = t;
801 ec_dbg_req("Command(%s) started", acpi_ec_cmd_string(t->command));
802 start_transaction(ec);
803 spin_unlock_irqrestore(&ec->lock, tmp);
804
805 ret = ec_poll(ec);
806
807 spin_lock_irqsave(&ec->lock, tmp);
808 if (t->irq_count == ec_storm_threshold)
809 acpi_ec_unmask_gpe(ec);
810 ec_dbg_req("Command(%s) stopped", acpi_ec_cmd_string(t->command));
811 ec->curr = NULL;
812 /* Disable GPE for command processing (IBF=0/OBF=1) */
813 acpi_ec_complete_request(ec);
814 ec_dbg_ref(ec, "Decrease command");
815unlock:
816 spin_unlock_irqrestore(&ec->lock, tmp);
817 return ret;
818}
819
820static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
821{
822 int status;
823 u32 glk;
824
825 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
826 return -EINVAL;
827 if (t->rdata)
828 memset(t->rdata, 0, t->rlen);
829
830 mutex_lock(&ec->mutex);
831 if (ec->global_lock) {
832 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
833 if (ACPI_FAILURE(status)) {
834 status = -ENODEV;
835 goto unlock;
836 }
837 }
838
839 status = acpi_ec_transaction_unlocked(ec, t);
840
841 if (ec->global_lock)
842 acpi_release_global_lock(glk);
843unlock:
844 mutex_unlock(&ec->mutex);
845 return status;
846}
847
848static int acpi_ec_burst_enable(struct acpi_ec *ec)
849{
850 u8 d;
851 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
852 .wdata = NULL, .rdata = &d,
853 .wlen = 0, .rlen = 1};
854
855 return acpi_ec_transaction(ec, &t);
856}
857
858static int acpi_ec_burst_disable(struct acpi_ec *ec)
859{
860 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
861 .wdata = NULL, .rdata = NULL,
862 .wlen = 0, .rlen = 0};
863
864 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
865 acpi_ec_transaction(ec, &t) : 0;
866}
867
868static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
869{
870 int result;
871 u8 d;
872 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
873 .wdata = &address, .rdata = &d,
874 .wlen = 1, .rlen = 1};
875
876 result = acpi_ec_transaction(ec, &t);
877 *data = d;
878 return result;
879}
880
881static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
882{
883 u8 wdata[2] = { address, data };
884 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
885 .wdata = wdata, .rdata = NULL,
886 .wlen = 2, .rlen = 0};
887
888 return acpi_ec_transaction(ec, &t);
889}
890
891int ec_read(u8 addr, u8 *val)
892{
893 int err;
894 u8 temp_data;
895
896 if (!first_ec)
897 return -ENODEV;
898
899 err = acpi_ec_read(first_ec, addr, &temp_data);
900
901 if (!err) {
902 *val = temp_data;
903 return 0;
904 }
905 return err;
906}
907EXPORT_SYMBOL(ec_read);
908
909int ec_write(u8 addr, u8 val)
910{
911 int err;
912
913 if (!first_ec)
914 return -ENODEV;
915
916 err = acpi_ec_write(first_ec, addr, val);
917
918 return err;
919}
920EXPORT_SYMBOL(ec_write);
921
922int ec_transaction(u8 command,
923 const u8 *wdata, unsigned wdata_len,
924 u8 *rdata, unsigned rdata_len)
925{
926 struct transaction t = {.command = command,
927 .wdata = wdata, .rdata = rdata,
928 .wlen = wdata_len, .rlen = rdata_len};
929
930 if (!first_ec)
931 return -ENODEV;
932
933 return acpi_ec_transaction(first_ec, &t);
934}
935EXPORT_SYMBOL(ec_transaction);
936
937/* Get the handle to the EC device */
938acpi_handle ec_get_handle(void)
939{
940 if (!first_ec)
941 return NULL;
942 return first_ec->handle;
943}
944EXPORT_SYMBOL(ec_get_handle);
945
946static void acpi_ec_start(struct acpi_ec *ec, bool resuming)
947{
948 unsigned long flags;
949
950 spin_lock_irqsave(&ec->lock, flags);
951 if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) {
952 ec_dbg_drv("Starting EC");
953 /* Enable GPE for event processing (SCI_EVT=1) */
954 if (!resuming) {
955 acpi_ec_submit_request(ec);
956 ec_dbg_ref(ec, "Increase driver");
957 }
958 ec_log_drv("EC started");
959 }
960 spin_unlock_irqrestore(&ec->lock, flags);
961}
962
963static bool acpi_ec_stopped(struct acpi_ec *ec)
964{
965 unsigned long flags;
966 bool flushed;
967
968 spin_lock_irqsave(&ec->lock, flags);
969 flushed = acpi_ec_flushed(ec);
970 spin_unlock_irqrestore(&ec->lock, flags);
971 return flushed;
972}
973
974static void acpi_ec_stop(struct acpi_ec *ec, bool suspending)
975{
976 unsigned long flags;
977
978 spin_lock_irqsave(&ec->lock, flags);
979 if (acpi_ec_started(ec)) {
980 ec_dbg_drv("Stopping EC");
981 set_bit(EC_FLAGS_STOPPED, &ec->flags);
982 spin_unlock_irqrestore(&ec->lock, flags);
983 wait_event(ec->wait, acpi_ec_stopped(ec));
984 spin_lock_irqsave(&ec->lock, flags);
985 /* Disable GPE for event processing (SCI_EVT=1) */
986 if (!suspending) {
987 acpi_ec_complete_request(ec);
988 ec_dbg_ref(ec, "Decrease driver");
989 } else if (!ec_freeze_events)
990 __acpi_ec_disable_event(ec);
991 clear_bit(EC_FLAGS_STARTED, &ec->flags);
992 clear_bit(EC_FLAGS_STOPPED, &ec->flags);
993 ec_log_drv("EC stopped");
994 }
995 spin_unlock_irqrestore(&ec->lock, flags);
996}
997
998static void acpi_ec_enter_noirq(struct acpi_ec *ec)
999{
1000 unsigned long flags;
1001
1002 spin_lock_irqsave(&ec->lock, flags);
1003 ec->busy_polling = true;
1004 ec->polling_guard = 0;
1005 ec_log_drv("interrupt blocked");
1006 spin_unlock_irqrestore(&ec->lock, flags);
1007}
1008
1009static void acpi_ec_leave_noirq(struct acpi_ec *ec)
1010{
1011 unsigned long flags;
1012
1013 spin_lock_irqsave(&ec->lock, flags);
1014 ec->busy_polling = ec_busy_polling;
1015 ec->polling_guard = ec_polling_guard;
1016 ec_log_drv("interrupt unblocked");
1017 spin_unlock_irqrestore(&ec->lock, flags);
1018}
1019
1020void acpi_ec_block_transactions(void)
1021{
1022 struct acpi_ec *ec = first_ec;
1023
1024 if (!ec)
1025 return;
1026
1027 mutex_lock(&ec->mutex);
1028 /* Prevent transactions from being carried out */
1029 acpi_ec_stop(ec, true);
1030 mutex_unlock(&ec->mutex);
1031}
1032
1033void acpi_ec_unblock_transactions(void)
1034{
1035 /*
1036 * Allow transactions to happen again (this function is called from
1037 * atomic context during wakeup, so we don't need to acquire the mutex).
1038 */
1039 if (first_ec)
1040 acpi_ec_start(first_ec, true);
1041}
1042
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001043/* --------------------------------------------------------------------------
1044 Event Management
1045 -------------------------------------------------------------------------- */
1046static struct acpi_ec_query_handler *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001047acpi_ec_get_query_handler_by_value(struct acpi_ec *ec, u8 value)
1048{
1049 struct acpi_ec_query_handler *handler;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001050
1051 mutex_lock(&ec->mutex);
1052 list_for_each_entry(handler, &ec->list, node) {
1053 if (value == handler->query_bit) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001054 kref_get(&handler->kref);
1055 mutex_unlock(&ec->mutex);
1056 return handler;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001057 }
1058 }
1059 mutex_unlock(&ec->mutex);
Olivier Deprez0e641232021-09-23 10:07:05 +02001060 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001061}
1062
1063static void acpi_ec_query_handler_release(struct kref *kref)
1064{
1065 struct acpi_ec_query_handler *handler =
1066 container_of(kref, struct acpi_ec_query_handler, kref);
1067
1068 kfree(handler);
1069}
1070
1071static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler)
1072{
1073 kref_put(&handler->kref, acpi_ec_query_handler_release);
1074}
1075
1076int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
1077 acpi_handle handle, acpi_ec_query_func func,
1078 void *data)
1079{
1080 struct acpi_ec_query_handler *handler =
1081 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
1082
1083 if (!handler)
1084 return -ENOMEM;
1085
1086 handler->query_bit = query_bit;
1087 handler->handle = handle;
1088 handler->func = func;
1089 handler->data = data;
1090 mutex_lock(&ec->mutex);
1091 kref_init(&handler->kref);
1092 list_add(&handler->node, &ec->list);
1093 mutex_unlock(&ec->mutex);
1094 return 0;
1095}
1096EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
1097
1098static void acpi_ec_remove_query_handlers(struct acpi_ec *ec,
1099 bool remove_all, u8 query_bit)
1100{
1101 struct acpi_ec_query_handler *handler, *tmp;
1102 LIST_HEAD(free_list);
1103
1104 mutex_lock(&ec->mutex);
1105 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
1106 if (remove_all || query_bit == handler->query_bit) {
1107 list_del_init(&handler->node);
1108 list_add(&handler->node, &free_list);
1109 }
1110 }
1111 mutex_unlock(&ec->mutex);
1112 list_for_each_entry_safe(handler, tmp, &free_list, node)
1113 acpi_ec_put_query_handler(handler);
1114}
1115
1116void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
1117{
1118 acpi_ec_remove_query_handlers(ec, false, query_bit);
1119}
1120EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
1121
1122static struct acpi_ec_query *acpi_ec_create_query(u8 *pval)
1123{
1124 struct acpi_ec_query *q;
1125 struct transaction *t;
1126
1127 q = kzalloc(sizeof (struct acpi_ec_query), GFP_KERNEL);
1128 if (!q)
1129 return NULL;
1130 INIT_WORK(&q->work, acpi_ec_event_processor);
1131 t = &q->transaction;
1132 t->command = ACPI_EC_COMMAND_QUERY;
1133 t->rdata = pval;
1134 t->rlen = 1;
1135 return q;
1136}
1137
1138static void acpi_ec_delete_query(struct acpi_ec_query *q)
1139{
1140 if (q) {
1141 if (q->handler)
1142 acpi_ec_put_query_handler(q->handler);
1143 kfree(q);
1144 }
1145}
1146
1147static void acpi_ec_event_processor(struct work_struct *work)
1148{
1149 struct acpi_ec_query *q = container_of(work, struct acpi_ec_query, work);
1150 struct acpi_ec_query_handler *handler = q->handler;
1151
1152 ec_dbg_evt("Query(0x%02x) started", handler->query_bit);
1153 if (handler->func)
1154 handler->func(handler->data);
1155 else if (handler->handle)
1156 acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
1157 ec_dbg_evt("Query(0x%02x) stopped", handler->query_bit);
1158 acpi_ec_delete_query(q);
1159}
1160
1161static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
1162{
1163 u8 value = 0;
1164 int result;
1165 struct acpi_ec_query *q;
1166
1167 q = acpi_ec_create_query(&value);
1168 if (!q)
1169 return -ENOMEM;
1170
1171 /*
1172 * Query the EC to find out which _Qxx method we need to evaluate.
1173 * Note that successful completion of the query causes the ACPI_EC_SCI
1174 * bit to be cleared (and thus clearing the interrupt source).
1175 */
1176 result = acpi_ec_transaction(ec, &q->transaction);
1177 if (!value)
1178 result = -ENODATA;
1179 if (result)
1180 goto err_exit;
1181
1182 q->handler = acpi_ec_get_query_handler_by_value(ec, value);
1183 if (!q->handler) {
1184 result = -ENODATA;
1185 goto err_exit;
1186 }
1187
1188 /*
1189 * It is reported that _Qxx are evaluated in a parallel way on
1190 * Windows:
1191 * https://bugzilla.kernel.org/show_bug.cgi?id=94411
1192 *
1193 * Put this log entry before schedule_work() in order to make
1194 * it appearing before any other log entries occurred during the
1195 * work queue execution.
1196 */
1197 ec_dbg_evt("Query(0x%02x) scheduled", value);
1198 if (!queue_work(ec_query_wq, &q->work)) {
1199 ec_dbg_evt("Query(0x%02x) overlapped", value);
1200 result = -EBUSY;
1201 }
1202
1203err_exit:
1204 if (result)
1205 acpi_ec_delete_query(q);
1206 if (data)
1207 *data = value;
1208 return result;
1209}
1210
1211static void acpi_ec_check_event(struct acpi_ec *ec)
1212{
1213 unsigned long flags;
1214
1215 if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT) {
1216 if (ec_guard(ec)) {
1217 spin_lock_irqsave(&ec->lock, flags);
1218 /*
1219 * Take care of the SCI_EVT unless no one else is
1220 * taking care of it.
1221 */
1222 if (!ec->curr)
1223 advance_transaction(ec);
1224 spin_unlock_irqrestore(&ec->lock, flags);
1225 }
1226 }
1227}
1228
1229static void acpi_ec_event_handler(struct work_struct *work)
1230{
1231 unsigned long flags;
1232 struct acpi_ec *ec = container_of(work, struct acpi_ec, work);
1233
1234 ec_dbg_evt("Event started");
1235
1236 spin_lock_irqsave(&ec->lock, flags);
1237 while (ec->nr_pending_queries) {
1238 spin_unlock_irqrestore(&ec->lock, flags);
1239 (void)acpi_ec_query(ec, NULL);
1240 spin_lock_irqsave(&ec->lock, flags);
1241 ec->nr_pending_queries--;
1242 /*
1243 * Before exit, make sure that this work item can be
1244 * scheduled again. There might be QR_EC failures, leaving
1245 * EC_FLAGS_QUERY_PENDING uncleared and preventing this work
1246 * item from being scheduled again.
1247 */
1248 if (!ec->nr_pending_queries) {
1249 if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS ||
1250 ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY)
1251 acpi_ec_complete_query(ec);
1252 }
1253 }
1254 spin_unlock_irqrestore(&ec->lock, flags);
1255
1256 ec_dbg_evt("Event stopped");
1257
1258 acpi_ec_check_event(ec);
1259}
1260
1261static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
1262 u32 gpe_number, void *data)
1263{
1264 unsigned long flags;
1265 struct acpi_ec *ec = data;
1266
1267 spin_lock_irqsave(&ec->lock, flags);
1268 advance_transaction(ec);
1269 spin_unlock_irqrestore(&ec->lock, flags);
1270 return ACPI_INTERRUPT_HANDLED;
1271}
1272
1273/* --------------------------------------------------------------------------
1274 * Address Space Management
1275 * -------------------------------------------------------------------------- */
1276
1277static acpi_status
1278acpi_ec_space_handler(u32 function, acpi_physical_address address,
1279 u32 bits, u64 *value64,
1280 void *handler_context, void *region_context)
1281{
1282 struct acpi_ec *ec = handler_context;
1283 int result = 0, i, bytes = bits / 8;
1284 u8 *value = (u8 *)value64;
1285
1286 if ((address > 0xFF) || !value || !handler_context)
1287 return AE_BAD_PARAMETER;
1288
1289 if (function != ACPI_READ && function != ACPI_WRITE)
1290 return AE_BAD_PARAMETER;
1291
1292 if (ec->busy_polling || bits > 8)
1293 acpi_ec_burst_enable(ec);
1294
1295 for (i = 0; i < bytes; ++i, ++address, ++value)
1296 result = (function == ACPI_READ) ?
1297 acpi_ec_read(ec, address, value) :
1298 acpi_ec_write(ec, address, *value);
1299
1300 if (ec->busy_polling || bits > 8)
1301 acpi_ec_burst_disable(ec);
1302
1303 switch (result) {
1304 case -EINVAL:
1305 return AE_BAD_PARAMETER;
1306 case -ENODEV:
1307 return AE_NOT_FOUND;
1308 case -ETIME:
1309 return AE_TIME;
1310 default:
1311 return AE_OK;
1312 }
1313}
1314
1315/* --------------------------------------------------------------------------
1316 * Driver Interface
1317 * -------------------------------------------------------------------------- */
1318
1319static acpi_status
1320ec_parse_io_ports(struct acpi_resource *resource, void *context);
1321
1322static void acpi_ec_free(struct acpi_ec *ec)
1323{
1324 if (first_ec == ec)
1325 first_ec = NULL;
1326 if (boot_ec == ec)
1327 boot_ec = NULL;
1328 kfree(ec);
1329}
1330
1331static struct acpi_ec *acpi_ec_alloc(void)
1332{
1333 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1334
1335 if (!ec)
1336 return NULL;
1337 mutex_init(&ec->mutex);
1338 init_waitqueue_head(&ec->wait);
1339 INIT_LIST_HEAD(&ec->list);
1340 spin_lock_init(&ec->lock);
1341 INIT_WORK(&ec->work, acpi_ec_event_handler);
1342 ec->timestamp = jiffies;
1343 ec->busy_polling = true;
1344 ec->polling_guard = 0;
1345 return ec;
1346}
1347
1348static acpi_status
1349acpi_ec_register_query_methods(acpi_handle handle, u32 level,
1350 void *context, void **return_value)
1351{
1352 char node_name[5];
1353 struct acpi_buffer buffer = { sizeof(node_name), node_name };
1354 struct acpi_ec *ec = context;
1355 int value = 0;
1356 acpi_status status;
1357
1358 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
1359
1360 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1)
1361 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
1362 return AE_OK;
1363}
1364
1365static acpi_status
1366ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
1367{
1368 acpi_status status;
1369 unsigned long long tmp = 0;
1370 struct acpi_ec *ec = context;
1371
1372 /* clear addr values, ec_parse_io_ports depend on it */
1373 ec->command_addr = ec->data_addr = 0;
1374
1375 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1376 ec_parse_io_ports, ec);
1377 if (ACPI_FAILURE(status))
1378 return status;
1379 if (ec->data_addr == 0 || ec->command_addr == 0)
1380 return AE_OK;
1381
1382 if (boot_ec && boot_ec_is_ecdt && EC_FLAGS_IGNORE_DSDT_GPE) {
1383 /*
1384 * Always inherit the GPE number setting from the ECDT
1385 * EC.
1386 */
1387 ec->gpe = boot_ec->gpe;
1388 } else {
1389 /* Get GPE bit assignment (EC events). */
1390 /* TODO: Add support for _GPE returning a package */
1391 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
1392 if (ACPI_FAILURE(status))
1393 return status;
1394 ec->gpe = tmp;
1395 }
1396 /* Use the global lock for all EC transactions? */
1397 tmp = 0;
1398 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
1399 ec->global_lock = tmp;
1400 ec->handle = handle;
1401 return AE_CTRL_TERMINATE;
1402}
1403
1404/*
1405 * Note: This function returns an error code only when the address space
1406 * handler is not installed, which means "not able to handle
1407 * transactions".
1408 */
1409static int ec_install_handlers(struct acpi_ec *ec, bool handle_events)
1410{
1411 acpi_status status;
1412
1413 acpi_ec_start(ec, false);
1414
1415 if (!test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) {
1416 acpi_ec_enter_noirq(ec);
1417 status = acpi_install_address_space_handler(ec->handle,
1418 ACPI_ADR_SPACE_EC,
1419 &acpi_ec_space_handler,
1420 NULL, ec);
1421 if (ACPI_FAILURE(status)) {
1422 if (status == AE_NOT_FOUND) {
1423 /*
1424 * Maybe OS fails in evaluating the _REG
1425 * object. The AE_NOT_FOUND error will be
1426 * ignored and OS * continue to initialize
1427 * EC.
1428 */
1429 pr_err("Fail in evaluating the _REG object"
1430 " of EC device. Broken bios is suspected.\n");
1431 } else {
1432 acpi_ec_stop(ec, false);
1433 return -ENODEV;
1434 }
1435 }
1436 set_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags);
1437 }
1438
1439 if (!handle_events)
1440 return 0;
1441
1442 if (!test_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags)) {
1443 /* Find and register all query methods */
1444 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
1445 acpi_ec_register_query_methods,
1446 NULL, ec, NULL);
1447 set_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags);
1448 }
1449 if (!test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags)) {
1450 status = acpi_install_gpe_raw_handler(NULL, ec->gpe,
1451 ACPI_GPE_EDGE_TRIGGERED,
1452 &acpi_ec_gpe_handler, ec);
1453 /* This is not fatal as we can poll EC events */
1454 if (ACPI_SUCCESS(status)) {
1455 set_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags);
1456 acpi_ec_leave_noirq(ec);
1457 if (test_bit(EC_FLAGS_STARTED, &ec->flags) &&
1458 ec->reference_count >= 1)
1459 acpi_ec_enable_gpe(ec, true);
1460 }
1461 }
1462 /* EC is fully operational, allow queries */
1463 acpi_ec_enable_event(ec);
1464
1465 return 0;
1466}
1467
1468static void ec_remove_handlers(struct acpi_ec *ec)
1469{
1470 if (test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) {
1471 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
1472 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
1473 pr_err("failed to remove space handler\n");
1474 clear_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags);
1475 }
1476
1477 /*
1478 * Stops handling the EC transactions after removing the operation
1479 * region handler. This is required because _REG(DISCONNECT)
1480 * invoked during the removal can result in new EC transactions.
1481 *
1482 * Flushes the EC requests and thus disables the GPE before
1483 * removing the GPE handler. This is required by the current ACPICA
1484 * GPE core. ACPICA GPE core will automatically disable a GPE when
1485 * it is indicated but there is no way to handle it. So the drivers
1486 * must disable the GPEs prior to removing the GPE handlers.
1487 */
1488 acpi_ec_stop(ec, false);
1489
1490 if (test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags)) {
1491 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
1492 &acpi_ec_gpe_handler)))
1493 pr_err("failed to remove gpe handler\n");
1494 clear_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags);
1495 }
1496 if (test_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags)) {
1497 acpi_ec_remove_query_handlers(ec, true, 0);
1498 clear_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags);
1499 }
1500}
1501
1502static int acpi_ec_setup(struct acpi_ec *ec, bool handle_events)
1503{
1504 int ret;
1505
1506 ret = ec_install_handlers(ec, handle_events);
1507 if (ret)
1508 return ret;
1509
1510 /* First EC capable of handling transactions */
1511 if (!first_ec) {
1512 first_ec = ec;
1513 acpi_handle_info(first_ec->handle, "Used as first EC\n");
1514 }
1515
1516 acpi_handle_info(ec->handle,
1517 "GPE=0x%x, EC_CMD/EC_SC=0x%lx, EC_DATA=0x%lx\n",
1518 ec->gpe, ec->command_addr, ec->data_addr);
1519 return ret;
1520}
1521
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001522static bool acpi_ec_ecdt_get_handle(acpi_handle *phandle)
1523{
1524 struct acpi_table_ecdt *ecdt_ptr;
1525 acpi_status status;
1526 acpi_handle handle;
1527
1528 status = acpi_get_table(ACPI_SIG_ECDT, 1,
1529 (struct acpi_table_header **)&ecdt_ptr);
1530 if (ACPI_FAILURE(status))
1531 return false;
1532
1533 status = acpi_get_handle(NULL, ecdt_ptr->id, &handle);
1534 if (ACPI_FAILURE(status))
1535 return false;
1536
1537 *phandle = handle;
1538 return true;
1539}
1540
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001541static int acpi_ec_add(struct acpi_device *device)
1542{
1543 struct acpi_ec *ec = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00001544 bool dep_update = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001545 acpi_status status;
David Brazdil0f672f62019-12-10 10:32:29 +00001546 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001547
1548 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1549 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1550
1551 if (!strcmp(acpi_device_hid(device), ACPI_ECDT_HID)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001552 boot_ec_is_ecdt = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001553 ec = boot_ec;
David Brazdil0f672f62019-12-10 10:32:29 +00001554 dep_update = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001555 } else {
1556 ec = acpi_ec_alloc();
1557 if (!ec)
1558 return -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +00001559
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001560 status = ec_parse_device(device->handle, 0, ec, NULL);
1561 if (status != AE_CTRL_TERMINATE) {
1562 ret = -EINVAL;
1563 goto err_alloc;
1564 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001565
David Brazdil0f672f62019-12-10 10:32:29 +00001566 if (boot_ec && ec->command_addr == boot_ec->command_addr &&
1567 ec->data_addr == boot_ec->data_addr) {
1568 boot_ec_is_ecdt = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001569 /*
1570 * Trust PNP0C09 namespace location rather than
1571 * ECDT ID. But trust ECDT GPE rather than _GPE
1572 * because of ASUS quirks, so do not change
1573 * boot_ec->gpe to ec->gpe.
1574 */
1575 boot_ec->handle = ec->handle;
1576 acpi_handle_debug(ec->handle, "duplicated.\n");
1577 acpi_ec_free(ec);
1578 ec = boot_ec;
1579 }
David Brazdil0f672f62019-12-10 10:32:29 +00001580 }
1581
1582 ret = acpi_ec_setup(ec, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001583 if (ret)
1584 goto err_query;
1585
David Brazdil0f672f62019-12-10 10:32:29 +00001586 if (ec == boot_ec)
1587 acpi_handle_info(boot_ec->handle,
1588 "Boot %s EC used to handle transactions and events\n",
1589 boot_ec_is_ecdt ? "ECDT" : "DSDT");
1590
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001591 device->driver_data = ec;
1592
1593 ret = !!request_region(ec->data_addr, 1, "EC data");
1594 WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
1595 ret = !!request_region(ec->command_addr, 1, "EC cmd");
1596 WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
1597
David Brazdil0f672f62019-12-10 10:32:29 +00001598 if (dep_update) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001599 /* Reprobe devices depending on the EC */
1600 acpi_walk_dep_device_list(ec->handle);
1601 }
1602 acpi_handle_debug(ec->handle, "enumerated.\n");
1603 return 0;
1604
1605err_query:
1606 if (ec != boot_ec)
1607 acpi_ec_remove_query_handlers(ec, true, 0);
1608err_alloc:
1609 if (ec != boot_ec)
1610 acpi_ec_free(ec);
1611 return ret;
1612}
1613
1614static int acpi_ec_remove(struct acpi_device *device)
1615{
1616 struct acpi_ec *ec;
1617
1618 if (!device)
1619 return -EINVAL;
1620
1621 ec = acpi_driver_data(device);
1622 release_region(ec->data_addr, 1);
1623 release_region(ec->command_addr, 1);
1624 device->driver_data = NULL;
1625 if (ec != boot_ec) {
1626 ec_remove_handlers(ec);
1627 acpi_ec_free(ec);
1628 }
1629 return 0;
1630}
1631
1632static acpi_status
1633ec_parse_io_ports(struct acpi_resource *resource, void *context)
1634{
1635 struct acpi_ec *ec = context;
1636
1637 if (resource->type != ACPI_RESOURCE_TYPE_IO)
1638 return AE_OK;
1639
1640 /*
1641 * The first address region returned is the data port, and
1642 * the second address region returned is the status/command
1643 * port.
1644 */
1645 if (ec->data_addr == 0)
1646 ec->data_addr = resource->data.io.minimum;
1647 else if (ec->command_addr == 0)
1648 ec->command_addr = resource->data.io.minimum;
1649 else
1650 return AE_CTRL_TERMINATE;
1651
1652 return AE_OK;
1653}
1654
1655static const struct acpi_device_id ec_device_ids[] = {
1656 {"PNP0C09", 0},
1657 {ACPI_ECDT_HID, 0},
1658 {"", 0},
1659};
1660
1661/*
1662 * This function is not Windows-compatible as Windows never enumerates the
1663 * namespace EC before the main ACPI device enumeration process. It is
1664 * retained for historical reason and will be deprecated in the future.
1665 */
David Brazdil0f672f62019-12-10 10:32:29 +00001666void __init acpi_ec_dsdt_probe(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001667{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001668 struct acpi_ec *ec;
David Brazdil0f672f62019-12-10 10:32:29 +00001669 acpi_status status;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001670 int ret;
1671
1672 /*
1673 * If a platform has ECDT, there is no need to proceed as the
1674 * following probe is not a part of the ACPI device enumeration,
1675 * executing _STA is not safe, and thus this probe may risk of
1676 * picking up an invalid EC device.
1677 */
1678 if (boot_ec)
David Brazdil0f672f62019-12-10 10:32:29 +00001679 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001680
1681 ec = acpi_ec_alloc();
1682 if (!ec)
David Brazdil0f672f62019-12-10 10:32:29 +00001683 return;
1684
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001685 /*
1686 * At this point, the namespace is initialized, so start to find
1687 * the namespace objects.
1688 */
David Brazdil0f672f62019-12-10 10:32:29 +00001689 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device, ec, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001690 if (ACPI_FAILURE(status) || !ec->handle) {
David Brazdil0f672f62019-12-10 10:32:29 +00001691 acpi_ec_free(ec);
1692 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001693 }
David Brazdil0f672f62019-12-10 10:32:29 +00001694
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001695 /*
1696 * When the DSDT EC is available, always re-configure boot EC to
1697 * have _REG evaluated. _REG can only be evaluated after the
1698 * namespace initialization.
1699 * At this point, the GPE is not fully initialized, so do not to
1700 * handle the events.
1701 */
David Brazdil0f672f62019-12-10 10:32:29 +00001702 ret = acpi_ec_setup(ec, false);
1703 if (ret) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001704 acpi_ec_free(ec);
David Brazdil0f672f62019-12-10 10:32:29 +00001705 return;
1706 }
1707
1708 boot_ec = ec;
1709
1710 acpi_handle_info(ec->handle,
1711 "Boot DSDT EC used to handle transactions\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001712}
1713
1714/*
1715 * If the DSDT EC is not functioning, we still need to prepare a fully
1716 * functioning ECDT EC first in order to handle the events.
1717 * https://bugzilla.kernel.org/show_bug.cgi?id=115021
1718 */
1719static int __init acpi_ec_ecdt_start(void)
1720{
1721 acpi_handle handle;
1722
1723 if (!boot_ec)
1724 return -ENODEV;
1725 /* In case acpi_ec_ecdt_start() is called after acpi_ec_add() */
1726 if (!boot_ec_is_ecdt)
1727 return -ENODEV;
1728
1729 /*
1730 * At this point, the namespace and the GPE is initialized, so
1731 * start to find the namespace objects and handle the events.
1732 *
1733 * Note: ec->handle can be valid if this function is called after
1734 * acpi_ec_add(), hence the fast path.
1735 */
1736 if (boot_ec->handle == ACPI_ROOT_OBJECT) {
1737 if (!acpi_ec_ecdt_get_handle(&handle))
1738 return -ENODEV;
1739 boot_ec->handle = handle;
1740 }
1741
1742 /* Register to ACPI bus with PM ops attached */
1743 return acpi_bus_register_early_device(ACPI_BUS_TYPE_ECDT_EC);
1744}
1745
1746#if 0
1747/*
1748 * Some EC firmware variations refuses to respond QR_EC when SCI_EVT is not
1749 * set, for which case, we complete the QR_EC without issuing it to the
1750 * firmware.
1751 * https://bugzilla.kernel.org/show_bug.cgi?id=82611
1752 * https://bugzilla.kernel.org/show_bug.cgi?id=97381
1753 */
1754static int ec_flag_query_handshake(const struct dmi_system_id *id)
1755{
1756 pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
1757 EC_FLAGS_QUERY_HANDSHAKE = 1;
1758 return 0;
1759}
1760#endif
1761
1762/*
David Brazdil0f672f62019-12-10 10:32:29 +00001763 * On some hardware it is necessary to clear events accumulated by the EC during
1764 * sleep. These ECs stop reporting GPEs until they are manually polled, if too
1765 * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
1766 *
1767 * https://bugzilla.kernel.org/show_bug.cgi?id=44161
1768 *
1769 * Ideally, the EC should also be instructed NOT to accumulate events during
1770 * sleep (which Windows seems to do somehow), but the interface to control this
1771 * behaviour is not known at this time.
1772 *
1773 * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
1774 * however it is very likely that other Samsung models are affected.
1775 *
1776 * On systems which don't accumulate _Q events during sleep, this extra check
1777 * should be harmless.
1778 */
1779static int ec_clear_on_resume(const struct dmi_system_id *id)
1780{
1781 pr_debug("Detected system needing EC poll on resume.\n");
1782 EC_FLAGS_CLEAR_ON_RESUME = 1;
1783 ec_event_clearing = ACPI_EC_EVT_TIMING_STATUS;
1784 return 0;
1785}
1786
1787/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001788 * Some ECDTs contain wrong register addresses.
1789 * MSI MS-171F
1790 * https://bugzilla.kernel.org/show_bug.cgi?id=12461
1791 */
1792static int ec_correct_ecdt(const struct dmi_system_id *id)
1793{
1794 pr_debug("Detected system needing ECDT address correction.\n");
1795 EC_FLAGS_CORRECT_ECDT = 1;
1796 return 0;
1797}
1798
1799/*
1800 * Some DSDTs contain wrong GPE setting.
1801 * Asus FX502VD/VE, GL702VMK, X550VXK, X580VD
1802 * https://bugzilla.kernel.org/show_bug.cgi?id=195651
1803 */
1804static int ec_honor_ecdt_gpe(const struct dmi_system_id *id)
1805{
1806 pr_debug("Detected system needing ignore DSDT GPE setting.\n");
1807 EC_FLAGS_IGNORE_DSDT_GPE = 1;
1808 return 0;
1809}
1810
1811static const struct dmi_system_id ec_dmi_table[] __initconst = {
1812 {
1813 ec_correct_ecdt, "MSI MS-171F", {
1814 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star"),
1815 DMI_MATCH(DMI_PRODUCT_NAME, "MS-171F"),}, NULL},
1816 {
1817 ec_honor_ecdt_gpe, "ASUS FX502VD", {
1818 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1819 DMI_MATCH(DMI_PRODUCT_NAME, "FX502VD"),}, NULL},
1820 {
1821 ec_honor_ecdt_gpe, "ASUS FX502VE", {
1822 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1823 DMI_MATCH(DMI_PRODUCT_NAME, "FX502VE"),}, NULL},
1824 {
1825 ec_honor_ecdt_gpe, "ASUS GL702VMK", {
1826 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1827 DMI_MATCH(DMI_PRODUCT_NAME, "GL702VMK"),}, NULL},
1828 {
Olivier Deprez0e641232021-09-23 10:07:05 +02001829 ec_honor_ecdt_gpe, "ASUSTeK COMPUTER INC. X505BA", {
1830 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1831 DMI_MATCH(DMI_PRODUCT_NAME, "X505BA"),}, NULL},
1832 {
1833 ec_honor_ecdt_gpe, "ASUSTeK COMPUTER INC. X505BP", {
1834 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1835 DMI_MATCH(DMI_PRODUCT_NAME, "X505BP"),}, NULL},
1836 {
1837 ec_honor_ecdt_gpe, "ASUSTeK COMPUTER INC. X542BA", {
1838 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1839 DMI_MATCH(DMI_PRODUCT_NAME, "X542BA"),}, NULL},
1840 {
1841 ec_honor_ecdt_gpe, "ASUSTeK COMPUTER INC. X542BP", {
1842 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1843 DMI_MATCH(DMI_PRODUCT_NAME, "X542BP"),}, NULL},
1844 {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001845 ec_honor_ecdt_gpe, "ASUS X550VXK", {
1846 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1847 DMI_MATCH(DMI_PRODUCT_NAME, "X550VXK"),}, NULL},
1848 {
1849 ec_honor_ecdt_gpe, "ASUS X580VD", {
1850 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1851 DMI_MATCH(DMI_PRODUCT_NAME, "X580VD"),}, NULL},
David Brazdil0f672f62019-12-10 10:32:29 +00001852 {
1853 ec_clear_on_resume, "Samsung hardware", {
1854 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001855 {},
1856};
1857
David Brazdil0f672f62019-12-10 10:32:29 +00001858void __init acpi_ec_ecdt_probe(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001859{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001860 struct acpi_table_ecdt *ecdt_ptr;
1861 struct acpi_ec *ec;
David Brazdil0f672f62019-12-10 10:32:29 +00001862 acpi_status status;
1863 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001864
David Brazdil0f672f62019-12-10 10:32:29 +00001865 /* Generate a boot ec context. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001866 dmi_check_system(ec_dmi_table);
1867 status = acpi_get_table(ACPI_SIG_ECDT, 1,
1868 (struct acpi_table_header **)&ecdt_ptr);
David Brazdil0f672f62019-12-10 10:32:29 +00001869 if (ACPI_FAILURE(status))
1870 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001871
1872 if (!ecdt_ptr->control.address || !ecdt_ptr->data.address) {
1873 /*
1874 * Asus X50GL:
1875 * https://bugzilla.kernel.org/show_bug.cgi?id=11880
1876 */
David Brazdil0f672f62019-12-10 10:32:29 +00001877 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001878 }
1879
David Brazdil0f672f62019-12-10 10:32:29 +00001880 ec = acpi_ec_alloc();
1881 if (!ec)
1882 return;
1883
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001884 if (EC_FLAGS_CORRECT_ECDT) {
1885 ec->command_addr = ecdt_ptr->data.address;
1886 ec->data_addr = ecdt_ptr->control.address;
1887 } else {
1888 ec->command_addr = ecdt_ptr->control.address;
1889 ec->data_addr = ecdt_ptr->data.address;
1890 }
1891 ec->gpe = ecdt_ptr->gpe;
David Brazdil0f672f62019-12-10 10:32:29 +00001892 ec->handle = ACPI_ROOT_OBJECT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001893
1894 /*
1895 * At this point, the namespace is not initialized, so do not find
1896 * the namespace objects, or handle the events.
1897 */
David Brazdil0f672f62019-12-10 10:32:29 +00001898 ret = acpi_ec_setup(ec, false);
1899 if (ret) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001900 acpi_ec_free(ec);
David Brazdil0f672f62019-12-10 10:32:29 +00001901 return;
1902 }
1903
1904 boot_ec = ec;
1905 boot_ec_is_ecdt = true;
1906
1907 pr_info("Boot ECDT EC used to handle transactions\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001908}
1909
1910#ifdef CONFIG_PM_SLEEP
1911static int acpi_ec_suspend(struct device *dev)
1912{
1913 struct acpi_ec *ec =
1914 acpi_driver_data(to_acpi_device(dev));
1915
David Brazdil0f672f62019-12-10 10:32:29 +00001916 if (!pm_suspend_no_platform() && ec_freeze_events)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001917 acpi_ec_disable_event(ec);
1918 return 0;
1919}
1920
1921static int acpi_ec_suspend_noirq(struct device *dev)
1922{
1923 struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev));
1924
1925 /*
1926 * The SCI handler doesn't run at this point, so the GPE can be
1927 * masked at the low level without side effects.
1928 */
1929 if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) &&
1930 ec->reference_count >= 1)
1931 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
1932
David Brazdil0f672f62019-12-10 10:32:29 +00001933 acpi_ec_enter_noirq(ec);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001934
1935 return 0;
1936}
1937
1938static int acpi_ec_resume_noirq(struct device *dev)
1939{
1940 struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev));
1941
David Brazdil0f672f62019-12-10 10:32:29 +00001942 acpi_ec_leave_noirq(ec);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001943
1944 if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) &&
1945 ec->reference_count >= 1)
1946 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
1947
1948 return 0;
1949}
1950
1951static int acpi_ec_resume(struct device *dev)
1952{
1953 struct acpi_ec *ec =
1954 acpi_driver_data(to_acpi_device(dev));
1955
1956 acpi_ec_enable_event(ec);
1957 return 0;
1958}
David Brazdil0f672f62019-12-10 10:32:29 +00001959
1960void acpi_ec_mark_gpe_for_wake(void)
1961{
1962 if (first_ec && !ec_no_wakeup)
1963 acpi_mark_gpe_for_wake(NULL, first_ec->gpe);
1964}
1965EXPORT_SYMBOL_GPL(acpi_ec_mark_gpe_for_wake);
1966
1967void acpi_ec_set_gpe_wake_mask(u8 action)
1968{
1969 if (pm_suspend_no_platform() && first_ec && !ec_no_wakeup)
1970 acpi_set_gpe_wake_mask(NULL, first_ec->gpe, action);
1971}
1972
1973bool acpi_ec_dispatch_gpe(void)
1974{
1975 u32 ret;
1976
1977 if (!first_ec)
Olivier Deprez0e641232021-09-23 10:07:05 +02001978 return acpi_any_gpe_status_set(U32_MAX);
David Brazdil0f672f62019-12-10 10:32:29 +00001979
Olivier Deprez0e641232021-09-23 10:07:05 +02001980 /*
1981 * Report wakeup if the status bit is set for any enabled GPE other
1982 * than the EC one.
1983 */
1984 if (acpi_any_gpe_status_set(first_ec->gpe))
David Brazdil0f672f62019-12-10 10:32:29 +00001985 return true;
Olivier Deprez0e641232021-09-23 10:07:05 +02001986
1987 /*
1988 * Dispatch the EC GPE in-band, but do not report wakeup in any case
1989 * to allow the caller to process events properly after that.
1990 */
1991 ret = acpi_dispatch_gpe(NULL, first_ec->gpe);
1992 if (ret == ACPI_INTERRUPT_HANDLED)
1993 pm_pr_dbg("EC GPE dispatched\n");
1994
1995 /* Flush the event and query workqueues. */
1996 acpi_ec_flush_work();
1997
David Brazdil0f672f62019-12-10 10:32:29 +00001998 return false;
1999}
2000#endif /* CONFIG_PM_SLEEP */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002001
2002static const struct dev_pm_ops acpi_ec_pm = {
2003 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend_noirq, acpi_ec_resume_noirq)
2004 SET_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend, acpi_ec_resume)
2005};
2006
2007static int param_set_event_clearing(const char *val,
2008 const struct kernel_param *kp)
2009{
2010 int result = 0;
2011
2012 if (!strncmp(val, "status", sizeof("status") - 1)) {
2013 ec_event_clearing = ACPI_EC_EVT_TIMING_STATUS;
2014 pr_info("Assuming SCI_EVT clearing on EC_SC accesses\n");
2015 } else if (!strncmp(val, "query", sizeof("query") - 1)) {
2016 ec_event_clearing = ACPI_EC_EVT_TIMING_QUERY;
2017 pr_info("Assuming SCI_EVT clearing on QR_EC writes\n");
2018 } else if (!strncmp(val, "event", sizeof("event") - 1)) {
2019 ec_event_clearing = ACPI_EC_EVT_TIMING_EVENT;
2020 pr_info("Assuming SCI_EVT clearing on event reads\n");
2021 } else
2022 result = -EINVAL;
2023 return result;
2024}
2025
2026static int param_get_event_clearing(char *buffer,
2027 const struct kernel_param *kp)
2028{
2029 switch (ec_event_clearing) {
2030 case ACPI_EC_EVT_TIMING_STATUS:
2031 return sprintf(buffer, "status");
2032 case ACPI_EC_EVT_TIMING_QUERY:
2033 return sprintf(buffer, "query");
2034 case ACPI_EC_EVT_TIMING_EVENT:
2035 return sprintf(buffer, "event");
2036 default:
2037 return sprintf(buffer, "invalid");
2038 }
2039 return 0;
2040}
2041
2042module_param_call(ec_event_clearing, param_set_event_clearing, param_get_event_clearing,
2043 NULL, 0644);
2044MODULE_PARM_DESC(ec_event_clearing, "Assumed SCI_EVT clearing timing");
2045
2046static struct acpi_driver acpi_ec_driver = {
2047 .name = "ec",
2048 .class = ACPI_EC_CLASS,
2049 .ids = ec_device_ids,
2050 .ops = {
2051 .add = acpi_ec_add,
2052 .remove = acpi_ec_remove,
2053 },
2054 .drv.pm = &acpi_ec_pm,
2055};
2056
Olivier Deprez0e641232021-09-23 10:07:05 +02002057static void acpi_ec_destroy_workqueues(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002058{
Olivier Deprez0e641232021-09-23 10:07:05 +02002059 if (ec_wq) {
2060 destroy_workqueue(ec_wq);
2061 ec_wq = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002062 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002063 if (ec_query_wq) {
2064 destroy_workqueue(ec_query_wq);
2065 ec_query_wq = NULL;
2066 }
2067}
2068
Olivier Deprez0e641232021-09-23 10:07:05 +02002069static int acpi_ec_init_workqueues(void)
2070{
2071 if (!ec_wq)
2072 ec_wq = alloc_ordered_workqueue("kec", 0);
2073
2074 if (!ec_query_wq)
2075 ec_query_wq = alloc_workqueue("kec_query", 0, ec_max_queries);
2076
2077 if (!ec_wq || !ec_query_wq) {
2078 acpi_ec_destroy_workqueues();
2079 return -ENODEV;
2080 }
2081 return 0;
2082}
2083
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002084static const struct dmi_system_id acpi_ec_no_wakeup[] = {
2085 {
2086 .ident = "Thinkpad X1 Carbon 6th",
2087 .matches = {
2088 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
2089 DMI_MATCH(DMI_PRODUCT_FAMILY, "Thinkpad X1 Carbon 6th"),
2090 },
2091 },
2092 {
2093 .ident = "ThinkPad X1 Carbon 6th",
2094 .matches = {
2095 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
2096 DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Carbon 6th"),
2097 },
2098 },
2099 {
2100 .ident = "ThinkPad X1 Yoga 3rd",
2101 .matches = {
2102 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
2103 DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Yoga 3rd"),
2104 },
2105 },
2106 { },
2107};
2108
2109int __init acpi_ec_init(void)
2110{
2111 int result;
2112 int ecdt_fail, dsdt_fail;
2113
Olivier Deprez0e641232021-09-23 10:07:05 +02002114 result = acpi_ec_init_workqueues();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002115 if (result)
2116 return result;
2117
2118 /*
2119 * Disable EC wakeup on following systems to prevent periodic
2120 * wakeup from EC GPE.
2121 */
2122 if (dmi_check_system(acpi_ec_no_wakeup)) {
2123 ec_no_wakeup = true;
2124 pr_debug("Disabling EC wakeup on suspend-to-idle\n");
2125 }
2126
2127 /* Drivers must be started after acpi_ec_query_init() */
2128 dsdt_fail = acpi_bus_register_driver(&acpi_ec_driver);
2129 /*
2130 * Register ECDT to ACPI bus only when PNP0C09 probe fails. This is
2131 * useful for platforms (confirmed on ASUS X550ZE) with valid ECDT
2132 * settings but invalid DSDT settings.
2133 * https://bugzilla.kernel.org/show_bug.cgi?id=196847
2134 */
2135 ecdt_fail = acpi_ec_ecdt_start();
2136 return ecdt_fail && dsdt_fail ? -ENODEV : 0;
2137}
2138
2139/* EC driver currently not unloadable */
2140#if 0
2141static void __exit acpi_ec_exit(void)
2142{
2143
2144 acpi_bus_unregister_driver(&acpi_ec_driver);
Olivier Deprez0e641232021-09-23 10:07:05 +02002145 acpi_ec_destroy_workqueues();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002146}
2147#endif /* 0 */