evglock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: evglock - Global Lock support
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acevents.h"
  12. #include "acinterp.h"
  13. #define _COMPONENT ACPI_EVENTS
  14. ACPI_MODULE_NAME("evglock")
  15. #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
  16. /* Local prototypes */
  17. static u32 acpi_ev_global_lock_handler(void *context);
  18. /*******************************************************************************
  19. *
  20. * FUNCTION: acpi_ev_init_global_lock_handler
  21. *
  22. * PARAMETERS: None
  23. *
  24. * RETURN: Status
  25. *
  26. * DESCRIPTION: Install a handler for the global lock release event
  27. *
  28. ******************************************************************************/
  29. acpi_status acpi_ev_init_global_lock_handler(void)
  30. {
  31. acpi_status status;
  32. ACPI_FUNCTION_TRACE(ev_init_global_lock_handler);
  33. /* If Hardware Reduced flag is set, there is no global lock */
  34. if (acpi_gbl_reduced_hardware) {
  35. return_ACPI_STATUS(AE_OK);
  36. }
  37. /* Attempt installation of the global lock handler */
  38. status = acpi_install_fixed_event_handler(ACPI_EVENT_GLOBAL,
  39. acpi_ev_global_lock_handler,
  40. NULL);
  41. /*
  42. * If the global lock does not exist on this platform, the attempt to
  43. * enable GBL_STATUS will fail (the GBL_ENABLE bit will not stick).
  44. * Map to AE_OK, but mark global lock as not present. Any attempt to
  45. * actually use the global lock will be flagged with an error.
  46. */
  47. acpi_gbl_global_lock_present = FALSE;
  48. if (status == AE_NO_HARDWARE_RESPONSE) {
  49. ACPI_ERROR((AE_INFO,
  50. "No response from Global Lock hardware, disabling lock"));
  51. return_ACPI_STATUS(AE_OK);
  52. }
  53. status = acpi_os_create_lock(&acpi_gbl_global_lock_pending_lock);
  54. if (ACPI_FAILURE(status)) {
  55. return_ACPI_STATUS(status);
  56. }
  57. acpi_gbl_global_lock_pending = FALSE;
  58. acpi_gbl_global_lock_present = TRUE;
  59. return_ACPI_STATUS(status);
  60. }
  61. /*******************************************************************************
  62. *
  63. * FUNCTION: acpi_ev_remove_global_lock_handler
  64. *
  65. * PARAMETERS: None
  66. *
  67. * RETURN: Status
  68. *
  69. * DESCRIPTION: Remove the handler for the Global Lock
  70. *
  71. ******************************************************************************/
  72. acpi_status acpi_ev_remove_global_lock_handler(void)
  73. {
  74. acpi_status status;
  75. ACPI_FUNCTION_TRACE(ev_remove_global_lock_handler);
  76. acpi_gbl_global_lock_present = FALSE;
  77. status = acpi_remove_fixed_event_handler(ACPI_EVENT_GLOBAL,
  78. acpi_ev_global_lock_handler);
  79. acpi_os_delete_lock(acpi_gbl_global_lock_pending_lock);
  80. return_ACPI_STATUS(status);
  81. }
  82. /*******************************************************************************
  83. *
  84. * FUNCTION: acpi_ev_global_lock_handler
  85. *
  86. * PARAMETERS: context - From thread interface, not used
  87. *
  88. * RETURN: ACPI_INTERRUPT_HANDLED
  89. *
  90. * DESCRIPTION: Invoked directly from the SCI handler when a global lock
  91. * release interrupt occurs. If there is actually a pending
  92. * request for the lock, signal the waiting thread.
  93. *
  94. ******************************************************************************/
  95. static u32 acpi_ev_global_lock_handler(void *context)
  96. {
  97. acpi_status status;
  98. acpi_cpu_flags flags;
  99. flags = acpi_os_acquire_lock(acpi_gbl_global_lock_pending_lock);
  100. /*
  101. * If a request for the global lock is not actually pending,
  102. * we are done. This handles "spurious" global lock interrupts
  103. * which are possible (and have been seen) with bad BIOSs.
  104. */
  105. if (!acpi_gbl_global_lock_pending) {
  106. goto cleanup_and_exit;
  107. }
  108. /*
  109. * Send a unit to the global lock semaphore. The actual acquisition
  110. * of the global lock will be performed by the waiting thread.
  111. */
  112. status = acpi_os_signal_semaphore(acpi_gbl_global_lock_semaphore, 1);
  113. if (ACPI_FAILURE(status)) {
  114. ACPI_ERROR((AE_INFO, "Could not signal Global Lock semaphore"));
  115. }
  116. acpi_gbl_global_lock_pending = FALSE;
  117. cleanup_and_exit:
  118. acpi_os_release_lock(acpi_gbl_global_lock_pending_lock, flags);
  119. return (ACPI_INTERRUPT_HANDLED);
  120. }
  121. /******************************************************************************
  122. *
  123. * FUNCTION: acpi_ev_acquire_global_lock
  124. *
  125. * PARAMETERS: timeout - Max time to wait for the lock, in millisec.
  126. *
  127. * RETURN: Status
  128. *
  129. * DESCRIPTION: Attempt to gain ownership of the Global Lock.
  130. *
  131. * MUTEX: Interpreter must be locked
  132. *
  133. * Note: The original implementation allowed multiple threads to "acquire" the
  134. * Global Lock, and the OS would hold the lock until the last thread had
  135. * released it. However, this could potentially starve the BIOS out of the
  136. * lock, especially in the case where there is a tight handshake between the
  137. * Embedded Controller driver and the BIOS. Therefore, this implementation
  138. * allows only one thread to acquire the HW Global Lock at a time, and makes
  139. * the global lock appear as a standard mutex on the OS side.
  140. *
  141. *****************************************************************************/
  142. acpi_status acpi_ev_acquire_global_lock(u16 timeout)
  143. {
  144. acpi_cpu_flags flags;
  145. acpi_status status;
  146. u8 acquired = FALSE;
  147. ACPI_FUNCTION_TRACE(ev_acquire_global_lock);
  148. /*
  149. * Only one thread can acquire the GL at a time, the global_lock_mutex
  150. * enforces this. This interface releases the interpreter if we must wait.
  151. */
  152. status =
  153. acpi_ex_system_wait_mutex(acpi_gbl_global_lock_mutex->mutex.
  154. os_mutex, timeout);
  155. if (ACPI_FAILURE(status)) {
  156. return_ACPI_STATUS(status);
  157. }
  158. /*
  159. * Update the global lock handle and check for wraparound. The handle is
  160. * only used for the external global lock interfaces, but it is updated
  161. * here to properly handle the case where a single thread may acquire the
  162. * lock via both the AML and the acpi_acquire_global_lock interfaces. The
  163. * handle is therefore updated on the first acquire from a given thread
  164. * regardless of where the acquisition request originated.
  165. */
  166. acpi_gbl_global_lock_handle++;
  167. if (acpi_gbl_global_lock_handle == 0) {
  168. acpi_gbl_global_lock_handle = 1;
  169. }
  170. /*
  171. * Make sure that a global lock actually exists. If not, just
  172. * treat the lock as a standard mutex.
  173. */
  174. if (!acpi_gbl_global_lock_present) {
  175. acpi_gbl_global_lock_acquired = TRUE;
  176. return_ACPI_STATUS(AE_OK);
  177. }
  178. flags = acpi_os_acquire_lock(acpi_gbl_global_lock_pending_lock);
  179. do {
  180. /* Attempt to acquire the actual hardware lock */
  181. ACPI_ACQUIRE_GLOBAL_LOCK(acpi_gbl_FACS, acquired);
  182. if (acquired) {
  183. acpi_gbl_global_lock_acquired = TRUE;
  184. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  185. "Acquired hardware Global Lock\n"));
  186. break;
  187. }
  188. /*
  189. * Did not get the lock. The pending bit was set above, and
  190. * we must now wait until we receive the global lock
  191. * released interrupt.
  192. */
  193. acpi_gbl_global_lock_pending = TRUE;
  194. acpi_os_release_lock(acpi_gbl_global_lock_pending_lock, flags);
  195. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  196. "Waiting for hardware Global Lock\n"));
  197. /*
  198. * Wait for handshake with the global lock interrupt handler.
  199. * This interface releases the interpreter if we must wait.
  200. */
  201. status =
  202. acpi_ex_system_wait_semaphore
  203. (acpi_gbl_global_lock_semaphore, ACPI_WAIT_FOREVER);
  204. flags = acpi_os_acquire_lock(acpi_gbl_global_lock_pending_lock);
  205. } while (ACPI_SUCCESS(status));
  206. acpi_gbl_global_lock_pending = FALSE;
  207. acpi_os_release_lock(acpi_gbl_global_lock_pending_lock, flags);
  208. return_ACPI_STATUS(status);
  209. }
  210. /*******************************************************************************
  211. *
  212. * FUNCTION: acpi_ev_release_global_lock
  213. *
  214. * PARAMETERS: None
  215. *
  216. * RETURN: Status
  217. *
  218. * DESCRIPTION: Releases ownership of the Global Lock.
  219. *
  220. ******************************************************************************/
  221. acpi_status acpi_ev_release_global_lock(void)
  222. {
  223. u8 pending = FALSE;
  224. acpi_status status = AE_OK;
  225. ACPI_FUNCTION_TRACE(ev_release_global_lock);
  226. /* Lock must be already acquired */
  227. if (!acpi_gbl_global_lock_acquired) {
  228. ACPI_WARNING((AE_INFO,
  229. "Cannot release the ACPI Global Lock, it has not been acquired"));
  230. return_ACPI_STATUS(AE_NOT_ACQUIRED);
  231. }
  232. if (acpi_gbl_global_lock_present) {
  233. /* Allow any thread to release the lock */
  234. ACPI_RELEASE_GLOBAL_LOCK(acpi_gbl_FACS, pending);
  235. /*
  236. * If the pending bit was set, we must write GBL_RLS to the control
  237. * register
  238. */
  239. if (pending) {
  240. status =
  241. acpi_write_bit_register
  242. (ACPI_BITREG_GLOBAL_LOCK_RELEASE,
  243. ACPI_ENABLE_EVENT);
  244. }
  245. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  246. "Released hardware Global Lock\n"));
  247. }
  248. acpi_gbl_global_lock_acquired = FALSE;
  249. /* Release the local GL mutex */
  250. acpi_os_release_mutex(acpi_gbl_global_lock_mutex->mutex.os_mutex);
  251. return_ACPI_STATUS(status);
  252. }
  253. #endif /* !ACPI_REDUCED_HARDWARE */