exmutex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /******************************************************************************
  2. *
  3. * Module Name: exmutex - ASL Mutex Acquire/Release functions
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2007, R. Byron Moore
  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 <acpi/acinterp.h>
  44. #include <acpi/acevents.h>
  45. #define _COMPONENT ACPI_EXECUTER
  46. ACPI_MODULE_NAME("exmutex")
  47. /* Local prototypes */
  48. static void
  49. acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
  50. struct acpi_thread_state *thread);
  51. /*******************************************************************************
  52. *
  53. * FUNCTION: acpi_ex_unlink_mutex
  54. *
  55. * PARAMETERS: obj_desc - The mutex to be unlinked
  56. *
  57. * RETURN: None
  58. *
  59. * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
  60. *
  61. ******************************************************************************/
  62. void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc,
  63. struct acpi_thread_state *thread)
  64. {
  65. if (!thread) {
  66. return;
  67. }
  68. /* Doubly linked list */
  69. if (obj_desc->mutex.next) {
  70. (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev;
  71. }
  72. if (obj_desc->mutex.prev) {
  73. (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next;
  74. } else {
  75. thread->acquired_mutex_list = obj_desc->mutex.next;
  76. }
  77. }
  78. /*******************************************************************************
  79. *
  80. * FUNCTION: acpi_ex_link_mutex
  81. *
  82. * PARAMETERS: obj_desc - The mutex to be linked
  83. * Thread - Current executing thread object
  84. *
  85. * RETURN: None
  86. *
  87. * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
  88. *
  89. ******************************************************************************/
  90. static void
  91. acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
  92. struct acpi_thread_state *thread)
  93. {
  94. union acpi_operand_object *list_head;
  95. list_head = thread->acquired_mutex_list;
  96. /* This object will be the first object in the list */
  97. obj_desc->mutex.prev = NULL;
  98. obj_desc->mutex.next = list_head;
  99. /* Update old first object to point back to this object */
  100. if (list_head) {
  101. list_head->mutex.prev = obj_desc;
  102. }
  103. /* Update list head */
  104. thread->acquired_mutex_list = obj_desc;
  105. }
  106. /*******************************************************************************
  107. *
  108. * FUNCTION: acpi_ex_acquire_mutex
  109. *
  110. * PARAMETERS: time_desc - Timeout integer
  111. * obj_desc - Mutex object
  112. * walk_state - Current method execution state
  113. *
  114. * RETURN: Status
  115. *
  116. * DESCRIPTION: Acquire an AML mutex
  117. *
  118. ******************************************************************************/
  119. acpi_status
  120. acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
  121. union acpi_operand_object *obj_desc,
  122. struct acpi_walk_state *walk_state)
  123. {
  124. acpi_status status;
  125. ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
  126. if (!obj_desc) {
  127. return_ACPI_STATUS(AE_BAD_PARAMETER);
  128. }
  129. /* Sanity check: we must have a valid thread ID */
  130. if (!walk_state->thread) {
  131. ACPI_ERROR((AE_INFO,
  132. "Cannot acquire Mutex [%4.4s], null thread info",
  133. acpi_ut_get_node_name(obj_desc->mutex.node)));
  134. return_ACPI_STATUS(AE_AML_INTERNAL);
  135. }
  136. /*
  137. * Current Sync must be less than or equal to the sync level of the
  138. * mutex. This mechanism provides some deadlock prevention
  139. */
  140. if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
  141. ACPI_ERROR((AE_INFO,
  142. "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%d)",
  143. acpi_ut_get_node_name(obj_desc->mutex.node),
  144. walk_state->thread->current_sync_level));
  145. return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
  146. }
  147. /* Support for multiple acquires by the owning thread */
  148. if (obj_desc->mutex.owner_thread_id == acpi_os_get_thread_id()) {
  149. /*
  150. * The mutex is already owned by this thread, just increment the
  151. * acquisition depth
  152. */
  153. obj_desc->mutex.acquisition_depth++;
  154. return_ACPI_STATUS(AE_OK);
  155. }
  156. /* Acquire the mutex, wait if necessary. Special case for Global Lock */
  157. if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) {
  158. status =
  159. acpi_ev_acquire_global_lock((u16) time_desc->integer.value);
  160. } else {
  161. status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
  162. (u16) time_desc->integer.
  163. value);
  164. }
  165. if (ACPI_FAILURE(status)) {
  166. /* Includes failure from a timeout on time_desc */
  167. return_ACPI_STATUS(status);
  168. }
  169. /* Have the mutex: update mutex and walk info and save the sync_level */
  170. obj_desc->mutex.owner_thread_id = acpi_os_get_thread_id();
  171. obj_desc->mutex.acquisition_depth = 1;
  172. obj_desc->mutex.original_sync_level =
  173. walk_state->thread->current_sync_level;
  174. walk_state->thread->current_sync_level = obj_desc->mutex.sync_level;
  175. /* Link the mutex to the current thread for force-unlock at method exit */
  176. acpi_ex_link_mutex(obj_desc, walk_state->thread);
  177. return_ACPI_STATUS(AE_OK);
  178. }
  179. /*******************************************************************************
  180. *
  181. * FUNCTION: acpi_ex_release_mutex
  182. *
  183. * PARAMETERS: obj_desc - The object descriptor for this op
  184. * walk_state - Current method execution state
  185. *
  186. * RETURN: Status
  187. *
  188. * DESCRIPTION: Release a previously acquired Mutex.
  189. *
  190. ******************************************************************************/
  191. acpi_status
  192. acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
  193. struct acpi_walk_state *walk_state)
  194. {
  195. acpi_status status = AE_OK;
  196. ACPI_FUNCTION_TRACE(ex_release_mutex);
  197. if (!obj_desc) {
  198. return_ACPI_STATUS(AE_BAD_PARAMETER);
  199. }
  200. /* The mutex must have been previously acquired in order to release it */
  201. if (!obj_desc->mutex.owner_thread_id) {
  202. ACPI_ERROR((AE_INFO,
  203. "Cannot release Mutex [%4.4s], not acquired",
  204. acpi_ut_get_node_name(obj_desc->mutex.node)));
  205. return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
  206. }
  207. /* Sanity check: we must have a valid thread ID */
  208. if (!walk_state->thread) {
  209. ACPI_ERROR((AE_INFO,
  210. "Cannot release Mutex [%4.4s], null thread info",
  211. acpi_ut_get_node_name(obj_desc->mutex.node)));
  212. return_ACPI_STATUS(AE_AML_INTERNAL);
  213. }
  214. /*
  215. * The Mutex is owned, but this thread must be the owner.
  216. * Special case for Global Lock, any thread can release
  217. */
  218. if ((obj_desc->mutex.owner_thread_id !=
  219. walk_state->thread->thread_id)
  220. && (obj_desc->mutex.os_mutex != acpi_gbl_global_lock_mutex)) {
  221. ACPI_ERROR((AE_INFO,
  222. "Thread %lX cannot release Mutex [%4.4s] acquired by thread %lX",
  223. (unsigned long)walk_state->thread->thread_id,
  224. acpi_ut_get_node_name(obj_desc->mutex.node),
  225. (unsigned long)obj_desc->mutex.owner_thread_id));
  226. return_ACPI_STATUS(AE_AML_NOT_OWNER);
  227. }
  228. /*
  229. * The sync level of the mutex must be less than or equal to the current
  230. * sync level
  231. */
  232. if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) {
  233. ACPI_ERROR((AE_INFO,
  234. "Cannot release Mutex [%4.4s], incorrect SyncLevel",
  235. acpi_ut_get_node_name(obj_desc->mutex.node)));
  236. return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
  237. }
  238. /* Match multiple Acquires with multiple Releases */
  239. obj_desc->mutex.acquisition_depth--;
  240. if (obj_desc->mutex.acquisition_depth != 0) {
  241. /* Just decrement the depth and return */
  242. return_ACPI_STATUS(AE_OK);
  243. }
  244. /* Unlink the mutex from the owner's list */
  245. acpi_ex_unlink_mutex(obj_desc, walk_state->thread);
  246. /* Release the mutex, special case for Global Lock */
  247. if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) {
  248. status = acpi_ev_release_global_lock();
  249. } else {
  250. acpi_os_release_mutex(obj_desc->mutex.os_mutex);
  251. }
  252. /* Update the mutex and restore sync_level */
  253. obj_desc->mutex.owner_thread_id = ACPI_MUTEX_NOT_ACQUIRED;
  254. walk_state->thread->current_sync_level =
  255. obj_desc->mutex.original_sync_level;
  256. return_ACPI_STATUS(status);
  257. }
  258. /*******************************************************************************
  259. *
  260. * FUNCTION: acpi_ex_release_all_mutexes
  261. *
  262. * PARAMETERS: Thread - Current executing thread object
  263. *
  264. * RETURN: Status
  265. *
  266. * DESCRIPTION: Release all mutexes held by this thread
  267. *
  268. * NOTE: This function is called as the thread is exiting the interpreter.
  269. * Mutexes are not released when an individual control method is exited, but
  270. * only when the parent thread actually exits the interpreter. This allows one
  271. * method to acquire a mutex, and a different method to release it, as long as
  272. * this is performed underneath a single parent control method.
  273. *
  274. ******************************************************************************/
  275. void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
  276. {
  277. union acpi_operand_object *next = thread->acquired_mutex_list;
  278. union acpi_operand_object *obj_desc;
  279. ACPI_FUNCTION_ENTRY();
  280. /* Traverse the list of owned mutexes, releasing each one */
  281. while (next) {
  282. obj_desc = next;
  283. next = obj_desc->mutex.next;
  284. obj_desc->mutex.prev = NULL;
  285. obj_desc->mutex.next = NULL;
  286. obj_desc->mutex.acquisition_depth = 0;
  287. /* Release the mutex, special case for Global Lock */
  288. if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) {
  289. /* Ignore errors */
  290. (void)acpi_ev_release_global_lock();
  291. } else {
  292. acpi_os_release_mutex(obj_desc->mutex.os_mutex);
  293. }
  294. /* Mark mutex unowned */
  295. obj_desc->mutex.owner_thread_id = ACPI_MUTEX_NOT_ACQUIRED;
  296. /* Update Thread sync_level (Last mutex is the important one) */
  297. thread->current_sync_level =
  298. obj_desc->mutex.original_sync_level;
  299. }
  300. }