utmutex.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*******************************************************************************
  2. *
  3. * Module Name: utmutex - local mutex 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. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utmutex")
  46. /* Local prototypes */
  47. static acpi_status acpi_ut_create_mutex(acpi_mutex_handle mutex_id);
  48. static void acpi_ut_delete_mutex(acpi_mutex_handle mutex_id);
  49. /*******************************************************************************
  50. *
  51. * FUNCTION: acpi_ut_mutex_initialize
  52. *
  53. * PARAMETERS: None.
  54. *
  55. * RETURN: Status
  56. *
  57. * DESCRIPTION: Create the system mutex objects. This includes mutexes,
  58. * spin locks, and reader/writer locks.
  59. *
  60. ******************************************************************************/
  61. acpi_status acpi_ut_mutex_initialize(void)
  62. {
  63. u32 i;
  64. acpi_status status;
  65. ACPI_FUNCTION_TRACE(ut_mutex_initialize);
  66. /* Create each of the predefined mutex objects */
  67. for (i = 0; i < ACPI_NUM_MUTEX; i++) {
  68. status = acpi_ut_create_mutex(i);
  69. if (ACPI_FAILURE(status)) {
  70. return_ACPI_STATUS(status);
  71. }
  72. }
  73. /* Create the spinlocks for use at interrupt level or for speed */
  74. status = acpi_os_create_lock (&acpi_gbl_gpe_lock);
  75. if (ACPI_FAILURE (status)) {
  76. return_ACPI_STATUS (status);
  77. }
  78. status = acpi_os_create_lock (&acpi_gbl_hardware_lock);
  79. if (ACPI_FAILURE (status)) {
  80. return_ACPI_STATUS (status);
  81. }
  82. status = acpi_os_create_lock(&acpi_gbl_reference_count_lock);
  83. if (ACPI_FAILURE(status)) {
  84. return_ACPI_STATUS(status);
  85. }
  86. /* Mutex for _OSI support */
  87. status = acpi_os_create_mutex(&acpi_gbl_osi_mutex);
  88. if (ACPI_FAILURE(status)) {
  89. return_ACPI_STATUS(status);
  90. }
  91. /* Create the reader/writer lock for namespace access */
  92. status = acpi_ut_create_rw_lock(&acpi_gbl_namespace_rw_lock);
  93. if (ACPI_FAILURE(status)) {
  94. return_ACPI_STATUS(status);
  95. }
  96. #ifdef ACPI_DEBUGGER
  97. /* Debugger Support */
  98. status = acpi_os_create_mutex(&acpi_gbl_db_command_ready);
  99. if (ACPI_FAILURE(status)) {
  100. return_ACPI_STATUS(status);
  101. }
  102. status = acpi_os_create_mutex(&acpi_gbl_db_command_complete);
  103. #endif
  104. return_ACPI_STATUS(status);
  105. }
  106. /*******************************************************************************
  107. *
  108. * FUNCTION: acpi_ut_mutex_terminate
  109. *
  110. * PARAMETERS: None.
  111. *
  112. * RETURN: None.
  113. *
  114. * DESCRIPTION: Delete all of the system mutex objects. This includes mutexes,
  115. * spin locks, and reader/writer locks.
  116. *
  117. ******************************************************************************/
  118. void acpi_ut_mutex_terminate(void)
  119. {
  120. u32 i;
  121. ACPI_FUNCTION_TRACE(ut_mutex_terminate);
  122. /* Delete each predefined mutex object */
  123. for (i = 0; i < ACPI_NUM_MUTEX; i++) {
  124. acpi_ut_delete_mutex(i);
  125. }
  126. acpi_os_delete_mutex(acpi_gbl_osi_mutex);
  127. /* Delete the spinlocks */
  128. acpi_os_delete_lock(acpi_gbl_gpe_lock);
  129. acpi_os_delete_lock(acpi_gbl_hardware_lock);
  130. acpi_os_delete_lock(acpi_gbl_reference_count_lock);
  131. /* Delete the reader/writer lock */
  132. acpi_ut_delete_rw_lock(&acpi_gbl_namespace_rw_lock);
  133. #ifdef ACPI_DEBUGGER
  134. acpi_os_delete_mutex(acpi_gbl_db_command_ready);
  135. acpi_os_delete_mutex(acpi_gbl_db_command_complete);
  136. #endif
  137. return_VOID;
  138. }
  139. /*******************************************************************************
  140. *
  141. * FUNCTION: acpi_ut_create_mutex
  142. *
  143. * PARAMETERS: mutex_ID - ID of the mutex to be created
  144. *
  145. * RETURN: Status
  146. *
  147. * DESCRIPTION: Create a mutex object.
  148. *
  149. ******************************************************************************/
  150. static acpi_status acpi_ut_create_mutex(acpi_mutex_handle mutex_id)
  151. {
  152. acpi_status status = AE_OK;
  153. ACPI_FUNCTION_TRACE_U32(ut_create_mutex, mutex_id);
  154. if (!acpi_gbl_mutex_info[mutex_id].mutex) {
  155. status =
  156. acpi_os_create_mutex(&acpi_gbl_mutex_info[mutex_id].mutex);
  157. acpi_gbl_mutex_info[mutex_id].thread_id =
  158. ACPI_MUTEX_NOT_ACQUIRED;
  159. acpi_gbl_mutex_info[mutex_id].use_count = 0;
  160. }
  161. return_ACPI_STATUS(status);
  162. }
  163. /*******************************************************************************
  164. *
  165. * FUNCTION: acpi_ut_delete_mutex
  166. *
  167. * PARAMETERS: mutex_ID - ID of the mutex to be deleted
  168. *
  169. * RETURN: Status
  170. *
  171. * DESCRIPTION: Delete a mutex object.
  172. *
  173. ******************************************************************************/
  174. static void acpi_ut_delete_mutex(acpi_mutex_handle mutex_id)
  175. {
  176. ACPI_FUNCTION_TRACE_U32(ut_delete_mutex, mutex_id);
  177. acpi_os_delete_mutex(acpi_gbl_mutex_info[mutex_id].mutex);
  178. acpi_gbl_mutex_info[mutex_id].mutex = NULL;
  179. acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
  180. return_VOID;
  181. }
  182. /*******************************************************************************
  183. *
  184. * FUNCTION: acpi_ut_acquire_mutex
  185. *
  186. * PARAMETERS: mutex_ID - ID of the mutex to be acquired
  187. *
  188. * RETURN: Status
  189. *
  190. * DESCRIPTION: Acquire a mutex object.
  191. *
  192. ******************************************************************************/
  193. acpi_status acpi_ut_acquire_mutex(acpi_mutex_handle mutex_id)
  194. {
  195. acpi_status status;
  196. acpi_thread_id this_thread_id;
  197. ACPI_FUNCTION_NAME(ut_acquire_mutex);
  198. if (mutex_id > ACPI_MAX_MUTEX) {
  199. return (AE_BAD_PARAMETER);
  200. }
  201. this_thread_id = acpi_os_get_thread_id();
  202. #ifdef ACPI_MUTEX_DEBUG
  203. {
  204. u32 i;
  205. /*
  206. * Mutex debug code, for internal debugging only.
  207. *
  208. * Deadlock prevention. Check if this thread owns any mutexes of value
  209. * greater than or equal to this one. If so, the thread has violated
  210. * the mutex ordering rule. This indicates a coding error somewhere in
  211. * the ACPI subsystem code.
  212. */
  213. for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
  214. if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
  215. if (i == mutex_id) {
  216. ACPI_ERROR((AE_INFO,
  217. "Mutex [%s] already acquired by this thread [%u]",
  218. acpi_ut_get_mutex_name
  219. (mutex_id),
  220. (u32)this_thread_id));
  221. return (AE_ALREADY_ACQUIRED);
  222. }
  223. ACPI_ERROR((AE_INFO,
  224. "Invalid acquire order: Thread %u owns [%s], wants [%s]",
  225. (u32)this_thread_id,
  226. acpi_ut_get_mutex_name(i),
  227. acpi_ut_get_mutex_name(mutex_id)));
  228. return (AE_ACQUIRE_DEADLOCK);
  229. }
  230. }
  231. }
  232. #endif
  233. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  234. "Thread %u attempting to acquire Mutex [%s]\n",
  235. (u32)this_thread_id,
  236. acpi_ut_get_mutex_name(mutex_id)));
  237. status = acpi_os_acquire_mutex(acpi_gbl_mutex_info[mutex_id].mutex,
  238. ACPI_WAIT_FOREVER);
  239. if (ACPI_SUCCESS(status)) {
  240. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  241. "Thread %u acquired Mutex [%s]\n",
  242. (u32)this_thread_id,
  243. acpi_ut_get_mutex_name(mutex_id)));
  244. acpi_gbl_mutex_info[mutex_id].use_count++;
  245. acpi_gbl_mutex_info[mutex_id].thread_id = this_thread_id;
  246. } else {
  247. ACPI_EXCEPTION((AE_INFO, status,
  248. "Thread %u could not acquire Mutex [0x%X]",
  249. (u32)this_thread_id, mutex_id));
  250. }
  251. return (status);
  252. }
  253. /*******************************************************************************
  254. *
  255. * FUNCTION: acpi_ut_release_mutex
  256. *
  257. * PARAMETERS: mutex_ID - ID of the mutex to be released
  258. *
  259. * RETURN: Status
  260. *
  261. * DESCRIPTION: Release a mutex object.
  262. *
  263. ******************************************************************************/
  264. acpi_status acpi_ut_release_mutex(acpi_mutex_handle mutex_id)
  265. {
  266. ACPI_FUNCTION_NAME(ut_release_mutex);
  267. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Thread %u releasing Mutex [%s]\n",
  268. (u32)acpi_os_get_thread_id(),
  269. acpi_ut_get_mutex_name(mutex_id)));
  270. if (mutex_id > ACPI_MAX_MUTEX) {
  271. return (AE_BAD_PARAMETER);
  272. }
  273. /*
  274. * Mutex must be acquired in order to release it!
  275. */
  276. if (acpi_gbl_mutex_info[mutex_id].thread_id == ACPI_MUTEX_NOT_ACQUIRED) {
  277. ACPI_ERROR((AE_INFO,
  278. "Mutex [0x%X] is not acquired, cannot release",
  279. mutex_id));
  280. return (AE_NOT_ACQUIRED);
  281. }
  282. #ifdef ACPI_MUTEX_DEBUG
  283. {
  284. u32 i;
  285. /*
  286. * Mutex debug code, for internal debugging only.
  287. *
  288. * Deadlock prevention. Check if this thread owns any mutexes of value
  289. * greater than this one. If so, the thread has violated the mutex
  290. * ordering rule. This indicates a coding error somewhere in
  291. * the ACPI subsystem code.
  292. */
  293. for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
  294. if (acpi_gbl_mutex_info[i].thread_id ==
  295. acpi_os_get_thread_id()) {
  296. if (i == mutex_id) {
  297. continue;
  298. }
  299. ACPI_ERROR((AE_INFO,
  300. "Invalid release order: owns [%s], releasing [%s]",
  301. acpi_ut_get_mutex_name(i),
  302. acpi_ut_get_mutex_name(mutex_id)));
  303. return (AE_RELEASE_DEADLOCK);
  304. }
  305. }
  306. }
  307. #endif
  308. /* Mark unlocked FIRST */
  309. acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
  310. acpi_os_release_mutex(acpi_gbl_mutex_info[mutex_id].mutex);
  311. return (AE_OK);
  312. }