ec.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /*
  2. * ec.c - ACPI Embedded Controller Driver (v2.2)
  3. *
  4. * Copyright (C) 2001-2014 Intel Corporation
  5. * Author: 2014 Lv Zheng <lv.zheng@intel.com>
  6. * 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
  7. * 2006 Denis Sadykov <denis.m.sadykov@intel.com>
  8. * 2004 Luming Yu <luming.yu@intel.com>
  9. * 2001, 2002 Andy Grover <andrew.grover@intel.com>
  10. * 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  11. * Copyright (C) 2008 Alexey Starikovskiy <astarikovskiy@suse.de>
  12. *
  13. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or (at
  18. * your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  28. *
  29. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  30. */
  31. /* Uncomment next line to get verbose printout */
  32. /* #define DEBUG */
  33. #define pr_fmt(fmt) "ACPI : EC: " fmt
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/types.h>
  38. #include <linux/delay.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/list.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/slab.h>
  43. #include <linux/acpi.h>
  44. #include <linux/dmi.h>
  45. #include <asm/io.h>
  46. #include "internal.h"
  47. #define ACPI_EC_CLASS "embedded_controller"
  48. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  49. #define ACPI_EC_FILE_INFO "info"
  50. /* EC status register */
  51. #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
  52. #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
  53. #define ACPI_EC_FLAG_CMD 0x08 /* Input buffer contains a command */
  54. #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
  55. #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
  56. /* EC commands */
  57. enum ec_command {
  58. ACPI_EC_COMMAND_READ = 0x80,
  59. ACPI_EC_COMMAND_WRITE = 0x81,
  60. ACPI_EC_BURST_ENABLE = 0x82,
  61. ACPI_EC_BURST_DISABLE = 0x83,
  62. ACPI_EC_COMMAND_QUERY = 0x84,
  63. };
  64. #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
  65. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  66. #define ACPI_EC_MSI_UDELAY 550 /* Wait 550us for MSI EC */
  67. #define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query
  68. * when trying to clear the EC */
  69. enum {
  70. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  71. EC_FLAGS_GPE_STORM, /* GPE storm detected */
  72. EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and
  73. * OpReg are installed */
  74. EC_FLAGS_BLOCKED, /* Transactions are blocked */
  75. };
  76. #define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */
  77. #define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */
  78. /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
  79. static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
  80. module_param(ec_delay, uint, 0644);
  81. MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
  82. /*
  83. * If the number of false interrupts per one transaction exceeds
  84. * this threshold, will think there is a GPE storm happened and
  85. * will disable the GPE for normal transaction.
  86. */
  87. static unsigned int ec_storm_threshold __read_mostly = 8;
  88. module_param(ec_storm_threshold, uint, 0644);
  89. MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
  90. struct acpi_ec_query_handler {
  91. struct list_head node;
  92. acpi_ec_query_func func;
  93. acpi_handle handle;
  94. void *data;
  95. u8 query_bit;
  96. };
  97. struct transaction {
  98. const u8 *wdata;
  99. u8 *rdata;
  100. unsigned short irq_count;
  101. u8 command;
  102. u8 wi;
  103. u8 ri;
  104. u8 wlen;
  105. u8 rlen;
  106. u8 flags;
  107. };
  108. struct acpi_ec *boot_ec, *first_ec;
  109. EXPORT_SYMBOL(first_ec);
  110. static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
  111. static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
  112. static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
  113. static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */
  114. /* --------------------------------------------------------------------------
  115. Transaction Management
  116. -------------------------------------------------------------------------- */
  117. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  118. {
  119. u8 x = inb(ec->command_addr);
  120. pr_debug("EC_SC(R) = 0x%2.2x "
  121. "SCI_EVT=%d BURST=%d CMD=%d IBF=%d OBF=%d\n",
  122. x,
  123. !!(x & ACPI_EC_FLAG_SCI),
  124. !!(x & ACPI_EC_FLAG_BURST),
  125. !!(x & ACPI_EC_FLAG_CMD),
  126. !!(x & ACPI_EC_FLAG_IBF),
  127. !!(x & ACPI_EC_FLAG_OBF));
  128. return x;
  129. }
  130. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  131. {
  132. u8 x = inb(ec->data_addr);
  133. pr_debug("EC_DATA(R) = 0x%2.2x\n", x);
  134. return x;
  135. }
  136. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  137. {
  138. pr_debug("EC_SC(W) = 0x%2.2x\n", command);
  139. outb(command, ec->command_addr);
  140. }
  141. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  142. {
  143. pr_debug("EC_DATA(W) = 0x%2.2x\n", data);
  144. outb(data, ec->data_addr);
  145. }
  146. static int ec_transaction_completed(struct acpi_ec *ec)
  147. {
  148. unsigned long flags;
  149. int ret = 0;
  150. spin_lock_irqsave(&ec->lock, flags);
  151. if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
  152. ret = 1;
  153. spin_unlock_irqrestore(&ec->lock, flags);
  154. return ret;
  155. }
  156. static bool advance_transaction(struct acpi_ec *ec)
  157. {
  158. struct transaction *t;
  159. u8 status;
  160. bool wakeup = false;
  161. pr_debug("===== %s =====\n", in_interrupt() ? "IRQ" : "TASK");
  162. status = acpi_ec_read_status(ec);
  163. t = ec->curr;
  164. if (!t)
  165. goto err;
  166. if (t->flags & ACPI_EC_COMMAND_POLL) {
  167. if (t->wlen > t->wi) {
  168. if ((status & ACPI_EC_FLAG_IBF) == 0)
  169. acpi_ec_write_data(ec, t->wdata[t->wi++]);
  170. else
  171. goto err;
  172. } else if (t->rlen > t->ri) {
  173. if ((status & ACPI_EC_FLAG_OBF) == 1) {
  174. t->rdata[t->ri++] = acpi_ec_read_data(ec);
  175. if (t->rlen == t->ri) {
  176. t->flags |= ACPI_EC_COMMAND_COMPLETE;
  177. wakeup = true;
  178. }
  179. } else
  180. goto err;
  181. } else if (t->wlen == t->wi &&
  182. (status & ACPI_EC_FLAG_IBF) == 0) {
  183. t->flags |= ACPI_EC_COMMAND_COMPLETE;
  184. wakeup = true;
  185. }
  186. return wakeup;
  187. } else {
  188. if ((status & ACPI_EC_FLAG_IBF) == 0) {
  189. acpi_ec_write_cmd(ec, t->command);
  190. t->flags |= ACPI_EC_COMMAND_POLL;
  191. } else
  192. goto err;
  193. return wakeup;
  194. }
  195. err:
  196. /*
  197. * If SCI bit is set, then don't think it's a false IRQ
  198. * otherwise will take a not handled IRQ as a false one.
  199. */
  200. if (!(status & ACPI_EC_FLAG_SCI)) {
  201. if (in_interrupt() && t)
  202. ++t->irq_count;
  203. }
  204. return wakeup;
  205. }
  206. static void start_transaction(struct acpi_ec *ec)
  207. {
  208. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  209. ec->curr->flags = 0;
  210. (void)advance_transaction(ec);
  211. }
  212. static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data);
  213. static int ec_check_sci_sync(struct acpi_ec *ec, u8 state)
  214. {
  215. if (state & ACPI_EC_FLAG_SCI) {
  216. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  217. return acpi_ec_sync_query(ec, NULL);
  218. }
  219. return 0;
  220. }
  221. static int ec_poll(struct acpi_ec *ec)
  222. {
  223. unsigned long flags;
  224. int repeat = 5; /* number of command restarts */
  225. while (repeat--) {
  226. unsigned long delay = jiffies +
  227. msecs_to_jiffies(ec_delay);
  228. do {
  229. /* don't sleep with disabled interrupts */
  230. if (EC_FLAGS_MSI || irqs_disabled()) {
  231. udelay(ACPI_EC_MSI_UDELAY);
  232. if (ec_transaction_completed(ec))
  233. return 0;
  234. } else {
  235. if (wait_event_timeout(ec->wait,
  236. ec_transaction_completed(ec),
  237. msecs_to_jiffies(1)))
  238. return 0;
  239. }
  240. spin_lock_irqsave(&ec->lock, flags);
  241. (void)advance_transaction(ec);
  242. spin_unlock_irqrestore(&ec->lock, flags);
  243. } while (time_before(jiffies, delay));
  244. pr_debug("controller reset, restart transaction\n");
  245. spin_lock_irqsave(&ec->lock, flags);
  246. start_transaction(ec);
  247. spin_unlock_irqrestore(&ec->lock, flags);
  248. }
  249. return -ETIME;
  250. }
  251. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  252. struct transaction *t)
  253. {
  254. unsigned long tmp;
  255. int ret = 0;
  256. if (EC_FLAGS_MSI)
  257. udelay(ACPI_EC_MSI_UDELAY);
  258. /* start transaction */
  259. spin_lock_irqsave(&ec->lock, tmp);
  260. /* following two actions should be kept atomic */
  261. ec->curr = t;
  262. start_transaction(ec);
  263. if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
  264. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  265. spin_unlock_irqrestore(&ec->lock, tmp);
  266. ret = ec_poll(ec);
  267. spin_lock_irqsave(&ec->lock, tmp);
  268. ec->curr = NULL;
  269. spin_unlock_irqrestore(&ec->lock, tmp);
  270. return ret;
  271. }
  272. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
  273. {
  274. int status;
  275. u32 glk;
  276. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  277. return -EINVAL;
  278. if (t->rdata)
  279. memset(t->rdata, 0, t->rlen);
  280. mutex_lock(&ec->mutex);
  281. if (test_bit(EC_FLAGS_BLOCKED, &ec->flags)) {
  282. status = -EINVAL;
  283. goto unlock;
  284. }
  285. if (ec->global_lock) {
  286. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  287. if (ACPI_FAILURE(status)) {
  288. status = -ENODEV;
  289. goto unlock;
  290. }
  291. }
  292. pr_debug("transaction start (cmd=0x%02x, addr=0x%02x)\n",
  293. t->command, t->wdata ? t->wdata[0] : 0);
  294. /* disable GPE during transaction if storm is detected */
  295. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  296. /* It has to be disabled, so that it doesn't trigger. */
  297. acpi_disable_gpe(NULL, ec->gpe);
  298. }
  299. status = acpi_ec_transaction_unlocked(ec, t);
  300. /* check if we received SCI during transaction */
  301. ec_check_sci_sync(ec, acpi_ec_read_status(ec));
  302. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  303. msleep(1);
  304. /* It is safe to enable the GPE outside of the transaction. */
  305. acpi_enable_gpe(NULL, ec->gpe);
  306. } else if (t->irq_count > ec_storm_threshold) {
  307. pr_info("GPE storm detected(%d GPEs), "
  308. "transactions will use polling mode\n",
  309. t->irq_count);
  310. set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
  311. }
  312. pr_debug("transaction end\n");
  313. if (ec->global_lock)
  314. acpi_release_global_lock(glk);
  315. unlock:
  316. mutex_unlock(&ec->mutex);
  317. return status;
  318. }
  319. static int acpi_ec_burst_enable(struct acpi_ec *ec)
  320. {
  321. u8 d;
  322. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  323. .wdata = NULL, .rdata = &d,
  324. .wlen = 0, .rlen = 1};
  325. return acpi_ec_transaction(ec, &t);
  326. }
  327. static int acpi_ec_burst_disable(struct acpi_ec *ec)
  328. {
  329. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  330. .wdata = NULL, .rdata = NULL,
  331. .wlen = 0, .rlen = 0};
  332. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  333. acpi_ec_transaction(ec, &t) : 0;
  334. }
  335. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
  336. {
  337. int result;
  338. u8 d;
  339. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  340. .wdata = &address, .rdata = &d,
  341. .wlen = 1, .rlen = 1};
  342. result = acpi_ec_transaction(ec, &t);
  343. *data = d;
  344. return result;
  345. }
  346. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  347. {
  348. u8 wdata[2] = { address, data };
  349. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  350. .wdata = wdata, .rdata = NULL,
  351. .wlen = 2, .rlen = 0};
  352. return acpi_ec_transaction(ec, &t);
  353. }
  354. int ec_read(u8 addr, u8 *val)
  355. {
  356. int err;
  357. u8 temp_data;
  358. if (!first_ec)
  359. return -ENODEV;
  360. err = acpi_ec_read(first_ec, addr, &temp_data);
  361. if (!err) {
  362. *val = temp_data;
  363. return 0;
  364. } else
  365. return err;
  366. }
  367. EXPORT_SYMBOL(ec_read);
  368. int ec_write(u8 addr, u8 val)
  369. {
  370. int err;
  371. if (!first_ec)
  372. return -ENODEV;
  373. err = acpi_ec_write(first_ec, addr, val);
  374. return err;
  375. }
  376. EXPORT_SYMBOL(ec_write);
  377. int ec_transaction(u8 command,
  378. const u8 * wdata, unsigned wdata_len,
  379. u8 * rdata, unsigned rdata_len)
  380. {
  381. struct transaction t = {.command = command,
  382. .wdata = wdata, .rdata = rdata,
  383. .wlen = wdata_len, .rlen = rdata_len};
  384. if (!first_ec)
  385. return -ENODEV;
  386. return acpi_ec_transaction(first_ec, &t);
  387. }
  388. EXPORT_SYMBOL(ec_transaction);
  389. /* Get the handle to the EC device */
  390. acpi_handle ec_get_handle(void)
  391. {
  392. if (!first_ec)
  393. return NULL;
  394. return first_ec->handle;
  395. }
  396. EXPORT_SYMBOL(ec_get_handle);
  397. /*
  398. * Process _Q events that might have accumulated in the EC.
  399. * Run with locked ec mutex.
  400. */
  401. static void acpi_ec_clear(struct acpi_ec *ec)
  402. {
  403. int i, status;
  404. u8 value = 0;
  405. for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
  406. status = acpi_ec_sync_query(ec, &value);
  407. if (status || !value)
  408. break;
  409. }
  410. if (unlikely(i == ACPI_EC_CLEAR_MAX))
  411. pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
  412. else
  413. pr_info("%d stale EC events cleared\n", i);
  414. }
  415. void acpi_ec_block_transactions(void)
  416. {
  417. struct acpi_ec *ec = first_ec;
  418. if (!ec)
  419. return;
  420. mutex_lock(&ec->mutex);
  421. /* Prevent transactions from being carried out */
  422. set_bit(EC_FLAGS_BLOCKED, &ec->flags);
  423. mutex_unlock(&ec->mutex);
  424. }
  425. void acpi_ec_unblock_transactions(void)
  426. {
  427. struct acpi_ec *ec = first_ec;
  428. if (!ec)
  429. return;
  430. mutex_lock(&ec->mutex);
  431. /* Allow transactions to be carried out again */
  432. clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
  433. if (EC_FLAGS_CLEAR_ON_RESUME)
  434. acpi_ec_clear(ec);
  435. mutex_unlock(&ec->mutex);
  436. }
  437. void acpi_ec_unblock_transactions_early(void)
  438. {
  439. /*
  440. * Allow transactions to happen again (this function is called from
  441. * atomic context during wakeup, so we don't need to acquire the mutex).
  442. */
  443. if (first_ec)
  444. clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags);
  445. }
  446. static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
  447. {
  448. int result;
  449. u8 d;
  450. struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
  451. .wdata = NULL, .rdata = &d,
  452. .wlen = 0, .rlen = 1};
  453. if (!ec || !data)
  454. return -EINVAL;
  455. /*
  456. * Query the EC to find out which _Qxx method we need to evaluate.
  457. * Note that successful completion of the query causes the ACPI_EC_SCI
  458. * bit to be cleared (and thus clearing the interrupt source).
  459. */
  460. result = acpi_ec_transaction_unlocked(ec, &t);
  461. if (result)
  462. return result;
  463. if (!d)
  464. return -ENODATA;
  465. *data = d;
  466. return 0;
  467. }
  468. /* --------------------------------------------------------------------------
  469. Event Management
  470. -------------------------------------------------------------------------- */
  471. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  472. acpi_handle handle, acpi_ec_query_func func,
  473. void *data)
  474. {
  475. struct acpi_ec_query_handler *handler =
  476. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  477. if (!handler)
  478. return -ENOMEM;
  479. handler->query_bit = query_bit;
  480. handler->handle = handle;
  481. handler->func = func;
  482. handler->data = data;
  483. mutex_lock(&ec->mutex);
  484. list_add(&handler->node, &ec->list);
  485. mutex_unlock(&ec->mutex);
  486. return 0;
  487. }
  488. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  489. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  490. {
  491. struct acpi_ec_query_handler *handler, *tmp;
  492. mutex_lock(&ec->mutex);
  493. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  494. if (query_bit == handler->query_bit) {
  495. list_del(&handler->node);
  496. kfree(handler);
  497. }
  498. }
  499. mutex_unlock(&ec->mutex);
  500. }
  501. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  502. static void acpi_ec_run(void *cxt)
  503. {
  504. struct acpi_ec_query_handler *handler = cxt;
  505. if (!handler)
  506. return;
  507. pr_debug("start query execution\n");
  508. if (handler->func)
  509. handler->func(handler->data);
  510. else if (handler->handle)
  511. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  512. pr_debug("stop query execution\n");
  513. kfree(handler);
  514. }
  515. static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data)
  516. {
  517. u8 value = 0;
  518. int status;
  519. struct acpi_ec_query_handler *handler, *copy;
  520. status = acpi_ec_query_unlocked(ec, &value);
  521. if (data)
  522. *data = value;
  523. if (status)
  524. return status;
  525. list_for_each_entry(handler, &ec->list, node) {
  526. if (value == handler->query_bit) {
  527. /* have custom handler for this bit */
  528. copy = kmalloc(sizeof(*handler), GFP_KERNEL);
  529. if (!copy)
  530. return -ENOMEM;
  531. memcpy(copy, handler, sizeof(*copy));
  532. pr_debug("push query execution (0x%2x) on queue\n",
  533. value);
  534. return acpi_os_execute((copy->func) ?
  535. OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
  536. acpi_ec_run, copy);
  537. }
  538. }
  539. return 0;
  540. }
  541. static void acpi_ec_gpe_query(void *ec_cxt)
  542. {
  543. struct acpi_ec *ec = ec_cxt;
  544. if (!ec)
  545. return;
  546. mutex_lock(&ec->mutex);
  547. acpi_ec_sync_query(ec, NULL);
  548. mutex_unlock(&ec->mutex);
  549. }
  550. static int ec_check_sci(struct acpi_ec *ec, u8 state)
  551. {
  552. if (state & ACPI_EC_FLAG_SCI) {
  553. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  554. pr_debug("push gpe query to the queue\n");
  555. return acpi_os_execute(OSL_NOTIFY_HANDLER,
  556. acpi_ec_gpe_query, ec);
  557. }
  558. }
  559. return 0;
  560. }
  561. static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
  562. u32 gpe_number, void *data)
  563. {
  564. unsigned long flags;
  565. struct acpi_ec *ec = data;
  566. spin_lock_irqsave(&ec->lock, flags);
  567. if (advance_transaction(ec))
  568. wake_up(&ec->wait);
  569. spin_unlock_irqrestore(&ec->lock, flags);
  570. ec_check_sci(ec, acpi_ec_read_status(ec));
  571. return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
  572. }
  573. /* --------------------------------------------------------------------------
  574. Address Space Management
  575. -------------------------------------------------------------------------- */
  576. static acpi_status
  577. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  578. u32 bits, u64 *value64,
  579. void *handler_context, void *region_context)
  580. {
  581. struct acpi_ec *ec = handler_context;
  582. int result = 0, i, bytes = bits / 8;
  583. u8 *value = (u8 *)value64;
  584. if ((address > 0xFF) || !value || !handler_context)
  585. return AE_BAD_PARAMETER;
  586. if (function != ACPI_READ && function != ACPI_WRITE)
  587. return AE_BAD_PARAMETER;
  588. if (EC_FLAGS_MSI || bits > 8)
  589. acpi_ec_burst_enable(ec);
  590. for (i = 0; i < bytes; ++i, ++address, ++value)
  591. result = (function == ACPI_READ) ?
  592. acpi_ec_read(ec, address, value) :
  593. acpi_ec_write(ec, address, *value);
  594. if (EC_FLAGS_MSI || bits > 8)
  595. acpi_ec_burst_disable(ec);
  596. switch (result) {
  597. case -EINVAL:
  598. return AE_BAD_PARAMETER;
  599. break;
  600. case -ENODEV:
  601. return AE_NOT_FOUND;
  602. break;
  603. case -ETIME:
  604. return AE_TIME;
  605. break;
  606. default:
  607. return AE_OK;
  608. }
  609. }
  610. /* --------------------------------------------------------------------------
  611. Driver Interface
  612. -------------------------------------------------------------------------- */
  613. static acpi_status
  614. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  615. static struct acpi_ec *make_acpi_ec(void)
  616. {
  617. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  618. if (!ec)
  619. return NULL;
  620. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  621. mutex_init(&ec->mutex);
  622. init_waitqueue_head(&ec->wait);
  623. INIT_LIST_HEAD(&ec->list);
  624. spin_lock_init(&ec->lock);
  625. return ec;
  626. }
  627. static acpi_status
  628. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  629. void *context, void **return_value)
  630. {
  631. char node_name[5];
  632. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  633. struct acpi_ec *ec = context;
  634. int value = 0;
  635. acpi_status status;
  636. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  637. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
  638. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  639. }
  640. return AE_OK;
  641. }
  642. static acpi_status
  643. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  644. {
  645. acpi_status status;
  646. unsigned long long tmp = 0;
  647. struct acpi_ec *ec = context;
  648. /* clear addr values, ec_parse_io_ports depend on it */
  649. ec->command_addr = ec->data_addr = 0;
  650. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  651. ec_parse_io_ports, ec);
  652. if (ACPI_FAILURE(status))
  653. return status;
  654. /* Get GPE bit assignment (EC events). */
  655. /* TODO: Add support for _GPE returning a package */
  656. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  657. if (ACPI_FAILURE(status))
  658. return status;
  659. ec->gpe = tmp;
  660. /* Use the global lock for all EC transactions? */
  661. tmp = 0;
  662. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  663. ec->global_lock = tmp;
  664. ec->handle = handle;
  665. return AE_CTRL_TERMINATE;
  666. }
  667. static int ec_install_handlers(struct acpi_ec *ec)
  668. {
  669. acpi_status status;
  670. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  671. return 0;
  672. status = acpi_install_gpe_handler(NULL, ec->gpe,
  673. ACPI_GPE_EDGE_TRIGGERED,
  674. &acpi_ec_gpe_handler, ec);
  675. if (ACPI_FAILURE(status))
  676. return -ENODEV;
  677. acpi_enable_gpe(NULL, ec->gpe);
  678. status = acpi_install_address_space_handler(ec->handle,
  679. ACPI_ADR_SPACE_EC,
  680. &acpi_ec_space_handler,
  681. NULL, ec);
  682. if (ACPI_FAILURE(status)) {
  683. if (status == AE_NOT_FOUND) {
  684. /*
  685. * Maybe OS fails in evaluating the _REG object.
  686. * The AE_NOT_FOUND error will be ignored and OS
  687. * continue to initialize EC.
  688. */
  689. pr_err("Fail in evaluating the _REG object"
  690. " of EC device. Broken bios is suspected.\n");
  691. } else {
  692. acpi_disable_gpe(NULL, ec->gpe);
  693. acpi_remove_gpe_handler(NULL, ec->gpe,
  694. &acpi_ec_gpe_handler);
  695. return -ENODEV;
  696. }
  697. }
  698. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  699. return 0;
  700. }
  701. static void ec_remove_handlers(struct acpi_ec *ec)
  702. {
  703. acpi_disable_gpe(NULL, ec->gpe);
  704. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  705. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  706. pr_err("failed to remove space handler\n");
  707. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  708. &acpi_ec_gpe_handler)))
  709. pr_err("failed to remove gpe handler\n");
  710. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  711. }
  712. static int acpi_ec_add(struct acpi_device *device)
  713. {
  714. struct acpi_ec *ec = NULL;
  715. int ret;
  716. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  717. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  718. /* Check for boot EC */
  719. if (boot_ec &&
  720. (boot_ec->handle == device->handle ||
  721. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  722. ec = boot_ec;
  723. boot_ec = NULL;
  724. } else {
  725. ec = make_acpi_ec();
  726. if (!ec)
  727. return -ENOMEM;
  728. }
  729. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  730. AE_CTRL_TERMINATE) {
  731. kfree(ec);
  732. return -EINVAL;
  733. }
  734. /* Find and register all query methods */
  735. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  736. acpi_ec_register_query_methods, NULL, ec, NULL);
  737. if (!first_ec)
  738. first_ec = ec;
  739. device->driver_data = ec;
  740. ret = !!request_region(ec->data_addr, 1, "EC data");
  741. WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
  742. ret = !!request_region(ec->command_addr, 1, "EC cmd");
  743. WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
  744. pr_info("GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  745. ec->gpe, ec->command_addr, ec->data_addr);
  746. ret = ec_install_handlers(ec);
  747. /* EC is fully operational, allow queries */
  748. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  749. /* Clear stale _Q events if hardware might require that */
  750. if (EC_FLAGS_CLEAR_ON_RESUME) {
  751. mutex_lock(&ec->mutex);
  752. acpi_ec_clear(ec);
  753. mutex_unlock(&ec->mutex);
  754. }
  755. return ret;
  756. }
  757. static int acpi_ec_remove(struct acpi_device *device)
  758. {
  759. struct acpi_ec *ec;
  760. struct acpi_ec_query_handler *handler, *tmp;
  761. if (!device)
  762. return -EINVAL;
  763. ec = acpi_driver_data(device);
  764. ec_remove_handlers(ec);
  765. mutex_lock(&ec->mutex);
  766. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  767. list_del(&handler->node);
  768. kfree(handler);
  769. }
  770. mutex_unlock(&ec->mutex);
  771. release_region(ec->data_addr, 1);
  772. release_region(ec->command_addr, 1);
  773. device->driver_data = NULL;
  774. if (ec == first_ec)
  775. first_ec = NULL;
  776. kfree(ec);
  777. return 0;
  778. }
  779. static acpi_status
  780. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  781. {
  782. struct acpi_ec *ec = context;
  783. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  784. return AE_OK;
  785. /*
  786. * The first address region returned is the data port, and
  787. * the second address region returned is the status/command
  788. * port.
  789. */
  790. if (ec->data_addr == 0)
  791. ec->data_addr = resource->data.io.minimum;
  792. else if (ec->command_addr == 0)
  793. ec->command_addr = resource->data.io.minimum;
  794. else
  795. return AE_CTRL_TERMINATE;
  796. return AE_OK;
  797. }
  798. int __init acpi_boot_ec_enable(void)
  799. {
  800. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  801. return 0;
  802. if (!ec_install_handlers(boot_ec)) {
  803. first_ec = boot_ec;
  804. return 0;
  805. }
  806. return -EFAULT;
  807. }
  808. static const struct acpi_device_id ec_device_ids[] = {
  809. {"PNP0C09", 0},
  810. {"", 0},
  811. };
  812. /* Some BIOS do not survive early DSDT scan, skip it */
  813. static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
  814. {
  815. EC_FLAGS_SKIP_DSDT_SCAN = 1;
  816. return 0;
  817. }
  818. /* ASUStek often supplies us with broken ECDT, validate it */
  819. static int ec_validate_ecdt(const struct dmi_system_id *id)
  820. {
  821. EC_FLAGS_VALIDATE_ECDT = 1;
  822. return 0;
  823. }
  824. /* MSI EC needs special treatment, enable it */
  825. static int ec_flag_msi(const struct dmi_system_id *id)
  826. {
  827. pr_debug("Detected MSI hardware, enabling workarounds.\n");
  828. EC_FLAGS_MSI = 1;
  829. EC_FLAGS_VALIDATE_ECDT = 1;
  830. return 0;
  831. }
  832. /*
  833. * Clevo M720 notebook actually works ok with IRQ mode, if we lifted
  834. * the GPE storm threshold back to 20
  835. */
  836. static int ec_enlarge_storm_threshold(const struct dmi_system_id *id)
  837. {
  838. pr_debug("Setting the EC GPE storm threshold to 20\n");
  839. ec_storm_threshold = 20;
  840. return 0;
  841. }
  842. /*
  843. * On some hardware it is necessary to clear events accumulated by the EC during
  844. * sleep. These ECs stop reporting GPEs until they are manually polled, if too
  845. * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
  846. *
  847. * https://bugzilla.kernel.org/show_bug.cgi?id=44161
  848. *
  849. * Ideally, the EC should also be instructed NOT to accumulate events during
  850. * sleep (which Windows seems to do somehow), but the interface to control this
  851. * behaviour is not known at this time.
  852. *
  853. * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
  854. * however it is very likely that other Samsung models are affected.
  855. *
  856. * On systems which don't accumulate _Q events during sleep, this extra check
  857. * should be harmless.
  858. */
  859. static int ec_clear_on_resume(const struct dmi_system_id *id)
  860. {
  861. pr_debug("Detected system needing EC poll on resume.\n");
  862. EC_FLAGS_CLEAR_ON_RESUME = 1;
  863. return 0;
  864. }
  865. static struct dmi_system_id ec_dmi_table[] __initdata = {
  866. {
  867. ec_skip_dsdt_scan, "Compal JFL92", {
  868. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  869. DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
  870. {
  871. ec_flag_msi, "MSI hardware", {
  872. DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
  873. {
  874. ec_flag_msi, "MSI hardware", {
  875. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
  876. {
  877. ec_flag_msi, "MSI hardware", {
  878. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
  879. {
  880. ec_flag_msi, "MSI hardware", {
  881. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
  882. {
  883. ec_flag_msi, "Quanta hardware", {
  884. DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
  885. DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
  886. {
  887. ec_flag_msi, "Quanta hardware", {
  888. DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
  889. DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
  890. {
  891. ec_validate_ecdt, "ASUS hardware", {
  892. DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
  893. {
  894. ec_validate_ecdt, "ASUS hardware", {
  895. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
  896. {
  897. ec_enlarge_storm_threshold, "CLEVO hardware", {
  898. DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
  899. DMI_MATCH(DMI_PRODUCT_NAME, "M720T/M730T"),}, NULL},
  900. {
  901. ec_skip_dsdt_scan, "HP Folio 13", {
  902. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  903. DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13"),}, NULL},
  904. {
  905. ec_validate_ecdt, "ASUS hardware", {
  906. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."),
  907. DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL},
  908. {
  909. ec_clear_on_resume, "Samsung hardware", {
  910. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
  911. {},
  912. };
  913. int __init acpi_ec_ecdt_probe(void)
  914. {
  915. acpi_status status;
  916. struct acpi_ec *saved_ec = NULL;
  917. struct acpi_table_ecdt *ecdt_ptr;
  918. boot_ec = make_acpi_ec();
  919. if (!boot_ec)
  920. return -ENOMEM;
  921. /*
  922. * Generate a boot ec context
  923. */
  924. dmi_check_system(ec_dmi_table);
  925. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  926. (struct acpi_table_header **)&ecdt_ptr);
  927. if (ACPI_SUCCESS(status)) {
  928. pr_info("EC description table is found, configuring boot EC\n");
  929. boot_ec->command_addr = ecdt_ptr->control.address;
  930. boot_ec->data_addr = ecdt_ptr->data.address;
  931. boot_ec->gpe = ecdt_ptr->gpe;
  932. boot_ec->handle = ACPI_ROOT_OBJECT;
  933. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  934. /* Don't trust ECDT, which comes from ASUSTek */
  935. if (!EC_FLAGS_VALIDATE_ECDT)
  936. goto install;
  937. saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
  938. if (!saved_ec)
  939. return -ENOMEM;
  940. /* fall through */
  941. }
  942. if (EC_FLAGS_SKIP_DSDT_SCAN) {
  943. kfree(saved_ec);
  944. return -ENODEV;
  945. }
  946. /* This workaround is needed only on some broken machines,
  947. * which require early EC, but fail to provide ECDT */
  948. pr_debug("Look up EC in DSDT\n");
  949. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  950. boot_ec, NULL);
  951. /* Check that acpi_get_devices actually find something */
  952. if (ACPI_FAILURE(status) || !boot_ec->handle)
  953. goto error;
  954. if (saved_ec) {
  955. /* try to find good ECDT from ASUSTek */
  956. if (saved_ec->command_addr != boot_ec->command_addr ||
  957. saved_ec->data_addr != boot_ec->data_addr ||
  958. saved_ec->gpe != boot_ec->gpe ||
  959. saved_ec->handle != boot_ec->handle)
  960. pr_info("ASUSTek keeps feeding us with broken "
  961. "ECDT tables, which are very hard to workaround. "
  962. "Trying to use DSDT EC info instead. Please send "
  963. "output of acpidump to linux-acpi@vger.kernel.org\n");
  964. kfree(saved_ec);
  965. saved_ec = NULL;
  966. } else {
  967. /* We really need to limit this workaround, the only ASUS,
  968. * which needs it, has fake EC._INI method, so use it as flag.
  969. * Keep boot_ec struct as it will be needed soon.
  970. */
  971. if (!dmi_name_in_vendors("ASUS") ||
  972. !acpi_has_method(boot_ec->handle, "_INI"))
  973. return -ENODEV;
  974. }
  975. install:
  976. if (!ec_install_handlers(boot_ec)) {
  977. first_ec = boot_ec;
  978. return 0;
  979. }
  980. error:
  981. kfree(boot_ec);
  982. kfree(saved_ec);
  983. boot_ec = NULL;
  984. return -ENODEV;
  985. }
  986. static struct acpi_driver acpi_ec_driver = {
  987. .name = "ec",
  988. .class = ACPI_EC_CLASS,
  989. .ids = ec_device_ids,
  990. .ops = {
  991. .add = acpi_ec_add,
  992. .remove = acpi_ec_remove,
  993. },
  994. };
  995. int __init acpi_ec_init(void)
  996. {
  997. int result = 0;
  998. /* Now register the driver for the EC */
  999. result = acpi_bus_register_driver(&acpi_ec_driver);
  1000. if (result < 0)
  1001. return -ENODEV;
  1002. return result;
  1003. }
  1004. /* EC driver currently not unloadable */
  1005. #if 0
  1006. static void __exit acpi_ec_exit(void)
  1007. {
  1008. acpi_bus_unregister_driver(&acpi_ec_driver);
  1009. return;
  1010. }
  1011. #endif /* 0 */