ec.c 58 KB

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