ec.c 29 KB

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