Path: blob/main/sys/contrib/dev/acpica/components/resources/rscreate.c
48406 views
/*******************************************************************************1*2* Module Name: rscreate - Create resource lists/tables3*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/acresrc.h>154#include <contrib/dev/acpica/include/acnamesp.h>155156#define _COMPONENT ACPI_RESOURCES157ACPI_MODULE_NAME ("rscreate")158159160/*******************************************************************************161*162* FUNCTION: AcpiBufferToResource163*164* PARAMETERS: AmlBuffer - Pointer to the resource byte stream165* AmlBufferLength - Length of the AmlBuffer166* ResourcePtr - Where the converted resource is returned167*168* RETURN: Status169*170* DESCRIPTION: Convert a raw AML buffer to a resource list171*172******************************************************************************/173174ACPI_STATUS175AcpiBufferToResource (176UINT8 *AmlBuffer,177UINT16 AmlBufferLength,178ACPI_RESOURCE **ResourcePtr)179{180ACPI_STATUS Status;181ACPI_SIZE ListSizeNeeded;182void *Resource;183void *CurrentResourcePtr;184185186ACPI_FUNCTION_TRACE (AcpiBufferToResource);187188189/*190* Note: we allow AE_AML_NO_RESOURCE_END_TAG, since an end tag191* is not required here.192*/193194/* Get the required length for the converted resource */195196Status = AcpiRsGetListLength (197AmlBuffer, AmlBufferLength, &ListSizeNeeded);198if (Status == AE_AML_NO_RESOURCE_END_TAG)199{200Status = AE_OK;201}202if (ACPI_FAILURE (Status))203{204return_ACPI_STATUS (Status);205}206207/* Allocate a buffer for the converted resource */208209Resource = ACPI_ALLOCATE_ZEROED (ListSizeNeeded);210CurrentResourcePtr = Resource;211if (!Resource)212{213return_ACPI_STATUS (AE_NO_MEMORY);214}215216/* Perform the AML-to-Resource conversion */217218Status = AcpiUtWalkAmlResources (NULL, AmlBuffer, AmlBufferLength,219AcpiRsConvertAmlToResources, &CurrentResourcePtr);220if (Status == AE_AML_NO_RESOURCE_END_TAG)221{222Status = AE_OK;223}224if (ACPI_FAILURE (Status))225{226ACPI_FREE (Resource);227}228else229{230*ResourcePtr = Resource;231}232233return_ACPI_STATUS (Status);234}235236ACPI_EXPORT_SYMBOL (AcpiBufferToResource)237238239/*******************************************************************************240*241* FUNCTION: AcpiRsCreateResourceList242*243* PARAMETERS: AmlBuffer - Pointer to the resource byte stream244* OutputBuffer - Pointer to the user's buffer245*246* RETURN: Status: AE_OK if okay, else a valid ACPI_STATUS code247* If OutputBuffer is not large enough, OutputBufferLength248* indicates how large OutputBuffer should be, else it249* indicates how may UINT8 elements of OutputBuffer are valid.250*251* DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method252* execution and parses the stream to create a linked list253* of device resources.254*255******************************************************************************/256257ACPI_STATUS258AcpiRsCreateResourceList (259ACPI_OPERAND_OBJECT *AmlBuffer,260ACPI_BUFFER *OutputBuffer)261{262263ACPI_STATUS Status;264UINT8 *AmlStart;265ACPI_SIZE ListSizeNeeded = 0;266UINT32 AmlBufferLength;267void *Resource;268269270ACPI_FUNCTION_TRACE (RsCreateResourceList);271272273ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlBuffer = %p\n",274AmlBuffer));275276/* Params already validated, so we don't re-validate here */277278AmlBufferLength = AmlBuffer->Buffer.Length;279AmlStart = AmlBuffer->Buffer.Pointer;280281/*282* Pass the AmlBuffer into a module that can calculate283* the buffer size needed for the linked list284*/285Status = AcpiRsGetListLength (AmlStart, AmlBufferLength,286&ListSizeNeeded);287288ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Status=%X ListSizeNeeded=%X\n",289Status, (UINT32) ListSizeNeeded));290if (ACPI_FAILURE (Status))291{292return_ACPI_STATUS (Status);293}294295/* Validate/Allocate/Clear caller buffer */296297Status = AcpiUtInitializeBuffer (OutputBuffer, ListSizeNeeded);298if (ACPI_FAILURE (Status))299{300return_ACPI_STATUS (Status);301}302303/* Do the conversion */304305Resource = OutputBuffer->Pointer;306Status = AcpiUtWalkAmlResources (NULL, AmlStart, AmlBufferLength,307AcpiRsConvertAmlToResources, &Resource);308if (ACPI_FAILURE (Status))309{310return_ACPI_STATUS (Status);311}312313ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",314OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));315return_ACPI_STATUS (AE_OK);316}317318319/*******************************************************************************320*321* FUNCTION: AcpiRsCreatePciRoutingTable322*323* PARAMETERS: PackageObject - Pointer to a package containing one324* of more ACPI_OPERAND_OBJECTs325* OutputBuffer - Pointer to the user's buffer326*327* RETURN: Status AE_OK if okay, else a valid ACPI_STATUS code.328* If the OutputBuffer is too small, the error will be329* AE_BUFFER_OVERFLOW and OutputBuffer->Length will point330* to the size buffer needed.331*332* DESCRIPTION: Takes the ACPI_OPERAND_OBJECT package and creates a333* linked list of PCI interrupt descriptions334*335* NOTE: It is the caller's responsibility to ensure that the start of the336* output buffer is aligned properly (if necessary).337*338******************************************************************************/339340ACPI_STATUS341AcpiRsCreatePciRoutingTable (342ACPI_OPERAND_OBJECT *PackageObject,343ACPI_BUFFER *OutputBuffer)344{345UINT8 *Buffer;346ACPI_OPERAND_OBJECT **TopObjectList;347ACPI_OPERAND_OBJECT **SubObjectList;348ACPI_OPERAND_OBJECT *ObjDesc;349ACPI_SIZE BufferSizeNeeded = 0;350UINT32 NumberOfElements;351UINT32 Index;352ACPI_PCI_ROUTING_TABLE *UserPrt;353ACPI_NAMESPACE_NODE *Node;354ACPI_STATUS Status;355ACPI_BUFFER PathBuffer;356357358ACPI_FUNCTION_TRACE (RsCreatePciRoutingTable);359360361/* Params already validated, so we don't re-validate here */362363/* Get the required buffer length */364365Status = AcpiRsGetPciRoutingTableLength (366PackageObject,&BufferSizeNeeded);367if (ACPI_FAILURE (Status))368{369return_ACPI_STATUS (Status);370}371372ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "BufferSizeNeeded = %X\n",373(UINT32) BufferSizeNeeded));374375/* Validate/Allocate/Clear caller buffer */376377Status = AcpiUtInitializeBuffer (OutputBuffer, BufferSizeNeeded);378if (ACPI_FAILURE (Status))379{380return_ACPI_STATUS (Status);381}382383/*384* Loop through the ACPI_INTERNAL_OBJECTS - Each object should be a385* package that in turn contains an UINT64 Address, a UINT8 Pin,386* a Name, and a UINT8 SourceIndex.387*/388TopObjectList = PackageObject->Package.Elements;389NumberOfElements = PackageObject->Package.Count;390Buffer = OutputBuffer->Pointer;391UserPrt = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);392393for (Index = 0; Index < NumberOfElements; Index++)394{395/*396* Point UserPrt past this current structure397*398* NOTE: On the first iteration, UserPrt->Length will399* be zero because we cleared the return buffer earlier400*/401Buffer += UserPrt->Length;402UserPrt = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);403404/*405* Fill in the Length field with the information we have at this406* point. The minus four is to subtract the size of the UINT8407* Source[4] member because it is added below.408*/409UserPrt->Length = (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);410411/* Each subpackage must be of length 4 */412413if ((*TopObjectList)->Package.Count != 4)414{415ACPI_ERROR ((AE_INFO,416"(PRT[%u]) Need package of length 4, found length %u",417Index, (*TopObjectList)->Package.Count));418return_ACPI_STATUS (AE_AML_PACKAGE_LIMIT);419}420421/*422* Dereference the subpackage.423* The SubObjectList will now point to an array of the four IRQ424* elements: [Address, Pin, Source, SourceIndex]425*/426SubObjectList = (*TopObjectList)->Package.Elements;427428/* 1) First subobject: Dereference the PRT.Address */429430ObjDesc = SubObjectList[0];431if (!ObjDesc || ObjDesc->Common.Type != ACPI_TYPE_INTEGER)432{433ACPI_ERROR ((AE_INFO,434"(PRT[%u].Address) Need Integer, found %s",435Index, AcpiUtGetObjectTypeName (ObjDesc)));436return_ACPI_STATUS (AE_BAD_DATA);437}438439UserPrt->Address = ObjDesc->Integer.Value;440441/* 2) Second subobject: Dereference the PRT.Pin */442443ObjDesc = SubObjectList[1];444if (!ObjDesc || ObjDesc->Common.Type != ACPI_TYPE_INTEGER)445{446ACPI_ERROR ((AE_INFO, "(PRT[%u].Pin) Need Integer, found %s",447Index, AcpiUtGetObjectTypeName (ObjDesc)));448return_ACPI_STATUS (AE_BAD_DATA);449}450451UserPrt->Pin = (UINT32) ObjDesc->Integer.Value;452453/*454* 3) Third subobject: Dereference the PRT.SourceName455* The name may be unresolved (slack mode), so allow a null object456*/457ObjDesc = SubObjectList[2];458if (ObjDesc)459{460switch (ObjDesc->Common.Type)461{462case ACPI_TYPE_LOCAL_REFERENCE:463464if (ObjDesc->Reference.Class != ACPI_REFCLASS_NAME)465{466ACPI_ERROR ((AE_INFO,467"(PRT[%u].Source) Need name, found Reference Class 0x%X",468Index, ObjDesc->Reference.Class));469return_ACPI_STATUS (AE_BAD_DATA);470}471472Node = ObjDesc->Reference.Node;473474/* Use *remaining* length of the buffer as max for pathname */475476PathBuffer.Length = OutputBuffer->Length -477(UINT32) ((UINT8 *) UserPrt->Source -478(UINT8 *) OutputBuffer->Pointer);479PathBuffer.Pointer = UserPrt->Source;480481Status = AcpiNsHandleToPathname (482(ACPI_HANDLE) Node, &PathBuffer, FALSE);483if (ACPI_FAILURE (Status))484{485return_ACPI_STATUS (Status);486}487488/* +1 to include null terminator */489490UserPrt->Length += (UINT32) strlen (UserPrt->Source) + 1;491break;492493case ACPI_TYPE_STRING:494495strcpy (UserPrt->Source, ObjDesc->String.Pointer);496497/*498* Add to the Length field the length of the string499* (add 1 for terminator)500*/501UserPrt->Length += ObjDesc->String.Length + 1;502break;503504case ACPI_TYPE_INTEGER:505/*506* If this is a number, then the Source Name is NULL, since507* the entire buffer was zeroed out, we can leave this alone.508*509* Add to the Length field the length of the UINT32 NULL510*/511UserPrt->Length += sizeof (UINT32);512break;513514default:515516ACPI_ERROR ((AE_INFO,517"(PRT[%u].Source) Need Ref/String/Integer, found %s",518Index, AcpiUtGetObjectTypeName (ObjDesc)));519return_ACPI_STATUS (AE_BAD_DATA);520}521}522523/* Now align the current length */524525UserPrt->Length = (UINT32) ACPI_ROUND_UP_TO_64BIT (UserPrt->Length);526527/* 4) Fourth subobject: Dereference the PRT.SourceIndex */528529ObjDesc = SubObjectList[3];530if (!ObjDesc || ObjDesc->Common.Type != ACPI_TYPE_INTEGER)531{532ACPI_ERROR ((AE_INFO,533"(PRT[%u].SourceIndex) Need Integer, found %s",534Index, AcpiUtGetObjectTypeName (ObjDesc)));535return_ACPI_STATUS (AE_BAD_DATA);536}537538UserPrt->SourceIndex = (UINT32) ObjDesc->Integer.Value;539540/* Point to the next ACPI_OPERAND_OBJECT in the top level package */541542TopObjectList++;543}544545ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",546OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));547return_ACPI_STATUS (AE_OK);548}549550551/*******************************************************************************552*553* FUNCTION: AcpiRsCreateAmlResources554*555* PARAMETERS: ResourceList - Pointer to the resource list buffer556* OutputBuffer - Where the AML buffer is returned557*558* RETURN: Status AE_OK if okay, else a valid ACPI_STATUS code.559* If the OutputBuffer is too small, the error will be560* AE_BUFFER_OVERFLOW and OutputBuffer->Length will point561* to the size buffer needed.562*563* DESCRIPTION: Converts a list of device resources to an AML bytestream564* to be used as input for the _SRS control method.565*566******************************************************************************/567568ACPI_STATUS569AcpiRsCreateAmlResources (570ACPI_BUFFER *ResourceList,571ACPI_BUFFER *OutputBuffer)572{573ACPI_STATUS Status;574ACPI_SIZE AmlSizeNeeded = 0;575576577ACPI_FUNCTION_TRACE (RsCreateAmlResources);578579580/* Params already validated, no need to re-validate here */581582ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "ResourceList Buffer = %p\n",583ResourceList->Pointer));584585/* Get the buffer size needed for the AML byte stream */586587Status = AcpiRsGetAmlLength (588ResourceList->Pointer, ResourceList->Length, &AmlSizeNeeded);589590ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlSizeNeeded=%X, %s\n",591(UINT32) AmlSizeNeeded, AcpiFormatException (Status)));592if (ACPI_FAILURE (Status))593{594return_ACPI_STATUS (Status);595}596597/* Validate/Allocate/Clear caller buffer */598599Status = AcpiUtInitializeBuffer (OutputBuffer, AmlSizeNeeded);600if (ACPI_FAILURE (Status))601{602return_ACPI_STATUS (Status);603}604605/* Do the conversion */606607Status = AcpiRsConvertResourcesToAml (ResourceList->Pointer,608AmlSizeNeeded, OutputBuffer->Pointer);609if (ACPI_FAILURE (Status))610{611return_ACPI_STATUS (Status);612}613614ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",615OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));616return_ACPI_STATUS (AE_OK);617}618619620