ec.c 61 KB

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