ec.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952
  1. /*
  2. * ec.c - ACPI Embedded Controller Driver (v3)
  3. *
  4. * Copyright (C) 2001-2015 Intel Corporation
  5. * Author: 2014, 2015 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. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. */
  27. /* Uncomment next line to get verbose printout */
  28. /* #define DEBUG */
  29. #define pr_fmt(fmt) "ACPI : EC: " fmt
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/types.h>
  34. #include <linux/delay.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/list.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/slab.h>
  39. #include <linux/acpi.h>
  40. #include <linux/dmi.h>
  41. #include <asm/io.h>
  42. #include "internal.h"
  43. #define ACPI_EC_CLASS "embedded_controller"
  44. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  45. #define ACPI_EC_FILE_INFO "info"
  46. /* EC status register */
  47. #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
  48. #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
  49. #define ACPI_EC_FLAG_CMD 0x08 /* Input buffer contains a command */
  50. #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
  51. #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
  52. /*
  53. * The SCI_EVT clearing timing is not defined by the ACPI specification.
  54. * This leads to lots of practical timing issues for the host EC driver.
  55. * The following variations are defined (from the target EC firmware's
  56. * perspective):
  57. * STATUS: After indicating SCI_EVT edge triggered IRQ to the host, the
  58. * target can clear SCI_EVT at any time so long as the host can see
  59. * the indication by reading the status register (EC_SC). So the
  60. * host should re-check SCI_EVT after the first time the SCI_EVT
  61. * indication is seen, which is the same time the query request
  62. * (QR_EC) is written to the command register (EC_CMD). SCI_EVT set
  63. * at any later time could indicate another event. Normally such
  64. * kind of EC firmware has implemented an event queue and will
  65. * return 0x00 to indicate "no outstanding event".
  66. * QUERY: After seeing the query request (QR_EC) written to the command
  67. * register (EC_CMD) by the host and having prepared the responding
  68. * event value in the data register (EC_DATA), the target can safely
  69. * clear SCI_EVT because the target can confirm that the current
  70. * event is being handled by the host. The host then should check
  71. * SCI_EVT right after reading the event response from the data
  72. * register (EC_DATA).
  73. * EVENT: After seeing the event response read from the data register
  74. * (EC_DATA) by the host, the target can clear SCI_EVT. As the
  75. * target requires time to notice the change in the data register
  76. * (EC_DATA), the host may be required to wait additional guarding
  77. * time before checking the SCI_EVT again. Such guarding may not be
  78. * necessary if the host is notified via another IRQ.
  79. */
  80. #define ACPI_EC_EVT_TIMING_STATUS 0x00
  81. #define ACPI_EC_EVT_TIMING_QUERY 0x01
  82. #define ACPI_EC_EVT_TIMING_EVENT 0x02
  83. /* EC commands */
  84. enum ec_command {
  85. ACPI_EC_COMMAND_READ = 0x80,
  86. ACPI_EC_COMMAND_WRITE = 0x81,
  87. ACPI_EC_BURST_ENABLE = 0x82,
  88. ACPI_EC_BURST_DISABLE = 0x83,
  89. ACPI_EC_COMMAND_QUERY = 0x84,
  90. };
  91. #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
  92. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  93. #define ACPI_EC_UDELAY_POLL 550 /* Wait 1ms for EC transaction polling */
  94. #define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query
  95. * when trying to clear the EC */
  96. #define ACPI_EC_MAX_QUERIES 16 /* Maximum number of parallel queries */
  97. enum {
  98. EC_FLAGS_QUERY_ENABLED, /* Query is enabled */
  99. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  100. EC_FLAGS_QUERY_GUARDING, /* Guard for SCI_EVT check */
  101. EC_FLAGS_GPE_HANDLER_INSTALLED, /* GPE handler installed */
  102. EC_FLAGS_EC_HANDLER_INSTALLED, /* OpReg handler installed */
  103. EC_FLAGS_EVT_HANDLER_INSTALLED, /* _Qxx handlers installed */
  104. EC_FLAGS_STARTED, /* Driver is started */
  105. EC_FLAGS_STOPPED, /* Driver is stopped */
  106. EC_FLAGS_COMMAND_STORM, /* GPE storms occurred to the
  107. * current command processing */
  108. };
  109. #define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */
  110. #define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */
  111. /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
  112. static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
  113. module_param(ec_delay, uint, 0644);
  114. MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
  115. static unsigned int ec_max_queries __read_mostly = ACPI_EC_MAX_QUERIES;
  116. module_param(ec_max_queries, uint, 0644);
  117. MODULE_PARM_DESC(ec_max_queries, "Maximum parallel _Qxx evaluations");
  118. static bool ec_busy_polling __read_mostly;
  119. module_param(ec_busy_polling, bool, 0644);
  120. MODULE_PARM_DESC(ec_busy_polling, "Use busy polling to advance EC transaction");
  121. static unsigned int ec_polling_guard __read_mostly = ACPI_EC_UDELAY_POLL;
  122. module_param(ec_polling_guard, uint, 0644);
  123. MODULE_PARM_DESC(ec_polling_guard, "Guard time(us) between EC accesses in polling modes");
  124. static unsigned int ec_event_clearing __read_mostly = ACPI_EC_EVT_TIMING_QUERY;
  125. /*
  126. * If the number of false interrupts per one transaction exceeds
  127. * this threshold, will think there is a GPE storm happened and
  128. * will disable the GPE for normal transaction.
  129. */
  130. static unsigned int ec_storm_threshold __read_mostly = 8;
  131. module_param(ec_storm_threshold, uint, 0644);
  132. MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
  133. static bool ec_freeze_events __read_mostly = true;
  134. module_param(ec_freeze_events, bool, 0644);
  135. MODULE_PARM_DESC(ec_freeze_events, "Disabling event handling during suspend/resume");
  136. struct acpi_ec_query_handler {
  137. struct list_head node;
  138. acpi_ec_query_func func;
  139. acpi_handle handle;
  140. void *data;
  141. u8 query_bit;
  142. struct kref kref;
  143. };
  144. struct transaction {
  145. const u8 *wdata;
  146. u8 *rdata;
  147. unsigned short irq_count;
  148. u8 command;
  149. u8 wi;
  150. u8 ri;
  151. u8 wlen;
  152. u8 rlen;
  153. u8 flags;
  154. };
  155. struct acpi_ec_query {
  156. struct transaction transaction;
  157. struct work_struct work;
  158. struct acpi_ec_query_handler *handler;
  159. };
  160. static int acpi_ec_query(struct acpi_ec *ec, u8 *data);
  161. static void advance_transaction(struct acpi_ec *ec);
  162. static void acpi_ec_event_handler(struct work_struct *work);
  163. static void acpi_ec_event_processor(struct work_struct *work);
  164. struct acpi_ec *boot_ec, *first_ec;
  165. EXPORT_SYMBOL(first_ec);
  166. static bool boot_ec_is_ecdt = false;
  167. static struct workqueue_struct *ec_query_wq;
  168. static int EC_FLAGS_QUERY_HANDSHAKE; /* Needs QR_EC issued when SCI_EVT set */
  169. static int EC_FLAGS_CORRECT_ECDT; /* Needs ECDT port address correction */
  170. /* --------------------------------------------------------------------------
  171. * Logging/Debugging
  172. * -------------------------------------------------------------------------- */
  173. /*
  174. * Splitters used by the developers to track the boundary of the EC
  175. * handling processes.
  176. */
  177. #ifdef DEBUG
  178. #define EC_DBG_SEP " "
  179. #define EC_DBG_DRV "+++++"
  180. #define EC_DBG_STM "====="
  181. #define EC_DBG_REQ "*****"
  182. #define EC_DBG_EVT "#####"
  183. #else
  184. #define EC_DBG_SEP ""
  185. #define EC_DBG_DRV
  186. #define EC_DBG_STM
  187. #define EC_DBG_REQ
  188. #define EC_DBG_EVT
  189. #endif
  190. #define ec_log_raw(fmt, ...) \
  191. pr_info(fmt "\n", ##__VA_ARGS__)
  192. #define ec_dbg_raw(fmt, ...) \
  193. pr_debug(fmt "\n", ##__VA_ARGS__)
  194. #define ec_log(filter, fmt, ...) \
  195. ec_log_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__)
  196. #define ec_dbg(filter, fmt, ...) \
  197. ec_dbg_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__)
  198. #define ec_log_drv(fmt, ...) \
  199. ec_log(EC_DBG_DRV, fmt, ##__VA_ARGS__)
  200. #define ec_dbg_drv(fmt, ...) \
  201. ec_dbg(EC_DBG_DRV, fmt, ##__VA_ARGS__)
  202. #define ec_dbg_stm(fmt, ...) \
  203. ec_dbg(EC_DBG_STM, fmt, ##__VA_ARGS__)
  204. #define ec_dbg_req(fmt, ...) \
  205. ec_dbg(EC_DBG_REQ, fmt, ##__VA_ARGS__)
  206. #define ec_dbg_evt(fmt, ...) \
  207. ec_dbg(EC_DBG_EVT, fmt, ##__VA_ARGS__)
  208. #define ec_dbg_ref(ec, fmt, ...) \
  209. ec_dbg_raw("%lu: " fmt, ec->reference_count, ## __VA_ARGS__)
  210. /* --------------------------------------------------------------------------
  211. * Device Flags
  212. * -------------------------------------------------------------------------- */
  213. static bool acpi_ec_started(struct acpi_ec *ec)
  214. {
  215. return test_bit(EC_FLAGS_STARTED, &ec->flags) &&
  216. !test_bit(EC_FLAGS_STOPPED, &ec->flags);
  217. }
  218. static bool acpi_ec_event_enabled(struct acpi_ec *ec)
  219. {
  220. /*
  221. * There is an OSPM early stage logic. During the early stages
  222. * (boot/resume), OSPMs shouldn't enable the event handling, only
  223. * the EC transactions are allowed to be performed.
  224. */
  225. if (!test_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags))
  226. return false;
  227. /*
  228. * However, disabling the event handling is experimental for late
  229. * stage (suspend), and is controlled by the boot parameter of
  230. * "ec_freeze_events":
  231. * 1. true: The EC event handling is disabled before entering
  232. * the noirq stage.
  233. * 2. false: The EC event handling is automatically disabled as
  234. * soon as the EC driver is stopped.
  235. */
  236. if (ec_freeze_events)
  237. return acpi_ec_started(ec);
  238. else
  239. return test_bit(EC_FLAGS_STARTED, &ec->flags);
  240. }
  241. static bool acpi_ec_flushed(struct acpi_ec *ec)
  242. {
  243. return ec->reference_count == 1;
  244. }
  245. /* --------------------------------------------------------------------------
  246. * EC Registers
  247. * -------------------------------------------------------------------------- */
  248. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  249. {
  250. u8 x = inb(ec->command_addr);
  251. ec_dbg_raw("EC_SC(R) = 0x%2.2x "
  252. "SCI_EVT=%d BURST=%d CMD=%d IBF=%d OBF=%d",
  253. x,
  254. !!(x & ACPI_EC_FLAG_SCI),
  255. !!(x & ACPI_EC_FLAG_BURST),
  256. !!(x & ACPI_EC_FLAG_CMD),
  257. !!(x & ACPI_EC_FLAG_IBF),
  258. !!(x & ACPI_EC_FLAG_OBF));
  259. return x;
  260. }
  261. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  262. {
  263. u8 x = inb(ec->data_addr);
  264. ec->timestamp = jiffies;
  265. ec_dbg_raw("EC_DATA(R) = 0x%2.2x", x);
  266. return x;
  267. }
  268. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  269. {
  270. ec_dbg_raw("EC_SC(W) = 0x%2.2x", command);
  271. outb(command, ec->command_addr);
  272. ec->timestamp = jiffies;
  273. }
  274. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  275. {
  276. ec_dbg_raw("EC_DATA(W) = 0x%2.2x", data);
  277. outb(data, ec->data_addr);
  278. ec->timestamp = jiffies;
  279. }
  280. #ifdef DEBUG
  281. static const char *acpi_ec_cmd_string(u8 cmd)
  282. {
  283. switch (cmd) {
  284. case 0x80:
  285. return "RD_EC";
  286. case 0x81:
  287. return "WR_EC";
  288. case 0x82:
  289. return "BE_EC";
  290. case 0x83:
  291. return "BD_EC";
  292. case 0x84:
  293. return "QR_EC";
  294. }
  295. return "UNKNOWN";
  296. }
  297. #else
  298. #define acpi_ec_cmd_string(cmd) "UNDEF"
  299. #endif
  300. /* --------------------------------------------------------------------------
  301. * GPE Registers
  302. * -------------------------------------------------------------------------- */
  303. static inline bool acpi_ec_is_gpe_raised(struct acpi_ec *ec)
  304. {
  305. acpi_event_status gpe_status = 0;
  306. (void)acpi_get_gpe_status(NULL, ec->gpe, &gpe_status);
  307. return (gpe_status & ACPI_EVENT_FLAG_STATUS_SET) ? true : false;
  308. }
  309. static inline void acpi_ec_enable_gpe(struct acpi_ec *ec, bool open)
  310. {
  311. if (open)
  312. acpi_enable_gpe(NULL, ec->gpe);
  313. else {
  314. BUG_ON(ec->reference_count < 1);
  315. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
  316. }
  317. if (acpi_ec_is_gpe_raised(ec)) {
  318. /*
  319. * On some platforms, EN=1 writes cannot trigger GPE. So
  320. * software need to manually trigger a pseudo GPE event on
  321. * EN=1 writes.
  322. */
  323. ec_dbg_raw("Polling quirk");
  324. advance_transaction(ec);
  325. }
  326. }
  327. static inline void acpi_ec_disable_gpe(struct acpi_ec *ec, bool close)
  328. {
  329. if (close)
  330. acpi_disable_gpe(NULL, ec->gpe);
  331. else {
  332. BUG_ON(ec->reference_count < 1);
  333. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
  334. }
  335. }
  336. static inline void acpi_ec_clear_gpe(struct acpi_ec *ec)
  337. {
  338. /*
  339. * GPE STS is a W1C register, which means:
  340. * 1. Software can clear it without worrying about clearing other
  341. * GPEs' STS bits when the hardware sets them in parallel.
  342. * 2. As long as software can ensure only clearing it when it is
  343. * set, hardware won't set it in parallel.
  344. * So software can clear GPE in any contexts.
  345. * Warning: do not move the check into advance_transaction() as the
  346. * EC commands will be sent without GPE raised.
  347. */
  348. if (!acpi_ec_is_gpe_raised(ec))
  349. return;
  350. acpi_clear_gpe(NULL, ec->gpe);
  351. }
  352. /* --------------------------------------------------------------------------
  353. * Transaction Management
  354. * -------------------------------------------------------------------------- */
  355. static void acpi_ec_submit_request(struct acpi_ec *ec)
  356. {
  357. ec->reference_count++;
  358. if (test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags) &&
  359. ec->reference_count == 1)
  360. acpi_ec_enable_gpe(ec, true);
  361. }
  362. static void acpi_ec_complete_request(struct acpi_ec *ec)
  363. {
  364. bool flushed = false;
  365. ec->reference_count--;
  366. if (test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags) &&
  367. ec->reference_count == 0)
  368. acpi_ec_disable_gpe(ec, true);
  369. flushed = acpi_ec_flushed(ec);
  370. if (flushed)
  371. wake_up(&ec->wait);
  372. }
  373. static void acpi_ec_set_storm(struct acpi_ec *ec, u8 flag)
  374. {
  375. if (!test_bit(flag, &ec->flags)) {
  376. acpi_ec_disable_gpe(ec, false);
  377. ec_dbg_drv("Polling enabled");
  378. set_bit(flag, &ec->flags);
  379. }
  380. }
  381. static void acpi_ec_clear_storm(struct acpi_ec *ec, u8 flag)
  382. {
  383. if (test_bit(flag, &ec->flags)) {
  384. clear_bit(flag, &ec->flags);
  385. acpi_ec_enable_gpe(ec, false);
  386. ec_dbg_drv("Polling disabled");
  387. }
  388. }
  389. /*
  390. * acpi_ec_submit_flushable_request() - Increase the reference count unless
  391. * the flush operation is not in
  392. * progress
  393. * @ec: the EC device
  394. *
  395. * This function must be used before taking a new action that should hold
  396. * the reference count. If this function returns false, then the action
  397. * must be discarded or it will prevent the flush operation from being
  398. * completed.
  399. */
  400. static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec)
  401. {
  402. if (!acpi_ec_started(ec))
  403. return false;
  404. acpi_ec_submit_request(ec);
  405. return true;
  406. }
  407. static void acpi_ec_submit_query(struct acpi_ec *ec)
  408. {
  409. if (acpi_ec_event_enabled(ec) &&
  410. !test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  411. ec_dbg_evt("Command(%s) submitted/blocked",
  412. acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
  413. ec->nr_pending_queries++;
  414. schedule_work(&ec->work);
  415. }
  416. }
  417. static void acpi_ec_complete_query(struct acpi_ec *ec)
  418. {
  419. if (test_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  420. clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
  421. ec_dbg_evt("Command(%s) unblocked",
  422. acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
  423. }
  424. }
  425. static inline void __acpi_ec_enable_event(struct acpi_ec *ec)
  426. {
  427. if (!test_and_set_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags))
  428. ec_log_drv("event unblocked");
  429. if (!test_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  430. advance_transaction(ec);
  431. }
  432. static inline void __acpi_ec_disable_event(struct acpi_ec *ec)
  433. {
  434. if (test_and_clear_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags))
  435. ec_log_drv("event blocked");
  436. }
  437. static void acpi_ec_enable_event(struct acpi_ec *ec)
  438. {
  439. unsigned long flags;
  440. spin_lock_irqsave(&ec->lock, flags);
  441. if (acpi_ec_started(ec))
  442. __acpi_ec_enable_event(ec);
  443. spin_unlock_irqrestore(&ec->lock, flags);
  444. }
  445. #ifdef CONFIG_PM_SLEEP
  446. static bool acpi_ec_query_flushed(struct acpi_ec *ec)
  447. {
  448. bool flushed;
  449. unsigned long flags;
  450. spin_lock_irqsave(&ec->lock, flags);
  451. flushed = !ec->nr_pending_queries;
  452. spin_unlock_irqrestore(&ec->lock, flags);
  453. return flushed;
  454. }
  455. static void __acpi_ec_flush_event(struct acpi_ec *ec)
  456. {
  457. /*
  458. * When ec_freeze_events is true, we need to flush events in
  459. * the proper position before entering the noirq stage.
  460. */
  461. wait_event(ec->wait, acpi_ec_query_flushed(ec));
  462. if (ec_query_wq)
  463. flush_workqueue(ec_query_wq);
  464. }
  465. static void acpi_ec_disable_event(struct acpi_ec *ec)
  466. {
  467. unsigned long flags;
  468. spin_lock_irqsave(&ec->lock, flags);
  469. __acpi_ec_disable_event(ec);
  470. spin_unlock_irqrestore(&ec->lock, flags);
  471. __acpi_ec_flush_event(ec);
  472. }
  473. #endif /* CONFIG_PM_SLEEP */
  474. static bool acpi_ec_guard_event(struct acpi_ec *ec)
  475. {
  476. bool guarded = true;
  477. unsigned long flags;
  478. spin_lock_irqsave(&ec->lock, flags);
  479. /*
  480. * If firmware SCI_EVT clearing timing is "event", we actually
  481. * don't know when the SCI_EVT will be cleared by firmware after
  482. * evaluating _Qxx, so we need to re-check SCI_EVT after waiting an
  483. * acceptable period.
  484. *
  485. * The guarding period begins when EC_FLAGS_QUERY_PENDING is
  486. * flagged, which means SCI_EVT check has just been performed.
  487. * But if the current transaction is ACPI_EC_COMMAND_QUERY, the
  488. * guarding should have already been performed (via
  489. * EC_FLAGS_QUERY_GUARDING) and should not be applied so that the
  490. * ACPI_EC_COMMAND_QUERY transaction can be transitioned into
  491. * ACPI_EC_COMMAND_POLL state immediately.
  492. */
  493. if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS ||
  494. ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY ||
  495. !test_bit(EC_FLAGS_QUERY_PENDING, &ec->flags) ||
  496. (ec->curr && ec->curr->command == ACPI_EC_COMMAND_QUERY))
  497. guarded = false;
  498. spin_unlock_irqrestore(&ec->lock, flags);
  499. return guarded;
  500. }
  501. static int ec_transaction_polled(struct acpi_ec *ec)
  502. {
  503. unsigned long flags;
  504. int ret = 0;
  505. spin_lock_irqsave(&ec->lock, flags);
  506. if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_POLL))
  507. ret = 1;
  508. spin_unlock_irqrestore(&ec->lock, flags);
  509. return ret;
  510. }
  511. static int ec_transaction_completed(struct acpi_ec *ec)
  512. {
  513. unsigned long flags;
  514. int ret = 0;
  515. spin_lock_irqsave(&ec->lock, flags);
  516. if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
  517. ret = 1;
  518. spin_unlock_irqrestore(&ec->lock, flags);
  519. return ret;
  520. }
  521. static inline void ec_transaction_transition(struct acpi_ec *ec, unsigned long flag)
  522. {
  523. ec->curr->flags |= flag;
  524. if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
  525. if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS &&
  526. flag == ACPI_EC_COMMAND_POLL)
  527. acpi_ec_complete_query(ec);
  528. if (ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY &&
  529. flag == ACPI_EC_COMMAND_COMPLETE)
  530. acpi_ec_complete_query(ec);
  531. if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT &&
  532. flag == ACPI_EC_COMMAND_COMPLETE)
  533. set_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags);
  534. }
  535. }
  536. static void advance_transaction(struct acpi_ec *ec)
  537. {
  538. struct transaction *t;
  539. u8 status;
  540. bool wakeup = false;
  541. ec_dbg_stm("%s (%d)", in_interrupt() ? "IRQ" : "TASK",
  542. smp_processor_id());
  543. /*
  544. * By always clearing STS before handling all indications, we can
  545. * ensure a hardware STS 0->1 change after this clearing can always
  546. * trigger a GPE interrupt.
  547. */
  548. acpi_ec_clear_gpe(ec);
  549. status = acpi_ec_read_status(ec);
  550. t = ec->curr;
  551. /*
  552. * Another IRQ or a guarded polling mode advancement is detected,
  553. * the next QR_EC submission is then allowed.
  554. */
  555. if (!t || !(t->flags & ACPI_EC_COMMAND_POLL)) {
  556. if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT &&
  557. (!ec->nr_pending_queries ||
  558. test_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags))) {
  559. clear_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags);
  560. acpi_ec_complete_query(ec);
  561. }
  562. }
  563. if (!t)
  564. goto err;
  565. if (t->flags & ACPI_EC_COMMAND_POLL) {
  566. if (t->wlen > t->wi) {
  567. if ((status & ACPI_EC_FLAG_IBF) == 0)
  568. acpi_ec_write_data(ec, t->wdata[t->wi++]);
  569. else
  570. goto err;
  571. } else if (t->rlen > t->ri) {
  572. if ((status & ACPI_EC_FLAG_OBF) == 1) {
  573. t->rdata[t->ri++] = acpi_ec_read_data(ec);
  574. if (t->rlen == t->ri) {
  575. ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE);
  576. if (t->command == ACPI_EC_COMMAND_QUERY)
  577. ec_dbg_evt("Command(%s) completed by hardware",
  578. acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
  579. wakeup = true;
  580. }
  581. } else
  582. goto err;
  583. } else if (t->wlen == t->wi &&
  584. (status & ACPI_EC_FLAG_IBF) == 0) {
  585. ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE);
  586. wakeup = true;
  587. }
  588. goto out;
  589. } else {
  590. if (EC_FLAGS_QUERY_HANDSHAKE &&
  591. !(status & ACPI_EC_FLAG_SCI) &&
  592. (t->command == ACPI_EC_COMMAND_QUERY)) {
  593. ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL);
  594. t->rdata[t->ri++] = 0x00;
  595. ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE);
  596. ec_dbg_evt("Command(%s) completed by software",
  597. acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
  598. wakeup = true;
  599. } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
  600. acpi_ec_write_cmd(ec, t->command);
  601. ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL);
  602. } else
  603. goto err;
  604. goto out;
  605. }
  606. err:
  607. /*
  608. * If SCI bit is set, then don't think it's a false IRQ
  609. * otherwise will take a not handled IRQ as a false one.
  610. */
  611. if (!(status & ACPI_EC_FLAG_SCI)) {
  612. if (in_interrupt() && t) {
  613. if (t->irq_count < ec_storm_threshold)
  614. ++t->irq_count;
  615. /* Allow triggering on 0 threshold */
  616. if (t->irq_count == ec_storm_threshold)
  617. acpi_ec_set_storm(ec, EC_FLAGS_COMMAND_STORM);
  618. }
  619. }
  620. out:
  621. if (status & ACPI_EC_FLAG_SCI)
  622. acpi_ec_submit_query(ec);
  623. if (wakeup && in_interrupt())
  624. wake_up(&ec->wait);
  625. }
  626. static void start_transaction(struct acpi_ec *ec)
  627. {
  628. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  629. ec->curr->flags = 0;
  630. }
  631. static int ec_guard(struct acpi_ec *ec)
  632. {
  633. unsigned long guard = usecs_to_jiffies(ec->polling_guard);
  634. unsigned long timeout = ec->timestamp + guard;
  635. /* Ensure guarding period before polling EC status */
  636. do {
  637. if (ec->busy_polling) {
  638. /* Perform busy polling */
  639. if (ec_transaction_completed(ec))
  640. return 0;
  641. udelay(jiffies_to_usecs(guard));
  642. } else {
  643. /*
  644. * Perform wait polling
  645. * 1. Wait the transaction to be completed by the
  646. * GPE handler after the transaction enters
  647. * ACPI_EC_COMMAND_POLL state.
  648. * 2. A special guarding logic is also required
  649. * for event clearing mode "event" before the
  650. * transaction enters ACPI_EC_COMMAND_POLL
  651. * state.
  652. */
  653. if (!ec_transaction_polled(ec) &&
  654. !acpi_ec_guard_event(ec))
  655. break;
  656. if (wait_event_timeout(ec->wait,
  657. ec_transaction_completed(ec),
  658. guard))
  659. return 0;
  660. }
  661. } while (time_before(jiffies, timeout));
  662. return -ETIME;
  663. }
  664. static int ec_poll(struct acpi_ec *ec)
  665. {
  666. unsigned long flags;
  667. int repeat = 5; /* number of command restarts */
  668. while (repeat--) {
  669. unsigned long delay = jiffies +
  670. msecs_to_jiffies(ec_delay);
  671. do {
  672. if (!ec_guard(ec))
  673. return 0;
  674. spin_lock_irqsave(&ec->lock, flags);
  675. advance_transaction(ec);
  676. spin_unlock_irqrestore(&ec->lock, flags);
  677. } while (time_before(jiffies, delay));
  678. pr_debug("controller reset, restart transaction\n");
  679. spin_lock_irqsave(&ec->lock, flags);
  680. start_transaction(ec);
  681. spin_unlock_irqrestore(&ec->lock, flags);
  682. }
  683. return -ETIME;
  684. }
  685. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  686. struct transaction *t)
  687. {
  688. unsigned long tmp;
  689. int ret = 0;
  690. /* start transaction */
  691. spin_lock_irqsave(&ec->lock, tmp);
  692. /* Enable GPE for command processing (IBF=0/OBF=1) */
  693. if (!acpi_ec_submit_flushable_request(ec)) {
  694. ret = -EINVAL;
  695. goto unlock;
  696. }
  697. ec_dbg_ref(ec, "Increase command");
  698. /* following two actions should be kept atomic */
  699. ec->curr = t;
  700. ec_dbg_req("Command(%s) started", acpi_ec_cmd_string(t->command));
  701. start_transaction(ec);
  702. spin_unlock_irqrestore(&ec->lock, tmp);
  703. ret = ec_poll(ec);
  704. spin_lock_irqsave(&ec->lock, tmp);
  705. if (t->irq_count == ec_storm_threshold)
  706. acpi_ec_clear_storm(ec, EC_FLAGS_COMMAND_STORM);
  707. ec_dbg_req("Command(%s) stopped", acpi_ec_cmd_string(t->command));
  708. ec->curr = NULL;
  709. /* Disable GPE for command processing (IBF=0/OBF=1) */
  710. acpi_ec_complete_request(ec);
  711. ec_dbg_ref(ec, "Decrease command");
  712. unlock:
  713. spin_unlock_irqrestore(&ec->lock, tmp);
  714. return ret;
  715. }
  716. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
  717. {
  718. int status;
  719. u32 glk;
  720. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  721. return -EINVAL;
  722. if (t->rdata)
  723. memset(t->rdata, 0, t->rlen);
  724. mutex_lock(&ec->mutex);
  725. if (ec->global_lock) {
  726. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  727. if (ACPI_FAILURE(status)) {
  728. status = -ENODEV;
  729. goto unlock;
  730. }
  731. }
  732. status = acpi_ec_transaction_unlocked(ec, t);
  733. if (ec->global_lock)
  734. acpi_release_global_lock(glk);
  735. unlock:
  736. mutex_unlock(&ec->mutex);
  737. return status;
  738. }
  739. static int acpi_ec_burst_enable(struct acpi_ec *ec)
  740. {
  741. u8 d;
  742. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  743. .wdata = NULL, .rdata = &d,
  744. .wlen = 0, .rlen = 1};
  745. return acpi_ec_transaction(ec, &t);
  746. }
  747. static int acpi_ec_burst_disable(struct acpi_ec *ec)
  748. {
  749. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  750. .wdata = NULL, .rdata = NULL,
  751. .wlen = 0, .rlen = 0};
  752. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  753. acpi_ec_transaction(ec, &t) : 0;
  754. }
  755. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
  756. {
  757. int result;
  758. u8 d;
  759. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  760. .wdata = &address, .rdata = &d,
  761. .wlen = 1, .rlen = 1};
  762. result = acpi_ec_transaction(ec, &t);
  763. *data = d;
  764. return result;
  765. }
  766. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  767. {
  768. u8 wdata[2] = { address, data };
  769. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  770. .wdata = wdata, .rdata = NULL,
  771. .wlen = 2, .rlen = 0};
  772. return acpi_ec_transaction(ec, &t);
  773. }
  774. int ec_read(u8 addr, u8 *val)
  775. {
  776. int err;
  777. u8 temp_data;
  778. if (!first_ec)
  779. return -ENODEV;
  780. err = acpi_ec_read(first_ec, addr, &temp_data);
  781. if (!err) {
  782. *val = temp_data;
  783. return 0;
  784. }
  785. return err;
  786. }
  787. EXPORT_SYMBOL(ec_read);
  788. int ec_write(u8 addr, u8 val)
  789. {
  790. int err;
  791. if (!first_ec)
  792. return -ENODEV;
  793. err = acpi_ec_write(first_ec, addr, val);
  794. return err;
  795. }
  796. EXPORT_SYMBOL(ec_write);
  797. int ec_transaction(u8 command,
  798. const u8 *wdata, unsigned wdata_len,
  799. u8 *rdata, unsigned rdata_len)
  800. {
  801. struct transaction t = {.command = command,
  802. .wdata = wdata, .rdata = rdata,
  803. .wlen = wdata_len, .rlen = rdata_len};
  804. if (!first_ec)
  805. return -ENODEV;
  806. return acpi_ec_transaction(first_ec, &t);
  807. }
  808. EXPORT_SYMBOL(ec_transaction);
  809. /* Get the handle to the EC device */
  810. acpi_handle ec_get_handle(void)
  811. {
  812. if (!first_ec)
  813. return NULL;
  814. return first_ec->handle;
  815. }
  816. EXPORT_SYMBOL(ec_get_handle);
  817. static void acpi_ec_start(struct acpi_ec *ec, bool resuming)
  818. {
  819. unsigned long flags;
  820. spin_lock_irqsave(&ec->lock, flags);
  821. if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) {
  822. ec_dbg_drv("Starting EC");
  823. /* Enable GPE for event processing (SCI_EVT=1) */
  824. if (!resuming) {
  825. acpi_ec_submit_request(ec);
  826. ec_dbg_ref(ec, "Increase driver");
  827. }
  828. ec_log_drv("EC started");
  829. }
  830. spin_unlock_irqrestore(&ec->lock, flags);
  831. }
  832. static bool acpi_ec_stopped(struct acpi_ec *ec)
  833. {
  834. unsigned long flags;
  835. bool flushed;
  836. spin_lock_irqsave(&ec->lock, flags);
  837. flushed = acpi_ec_flushed(ec);
  838. spin_unlock_irqrestore(&ec->lock, flags);
  839. return flushed;
  840. }
  841. static void acpi_ec_stop(struct acpi_ec *ec, bool suspending)
  842. {
  843. unsigned long flags;
  844. spin_lock_irqsave(&ec->lock, flags);
  845. if (acpi_ec_started(ec)) {
  846. ec_dbg_drv("Stopping EC");
  847. set_bit(EC_FLAGS_STOPPED, &ec->flags);
  848. spin_unlock_irqrestore(&ec->lock, flags);
  849. wait_event(ec->wait, acpi_ec_stopped(ec));
  850. spin_lock_irqsave(&ec->lock, flags);
  851. /* Disable GPE for event processing (SCI_EVT=1) */
  852. if (!suspending) {
  853. acpi_ec_complete_request(ec);
  854. ec_dbg_ref(ec, "Decrease driver");
  855. } else if (!ec_freeze_events)
  856. __acpi_ec_disable_event(ec);
  857. clear_bit(EC_FLAGS_STARTED, &ec->flags);
  858. clear_bit(EC_FLAGS_STOPPED, &ec->flags);
  859. ec_log_drv("EC stopped");
  860. }
  861. spin_unlock_irqrestore(&ec->lock, flags);
  862. }
  863. static void acpi_ec_enter_noirq(struct acpi_ec *ec)
  864. {
  865. unsigned long flags;
  866. spin_lock_irqsave(&ec->lock, flags);
  867. ec->busy_polling = true;
  868. ec->polling_guard = 0;
  869. ec_log_drv("interrupt blocked");
  870. spin_unlock_irqrestore(&ec->lock, flags);
  871. }
  872. static void acpi_ec_leave_noirq(struct acpi_ec *ec)
  873. {
  874. unsigned long flags;
  875. spin_lock_irqsave(&ec->lock, flags);
  876. ec->busy_polling = ec_busy_polling;
  877. ec->polling_guard = ec_polling_guard;
  878. ec_log_drv("interrupt unblocked");
  879. spin_unlock_irqrestore(&ec->lock, flags);
  880. }
  881. void acpi_ec_block_transactions(void)
  882. {
  883. struct acpi_ec *ec = first_ec;
  884. if (!ec)
  885. return;
  886. mutex_lock(&ec->mutex);
  887. /* Prevent transactions from being carried out */
  888. acpi_ec_stop(ec, true);
  889. mutex_unlock(&ec->mutex);
  890. }
  891. void acpi_ec_unblock_transactions(void)
  892. {
  893. /*
  894. * Allow transactions to happen again (this function is called from
  895. * atomic context during wakeup, so we don't need to acquire the mutex).
  896. */
  897. if (first_ec)
  898. acpi_ec_start(first_ec, true);
  899. }
  900. /* --------------------------------------------------------------------------
  901. Event Management
  902. -------------------------------------------------------------------------- */
  903. static struct acpi_ec_query_handler *
  904. acpi_ec_get_query_handler(struct acpi_ec_query_handler *handler)
  905. {
  906. if (handler)
  907. kref_get(&handler->kref);
  908. return handler;
  909. }
  910. static struct acpi_ec_query_handler *
  911. acpi_ec_get_query_handler_by_value(struct acpi_ec *ec, u8 value)
  912. {
  913. struct acpi_ec_query_handler *handler;
  914. bool found = false;
  915. mutex_lock(&ec->mutex);
  916. list_for_each_entry(handler, &ec->list, node) {
  917. if (value == handler->query_bit) {
  918. found = true;
  919. break;
  920. }
  921. }
  922. mutex_unlock(&ec->mutex);
  923. return found ? acpi_ec_get_query_handler(handler) : NULL;
  924. }
  925. static void acpi_ec_query_handler_release(struct kref *kref)
  926. {
  927. struct acpi_ec_query_handler *handler =
  928. container_of(kref, struct acpi_ec_query_handler, kref);
  929. kfree(handler);
  930. }
  931. static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler)
  932. {
  933. kref_put(&handler->kref, acpi_ec_query_handler_release);
  934. }
  935. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  936. acpi_handle handle, acpi_ec_query_func func,
  937. void *data)
  938. {
  939. struct acpi_ec_query_handler *handler =
  940. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  941. if (!handler)
  942. return -ENOMEM;
  943. handler->query_bit = query_bit;
  944. handler->handle = handle;
  945. handler->func = func;
  946. handler->data = data;
  947. mutex_lock(&ec->mutex);
  948. kref_init(&handler->kref);
  949. list_add(&handler->node, &ec->list);
  950. mutex_unlock(&ec->mutex);
  951. return 0;
  952. }
  953. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  954. static void acpi_ec_remove_query_handlers(struct acpi_ec *ec,
  955. bool remove_all, u8 query_bit)
  956. {
  957. struct acpi_ec_query_handler *handler, *tmp;
  958. LIST_HEAD(free_list);
  959. mutex_lock(&ec->mutex);
  960. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  961. if (remove_all || query_bit == handler->query_bit) {
  962. list_del_init(&handler->node);
  963. list_add(&handler->node, &free_list);
  964. }
  965. }
  966. mutex_unlock(&ec->mutex);
  967. list_for_each_entry_safe(handler, tmp, &free_list, node)
  968. acpi_ec_put_query_handler(handler);
  969. }
  970. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  971. {
  972. acpi_ec_remove_query_handlers(ec, false, query_bit);
  973. }
  974. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  975. static struct acpi_ec_query *acpi_ec_create_query(u8 *pval)
  976. {
  977. struct acpi_ec_query *q;
  978. struct transaction *t;
  979. q = kzalloc(sizeof (struct acpi_ec_query), GFP_KERNEL);
  980. if (!q)
  981. return NULL;
  982. INIT_WORK(&q->work, acpi_ec_event_processor);
  983. t = &q->transaction;
  984. t->command = ACPI_EC_COMMAND_QUERY;
  985. t->rdata = pval;
  986. t->rlen = 1;
  987. return q;
  988. }
  989. static void acpi_ec_delete_query(struct acpi_ec_query *q)
  990. {
  991. if (q) {
  992. if (q->handler)
  993. acpi_ec_put_query_handler(q->handler);
  994. kfree(q);
  995. }
  996. }
  997. static void acpi_ec_event_processor(struct work_struct *work)
  998. {
  999. struct acpi_ec_query *q = container_of(work, struct acpi_ec_query, work);
  1000. struct acpi_ec_query_handler *handler = q->handler;
  1001. ec_dbg_evt("Query(0x%02x) started", handler->query_bit);
  1002. if (handler->func)
  1003. handler->func(handler->data);
  1004. else if (handler->handle)
  1005. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  1006. ec_dbg_evt("Query(0x%02x) stopped", handler->query_bit);
  1007. acpi_ec_delete_query(q);
  1008. }
  1009. static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
  1010. {
  1011. u8 value = 0;
  1012. int result;
  1013. struct acpi_ec_query *q;
  1014. q = acpi_ec_create_query(&value);
  1015. if (!q)
  1016. return -ENOMEM;
  1017. /*
  1018. * Query the EC to find out which _Qxx method we need to evaluate.
  1019. * Note that successful completion of the query causes the ACPI_EC_SCI
  1020. * bit to be cleared (and thus clearing the interrupt source).
  1021. */
  1022. result = acpi_ec_transaction(ec, &q->transaction);
  1023. if (!value)
  1024. result = -ENODATA;
  1025. if (result)
  1026. goto err_exit;
  1027. q->handler = acpi_ec_get_query_handler_by_value(ec, value);
  1028. if (!q->handler) {
  1029. result = -ENODATA;
  1030. goto err_exit;
  1031. }
  1032. /*
  1033. * It is reported that _Qxx are evaluated in a parallel way on
  1034. * Windows:
  1035. * https://bugzilla.kernel.org/show_bug.cgi?id=94411
  1036. *
  1037. * Put this log entry before schedule_work() in order to make
  1038. * it appearing before any other log entries occurred during the
  1039. * work queue execution.
  1040. */
  1041. ec_dbg_evt("Query(0x%02x) scheduled", value);
  1042. if (!queue_work(ec_query_wq, &q->work)) {
  1043. ec_dbg_evt("Query(0x%02x) overlapped", value);
  1044. result = -EBUSY;
  1045. }
  1046. err_exit:
  1047. if (result)
  1048. acpi_ec_delete_query(q);
  1049. if (data)
  1050. *data = value;
  1051. return result;
  1052. }
  1053. static void acpi_ec_check_event(struct acpi_ec *ec)
  1054. {
  1055. unsigned long flags;
  1056. if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT) {
  1057. if (ec_guard(ec)) {
  1058. spin_lock_irqsave(&ec->lock, flags);
  1059. /*
  1060. * Take care of the SCI_EVT unless no one else is
  1061. * taking care of it.
  1062. */
  1063. if (!ec->curr)
  1064. advance_transaction(ec);
  1065. spin_unlock_irqrestore(&ec->lock, flags);
  1066. }
  1067. }
  1068. }
  1069. static void acpi_ec_event_handler(struct work_struct *work)
  1070. {
  1071. unsigned long flags;
  1072. struct acpi_ec *ec = container_of(work, struct acpi_ec, work);
  1073. ec_dbg_evt("Event started");
  1074. spin_lock_irqsave(&ec->lock, flags);
  1075. while (ec->nr_pending_queries) {
  1076. spin_unlock_irqrestore(&ec->lock, flags);
  1077. (void)acpi_ec_query(ec, NULL);
  1078. spin_lock_irqsave(&ec->lock, flags);
  1079. ec->nr_pending_queries--;
  1080. /*
  1081. * Before exit, make sure that this work item can be
  1082. * scheduled again. There might be QR_EC failures, leaving
  1083. * EC_FLAGS_QUERY_PENDING uncleared and preventing this work
  1084. * item from being scheduled again.
  1085. */
  1086. if (!ec->nr_pending_queries) {
  1087. if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS ||
  1088. ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY)
  1089. acpi_ec_complete_query(ec);
  1090. }
  1091. }
  1092. spin_unlock_irqrestore(&ec->lock, flags);
  1093. ec_dbg_evt("Event stopped");
  1094. acpi_ec_check_event(ec);
  1095. }
  1096. static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
  1097. u32 gpe_number, void *data)
  1098. {
  1099. unsigned long flags;
  1100. struct acpi_ec *ec = data;
  1101. spin_lock_irqsave(&ec->lock, flags);
  1102. advance_transaction(ec);
  1103. spin_unlock_irqrestore(&ec->lock, flags);
  1104. return ACPI_INTERRUPT_HANDLED;
  1105. }
  1106. /* --------------------------------------------------------------------------
  1107. * Address Space Management
  1108. * -------------------------------------------------------------------------- */
  1109. static acpi_status
  1110. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  1111. u32 bits, u64 *value64,
  1112. void *handler_context, void *region_context)
  1113. {
  1114. struct acpi_ec *ec = handler_context;
  1115. int result = 0, i, bytes = bits / 8;
  1116. u8 *value = (u8 *)value64;
  1117. if ((address > 0xFF) || !value || !handler_context)
  1118. return AE_BAD_PARAMETER;
  1119. if (function != ACPI_READ && function != ACPI_WRITE)
  1120. return AE_BAD_PARAMETER;
  1121. if (ec->busy_polling || bits > 8)
  1122. acpi_ec_burst_enable(ec);
  1123. for (i = 0; i < bytes; ++i, ++address, ++value)
  1124. result = (function == ACPI_READ) ?
  1125. acpi_ec_read(ec, address, value) :
  1126. acpi_ec_write(ec, address, *value);
  1127. if (ec->busy_polling || bits > 8)
  1128. acpi_ec_burst_disable(ec);
  1129. switch (result) {
  1130. case -EINVAL:
  1131. return AE_BAD_PARAMETER;
  1132. case -ENODEV:
  1133. return AE_NOT_FOUND;
  1134. case -ETIME:
  1135. return AE_TIME;
  1136. default:
  1137. return AE_OK;
  1138. }
  1139. }
  1140. /* --------------------------------------------------------------------------
  1141. * Driver Interface
  1142. * -------------------------------------------------------------------------- */
  1143. static acpi_status
  1144. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  1145. static void acpi_ec_free(struct acpi_ec *ec)
  1146. {
  1147. if (first_ec == ec)
  1148. first_ec = NULL;
  1149. if (boot_ec == ec)
  1150. boot_ec = NULL;
  1151. kfree(ec);
  1152. }
  1153. static struct acpi_ec *acpi_ec_alloc(void)
  1154. {
  1155. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  1156. if (!ec)
  1157. return NULL;
  1158. mutex_init(&ec->mutex);
  1159. init_waitqueue_head(&ec->wait);
  1160. INIT_LIST_HEAD(&ec->list);
  1161. spin_lock_init(&ec->lock);
  1162. INIT_WORK(&ec->work, acpi_ec_event_handler);
  1163. ec->timestamp = jiffies;
  1164. ec->busy_polling = true;
  1165. ec->polling_guard = 0;
  1166. return ec;
  1167. }
  1168. static acpi_status
  1169. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  1170. void *context, void **return_value)
  1171. {
  1172. char node_name[5];
  1173. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  1174. struct acpi_ec *ec = context;
  1175. int value = 0;
  1176. acpi_status status;
  1177. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  1178. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1)
  1179. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  1180. return AE_OK;
  1181. }
  1182. static acpi_status
  1183. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  1184. {
  1185. acpi_status status;
  1186. unsigned long long tmp = 0;
  1187. struct acpi_ec *ec = context;
  1188. /* clear addr values, ec_parse_io_ports depend on it */
  1189. ec->command_addr = ec->data_addr = 0;
  1190. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1191. ec_parse_io_ports, ec);
  1192. if (ACPI_FAILURE(status))
  1193. return status;
  1194. /* Get GPE bit assignment (EC events). */
  1195. /* TODO: Add support for _GPE returning a package */
  1196. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  1197. if (ACPI_FAILURE(status))
  1198. return status;
  1199. ec->gpe = tmp;
  1200. /* Use the global lock for all EC transactions? */
  1201. tmp = 0;
  1202. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  1203. ec->global_lock = tmp;
  1204. ec->handle = handle;
  1205. return AE_CTRL_TERMINATE;
  1206. }
  1207. /*
  1208. * Note: This function returns an error code only when the address space
  1209. * handler is not installed, which means "not able to handle
  1210. * transactions".
  1211. */
  1212. static int ec_install_handlers(struct acpi_ec *ec, bool handle_events)
  1213. {
  1214. acpi_status status;
  1215. acpi_ec_start(ec, false);
  1216. if (!test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) {
  1217. acpi_ec_enter_noirq(ec);
  1218. status = acpi_install_address_space_handler(ec->handle,
  1219. ACPI_ADR_SPACE_EC,
  1220. &acpi_ec_space_handler,
  1221. NULL, ec);
  1222. if (ACPI_FAILURE(status)) {
  1223. if (status == AE_NOT_FOUND) {
  1224. /*
  1225. * Maybe OS fails in evaluating the _REG
  1226. * object. The AE_NOT_FOUND error will be
  1227. * ignored and OS * continue to initialize
  1228. * EC.
  1229. */
  1230. pr_err("Fail in evaluating the _REG object"
  1231. " of EC device. Broken bios is suspected.\n");
  1232. } else {
  1233. acpi_ec_stop(ec, false);
  1234. return -ENODEV;
  1235. }
  1236. }
  1237. set_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags);
  1238. }
  1239. if (!handle_events)
  1240. return 0;
  1241. if (!test_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags)) {
  1242. /* Find and register all query methods */
  1243. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  1244. acpi_ec_register_query_methods,
  1245. NULL, ec, NULL);
  1246. set_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags);
  1247. }
  1248. if (!test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags)) {
  1249. status = acpi_install_gpe_raw_handler(NULL, ec->gpe,
  1250. ACPI_GPE_EDGE_TRIGGERED,
  1251. &acpi_ec_gpe_handler, ec);
  1252. /* This is not fatal as we can poll EC events */
  1253. if (ACPI_SUCCESS(status)) {
  1254. set_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags);
  1255. acpi_ec_leave_noirq(ec);
  1256. if (test_bit(EC_FLAGS_STARTED, &ec->flags) &&
  1257. ec->reference_count >= 1)
  1258. acpi_ec_enable_gpe(ec, true);
  1259. /* EC is fully operational, allow queries */
  1260. acpi_ec_enable_event(ec);
  1261. }
  1262. }
  1263. return 0;
  1264. }
  1265. static void ec_remove_handlers(struct acpi_ec *ec)
  1266. {
  1267. if (test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) {
  1268. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  1269. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  1270. pr_err("failed to remove space handler\n");
  1271. clear_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags);
  1272. }
  1273. /*
  1274. * Stops handling the EC transactions after removing the operation
  1275. * region handler. This is required because _REG(DISCONNECT)
  1276. * invoked during the removal can result in new EC transactions.
  1277. *
  1278. * Flushes the EC requests and thus disables the GPE before
  1279. * removing the GPE handler. This is required by the current ACPICA
  1280. * GPE core. ACPICA GPE core will automatically disable a GPE when
  1281. * it is indicated but there is no way to handle it. So the drivers
  1282. * must disable the GPEs prior to removing the GPE handlers.
  1283. */
  1284. acpi_ec_stop(ec, false);
  1285. if (test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags)) {
  1286. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  1287. &acpi_ec_gpe_handler)))
  1288. pr_err("failed to remove gpe handler\n");
  1289. clear_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags);
  1290. }
  1291. if (test_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags)) {
  1292. acpi_ec_remove_query_handlers(ec, true, 0);
  1293. clear_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags);
  1294. }
  1295. }
  1296. static int acpi_ec_setup(struct acpi_ec *ec, bool handle_events)
  1297. {
  1298. int ret;
  1299. ret = ec_install_handlers(ec, handle_events);
  1300. if (ret)
  1301. return ret;
  1302. /* First EC capable of handling transactions */
  1303. if (!first_ec) {
  1304. first_ec = ec;
  1305. acpi_handle_info(first_ec->handle, "Used as first EC\n");
  1306. }
  1307. acpi_handle_info(ec->handle,
  1308. "GPE=0x%lx, EC_CMD/EC_SC=0x%lx, EC_DATA=0x%lx\n",
  1309. ec->gpe, ec->command_addr, ec->data_addr);
  1310. return ret;
  1311. }
  1312. static int acpi_config_boot_ec(struct acpi_ec *ec, acpi_handle handle,
  1313. bool handle_events, bool is_ecdt)
  1314. {
  1315. int ret;
  1316. /*
  1317. * Changing the ACPI handle results in a re-configuration of the
  1318. * boot EC. And if it happens after the namespace initialization,
  1319. * it causes _REG evaluations.
  1320. */
  1321. if (boot_ec && boot_ec->handle != handle)
  1322. ec_remove_handlers(boot_ec);
  1323. /* Unset old boot EC */
  1324. if (boot_ec != ec)
  1325. acpi_ec_free(boot_ec);
  1326. /*
  1327. * ECDT device creation is split into acpi_ec_ecdt_probe() and
  1328. * acpi_ec_ecdt_start(). This function takes care of completing the
  1329. * ECDT parsing logic as the handle update should be performed
  1330. * between the installation/uninstallation of the handlers.
  1331. */
  1332. if (ec->handle != handle)
  1333. ec->handle = handle;
  1334. ret = acpi_ec_setup(ec, handle_events);
  1335. if (ret)
  1336. return ret;
  1337. /* Set new boot EC */
  1338. if (!boot_ec) {
  1339. boot_ec = ec;
  1340. boot_ec_is_ecdt = is_ecdt;
  1341. }
  1342. acpi_handle_info(boot_ec->handle,
  1343. "Used as boot %s EC to handle transactions%s\n",
  1344. is_ecdt ? "ECDT" : "DSDT",
  1345. handle_events ? " and events" : "");
  1346. return ret;
  1347. }
  1348. static bool acpi_ec_ecdt_get_handle(acpi_handle *phandle)
  1349. {
  1350. struct acpi_table_ecdt *ecdt_ptr;
  1351. acpi_status status;
  1352. acpi_handle handle;
  1353. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  1354. (struct acpi_table_header **)&ecdt_ptr);
  1355. if (ACPI_FAILURE(status))
  1356. return false;
  1357. status = acpi_get_handle(NULL, ecdt_ptr->id, &handle);
  1358. if (ACPI_FAILURE(status))
  1359. return false;
  1360. *phandle = handle;
  1361. return true;
  1362. }
  1363. static bool acpi_is_boot_ec(struct acpi_ec *ec)
  1364. {
  1365. if (!boot_ec)
  1366. return false;
  1367. if (ec->handle == boot_ec->handle &&
  1368. ec->gpe == boot_ec->gpe &&
  1369. ec->command_addr == boot_ec->command_addr &&
  1370. ec->data_addr == boot_ec->data_addr)
  1371. return true;
  1372. return false;
  1373. }
  1374. static int acpi_ec_add(struct acpi_device *device)
  1375. {
  1376. struct acpi_ec *ec = NULL;
  1377. int ret;
  1378. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  1379. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  1380. ec = acpi_ec_alloc();
  1381. if (!ec)
  1382. return -ENOMEM;
  1383. if (ec_parse_device(device->handle, 0, ec, NULL) !=
  1384. AE_CTRL_TERMINATE) {
  1385. ret = -EINVAL;
  1386. goto err_alloc;
  1387. }
  1388. if (acpi_is_boot_ec(ec)) {
  1389. boot_ec_is_ecdt = false;
  1390. acpi_handle_debug(ec->handle, "duplicated.\n");
  1391. acpi_ec_free(ec);
  1392. ec = boot_ec;
  1393. ret = acpi_config_boot_ec(ec, ec->handle, true, false);
  1394. } else
  1395. ret = acpi_ec_setup(ec, true);
  1396. if (ret)
  1397. goto err_query;
  1398. device->driver_data = ec;
  1399. ret = !!request_region(ec->data_addr, 1, "EC data");
  1400. WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
  1401. ret = !!request_region(ec->command_addr, 1, "EC cmd");
  1402. WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
  1403. /* Reprobe devices depending on the EC */
  1404. acpi_walk_dep_device_list(ec->handle);
  1405. acpi_handle_debug(ec->handle, "enumerated.\n");
  1406. return 0;
  1407. err_query:
  1408. if (ec != boot_ec)
  1409. acpi_ec_remove_query_handlers(ec, true, 0);
  1410. err_alloc:
  1411. if (ec != boot_ec)
  1412. acpi_ec_free(ec);
  1413. return ret;
  1414. }
  1415. static int acpi_ec_remove(struct acpi_device *device)
  1416. {
  1417. struct acpi_ec *ec;
  1418. if (!device)
  1419. return -EINVAL;
  1420. ec = acpi_driver_data(device);
  1421. release_region(ec->data_addr, 1);
  1422. release_region(ec->command_addr, 1);
  1423. device->driver_data = NULL;
  1424. if (ec != boot_ec) {
  1425. ec_remove_handlers(ec);
  1426. acpi_ec_free(ec);
  1427. }
  1428. return 0;
  1429. }
  1430. static acpi_status
  1431. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  1432. {
  1433. struct acpi_ec *ec = context;
  1434. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  1435. return AE_OK;
  1436. /*
  1437. * The first address region returned is the data port, and
  1438. * the second address region returned is the status/command
  1439. * port.
  1440. */
  1441. if (ec->data_addr == 0)
  1442. ec->data_addr = resource->data.io.minimum;
  1443. else if (ec->command_addr == 0)
  1444. ec->command_addr = resource->data.io.minimum;
  1445. else
  1446. return AE_CTRL_TERMINATE;
  1447. return AE_OK;
  1448. }
  1449. static const struct acpi_device_id ec_device_ids[] = {
  1450. {"PNP0C09", 0},
  1451. {"", 0},
  1452. };
  1453. int __init acpi_ec_dsdt_probe(void)
  1454. {
  1455. acpi_status status;
  1456. struct acpi_ec *ec;
  1457. int ret;
  1458. ec = acpi_ec_alloc();
  1459. if (!ec)
  1460. return -ENOMEM;
  1461. /*
  1462. * At this point, the namespace is initialized, so start to find
  1463. * the namespace objects.
  1464. */
  1465. status = acpi_get_devices(ec_device_ids[0].id,
  1466. ec_parse_device, ec, NULL);
  1467. if (ACPI_FAILURE(status) || !ec->handle) {
  1468. ret = -ENODEV;
  1469. goto error;
  1470. }
  1471. /*
  1472. * When the DSDT EC is available, always re-configure boot EC to
  1473. * have _REG evaluated. _REG can only be evaluated after the
  1474. * namespace initialization.
  1475. * At this point, the GPE is not fully initialized, so do not to
  1476. * handle the events.
  1477. */
  1478. ret = acpi_config_boot_ec(ec, ec->handle, false, false);
  1479. error:
  1480. if (ret)
  1481. acpi_ec_free(ec);
  1482. return ret;
  1483. }
  1484. /*
  1485. * If the DSDT EC is not functioning, we still need to prepare a fully
  1486. * functioning ECDT EC first in order to handle the events.
  1487. * https://bugzilla.kernel.org/show_bug.cgi?id=115021
  1488. */
  1489. int __init acpi_ec_ecdt_start(void)
  1490. {
  1491. acpi_handle handle;
  1492. if (!boot_ec)
  1493. return -ENODEV;
  1494. /*
  1495. * The DSDT EC should have already been started in
  1496. * acpi_ec_add().
  1497. */
  1498. if (!boot_ec_is_ecdt)
  1499. return -ENODEV;
  1500. /*
  1501. * At this point, the namespace and the GPE is initialized, so
  1502. * start to find the namespace objects and handle the events.
  1503. */
  1504. if (!acpi_ec_ecdt_get_handle(&handle))
  1505. return -ENODEV;
  1506. return acpi_config_boot_ec(boot_ec, handle, true, true);
  1507. }
  1508. #if 0
  1509. /*
  1510. * Some EC firmware variations refuses to respond QR_EC when SCI_EVT is not
  1511. * set, for which case, we complete the QR_EC without issuing it to the
  1512. * firmware.
  1513. * https://bugzilla.kernel.org/show_bug.cgi?id=82611
  1514. * https://bugzilla.kernel.org/show_bug.cgi?id=97381
  1515. */
  1516. static int ec_flag_query_handshake(const struct dmi_system_id *id)
  1517. {
  1518. pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
  1519. EC_FLAGS_QUERY_HANDSHAKE = 1;
  1520. return 0;
  1521. }
  1522. #endif
  1523. /*
  1524. * Some ECDTs contain wrong register addresses.
  1525. * MSI MS-171F
  1526. * https://bugzilla.kernel.org/show_bug.cgi?id=12461
  1527. */
  1528. static int ec_correct_ecdt(const struct dmi_system_id *id)
  1529. {
  1530. pr_debug("Detected system needing ECDT address correction.\n");
  1531. EC_FLAGS_CORRECT_ECDT = 1;
  1532. return 0;
  1533. }
  1534. static struct dmi_system_id ec_dmi_table[] __initdata = {
  1535. {
  1536. ec_correct_ecdt, "MSI MS-171F", {
  1537. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star"),
  1538. DMI_MATCH(DMI_PRODUCT_NAME, "MS-171F"),}, NULL},
  1539. {},
  1540. };
  1541. int __init acpi_ec_ecdt_probe(void)
  1542. {
  1543. int ret;
  1544. acpi_status status;
  1545. struct acpi_table_ecdt *ecdt_ptr;
  1546. struct acpi_ec *ec;
  1547. ec = acpi_ec_alloc();
  1548. if (!ec)
  1549. return -ENOMEM;
  1550. /*
  1551. * Generate a boot ec context
  1552. */
  1553. dmi_check_system(ec_dmi_table);
  1554. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  1555. (struct acpi_table_header **)&ecdt_ptr);
  1556. if (ACPI_FAILURE(status)) {
  1557. ret = -ENODEV;
  1558. goto error;
  1559. }
  1560. if (!ecdt_ptr->control.address || !ecdt_ptr->data.address) {
  1561. /*
  1562. * Asus X50GL:
  1563. * https://bugzilla.kernel.org/show_bug.cgi?id=11880
  1564. */
  1565. ret = -ENODEV;
  1566. goto error;
  1567. }
  1568. if (EC_FLAGS_CORRECT_ECDT) {
  1569. ec->command_addr = ecdt_ptr->data.address;
  1570. ec->data_addr = ecdt_ptr->control.address;
  1571. } else {
  1572. ec->command_addr = ecdt_ptr->control.address;
  1573. ec->data_addr = ecdt_ptr->data.address;
  1574. }
  1575. ec->gpe = ecdt_ptr->gpe;
  1576. /*
  1577. * At this point, the namespace is not initialized, so do not find
  1578. * the namespace objects, or handle the events.
  1579. */
  1580. ret = acpi_config_boot_ec(ec, ACPI_ROOT_OBJECT, false, true);
  1581. error:
  1582. if (ret)
  1583. acpi_ec_free(ec);
  1584. return ret;
  1585. }
  1586. #ifdef CONFIG_PM_SLEEP
  1587. static int acpi_ec_suspend_noirq(struct device *dev)
  1588. {
  1589. struct acpi_ec *ec =
  1590. acpi_driver_data(to_acpi_device(dev));
  1591. acpi_ec_enter_noirq(ec);
  1592. return 0;
  1593. }
  1594. static int acpi_ec_resume_noirq(struct device *dev)
  1595. {
  1596. struct acpi_ec *ec =
  1597. acpi_driver_data(to_acpi_device(dev));
  1598. acpi_ec_leave_noirq(ec);
  1599. return 0;
  1600. }
  1601. static int acpi_ec_suspend(struct device *dev)
  1602. {
  1603. struct acpi_ec *ec =
  1604. acpi_driver_data(to_acpi_device(dev));
  1605. if (ec_freeze_events)
  1606. acpi_ec_disable_event(ec);
  1607. return 0;
  1608. }
  1609. static int acpi_ec_resume(struct device *dev)
  1610. {
  1611. struct acpi_ec *ec =
  1612. acpi_driver_data(to_acpi_device(dev));
  1613. acpi_ec_enable_event(ec);
  1614. return 0;
  1615. }
  1616. #endif
  1617. static const struct dev_pm_ops acpi_ec_pm = {
  1618. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend_noirq, acpi_ec_resume_noirq)
  1619. SET_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend, acpi_ec_resume)
  1620. };
  1621. static int param_set_event_clearing(const char *val, struct kernel_param *kp)
  1622. {
  1623. int result = 0;
  1624. if (!strncmp(val, "status", sizeof("status") - 1)) {
  1625. ec_event_clearing = ACPI_EC_EVT_TIMING_STATUS;
  1626. pr_info("Assuming SCI_EVT clearing on EC_SC accesses\n");
  1627. } else if (!strncmp(val, "query", sizeof("query") - 1)) {
  1628. ec_event_clearing = ACPI_EC_EVT_TIMING_QUERY;
  1629. pr_info("Assuming SCI_EVT clearing on QR_EC writes\n");
  1630. } else if (!strncmp(val, "event", sizeof("event") - 1)) {
  1631. ec_event_clearing = ACPI_EC_EVT_TIMING_EVENT;
  1632. pr_info("Assuming SCI_EVT clearing on event reads\n");
  1633. } else
  1634. result = -EINVAL;
  1635. return result;
  1636. }
  1637. static int param_get_event_clearing(char *buffer, struct kernel_param *kp)
  1638. {
  1639. switch (ec_event_clearing) {
  1640. case ACPI_EC_EVT_TIMING_STATUS:
  1641. return sprintf(buffer, "status");
  1642. case ACPI_EC_EVT_TIMING_QUERY:
  1643. return sprintf(buffer, "query");
  1644. case ACPI_EC_EVT_TIMING_EVENT:
  1645. return sprintf(buffer, "event");
  1646. default:
  1647. return sprintf(buffer, "invalid");
  1648. }
  1649. return 0;
  1650. }
  1651. module_param_call(ec_event_clearing, param_set_event_clearing, param_get_event_clearing,
  1652. NULL, 0644);
  1653. MODULE_PARM_DESC(ec_event_clearing, "Assumed SCI_EVT clearing timing");
  1654. static struct acpi_driver acpi_ec_driver = {
  1655. .name = "ec",
  1656. .class = ACPI_EC_CLASS,
  1657. .ids = ec_device_ids,
  1658. .ops = {
  1659. .add = acpi_ec_add,
  1660. .remove = acpi_ec_remove,
  1661. },
  1662. .drv.pm = &acpi_ec_pm,
  1663. };
  1664. static inline int acpi_ec_query_init(void)
  1665. {
  1666. if (!ec_query_wq) {
  1667. ec_query_wq = alloc_workqueue("kec_query", 0,
  1668. ec_max_queries);
  1669. if (!ec_query_wq)
  1670. return -ENODEV;
  1671. }
  1672. return 0;
  1673. }
  1674. static inline void acpi_ec_query_exit(void)
  1675. {
  1676. if (ec_query_wq) {
  1677. destroy_workqueue(ec_query_wq);
  1678. ec_query_wq = NULL;
  1679. }
  1680. }
  1681. int __init acpi_ec_init(void)
  1682. {
  1683. int result;
  1684. /* register workqueue for _Qxx evaluations */
  1685. result = acpi_ec_query_init();
  1686. if (result)
  1687. goto err_exit;
  1688. /* Now register the driver for the EC */
  1689. result = acpi_bus_register_driver(&acpi_ec_driver);
  1690. if (result)
  1691. goto err_exit;
  1692. err_exit:
  1693. if (result)
  1694. acpi_ec_query_exit();
  1695. return result;
  1696. }
  1697. /* EC driver currently not unloadable */
  1698. #if 0
  1699. static void __exit acpi_ec_exit(void)
  1700. {
  1701. acpi_bus_unregister_driver(&acpi_ec_driver);
  1702. acpi_ec_query_exit();
  1703. }
  1704. #endif /* 0 */