Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / acpi / acpica / evglock.c
blob56a562a1e5d7bde44f5d15b40dc250843b2de41f
1 /******************************************************************************
3 * Module Name: evglock - Global Lock support
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2011, Intel Corp.
9 * All rights reserved.
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.
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.
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.
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acevents.h"
47 #include "acinterp.h"
49 #define _COMPONENT ACPI_EVENTS
50 ACPI_MODULE_NAME("evglock")
52 /* Local prototypes */
53 static u32 acpi_ev_global_lock_handler(void *context);
55 /*******************************************************************************
57 * FUNCTION: acpi_ev_init_global_lock_handler
59 * PARAMETERS: None
61 * RETURN: Status
63 * DESCRIPTION: Install a handler for the global lock release event
65 ******************************************************************************/
67 acpi_status acpi_ev_init_global_lock_handler(void)
69 acpi_status status;
71 ACPI_FUNCTION_TRACE(ev_init_global_lock_handler);
73 /* Attempt installation of the global lock handler */
75 status = acpi_install_fixed_event_handler(ACPI_EVENT_GLOBAL,
76 acpi_ev_global_lock_handler,
77 NULL);
80 * If the global lock does not exist on this platform, the attempt to
81 * enable GBL_STATUS will fail (the GBL_ENABLE bit will not stick).
82 * Map to AE_OK, but mark global lock as not present. Any attempt to
83 * actually use the global lock will be flagged with an error.
85 acpi_gbl_global_lock_present = FALSE;
86 if (status == AE_NO_HARDWARE_RESPONSE) {
87 ACPI_ERROR((AE_INFO,
88 "No response from Global Lock hardware, disabling lock"));
90 return_ACPI_STATUS(AE_OK);
93 status = acpi_os_create_lock(&acpi_gbl_global_lock_pending_lock);
94 if (ACPI_FAILURE(status)) {
95 return_ACPI_STATUS(status);
98 acpi_gbl_global_lock_pending = FALSE;
99 acpi_gbl_global_lock_present = TRUE;
100 return_ACPI_STATUS(status);
103 /*******************************************************************************
105 * FUNCTION: acpi_ev_remove_global_lock_handler
107 * PARAMETERS: None
109 * RETURN: Status
111 * DESCRIPTION: Remove the handler for the Global Lock
113 ******************************************************************************/
115 acpi_status acpi_ev_remove_global_lock_handler(void)
117 acpi_status status;
119 ACPI_FUNCTION_TRACE(ev_remove_global_lock_handler);
121 acpi_gbl_global_lock_present = FALSE;
122 status = acpi_remove_fixed_event_handler(ACPI_EVENT_GLOBAL,
123 acpi_ev_global_lock_handler);
125 return_ACPI_STATUS(status);
128 /*******************************************************************************
130 * FUNCTION: acpi_ev_global_lock_handler
132 * PARAMETERS: Context - From thread interface, not used
134 * RETURN: ACPI_INTERRUPT_HANDLED
136 * DESCRIPTION: Invoked directly from the SCI handler when a global lock
137 * release interrupt occurs. If there is actually a pending
138 * request for the lock, signal the waiting thread.
140 ******************************************************************************/
142 static u32 acpi_ev_global_lock_handler(void *context)
144 acpi_status status;
145 acpi_cpu_flags flags;
147 flags = acpi_os_acquire_lock(acpi_gbl_global_lock_pending_lock);
150 * If a request for the global lock is not actually pending,
151 * we are done. This handles "spurious" global lock interrupts
152 * which are possible (and have been seen) with bad BIOSs.
154 if (!acpi_gbl_global_lock_pending) {
155 goto cleanup_and_exit;
159 * Send a unit to the global lock semaphore. The actual acquisition
160 * of the global lock will be performed by the waiting thread.
162 status = acpi_os_signal_semaphore(acpi_gbl_global_lock_semaphore, 1);
163 if (ACPI_FAILURE(status)) {
164 ACPI_ERROR((AE_INFO, "Could not signal Global Lock semaphore"));
167 acpi_gbl_global_lock_pending = FALSE;
169 cleanup_and_exit:
171 acpi_os_release_lock(acpi_gbl_global_lock_pending_lock, flags);
172 return (ACPI_INTERRUPT_HANDLED);
175 /******************************************************************************
177 * FUNCTION: acpi_ev_acquire_global_lock
179 * PARAMETERS: Timeout - Max time to wait for the lock, in millisec.
181 * RETURN: Status
183 * DESCRIPTION: Attempt to gain ownership of the Global Lock.
185 * MUTEX: Interpreter must be locked
187 * Note: The original implementation allowed multiple threads to "acquire" the
188 * Global Lock, and the OS would hold the lock until the last thread had
189 * released it. However, this could potentially starve the BIOS out of the
190 * lock, especially in the case where there is a tight handshake between the
191 * Embedded Controller driver and the BIOS. Therefore, this implementation
192 * allows only one thread to acquire the HW Global Lock at a time, and makes
193 * the global lock appear as a standard mutex on the OS side.
195 *****************************************************************************/
197 acpi_status acpi_ev_acquire_global_lock(u16 timeout)
199 acpi_cpu_flags flags;
200 acpi_status status;
201 u8 acquired = FALSE;
203 ACPI_FUNCTION_TRACE(ev_acquire_global_lock);
206 * Only one thread can acquire the GL at a time, the global_lock_mutex
207 * enforces this. This interface releases the interpreter if we must wait.
209 status =
210 acpi_ex_system_wait_mutex(acpi_gbl_global_lock_mutex->mutex.
211 os_mutex, timeout);
212 if (ACPI_FAILURE(status)) {
213 return_ACPI_STATUS(status);
217 * Update the global lock handle and check for wraparound. The handle is
218 * only used for the external global lock interfaces, but it is updated
219 * here to properly handle the case where a single thread may acquire the
220 * lock via both the AML and the acpi_acquire_global_lock interfaces. The
221 * handle is therefore updated on the first acquire from a given thread
222 * regardless of where the acquisition request originated.
224 acpi_gbl_global_lock_handle++;
225 if (acpi_gbl_global_lock_handle == 0) {
226 acpi_gbl_global_lock_handle = 1;
230 * Make sure that a global lock actually exists. If not, just
231 * treat the lock as a standard mutex.
233 if (!acpi_gbl_global_lock_present) {
234 acpi_gbl_global_lock_acquired = TRUE;
235 return_ACPI_STATUS(AE_OK);
238 flags = acpi_os_acquire_lock(acpi_gbl_global_lock_pending_lock);
240 do {
242 /* Attempt to acquire the actual hardware lock */
244 ACPI_ACQUIRE_GLOBAL_LOCK(acpi_gbl_FACS, acquired);
245 if (acquired) {
246 acpi_gbl_global_lock_acquired = TRUE;
247 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
248 "Acquired hardware Global Lock\n"));
249 break;
253 * Did not get the lock. The pending bit was set above, and
254 * we must now wait until we receive the global lock
255 * released interrupt.
257 acpi_gbl_global_lock_pending = TRUE;
258 acpi_os_release_lock(acpi_gbl_global_lock_pending_lock, flags);
260 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
261 "Waiting for hardware Global Lock\n"));
264 * Wait for handshake with the global lock interrupt handler.
265 * This interface releases the interpreter if we must wait.
267 status =
268 acpi_ex_system_wait_semaphore
269 (acpi_gbl_global_lock_semaphore, ACPI_WAIT_FOREVER);
271 flags = acpi_os_acquire_lock(acpi_gbl_global_lock_pending_lock);
273 } while (ACPI_SUCCESS(status));
275 acpi_gbl_global_lock_pending = FALSE;
276 acpi_os_release_lock(acpi_gbl_global_lock_pending_lock, flags);
278 return_ACPI_STATUS(status);
281 /*******************************************************************************
283 * FUNCTION: acpi_ev_release_global_lock
285 * PARAMETERS: None
287 * RETURN: Status
289 * DESCRIPTION: Releases ownership of the Global Lock.
291 ******************************************************************************/
293 acpi_status acpi_ev_release_global_lock(void)
295 u8 pending = FALSE;
296 acpi_status status = AE_OK;
298 ACPI_FUNCTION_TRACE(ev_release_global_lock);
300 /* Lock must be already acquired */
302 if (!acpi_gbl_global_lock_acquired) {
303 ACPI_WARNING((AE_INFO,
304 "Cannot release the ACPI Global Lock, it has not been acquired"));
305 return_ACPI_STATUS(AE_NOT_ACQUIRED);
308 if (acpi_gbl_global_lock_present) {
310 /* Allow any thread to release the lock */
312 ACPI_RELEASE_GLOBAL_LOCK(acpi_gbl_FACS, pending);
315 * If the pending bit was set, we must write GBL_RLS to the control
316 * register
318 if (pending) {
319 status =
320 acpi_write_bit_register
321 (ACPI_BITREG_GLOBAL_LOCK_RELEASE,
322 ACPI_ENABLE_EVENT);
325 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
326 "Released hardware Global Lock\n"));
329 acpi_gbl_global_lock_acquired = FALSE;
331 /* Release the local GL mutex */
333 acpi_os_release_mutex(acpi_gbl_global_lock_mutex->mutex.os_mutex);
334 return_ACPI_STATUS(status);