ec.c 61 KB

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