evgpeutil.c 11 KB

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