evgpeutil.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /******************************************************************************
  2. *
  3. * Module Name: evgpeutil - GPE utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2011, 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_device
  131. *
  132. * PARAMETERS: GPE_WALK_CALLBACK
  133. *
  134. * RETURN: Status
  135. *
  136. * DESCRIPTION: Matches the input GPE index (0-current_gpe_count) with a GPE
  137. * block device. NULL if the GPE is one of the FADT-defined GPEs.
  138. *
  139. ******************************************************************************/
  140. acpi_status
  141. acpi_ev_get_gpe_device(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
  142. struct acpi_gpe_block_info *gpe_block, void *context)
  143. {
  144. struct acpi_gpe_device_info *info = context;
  145. /* Increment Index by the number of GPEs in this block */
  146. info->next_block_base_index += gpe_block->gpe_count;
  147. if (info->index < info->next_block_base_index) {
  148. /*
  149. * The GPE index is within this block, get the node. Leave the node
  150. * NULL for the FADT-defined GPEs
  151. */
  152. if ((gpe_block->node)->type == ACPI_TYPE_DEVICE) {
  153. info->gpe_device = gpe_block->node;
  154. }
  155. info->status = AE_OK;
  156. return (AE_CTRL_END);
  157. }
  158. return (AE_OK);
  159. }
  160. /*******************************************************************************
  161. *
  162. * FUNCTION: acpi_ev_get_gpe_xrupt_block
  163. *
  164. * PARAMETERS: interrupt_number - Interrupt for a GPE block
  165. *
  166. * RETURN: A GPE interrupt block
  167. *
  168. * DESCRIPTION: Get or Create a GPE interrupt block. There is one interrupt
  169. * block per unique interrupt level used for GPEs. Should be
  170. * called only when the GPE lists are semaphore locked and not
  171. * subject to change.
  172. *
  173. ******************************************************************************/
  174. struct acpi_gpe_xrupt_info *acpi_ev_get_gpe_xrupt_block(u32 interrupt_number)
  175. {
  176. struct acpi_gpe_xrupt_info *next_gpe_xrupt;
  177. struct acpi_gpe_xrupt_info *gpe_xrupt;
  178. acpi_status status;
  179. acpi_cpu_flags flags;
  180. ACPI_FUNCTION_TRACE(ev_get_gpe_xrupt_block);
  181. /* No need for lock since we are not changing any list elements here */
  182. next_gpe_xrupt = acpi_gbl_gpe_xrupt_list_head;
  183. while (next_gpe_xrupt) {
  184. if (next_gpe_xrupt->interrupt_number == interrupt_number) {
  185. return_PTR(next_gpe_xrupt);
  186. }
  187. next_gpe_xrupt = next_gpe_xrupt->next;
  188. }
  189. /* Not found, must allocate a new xrupt descriptor */
  190. gpe_xrupt = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_xrupt_info));
  191. if (!gpe_xrupt) {
  192. return_PTR(NULL);
  193. }
  194. gpe_xrupt->interrupt_number = interrupt_number;
  195. /* Install new interrupt descriptor with spin lock */
  196. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  197. if (acpi_gbl_gpe_xrupt_list_head) {
  198. next_gpe_xrupt = acpi_gbl_gpe_xrupt_list_head;
  199. while (next_gpe_xrupt->next) {
  200. next_gpe_xrupt = next_gpe_xrupt->next;
  201. }
  202. next_gpe_xrupt->next = gpe_xrupt;
  203. gpe_xrupt->previous = next_gpe_xrupt;
  204. } else {
  205. acpi_gbl_gpe_xrupt_list_head = gpe_xrupt;
  206. }
  207. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  208. /* Install new interrupt handler if not SCI_INT */
  209. if (interrupt_number != acpi_gbl_FADT.sci_interrupt) {
  210. status = acpi_os_install_interrupt_handler(interrupt_number,
  211. acpi_ev_gpe_xrupt_handler,
  212. gpe_xrupt);
  213. if (ACPI_FAILURE(status)) {
  214. ACPI_ERROR((AE_INFO,
  215. "Could not install GPE interrupt handler at level 0x%X",
  216. interrupt_number));
  217. return_PTR(NULL);
  218. }
  219. }
  220. return_PTR(gpe_xrupt);
  221. }
  222. /*******************************************************************************
  223. *
  224. * FUNCTION: acpi_ev_delete_gpe_xrupt
  225. *
  226. * PARAMETERS: gpe_xrupt - A GPE interrupt info block
  227. *
  228. * RETURN: Status
  229. *
  230. * DESCRIPTION: Remove and free a gpe_xrupt block. Remove an associated
  231. * interrupt handler if not the SCI interrupt.
  232. *
  233. ******************************************************************************/
  234. acpi_status acpi_ev_delete_gpe_xrupt(struct acpi_gpe_xrupt_info *gpe_xrupt)
  235. {
  236. acpi_status status;
  237. acpi_cpu_flags flags;
  238. ACPI_FUNCTION_TRACE(ev_delete_gpe_xrupt);
  239. /* We never want to remove the SCI interrupt handler */
  240. if (gpe_xrupt->interrupt_number == acpi_gbl_FADT.sci_interrupt) {
  241. gpe_xrupt->gpe_block_list_head = NULL;
  242. return_ACPI_STATUS(AE_OK);
  243. }
  244. /* Disable this interrupt */
  245. status =
  246. acpi_os_remove_interrupt_handler(gpe_xrupt->interrupt_number,
  247. acpi_ev_gpe_xrupt_handler);
  248. if (ACPI_FAILURE(status)) {
  249. return_ACPI_STATUS(status);
  250. }
  251. /* Unlink the interrupt block with lock */
  252. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  253. if (gpe_xrupt->previous) {
  254. gpe_xrupt->previous->next = gpe_xrupt->next;
  255. } else {
  256. /* No previous, update list head */
  257. acpi_gbl_gpe_xrupt_list_head = gpe_xrupt->next;
  258. }
  259. if (gpe_xrupt->next) {
  260. gpe_xrupt->next->previous = gpe_xrupt->previous;
  261. }
  262. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  263. /* Free the block */
  264. ACPI_FREE(gpe_xrupt);
  265. return_ACPI_STATUS(AE_OK);
  266. }
  267. /*******************************************************************************
  268. *
  269. * FUNCTION: acpi_ev_delete_gpe_handlers
  270. *
  271. * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
  272. * gpe_block - Gpe Block info
  273. *
  274. * RETURN: Status
  275. *
  276. * DESCRIPTION: Delete all Handler objects found in the GPE data structs.
  277. * Used only prior to termination.
  278. *
  279. ******************************************************************************/
  280. acpi_status
  281. acpi_ev_delete_gpe_handlers(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
  282. struct acpi_gpe_block_info *gpe_block,
  283. void *context)
  284. {
  285. struct acpi_gpe_event_info *gpe_event_info;
  286. u32 i;
  287. u32 j;
  288. ACPI_FUNCTION_TRACE(ev_delete_gpe_handlers);
  289. /* Examine each GPE Register within the block */
  290. for (i = 0; i < gpe_block->register_count; i++) {
  291. /* Now look at the individual GPEs in this byte register */
  292. for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
  293. gpe_event_info = &gpe_block->event_info[((acpi_size) i *
  294. ACPI_GPE_REGISTER_WIDTH)
  295. + j];
  296. if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
  297. ACPI_GPE_DISPATCH_HANDLER) {
  298. ACPI_FREE(gpe_event_info->dispatch.handler);
  299. gpe_event_info->dispatch.handler = NULL;
  300. gpe_event_info->flags &=
  301. ~ACPI_GPE_DISPATCH_MASK;
  302. }
  303. }
  304. }
  305. return_ACPI_STATUS(AE_OK);
  306. }