Path: blob/main/sys/contrib/dev/acpica/components/executer/exserial.c
48521 views
/******************************************************************************1*2* Module Name: exserial - FieldUnit support for serial address spaces3*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 ("exserial")160161162/*******************************************************************************163*164* FUNCTION: AcpiExReadGpio165*166* PARAMETERS: ObjDesc - The named field to read167* Buffer - Where the return data is returned168*169* RETURN: Status170*171* DESCRIPTION: Read from a named field that references a Generic Serial Bus172* field173*174******************************************************************************/175176ACPI_STATUS177AcpiExReadGpio (178ACPI_OPERAND_OBJECT *ObjDesc,179void *Buffer)180{181ACPI_STATUS Status;182183184ACPI_FUNCTION_TRACE_PTR (ExReadGpio, ObjDesc);185186187/*188* For GPIO (GeneralPurposeIo), the Address will be the bit offset189* from the previous Connection() operator, making it effectively a190* pin number index. The BitLength is the length of the field, which191* is thus the number of pins.192*/193ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,194"GPIO FieldRead [FROM]: Pin %u Bits %u\n",195ObjDesc->Field.PinNumberIndex, ObjDesc->Field.BitLength));196197/* Lock entire transaction if requested */198199AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);200201/* Perform the read */202203Status = AcpiExAccessRegion (204ObjDesc, 0, (UINT64 *) Buffer, ACPI_READ);205206AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);207return_ACPI_STATUS (Status);208}209210211/*******************************************************************************212*213* FUNCTION: AcpiExWriteGpio214*215* PARAMETERS: SourceDesc - Contains data to write. Expect to be216* an Integer object.217* ObjDesc - The named field218* ResultDesc - Where the return value is returned, if any219*220* RETURN: Status221*222* DESCRIPTION: Write to a named field that references a General Purpose I/O223* field.224*225******************************************************************************/226227ACPI_STATUS228AcpiExWriteGpio (229ACPI_OPERAND_OBJECT *SourceDesc,230ACPI_OPERAND_OBJECT *ObjDesc,231ACPI_OPERAND_OBJECT **ReturnBuffer)232{233ACPI_STATUS Status;234void *Buffer;235236237ACPI_FUNCTION_TRACE_PTR (ExWriteGpio, ObjDesc);238239240/*241* For GPIO (GeneralPurposeIo), we will bypass the entire field242* mechanism and handoff the bit address and bit width directly to243* the handler. The Address will be the bit offset244* from the previous Connection() operator, making it effectively a245* pin number index. The BitLength is the length of the field, which246* is thus the number of pins.247*/248if (SourceDesc->Common.Type != ACPI_TYPE_INTEGER)249{250return_ACPI_STATUS (AE_AML_OPERAND_TYPE);251}252253ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,254"GPIO FieldWrite [FROM]: (%s:%X), Value %.8X [TO]: Pin %u Bits %u\n",255AcpiUtGetTypeName (SourceDesc->Common.Type),256SourceDesc->Common.Type, (UINT32) SourceDesc->Integer.Value,257ObjDesc->Field.PinNumberIndex, ObjDesc->Field.BitLength));258259Buffer = &SourceDesc->Integer.Value;260261/* Lock entire transaction if requested */262263AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);264265/* Perform the write */266267Status = AcpiExAccessRegion (268ObjDesc, 0, (UINT64 *) Buffer, ACPI_WRITE);269AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);270return_ACPI_STATUS (Status);271}272273274/*******************************************************************************275*276* FUNCTION: AcpiExReadSerialBus277*278* PARAMETERS: ObjDesc - The named field to read279* ReturnBuffer - Where the return value is returned, if any280*281* RETURN: Status282*283* DESCRIPTION: Read from a named field that references a serial bus284* (SMBus, IPMI, or GSBus).285*286******************************************************************************/287288ACPI_STATUS289AcpiExReadSerialBus (290ACPI_OPERAND_OBJECT *ObjDesc,291ACPI_OPERAND_OBJECT **ReturnBuffer)292{293ACPI_STATUS Status;294UINT32 BufferLength;295ACPI_OPERAND_OBJECT *BufferDesc;296UINT32 Function;297UINT16 AccessorType;298299300ACPI_FUNCTION_TRACE_PTR (ExReadSerialBus, ObjDesc);301302303/*304* This is an SMBus, GSBus or IPMI read. We must create a buffer to305* hold the data and then directly access the region handler.306*307* Note: SMBus and GSBus protocol value is passed in upper 16-bits308* of Function309*310* Common buffer format:311* Status; (Byte 0 of the data buffer)312* Length; (Byte 1 of the data buffer)313* Data[x-1]: (Bytes 2-x of the arbitrary length data buffer)314*/315switch (ObjDesc->Field.RegionObj->Region.SpaceId)316{317case ACPI_ADR_SPACE_SMBUS:318319BufferLength = ACPI_SMBUS_BUFFER_SIZE;320Function = ACPI_READ | (ObjDesc->Field.Attribute << 16);321break;322323case ACPI_ADR_SPACE_IPMI:324325BufferLength = ACPI_IPMI_BUFFER_SIZE;326Function = ACPI_READ;327break;328329case ACPI_ADR_SPACE_GSBUS:330331AccessorType = ObjDesc->Field.Attribute;332if (AccessorType == AML_FIELD_ATTRIB_RAW_PROCESS_BYTES)333{334ACPI_ERROR ((AE_INFO,335"Invalid direct read using bidirectional write-then-read protocol"));336337return_ACPI_STATUS (AE_AML_PROTOCOL);338}339340Status = AcpiExGetProtocolBufferLength (AccessorType, &BufferLength);341if (ACPI_FAILURE (Status))342{343ACPI_ERROR ((AE_INFO,344"Invalid protocol ID for GSBus: 0x%4.4X", AccessorType));345346return_ACPI_STATUS (Status);347}348349/* Add header length to get the full size of the buffer */350351BufferLength += ACPI_SERIAL_HEADER_SIZE;352Function = ACPI_READ | (AccessorType << 16);353break;354355case ACPI_ADR_SPACE_PLATFORM_RT:356357BufferLength = ACPI_PRM_INPUT_BUFFER_SIZE;358Function = ACPI_READ;359break;360361case ACPI_ADR_SPACE_FIXED_HARDWARE:362363BufferLength = ACPI_FFH_INPUT_BUFFER_SIZE;364Function = ACPI_READ;365break;366367default:368return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);369}370371/* Create the local transfer buffer that is returned to the caller */372373BufferDesc = AcpiUtCreateBufferObject (BufferLength);374if (!BufferDesc)375{376return_ACPI_STATUS (AE_NO_MEMORY);377}378379/* Lock entire transaction if requested */380381AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);382383/* Call the region handler for the write-then-read */384385Status = AcpiExAccessRegion (ObjDesc, 0,386ACPI_CAST_PTR (UINT64, BufferDesc->Buffer.Pointer), Function);387AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);388389*ReturnBuffer = BufferDesc;390return_ACPI_STATUS (Status);391}392393394/*******************************************************************************395*396* FUNCTION: AcpiExWriteSerialBus397*398* PARAMETERS: SourceDesc - Contains data to write399* ObjDesc - The named field400* ReturnBuffer - Where the return value is returned, if any401*402* RETURN: Status403*404* DESCRIPTION: Write to a named field that references a serial bus405* (SMBus, IPMI, GSBus).406*407******************************************************************************/408409ACPI_STATUS410AcpiExWriteSerialBus (411ACPI_OPERAND_OBJECT *SourceDesc,412ACPI_OPERAND_OBJECT *ObjDesc,413ACPI_OPERAND_OBJECT **ReturnBuffer)414{415ACPI_STATUS Status;416UINT32 BufferLength;417UINT32 DataLength;418void *Buffer;419ACPI_OPERAND_OBJECT *BufferDesc;420UINT32 Function;421UINT16 AccessorType;422423424ACPI_FUNCTION_TRACE_PTR (ExWriteSerialBus, ObjDesc);425426427/*428* This is an SMBus, GSBus or IPMI write. We will bypass the entire429* field mechanism and handoff the buffer directly to the handler.430* For these address spaces, the buffer is bidirectional; on a431* write, return data is returned in the same buffer.432*433* Source must be a buffer of sufficient size, these are fixed size:434* ACPI_SMBUS_BUFFER_SIZE, or ACPI_IPMI_BUFFER_SIZE.435*436* Note: SMBus and GSBus protocol type is passed in upper 16-bits437* of Function438*439* Common buffer format:440* Status; (Byte 0 of the data buffer)441* Length; (Byte 1 of the data buffer)442* Data[x-1]: (Bytes 2-x of the arbitrary length data buffer)443*/444if (SourceDesc->Common.Type != ACPI_TYPE_BUFFER)445{446ACPI_ERROR ((AE_INFO,447"SMBus/IPMI/GenericSerialBus write requires "448"Buffer, found type %s",449AcpiUtGetObjectTypeName (SourceDesc)));450451return_ACPI_STATUS (AE_AML_OPERAND_TYPE);452}453454switch (ObjDesc->Field.RegionObj->Region.SpaceId)455{456case ACPI_ADR_SPACE_SMBUS:457458BufferLength = ACPI_SMBUS_BUFFER_SIZE;459Function = ACPI_WRITE | (ObjDesc->Field.Attribute << 16);460break;461462case ACPI_ADR_SPACE_IPMI:463464BufferLength = ACPI_IPMI_BUFFER_SIZE;465Function = ACPI_WRITE;466break;467468case ACPI_ADR_SPACE_GSBUS:469470AccessorType = ObjDesc->Field.Attribute;471Status = AcpiExGetProtocolBufferLength (AccessorType, &BufferLength);472if (ACPI_FAILURE (Status))473{474ACPI_ERROR ((AE_INFO,475"Invalid protocol ID for GSBus: 0x%4.4X", AccessorType));476477return_ACPI_STATUS (Status);478}479480/* Add header length to get the full size of the buffer */481482BufferLength += ACPI_SERIAL_HEADER_SIZE;483Function = ACPI_WRITE | (AccessorType << 16);484break;485486case ACPI_ADR_SPACE_PLATFORM_RT:487488BufferLength = ACPI_PRM_INPUT_BUFFER_SIZE;489Function = ACPI_WRITE;490break;491492case ACPI_ADR_SPACE_FIXED_HARDWARE:493494BufferLength = ACPI_FFH_INPUT_BUFFER_SIZE;495Function = ACPI_WRITE;496break;497498default:499return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);500}501502/* Create the transfer/bidirectional/return buffer */503504BufferDesc = AcpiUtCreateBufferObject (BufferLength);505if (!BufferDesc)506{507return_ACPI_STATUS (AE_NO_MEMORY);508}509510/* Copy the input buffer data to the transfer buffer */511512Buffer = BufferDesc->Buffer.Pointer;513DataLength = ACPI_MIN (BufferLength, SourceDesc->Buffer.Length);514memcpy (Buffer, SourceDesc->Buffer.Pointer, DataLength);515516/* Lock entire transaction if requested */517518AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);519520/*521* Perform the write (returns status and perhaps data in the522* same buffer)523*/524Status = AcpiExAccessRegion (525ObjDesc, 0, (UINT64 *) Buffer, Function);526AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);527528*ReturnBuffer = BufferDesc;529return_ACPI_STATUS (Status);530}531532533