ec.c 46 KB

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