evgpeutil.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /******************************************************************************
  2. *
  3. * Module Name: evgpeutil - GPE utilities
  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. #define _COMPONENT ACPI_EVENTS
  46. ACPI_MODULE_NAME("evgpeutil")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_ev_walk_gpe_list
  50. *
  51. * PARAMETERS: gpe_walk_callback - Routine called for each GPE block
  52. * Context - Value passed to callback
  53. *
  54. * RETURN: Status
  55. *
  56. * DESCRIPTION: Walk the GPE lists.
  57. *
  58. ******************************************************************************/
  59. acpi_status
  60. acpi_ev_walk_gpe_list(acpi_gpe_callback gpe_walk_callback, void *context)
  61. {
  62. struct acpi_gpe_block_info *gpe_block;
  63. struct acpi_gpe_xrupt_info *gpe_xrupt_info;
  64. acpi_status status = AE_OK;
  65. acpi_cpu_flags flags;
  66. ACPI_FUNCTION_TRACE(ev_walk_gpe_list);
  67. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  68. /* Walk the interrupt level descriptor list */
  69. gpe_xrupt_info = acpi_gbl_gpe_xrupt_list_head;
  70. while (gpe_xrupt_info) {
  71. /* Walk all Gpe Blocks attached to this interrupt level */
  72. gpe_block = gpe_xrupt_info->gpe_block_list_head;
  73. while (gpe_block) {
  74. /* One callback per GPE block */
  75. status =
  76. gpe_walk_callback(gpe_xrupt_info, gpe_block,
  77. context);
  78. if (ACPI_FAILURE(status)) {
  79. if (status == AE_CTRL_END) { /* Callback abort */
  80. status = AE_OK;
  81. }
  82. goto unlock_and_exit;
  83. }
  84. gpe_block = gpe_block->next;
  85. }
  86. gpe_xrupt_info = gpe_xrupt_info->next;
  87. }
  88. unlock_and_exit:
  89. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  90. return_ACPI_STATUS(status);
  91. }
  92. /*******************************************************************************
  93. *
  94. * FUNCTION: acpi_ev_valid_gpe_event
  95. *
  96. * PARAMETERS: gpe_event_info - Info for this GPE
  97. *
  98. * RETURN: TRUE if the gpe_event is valid
  99. *
  100. * DESCRIPTION: Validate a GPE event. DO NOT CALL FROM INTERRUPT LEVEL.
  101. * Should be called only when the GPE lists are semaphore locked
  102. * and not subject to change.
  103. *
  104. ******************************************************************************/
  105. u8 acpi_ev_valid_gpe_event(struct acpi_gpe_event_info *gpe_event_info)
  106. {
  107. struct acpi_gpe_xrupt_info *gpe_xrupt_block;
  108. struct acpi_gpe_block_info *gpe_block;
  109. ACPI_FUNCTION_ENTRY();
  110. /* No need for spin lock since we are not changing any list elements */
  111. /* Walk the GPE interrupt levels */
  112. gpe_xrupt_block = acpi_gbl_gpe_xrupt_list_head;
  113. while (gpe_xrupt_block) {
  114. gpe_block = gpe_xrupt_block->gpe_block_list_head;
  115. /* Walk the GPE blocks on this interrupt level */
  116. while (gpe_block) {
  117. if ((&gpe_block->event_info[0] <= gpe_event_info) &&
  118. (&gpe_block->event_info[gpe_block->gpe_count] >
  119. gpe_event_info)) {
  120. return (TRUE);
  121. }
  122. gpe_block = gpe_block->next;
  123. }
  124. gpe_xrupt_block = gpe_xrupt_block->next;
  125. }
  126. return (FALSE);
  127. }
  128. /*******************************************************************************
  129. *
  130. * FUNCTION: acpi_ev_get_gpe_xrupt_block
  131. *
  132. * PARAMETERS: interrupt_number - Interrupt for a GPE block
  133. *
  134. * RETURN: A GPE interrupt block
  135. *
  136. * DESCRIPTION: Get or Create a GPE interrupt block. There is one interrupt
  137. * block per unique interrupt level used for GPEs. Should be
  138. * called only when the GPE lists are semaphore locked and not
  139. * subject to change.
  140. *
  141. ******************************************************************************/
  142. struct acpi_gpe_xrupt_info *acpi_ev_get_gpe_xrupt_block(u32 interrupt_number)
  143. {
  144. struct acpi_gpe_xrupt_info *next_gpe_xrupt;
  145. struct acpi_gpe_xrupt_info *gpe_xrupt;
  146. acpi_status status;
  147. acpi_cpu_flags flags;
  148. ACPI_FUNCTION_TRACE(ev_get_gpe_xrupt_block);
  149. /* No need for lock since we are not changing any list elements here */
  150. next_gpe_xrupt = acpi_gbl_gpe_xrupt_list_head;
  151. while (next_gpe_xrupt) {
  152. if (next_gpe_xrupt->interrupt_number == interrupt_number) {
  153. return_PTR(next_gpe_xrupt);
  154. }
  155. next_gpe_xrupt = next_gpe_xrupt->next;
  156. }
  157. /* Not found, must allocate a new xrupt descriptor */
  158. gpe_xrupt = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_xrupt_info));
  159. if (!gpe_xrupt) {
  160. return_PTR(NULL);
  161. }
  162. gpe_xrupt->interrupt_number = interrupt_number;
  163. /* Install new interrupt descriptor with spin lock */
  164. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  165. if (acpi_gbl_gpe_xrupt_list_head) {
  166. next_gpe_xrupt = acpi_gbl_gpe_xrupt_list_head;
  167. while (next_gpe_xrupt->next) {
  168. next_gpe_xrupt = next_gpe_xrupt->next;
  169. }
  170. next_gpe_xrupt->next = gpe_xrupt;
  171. gpe_xrupt->previous = next_gpe_xrupt;
  172. } else {
  173. acpi_gbl_gpe_xrupt_list_head = gpe_xrupt;
  174. }
  175. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  176. /* Install new interrupt handler if not SCI_INT */
  177. if (interrupt_number != acpi_gbl_FADT.sci_interrupt) {
  178. status = acpi_os_install_interrupt_handler(interrupt_number,
  179. acpi_ev_gpe_xrupt_handler,
  180. gpe_xrupt);
  181. if (ACPI_FAILURE(status)) {
  182. ACPI_ERROR((AE_INFO,
  183. "Could not install GPE interrupt handler at level 0x%X",
  184. interrupt_number));
  185. return_PTR(NULL);
  186. }
  187. }
  188. return_PTR(gpe_xrupt);
  189. }
  190. /*******************************************************************************
  191. *
  192. * FUNCTION: acpi_ev_delete_gpe_xrupt
  193. *
  194. * PARAMETERS: gpe_xrupt - A GPE interrupt info block
  195. *
  196. * RETURN: Status
  197. *
  198. * DESCRIPTION: Remove and free a gpe_xrupt block. Remove an associated
  199. * interrupt handler if not the SCI interrupt.
  200. *
  201. ******************************************************************************/
  202. acpi_status acpi_ev_delete_gpe_xrupt(struct acpi_gpe_xrupt_info *gpe_xrupt)
  203. {
  204. acpi_status status;
  205. acpi_cpu_flags flags;
  206. ACPI_FUNCTION_TRACE(ev_delete_gpe_xrupt);
  207. /* We never want to remove the SCI interrupt handler */
  208. if (gpe_xrupt->interrupt_number == acpi_gbl_FADT.sci_interrupt) {
  209. gpe_xrupt->gpe_block_list_head = NULL;
  210. return_ACPI_STATUS(AE_OK);
  211. }
  212. /* Disable this interrupt */
  213. status =
  214. acpi_os_remove_interrupt_handler(gpe_xrupt->interrupt_number,
  215. acpi_ev_gpe_xrupt_handler);
  216. if (ACPI_FAILURE(status)) {
  217. return_ACPI_STATUS(status);
  218. }
  219. /* Unlink the interrupt block with lock */
  220. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  221. if (gpe_xrupt->previous) {
  222. gpe_xrupt->previous->next = gpe_xrupt->next;
  223. } else {
  224. /* No previous, update list head */
  225. acpi_gbl_gpe_xrupt_list_head = gpe_xrupt->next;
  226. }
  227. if (gpe_xrupt->next) {
  228. gpe_xrupt->next->previous = gpe_xrupt->previous;
  229. }
  230. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  231. /* Free the block */
  232. ACPI_FREE(gpe_xrupt);
  233. return_ACPI_STATUS(AE_OK);
  234. }
  235. /*******************************************************************************
  236. *
  237. * FUNCTION: acpi_ev_delete_gpe_handlers
  238. *
  239. * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
  240. * gpe_block - Gpe Block info
  241. *
  242. * RETURN: Status
  243. *
  244. * DESCRIPTION: Delete all Handler objects found in the GPE data structs.
  245. * Used only prior to termination.
  246. *
  247. ******************************************************************************/
  248. acpi_status
  249. acpi_ev_delete_gpe_handlers(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
  250. struct acpi_gpe_block_info *gpe_block,
  251. void *context)
  252. {
  253. struct acpi_gpe_event_info *gpe_event_info;
  254. u32 i;
  255. u32 j;
  256. ACPI_FUNCTION_TRACE(ev_delete_gpe_handlers);
  257. /* Examine each GPE Register within the block */
  258. for (i = 0; i < gpe_block->register_count; i++) {
  259. /* Now look at the individual GPEs in this byte register */
  260. for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
  261. gpe_event_info = &gpe_block->event_info[((acpi_size) i *
  262. ACPI_GPE_REGISTER_WIDTH)
  263. + j];
  264. if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
  265. ACPI_GPE_DISPATCH_HANDLER) {
  266. ACPI_FREE(gpe_event_info->dispatch.handler);
  267. gpe_event_info->dispatch.handler = NULL;
  268. gpe_event_info->flags &=
  269. ~ACPI_GPE_DISPATCH_MASK;
  270. }
  271. }
  272. }
  273. return_ACPI_STATUS(AE_OK);
  274. }