Path: blob/main/sys/contrib/dev/acpica/components/executer/exfield.c
48521 views
/******************************************************************************1*2* Module Name: exfield - AML execution - FieldUnit read/write3*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/acdispat.h>154#include <contrib/dev/acpica/include/acinterp.h>155#include <contrib/dev/acpica/include/amlcode.h>156157158#define _COMPONENT ACPI_EXECUTER159ACPI_MODULE_NAME ("exfield")160161162/*163* This table maps the various Attrib protocols to the byte transfer164* length. Used for the generic serial bus.165*/166#define ACPI_INVALID_PROTOCOL_ID 0x80167#define ACPI_MAX_PROTOCOL_ID 0x0F168169static const UINT8 AcpiProtocolLengths[] =170{171ACPI_INVALID_PROTOCOL_ID, /* 0 - reserved */172ACPI_INVALID_PROTOCOL_ID, /* 1 - reserved */1730x00, /* 2 - ATTRIB_QUICK */174ACPI_INVALID_PROTOCOL_ID, /* 3 - reserved */1750x01, /* 4 - ATTRIB_SEND_RECEIVE */176ACPI_INVALID_PROTOCOL_ID, /* 5 - reserved */1770x01, /* 6 - ATTRIB_BYTE */178ACPI_INVALID_PROTOCOL_ID, /* 7 - reserved */1790x02, /* 8 - ATTRIB_WORD */180ACPI_INVALID_PROTOCOL_ID, /* 9 - reserved */1810xFF, /* A - ATTRIB_BLOCK */1820xFF, /* B - ATTRIB_BYTES */1830x02, /* C - ATTRIB_PROCESS_CALL */1840xFF, /* D - ATTRIB_BLOCK_PROCESS_CALL */1850xFF, /* E - ATTRIB_RAW_BYTES */1860xFF /* F - ATTRIB_RAW_PROCESS_BYTES */187};188189#define PCC_MASTER_SUBSPACE 3190191/*192* The following macros determine a given offset is a COMD field.193* According to the specification, generic subspaces (types 0-2) contains a194* 2-byte COMD field at offset 4 and master subspaces (type 3) contains a 4-byte195* COMD field starting at offset 12.196*/197#define GENERIC_SUBSPACE_COMMAND(a) (4 == a || a == 5)198#define MASTER_SUBSPACE_COMMAND(a) (12 <= a && a <= 15)199200201/*******************************************************************************202*203* FUNCTION: AcpiExGetProtocolBufferLength204*205* PARAMETERS: ProtocolId - The type of the protocol indicated by region206* field access attributes207* ReturnLength - Where the protocol byte transfer length is208* returned209*210* RETURN: Status and decoded byte transfer length211*212* DESCRIPTION: This routine returns the length of the GenericSerialBus213* protocol bytes214*215******************************************************************************/216217ACPI_STATUS218AcpiExGetProtocolBufferLength (219UINT32 ProtocolId,220UINT32 *ReturnLength)221{222223if ((ProtocolId > ACPI_MAX_PROTOCOL_ID) ||224(AcpiProtocolLengths[ProtocolId] == ACPI_INVALID_PROTOCOL_ID))225{226ACPI_ERROR ((AE_INFO,227"Invalid Field/AccessAs protocol ID: 0x%4.4X", ProtocolId));228229return (AE_AML_PROTOCOL);230}231232*ReturnLength = AcpiProtocolLengths[ProtocolId];233return (AE_OK);234}235236237/*******************************************************************************238*239* FUNCTION: AcpiExReadDataFromField240*241* PARAMETERS: WalkState - Current execution state242* ObjDesc - The named field243* RetBufferDesc - Where the return data object is stored244*245* RETURN: Status246*247* DESCRIPTION: Read from a named field. Returns either an Integer or a248* Buffer, depending on the size of the field and whether if a249* field is created by the CreateField() operator.250*251******************************************************************************/252253ACPI_STATUS254AcpiExReadDataFromField (255ACPI_WALK_STATE *WalkState,256ACPI_OPERAND_OBJECT *ObjDesc,257ACPI_OPERAND_OBJECT **RetBufferDesc)258{259ACPI_STATUS Status;260ACPI_OPERAND_OBJECT *BufferDesc;261void *Buffer;262UINT32 BufferLength;263264265ACPI_FUNCTION_TRACE_PTR (ExReadDataFromField, ObjDesc);266267268/* Parameter validation */269270if (!ObjDesc)271{272return_ACPI_STATUS (AE_AML_NO_OPERAND);273}274if (!RetBufferDesc)275{276return_ACPI_STATUS (AE_BAD_PARAMETER);277}278279if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)280{281/*282* If the BufferField arguments have not been previously evaluated,283* evaluate them now and save the results.284*/285if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))286{287Status = AcpiDsGetBufferFieldArguments (ObjDesc);288if (ACPI_FAILURE (Status))289{290return_ACPI_STATUS (Status);291}292}293}294else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&295(ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||296ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||297ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI ||298ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_PLATFORM_RT ||299ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_FIXED_HARDWARE))300{301/* SMBus, GSBus, IPMI serial */302303Status = AcpiExReadSerialBus (ObjDesc, RetBufferDesc);304return_ACPI_STATUS (Status);305}306307/*308* Allocate a buffer for the contents of the field.309*310* If the field is larger than the current integer width, create311* a BUFFER to hold it. Otherwise, use an INTEGER. This allows312* the use of arithmetic operators on the returned value if the313* field size is equal or smaller than an Integer.314*315* However, all buffer fields created by CreateField operator needs to316* remain as a buffer to match other AML interpreter implementations.317*318* Note: Field.length is in bits.319*/320BufferLength = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (321ObjDesc->Field.BitLength);322323if (BufferLength > AcpiGbl_IntegerByteWidth ||324(ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD &&325ObjDesc->BufferField.IsCreateField))326{327/* Field is too large for an Integer, create a Buffer instead */328329BufferDesc = AcpiUtCreateBufferObject (BufferLength);330if (!BufferDesc)331{332return_ACPI_STATUS (AE_NO_MEMORY);333}334Buffer = BufferDesc->Buffer.Pointer;335}336else337{338/* Field will fit within an Integer (normal case) */339340BufferDesc = AcpiUtCreateIntegerObject ((UINT64) 0);341if (!BufferDesc)342{343return_ACPI_STATUS (AE_NO_MEMORY);344}345346BufferLength = AcpiGbl_IntegerByteWidth;347Buffer = &BufferDesc->Integer.Value;348}349350if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&351(ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))352{353/* General Purpose I/O */354355Status = AcpiExReadGpio (ObjDesc, Buffer);356goto Exit;357}358else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&359(ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_PLATFORM_COMM))360{361/*362* Reading from a PCC field unit does not require the handler because363* it only requires reading from the InternalPccBuffer.364*/365ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,366"PCC FieldRead bits %u\n", ObjDesc->Field.BitLength));367368memcpy (Buffer, ObjDesc->Field.RegionObj->Field.InternalPccBuffer +369ObjDesc->Field.BaseByteOffset, (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (370ObjDesc->Field.BitLength));371372*RetBufferDesc = BufferDesc;373return AE_OK;374}375376ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,377"FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",378ObjDesc, ObjDesc->Common.Type, Buffer, BufferLength));379ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,380"FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",381ObjDesc->CommonField.BitLength,382ObjDesc->CommonField.StartFieldBitOffset,383ObjDesc->CommonField.BaseByteOffset));384385/* Lock entire transaction if requested */386387AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);388389/* Read from the field */390391Status = AcpiExExtractFromField (ObjDesc, Buffer, BufferLength);392AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);393394395Exit:396if (ACPI_FAILURE (Status))397{398AcpiUtRemoveReference (BufferDesc);399}400else401{402*RetBufferDesc = BufferDesc;403}404405return_ACPI_STATUS (Status);406}407408409/*******************************************************************************410*411* FUNCTION: AcpiExWriteDataToField412*413* PARAMETERS: SourceDesc - Contains data to write414* ObjDesc - The named field415* ResultDesc - Where the return value is returned, if any416*417* RETURN: Status418*419* DESCRIPTION: Write to a named field420*421******************************************************************************/422423ACPI_STATUS424AcpiExWriteDataToField (425ACPI_OPERAND_OBJECT *SourceDesc,426ACPI_OPERAND_OBJECT *ObjDesc,427ACPI_OPERAND_OBJECT **ResultDesc)428{429ACPI_STATUS Status;430UINT32 BufferLength;431UINT32 DataLength;432void *Buffer;433434435ACPI_FUNCTION_TRACE_PTR (ExWriteDataToField, ObjDesc);436437438/* Parameter validation */439440if (!SourceDesc || !ObjDesc)441{442return_ACPI_STATUS (AE_AML_NO_OPERAND);443}444445if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)446{447/*448* If the BufferField arguments have not been previously evaluated,449* evaluate them now and save the results.450*/451if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))452{453Status = AcpiDsGetBufferFieldArguments (ObjDesc);454if (ACPI_FAILURE (Status))455{456return_ACPI_STATUS (Status);457}458}459}460else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&461(ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))462{463/* General Purpose I/O */464465Status = AcpiExWriteGpio (SourceDesc, ObjDesc, ResultDesc);466return_ACPI_STATUS (Status);467}468else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&469(ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||470ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||471ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI ||472ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_PLATFORM_RT ||473ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_FIXED_HARDWARE))474{475/* SMBus, GSBus, IPMI serial */476477Status = AcpiExWriteSerialBus (SourceDesc, ObjDesc, ResultDesc);478return_ACPI_STATUS (Status);479}480else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&481(ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_PLATFORM_COMM))482{483/*484* According to the spec a write to the COMD field will invoke the485* region handler. Otherwise, write to the PccInternal buffer. This486* implementation will use the offsets specified rather than the name487* of the field. This is considered safer because some firmware tools488* are known to obfiscate named objects.489*/490DataLength = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (491ObjDesc->Field.BitLength);492memcpy (ObjDesc->Field.RegionObj->Field.InternalPccBuffer +493ObjDesc->Field.BaseByteOffset,494SourceDesc->Buffer.Pointer, DataLength);495496if (MASTER_SUBSPACE_COMMAND (ObjDesc->Field.BaseByteOffset))497{498/* Perform the write */499500ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,501"PCC COMD field has been written. Invoking PCC handler now.\n"));502503Status = AcpiExAccessRegion (504ObjDesc, 0, (UINT64 *) ObjDesc->Field.RegionObj->Field.InternalPccBuffer,505ACPI_WRITE);506return_ACPI_STATUS (Status);507}508return (AE_OK);509}510511512/* Get a pointer to the data to be written */513514switch (SourceDesc->Common.Type)515{516case ACPI_TYPE_INTEGER:517518Buffer = &SourceDesc->Integer.Value;519BufferLength = sizeof (SourceDesc->Integer.Value);520break;521522case ACPI_TYPE_BUFFER:523524Buffer = SourceDesc->Buffer.Pointer;525BufferLength = SourceDesc->Buffer.Length;526break;527528case ACPI_TYPE_STRING:529530Buffer = SourceDesc->String.Pointer;531BufferLength = SourceDesc->String.Length;532break;533534default:535return_ACPI_STATUS (AE_AML_OPERAND_TYPE);536}537538ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,539"FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",540SourceDesc, AcpiUtGetTypeName (SourceDesc->Common.Type),541SourceDesc->Common.Type, Buffer, BufferLength));542543ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,544"FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",545ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type),546ObjDesc->Common.Type,547ObjDesc->CommonField.BitLength,548ObjDesc->CommonField.StartFieldBitOffset,549ObjDesc->CommonField.BaseByteOffset));550551/* Lock entire transaction if requested */552553AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);554555/* Write to the field */556557Status = AcpiExInsertIntoField (ObjDesc, Buffer, BufferLength);558AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);559return_ACPI_STATUS (Status);560}561562563