/******************************************************************************1*2* Module Name: evglock - Global Lock support3*4*****************************************************************************/56/*7* Copyright (C) 2000 - 2011, Intel Corp.8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions, and the following disclaimer,15* without modification.16* 2. Redistributions in binary form must reproduce at minimum a disclaimer17* substantially similar to the "NO WARRANTY" disclaimer below18* ("Disclaimer") and any redistribution must be conditioned upon19* including a substantially similar Disclaimer requirement for further20* binary redistribution.21* 3. Neither the names of the above-listed copyright holders nor the names22* of any contributors may be used to endorse or promote products derived23* from this software without specific prior written permission.24*25* Alternatively, this software may be distributed under the terms of the26* GNU General Public License ("GPL") version 2 as published by the Free27* Software Foundation.28*29* NO WARRANTY30* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS31* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT32* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR33* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT34* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL35* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS36* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)37* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,38* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING39* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE40* POSSIBILITY OF SUCH DAMAGES.41*/4243#include <acpi/acpi.h>44#include "accommon.h"45#include "acevents.h"46#include "acinterp.h"4748#define _COMPONENT ACPI_EVENTS49ACPI_MODULE_NAME("evglock")5051/* Local prototypes */52static u32 acpi_ev_global_lock_handler(void *context);5354/*******************************************************************************55*56* FUNCTION: acpi_ev_init_global_lock_handler57*58* PARAMETERS: None59*60* RETURN: Status61*62* DESCRIPTION: Install a handler for the global lock release event63*64******************************************************************************/6566acpi_status acpi_ev_init_global_lock_handler(void)67{68acpi_status status;6970ACPI_FUNCTION_TRACE(ev_init_global_lock_handler);7172/* Attempt installation of the global lock handler */7374status = acpi_install_fixed_event_handler(ACPI_EVENT_GLOBAL,75acpi_ev_global_lock_handler,76NULL);7778/*79* If the global lock does not exist on this platform, the attempt to80* enable GBL_STATUS will fail (the GBL_ENABLE bit will not stick).81* Map to AE_OK, but mark global lock as not present. Any attempt to82* actually use the global lock will be flagged with an error.83*/84acpi_gbl_global_lock_present = FALSE;85if (status == AE_NO_HARDWARE_RESPONSE) {86ACPI_ERROR((AE_INFO,87"No response from Global Lock hardware, disabling lock"));8889return_ACPI_STATUS(AE_OK);90}9192status = acpi_os_create_lock(&acpi_gbl_global_lock_pending_lock);93if (ACPI_FAILURE(status)) {94return_ACPI_STATUS(status);95}9697acpi_gbl_global_lock_pending = FALSE;98acpi_gbl_global_lock_present = TRUE;99return_ACPI_STATUS(status);100}101102/*******************************************************************************103*104* FUNCTION: acpi_ev_remove_global_lock_handler105*106* PARAMETERS: None107*108* RETURN: Status109*110* DESCRIPTION: Remove the handler for the Global Lock111*112******************************************************************************/113114acpi_status acpi_ev_remove_global_lock_handler(void)115{116acpi_status status;117118ACPI_FUNCTION_TRACE(ev_remove_global_lock_handler);119120acpi_gbl_global_lock_present = FALSE;121status = acpi_remove_fixed_event_handler(ACPI_EVENT_GLOBAL,122acpi_ev_global_lock_handler);123124return_ACPI_STATUS(status);125}126127/*******************************************************************************128*129* FUNCTION: acpi_ev_global_lock_handler130*131* PARAMETERS: Context - From thread interface, not used132*133* RETURN: ACPI_INTERRUPT_HANDLED134*135* DESCRIPTION: Invoked directly from the SCI handler when a global lock136* release interrupt occurs. If there is actually a pending137* request for the lock, signal the waiting thread.138*139******************************************************************************/140141static u32 acpi_ev_global_lock_handler(void *context)142{143acpi_status status;144acpi_cpu_flags flags;145146flags = acpi_os_acquire_lock(acpi_gbl_global_lock_pending_lock);147148/*149* If a request for the global lock is not actually pending,150* we are done. This handles "spurious" global lock interrupts151* which are possible (and have been seen) with bad BIOSs.152*/153if (!acpi_gbl_global_lock_pending) {154goto cleanup_and_exit;155}156157/*158* Send a unit to the global lock semaphore. The actual acquisition159* of the global lock will be performed by the waiting thread.160*/161status = acpi_os_signal_semaphore(acpi_gbl_global_lock_semaphore, 1);162if (ACPI_FAILURE(status)) {163ACPI_ERROR((AE_INFO, "Could not signal Global Lock semaphore"));164}165166acpi_gbl_global_lock_pending = FALSE;167168cleanup_and_exit:169170acpi_os_release_lock(acpi_gbl_global_lock_pending_lock, flags);171return (ACPI_INTERRUPT_HANDLED);172}173174/******************************************************************************175*176* FUNCTION: acpi_ev_acquire_global_lock177*178* PARAMETERS: Timeout - Max time to wait for the lock, in millisec.179*180* RETURN: Status181*182* DESCRIPTION: Attempt to gain ownership of the Global Lock.183*184* MUTEX: Interpreter must be locked185*186* Note: The original implementation allowed multiple threads to "acquire" the187* Global Lock, and the OS would hold the lock until the last thread had188* released it. However, this could potentially starve the BIOS out of the189* lock, especially in the case where there is a tight handshake between the190* Embedded Controller driver and the BIOS. Therefore, this implementation191* allows only one thread to acquire the HW Global Lock at a time, and makes192* the global lock appear as a standard mutex on the OS side.193*194*****************************************************************************/195196acpi_status acpi_ev_acquire_global_lock(u16 timeout)197{198acpi_cpu_flags flags;199acpi_status status;200u8 acquired = FALSE;201202ACPI_FUNCTION_TRACE(ev_acquire_global_lock);203204/*205* Only one thread can acquire the GL at a time, the global_lock_mutex206* enforces this. This interface releases the interpreter if we must wait.207*/208status =209acpi_ex_system_wait_mutex(acpi_gbl_global_lock_mutex->mutex.210os_mutex, timeout);211if (ACPI_FAILURE(status)) {212return_ACPI_STATUS(status);213}214215/*216* Update the global lock handle and check for wraparound. The handle is217* only used for the external global lock interfaces, but it is updated218* here to properly handle the case where a single thread may acquire the219* lock via both the AML and the acpi_acquire_global_lock interfaces. The220* handle is therefore updated on the first acquire from a given thread221* regardless of where the acquisition request originated.222*/223acpi_gbl_global_lock_handle++;224if (acpi_gbl_global_lock_handle == 0) {225acpi_gbl_global_lock_handle = 1;226}227228/*229* Make sure that a global lock actually exists. If not, just230* treat the lock as a standard mutex.231*/232if (!acpi_gbl_global_lock_present) {233acpi_gbl_global_lock_acquired = TRUE;234return_ACPI_STATUS(AE_OK);235}236237flags = acpi_os_acquire_lock(acpi_gbl_global_lock_pending_lock);238239do {240241/* Attempt to acquire the actual hardware lock */242243ACPI_ACQUIRE_GLOBAL_LOCK(acpi_gbl_FACS, acquired);244if (acquired) {245acpi_gbl_global_lock_acquired = TRUE;246ACPI_DEBUG_PRINT((ACPI_DB_EXEC,247"Acquired hardware Global Lock\n"));248break;249}250251/*252* Did not get the lock. The pending bit was set above, and253* we must now wait until we receive the global lock254* released interrupt.255*/256acpi_gbl_global_lock_pending = TRUE;257acpi_os_release_lock(acpi_gbl_global_lock_pending_lock, flags);258259ACPI_DEBUG_PRINT((ACPI_DB_EXEC,260"Waiting for hardware Global Lock\n"));261262/*263* Wait for handshake with the global lock interrupt handler.264* This interface releases the interpreter if we must wait.265*/266status =267acpi_ex_system_wait_semaphore268(acpi_gbl_global_lock_semaphore, ACPI_WAIT_FOREVER);269270flags = acpi_os_acquire_lock(acpi_gbl_global_lock_pending_lock);271272} while (ACPI_SUCCESS(status));273274acpi_gbl_global_lock_pending = FALSE;275acpi_os_release_lock(acpi_gbl_global_lock_pending_lock, flags);276277return_ACPI_STATUS(status);278}279280/*******************************************************************************281*282* FUNCTION: acpi_ev_release_global_lock283*284* PARAMETERS: None285*286* RETURN: Status287*288* DESCRIPTION: Releases ownership of the Global Lock.289*290******************************************************************************/291292acpi_status acpi_ev_release_global_lock(void)293{294u8 pending = FALSE;295acpi_status status = AE_OK;296297ACPI_FUNCTION_TRACE(ev_release_global_lock);298299/* Lock must be already acquired */300301if (!acpi_gbl_global_lock_acquired) {302ACPI_WARNING((AE_INFO,303"Cannot release the ACPI Global Lock, it has not been acquired"));304return_ACPI_STATUS(AE_NOT_ACQUIRED);305}306307if (acpi_gbl_global_lock_present) {308309/* Allow any thread to release the lock */310311ACPI_RELEASE_GLOBAL_LOCK(acpi_gbl_FACS, pending);312313/*314* If the pending bit was set, we must write GBL_RLS to the control315* register316*/317if (pending) {318status =319acpi_write_bit_register320(ACPI_BITREG_GLOBAL_LOCK_RELEASE,321ACPI_ENABLE_EVENT);322}323324ACPI_DEBUG_PRINT((ACPI_DB_EXEC,325"Released hardware Global Lock\n"));326}327328acpi_gbl_global_lock_acquired = FALSE;329330/* Release the local GL mutex */331332acpi_os_release_mutex(acpi_gbl_global_lock_mutex->mutex.os_mutex);333return_ACPI_STATUS(status);334}335336337