utmutex.c 9.4 KB

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