evglock.c 10 KB

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