dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / uts / intel / io / acpica / events / evglock.c
blobcaafa977bcffd51ce10ee4b78401e59f32127115
1 /******************************************************************************
3 * Module Name: evglock - Global Lock support
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, 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.h"
45 #include "accommon.h"
46 #include "acevents.h"
47 #include "acinterp.h"
49 #define _COMPONENT ACPI_EVENTS
50 ACPI_MODULE_NAME ("evglock")
52 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
54 /* Local prototypes */
56 static UINT32
57 AcpiEvGlobalLockHandler (
58 void *Context);
61 /*******************************************************************************
63 * FUNCTION: AcpiEvInitGlobalLockHandler
65 * PARAMETERS: None
67 * RETURN: Status
69 * DESCRIPTION: Install a handler for the global lock release event
71 ******************************************************************************/
73 ACPI_STATUS
74 AcpiEvInitGlobalLockHandler (
75 void)
77 ACPI_STATUS Status;
80 ACPI_FUNCTION_TRACE (EvInitGlobalLockHandler);
83 /* If Hardware Reduced flag is set, there is no global lock */
85 if (AcpiGbl_ReducedHardware)
87 return_ACPI_STATUS (AE_OK);
90 /* Attempt installation of the global lock handler */
92 Status = AcpiInstallFixedEventHandler (ACPI_EVENT_GLOBAL,
93 AcpiEvGlobalLockHandler, NULL);
96 * If the global lock does not exist on this platform, the attempt to
97 * enable GBL_STATUS will fail (the GBL_ENABLE bit will not stick).
98 * Map to AE_OK, but mark global lock as not present. Any attempt to
99 * actually use the global lock will be flagged with an error.
101 AcpiGbl_GlobalLockPresent = FALSE;
102 if (Status == AE_NO_HARDWARE_RESPONSE)
104 ACPI_ERROR ((AE_INFO,
105 "No response from Global Lock hardware, disabling lock"));
107 return_ACPI_STATUS (AE_OK);
110 Status = AcpiOsCreateLock (&AcpiGbl_GlobalLockPendingLock);
111 if (ACPI_FAILURE (Status))
113 return_ACPI_STATUS (Status);
116 AcpiGbl_GlobalLockPending = FALSE;
117 AcpiGbl_GlobalLockPresent = TRUE;
118 return_ACPI_STATUS (Status);
122 /*******************************************************************************
124 * FUNCTION: AcpiEvRemoveGlobalLockHandler
126 * PARAMETERS: None
128 * RETURN: Status
130 * DESCRIPTION: Remove the handler for the Global Lock
132 ******************************************************************************/
134 ACPI_STATUS
135 AcpiEvRemoveGlobalLockHandler (
136 void)
138 ACPI_STATUS Status;
141 ACPI_FUNCTION_TRACE (EvRemoveGlobalLockHandler);
144 AcpiGbl_GlobalLockPresent = FALSE;
145 Status = AcpiRemoveFixedEventHandler (ACPI_EVENT_GLOBAL,
146 AcpiEvGlobalLockHandler);
148 AcpiOsDeleteLock (AcpiGbl_GlobalLockPendingLock);
149 return_ACPI_STATUS (Status);
153 /*******************************************************************************
155 * FUNCTION: AcpiEvGlobalLockHandler
157 * PARAMETERS: Context - From thread interface, not used
159 * RETURN: ACPI_INTERRUPT_HANDLED
161 * DESCRIPTION: Invoked directly from the SCI handler when a global lock
162 * release interrupt occurs. If there is actually a pending
163 * request for the lock, signal the waiting thread.
165 ******************************************************************************/
167 static UINT32
168 AcpiEvGlobalLockHandler (
169 void *Context)
171 ACPI_STATUS Status;
172 ACPI_CPU_FLAGS Flags;
175 Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
178 * If a request for the global lock is not actually pending,
179 * we are done. This handles "spurious" global lock interrupts
180 * which are possible (and have been seen) with bad BIOSs.
182 if (!AcpiGbl_GlobalLockPending)
184 goto CleanupAndExit;
188 * Send a unit to the global lock semaphore. The actual acquisition
189 * of the global lock will be performed by the waiting thread.
191 Status = AcpiOsSignalSemaphore (AcpiGbl_GlobalLockSemaphore, 1);
192 if (ACPI_FAILURE (Status))
194 ACPI_ERROR ((AE_INFO, "Could not signal Global Lock semaphore"));
197 AcpiGbl_GlobalLockPending = FALSE;
200 CleanupAndExit:
202 AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
203 return (ACPI_INTERRUPT_HANDLED);
207 /******************************************************************************
209 * FUNCTION: AcpiEvAcquireGlobalLock
211 * PARAMETERS: Timeout - Max time to wait for the lock, in millisec.
213 * RETURN: Status
215 * DESCRIPTION: Attempt to gain ownership of the Global Lock.
217 * MUTEX: Interpreter must be locked
219 * Note: The original implementation allowed multiple threads to "acquire" the
220 * Global Lock, and the OS would hold the lock until the last thread had
221 * released it. However, this could potentially starve the BIOS out of the
222 * lock, especially in the case where there is a tight handshake between the
223 * Embedded Controller driver and the BIOS. Therefore, this implementation
224 * allows only one thread to acquire the HW Global Lock at a time, and makes
225 * the global lock appear as a standard mutex on the OS side.
227 *****************************************************************************/
229 ACPI_STATUS
230 AcpiEvAcquireGlobalLock (
231 UINT16 Timeout)
233 ACPI_CPU_FLAGS Flags;
234 ACPI_STATUS Status;
235 BOOLEAN Acquired = FALSE;
238 ACPI_FUNCTION_TRACE (EvAcquireGlobalLock);
242 * Only one thread can acquire the GL at a time, the GlobalLockMutex
243 * enforces this. This interface releases the interpreter if we must wait.
245 Status = AcpiExSystemWaitMutex (AcpiGbl_GlobalLockMutex->Mutex.OsMutex,
246 Timeout);
247 if (ACPI_FAILURE (Status))
249 return_ACPI_STATUS (Status);
253 * Update the global lock handle and check for wraparound. The handle is
254 * only used for the external global lock interfaces, but it is updated
255 * here to properly handle the case where a single thread may acquire the
256 * lock via both the AML and the AcpiAcquireGlobalLock interfaces. The
257 * handle is therefore updated on the first acquire from a given thread
258 * regardless of where the acquisition request originated.
260 AcpiGbl_GlobalLockHandle++;
261 if (AcpiGbl_GlobalLockHandle == 0)
263 AcpiGbl_GlobalLockHandle = 1;
267 * Make sure that a global lock actually exists. If not, just
268 * treat the lock as a standard mutex.
270 if (!AcpiGbl_GlobalLockPresent)
272 AcpiGbl_GlobalLockAcquired = TRUE;
273 return_ACPI_STATUS (AE_OK);
276 Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
280 /* Attempt to acquire the actual hardware lock */
282 ACPI_ACQUIRE_GLOBAL_LOCK (AcpiGbl_FACS, Acquired);
283 if (Acquired)
285 AcpiGbl_GlobalLockAcquired = TRUE;
286 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
287 "Acquired hardware Global Lock\n"));
288 break;
292 * Did not get the lock. The pending bit was set above, and
293 * we must now wait until we receive the global lock
294 * released interrupt.
296 AcpiGbl_GlobalLockPending = TRUE;
297 AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
299 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
300 "Waiting for hardware Global Lock\n"));
303 * Wait for handshake with the global lock interrupt handler.
304 * This interface releases the interpreter if we must wait.
306 Status = AcpiExSystemWaitSemaphore (
307 AcpiGbl_GlobalLockSemaphore, ACPI_WAIT_FOREVER);
309 Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock);
311 } while (ACPI_SUCCESS (Status));
313 AcpiGbl_GlobalLockPending = FALSE;
314 AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags);
316 return_ACPI_STATUS (Status);
320 /*******************************************************************************
322 * FUNCTION: AcpiEvReleaseGlobalLock
324 * PARAMETERS: None
326 * RETURN: Status
328 * DESCRIPTION: Releases ownership of the Global Lock.
330 ******************************************************************************/
332 ACPI_STATUS
333 AcpiEvReleaseGlobalLock (
334 void)
336 BOOLEAN Pending = FALSE;
337 ACPI_STATUS Status = AE_OK;
340 ACPI_FUNCTION_TRACE (EvReleaseGlobalLock);
343 /* Lock must be already acquired */
345 if (!AcpiGbl_GlobalLockAcquired)
347 ACPI_WARNING ((AE_INFO,
348 "Cannot release the ACPI Global Lock, it has not been acquired"));
349 return_ACPI_STATUS (AE_NOT_ACQUIRED);
352 if (AcpiGbl_GlobalLockPresent)
354 /* Allow any thread to release the lock */
356 ACPI_RELEASE_GLOBAL_LOCK (AcpiGbl_FACS, Pending);
359 * If the pending bit was set, we must write GBL_RLS to the control
360 * register
362 if (Pending)
364 Status = AcpiWriteBitRegister (
365 ACPI_BITREG_GLOBAL_LOCK_RELEASE, ACPI_ENABLE_EVENT);
368 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Released hardware Global Lock\n"));
371 AcpiGbl_GlobalLockAcquired = FALSE;
373 /* Release the local GL mutex */
375 AcpiOsReleaseMutex (AcpiGbl_GlobalLockMutex->Mutex.OsMutex);
376 return_ACPI_STATUS (Status);
379 #endif /* !ACPI_REDUCED_HARDWARE */