1 /*******************************************************************************
3 * Module Name: utmutex - local mutex support
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2008, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME("utmutex")
50 /* Local prototypes */
51 static acpi_status acpi_ut_create_mutex(acpi_mutex_handle mutex_id);
53 static acpi_status acpi_ut_delete_mutex(acpi_mutex_handle mutex_id);
55 /*******************************************************************************
57 * FUNCTION: acpi_ut_mutex_initialize
63 * DESCRIPTION: Create the system mutex objects.
65 ******************************************************************************/
67 acpi_status acpi_ut_mutex_initialize(void)
72 ACPI_FUNCTION_TRACE(ut_mutex_initialize);
75 * Create each of the predefined mutex objects
77 for (i = 0; i < ACPI_NUM_MUTEX; i++) {
78 status = acpi_ut_create_mutex(i);
79 if (ACPI_FAILURE(status)) {
80 return_ACPI_STATUS(status);
84 /* Create the spinlocks for use at interrupt level */
86 spin_lock_init(acpi_gbl_gpe_lock);
87 spin_lock_init(acpi_gbl_hardware_lock);
89 return_ACPI_STATUS(status);
92 /*******************************************************************************
94 * FUNCTION: acpi_ut_mutex_terminate
100 * DESCRIPTION: Delete all of the system mutex objects.
102 ******************************************************************************/
104 void acpi_ut_mutex_terminate(void)
108 ACPI_FUNCTION_TRACE(ut_mutex_terminate);
111 * Delete each predefined mutex object
113 for (i = 0; i < ACPI_NUM_MUTEX; i++) {
114 (void)acpi_ut_delete_mutex(i);
117 /* Delete the spinlocks */
119 acpi_os_delete_lock(acpi_gbl_gpe_lock);
120 acpi_os_delete_lock(acpi_gbl_hardware_lock);
124 /*******************************************************************************
126 * FUNCTION: acpi_ut_create_mutex
128 * PARAMETERS: mutex_iD - ID of the mutex to be created
132 * DESCRIPTION: Create a mutex object.
134 ******************************************************************************/
136 static acpi_status acpi_ut_create_mutex(acpi_mutex_handle mutex_id)
138 acpi_status status = AE_OK;
140 ACPI_FUNCTION_TRACE_U32(ut_create_mutex, mutex_id);
142 if (mutex_id > ACPI_MAX_MUTEX) {
143 return_ACPI_STATUS(AE_BAD_PARAMETER);
146 if (!acpi_gbl_mutex_info[mutex_id].mutex) {
148 acpi_os_create_mutex(&acpi_gbl_mutex_info[mutex_id].mutex);
149 acpi_gbl_mutex_info[mutex_id].thread_id =
150 ACPI_MUTEX_NOT_ACQUIRED;
151 acpi_gbl_mutex_info[mutex_id].use_count = 0;
154 return_ACPI_STATUS(status);
157 /*******************************************************************************
159 * FUNCTION: acpi_ut_delete_mutex
161 * PARAMETERS: mutex_iD - ID of the mutex to be deleted
165 * DESCRIPTION: Delete a mutex object.
167 ******************************************************************************/
169 static acpi_status acpi_ut_delete_mutex(acpi_mutex_handle mutex_id)
172 ACPI_FUNCTION_TRACE_U32(ut_delete_mutex, mutex_id);
174 if (mutex_id > ACPI_MAX_MUTEX) {
175 return_ACPI_STATUS(AE_BAD_PARAMETER);
178 acpi_os_delete_mutex(acpi_gbl_mutex_info[mutex_id].mutex);
180 acpi_gbl_mutex_info[mutex_id].mutex = NULL;
181 acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
183 return_ACPI_STATUS(AE_OK);
186 /*******************************************************************************
188 * FUNCTION: acpi_ut_acquire_mutex
190 * PARAMETERS: mutex_iD - ID of the mutex to be acquired
194 * DESCRIPTION: Acquire a mutex object.
196 ******************************************************************************/
198 acpi_status acpi_ut_acquire_mutex(acpi_mutex_handle mutex_id)
201 acpi_thread_id this_thread_id;
203 ACPI_FUNCTION_NAME(ut_acquire_mutex);
205 if (mutex_id > ACPI_MAX_MUTEX) {
206 return (AE_BAD_PARAMETER);
209 this_thread_id = acpi_os_get_thread_id();
211 #ifdef ACPI_MUTEX_DEBUG
215 * Mutex debug code, for internal debugging only.
217 * Deadlock prevention. Check if this thread owns any mutexes of value
218 * greater than or equal to this one. If so, the thread has violated
219 * the mutex ordering rule. This indicates a coding error somewhere in
220 * the ACPI subsystem code.
222 for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
223 if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
226 "Mutex [%s] already acquired by this thread [%X]",
227 acpi_ut_get_mutex_name
231 return (AE_ALREADY_ACQUIRED);
235 "Invalid acquire order: Thread %X owns [%s], wants [%s]",
237 acpi_ut_get_mutex_name(i),
238 acpi_ut_get_mutex_name(mutex_id)));
240 return (AE_ACQUIRE_DEADLOCK);
246 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
247 "Thread %lX attempting to acquire Mutex [%s]\n",
248 (unsigned long)this_thread_id,
249 acpi_ut_get_mutex_name(mutex_id)));
251 status = acpi_os_acquire_mutex(acpi_gbl_mutex_info[mutex_id].mutex,
253 if (ACPI_SUCCESS(status)) {
254 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
255 "Thread %lX acquired Mutex [%s]\n",
256 (unsigned long)this_thread_id,
257 acpi_ut_get_mutex_name(mutex_id)));
259 acpi_gbl_mutex_info[mutex_id].use_count++;
260 acpi_gbl_mutex_info[mutex_id].thread_id = this_thread_id;
262 ACPI_EXCEPTION((AE_INFO, status,
263 "Thread %lX could not acquire Mutex [%X]",
264 (unsigned long)this_thread_id, mutex_id));
270 /*******************************************************************************
272 * FUNCTION: acpi_ut_release_mutex
274 * PARAMETERS: mutex_iD - ID of the mutex to be released
278 * DESCRIPTION: Release a mutex object.
280 ******************************************************************************/
282 acpi_status acpi_ut_release_mutex(acpi_mutex_handle mutex_id)
284 acpi_thread_id this_thread_id;
286 ACPI_FUNCTION_NAME(ut_release_mutex);
288 this_thread_id = acpi_os_get_thread_id();
289 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
290 "Thread %lX releasing Mutex [%s]\n",
291 (unsigned long)this_thread_id,
292 acpi_ut_get_mutex_name(mutex_id)));
294 if (mutex_id > ACPI_MAX_MUTEX) {
295 return (AE_BAD_PARAMETER);
299 * Mutex must be acquired in order to release it!
301 if (acpi_gbl_mutex_info[mutex_id].thread_id == ACPI_MUTEX_NOT_ACQUIRED) {
303 "Mutex [%X] is not acquired, cannot release",
306 return (AE_NOT_ACQUIRED);
308 #ifdef ACPI_MUTEX_DEBUG
312 * Mutex debug code, for internal debugging only.
314 * Deadlock prevention. Check if this thread owns any mutexes of value
315 * greater than this one. If so, the thread has violated the mutex
316 * ordering rule. This indicates a coding error somewhere in
317 * the ACPI subsystem code.
319 for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
320 if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
326 "Invalid release order: owns [%s], releasing [%s]",
327 acpi_ut_get_mutex_name(i),
328 acpi_ut_get_mutex_name(mutex_id)));
330 return (AE_RELEASE_DEADLOCK);
336 /* Mark unlocked FIRST */
338 acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
340 acpi_os_release_mutex(acpi_gbl_mutex_info[mutex_id].mutex);