Path: blob/main/sys/contrib/dev/acpica/components/events/evxface.c
48526 views
/******************************************************************************1*2* Module Name: evxface - External interfaces for ACPI events3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#define EXPORT_ACPI_INTERFACES152153#include <contrib/dev/acpica/include/acpi.h>154#include <contrib/dev/acpica/include/accommon.h>155#include <contrib/dev/acpica/include/acnamesp.h>156#include <contrib/dev/acpica/include/acevents.h>157#include <contrib/dev/acpica/include/acinterp.h>158159#define _COMPONENT ACPI_EVENTS160ACPI_MODULE_NAME ("evxface")161162#if (!ACPI_REDUCED_HARDWARE)163164/* Local prototypes */165166static ACPI_STATUS167AcpiEvInstallGpeHandler (168ACPI_HANDLE GpeDevice,169UINT32 GpeNumber,170UINT32 Type,171BOOLEAN IsRawHandler,172ACPI_GPE_HANDLER Address,173void *Context);174175#endif176177178/*******************************************************************************179*180* FUNCTION: AcpiInstallNotifyHandler181*182* PARAMETERS: Device - The device for which notifies will be handled183* HandlerType - The type of handler:184* ACPI_SYSTEM_NOTIFY: System Handler (00-7F)185* ACPI_DEVICE_NOTIFY: Device Handler (80-FF)186* ACPI_ALL_NOTIFY: Both System and Device187* Handler - Address of the handler188* Context - Value passed to the handler on each GPE189*190* RETURN: Status191*192* DESCRIPTION: Install a handler for notifications on an ACPI Device,193* ThermalZone, or Processor object.194*195* NOTES: The Root namespace object may have only one handler for each196* type of notify (System/Device). Device/Thermal/Processor objects197* may have one device notify handler, and multiple system notify198* handlers.199*200******************************************************************************/201202ACPI_STATUS203AcpiInstallNotifyHandler (204ACPI_HANDLE Device,205UINT32 HandlerType,206ACPI_NOTIFY_HANDLER Handler,207void *Context)208{209ACPI_NAMESPACE_NODE *Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Device);210ACPI_OPERAND_OBJECT *ObjDesc;211ACPI_OPERAND_OBJECT *HandlerObj;212ACPI_STATUS Status;213UINT32 i;214215216ACPI_FUNCTION_TRACE (AcpiInstallNotifyHandler);217218219/* Parameter validation */220221if ((!Device) || (!Handler) || (!HandlerType) ||222(HandlerType > ACPI_MAX_NOTIFY_HANDLER_TYPE))223{224return_ACPI_STATUS (AE_BAD_PARAMETER);225}226227Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);228if (ACPI_FAILURE (Status))229{230return_ACPI_STATUS (Status);231}232233/*234* Root Object:235* Registering a notify handler on the root object indicates that the236* caller wishes to receive notifications for all objects. Note that237* only one global handler can be registered per notify type.238* Ensure that a handler is not already installed.239*/240if (Device == ACPI_ROOT_OBJECT)241{242for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)243{244if (HandlerType & (i+1))245{246if (AcpiGbl_GlobalNotify[i].Handler)247{248Status = AE_ALREADY_EXISTS;249goto UnlockAndExit;250}251252AcpiGbl_GlobalNotify[i].Handler = Handler;253AcpiGbl_GlobalNotify[i].Context = Context;254}255}256257goto UnlockAndExit; /* Global notify handler installed, all done */258}259260/*261* All Other Objects:262* Caller will only receive notifications specific to the target263* object. Note that only certain object types are allowed to264* receive notifications.265*/266267/* Are Notifies allowed on this object? */268269if (!AcpiEvIsNotifyObject (Node))270{271Status = AE_TYPE;272goto UnlockAndExit;273}274275/* Check for an existing internal object, might not exist */276277ObjDesc = AcpiNsGetAttachedObject (Node);278if (!ObjDesc)279{280/* Create a new object */281282ObjDesc = AcpiUtCreateInternalObject (Node->Type);283if (!ObjDesc)284{285Status = AE_NO_MEMORY;286goto UnlockAndExit;287}288289/* Attach new object to the Node, remove local reference */290291Status = AcpiNsAttachObject (Device, ObjDesc, Node->Type);292AcpiUtRemoveReference (ObjDesc);293if (ACPI_FAILURE (Status))294{295goto UnlockAndExit;296}297}298299/* Ensure that the handler is not already installed in the lists */300301for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)302{303if (HandlerType & (i+1))304{305HandlerObj = ObjDesc->CommonNotify.NotifyList[i];306while (HandlerObj)307{308if (HandlerObj->Notify.Handler == Handler)309{310Status = AE_ALREADY_EXISTS;311goto UnlockAndExit;312}313314HandlerObj = HandlerObj->Notify.Next[i];315}316}317}318319/* Create and populate a new notify handler object */320321HandlerObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_NOTIFY);322if (!HandlerObj)323{324Status = AE_NO_MEMORY;325goto UnlockAndExit;326}327328HandlerObj->Notify.Node = Node;329HandlerObj->Notify.HandlerType = HandlerType;330HandlerObj->Notify.Handler = Handler;331HandlerObj->Notify.Context = Context;332333/* Install the handler at the list head(s) */334335for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)336{337if (HandlerType & (i+1))338{339HandlerObj->Notify.Next[i] =340ObjDesc->CommonNotify.NotifyList[i];341342ObjDesc->CommonNotify.NotifyList[i] = HandlerObj;343}344}345346/* Add an extra reference if handler was installed in both lists */347348if (HandlerType == ACPI_ALL_NOTIFY)349{350AcpiUtAddReference (HandlerObj);351}352353354UnlockAndExit:355(void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);356return_ACPI_STATUS (Status);357}358359ACPI_EXPORT_SYMBOL (AcpiInstallNotifyHandler)360361362/*******************************************************************************363*364* FUNCTION: AcpiRemoveNotifyHandler365*366* PARAMETERS: Device - The device for which the handler is installed367* HandlerType - The type of handler:368* ACPI_SYSTEM_NOTIFY: System Handler (00-7F)369* ACPI_DEVICE_NOTIFY: Device Handler (80-FF)370* ACPI_ALL_NOTIFY: Both System and Device371* Handler - Address of the handler372*373* RETURN: Status374*375* DESCRIPTION: Remove a handler for notifies on an ACPI device376*377******************************************************************************/378379ACPI_STATUS380AcpiRemoveNotifyHandler (381ACPI_HANDLE Device,382UINT32 HandlerType,383ACPI_NOTIFY_HANDLER Handler)384{385ACPI_NAMESPACE_NODE *Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Device);386ACPI_OPERAND_OBJECT *ObjDesc;387ACPI_OPERAND_OBJECT *HandlerObj;388ACPI_OPERAND_OBJECT *PreviousHandlerObj;389ACPI_STATUS Status = AE_OK;390UINT32 i;391392393ACPI_FUNCTION_TRACE (AcpiRemoveNotifyHandler);394395396/* Parameter validation */397398if ((!Device) || (!Handler) || (!HandlerType) ||399(HandlerType > ACPI_MAX_NOTIFY_HANDLER_TYPE))400{401return_ACPI_STATUS (AE_BAD_PARAMETER);402}403404/* Root Object. Global handlers are removed here */405406if (Device == ACPI_ROOT_OBJECT)407{408for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)409{410if (HandlerType & (i+1))411{412Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);413if (ACPI_FAILURE (Status))414{415return_ACPI_STATUS (Status);416}417418if (!AcpiGbl_GlobalNotify[i].Handler ||419(AcpiGbl_GlobalNotify[i].Handler != Handler))420{421Status = AE_NOT_EXIST;422goto UnlockAndExit;423}424425ACPI_DEBUG_PRINT ((ACPI_DB_INFO,426"Removing global notify handler\n"));427428AcpiGbl_GlobalNotify[i].Handler = NULL;429AcpiGbl_GlobalNotify[i].Context = NULL;430431(void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);432433/* Make sure all deferred notify tasks are completed */434435AcpiOsWaitEventsComplete ();436}437}438439return_ACPI_STATUS (AE_OK);440}441442/* All other objects: Are Notifies allowed on this object? */443444if (!AcpiEvIsNotifyObject (Node))445{446return_ACPI_STATUS (AE_TYPE);447}448449/* Must have an existing internal object */450451ObjDesc = AcpiNsGetAttachedObject (Node);452if (!ObjDesc)453{454return_ACPI_STATUS (AE_NOT_EXIST);455}456457/* Internal object exists. Find the handler and remove it */458459for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)460{461if (HandlerType & (i+1))462{463Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);464if (ACPI_FAILURE (Status))465{466return_ACPI_STATUS (Status);467}468469HandlerObj = ObjDesc->CommonNotify.NotifyList[i];470PreviousHandlerObj = NULL;471472/* Attempt to find the handler in the handler list */473474while (HandlerObj &&475(HandlerObj->Notify.Handler != Handler))476{477PreviousHandlerObj = HandlerObj;478HandlerObj = HandlerObj->Notify.Next[i];479}480481if (!HandlerObj)482{483Status = AE_NOT_EXIST;484goto UnlockAndExit;485}486487/* Remove the handler object from the list */488489if (PreviousHandlerObj) /* Handler is not at the list head */490{491PreviousHandlerObj->Notify.Next[i] =492HandlerObj->Notify.Next[i];493}494else /* Handler is at the list head */495{496ObjDesc->CommonNotify.NotifyList[i] =497HandlerObj->Notify.Next[i];498}499500(void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);501502/* Make sure all deferred notify tasks are completed */503504AcpiOsWaitEventsComplete ();505AcpiUtRemoveReference (HandlerObj);506}507}508509return_ACPI_STATUS (Status);510511512UnlockAndExit:513(void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);514return_ACPI_STATUS (Status);515}516517ACPI_EXPORT_SYMBOL (AcpiRemoveNotifyHandler)518519520/*******************************************************************************521*522* FUNCTION: AcpiInstallExceptionHandler523*524* PARAMETERS: Handler - Pointer to the handler function for the525* event526*527* RETURN: Status528*529* DESCRIPTION: Saves the pointer to the handler function530*531******************************************************************************/532533ACPI_STATUS534AcpiInstallExceptionHandler (535ACPI_EXCEPTION_HANDLER Handler)536{537ACPI_STATUS Status;538539540ACPI_FUNCTION_TRACE (AcpiInstallExceptionHandler);541542543Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);544if (ACPI_FAILURE (Status))545{546return_ACPI_STATUS (Status);547}548549/* Don't allow two handlers. */550551if (AcpiGbl_ExceptionHandler)552{553Status = AE_ALREADY_EXISTS;554goto Cleanup;555}556557/* Install the handler */558559AcpiGbl_ExceptionHandler = Handler;560561Cleanup:562(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);563return_ACPI_STATUS (Status);564}565566ACPI_EXPORT_SYMBOL (AcpiInstallExceptionHandler)567568569#if (!ACPI_REDUCED_HARDWARE)570/*******************************************************************************571*572* FUNCTION: AcpiInstallSciHandler573*574* PARAMETERS: Address - Address of the handler575* Context - Value passed to the handler on each SCI576*577* RETURN: Status578*579* DESCRIPTION: Install a handler for a System Control Interrupt.580*581******************************************************************************/582583ACPI_STATUS584AcpiInstallSciHandler (585ACPI_SCI_HANDLER Address,586void *Context)587{588ACPI_SCI_HANDLER_INFO *NewSciHandler;589ACPI_SCI_HANDLER_INFO *SciHandler;590ACPI_CPU_FLAGS Flags;591ACPI_STATUS Status;592593594ACPI_FUNCTION_TRACE (AcpiInstallSciHandler);595596597if (!Address)598{599return_ACPI_STATUS (AE_BAD_PARAMETER);600}601602/* Allocate and init a handler object */603604NewSciHandler = ACPI_ALLOCATE (sizeof (ACPI_SCI_HANDLER_INFO));605if (!NewSciHandler)606{607return_ACPI_STATUS (AE_NO_MEMORY);608}609610NewSciHandler->Address = Address;611NewSciHandler->Context = Context;612613Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);614if (ACPI_FAILURE (Status))615{616goto Exit;617}618619/* Lock list during installation */620621Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);622SciHandler = AcpiGbl_SciHandlerList;623624/* Ensure handler does not already exist */625626while (SciHandler)627{628if (Address == SciHandler->Address)629{630Status = AE_ALREADY_EXISTS;631goto UnlockAndExit;632}633634SciHandler = SciHandler->Next;635}636637/* Install the new handler into the global list (at head) */638639NewSciHandler->Next = AcpiGbl_SciHandlerList;640AcpiGbl_SciHandlerList = NewSciHandler;641642643UnlockAndExit:644645AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);646(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);647648Exit:649if (ACPI_FAILURE (Status))650{651ACPI_FREE (NewSciHandler);652}653return_ACPI_STATUS (Status);654}655656ACPI_EXPORT_SYMBOL (AcpiInstallSciHandler)657658659/*******************************************************************************660*661* FUNCTION: AcpiRemoveSciHandler662*663* PARAMETERS: Address - Address of the handler664*665* RETURN: Status666*667* DESCRIPTION: Remove a handler for a System Control Interrupt.668*669******************************************************************************/670671ACPI_STATUS672AcpiRemoveSciHandler (673ACPI_SCI_HANDLER Address)674{675ACPI_SCI_HANDLER_INFO *PrevSciHandler;676ACPI_SCI_HANDLER_INFO *NextSciHandler;677ACPI_CPU_FLAGS Flags;678ACPI_STATUS Status;679680681ACPI_FUNCTION_TRACE (AcpiRemoveSciHandler);682683684if (!Address)685{686return_ACPI_STATUS (AE_BAD_PARAMETER);687}688689Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);690if (ACPI_FAILURE (Status))691{692return_ACPI_STATUS (Status);693}694695/* Remove the SCI handler with lock */696697Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);698699PrevSciHandler = NULL;700NextSciHandler = AcpiGbl_SciHandlerList;701while (NextSciHandler)702{703if (NextSciHandler->Address == Address)704{705/* Unlink and free the SCI handler info block */706707if (PrevSciHandler)708{709PrevSciHandler->Next = NextSciHandler->Next;710}711else712{713AcpiGbl_SciHandlerList = NextSciHandler->Next;714}715716AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);717ACPI_FREE (NextSciHandler);718goto UnlockAndExit;719}720721PrevSciHandler = NextSciHandler;722NextSciHandler = NextSciHandler->Next;723}724725AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);726Status = AE_NOT_EXIST;727728729UnlockAndExit:730(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);731return_ACPI_STATUS (Status);732}733734ACPI_EXPORT_SYMBOL (AcpiRemoveSciHandler)735736737/*******************************************************************************738*739* FUNCTION: AcpiInstallGlobalEventHandler740*741* PARAMETERS: Handler - Pointer to the global event handler function742* Context - Value passed to the handler on each event743*744* RETURN: Status745*746* DESCRIPTION: Saves the pointer to the handler function. The global handler747* is invoked upon each incoming GPE and Fixed Event. It is748* invoked at interrupt level at the time of the event dispatch.749* Can be used to update event counters, etc.750*751******************************************************************************/752753ACPI_STATUS754AcpiInstallGlobalEventHandler (755ACPI_GBL_EVENT_HANDLER Handler,756void *Context)757{758ACPI_STATUS Status;759760761ACPI_FUNCTION_TRACE (AcpiInstallGlobalEventHandler);762763764/* Parameter validation */765766if (!Handler)767{768return_ACPI_STATUS (AE_BAD_PARAMETER);769}770771Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);772if (ACPI_FAILURE (Status))773{774return_ACPI_STATUS (Status);775}776777/* Don't allow two handlers. */778779if (AcpiGbl_GlobalEventHandler)780{781Status = AE_ALREADY_EXISTS;782goto Cleanup;783}784785AcpiGbl_GlobalEventHandler = Handler;786AcpiGbl_GlobalEventHandlerContext = Context;787788789Cleanup:790(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);791return_ACPI_STATUS (Status);792}793794ACPI_EXPORT_SYMBOL (AcpiInstallGlobalEventHandler)795796797/*******************************************************************************798*799* FUNCTION: AcpiInstallFixedEventHandler800*801* PARAMETERS: Event - Event type to enable.802* Handler - Pointer to the handler function for the803* event804* Context - Value passed to the handler on each GPE805*806* RETURN: Status807*808* DESCRIPTION: Saves the pointer to the handler function and then enables the809* event.810*811******************************************************************************/812813ACPI_STATUS814AcpiInstallFixedEventHandler (815UINT32 Event,816ACPI_EVENT_HANDLER Handler,817void *Context)818{819ACPI_STATUS Status;820821822ACPI_FUNCTION_TRACE (AcpiInstallFixedEventHandler);823824825/* Parameter validation */826827if (Event > ACPI_EVENT_MAX)828{829return_ACPI_STATUS (AE_BAD_PARAMETER);830}831832Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);833if (ACPI_FAILURE (Status))834{835return_ACPI_STATUS (Status);836}837838/* Do not allow multiple handlers */839840if (AcpiGbl_FixedEventHandlers[Event].Handler)841{842Status = AE_ALREADY_EXISTS;843goto Cleanup;844}845846/* Install the handler before enabling the event */847848AcpiGbl_FixedEventHandlers[Event].Handler = Handler;849AcpiGbl_FixedEventHandlers[Event].Context = Context;850851Status = AcpiEnableEvent (Event, 0);852if (ACPI_FAILURE (Status))853{854ACPI_WARNING ((AE_INFO,855"Could not enable fixed event - %s (%u)",856AcpiUtGetEventName (Event), Event));857858/* Remove the handler */859860AcpiGbl_FixedEventHandlers[Event].Handler = NULL;861AcpiGbl_FixedEventHandlers[Event].Context = NULL;862}863else864{865ACPI_DEBUG_PRINT ((ACPI_DB_INFO,866"Enabled fixed event %s (%X), Handler=%p\n",867AcpiUtGetEventName (Event), Event, Handler));868}869870871Cleanup:872(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);873return_ACPI_STATUS (Status);874}875876ACPI_EXPORT_SYMBOL (AcpiInstallFixedEventHandler)877878879/*******************************************************************************880*881* FUNCTION: AcpiRemoveFixedEventHandler882*883* PARAMETERS: Event - Event type to disable.884* Handler - Address of the handler885*886* RETURN: Status887*888* DESCRIPTION: Disables the event and unregisters the event handler.889*890******************************************************************************/891892ACPI_STATUS893AcpiRemoveFixedEventHandler (894UINT32 Event,895ACPI_EVENT_HANDLER Handler)896{897ACPI_STATUS Status = AE_OK;898899900ACPI_FUNCTION_TRACE (AcpiRemoveFixedEventHandler);901902903/* Parameter validation */904905if (Event > ACPI_EVENT_MAX)906{907return_ACPI_STATUS (AE_BAD_PARAMETER);908}909910Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);911if (ACPI_FAILURE (Status))912{913return_ACPI_STATUS (Status);914}915916/* Disable the event before removing the handler */917918Status = AcpiDisableEvent (Event, 0);919920/* Always Remove the handler */921922AcpiGbl_FixedEventHandlers[Event].Handler = NULL;923AcpiGbl_FixedEventHandlers[Event].Context = NULL;924925if (ACPI_FAILURE (Status))926{927ACPI_WARNING ((AE_INFO,928"Could not disable fixed event - %s (%u)",929AcpiUtGetEventName (Event), Event));930}931else932{933ACPI_DEBUG_PRINT ((ACPI_DB_INFO,934"Disabled fixed event - %s (%X)\n",935AcpiUtGetEventName (Event), Event));936}937938(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);939return_ACPI_STATUS (Status);940}941942ACPI_EXPORT_SYMBOL (AcpiRemoveFixedEventHandler)943944945/*******************************************************************************946*947* FUNCTION: AcpiEvInstallGpeHandler948*949* PARAMETERS: GpeDevice - Namespace node for the GPE (NULL for FADT950* defined GPEs)951* GpeNumber - The GPE number within the GPE block952* Type - Whether this GPE should be treated as an953* edge- or level-triggered interrupt.954* IsRawHandler - Whether this GPE should be handled using955* the special GPE handler mode.956* Address - Address of the handler957* Context - Value passed to the handler on each GPE958*959* RETURN: Status960*961* DESCRIPTION: Internal function to install a handler for a General Purpose962* Event.963*964******************************************************************************/965966static ACPI_STATUS967AcpiEvInstallGpeHandler (968ACPI_HANDLE GpeDevice,969UINT32 GpeNumber,970UINT32 Type,971BOOLEAN IsRawHandler,972ACPI_GPE_HANDLER Address,973void *Context)974{975ACPI_GPE_EVENT_INFO *GpeEventInfo;976ACPI_GPE_HANDLER_INFO *Handler;977ACPI_STATUS Status;978ACPI_CPU_FLAGS Flags;979980981ACPI_FUNCTION_TRACE (EvInstallGpeHandler);982983984/* Parameter validation */985986if ((!Address) || (Type & ~ACPI_GPE_XRUPT_TYPE_MASK))987{988return_ACPI_STATUS (AE_BAD_PARAMETER);989}990991Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);992if (ACPI_FAILURE (Status))993{994return_ACPI_STATUS (Status);995}996997/* Allocate and init handler object (before lock) */998999Handler = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_GPE_HANDLER_INFO));1000if (!Handler)1001{1002Status = AE_NO_MEMORY;1003goto UnlockAndExit;1004}10051006Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);10071008/* Ensure that we have a valid GPE number */10091010GpeEventInfo = AcpiEvGetGpeEventInfo (GpeDevice, GpeNumber);1011if (!GpeEventInfo)1012{1013Status = AE_BAD_PARAMETER;1014goto FreeAndExit;1015}10161017/* Make sure that there isn't a handler there already */10181019if ((ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==1020ACPI_GPE_DISPATCH_HANDLER) ||1021(ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==1022ACPI_GPE_DISPATCH_RAW_HANDLER))1023{1024Status = AE_ALREADY_EXISTS;1025goto FreeAndExit;1026}10271028Handler->Address = Address;1029Handler->Context = Context;1030Handler->MethodNode = GpeEventInfo->Dispatch.MethodNode;1031Handler->OriginalFlags = (UINT8) (GpeEventInfo->Flags &1032(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK));10331034/*1035* If the GPE is associated with a method, it may have been enabled1036* automatically during initialization, in which case it has to be1037* disabled now to avoid spurious execution of the handler.1038*/1039if (((ACPI_GPE_DISPATCH_TYPE (Handler->OriginalFlags) ==1040ACPI_GPE_DISPATCH_METHOD) ||1041(ACPI_GPE_DISPATCH_TYPE (Handler->OriginalFlags) ==1042ACPI_GPE_DISPATCH_NOTIFY)) &&1043GpeEventInfo->RuntimeCount)1044{1045Handler->OriginallyEnabled = TRUE;1046(void) AcpiEvRemoveGpeReference (GpeEventInfo);10471048/* Sanity check of original type against new type */10491050if (Type != (UINT32) (GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK))1051{1052ACPI_WARNING ((AE_INFO, "GPE type mismatch (level/edge)"));1053}1054}10551056/* Install the handler */10571058GpeEventInfo->Dispatch.Handler = Handler;10591060/* Setup up dispatch flags to indicate handler (vs. method/notify) */10611062GpeEventInfo->Flags &= ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);1063GpeEventInfo->Flags |= (UINT8) (Type | (IsRawHandler ?1064ACPI_GPE_DISPATCH_RAW_HANDLER : ACPI_GPE_DISPATCH_HANDLER));10651066AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);106710681069UnlockAndExit:1070(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);1071return_ACPI_STATUS (Status);10721073FreeAndExit:1074AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);1075ACPI_FREE (Handler);1076goto UnlockAndExit;1077}107810791080/*******************************************************************************1081*1082* FUNCTION: AcpiInstallGpeHandler1083*1084* PARAMETERS: GpeDevice - Namespace node for the GPE (NULL for FADT1085* defined GPEs)1086* GpeNumber - The GPE number within the GPE block1087* Type - Whether this GPE should be treated as an1088* edge- or level-triggered interrupt.1089* Address - Address of the handler1090* Context - Value passed to the handler on each GPE1091*1092* RETURN: Status1093*1094* DESCRIPTION: Install a handler for a General Purpose Event.1095*1096******************************************************************************/10971098ACPI_STATUS1099AcpiInstallGpeHandler (1100ACPI_HANDLE GpeDevice,1101UINT32 GpeNumber,1102UINT32 Type,1103ACPI_GPE_HANDLER Address,1104void *Context)1105{1106ACPI_STATUS Status;110711081109ACPI_FUNCTION_TRACE (AcpiInstallGpeHandler);111011111112Status = AcpiEvInstallGpeHandler (GpeDevice, GpeNumber, Type,1113FALSE, Address, Context);11141115return_ACPI_STATUS (Status);1116}11171118ACPI_EXPORT_SYMBOL (AcpiInstallGpeHandler)111911201121/*******************************************************************************1122*1123* FUNCTION: AcpiInstallGpeRawHandler1124*1125* PARAMETERS: GpeDevice - Namespace node for the GPE (NULL for FADT1126* defined GPEs)1127* GpeNumber - The GPE number within the GPE block1128* Type - Whether this GPE should be treated as an1129* edge- or level-triggered interrupt.1130* Address - Address of the handler1131* Context - Value passed to the handler on each GPE1132*1133* RETURN: Status1134*1135* DESCRIPTION: Install a handler for a General Purpose Event.1136*1137******************************************************************************/11381139ACPI_STATUS1140AcpiInstallGpeRawHandler (1141ACPI_HANDLE GpeDevice,1142UINT32 GpeNumber,1143UINT32 Type,1144ACPI_GPE_HANDLER Address,1145void *Context)1146{1147ACPI_STATUS Status;114811491150ACPI_FUNCTION_TRACE (AcpiInstallGpeRawHandler);115111521153Status = AcpiEvInstallGpeHandler (GpeDevice, GpeNumber, Type,1154TRUE, Address, Context);11551156return_ACPI_STATUS (Status);1157}11581159ACPI_EXPORT_SYMBOL (AcpiInstallGpeRawHandler)116011611162/*******************************************************************************1163*1164* FUNCTION: AcpiRemoveGpeHandler1165*1166* PARAMETERS: GpeDevice - Namespace node for the GPE (NULL for FADT1167* defined GPEs)1168* GpeNumber - The event to remove a handler1169* Address - Address of the handler1170*1171* RETURN: Status1172*1173* DESCRIPTION: Remove a handler for a General Purpose AcpiEvent.1174*1175******************************************************************************/11761177ACPI_STATUS1178AcpiRemoveGpeHandler (1179ACPI_HANDLE GpeDevice,1180UINT32 GpeNumber,1181ACPI_GPE_HANDLER Address)1182{1183ACPI_GPE_EVENT_INFO *GpeEventInfo;1184ACPI_GPE_HANDLER_INFO *Handler;1185ACPI_STATUS Status;1186ACPI_CPU_FLAGS Flags;118711881189ACPI_FUNCTION_TRACE (AcpiRemoveGpeHandler);119011911192/* Parameter validation */11931194if (!Address)1195{1196return_ACPI_STATUS (AE_BAD_PARAMETER);1197}11981199Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);1200if (ACPI_FAILURE (Status))1201{1202return_ACPI_STATUS (Status);1203}12041205Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);12061207/* Ensure that we have a valid GPE number */12081209GpeEventInfo = AcpiEvGetGpeEventInfo (GpeDevice, GpeNumber);1210if (!GpeEventInfo)1211{1212Status = AE_BAD_PARAMETER;1213goto UnlockAndExit;1214}12151216/* Make sure that a handler is indeed installed */12171218if ((ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) !=1219ACPI_GPE_DISPATCH_HANDLER) &&1220(ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) !=1221ACPI_GPE_DISPATCH_RAW_HANDLER))1222{1223Status = AE_NOT_EXIST;1224goto UnlockAndExit;1225}12261227/* Make sure that the installed handler is the same */12281229if (GpeEventInfo->Dispatch.Handler->Address != Address)1230{1231Status = AE_BAD_PARAMETER;1232goto UnlockAndExit;1233}12341235/* Remove the handler */12361237Handler = GpeEventInfo->Dispatch.Handler;1238GpeEventInfo->Dispatch.Handler = NULL;12391240/* Restore Method node (if any), set dispatch flags */12411242GpeEventInfo->Dispatch.MethodNode = Handler->MethodNode;1243GpeEventInfo->Flags &=1244~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);1245GpeEventInfo->Flags |= Handler->OriginalFlags;12461247/*1248* If the GPE was previously associated with a method and it was1249* enabled, it should be enabled at this point to restore the1250* post-initialization configuration.1251*/1252if (((ACPI_GPE_DISPATCH_TYPE (Handler->OriginalFlags) ==1253ACPI_GPE_DISPATCH_METHOD) ||1254(ACPI_GPE_DISPATCH_TYPE (Handler->OriginalFlags) ==1255ACPI_GPE_DISPATCH_NOTIFY)) &&1256Handler->OriginallyEnabled)1257{1258(void) AcpiEvAddGpeReference (GpeEventInfo, FALSE);1259if (ACPI_GPE_IS_POLLING_NEEDED (GpeEventInfo))1260{1261/* Poll edge triggered GPEs to handle existing events */12621263AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);1264(void) AcpiEvDetectGpe (1265GpeDevice, GpeEventInfo, GpeNumber);1266Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);1267}1268}12691270AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);1271(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);12721273/* Make sure all deferred GPE tasks are completed */12741275AcpiOsWaitEventsComplete ();12761277/* Now we can free the handler object */12781279ACPI_FREE (Handler);1280return_ACPI_STATUS (Status);12811282UnlockAndExit:1283AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);1284(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);1285return_ACPI_STATUS (Status);1286}12871288ACPI_EXPORT_SYMBOL (AcpiRemoveGpeHandler)128912901291/*******************************************************************************1292*1293* FUNCTION: AcpiAcquireGlobalLock1294*1295* PARAMETERS: Timeout - How long the caller is willing to wait1296* Handle - Where the handle to the lock is returned1297* (if acquired)1298*1299* RETURN: Status1300*1301* DESCRIPTION: Acquire the ACPI Global Lock1302*1303* Note: Allows callers with the same thread ID to acquire the global lock1304* multiple times. In other words, externally, the behavior of the global lock1305* is identical to an AML mutex. On the first acquire, a new handle is1306* returned. On any subsequent calls to acquire by the same thread, the same1307* handle is returned.1308*1309******************************************************************************/13101311ACPI_STATUS1312AcpiAcquireGlobalLock (1313UINT16 Timeout,1314UINT32 *Handle)1315{1316ACPI_STATUS Status;131713181319if (!Handle)1320{1321return (AE_BAD_PARAMETER);1322}13231324/* Must lock interpreter to prevent race conditions */13251326AcpiExEnterInterpreter ();13271328Status = AcpiExAcquireMutexObject (Timeout,1329AcpiGbl_GlobalLockMutex, AcpiOsGetThreadId ());13301331if (ACPI_SUCCESS (Status))1332{1333/* Return the global lock handle (updated in AcpiEvAcquireGlobalLock) */13341335*Handle = AcpiGbl_GlobalLockHandle;1336}13371338AcpiExExitInterpreter ();1339return (Status);1340}13411342ACPI_EXPORT_SYMBOL (AcpiAcquireGlobalLock)134313441345/*******************************************************************************1346*1347* FUNCTION: AcpiReleaseGlobalLock1348*1349* PARAMETERS: Handle - Returned from AcpiAcquireGlobalLock1350*1351* RETURN: Status1352*1353* DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.1354*1355******************************************************************************/13561357ACPI_STATUS1358AcpiReleaseGlobalLock (1359UINT32 Handle)1360{1361ACPI_STATUS Status;136213631364if (!Handle || (Handle != AcpiGbl_GlobalLockHandle))1365{1366return (AE_NOT_ACQUIRED);1367}13681369Status = AcpiExReleaseMutexObject (AcpiGbl_GlobalLockMutex);1370return (Status);1371}13721373ACPI_EXPORT_SYMBOL (AcpiReleaseGlobalLock)13741375#endif /* !ACPI_REDUCED_HARDWARE */137613771378