ec.c 55 KB

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