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