evgpeutil.c 11 KB

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