evglock.c 10 KB

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