Merge tag 'v3.9-rc3' into drm-intel-next-queued
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / acpica / utmutex.c
CommitLineData
73459f73
RM
1/*******************************************************************************
2 *
3 * Module Name: utmutex - local mutex support
4 *
5 ******************************************************************************/
6
7/*
25f044e6 8 * Copyright (C) 2000 - 2013, 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>
e2f7a777 45#include "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
2147d3f0 53static void 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 *
8a335a23
BM
63 * DESCRIPTION: Create the system mutex objects. This includes mutexes,
64 * spin locks, and reader/writer locks.
73459f73
RM
65 *
66 ******************************************************************************/
67
4be44fcd 68acpi_status acpi_ut_mutex_initialize(void)
73459f73 69{
4be44fcd
LB
70 u32 i;
71 acpi_status status;
73459f73 72
b229cf92 73 ACPI_FUNCTION_TRACE(ut_mutex_initialize);
73459f73 74
8a335a23
BM
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
3854c8e3
LM
86 status = acpi_os_create_lock (&acpi_gbl_gpe_lock);
87 if (ACPI_FAILURE (status)) {
88 return_ACPI_STATUS (status);
89 }
90
91 status = acpi_os_create_lock (&acpi_gbl_hardware_lock);
92 if (ACPI_FAILURE (status)) {
93 return_ACPI_STATUS (status);
94 }
95
b0ed7a91
LM
96 /* Mutex for _OSI support */
97 status = acpi_os_create_mutex(&acpi_gbl_osi_mutex);
98 if (ACPI_FAILURE(status)) {
99 return_ACPI_STATUS(status);
100 }
101
8a335a23
BM
102 /* Create the reader/writer lock for namespace access */
103
104 status = acpi_ut_create_rw_lock(&acpi_gbl_namespace_rw_lock);
4be44fcd 105 return_ACPI_STATUS(status);
73459f73
RM
106}
107
73459f73
RM
108/*******************************************************************************
109 *
110 * FUNCTION: acpi_ut_mutex_terminate
111 *
112 * PARAMETERS: None.
113 *
114 * RETURN: None.
115 *
8a335a23
BM
116 * DESCRIPTION: Delete all of the system mutex objects. This includes mutexes,
117 * spin locks, and reader/writer locks.
73459f73
RM
118 *
119 ******************************************************************************/
120
4be44fcd 121void acpi_ut_mutex_terminate(void)
73459f73 122{
4be44fcd 123 u32 i;
73459f73 124
b229cf92 125 ACPI_FUNCTION_TRACE(ut_mutex_terminate);
73459f73 126
8a335a23
BM
127 /* Delete each predefined mutex object */
128
4c90ece2 129 for (i = 0; i < ACPI_NUM_MUTEX; i++) {
2147d3f0 130 acpi_ut_delete_mutex(i);
73459f73
RM
131 }
132
b0ed7a91
LM
133 acpi_os_delete_mutex(acpi_gbl_osi_mutex);
134
4c90ece2
BM
135 /* Delete the spinlocks */
136
4be44fcd 137 acpi_os_delete_lock(acpi_gbl_gpe_lock);
4c90ece2 138 acpi_os_delete_lock(acpi_gbl_hardware_lock);
8a335a23
BM
139
140 /* Delete the reader/writer lock */
141
142 acpi_ut_delete_rw_lock(&acpi_gbl_namespace_rw_lock);
73459f73
RM
143 return_VOID;
144}
145
73459f73
RM
146/*******************************************************************************
147 *
148 * FUNCTION: acpi_ut_create_mutex
149 *
ba494bee 150 * PARAMETERS: mutex_ID - ID of the mutex to be created
73459f73
RM
151 *
152 * RETURN: Status
153 *
154 * DESCRIPTION: Create a mutex object.
155 *
156 ******************************************************************************/
157
4be44fcd 158static acpi_status acpi_ut_create_mutex(acpi_mutex_handle mutex_id)
73459f73 159{
4be44fcd 160 acpi_status status = AE_OK;
73459f73 161
b229cf92 162 ACPI_FUNCTION_TRACE_U32(ut_create_mutex, mutex_id);
73459f73 163
73459f73 164 if (!acpi_gbl_mutex_info[mutex_id].mutex) {
967440e3
BM
165 status =
166 acpi_os_create_mutex(&acpi_gbl_mutex_info[mutex_id].mutex);
4be44fcd
LB
167 acpi_gbl_mutex_info[mutex_id].thread_id =
168 ACPI_MUTEX_NOT_ACQUIRED;
73459f73
RM
169 acpi_gbl_mutex_info[mutex_id].use_count = 0;
170 }
171
4be44fcd 172 return_ACPI_STATUS(status);
73459f73
RM
173}
174
73459f73
RM
175/*******************************************************************************
176 *
177 * FUNCTION: acpi_ut_delete_mutex
178 *
ba494bee 179 * PARAMETERS: mutex_ID - ID of the mutex to be deleted
73459f73
RM
180 *
181 * RETURN: Status
182 *
183 * DESCRIPTION: Delete a mutex object.
184 *
185 ******************************************************************************/
186
2147d3f0 187static void acpi_ut_delete_mutex(acpi_mutex_handle mutex_id)
73459f73 188{
73459f73 189
b229cf92 190 ACPI_FUNCTION_TRACE_U32(ut_delete_mutex, mutex_id);
73459f73 191
967440e3 192 acpi_os_delete_mutex(acpi_gbl_mutex_info[mutex_id].mutex);
73459f73
RM
193
194 acpi_gbl_mutex_info[mutex_id].mutex = NULL;
f9f4601f 195 acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
68aafc35
BM
196
197 return_VOID;
73459f73
RM
198}
199
73459f73
RM
200/*******************************************************************************
201 *
202 * FUNCTION: acpi_ut_acquire_mutex
203 *
ba494bee 204 * PARAMETERS: mutex_ID - ID of the mutex to be acquired
73459f73
RM
205 *
206 * RETURN: Status
207 *
208 * DESCRIPTION: Acquire a mutex object.
209 *
210 ******************************************************************************/
211
4be44fcd 212acpi_status acpi_ut_acquire_mutex(acpi_mutex_handle mutex_id)
73459f73 213{
4be44fcd 214 acpi_status status;
8313524a 215 acpi_thread_id this_thread_id;
73459f73 216
b229cf92 217 ACPI_FUNCTION_NAME(ut_acquire_mutex);
73459f73 218
4c90ece2 219 if (mutex_id > ACPI_MAX_MUTEX) {
73459f73
RM
220 return (AE_BAD_PARAMETER);
221 }
222
4be44fcd 223 this_thread_id = acpi_os_get_thread_id();
73459f73
RM
224
225#ifdef ACPI_MUTEX_DEBUG
226 {
4be44fcd 227 u32 i;
73459f73
RM
228 /*
229 * Mutex debug code, for internal debugging only.
230 *
73a3090a
BM
231 * Deadlock prevention. Check if this thread owns any mutexes of value
232 * greater than or equal to this one. If so, the thread has violated
233 * the mutex ordering rule. This indicates a coding error somewhere in
73459f73
RM
234 * the ACPI subsystem code.
235 */
b53ce3f7 236 for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
bda663d3 237 if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
73459f73 238 if (i == mutex_id) {
b8e4d893 239 ACPI_ERROR((AE_INFO,
28eb3fcf 240 "Mutex [%s] already acquired by this thread [%u]",
b8e4d893
BM
241 acpi_ut_get_mutex_name
242 (mutex_id),
28eb3fcf 243 (u32)this_thread_id));
73459f73
RM
244
245 return (AE_ALREADY_ACQUIRED);
246 }
247
b8e4d893 248 ACPI_ERROR((AE_INFO,
28eb3fcf
LM
249 "Invalid acquire order: Thread %u owns [%s], wants [%s]",
250 (u32)this_thread_id,
b8e4d893
BM
251 acpi_ut_get_mutex_name(i),
252 acpi_ut_get_mutex_name(mutex_id)));
73459f73
RM
253
254 return (AE_ACQUIRE_DEADLOCK);
255 }
256 }
257 }
258#endif
259
4be44fcd 260 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
28eb3fcf
LM
261 "Thread %u attempting to acquire Mutex [%s]\n",
262 (u32)this_thread_id,
965a3d44 263 acpi_ut_get_mutex_name(mutex_id)));
73459f73 264
967440e3
BM
265 status = acpi_os_acquire_mutex(acpi_gbl_mutex_info[mutex_id].mutex,
266 ACPI_WAIT_FOREVER);
4be44fcd
LB
267 if (ACPI_SUCCESS(status)) {
268 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
28eb3fcf
LM
269 "Thread %u acquired Mutex [%s]\n",
270 (u32)this_thread_id,
4be44fcd 271 acpi_ut_get_mutex_name(mutex_id)));
73459f73
RM
272
273 acpi_gbl_mutex_info[mutex_id].use_count++;
f9f4601f 274 acpi_gbl_mutex_info[mutex_id].thread_id = this_thread_id;
4be44fcd 275 } else {
b8e4d893 276 ACPI_EXCEPTION((AE_INFO, status,
28eb3fcf
LM
277 "Thread %u could not acquire Mutex [0x%X]",
278 (u32)this_thread_id, mutex_id));
73459f73
RM
279 }
280
281 return (status);
282}
283
73459f73
RM
284/*******************************************************************************
285 *
286 * FUNCTION: acpi_ut_release_mutex
287 *
ba494bee 288 * PARAMETERS: mutex_ID - ID of the mutex to be released
73459f73
RM
289 *
290 * RETURN: Status
291 *
292 * DESCRIPTION: Release a mutex object.
293 *
294 ******************************************************************************/
295
4be44fcd 296acpi_status acpi_ut_release_mutex(acpi_mutex_handle mutex_id)
73459f73 297{
b229cf92 298 ACPI_FUNCTION_NAME(ut_release_mutex);
73459f73 299
28eb3fcf 300 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Thread %u releasing Mutex [%s]\n",
42f52843 301 (u32)acpi_os_get_thread_id(),
4be44fcd 302 acpi_ut_get_mutex_name(mutex_id)));
73459f73 303
4c90ece2 304 if (mutex_id > ACPI_MAX_MUTEX) {
73459f73
RM
305 return (AE_BAD_PARAMETER);
306 }
307
308 /*
309 * Mutex must be acquired in order to release it!
310 */
f9f4601f 311 if (acpi_gbl_mutex_info[mutex_id].thread_id == ACPI_MUTEX_NOT_ACQUIRED) {
b8e4d893 312 ACPI_ERROR((AE_INFO,
f6a22b0b 313 "Mutex [0x%X] is not acquired, cannot release",
b8e4d893 314 mutex_id));
73459f73
RM
315
316 return (AE_NOT_ACQUIRED);
317 }
73459f73
RM
318#ifdef ACPI_MUTEX_DEBUG
319 {
4be44fcd 320 u32 i;
73459f73
RM
321 /*
322 * Mutex debug code, for internal debugging only.
323 *
73a3090a
BM
324 * Deadlock prevention. Check if this thread owns any mutexes of value
325 * greater than this one. If so, the thread has violated the mutex
326 * ordering rule. This indicates a coding error somewhere in
73459f73
RM
327 * the ACPI subsystem code.
328 */
b53ce3f7 329 for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
42f52843
BM
330 if (acpi_gbl_mutex_info[i].thread_id ==
331 acpi_os_get_thread_id()) {
73459f73
RM
332 if (i == mutex_id) {
333 continue;
334 }
335
b8e4d893
BM
336 ACPI_ERROR((AE_INFO,
337 "Invalid release order: owns [%s], releasing [%s]",
338 acpi_ut_get_mutex_name(i),
339 acpi_ut_get_mutex_name(mutex_id)));
73459f73
RM
340
341 return (AE_RELEASE_DEADLOCK);
342 }
343 }
344 }
345#endif
346
347 /* Mark unlocked FIRST */
348
f9f4601f 349 acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
73459f73 350
967440e3
BM
351 acpi_os_release_mutex(acpi_gbl_mutex_info[mutex_id].mutex);
352 return (AE_OK);
73459f73 353}