evglock.c 8.8 KB

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