evglock.c revision 245582
1220604Sjkim/****************************************************************************** 2220604Sjkim * 3220604Sjkim * Module Name: evglock - Global Lock support 4220604Sjkim * 5220604Sjkim *****************************************************************************/ 6220604Sjkim 7220604Sjkim/* 8245582Sjkim * Copyright (C) 2000 - 2013, Intel Corp. 9220604Sjkim * All rights reserved. 10220604Sjkim * 11220604Sjkim * Redistribution and use in source and binary forms, with or without 12220604Sjkim * modification, are permitted provided that the following conditions 13220604Sjkim * are met: 14220604Sjkim * 1. Redistributions of source code must retain the above copyright 15220604Sjkim * notice, this list of conditions, and the following disclaimer, 16220604Sjkim * without modification. 17220604Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer 18220604Sjkim * substantially similar to the "NO WARRANTY" disclaimer below 19220604Sjkim * ("Disclaimer") and any redistribution must be conditioned upon 20220604Sjkim * including a substantially similar Disclaimer requirement for further 21220604Sjkim * binary redistribution. 22220604Sjkim * 3. Neither the names of the above-listed copyright holders nor the names 23220604Sjkim * of any contributors may be used to endorse or promote products derived 24220604Sjkim * from this software without specific prior written permission. 25220604Sjkim * 26220604Sjkim * Alternatively, this software may be distributed under the terms of the 27220604Sjkim * GNU General Public License ("GPL") version 2 as published by the Free 28220604Sjkim * Software Foundation. 29220604Sjkim * 30220604Sjkim * NO WARRANTY 31220604Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32220604Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33220604Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 34220604Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35220604Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36220604Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37220604Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38220604Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39220604Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 40220604Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41220604Sjkim * POSSIBILITY OF SUCH DAMAGES. 42220604Sjkim */ 43220604Sjkim 44220663Sjkim#include <contrib/dev/acpica/include/acpi.h> 45220663Sjkim#include <contrib/dev/acpica/include/accommon.h> 46220663Sjkim#include <contrib/dev/acpica/include/acevents.h> 47220663Sjkim#include <contrib/dev/acpica/include/acinterp.h> 48220604Sjkim 49220604Sjkim#define _COMPONENT ACPI_EVENTS 50220604Sjkim ACPI_MODULE_NAME ("evglock") 51220604Sjkim 52231844Sjkim#if (!ACPI_REDUCED_HARDWARE) /* Entire module */ 53220604Sjkim 54220604Sjkim/* Local prototypes */ 55220604Sjkim 56220604Sjkimstatic UINT32 57220604SjkimAcpiEvGlobalLockHandler ( 58220604Sjkim void *Context); 59220604Sjkim 60220604Sjkim 61220604Sjkim/******************************************************************************* 62220604Sjkim * 63220604Sjkim * FUNCTION: AcpiEvInitGlobalLockHandler 64220604Sjkim * 65220604Sjkim * PARAMETERS: None 66220604Sjkim * 67220604Sjkim * RETURN: Status 68220604Sjkim * 69220604Sjkim * DESCRIPTION: Install a handler for the global lock release event 70220604Sjkim * 71220604Sjkim ******************************************************************************/ 72220604Sjkim 73220604SjkimACPI_STATUS 74220604SjkimAcpiEvInitGlobalLockHandler ( 75220604Sjkim void) 76220604Sjkim{ 77220604Sjkim ACPI_STATUS Status; 78220604Sjkim 79220604Sjkim 80220604Sjkim ACPI_FUNCTION_TRACE (EvInitGlobalLockHandler); 81220604Sjkim 82220604Sjkim 83228110Sjkim /* If Hardware Reduced flag is set, there is no global lock */ 84228110Sjkim 85228110Sjkim if (AcpiGbl_ReducedHardware) 86228110Sjkim { 87228110Sjkim return_ACPI_STATUS (AE_OK); 88228110Sjkim } 89228110Sjkim 90220604Sjkim /* Attempt installation of the global lock handler */ 91220604Sjkim 92220604Sjkim Status = AcpiInstallFixedEventHandler (ACPI_EVENT_GLOBAL, 93220604Sjkim AcpiEvGlobalLockHandler, NULL); 94220604Sjkim 95220604Sjkim /* 96220604Sjkim * If the global lock does not exist on this platform, the attempt to 97220604Sjkim * enable GBL_STATUS will fail (the GBL_ENABLE bit will not stick). 98220604Sjkim * Map to AE_OK, but mark global lock as not present. Any attempt to 99220604Sjkim * actually use the global lock will be flagged with an error. 100220604Sjkim */ 101220604Sjkim AcpiGbl_GlobalLockPresent = FALSE; 102220604Sjkim if (Status == AE_NO_HARDWARE_RESPONSE) 103220604Sjkim { 104220604Sjkim ACPI_ERROR ((AE_INFO, 105220604Sjkim "No response from Global Lock hardware, disabling lock")); 106220604Sjkim 107220604Sjkim return_ACPI_STATUS (AE_OK); 108220604Sjkim } 109220604Sjkim 110220604Sjkim Status = AcpiOsCreateLock (&AcpiGbl_GlobalLockPendingLock); 111220604Sjkim if (ACPI_FAILURE (Status)) 112220604Sjkim { 113220604Sjkim return_ACPI_STATUS (Status); 114220604Sjkim } 115220604Sjkim 116220604Sjkim AcpiGbl_GlobalLockPending = FALSE; 117220604Sjkim AcpiGbl_GlobalLockPresent = TRUE; 118220604Sjkim return_ACPI_STATUS (Status); 119220604Sjkim} 120220604Sjkim 121220604Sjkim 122220604Sjkim/******************************************************************************* 123220604Sjkim * 124220604Sjkim * FUNCTION: AcpiEvRemoveGlobalLockHandler 125220604Sjkim * 126220604Sjkim * PARAMETERS: None 127220604Sjkim * 128220604Sjkim * RETURN: Status 129220604Sjkim * 130220604Sjkim * DESCRIPTION: Remove the handler for the Global Lock 131220604Sjkim * 132220604Sjkim ******************************************************************************/ 133220604Sjkim 134220604SjkimACPI_STATUS 135220604SjkimAcpiEvRemoveGlobalLockHandler ( 136220604Sjkim void) 137220604Sjkim{ 138220604Sjkim ACPI_STATUS Status; 139220604Sjkim 140220604Sjkim 141220604Sjkim ACPI_FUNCTION_TRACE (EvRemoveGlobalLockHandler); 142220604Sjkim 143220604Sjkim AcpiGbl_GlobalLockPresent = FALSE; 144220604Sjkim Status = AcpiRemoveFixedEventHandler (ACPI_EVENT_GLOBAL, 145220604Sjkim AcpiEvGlobalLockHandler); 146220604Sjkim 147220604Sjkim return_ACPI_STATUS (Status); 148220604Sjkim} 149220604Sjkim 150220604Sjkim 151220604Sjkim/******************************************************************************* 152220604Sjkim * 153220604Sjkim * FUNCTION: AcpiEvGlobalLockHandler 154220604Sjkim * 155220604Sjkim * PARAMETERS: Context - From thread interface, not used 156220604Sjkim * 157220604Sjkim * RETURN: ACPI_INTERRUPT_HANDLED 158220604Sjkim * 159220604Sjkim * DESCRIPTION: Invoked directly from the SCI handler when a global lock 160220604Sjkim * release interrupt occurs. If there is actually a pending 161220604Sjkim * request for the lock, signal the waiting thread. 162220604Sjkim * 163220604Sjkim ******************************************************************************/ 164220604Sjkim 165220604Sjkimstatic UINT32 166220604SjkimAcpiEvGlobalLockHandler ( 167220604Sjkim void *Context) 168220604Sjkim{ 169220604Sjkim ACPI_STATUS Status; 170220604Sjkim ACPI_CPU_FLAGS Flags; 171220604Sjkim 172220604Sjkim 173220604Sjkim Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock); 174220604Sjkim 175220604Sjkim /* 176220604Sjkim * If a request for the global lock is not actually pending, 177220604Sjkim * we are done. This handles "spurious" global lock interrupts 178220604Sjkim * which are possible (and have been seen) with bad BIOSs. 179220604Sjkim */ 180220604Sjkim if (!AcpiGbl_GlobalLockPending) 181220604Sjkim { 182220604Sjkim goto CleanupAndExit; 183220604Sjkim } 184220604Sjkim 185220604Sjkim /* 186220604Sjkim * Send a unit to the global lock semaphore. The actual acquisition 187220604Sjkim * of the global lock will be performed by the waiting thread. 188220604Sjkim */ 189220604Sjkim Status = AcpiOsSignalSemaphore (AcpiGbl_GlobalLockSemaphore, 1); 190220604Sjkim if (ACPI_FAILURE (Status)) 191220604Sjkim { 192220604Sjkim ACPI_ERROR ((AE_INFO, "Could not signal Global Lock semaphore")); 193220604Sjkim } 194220604Sjkim 195220604Sjkim AcpiGbl_GlobalLockPending = FALSE; 196220604Sjkim 197220604Sjkim 198220604SjkimCleanupAndExit: 199220604Sjkim 200220604Sjkim AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags); 201220604Sjkim return (ACPI_INTERRUPT_HANDLED); 202220604Sjkim} 203220604Sjkim 204220604Sjkim 205220604Sjkim/****************************************************************************** 206220604Sjkim * 207220604Sjkim * FUNCTION: AcpiEvAcquireGlobalLock 208220604Sjkim * 209220604Sjkim * PARAMETERS: Timeout - Max time to wait for the lock, in millisec. 210220604Sjkim * 211220604Sjkim * RETURN: Status 212220604Sjkim * 213220604Sjkim * DESCRIPTION: Attempt to gain ownership of the Global Lock. 214220604Sjkim * 215220604Sjkim * MUTEX: Interpreter must be locked 216220604Sjkim * 217220604Sjkim * Note: The original implementation allowed multiple threads to "acquire" the 218220604Sjkim * Global Lock, and the OS would hold the lock until the last thread had 219220604Sjkim * released it. However, this could potentially starve the BIOS out of the 220220604Sjkim * lock, especially in the case where there is a tight handshake between the 221220604Sjkim * Embedded Controller driver and the BIOS. Therefore, this implementation 222220604Sjkim * allows only one thread to acquire the HW Global Lock at a time, and makes 223220604Sjkim * the global lock appear as a standard mutex on the OS side. 224220604Sjkim * 225220604Sjkim *****************************************************************************/ 226220604Sjkim 227220604SjkimACPI_STATUS 228220604SjkimAcpiEvAcquireGlobalLock ( 229220604Sjkim UINT16 Timeout) 230220604Sjkim{ 231220604Sjkim ACPI_CPU_FLAGS Flags; 232220604Sjkim ACPI_STATUS Status; 233220604Sjkim BOOLEAN Acquired = FALSE; 234220604Sjkim 235220604Sjkim 236220604Sjkim ACPI_FUNCTION_TRACE (EvAcquireGlobalLock); 237220604Sjkim 238220604Sjkim 239220604Sjkim /* 240220604Sjkim * Only one thread can acquire the GL at a time, the GlobalLockMutex 241220604Sjkim * enforces this. This interface releases the interpreter if we must wait. 242220604Sjkim */ 243220604Sjkim Status = AcpiExSystemWaitMutex (AcpiGbl_GlobalLockMutex->Mutex.OsMutex, 244220604Sjkim Timeout); 245220604Sjkim if (ACPI_FAILURE (Status)) 246220604Sjkim { 247220604Sjkim return_ACPI_STATUS (Status); 248220604Sjkim } 249220604Sjkim 250220604Sjkim /* 251220604Sjkim * Update the global lock handle and check for wraparound. The handle is 252220604Sjkim * only used for the external global lock interfaces, but it is updated 253220604Sjkim * here to properly handle the case where a single thread may acquire the 254220604Sjkim * lock via both the AML and the AcpiAcquireGlobalLock interfaces. The 255220604Sjkim * handle is therefore updated on the first acquire from a given thread 256220604Sjkim * regardless of where the acquisition request originated. 257220604Sjkim */ 258220604Sjkim AcpiGbl_GlobalLockHandle++; 259220604Sjkim if (AcpiGbl_GlobalLockHandle == 0) 260220604Sjkim { 261220604Sjkim AcpiGbl_GlobalLockHandle = 1; 262220604Sjkim } 263220604Sjkim 264220604Sjkim /* 265220604Sjkim * Make sure that a global lock actually exists. If not, just 266220604Sjkim * treat the lock as a standard mutex. 267220604Sjkim */ 268220604Sjkim if (!AcpiGbl_GlobalLockPresent) 269220604Sjkim { 270220604Sjkim AcpiGbl_GlobalLockAcquired = TRUE; 271220604Sjkim return_ACPI_STATUS (AE_OK); 272220604Sjkim } 273220604Sjkim 274220604Sjkim Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock); 275220604Sjkim 276220604Sjkim do 277220604Sjkim { 278220604Sjkim /* Attempt to acquire the actual hardware lock */ 279220604Sjkim 280220604Sjkim ACPI_ACQUIRE_GLOBAL_LOCK (AcpiGbl_FACS, Acquired); 281220604Sjkim if (Acquired) 282220604Sjkim { 283220604Sjkim AcpiGbl_GlobalLockAcquired = TRUE; 284220604Sjkim ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, 285220604Sjkim "Acquired hardware Global Lock\n")); 286220604Sjkim break; 287220604Sjkim } 288220604Sjkim 289220604Sjkim /* 290220604Sjkim * Did not get the lock. The pending bit was set above, and 291220604Sjkim * we must now wait until we receive the global lock 292220604Sjkim * released interrupt. 293220604Sjkim */ 294220604Sjkim AcpiGbl_GlobalLockPending = TRUE; 295220604Sjkim AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags); 296220604Sjkim 297220604Sjkim ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, 298220604Sjkim "Waiting for hardware Global Lock\n")); 299220604Sjkim 300220604Sjkim /* 301220604Sjkim * Wait for handshake with the global lock interrupt handler. 302220604Sjkim * This interface releases the interpreter if we must wait. 303220604Sjkim */ 304220604Sjkim Status = AcpiExSystemWaitSemaphore (AcpiGbl_GlobalLockSemaphore, 305220604Sjkim ACPI_WAIT_FOREVER); 306220604Sjkim 307220604Sjkim Flags = AcpiOsAcquireLock (AcpiGbl_GlobalLockPendingLock); 308220604Sjkim 309220604Sjkim } while (ACPI_SUCCESS (Status)); 310220604Sjkim 311220604Sjkim AcpiGbl_GlobalLockPending = FALSE; 312220604Sjkim AcpiOsReleaseLock (AcpiGbl_GlobalLockPendingLock, Flags); 313220604Sjkim 314220604Sjkim return_ACPI_STATUS (Status); 315220604Sjkim} 316220604Sjkim 317220604Sjkim 318220604Sjkim/******************************************************************************* 319220604Sjkim * 320220604Sjkim * FUNCTION: AcpiEvReleaseGlobalLock 321220604Sjkim * 322220604Sjkim * PARAMETERS: None 323220604Sjkim * 324220604Sjkim * RETURN: Status 325220604Sjkim * 326220604Sjkim * DESCRIPTION: Releases ownership of the Global Lock. 327220604Sjkim * 328220604Sjkim ******************************************************************************/ 329220604Sjkim 330220604SjkimACPI_STATUS 331220604SjkimAcpiEvReleaseGlobalLock ( 332220604Sjkim void) 333220604Sjkim{ 334220604Sjkim BOOLEAN Pending = FALSE; 335220604Sjkim ACPI_STATUS Status = AE_OK; 336220604Sjkim 337220604Sjkim 338220604Sjkim ACPI_FUNCTION_TRACE (EvReleaseGlobalLock); 339220604Sjkim 340220604Sjkim 341220604Sjkim /* Lock must be already acquired */ 342220604Sjkim 343220604Sjkim if (!AcpiGbl_GlobalLockAcquired) 344220604Sjkim { 345220604Sjkim ACPI_WARNING ((AE_INFO, 346220604Sjkim "Cannot release the ACPI Global Lock, it has not been acquired")); 347220604Sjkim return_ACPI_STATUS (AE_NOT_ACQUIRED); 348220604Sjkim } 349220604Sjkim 350220604Sjkim if (AcpiGbl_GlobalLockPresent) 351220604Sjkim { 352220604Sjkim /* Allow any thread to release the lock */ 353220604Sjkim 354220604Sjkim ACPI_RELEASE_GLOBAL_LOCK (AcpiGbl_FACS, Pending); 355220604Sjkim 356220604Sjkim /* 357220604Sjkim * If the pending bit was set, we must write GBL_RLS to the control 358220604Sjkim * register 359220604Sjkim */ 360220604Sjkim if (Pending) 361220604Sjkim { 362220604Sjkim Status = AcpiWriteBitRegister ( 363220604Sjkim ACPI_BITREG_GLOBAL_LOCK_RELEASE, ACPI_ENABLE_EVENT); 364220604Sjkim } 365220604Sjkim 366220604Sjkim ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Released hardware Global Lock\n")); 367220604Sjkim } 368220604Sjkim 369220604Sjkim AcpiGbl_GlobalLockAcquired = FALSE; 370220604Sjkim 371220604Sjkim /* Release the local GL mutex */ 372220604Sjkim 373220604Sjkim AcpiOsReleaseMutex (AcpiGbl_GlobalLockMutex->Mutex.OsMutex); 374220604Sjkim return_ACPI_STATUS (Status); 375220604Sjkim} 376231844Sjkim 377231844Sjkim#endif /* !ACPI_REDUCED_HARDWARE */ 378