evgpe.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /******************************************************************************
  2. *
  3. * Module Name: evgpe - General Purpose Event handling and dispatch
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2010, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acevents.h"
  45. #include "acnamesp.h"
  46. #define _COMPONENT ACPI_EVENTS
  47. ACPI_MODULE_NAME("evgpe")
  48. /* Local prototypes */
  49. static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context);
  50. /*******************************************************************************
  51. *
  52. * FUNCTION: acpi_ev_update_gpe_enable_masks
  53. *
  54. * PARAMETERS: gpe_event_info - GPE to update
  55. *
  56. * RETURN: Status
  57. *
  58. * DESCRIPTION: Updates GPE register enable masks based on the GPE type
  59. *
  60. ******************************************************************************/
  61. acpi_status
  62. acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info)
  63. {
  64. struct acpi_gpe_register_info *gpe_register_info;
  65. u8 register_bit;
  66. ACPI_FUNCTION_TRACE(ev_update_gpe_enable_masks);
  67. gpe_register_info = gpe_event_info->register_info;
  68. if (!gpe_register_info) {
  69. return_ACPI_STATUS(AE_NOT_EXIST);
  70. }
  71. register_bit = (u8)
  72. (1 <<
  73. (gpe_event_info->gpe_number - gpe_register_info->base_gpe_number));
  74. ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake, register_bit);
  75. ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
  76. if (gpe_event_info->runtime_count)
  77. ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
  78. if (gpe_event_info->wakeup_count)
  79. ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
  80. return_ACPI_STATUS(AE_OK);
  81. }
  82. /*******************************************************************************
  83. *
  84. * FUNCTION: acpi_ev_enable_gpe
  85. *
  86. * PARAMETERS: gpe_event_info - GPE to enable
  87. *
  88. * RETURN: Status
  89. *
  90. * DESCRIPTION: Enable a GPE based on the GPE type
  91. *
  92. ******************************************************************************/
  93. acpi_status acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
  94. {
  95. acpi_status status;
  96. ACPI_FUNCTION_TRACE(ev_enable_gpe);
  97. /* Make sure HW enable masks are updated */
  98. status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
  99. if (ACPI_FAILURE(status))
  100. return_ACPI_STATUS(status);
  101. /* Clear the GPE (of stale events), then enable it */
  102. status = acpi_hw_clear_gpe(gpe_event_info);
  103. if (ACPI_FAILURE(status))
  104. return_ACPI_STATUS(status);
  105. /* Enable the requested GPE */
  106. status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
  107. return_ACPI_STATUS(status);
  108. }
  109. /*******************************************************************************
  110. *
  111. * FUNCTION: acpi_ev_disable_gpe
  112. *
  113. * PARAMETERS: gpe_event_info - GPE to disable
  114. *
  115. * RETURN: Status
  116. *
  117. * DESCRIPTION: Disable a GPE based on the GPE type
  118. *
  119. ******************************************************************************/
  120. acpi_status acpi_ev_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
  121. {
  122. acpi_status status;
  123. ACPI_FUNCTION_TRACE(ev_disable_gpe);
  124. /* Make sure HW enable masks are updated */
  125. status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
  126. if (ACPI_FAILURE(status))
  127. return_ACPI_STATUS(status);
  128. /*
  129. * Even if we don't know the GPE type, make sure that we always
  130. * disable it. low_disable_gpe will just clear the enable bit for this
  131. * GPE and write it. It will not write out the current GPE enable mask,
  132. * since this may inadvertently enable GPEs too early, if a rogue GPE has
  133. * come in during ACPICA initialization - possibly as a result of AML or
  134. * other code that has enabled the GPE.
  135. */
  136. status = acpi_hw_low_disable_gpe(gpe_event_info);
  137. return_ACPI_STATUS(status);
  138. }
  139. /*******************************************************************************
  140. *
  141. * FUNCTION: acpi_ev_get_gpe_event_info
  142. *
  143. * PARAMETERS: gpe_device - Device node. NULL for GPE0/GPE1
  144. * gpe_number - Raw GPE number
  145. *
  146. * RETURN: A GPE event_info struct. NULL if not a valid GPE
  147. *
  148. * DESCRIPTION: Returns the event_info struct associated with this GPE.
  149. * Validates the gpe_block and the gpe_number
  150. *
  151. * Should be called only when the GPE lists are semaphore locked
  152. * and not subject to change.
  153. *
  154. ******************************************************************************/
  155. struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
  156. u32 gpe_number)
  157. {
  158. union acpi_operand_object *obj_desc;
  159. struct acpi_gpe_block_info *gpe_block;
  160. u32 i;
  161. ACPI_FUNCTION_ENTRY();
  162. /* A NULL gpe_block means use the FADT-defined GPE block(s) */
  163. if (!gpe_device) {
  164. /* Examine GPE Block 0 and 1 (These blocks are permanent) */
  165. for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
  166. gpe_block = acpi_gbl_gpe_fadt_blocks[i];
  167. if (gpe_block) {
  168. if ((gpe_number >= gpe_block->block_base_number)
  169. && (gpe_number <
  170. gpe_block->block_base_number +
  171. (gpe_block->register_count * 8))) {
  172. return (&gpe_block->
  173. event_info[gpe_number -
  174. gpe_block->
  175. block_base_number]);
  176. }
  177. }
  178. }
  179. /* The gpe_number was not in the range of either FADT GPE block */
  180. return (NULL);
  181. }
  182. /* A Non-NULL gpe_device means this is a GPE Block Device */
  183. obj_desc = acpi_ns_get_attached_object((struct acpi_namespace_node *)
  184. gpe_device);
  185. if (!obj_desc || !obj_desc->device.gpe_block) {
  186. return (NULL);
  187. }
  188. gpe_block = obj_desc->device.gpe_block;
  189. if ((gpe_number >= gpe_block->block_base_number) &&
  190. (gpe_number <
  191. gpe_block->block_base_number + (gpe_block->register_count * 8))) {
  192. return (&gpe_block->
  193. event_info[gpe_number - gpe_block->block_base_number]);
  194. }
  195. return (NULL);
  196. }
  197. /*******************************************************************************
  198. *
  199. * FUNCTION: acpi_ev_gpe_detect
  200. *
  201. * PARAMETERS: gpe_xrupt_list - Interrupt block for this interrupt.
  202. * Can have multiple GPE blocks attached.
  203. *
  204. * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
  205. *
  206. * DESCRIPTION: Detect if any GP events have occurred. This function is
  207. * executed at interrupt level.
  208. *
  209. ******************************************************************************/
  210. u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
  211. {
  212. acpi_status status;
  213. struct acpi_gpe_block_info *gpe_block;
  214. struct acpi_gpe_register_info *gpe_register_info;
  215. u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
  216. u8 enabled_status_byte;
  217. u32 status_reg;
  218. u32 enable_reg;
  219. acpi_cpu_flags flags;
  220. u32 i;
  221. u32 j;
  222. ACPI_FUNCTION_NAME(ev_gpe_detect);
  223. /* Check for the case where there are no GPEs */
  224. if (!gpe_xrupt_list) {
  225. return (int_status);
  226. }
  227. /*
  228. * We need to obtain the GPE lock for both the data structs and registers
  229. * Note: Not necessary to obtain the hardware lock, since the GPE
  230. * registers are owned by the gpe_lock.
  231. */
  232. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  233. /* Examine all GPE blocks attached to this interrupt level */
  234. gpe_block = gpe_xrupt_list->gpe_block_list_head;
  235. while (gpe_block) {
  236. /*
  237. * Read all of the 8-bit GPE status and enable registers in this GPE
  238. * block, saving all of them. Find all currently active GP events.
  239. */
  240. for (i = 0; i < gpe_block->register_count; i++) {
  241. /* Get the next status/enable pair */
  242. gpe_register_info = &gpe_block->register_info[i];
  243. /* Read the Status Register */
  244. status =
  245. acpi_hw_read(&status_reg,
  246. &gpe_register_info->status_address);
  247. if (ACPI_FAILURE(status)) {
  248. goto unlock_and_exit;
  249. }
  250. /* Read the Enable Register */
  251. status =
  252. acpi_hw_read(&enable_reg,
  253. &gpe_register_info->enable_address);
  254. if (ACPI_FAILURE(status)) {
  255. goto unlock_and_exit;
  256. }
  257. ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
  258. "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
  259. gpe_register_info->base_gpe_number,
  260. status_reg, enable_reg));
  261. /* Check if there is anything active at all in this register */
  262. enabled_status_byte = (u8) (status_reg & enable_reg);
  263. if (!enabled_status_byte) {
  264. /* No active GPEs in this register, move on */
  265. continue;
  266. }
  267. /* Now look at the individual GPEs in this byte register */
  268. for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
  269. /* Examine one GPE bit */
  270. if (enabled_status_byte & (1 << j)) {
  271. /*
  272. * Found an active GPE. Dispatch the event to a handler
  273. * or method.
  274. */
  275. int_status |=
  276. acpi_ev_gpe_dispatch(&gpe_block->
  277. event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
  278. }
  279. }
  280. }
  281. gpe_block = gpe_block->next;
  282. }
  283. unlock_and_exit:
  284. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  285. return (int_status);
  286. }
  287. /*******************************************************************************
  288. *
  289. * FUNCTION: acpi_ev_asynch_execute_gpe_method
  290. *
  291. * PARAMETERS: Context (gpe_event_info) - Info for this GPE
  292. *
  293. * RETURN: None
  294. *
  295. * DESCRIPTION: Perform the actual execution of a GPE control method. This
  296. * function is called from an invocation of acpi_os_execute and
  297. * therefore does NOT execute at interrupt level - so that
  298. * the control method itself is not executed in the context of
  299. * an interrupt handler.
  300. *
  301. ******************************************************************************/
  302. static void acpi_ev_asynch_enable_gpe(void *context);
  303. static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
  304. {
  305. struct acpi_gpe_event_info *gpe_event_info = (void *)context;
  306. acpi_status status;
  307. struct acpi_gpe_event_info local_gpe_event_info;
  308. struct acpi_evaluate_info *info;
  309. ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
  310. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  311. if (ACPI_FAILURE(status)) {
  312. return_VOID;
  313. }
  314. /* Must revalidate the gpe_number/gpe_block */
  315. if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
  316. status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  317. return_VOID;
  318. }
  319. /* Set the GPE flags for return to enabled state */
  320. (void)acpi_ev_update_gpe_enable_masks(gpe_event_info);
  321. /*
  322. * Take a snapshot of the GPE info for this level - we copy the info to
  323. * prevent a race condition with remove_handler/remove_block.
  324. */
  325. ACPI_MEMCPY(&local_gpe_event_info, gpe_event_info,
  326. sizeof(struct acpi_gpe_event_info));
  327. status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  328. if (ACPI_FAILURE(status)) {
  329. return_VOID;
  330. }
  331. /*
  332. * Must check for control method type dispatch one more time to avoid a
  333. * race with ev_gpe_install_handler
  334. */
  335. if ((local_gpe_event_info.flags & ACPI_GPE_DISPATCH_MASK) ==
  336. ACPI_GPE_DISPATCH_METHOD) {
  337. /* Allocate the evaluation information block */
  338. info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
  339. if (!info) {
  340. status = AE_NO_MEMORY;
  341. } else {
  342. /*
  343. * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
  344. * control method that corresponds to this GPE
  345. */
  346. info->prefix_node =
  347. local_gpe_event_info.dispatch.method_node;
  348. info->flags = ACPI_IGNORE_RETURN_VALUE;
  349. status = acpi_ns_evaluate(info);
  350. ACPI_FREE(info);
  351. }
  352. if (ACPI_FAILURE(status)) {
  353. ACPI_EXCEPTION((AE_INFO, status,
  354. "while evaluating GPE method [%4.4s]",
  355. acpi_ut_get_node_name
  356. (local_gpe_event_info.dispatch.
  357. method_node)));
  358. }
  359. }
  360. /* Defer enabling of GPE until all notify handlers are done */
  361. acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_ev_asynch_enable_gpe,
  362. gpe_event_info);
  363. return_VOID;
  364. }
  365. static void acpi_ev_asynch_enable_gpe(void *context)
  366. {
  367. struct acpi_gpe_event_info *gpe_event_info = context;
  368. acpi_status status;
  369. if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
  370. ACPI_GPE_LEVEL_TRIGGERED) {
  371. /*
  372. * GPE is level-triggered, we clear the GPE status bit after handling
  373. * the event.
  374. */
  375. status = acpi_hw_clear_gpe(gpe_event_info);
  376. if (ACPI_FAILURE(status)) {
  377. return_VOID;
  378. }
  379. }
  380. /* Enable this GPE */
  381. (void)acpi_hw_write_gpe_enable_reg(gpe_event_info);
  382. return_VOID;
  383. }
  384. /*******************************************************************************
  385. *
  386. * FUNCTION: acpi_ev_gpe_dispatch
  387. *
  388. * PARAMETERS: gpe_event_info - Info for this GPE
  389. * gpe_number - Number relative to the parent GPE block
  390. *
  391. * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
  392. *
  393. * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
  394. * or method (e.g. _Lxx/_Exx) handler.
  395. *
  396. * This function executes at interrupt level.
  397. *
  398. ******************************************************************************/
  399. u32
  400. acpi_ev_gpe_dispatch(struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
  401. {
  402. acpi_status status;
  403. ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
  404. acpi_os_gpe_count(gpe_number);
  405. /*
  406. * If edge-triggered, clear the GPE status bit now. Note that
  407. * level-triggered events are cleared after the GPE is serviced.
  408. */
  409. if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
  410. ACPI_GPE_EDGE_TRIGGERED) {
  411. status = acpi_hw_clear_gpe(gpe_event_info);
  412. if (ACPI_FAILURE(status)) {
  413. ACPI_EXCEPTION((AE_INFO, status,
  414. "Unable to clear GPE[%2X]",
  415. gpe_number));
  416. return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
  417. }
  418. }
  419. /*
  420. * Dispatch the GPE to either an installed handler, or the control method
  421. * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
  422. * it and do not attempt to run the method. If there is neither a handler
  423. * nor a method, we disable this GPE to prevent further such pointless
  424. * events from firing.
  425. */
  426. switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
  427. case ACPI_GPE_DISPATCH_HANDLER:
  428. /*
  429. * Invoke the installed handler (at interrupt level)
  430. * Ignore return status for now.
  431. * TBD: leave GPE disabled on error?
  432. */
  433. (void)gpe_event_info->dispatch.handler->address(gpe_event_info->
  434. dispatch.
  435. handler->
  436. context);
  437. /* It is now safe to clear level-triggered events. */
  438. if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
  439. ACPI_GPE_LEVEL_TRIGGERED) {
  440. status = acpi_hw_clear_gpe(gpe_event_info);
  441. if (ACPI_FAILURE(status)) {
  442. ACPI_EXCEPTION((AE_INFO, status,
  443. "Unable to clear GPE[%2X]",
  444. gpe_number));
  445. return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
  446. }
  447. }
  448. break;
  449. case ACPI_GPE_DISPATCH_METHOD:
  450. /*
  451. * Disable the GPE, so it doesn't keep firing before the method has a
  452. * chance to run (it runs asynchronously with interrupts enabled).
  453. */
  454. status = acpi_ev_disable_gpe(gpe_event_info);
  455. if (ACPI_FAILURE(status)) {
  456. ACPI_EXCEPTION((AE_INFO, status,
  457. "Unable to disable GPE[%2X]",
  458. gpe_number));
  459. return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
  460. }
  461. /*
  462. * Execute the method associated with the GPE
  463. * NOTE: Level-triggered GPEs are cleared after the method completes.
  464. */
  465. status = acpi_os_execute(OSL_GPE_HANDLER,
  466. acpi_ev_asynch_execute_gpe_method,
  467. gpe_event_info);
  468. if (ACPI_FAILURE(status)) {
  469. ACPI_EXCEPTION((AE_INFO, status,
  470. "Unable to queue handler for GPE[%2X] - event disabled",
  471. gpe_number));
  472. }
  473. break;
  474. default:
  475. /* No handler or method to run! */
  476. ACPI_ERROR((AE_INFO,
  477. "No handler or method for GPE[%2X], disabling event",
  478. gpe_number));
  479. /*
  480. * Disable the GPE. The GPE will remain disabled until the ACPICA
  481. * Core Subsystem is restarted, or a handler is installed.
  482. */
  483. status = acpi_ev_disable_gpe(gpe_event_info);
  484. if (ACPI_FAILURE(status)) {
  485. ACPI_EXCEPTION((AE_INFO, status,
  486. "Unable to disable GPE[%2X]",
  487. gpe_number));
  488. return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
  489. }
  490. break;
  491. }
  492. return_UINT32(ACPI_INTERRUPT_HANDLED);
  493. }