1 /******************************************************************************
3 * Module Name: evglock - Global Lock support
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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 <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acevents.h>
47 #include <contrib/dev/acpica/include/acinterp.h>
49 #define _COMPONENT ACPI_EVENTS
50 ACPI_MODULE_NAME ("evglock")
53 /* Local prototypes */
56 AcpiEvGlobalLockHandler (
60 /*******************************************************************************
62 * FUNCTION: AcpiEvInitGlobalLockHandler
68 * DESCRIPTION: Install a handler for the global lock release event
70 ******************************************************************************/
73 AcpiEvInitGlobalLockHandler (
79 ACPI_FUNCTION_TRACE (EvInitGlobalLockHandler);
82 /* Attempt installation of the global lock handler */
84 Status = AcpiInstallFixedEventHandler (ACPI_EVENT_GLOBAL,
85 AcpiEvGlobalLockHandler, NULL);
88 * If the global lock does not exist on this platform, the attempt to
89 * enable GBL_STATUS will fail (the GBL_ENABLE bit will not stick).
90 * Map to AE_OK, but mark global lock as not present. Any attempt to
91 * actually use the global lock will be flagged with an error.
93 AcpiGbl_GlobalLockPresent = FALSE;
94 if (Status == AE_NO_HARDWARE_RESPONSE)
97 "No response from Global Lock hardware, disabling lock"));
99 return_ACPI_STATUS (AE_OK);
102 Status = AcpiOsCreateLock (&AcpiGbl_GlobalLockPendingLock);
103 if (ACPI_FAILURE (Status))
105 return_ACPI_STATUS (Status);
108 AcpiGbl_GlobalLockPending = FALSE;
109 AcpiGbl_GlobalLockPresent = TRUE;
110 return_ACPI_STATUS (Status);
114 /*******************************************************************************
116 * FUNCTION: AcpiEvRemoveGlobalLockHandler
122 * DESCRIPTION: Remove the handler for the Global Lock
124 ******************************************************************************/
127 AcpiEvRemoveGlobalLockHandler (
133 ACPI_FUNCTION_TRACE (EvRemoveGlobalLockHandler);
135 AcpiGbl_GlobalLockPresent = FALSE;
136 Status = AcpiRemoveFixedEventHandler (ACPI_EVENT_GLOBAL,
137 AcpiEvGlobalLockHandler);
139 return_ACPI_STATUS (Status);
143 /*******************************************************************************
145 * FUNCTION: AcpiEvGlobalLockHandler
147 * PARAMETERS: Context - From thread interface, not used
149 * RETURN: ACPI_INTERRUPT_HANDLED
151 * DESCRIPTION: Invoked directly from the SCI handler when a global lock
152 * release interrupt occurs. If there is actually a pending
153 * request for the lock, signal the waiting thread.
155 ******************************************************************************/
158 AcpiEvGlobalLockHandler (
162 ACPI_CPU_FLAGS Flags;
165 Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
168 * If a request for the global lock is not actually pending,
169 * we are done. This handles "spurious" global lock interrupts
170 * which are possible (and have been seen) with bad BIOSs.
172 if (!AcpiGbl_GlobalLockPending)
178 * Send a unit to the global lock semaphore. The actual acquisition
179 * of the global lock will be performed by the waiting thread.
181 Status = AcpiOsSignalSemaphore (AcpiGbl_GlobalLockSemaphore, 1);
182 if (ACPI_FAILURE (Status))
184 ACPI_ERROR ((AE_INFO, "Could not signal Global Lock semaphore"));
187 AcpiGbl_GlobalLockPending = FALSE;
192 AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
193 return (ACPI_INTERRUPT_HANDLED);
197 /******************************************************************************
199 * FUNCTION: AcpiEvAcquireGlobalLock
201 * PARAMETERS: Timeout - Max time to wait for the lock, in millisec.
205 * DESCRIPTION: Attempt to gain ownership of the Global Lock.
207 * MUTEX: Interpreter must be locked
209 * Note: The original implementation allowed multiple threads to "acquire" the
210 * Global Lock, and the OS would hold the lock until the last thread had
211 * released it. However, this could potentially starve the BIOS out of the
212 * lock, especially in the case where there is a tight handshake between the
213 * Embedded Controller driver and the BIOS. Therefore, this implementation
214 * allows only one thread to acquire the HW Global Lock at a time, and makes
215 * the global lock appear as a standard mutex on the OS side.
217 *****************************************************************************/
220 AcpiEvAcquireGlobalLock (
223 ACPI_CPU_FLAGS Flags;
225 BOOLEAN Acquired = FALSE;
228 ACPI_FUNCTION_TRACE (EvAcquireGlobalLock);
232 * Only one thread can acquire the GL at a time, the GlobalLockMutex
233 * enforces this. This interface releases the interpreter if we must wait.
235 Status = AcpiExSystemWaitMutex (AcpiGbl_GlobalLockMutex->Mutex.OsMutex,
237 if (ACPI_FAILURE (Status))
239 return_ACPI_STATUS (Status);
243 * Update the global lock handle and check for wraparound. The handle is
244 * only used for the external global lock interfaces, but it is updated
245 * here to properly handle the case where a single thread may acquire the
246 * lock via both the AML and the AcpiAcquireGlobalLock interfaces. The
247 * handle is therefore updated on the first acquire from a given thread
248 * regardless of where the acquisition request originated.
250 AcpiGbl_GlobalLockHandle++;
251 if (AcpiGbl_GlobalLockHandle == 0)
253 AcpiGbl_GlobalLockHandle = 1;
257 * Make sure that a global lock actually exists. If not, just
258 * treat the lock as a standard mutex.
260 if (!AcpiGbl_GlobalLockPresent)
262 AcpiGbl_GlobalLockAcquired = TRUE;
263 return_ACPI_STATUS (AE_OK);
266 Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
270 /* Attempt to acquire the actual hardware lock */
272 ACPI_ACQUIRE_GLOBAL_LOCK (AcpiGbl_FACS, Acquired);
275 AcpiGbl_GlobalLockAcquired = TRUE;
276 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
277 "Acquired hardware Global Lock\n"));
282 * Did not get the lock. The pending bit was set above, and
283 * we must now wait until we receive the global lock
284 * released interrupt.
286 AcpiGbl_GlobalLockPending = TRUE;
287 AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
289 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
290 "Waiting for hardware Global Lock\n"));
293 * Wait for handshake with the global lock interrupt handler.
294 * This interface releases the interpreter if we must wait.
296 Status = AcpiExSystemWaitSemaphore (AcpiGbl_GlobalLockSemaphore,
299 Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
301 } while (ACPI_SUCCESS (Status));
303 AcpiGbl_GlobalLockPending = FALSE;
304 AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
306 return_ACPI_STATUS (Status);
310 /*******************************************************************************
312 * FUNCTION: AcpiEvReleaseGlobalLock
318 * DESCRIPTION: Releases ownership of the Global Lock.
320 ******************************************************************************/
323 AcpiEvReleaseGlobalLock (
326 BOOLEAN Pending = FALSE;
327 ACPI_STATUS Status = AE_OK;
330 ACPI_FUNCTION_TRACE (EvReleaseGlobalLock);
333 /* Lock must be already acquired */
335 if (!AcpiGbl_GlobalLockAcquired)
337 ACPI_WARNING ((AE_INFO,
338 "Cannot release the ACPI Global Lock, it has not been acquired"));
339 return_ACPI_STATUS (AE_NOT_ACQUIRED);
342 if (AcpiGbl_GlobalLockPresent)
344 /* Allow any thread to release the lock */
346 ACPI_RELEASE_GLOBAL_LOCK (AcpiGbl_FACS, Pending);
349 * If the pending bit was set, we must write GBL_RLS to the control
354 Status = AcpiWriteBitRegister (
355 ACPI_BITREG_GLOBAL_LOCK_RELEASE, ACPI_ENABLE_EVENT);
358 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Released hardware Global Lock\n"));
361 AcpiGbl_GlobalLockAcquired = FALSE;
363 /* Release the local GL mutex */
365 AcpiOsReleaseMutex (AcpiGbl_GlobalLockMutex->Mutex.OsMutex);
366 return_ACPI_STATUS (Status);