Path: blob/main/sys/contrib/dev/acpica/components/hardware/hwxface.c
48521 views
/******************************************************************************1*2* Module Name: hwxface - Public ACPICA hardware interfaces3*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>156157#define _COMPONENT ACPI_HARDWARE158ACPI_MODULE_NAME ("hwxface")159160161/******************************************************************************162*163* FUNCTION: AcpiReset164*165* PARAMETERS: None166*167* RETURN: Status168*169* DESCRIPTION: Set reset register in memory or IO space. Note: Does not170* support reset register in PCI config space, this must be171* handled separately.172*173******************************************************************************/174175ACPI_STATUS176AcpiReset (177void)178{179ACPI_GENERIC_ADDRESS *ResetReg;180ACPI_STATUS Status;181182183ACPI_FUNCTION_TRACE (AcpiReset);184185186ResetReg = &AcpiGbl_FADT.ResetRegister;187188/* Check if the reset register is supported */189190if (!(AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) ||191!ResetReg->Address)192{193return_ACPI_STATUS (AE_NOT_EXIST);194}195196if (ResetReg->SpaceId == ACPI_ADR_SPACE_SYSTEM_IO)197{198/*199* For I/O space, write directly to the OSL. This bypasses the port200* validation mechanism, which may block a valid write to the reset201* register.202*203* NOTE:204* The ACPI spec requires the reset register width to be 8, so we205* hardcode it here and ignore the FADT value. This maintains206* compatibility with other ACPI implementations that have allowed207* BIOS code with bad register width values to go unnoticed.208*/209Status = AcpiOsWritePort ((ACPI_IO_ADDRESS) ResetReg->Address,210AcpiGbl_FADT.ResetValue, ACPI_RESET_REGISTER_WIDTH);211}212else213{214/* Write the reset value to the reset register */215216Status = AcpiHwWrite (AcpiGbl_FADT.ResetValue, ResetReg);217}218219return_ACPI_STATUS (Status);220}221222ACPI_EXPORT_SYMBOL (AcpiReset)223224225/******************************************************************************226*227* FUNCTION: AcpiRead228*229* PARAMETERS: Value - Where the value is returned230* Reg - GAS register structure231*232* RETURN: Status233*234* DESCRIPTION: Read from either memory or IO space.235*236* LIMITATIONS: <These limitations also apply to AcpiWrite>237* BitWidth must be exactly 8, 16, 32, or 64.238* SpaceID must be SystemMemory or SystemIO.239* BitOffset and AccessWidth are currently ignored, as there has240* not been a need to implement these.241*242******************************************************************************/243244ACPI_STATUS245AcpiRead (246UINT64 *ReturnValue,247ACPI_GENERIC_ADDRESS *Reg)248{249ACPI_STATUS Status;250251252ACPI_FUNCTION_NAME (AcpiRead);253254255Status = AcpiHwRead (ReturnValue, Reg);256return (Status);257}258259ACPI_EXPORT_SYMBOL (AcpiRead)260261262/******************************************************************************263*264* FUNCTION: AcpiWrite265*266* PARAMETERS: Value - Value to be written267* Reg - GAS register structure268*269* RETURN: Status270*271* DESCRIPTION: Write to either memory or IO space.272*273******************************************************************************/274275ACPI_STATUS276AcpiWrite (277UINT64 Value,278ACPI_GENERIC_ADDRESS *Reg)279{280ACPI_STATUS Status;281282283ACPI_FUNCTION_NAME (AcpiWrite);284285286Status = AcpiHwWrite (Value, Reg);287return (Status);288}289290ACPI_EXPORT_SYMBOL (AcpiWrite)291292293#if (!ACPI_REDUCED_HARDWARE)294/*******************************************************************************295*296* FUNCTION: AcpiReadBitRegister297*298* PARAMETERS: RegisterId - ID of ACPI Bit Register to access299* ReturnValue - Value that was read from the register,300* normalized to bit position zero.301*302* RETURN: Status and the value read from the specified Register. Value303* returned is normalized to bit0 (is shifted all the way right)304*305* DESCRIPTION: ACPI BitRegister read function. Does not acquire the HW lock.306*307* SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and308* PM2 Control.309*310* Note: The hardware lock is not required when reading the ACPI bit registers311* since almost all of them are single bit and it does not matter that312* the parent hardware register can be split across two physical313* registers. The only multi-bit field is SLP_TYP in the PM1 control314* register, but this field does not cross an 8-bit boundary (nor does315* it make much sense to actually read this field.)316*317******************************************************************************/318319ACPI_STATUS320AcpiReadBitRegister (321UINT32 RegisterId,322UINT32 *ReturnValue)323{324ACPI_BIT_REGISTER_INFO *BitRegInfo;325UINT32 RegisterValue;326UINT32 Value;327ACPI_STATUS Status;328329330ACPI_FUNCTION_TRACE_U32 (AcpiReadBitRegister, RegisterId);331332333/* Get the info structure corresponding to the requested ACPI Register */334335BitRegInfo = AcpiHwGetBitRegisterInfo (RegisterId);336if (!BitRegInfo)337{338return_ACPI_STATUS (AE_BAD_PARAMETER);339}340341/* Read the entire parent register */342343Status = AcpiHwRegisterRead (BitRegInfo->ParentRegister,344&RegisterValue);345if (ACPI_FAILURE (Status))346{347return_ACPI_STATUS (Status);348}349350/* Normalize the value that was read, mask off other bits */351352Value = ((RegisterValue & BitRegInfo->AccessBitMask)353>> BitRegInfo->BitPosition);354355ACPI_DEBUG_PRINT ((ACPI_DB_IO,356"BitReg %X, ParentReg %X, Actual %8.8X, ReturnValue %8.8X\n",357RegisterId, BitRegInfo->ParentRegister, RegisterValue, Value));358359*ReturnValue = Value;360return_ACPI_STATUS (AE_OK);361}362363ACPI_EXPORT_SYMBOL (AcpiReadBitRegister)364365366/*******************************************************************************367*368* FUNCTION: AcpiWriteBitRegister369*370* PARAMETERS: RegisterId - ID of ACPI Bit Register to access371* Value - Value to write to the register, in bit372* position zero. The bit is automatically373* shifted to the correct position.374*375* RETURN: Status376*377* DESCRIPTION: ACPI Bit Register write function. Acquires the hardware lock378* since most operations require a read/modify/write sequence.379*380* SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and381* PM2 Control.382*383* Note that at this level, the fact that there may be actually two384* hardware registers (A and B - and B may not exist) is abstracted.385*386******************************************************************************/387388ACPI_STATUS389AcpiWriteBitRegister (390UINT32 RegisterId,391UINT32 Value)392{393ACPI_BIT_REGISTER_INFO *BitRegInfo;394ACPI_CPU_FLAGS LockFlags;395UINT32 RegisterValue;396ACPI_STATUS Status = AE_OK;397398399ACPI_FUNCTION_TRACE_U32 (AcpiWriteBitRegister, RegisterId);400401402/* Get the info structure corresponding to the requested ACPI Register */403404BitRegInfo = AcpiHwGetBitRegisterInfo (RegisterId);405if (!BitRegInfo)406{407return_ACPI_STATUS (AE_BAD_PARAMETER);408}409410LockFlags = AcpiOsAcquireLock (AcpiGbl_HardwareLock);411412/*413* At this point, we know that the parent register is one of the414* following: PM1 Status, PM1 Enable, PM1 Control, or PM2 Control415*/416if (BitRegInfo->ParentRegister != ACPI_REGISTER_PM1_STATUS)417{418/*419* 1) Case for PM1 Enable, PM1 Control, and PM2 Control420*421* Perform a register read to preserve the bits that we are not422* interested in423*/424Status = AcpiHwRegisterRead (BitRegInfo->ParentRegister,425&RegisterValue);426if (ACPI_FAILURE (Status))427{428goto UnlockAndExit;429}430431/*432* Insert the input bit into the value that was just read433* and write the register434*/435ACPI_REGISTER_INSERT_VALUE (RegisterValue, BitRegInfo->BitPosition,436BitRegInfo->AccessBitMask, Value);437438Status = AcpiHwRegisterWrite (BitRegInfo->ParentRegister,439RegisterValue);440}441else442{443/*444* 2) Case for PM1 Status445*446* The Status register is different from the rest. Clear an event447* by writing 1, writing 0 has no effect. So, the only relevant448* information is the single bit we're interested in, all others449* should be written as 0 so they will be left unchanged.450*/451RegisterValue = ACPI_REGISTER_PREPARE_BITS (Value,452BitRegInfo->BitPosition, BitRegInfo->AccessBitMask);453454/* No need to write the register if value is all zeros */455456if (RegisterValue)457{458Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_STATUS,459RegisterValue);460}461}462463ACPI_DEBUG_PRINT ((ACPI_DB_IO,464"BitReg %X, ParentReg %X, Value %8.8X, Actual %8.8X\n",465RegisterId, BitRegInfo->ParentRegister, Value, RegisterValue));466467468UnlockAndExit:469470AcpiOsReleaseLock (AcpiGbl_HardwareLock, LockFlags);471return_ACPI_STATUS (Status);472}473474ACPI_EXPORT_SYMBOL (AcpiWriteBitRegister)475476#endif /* !ACPI_REDUCED_HARDWARE */477478479/*******************************************************************************480*481* FUNCTION: AcpiGetSleepTypeData482*483* PARAMETERS: SleepState - Numeric sleep state484* *SleepTypeA - Where SLP_TYPa is returned485* *SleepTypeB - Where SLP_TYPb is returned486*487* RETURN: Status488*489* DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested490* sleep state via the appropriate \_Sx object.491*492* The sleep state package returned from the corresponding \_Sx_ object493* must contain at least one integer.494*495* March 2005:496* Added support for a package that contains two integers. This497* goes against the ACPI specification which defines this object as a498* package with one encoded DWORD integer. However, existing practice499* by many BIOS vendors is to return a package with 2 or more integer500* elements, at least one per sleep type (A/B).501*502* January 2013:503* Therefore, we must be prepared to accept a package with either a504* single integer or multiple integers.505*506* The single integer DWORD format is as follows:507* BYTE 0 - Value for the PM1A SLP_TYP register508* BYTE 1 - Value for the PM1B SLP_TYP register509* BYTE 2-3 - Reserved510*511* The dual integer format is as follows:512* Integer 0 - Value for the PM1A SLP_TYP register513* Integer 1 - Value for the PM1A SLP_TYP register514*515******************************************************************************/516517ACPI_STATUS518AcpiGetSleepTypeData (519UINT8 SleepState,520UINT8 *SleepTypeA,521UINT8 *SleepTypeB)522{523ACPI_STATUS Status;524ACPI_EVALUATE_INFO *Info;525ACPI_OPERAND_OBJECT **Elements;526527528ACPI_FUNCTION_TRACE (AcpiGetSleepTypeData);529530531/* Validate parameters */532533if ((SleepState > ACPI_S_STATES_MAX) ||534!SleepTypeA || !SleepTypeB)535{536return_ACPI_STATUS (AE_BAD_PARAMETER);537}538539/* Allocate the evaluation information block */540541Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));542if (!Info)543{544return_ACPI_STATUS (AE_NO_MEMORY);545}546547/*548* Evaluate the \_Sx namespace object containing the register values549* for this state550*/551Info->RelativePathname = AcpiGbl_SleepStateNames[SleepState];552553Status = AcpiNsEvaluate (Info);554if (ACPI_FAILURE (Status))555{556if (Status == AE_NOT_FOUND)557{558/* The _Sx states are optional, ignore NOT_FOUND */559560goto FinalCleanup;561}562563goto WarningCleanup;564}565566/* Must have a return object */567568if (!Info->ReturnObject)569{570ACPI_ERROR ((AE_INFO, "No Sleep State object returned from [%s]",571Info->RelativePathname));572Status = AE_AML_NO_RETURN_VALUE;573goto WarningCleanup;574}575576/* Return object must be of type Package */577578if (Info->ReturnObject->Common.Type != ACPI_TYPE_PACKAGE)579{580ACPI_ERROR ((AE_INFO, "Sleep State return object is not a Package"));581Status = AE_AML_OPERAND_TYPE;582goto ReturnValueCleanup;583}584585/*586* Any warnings about the package length or the object types have587* already been issued by the predefined name module -- there is no588* need to repeat them here.589*/590Elements = Info->ReturnObject->Package.Elements;591switch (Info->ReturnObject->Package.Count)592{593case 0:594595Status = AE_AML_PACKAGE_LIMIT;596break;597598case 1:599600if (Elements[0]->Common.Type != ACPI_TYPE_INTEGER)601{602Status = AE_AML_OPERAND_TYPE;603break;604}605606/* A valid _Sx_ package with one integer */607608*SleepTypeA = (UINT8) Elements[0]->Integer.Value;609*SleepTypeB = (UINT8) (Elements[0]->Integer.Value >> 8);610break;611612case 2:613default:614615if ((Elements[0]->Common.Type != ACPI_TYPE_INTEGER) ||616(Elements[1]->Common.Type != ACPI_TYPE_INTEGER))617{618Status = AE_AML_OPERAND_TYPE;619break;620}621622/* A valid _Sx_ package with two integers */623624*SleepTypeA = (UINT8) Elements[0]->Integer.Value;625*SleepTypeB = (UINT8) Elements[1]->Integer.Value;626break;627}628629ReturnValueCleanup:630AcpiUtRemoveReference (Info->ReturnObject);631632WarningCleanup:633if (ACPI_FAILURE (Status))634{635ACPI_EXCEPTION ((AE_INFO, Status,636"While evaluating Sleep State [%s]",637Info->RelativePathname));638}639640FinalCleanup:641ACPI_FREE (Info);642return_ACPI_STATUS (Status);643}644645ACPI_EXPORT_SYMBOL (AcpiGetSleepTypeData)646647648