Path: blob/main/sys/contrib/dev/acpica/components/events/evgpeinit.c
48525 views
/******************************************************************************1*2* Module Name: evgpeinit - System GPE initialization and update3*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>154#include <contrib/dev/acpica/include/acnamesp.h>155156#define _COMPONENT ACPI_EVENTS157ACPI_MODULE_NAME ("evgpeinit")158159#if (!ACPI_REDUCED_HARDWARE) /* Entire module */160161/*162* Note: History of _PRW support in ACPICA163*164* Originally (2000 - 2010), the GPE initialization code performed a walk of165* the entire namespace to execute the _PRW methods and detect all GPEs166* capable of waking the system.167*168* As of 10/2010, the _PRW method execution has been removed since it is169* actually unnecessary. The host OS must in fact execute all _PRW methods170* in order to identify the device/power-resource dependencies. We now put171* the onus on the host OS to identify the wake GPEs as part of this process172* and to inform ACPICA of these GPEs via the AcpiSetupGpeForWake interface. This173* not only reduces the complexity of the ACPICA initialization code, but in174* some cases (on systems with very large namespaces) it should reduce the175* kernel boot time as well.176*/177178/*******************************************************************************179*180* FUNCTION: AcpiEvGpeInitialize181*182* PARAMETERS: None183*184* RETURN: Status185*186* DESCRIPTION: Initialize the GPE data structures and the FADT GPE 0/1 blocks187*188******************************************************************************/189190ACPI_STATUS191AcpiEvGpeInitialize (192void)193{194UINT32 RegisterCount0 = 0;195UINT32 RegisterCount1 = 0;196UINT32 GpeNumberMax = 0;197ACPI_STATUS Status;198199200ACPI_FUNCTION_TRACE (EvGpeInitialize);201202203ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INIT,204"Initializing General Purpose Events (GPEs):\n"));205206Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);207if (ACPI_FAILURE (Status))208{209return_ACPI_STATUS (Status);210}211212/*213* Initialize the GPE Block(s) defined in the FADT214*215* Why the GPE register block lengths are divided by 2: From the ACPI216* Spec, section "General-Purpose Event Registers", we have:217*218* "Each register block contains two registers of equal length219* GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the220* GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN221* The length of the GPE1_STS and GPE1_EN registers is equal to222* half the GPE1_LEN. If a generic register block is not supported223* then its respective block pointer and block length values in the224* FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need225* to be the same size."226*/227228/*229* Determine the maximum GPE number for this machine.230*231* Note: both GPE0 and GPE1 are optional, and either can exist without232* the other.233*234* If EITHER the register length OR the block address are zero, then that235* particular block is not supported.236*/237if (AcpiGbl_FADT.Gpe0BlockLength &&238AcpiGbl_FADT.XGpe0Block.Address)239{240/* GPE block 0 exists (has both length and address > 0) */241242RegisterCount0 = (UINT16) (AcpiGbl_FADT.Gpe0BlockLength / 2);243GpeNumberMax = (RegisterCount0 * ACPI_GPE_REGISTER_WIDTH) - 1;244245/* Install GPE Block 0 */246247Status = AcpiEvCreateGpeBlock (AcpiGbl_FadtGpeDevice,248AcpiGbl_FADT.XGpe0Block.Address,249AcpiGbl_FADT.XGpe0Block.SpaceId,250RegisterCount0, 0,251AcpiGbl_FADT.SciInterrupt, &AcpiGbl_GpeFadtBlocks[0]);252253if (ACPI_FAILURE (Status))254{255ACPI_EXCEPTION ((AE_INFO, Status,256"Could not create GPE Block 0"));257}258}259260if (AcpiGbl_FADT.Gpe1BlockLength &&261AcpiGbl_FADT.XGpe1Block.Address)262{263/* GPE block 1 exists (has both length and address > 0) */264265RegisterCount1 = (UINT16) (AcpiGbl_FADT.Gpe1BlockLength / 2);266267/* Check for GPE0/GPE1 overlap (if both banks exist) */268269if ((RegisterCount0) &&270(GpeNumberMax >= AcpiGbl_FADT.Gpe1Base))271{272ACPI_ERROR ((AE_INFO,273"GPE0 block (GPE 0 to %u) overlaps the GPE1 block "274"(GPE %u to %u) - Ignoring GPE1",275GpeNumberMax, AcpiGbl_FADT.Gpe1Base,276AcpiGbl_FADT.Gpe1Base +277((RegisterCount1 * ACPI_GPE_REGISTER_WIDTH) - 1)));278279/* Ignore GPE1 block by setting the register count to zero */280281RegisterCount1 = 0;282}283else284{285/* Install GPE Block 1 */286287Status = AcpiEvCreateGpeBlock (AcpiGbl_FadtGpeDevice,288AcpiGbl_FADT.XGpe1Block.Address,289AcpiGbl_FADT.XGpe1Block.SpaceId,290RegisterCount1,291AcpiGbl_FADT.Gpe1Base,292AcpiGbl_FADT.SciInterrupt, &AcpiGbl_GpeFadtBlocks[1]);293294if (ACPI_FAILURE (Status))295{296ACPI_EXCEPTION ((AE_INFO, Status,297"Could not create GPE Block 1"));298}299300/*301* GPE0 and GPE1 do not have to be contiguous in the GPE number302* space. However, GPE0 always starts at GPE number zero.303*/304}305}306307/* Exit if there are no GPE registers */308309if ((RegisterCount0 + RegisterCount1) == 0)310{311/* GPEs are not required by ACPI, this is OK */312313ACPI_DEBUG_PRINT ((ACPI_DB_INIT,314"There are no GPE blocks defined in the FADT\n"));315goto Cleanup;316}317318319Cleanup:320(void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);321return_ACPI_STATUS (AE_OK);322}323324325/*******************************************************************************326*327* FUNCTION: AcpiEvUpdateGpes328*329* PARAMETERS: TableOwnerId - ID of the newly-loaded ACPI table330*331* RETURN: None332*333* DESCRIPTION: Check for new GPE methods (_Lxx/_Exx) made available as a334* result of a Load() or LoadTable() operation. If new GPE335* methods have been installed, register the new methods.336*337******************************************************************************/338339void340AcpiEvUpdateGpes (341ACPI_OWNER_ID TableOwnerId)342{343ACPI_GPE_XRUPT_INFO *GpeXruptInfo;344ACPI_GPE_BLOCK_INFO *GpeBlock;345ACPI_GPE_WALK_INFO WalkInfo;346ACPI_STATUS Status = AE_OK;347348349/*350* Find any _Lxx/_Exx GPE methods that have just been loaded.351*352* Any GPEs that correspond to new _Lxx/_Exx methods are immediately353* enabled.354*355* Examine the namespace underneath each GpeDevice within the356* GpeBlock lists.357*/358Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);359if (ACPI_FAILURE (Status))360{361return;362}363364WalkInfo.Count = 0;365WalkInfo.OwnerId = TableOwnerId;366WalkInfo.ExecuteByOwnerId = TRUE;367368/* Walk the interrupt level descriptor list */369370GpeXruptInfo = AcpiGbl_GpeXruptListHead;371while (GpeXruptInfo)372{373/* Walk all Gpe Blocks attached to this interrupt level */374375GpeBlock = GpeXruptInfo->GpeBlockListHead;376while (GpeBlock)377{378WalkInfo.GpeBlock = GpeBlock;379WalkInfo.GpeDevice = GpeBlock->Node;380381Status = AcpiNsWalkNamespace (ACPI_TYPE_METHOD,382WalkInfo.GpeDevice, ACPI_UINT32_MAX,383ACPI_NS_WALK_NO_UNLOCK, AcpiEvMatchGpeMethod,384NULL, &WalkInfo, NULL);385if (ACPI_FAILURE (Status))386{387ACPI_EXCEPTION ((AE_INFO, Status,388"While decoding _Lxx/_Exx methods"));389}390391GpeBlock = GpeBlock->Next;392}393394GpeXruptInfo = GpeXruptInfo->Next;395}396397if (WalkInfo.Count)398{399ACPI_INFO (("Enabled %u new GPEs", WalkInfo.Count));400}401402(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);403return;404}405406407/*******************************************************************************408*409* FUNCTION: AcpiEvMatchGpeMethod410*411* PARAMETERS: Callback from WalkNamespace412*413* RETURN: Status414*415* DESCRIPTION: Called from AcpiWalkNamespace. Expects each object to be a416* control method under the _GPE portion of the namespace.417* Extract the name and GPE type from the object, saving this418* information for quick lookup during GPE dispatch. Allows a419* per-OwnerId evaluation if ExecuteByOwnerId is TRUE in the420* WalkInfo parameter block.421*422* The name of each GPE control method is of the form:423* "_Lxx" or "_Exx", where:424* L - means that the GPE is level triggered425* E - means that the GPE is edge triggered426* xx - is the GPE number [in HEX]427*428* If WalkInfo->ExecuteByOwnerId is TRUE, we only execute examine GPE methods429* with that owner.430*431******************************************************************************/432433ACPI_STATUS434AcpiEvMatchGpeMethod (435ACPI_HANDLE ObjHandle,436UINT32 Level,437void *Context,438void **ReturnValue)439{440ACPI_NAMESPACE_NODE *MethodNode = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, ObjHandle);441ACPI_GPE_WALK_INFO *WalkInfo = ACPI_CAST_PTR (ACPI_GPE_WALK_INFO, Context);442ACPI_GPE_EVENT_INFO *GpeEventInfo;443ACPI_STATUS Status;444UINT32 GpeNumber;445UINT8 TempGpeNumber;446char Name[ACPI_NAMESEG_SIZE + 1];447UINT8 Type;448449450ACPI_FUNCTION_TRACE (EvMatchGpeMethod);451452453/* Check if requested OwnerId matches this OwnerId */454455if ((WalkInfo->ExecuteByOwnerId) &&456(MethodNode->OwnerId != WalkInfo->OwnerId))457{458return_ACPI_STATUS (AE_OK);459}460461/*462* Match and decode the _Lxx and _Exx GPE method names463*464* 1) Extract the method name and null terminate it465*/466ACPI_MOVE_32_TO_32 (Name, &MethodNode->Name.Integer);467Name[ACPI_NAMESEG_SIZE] = 0;468469/* 2) Name must begin with an underscore */470471if (Name[0] != '_')472{473return_ACPI_STATUS (AE_OK); /* Ignore this method */474}475476/*477* 3) Edge/Level determination is based on the 2nd character478* of the method name479*/480switch (Name[1])481{482case 'L':483484Type = ACPI_GPE_LEVEL_TRIGGERED;485break;486487case 'E':488489Type = ACPI_GPE_EDGE_TRIGGERED;490break;491492default:493494/* Unknown method type, just ignore it */495496ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,497"Ignoring unknown GPE method type: %s "498"(name not of form _Lxx or _Exx)", Name));499return_ACPI_STATUS (AE_OK);500}501502/* 4) The last two characters of the name are the hex GPE Number */503504Status = AcpiUtAsciiToHexByte (&Name[2], &TempGpeNumber);505if (ACPI_FAILURE (Status))506{507/* Conversion failed; invalid method, just ignore it */508509ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,510"Could not extract GPE number from name: %s "511"(name is not of form _Lxx or _Exx)", Name));512return_ACPI_STATUS (AE_OK);513}514515/* Ensure that we have a valid GPE number for this GPE block */516517GpeNumber = (UINT32) TempGpeNumber;518GpeEventInfo = AcpiEvLowGetGpeInfo (GpeNumber, WalkInfo->GpeBlock);519if (!GpeEventInfo)520{521/*522* This GpeNumber is not valid for this GPE block, just ignore it.523* However, it may be valid for a different GPE block, since GPE0524* and GPE1 methods both appear under \_GPE.525*/526return_ACPI_STATUS (AE_OK);527}528529if ((ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==530ACPI_GPE_DISPATCH_HANDLER) ||531(ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==532ACPI_GPE_DISPATCH_RAW_HANDLER))533{534/* If there is already a handler, ignore this GPE method */535536return_ACPI_STATUS (AE_OK);537}538539if (ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==540ACPI_GPE_DISPATCH_METHOD)541{542/*543* If there is already a method, ignore this method. But check544* for a type mismatch (if both the _Lxx AND _Exx exist)545*/546if (Type != (GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK))547{548ACPI_ERROR ((AE_INFO,549"For GPE 0x%.2X, found both _L%2.2X and _E%2.2X methods",550GpeNumber, GpeNumber, GpeNumber));551}552return_ACPI_STATUS (AE_OK);553}554555/* Disable the GPE in case it's been enabled already. */556557(void) AcpiHwLowSetGpe (GpeEventInfo, ACPI_GPE_DISABLE);558559/*560* Add the GPE information from above to the GpeEventInfo block for561* use during dispatch of this GPE.562*/563GpeEventInfo->Flags &= ~(ACPI_GPE_DISPATCH_MASK);564GpeEventInfo->Flags |= (UINT8) (Type | ACPI_GPE_DISPATCH_METHOD);565GpeEventInfo->Dispatch.MethodNode = MethodNode;566WalkInfo->Count++;567568ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,569"Registered GPE method %s as GPE number 0x%.2X\n",570Name, GpeNumber));571return_ACPI_STATUS (AE_OK);572}573574#endif /* !ACPI_REDUCED_HARDWARE */575576577