evglock.c 10 KB

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