Path: blob/main/sys/contrib/dev/acpica/components/events/evgpeutil.c
48525 views
/******************************************************************************1*2* Module Name: evgpeutil - GPE utilities3*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#include <contrib/dev/acpica/include/acpi.h>152#include <contrib/dev/acpica/include/accommon.h>153#include <contrib/dev/acpica/include/acevents.h>154155#define _COMPONENT ACPI_EVENTS156ACPI_MODULE_NAME ("evgpeutil")157158159#if (!ACPI_REDUCED_HARDWARE) /* Entire module */160/*******************************************************************************161*162* FUNCTION: AcpiEvWalkGpeList163*164* PARAMETERS: GpeWalkCallback - Routine called for each GPE block165* Context - Value passed to callback166*167* RETURN: Status168*169* DESCRIPTION: Walk the GPE lists.170*171******************************************************************************/172173ACPI_STATUS174AcpiEvWalkGpeList (175ACPI_GPE_CALLBACK GpeWalkCallback,176void *Context)177{178ACPI_GPE_BLOCK_INFO *GpeBlock;179ACPI_GPE_XRUPT_INFO *GpeXruptInfo;180ACPI_STATUS Status = AE_OK;181ACPI_CPU_FLAGS Flags;182183184ACPI_FUNCTION_TRACE (EvWalkGpeList);185186187Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);188189/* Walk the interrupt level descriptor list */190191GpeXruptInfo = AcpiGbl_GpeXruptListHead;192while (GpeXruptInfo)193{194/* Walk all Gpe Blocks attached to this interrupt level */195196GpeBlock = GpeXruptInfo->GpeBlockListHead;197while (GpeBlock)198{199/* One callback per GPE block */200201Status = GpeWalkCallback (GpeXruptInfo, GpeBlock, Context);202if (ACPI_FAILURE (Status))203{204if (Status == AE_CTRL_END) /* Callback abort */205{206Status = AE_OK;207}208goto UnlockAndExit;209}210211GpeBlock = GpeBlock->Next;212}213214GpeXruptInfo = GpeXruptInfo->Next;215}216217UnlockAndExit:218AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);219return_ACPI_STATUS (Status);220}221222223/*******************************************************************************224*225* FUNCTION: AcpiEvGetGpeDevice226*227* PARAMETERS: GPE_WALK_CALLBACK228*229* RETURN: Status230*231* DESCRIPTION: Matches the input GPE index (0-CurrentGpeCount) with a GPE232* block device. NULL if the GPE is one of the FADT-defined GPEs.233*234******************************************************************************/235236ACPI_STATUS237AcpiEvGetGpeDevice (238ACPI_GPE_XRUPT_INFO *GpeXruptInfo,239ACPI_GPE_BLOCK_INFO *GpeBlock,240void *Context)241{242ACPI_GPE_DEVICE_INFO *Info = Context;243244245/* Increment Index by the number of GPEs in this block */246247Info->NextBlockBaseIndex += GpeBlock->GpeCount;248249if (Info->Index < Info->NextBlockBaseIndex)250{251/*252* The GPE index is within this block, get the node. Leave the node253* NULL for the FADT-defined GPEs254*/255if ((GpeBlock->Node)->Type == ACPI_TYPE_DEVICE)256{257Info->GpeDevice = GpeBlock->Node;258}259260Info->Status = AE_OK;261return (AE_CTRL_END);262}263264return (AE_OK);265}266267268/*******************************************************************************269*270* FUNCTION: AcpiEvGetGpeXruptBlock271*272* PARAMETERS: InterruptNumber - Interrupt for a GPE block273* GpeXruptBlock - Where the block is returned274*275* RETURN: Status276*277* DESCRIPTION: Get or Create a GPE interrupt block. There is one interrupt278* block per unique interrupt level used for GPEs. Should be279* called only when the GPE lists are semaphore locked and not280* subject to change.281*282******************************************************************************/283284ACPI_STATUS285AcpiEvGetGpeXruptBlock (286UINT32 InterruptNumber,287ACPI_GPE_XRUPT_INFO **GpeXruptBlock)288{289ACPI_GPE_XRUPT_INFO *NextGpeXrupt;290ACPI_GPE_XRUPT_INFO *GpeXrupt;291ACPI_STATUS Status;292ACPI_CPU_FLAGS Flags;293294295ACPI_FUNCTION_TRACE (EvGetGpeXruptBlock);296297298/* No need for lock since we are not changing any list elements here */299300NextGpeXrupt = AcpiGbl_GpeXruptListHead;301while (NextGpeXrupt)302{303if (NextGpeXrupt->InterruptNumber == InterruptNumber)304{305*GpeXruptBlock = NextGpeXrupt;306return_ACPI_STATUS (AE_OK);307}308309NextGpeXrupt = NextGpeXrupt->Next;310}311312/* Not found, must allocate a new xrupt descriptor */313314GpeXrupt = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_GPE_XRUPT_INFO));315if (!GpeXrupt)316{317return_ACPI_STATUS (AE_NO_MEMORY);318}319320GpeXrupt->InterruptNumber = InterruptNumber;321322/* Install new interrupt descriptor with spin lock */323324Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);325if (AcpiGbl_GpeXruptListHead)326{327NextGpeXrupt = AcpiGbl_GpeXruptListHead;328while (NextGpeXrupt->Next)329{330NextGpeXrupt = NextGpeXrupt->Next;331}332333NextGpeXrupt->Next = GpeXrupt;334GpeXrupt->Previous = NextGpeXrupt;335}336else337{338AcpiGbl_GpeXruptListHead = GpeXrupt;339}340341AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);342343/* Install new interrupt handler if not SCI_INT */344345if (InterruptNumber != AcpiGbl_FADT.SciInterrupt)346{347Status = AcpiOsInstallInterruptHandler (InterruptNumber,348AcpiEvGpeXruptHandler, GpeXrupt);349if (ACPI_FAILURE (Status))350{351ACPI_EXCEPTION ((AE_INFO, Status,352"Could not install GPE interrupt handler at level 0x%X",353InterruptNumber));354return_ACPI_STATUS (Status);355}356}357358*GpeXruptBlock = GpeXrupt;359return_ACPI_STATUS (AE_OK);360}361362363/*******************************************************************************364*365* FUNCTION: AcpiEvDeleteGpeXrupt366*367* PARAMETERS: GpeXrupt - A GPE interrupt info block368*369* RETURN: Status370*371* DESCRIPTION: Remove and free a GpeXrupt block. Remove an associated372* interrupt handler if not the SCI interrupt.373*374******************************************************************************/375376ACPI_STATUS377AcpiEvDeleteGpeXrupt (378ACPI_GPE_XRUPT_INFO *GpeXrupt)379{380ACPI_STATUS Status;381ACPI_CPU_FLAGS Flags;382383384ACPI_FUNCTION_TRACE (EvDeleteGpeXrupt);385386387/* We never want to remove the SCI interrupt handler */388389if (GpeXrupt->InterruptNumber == AcpiGbl_FADT.SciInterrupt)390{391GpeXrupt->GpeBlockListHead = NULL;392return_ACPI_STATUS (AE_OK);393}394395/* Disable this interrupt */396397Status = AcpiOsRemoveInterruptHandler (398GpeXrupt->InterruptNumber, AcpiEvGpeXruptHandler);399if (ACPI_FAILURE (Status))400{401return_ACPI_STATUS (Status);402}403404/* Unlink the interrupt block with lock */405406Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);407if (GpeXrupt->Previous)408{409GpeXrupt->Previous->Next = GpeXrupt->Next;410}411else412{413/* No previous, update list head */414415AcpiGbl_GpeXruptListHead = GpeXrupt->Next;416}417418if (GpeXrupt->Next)419{420GpeXrupt->Next->Previous = GpeXrupt->Previous;421}422AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);423424/* Free the block */425426ACPI_FREE (GpeXrupt);427return_ACPI_STATUS (AE_OK);428}429430431/*******************************************************************************432*433* FUNCTION: AcpiEvDeleteGpeHandlers434*435* PARAMETERS: GpeXruptInfo - GPE Interrupt info436* GpeBlock - Gpe Block info437*438* RETURN: Status439*440* DESCRIPTION: Delete all Handler objects found in the GPE data structs.441* Used only prior to termination.442*443******************************************************************************/444445ACPI_STATUS446AcpiEvDeleteGpeHandlers (447ACPI_GPE_XRUPT_INFO *GpeXruptInfo,448ACPI_GPE_BLOCK_INFO *GpeBlock,449void *Context)450{451ACPI_GPE_EVENT_INFO *GpeEventInfo;452ACPI_GPE_NOTIFY_INFO *Notify;453ACPI_GPE_NOTIFY_INFO *Next;454UINT32 i;455UINT32 j;456457458ACPI_FUNCTION_TRACE (EvDeleteGpeHandlers);459460461/* Examine each GPE Register within the block */462463for (i = 0; i < GpeBlock->RegisterCount; i++)464{465/* Now look at the individual GPEs in this byte register */466467for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++)468{469GpeEventInfo = &GpeBlock->EventInfo[((ACPI_SIZE) i *470ACPI_GPE_REGISTER_WIDTH) + j];471472if ((ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==473ACPI_GPE_DISPATCH_HANDLER) ||474(ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==475ACPI_GPE_DISPATCH_RAW_HANDLER))476{477/* Delete an installed handler block */478479ACPI_FREE (GpeEventInfo->Dispatch.Handler);480GpeEventInfo->Dispatch.Handler = NULL;481GpeEventInfo->Flags &= ~ACPI_GPE_DISPATCH_MASK;482}483else if (ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==484ACPI_GPE_DISPATCH_NOTIFY)485{486/* Delete the implicit notification device list */487488Notify = GpeEventInfo->Dispatch.NotifyList;489while (Notify)490{491Next = Notify->Next;492ACPI_FREE (Notify);493Notify = Next;494}495496GpeEventInfo->Dispatch.NotifyList = NULL;497GpeEventInfo->Flags &= ~ACPI_GPE_DISPATCH_MASK;498}499}500}501502return_ACPI_STATUS (AE_OK);503}504505#endif /* !ACPI_REDUCED_HARDWARE */506507508