ec.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ec.c - ACPI Embedded Controller Driver (v3)
  4. *
  5. * Copyright (C) 2001-2015 Intel Corporation
  6. * Author: 2014, 2015 Lv Zheng <lv.zheng@intel.com>
  7. * 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
  8. * 2006 Denis Sadykov <denis.m.sadykov@intel.com>
  9. * 2004 Luming Yu <luming.yu@intel.com>
  10. * 2001, 2002 Andy Grover <andrew.grover@intel.com>
  11. * 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  12. * Copyright (C) 2008 Alexey Starikovskiy <astarikovskiy@suse.de>
  13. */
  14. /* Uncomment next line to get verbose printout */
  15. /* #define DEBUG */
  16. #define pr_fmt(fmt) "ACPI: EC: " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/types.h>
  21. #include <linux/delay.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/list.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/slab.h>
  26. #include <linux/suspend.h>
  27. #include <linux/acpi.h>
  28. #include <linux/dmi.h>
  29. #include <asm/io.h>
  30. #include "internal.h"
  31. #define ACPI_EC_CLASS "embedded_controller"
  32. #define ACPI_EC_DEVICE_NAME "Embedded Controller"
  33. /* EC status register */
  34. #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
  35. #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
  36. #define ACPI_EC_FLAG_CMD 0x08 /* Input buffer contains a command */
  37. #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
  38. #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
  39. /*
  40. * The SCI_EVT clearing timing is not defined by the ACPI specification.
  41. * This leads to lots of practical timing issues for the host EC driver.
  42. * The following variations are defined (from the target EC firmware's
  43. * perspective):
  44. * STATUS: After indicating SCI_EVT edge triggered IRQ to the host, the
  45. * target can clear SCI_EVT at any time so long as the host can see
  46. * the indication by reading the status register (EC_SC). So the
  47. * host should re-check SCI_EVT after the first time the SCI_EVT
  48. * indication is seen, which is the same time the query request
  49. * (QR_EC) is written to the command register (EC_CMD). SCI_EVT set
  50. * at any later time could indicate another event. Normally such
  51. * kind of EC firmware has implemented an event queue and will
  52. * return 0x00 to indicate "no outstanding event".
  53. * QUERY: After seeing the query request (QR_EC) written to the command
  54. * register (EC_CMD) by the host and having prepared the responding
  55. * event value in the data register (EC_DATA), the target can safely
  56. * clear SCI_EVT because the target can confirm that the current
  57. * event is being handled by the host. The host then should check
  58. * SCI_EVT right after reading the event response from the data
  59. * register (EC_DATA).
  60. * EVENT: After seeing the event response read from the data register
  61. * (EC_DATA) by the host, the target can clear SCI_EVT. As the
  62. * target requires time to notice the change in the data register
  63. * (EC_DATA), the host may be required to wait additional guarding
  64. * time before checking the SCI_EVT again. Such guarding may not be
  65. * necessary if the host is notified via another IRQ.
  66. */
  67. #define ACPI_EC_EVT_TIMING_STATUS 0x00
  68. #define ACPI_EC_EVT_TIMING_QUERY 0x01
  69. #define ACPI_EC_EVT_TIMING_EVENT 0x02
  70. /* EC commands */
  71. enum ec_command {
  72. ACPI_EC_COMMAND_READ = 0x80,
  73. ACPI_EC_COMMAND_WRITE = 0x81,
  74. ACPI_EC_BURST_ENABLE = 0x82,
  75. ACPI_EC_BURST_DISABLE = 0x83,
  76. ACPI_EC_COMMAND_QUERY = 0x84,
  77. };
  78. #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
  79. #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
  80. #define ACPI_EC_UDELAY_POLL 550 /* Wait 1ms for EC transaction polling */
  81. #define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query
  82. * when trying to clear the EC */
  83. #define ACPI_EC_MAX_QUERIES 16 /* Maximum number of parallel queries */
  84. enum {
  85. EC_FLAGS_QUERY_ENABLED, /* Query is enabled */
  86. EC_FLAGS_QUERY_PENDING, /* Query is pending */
  87. EC_FLAGS_QUERY_GUARDING, /* Guard for SCI_EVT check */
  88. EC_FLAGS_EVENT_HANDLER_INSTALLED, /* Event handler installed */
  89. EC_FLAGS_EC_HANDLER_INSTALLED, /* OpReg handler installed */
  90. EC_FLAGS_QUERY_METHODS_INSTALLED, /* _Qxx handlers installed */
  91. EC_FLAGS_STARTED, /* Driver is started */
  92. EC_FLAGS_STOPPED, /* Driver is stopped */
  93. EC_FLAGS_EVENTS_MASKED, /* Events masked */
  94. };
  95. #define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */
  96. #define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */
  97. /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
  98. static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
  99. module_param(ec_delay, uint, 0644);
  100. MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
  101. static unsigned int ec_max_queries __read_mostly = ACPI_EC_MAX_QUERIES;
  102. module_param(ec_max_queries, uint, 0644);
  103. MODULE_PARM_DESC(ec_max_queries, "Maximum parallel _Qxx evaluations");
  104. static bool ec_busy_polling __read_mostly;
  105. module_param(ec_busy_polling, bool, 0644);
  106. MODULE_PARM_DESC(ec_busy_polling, "Use busy polling to advance EC transaction");
  107. static unsigned int ec_polling_guard __read_mostly = ACPI_EC_UDELAY_POLL;
  108. module_param(ec_polling_guard, uint, 0644);
  109. MODULE_PARM_DESC(ec_polling_guard, "Guard time(us) between EC accesses in polling modes");
  110. static unsigned int ec_event_clearing __read_mostly = ACPI_EC_EVT_TIMING_QUERY;
  111. /*
  112. * If the number of false interrupts per one transaction exceeds
  113. * this threshold, will think there is a GPE storm happened and
  114. * will disable the GPE for normal transaction.
  115. */
  116. static unsigned int ec_storm_threshold __read_mostly = 8;
  117. module_param(ec_storm_threshold, uint, 0644);
  118. MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
  119. static bool ec_freeze_events __read_mostly = false;
  120. module_param(ec_freeze_events, bool, 0644);
  121. MODULE_PARM_DESC(ec_freeze_events, "Disabling event handling during suspend/resume");
  122. static bool ec_no_wakeup __read_mostly;
  123. module_param(ec_no_wakeup, bool, 0644);
  124. MODULE_PARM_DESC(ec_no_wakeup, "Do not wake up from suspend-to-idle");
  125. struct acpi_ec_query_handler {
  126. struct list_head node;
  127. acpi_ec_query_func func;
  128. acpi_handle handle;
  129. void *data;
  130. u8 query_bit;
  131. struct kref kref;
  132. };
  133. struct transaction {
  134. const u8 *wdata;
  135. u8 *rdata;
  136. unsigned short irq_count;
  137. u8 command;
  138. u8 wi;
  139. u8 ri;
  140. u8 wlen;
  141. u8 rlen;
  142. u8 flags;
  143. };
  144. struct acpi_ec_query {
  145. struct transaction transaction;
  146. struct work_struct work;
  147. struct acpi_ec_query_handler *handler;
  148. struct acpi_ec *ec;
  149. };
  150. static int acpi_ec_query(struct acpi_ec *ec, u8 *data);
  151. static void advance_transaction(struct acpi_ec *ec);
  152. static void acpi_ec_event_handler(struct work_struct *work);
  153. static void acpi_ec_event_processor(struct work_struct *work);
  154. struct acpi_ec *first_ec;
  155. EXPORT_SYMBOL(first_ec);
  156. static struct acpi_ec *boot_ec;
  157. static bool boot_ec_is_ecdt = false;
  158. static struct workqueue_struct *ec_wq;
  159. static struct workqueue_struct *ec_query_wq;
  160. static int EC_FLAGS_CORRECT_ECDT; /* Needs ECDT port address correction */
  161. static int EC_FLAGS_IGNORE_DSDT_GPE; /* Needs ECDT GPE as correction setting */
  162. static int EC_FLAGS_TRUST_DSDT_GPE; /* Needs DSDT GPE as correction setting */
  163. static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */
  164. /* --------------------------------------------------------------------------
  165. * Logging/Debugging
  166. * -------------------------------------------------------------------------- */
  167. /*
  168. * Splitters used by the developers to track the boundary of the EC
  169. * handling processes.
  170. */
  171. #ifdef DEBUG
  172. #define EC_DBG_SEP " "
  173. #define EC_DBG_DRV "+++++"
  174. #define EC_DBG_STM "====="
  175. #define EC_DBG_REQ "*****"
  176. #define EC_DBG_EVT "#####"
  177. #else
  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. #endif
  184. #define ec_log_raw(fmt, ...) \
  185. pr_info(fmt "\n", ##__VA_ARGS__)
  186. #define ec_dbg_raw(fmt, ...) \
  187. pr_debug(fmt "\n", ##__VA_ARGS__)
  188. #define ec_log(filter, fmt, ...) \
  189. ec_log_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__)
  190. #define ec_dbg(filter, fmt, ...) \
  191. ec_dbg_raw(filter EC_DBG_SEP fmt EC_DBG_SEP filter, ##__VA_ARGS__)
  192. #define ec_log_drv(fmt, ...) \
  193. ec_log(EC_DBG_DRV, fmt, ##__VA_ARGS__)
  194. #define ec_dbg_drv(fmt, ...) \
  195. ec_dbg(EC_DBG_DRV, fmt, ##__VA_ARGS__)
  196. #define ec_dbg_stm(fmt, ...) \
  197. ec_dbg(EC_DBG_STM, fmt, ##__VA_ARGS__)
  198. #define ec_dbg_req(fmt, ...) \
  199. ec_dbg(EC_DBG_REQ, fmt, ##__VA_ARGS__)
  200. #define ec_dbg_evt(fmt, ...) \
  201. ec_dbg(EC_DBG_EVT, fmt, ##__VA_ARGS__)
  202. #define ec_dbg_ref(ec, fmt, ...) \
  203. ec_dbg_raw("%lu: " fmt, ec->reference_count, ## __VA_ARGS__)
  204. /* --------------------------------------------------------------------------
  205. * Device Flags
  206. * -------------------------------------------------------------------------- */
  207. static bool acpi_ec_started(struct acpi_ec *ec)
  208. {
  209. return test_bit(EC_FLAGS_STARTED, &ec->flags) &&
  210. !test_bit(EC_FLAGS_STOPPED, &ec->flags);
  211. }
  212. static bool acpi_ec_event_enabled(struct acpi_ec *ec)
  213. {
  214. /*
  215. * There is an OSPM early stage logic. During the early stages
  216. * (boot/resume), OSPMs shouldn't enable the event handling, only
  217. * the EC transactions are allowed to be performed.
  218. */
  219. if (!test_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags))
  220. return false;
  221. /*
  222. * However, disabling the event handling is experimental for late
  223. * stage (suspend), and is controlled by the boot parameter of
  224. * "ec_freeze_events":
  225. * 1. true: The EC event handling is disabled before entering
  226. * the noirq stage.
  227. * 2. false: The EC event handling is automatically disabled as
  228. * soon as the EC driver is stopped.
  229. */
  230. if (ec_freeze_events)
  231. return acpi_ec_started(ec);
  232. else
  233. return test_bit(EC_FLAGS_STARTED, &ec->flags);
  234. }
  235. static bool acpi_ec_flushed(struct acpi_ec *ec)
  236. {
  237. return ec->reference_count == 1;
  238. }
  239. /* --------------------------------------------------------------------------
  240. * EC Registers
  241. * -------------------------------------------------------------------------- */
  242. static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
  243. {
  244. u8 x = inb(ec->command_addr);
  245. ec_dbg_raw("EC_SC(R) = 0x%2.2x "
  246. "SCI_EVT=%d BURST=%d CMD=%d IBF=%d OBF=%d",
  247. x,
  248. !!(x & ACPI_EC_FLAG_SCI),
  249. !!(x & ACPI_EC_FLAG_BURST),
  250. !!(x & ACPI_EC_FLAG_CMD),
  251. !!(x & ACPI_EC_FLAG_IBF),
  252. !!(x & ACPI_EC_FLAG_OBF));
  253. return x;
  254. }
  255. static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
  256. {
  257. u8 x = inb(ec->data_addr);
  258. ec->timestamp = jiffies;
  259. ec_dbg_raw("EC_DATA(R) = 0x%2.2x", x);
  260. return x;
  261. }
  262. static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
  263. {
  264. ec_dbg_raw("EC_SC(W) = 0x%2.2x", command);
  265. outb(command, ec->command_addr);
  266. ec->timestamp = jiffies;
  267. }
  268. static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
  269. {
  270. ec_dbg_raw("EC_DATA(W) = 0x%2.2x", data);
  271. outb(data, ec->data_addr);
  272. ec->timestamp = jiffies;
  273. }
  274. #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
  275. static const char *acpi_ec_cmd_string(u8 cmd)
  276. {
  277. switch (cmd) {
  278. case 0x80:
  279. return "RD_EC";
  280. case 0x81:
  281. return "WR_EC";
  282. case 0x82:
  283. return "BE_EC";
  284. case 0x83:
  285. return "BD_EC";
  286. case 0x84:
  287. return "QR_EC";
  288. }
  289. return "UNKNOWN";
  290. }
  291. #else
  292. #define acpi_ec_cmd_string(cmd) "UNDEF"
  293. #endif
  294. /* --------------------------------------------------------------------------
  295. * GPE Registers
  296. * -------------------------------------------------------------------------- */
  297. static inline bool acpi_ec_is_gpe_raised(struct acpi_ec *ec)
  298. {
  299. acpi_event_status gpe_status = 0;
  300. (void)acpi_get_gpe_status(NULL, ec->gpe, &gpe_status);
  301. return (gpe_status & ACPI_EVENT_FLAG_STATUS_SET) ? true : false;
  302. }
  303. static inline void acpi_ec_enable_gpe(struct acpi_ec *ec, bool open)
  304. {
  305. if (open)
  306. acpi_enable_gpe(NULL, ec->gpe);
  307. else {
  308. BUG_ON(ec->reference_count < 1);
  309. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
  310. }
  311. if (acpi_ec_is_gpe_raised(ec)) {
  312. /*
  313. * On some platforms, EN=1 writes cannot trigger GPE. So
  314. * software need to manually trigger a pseudo GPE event on
  315. * EN=1 writes.
  316. */
  317. ec_dbg_raw("Polling quirk");
  318. advance_transaction(ec);
  319. }
  320. }
  321. static inline void acpi_ec_disable_gpe(struct acpi_ec *ec, bool close)
  322. {
  323. if (close)
  324. acpi_disable_gpe(NULL, ec->gpe);
  325. else {
  326. BUG_ON(ec->reference_count < 1);
  327. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
  328. }
  329. }
  330. static inline void acpi_ec_clear_gpe(struct acpi_ec *ec)
  331. {
  332. /*
  333. * GPE STS is a W1C register, which means:
  334. * 1. Software can clear it without worrying about clearing other
  335. * GPEs' STS bits when the hardware sets them in parallel.
  336. * 2. As long as software can ensure only clearing it when it is
  337. * set, hardware won't set it in parallel.
  338. * So software can clear GPE in any contexts.
  339. * Warning: do not move the check into advance_transaction() as the
  340. * EC commands will be sent without GPE raised.
  341. */
  342. if (!acpi_ec_is_gpe_raised(ec))
  343. return;
  344. acpi_clear_gpe(NULL, ec->gpe);
  345. }
  346. /* --------------------------------------------------------------------------
  347. * Transaction Management
  348. * -------------------------------------------------------------------------- */
  349. static void acpi_ec_submit_request(struct acpi_ec *ec)
  350. {
  351. ec->reference_count++;
  352. if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags) &&
  353. ec->gpe >= 0 && ec->reference_count == 1)
  354. acpi_ec_enable_gpe(ec, true);
  355. }
  356. static void acpi_ec_complete_request(struct acpi_ec *ec)
  357. {
  358. bool flushed = false;
  359. ec->reference_count--;
  360. if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags) &&
  361. ec->gpe >= 0 && ec->reference_count == 0)
  362. acpi_ec_disable_gpe(ec, true);
  363. flushed = acpi_ec_flushed(ec);
  364. if (flushed)
  365. wake_up(&ec->wait);
  366. }
  367. static void acpi_ec_mask_events(struct acpi_ec *ec)
  368. {
  369. if (!test_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags)) {
  370. if (ec->gpe >= 0)
  371. acpi_ec_disable_gpe(ec, false);
  372. else
  373. disable_irq_nosync(ec->irq);
  374. ec_dbg_drv("Polling enabled");
  375. set_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags);
  376. }
  377. }
  378. static void acpi_ec_unmask_events(struct acpi_ec *ec)
  379. {
  380. if (test_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags)) {
  381. clear_bit(EC_FLAGS_EVENTS_MASKED, &ec->flags);
  382. if (ec->gpe >= 0)
  383. acpi_ec_enable_gpe(ec, false);
  384. else
  385. enable_irq(ec->irq);
  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. acpi_ec_mask_events(ec);
  410. if (!acpi_ec_event_enabled(ec))
  411. return;
  412. if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
  413. ec_dbg_evt("Command(%s) submitted/blocked",
  414. acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
  415. ec->nr_pending_queries++;
  416. ec->events_in_progress++;
  417. queue_work(ec_wq, &ec->work);
  418. }
  419. }
  420. static void acpi_ec_complete_query(struct acpi_ec *ec)
  421. {
  422. if (test_and_clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
  423. ec_dbg_evt("Command(%s) unblocked",
  424. acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
  425. acpi_ec_unmask_events(ec);
  426. }
  427. static inline void __acpi_ec_enable_event(struct acpi_ec *ec)
  428. {
  429. if (!test_and_set_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags))
  430. ec_log_drv("event unblocked");
  431. /*
  432. * Unconditionally invoke this once after enabling the event
  433. * handling mechanism to detect the pending events.
  434. */
  435. advance_transaction(ec);
  436. }
  437. static inline void __acpi_ec_disable_event(struct acpi_ec *ec)
  438. {
  439. if (test_and_clear_bit(EC_FLAGS_QUERY_ENABLED, &ec->flags))
  440. ec_log_drv("event blocked");
  441. }
  442. /*
  443. * Process _Q events that might have accumulated in the EC.
  444. * Run with locked ec mutex.
  445. */
  446. static void acpi_ec_clear(struct acpi_ec *ec)
  447. {
  448. int i, status;
  449. u8 value = 0;
  450. for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
  451. status = acpi_ec_query(ec, &value);
  452. if (status || !value)
  453. break;
  454. }
  455. if (unlikely(i == ACPI_EC_CLEAR_MAX))
  456. pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
  457. else
  458. pr_info("%d stale EC events cleared\n", i);
  459. }
  460. static void acpi_ec_enable_event(struct acpi_ec *ec)
  461. {
  462. unsigned long flags;
  463. spin_lock_irqsave(&ec->lock, flags);
  464. if (acpi_ec_started(ec))
  465. __acpi_ec_enable_event(ec);
  466. spin_unlock_irqrestore(&ec->lock, flags);
  467. /* Drain additional events if hardware requires that */
  468. if (EC_FLAGS_CLEAR_ON_RESUME)
  469. acpi_ec_clear(ec);
  470. }
  471. #ifdef CONFIG_PM_SLEEP
  472. static void __acpi_ec_flush_work(void)
  473. {
  474. flush_workqueue(ec_wq); /* flush ec->work */
  475. flush_workqueue(ec_query_wq); /* flush queries */
  476. }
  477. static void acpi_ec_disable_event(struct acpi_ec *ec)
  478. {
  479. unsigned long flags;
  480. spin_lock_irqsave(&ec->lock, flags);
  481. __acpi_ec_disable_event(ec);
  482. spin_unlock_irqrestore(&ec->lock, flags);
  483. /*
  484. * When ec_freeze_events is true, we need to flush events in
  485. * the proper position before entering the noirq stage.
  486. */
  487. __acpi_ec_flush_work();
  488. }
  489. void acpi_ec_flush_work(void)
  490. {
  491. /* Without ec_wq there is nothing to flush. */
  492. if (!ec_wq)
  493. return;
  494. __acpi_ec_flush_work();
  495. }
  496. #endif /* CONFIG_PM_SLEEP */
  497. static bool acpi_ec_guard_event(struct acpi_ec *ec)
  498. {
  499. bool guarded = true;
  500. unsigned long flags;
  501. spin_lock_irqsave(&ec->lock, flags);
  502. /*
  503. * If firmware SCI_EVT clearing timing is "event", we actually
  504. * don't know when the SCI_EVT will be cleared by firmware after
  505. * evaluating _Qxx, so we need to re-check SCI_EVT after waiting an
  506. * acceptable period.
  507. *
  508. * The guarding period begins when EC_FLAGS_QUERY_PENDING is
  509. * flagged, which means SCI_EVT check has just been performed.
  510. * But if the current transaction is ACPI_EC_COMMAND_QUERY, the
  511. * guarding should have already been performed (via
  512. * EC_FLAGS_QUERY_GUARDING) and should not be applied so that the
  513. * ACPI_EC_COMMAND_QUERY transaction can be transitioned into
  514. * ACPI_EC_COMMAND_POLL state immediately.
  515. */
  516. if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS ||
  517. ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY ||
  518. !test_bit(EC_FLAGS_QUERY_PENDING, &ec->flags) ||
  519. (ec->curr && ec->curr->command == ACPI_EC_COMMAND_QUERY))
  520. guarded = false;
  521. spin_unlock_irqrestore(&ec->lock, flags);
  522. return guarded;
  523. }
  524. static int ec_transaction_polled(struct acpi_ec *ec)
  525. {
  526. unsigned long flags;
  527. int ret = 0;
  528. spin_lock_irqsave(&ec->lock, flags);
  529. if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_POLL))
  530. ret = 1;
  531. spin_unlock_irqrestore(&ec->lock, flags);
  532. return ret;
  533. }
  534. static int ec_transaction_completed(struct acpi_ec *ec)
  535. {
  536. unsigned long flags;
  537. int ret = 0;
  538. spin_lock_irqsave(&ec->lock, flags);
  539. if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
  540. ret = 1;
  541. spin_unlock_irqrestore(&ec->lock, flags);
  542. return ret;
  543. }
  544. static inline void ec_transaction_transition(struct acpi_ec *ec, unsigned long flag)
  545. {
  546. ec->curr->flags |= flag;
  547. if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
  548. if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS &&
  549. flag == ACPI_EC_COMMAND_POLL)
  550. acpi_ec_complete_query(ec);
  551. if (ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY &&
  552. flag == ACPI_EC_COMMAND_COMPLETE)
  553. acpi_ec_complete_query(ec);
  554. if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT &&
  555. flag == ACPI_EC_COMMAND_COMPLETE)
  556. set_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags);
  557. }
  558. }
  559. static void advance_transaction(struct acpi_ec *ec)
  560. {
  561. struct transaction *t;
  562. u8 status;
  563. bool wakeup = false;
  564. ec_dbg_stm("%s (%d)", in_interrupt() ? "IRQ" : "TASK",
  565. smp_processor_id());
  566. /*
  567. * By always clearing STS before handling all indications, we can
  568. * ensure a hardware STS 0->1 change after this clearing can always
  569. * trigger a GPE interrupt.
  570. */
  571. if (ec->gpe >= 0)
  572. acpi_ec_clear_gpe(ec);
  573. status = acpi_ec_read_status(ec);
  574. t = ec->curr;
  575. /*
  576. * Another IRQ or a guarded polling mode advancement is detected,
  577. * the next QR_EC submission is then allowed.
  578. */
  579. if (!t || !(t->flags & ACPI_EC_COMMAND_POLL)) {
  580. if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT &&
  581. (!ec->nr_pending_queries ||
  582. test_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags))) {
  583. clear_bit(EC_FLAGS_QUERY_GUARDING, &ec->flags);
  584. acpi_ec_complete_query(ec);
  585. }
  586. }
  587. if (!t)
  588. goto err;
  589. if (t->flags & ACPI_EC_COMMAND_POLL) {
  590. if (t->wlen > t->wi) {
  591. if ((status & ACPI_EC_FLAG_IBF) == 0)
  592. acpi_ec_write_data(ec, t->wdata[t->wi++]);
  593. else
  594. goto err;
  595. } else if (t->rlen > t->ri) {
  596. if ((status & ACPI_EC_FLAG_OBF) == 1) {
  597. t->rdata[t->ri++] = acpi_ec_read_data(ec);
  598. if (t->rlen == t->ri) {
  599. ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE);
  600. if (t->command == ACPI_EC_COMMAND_QUERY)
  601. ec_dbg_evt("Command(%s) completed by hardware",
  602. acpi_ec_cmd_string(ACPI_EC_COMMAND_QUERY));
  603. wakeup = true;
  604. }
  605. } else
  606. goto err;
  607. } else if (t->wlen == t->wi &&
  608. (status & ACPI_EC_FLAG_IBF) == 0) {
  609. ec_transaction_transition(ec, ACPI_EC_COMMAND_COMPLETE);
  610. wakeup = true;
  611. }
  612. goto out;
  613. } else if (!(status & ACPI_EC_FLAG_IBF)) {
  614. acpi_ec_write_cmd(ec, t->command);
  615. ec_transaction_transition(ec, ACPI_EC_COMMAND_POLL);
  616. goto out;
  617. }
  618. err:
  619. /*
  620. * If SCI bit is set, then don't think it's a false IRQ
  621. * otherwise will take a not handled IRQ as a false one.
  622. */
  623. if (!(status & ACPI_EC_FLAG_SCI)) {
  624. if (in_interrupt() && t) {
  625. if (t->irq_count < ec_storm_threshold)
  626. ++t->irq_count;
  627. /* Allow triggering on 0 threshold */
  628. if (t->irq_count == ec_storm_threshold)
  629. acpi_ec_mask_events(ec);
  630. }
  631. }
  632. out:
  633. if (status & ACPI_EC_FLAG_SCI)
  634. acpi_ec_submit_query(ec);
  635. if (wakeup && in_interrupt())
  636. wake_up(&ec->wait);
  637. }
  638. static void start_transaction(struct acpi_ec *ec)
  639. {
  640. ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
  641. ec->curr->flags = 0;
  642. }
  643. static int ec_guard(struct acpi_ec *ec)
  644. {
  645. unsigned long guard = usecs_to_jiffies(ec->polling_guard);
  646. unsigned long timeout = ec->timestamp + guard;
  647. /* Ensure guarding period before polling EC status */
  648. do {
  649. if (ec->busy_polling) {
  650. /* Perform busy polling */
  651. if (ec_transaction_completed(ec))
  652. return 0;
  653. udelay(jiffies_to_usecs(guard));
  654. } else {
  655. /*
  656. * Perform wait polling
  657. * 1. Wait the transaction to be completed by the
  658. * GPE handler after the transaction enters
  659. * ACPI_EC_COMMAND_POLL state.
  660. * 2. A special guarding logic is also required
  661. * for event clearing mode "event" before the
  662. * transaction enters ACPI_EC_COMMAND_POLL
  663. * state.
  664. */
  665. if (!ec_transaction_polled(ec) &&
  666. !acpi_ec_guard_event(ec))
  667. break;
  668. if (wait_event_timeout(ec->wait,
  669. ec_transaction_completed(ec),
  670. guard))
  671. return 0;
  672. }
  673. } while (time_before(jiffies, timeout));
  674. return -ETIME;
  675. }
  676. static int ec_poll(struct acpi_ec *ec)
  677. {
  678. unsigned long flags;
  679. int repeat = 5; /* number of command restarts */
  680. while (repeat--) {
  681. unsigned long delay = jiffies +
  682. msecs_to_jiffies(ec_delay);
  683. do {
  684. if (!ec_guard(ec))
  685. return 0;
  686. spin_lock_irqsave(&ec->lock, flags);
  687. advance_transaction(ec);
  688. spin_unlock_irqrestore(&ec->lock, flags);
  689. } while (time_before(jiffies, delay));
  690. pr_debug("controller reset, restart transaction\n");
  691. spin_lock_irqsave(&ec->lock, flags);
  692. start_transaction(ec);
  693. spin_unlock_irqrestore(&ec->lock, flags);
  694. }
  695. return -ETIME;
  696. }
  697. static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
  698. struct transaction *t)
  699. {
  700. unsigned long tmp;
  701. int ret = 0;
  702. /* start transaction */
  703. spin_lock_irqsave(&ec->lock, tmp);
  704. /* Enable GPE for command processing (IBF=0/OBF=1) */
  705. if (!acpi_ec_submit_flushable_request(ec)) {
  706. ret = -EINVAL;
  707. goto unlock;
  708. }
  709. ec_dbg_ref(ec, "Increase command");
  710. /* following two actions should be kept atomic */
  711. ec->curr = t;
  712. ec_dbg_req("Command(%s) started", acpi_ec_cmd_string(t->command));
  713. start_transaction(ec);
  714. spin_unlock_irqrestore(&ec->lock, tmp);
  715. ret = ec_poll(ec);
  716. spin_lock_irqsave(&ec->lock, tmp);
  717. if (t->irq_count == ec_storm_threshold)
  718. acpi_ec_unmask_events(ec);
  719. ec_dbg_req("Command(%s) stopped", acpi_ec_cmd_string(t->command));
  720. ec->curr = NULL;
  721. /* Disable GPE for command processing (IBF=0/OBF=1) */
  722. acpi_ec_complete_request(ec);
  723. ec_dbg_ref(ec, "Decrease command");
  724. unlock:
  725. spin_unlock_irqrestore(&ec->lock, tmp);
  726. return ret;
  727. }
  728. static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
  729. {
  730. int status;
  731. u32 glk;
  732. if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
  733. return -EINVAL;
  734. if (t->rdata)
  735. memset(t->rdata, 0, t->rlen);
  736. mutex_lock(&ec->mutex);
  737. if (ec->global_lock) {
  738. status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
  739. if (ACPI_FAILURE(status)) {
  740. status = -ENODEV;
  741. goto unlock;
  742. }
  743. }
  744. status = acpi_ec_transaction_unlocked(ec, t);
  745. if (ec->global_lock)
  746. acpi_release_global_lock(glk);
  747. unlock:
  748. mutex_unlock(&ec->mutex);
  749. return status;
  750. }
  751. static int acpi_ec_burst_enable(struct acpi_ec *ec)
  752. {
  753. u8 d;
  754. struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
  755. .wdata = NULL, .rdata = &d,
  756. .wlen = 0, .rlen = 1};
  757. return acpi_ec_transaction(ec, &t);
  758. }
  759. static int acpi_ec_burst_disable(struct acpi_ec *ec)
  760. {
  761. struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
  762. .wdata = NULL, .rdata = NULL,
  763. .wlen = 0, .rlen = 0};
  764. return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
  765. acpi_ec_transaction(ec, &t) : 0;
  766. }
  767. static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
  768. {
  769. int result;
  770. u8 d;
  771. struct transaction t = {.command = ACPI_EC_COMMAND_READ,
  772. .wdata = &address, .rdata = &d,
  773. .wlen = 1, .rlen = 1};
  774. result = acpi_ec_transaction(ec, &t);
  775. *data = d;
  776. return result;
  777. }
  778. static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
  779. {
  780. u8 wdata[2] = { address, data };
  781. struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
  782. .wdata = wdata, .rdata = NULL,
  783. .wlen = 2, .rlen = 0};
  784. return acpi_ec_transaction(ec, &t);
  785. }
  786. int ec_read(u8 addr, u8 *val)
  787. {
  788. int err;
  789. u8 temp_data;
  790. if (!first_ec)
  791. return -ENODEV;
  792. err = acpi_ec_read(first_ec, addr, &temp_data);
  793. if (!err) {
  794. *val = temp_data;
  795. return 0;
  796. }
  797. return err;
  798. }
  799. EXPORT_SYMBOL(ec_read);
  800. int ec_write(u8 addr, u8 val)
  801. {
  802. int err;
  803. if (!first_ec)
  804. return -ENODEV;
  805. err = acpi_ec_write(first_ec, addr, val);
  806. return err;
  807. }
  808. EXPORT_SYMBOL(ec_write);
  809. int ec_transaction(u8 command,
  810. const u8 *wdata, unsigned wdata_len,
  811. u8 *rdata, unsigned rdata_len)
  812. {
  813. struct transaction t = {.command = command,
  814. .wdata = wdata, .rdata = rdata,
  815. .wlen = wdata_len, .rlen = rdata_len};
  816. if (!first_ec)
  817. return -ENODEV;
  818. return acpi_ec_transaction(first_ec, &t);
  819. }
  820. EXPORT_SYMBOL(ec_transaction);
  821. /* Get the handle to the EC device */
  822. acpi_handle ec_get_handle(void)
  823. {
  824. if (!first_ec)
  825. return NULL;
  826. return first_ec->handle;
  827. }
  828. EXPORT_SYMBOL(ec_get_handle);
  829. static void acpi_ec_start(struct acpi_ec *ec, bool resuming)
  830. {
  831. unsigned long flags;
  832. spin_lock_irqsave(&ec->lock, flags);
  833. if (!test_and_set_bit(EC_FLAGS_STARTED, &ec->flags)) {
  834. ec_dbg_drv("Starting EC");
  835. /* Enable GPE for event processing (SCI_EVT=1) */
  836. if (!resuming) {
  837. acpi_ec_submit_request(ec);
  838. ec_dbg_ref(ec, "Increase driver");
  839. }
  840. ec_log_drv("EC started");
  841. }
  842. spin_unlock_irqrestore(&ec->lock, flags);
  843. }
  844. static bool acpi_ec_stopped(struct acpi_ec *ec)
  845. {
  846. unsigned long flags;
  847. bool flushed;
  848. spin_lock_irqsave(&ec->lock, flags);
  849. flushed = acpi_ec_flushed(ec);
  850. spin_unlock_irqrestore(&ec->lock, flags);
  851. return flushed;
  852. }
  853. static void acpi_ec_stop(struct acpi_ec *ec, bool suspending)
  854. {
  855. unsigned long flags;
  856. spin_lock_irqsave(&ec->lock, flags);
  857. if (acpi_ec_started(ec)) {
  858. ec_dbg_drv("Stopping EC");
  859. set_bit(EC_FLAGS_STOPPED, &ec->flags);
  860. spin_unlock_irqrestore(&ec->lock, flags);
  861. wait_event(ec->wait, acpi_ec_stopped(ec));
  862. spin_lock_irqsave(&ec->lock, flags);
  863. /* Disable GPE for event processing (SCI_EVT=1) */
  864. if (!suspending) {
  865. acpi_ec_complete_request(ec);
  866. ec_dbg_ref(ec, "Decrease driver");
  867. } else if (!ec_freeze_events)
  868. __acpi_ec_disable_event(ec);
  869. clear_bit(EC_FLAGS_STARTED, &ec->flags);
  870. clear_bit(EC_FLAGS_STOPPED, &ec->flags);
  871. ec_log_drv("EC stopped");
  872. }
  873. spin_unlock_irqrestore(&ec->lock, flags);
  874. }
  875. static void acpi_ec_enter_noirq(struct acpi_ec *ec)
  876. {
  877. unsigned long flags;
  878. spin_lock_irqsave(&ec->lock, flags);
  879. ec->busy_polling = true;
  880. ec->polling_guard = 0;
  881. ec_log_drv("interrupt blocked");
  882. spin_unlock_irqrestore(&ec->lock, flags);
  883. }
  884. static void acpi_ec_leave_noirq(struct acpi_ec *ec)
  885. {
  886. unsigned long flags;
  887. spin_lock_irqsave(&ec->lock, flags);
  888. ec->busy_polling = ec_busy_polling;
  889. ec->polling_guard = ec_polling_guard;
  890. ec_log_drv("interrupt unblocked");
  891. spin_unlock_irqrestore(&ec->lock, flags);
  892. }
  893. void acpi_ec_block_transactions(void)
  894. {
  895. struct acpi_ec *ec = first_ec;
  896. if (!ec)
  897. return;
  898. mutex_lock(&ec->mutex);
  899. /* Prevent transactions from being carried out */
  900. acpi_ec_stop(ec, true);
  901. mutex_unlock(&ec->mutex);
  902. }
  903. void acpi_ec_unblock_transactions(void)
  904. {
  905. /*
  906. * Allow transactions to happen again (this function is called from
  907. * atomic context during wakeup, so we don't need to acquire the mutex).
  908. */
  909. if (first_ec)
  910. acpi_ec_start(first_ec, true);
  911. }
  912. /* --------------------------------------------------------------------------
  913. Event Management
  914. -------------------------------------------------------------------------- */
  915. static struct acpi_ec_query_handler *
  916. acpi_ec_get_query_handler_by_value(struct acpi_ec *ec, u8 value)
  917. {
  918. struct acpi_ec_query_handler *handler;
  919. mutex_lock(&ec->mutex);
  920. list_for_each_entry(handler, &ec->list, node) {
  921. if (value == handler->query_bit) {
  922. kref_get(&handler->kref);
  923. mutex_unlock(&ec->mutex);
  924. return handler;
  925. }
  926. }
  927. mutex_unlock(&ec->mutex);
  928. return NULL;
  929. }
  930. static void acpi_ec_query_handler_release(struct kref *kref)
  931. {
  932. struct acpi_ec_query_handler *handler =
  933. container_of(kref, struct acpi_ec_query_handler, kref);
  934. kfree(handler);
  935. }
  936. static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler)
  937. {
  938. kref_put(&handler->kref, acpi_ec_query_handler_release);
  939. }
  940. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  941. acpi_handle handle, acpi_ec_query_func func,
  942. void *data)
  943. {
  944. struct acpi_ec_query_handler *handler =
  945. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  946. if (!handler)
  947. return -ENOMEM;
  948. handler->query_bit = query_bit;
  949. handler->handle = handle;
  950. handler->func = func;
  951. handler->data = data;
  952. mutex_lock(&ec->mutex);
  953. kref_init(&handler->kref);
  954. list_add(&handler->node, &ec->list);
  955. mutex_unlock(&ec->mutex);
  956. return 0;
  957. }
  958. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  959. static void acpi_ec_remove_query_handlers(struct acpi_ec *ec,
  960. bool remove_all, u8 query_bit)
  961. {
  962. struct acpi_ec_query_handler *handler, *tmp;
  963. LIST_HEAD(free_list);
  964. mutex_lock(&ec->mutex);
  965. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  966. if (remove_all || query_bit == handler->query_bit) {
  967. list_del_init(&handler->node);
  968. list_add(&handler->node, &free_list);
  969. }
  970. }
  971. mutex_unlock(&ec->mutex);
  972. list_for_each_entry_safe(handler, tmp, &free_list, node)
  973. acpi_ec_put_query_handler(handler);
  974. }
  975. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  976. {
  977. acpi_ec_remove_query_handlers(ec, false, query_bit);
  978. }
  979. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  980. static struct acpi_ec_query *acpi_ec_create_query(struct acpi_ec *ec, u8 *pval)
  981. {
  982. struct acpi_ec_query *q;
  983. struct transaction *t;
  984. q = kzalloc(sizeof (struct acpi_ec_query), GFP_KERNEL);
  985. if (!q)
  986. return NULL;
  987. INIT_WORK(&q->work, acpi_ec_event_processor);
  988. t = &q->transaction;
  989. t->command = ACPI_EC_COMMAND_QUERY;
  990. t->rdata = pval;
  991. t->rlen = 1;
  992. q->ec = ec;
  993. return q;
  994. }
  995. static void acpi_ec_delete_query(struct acpi_ec_query *q)
  996. {
  997. if (q) {
  998. if (q->handler)
  999. acpi_ec_put_query_handler(q->handler);
  1000. kfree(q);
  1001. }
  1002. }
  1003. static void acpi_ec_event_processor(struct work_struct *work)
  1004. {
  1005. struct acpi_ec_query *q = container_of(work, struct acpi_ec_query, work);
  1006. struct acpi_ec_query_handler *handler = q->handler;
  1007. struct acpi_ec *ec = q->ec;
  1008. ec_dbg_evt("Query(0x%02x) started", handler->query_bit);
  1009. if (handler->func)
  1010. handler->func(handler->data);
  1011. else if (handler->handle)
  1012. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  1013. ec_dbg_evt("Query(0x%02x) stopped", handler->query_bit);
  1014. spin_lock_irq(&ec->lock);
  1015. ec->queries_in_progress--;
  1016. spin_unlock_irq(&ec->lock);
  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(ec, &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 Windows:
  1044. * https://bugzilla.kernel.org/show_bug.cgi?id=94411
  1045. *
  1046. * Put this log entry before queue_work() to make it appear in the log
  1047. * before any other messages emitted during workqueue handling.
  1048. */
  1049. ec_dbg_evt("Query(0x%02x) scheduled", value);
  1050. spin_lock_irq(&ec->lock);
  1051. ec->queries_in_progress++;
  1052. queue_work(ec_query_wq, &q->work);
  1053. spin_unlock_irq(&ec->lock);
  1054. err_exit:
  1055. if (result)
  1056. acpi_ec_delete_query(q);
  1057. if (data)
  1058. *data = value;
  1059. return result;
  1060. }
  1061. static void acpi_ec_check_event(struct acpi_ec *ec)
  1062. {
  1063. unsigned long flags;
  1064. if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT) {
  1065. if (ec_guard(ec)) {
  1066. spin_lock_irqsave(&ec->lock, flags);
  1067. /*
  1068. * Take care of the SCI_EVT unless no one else is
  1069. * taking care of it.
  1070. */
  1071. if (!ec->curr)
  1072. advance_transaction(ec);
  1073. spin_unlock_irqrestore(&ec->lock, flags);
  1074. }
  1075. }
  1076. }
  1077. static void acpi_ec_event_handler(struct work_struct *work)
  1078. {
  1079. unsigned long flags;
  1080. struct acpi_ec *ec = container_of(work, struct acpi_ec, work);
  1081. ec_dbg_evt("Event started");
  1082. spin_lock_irqsave(&ec->lock, flags);
  1083. while (ec->nr_pending_queries) {
  1084. spin_unlock_irqrestore(&ec->lock, flags);
  1085. (void)acpi_ec_query(ec, NULL);
  1086. spin_lock_irqsave(&ec->lock, flags);
  1087. ec->nr_pending_queries--;
  1088. /*
  1089. * Before exit, make sure that this work item can be
  1090. * scheduled again. There might be QR_EC failures, leaving
  1091. * EC_FLAGS_QUERY_PENDING uncleared and preventing this work
  1092. * item from being scheduled again.
  1093. */
  1094. if (!ec->nr_pending_queries) {
  1095. if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS ||
  1096. ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY)
  1097. acpi_ec_complete_query(ec);
  1098. }
  1099. }
  1100. spin_unlock_irqrestore(&ec->lock, flags);
  1101. ec_dbg_evt("Event stopped");
  1102. acpi_ec_check_event(ec);
  1103. spin_lock_irqsave(&ec->lock, flags);
  1104. ec->events_in_progress--;
  1105. spin_unlock_irqrestore(&ec->lock, flags);
  1106. }
  1107. static void acpi_ec_handle_interrupt(struct acpi_ec *ec)
  1108. {
  1109. unsigned long flags;
  1110. spin_lock_irqsave(&ec->lock, flags);
  1111. advance_transaction(ec);
  1112. spin_unlock_irqrestore(&ec->lock, flags);
  1113. }
  1114. static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
  1115. u32 gpe_number, void *data)
  1116. {
  1117. acpi_ec_handle_interrupt(data);
  1118. return ACPI_INTERRUPT_HANDLED;
  1119. }
  1120. static irqreturn_t acpi_ec_irq_handler(int irq, void *data)
  1121. {
  1122. acpi_ec_handle_interrupt(data);
  1123. return IRQ_HANDLED;
  1124. }
  1125. /* --------------------------------------------------------------------------
  1126. * Address Space Management
  1127. * -------------------------------------------------------------------------- */
  1128. static acpi_status
  1129. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  1130. u32 bits, u64 *value64,
  1131. void *handler_context, void *region_context)
  1132. {
  1133. struct acpi_ec *ec = handler_context;
  1134. int result = 0, i, bytes = bits / 8;
  1135. u8 *value = (u8 *)value64;
  1136. if ((address > 0xFF) || !value || !handler_context)
  1137. return AE_BAD_PARAMETER;
  1138. if (function != ACPI_READ && function != ACPI_WRITE)
  1139. return AE_BAD_PARAMETER;
  1140. if (ec->busy_polling || bits > 8)
  1141. acpi_ec_burst_enable(ec);
  1142. for (i = 0; i < bytes; ++i, ++address, ++value)
  1143. result = (function == ACPI_READ) ?
  1144. acpi_ec_read(ec, address, value) :
  1145. acpi_ec_write(ec, address, *value);
  1146. if (ec->busy_polling || bits > 8)
  1147. acpi_ec_burst_disable(ec);
  1148. switch (result) {
  1149. case -EINVAL:
  1150. return AE_BAD_PARAMETER;
  1151. case -ENODEV:
  1152. return AE_NOT_FOUND;
  1153. case -ETIME:
  1154. return AE_TIME;
  1155. default:
  1156. return AE_OK;
  1157. }
  1158. }
  1159. /* --------------------------------------------------------------------------
  1160. * Driver Interface
  1161. * -------------------------------------------------------------------------- */
  1162. static acpi_status
  1163. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  1164. static void acpi_ec_free(struct acpi_ec *ec)
  1165. {
  1166. if (first_ec == ec)
  1167. first_ec = NULL;
  1168. if (boot_ec == ec)
  1169. boot_ec = NULL;
  1170. kfree(ec);
  1171. }
  1172. static struct acpi_ec *acpi_ec_alloc(void)
  1173. {
  1174. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  1175. if (!ec)
  1176. return NULL;
  1177. mutex_init(&ec->mutex);
  1178. init_waitqueue_head(&ec->wait);
  1179. INIT_LIST_HEAD(&ec->list);
  1180. spin_lock_init(&ec->lock);
  1181. INIT_WORK(&ec->work, acpi_ec_event_handler);
  1182. ec->timestamp = jiffies;
  1183. ec->busy_polling = true;
  1184. ec->polling_guard = 0;
  1185. ec->gpe = -1;
  1186. ec->irq = -1;
  1187. return ec;
  1188. }
  1189. static acpi_status
  1190. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  1191. void *context, void **return_value)
  1192. {
  1193. char node_name[5];
  1194. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  1195. struct acpi_ec *ec = context;
  1196. int value = 0;
  1197. acpi_status status;
  1198. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  1199. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1)
  1200. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  1201. return AE_OK;
  1202. }
  1203. static acpi_status
  1204. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  1205. {
  1206. acpi_status status;
  1207. unsigned long long tmp = 0;
  1208. struct acpi_ec *ec = context;
  1209. /* clear addr values, ec_parse_io_ports depend on it */
  1210. ec->command_addr = ec->data_addr = 0;
  1211. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1212. ec_parse_io_ports, ec);
  1213. if (ACPI_FAILURE(status))
  1214. return status;
  1215. if (ec->data_addr == 0 || ec->command_addr == 0)
  1216. return AE_OK;
  1217. if (boot_ec && boot_ec_is_ecdt && EC_FLAGS_IGNORE_DSDT_GPE) {
  1218. /*
  1219. * Always inherit the GPE number setting from the ECDT
  1220. * EC.
  1221. */
  1222. ec->gpe = boot_ec->gpe;
  1223. } else {
  1224. /* Get GPE bit assignment (EC events). */
  1225. /* TODO: Add support for _GPE returning a package */
  1226. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  1227. if (ACPI_SUCCESS(status))
  1228. ec->gpe = tmp;
  1229. /*
  1230. * Errors are non-fatal, allowing for ACPI Reduced Hardware
  1231. * platforms which use GpioInt instead of GPE.
  1232. */
  1233. }
  1234. /* Use the global lock for all EC transactions? */
  1235. tmp = 0;
  1236. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  1237. ec->global_lock = tmp;
  1238. ec->handle = handle;
  1239. return AE_CTRL_TERMINATE;
  1240. }
  1241. static bool install_gpe_event_handler(struct acpi_ec *ec)
  1242. {
  1243. acpi_status status;
  1244. status = acpi_install_gpe_raw_handler(NULL, ec->gpe,
  1245. ACPI_GPE_EDGE_TRIGGERED,
  1246. &acpi_ec_gpe_handler, ec);
  1247. if (ACPI_FAILURE(status))
  1248. return false;
  1249. if (test_bit(EC_FLAGS_STARTED, &ec->flags) && ec->reference_count >= 1)
  1250. acpi_ec_enable_gpe(ec, true);
  1251. return true;
  1252. }
  1253. static bool install_gpio_irq_event_handler(struct acpi_ec *ec)
  1254. {
  1255. return request_irq(ec->irq, acpi_ec_irq_handler, IRQF_SHARED,
  1256. "ACPI EC", ec) >= 0;
  1257. }
  1258. /**
  1259. * ec_install_handlers - Install service callbacks and register query methods.
  1260. * @ec: Target EC.
  1261. * @device: ACPI device object corresponding to @ec.
  1262. *
  1263. * Install a handler for the EC address space type unless it has been installed
  1264. * already. If @device is not NULL, also look for EC query methods in the
  1265. * namespace and register them, and install an event (either GPE or GPIO IRQ)
  1266. * handler for the EC, if possible.
  1267. *
  1268. * Return:
  1269. * -ENODEV if the address space handler cannot be installed, which means
  1270. * "unable to handle transactions",
  1271. * -EPROBE_DEFER if GPIO IRQ acquisition needs to be deferred,
  1272. * or 0 (success) otherwise.
  1273. */
  1274. static int ec_install_handlers(struct acpi_ec *ec, struct acpi_device *device)
  1275. {
  1276. acpi_status status;
  1277. acpi_ec_start(ec, false);
  1278. if (!test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) {
  1279. acpi_ec_enter_noirq(ec);
  1280. status = acpi_install_address_space_handler(ec->handle,
  1281. ACPI_ADR_SPACE_EC,
  1282. &acpi_ec_space_handler,
  1283. NULL, ec);
  1284. if (ACPI_FAILURE(status)) {
  1285. acpi_ec_stop(ec, false);
  1286. return -ENODEV;
  1287. }
  1288. set_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags);
  1289. }
  1290. if (!device)
  1291. return 0;
  1292. if (ec->gpe < 0) {
  1293. /* ACPI reduced hardware platforms use a GpioInt from _CRS. */
  1294. int irq = acpi_dev_gpio_irq_get(device, 0);
  1295. /*
  1296. * Bail out right away for deferred probing or complete the
  1297. * initialization regardless of any other errors.
  1298. */
  1299. if (irq == -EPROBE_DEFER)
  1300. return -EPROBE_DEFER;
  1301. else if (irq >= 0)
  1302. ec->irq = irq;
  1303. }
  1304. if (!test_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags)) {
  1305. /* Find and register all query methods */
  1306. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  1307. acpi_ec_register_query_methods,
  1308. NULL, ec, NULL);
  1309. set_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags);
  1310. }
  1311. if (!test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) {
  1312. bool ready = false;
  1313. if (ec->gpe >= 0)
  1314. ready = install_gpe_event_handler(ec);
  1315. else if (ec->irq >= 0)
  1316. ready = install_gpio_irq_event_handler(ec);
  1317. if (ready) {
  1318. set_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags);
  1319. acpi_ec_leave_noirq(ec);
  1320. }
  1321. /*
  1322. * Failures to install an event handler are not fatal, because
  1323. * the EC can be polled for events.
  1324. */
  1325. }
  1326. /* EC is fully operational, allow queries */
  1327. acpi_ec_enable_event(ec);
  1328. return 0;
  1329. }
  1330. static void ec_remove_handlers(struct acpi_ec *ec)
  1331. {
  1332. if (test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) {
  1333. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  1334. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  1335. pr_err("failed to remove space handler\n");
  1336. clear_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags);
  1337. }
  1338. /*
  1339. * Stops handling the EC transactions after removing the operation
  1340. * region handler. This is required because _REG(DISCONNECT)
  1341. * invoked during the removal can result in new EC transactions.
  1342. *
  1343. * Flushes the EC requests and thus disables the GPE before
  1344. * removing the GPE handler. This is required by the current ACPICA
  1345. * GPE core. ACPICA GPE core will automatically disable a GPE when
  1346. * it is indicated but there is no way to handle it. So the drivers
  1347. * must disable the GPEs prior to removing the GPE handlers.
  1348. */
  1349. acpi_ec_stop(ec, false);
  1350. if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) {
  1351. if (ec->gpe >= 0 &&
  1352. ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  1353. &acpi_ec_gpe_handler)))
  1354. pr_err("failed to remove gpe handler\n");
  1355. if (ec->irq >= 0)
  1356. free_irq(ec->irq, ec);
  1357. clear_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags);
  1358. }
  1359. if (test_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags)) {
  1360. acpi_ec_remove_query_handlers(ec, true, 0);
  1361. clear_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags);
  1362. }
  1363. }
  1364. static int acpi_ec_setup(struct acpi_ec *ec, struct acpi_device *device)
  1365. {
  1366. int ret;
  1367. ret = ec_install_handlers(ec, device);
  1368. if (ret)
  1369. return ret;
  1370. /* First EC capable of handling transactions */
  1371. if (!first_ec)
  1372. first_ec = ec;
  1373. pr_info("EC_CMD/EC_SC=0x%lx, EC_DATA=0x%lx\n", ec->command_addr,
  1374. ec->data_addr);
  1375. if (test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) {
  1376. if (ec->gpe >= 0)
  1377. pr_info("GPE=0x%x\n", ec->gpe);
  1378. else
  1379. pr_info("IRQ=%d\n", ec->irq);
  1380. }
  1381. return ret;
  1382. }
  1383. static int acpi_ec_add(struct acpi_device *device)
  1384. {
  1385. struct acpi_ec *ec;
  1386. int ret;
  1387. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  1388. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  1389. if (boot_ec && (boot_ec->handle == device->handle ||
  1390. !strcmp(acpi_device_hid(device), ACPI_ECDT_HID))) {
  1391. /* Fast path: this device corresponds to the boot EC. */
  1392. ec = boot_ec;
  1393. } else {
  1394. acpi_status status;
  1395. ec = acpi_ec_alloc();
  1396. if (!ec)
  1397. return -ENOMEM;
  1398. status = ec_parse_device(device->handle, 0, ec, NULL);
  1399. if (status != AE_CTRL_TERMINATE) {
  1400. ret = -EINVAL;
  1401. goto err;
  1402. }
  1403. if (boot_ec && ec->command_addr == boot_ec->command_addr &&
  1404. ec->data_addr == boot_ec->data_addr &&
  1405. !EC_FLAGS_TRUST_DSDT_GPE) {
  1406. /*
  1407. * Trust PNP0C09 namespace location rather than
  1408. * ECDT ID. But trust ECDT GPE rather than _GPE
  1409. * because of ASUS quirks, so do not change
  1410. * boot_ec->gpe to ec->gpe.
  1411. */
  1412. boot_ec->handle = ec->handle;
  1413. acpi_handle_debug(ec->handle, "duplicated.\n");
  1414. acpi_ec_free(ec);
  1415. ec = boot_ec;
  1416. }
  1417. }
  1418. ret = acpi_ec_setup(ec, device);
  1419. if (ret)
  1420. goto err;
  1421. if (ec == boot_ec)
  1422. acpi_handle_info(boot_ec->handle,
  1423. "Boot %s EC initialization complete\n",
  1424. boot_ec_is_ecdt ? "ECDT" : "DSDT");
  1425. acpi_handle_info(ec->handle,
  1426. "EC: Used to handle transactions and events\n");
  1427. device->driver_data = ec;
  1428. ret = !!request_region(ec->data_addr, 1, "EC data");
  1429. WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
  1430. ret = !!request_region(ec->command_addr, 1, "EC cmd");
  1431. WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
  1432. /* Reprobe devices depending on the EC */
  1433. acpi_walk_dep_device_list(ec->handle);
  1434. acpi_handle_debug(ec->handle, "enumerated.\n");
  1435. return 0;
  1436. err:
  1437. if (ec != boot_ec)
  1438. acpi_ec_free(ec);
  1439. return ret;
  1440. }
  1441. static int acpi_ec_remove(struct acpi_device *device)
  1442. {
  1443. struct acpi_ec *ec;
  1444. if (!device)
  1445. return -EINVAL;
  1446. ec = acpi_driver_data(device);
  1447. release_region(ec->data_addr, 1);
  1448. release_region(ec->command_addr, 1);
  1449. device->driver_data = NULL;
  1450. if (ec != boot_ec) {
  1451. ec_remove_handlers(ec);
  1452. acpi_ec_free(ec);
  1453. }
  1454. return 0;
  1455. }
  1456. static acpi_status
  1457. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  1458. {
  1459. struct acpi_ec *ec = context;
  1460. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  1461. return AE_OK;
  1462. /*
  1463. * The first address region returned is the data port, and
  1464. * the second address region returned is the status/command
  1465. * port.
  1466. */
  1467. if (ec->data_addr == 0)
  1468. ec->data_addr = resource->data.io.minimum;
  1469. else if (ec->command_addr == 0)
  1470. ec->command_addr = resource->data.io.minimum;
  1471. else
  1472. return AE_CTRL_TERMINATE;
  1473. return AE_OK;
  1474. }
  1475. static const struct acpi_device_id ec_device_ids[] = {
  1476. {"PNP0C09", 0},
  1477. {ACPI_ECDT_HID, 0},
  1478. {"", 0},
  1479. };
  1480. /*
  1481. * This function is not Windows-compatible as Windows never enumerates the
  1482. * namespace EC before the main ACPI device enumeration process. It is
  1483. * retained for historical reason and will be deprecated in the future.
  1484. */
  1485. void __init acpi_ec_dsdt_probe(void)
  1486. {
  1487. struct acpi_ec *ec;
  1488. acpi_status status;
  1489. int ret;
  1490. /*
  1491. * If a platform has ECDT, there is no need to proceed as the
  1492. * following probe is not a part of the ACPI device enumeration,
  1493. * executing _STA is not safe, and thus this probe may risk of
  1494. * picking up an invalid EC device.
  1495. */
  1496. if (boot_ec)
  1497. return;
  1498. ec = acpi_ec_alloc();
  1499. if (!ec)
  1500. return;
  1501. /*
  1502. * At this point, the namespace is initialized, so start to find
  1503. * the namespace objects.
  1504. */
  1505. status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device, ec, NULL);
  1506. if (ACPI_FAILURE(status) || !ec->handle) {
  1507. acpi_ec_free(ec);
  1508. return;
  1509. }
  1510. /*
  1511. * When the DSDT EC is available, always re-configure boot EC to
  1512. * have _REG evaluated. _REG can only be evaluated after the
  1513. * namespace initialization.
  1514. * At this point, the GPE is not fully initialized, so do not to
  1515. * handle the events.
  1516. */
  1517. ret = acpi_ec_setup(ec, NULL);
  1518. if (ret) {
  1519. acpi_ec_free(ec);
  1520. return;
  1521. }
  1522. boot_ec = ec;
  1523. acpi_handle_info(ec->handle,
  1524. "Boot DSDT EC used to handle transactions\n");
  1525. }
  1526. /*
  1527. * acpi_ec_ecdt_start - Finalize the boot ECDT EC initialization.
  1528. *
  1529. * First, look for an ACPI handle for the boot ECDT EC if acpi_ec_add() has not
  1530. * found a matching object in the namespace.
  1531. *
  1532. * Next, in case the DSDT EC is not functioning, it is still necessary to
  1533. * provide a functional ECDT EC to handle events, so add an extra device object
  1534. * to represent it (see https://bugzilla.kernel.org/show_bug.cgi?id=115021).
  1535. *
  1536. * This is useful on platforms with valid ECDT and invalid DSDT EC settings,
  1537. * like ASUS X550ZE (see https://bugzilla.kernel.org/show_bug.cgi?id=196847).
  1538. */
  1539. static void __init acpi_ec_ecdt_start(void)
  1540. {
  1541. struct acpi_table_ecdt *ecdt_ptr;
  1542. acpi_handle handle;
  1543. acpi_status status;
  1544. /* Bail out if a matching EC has been found in the namespace. */
  1545. if (!boot_ec || boot_ec->handle != ACPI_ROOT_OBJECT)
  1546. return;
  1547. /* Look up the object pointed to from the ECDT in the namespace. */
  1548. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  1549. (struct acpi_table_header **)&ecdt_ptr);
  1550. if (ACPI_FAILURE(status))
  1551. return;
  1552. status = acpi_get_handle(NULL, ecdt_ptr->id, &handle);
  1553. if (ACPI_SUCCESS(status)) {
  1554. boot_ec->handle = handle;
  1555. /* Add a special ACPI device object to represent the boot EC. */
  1556. acpi_bus_register_early_device(ACPI_BUS_TYPE_ECDT_EC);
  1557. }
  1558. acpi_put_table((struct acpi_table_header *)ecdt_ptr);
  1559. }
  1560. /*
  1561. * On some hardware it is necessary to clear events accumulated by the EC during
  1562. * sleep. These ECs stop reporting GPEs until they are manually polled, if too
  1563. * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
  1564. *
  1565. * https://bugzilla.kernel.org/show_bug.cgi?id=44161
  1566. *
  1567. * Ideally, the EC should also be instructed NOT to accumulate events during
  1568. * sleep (which Windows seems to do somehow), but the interface to control this
  1569. * behaviour is not known at this time.
  1570. *
  1571. * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
  1572. * however it is very likely that other Samsung models are affected.
  1573. *
  1574. * On systems which don't accumulate _Q events during sleep, this extra check
  1575. * should be harmless.
  1576. */
  1577. static int ec_clear_on_resume(const struct dmi_system_id *id)
  1578. {
  1579. pr_debug("Detected system needing EC poll on resume.\n");
  1580. EC_FLAGS_CLEAR_ON_RESUME = 1;
  1581. ec_event_clearing = ACPI_EC_EVT_TIMING_STATUS;
  1582. return 0;
  1583. }
  1584. /*
  1585. * Some ECDTs contain wrong register addresses.
  1586. * MSI MS-171F
  1587. * https://bugzilla.kernel.org/show_bug.cgi?id=12461
  1588. */
  1589. static int ec_correct_ecdt(const struct dmi_system_id *id)
  1590. {
  1591. pr_debug("Detected system needing ECDT address correction.\n");
  1592. EC_FLAGS_CORRECT_ECDT = 1;
  1593. return 0;
  1594. }
  1595. /*
  1596. * Some ECDTs contain wrong GPE setting, but they share the same port addresses
  1597. * with DSDT EC, don't duplicate the DSDT EC with ECDT EC in this case.
  1598. * https://bugzilla.kernel.org/show_bug.cgi?id=209989
  1599. */
  1600. static int ec_honor_dsdt_gpe(const struct dmi_system_id *id)
  1601. {
  1602. pr_debug("Detected system needing DSDT GPE setting.\n");
  1603. EC_FLAGS_TRUST_DSDT_GPE = 1;
  1604. return 0;
  1605. }
  1606. /*
  1607. * Some DSDTs contain wrong GPE setting.
  1608. * Asus FX502VD/VE, GL702VMK, X550VXK, X580VD
  1609. * https://bugzilla.kernel.org/show_bug.cgi?id=195651
  1610. */
  1611. static int ec_honor_ecdt_gpe(const struct dmi_system_id *id)
  1612. {
  1613. pr_debug("Detected system needing ignore DSDT GPE setting.\n");
  1614. EC_FLAGS_IGNORE_DSDT_GPE = 1;
  1615. return 0;
  1616. }
  1617. static const struct dmi_system_id ec_dmi_table[] __initconst = {
  1618. {
  1619. ec_correct_ecdt, "MSI MS-171F", {
  1620. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star"),
  1621. DMI_MATCH(DMI_PRODUCT_NAME, "MS-171F"),}, NULL},
  1622. {
  1623. ec_honor_ecdt_gpe, "ASUS FX502VD", {
  1624. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1625. DMI_MATCH(DMI_PRODUCT_NAME, "FX502VD"),}, NULL},
  1626. {
  1627. ec_honor_ecdt_gpe, "ASUS FX502VE", {
  1628. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1629. DMI_MATCH(DMI_PRODUCT_NAME, "FX502VE"),}, NULL},
  1630. {
  1631. ec_honor_ecdt_gpe, "ASUS GL702VMK", {
  1632. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1633. DMI_MATCH(DMI_PRODUCT_NAME, "GL702VMK"),}, NULL},
  1634. {
  1635. ec_honor_ecdt_gpe, "ASUSTeK COMPUTER INC. X505BA", {
  1636. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1637. DMI_MATCH(DMI_PRODUCT_NAME, "X505BA"),}, NULL},
  1638. {
  1639. ec_honor_ecdt_gpe, "ASUSTeK COMPUTER INC. X505BP", {
  1640. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1641. DMI_MATCH(DMI_PRODUCT_NAME, "X505BP"),}, NULL},
  1642. {
  1643. ec_honor_ecdt_gpe, "ASUSTeK COMPUTER INC. X542BA", {
  1644. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1645. DMI_MATCH(DMI_PRODUCT_NAME, "X542BA"),}, NULL},
  1646. {
  1647. ec_honor_ecdt_gpe, "ASUSTeK COMPUTER INC. X542BP", {
  1648. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1649. DMI_MATCH(DMI_PRODUCT_NAME, "X542BP"),}, NULL},
  1650. {
  1651. ec_honor_ecdt_gpe, "ASUS X550VXK", {
  1652. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1653. DMI_MATCH(DMI_PRODUCT_NAME, "X550VXK"),}, NULL},
  1654. {
  1655. ec_honor_ecdt_gpe, "ASUS X580VD", {
  1656. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1657. DMI_MATCH(DMI_PRODUCT_NAME, "X580VD"),}, NULL},
  1658. {
  1659. /* https://bugzilla.kernel.org/show_bug.cgi?id=209989 */
  1660. ec_honor_dsdt_gpe, "HP Pavilion Gaming Laptop 15-cx0xxx", {
  1661. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  1662. DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion Gaming Laptop 15-cx0xxx"),}, NULL},
  1663. {
  1664. ec_clear_on_resume, "Samsung hardware", {
  1665. DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
  1666. {},
  1667. };
  1668. void __init acpi_ec_ecdt_probe(void)
  1669. {
  1670. struct acpi_table_ecdt *ecdt_ptr;
  1671. struct acpi_ec *ec;
  1672. acpi_status status;
  1673. int ret;
  1674. /* Generate a boot ec context. */
  1675. dmi_check_system(ec_dmi_table);
  1676. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  1677. (struct acpi_table_header **)&ecdt_ptr);
  1678. if (ACPI_FAILURE(status))
  1679. return;
  1680. if (!ecdt_ptr->control.address || !ecdt_ptr->data.address) {
  1681. /*
  1682. * Asus X50GL:
  1683. * https://bugzilla.kernel.org/show_bug.cgi?id=11880
  1684. */
  1685. goto out;
  1686. }
  1687. ec = acpi_ec_alloc();
  1688. if (!ec)
  1689. goto out;
  1690. if (EC_FLAGS_CORRECT_ECDT) {
  1691. ec->command_addr = ecdt_ptr->data.address;
  1692. ec->data_addr = ecdt_ptr->control.address;
  1693. } else {
  1694. ec->command_addr = ecdt_ptr->control.address;
  1695. ec->data_addr = ecdt_ptr->data.address;
  1696. }
  1697. /*
  1698. * Ignore the GPE value on Reduced Hardware platforms.
  1699. * Some products have this set to an erroneous value.
  1700. */
  1701. if (!acpi_gbl_reduced_hardware)
  1702. ec->gpe = ecdt_ptr->gpe;
  1703. ec->handle = ACPI_ROOT_OBJECT;
  1704. /*
  1705. * At this point, the namespace is not initialized, so do not find
  1706. * the namespace objects, or handle the events.
  1707. */
  1708. ret = acpi_ec_setup(ec, NULL);
  1709. if (ret) {
  1710. acpi_ec_free(ec);
  1711. goto out;
  1712. }
  1713. boot_ec = ec;
  1714. boot_ec_is_ecdt = true;
  1715. pr_info("Boot ECDT EC used to handle transactions\n");
  1716. out:
  1717. acpi_put_table((struct acpi_table_header *)ecdt_ptr);
  1718. }
  1719. #ifdef CONFIG_PM_SLEEP
  1720. static int acpi_ec_suspend(struct device *dev)
  1721. {
  1722. struct acpi_ec *ec =
  1723. acpi_driver_data(to_acpi_device(dev));
  1724. if (!pm_suspend_no_platform() && ec_freeze_events)
  1725. acpi_ec_disable_event(ec);
  1726. return 0;
  1727. }
  1728. static int acpi_ec_suspend_noirq(struct device *dev)
  1729. {
  1730. struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev));
  1731. /*
  1732. * The SCI handler doesn't run at this point, so the GPE can be
  1733. * masked at the low level without side effects.
  1734. */
  1735. if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) &&
  1736. ec->gpe >= 0 && ec->reference_count >= 1)
  1737. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
  1738. acpi_ec_enter_noirq(ec);
  1739. return 0;
  1740. }
  1741. static int acpi_ec_resume_noirq(struct device *dev)
  1742. {
  1743. struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev));
  1744. acpi_ec_leave_noirq(ec);
  1745. if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) &&
  1746. ec->gpe >= 0 && ec->reference_count >= 1)
  1747. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
  1748. return 0;
  1749. }
  1750. static int acpi_ec_resume(struct device *dev)
  1751. {
  1752. struct acpi_ec *ec =
  1753. acpi_driver_data(to_acpi_device(dev));
  1754. acpi_ec_enable_event(ec);
  1755. return 0;
  1756. }
  1757. void acpi_ec_mark_gpe_for_wake(void)
  1758. {
  1759. if (first_ec && !ec_no_wakeup)
  1760. acpi_mark_gpe_for_wake(NULL, first_ec->gpe);
  1761. }
  1762. EXPORT_SYMBOL_GPL(acpi_ec_mark_gpe_for_wake);
  1763. void acpi_ec_set_gpe_wake_mask(u8 action)
  1764. {
  1765. if (pm_suspend_no_platform() && first_ec && !ec_no_wakeup)
  1766. acpi_set_gpe_wake_mask(NULL, first_ec->gpe, action);
  1767. }
  1768. bool acpi_ec_dispatch_gpe(void)
  1769. {
  1770. bool work_in_progress;
  1771. u32 ret;
  1772. if (!first_ec)
  1773. return acpi_any_gpe_status_set(U32_MAX);
  1774. /*
  1775. * Report wakeup if the status bit is set for any enabled GPE other
  1776. * than the EC one.
  1777. */
  1778. if (acpi_any_gpe_status_set(first_ec->gpe))
  1779. return true;
  1780. /*
  1781. * Dispatch the EC GPE in-band, but do not report wakeup in any case
  1782. * to allow the caller to process events properly after that.
  1783. */
  1784. ret = acpi_dispatch_gpe(NULL, first_ec->gpe);
  1785. if (ret == ACPI_INTERRUPT_HANDLED)
  1786. pm_pr_dbg("ACPI EC GPE dispatched\n");
  1787. /* Drain EC work. */
  1788. do {
  1789. acpi_ec_flush_work();
  1790. pm_pr_dbg("ACPI EC work flushed\n");
  1791. spin_lock_irq(&first_ec->lock);
  1792. work_in_progress = first_ec->events_in_progress +
  1793. first_ec->queries_in_progress > 0;
  1794. spin_unlock_irq(&first_ec->lock);
  1795. } while (work_in_progress && !pm_wakeup_pending());
  1796. return false;
  1797. }
  1798. #endif /* CONFIG_PM_SLEEP */
  1799. static const struct dev_pm_ops acpi_ec_pm = {
  1800. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend_noirq, acpi_ec_resume_noirq)
  1801. SET_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend, acpi_ec_resume)
  1802. };
  1803. static int param_set_event_clearing(const char *val,
  1804. const struct kernel_param *kp)
  1805. {
  1806. int result = 0;
  1807. if (!strncmp(val, "status", sizeof("status") - 1)) {
  1808. ec_event_clearing = ACPI_EC_EVT_TIMING_STATUS;
  1809. pr_info("Assuming SCI_EVT clearing on EC_SC accesses\n");
  1810. } else if (!strncmp(val, "query", sizeof("query") - 1)) {
  1811. ec_event_clearing = ACPI_EC_EVT_TIMING_QUERY;
  1812. pr_info("Assuming SCI_EVT clearing on QR_EC writes\n");
  1813. } else if (!strncmp(val, "event", sizeof("event") - 1)) {
  1814. ec_event_clearing = ACPI_EC_EVT_TIMING_EVENT;
  1815. pr_info("Assuming SCI_EVT clearing on event reads\n");
  1816. } else
  1817. result = -EINVAL;
  1818. return result;
  1819. }
  1820. static int param_get_event_clearing(char *buffer,
  1821. const struct kernel_param *kp)
  1822. {
  1823. switch (ec_event_clearing) {
  1824. case ACPI_EC_EVT_TIMING_STATUS:
  1825. return sprintf(buffer, "status\n");
  1826. case ACPI_EC_EVT_TIMING_QUERY:
  1827. return sprintf(buffer, "query\n");
  1828. case ACPI_EC_EVT_TIMING_EVENT:
  1829. return sprintf(buffer, "event\n");
  1830. default:
  1831. return sprintf(buffer, "invalid\n");
  1832. }
  1833. return 0;
  1834. }
  1835. module_param_call(ec_event_clearing, param_set_event_clearing, param_get_event_clearing,
  1836. NULL, 0644);
  1837. MODULE_PARM_DESC(ec_event_clearing, "Assumed SCI_EVT clearing timing");
  1838. static struct acpi_driver acpi_ec_driver = {
  1839. .name = "ec",
  1840. .class = ACPI_EC_CLASS,
  1841. .ids = ec_device_ids,
  1842. .ops = {
  1843. .add = acpi_ec_add,
  1844. .remove = acpi_ec_remove,
  1845. },
  1846. .drv.pm = &acpi_ec_pm,
  1847. };
  1848. static void acpi_ec_destroy_workqueues(void)
  1849. {
  1850. if (ec_wq) {
  1851. destroy_workqueue(ec_wq);
  1852. ec_wq = NULL;
  1853. }
  1854. if (ec_query_wq) {
  1855. destroy_workqueue(ec_query_wq);
  1856. ec_query_wq = NULL;
  1857. }
  1858. }
  1859. static int acpi_ec_init_workqueues(void)
  1860. {
  1861. if (!ec_wq)
  1862. ec_wq = alloc_ordered_workqueue("kec", 0);
  1863. if (!ec_query_wq)
  1864. ec_query_wq = alloc_workqueue("kec_query", 0, ec_max_queries);
  1865. if (!ec_wq || !ec_query_wq) {
  1866. acpi_ec_destroy_workqueues();
  1867. return -ENODEV;
  1868. }
  1869. return 0;
  1870. }
  1871. static const struct dmi_system_id acpi_ec_no_wakeup[] = {
  1872. {
  1873. .ident = "Thinkpad X1 Carbon 6th",
  1874. .matches = {
  1875. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1876. DMI_MATCH(DMI_PRODUCT_FAMILY, "Thinkpad X1 Carbon 6th"),
  1877. },
  1878. },
  1879. {
  1880. .ident = "ThinkPad X1 Carbon 6th",
  1881. .matches = {
  1882. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1883. DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Carbon 6th"),
  1884. },
  1885. },
  1886. {
  1887. .ident = "ThinkPad X1 Yoga 3rd",
  1888. .matches = {
  1889. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1890. DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Yoga 3rd"),
  1891. },
  1892. },
  1893. { },
  1894. };
  1895. void __init acpi_ec_init(void)
  1896. {
  1897. int result;
  1898. result = acpi_ec_init_workqueues();
  1899. if (result)
  1900. return;
  1901. /*
  1902. * Disable EC wakeup on following systems to prevent periodic
  1903. * wakeup from EC GPE.
  1904. */
  1905. if (dmi_check_system(acpi_ec_no_wakeup)) {
  1906. ec_no_wakeup = true;
  1907. pr_debug("Disabling EC wakeup on suspend-to-idle\n");
  1908. }
  1909. /* Driver must be registered after acpi_ec_init_workqueues(). */
  1910. acpi_bus_register_driver(&acpi_ec_driver);
  1911. acpi_ec_ecdt_start();
  1912. }
  1913. /* EC driver currently not unloadable */
  1914. #if 0
  1915. static void __exit acpi_ec_exit(void)
  1916. {
  1917. acpi_bus_unregister_driver(&acpi_ec_driver);
  1918. acpi_ec_destroy_workqueues();
  1919. }
  1920. #endif /* 0 */