ec.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  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. void acpi_ec_dispatch_gpe(void)
  914. {
  915. if (first_ec)
  916. acpi_dispatch_gpe(NULL, first_ec->gpe);
  917. }
  918. /* --------------------------------------------------------------------------
  919. Event Management
  920. -------------------------------------------------------------------------- */
  921. static struct acpi_ec_query_handler *
  922. acpi_ec_get_query_handler(struct acpi_ec_query_handler *handler)
  923. {
  924. if (handler)
  925. kref_get(&handler->kref);
  926. return handler;
  927. }
  928. static struct acpi_ec_query_handler *
  929. acpi_ec_get_query_handler_by_value(struct acpi_ec *ec, u8 value)
  930. {
  931. struct acpi_ec_query_handler *handler;
  932. bool found = false;
  933. mutex_lock(&ec->mutex);
  934. list_for_each_entry(handler, &ec->list, node) {
  935. if (value == handler->query_bit) {
  936. found = true;
  937. break;
  938. }
  939. }
  940. mutex_unlock(&ec->mutex);
  941. return found ? acpi_ec_get_query_handler(handler) : NULL;
  942. }
  943. static void acpi_ec_query_handler_release(struct kref *kref)
  944. {
  945. struct acpi_ec_query_handler *handler =
  946. container_of(kref, struct acpi_ec_query_handler, kref);
  947. kfree(handler);
  948. }
  949. static void acpi_ec_put_query_handler(struct acpi_ec_query_handler *handler)
  950. {
  951. kref_put(&handler->kref, acpi_ec_query_handler_release);
  952. }
  953. int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
  954. acpi_handle handle, acpi_ec_query_func func,
  955. void *data)
  956. {
  957. struct acpi_ec_query_handler *handler =
  958. kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
  959. if (!handler)
  960. return -ENOMEM;
  961. handler->query_bit = query_bit;
  962. handler->handle = handle;
  963. handler->func = func;
  964. handler->data = data;
  965. mutex_lock(&ec->mutex);
  966. kref_init(&handler->kref);
  967. list_add(&handler->node, &ec->list);
  968. mutex_unlock(&ec->mutex);
  969. return 0;
  970. }
  971. EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
  972. static void acpi_ec_remove_query_handlers(struct acpi_ec *ec,
  973. bool remove_all, u8 query_bit)
  974. {
  975. struct acpi_ec_query_handler *handler, *tmp;
  976. LIST_HEAD(free_list);
  977. mutex_lock(&ec->mutex);
  978. list_for_each_entry_safe(handler, tmp, &ec->list, node) {
  979. if (remove_all || query_bit == handler->query_bit) {
  980. list_del_init(&handler->node);
  981. list_add(&handler->node, &free_list);
  982. }
  983. }
  984. mutex_unlock(&ec->mutex);
  985. list_for_each_entry_safe(handler, tmp, &free_list, node)
  986. acpi_ec_put_query_handler(handler);
  987. }
  988. void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
  989. {
  990. acpi_ec_remove_query_handlers(ec, false, query_bit);
  991. }
  992. EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
  993. static struct acpi_ec_query *acpi_ec_create_query(u8 *pval)
  994. {
  995. struct acpi_ec_query *q;
  996. struct transaction *t;
  997. q = kzalloc(sizeof (struct acpi_ec_query), GFP_KERNEL);
  998. if (!q)
  999. return NULL;
  1000. INIT_WORK(&q->work, acpi_ec_event_processor);
  1001. t = &q->transaction;
  1002. t->command = ACPI_EC_COMMAND_QUERY;
  1003. t->rdata = pval;
  1004. t->rlen = 1;
  1005. return q;
  1006. }
  1007. static void acpi_ec_delete_query(struct acpi_ec_query *q)
  1008. {
  1009. if (q) {
  1010. if (q->handler)
  1011. acpi_ec_put_query_handler(q->handler);
  1012. kfree(q);
  1013. }
  1014. }
  1015. static void acpi_ec_event_processor(struct work_struct *work)
  1016. {
  1017. struct acpi_ec_query *q = container_of(work, struct acpi_ec_query, work);
  1018. struct acpi_ec_query_handler *handler = q->handler;
  1019. ec_dbg_evt("Query(0x%02x) started", handler->query_bit);
  1020. if (handler->func)
  1021. handler->func(handler->data);
  1022. else if (handler->handle)
  1023. acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
  1024. ec_dbg_evt("Query(0x%02x) stopped", handler->query_bit);
  1025. acpi_ec_delete_query(q);
  1026. }
  1027. static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
  1028. {
  1029. u8 value = 0;
  1030. int result;
  1031. struct acpi_ec_query *q;
  1032. q = acpi_ec_create_query(&value);
  1033. if (!q)
  1034. return -ENOMEM;
  1035. /*
  1036. * Query the EC to find out which _Qxx method we need to evaluate.
  1037. * Note that successful completion of the query causes the ACPI_EC_SCI
  1038. * bit to be cleared (and thus clearing the interrupt source).
  1039. */
  1040. result = acpi_ec_transaction(ec, &q->transaction);
  1041. if (!value)
  1042. result = -ENODATA;
  1043. if (result)
  1044. goto err_exit;
  1045. q->handler = acpi_ec_get_query_handler_by_value(ec, value);
  1046. if (!q->handler) {
  1047. result = -ENODATA;
  1048. goto err_exit;
  1049. }
  1050. /*
  1051. * It is reported that _Qxx are evaluated in a parallel way on
  1052. * Windows:
  1053. * https://bugzilla.kernel.org/show_bug.cgi?id=94411
  1054. *
  1055. * Put this log entry before schedule_work() in order to make
  1056. * it appearing before any other log entries occurred during the
  1057. * work queue execution.
  1058. */
  1059. ec_dbg_evt("Query(0x%02x) scheduled", value);
  1060. if (!queue_work(ec_query_wq, &q->work)) {
  1061. ec_dbg_evt("Query(0x%02x) overlapped", value);
  1062. result = -EBUSY;
  1063. }
  1064. err_exit:
  1065. if (result)
  1066. acpi_ec_delete_query(q);
  1067. if (data)
  1068. *data = value;
  1069. return result;
  1070. }
  1071. static void acpi_ec_check_event(struct acpi_ec *ec)
  1072. {
  1073. unsigned long flags;
  1074. if (ec_event_clearing == ACPI_EC_EVT_TIMING_EVENT) {
  1075. if (ec_guard(ec)) {
  1076. spin_lock_irqsave(&ec->lock, flags);
  1077. /*
  1078. * Take care of the SCI_EVT unless no one else is
  1079. * taking care of it.
  1080. */
  1081. if (!ec->curr)
  1082. advance_transaction(ec);
  1083. spin_unlock_irqrestore(&ec->lock, flags);
  1084. }
  1085. }
  1086. }
  1087. static void acpi_ec_event_handler(struct work_struct *work)
  1088. {
  1089. unsigned long flags;
  1090. struct acpi_ec *ec = container_of(work, struct acpi_ec, work);
  1091. ec_dbg_evt("Event started");
  1092. spin_lock_irqsave(&ec->lock, flags);
  1093. while (ec->nr_pending_queries) {
  1094. spin_unlock_irqrestore(&ec->lock, flags);
  1095. (void)acpi_ec_query(ec, NULL);
  1096. spin_lock_irqsave(&ec->lock, flags);
  1097. ec->nr_pending_queries--;
  1098. /*
  1099. * Before exit, make sure that this work item can be
  1100. * scheduled again. There might be QR_EC failures, leaving
  1101. * EC_FLAGS_QUERY_PENDING uncleared and preventing this work
  1102. * item from being scheduled again.
  1103. */
  1104. if (!ec->nr_pending_queries) {
  1105. if (ec_event_clearing == ACPI_EC_EVT_TIMING_STATUS ||
  1106. ec_event_clearing == ACPI_EC_EVT_TIMING_QUERY)
  1107. acpi_ec_complete_query(ec);
  1108. }
  1109. }
  1110. spin_unlock_irqrestore(&ec->lock, flags);
  1111. ec_dbg_evt("Event stopped");
  1112. acpi_ec_check_event(ec);
  1113. }
  1114. static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
  1115. u32 gpe_number, void *data)
  1116. {
  1117. unsigned long flags;
  1118. struct acpi_ec *ec = data;
  1119. spin_lock_irqsave(&ec->lock, flags);
  1120. advance_transaction(ec);
  1121. spin_unlock_irqrestore(&ec->lock, flags);
  1122. return ACPI_INTERRUPT_HANDLED;
  1123. }
  1124. /* --------------------------------------------------------------------------
  1125. * Address Space Management
  1126. * -------------------------------------------------------------------------- */
  1127. static acpi_status
  1128. acpi_ec_space_handler(u32 function, acpi_physical_address address,
  1129. u32 bits, u64 *value64,
  1130. void *handler_context, void *region_context)
  1131. {
  1132. struct acpi_ec *ec = handler_context;
  1133. int result = 0, i, bytes = bits / 8;
  1134. u8 *value = (u8 *)value64;
  1135. if ((address > 0xFF) || !value || !handler_context)
  1136. return AE_BAD_PARAMETER;
  1137. if (function != ACPI_READ && function != ACPI_WRITE)
  1138. return AE_BAD_PARAMETER;
  1139. if (ec->busy_polling || bits > 8)
  1140. acpi_ec_burst_enable(ec);
  1141. for (i = 0; i < bytes; ++i, ++address, ++value)
  1142. result = (function == ACPI_READ) ?
  1143. acpi_ec_read(ec, address, value) :
  1144. acpi_ec_write(ec, address, *value);
  1145. if (ec->busy_polling || bits > 8)
  1146. acpi_ec_burst_disable(ec);
  1147. switch (result) {
  1148. case -EINVAL:
  1149. return AE_BAD_PARAMETER;
  1150. case -ENODEV:
  1151. return AE_NOT_FOUND;
  1152. case -ETIME:
  1153. return AE_TIME;
  1154. default:
  1155. return AE_OK;
  1156. }
  1157. }
  1158. /* --------------------------------------------------------------------------
  1159. * Driver Interface
  1160. * -------------------------------------------------------------------------- */
  1161. static acpi_status
  1162. ec_parse_io_ports(struct acpi_resource *resource, void *context);
  1163. static void acpi_ec_free(struct acpi_ec *ec)
  1164. {
  1165. if (first_ec == ec)
  1166. first_ec = NULL;
  1167. if (boot_ec == ec)
  1168. boot_ec = NULL;
  1169. kfree(ec);
  1170. }
  1171. static struct acpi_ec *acpi_ec_alloc(void)
  1172. {
  1173. struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
  1174. if (!ec)
  1175. return NULL;
  1176. mutex_init(&ec->mutex);
  1177. init_waitqueue_head(&ec->wait);
  1178. INIT_LIST_HEAD(&ec->list);
  1179. spin_lock_init(&ec->lock);
  1180. INIT_WORK(&ec->work, acpi_ec_event_handler);
  1181. ec->timestamp = jiffies;
  1182. ec->busy_polling = true;
  1183. ec->polling_guard = 0;
  1184. return ec;
  1185. }
  1186. static acpi_status
  1187. acpi_ec_register_query_methods(acpi_handle handle, u32 level,
  1188. void *context, void **return_value)
  1189. {
  1190. char node_name[5];
  1191. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  1192. struct acpi_ec *ec = context;
  1193. int value = 0;
  1194. acpi_status status;
  1195. status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  1196. if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1)
  1197. acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
  1198. return AE_OK;
  1199. }
  1200. static acpi_status
  1201. ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
  1202. {
  1203. acpi_status status;
  1204. unsigned long long tmp = 0;
  1205. struct acpi_ec *ec = context;
  1206. /* clear addr values, ec_parse_io_ports depend on it */
  1207. ec->command_addr = ec->data_addr = 0;
  1208. status = acpi_walk_resources(handle, METHOD_NAME__CRS,
  1209. ec_parse_io_ports, ec);
  1210. if (ACPI_FAILURE(status))
  1211. return status;
  1212. if (ec->data_addr == 0 || ec->command_addr == 0)
  1213. return AE_OK;
  1214. if (boot_ec && boot_ec_is_ecdt && EC_FLAGS_IGNORE_DSDT_GPE) {
  1215. /*
  1216. * Always inherit the GPE number setting from the ECDT
  1217. * EC.
  1218. */
  1219. ec->gpe = boot_ec->gpe;
  1220. } else {
  1221. /* Get GPE bit assignment (EC events). */
  1222. /* TODO: Add support for _GPE returning a package */
  1223. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  1224. if (ACPI_FAILURE(status))
  1225. return status;
  1226. ec->gpe = tmp;
  1227. }
  1228. /* Use the global lock for all EC transactions? */
  1229. tmp = 0;
  1230. acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
  1231. ec->global_lock = tmp;
  1232. ec->handle = handle;
  1233. return AE_CTRL_TERMINATE;
  1234. }
  1235. /*
  1236. * Note: This function returns an error code only when the address space
  1237. * handler is not installed, which means "not able to handle
  1238. * transactions".
  1239. */
  1240. static int ec_install_handlers(struct acpi_ec *ec, bool handle_events)
  1241. {
  1242. acpi_status status;
  1243. acpi_ec_start(ec, false);
  1244. if (!test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) {
  1245. acpi_ec_enter_noirq(ec);
  1246. status = acpi_install_address_space_handler(ec->handle,
  1247. ACPI_ADR_SPACE_EC,
  1248. &acpi_ec_space_handler,
  1249. NULL, ec);
  1250. if (ACPI_FAILURE(status)) {
  1251. if (status == AE_NOT_FOUND) {
  1252. /*
  1253. * Maybe OS fails in evaluating the _REG
  1254. * object. The AE_NOT_FOUND error will be
  1255. * ignored and OS * continue to initialize
  1256. * EC.
  1257. */
  1258. pr_err("Fail in evaluating the _REG object"
  1259. " of EC device. Broken bios is suspected.\n");
  1260. } else {
  1261. acpi_ec_stop(ec, false);
  1262. return -ENODEV;
  1263. }
  1264. }
  1265. set_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags);
  1266. }
  1267. if (!handle_events)
  1268. return 0;
  1269. if (!test_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags)) {
  1270. /* Find and register all query methods */
  1271. acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
  1272. acpi_ec_register_query_methods,
  1273. NULL, ec, NULL);
  1274. set_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags);
  1275. }
  1276. if (!test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags)) {
  1277. status = acpi_install_gpe_raw_handler(NULL, ec->gpe,
  1278. ACPI_GPE_EDGE_TRIGGERED,
  1279. &acpi_ec_gpe_handler, ec);
  1280. /* This is not fatal as we can poll EC events */
  1281. if (ACPI_SUCCESS(status)) {
  1282. set_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags);
  1283. acpi_ec_leave_noirq(ec);
  1284. if (test_bit(EC_FLAGS_STARTED, &ec->flags) &&
  1285. ec->reference_count >= 1)
  1286. acpi_ec_enable_gpe(ec, true);
  1287. }
  1288. }
  1289. /* EC is fully operational, allow queries */
  1290. acpi_ec_enable_event(ec);
  1291. return 0;
  1292. }
  1293. static void ec_remove_handlers(struct acpi_ec *ec)
  1294. {
  1295. if (test_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags)) {
  1296. if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
  1297. ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
  1298. pr_err("failed to remove space handler\n");
  1299. clear_bit(EC_FLAGS_EC_HANDLER_INSTALLED, &ec->flags);
  1300. }
  1301. /*
  1302. * Stops handling the EC transactions after removing the operation
  1303. * region handler. This is required because _REG(DISCONNECT)
  1304. * invoked during the removal can result in new EC transactions.
  1305. *
  1306. * Flushes the EC requests and thus disables the GPE before
  1307. * removing the GPE handler. This is required by the current ACPICA
  1308. * GPE core. ACPICA GPE core will automatically disable a GPE when
  1309. * it is indicated but there is no way to handle it. So the drivers
  1310. * must disable the GPEs prior to removing the GPE handlers.
  1311. */
  1312. acpi_ec_stop(ec, false);
  1313. if (test_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags)) {
  1314. if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
  1315. &acpi_ec_gpe_handler)))
  1316. pr_err("failed to remove gpe handler\n");
  1317. clear_bit(EC_FLAGS_GPE_HANDLER_INSTALLED, &ec->flags);
  1318. }
  1319. if (test_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags)) {
  1320. acpi_ec_remove_query_handlers(ec, true, 0);
  1321. clear_bit(EC_FLAGS_EVT_HANDLER_INSTALLED, &ec->flags);
  1322. }
  1323. }
  1324. static int acpi_ec_setup(struct acpi_ec *ec, bool handle_events)
  1325. {
  1326. int ret;
  1327. ret = ec_install_handlers(ec, handle_events);
  1328. if (ret)
  1329. return ret;
  1330. /* First EC capable of handling transactions */
  1331. if (!first_ec) {
  1332. first_ec = ec;
  1333. acpi_handle_info(first_ec->handle, "Used as first EC\n");
  1334. }
  1335. acpi_handle_info(ec->handle,
  1336. "GPE=0x%x, EC_CMD/EC_SC=0x%lx, EC_DATA=0x%lx\n",
  1337. ec->gpe, ec->command_addr, ec->data_addr);
  1338. return ret;
  1339. }
  1340. static int acpi_config_boot_ec(struct acpi_ec *ec, acpi_handle handle,
  1341. bool handle_events, bool is_ecdt)
  1342. {
  1343. int ret;
  1344. /*
  1345. * Changing the ACPI handle results in a re-configuration of the
  1346. * boot EC. And if it happens after the namespace initialization,
  1347. * it causes _REG evaluations.
  1348. */
  1349. if (boot_ec && boot_ec->handle != handle)
  1350. ec_remove_handlers(boot_ec);
  1351. /* Unset old boot EC */
  1352. if (boot_ec != ec)
  1353. acpi_ec_free(boot_ec);
  1354. /*
  1355. * ECDT device creation is split into acpi_ec_ecdt_probe() and
  1356. * acpi_ec_ecdt_start(). This function takes care of completing the
  1357. * ECDT parsing logic as the handle update should be performed
  1358. * between the installation/uninstallation of the handlers.
  1359. */
  1360. if (ec->handle != handle)
  1361. ec->handle = handle;
  1362. ret = acpi_ec_setup(ec, handle_events);
  1363. if (ret)
  1364. return ret;
  1365. /* Set new boot EC */
  1366. if (!boot_ec) {
  1367. boot_ec = ec;
  1368. boot_ec_is_ecdt = is_ecdt;
  1369. }
  1370. acpi_handle_info(boot_ec->handle,
  1371. "Used as boot %s EC to handle transactions%s\n",
  1372. is_ecdt ? "ECDT" : "DSDT",
  1373. handle_events ? " and events" : "");
  1374. return ret;
  1375. }
  1376. static bool acpi_ec_ecdt_get_handle(acpi_handle *phandle)
  1377. {
  1378. struct acpi_table_ecdt *ecdt_ptr;
  1379. acpi_status status;
  1380. acpi_handle handle;
  1381. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  1382. (struct acpi_table_header **)&ecdt_ptr);
  1383. if (ACPI_FAILURE(status))
  1384. return false;
  1385. status = acpi_get_handle(NULL, ecdt_ptr->id, &handle);
  1386. if (ACPI_FAILURE(status))
  1387. return false;
  1388. *phandle = handle;
  1389. return true;
  1390. }
  1391. static bool acpi_is_boot_ec(struct acpi_ec *ec)
  1392. {
  1393. if (!boot_ec)
  1394. return false;
  1395. if (ec->command_addr == boot_ec->command_addr &&
  1396. ec->data_addr == boot_ec->data_addr)
  1397. return true;
  1398. return false;
  1399. }
  1400. static int acpi_ec_add(struct acpi_device *device)
  1401. {
  1402. struct acpi_ec *ec = NULL;
  1403. int ret;
  1404. bool is_ecdt = false;
  1405. acpi_status status;
  1406. strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
  1407. strcpy(acpi_device_class(device), ACPI_EC_CLASS);
  1408. if (!strcmp(acpi_device_hid(device), ACPI_ECDT_HID)) {
  1409. is_ecdt = true;
  1410. ec = boot_ec;
  1411. } else {
  1412. ec = acpi_ec_alloc();
  1413. if (!ec)
  1414. return -ENOMEM;
  1415. status = ec_parse_device(device->handle, 0, ec, NULL);
  1416. if (status != AE_CTRL_TERMINATE) {
  1417. ret = -EINVAL;
  1418. goto err_alloc;
  1419. }
  1420. }
  1421. if (acpi_is_boot_ec(ec)) {
  1422. boot_ec_is_ecdt = is_ecdt;
  1423. if (!is_ecdt) {
  1424. /*
  1425. * Trust PNP0C09 namespace location rather than
  1426. * ECDT ID. But trust ECDT GPE rather than _GPE
  1427. * because of ASUS quirks, so do not change
  1428. * boot_ec->gpe to ec->gpe.
  1429. */
  1430. boot_ec->handle = ec->handle;
  1431. acpi_handle_debug(ec->handle, "duplicated.\n");
  1432. acpi_ec_free(ec);
  1433. ec = boot_ec;
  1434. }
  1435. ret = acpi_config_boot_ec(ec, ec->handle, true, is_ecdt);
  1436. } else
  1437. ret = acpi_ec_setup(ec, true);
  1438. if (ret)
  1439. goto err_query;
  1440. device->driver_data = ec;
  1441. ret = !!request_region(ec->data_addr, 1, "EC data");
  1442. WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
  1443. ret = !!request_region(ec->command_addr, 1, "EC cmd");
  1444. WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
  1445. if (!is_ecdt) {
  1446. /* Reprobe devices depending on the EC */
  1447. acpi_walk_dep_device_list(ec->handle);
  1448. }
  1449. acpi_handle_debug(ec->handle, "enumerated.\n");
  1450. return 0;
  1451. err_query:
  1452. if (ec != boot_ec)
  1453. acpi_ec_remove_query_handlers(ec, true, 0);
  1454. err_alloc:
  1455. if (ec != boot_ec)
  1456. acpi_ec_free(ec);
  1457. return ret;
  1458. }
  1459. static int acpi_ec_remove(struct acpi_device *device)
  1460. {
  1461. struct acpi_ec *ec;
  1462. if (!device)
  1463. return -EINVAL;
  1464. ec = acpi_driver_data(device);
  1465. release_region(ec->data_addr, 1);
  1466. release_region(ec->command_addr, 1);
  1467. device->driver_data = NULL;
  1468. if (ec != boot_ec) {
  1469. ec_remove_handlers(ec);
  1470. acpi_ec_free(ec);
  1471. }
  1472. return 0;
  1473. }
  1474. static acpi_status
  1475. ec_parse_io_ports(struct acpi_resource *resource, void *context)
  1476. {
  1477. struct acpi_ec *ec = context;
  1478. if (resource->type != ACPI_RESOURCE_TYPE_IO)
  1479. return AE_OK;
  1480. /*
  1481. * The first address region returned is the data port, and
  1482. * the second address region returned is the status/command
  1483. * port.
  1484. */
  1485. if (ec->data_addr == 0)
  1486. ec->data_addr = resource->data.io.minimum;
  1487. else if (ec->command_addr == 0)
  1488. ec->command_addr = resource->data.io.minimum;
  1489. else
  1490. return AE_CTRL_TERMINATE;
  1491. return AE_OK;
  1492. }
  1493. static const struct acpi_device_id ec_device_ids[] = {
  1494. {"PNP0C09", 0},
  1495. {ACPI_ECDT_HID, 0},
  1496. {"", 0},
  1497. };
  1498. /*
  1499. * This function is not Windows-compatible as Windows never enumerates the
  1500. * namespace EC before the main ACPI device enumeration process. It is
  1501. * retained for historical reason and will be deprecated in the future.
  1502. */
  1503. int __init acpi_ec_dsdt_probe(void)
  1504. {
  1505. acpi_status status;
  1506. struct acpi_ec *ec;
  1507. int ret;
  1508. /*
  1509. * If a platform has ECDT, there is no need to proceed as the
  1510. * following probe is not a part of the ACPI device enumeration,
  1511. * executing _STA is not safe, and thus this probe may risk of
  1512. * picking up an invalid EC device.
  1513. */
  1514. if (boot_ec)
  1515. return -ENODEV;
  1516. ec = acpi_ec_alloc();
  1517. if (!ec)
  1518. return -ENOMEM;
  1519. /*
  1520. * At this point, the namespace is initialized, so start to find
  1521. * the namespace objects.
  1522. */
  1523. status = acpi_get_devices(ec_device_ids[0].id,
  1524. ec_parse_device, ec, NULL);
  1525. if (ACPI_FAILURE(status) || !ec->handle) {
  1526. ret = -ENODEV;
  1527. goto error;
  1528. }
  1529. /*
  1530. * When the DSDT EC is available, always re-configure boot EC to
  1531. * have _REG evaluated. _REG can only be evaluated after the
  1532. * namespace initialization.
  1533. * At this point, the GPE is not fully initialized, so do not to
  1534. * handle the events.
  1535. */
  1536. ret = acpi_config_boot_ec(ec, ec->handle, false, false);
  1537. error:
  1538. if (ret)
  1539. acpi_ec_free(ec);
  1540. return ret;
  1541. }
  1542. /*
  1543. * If the DSDT EC is not functioning, we still need to prepare a fully
  1544. * functioning ECDT EC first in order to handle the events.
  1545. * https://bugzilla.kernel.org/show_bug.cgi?id=115021
  1546. */
  1547. static int __init acpi_ec_ecdt_start(void)
  1548. {
  1549. acpi_handle handle;
  1550. if (!boot_ec)
  1551. return -ENODEV;
  1552. /* In case acpi_ec_ecdt_start() is called after acpi_ec_add() */
  1553. if (!boot_ec_is_ecdt)
  1554. return -ENODEV;
  1555. /*
  1556. * At this point, the namespace and the GPE is initialized, so
  1557. * start to find the namespace objects and handle the events.
  1558. *
  1559. * Note: ec->handle can be valid if this function is called after
  1560. * acpi_ec_add(), hence the fast path.
  1561. */
  1562. if (boot_ec->handle == ACPI_ROOT_OBJECT) {
  1563. if (!acpi_ec_ecdt_get_handle(&handle))
  1564. return -ENODEV;
  1565. boot_ec->handle = handle;
  1566. }
  1567. /* Register to ACPI bus with PM ops attached */
  1568. return acpi_bus_register_early_device(ACPI_BUS_TYPE_ECDT_EC);
  1569. }
  1570. #if 0
  1571. /*
  1572. * Some EC firmware variations refuses to respond QR_EC when SCI_EVT is not
  1573. * set, for which case, we complete the QR_EC without issuing it to the
  1574. * firmware.
  1575. * https://bugzilla.kernel.org/show_bug.cgi?id=82611
  1576. * https://bugzilla.kernel.org/show_bug.cgi?id=97381
  1577. */
  1578. static int ec_flag_query_handshake(const struct dmi_system_id *id)
  1579. {
  1580. pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
  1581. EC_FLAGS_QUERY_HANDSHAKE = 1;
  1582. return 0;
  1583. }
  1584. #endif
  1585. /*
  1586. * Some ECDTs contain wrong register addresses.
  1587. * MSI MS-171F
  1588. * https://bugzilla.kernel.org/show_bug.cgi?id=12461
  1589. */
  1590. static int ec_correct_ecdt(const struct dmi_system_id *id)
  1591. {
  1592. pr_debug("Detected system needing ECDT address correction.\n");
  1593. EC_FLAGS_CORRECT_ECDT = 1;
  1594. return 0;
  1595. }
  1596. /*
  1597. * Some DSDTs contain wrong GPE setting.
  1598. * Asus FX502VD/VE, GL702VMK, X550VXK, X580VD
  1599. * https://bugzilla.kernel.org/show_bug.cgi?id=195651
  1600. */
  1601. static int ec_honor_ecdt_gpe(const struct dmi_system_id *id)
  1602. {
  1603. pr_debug("Detected system needing ignore DSDT GPE setting.\n");
  1604. EC_FLAGS_IGNORE_DSDT_GPE = 1;
  1605. return 0;
  1606. }
  1607. static const struct dmi_system_id ec_dmi_table[] __initconst = {
  1608. {
  1609. ec_correct_ecdt, "MSI MS-171F", {
  1610. DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star"),
  1611. DMI_MATCH(DMI_PRODUCT_NAME, "MS-171F"),}, NULL},
  1612. {
  1613. ec_honor_ecdt_gpe, "ASUS FX502VD", {
  1614. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1615. DMI_MATCH(DMI_PRODUCT_NAME, "FX502VD"),}, NULL},
  1616. {
  1617. ec_honor_ecdt_gpe, "ASUS FX502VE", {
  1618. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1619. DMI_MATCH(DMI_PRODUCT_NAME, "FX502VE"),}, NULL},
  1620. {
  1621. ec_honor_ecdt_gpe, "ASUS GL702VMK", {
  1622. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1623. DMI_MATCH(DMI_PRODUCT_NAME, "GL702VMK"),}, NULL},
  1624. {
  1625. ec_honor_ecdt_gpe, "ASUS X550VXK", {
  1626. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1627. DMI_MATCH(DMI_PRODUCT_NAME, "X550VXK"),}, NULL},
  1628. {
  1629. ec_honor_ecdt_gpe, "ASUS X580VD", {
  1630. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  1631. DMI_MATCH(DMI_PRODUCT_NAME, "X580VD"),}, NULL},
  1632. {},
  1633. };
  1634. int __init acpi_ec_ecdt_probe(void)
  1635. {
  1636. int ret;
  1637. acpi_status status;
  1638. struct acpi_table_ecdt *ecdt_ptr;
  1639. struct acpi_ec *ec;
  1640. ec = acpi_ec_alloc();
  1641. if (!ec)
  1642. return -ENOMEM;
  1643. /*
  1644. * Generate a boot ec context
  1645. */
  1646. dmi_check_system(ec_dmi_table);
  1647. status = acpi_get_table(ACPI_SIG_ECDT, 1,
  1648. (struct acpi_table_header **)&ecdt_ptr);
  1649. if (ACPI_FAILURE(status)) {
  1650. ret = -ENODEV;
  1651. goto error;
  1652. }
  1653. if (!ecdt_ptr->control.address || !ecdt_ptr->data.address) {
  1654. /*
  1655. * Asus X50GL:
  1656. * https://bugzilla.kernel.org/show_bug.cgi?id=11880
  1657. */
  1658. ret = -ENODEV;
  1659. goto error;
  1660. }
  1661. if (EC_FLAGS_CORRECT_ECDT) {
  1662. ec->command_addr = ecdt_ptr->data.address;
  1663. ec->data_addr = ecdt_ptr->control.address;
  1664. } else {
  1665. ec->command_addr = ecdt_ptr->control.address;
  1666. ec->data_addr = ecdt_ptr->data.address;
  1667. }
  1668. ec->gpe = ecdt_ptr->gpe;
  1669. /*
  1670. * At this point, the namespace is not initialized, so do not find
  1671. * the namespace objects, or handle the events.
  1672. */
  1673. ret = acpi_config_boot_ec(ec, ACPI_ROOT_OBJECT, false, true);
  1674. error:
  1675. if (ret)
  1676. acpi_ec_free(ec);
  1677. return ret;
  1678. }
  1679. #ifdef CONFIG_PM_SLEEP
  1680. static int acpi_ec_suspend(struct device *dev)
  1681. {
  1682. struct acpi_ec *ec =
  1683. acpi_driver_data(to_acpi_device(dev));
  1684. if (acpi_sleep_no_ec_events() && ec_freeze_events)
  1685. acpi_ec_disable_event(ec);
  1686. return 0;
  1687. }
  1688. static int acpi_ec_suspend_noirq(struct device *dev)
  1689. {
  1690. struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev));
  1691. /*
  1692. * The SCI handler doesn't run at this point, so the GPE can be
  1693. * masked at the low level without side effects.
  1694. */
  1695. if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) &&
  1696. ec->reference_count >= 1)
  1697. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
  1698. if (acpi_sleep_no_ec_events())
  1699. acpi_ec_enter_noirq(ec);
  1700. return 0;
  1701. }
  1702. static int acpi_ec_resume_noirq(struct device *dev)
  1703. {
  1704. struct acpi_ec *ec = acpi_driver_data(to_acpi_device(dev));
  1705. if (acpi_sleep_no_ec_events())
  1706. acpi_ec_leave_noirq(ec);
  1707. if (ec_no_wakeup && test_bit(EC_FLAGS_STARTED, &ec->flags) &&
  1708. ec->reference_count >= 1)
  1709. acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
  1710. return 0;
  1711. }
  1712. static int acpi_ec_resume(struct device *dev)
  1713. {
  1714. struct acpi_ec *ec =
  1715. acpi_driver_data(to_acpi_device(dev));
  1716. acpi_ec_enable_event(ec);
  1717. return 0;
  1718. }
  1719. #endif
  1720. static const struct dev_pm_ops acpi_ec_pm = {
  1721. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend_noirq, acpi_ec_resume_noirq)
  1722. SET_SYSTEM_SLEEP_PM_OPS(acpi_ec_suspend, acpi_ec_resume)
  1723. };
  1724. static int param_set_event_clearing(const char *val,
  1725. const struct kernel_param *kp)
  1726. {
  1727. int result = 0;
  1728. if (!strncmp(val, "status", sizeof("status") - 1)) {
  1729. ec_event_clearing = ACPI_EC_EVT_TIMING_STATUS;
  1730. pr_info("Assuming SCI_EVT clearing on EC_SC accesses\n");
  1731. } else if (!strncmp(val, "query", sizeof("query") - 1)) {
  1732. ec_event_clearing = ACPI_EC_EVT_TIMING_QUERY;
  1733. pr_info("Assuming SCI_EVT clearing on QR_EC writes\n");
  1734. } else if (!strncmp(val, "event", sizeof("event") - 1)) {
  1735. ec_event_clearing = ACPI_EC_EVT_TIMING_EVENT;
  1736. pr_info("Assuming SCI_EVT clearing on event reads\n");
  1737. } else
  1738. result = -EINVAL;
  1739. return result;
  1740. }
  1741. static int param_get_event_clearing(char *buffer,
  1742. const struct kernel_param *kp)
  1743. {
  1744. switch (ec_event_clearing) {
  1745. case ACPI_EC_EVT_TIMING_STATUS:
  1746. return sprintf(buffer, "status");
  1747. case ACPI_EC_EVT_TIMING_QUERY:
  1748. return sprintf(buffer, "query");
  1749. case ACPI_EC_EVT_TIMING_EVENT:
  1750. return sprintf(buffer, "event");
  1751. default:
  1752. return sprintf(buffer, "invalid");
  1753. }
  1754. return 0;
  1755. }
  1756. module_param_call(ec_event_clearing, param_set_event_clearing, param_get_event_clearing,
  1757. NULL, 0644);
  1758. MODULE_PARM_DESC(ec_event_clearing, "Assumed SCI_EVT clearing timing");
  1759. static struct acpi_driver acpi_ec_driver = {
  1760. .name = "ec",
  1761. .class = ACPI_EC_CLASS,
  1762. .ids = ec_device_ids,
  1763. .ops = {
  1764. .add = acpi_ec_add,
  1765. .remove = acpi_ec_remove,
  1766. },
  1767. .drv.pm = &acpi_ec_pm,
  1768. };
  1769. static inline int acpi_ec_query_init(void)
  1770. {
  1771. if (!ec_query_wq) {
  1772. ec_query_wq = alloc_workqueue("kec_query", 0,
  1773. ec_max_queries);
  1774. if (!ec_query_wq)
  1775. return -ENODEV;
  1776. }
  1777. return 0;
  1778. }
  1779. static inline void acpi_ec_query_exit(void)
  1780. {
  1781. if (ec_query_wq) {
  1782. destroy_workqueue(ec_query_wq);
  1783. ec_query_wq = NULL;
  1784. }
  1785. }
  1786. static const struct dmi_system_id acpi_ec_no_wakeup[] = {
  1787. {
  1788. .ident = "Thinkpad X1 Carbon 6th",
  1789. .matches = {
  1790. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1791. DMI_MATCH(DMI_PRODUCT_FAMILY, "Thinkpad X1 Carbon 6th"),
  1792. },
  1793. },
  1794. {
  1795. .ident = "ThinkPad X1 Carbon 6th",
  1796. .matches = {
  1797. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1798. DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Carbon 6th"),
  1799. },
  1800. },
  1801. {
  1802. .ident = "ThinkPad X1 Yoga 3rd",
  1803. .matches = {
  1804. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  1805. DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Yoga 3rd"),
  1806. },
  1807. },
  1808. { },
  1809. };
  1810. int __init acpi_ec_init(void)
  1811. {
  1812. int result;
  1813. int ecdt_fail, dsdt_fail;
  1814. /* register workqueue for _Qxx evaluations */
  1815. result = acpi_ec_query_init();
  1816. if (result)
  1817. return result;
  1818. /*
  1819. * Disable EC wakeup on following systems to prevent periodic
  1820. * wakeup from EC GPE.
  1821. */
  1822. if (dmi_check_system(acpi_ec_no_wakeup)) {
  1823. ec_no_wakeup = true;
  1824. pr_debug("Disabling EC wakeup on suspend-to-idle\n");
  1825. }
  1826. /* Drivers must be started after acpi_ec_query_init() */
  1827. dsdt_fail = acpi_bus_register_driver(&acpi_ec_driver);
  1828. /*
  1829. * Register ECDT to ACPI bus only when PNP0C09 probe fails. This is
  1830. * useful for platforms (confirmed on ASUS X550ZE) with valid ECDT
  1831. * settings but invalid DSDT settings.
  1832. * https://bugzilla.kernel.org/show_bug.cgi?id=196847
  1833. */
  1834. ecdt_fail = acpi_ec_ecdt_start();
  1835. return ecdt_fail && dsdt_fail ? -ENODEV : 0;
  1836. }
  1837. /* EC driver currently not unloadable */
  1838. #if 0
  1839. static void __exit acpi_ec_exit(void)
  1840. {
  1841. acpi_bus_unregister_driver(&acpi_ec_driver);
  1842. acpi_ec_query_exit();
  1843. }
  1844. #endif /* 0 */