]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/acpi/acpica/utmutex.c
ACPICA: create acpica/ directory
[mirror_ubuntu-artful-kernel.git] / drivers / acpi / acpica / utmutex.c
CommitLineData
73459f73
RM
1/*******************************************************************************
2 *
3 * Module Name: utmutex - local mutex support
4 *
5 ******************************************************************************/
6
7/*
75a44ce0 8 * Copyright (C) 2000 - 2008, Intel Corp.
73459f73
RM
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
25 *
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.
29 *
30 * NO WARRANTY
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.
42 */
43
73459f73 44#include <acpi/acpi.h>
50df4d8b 45#include <acpi/accommon.h>
73459f73
RM
46
47#define _COMPONENT ACPI_UTILITIES
4be44fcd 48ACPI_MODULE_NAME("utmutex")
73459f73
RM
49
50/* Local prototypes */
4be44fcd 51static acpi_status acpi_ut_create_mutex(acpi_mutex_handle mutex_id);
73459f73 52
4be44fcd 53static acpi_status acpi_ut_delete_mutex(acpi_mutex_handle mutex_id);
73459f73
RM
54
55/*******************************************************************************
56 *
57 * FUNCTION: acpi_ut_mutex_initialize
58 *
59 * PARAMETERS: None.
60 *
61 * RETURN: Status
62 *
63 * DESCRIPTION: Create the system mutex objects.
64 *
65 ******************************************************************************/
66
4be44fcd 67acpi_status acpi_ut_mutex_initialize(void)
73459f73 68{
4be44fcd
LB
69 u32 i;
70 acpi_status status;
73459f73 71
b229cf92 72 ACPI_FUNCTION_TRACE(ut_mutex_initialize);
73459f73
RM
73
74 /*
75 * Create each of the predefined mutex objects
76 */
4c90ece2 77 for (i = 0; i < ACPI_NUM_MUTEX; i++) {
4be44fcd
LB
78 status = acpi_ut_create_mutex(i);
79 if (ACPI_FAILURE(status)) {
80 return_ACPI_STATUS(status);
73459f73
RM
81 }
82 }
83
4c90ece2
BM
84 /* Create the spinlocks for use at interrupt level */
85
967440e3
BM
86 spin_lock_init(acpi_gbl_gpe_lock);
87 spin_lock_init(acpi_gbl_hardware_lock);
4c90ece2 88
4be44fcd 89 return_ACPI_STATUS(status);
73459f73
RM
90}
91
73459f73
RM
92/*******************************************************************************
93 *
94 * FUNCTION: acpi_ut_mutex_terminate
95 *
96 * PARAMETERS: None.
97 *
98 * RETURN: None.
99 *
100 * DESCRIPTION: Delete all of the system mutex objects.
101 *
102 ******************************************************************************/
103
4be44fcd 104void acpi_ut_mutex_terminate(void)
73459f73 105{
4be44fcd 106 u32 i;
73459f73 107
b229cf92 108 ACPI_FUNCTION_TRACE(ut_mutex_terminate);
73459f73
RM
109
110 /*
111 * Delete each predefined mutex object
112 */
4c90ece2 113 for (i = 0; i < ACPI_NUM_MUTEX; i++) {
4be44fcd 114 (void)acpi_ut_delete_mutex(i);
73459f73
RM
115 }
116
4c90ece2
BM
117 /* Delete the spinlocks */
118
4be44fcd 119 acpi_os_delete_lock(acpi_gbl_gpe_lock);
4c90ece2 120 acpi_os_delete_lock(acpi_gbl_hardware_lock);
73459f73
RM
121 return_VOID;
122}
123
73459f73
RM
124/*******************************************************************************
125 *
126 * FUNCTION: acpi_ut_create_mutex
127 *
128 * PARAMETERS: mutex_iD - ID of the mutex to be created
129 *
130 * RETURN: Status
131 *
132 * DESCRIPTION: Create a mutex object.
133 *
134 ******************************************************************************/
135
4be44fcd 136static acpi_status acpi_ut_create_mutex(acpi_mutex_handle mutex_id)
73459f73 137{
4be44fcd 138 acpi_status status = AE_OK;
73459f73 139
b229cf92 140 ACPI_FUNCTION_TRACE_U32(ut_create_mutex, mutex_id);
73459f73 141
4c90ece2 142 if (mutex_id > ACPI_MAX_MUTEX) {
4be44fcd 143 return_ACPI_STATUS(AE_BAD_PARAMETER);
73459f73
RM
144 }
145
146 if (!acpi_gbl_mutex_info[mutex_id].mutex) {
967440e3
BM
147 status =
148 acpi_os_create_mutex(&acpi_gbl_mutex_info[mutex_id].mutex);
4be44fcd
LB
149 acpi_gbl_mutex_info[mutex_id].thread_id =
150 ACPI_MUTEX_NOT_ACQUIRED;
73459f73
RM
151 acpi_gbl_mutex_info[mutex_id].use_count = 0;
152 }
153
4be44fcd 154 return_ACPI_STATUS(status);
73459f73
RM
155}
156
73459f73
RM
157/*******************************************************************************
158 *
159 * FUNCTION: acpi_ut_delete_mutex
160 *
161 * PARAMETERS: mutex_iD - ID of the mutex to be deleted
162 *
163 * RETURN: Status
164 *
165 * DESCRIPTION: Delete a mutex object.
166 *
167 ******************************************************************************/
168
4be44fcd 169static acpi_status acpi_ut_delete_mutex(acpi_mutex_handle mutex_id)
73459f73 170{
73459f73 171
b229cf92 172 ACPI_FUNCTION_TRACE_U32(ut_delete_mutex, mutex_id);
73459f73 173
4c90ece2 174 if (mutex_id > ACPI_MAX_MUTEX) {
4be44fcd 175 return_ACPI_STATUS(AE_BAD_PARAMETER);
73459f73
RM
176 }
177
967440e3 178 acpi_os_delete_mutex(acpi_gbl_mutex_info[mutex_id].mutex);
73459f73
RM
179
180 acpi_gbl_mutex_info[mutex_id].mutex = NULL;
f9f4601f 181 acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
73459f73 182
967440e3 183 return_ACPI_STATUS(AE_OK);
73459f73
RM
184}
185
73459f73
RM
186/*******************************************************************************
187 *
188 * FUNCTION: acpi_ut_acquire_mutex
189 *
190 * PARAMETERS: mutex_iD - ID of the mutex to be acquired
191 *
192 * RETURN: Status
193 *
194 * DESCRIPTION: Acquire a mutex object.
195 *
196 ******************************************************************************/
197
4be44fcd 198acpi_status acpi_ut_acquire_mutex(acpi_mutex_handle mutex_id)
73459f73 199{
4be44fcd 200 acpi_status status;
8313524a 201 acpi_thread_id this_thread_id;
73459f73 202
b229cf92 203 ACPI_FUNCTION_NAME(ut_acquire_mutex);
73459f73 204
4c90ece2 205 if (mutex_id > ACPI_MAX_MUTEX) {
73459f73
RM
206 return (AE_BAD_PARAMETER);
207 }
208
4be44fcd 209 this_thread_id = acpi_os_get_thread_id();
73459f73
RM
210
211#ifdef ACPI_MUTEX_DEBUG
212 {
4be44fcd 213 u32 i;
73459f73
RM
214 /*
215 * Mutex debug code, for internal debugging only.
216 *
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.
221 */
b53ce3f7 222 for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
bda663d3 223 if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
73459f73 224 if (i == mutex_id) {
b8e4d893
BM
225 ACPI_ERROR((AE_INFO,
226 "Mutex [%s] already acquired by this thread [%X]",
227 acpi_ut_get_mutex_name
228 (mutex_id),
229 this_thread_id));
73459f73
RM
230
231 return (AE_ALREADY_ACQUIRED);
232 }
233
b8e4d893
BM
234 ACPI_ERROR((AE_INFO,
235 "Invalid acquire order: Thread %X owns [%s], wants [%s]",
236 this_thread_id,
237 acpi_ut_get_mutex_name(i),
238 acpi_ut_get_mutex_name(mutex_id)));
73459f73
RM
239
240 return (AE_ACQUIRE_DEADLOCK);
241 }
242 }
243 }
244#endif
245
4be44fcd 246 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
965a3d44 247 "Thread %lX attempting to acquire Mutex [%s]\n",
fd350943 248 (unsigned long)this_thread_id,
965a3d44 249 acpi_ut_get_mutex_name(mutex_id)));
73459f73 250
967440e3
BM
251 status = acpi_os_acquire_mutex(acpi_gbl_mutex_info[mutex_id].mutex,
252 ACPI_WAIT_FOREVER);
4be44fcd
LB
253 if (ACPI_SUCCESS(status)) {
254 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
965a3d44 255 "Thread %lX acquired Mutex [%s]\n",
fd350943 256 (unsigned long)this_thread_id,
4be44fcd 257 acpi_ut_get_mutex_name(mutex_id)));
73459f73
RM
258
259 acpi_gbl_mutex_info[mutex_id].use_count++;
f9f4601f 260 acpi_gbl_mutex_info[mutex_id].thread_id = this_thread_id;
4be44fcd 261 } else {
b8e4d893 262 ACPI_EXCEPTION((AE_INFO, status,
965a3d44 263 "Thread %lX could not acquire Mutex [%X]",
fd350943 264 (unsigned long)this_thread_id, mutex_id));
73459f73
RM
265 }
266
267 return (status);
268}
269
73459f73
RM
270/*******************************************************************************
271 *
272 * FUNCTION: acpi_ut_release_mutex
273 *
274 * PARAMETERS: mutex_iD - ID of the mutex to be released
275 *
276 * RETURN: Status
277 *
278 * DESCRIPTION: Release a mutex object.
279 *
280 ******************************************************************************/
281
4be44fcd 282acpi_status acpi_ut_release_mutex(acpi_mutex_handle mutex_id)
73459f73 283{
8313524a 284 acpi_thread_id this_thread_id;
73459f73 285
b229cf92 286 ACPI_FUNCTION_NAME(ut_release_mutex);
73459f73 287
4be44fcd
LB
288 this_thread_id = acpi_os_get_thread_id();
289 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
965a3d44 290 "Thread %lX releasing Mutex [%s]\n",
fd350943 291 (unsigned long)this_thread_id,
4be44fcd 292 acpi_ut_get_mutex_name(mutex_id)));
73459f73 293
4c90ece2 294 if (mutex_id > ACPI_MAX_MUTEX) {
73459f73
RM
295 return (AE_BAD_PARAMETER);
296 }
297
298 /*
299 * Mutex must be acquired in order to release it!
300 */
f9f4601f 301 if (acpi_gbl_mutex_info[mutex_id].thread_id == ACPI_MUTEX_NOT_ACQUIRED) {
b8e4d893
BM
302 ACPI_ERROR((AE_INFO,
303 "Mutex [%X] is not acquired, cannot release",
304 mutex_id));
73459f73
RM
305
306 return (AE_NOT_ACQUIRED);
307 }
73459f73
RM
308#ifdef ACPI_MUTEX_DEBUG
309 {
4be44fcd 310 u32 i;
73459f73
RM
311 /*
312 * Mutex debug code, for internal debugging only.
313 *
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.
318 */
b53ce3f7 319 for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
bda663d3 320 if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
73459f73
RM
321 if (i == mutex_id) {
322 continue;
323 }
324
b8e4d893
BM
325 ACPI_ERROR((AE_INFO,
326 "Invalid release order: owns [%s], releasing [%s]",
327 acpi_ut_get_mutex_name(i),
328 acpi_ut_get_mutex_name(mutex_id)));
73459f73
RM
329
330 return (AE_RELEASE_DEADLOCK);
331 }
332 }
333 }
334#endif
335
336 /* Mark unlocked FIRST */
337
f9f4601f 338 acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
73459f73 339
967440e3
BM
340 acpi_os_release_mutex(acpi_gbl_mutex_info[mutex_id].mutex);
341 return (AE_OK);
73459f73 342}