exmutex.c 10 KB

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