ec.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. if (t->command == ACPI_EC_COMMAND_QUERY)
  178. pr_debug("hardware QR_EC completion\n");
  179. wakeup = true;
  180. }
  181. } else
  182. goto err;
  183. } else if (t->wlen == t->wi &&
  184. (status & ACPI_EC_FLAG_IBF) == 0) {
  185. t->flags |= ACPI_EC_COMMAND_COMPLETE;
  186. wakeup = true;
  187. }
  188. return wakeup;
  189. } else {
  190. /*
  191. * There is firmware refusing to respond QR_EC when SCI_EVT
  192. * is not set, for which case, we complete the QR_EC
  193. * without issuing it to the firmware.
  194. * https://bugzilla.kernel.org/show_bug.cgi?id=86211
  195. */
  196. if (!(status & ACPI_EC_FLAG_SCI) &&
  197. (t->command == ACPI_EC_COMMAND_QUERY)) {
  198. t->flags |= ACPI_EC_COMMAND_POLL;
  199. t->rdata[t->ri++] = 0x00;
  200. t->flags |= ACPI_EC_COMMAND_COMPLETE;
  201. pr_debug("software QR_EC completion\n");
  202. wakeup = true;
  203. } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
  204. acpi_ec_write_cmd(ec, t->command);
  205. t->flags |= ACPI_EC_COMMAND_POLL;
  206. } else
  207. goto err;
  208. return wakeup;
  209. }
  210. err:
  211. /*
  212. * If SCI bit is set, then don't think it's a false IRQ
  213. * otherwise will take a not handled IRQ as a false one.
  214. */
  215. if (!(status & ACPI_EC_FLAG_SCI)) {
  216. if (in_interrupt() && t)
  217. ++t->irq_count;
  218. }
  219. return wakeup;
  220. }
  221. static void start_transaction(struct acpi_ec *ec)
  222. {
  223. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  224. ec->curr->flags = 0;
  225. (void)advance_transaction(ec);
  226. }
  227. static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data);
  228. static int ec_check_sci_sync(struct acpi_ec *ec, u8 state)
  229. {
  230. if (state & ACPI_EC_FLAG_SCI) {
  231. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  232. return acpi_ec_sync_query(ec, NULL);
  233. }
  234. return 0;
  235. }
  236. static int ec_poll(struct acpi_ec *ec)
  237. {
  238. unsigned long flags;
  239. int repeat = 5; /* number of command restarts */
  240. while (repeat--) {
  241. unsigned long delay = jiffies +
  242. msecs_to_jiffies(ec_delay);
  243. do {
  244. /* don't sleep with disabled interrupts */
  245. if (EC_FLAGS_MSI || irqs_disabled()) {
  246. udelay(ACPI_EC_MSI_UDELAY);
  247. if (ec_transaction_completed(ec))
  248. return 0;
  249. } else {
  250. if (wait_event_timeout(ec->wait,
  251. ec_transaction_completed(ec),
  252. msecs_to_jiffies(1)))
  253. return 0;
  254. }
  255. spin_lock_irqsave(&ec->lock, flags);
  256. (void)advance_transaction(ec);
  257. spin_unlock_irqrestore(&ec->lock, flags);
  258. } while (time_before(jiffies, delay));
  259. pr_debug("controller reset, restart transaction\n");
  260. spin_lock_irqsave(&ec->lock, flags);
  261. start_transaction(ec);
  262. spin_unlock_irqrestore(&ec->lock, flags);
  263. }
  264. return -ETIME;
  265. }
  266. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  267. struct transaction *t)
  268. {
  269. unsigned long tmp;
  270. int ret = 0;
  271. if (EC_FLAGS_MSI)
  272. udelay(ACPI_EC_MSI_UDELAY);
  273. /* start transaction */
  274. spin_lock_irqsave(&ec->lock, tmp);
  275. /* following two actions should be kept atomic */
  276. ec->curr = t;
  277. start_transaction(ec);
  278. spin_unlock_irqrestore(&ec->lock, tmp);
  279. ret = ec_poll(ec);
  280. spin_lock_irqsave(&ec->lock, tmp);
  281. if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
  282. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  283. ec->curr = NULL;
  284. spin_unlock_irqrestore(&ec->lock, tmp);
  285. return ret;
  286. }
  287. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
  288. {
  289. int status;
  290. u32 glk;
  291. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  292. return -EINVAL;
  293. if (t->rdata)
  294. memset(t->rdata, 0, t->rlen);
  295. mutex_lock(&ec->mutex);
  296. if (test_bit(EC_FLAGS_BLOCKED, &ec->flags)) {
  297. status = -EINVAL;
  298. goto unlock;
  299. }
  300. if (ec->global_lock) {
  301. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  302. if (ACPI_FAILURE(status)) {
  303. status = -ENODEV;
  304. goto unlock;
  305. }
  306. }
  307. pr_debug("transaction start (cmd=0x%02x, addr=0x%02x)\n",
  308. t->command, t->wdata ? t->wdata[0] : 0);
  309. /* disable GPE during transaction if storm is detected */
  310. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  311. /* It has to be disabled, so that it doesn't trigger. */
  312. acpi_disable_gpe(NULL, ec->gpe);
  313. }
  314. status = acpi_ec_transaction_unlocked(ec, t);
  315. /* check if we received SCI during transaction */
  316. ec_check_sci_sync(ec, acpi_ec_read_status(ec));
  317. if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
  318. msleep(1);
  319. /* It is safe to enable the GPE outside of the transaction. */
  320. acpi_enable_gpe(NULL, ec->gpe);
  321. } else if (t->irq_count > ec_storm_threshold) {
  322. pr_info("GPE storm detected(%d GPEs), "
  323. "transactions will use polling mode\n",
  324. t->irq_count);
  325. set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
  326. }
  327. pr_debug("transaction end\n");
  328. if (ec->global_lock)
  329. acpi_release_global_lock(glk);
  330. unlock:
  331. mutex_unlock(&ec->mutex);
  332. return status;
  333. }
  334. static int acpi_ec_burst_enable(struct acpi_ec *ec)
  335. {
  336. u8 d;
  337. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  338. .wdata = NULL, .rdata = &d,
  339. .wlen = 0, .rlen = 1};
  340. return acpi_ec_transaction(ec, &t);
  341. }
  342. static int acpi_ec_burst_disable(struct acpi_ec *ec)
  343. {
  344. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  345. .wdata = NULL, .rdata = NULL,
  346. .wlen = 0, .rlen = 0};
  347. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  348. acpi_ec_transaction(ec, &t) : 0;
  349. }
  350. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
  351. {
  352. int result;
  353. u8 d;
  354. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  355. .wdata = &address, .rdata = &d,
  356. .wlen = 1, .rlen = 1};
  357. result = acpi_ec_transaction(ec, &t);
  358. *data = d;
  359. return result;
  360. }
  361. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  362. {
  363. u8 wdata[2] = { address, data };
  364. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  365. .wdata = wdata, .rdata = NULL,
  366. .wlen = 2, .rlen = 0};
  367. return acpi_ec_transaction(ec, &t);
  368. }
  369. int ec_read(u8 addr, u8 *val)
  370. {
  371. int err;
  372. u8 temp_data;
  373. if (!first_ec)
  374. return -ENODEV;
  375. err = acpi_ec_read(first_ec, addr, &temp_data);
  376. if (!err) {
  377. *val = temp_data;
  378. return 0;
  379. } else
  380. return err;
  381. }
  382. EXPORT_SYMBOL(ec_read);
  383. int ec_write(u8 addr, u8 val)
  384. {
  385. int err;
  386. if (!first_ec)
  387. return -ENODEV;
  388. err = acpi_ec_write(first_ec, addr, val);
  389. return err;
  390. }
  391. EXPORT_SYMBOL(ec_write);
  392. int ec_transaction(u8 command,
  393. const u8 * wdata, unsigned wdata_len,
  394. u8 * rdata, unsigned rdata_len)
  395. {
  396. struct transaction t = {.command = command,
  397. .wdata = wdata, .rdata = rdata,
  398. .wlen = wdata_len, .rlen = rdata_len};
  399. if (!first_ec)
  400. return -ENODEV;
  401. return acpi_ec_transaction(first_ec, &t);
  402. }
  403. EXPORT_SYMBOL(ec_transaction);
  404. /* Get the handle to the EC device */
  405. acpi_handle ec_get_handle(void)
  406. {
  407. if (!first_ec)
  408. return NULL;
  409. return first_ec->handle;
  410. }
  411. EXPORT_SYMBOL(ec_get_handle);
  412. /*
  413. * Process _Q events that might have accumulated in the EC.
  414. * Run with locked ec mutex.
  415. */
  416. static void acpi_ec_clear(struct acpi_ec *ec)
  417. {
  418. int i, status;
  419. u8 value = 0;
  420. for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
  421. status = acpi_ec_sync_query(ec, &value);
  422. if (status || !value)
  423. break;
  424. }
  425. if (unlikely(i == ACPI_EC_CLEAR_MAX))
  426. pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
  427. else
  428. pr_info("%d stale EC events cleared\n", i);
  429. }
  430. void acpi_ec_block_transactions(void)
  431. {
  432. struct acpi_ec *ec = first_ec;
  433. if (!ec)
  434. return;
  435. mutex_lock(&ec->mutex);
  436. /* Prevent transactions from being carried out */
  437. set_bit(EC_FLAGS_BLOCKED, &ec->flags);
  438. mutex_unlock(&ec->mutex);
  439. }
  440. void acpi_ec_unblock_transactions(void)
  441. {
  442. struct acpi_ec *ec = first_ec;
  443. if (!ec)
  444. return;
  445. mutex_lock(&ec->mutex);
  446. /* Allow transactions to be carried out again */
  447. clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
  448. if (EC_FLAGS_CLEAR_ON_RESUME)
  449. acpi_ec_clear(ec);
  450. mutex_unlock(&ec->mutex);
  451. }
  452. void acpi_ec_unblock_transactions_early(void)
  453. {
  454. /*
  455. * Allow transactions to happen again (this function is called from
  456. * atomic context during wakeup, so we don't need to acquire the mutex).
  457. */
  458. if (first_ec)
  459. clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags);
  460. }
  461. static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
  462. {
  463. int result;
  464. u8 d;
  465. struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
  466. .wdata = NULL, .rdata = &d,
  467. .wlen = 0, .rlen = 1};
  468. if (!ec || !data)
  469. return -EINVAL;
  470. /*
  471. * Query the EC to find out which _Qxx method we need to evaluate.
  472. * Note that successful completion of the query causes the ACPI_EC_SCI
  473. * bit to be cleared (and thus clearing the interrupt source).
  474. */
  475. result = acpi_ec_transaction_unlocked(ec, &t);
  476. if (result)
  477. return result;
  478. if (!d)
  479. return -ENODATA;
  480. *data = d;
  481. return 0;
  482. }
  483. /* --------------------------------------------------------------------------
  484. Event Management
  485. -------------------------------------------------------------------------- */
  486. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  487. acpi_handle handle, acpi_ec_query_func func,
  488. void *data)
  489. {
  490. struct acpi_ec_query_handler *handler =
  491. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  492. if (!handler)
  493. return -ENOMEM;
  494. handler->query_bit = query_bit;
  495. handler->handle = handle;
  496. handler->func = func;
  497. handler->data = data;
  498. mutex_lock(&ec->mutex);
  499. list_add(&handler->node, &ec->list);
  500. mutex_unlock(&ec->mutex);
  501. return 0;
  502. }
  503. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  504. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  505. {
  506. struct acpi_ec_query_handler *handler, *tmp;
  507. mutex_lock(&ec->mutex);
  508. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  509. if (query_bit == handler->query_bit) {
  510. list_del(&handler->node);
  511. kfree(handler);
  512. }
  513. }
  514. mutex_unlock(&ec->mutex);
  515. }
  516. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  517. static void acpi_ec_run(void *cxt)
  518. {
  519. struct acpi_ec_query_handler *handler = cxt;
  520. if (!handler)
  521. return;
  522. pr_debug("start query execution\n");
  523. if (handler->func)
  524. handler->func(handler->data);
  525. else if (handler->handle)
  526. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  527. pr_debug("stop query execution\n");
  528. kfree(handler);
  529. }
  530. static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data)
  531. {
  532. u8 value = 0;
  533. int status;
  534. struct acpi_ec_query_handler *handler, *copy;
  535. status = acpi_ec_query_unlocked(ec, &value);
  536. if (data)
  537. *data = value;
  538. if (status)
  539. return status;
  540. list_for_each_entry(handler, &ec->list, node) {
  541. if (value == handler->query_bit) {
  542. /* have custom handler for this bit */
  543. copy = kmalloc(sizeof(*handler), GFP_KERNEL);
  544. if (!copy)
  545. return -ENOMEM;
  546. memcpy(copy, handler, sizeof(*copy));
  547. pr_debug("push query execution (0x%2x) on queue\n",
  548. value);
  549. return acpi_os_execute((copy->func) ?
  550. OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
  551. acpi_ec_run, copy);
  552. }
  553. }
  554. return 0;
  555. }
  556. static void acpi_ec_gpe_query(void *ec_cxt)
  557. {
  558. struct acpi_ec *ec = ec_cxt;
  559. if (!ec)
  560. return;
  561. mutex_lock(&ec->mutex);
  562. acpi_ec_sync_query(ec, NULL);
  563. mutex_unlock(&ec->mutex);
  564. }
  565. static int ec_check_sci(struct acpi_ec *ec, u8 state)
  566. {
  567. if (state & ACPI_EC_FLAG_SCI) {
  568. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  569. pr_debug("push gpe query to the queue\n");
  570. return acpi_os_execute(OSL_NOTIFY_HANDLER,
  571. acpi_ec_gpe_query, ec);
  572. }
  573. }
  574. return 0;
  575. }
  576. static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
  577. u32 gpe_number, void *data)
  578. {
  579. unsigned long flags;
  580. struct acpi_ec *ec = data;
  581. spin_lock_irqsave(&ec->lock, flags);
  582. if (advance_transaction(ec))
  583. wake_up(&ec->wait);
  584. spin_unlock_irqrestore(&ec->lock, flags);
  585. ec_check_sci(ec, acpi_ec_read_status(ec));
  586. return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
  587. }
  588. /* --------------------------------------------------------------------------
  589. Address Space Management
  590. -------------------------------------------------------------------------- */
  591. static acpi_status
  592. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  593. u32 bits, u64 *value64,
  594. void *handler_context, void *region_context)
  595. {
  596. struct acpi_ec *ec = handler_context;
  597. int result = 0, i, bytes = bits / 8;
  598. u8 *value = (u8 *)value64;
  599. if ((address > 0xFF) || !value || !handler_context)
  600. return AE_BAD_PARAMETER;
  601. if (function != ACPI_READ && function != ACPI_WRITE)
  602. return AE_BAD_PARAMETER;
  603. if (EC_FLAGS_MSI || bits > 8)
  604. acpi_ec_burst_enable(ec);
  605. for (i = 0; i < bytes; ++i, ++address, ++value)
  606. result = (function == ACPI_READ) ?
  607. acpi_ec_read(ec, address, value) :
  608. acpi_ec_write(ec, address, *value);
  609. if (EC_FLAGS_MSI || bits > 8)
  610. acpi_ec_burst_disable(ec);
  611. switch (result) {
  612. case -EINVAL:
  613. return AE_BAD_PARAMETER;
  614. break;
  615. case -ENODEV:
  616. return AE_NOT_FOUND;
  617. break;
  618. case -ETIME:
  619. return AE_TIME;
  620. break;
  621. default:
  622. return AE_OK;
  623. }
  624. }
  625. /* --------------------------------------------------------------------------
  626. Driver Interface
  627. -------------------------------------------------------------------------- */
  628. static acpi_status
  629. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  630. static struct acpi_ec *make_acpi_ec(void)
  631. {
  632. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  633. if (!ec)
  634. return NULL;
  635. ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
  636. mutex_init(&ec->mutex);
  637. init_waitqueue_head(&ec->wait);
  638. INIT_LIST_HEAD(&ec->list);
  639. spin_lock_init(&ec->lock);
  640. return ec;
  641. }
  642. static acpi_status
  643. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  644. void *context, void **return_value)
  645. {
  646. char node_name[5];
  647. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  648. struct acpi_ec *ec = context;
  649. int value = 0;
  650. acpi_status status;
  651. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  652. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
  653. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  654. }
  655. return AE_OK;
  656. }
  657. static acpi_status
  658. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  659. {
  660. acpi_status status;
  661. unsigned long long tmp = 0;
  662. struct acpi_ec *ec = context;
  663. /* clear addr values, ec_parse_io_ports depend on it */
  664. ec->command_addr = ec->data_addr = 0;
  665. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  666. ec_parse_io_ports, ec);
  667. if (ACPI_FAILURE(status))
  668. return status;
  669. /* Get GPE bit assignment (EC events). */
  670. /* TODO: Add support for _GPE returning a package */
  671. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  672. if (ACPI_FAILURE(status))
  673. return status;
  674. ec->gpe = tmp;
  675. /* Use the global lock for all EC transactions? */
  676. tmp = 0;
  677. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  678. ec->global_lock = tmp;
  679. ec->handle = handle;
  680. return AE_CTRL_TERMINATE;
  681. }
  682. static int ec_install_handlers(struct acpi_ec *ec)
  683. {
  684. acpi_status status;
  685. if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
  686. return 0;
  687. status = acpi_install_gpe_handler(NULL, ec->gpe,
  688. ACPI_GPE_EDGE_TRIGGERED,
  689. &acpi_ec_gpe_handler, ec);
  690. if (ACPI_FAILURE(status))
  691. return -ENODEV;
  692. acpi_enable_gpe(NULL, ec->gpe);
  693. status = acpi_install_address_space_handler(ec->handle,
  694. ACPI_ADR_SPACE_EC,
  695. &acpi_ec_space_handler,
  696. NULL, ec);
  697. if (ACPI_FAILURE(status)) {
  698. if (status == AE_NOT_FOUND) {
  699. /*
  700. * Maybe OS fails in evaluating the _REG object.
  701. * The AE_NOT_FOUND error will be ignored and OS
  702. * continue to initialize EC.
  703. */
  704. pr_err("Fail in evaluating the _REG object"
  705. " of EC device. Broken bios is suspected.\n");
  706. } else {
  707. acpi_disable_gpe(NULL, ec->gpe);
  708. acpi_remove_gpe_handler(NULL, ec->gpe,
  709. &acpi_ec_gpe_handler);
  710. return -ENODEV;
  711. }
  712. }
  713. set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  714. return 0;
  715. }
  716. static void ec_remove_handlers(struct acpi_ec *ec)
  717. {
  718. acpi_disable_gpe(NULL, ec->gpe);
  719. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  720. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  721. pr_err("failed to remove space handler\n");
  722. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  723. &acpi_ec_gpe_handler)))
  724. pr_err("failed to remove gpe handler\n");
  725. clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
  726. }
  727. static int acpi_ec_add(struct acpi_device *device)
  728. {
  729. struct acpi_ec *ec = NULL;
  730. int ret;
  731. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  732. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  733. /* Check for boot EC */
  734. if (boot_ec &&
  735. (boot_ec->handle == device->handle ||
  736. boot_ec->handle == ACPI_ROOT_OBJECT)) {
  737. ec = boot_ec;
  738. boot_ec = NULL;
  739. } else {
  740. ec = make_acpi_ec();
  741. if (!ec)
  742. return -ENOMEM;
  743. }
  744. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  745. AE_CTRL_TERMINATE) {
  746. kfree(ec);
  747. return -EINVAL;
  748. }
  749. /* Find and register all query methods */
  750. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  751. acpi_ec_register_query_methods, NULL, ec, NULL);
  752. if (!first_ec)
  753. first_ec = ec;
  754. device->driver_data = ec;
  755. ret = !!request_region(ec->data_addr, 1, "EC data");
  756. WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
  757. ret = !!request_region(ec->command_addr, 1, "EC cmd");
  758. WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
  759. pr_info("GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
  760. ec->gpe, ec->command_addr, ec->data_addr);
  761. ret = ec_install_handlers(ec);
  762. /* EC is fully operational, allow queries */
  763. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  764. /* Clear stale _Q events if hardware might require that */
  765. if (EC_FLAGS_CLEAR_ON_RESUME) {
  766. mutex_lock(&ec->mutex);
  767. acpi_ec_clear(ec);
  768. mutex_unlock(&ec->mutex);
  769. }
  770. return ret;
  771. }
  772. static int acpi_ec_remove(struct acpi_device *device)
  773. {
  774. struct acpi_ec *ec;
  775. struct acpi_ec_query_handler *handler, *tmp;
  776. if (!device)
  777. return -EINVAL;
  778. ec = acpi_driver_data(device);
  779. ec_remove_handlers(ec);
  780. mutex_lock(&ec->mutex);
  781. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  782. list_del(&handler->node);
  783. kfree(handler);
  784. }
  785. mutex_unlock(&ec->mutex);
  786. release_region(ec->data_addr, 1);
  787. release_region(ec->command_addr, 1);
  788. device->driver_data = NULL;
  789. if (ec == first_ec)
  790. first_ec = NULL;
  791. kfree(ec);
  792. return 0;
  793. }
  794. static acpi_status
  795. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  796. {
  797. struct acpi_ec *ec = context;
  798. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  799. return AE_OK;
  800. /*
  801. * The first address region returned is the data port, and
  802. * the second address region returned is the status/command
  803. * port.
  804. */
  805. if (ec->data_addr == 0)
  806. ec->data_addr = resource->data.io.minimum;
  807. else if (ec->command_addr == 0)
  808. ec->command_addr = resource->data.io.minimum;
  809. else
  810. return AE_CTRL_TERMINATE;
  811. return AE_OK;
  812. }
  813. int __init acpi_boot_ec_enable(void)
  814. {
  815. if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
  816. return 0;
  817. if (!ec_install_handlers(boot_ec)) {
  818. first_ec = boot_ec;
  819. return 0;
  820. }
  821. return -EFAULT;
  822. }
  823. static const struct acpi_device_id ec_device_ids[] = {
  824. {"PNP0C09", 0},
  825. {"", 0},
  826. };
  827. /* Some BIOS do not survive early DSDT scan, skip it */
  828. static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
  829. {
  830. EC_FLAGS_SKIP_DSDT_SCAN = 1;
  831. return 0;
  832. }
  833. /* ASUStek often supplies us with broken ECDT, validate it */
  834. static int ec_validate_ecdt(const struct dmi_system_id *id)
  835. {
  836. EC_FLAGS_VALIDATE_ECDT = 1;
  837. return 0;
  838. }
  839. /* MSI EC needs special treatment, enable it */
  840. static int ec_flag_msi(const struct dmi_system_id *id)
  841. {
  842. pr_debug("Detected MSI hardware, enabling workarounds.\n");
  843. EC_FLAGS_MSI = 1;
  844. EC_FLAGS_VALIDATE_ECDT = 1;
  845. return 0;
  846. }
  847. /*
  848. * Clevo M720 notebook actually works ok with IRQ mode, if we lifted
  849. * the GPE storm threshold back to 20
  850. */
  851. static int ec_enlarge_storm_threshold(const struct dmi_system_id *id)
  852. {
  853. pr_debug("Setting the EC GPE storm threshold to 20\n");
  854. ec_storm_threshold = 20;
  855. return 0;
  856. }
  857. /*
  858. * On some hardware it is necessary to clear events accumulated by the EC during
  859. * sleep. These ECs stop reporting GPEs until they are manually polled, if too
  860. * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
  861. *
  862. * https://bugzilla.kernel.org/show_bug.cgi?id=44161
  863. *
  864. * Ideally, the EC should also be instructed NOT to accumulate events during
  865. * sleep (which Windows seems to do somehow), but the interface to control this
  866. * behaviour is not known at this time.
  867. *
  868. * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
  869. * however it is very likely that other Samsung models are affected.
  870. *
  871. * On systems which don't accumulate _Q events during sleep, this extra check
  872. * should be harmless.
  873. */
  874. static int ec_clear_on_resume(const struct dmi_system_id *id)
  875. {
  876. pr_debug("Detected system needing EC poll on resume.\n");
  877. EC_FLAGS_CLEAR_ON_RESUME = 1;
  878. return 0;
  879. }
  880. static struct dmi_system_id ec_dmi_table[] __initdata = {
  881. {
  882. ec_skip_dsdt_scan, "Compal JFL92", {
  883. DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
  884. DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
  885. {
  886. ec_flag_msi, "MSI hardware", {
  887. DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
  888. {
  889. ec_flag_msi, "MSI hardware", {
  890. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
  891. {
  892. ec_flag_msi, "MSI hardware", {
  893. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
  894. {
  895. ec_flag_msi, "MSI hardware", {
  896. DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
  897. {
  898. ec_flag_msi, "Quanta hardware", {
  899. DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
  900. DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
  901. {
  902. ec_flag_msi, "Quanta hardware", {
  903. DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
  904. DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
  905. {
  906. ec_flag_msi, "Clevo W350etq", {
  907. DMI_MATCH(DMI_SYS_VENDOR, "CLEVO CO."),
  908. DMI_MATCH(DMI_PRODUCT_NAME, "W35_37ET"),}, NULL},
  909. {
  910. ec_validate_ecdt, "ASUS hardware", {
  911. DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
  912. {
  913. ec_validate_ecdt, "ASUS hardware", {
  914. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
  915. {
  916. ec_enlarge_storm_threshold, "CLEVO hardware", {
  917. DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
  918. DMI_MATCH(DMI_PRODUCT_NAME, "M720T/M730T"),}, NULL},
  919. {
  920. ec_skip_dsdt_scan, "HP Folio 13", {
  921. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  922. DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13"),}, NULL},
  923. {
  924. ec_validate_ecdt, "ASUS hardware", {
  925. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."),
  926. DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL},
  927. {
  928. ec_clear_on_resume, "Samsung hardware", {
  929. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
  930. {},
  931. };
  932. int __init acpi_ec_ecdt_probe(void)
  933. {
  934. acpi_status status;
  935. struct acpi_ec *saved_ec = NULL;
  936. struct acpi_table_ecdt *ecdt_ptr;
  937. boot_ec = make_acpi_ec();
  938. if (!boot_ec)
  939. return -ENOMEM;
  940. /*
  941. * Generate a boot ec context
  942. */
  943. dmi_check_system(ec_dmi_table);
  944. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  945. (struct acpi_table_header **)&ecdt_ptr);
  946. if (ACPI_SUCCESS(status)) {
  947. pr_info("EC description table is found, configuring boot EC\n");
  948. boot_ec->command_addr = ecdt_ptr->control.address;
  949. boot_ec->data_addr = ecdt_ptr->data.address;
  950. boot_ec->gpe = ecdt_ptr->gpe;
  951. boot_ec->handle = ACPI_ROOT_OBJECT;
  952. acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
  953. /* Don't trust ECDT, which comes from ASUSTek */
  954. if (!EC_FLAGS_VALIDATE_ECDT)
  955. goto install;
  956. saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
  957. if (!saved_ec)
  958. return -ENOMEM;
  959. /* fall through */
  960. }
  961. if (EC_FLAGS_SKIP_DSDT_SCAN) {
  962. kfree(saved_ec);
  963. return -ENODEV;
  964. }
  965. /* This workaround is needed only on some broken machines,
  966. * which require early EC, but fail to provide ECDT */
  967. pr_debug("Look up EC in DSDT\n");
  968. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
  969. boot_ec, NULL);
  970. /* Check that acpi_get_devices actually find something */
  971. if (ACPI_FAILURE(status) || !boot_ec->handle)
  972. goto error;
  973. if (saved_ec) {
  974. /* try to find good ECDT from ASUSTek */
  975. if (saved_ec->command_addr != boot_ec->command_addr ||
  976. saved_ec->data_addr != boot_ec->data_addr ||
  977. saved_ec->gpe != boot_ec->gpe ||
  978. saved_ec->handle != boot_ec->handle)
  979. pr_info("ASUSTek keeps feeding us with broken "
  980. "ECDT tables, which are very hard to workaround. "
  981. "Trying to use DSDT EC info instead. Please send "
  982. "output of acpidump to linux-acpi@vger.kernel.org\n");
  983. kfree(saved_ec);
  984. saved_ec = NULL;
  985. } else {
  986. /* We really need to limit this workaround, the only ASUS,
  987. * which needs it, has fake EC._INI method, so use it as flag.
  988. * Keep boot_ec struct as it will be needed soon.
  989. */
  990. if (!dmi_name_in_vendors("ASUS") ||
  991. !acpi_has_method(boot_ec->handle, "_INI"))
  992. return -ENODEV;
  993. }
  994. install:
  995. if (!ec_install_handlers(boot_ec)) {
  996. first_ec = boot_ec;
  997. return 0;
  998. }
  999. error:
  1000. kfree(boot_ec);
  1001. kfree(saved_ec);
  1002. boot_ec = NULL;
  1003. return -ENODEV;
  1004. }
  1005. static struct acpi_driver acpi_ec_driver = {
  1006. .name = "ec",
  1007. .class = ACPI_EC_CLASS,
  1008. .ids = ec_device_ids,
  1009. .ops = {
  1010. .add = acpi_ec_add,
  1011. .remove = acpi_ec_remove,
  1012. },
  1013. };
  1014. int __init acpi_ec_init(void)
  1015. {
  1016. int result = 0;
  1017. /* Now register the driver for the EC */
  1018. result = acpi_bus_register_driver(&acpi_ec_driver);
  1019. if (result < 0)
  1020. return -ENODEV;
  1021. return result;
  1022. }
  1023. /* EC driver currently not unloadable */
  1024. #if 0
  1025. static void __exit acpi_ec_exit(void)
  1026. {
  1027. acpi_bus_unregister_driver(&acpi_ec_driver);
  1028. return;
  1029. }
  1030. #endif /* 0 */