Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
a8d4fc22 | 3 | * ec.c - ACPI Embedded Controller Driver (v3) |
1da177e4 | 4 | * |
a8d4fc22 LZ |
5 | * Copyright (C) 2001-2015 Intel Corporation |
6 | * Author: 2014, 2015 Lv Zheng <lv.zheng@intel.com> | |
4a3f6b5b LZ |
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> | |
1da177e4 LT |
13 | */ |
14 | ||
7c6db4e0 | 15 | /* Uncomment next line to get verbose printout */ |
d772b3b3 | 16 | /* #define DEBUG */ |
b6aeab44 | 17 | #define pr_fmt(fmt) "ACPI: EC: " fmt |
d772b3b3 | 18 | |
1da177e4 LT |
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> | |
451566f4 | 24 | #include <linux/interrupt.h> |
837012ed | 25 | #include <linux/list.h> |
7c6db4e0 | 26 | #include <linux/spinlock.h> |
5a0e3ad6 | 27 | #include <linux/slab.h> |
10a08fd6 | 28 | #include <linux/suspend.h> |
8b48463f | 29 | #include <linux/acpi.h> |
eb27cae8 | 30 | #include <linux/dmi.h> |
8b48463f | 31 | #include <asm/io.h> |
1da177e4 | 32 | |
1195a098 TR |
33 | #include "internal.h" |
34 | ||
1da177e4 | 35 | #define ACPI_EC_CLASS "embedded_controller" |
1da177e4 LT |
36 | #define ACPI_EC_DEVICE_NAME "Embedded Controller" |
37 | #define ACPI_EC_FILE_INFO "info" | |
837012ed | 38 | |
703959d4 | 39 | /* EC status register */ |
1da177e4 LT |
40 | #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */ |
41 | #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */ | |
dd43de20 | 42 | #define ACPI_EC_FLAG_CMD 0x08 /* Input buffer contains a command */ |
451566f4 | 43 | #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */ |
1da177e4 | 44 | #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */ |
4350933a | 45 | |
1d68d261 LZ |
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 | ||
703959d4 | 78 | /* EC commands */ |
3261ff4d | 79 | enum ec_command { |
6ccedb10 AS |
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, | |
3261ff4d | 85 | }; |
837012ed | 86 | |
5c406412 | 87 | #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */ |
703959d4 | 88 | #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */ |
d8d031a6 | 89 | #define ACPI_EC_UDELAY_POLL 550 /* Wait 1ms for EC transaction polling */ |
ad332c8a KC |
90 | #define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query |
91 | * when trying to clear the EC */ | |
e1191bd4 | 92 | #define ACPI_EC_MAX_QUERIES 16 /* Maximum number of parallel queries */ |
703959d4 | 93 | |
080e412c | 94 | enum { |
750f628b | 95 | EC_FLAGS_QUERY_ENABLED, /* Query is enabled */ |
37d11391 | 96 | EC_FLAGS_QUERY_PENDING, /* Query is pending */ |
1d68d261 | 97 | EC_FLAGS_QUERY_GUARDING, /* Guard for SCI_EVT check */ |
0e1affe4 LZ |
98 | EC_FLAGS_GPE_HANDLER_INSTALLED, /* GPE handler installed */ |
99 | EC_FLAGS_EC_HANDLER_INSTALLED, /* OpReg handler installed */ | |
2a570840 | 100 | EC_FLAGS_EVT_HANDLER_INSTALLED, /* _Qxx handlers installed */ |
ad479e7f LZ |
101 | EC_FLAGS_STARTED, /* Driver is started */ |
102 | EC_FLAGS_STOPPED, /* Driver is stopped */ | |
56f77eeb | 103 | EC_FLAGS_GPE_MASKED, /* GPE masked */ |
1da177e4 | 104 | }; |
6ffb221a | 105 | |
f92fca00 LZ |
106 | #define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */ |
107 | #define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */ | |
108 | ||
7a18e96d TR |
109 | /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */ |
110 | static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY; | |
111 | module_param(ec_delay, uint, 0644); | |
112 | MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes"); | |
113 | ||
e1191bd4 LZ |
114 | static unsigned int ec_max_queries __read_mostly = ACPI_EC_MAX_QUERIES; |
115 | module_param(ec_max_queries, uint, 0644); | |
116 | MODULE_PARM_DESC(ec_max_queries, "Maximum parallel _Qxx evaluations"); | |
117 | ||
15de603b LZ |
118 | static bool ec_busy_polling __read_mostly; |
119 | module_param(ec_busy_polling, bool, 0644); | |
120 | MODULE_PARM_DESC(ec_busy_polling, "Use busy polling to advance EC transaction"); | |
121 | ||
122 | static unsigned int ec_polling_guard __read_mostly = ACPI_EC_UDELAY_POLL; | |
123 | module_param(ec_polling_guard, uint, 0644); | |
124 | MODULE_PARM_DESC(ec_polling_guard, "Guard time(us) between EC accesses in polling modes"); | |
125 | ||
3cb02aeb | 126 | static unsigned int ec_event_clearing __read_mostly = ACPI_EC_EVT_TIMING_QUERY; |
1d68d261 | 127 | |
a520d52e FT |
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 | */ | |
133 | static unsigned int ec_storm_threshold __read_mostly = 8; | |
134 | module_param(ec_storm_threshold, uint, 0644); | |
135 | MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm"); | |
136 | ||
9c40f956 | 137 | static bool ec_freeze_events __read_mostly = false; |
39a2a2aa LZ |
138 | module_param(ec_freeze_events, bool, 0644); |
139 | MODULE_PARM_DESC(ec_freeze_events, "Disabling event handling during suspend/resume"); | |
140 | ||
76380636 RW |
141 | static bool ec_no_wakeup __read_mostly; |
142 | module_param(ec_no_wakeup, bool, 0644); | |
143 | MODULE_PARM_DESC(ec_no_wakeup, "Do not wake up from suspend-to-idle"); | |
144 | ||
837012ed AS |
145 | struct 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; | |
01305d41 | 151 | struct kref kref; |
837012ed AS |
152 | }; |
153 | ||
8463200a | 154 | struct transaction { |
7c6db4e0 AS |
155 | const u8 *wdata; |
156 | u8 *rdata; | |
157 | unsigned short irq_count; | |
8463200a | 158 | u8 command; |
a2f93aea AS |
159 | u8 wi; |
160 | u8 ri; | |
7c6db4e0 AS |
161 | u8 wlen; |
162 | u8 rlen; | |
f92fca00 | 163 | u8 flags; |
7c6db4e0 AS |
164 | }; |
165 | ||
02b771b6 LZ |
166 | struct acpi_ec_query { |
167 | struct transaction transaction; | |
168 | struct work_struct work; | |
169 | struct acpi_ec_query_handler *handler; | |
170 | }; | |
171 | ||
550b3aac | 172 | static int acpi_ec_query(struct acpi_ec *ec, u8 *data); |
ca37bfdf | 173 | static void advance_transaction(struct acpi_ec *ec); |
02b771b6 LZ |
174 | static void acpi_ec_event_handler(struct work_struct *work); |
175 | static void acpi_ec_event_processor(struct work_struct *work); | |
74443bbe | 176 | |
a9c30768 | 177 | struct acpi_ec *first_ec; |
1195a098 | 178 | EXPORT_SYMBOL(first_ec); |
a9c30768 RW |
179 | |
180 | static struct acpi_ec *boot_ec; | |
72c77b7e | 181 | static bool boot_ec_is_ecdt = false; |
e1191bd4 | 182 | static struct workqueue_struct *ec_query_wq; |
703959d4 | 183 | |
79149001 | 184 | static int EC_FLAGS_QUERY_HANDSHAKE; /* Needs QR_EC issued when SCI_EVT set */ |
59f0aa94 | 185 | static int EC_FLAGS_CORRECT_ECDT; /* Needs ECDT port address correction */ |
440f53da | 186 | static int EC_FLAGS_IGNORE_DSDT_GPE; /* Needs ECDT GPE as correction setting */ |
b6a3e147 | 187 | static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */ |
5423a0cb | 188 | |
3535a3c1 LZ |
189 | /* -------------------------------------------------------------------------- |
190 | * Logging/Debugging | |
191 | * -------------------------------------------------------------------------- */ | |
192 | ||
193 | /* | |
194 | * Splitters used by the developers to track the boundary of the EC | |
195 | * handling processes. | |
196 | */ | |
197 | #ifdef DEBUG | |
198 | #define EC_DBG_SEP " " | |
199 | #define EC_DBG_DRV "+++++" | |
200 | #define EC_DBG_STM "=====" | |
201 | #define EC_DBG_REQ "*****" | |
202 | #define EC_DBG_EVT "#####" | |
203 | #else | |
204 | #define EC_DBG_SEP "" | |
205 | #define EC_DBG_DRV | |
206 | #define EC_DBG_STM | |
207 | #define EC_DBG_REQ | |
208 | #define EC_DBG_EVT | |
209 | #endif | |
210 | ||
211 | #define ec_log_raw(fmt, ...) \ | |
212 | pr_info(fmt "\n", ##__VA_ARGS__) | |
213 | #define ec_dbg_raw(fmt, ...) \ | |
214 | pr_debug(fmt "\n", ##__VA_ARGS__) | |
215 | #define ec_log(filter, fmt, ...) \ | |
216 | ec_log_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__) | |
217 | #define ec_dbg(filter, fmt, ...) \ | |
218 | ec_dbg_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__) | |
219 | ||
220 | #define ec_log_drv(fmt, ...) \ | |
221 | ec_log(EC_DBG_DRV, fmt, ##__VA_ARGS__) | |
222 | #define ec_dbg_drv(fmt, ...) \ | |
223 | ec_dbg(EC_DBG_DRV, fmt, ##__VA_ARGS__) | |
224 | #define ec_dbg_stm(fmt, ...) \ | |
225 | ec_dbg(EC_DBG_STM, fmt, ##__VA_ARGS__) | |
226 | #define ec_dbg_req(fmt, ...) \ | |
227 | ec_dbg(EC_DBG_REQ, fmt, ##__VA_ARGS__) | |
228 | #define ec_dbg_evt(fmt, ...) \ | |
229 | ec_dbg(EC_DBG_EVT, fmt, ##__VA_ARGS__) | |
770970f0 LZ |
230 | #define ec_dbg_ref(ec, fmt, ...) \ |
231 | ec_dbg_raw("%lu: " fmt, ec->reference_count, ## __VA_ARGS__) | |
3535a3c1 | 232 | |
ad479e7f LZ |
233 | /* -------------------------------------------------------------------------- |
234 | * Device Flags | |
235 | * -------------------------------------------------------------------------- */ | |
236 | ||
237 | static bool acpi_ec_started(struct acpi_ec *ec) | |
238 | { | |
239 | return test_bit(EC_FLAGS_STARTED, &ec->flags) && | |
240 | !test_bit(EC_FLAGS_STOPPED, &ec->flags); | |
241 | } | |
242 | ||
750f628b LZ |
243 | static bool acpi_ec_event_enabled(struct acpi_ec *ec) |
244 | { | |
245 | /* | |
246 | * There is an OSPM early stage logic. During the early stages | |
247 | * (boot/resume), OSPMs shouldn't enable the event handling, only | |
248 | * the EC transactions are allowed to be performed. | |
249 | */ | |
250 | if (!test_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) | |
251 | return false; | |
252 | /* | |
39a2a2aa LZ |
253 | * However, disabling the event handling is experimental for late |
254 | * stage (suspend), and is controlled by the boot parameter of | |
255 | * "ec_freeze_events": | |
256 | * 1. true: The EC event handling is disabled before entering | |
257 | * the noirq stage. | |
258 | * 2. false: The EC event handling is automatically disabled as | |
259 | * soon as the EC driver is stopped. | |
750f628b | 260 | */ |
39a2a2aa LZ |
261 | if (ec_freeze_events) |
262 | return acpi_ec_started(ec); | |
263 | else | |
264 | return test_bit(EC_FLAGS_STARTED, &ec->flags); | |
750f628b LZ |
265 | } |
266 | ||
9887d22a LZ |
267 | static bool acpi_ec_flushed(struct acpi_ec *ec) |
268 | { | |
269 | return ec->reference_count == 1; | |
270 | } | |
271 | ||
1da177e4 | 272 | /* -------------------------------------------------------------------------- |
ca37bfdf | 273 | * EC Registers |
7a73e60e | 274 | * -------------------------------------------------------------------------- */ |
1da177e4 | 275 | |
6ffb221a | 276 | static inline u8 acpi_ec_read_status(struct acpi_ec *ec) |
1da177e4 | 277 | { |
3ebe08a7 | 278 | u8 x = inb(ec->command_addr); |
7a73e60e | 279 | |
3535a3c1 LZ |
280 | ec_dbg_raw("EC_SC(R) = 0x%2.2x " |
281 | "SCI_EVT=%d BURST=%d CMD=%d IBF=%d OBF=%d", | |
282 | x, | |
283 | !!(x & ACPI_EC_FLAG_SCI), | |
284 | !!(x & ACPI_EC_FLAG_BURST), | |
285 | !!(x & ACPI_EC_FLAG_CMD), | |
286 | !!(x & ACPI_EC_FLAG_IBF), | |
287 | !!(x & ACPI_EC_FLAG_OBF)); | |
3ebe08a7 | 288 | return x; |
451566f4 DT |
289 | } |
290 | ||
6ffb221a | 291 | static inline u8 acpi_ec_read_data(struct acpi_ec *ec) |
703959d4 | 292 | { |
3ebe08a7 | 293 | u8 x = inb(ec->data_addr); |
7a73e60e | 294 | |
d8d031a6 | 295 | ec->timestamp = jiffies; |
3535a3c1 | 296 | ec_dbg_raw("EC_DATA(R) = 0x%2.2x", x); |
7c6db4e0 | 297 | return x; |
7c6db5e5 DS |
298 | } |
299 | ||
6ffb221a | 300 | static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command) |
45bea155 | 301 | { |
3535a3c1 | 302 | ec_dbg_raw("EC_SC(W) = 0x%2.2x", command); |
6ffb221a | 303 | outb(command, ec->command_addr); |
d8d031a6 | 304 | ec->timestamp = jiffies; |
45bea155 LY |
305 | } |
306 | ||
6ffb221a | 307 | static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data) |
45bea155 | 308 | { |
3535a3c1 | 309 | ec_dbg_raw("EC_DATA(W) = 0x%2.2x", data); |
6ffb221a | 310 | outb(data, ec->data_addr); |
d8d031a6 | 311 | ec->timestamp = jiffies; |
703959d4 | 312 | } |
45bea155 | 313 | |
4625d752 | 314 | #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG) |
e34c0e2b LZ |
315 | static const char *acpi_ec_cmd_string(u8 cmd) |
316 | { | |
317 | switch (cmd) { | |
318 | case 0x80: | |
319 | return "RD_EC"; | |
320 | case 0x81: | |
321 | return "WR_EC"; | |
322 | case 0x82: | |
323 | return "BE_EC"; | |
324 | case 0x83: | |
325 | return "BD_EC"; | |
326 | case 0x84: | |
327 | return "QR_EC"; | |
328 | } | |
329 | return "UNKNOWN"; | |
330 | } | |
331 | #else | |
332 | #define acpi_ec_cmd_string(cmd) "UNDEF" | |
333 | #endif | |
334 | ||
ca37bfdf LZ |
335 | /* -------------------------------------------------------------------------- |
336 | * GPE Registers | |
337 | * -------------------------------------------------------------------------- */ | |
338 | ||
339 | static inline bool acpi_ec_is_gpe_raised(struct acpi_ec *ec) | |
340 | { | |
341 | acpi_event_status gpe_status = 0; | |
342 | ||
343 | (void)acpi_get_gpe_status(NULL, ec->gpe, &gpe_status); | |
7c0b2595 | 344 | return (gpe_status & ACPI_EVENT_FLAG_STATUS_SET) ? true : false; |
ca37bfdf LZ |
345 | } |
346 | ||
347 | static inline void acpi_ec_enable_gpe(struct acpi_ec *ec, bool open) | |
348 | { | |
349 | if (open) | |
350 | acpi_enable_gpe(NULL, ec->gpe); | |
e1d4d90f LZ |
351 | else { |
352 | BUG_ON(ec->reference_count < 1); | |
ca37bfdf | 353 | acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE); |
e1d4d90f | 354 | } |
ca37bfdf LZ |
355 | if (acpi_ec_is_gpe_raised(ec)) { |
356 | /* | |
357 | * On some platforms, EN=1 writes cannot trigger GPE. So | |
358 | * software need to manually trigger a pseudo GPE event on | |
359 | * EN=1 writes. | |
360 | */ | |
3535a3c1 | 361 | ec_dbg_raw("Polling quirk"); |
ca37bfdf LZ |
362 | advance_transaction(ec); |
363 | } | |
364 | } | |
365 | ||
366 | static inline void acpi_ec_disable_gpe(struct acpi_ec *ec, bool close) | |
367 | { | |
368 | if (close) | |
369 | acpi_disable_gpe(NULL, ec->gpe); | |
e1d4d90f LZ |
370 | else { |
371 | BUG_ON(ec->reference_count < 1); | |
ca37bfdf | 372 | acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE); |
e1d4d90f | 373 | } |
ca37bfdf LZ |
374 | } |
375 | ||
376 | static inline void acpi_ec_clear_gpe(struct acpi_ec *ec) | |
377 | { | |
378 | /* | |
379 | * GPE STS is a W1C register, which means: | |
380 | * 1. Software can clear it without worrying about clearing other | |
381 | * GPEs' STS bits when the hardware sets them in parallel. | |
382 | * 2. As long as software can ensure only clearing it when it is | |
383 | * set, hardware won't set it in parallel. | |
384 | * So software can clear GPE in any contexts. | |
385 | * Warning: do not move the check into advance_transaction() as the | |
386 | * EC commands will be sent without GPE raised. | |
387 | */ | |
388 | if (!acpi_ec_is_gpe_raised(ec)) | |
389 | return; | |
390 | acpi_clear_gpe(NULL, ec->gpe); | |
391 | } | |
392 | ||
393 | /* -------------------------------------------------------------------------- | |
394 | * Transaction Management | |
395 | * -------------------------------------------------------------------------- */ | |
396 | ||
9887d22a LZ |
397 | static void acpi_ec_submit_request(struct acpi_ec *ec) |
398 | { | |
399 | ec->reference_count++; | |
0e1affe4 LZ |
400 | if (test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags) && |
401 | ec->reference_count == 1) | |
9887d22a LZ |
402 | acpi_ec_enable_gpe(ec, true); |
403 | } | |
404 | ||
405 | static void acpi_ec_complete_request(struct acpi_ec *ec) | |
406 | { | |
407 | bool flushed = false; | |
408 | ||
409 | ec->reference_count--; | |
0e1affe4 LZ |
410 | if (test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags) && |
411 | ec->reference_count == 0) | |
9887d22a LZ |
412 | acpi_ec_disable_gpe(ec, true); |
413 | flushed = acpi_ec_flushed(ec); | |
414 | if (flushed) | |
415 | wake_up(&ec->wait); | |
416 | } | |
417 | ||
56f77eeb | 418 | static void acpi_ec_mask_gpe(struct acpi_ec *ec) |
e1d4d90f | 419 | { |
56f77eeb | 420 | if (!test_bit(EC_FLAGS_GPE_MASKED, &ec->flags)) { |
e1d4d90f | 421 | acpi_ec_disable_gpe(ec, false); |
3535a3c1 | 422 | ec_dbg_drv("Polling enabled"); |
56f77eeb | 423 | set_bit(EC_FLAGS_GPE_MASKED, &ec->flags); |
e1d4d90f LZ |
424 | } |
425 | } | |
426 | ||
56f77eeb | 427 | static void acpi_ec_unmask_gpe(struct acpi_ec *ec) |
e1d4d90f | 428 | { |
56f77eeb LZ |
429 | if (test_bit(EC_FLAGS_GPE_MASKED, &ec->flags)) { |
430 | clear_bit(EC_FLAGS_GPE_MASKED, &ec->flags); | |
e1d4d90f | 431 | acpi_ec_enable_gpe(ec, false); |
3535a3c1 | 432 | ec_dbg_drv("Polling disabled"); |
e1d4d90f LZ |
433 | } |
434 | } | |
435 | ||
9887d22a LZ |
436 | /* |
437 | * acpi_ec_submit_flushable_request() - Increase the reference count unless | |
438 | * the flush operation is not in | |
439 | * progress | |
440 | * @ec: the EC device | |
441 | * | |
442 | * This function must be used before taking a new action that should hold | |
443 | * the reference count. If this function returns false, then the action | |
444 | * must be discarded or it will prevent the flush operation from being | |
445 | * completed. | |
446 | */ | |
37d11391 | 447 | static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec) |
9887d22a | 448 | { |
37d11391 RW |
449 | if (!acpi_ec_started(ec)) |
450 | return false; | |
9887d22a LZ |
451 | acpi_ec_submit_request(ec); |
452 | return true; | |
453 | } | |
454 | ||
37d11391 | 455 | static void acpi_ec_submit_query(struct acpi_ec *ec) |
74443bbe | 456 | { |
56f77eeb | 457 | acpi_ec_mask_gpe(ec); |
1ab69f27 LZ |
458 | if (!acpi_ec_event_enabled(ec)) |
459 | return; | |
460 | if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) { | |
9d8993be LZ |
461 | ec_dbg_evt("Command(%s) submitted/blocked", |
462 | acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY)); | |
463 | ec->nr_pending_queries++; | |
74443bbe LZ |
464 | schedule_work(&ec->work); |
465 | } | |
466 | } | |
467 | ||
37d11391 | 468 | static void acpi_ec_complete_query(struct acpi_ec *ec) |
74443bbe | 469 | { |
1ab69f27 | 470 | if (test_and_clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) |
9d8993be LZ |
471 | ec_dbg_evt("Command(%s) unblocked", |
472 | acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY)); | |
56f77eeb | 473 | acpi_ec_unmask_gpe(ec); |
74443bbe LZ |
474 | } |
475 | ||
750f628b LZ |
476 | static inline void __acpi_ec_enable_event(struct acpi_ec *ec) |
477 | { | |
478 | if (!test_and_set_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) | |
479 | ec_log_drv("event unblocked"); | |
53c5eaab LZ |
480 | /* |
481 | * Unconditionally invoke this once after enabling the event | |
482 | * handling mechanism to detect the pending events. | |
483 | */ | |
484 | advance_transaction(ec); | |
750f628b LZ |
485 | } |
486 | ||
487 | static inline void __acpi_ec_disable_event(struct acpi_ec *ec) | |
488 | { | |
489 | if (test_and_clear_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags)) | |
490 | ec_log_drv("event blocked"); | |
491 | } | |
492 | ||
b6a3e147 ZR |
493 | /* |
494 | * Process _Q events that might have accumulated in the EC. | |
495 | * Run with locked ec mutex. | |
496 | */ | |
497 | static void acpi_ec_clear(struct acpi_ec *ec) | |
498 | { | |
499 | int i, status; | |
500 | u8 value = 0; | |
501 | ||
502 | for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) { | |
503 | status = acpi_ec_query(ec, &value); | |
504 | if (status || !value) | |
505 | break; | |
506 | } | |
507 | if (unlikely(i == ACPI_EC_CLEAR_MAX)) | |
508 | pr_warn("Warning: Maximum of %d stale EC events cleared\n", i); | |
509 | else | |
510 | pr_info("%d stale EC events cleared\n", i); | |
511 | } | |
512 | ||
750f628b LZ |
513 | static void acpi_ec_enable_event(struct acpi_ec *ec) |
514 | { | |
515 | unsigned long flags; | |
516 | ||
517 | spin_lock_irqsave(&ec->lock, flags); | |
518 | if (acpi_ec_started(ec)) | |
519 | __acpi_ec_enable_event(ec); | |
520 | spin_unlock_irqrestore(&ec->lock, flags); | |
b6a3e147 ZR |
521 | |
522 | /* Drain additional events if hardware requires that */ | |
523 | if (EC_FLAGS_CLEAR_ON_RESUME) | |
524 | acpi_ec_clear(ec); | |
750f628b LZ |
525 | } |
526 | ||
eab05ec3 | 527 | #ifdef CONFIG_PM_SLEEP |
39a2a2aa LZ |
528 | static bool acpi_ec_query_flushed(struct acpi_ec *ec) |
529 | { | |
530 | bool flushed; | |
531 | unsigned long flags; | |
532 | ||
533 | spin_lock_irqsave(&ec->lock, flags); | |
534 | flushed = !ec->nr_pending_queries; | |
535 | spin_unlock_irqrestore(&ec->lock, flags); | |
536 | return flushed; | |
537 | } | |
538 | ||
539 | static void __acpi_ec_flush_event(struct acpi_ec *ec) | |
540 | { | |
541 | /* | |
542 | * When ec_freeze_events is true, we need to flush events in | |
543 | * the proper position before entering the noirq stage. | |
544 | */ | |
545 | wait_event(ec->wait, acpi_ec_query_flushed(ec)); | |
546 | if (ec_query_wq) | |
547 | flush_workqueue(ec_query_wq); | |
548 | } | |
549 | ||
550 | static void acpi_ec_disable_event(struct acpi_ec *ec) | |
551 | { | |
552 | unsigned long flags; | |
553 | ||
554 | spin_lock_irqsave(&ec->lock, flags); | |
555 | __acpi_ec_disable_event(ec); | |
556 | spin_unlock_irqrestore(&ec->lock, flags); | |
557 | __acpi_ec_flush_event(ec); | |
558 | } | |
880a6627 RW |
559 | |
560 | void acpi_ec_flush_work(void) | |
561 | { | |
562 | if (first_ec) | |
563 | __acpi_ec_flush_event(first_ec); | |
564 | ||
565 | flush_scheduled_work(); | |
566 | } | |
eab05ec3 | 567 | #endif /* CONFIG_PM_SLEEP */ |
39a2a2aa | 568 | |
1d68d261 LZ |
569 | static bool acpi_ec_guard_event(struct acpi_ec *ec) |
570 | { | |
61197547 LZ |
571 | bool guarded = true; |
572 | unsigned long flags; | |
573 | ||
574 | spin_lock_irqsave(&ec->lock, flags); | |
575 | /* | |
576 | * If firmware SCI_EVT clearing timing is "event", we actually | |
577 | * don't know when the SCI_EVT will be cleared by firmware after | |
578 | * evaluating _Qxx, so we need to re-check SCI_EVT after waiting an | |
579 | * acceptable period. | |
580 | * | |
581 | * The guarding period begins when EC_FLAGS_QUERY_PENDING is | |
582 | * flagged, which means SCI_EVT check has just been performed. | |
583 | * But if the current transaction is ACPI_EC_COMMAND_QUERY, the | |
584 | * guarding should have already been performed (via | |
585 | * EC_FLAGS_QUERY_GUARDING) and should not be applied so that the | |
586 | * ACPI_EC_COMMAND_QUERY transaction can be transitioned into | |
587 | * ACPI_EC_COMMAND_POLL state immediately. | |
588 | */ | |
1d68d261 LZ |
589 | if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS || |
590 | ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY || | |
591 | !test_bit(EC_FLAGS_QUERY_PENDING, &ec->flags) || | |
592 | (ec->curr && ec->curr->command == ACPI_EC_COMMAND_QUERY)) | |
61197547 LZ |
593 | guarded = false; |
594 | spin_unlock_irqrestore(&ec->lock, flags); | |
595 | return guarded; | |
1d68d261 LZ |
596 | } |
597 | ||
d8d031a6 LZ |
598 | static int ec_transaction_polled(struct acpi_ec *ec) |
599 | { | |
600 | unsigned long flags; | |
601 | int ret = 0; | |
602 | ||
603 | spin_lock_irqsave(&ec->lock, flags); | |
604 | if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_POLL)) | |
605 | ret = 1; | |
606 | spin_unlock_irqrestore(&ec->lock, flags); | |
607 | return ret; | |
608 | } | |
609 | ||
f92fca00 | 610 | static int ec_transaction_completed(struct acpi_ec *ec) |
6ffb221a | 611 | { |
7c6db4e0 AS |
612 | unsigned long flags; |
613 | int ret = 0; | |
7a73e60e | 614 | |
f351d027 | 615 | spin_lock_irqsave(&ec->lock, flags); |
c0d65341 | 616 | if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE)) |
7c6db4e0 | 617 | ret = 1; |
f351d027 | 618 | spin_unlock_irqrestore(&ec->lock, flags); |
7c6db4e0 | 619 | return ret; |
45bea155 | 620 | } |
451566f4 | 621 | |
f8b8eb71 LZ |
622 | static inline void ec_transaction_transition(struct acpi_ec *ec, unsigned long flag) |
623 | { | |
624 | ec->curr->flags |= flag; | |
625 | if (ec->curr->command == ACPI_EC_COMMAND_QUERY) { | |
1d68d261 LZ |
626 | if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS && |
627 | flag == ACPI_EC_COMMAND_POLL) | |
628 | acpi_ec_complete_query(ec); | |
629 | if (ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY && | |
630 | flag == ACPI_EC_COMMAND_COMPLETE) | |
f8b8eb71 | 631 | acpi_ec_complete_query(ec); |
1d68d261 LZ |
632 | if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT && |
633 | flag == ACPI_EC_COMMAND_COMPLETE) | |
634 | set_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags); | |
f8b8eb71 LZ |
635 | } |
636 | } | |
637 | ||
0c78808f | 638 | static void advance_transaction(struct acpi_ec *ec) |
7c6db5e5 | 639 | { |
36b15875 | 640 | struct transaction *t; |
66b42b78 | 641 | u8 status; |
c0d65341 | 642 | bool wakeup = false; |
b76b51ba | 643 | |
3535a3c1 LZ |
644 | ec_dbg_stm("%s (%d)", in_interrupt() ? "IRQ" : "TASK", |
645 | smp_processor_id()); | |
ca37bfdf LZ |
646 | /* |
647 | * By always clearing STS before handling all indications, we can | |
648 | * ensure a hardware STS 0->1 change after this clearing can always | |
649 | * trigger a GPE interrupt. | |
650 | */ | |
651 | acpi_ec_clear_gpe(ec); | |
66b42b78 | 652 | status = acpi_ec_read_status(ec); |
36b15875 | 653 | t = ec->curr; |
1d68d261 LZ |
654 | /* |
655 | * Another IRQ or a guarded polling mode advancement is detected, | |
656 | * the next QR_EC submission is then allowed. | |
657 | */ | |
658 | if (!t || !(t->flags & ACPI_EC_COMMAND_POLL)) { | |
659 | if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT && | |
66db3834 LZ |
660 | (!ec->nr_pending_queries || |
661 | test_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags))) { | |
1d68d261 LZ |
662 | clear_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags); |
663 | acpi_ec_complete_query(ec); | |
664 | } | |
665 | } | |
b76b51ba | 666 | if (!t) |
f92fca00 LZ |
667 | goto err; |
668 | if (t->flags & ACPI_EC_COMMAND_POLL) { | |
669 | if (t->wlen > t->wi) { | |
670 | if ((status & ACPI_EC_FLAG_IBF) == 0) | |
671 | acpi_ec_write_data(ec, t->wdata[t->wi++]); | |
672 | else | |
673 | goto err; | |
674 | } else if (t->rlen > t->ri) { | |
675 | if ((status & ACPI_EC_FLAG_OBF) == 1) { | |
676 | t->rdata[t->ri++] = acpi_ec_read_data(ec); | |
c0d65341 | 677 | if (t->rlen == t->ri) { |
f8b8eb71 | 678 | ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE); |
3afcf2ec | 679 | if (t->command == ACPI_EC_COMMAND_QUERY) |
9d8993be LZ |
680 | ec_dbg_evt("Command(%s) completed by hardware", |
681 | acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY)); | |
c0d65341 LZ |
682 | wakeup = true; |
683 | } | |
f92fca00 LZ |
684 | } else |
685 | goto err; | |
686 | } else if (t->wlen == t->wi && | |
c0d65341 | 687 | (status & ACPI_EC_FLAG_IBF) == 0) { |
f8b8eb71 | 688 | ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE); |
c0d65341 LZ |
689 | wakeup = true; |
690 | } | |
0c78808f | 691 | goto out; |
f92fca00 | 692 | } else { |
79149001 LZ |
693 | if (EC_FLAGS_QUERY_HANDSHAKE && |
694 | !(status & ACPI_EC_FLAG_SCI) && | |
3afcf2ec | 695 | (t->command == ACPI_EC_COMMAND_QUERY)) { |
f8b8eb71 | 696 | ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL); |
3afcf2ec | 697 | t->rdata[t->ri++] = 0x00; |
f8b8eb71 | 698 | ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE); |
9d8993be LZ |
699 | ec_dbg_evt("Command(%s) completed by software", |
700 | acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY)); | |
3afcf2ec LZ |
701 | wakeup = true; |
702 | } else if ((status & ACPI_EC_FLAG_IBF) == 0) { | |
f92fca00 | 703 | acpi_ec_write_cmd(ec, t->command); |
f8b8eb71 | 704 | ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL); |
7c6db4e0 | 705 | } else |
dd15f8c4 | 706 | goto err; |
0c78808f | 707 | goto out; |
f92fca00 | 708 | } |
dd15f8c4 | 709 | err: |
a3cd8d27 FT |
710 | /* |
711 | * If SCI bit is set, then don't think it's a false IRQ | |
712 | * otherwise will take a not handled IRQ as a false one. | |
713 | */ | |
f92fca00 | 714 | if (!(status & ACPI_EC_FLAG_SCI)) { |
e1d4d90f LZ |
715 | if (in_interrupt() && t) { |
716 | if (t->irq_count < ec_storm_threshold) | |
717 | ++t->irq_count; | |
718 | /* Allow triggering on 0 threshold */ | |
719 | if (t->irq_count == ec_storm_threshold) | |
56f77eeb | 720 | acpi_ec_mask_gpe(ec); |
e1d4d90f | 721 | } |
f92fca00 | 722 | } |
0c78808f | 723 | out: |
74443bbe | 724 | if (status & ACPI_EC_FLAG_SCI) |
37d11391 | 725 | acpi_ec_submit_query(ec); |
0c78808f LZ |
726 | if (wakeup && in_interrupt()) |
727 | wake_up(&ec->wait); | |
f92fca00 | 728 | } |
a3cd8d27 | 729 | |
f92fca00 LZ |
730 | static void start_transaction(struct acpi_ec *ec) |
731 | { | |
732 | ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0; | |
733 | ec->curr->flags = 0; | |
d8d031a6 LZ |
734 | } |
735 | ||
736 | static int ec_guard(struct acpi_ec *ec) | |
737 | { | |
c3a696b6 | 738 | unsigned long guard = usecs_to_jiffies(ec->polling_guard); |
d8d031a6 LZ |
739 | unsigned long timeout = ec->timestamp + guard; |
740 | ||
61197547 | 741 | /* Ensure guarding period before polling EC status */ |
d8d031a6 | 742 | do { |
c3a696b6 | 743 | if (ec->busy_polling) { |
d8d031a6 LZ |
744 | /* Perform busy polling */ |
745 | if (ec_transaction_completed(ec)) | |
746 | return 0; | |
747 | udelay(jiffies_to_usecs(guard)); | |
748 | } else { | |
749 | /* | |
750 | * Perform wait polling | |
61197547 LZ |
751 | * 1. Wait the transaction to be completed by the |
752 | * GPE handler after the transaction enters | |
753 | * ACPI_EC_COMMAND_POLL state. | |
754 | * 2. A special guarding logic is also required | |
755 | * for event clearing mode "event" before the | |
756 | * transaction enters ACPI_EC_COMMAND_POLL | |
757 | * state. | |
d8d031a6 | 758 | */ |
1d68d261 LZ |
759 | if (!ec_transaction_polled(ec) && |
760 | !acpi_ec_guard_event(ec)) | |
d8d031a6 LZ |
761 | break; |
762 | if (wait_event_timeout(ec->wait, | |
763 | ec_transaction_completed(ec), | |
764 | guard)) | |
765 | return 0; | |
766 | } | |
d8d031a6 LZ |
767 | } while (time_before(jiffies, timeout)); |
768 | return -ETIME; | |
845625cd | 769 | } |
03d1d99c | 770 | |
7c6db4e0 AS |
771 | static int ec_poll(struct acpi_ec *ec) |
772 | { | |
2a84cb98 | 773 | unsigned long flags; |
28fe5c82 | 774 | int repeat = 5; /* number of command restarts */ |
7a73e60e | 775 | |
2a84cb98 AS |
776 | while (repeat--) { |
777 | unsigned long delay = jiffies + | |
7a18e96d | 778 | msecs_to_jiffies(ec_delay); |
2a84cb98 | 779 | do { |
d8d031a6 LZ |
780 | if (!ec_guard(ec)) |
781 | return 0; | |
f92fca00 | 782 | spin_lock_irqsave(&ec->lock, flags); |
d8d031a6 | 783 | advance_transaction(ec); |
f92fca00 | 784 | spin_unlock_irqrestore(&ec->lock, flags); |
2a84cb98 | 785 | } while (time_before(jiffies, delay)); |
16a26e85 | 786 | pr_debug("controller reset, restart transaction\n"); |
f351d027 | 787 | spin_lock_irqsave(&ec->lock, flags); |
2a84cb98 | 788 | start_transaction(ec); |
f351d027 | 789 | spin_unlock_irqrestore(&ec->lock, flags); |
af3fd140 | 790 | } |
b77d81b2 | 791 | return -ETIME; |
1da177e4 LT |
792 | } |
793 | ||
8463200a | 794 | static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, |
2a84cb98 | 795 | struct transaction *t) |
45bea155 | 796 | { |
7c6db4e0 | 797 | unsigned long tmp; |
7c6db4e0 | 798 | int ret = 0; |
7a73e60e | 799 | |
7c6db4e0 | 800 | /* start transaction */ |
f351d027 | 801 | spin_lock_irqsave(&ec->lock, tmp); |
9887d22a | 802 | /* Enable GPE for command processing (IBF=0/OBF=1) */ |
37d11391 | 803 | if (!acpi_ec_submit_flushable_request(ec)) { |
ad479e7f LZ |
804 | ret = -EINVAL; |
805 | goto unlock; | |
806 | } | |
770970f0 | 807 | ec_dbg_ref(ec, "Increase command"); |
7c6db4e0 | 808 | /* following two actions should be kept atomic */ |
8463200a | 809 | ec->curr = t; |
3535a3c1 | 810 | ec_dbg_req("Command(%s) started", acpi_ec_cmd_string(t->command)); |
a2f93aea | 811 | start_transaction(ec); |
df9ff918 | 812 | spin_unlock_irqrestore(&ec->lock, tmp); |
d8d031a6 | 813 | |
df9ff918 | 814 | ret = ec_poll(ec); |
d8d031a6 | 815 | |
df9ff918 | 816 | spin_lock_irqsave(&ec->lock, tmp); |
e1d4d90f | 817 | if (t->irq_count == ec_storm_threshold) |
56f77eeb | 818 | acpi_ec_unmask_gpe(ec); |
3535a3c1 | 819 | ec_dbg_req("Command(%s) stopped", acpi_ec_cmd_string(t->command)); |
8463200a | 820 | ec->curr = NULL; |
9887d22a LZ |
821 | /* Disable GPE for command processing (IBF=0/OBF=1) */ |
822 | acpi_ec_complete_request(ec); | |
770970f0 | 823 | ec_dbg_ref(ec, "Decrease command"); |
ad479e7f | 824 | unlock: |
f351d027 | 825 | spin_unlock_irqrestore(&ec->lock, tmp); |
7c6db4e0 AS |
826 | return ret; |
827 | } | |
828 | ||
2a84cb98 | 829 | static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t) |
1da177e4 | 830 | { |
d7a76e4c | 831 | int status; |
50526df6 | 832 | u32 glk; |
7a73e60e | 833 | |
8463200a | 834 | if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata)) |
d550d98d | 835 | return -EINVAL; |
8463200a AS |
836 | if (t->rdata) |
837 | memset(t->rdata, 0, t->rlen); | |
d8d031a6 | 838 | |
f351d027 | 839 | mutex_lock(&ec->mutex); |
703959d4 | 840 | if (ec->global_lock) { |
1da177e4 | 841 | status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk); |
c24e912b | 842 | if (ACPI_FAILURE(status)) { |
7c6db4e0 AS |
843 | status = -ENODEV; |
844 | goto unlock; | |
c24e912b | 845 | } |
1da177e4 | 846 | } |
a62e8f19 | 847 | |
2a84cb98 | 848 | status = acpi_ec_transaction_unlocked(ec, t); |
a62e8f19 | 849 | |
703959d4 | 850 | if (ec->global_lock) |
1da177e4 | 851 | acpi_release_global_lock(glk); |
7c6db4e0 | 852 | unlock: |
f351d027 | 853 | mutex_unlock(&ec->mutex); |
d550d98d | 854 | return status; |
1da177e4 LT |
855 | } |
856 | ||
8a383ef0 | 857 | static int acpi_ec_burst_enable(struct acpi_ec *ec) |
c45aac43 AS |
858 | { |
859 | u8 d; | |
8463200a AS |
860 | struct transaction t = {.command = ACPI_EC_BURST_ENABLE, |
861 | .wdata = NULL, .rdata = &d, | |
862 | .wlen = 0, .rlen = 1}; | |
863 | ||
2a84cb98 | 864 | return acpi_ec_transaction(ec, &t); |
c45aac43 AS |
865 | } |
866 | ||
8a383ef0 | 867 | static int acpi_ec_burst_disable(struct acpi_ec *ec) |
c45aac43 | 868 | { |
8463200a AS |
869 | struct transaction t = {.command = ACPI_EC_BURST_DISABLE, |
870 | .wdata = NULL, .rdata = NULL, | |
871 | .wlen = 0, .rlen = 0}; | |
872 | ||
7c6db4e0 | 873 | return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ? |
2a84cb98 | 874 | acpi_ec_transaction(ec, &t) : 0; |
c45aac43 AS |
875 | } |
876 | ||
7a73e60e | 877 | static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data) |
3576cf61 DS |
878 | { |
879 | int result; | |
880 | u8 d; | |
8463200a AS |
881 | struct transaction t = {.command = ACPI_EC_COMMAND_READ, |
882 | .wdata = &address, .rdata = &d, | |
883 | .wlen = 1, .rlen = 1}; | |
3576cf61 | 884 | |
2a84cb98 | 885 | result = acpi_ec_transaction(ec, &t); |
3576cf61 DS |
886 | *data = d; |
887 | return result; | |
888 | } | |
6ffb221a | 889 | |
3576cf61 DS |
890 | static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data) |
891 | { | |
6ccedb10 | 892 | u8 wdata[2] = { address, data }; |
8463200a AS |
893 | struct transaction t = {.command = ACPI_EC_COMMAND_WRITE, |
894 | .wdata = wdata, .rdata = NULL, | |
895 | .wlen = 2, .rlen = 0}; | |
896 | ||
2a84cb98 | 897 | return acpi_ec_transaction(ec, &t); |
3576cf61 DS |
898 | } |
899 | ||
b76b51ba | 900 | int ec_read(u8 addr, u8 *val) |
1da177e4 | 901 | { |
1da177e4 | 902 | int err; |
6ffb221a | 903 | u8 temp_data; |
1da177e4 LT |
904 | |
905 | if (!first_ec) | |
906 | return -ENODEV; | |
907 | ||
d033879c | 908 | err = acpi_ec_read(first_ec, addr, &temp_data); |
1da177e4 LT |
909 | |
910 | if (!err) { | |
911 | *val = temp_data; | |
912 | return 0; | |
7a73e60e LZ |
913 | } |
914 | return err; | |
1da177e4 LT |
915 | } |
916 | EXPORT_SYMBOL(ec_read); | |
917 | ||
50526df6 | 918 | int ec_write(u8 addr, u8 val) |
1da177e4 | 919 | { |
1da177e4 LT |
920 | int err; |
921 | ||
922 | if (!first_ec) | |
923 | return -ENODEV; | |
924 | ||
d033879c | 925 | err = acpi_ec_write(first_ec, addr, val); |
1da177e4 LT |
926 | |
927 | return err; | |
928 | } | |
929 | EXPORT_SYMBOL(ec_write); | |
930 | ||
616362de | 931 | int ec_transaction(u8 command, |
7a73e60e LZ |
932 | const u8 *wdata, unsigned wdata_len, |
933 | u8 *rdata, unsigned rdata_len) | |
45bea155 | 934 | { |
8463200a AS |
935 | struct transaction t = {.command = command, |
936 | .wdata = wdata, .rdata = rdata, | |
937 | .wlen = wdata_len, .rlen = rdata_len}; | |
7a73e60e | 938 | |
d7a76e4c LP |
939 | if (!first_ec) |
940 | return -ENODEV; | |
45bea155 | 941 | |
2a84cb98 | 942 | return acpi_ec_transaction(first_ec, &t); |
45bea155 | 943 | } |
ab9e43c6 LP |
944 | EXPORT_SYMBOL(ec_transaction); |
945 | ||
3e2abc5a SF |
946 | /* Get the handle to the EC device */ |
947 | acpi_handle ec_get_handle(void) | |
948 | { | |
949 | if (!first_ec) | |
950 | return NULL; | |
951 | return first_ec->handle; | |
952 | } | |
3e2abc5a SF |
953 | EXPORT_SYMBOL(ec_get_handle); |
954 | ||
ad479e7f LZ |
955 | static void acpi_ec_start(struct acpi_ec *ec, bool resuming) |
956 | { | |
957 | unsigned long flags; | |
958 | ||
959 | spin_lock_irqsave(&ec->lock, flags); | |
960 | if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) { | |
3535a3c1 | 961 | ec_dbg_drv("Starting EC"); |
9887d22a | 962 | /* Enable GPE for event processing (SCI_EVT=1) */ |
770970f0 | 963 | if (!resuming) { |
9887d22a | 964 | acpi_ec_submit_request(ec); |
770970f0 | 965 | ec_dbg_ref(ec, "Increase driver"); |
c2b46d67 | 966 | } |
3535a3c1 | 967 | ec_log_drv("EC started"); |
ad479e7f LZ |
968 | } |
969 | spin_unlock_irqrestore(&ec->lock, flags); | |
970 | } | |
971 | ||
9887d22a LZ |
972 | static bool acpi_ec_stopped(struct acpi_ec *ec) |
973 | { | |
974 | unsigned long flags; | |
975 | bool flushed; | |
976 | ||
977 | spin_lock_irqsave(&ec->lock, flags); | |
978 | flushed = acpi_ec_flushed(ec); | |
979 | spin_unlock_irqrestore(&ec->lock, flags); | |
980 | return flushed; | |
981 | } | |
982 | ||
ad479e7f LZ |
983 | static void acpi_ec_stop(struct acpi_ec *ec, bool suspending) |
984 | { | |
985 | unsigned long flags; | |
986 | ||
987 | spin_lock_irqsave(&ec->lock, flags); | |
988 | if (acpi_ec_started(ec)) { | |
3535a3c1 | 989 | ec_dbg_drv("Stopping EC"); |
ad479e7f | 990 | set_bit(EC_FLAGS_STOPPED, &ec->flags); |
9887d22a LZ |
991 | spin_unlock_irqrestore(&ec->lock, flags); |
992 | wait_event(ec->wait, acpi_ec_stopped(ec)); | |
993 | spin_lock_irqsave(&ec->lock, flags); | |
994 | /* Disable GPE for event processing (SCI_EVT=1) */ | |
770970f0 | 995 | if (!suspending) { |
9887d22a | 996 | acpi_ec_complete_request(ec); |
770970f0 | 997 | ec_dbg_ref(ec, "Decrease driver"); |
39a2a2aa | 998 | } else if (!ec_freeze_events) |
750f628b | 999 | __acpi_ec_disable_event(ec); |
ad479e7f LZ |
1000 | clear_bit(EC_FLAGS_STARTED, &ec->flags); |
1001 | clear_bit(EC_FLAGS_STOPPED, &ec->flags); | |
3535a3c1 | 1002 | ec_log_drv("EC stopped"); |
ad479e7f LZ |
1003 | } |
1004 | spin_unlock_irqrestore(&ec->lock, flags); | |
1005 | } | |
1006 | ||
c3a696b6 LZ |
1007 | static void acpi_ec_enter_noirq(struct acpi_ec *ec) |
1008 | { | |
1009 | unsigned long flags; | |
1010 | ||
1011 | spin_lock_irqsave(&ec->lock, flags); | |
1012 | ec->busy_polling = true; | |
1013 | ec->polling_guard = 0; | |
1014 | ec_log_drv("interrupt blocked"); | |
1015 | spin_unlock_irqrestore(&ec->lock, flags); | |
1016 | } | |
1017 | ||
1018 | static void acpi_ec_leave_noirq(struct acpi_ec *ec) | |
1019 | { | |
1020 | unsigned long flags; | |
1021 | ||
1022 | spin_lock_irqsave(&ec->lock, flags); | |
1023 | ec->busy_polling = ec_busy_polling; | |
1024 | ec->polling_guard = ec_polling_guard; | |
1025 | ec_log_drv("interrupt unblocked"); | |
1026 | spin_unlock_irqrestore(&ec->lock, flags); | |
1027 | } | |
1028 | ||
fe955682 | 1029 | void acpi_ec_block_transactions(void) |
f6bb13aa RW |
1030 | { |
1031 | struct acpi_ec *ec = first_ec; | |
1032 | ||
1033 | if (!ec) | |
1034 | return; | |
1035 | ||
f351d027 | 1036 | mutex_lock(&ec->mutex); |
f6bb13aa | 1037 | /* Prevent transactions from being carried out */ |
ad479e7f | 1038 | acpi_ec_stop(ec, true); |
f351d027 | 1039 | mutex_unlock(&ec->mutex); |
f6bb13aa RW |
1040 | } |
1041 | ||
fe955682 | 1042 | void acpi_ec_unblock_transactions(void) |
d5a64513 RW |
1043 | { |
1044 | /* | |
1045 | * Allow transactions to happen again (this function is called from | |
1046 | * atomic context during wakeup, so we don't need to acquire the mutex). | |
1047 | */ | |
1048 | if (first_ec) | |
ad479e7f | 1049 | acpi_ec_start(first_ec, true); |
d5a64513 RW |
1050 | } |
1051 | ||
10a08fd6 | 1052 | #ifdef CONFIG_PM_SLEEP |
f941d3e4 RW |
1053 | void acpi_ec_mark_gpe_for_wake(void) |
1054 | { | |
1055 | if (first_ec && !ec_no_wakeup) | |
1056 | acpi_mark_gpe_for_wake(NULL, first_ec->gpe); | |
1057 | } | |
10a08fd6 | 1058 | EXPORT_SYMBOL_GPL(acpi_ec_mark_gpe_for_wake); |
f941d3e4 RW |
1059 | |
1060 | void acpi_ec_set_gpe_wake_mask(u8 action) | |
1061 | { | |
10a08fd6 | 1062 | if (pm_suspend_no_platform() && first_ec && !ec_no_wakeup) |
f941d3e4 RW |
1063 | acpi_set_gpe_wake_mask(NULL, first_ec->gpe, action); |
1064 | } | |
10a08fd6 RW |
1065 | EXPORT_SYMBOL_GPL(acpi_ec_set_gpe_wake_mask); |
1066 | #endif | |
f941d3e4 | 1067 | |
9089f16e | 1068 | bool acpi_ec_dispatch_gpe(void) |
68e22011 | 1069 | { |
9089f16e RW |
1070 | u32 ret; |
1071 | ||
1072 | if (!first_ec) | |
1073 | return false; | |
1074 | ||
1075 | ret = acpi_dispatch_gpe(NULL, first_ec->gpe); | |
1076 | return ret == ACPI_INTERRUPT_HANDLED; | |
68e22011 RW |
1077 | } |
1078 | ||
1da177e4 LT |
1079 | /* -------------------------------------------------------------------------- |
1080 | Event Management | |
1081 | -------------------------------------------------------------------------- */ | |
01305d41 LZ |
1082 | static struct acpi_ec_query_handler * |
1083 | acpi_ec_get_query_handler(struct acpi_ec_query_handler *handler) | |
1084 | { | |
1085 | if (handler) | |
1086 | kref_get(&handler->kref); | |
1087 | return handler; | |
1088 | } | |
1089 | ||
0700c047 LZ |
1090 | static struct acpi_ec_query_handler * |
1091 | acpi_ec_get_query_handler_by_value(struct acpi_ec *ec, u8 value) | |
1092 | { | |
1093 | struct acpi_ec_query_handler *handler; | |
1094 | bool found = false; | |
1095 | ||
1096 | mutex_lock(&ec->mutex); | |
1097 | list_for_each_entry(handler, &ec->list, node) { | |
1098 | if (value == handler->query_bit) { | |
1099 | found = true; | |
1100 | break; | |
1101 | } | |
1102 | } | |
1103 | mutex_unlock(&ec->mutex); | |
1104 | return found ? acpi_ec_get_query_handler(handler) : NULL; | |
1105 | } | |
1106 | ||
01305d41 LZ |
1107 | static void acpi_ec_query_handler_release(struct kref *kref) |
1108 | { | |
1109 | struct acpi_ec_query_handler *handler = | |
1110 | container_of(kref, struct acpi_ec_query_handler, kref); | |
1111 | ||
1112 | kfree(handler); | |
1113 | } | |
1114 | ||
1115 | static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler) | |
1116 | { | |
1117 | kref_put(&handler->kref, acpi_ec_query_handler_release); | |
1118 | } | |
1119 | ||
837012ed AS |
1120 | int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit, |
1121 | acpi_handle handle, acpi_ec_query_func func, | |
1122 | void *data) | |
1123 | { | |
1124 | struct acpi_ec_query_handler *handler = | |
1125 | kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL); | |
7a73e60e | 1126 | |
837012ed AS |
1127 | if (!handler) |
1128 | return -ENOMEM; | |
1129 | ||
1130 | handler->query_bit = query_bit; | |
1131 | handler->handle = handle; | |
1132 | handler->func = func; | |
1133 | handler->data = data; | |
f351d027 | 1134 | mutex_lock(&ec->mutex); |
01305d41 | 1135 | kref_init(&handler->kref); |
30c08574 | 1136 | list_add(&handler->node, &ec->list); |
f351d027 | 1137 | mutex_unlock(&ec->mutex); |
837012ed AS |
1138 | return 0; |
1139 | } | |
837012ed AS |
1140 | EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler); |
1141 | ||
0700c047 LZ |
1142 | static void acpi_ec_remove_query_handlers(struct acpi_ec *ec, |
1143 | bool remove_all, u8 query_bit) | |
837012ed | 1144 | { |
1544fdbc | 1145 | struct acpi_ec_query_handler *handler, *tmp; |
01305d41 | 1146 | LIST_HEAD(free_list); |
7a73e60e | 1147 | |
f351d027 | 1148 | mutex_lock(&ec->mutex); |
1544fdbc | 1149 | list_for_each_entry_safe(handler, tmp, &ec->list, node) { |
0700c047 | 1150 | if (remove_all || query_bit == handler->query_bit) { |
01305d41 LZ |
1151 | list_del_init(&handler->node); |
1152 | list_add(&handler->node, &free_list); | |
837012ed AS |
1153 | } |
1154 | } | |
f351d027 | 1155 | mutex_unlock(&ec->mutex); |
6b5eab54 | 1156 | list_for_each_entry_safe(handler, tmp, &free_list, node) |
01305d41 | 1157 | acpi_ec_put_query_handler(handler); |
837012ed | 1158 | } |
0700c047 LZ |
1159 | |
1160 | void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit) | |
1161 | { | |
1162 | acpi_ec_remove_query_handlers(ec, false, query_bit); | |
1163 | } | |
837012ed | 1164 | EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler); |
1da177e4 | 1165 | |
02b771b6 | 1166 | static struct acpi_ec_query *acpi_ec_create_query(u8 *pval) |
45bea155 | 1167 | { |
02b771b6 LZ |
1168 | struct acpi_ec_query *q; |
1169 | struct transaction *t; | |
1170 | ||
1171 | q = kzalloc(sizeof (struct acpi_ec_query), GFP_KERNEL); | |
1172 | if (!q) | |
1173 | return NULL; | |
1174 | INIT_WORK(&q->work, acpi_ec_event_processor); | |
1175 | t = &q->transaction; | |
1176 | t->command = ACPI_EC_COMMAND_QUERY; | |
1177 | t->rdata = pval; | |
1178 | t->rlen = 1; | |
1179 | return q; | |
1180 | } | |
1181 | ||
1182 | static void acpi_ec_delete_query(struct acpi_ec_query *q) | |
1183 | { | |
1184 | if (q) { | |
1185 | if (q->handler) | |
1186 | acpi_ec_put_query_handler(q->handler); | |
1187 | kfree(q); | |
1188 | } | |
1189 | } | |
1190 | ||
1191 | static void acpi_ec_event_processor(struct work_struct *work) | |
1192 | { | |
1193 | struct acpi_ec_query *q = container_of(work, struct acpi_ec_query, work); | |
1194 | struct acpi_ec_query_handler *handler = q->handler; | |
7a73e60e | 1195 | |
3535a3c1 | 1196 | ec_dbg_evt("Query(0x%02x) started", handler->query_bit); |
a62e8f19 AS |
1197 | if (handler->func) |
1198 | handler->func(handler->data); | |
1199 | else if (handler->handle) | |
1200 | acpi_evaluate_object(handler->handle, NULL, NULL, NULL); | |
3535a3c1 | 1201 | ec_dbg_evt("Query(0x%02x) stopped", handler->query_bit); |
02b771b6 | 1202 | acpi_ec_delete_query(q); |
a62e8f19 AS |
1203 | } |
1204 | ||
550b3aac | 1205 | static int acpi_ec_query(struct acpi_ec *ec, u8 *data) |
a62e8f19 AS |
1206 | { |
1207 | u8 value = 0; | |
c2cf5769 | 1208 | int result; |
02b771b6 LZ |
1209 | struct acpi_ec_query *q; |
1210 | ||
1211 | q = acpi_ec_create_query(&value); | |
1212 | if (!q) | |
1213 | return -ENOMEM; | |
3eba563e | 1214 | |
550b3aac LZ |
1215 | /* |
1216 | * Query the EC to find out which _Qxx method we need to evaluate. | |
1217 | * Note that successful completion of the query causes the ACPI_EC_SCI | |
1218 | * bit to be cleared (and thus clearing the interrupt source). | |
1219 | */ | |
02b771b6 | 1220 | result = acpi_ec_transaction(ec, &q->transaction); |
550b3aac | 1221 | if (!value) |
02b771b6 LZ |
1222 | result = -ENODATA; |
1223 | if (result) | |
1224 | goto err_exit; | |
3eba563e | 1225 | |
0700c047 LZ |
1226 | q->handler = acpi_ec_get_query_handler_by_value(ec, value); |
1227 | if (!q->handler) { | |
1228 | result = -ENODATA; | |
1229 | goto err_exit; | |
1230 | } | |
1231 | ||
1232 | /* | |
1233 | * It is reported that _Qxx are evaluated in a parallel way on | |
1234 | * Windows: | |
1235 | * https://bugzilla.kernel.org/show_bug.cgi?id=94411 | |
1236 | * | |
1237 | * Put this log entry before schedule_work() in order to make | |
1238 | * it appearing before any other log entries occurred during the | |
1239 | * work queue execution. | |
1240 | */ | |
1241 | ec_dbg_evt("Query(0x%02x) scheduled", value); | |
e1191bd4 | 1242 | if (!queue_work(ec_query_wq, &q->work)) { |
0700c047 LZ |
1243 | ec_dbg_evt("Query(0x%02x) overlapped", value); |
1244 | result = -EBUSY; | |
837012ed | 1245 | } |
02b771b6 LZ |
1246 | |
1247 | err_exit: | |
4981c2b7 | 1248 | if (result) |
02b771b6 LZ |
1249 | acpi_ec_delete_query(q); |
1250 | if (data) | |
1251 | *data = value; | |
c2cf5769 | 1252 | return result; |
a62e8f19 AS |
1253 | } |
1254 | ||
1d68d261 LZ |
1255 | static void acpi_ec_check_event(struct acpi_ec *ec) |
1256 | { | |
1257 | unsigned long flags; | |
1258 | ||
1259 | if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT) { | |
1260 | if (ec_guard(ec)) { | |
1261 | spin_lock_irqsave(&ec->lock, flags); | |
1262 | /* | |
1263 | * Take care of the SCI_EVT unless no one else is | |
1264 | * taking care of it. | |
1265 | */ | |
1266 | if (!ec->curr) | |
1267 | advance_transaction(ec); | |
1268 | spin_unlock_irqrestore(&ec->lock, flags); | |
1269 | } | |
1270 | } | |
1271 | } | |
1272 | ||
9d8993be | 1273 | static void acpi_ec_event_handler(struct work_struct *work) |
a62e8f19 | 1274 | { |
9d8993be | 1275 | unsigned long flags; |
74443bbe | 1276 | struct acpi_ec *ec = container_of(work, struct acpi_ec, work); |
7a73e60e | 1277 | |
9d8993be LZ |
1278 | ec_dbg_evt("Event started"); |
1279 | ||
1280 | spin_lock_irqsave(&ec->lock, flags); | |
1281 | while (ec->nr_pending_queries) { | |
1282 | spin_unlock_irqrestore(&ec->lock, flags); | |
1283 | (void)acpi_ec_query(ec, NULL); | |
1284 | spin_lock_irqsave(&ec->lock, flags); | |
1285 | ec->nr_pending_queries--; | |
66db3834 LZ |
1286 | /* |
1287 | * Before exit, make sure that this work item can be | |
1288 | * scheduled again. There might be QR_EC failures, leaving | |
1289 | * EC_FLAGS_QUERY_PENDING uncleared and preventing this work | |
1290 | * item from being scheduled again. | |
1291 | */ | |
1292 | if (!ec->nr_pending_queries) { | |
1293 | if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS || | |
1294 | ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY) | |
1295 | acpi_ec_complete_query(ec); | |
1296 | } | |
9d8993be LZ |
1297 | } |
1298 | spin_unlock_irqrestore(&ec->lock, flags); | |
1299 | ||
1300 | ec_dbg_evt("Event stopped"); | |
1d68d261 LZ |
1301 | |
1302 | acpi_ec_check_event(ec); | |
45bea155 | 1303 | } |
1da177e4 | 1304 | |
8b6cd8ad LM |
1305 | static u32 acpi_ec_gpe_handler(acpi_handle gpe_device, |
1306 | u32 gpe_number, void *data) | |
1da177e4 | 1307 | { |
f92fca00 | 1308 | unsigned long flags; |
3d02b90b | 1309 | struct acpi_ec *ec = data; |
7c6db4e0 | 1310 | |
f92fca00 | 1311 | spin_lock_irqsave(&ec->lock, flags); |
0c78808f | 1312 | advance_transaction(ec); |
c0d65341 | 1313 | spin_unlock_irqrestore(&ec->lock, flags); |
ca37bfdf | 1314 | return ACPI_INTERRUPT_HANDLED; |
845625cd AS |
1315 | } |
1316 | ||
1da177e4 | 1317 | /* -------------------------------------------------------------------------- |
7a73e60e LZ |
1318 | * Address Space Management |
1319 | * -------------------------------------------------------------------------- */ | |
1da177e4 | 1320 | |
1da177e4 | 1321 | static acpi_status |
5b7734b4 | 1322 | acpi_ec_space_handler(u32 function, acpi_physical_address address, |
dadf28a1 | 1323 | u32 bits, u64 *value64, |
50526df6 | 1324 | void *handler_context, void *region_context) |
1da177e4 | 1325 | { |
3d02b90b | 1326 | struct acpi_ec *ec = handler_context; |
dadf28a1 AS |
1327 | int result = 0, i, bytes = bits / 8; |
1328 | u8 *value = (u8 *)value64; | |
1da177e4 | 1329 | |
1da177e4 | 1330 | if ((address > 0xFF) || !value || !handler_context) |
d550d98d | 1331 | return AE_BAD_PARAMETER; |
1da177e4 | 1332 | |
5b7734b4 | 1333 | if (function != ACPI_READ && function != ACPI_WRITE) |
d550d98d | 1334 | return AE_BAD_PARAMETER; |
1da177e4 | 1335 | |
c3a696b6 | 1336 | if (ec->busy_polling || bits > 8) |
6a63b06f | 1337 | acpi_ec_burst_enable(ec); |
b3b233c7 | 1338 | |
dadf28a1 AS |
1339 | for (i = 0; i < bytes; ++i, ++address, ++value) |
1340 | result = (function == ACPI_READ) ? | |
1341 | acpi_ec_read(ec, address, value) : | |
1342 | acpi_ec_write(ec, address, *value); | |
1da177e4 | 1343 | |
c3a696b6 | 1344 | if (ec->busy_polling || bits > 8) |
6a63b06f | 1345 | acpi_ec_burst_disable(ec); |
b3b233c7 | 1346 | |
1da177e4 LT |
1347 | switch (result) { |
1348 | case -EINVAL: | |
d550d98d | 1349 | return AE_BAD_PARAMETER; |
1da177e4 | 1350 | case -ENODEV: |
d550d98d | 1351 | return AE_NOT_FOUND; |
1da177e4 | 1352 | case -ETIME: |
d550d98d | 1353 | return AE_TIME; |
1da177e4 | 1354 | default: |
d550d98d | 1355 | return AE_OK; |
1da177e4 | 1356 | } |
1da177e4 LT |
1357 | } |
1358 | ||
1da177e4 | 1359 | /* -------------------------------------------------------------------------- |
7a73e60e LZ |
1360 | * Driver Interface |
1361 | * -------------------------------------------------------------------------- */ | |
1362 | ||
c0900c35 AS |
1363 | static acpi_status |
1364 | ec_parse_io_ports(struct acpi_resource *resource, void *context); | |
1365 | ||
72c77b7e LZ |
1366 | static void acpi_ec_free(struct acpi_ec *ec) |
1367 | { | |
1368 | if (first_ec == ec) | |
1369 | first_ec = NULL; | |
1370 | if (boot_ec == ec) | |
1371 | boot_ec = NULL; | |
1372 | kfree(ec); | |
1373 | } | |
1374 | ||
1375 | static struct acpi_ec *acpi_ec_alloc(void) | |
c0900c35 AS |
1376 | { |
1377 | struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL); | |
7a73e60e | 1378 | |
c0900c35 AS |
1379 | if (!ec) |
1380 | return NULL; | |
f351d027 | 1381 | mutex_init(&ec->mutex); |
c0900c35 | 1382 | init_waitqueue_head(&ec->wait); |
837012ed | 1383 | INIT_LIST_HEAD(&ec->list); |
f351d027 | 1384 | spin_lock_init(&ec->lock); |
9d8993be | 1385 | INIT_WORK(&ec->work, acpi_ec_event_handler); |
d8d031a6 | 1386 | ec->timestamp = jiffies; |
c3a696b6 LZ |
1387 | ec->busy_polling = true; |
1388 | ec->polling_guard = 0; | |
c0900c35 AS |
1389 | return ec; |
1390 | } | |
837012ed | 1391 | |
c019b193 AS |
1392 | static acpi_status |
1393 | acpi_ec_register_query_methods(acpi_handle handle, u32 level, | |
1394 | void *context, void **return_value) | |
1395 | { | |
0175d562 LM |
1396 | char node_name[5]; |
1397 | struct acpi_buffer buffer = { sizeof(node_name), node_name }; | |
c019b193 AS |
1398 | struct acpi_ec *ec = context; |
1399 | int value = 0; | |
0175d562 LM |
1400 | acpi_status status; |
1401 | ||
1402 | status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); | |
1403 | ||
7a73e60e | 1404 | if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) |
c019b193 | 1405 | acpi_ec_add_query_handler(ec, value, handle, NULL, NULL); |
c019b193 AS |
1406 | return AE_OK; |
1407 | } | |
1408 | ||
cd8c93a4 AS |
1409 | static acpi_status |
1410 | ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval) | |
837012ed | 1411 | { |
cd8c93a4 | 1412 | acpi_status status; |
d21cf3c1 | 1413 | unsigned long long tmp = 0; |
cd8c93a4 | 1414 | struct acpi_ec *ec = context; |
a5032bfd AS |
1415 | |
1416 | /* clear addr values, ec_parse_io_ports depend on it */ | |
1417 | ec->command_addr = ec->data_addr = 0; | |
1418 | ||
cd8c93a4 AS |
1419 | status = acpi_walk_resources(handle, METHOD_NAME__CRS, |
1420 | ec_parse_io_ports, ec); | |
1421 | if (ACPI_FAILURE(status)) | |
1422 | return status; | |
ae56c9fd LZ |
1423 | if (ec->data_addr == 0 || ec->command_addr == 0) |
1424 | return AE_OK; | |
837012ed | 1425 | |
440f53da CC |
1426 | if (boot_ec && boot_ec_is_ecdt && EC_FLAGS_IGNORE_DSDT_GPE) { |
1427 | /* | |
1428 | * Always inherit the GPE number setting from the ECDT | |
1429 | * EC. | |
1430 | */ | |
1431 | ec->gpe = boot_ec->gpe; | |
1432 | } else { | |
1433 | /* Get GPE bit assignment (EC events). */ | |
1434 | /* TODO: Add support for _GPE returning a package */ | |
1435 | status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp); | |
1436 | if (ACPI_FAILURE(status)) | |
1437 | return status; | |
1438 | ec->gpe = tmp; | |
1439 | } | |
837012ed | 1440 | /* Use the global lock for all EC transactions? */ |
d21cf3c1 | 1441 | tmp = 0; |
27663c58 MW |
1442 | acpi_evaluate_integer(handle, "_GLK", NULL, &tmp); |
1443 | ec->global_lock = tmp; | |
837012ed | 1444 | ec->handle = handle; |
cd8c93a4 | 1445 | return AE_CTRL_TERMINATE; |
837012ed AS |
1446 | } |
1447 | ||
72c77b7e LZ |
1448 | /* |
1449 | * Note: This function returns an error code only when the address space | |
1450 | * handler is not installed, which means "not able to handle | |
1451 | * transactions". | |
1452 | */ | |
2a570840 | 1453 | static int ec_install_handlers(struct acpi_ec *ec, bool handle_events) |
5efc5476 BH |
1454 | { |
1455 | acpi_status status; | |
7a73e60e | 1456 | |
ad479e7f | 1457 | acpi_ec_start(ec, false); |
0e1affe4 LZ |
1458 | |
1459 | if (!test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) { | |
c3a696b6 | 1460 | acpi_ec_enter_noirq(ec); |
0e1affe4 LZ |
1461 | status = acpi_install_address_space_handler(ec->handle, |
1462 | ACPI_ADR_SPACE_EC, | |
1463 | &acpi_ec_space_handler, | |
1464 | NULL, ec); | |
1465 | if (ACPI_FAILURE(status)) { | |
1466 | if (status == AE_NOT_FOUND) { | |
1467 | /* | |
1468 | * Maybe OS fails in evaluating the _REG | |
1469 | * object. The AE_NOT_FOUND error will be | |
1470 | * ignored and OS * continue to initialize | |
1471 | * EC. | |
1472 | */ | |
1473 | pr_err("Fail in evaluating the _REG object" | |
1474 | " of EC device. Broken bios is suspected.\n"); | |
1475 | } else { | |
1476 | acpi_ec_stop(ec, false); | |
1477 | return -ENODEV; | |
1478 | } | |
1479 | } | |
1480 | set_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags); | |
1481 | } | |
1482 | ||
2a570840 LZ |
1483 | if (!handle_events) |
1484 | return 0; | |
1485 | ||
1486 | if (!test_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags)) { | |
1487 | /* Find and register all query methods */ | |
1488 | acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1, | |
1489 | acpi_ec_register_query_methods, | |
1490 | NULL, ec, NULL); | |
1491 | set_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags); | |
1492 | } | |
0e1affe4 LZ |
1493 | if (!test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags)) { |
1494 | status = acpi_install_gpe_raw_handler(NULL, ec->gpe, | |
1495 | ACPI_GPE_EDGE_TRIGGERED, | |
1496 | &acpi_ec_gpe_handler, ec); | |
1497 | /* This is not fatal as we can poll EC events */ | |
1498 | if (ACPI_SUCCESS(status)) { | |
1499 | set_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags); | |
c3a696b6 | 1500 | acpi_ec_leave_noirq(ec); |
0e1affe4 LZ |
1501 | if (test_bit(EC_FLAGS_STARTED, &ec->flags) && |
1502 | ec->reference_count >= 1) | |
1503 | acpi_ec_enable_gpe(ec, true); | |
5efc5476 BH |
1504 | } |
1505 | } | |
53c5eaab LZ |
1506 | /* EC is fully operational, allow queries */ |
1507 | acpi_ec_enable_event(ec); | |
5efc5476 | 1508 | |
5efc5476 BH |
1509 | return 0; |
1510 | } | |
1511 | ||
4c611060 AS |
1512 | static void ec_remove_handlers(struct acpi_ec *ec) |
1513 | { | |
0e1affe4 LZ |
1514 | if (test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) { |
1515 | if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle, | |
1516 | ACPI_ADR_SPACE_EC, &acpi_ec_space_handler))) | |
1517 | pr_err("failed to remove space handler\n"); | |
1518 | clear_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags); | |
1519 | } | |
1520 | ||
fa5b4a50 LZ |
1521 | /* |
1522 | * Stops handling the EC transactions after removing the operation | |
1523 | * region handler. This is required because _REG(DISCONNECT) | |
1524 | * invoked during the removal can result in new EC transactions. | |
1525 | * | |
1526 | * Flushes the EC requests and thus disables the GPE before | |
1527 | * removing the GPE handler. This is required by the current ACPICA | |
1528 | * GPE core. ACPICA GPE core will automatically disable a GPE when | |
1529 | * it is indicated but there is no way to handle it. So the drivers | |
1530 | * must disable the GPEs prior to removing the GPE handlers. | |
1531 | */ | |
1532 | acpi_ec_stop(ec, false); | |
1533 | ||
0e1affe4 LZ |
1534 | if (test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags)) { |
1535 | if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe, | |
1536 | &acpi_ec_gpe_handler))) | |
1537 | pr_err("failed to remove gpe handler\n"); | |
1538 | clear_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags); | |
1539 | } | |
2a570840 LZ |
1540 | if (test_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags)) { |
1541 | acpi_ec_remove_query_handlers(ec, true, 0); | |
1542 | clear_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags); | |
1543 | } | |
4c611060 AS |
1544 | } |
1545 | ||
2a570840 | 1546 | static int acpi_ec_setup(struct acpi_ec *ec, bool handle_events) |
1da177e4 | 1547 | { |
72c77b7e | 1548 | int ret; |
c0900c35 | 1549 | |
2a570840 | 1550 | ret = ec_install_handlers(ec, handle_events); |
72c77b7e LZ |
1551 | if (ret) |
1552 | return ret; | |
1553 | ||
1554 | /* First EC capable of handling transactions */ | |
1555 | if (!first_ec) { | |
1556 | first_ec = ec; | |
1557 | acpi_handle_info(first_ec->handle, "Used as first EC\n"); | |
a5032bfd | 1558 | } |
72c77b7e LZ |
1559 | |
1560 | acpi_handle_info(ec->handle, | |
3522f867 | 1561 | "GPE=0x%x, EC_CMD/EC_SC=0x%lx, EC_DATA=0x%lx\n", |
72c77b7e LZ |
1562 | ec->gpe, ec->command_addr, ec->data_addr); |
1563 | return ret; | |
1564 | } | |
1565 | ||
97cb159f LZ |
1566 | static bool acpi_ec_ecdt_get_handle(acpi_handle *phandle) |
1567 | { | |
1568 | struct acpi_table_ecdt *ecdt_ptr; | |
1569 | acpi_status status; | |
1570 | acpi_handle handle; | |
1571 | ||
1572 | status = acpi_get_table(ACPI_SIG_ECDT, 1, | |
1573 | (struct acpi_table_header **)&ecdt_ptr); | |
1574 | if (ACPI_FAILURE(status)) | |
1575 | return false; | |
1576 | ||
1577 | status = acpi_get_handle(NULL, ecdt_ptr->id, &handle); | |
1578 | if (ACPI_FAILURE(status)) | |
1579 | return false; | |
1580 | ||
1581 | *phandle = handle; | |
1582 | return true; | |
1583 | } | |
1584 | ||
fd6231e7 LZ |
1585 | static int acpi_ec_add(struct acpi_device *device) |
1586 | { | |
1587 | struct acpi_ec *ec = NULL; | |
116f2b34 | 1588 | bool dep_update = true; |
a64a62ce | 1589 | acpi_status status; |
116f2b34 | 1590 | int ret; |
fd6231e7 LZ |
1591 | |
1592 | strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME); | |
1593 | strcpy(acpi_device_class(device), ACPI_EC_CLASS); | |
1594 | ||
a64a62ce | 1595 | if (!strcmp(acpi_device_hid(device), ACPI_ECDT_HID)) { |
116f2b34 | 1596 | boot_ec_is_ecdt = true; |
a64a62ce | 1597 | ec = boot_ec; |
116f2b34 | 1598 | dep_update = false; |
a64a62ce LZ |
1599 | } else { |
1600 | ec = acpi_ec_alloc(); | |
1601 | if (!ec) | |
1602 | return -ENOMEM; | |
116f2b34 | 1603 | |
a64a62ce LZ |
1604 | status = ec_parse_device(device->handle, 0, ec, NULL); |
1605 | if (status != AE_CTRL_TERMINATE) { | |
46922d2a LZ |
1606 | ret = -EINVAL; |
1607 | goto err_alloc; | |
a64a62ce | 1608 | } |
4c611060 | 1609 | |
116f2b34 RW |
1610 | if (boot_ec && ec->command_addr == boot_ec->command_addr && |
1611 | ec->data_addr == boot_ec->data_addr) { | |
1612 | boot_ec_is_ecdt = false; | |
a64a62ce LZ |
1613 | /* |
1614 | * Trust PNP0C09 namespace location rather than | |
1615 | * ECDT ID. But trust ECDT GPE rather than _GPE | |
1616 | * because of ASUS quirks, so do not change | |
1617 | * boot_ec->gpe to ec->gpe. | |
1618 | */ | |
1619 | boot_ec->handle = ec->handle; | |
1620 | acpi_handle_debug(ec->handle, "duplicated.\n"); | |
1621 | acpi_ec_free(ec); | |
1622 | ec = boot_ec; | |
1623 | } | |
d2c62aef RW |
1624 | } |
1625 | ||
1626 | ret = acpi_ec_setup(ec, true); | |
46922d2a LZ |
1627 | if (ret) |
1628 | goto err_query; | |
1629 | ||
d2c62aef RW |
1630 | if (ec == boot_ec) |
1631 | acpi_handle_info(boot_ec->handle, | |
1632 | "Boot %s EC used to handle transactions and events\n", | |
116f2b34 | 1633 | boot_ec_is_ecdt ? "ECDT" : "DSDT"); |
d2c62aef | 1634 | |
db89b4f0 | 1635 | device->driver_data = ec; |
de4f1046 | 1636 | |
d6795fe3 AK |
1637 | ret = !!request_region(ec->data_addr, 1, "EC data"); |
1638 | WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr); | |
1639 | ret = !!request_region(ec->command_addr, 1, "EC cmd"); | |
1640 | WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr); | |
de4f1046 | 1641 | |
116f2b34 | 1642 | if (dep_update) { |
a64a62ce LZ |
1643 | /* Reprobe devices depending on the EC */ |
1644 | acpi_walk_dep_device_list(ec->handle); | |
1645 | } | |
97cb159f | 1646 | acpi_handle_debug(ec->handle, "enumerated.\n"); |
46922d2a LZ |
1647 | return 0; |
1648 | ||
1649 | err_query: | |
97cb159f LZ |
1650 | if (ec != boot_ec) |
1651 | acpi_ec_remove_query_handlers(ec, true, 0); | |
46922d2a | 1652 | err_alloc: |
97cb159f LZ |
1653 | if (ec != boot_ec) |
1654 | acpi_ec_free(ec); | |
5efc5476 | 1655 | return ret; |
1da177e4 LT |
1656 | } |
1657 | ||
51fac838 | 1658 | static int acpi_ec_remove(struct acpi_device *device) |
1da177e4 | 1659 | { |
01f22462 | 1660 | struct acpi_ec *ec; |
1da177e4 | 1661 | |
1da177e4 | 1662 | if (!device) |
d550d98d | 1663 | return -EINVAL; |
1da177e4 LT |
1664 | |
1665 | ec = acpi_driver_data(device); | |
de4f1046 TR |
1666 | release_region(ec->data_addr, 1); |
1667 | release_region(ec->command_addr, 1); | |
db89b4f0 | 1668 | device->driver_data = NULL; |
97cb159f LZ |
1669 | if (ec != boot_ec) { |
1670 | ec_remove_handlers(ec); | |
1671 | acpi_ec_free(ec); | |
1672 | } | |
d550d98d | 1673 | return 0; |
1da177e4 LT |
1674 | } |
1675 | ||
1da177e4 | 1676 | static acpi_status |
c0900c35 | 1677 | ec_parse_io_ports(struct acpi_resource *resource, void *context) |
1da177e4 | 1678 | { |
3d02b90b | 1679 | struct acpi_ec *ec = context; |
1da177e4 | 1680 | |
01f22462 | 1681 | if (resource->type != ACPI_RESOURCE_TYPE_IO) |
1da177e4 | 1682 | return AE_OK; |
1da177e4 LT |
1683 | |
1684 | /* | |
1685 | * The first address region returned is the data port, and | |
1686 | * the second address region returned is the status/command | |
1687 | * port. | |
1688 | */ | |
de4f1046 | 1689 | if (ec->data_addr == 0) |
6ffb221a | 1690 | ec->data_addr = resource->data.io.minimum; |
de4f1046 | 1691 | else if (ec->command_addr == 0) |
6ffb221a | 1692 | ec->command_addr = resource->data.io.minimum; |
01f22462 | 1693 | else |
1da177e4 | 1694 | return AE_CTRL_TERMINATE; |
1da177e4 | 1695 | |
1da177e4 LT |
1696 | return AE_OK; |
1697 | } | |
1698 | ||
dcf15cbd LZ |
1699 | static const struct acpi_device_id ec_device_ids[] = { |
1700 | {"PNP0C09", 0}, | |
a64a62ce | 1701 | {ACPI_ECDT_HID, 0}, |
dcf15cbd LZ |
1702 | {"", 0}, |
1703 | }; | |
1704 | ||
c712bb58 LZ |
1705 | /* |
1706 | * This function is not Windows-compatible as Windows never enumerates the | |
1707 | * namespace EC before the main ACPI device enumeration process. It is | |
1708 | * retained for historical reason and will be deprecated in the future. | |
1709 | */ | |
fdb3c177 | 1710 | void __init acpi_ec_dsdt_probe(void) |
c04209a7 | 1711 | { |
fd6231e7 | 1712 | struct acpi_ec *ec; |
fdb3c177 | 1713 | acpi_status status; |
fd6231e7 | 1714 | int ret; |
dcf15cbd | 1715 | |
c712bb58 LZ |
1716 | /* |
1717 | * If a platform has ECDT, there is no need to proceed as the | |
1718 | * following probe is not a part of the ACPI device enumeration, | |
1719 | * executing _STA is not safe, and thus this probe may risk of | |
1720 | * picking up an invalid EC device. | |
1721 | */ | |
1722 | if (boot_ec) | |
fdb3c177 | 1723 | return; |
c712bb58 | 1724 | |
fd6231e7 LZ |
1725 | ec = acpi_ec_alloc(); |
1726 | if (!ec) | |
fdb3c177 RW |
1727 | return; |
1728 | ||
dcf15cbd | 1729 | /* |
2a570840 LZ |
1730 | * At this point, the namespace is initialized, so start to find |
1731 | * the namespace objects. | |
dcf15cbd | 1732 | */ |
fdb3c177 | 1733 | status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device, ec, NULL); |
fd6231e7 | 1734 | if (ACPI_FAILURE(status) || !ec->handle) { |
fdb3c177 RW |
1735 | acpi_ec_free(ec); |
1736 | return; | |
c04209a7 | 1737 | } |
fdb3c177 | 1738 | |
2a570840 LZ |
1739 | /* |
1740 | * When the DSDT EC is available, always re-configure boot EC to | |
1741 | * have _REG evaluated. _REG can only be evaluated after the | |
1742 | * namespace initialization. | |
1743 | * At this point, the GPE is not fully initialized, so do not to | |
1744 | * handle the events. | |
1745 | */ | |
c746b6b6 RW |
1746 | ret = acpi_ec_setup(ec, false); |
1747 | if (ret) { | |
72c77b7e | 1748 | acpi_ec_free(ec); |
c746b6b6 RW |
1749 | return; |
1750 | } | |
1751 | ||
1752 | boot_ec = ec; | |
1753 | ||
1754 | acpi_handle_info(ec->handle, | |
1755 | "Boot DSDT EC used to handle transactions\n"); | |
c04209a7 AS |
1756 | } |
1757 | ||
2a570840 LZ |
1758 | /* |
1759 | * If the DSDT EC is not functioning, we still need to prepare a fully | |
1760 | * functioning ECDT EC first in order to handle the events. | |
1761 | * https://bugzilla.kernel.org/show_bug.cgi?id=115021 | |
1762 | */ | |
98529b92 | 1763 | static int __init acpi_ec_ecdt_start(void) |
2a570840 | 1764 | { |
2a570840 LZ |
1765 | acpi_handle handle; |
1766 | ||
1767 | if (!boot_ec) | |
1768 | return -ENODEV; | |
69b957c2 | 1769 | /* In case acpi_ec_ecdt_start() is called after acpi_ec_add() */ |
2a570840 LZ |
1770 | if (!boot_ec_is_ecdt) |
1771 | return -ENODEV; | |
1772 | ||
2a570840 LZ |
1773 | /* |
1774 | * At this point, the namespace and the GPE is initialized, so | |
1775 | * start to find the namespace objects and handle the events. | |
69b957c2 LZ |
1776 | * |
1777 | * Note: ec->handle can be valid if this function is called after | |
1778 | * acpi_ec_add(), hence the fast path. | |
2a570840 | 1779 | */ |
a64a62ce LZ |
1780 | if (boot_ec->handle == ACPI_ROOT_OBJECT) { |
1781 | if (!acpi_ec_ecdt_get_handle(&handle)) | |
1782 | return -ENODEV; | |
1783 | boot_ec->handle = handle; | |
1784 | } | |
1785 | ||
1786 | /* Register to ACPI bus with PM ops attached */ | |
1787 | return acpi_bus_register_early_device(ACPI_BUS_TYPE_ECDT_EC); | |
2a570840 LZ |
1788 | } |
1789 | ||
3cb02aeb | 1790 | #if 0 |
79149001 | 1791 | /* |
3cb02aeb LZ |
1792 | * Some EC firmware variations refuses to respond QR_EC when SCI_EVT is not |
1793 | * set, for which case, we complete the QR_EC without issuing it to the | |
1794 | * firmware. | |
1795 | * https://bugzilla.kernel.org/show_bug.cgi?id=82611 | |
1796 | * https://bugzilla.kernel.org/show_bug.cgi?id=97381 | |
79149001 LZ |
1797 | */ |
1798 | static int ec_flag_query_handshake(const struct dmi_system_id *id) | |
1799 | { | |
1800 | pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n"); | |
1801 | EC_FLAGS_QUERY_HANDSHAKE = 1; | |
1802 | return 0; | |
1803 | } | |
3cb02aeb | 1804 | #endif |
79149001 | 1805 | |
b6a3e147 ZR |
1806 | /* |
1807 | * On some hardware it is necessary to clear events accumulated by the EC during | |
1808 | * sleep. These ECs stop reporting GPEs until they are manually polled, if too | |
1809 | * many events are accumulated. (e.g. Samsung Series 5/9 notebooks) | |
1810 | * | |
1811 | * https://bugzilla.kernel.org/show_bug.cgi?id=44161 | |
1812 | * | |
1813 | * Ideally, the EC should also be instructed NOT to accumulate events during | |
1814 | * sleep (which Windows seems to do somehow), but the interface to control this | |
1815 | * behaviour is not known at this time. | |
1816 | * | |
1817 | * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx, | |
1818 | * however it is very likely that other Samsung models are affected. | |
1819 | * | |
1820 | * On systems which don't accumulate _Q events during sleep, this extra check | |
1821 | * should be harmless. | |
1822 | */ | |
1823 | static int ec_clear_on_resume(const struct dmi_system_id *id) | |
1824 | { | |
1825 | pr_debug("Detected system needing EC poll on resume.\n"); | |
1826 | EC_FLAGS_CLEAR_ON_RESUME = 1; | |
1827 | ec_event_clearing = ACPI_EC_EVT_TIMING_STATUS; | |
1828 | return 0; | |
1829 | } | |
1830 | ||
bc539567 LZ |
1831 | /* |
1832 | * Some ECDTs contain wrong register addresses. | |
1833 | * MSI MS-171F | |
1834 | * https://bugzilla.kernel.org/show_bug.cgi?id=12461 | |
1835 | */ | |
59f0aa94 LZ |
1836 | static int ec_correct_ecdt(const struct dmi_system_id *id) |
1837 | { | |
1838 | pr_debug("Detected system needing ECDT address correction.\n"); | |
1839 | EC_FLAGS_CORRECT_ECDT = 1; | |
1840 | return 0; | |
1841 | } | |
1842 | ||
440f53da CC |
1843 | /* |
1844 | * Some DSDTs contain wrong GPE setting. | |
ef75040a | 1845 | * Asus FX502VD/VE, GL702VMK, X550VXK, X580VD |
440f53da CC |
1846 | * https://bugzilla.kernel.org/show_bug.cgi?id=195651 |
1847 | */ | |
1848 | static int ec_honor_ecdt_gpe(const struct dmi_system_id *id) | |
1849 | { | |
1850 | pr_debug("Detected system needing ignore DSDT GPE setting.\n"); | |
1851 | EC_FLAGS_IGNORE_DSDT_GPE = 1; | |
1852 | return 0; | |
1853 | } | |
1854 | ||
6faadbbb | 1855 | static const struct dmi_system_id ec_dmi_table[] __initconst = { |
0adf3c74 | 1856 | { |
59f0aa94 | 1857 | ec_correct_ecdt, "MSI MS-171F", { |
3174abcf LZ |
1858 | DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star"), |
1859 | DMI_MATCH(DMI_PRODUCT_NAME, "MS-171F"),}, NULL}, | |
440f53da CC |
1860 | { |
1861 | ec_honor_ecdt_gpe, "ASUS FX502VD", { | |
1862 | DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."), | |
1863 | DMI_MATCH(DMI_PRODUCT_NAME, "FX502VD"),}, NULL}, | |
1864 | { | |
1865 | ec_honor_ecdt_gpe, "ASUS FX502VE", { | |
1866 | DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."), | |
1867 | DMI_MATCH(DMI_PRODUCT_NAME, "FX502VE"),}, NULL}, | |
1868 | { | |
ef75040a CC |
1869 | ec_honor_ecdt_gpe, "ASUS GL702VMK", { |
1870 | DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."), | |
1871 | DMI_MATCH(DMI_PRODUCT_NAME, "GL702VMK"),}, NULL}, | |
1872 | { | |
440f53da CC |
1873 | ec_honor_ecdt_gpe, "ASUS X550VXK", { |
1874 | DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."), | |
1875 | DMI_MATCH(DMI_PRODUCT_NAME, "X550VXK"),}, NULL}, | |
1876 | { | |
1877 | ec_honor_ecdt_gpe, "ASUS X580VD", { | |
1878 | DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."), | |
1879 | DMI_MATCH(DMI_PRODUCT_NAME, "X580VD"),}, NULL}, | |
b6a3e147 ZR |
1880 | { |
1881 | ec_clear_on_resume, "Samsung hardware", { | |
1882 | DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL}, | |
0adf3c74 AS |
1883 | {}, |
1884 | }; | |
1885 | ||
fdb3c177 | 1886 | void __init acpi_ec_ecdt_probe(void) |
c0900c35 | 1887 | { |
50526df6 | 1888 | struct acpi_table_ecdt *ecdt_ptr; |
fd6231e7 | 1889 | struct acpi_ec *ec; |
fdb3c177 RW |
1890 | acpi_status status; |
1891 | int ret; | |
45bea155 | 1892 | |
fdb3c177 | 1893 | /* Generate a boot ec context. */ |
0adf3c74 | 1894 | dmi_check_system(ec_dmi_table); |
15a58ed1 AS |
1895 | status = acpi_get_table(ACPI_SIG_ECDT, 1, |
1896 | (struct acpi_table_header **)&ecdt_ptr); | |
fdb3c177 RW |
1897 | if (ACPI_FAILURE(status)) |
1898 | return; | |
0adf3c74 | 1899 | |
59f0aa94 LZ |
1900 | if (!ecdt_ptr->control.address || !ecdt_ptr->data.address) { |
1901 | /* | |
1902 | * Asus X50GL: | |
1903 | * https://bugzilla.kernel.org/show_bug.cgi?id=11880 | |
1904 | */ | |
fdb3c177 | 1905 | return; |
ed4b197d | 1906 | } |
478fa03b | 1907 | |
fdb3c177 RW |
1908 | ec = acpi_ec_alloc(); |
1909 | if (!ec) | |
1910 | return; | |
1911 | ||
59f0aa94 | 1912 | if (EC_FLAGS_CORRECT_ECDT) { |
fd6231e7 LZ |
1913 | ec->command_addr = ecdt_ptr->data.address; |
1914 | ec->data_addr = ecdt_ptr->control.address; | |
c6cb0e87 | 1915 | } else { |
fd6231e7 LZ |
1916 | ec->command_addr = ecdt_ptr->control.address; |
1917 | ec->data_addr = ecdt_ptr->data.address; | |
c6cb0e87 | 1918 | } |
fd6231e7 | 1919 | ec->gpe = ecdt_ptr->gpe; |
1568426c | 1920 | ec->handle = ACPI_ROOT_OBJECT; |
2a570840 LZ |
1921 | |
1922 | /* | |
1923 | * At this point, the namespace is not initialized, so do not find | |
1924 | * the namespace objects, or handle the events. | |
1925 | */ | |
1568426c RW |
1926 | ret = acpi_ec_setup(ec, false); |
1927 | if (ret) { | |
72c77b7e | 1928 | acpi_ec_free(ec); |
1568426c RW |
1929 | return; |
1930 | } | |
1931 | ||
1932 | boot_ec = ec; | |
1933 | boot_ec_is_ecdt = true; | |
1934 | ||
1935 | pr_info("Boot ECDT EC used to handle transactions\n"); | |
1da177e4 LT |
1936 | } |
1937 | ||
df45db61 | 1938 | #ifdef CONFIG_PM_SLEEP |
39a2a2aa LZ |
1939 | static int acpi_ec_suspend(struct device *dev) |
1940 | { | |
1941 | struct acpi_ec *ec = | |
1942 | acpi_driver_data(to_acpi_device(dev)); | |
1943 | ||
8110dd28 | 1944 | if (acpi_sleep_no_ec_events() && ec_freeze_events) |
39a2a2aa LZ |
1945 | acpi_ec_disable_event(ec); |
1946 | return 0; | |
1947 | } | |
1948 | ||
76380636 RW |
1949 | static int acpi_ec_suspend_noirq(struct device *dev) |
1950 | { | |
1951 | struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev)); | |
1952 | ||
1953 | /* | |
1954 | * The SCI handler doesn't run at this point, so the GPE can be | |
1955 | * masked at the low level without side effects. | |
1956 | */ | |
1957 | if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) && | |
1958 | ec->reference_count >= 1) | |
1959 | acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE); | |
1960 | ||
3cd091a7 RW |
1961 | if (acpi_sleep_no_ec_events()) |
1962 | acpi_ec_enter_noirq(ec); | |
1963 | ||
76380636 RW |
1964 | return 0; |
1965 | } | |
1966 | ||
1967 | static int acpi_ec_resume_noirq(struct device *dev) | |
1968 | { | |
1969 | struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev)); | |
1970 | ||
3cd091a7 RW |
1971 | if (acpi_sleep_no_ec_events()) |
1972 | acpi_ec_leave_noirq(ec); | |
1973 | ||
76380636 RW |
1974 | if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) && |
1975 | ec->reference_count >= 1) | |
1976 | acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE); | |
1977 | ||
1978 | return 0; | |
1979 | } | |
1980 | ||
c2b46d67 LZ |
1981 | static int acpi_ec_resume(struct device *dev) |
1982 | { | |
1983 | struct acpi_ec *ec = | |
1984 | acpi_driver_data(to_acpi_device(dev)); | |
1985 | ||
1986 | acpi_ec_enable_event(ec); | |
1987 | return 0; | |
1988 | } | |
df45db61 LZ |
1989 | #endif |
1990 | ||
1991 | static const struct dev_pm_ops acpi_ec_pm = { | |
76380636 | 1992 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend_noirq, acpi_ec_resume_noirq) |
39a2a2aa | 1993 | SET_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend, acpi_ec_resume) |
df45db61 LZ |
1994 | }; |
1995 | ||
e4dca7b7 KC |
1996 | static int param_set_event_clearing(const char *val, |
1997 | const struct kernel_param *kp) | |
1d68d261 LZ |
1998 | { |
1999 | int result = 0; | |
2000 | ||
2001 | if (!strncmp(val, "status", sizeof("status") - 1)) { | |
2002 | ec_event_clearing = ACPI_EC_EVT_TIMING_STATUS; | |
2003 | pr_info("Assuming SCI_EVT clearing on EC_SC accesses\n"); | |
2004 | } else if (!strncmp(val, "query", sizeof("query") - 1)) { | |
2005 | ec_event_clearing = ACPI_EC_EVT_TIMING_QUERY; | |
2006 | pr_info("Assuming SCI_EVT clearing on QR_EC writes\n"); | |
2007 | } else if (!strncmp(val, "event", sizeof("event") - 1)) { | |
2008 | ec_event_clearing = ACPI_EC_EVT_TIMING_EVENT; | |
2009 | pr_info("Assuming SCI_EVT clearing on event reads\n"); | |
2010 | } else | |
2011 | result = -EINVAL; | |
2012 | return result; | |
2013 | } | |
2014 | ||
e4dca7b7 KC |
2015 | static int param_get_event_clearing(char *buffer, |
2016 | const struct kernel_param *kp) | |
1d68d261 LZ |
2017 | { |
2018 | switch (ec_event_clearing) { | |
2019 | case ACPI_EC_EVT_TIMING_STATUS: | |
2020 | return sprintf(buffer, "status"); | |
2021 | case ACPI_EC_EVT_TIMING_QUERY: | |
2022 | return sprintf(buffer, "query"); | |
2023 | case ACPI_EC_EVT_TIMING_EVENT: | |
2024 | return sprintf(buffer, "event"); | |
2025 | default: | |
2026 | return sprintf(buffer, "invalid"); | |
2027 | } | |
2028 | return 0; | |
2029 | } | |
2030 | ||
2031 | module_param_call(ec_event_clearing, param_set_event_clearing, param_get_event_clearing, | |
2032 | NULL, 0644); | |
2033 | MODULE_PARM_DESC(ec_event_clearing, "Assumed SCI_EVT clearing timing"); | |
2034 | ||
223883b7 AS |
2035 | static struct acpi_driver acpi_ec_driver = { |
2036 | .name = "ec", | |
2037 | .class = ACPI_EC_CLASS, | |
2038 | .ids = ec_device_ids, | |
2039 | .ops = { | |
2040 | .add = acpi_ec_add, | |
2041 | .remove = acpi_ec_remove, | |
223883b7 | 2042 | }, |
df45db61 | 2043 | .drv.pm = &acpi_ec_pm, |
223883b7 AS |
2044 | }; |
2045 | ||
e1191bd4 LZ |
2046 | static inline int acpi_ec_query_init(void) |
2047 | { | |
2048 | if (!ec_query_wq) { | |
2049 | ec_query_wq = alloc_workqueue("kec_query", 0, | |
2050 | ec_max_queries); | |
2051 | if (!ec_query_wq) | |
2052 | return -ENODEV; | |
2053 | } | |
2054 | return 0; | |
2055 | } | |
2056 | ||
2057 | static inline void acpi_ec_query_exit(void) | |
2058 | { | |
2059 | if (ec_query_wq) { | |
2060 | destroy_workqueue(ec_query_wq); | |
2061 | ec_query_wq = NULL; | |
2062 | } | |
2063 | } | |
2064 | ||
8195a655 MW |
2065 | static const struct dmi_system_id acpi_ec_no_wakeup[] = { |
2066 | { | |
2067 | .ident = "Thinkpad X1 Carbon 6th", | |
2068 | .matches = { | |
2069 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), | |
2c4d6baf | 2070 | DMI_MATCH(DMI_PRODUCT_FAMILY, "Thinkpad X1 Carbon 6th"), |
8195a655 MW |
2071 | }, |
2072 | }, | |
4c3be61e MW |
2073 | { |
2074 | .ident = "ThinkPad X1 Carbon 6th", | |
2075 | .matches = { | |
2076 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), | |
2077 | DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Carbon 6th"), | |
2078 | }, | |
2079 | }, | |
b047c62e AM |
2080 | { |
2081 | .ident = "ThinkPad X1 Yoga 3rd", | |
2082 | .matches = { | |
2083 | DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), | |
2084 | DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Yoga 3rd"), | |
2085 | }, | |
2086 | }, | |
8195a655 MW |
2087 | { }, |
2088 | }; | |
2089 | ||
a5f820fe | 2090 | int __init acpi_ec_init(void) |
1da177e4 | 2091 | { |
e1191bd4 | 2092 | int result; |
98529b92 | 2093 | int ecdt_fail, dsdt_fail; |
1da177e4 | 2094 | |
e1191bd4 LZ |
2095 | /* register workqueue for _Qxx evaluations */ |
2096 | result = acpi_ec_query_init(); | |
2097 | if (result) | |
98529b92 | 2098 | return result; |
1da177e4 | 2099 | |
8195a655 MW |
2100 | /* |
2101 | * Disable EC wakeup on following systems to prevent periodic | |
2102 | * wakeup from EC GPE. | |
2103 | */ | |
2104 | if (dmi_check_system(acpi_ec_no_wakeup)) { | |
2105 | ec_no_wakeup = true; | |
2106 | pr_debug("Disabling EC wakeup on suspend-to-idle\n"); | |
2107 | } | |
2108 | ||
98529b92 | 2109 | /* Drivers must be started after acpi_ec_query_init() */ |
98529b92 | 2110 | dsdt_fail = acpi_bus_register_driver(&acpi_ec_driver); |
a64a62ce LZ |
2111 | /* |
2112 | * Register ECDT to ACPI bus only when PNP0C09 probe fails. This is | |
2113 | * useful for platforms (confirmed on ASUS X550ZE) with valid ECDT | |
2114 | * settings but invalid DSDT settings. | |
2115 | * https://bugzilla.kernel.org/show_bug.cgi?id=196847 | |
2116 | */ | |
69b957c2 | 2117 | ecdt_fail = acpi_ec_ecdt_start(); |
98529b92 | 2118 | return ecdt_fail && dsdt_fail ? -ENODEV : 0; |
1da177e4 LT |
2119 | } |
2120 | ||
1da177e4 LT |
2121 | /* EC driver currently not unloadable */ |
2122 | #if 0 | |
50526df6 | 2123 | static void __exit acpi_ec_exit(void) |
1da177e4 | 2124 | { |
1da177e4 LT |
2125 | |
2126 | acpi_bus_unregister_driver(&acpi_ec_driver); | |
e1191bd4 | 2127 | acpi_ec_query_exit(); |
1da177e4 | 2128 | } |
7843932a | 2129 | #endif /* 0 */ |