ec.c 55 KB

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