xref: /openbmc/linux/drivers/acpi/acpica/utmutex.c (revision b8bb76713ec50df2f11efee386e16f93d51e1076)
1 /*******************************************************************************
2  *
3  * Module Name: utmutex - local mutex support
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2008, Intel Corp.
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 #include "accommon.h"
46 
47 #define _COMPONENT          ACPI_UTILITIES
48 ACPI_MODULE_NAME("utmutex")
49 
50 /* Local prototypes */
51 static acpi_status acpi_ut_create_mutex(acpi_mutex_handle mutex_id);
52 
53 static acpi_status acpi_ut_delete_mutex(acpi_mutex_handle mutex_id);
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 
67 acpi_status acpi_ut_mutex_initialize(void)
68 {
69 	u32 i;
70 	acpi_status status;
71 
72 	ACPI_FUNCTION_TRACE(ut_mutex_initialize);
73 
74 	/*
75 	 * Create each of the predefined mutex objects
76 	 */
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);
81 		}
82 	}
83 
84 	/* Create the spinlocks for use at interrupt level */
85 
86 	spin_lock_init(acpi_gbl_gpe_lock);
87 	spin_lock_init(acpi_gbl_hardware_lock);
88 
89 	return_ACPI_STATUS(status);
90 }
91 
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 
104 void acpi_ut_mutex_terminate(void)
105 {
106 	u32 i;
107 
108 	ACPI_FUNCTION_TRACE(ut_mutex_terminate);
109 
110 	/*
111 	 * Delete each predefined mutex object
112 	 */
113 	for (i = 0; i < ACPI_NUM_MUTEX; i++) {
114 		(void)acpi_ut_delete_mutex(i);
115 	}
116 
117 	/* Delete the spinlocks */
118 
119 	acpi_os_delete_lock(acpi_gbl_gpe_lock);
120 	acpi_os_delete_lock(acpi_gbl_hardware_lock);
121 	return_VOID;
122 }
123 
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 
136 static acpi_status acpi_ut_create_mutex(acpi_mutex_handle mutex_id)
137 {
138 	acpi_status status = AE_OK;
139 
140 	ACPI_FUNCTION_TRACE_U32(ut_create_mutex, mutex_id);
141 
142 	if (mutex_id > ACPI_MAX_MUTEX) {
143 		return_ACPI_STATUS(AE_BAD_PARAMETER);
144 	}
145 
146 	if (!acpi_gbl_mutex_info[mutex_id].mutex) {
147 		status =
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;
152 	}
153 
154 	return_ACPI_STATUS(status);
155 }
156 
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 
169 static acpi_status acpi_ut_delete_mutex(acpi_mutex_handle mutex_id)
170 {
171 
172 	ACPI_FUNCTION_TRACE_U32(ut_delete_mutex, mutex_id);
173 
174 	if (mutex_id > ACPI_MAX_MUTEX) {
175 		return_ACPI_STATUS(AE_BAD_PARAMETER);
176 	}
177 
178 	acpi_os_delete_mutex(acpi_gbl_mutex_info[mutex_id].mutex);
179 
180 	acpi_gbl_mutex_info[mutex_id].mutex = NULL;
181 	acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
182 
183 	return_ACPI_STATUS(AE_OK);
184 }
185 
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 
198 acpi_status acpi_ut_acquire_mutex(acpi_mutex_handle mutex_id)
199 {
200 	acpi_status status;
201 	acpi_thread_id this_thread_id;
202 
203 	ACPI_FUNCTION_NAME(ut_acquire_mutex);
204 
205 	if (mutex_id > ACPI_MAX_MUTEX) {
206 		return (AE_BAD_PARAMETER);
207 	}
208 
209 	this_thread_id = acpi_os_get_thread_id();
210 
211 #ifdef ACPI_MUTEX_DEBUG
212 	{
213 		u32 i;
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 		 */
222 		for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
223 			if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
224 				if (i == mutex_id) {
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));
230 
231 					return (AE_ALREADY_ACQUIRED);
232 				}
233 
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)));
239 
240 				return (AE_ACQUIRE_DEADLOCK);
241 			}
242 		}
243 	}
244 #endif
245 
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)));
250 
251 	status = acpi_os_acquire_mutex(acpi_gbl_mutex_info[mutex_id].mutex,
252 				       ACPI_WAIT_FOREVER);
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)));
258 
259 		acpi_gbl_mutex_info[mutex_id].use_count++;
260 		acpi_gbl_mutex_info[mutex_id].thread_id = this_thread_id;
261 	} else {
262 		ACPI_EXCEPTION((AE_INFO, status,
263 				"Thread %lX could not acquire Mutex [%X]",
264 				(unsigned long)this_thread_id, mutex_id));
265 	}
266 
267 	return (status);
268 }
269 
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 
282 acpi_status acpi_ut_release_mutex(acpi_mutex_handle mutex_id)
283 {
284 	acpi_thread_id this_thread_id;
285 
286 	ACPI_FUNCTION_NAME(ut_release_mutex);
287 
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)));
293 
294 	if (mutex_id > ACPI_MAX_MUTEX) {
295 		return (AE_BAD_PARAMETER);
296 	}
297 
298 	/*
299 	 * Mutex must be acquired in order to release it!
300 	 */
301 	if (acpi_gbl_mutex_info[mutex_id].thread_id == ACPI_MUTEX_NOT_ACQUIRED) {
302 		ACPI_ERROR((AE_INFO,
303 			    "Mutex [%X] is not acquired, cannot release",
304 			    mutex_id));
305 
306 		return (AE_NOT_ACQUIRED);
307 	}
308 #ifdef ACPI_MUTEX_DEBUG
309 	{
310 		u32 i;
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 		 */
319 		for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
320 			if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
321 				if (i == mutex_id) {
322 					continue;
323 				}
324 
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)));
329 
330 				return (AE_RELEASE_DEADLOCK);
331 			}
332 		}
333 	}
334 #endif
335 
336 	/* Mark unlocked FIRST */
337 
338 	acpi_gbl_mutex_info[mutex_id].thread_id = ACPI_MUTEX_NOT_ACQUIRED;
339 
340 	acpi_os_release_mutex(acpi_gbl_mutex_info[mutex_id].mutex);
341 	return (AE_OK);
342 }
343